From 96c3d8f756f37f3f22744a5b933e7d29b2c1563e Mon Sep 17 00:00:00 2001 From: Benjamin Kapner Date: Thu, 1 Oct 2026 17:03:56 +0300 Subject: [PATCH 1/3] feat(review): stamp finding ids on inline comments Copy findings[].id onto the diff comment as an HTML marker when it matches f_ plus alphanumeric. This command does not mint ids. A missing or invalid id leaves the comment body unchanged, so reviews stay the same until the agent starts emitting ids. Addresses: AISDLC-122 Signed-off-by: Benjamin Kapner Co-authored-by: Cursor --- internal/cli/postreview.go | 16 ++++++++++- internal/cli/postreview_test.go | 50 ++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/internal/cli/postreview.go b/internal/cli/postreview.go index a2ab6c257f..9f744b324e 100644 --- a/internal/cli/postreview.go +++ b/internal/cli/postreview.go @@ -35,6 +35,11 @@ var hexSHARe = regexp.MustCompile(`^[0-9a-fA-F]{40}$|^[0-9a-fA-F]{64}$`) var reasonRe = regexp.MustCompile(`^[a-zA-Z0-9_-]*$`) var hunkHeaderRe = regexp.MustCompile(`^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@`) +// findingIDRe matches the opaque id the review agent emits (f_ plus +// alphanumeric). Anything else is left off the comment so a crafted id +// cannot inject markup. AISDLC-118's thread fetch looks for this form. +var findingIDRe = regexp.MustCompile(`^f_[a-zA-Z0-9]+$`) + func newPostReviewCmd() *cobra.Command { var ( repo string @@ -200,6 +205,10 @@ type ReviewFinding struct { Description string `json:"description"` Remediation string `json:"remediation,omitempty"` Actionable bool `json:"actionable,omitempty"` + // Id is an opaque stable identifier (f_ plus alphanumeric) assigned + // upstream. This command copies it onto the inline comment; it does + // not mint ids. Empty until the agents repo starts emitting it. + Id string `json:"id,omitempty"` } // reviewActionToEvent maps a ReviewResult action to a GitHub PR review event. @@ -532,9 +541,14 @@ func findingsToReviewComments(findings []ReviewFinding, diffHunks map[string][][ } // formatFindingComment renders a single review finding as a Markdown -// inline comment body. +// inline comment body. A valid id is stamped first so a later fetch of +// resolved review threads can match the comment without parsing prose. +// File-level comments use this same helper, so they carry the stamp too. func formatFindingComment(f ReviewFinding) string { var b strings.Builder + if findingIDRe.MatchString(f.Id) { + fmt.Fprintf(&b, "\n", f.Id) + } fmt.Fprintf(&b, "**[%s]** %s", f.Severity, f.Category) b.WriteString("\n\n") b.WriteString(strings.TrimSpace(f.Description)) diff --git a/internal/cli/postreview_test.go b/internal/cli/postreview_test.go index 46bb6e4eaf..35ad52fe2f 100644 --- a/internal/cli/postreview_test.go +++ b/internal/cli/postreview_test.go @@ -9,6 +9,7 @@ import ( "net/http/httptest" "os" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -69,12 +70,13 @@ func TestParseReviewResult_HeadSHA(t *testing.T) { } func TestParseReviewResult_Findings(t *testing.T) { - input := `{"body":"Review","action":"approve","findings":[{"severity":"low","category":"docs","file":"README.md","line":12,"description":"Missing usage note","remediation":"Add a short note","actionable":true}]}` + input := `{"body":"Review","action":"approve","findings":[{"severity":"low","category":"docs","file":"README.md","line":12,"description":"Missing usage note","remediation":"Add a short note","actionable":true,"id":"f_abc123"}]}` result, err := parseReviewResult(input) require.NoError(t, err) require.Len(t, result.Findings, 1) assert.Equal(t, "low", result.Findings[0].Severity) assert.True(t, result.Findings[0].Actionable) + assert.Equal(t, "f_abc123", result.Findings[0].Id) } func TestReviewActionToEvent(t *testing.T) { @@ -1234,7 +1236,53 @@ func TestFormatFindingComment(t *testing.T) { assert.Contains(t, body, "**[low]** style") assert.Contains(t, body, "Consider renaming.") assert.NotContains(t, body, "Suggested fix:") + assert.NotContains(t, body, "\n")) + assert.Contains(t, body, "**[high]** logic-error") + }) + + t.Run("invalid id is not stamped", func(t *testing.T) { + f := ReviewFinding{ + Id: "", + Severity: "high", + Category: "logic-error", + Description: "Missing nil check.", + } + body := formatFindingComment(f) + assert.NotContains(t, body, "") + assert.NotContains(t, inline[1].Body, "") + assert.Contains(t, fileLevel[0].Body, "Line 50") } func TestResolvePostReviewClient_GitHubDefault(t *testing.T) { From c9c5843b5cbd062a7123bbe4ca852d6cd9de1539 Mon Sep 17 00:00:00 2001 From: Benjamin Kapner Date: Thu, 1 Oct 2026 14:30:25 +0000 Subject: [PATCH 2/3] refactor(review): rename finding Id field to ID Match the Go initialism convention used by every other ID field in the repo. The JSON tag stays "id", so the wire format and the inline comment stamp are unchanged. Addresses: AISDLC-122 Signed-off-by: Benjamin Kapner Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01CSkYsMhZ3avRPTwCF8avxS --- internal/cli/postreview.go | 8 ++++---- internal/cli/postreview_test.go | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/cli/postreview.go b/internal/cli/postreview.go index 9f744b324e..c4eff6e03e 100644 --- a/internal/cli/postreview.go +++ b/internal/cli/postreview.go @@ -205,10 +205,10 @@ type ReviewFinding struct { Description string `json:"description"` Remediation string `json:"remediation,omitempty"` Actionable bool `json:"actionable,omitempty"` - // Id is an opaque stable identifier (f_ plus alphanumeric) assigned + // ID is an opaque stable identifier (f_ plus alphanumeric) assigned // upstream. This command copies it onto the inline comment; it does // not mint ids. Empty until the agents repo starts emitting it. - Id string `json:"id,omitempty"` + ID string `json:"id,omitempty"` } // reviewActionToEvent maps a ReviewResult action to a GitHub PR review event. @@ -546,8 +546,8 @@ func findingsToReviewComments(findings []ReviewFinding, diffHunks map[string][][ // File-level comments use this same helper, so they carry the stamp too. func formatFindingComment(f ReviewFinding) string { var b strings.Builder - if findingIDRe.MatchString(f.Id) { - fmt.Fprintf(&b, "\n", f.Id) + if findingIDRe.MatchString(f.ID) { + fmt.Fprintf(&b, "\n", f.ID) } fmt.Fprintf(&b, "**[%s]** %s", f.Severity, f.Category) b.WriteString("\n\n") diff --git a/internal/cli/postreview_test.go b/internal/cli/postreview_test.go index 35ad52fe2f..e51d43d8bc 100644 --- a/internal/cli/postreview_test.go +++ b/internal/cli/postreview_test.go @@ -76,7 +76,7 @@ func TestParseReviewResult_Findings(t *testing.T) { require.Len(t, result.Findings, 1) assert.Equal(t, "low", result.Findings[0].Severity) assert.True(t, result.Findings[0].Actionable) - assert.Equal(t, "f_abc123", result.Findings[0].Id) + assert.Equal(t, "f_abc123", result.Findings[0].ID) } func TestReviewActionToEvent(t *testing.T) { @@ -1241,7 +1241,7 @@ func TestFormatFindingComment(t *testing.T) { t.Run("valid id", func(t *testing.T) { f := ReviewFinding{ - Id: "f_abc123", + ID: "f_abc123", Severity: "high", Category: "logic-error", Description: "Missing nil check.", @@ -1253,7 +1253,7 @@ func TestFormatFindingComment(t *testing.T) { t.Run("invalid id is not stamped", func(t *testing.T) { f := ReviewFinding{ - Id: "", + ID: "", Severity: "high", Category: "logic-error", Description: "Missing nil check.", @@ -1267,9 +1267,9 @@ func TestFormatFindingComment(t *testing.T) { func TestFindingsToReviewComments_StampsFindingID(t *testing.T) { findings := []ReviewFinding{ - {Id: "f_inline", File: "a.go", Line: 10, Severity: "high", Category: "bug", Description: "In hunk"}, - {Id: "f_file", File: "a.go", Line: 50, Severity: "low", Category: "style", Description: "Outside hunk"}, - {Id: "../x", File: "a.go", Line: 12, Severity: "medium", Category: "bug", Description: "Bad id"}, + {ID: "f_inline", File: "a.go", Line: 10, Severity: "high", Category: "bug", Description: "In hunk"}, + {ID: "f_file", File: "a.go", Line: 50, Severity: "low", Category: "style", Description: "Outside hunk"}, + {ID: "../x", File: "a.go", Line: 12, Severity: "medium", Category: "bug", Description: "Bad id"}, } diffHunks := map[string][][2]int{ "a.go": {{5, 15}}, From 12e851710d813143e797ce2f042d542bb54e144e Mon Sep 17 00:00:00 2001 From: Benjamin Kapner Date: Sun, 4 Oct 2026 10:21:17 +0300 Subject: [PATCH 3/3] fix(review): ignore bad finding ids and scan before stamp Treat a non-string findings[].id as missing so the rest of the review still posts. Scan the id like other finding fields and drop the stamp if it looks like a secret. Point the regex comment at #7907. Addresses: AISDLC-122 Signed-off-by: Benjamin Kapner --- internal/cli/postreview.go | 25 +++++++++-- internal/cli/postreview_test.go | 75 ++++++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/internal/cli/postreview.go b/internal/cli/postreview.go index c4eff6e03e..c7c64a1a5d 100644 --- a/internal/cli/postreview.go +++ b/internal/cli/postreview.go @@ -37,7 +37,7 @@ var hunkHeaderRe = regexp.MustCompile(`^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@`) // findingIDRe matches the opaque id the review agent emits (f_ plus // alphanumeric). Anything else is left off the comment so a crafted id -// cannot inject markup. AISDLC-118's thread fetch looks for this form. +// cannot inject markup. The resolved-thread fetch matches this form (#7907). var findingIDRe = regexp.MustCompile(`^f_[a-zA-Z0-9]+$`) func newPostReviewCmd() *cobra.Command { @@ -208,7 +208,20 @@ type ReviewFinding struct { // ID is an opaque stable identifier (f_ plus alphanumeric) assigned // upstream. This command copies it onto the inline comment; it does // not mint ids. Empty until the agents repo starts emitting it. - ID string `json:"id,omitempty"` + ID findingID `json:"id,omitempty"` +} + +// findingID is an optional review finding id. Non-string JSON values are +// ignored so a bad id does not fail the whole review parse. +type findingID string + +func (id *findingID) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return nil + } + *id = findingID(s) + return nil } // reviewActionToEvent maps a ReviewResult action to a GitHub PR review event. @@ -546,7 +559,7 @@ func findingsToReviewComments(findings []ReviewFinding, diffHunks map[string][][ // File-level comments use this same helper, so they carry the stamp too. func formatFindingComment(f ReviewFinding) string { var b strings.Builder - if findingIDRe.MatchString(f.ID) { + if findingIDRe.MatchString(string(f.ID)) { fmt.Fprintf(&b, "\n", f.ID) } fmt.Fprintf(&b, "**[%s]** %s", f.Severity, f.Category) @@ -837,6 +850,12 @@ func sanitizeReviewResult(r ReviewResult, printer *ui.Printer) ReviewResult { r.Findings[i].Remediation = result.Sanitized } } + if r.Findings[i].ID != "" { + result := pipeline.Scan(string(r.Findings[i].ID)) + if len(result.Findings) > 0 || result.Sanitized != "" { + r.Findings[i].ID = "" + } + } } return r diff --git a/internal/cli/postreview_test.go b/internal/cli/postreview_test.go index e51d43d8bc..0dc0048ebb 100644 --- a/internal/cli/postreview_test.go +++ b/internal/cli/postreview_test.go @@ -76,7 +76,33 @@ func TestParseReviewResult_Findings(t *testing.T) { require.Len(t, result.Findings, 1) assert.Equal(t, "low", result.Findings[0].Severity) assert.True(t, result.Findings[0].Actionable) - assert.Equal(t, "f_abc123", result.Findings[0].ID) + assert.EqualValues(t, "f_abc123", result.Findings[0].ID) +} + +func TestParseReviewResult_NonStringIDIgnored(t *testing.T) { + tests := []struct { + name string + id string + }{ + {"number", "123"}, + {"boolean", "true"}, + {"object", `{"x":1}`}, + {"array", `["f_abc"]`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + input := fmt.Sprintf(`{"body":"Review","action":"approve","head_sha":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","findings":[{"severity":"low","category":"docs","file":"README.md","line":12,"description":"note","id":%s}]}`, tt.id) + result, err := parseReviewResult(input) + require.NoError(t, err) + assert.Equal(t, "Review", result.Body) + assert.Equal(t, "approve", result.Action) + assert.Equal(t, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", result.HeadSHA) + require.Len(t, result.Findings, 1) + assert.Equal(t, "note", result.Findings[0].Description) + assert.Empty(t, result.Findings[0].ID) + assert.NotContains(t, formatFindingComment(result.Findings[0]), "") +} + func TestSanitizeReviewResult_EmptyBody(t *testing.T) { printer := ui.New(io.Discard) r := ReviewResult{