Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/workflow/safe_outputs_tools_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"encoding/json"
"fmt"
"path/filepath"
"strings"

"github.com/github/gh-aw/pkg/sliceutil"
"github.com/github/gh-aw/pkg/stringutil"
Expand Down Expand Up @@ -341,6 +342,22 @@
}
}

// submit_pull_request_review event: when allowed-events restricts the set of review
// decisions, narrow the tool schema's event enum to match so the agent cannot select
// an event that runtime policy will reject. This retains runtime enforcement as
// defense in depth while preventing the doomed call in the first place.
if safeOutputs.SubmitPullRequestReview != nil && len(safeOutputs.SubmitPullRequestReview.AllowedEvents) > 0 {
allowedEvents := safeOutputs.SubmitPullRequestReview.AllowedEvents
injections["submit_pull_request_review"] = map[string]any{
"event": map[string]any{
"type": "string",
"enum": allowedEvents,
Comment on lines +355 to +360
"description": "Review decision. Restricted by allowed-events configuration to: " + strings.Join(allowedEvents, ", ") + ".",
"x-synonyms": []string{"action"},
},
}
}

if safeOutputs.DataEnabled {
dataProperty := map[string]any{"$ref": "#/0/inputSchema/$defs/structured_data"}
if safeOutputs.NormalizedDataSchema != nil {
Expand Down Expand Up @@ -374,7 +391,7 @@
}
result, err := json.Marshal(empty)
if err != nil {
return "", fmt.Errorf("failed to marshal empty tools meta: %w", err)

Check failure on line 394 in pkg/workflow/safe_outputs_tools_generation.go

View workflow job for this annotation

GitHub Actions / lint-go

avoid generic 'failed to ...: %w' wrapping; add specific recovery guidance

Check failure on line 394 in pkg/workflow/safe_outputs_tools_generation.go

View workflow job for this annotation

GitHub Actions / lint-go

error message uses negative language without constructive guidance; include expected/requires/should/example details
}
return string(result), nil
}
Expand Down Expand Up @@ -431,7 +448,7 @@
result, err := json.MarshalIndent(meta, "", " ")
if err != nil {
safeOutputsConfigLog.Printf("Failed to marshal tools meta: %v", err)
return "", fmt.Errorf("failed to marshal tools meta: %w", err)

Check failure on line 451 in pkg/workflow/safe_outputs_tools_generation.go

View workflow job for this annotation

GitHub Actions / lint-go

avoid generic 'failed to ...: %w' wrapping; add specific recovery guidance

Check failure on line 451 in pkg/workflow/safe_outputs_tools_generation.go

View workflow job for this annotation

GitHub Actions / lint-go

error message uses negative language without constructive guidance; include expected/requires/should/example details
}

safeOutputsConfigLog.Printf("Successfully generated tools meta JSON: %d description suffixes, %d repo params, %d dynamic tools",
Expand Down
39 changes: 39 additions & 0 deletions pkg/workflow/safe_outputs_tools_generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,45 @@ func TestComputePropertyInjectionsNilCloseIssues(t *testing.T) {
assert.Empty(t, injections)
}

// TestComputePropertyInjectionsAllowedEventsSubmitPRReview verifies that a configured
// allowed-events list narrows the submit_pull_request_review event enum in the tool schema.
func TestComputePropertyInjectionsAllowedEventsSubmitPRReview(t *testing.T) {
injections := computePropertyInjections(&SafeOutputsConfig{
SubmitPullRequestReview: &SubmitPullRequestReviewConfig{
AllowedEvents: []string{"COMMENT"},
},
})

require.Contains(t, injections, "submit_pull_request_review")
prop, ok := injections["submit_pull_request_review"]["event"].(map[string]any)
require.True(t, ok, "event should be a property map")
assert.Equal(t, []string{"COMMENT"}, prop["enum"])
}

// TestComputePropertyInjectionsAllowedEventsMultipleSubmitPRReview verifies multiple allowed
// events are all present in the narrowed enum.
func TestComputePropertyInjectionsAllowedEventsMultipleSubmitPRReview(t *testing.T) {
injections := computePropertyInjections(&SafeOutputsConfig{
SubmitPullRequestReview: &SubmitPullRequestReviewConfig{
AllowedEvents: []string{"COMMENT", "REQUEST_CHANGES"},
},
})

prop, ok := injections["submit_pull_request_review"]["event"].(map[string]any)
require.True(t, ok, "event should be a property map")
assert.Equal(t, []string{"COMMENT", "REQUEST_CHANGES"}, prop["enum"])
}

// TestComputePropertyInjectionsNoAllowedEventsSubmitPRReview verifies that no injection
// happens when allowed-events is not configured, so the static schema's full enum applies.
func TestComputePropertyInjectionsNoAllowedEventsSubmitPRReview(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/tdd] The close_issue nil-config test (TestComputePropertyInjectionsNilCloseIssues) covers nil config explicitly — adding the same for submit_pull_request_review would mirror the pattern and document the nil contract.

💡 Suggested addition
func TestComputePropertyInjectionsNilSubmitPRReview(t *testing.T) {
    injections := computePropertyInjections(&SafeOutputsConfig{
        SubmitPullRequestReview: nil,
    })
    assert.NotContains(t, injections, "submit_pull_request_review")
}

Low-risk omission — the != nil guard already handles this — but the symmetry with close_issue tests makes the contract explicit.

@copilot please address this.

injections := computePropertyInjections(&SafeOutputsConfig{
SubmitPullRequestReview: &SubmitPullRequestReviewConfig{},
})

assert.NotContains(t, injections, "submit_pull_request_review", "no allowed-events should not inject an event enum")
}

// TestPreprocessStateReasonListSlice verifies that a []any slice is converted to allowed-state-reason.
func TestPreprocessStateReasonListSlice(t *testing.T) {
configData := map[string]any{
Expand Down
Loading