From 4da8d8e0f12b922f77145e79fa2c9eb86bf2a323 Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Tue, 17 Nov 2026 08:35:56 +0200 Subject: [PATCH 1/7] config: add typed trigger predicates --- internal/config/build.go | 56 +++++++++++++++++++ internal/config/fields.go | 4 +- internal/config/trigger_test.go | 55 ++++++++++++++++++ internal/core/configschema.go | 14 +++++ internal/schemagen/build.go | 1 + internal/schemagen/config_parity_test.go | 4 ++ internal/schemagen/model.go | 3 + internal/schemagen/overlay.json | 2 + internal/schemagen/semantics_test.go | 4 +- .../testdata/baseline/config.schema.json | 39 +++++++++++++ skills/verdict/schemas/config.schema.json | 39 +++++++++++++ 11 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 internal/config/trigger_test.go diff --git a/internal/config/build.go b/internal/config/build.go index 3d153ed0b..811148573 100644 --- a/internal/config/build.go +++ b/internal/config/build.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "regexp" "github.com/stainless-code/verdict/internal/core" ) @@ -34,6 +35,9 @@ func buildConfig(root map[string]any) (*core.Config, *core.RefusalError) { if cfg.Requirements, ref = presentNested(root, requirementsKey, rootLocation, buildRequirements); ref != nil { return nil, ref } + if cfg.Trigger, ref = presentNested(root, "trigger", rootLocation, buildTrigger); ref != nil { + return nil, ref + } if cfg.Index, ref = presentNested(root, "index", rootLocation, buildIndex); ref != nil { return nil, ref } @@ -314,6 +318,58 @@ func buildRequirements(raw any, path string) (*core.ConfigRequirements, *core.Re return out, nil } +// triggerStrings reads one non-empty list of single-line predicate values. +func triggerStrings(raw any, path string) ([]string, *core.RefusalError) { + return panelMembers(raw, path, 1) +} + +// buildTrigger validates the closed trigger predicate set. Every list is +// non-empty when present, globs stay portable, and title_matches compiles +// here so a bad expression never reaches evaluation. +func buildTrigger(raw any, path string) (*core.ConfigTrigger, *core.RefusalError) { + obj, ref := asObject(raw, path) + if ref != nil { + return nil, ref + } + out := &core.ConfigTrigger{} + if out.Authors, ref = presentStrings(obj, "authors", path, triggerStrings); ref != nil { + return nil, ref + } + if out.PathsInclude, ref = presentStrings(obj, "paths_include", path, func(v any, at string) ([]string, *core.RefusalError) { + return globArray(v, at, 1) + }); ref != nil { + return nil, ref + } + if out.PathsExclude, ref = presentStrings(obj, "paths_exclude", path, func(v any, at string) ([]string, *core.RefusalError) { + return globArray(v, at, 1) + }); ref != nil { + return nil, ref + } + if out.Labels, ref = presentStrings(obj, "labels", path, triggerStrings); ref != nil { + return nil, ref + } + if out.BaseBranches, ref = presentStrings(obj, "base_branches", path, triggerStrings); ref != nil { + return nil, ref + } + title, ref := presentValue(obj, "title_matches", path, nonEmptyString) + if ref != nil { + return nil, ref + } + if title != nil { + if ref := checkSingleLine(*title, path+".title_matches"); ref != nil { + return nil, ref + } + if _, err := regexp.Compile(*title); err != nil { + return nil, refuse(CodeBadConfig, "%s.title_matches: invalid regexp: %v", path, err) + } + out.TitleMatches = *title + } + if out.Draft, ref = presentValue(obj, "draft", path, asBool); ref != nil { + return nil, ref + } + return out, nil +} + // buildClauseList validates an optional non-empty clause array. Absent // reads nil; present must hold at least one clause. func buildClauseList(raw any, path string) ([]core.BlockClause, *core.RefusalError) { diff --git a/internal/config/fields.go b/internal/config/fields.go index d2b94311b..90458343b 100644 --- a/internal/config/fields.go +++ b/internal/config/fields.go @@ -16,7 +16,7 @@ const requirementsKey = "requirements" var ( topFields = []string{ - "$schema", "schema_version", "defaults", "requirements", "scopes", "profiles", "default_profile", + "$schema", "schema_version", "defaults", "requirements", "trigger", "scopes", "profiles", "default_profile", "index", "severity_weights", "rigor_policy", } defaultsFields = []string{ @@ -26,6 +26,7 @@ var ( kbPolicyFields = []string{"min_judged", "max_fp_rate", "max_chars"} rigorPolicyFields = append([]string{"mode"}, severityEnum...) requirementsFields = []string{"block_clauses", "require_checks", "require_bind"} + triggerFields = []string{"authors", "paths_include", "paths_exclude", "labels", "title_matches", "base_branches", "draft"} clauseFields = []string{"name", "threshold", "categories", "paths"} scopeFields = []string{"name", "include", "exclude", "profile", "defaults", "requirements"} profileFields = []string{"depth", "checks", "max_lenses"} @@ -69,6 +70,7 @@ func checkUnknownFields(root map[string]any) *core.RefusalError { }{ {key: defaultsKey, check: checkDefaultsKeys}, {key: requirementsKey, check: checkRequirementsKeys}, + {key: "trigger", check: checkKeysSection(triggerFields)}, {key: "scopes", check: checkScopeListKeys}, {key: "profiles", check: checkProfilesKeys}, {key: "severity_weights", check: checkKeysSection(severityEnum)}, diff --git a/internal/config/trigger_test.go b/internal/config/trigger_test.go new file mode 100644 index 000000000..0ef59294d --- /dev/null +++ b/internal/config/trigger_test.go @@ -0,0 +1,55 @@ +package config + +import ( + "reflect" + "testing" + + "github.com/stainless-code/verdict/internal/core" +) + +func TestParseTriggerPredicates(t *testing.T) { + cfg, ref := Parse([]byte(`{"schema_version":"1","trigger":{ + "authors":["ana","bo"], + "paths_include":["src/**","go.mod"], + "paths_exclude":["**/generated/**"], + "labels":["security"], + "title_matches":"^fix: (retry|auth)", + "base_branches":["main","release/*"], + "draft":false + }}`)) + if ref != nil { + t.Fatalf("Parse: %v", ref) + } + draft := false + want := &core.ConfigTrigger{ + Authors: []string{"ana", "bo"}, + PathsInclude: []string{"src/**", "go.mod"}, + PathsExclude: []string{"**/generated/**"}, + Labels: []string{"security"}, + TitleMatches: "^fix: (retry|auth)", + BaseBranches: []string{"main", "release/*"}, + Draft: &draft, + } + if !reflect.DeepEqual(cfg.Trigger, want) { + t.Errorf("trigger = %#v, want %#v", cfg.Trigger, want) + } +} + +func TestParseTriggerRefusals(t *testing.T) { + for _, tc := range []struct { + name string + give string + fragment string + }{ + {"unknown key", `{"schema_version":"1","trigger":{"bogus":1}}`, "$.trigger.bogus: unknown field"}, + {"bad regexp", `{"schema_version":"1","trigger":{"title_matches":"("}}`, "$.trigger.title_matches"}, + {"absolute glob", `{"schema_version":"1","trigger":{"paths_include":["/etc/**"]}}`, "absolute"}, + {"wrong draft type", `{"schema_version":"1","trigger":{"draft":"yes"}}`, "$.trigger.draft"}, + {"empty authors", `{"schema_version":"1","trigger":{"authors":[]}}`, "$.trigger.authors"}, + {"null trigger", `{"schema_version":"1","trigger":null}`, "$.trigger"}, + } { + t.Run(tc.name, func(t *testing.T) { + checkParseRefusal(t, tc.give, CodeBadConfig, tc.fragment) + }) + } +} diff --git a/internal/core/configschema.go b/internal/core/configschema.go index 508b03d08..ba0779591 100644 --- a/internal/core/configschema.go +++ b/internal/core/configschema.go @@ -18,6 +18,7 @@ type Config struct { //nolint:govet // field order is the published property ord SchemaVersion string `const:"1" json:"schema_version"` Defaults *ConfigDefaults `json:"defaults,omitempty" nonnull:"true"` Requirements *ConfigRequirements `json:"requirements,omitempty" nonnull:"true"` + Trigger *ConfigTrigger `json:"trigger,omitempty" nonnull:"true"` Scopes []ConfigScope `json:"scopes,omitempty"` Profiles map[string]ConfigProfile `json:"profiles,omitempty"` DefaultProfile string `json:"default_profile,omitempty" nonnull:"true"` @@ -76,6 +77,19 @@ type ConfigRequirements struct { //nolint:govet // field order is the published RequireBind *bool `json:"require_bind,omitempty" nonnull:"true"` } +// ConfigTrigger is the closed set of local trigger predicates. Lists OR +// within a predicate and AND across predicates; an absent section runs every +// PR. All values come from the trusted base configuration, never the PR head. +type ConfigTrigger struct { + Authors []string `json:"authors,omitempty" minItems:"1" nonnull:"true"` + PathsInclude []string `json:"paths_include,omitempty" minItems:"1" nonnull:"true"` + PathsExclude []string `json:"paths_exclude,omitempty" minItems:"1" nonnull:"true"` + Labels []string `json:"labels,omitempty" minItems:"1" nonnull:"true"` + TitleMatches string `json:"title_matches,omitempty" nonnull:"true"` + BaseBranches []string `json:"base_branches,omitempty" minItems:"1" nonnull:"true"` + Draft *bool `json:"draft,omitempty" nonnull:"true"` +} + // BlockClause is one per-source, per-path blocking predicate: an open // finding on one of its paths, in one of its categories, at or above its // threshold rank fails compliance. Absent categories mean every category; diff --git a/internal/schemagen/build.go b/internal/schemagen/build.go index 6abc049bc..714b68b2f 100644 --- a/internal/schemagen/build.go +++ b/internal/schemagen/build.go @@ -186,6 +186,7 @@ func documents() []document { {typ: reflect.TypeFor[ConfigSeverityWeights](), name: "configSeverityWeights", closed: true}, {typ: reflect.TypeFor[ConfigIndex](), name: "configIndex", closed: true}, {typ: reflect.TypeFor[ConfigRequirements](), name: "requirements", closed: true}, + {typ: reflect.TypeFor[ConfigTrigger](), name: "trigger", closed: true}, {typ: reflect.TypeFor[ConfigRigorPolicy](), name: "rigorPolicy", closed: true}, {typ: reflect.TypeFor[BlockClause](), name: "blockClause", closed: true}, {typ: reflect.TypeFor[ConfigScope](), name: "scope", closed: true, constraints: []member{ diff --git a/internal/schemagen/config_parity_test.go b/internal/schemagen/config_parity_test.go index 9f35075c8..6f61ab94b 100644 --- a/internal/schemagen/config_parity_test.go +++ b/internal/schemagen/config_parity_test.go @@ -53,6 +53,7 @@ func TestSchemaParityValid(t *testing.T) { `{"schema_version":"1","defaults":{},"requirements":{},"scopes":[],"profiles":{}}`, `{"schema_version":"1","profiles":{"deep-review":{"depth":"deep","checks":["no-open-above-block"],"max_lenses":4}},"default_profile":"deep-review"}`, `{"schema_version":"1","scopes":[{"name":"req-only","include":["payments/**"],"requirements":{"require_bind":true}}]}`, + `{"schema_version":"1","trigger":{"authors":["ana"],"paths_include":["src/**"],"title_matches":"^fix:","draft":true}}`, } for _, doc := range docs { if violations := parityViolations(t, schema, doc); len(violations) != 0 { @@ -82,6 +83,9 @@ func TestSchemaParityStructural(t *testing.T) { {"profile non-object", `{"schema_version":"1","profiles":{"x":42}}`, config.CodeBadConfig, "profiles"}, {"profile extra key", `{"schema_version":"1","profiles":{"p":{"bogus":1}}}`, config.CodeBadConfig, "bogus"}, {"clause extra key", `{"schema_version":"1","requirements":{"block_clauses":[{"name":"c","threshold":"critical","bogus":1}]}}`, config.CodeBadConfig, "bogus"}, + {"trigger extra key", `{"schema_version":"1","trigger":{"bogus":1}}`, config.CodeBadConfig, "bogus"}, + {"trigger draft null", `{"schema_version":"1","trigger":{"draft":null}}`, config.CodeBadConfig, "draft"}, + {"trigger empty labels", `{"schema_version":"1","trigger":{"labels":[]}}`, config.CodeBadConfig, "labels"}, {"wrong check enum", `{"schema_version":"1","requirements":{"require_checks":["nope"]}}`, config.CodeBadRequirement, "require_checks"}, {"wrong profile check", `{"schema_version":"1","profiles":{"p":{"checks":["nope"]}}}`, config.CodeBadConfig, "checks"}, {"wrong category enum", `{"schema_version":"1","requirements":{"block_clauses":[{"name":"c","threshold":"critical","categories":["ux"]}]}}`, config.CodeBadRequirement, "categories"}, diff --git a/internal/schemagen/model.go b/internal/schemagen/model.go index 9fab0b445..52778170f 100644 --- a/internal/schemagen/model.go +++ b/internal/schemagen/model.go @@ -110,6 +110,9 @@ type ConfigRigorPolicy = core.ConfigRigorPolicy // ConfigRequirements is the configuration's conjunctive requirements. type ConfigRequirements = core.ConfigRequirements +// ConfigTrigger is the closed local trigger predicate set. +type ConfigTrigger = core.ConfigTrigger + // BlockClause is one per-source, per-path blocking predicate. type BlockClause = core.BlockClause diff --git a/internal/schemagen/overlay.json b/internal/schemagen/overlay.json index 946cab64f..a37a61749 100644 --- a/internal/schemagen/overlay.json +++ b/internal/schemagen/overlay.json @@ -17,10 +17,12 @@ "": "The typed repository configuration at .verdict/config.json: versioned shared defaults and review requirements with path scopes, named work profiles, and the default profile. Structural validation comes from this schema; the hand-written parser in internal/config owns the semantic checks the schema cannot express (duplicate keys, credential keys, glob portability, clause-name uniqueness, per-group clause coverage).", "/properties/defaults": "Ordinary review defaults: flag, then environment, then repository, then personal, then built-in. Nested objects merge per field; arrays replace.", "/properties/requirements": "Conjunctive review requirements: every applicable clause must hold for compliance, and required checks accumulate across matching scopes.", + "/properties/trigger": "Closed local trigger predicates read from the trusted base configuration: lists OR within one predicate and AND across predicates; an absent section runs every PR.", "/properties/profiles": "Named work-profile descriptions keyed by profile name; scope profile fields and default_profile resolve against these names.", "/$defs/defaults": "One defaults object: the pinned model roster, the estimate guard threshold, the diff cap, the chunk opt-in, the knowledge-base policy, the advisory effort tier, and the display and payload severity floors.", "/$defs/kbPolicy": "Knowledge-base retirement quorum and ceiling.", "/$defs/requirements": "Blocking clauses with per-source thresholds, prepublication check names, and the approval bind requirement.", + "/$defs/trigger": "One trigger predicate set: authors, changed-path globs, labels, a title regular expression, base branches, and draft state. title_matches compiles when the configuration is parsed.", "/$defs/blockClause": "One blocking predicate: a required name and severity threshold with optional category and path masks; absent masks mean every category or path.", "/$defs/scope": "One path-scoped entry: required include globs with optional excludes plus resolution inputs — at most one of a named profile or inline defaults, with optional inline requirements.", "/$defs/profile": "One work-profile description: provider-neutral depth, the checks the group must evaluate, and the lens budget.", diff --git a/internal/schemagen/semantics_test.go b/internal/schemagen/semantics_test.go index f8f8dff5a..f433be494 100644 --- a/internal/schemagen/semantics_test.go +++ b/internal/schemagen/semantics_test.go @@ -44,7 +44,9 @@ var publishedSemantics = map[string]string{ // index a code index rather than a Go one. // kb_policy.max_chars: additive optional non-null integer >= 1 controlling KB // recall character budget. - "config.schema.json": "e7bca2256eaa3417f675cd3428f9a9bcfc84ccefc7bf23ed68ff2102ab58a716", + // trigger: additive optional top-level predicate object with seven closed + // optional keys; no existing property, required key, or type changed. + "config.schema.json": "cb41b8b05baa14d1236da1d8a3d69a06913dfac560c17a95b71e71b21a65af38", // additive controls.schema.json (new bundle document; no existing schema // property, required key, or type changed). // strict_input: an optional controls.json field (default off) that lets diff --git a/internal/schemagen/testdata/baseline/config.schema.json b/internal/schemagen/testdata/baseline/config.schema.json index e61b7e0a9..abd7c6a56 100644 --- a/internal/schemagen/testdata/baseline/config.schema.json +++ b/internal/schemagen/testdata/baseline/config.schema.json @@ -15,6 +15,10 @@ "description": "Conjunctive review requirements: every applicable clause must hold for compliance, and required checks accumulate across matching scopes.", "$ref": "#/$defs/requirements" }, + "trigger": { + "description": "Closed local trigger predicates read from the trusted base configuration: lists OR within one predicate and AND across predicates; an absent section runs every PR.", + "$ref": "#/$defs/trigger" + }, "scopes": { "type": "array", "items": { "$ref": "#/$defs/scope" } @@ -112,6 +116,41 @@ "required": [], "additionalProperties": false }, + "trigger": { + "description": "One trigger predicate set: authors, changed-path globs, labels, a title regular expression, base branches, and draft state. title_matches compiles when the configuration is parsed.", + "type": "object", + "properties": { + "authors": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "paths_include": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "paths_exclude": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "labels": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "title_matches": { "type": "string" }, + "base_branches": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "draft": { "type": "boolean" } + }, + "required": [], + "additionalProperties": false + }, "rigorPolicy": { "type": "object", "properties": { diff --git a/skills/verdict/schemas/config.schema.json b/skills/verdict/schemas/config.schema.json index e61b7e0a9..abd7c6a56 100644 --- a/skills/verdict/schemas/config.schema.json +++ b/skills/verdict/schemas/config.schema.json @@ -15,6 +15,10 @@ "description": "Conjunctive review requirements: every applicable clause must hold for compliance, and required checks accumulate across matching scopes.", "$ref": "#/$defs/requirements" }, + "trigger": { + "description": "Closed local trigger predicates read from the trusted base configuration: lists OR within one predicate and AND across predicates; an absent section runs every PR.", + "$ref": "#/$defs/trigger" + }, "scopes": { "type": "array", "items": { "$ref": "#/$defs/scope" } @@ -112,6 +116,41 @@ "required": [], "additionalProperties": false }, + "trigger": { + "description": "One trigger predicate set: authors, changed-path globs, labels, a title regular expression, base branches, and draft state. title_matches compiles when the configuration is parsed.", + "type": "object", + "properties": { + "authors": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "paths_include": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "paths_exclude": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "labels": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "title_matches": { "type": "string" }, + "base_branches": { + "type": "array", + "items": { "type": "string" }, + "minItems": 1 + }, + "draft": { "type": "boolean" } + }, + "required": [], + "additionalProperties": false + }, "rigorPolicy": { "type": "object", "properties": { From d4dd9b435a358f7739d47dc2050c9f1200daaf1a Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Tue, 17 Nov 2026 08:46:53 +0200 Subject: [PATCH 2/7] trigger: evaluate trusted-base predicates against PR facts --- internal/core/fetch/consts.go | 1 + internal/core/fetch/exclusion_test.go | 2 +- internal/core/fetch/github.go | 2 +- internal/core/fetch/providers.go | 47 ++++- internal/core/fetch/service_test.go | 6 +- internal/core/fetch/triggerlabels_test.go | 46 +++++ internal/fetch/testdata/api-fallback.golden | 2 +- .../fetch/testdata/author-advisory.golden | 2 +- internal/fetch/testdata/azure-happy.golden | 5 +- internal/fetch/testdata/bad-chunk-knob.golden | 2 +- internal/fetch/testdata/base-sha-json.golden | 2 +- internal/fetch/testdata/base-sha-note.golden | 2 +- .../fetch/testdata/bitbucket-happy.golden | 5 +- .../fetch/testdata/bitbucket-paging.golden | 5 +- .../testdata/budget-over-consented.golden | 2 +- .../fetch/testdata/budget-over-refused.golden | 2 +- .../fetch/testdata/budget-repo-config.golden | 2 +- internal/fetch/testdata/budget-unknown.golden | 2 +- .../fetch/testdata/budget-unreadable.golden | 2 +- internal/fetch/testdata/budget-unset.golden | 2 +- internal/fetch/testdata/budget-within.golden | 2 +- .../testdata/cap-override-beats-chunk.golden | 2 +- internal/fetch/testdata/cap-override.golden | 2 +- .../testdata/controls-base-absent.golden | 2 +- .../testdata/controls-contents-error.golden | 2 +- internal/fetch/testdata/dump-json.golden | 2 +- internal/fetch/testdata/dump-text.golden | 2 +- internal/fetch/testdata/empty-diff.golden | 2 +- internal/fetch/testdata/file-paging.golden | 2 +- internal/fetch/testdata/forgejo-happy.golden | 5 +- .../fetch/testdata/gitcafe-base-note.golden | 5 +- internal/fetch/testdata/gitcafe-happy.golden | 5 +- .../fetch/testdata/gitlab-base-note.golden | 5 +- internal/fetch/testdata/gitlab-base.golden | 5 +- internal/fetch/testdata/gitlab-happy.golden | 5 +- internal/fetch/testdata/gitlab-text.golden | 5 +- internal/fetch/testdata/happy-json.golden | 2 +- internal/fetch/testdata/happy-text.golden | 2 +- .../testdata/over-cap-absent-text.golden | 2 +- .../fetch/testdata/over-cap-chunk-text.golden | 2 +- internal/fetch/testdata/over-cap-chunk.golden | 2 +- internal/fetch/testdata/over-cap-knob.golden | 2 +- .../fetch/testdata/over-cap-refusal.golden | 2 +- .../fetch/testdata/ratelimit-retry.golden | 4 +- .../fetch/testdata/sensitive-kb-head.golden | 2 +- internal/fetch/testdata/sensitive-kb.golden | 2 +- internal/fetch/testdata/since-alias.golden | 2 +- .../testdata/single-file-degenerate.golden | 2 +- .../fetch/testdata/threads-degraded.golden | 2 +- internal/fetch/testdata/undecodable-pr.golden | 2 +- .../fetch/testdata/upstream-too-large.golden | 2 +- internal/fetch/testdata/view-failed.golden | 2 +- internal/trigger/trigger.go | 187 ++++++++++++++++++ internal/trigger/trigger_test.go | 108 ++++++++++ 54 files changed, 451 insertions(+), 72 deletions(-) create mode 100644 internal/core/fetch/triggerlabels_test.go create mode 100644 internal/trigger/trigger.go create mode 100644 internal/trigger/trigger_test.go diff --git a/internal/core/fetch/consts.go b/internal/core/fetch/consts.go index 5b51c2288..7278b4f39 100644 --- a/internal/core/fetch/consts.go +++ b/internal/core/fetch/consts.go @@ -27,6 +27,7 @@ const ( keyItems = "items" keyJSON = "json" keyKind = "kind" + keyLabels = "labels" keyLogin = "login" keyNumber = "number" keyPath = "path" diff --git a/internal/core/fetch/exclusion_test.go b/internal/core/fetch/exclusion_test.go index 201e53344..05e297000 100644 --- a/internal/core/fetch/exclusion_test.go +++ b/internal/core/fetch/exclusion_test.go @@ -215,7 +215,7 @@ func testGitHubOmittedExcludes(t *testing.T) { t.Chdir(t.TempDir()) cmdr := fetchHappyCommander(t) view := "gh\x00pr\x00view\x007\x00--repo\x00o/r\x00--json\x00" + - "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits," + + "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits," + "files,additions,deletions,isDraft,reviewDecision,state" cmdr.out[view] = scriptResult{stdout: `{"number":7,"title":"T","body":"B","baseRefName":"main","headRefName":"feat",` + `"headRefOid":"` + fetchTestHead + `","baseRefOid":"` + fetchTestBase + `",` + diff --git a/internal/core/fetch/github.go b/internal/core/fetch/github.go index e37a8797a..7e5483559 100644 --- a/internal/core/fetch/github.go +++ b/internal/core/fetch/github.go @@ -72,7 +72,7 @@ func githubFetch(ctx context.Context, ctx2 *fetchRunCtx, r *session, dir, repo, func githubView(ctx context.Context, r *session, dir, repo, pr string) *core.RefusalError { fmt.Fprintf(r.Human, "» gh pr view %s#%s\n", repo, pr) viewRes, err := r.ghFetch(ctx, ghCmdPR, "view", pr, "--repo", repo, - flagJSON, "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,"+ + flagJSON, "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,"+ "files,additions,deletions,isDraft,reviewDecision,state") fmt.Fprint(r.Human, viewRes.Stderr) if err != nil { diff --git a/internal/core/fetch/providers.go b/internal/core/fetch/providers.go index 34d52c5fb..a3b657c93 100644 --- a/internal/core/fetch/providers.go +++ b/internal/core/fetch/providers.go @@ -1887,19 +1887,24 @@ type prSpec struct { BaseSHA any HeadSHA any Author any + Labels []any URL any State string Draft any Files []any } -// buildPR builds the single PR.json record: a nil draft reads false and a -// nil file slice reads [] so every provider emits both keys. +// buildPR builds the single PR.json record: a nil draft reads false, while +// nil label and file slices read [] so every provider emits the keys. func buildPR(spec *prSpec) *jq.Obj { files := spec.Files if files == nil { files = []any{} } + labels := spec.Labels + if labels == nil { + labels = []any{} + } adds, dels := sumStats(files) draft := spec.Draft if draft == nil { @@ -1916,6 +1921,7 @@ func buildPR(spec *prSpec) *jq.Obj { keyBaseSHA, spec.BaseSHA, keyHeadSHA, spec.HeadSHA, keyAuthor, spec.Author, + keyLabels, labels, keyURL, spec.URL, keyState, spec.State, keyDraft, draft, @@ -1925,6 +1931,24 @@ func buildPR(spec *prSpec) *jq.Obj { ) } +// prLabels normalizes host label rows: strings and objects carrying a name +// both read as label names, so the trigger predicate sees one shape. +func prLabels(raw any) []any { + var out []any + for _, entry := range jq.Elements(raw) { + if name, ok := entry.(string); ok && name != "" { + out = append(out, name) + continue + } + if m, ok := jq.AsMap(entry); ok { + if name := normStr(m, "name"); name != "" { + out = append(out, name) + } + } + } + return out +} + func prFileRow(path string, status, additions, deletions any) *jq.Obj { return jq.NewObj(keyPath, path, keyStatus, status, keyAdditions, additions, keyDeletions, deletions) @@ -1987,7 +2011,8 @@ func normalizeGitLab(metaRaw, changesRaw string, ns map[string]map[string]int64) Title: normStr(meta, keyTitle), Body: normStr(meta, keyDescription), BaseRef: normStr(meta, "target_branch"), HeadRef: normStr(meta, "source_branch"), BaseSHA: normStr(diffRefs, keyBaseSHA, "start_sha"), HeadSHA: headSHA, - Author: normStr(nestedMap(meta, keyAuthor), "username", "name"), URL: normStr(meta, "web_url"), + Author: normStr(nestedMap(meta, keyAuthor), "username", "name"), + Labels: prLabels(meta[keyLabels]), URL: normStr(meta, "web_url"), State: providerState(providerGitLab, normStr(meta, keyState)), Draft: meta[keyDraft], Files: gitlabFileRows(changesRaw, ns), }), nil @@ -2093,7 +2118,7 @@ func normalizeGitCafe(metaRaw, itemsRaw string) (*jq.Obj, error) { Title: normStr(res, keyTitle), Body: normStr(res, keyDescription), BaseRef: normStr(res, "targetBranch"), HeadRef: normStr(res, "sourceBranch"), BaseSHA: firstPresent(res["observedBaseOid"], res["baseOid"]), HeadSHA: firstPresent(res["sourceOid"], res["headOid"]), - Author: cafeAuthor(res), URL: cafeURL(meta, res), + Author: cafeAuthor(res), Labels: prLabels(res[keyLabels]), URL: cafeURL(meta, res), State: providerState(providerGitCafe, normStr(res, keyState)), Draft: res[keyDraft], Files: fileRows(cafeItemsList(itemsRaw), func(m map[string]any) *jq.Obj { return entryFileRow(m, keyPath) }), }), nil @@ -2165,7 +2190,8 @@ func normalizeForgejo(metaRaw, filesRaw string) (*jq.Obj, error) { Title: normStr(meta, keyTitle), Body: normStr(meta, keyBody), BaseRef: normStr(base, "label"), HeadRef: normStr(head, "label"), BaseSHA: normStr(base, "sha"), HeadSHA: normStr(head, "sha"), - Author: normStr(nestedMap(meta, "user"), keyLogin), URL: normStr(meta, "html_url"), + Author: normStr(nestedMap(meta, "user"), keyLogin), + Labels: prLabels(meta[keyLabels]), URL: normStr(meta, "html_url"), State: forgejoState(meta), Draft: forgejoDraft(meta), Files: fileRows(jsonArrayOrEmpty(filesRaw), func(m map[string]any) *jq.Obj { return entryFileRow(m, keyFilename) }), }), nil @@ -2193,10 +2219,10 @@ func normalizeBitbucket(metaRaw string, ds []any) (*jq.Obj, error) { BaseSHA: normStr(nestedMap(dest, "commit"), "hash"), HeadSHA: normStr(nestedMap(src, "commit"), "hash"), // The key chain stops at the first string (even ""), so nickname is // authoritative and display_name never wins. - Author: normStr(nestedMap(meta, keyAuthor), "nickname"), - URL: normStr(nestedMap(nestedMap(meta, "links"), "html"), "href"), - State: providerState(providerBitbucket, normStr(meta, keyState)), - Draft: meta[keyDraft], Files: fileRows(ds, bitbucketFileRow), + Author: normStr(nestedMap(meta, keyAuthor), "nickname"), Labels: prLabels(meta[keyLabels]), + URL: normStr(nestedMap(nestedMap(meta, "links"), "html"), "href"), + State: providerState(providerBitbucket, normStr(meta, keyState)), + Draft: meta[keyDraft], Files: fileRows(ds, bitbucketFileRow), }), nil } @@ -2224,7 +2250,8 @@ func normalizeAzure(metaRaw, changesRaw string, ns map[string]map[string]int64) HeadSHA: normStr(nestedMap(meta, "lastMergeSourceCommit"), keyCommitID), // The key chain stops at the first string (even ""), so uniqueName is // authoritative and displayName never wins. - Author: normStr(nestedMap(meta, "createdBy"), "uniqueName"), URL: normStr(meta, keyURL), + Author: normStr(nestedMap(meta, "createdBy"), "uniqueName"), + Labels: prLabels(meta[keyLabels]), URL: normStr(meta, keyURL), State: providerState(providerAzure, normStr(meta, keyStatus)), Draft: meta["isDraft"], Files: files, }), nil diff --git a/internal/core/fetch/service_test.go b/internal/core/fetch/service_test.go index 0ce1533a2..a8243625a 100644 --- a/internal/core/fetch/service_test.go +++ b/internal/core/fetch/service_test.go @@ -213,7 +213,7 @@ func fetchTestDiff() string { func fetchHappyCommander(t *testing.T) *scriptCommander { t.Helper() view := "pr\x00view\x007\x00--repo\x00o/r\x00--json\x00" + - "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits," + + "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits," + "files,additions,deletions,isDraft,reviewDecision,state" stat := "pr\x00view\x007\x00--repo\x00o/r\x00--json\x00files\x00--jq\x00" + ".files[] | \"\\(.path) (+\\(.additions)/-\\(.deletions))\"" @@ -266,7 +266,7 @@ func fetchHappyArgv() [][]string { return [][]string{ { "gh", "pr", "view", "7", "--repo", "o/r", "--json", - "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits," + + "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits," + "files,additions,deletions,isDraft,reviewDecision,state", }, {"gh", "pr", "diff", "7", "--repo", "o/r"}, @@ -428,7 +428,7 @@ func TestRunDiffTooLarge(t *testing.T) { big := "diff --git a/a.txt b/a.txt\n--- a/a.txt\n+++ b/a.txt\n@@ -1 +1 @@\n" + strings.Repeat(" context line padding the diff past the cap\n", 20000) view := "pr\x00view\x007\x00--repo\x00o/r\x00--json\x00" + - "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits," + + "number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits," + "files,additions,deletions,isDraft,reviewDecision,state" pr := strings.Replace(fetchTestPR(), `"files":[{"path":"a.txt","additions":1,"deletions":1}]`, `"files":[{"path":"a.txt","additions":1,"deletions":1},{"path":"b.txt","additions":1,"deletions":1}]`, 1) diff --git a/internal/core/fetch/triggerlabels_test.go b/internal/core/fetch/triggerlabels_test.go new file mode 100644 index 000000000..3d527d37c --- /dev/null +++ b/internal/core/fetch/triggerlabels_test.go @@ -0,0 +1,46 @@ +package fetchcore + +import ( + "testing" + + "github.com/stainless-code/verdict/internal/jq" +) + +func TestNormalizedPRIncludesLabels(t *testing.T) { + pr, err := normalizeGitLab(`{"iid":7,"title":"fix","labels":["security","deps"]}`, `[]`, nil) + if err != nil { + t.Fatalf("normalizeGitLab: %v", err) + } + labels, ok := pr.Get("labels") + if !ok { + t.Fatal("normalized PR has no labels key") + } + var got []string + for _, value := range jq.Elements(labels) { + got = append(got, jq.StrOf(value)) + } + want := []string{"security", "deps"} + if len(got) != len(want) { + t.Fatalf("labels = %v, want %v", got, want) + } + for i := range want { + if got[i] != want[i] { + t.Errorf("labels[%d] = %q, want %q", i, got[i], want[i]) + } + } +} + +func TestForgejoPRIncludesObjectLabels(t *testing.T) { + pr, err := normalizeForgejo(`{"number":7,"title":"fix","labels":[{"name":"security"}]}`, `[]`) + if err != nil { + t.Fatalf("normalizeForgejo: %v", err) + } + labels, _ := pr.Get("labels") + var got []string + for _, value := range jq.Elements(labels) { + got = append(got, jq.StrOf(value)) + } + if len(got) != 1 || got[0] != "security" { + t.Errorf("labels = %v, want [security]", got) + } +} diff --git a/internal/fetch/testdata/api-fallback.golden b/internal/fetch/testdata/api-fallback.golden index 020bcb1a6..05788e2d6 100644 --- a/internal/fetch/testdata/api-fallback.golden +++ b/internal/fetch/testdata/api-fallback.golden @@ -22,7 +22,7 @@ gh pr diff failed, trying API diff evals-stub-gh: network disabled (api repos/acme/app/pulls/4701 -H Accept: application/vnd.github.diff) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: api repos/acme/app/pulls/4701 -H Accept: application/vnd.github.diff --- file C/.verdict/.gitignore --- diff --git a/internal/fetch/testdata/author-advisory.golden b/internal/fetch/testdata/author-advisory.golden index 2fea7f80c..4a9fb19ff 100644 --- a/internal/fetch/testdata/author-advisory.golden +++ b/internal/fetch/testdata/author-advisory.golden @@ -76,7 +76,7 @@ done: .verdict/state/4701/PR.json .verdict/state/4701/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4701/PR.json + .verdict/state/4701/full.diff (+ .verdict/state/4701/roles.txt) to any agent; ask it to write .verdict/state/4701/review.json; prior threads are in .verdict/state/4701/threads.json (run verdict threads to refresh); author looks like claude (high); a reviewer from another model family is suggested --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: pr view 4701 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4701/reviews --paginate diff --git a/internal/fetch/testdata/azure-happy.golden b/internal/fetch/testdata/azure-happy.golden index be30ee959..242e55283 100644 --- a/internal/fetch/testdata/azure-happy.golden +++ b/internal/fetch/testdata/azure-happy.golden @@ -109,6 +109,7 @@ a.txt "base_sha": "c4d827b6a20aa8f3200cb15d134772e8471130da", "head_sha": "e516722ce9ce0870538023ae1e062b7f1a11e504", "author": "mate", + "labels": [], "url": "https://example.invalid/16", "state": "open", "draft": false, @@ -395,8 +396,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "899cbc451dddff6b7bceaea0a6169bcf648df2fa96277599a4bf872fadcb67ec", - "bytes": 551 + "sha256": "619e978f74eb11b7648dbe247f952247837c58d6f5d6ba3c7a405d3e6ceb9294", + "bytes": 567 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/bad-chunk-knob.golden b/internal/fetch/testdata/bad-chunk-knob.golden index 7ba5a75a6..e41d327d8 100644 --- a/internal/fetch/testdata/bad-chunk-knob.golden +++ b/internal/fetch/testdata/bad-chunk-knob.golden @@ -19,7 +19,7 @@ exit: 1 » gh pr diff -> .verdict/state/4703/full.diff --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4703 --repo acme/app --- file C/.verdict/.gitignore --- /state/ diff --git a/internal/fetch/testdata/base-sha-json.golden b/internal/fetch/testdata/base-sha-json.golden index cb41a329a..55c91458c 100644 --- a/internal/fetch/testdata/base-sha-json.golden +++ b/internal/fetch/testdata/base-sha-json.golden @@ -75,7 +75,7 @@ done: .verdict/state/4701/PR.json .verdict/state/4701/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4701/PR.json + .verdict/state/4701/full.diff (+ .verdict/state/4701/roles.txt) to any agent; ask it to write .verdict/state/4701/review.json; prior threads are in .verdict/state/4701/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: pr view 4701 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4701/reviews --paginate diff --git a/internal/fetch/testdata/base-sha-note.golden b/internal/fetch/testdata/base-sha-note.golden index f73dcecfc..c054087e2 100644 --- a/internal/fetch/testdata/base-sha-note.golden +++ b/internal/fetch/testdata/base-sha-note.golden @@ -76,7 +76,7 @@ done: .verdict/state/4701/PR.json .verdict/state/4701/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4701/PR.json + .verdict/state/4701/full.diff (+ .verdict/state/4701/roles.txt) to any agent; ask it to write .verdict/state/4701/review.json; prior threads are in .verdict/state/4701/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: pr view 4701 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4701/reviews --paginate diff --git a/internal/fetch/testdata/bitbucket-happy.golden b/internal/fetch/testdata/bitbucket-happy.golden index abeccc39d..78e11e10f 100644 --- a/internal/fetch/testdata/bitbucket-happy.golden +++ b/internal/fetch/testdata/bitbucket-happy.golden @@ -110,6 +110,7 @@ a.txt "base_sha": "488310afbc29fb48fbd84cf16c89d3726158a444", "head_sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "author": "mate", + "labels": [], "url": "https://example.invalid/11", "state": "open", "draft": false, @@ -392,8 +393,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "2564ed8657841142033b4e52874e8687cb55cbd052c404b81bf48d8f4cb61199", - "bytes": 558 + "sha256": "ee31aa4768c6a8971f20ced2f946569009707f940149d68c37338da71e6a65eb", + "bytes": 574 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/bitbucket-paging.golden b/internal/fetch/testdata/bitbucket-paging.golden index 9942354f3..adc6acb84 100644 --- a/internal/fetch/testdata/bitbucket-paging.golden +++ b/internal/fetch/testdata/bitbucket-paging.golden @@ -112,6 +112,7 @@ b.txt "base_sha": "488310afbc29fb48fbd84cf16c89d3726158a444", "head_sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "author": "mate", + "labels": [], "url": "https://example.invalid/17", "state": "open", "draft": false, @@ -386,8 +387,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "28ce1e54108103f858db3c24cd9691a68347a8f1b01e671fd2f3498dbc956f5d", - "bytes": 668 + "sha256": "aea63e20439ce8af3d9d1b333a667729e2cfed564f9d8471495436044cc40b92", + "bytes": 684 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/budget-over-consented.golden b/internal/fetch/testdata/budget-over-consented.golden index b53bb7d6f..745e4fd1a 100644 --- a/internal/fetch/testdata/budget-over-consented.golden +++ b/internal/fetch/testdata/budget-over-consented.golden @@ -80,7 +80,7 @@ done: .verdict/state/4705/PR.json .verdict/state/4705/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4705/PR.json + .verdict/state/4705/full.diff (+ .verdict/state/4705/roles.txt) to any agent; ask it to write .verdict/state/4705/review.json; prior threads are in .verdict/state/4705/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4705 --repo acme/app CALL: pr view 4705 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4705/reviews --paginate diff --git a/internal/fetch/testdata/budget-over-refused.golden b/internal/fetch/testdata/budget-over-refused.golden index 9c2a61d84..fe03d06e2 100644 --- a/internal/fetch/testdata/budget-over-refused.golden +++ b/internal/fetch/testdata/budget-over-refused.golden @@ -20,7 +20,7 @@ exit: 1 » estimate: tier standard ~$3/run from 2 comparable run(s) — OVER budget $0.50 (REVIEW_BUDGET_USD) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4705 --repo acme/app --- file C/.verdict/.gitignore --- /state/ diff --git a/internal/fetch/testdata/budget-repo-config.golden b/internal/fetch/testdata/budget-repo-config.golden index d2fd5fc7c..f726e45b3 100644 --- a/internal/fetch/testdata/budget-repo-config.golden +++ b/internal/fetch/testdata/budget-repo-config.golden @@ -20,7 +20,7 @@ exit: 1 » estimate: tier standard ~$3/run from 2 comparable run(s) — OVER budget $0.5 (.verdict/config.json) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4705 --repo acme/app CALL: api repos/acme/app/contents/.verdict/ignore?ref=cccccccccccccccccccccccccccccccccccccccc CALL: api repos/acme/app/contents/.gitattributes?ref=cccccccccccccccccccccccccccccccccccccccc diff --git a/internal/fetch/testdata/budget-unknown.golden b/internal/fetch/testdata/budget-unknown.golden index af50ba282..2c8edb523 100644 --- a/internal/fetch/testdata/budget-unknown.golden +++ b/internal/fetch/testdata/budget-unknown.golden @@ -79,7 +79,7 @@ done: .verdict/state/4705/PR.json .verdict/state/4705/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4705/PR.json + .verdict/state/4705/full.diff (+ .verdict/state/4705/roles.txt) to any agent; ask it to write .verdict/state/4705/review.json; prior threads are in .verdict/state/4705/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4705 --repo acme/app CALL: pr view 4705 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4705/reviews --paginate diff --git a/internal/fetch/testdata/budget-unreadable.golden b/internal/fetch/testdata/budget-unreadable.golden index 0bb564aec..9394964d6 100644 --- a/internal/fetch/testdata/budget-unreadable.golden +++ b/internal/fetch/testdata/budget-unreadable.golden @@ -82,7 +82,7 @@ done: .verdict/state/4705/PR.json .verdict/state/4705/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4705/PR.json + .verdict/state/4705/full.diff (+ .verdict/state/4705/roles.txt) to any agent; ask it to write .verdict/state/4705/review.json; prior threads are in .verdict/state/4705/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4705 --repo acme/app CALL: pr view 4705 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4705/reviews --paginate diff --git a/internal/fetch/testdata/budget-unset.golden b/internal/fetch/testdata/budget-unset.golden index 6d0a615dc..b35108d04 100644 --- a/internal/fetch/testdata/budget-unset.golden +++ b/internal/fetch/testdata/budget-unset.golden @@ -74,7 +74,7 @@ done: .verdict/state/4705/PR.json .verdict/state/4705/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4705/PR.json + .verdict/state/4705/full.diff (+ .verdict/state/4705/roles.txt) to any agent; ask it to write .verdict/state/4705/review.json; prior threads are in .verdict/state/4705/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4705 --repo acme/app CALL: pr view 4705 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4705/reviews --paginate diff --git a/internal/fetch/testdata/budget-within.golden b/internal/fetch/testdata/budget-within.golden index 7ca143f9b..db47ce4a3 100644 --- a/internal/fetch/testdata/budget-within.golden +++ b/internal/fetch/testdata/budget-within.golden @@ -79,7 +79,7 @@ done: .verdict/state/4705/PR.json .verdict/state/4705/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4705/PR.json + .verdict/state/4705/full.diff (+ .verdict/state/4705/roles.txt) to any agent; ask it to write .verdict/state/4705/review.json; prior threads are in .verdict/state/4705/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4705 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4705 --repo acme/app CALL: pr view 4705 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4705/reviews --paginate diff --git a/internal/fetch/testdata/cap-override-beats-chunk.golden b/internal/fetch/testdata/cap-override-beats-chunk.golden index 7efe70aed..8ddb6b826 100644 --- a/internal/fetch/testdata/cap-override-beats-chunk.golden +++ b/internal/fetch/testdata/cap-override-beats-chunk.golden @@ -80,7 +80,7 @@ done: .verdict/state/4703/PR.json .verdict/state/4703/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4703/PR.json + .verdict/state/4703/full.diff (+ .verdict/state/4703/roles.txt) to any agent; ask it to write .verdict/state/4703/review.json --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4703 --repo acme/app CALL: pr view 4703 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4703/reviews --paginate diff --git a/internal/fetch/testdata/cap-override.golden b/internal/fetch/testdata/cap-override.golden index 87b10af52..50b4b8a11 100644 --- a/internal/fetch/testdata/cap-override.golden +++ b/internal/fetch/testdata/cap-override.golden @@ -80,7 +80,7 @@ done: .verdict/state/4703/PR.json .verdict/state/4703/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4703/PR.json + .verdict/state/4703/full.diff (+ .verdict/state/4703/roles.txt) to any agent; ask it to write .verdict/state/4703/review.json --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4703 --repo acme/app CALL: pr view 4703 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4703/reviews --paginate diff --git a/internal/fetch/testdata/controls-base-absent.golden b/internal/fetch/testdata/controls-base-absent.golden index 9c7d28718..c2b3013fd 100644 --- a/internal/fetch/testdata/controls-base-absent.golden +++ b/internal/fetch/testdata/controls-base-absent.golden @@ -76,7 +76,7 @@ done: .verdict/state/4701/PR.json .verdict/state/4701/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4701/PR.json + .verdict/state/4701/full.diff (+ .verdict/state/4701/roles.txt) to any agent; ask it to write .verdict/state/4701/review.json; prior threads are in .verdict/state/4701/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: api repos/acme/app/contents/.verdict/ignore?ref=cccccccccccccccccccccccccccccccccccccccc CALL: api repos/acme/app/contents/.gitattributes?ref=cccccccccccccccccccccccccccccccccccccccc diff --git a/internal/fetch/testdata/controls-contents-error.golden b/internal/fetch/testdata/controls-contents-error.golden index 62b2534ea..3835d3b8c 100644 --- a/internal/fetch/testdata/controls-contents-error.golden +++ b/internal/fetch/testdata/controls-contents-error.golden @@ -19,7 +19,7 @@ exit: 1 » gh pr diff -> .verdict/state/4701/full.diff --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: api repos/acme/app/contents/.verdict/ignore?ref=cccccccccccccccccccccccccccccccccccccccc --- file C/.verdict/.gitignore --- diff --git a/internal/fetch/testdata/dump-json.golden b/internal/fetch/testdata/dump-json.golden index fb2578af0..b66f9c48d 100644 --- a/internal/fetch/testdata/dump-json.golden +++ b/internal/fetch/testdata/dump-json.golden @@ -75,7 +75,7 @@ done: .verdict/state/4702/PR.json .verdict/state/4702/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4702/PR.json + .verdict/state/4702/full.diff (+ .verdict/state/4702/roles.txt) to any agent; ask it to write .verdict/state/4702/review.json; prior threads are in .verdict/state/4702/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4702 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4702 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4702 --repo acme/app CALL: pr view 4702 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4702/reviews --paginate diff --git a/internal/fetch/testdata/dump-text.golden b/internal/fetch/testdata/dump-text.golden index 2c971a58a..af78b2649 100644 --- a/internal/fetch/testdata/dump-text.golden +++ b/internal/fetch/testdata/dump-text.golden @@ -135,7 +135,7 @@ done: .verdict/state/4702/PR.json .verdict/state/4702/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4702/PR.json + .verdict/state/4702/full.diff (+ .verdict/state/4702/roles.txt) to any agent; ask it to write .verdict/state/4702/review.json; prior threads are in .verdict/state/4702/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4702 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4702 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4702 --repo acme/app CALL: pr view 4702 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4702/reviews --paginate diff --git a/internal/fetch/testdata/empty-diff.golden b/internal/fetch/testdata/empty-diff.golden index 48687793c..be1e55350 100644 --- a/internal/fetch/testdata/empty-diff.golden +++ b/internal/fetch/testdata/empty-diff.golden @@ -73,7 +73,7 @@ done: .verdict/state/4701/PR.json .verdict/state/4701/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4701/PR.json + .verdict/state/4701/full.diff (+ .verdict/state/4701/roles.txt) to any agent; ask it to write .verdict/state/4701/review.json; prior threads are in .verdict/state/4701/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: pr view 4701 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4701/reviews --paginate diff --git a/internal/fetch/testdata/file-paging.golden b/internal/fetch/testdata/file-paging.golden index 0a69c3239..74ca467a4 100644 --- a/internal/fetch/testdata/file-paging.golden +++ b/internal/fetch/testdata/file-paging.golden @@ -77,7 +77,7 @@ done: .verdict/state/4712/PR.json .verdict/state/4712/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4712/PR.json + .verdict/state/4712/full.diff (+ .verdict/state/4712/roles.txt) to any agent; ask it to write .verdict/state/4712/review.json --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4712 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4712 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4712 --repo acme/app CALL: api repos/acme/app/pulls/4712/files --paginate CALL: pr view 4712 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" diff --git a/internal/fetch/testdata/forgejo-happy.golden b/internal/fetch/testdata/forgejo-happy.golden index 0ad121b35..3ab7fa801 100644 --- a/internal/fetch/testdata/forgejo-happy.golden +++ b/internal/fetch/testdata/forgejo-happy.golden @@ -105,6 +105,7 @@ a.txt "base_sha": "488310afbc29fb48fbd84cf16c89d3726158a444", "head_sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "author": "mate", + "labels": [], "url": "https://example.invalid/9", "state": "open", "draft": false, @@ -390,8 +391,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "a9ce0d415da67e17a0487b997cb1ef75a5402a2b0403380014fe0be3eab337c8", - "bytes": 552 + "sha256": "e942cf716c8a590073025aef9ee2e1a8611e85a2e227d75f8d14cfaadd6f1974", + "bytes": 568 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/gitcafe-base-note.golden b/internal/fetch/testdata/gitcafe-base-note.golden index beded27de..d96a53a20 100644 --- a/internal/fetch/testdata/gitcafe-base-note.golden +++ b/internal/fetch/testdata/gitcafe-base-note.golden @@ -110,6 +110,7 @@ a.txt "base_sha": "488310afbc29fb48fbd84cf16c89d3726158a444", "head_sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "author": "mate", + "labels": [], "url": "https://example.invalid/7", "state": "open", "draft": false, @@ -375,8 +376,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "385e0123b84fd63f99d03c6d65d9f6b6488bd37880e76eb5a2d461bbd91a4c1c", - "bytes": 553 + "sha256": "d78dc72bfa80982596fdf7b89f091d21e4a21d473c70467959e86905bf73e1af", + "bytes": 569 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/gitcafe-happy.golden b/internal/fetch/testdata/gitcafe-happy.golden index c3b5be9d8..8a030b708 100644 --- a/internal/fetch/testdata/gitcafe-happy.golden +++ b/internal/fetch/testdata/gitcafe-happy.golden @@ -109,6 +109,7 @@ a.txt "base_sha": "488310afbc29fb48fbd84cf16c89d3726158a444", "head_sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "author": "mate", + "labels": [], "url": "https://example.invalid/7", "state": "open", "draft": false, @@ -372,8 +373,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "385e0123b84fd63f99d03c6d65d9f6b6488bd37880e76eb5a2d461bbd91a4c1c", - "bytes": 553 + "sha256": "d78dc72bfa80982596fdf7b89f091d21e4a21d473c70467959e86905bf73e1af", + "bytes": 569 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/gitlab-base-note.golden b/internal/fetch/testdata/gitlab-base-note.golden index d55288253..270ef6c09 100644 --- a/internal/fetch/testdata/gitlab-base-note.golden +++ b/internal/fetch/testdata/gitlab-base-note.golden @@ -113,6 +113,7 @@ a.txt "base_sha": "488310afbc29fb48fbd84cf16c89d3726158a444", "head_sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "author": "mate", + "labels": [], "url": "https://example.invalid/5", "state": "open", "draft": false, @@ -375,8 +376,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "52c55ed450b01318747dcdf6af8a01cf27470f38cfeda51969e0df6866ef748a", - "bytes": 551 + "sha256": "f6c73c50b154e140b6389e2e7c8b354665c5fb72a80495d92f0cd5a24dd7fb6c", + "bytes": 567 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/gitlab-base.golden b/internal/fetch/testdata/gitlab-base.golden index c188fd97a..eb8aaaa95 100644 --- a/internal/fetch/testdata/gitlab-base.golden +++ b/internal/fetch/testdata/gitlab-base.golden @@ -112,6 +112,7 @@ a.txt "base_sha": "488310afbc29fb48fbd84cf16c89d3726158a444", "head_sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "author": "mate", + "labels": [], "url": "https://example.invalid/5", "state": "open", "draft": false, @@ -377,8 +378,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "52c55ed450b01318747dcdf6af8a01cf27470f38cfeda51969e0df6866ef748a", - "bytes": 551 + "sha256": "f6c73c50b154e140b6389e2e7c8b354665c5fb72a80495d92f0cd5a24dd7fb6c", + "bytes": 567 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/gitlab-happy.golden b/internal/fetch/testdata/gitlab-happy.golden index 944ec27ee..f3d2727f0 100644 --- a/internal/fetch/testdata/gitlab-happy.golden +++ b/internal/fetch/testdata/gitlab-happy.golden @@ -108,6 +108,7 @@ a.txt "base_sha": "488310afbc29fb48fbd84cf16c89d3726158a444", "head_sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "author": "mate", + "labels": [], "url": "https://example.invalid/5", "state": "open", "draft": false, @@ -368,8 +369,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "52c55ed450b01318747dcdf6af8a01cf27470f38cfeda51969e0df6866ef748a", - "bytes": 551 + "sha256": "f6c73c50b154e140b6389e2e7c8b354665c5fb72a80495d92f0cd5a24dd7fb6c", + "bytes": 567 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/gitlab-text.golden b/internal/fetch/testdata/gitlab-text.golden index 05f81ef11..a76fa233d 100644 --- a/internal/fetch/testdata/gitlab-text.golden +++ b/internal/fetch/testdata/gitlab-text.golden @@ -44,6 +44,7 @@ a.txt "base_sha": "488310afbc29fb48fbd84cf16c89d3726158a444", "head_sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "author": "mate", + "labels": [], "url": "https://example.invalid/5", "state": "open", "draft": false, @@ -304,8 +305,8 @@ rather than becoming ground truth. "kind": "bundle", "name": "PR.json", "status": "included", - "sha256": "52c55ed450b01318747dcdf6af8a01cf27470f38cfeda51969e0df6866ef748a", - "bytes": 551 + "sha256": "f6c73c50b154e140b6389e2e7c8b354665c5fb72a80495d92f0cd5a24dd7fb6c", + "bytes": 567 }, { "kind": "bundle", diff --git a/internal/fetch/testdata/happy-json.golden b/internal/fetch/testdata/happy-json.golden index f0ead9fa7..16d0c4183 100644 --- a/internal/fetch/testdata/happy-json.golden +++ b/internal/fetch/testdata/happy-json.golden @@ -74,7 +74,7 @@ done: .verdict/state/4701/PR.json .verdict/state/4701/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4701/PR.json + .verdict/state/4701/full.diff (+ .verdict/state/4701/roles.txt) to any agent; ask it to write .verdict/state/4701/review.json; prior threads are in .verdict/state/4701/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: pr view 4701 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4701/reviews --paginate diff --git a/internal/fetch/testdata/happy-text.golden b/internal/fetch/testdata/happy-text.golden index 919f070c3..d1efe0b5f 100644 --- a/internal/fetch/testdata/happy-text.golden +++ b/internal/fetch/testdata/happy-text.golden @@ -12,7 +12,7 @@ next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.m --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: pr view 4701 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4701/reviews --paginate diff --git a/internal/fetch/testdata/over-cap-absent-text.golden b/internal/fetch/testdata/over-cap-absent-text.golden index ed11d719b..e4a1e46b6 100644 --- a/internal/fetch/testdata/over-cap-absent-text.golden +++ b/internal/fetch/testdata/over-cap-absent-text.golden @@ -13,7 +13,7 @@ next: fit the diff under the cap with --ignore (repeatable on fetch and f try: split the range (--range/--base/--base-sha) or narrow with a paths filter, then re-fetch --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4703 --repo acme/app --- file C/.verdict/.gitignore --- /state/ diff --git a/internal/fetch/testdata/over-cap-chunk-text.golden b/internal/fetch/testdata/over-cap-chunk-text.golden index ab3120aef..8fe5c7628 100644 --- a/internal/fetch/testdata/over-cap-chunk-text.golden +++ b/internal/fetch/testdata/over-cap-chunk-text.golden @@ -13,7 +13,7 @@ next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.m » chunked: 2 chunk(s) at or under 512000 bytes + .verdict/state/4703/chunks/manifest.json --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4703 --repo acme/app CALL: pr view 4703 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4703/reviews --paginate diff --git a/internal/fetch/testdata/over-cap-chunk.golden b/internal/fetch/testdata/over-cap-chunk.golden index b74df9489..107476b8f 100644 --- a/internal/fetch/testdata/over-cap-chunk.golden +++ b/internal/fetch/testdata/over-cap-chunk.golden @@ -86,7 +86,7 @@ done: .verdict/state/4703/PR.json .verdict/state/4703/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4703/PR.json + .verdict/state/4703/chunks/manifest.json and the chunks it lists (+ .verdict/state/4703/roles.txt) to any agent; ask it to write .verdict/state/4703/review.json --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4703 --repo acme/app CALL: pr view 4703 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4703/reviews --paginate diff --git a/internal/fetch/testdata/over-cap-knob.golden b/internal/fetch/testdata/over-cap-knob.golden index 91adce865..76df14693 100644 --- a/internal/fetch/testdata/over-cap-knob.golden +++ b/internal/fetch/testdata/over-cap-knob.golden @@ -86,7 +86,7 @@ done: .verdict/state/4703/PR.json .verdict/state/4703/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4703/PR.json + .verdict/state/4703/chunks/manifest.json and the chunks it lists (+ .verdict/state/4703/roles.txt) to any agent; ask it to write .verdict/state/4703/review.json --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4703 --repo acme/app CALL: pr view 4703 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4703/reviews --paginate diff --git a/internal/fetch/testdata/over-cap-refusal.golden b/internal/fetch/testdata/over-cap-refusal.golden index bbe990ea3..3e08cf35c 100644 --- a/internal/fetch/testdata/over-cap-refusal.golden +++ b/internal/fetch/testdata/over-cap-refusal.golden @@ -57,7 +57,7 @@ candidates (suggested scopes — re-run narrowed, nothing auto-narrowed): - [range] split the range in half (~416339 bytes each): fetch-local --range/--base, fetch --base-sha/--since --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4703 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4703 --repo acme/app --- file C/.verdict/.gitignore --- /state/ diff --git a/internal/fetch/testdata/ratelimit-retry.golden b/internal/fetch/testdata/ratelimit-retry.golden index 6354f1cf3..784750928 100644 --- a/internal/fetch/testdata/ratelimit-retry.golden +++ b/internal/fetch/testdata/ratelimit-retry.golden @@ -79,8 +79,8 @@ next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.m --- file C/.gh-429-count --- 3 --- file C/.gh-calls.log --- -CALL: pr view 4713 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state -CALL: pr view 4713 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4713 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4713 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4713 --repo acme/app CALL: pr view 4713 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4713/reviews --paginate diff --git a/internal/fetch/testdata/sensitive-kb-head.golden b/internal/fetch/testdata/sensitive-kb-head.golden index 76f1b74ad..9d8caa72c 100644 --- a/internal/fetch/testdata/sensitive-kb-head.golden +++ b/internal/fetch/testdata/sensitive-kb-head.golden @@ -79,7 +79,7 @@ done: .verdict/state/4715/PR.json .verdict/state/4715/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4715/PR.json + .verdict/state/4715/full.diff (+ .verdict/state/4715/roles.txt) to any agent; ask it to write .verdict/state/4715/review.json --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4715 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4715 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4715 --repo acme/app CALL: pr view 4715 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4715/reviews --paginate diff --git a/internal/fetch/testdata/sensitive-kb.golden b/internal/fetch/testdata/sensitive-kb.golden index 342c3c4ea..76f95562a 100644 --- a/internal/fetch/testdata/sensitive-kb.golden +++ b/internal/fetch/testdata/sensitive-kb.golden @@ -77,7 +77,7 @@ done: .verdict/state/4715/PR.json .verdict/state/4715/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4715/PR.json + .verdict/state/4715/full.diff (+ .verdict/state/4715/roles.txt) to any agent; ask it to write .verdict/state/4715/review.json --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4715 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4715 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4715 --repo acme/app CALL: pr view 4715 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4715/reviews --paginate diff --git a/internal/fetch/testdata/since-alias.golden b/internal/fetch/testdata/since-alias.golden index cb41a329a..55c91458c 100644 --- a/internal/fetch/testdata/since-alias.golden +++ b/internal/fetch/testdata/since-alias.golden @@ -75,7 +75,7 @@ done: .verdict/state/4701/PR.json .verdict/state/4701/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4701/PR.json + .verdict/state/4701/full.diff (+ .verdict/state/4701/roles.txt) to any agent; ask it to write .verdict/state/4701/review.json; prior threads are in .verdict/state/4701/threads.json (run verdict threads to refresh) --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: pr view 4701 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4701/reviews --paginate diff --git a/internal/fetch/testdata/single-file-degenerate.golden b/internal/fetch/testdata/single-file-degenerate.golden index 850842375..ec67477b0 100644 --- a/internal/fetch/testdata/single-file-degenerate.golden +++ b/internal/fetch/testdata/single-file-degenerate.golden @@ -40,7 +40,7 @@ candidates (suggested scopes — re-run narrowed, nothing auto-narrowed): - [range] split the range in half (~610041 bytes each): fetch-local --range/--base, fetch --base-sha/--since --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4704 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4704 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4704 --repo acme/app --- file C/.verdict/.gitignore --- /state/ diff --git a/internal/fetch/testdata/threads-degraded.golden b/internal/fetch/testdata/threads-degraded.golden index 27cbc119f..7ade35223 100644 --- a/internal/fetch/testdata/threads-degraded.golden +++ b/internal/fetch/testdata/threads-degraded.golden @@ -86,7 +86,7 @@ done: .verdict/state/4701/PR.json .verdict/state/4701/full.diff .verdict/state/4 next: feed SKILL.md + prompts/system.md + prompts/review.md + prompts/diagrams.md + .verdict/state/4701/PR.json + .verdict/state/4701/full.diff (+ .verdict/state/4701/roles.txt) to any agent; ask it to write .verdict/state/4701/review.json --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4701 --repo acme/app CALL: pr view 4701 --repo acme/app --json files --jq .files[] | "\(.path) (+\(.additions)/-\(.deletions))" CALL: api repos/acme/app/pulls/4701/reviews --paginate diff --git a/internal/fetch/testdata/undecodable-pr.golden b/internal/fetch/testdata/undecodable-pr.golden index 01df9d895..8a947f2ed 100644 --- a/internal/fetch/testdata/undecodable-pr.golden +++ b/internal/fetch/testdata/undecodable-pr.golden @@ -19,7 +19,7 @@ exit: 1 » gh pr diff -> .verdict/state/4714/full.diff --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4714 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4714 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4714 --repo acme/app --- file C/.verdict/.gitignore --- /state/ diff --git a/internal/fetch/testdata/upstream-too-large.golden b/internal/fetch/testdata/upstream-too-large.golden index 88ab00817..991a7085a 100644 --- a/internal/fetch/testdata/upstream-too-large.golden +++ b/internal/fetch/testdata/upstream-too-large.golden @@ -22,7 +22,7 @@ gh pr diff failed, trying API diff HTTP 406: this diff is too large to be rendered --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4711 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4711 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state CALL: pr diff 4711 --repo acme/app CALL: api repos/acme/app/pulls/4711 -H Accept: application/vnd.github.diff --- file C/.verdict/.gitignore --- diff --git a/internal/fetch/testdata/view-failed.golden b/internal/fetch/testdata/view-failed.golden index af599d02b..1cede9c0e 100644 --- a/internal/fetch/testdata/view-failed.golden +++ b/internal/fetch/testdata/view-failed.golden @@ -19,7 +19,7 @@ exit: 1 gh: PR not found --- files --- --- file C/.gh-calls.log --- -CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,url,commits,files,additions,deletions,isDraft,reviewDecision,state +CALL: pr view 4701 --repo acme/app --json number,title,body,baseRefName,headRefName,headRefOid,baseRefOid,author,labels,url,commits,files,additions,deletions,isDraft,reviewDecision,state --- file C/.verdict/.gitignore --- /state/ /backtest/ diff --git a/internal/trigger/trigger.go b/internal/trigger/trigger.go new file mode 100644 index 000000000..a06780be4 --- /dev/null +++ b/internal/trigger/trigger.go @@ -0,0 +1,187 @@ +// Package trigger evaluates the closed local trigger predicate set against +// PR facts captured at fetch time. The predicates come from the trusted base +// configuration; the facts come from the bundle's PR.json. +package trigger + +import ( + "fmt" + "regexp" + + "github.com/stainless-code/verdict/internal/config" + "github.com/stainless-code/verdict/internal/core" + "github.com/stainless-code/verdict/internal/jq" + "github.com/stainless-code/verdict/internal/readcommon" +) + +// Facts are the PR inputs a trigger predicate can read. +type Facts struct { + Author string + Paths []string + Labels []string + Title string + BaseBranch string + Draft bool +} + +// Evaluate reports whether a PR runs the review and names the predicate that +// decided it: "matched" for a configured match, "no-trigger" when no predicate +// is set, or the failing predicate's key. Lists OR within one predicate and +// AND across predicates. +func Evaluate(pr Facts, predicates *core.ConfigTrigger) (bool, string) { + if predicates == nil || empty(*predicates) { + return true, "no-trigger" + } + if names := predicates.Authors; len(names) != 0 && !contains(names, pr.Author) { + return false, "authors" + } + if globs := predicates.PathsInclude; len(globs) != 0 && !anyPath(globs, pr.Paths) { + return false, "paths_include" + } + if globs := predicates.PathsExclude; len(globs) != 0 && anyPath(globs, pr.Paths) { + return false, "paths_exclude" + } + if labels := predicates.Labels; len(labels) != 0 && !anyContains(labels, pr.Labels) { + return false, "labels" + } + if pattern := predicates.TitleMatches; pattern != "" { + matched, err := regexp.MatchString(pattern, pr.Title) + if err != nil || !matched { + return false, "title_matches" + } + } + if branches := predicates.BaseBranches; len(branches) != 0 && !anyGlob(branches, pr.BaseBranch) { + return false, "base_branches" + } + if predicates.Draft != nil && *predicates.Draft != pr.Draft { + return false, "draft" + } + return true, "matched" +} + +func empty(p core.ConfigTrigger) bool { + return len(p.Authors)+len(p.PathsInclude)+len(p.PathsExclude)+len(p.Labels)+ + len(p.BaseBranches)+len(p.TitleMatches) == 0 && p.Draft == nil +} + +func contains(values []string, want string) bool { + for _, value := range values { + if value == want { + return true + } + } + return false +} + +func anyContains(values, candidates []string) bool { + for _, candidate := range candidates { + if contains(values, candidate) { + return true + } + } + return false +} + +func anyGlob(globs []string, path string) bool { + for _, pattern := range globs { + if config.Match(pattern, path) { + return true + } + } + return false +} + +func anyPath(globs, paths []string) bool { + for _, path := range paths { + if anyGlob(globs, path) { + return true + } + } + return false +} + +// ReadFacts reads the PR record a fetch produced. Both the raw host shape and +// the normalized bundle shape are accepted: author may be a login object or a +// string, and the draft and base-branch keys carry their provider-neutral +// spellings beside the GitHub ones. +func ReadFacts(dir string) (Facts, error) { + raw, err := jq.CappedFile(dir+"/PR.json", jq.MaxPRBytes) + if err != nil { + return Facts{}, fmt.Errorf("could not read %s/PR.json: %w", dir, err) + } + doc, err := jq.Decode(raw) + if err != nil { + return Facts{}, fmt.Errorf("could not decode %s/PR.json: %w", dir, err) + } + record, ok := jq.AsMap(doc) + if !ok { + return Facts{}, fmt.Errorf("%s/PR.json is not an object", dir) + } + return Facts{ + Author: readAuthor(dir, record), + Paths: stringsFrom(jq.Elements(record["files"]), "path"), + Labels: readLabels(record["labels"]), + Title: jq.StrOf(record["title"]), + BaseBranch: firstString(record["baseRefName"], record["base_ref"]), + Draft: readDraft(record), + }, nil +} + +func readAuthor(dir string, record map[string]any) string { + if author := jq.StrOf(record["author"]); author != "" { + return author + } + if m, ok := jq.AsMap(record["author"]); ok { + if login := jq.StrOf(m["login"]); login != "" { + return login + } + } + return readcommon.OpenBundle(dir).PrField("author") +} + +func firstString(values ...any) string { + for _, value := range values { + if str := jq.StrOf(value); str != "" { + return str + } + } + return "" +} + +func readDraft(record map[string]any) bool { + for _, key := range []string{"isDraft", "draft"} { + if value, ok := record[key].(bool); ok { + return value + } + } + return false +} + +func stringsFrom(entries []any, key string) []string { + out := make([]string, 0, len(entries)) + for _, entry := range entries { + m, ok := jq.AsMap(entry) + if !ok { + continue + } + if value := jq.StrOf(m[key]); value != "" { + out = append(out, value) + } + } + return out +} + +func readLabels(raw any) []string { + var out []string + for _, entry := range jq.Elements(raw) { + if value, ok := entry.(string); ok && value != "" { + out = append(out, value) + continue + } + if m, ok := jq.AsMap(entry); ok { + if value := jq.StrOf(m["name"]); value != "" { + out = append(out, value) + } + } + } + return out +} diff --git a/internal/trigger/trigger_test.go b/internal/trigger/trigger_test.go new file mode 100644 index 000000000..dd1e2492a --- /dev/null +++ b/internal/trigger/trigger_test.go @@ -0,0 +1,108 @@ +package trigger + +import ( + "os" + "path/filepath" + "reflect" + "testing" + + "github.com/stainless-code/verdict/internal/core" +) + +func predicate() *core.ConfigTrigger { + draft := true + return &core.ConfigTrigger{ + Authors: []string{"ana", "bo"}, + PathsInclude: []string{"src/**", "go.mod"}, + PathsExclude: []string{"**/generated/**"}, + Labels: []string{"security", "deps"}, + TitleMatches: "^fix: (retry|auth)", + BaseBranches: []string{"main", "release/*"}, + Draft: &draft, + } +} + +func facts() Facts { + return Facts{ + Author: "ana", + Paths: []string{"src/retry.go", "docs/README.md"}, + Labels: []string{"deps"}, + Title: "fix: retry count", + BaseBranch: "main", + Draft: true, + } +} + +func TestEvaluate(t *testing.T) { + for _, tc := range []struct { + name string + mutate func(*core.ConfigTrigger, *Facts) + wantRun bool + wantReason string + }{ + {name: "all match", wantRun: true, wantReason: "matched"}, + {name: "no predicates", mutate: func(p *core.ConfigTrigger, _ *Facts) { *p = core.ConfigTrigger{} }, wantRun: true, wantReason: "no-trigger"}, + { + name: "author OR second value matches", + mutate: func(p *core.ConfigTrigger, f *Facts) { + f.Author = "bo" + p.Authors = []string{"ana", "bo"} + }, + wantRun: true, wantReason: "matched", + }, + {name: "author fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Author = "mallory" }, wantReason: "authors"}, + {name: "path include fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Paths = []string{"docs/README.md"} }, wantReason: "paths_include"}, + { + name: "path exclude wins over include", + mutate: func(_ *core.ConfigTrigger, f *Facts) { + f.Paths = []string{"src/generated/retry.go"} + }, + wantReason: "paths_exclude", + }, + {name: "labels fail", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Labels = []string{"docs"} }, wantReason: "labels"}, + {name: "title fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Title = "chore: retry" }, wantReason: "title_matches"}, + {name: "base branch fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.BaseBranch = "dev" }, wantReason: "base_branches"}, + {name: "base branch glob matches", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.BaseBranch = "release/1.2" }, wantRun: true, wantReason: "matched"}, + {name: "draft fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Draft = false }, wantReason: "draft"}, + } { + t.Run(tc.name, func(t *testing.T) { + p, f := predicate(), facts() + if tc.mutate != nil { + tc.mutate(p, &f) + } + run, reason := Evaluate(f, p) + if run != tc.wantRun || reason != tc.wantReason { + t.Errorf("Evaluate = (%v, %q), want (%v, %q)", run, reason, tc.wantRun, tc.wantReason) + } + }) + } + run, reason := Evaluate(facts(), nil) + if !run || reason != "no-trigger" { + t.Errorf("Evaluate with nil predicates = (%v, %q), want (true, no-trigger)", run, reason) + } +} + +func TestReadFacts(t *testing.T) { + dir := t.TempDir() + raw := `{"provider":"github","title":"fix: retry","baseRefName":"main","isDraft":true, + "author":{"login":"ana"},"labels":[{"name":"deps"},{"name":"security"}], + "files":[{"path":"src/retry.go"},{"path":"docs/README.md"}]}` + if err := os.WriteFile(filepath.Join(dir, "PR.json"), []byte(raw), 0o600); err != nil { + t.Fatal(err) + } + got, err := ReadFacts(dir) + if err != nil { + t.Fatalf("ReadFacts: %v", err) + } + want := Facts{ + Author: "ana", + Paths: []string{"src/retry.go", "docs/README.md"}, + Labels: []string{"deps", "security"}, + Title: "fix: retry", + BaseBranch: "main", + Draft: true, + } + if !reflect.DeepEqual(got, want) { + t.Errorf("ReadFacts = %#v, want %#v", got, want) + } +} From 17dac96f0c087211640f7958de265a30f8ba6825 Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Tue, 17 Nov 2026 08:57:40 +0200 Subject: [PATCH 3/7] ready: gate on the base-branch trigger with --filter --- clients/ts/src/refusal-codes.gen.ts | 2 + cmd/verdict/usage.go | 5 +- docs/troubleshooting.md | 12 ++++ internal/archtest/imports_test.go | 1 + internal/config/runstate.go | 34 +++++++++ internal/core/hint.go | 2 + internal/core/ready/service.go | 6 ++ internal/core/ready/trigger.go | 73 +++++++++++++++++++ internal/core/ready/trigger_test.go | 107 ++++++++++++++++++++++++++++ internal/core/refusalcodes.go | 2 + internal/core/testdata/hints.golden | 2 + internal/ready/filter_test.go | 17 +++++ internal/ready/ready.go | 12 ++-- scripts/verdict-complete.sh | 2 +- 14 files changed, 271 insertions(+), 6 deletions(-) create mode 100644 internal/core/ready/trigger.go create mode 100644 internal/core/ready/trigger_test.go create mode 100644 internal/ready/filter_test.go diff --git a/clients/ts/src/refusal-codes.gen.ts b/clients/ts/src/refusal-codes.gen.ts index e4cb520ba..7aa7cdf57 100644 --- a/clients/ts/src/refusal-codes.gen.ts +++ b/clients/ts/src/refusal-codes.gen.ts @@ -99,6 +99,7 @@ export type RefusalCode = | "download-failed" | "duplicate-payload" | "fetch-failed" + | "filtered-out" | "fix-ceiling" | "fix-failed" | "fix-patch-secret" @@ -201,6 +202,7 @@ export type RefusalCode = | "stale-review-artifact" | "store-busy" | "threads-failed" + | "trigger-failed" | "triage-failed" | "uninstall-foreign" | "uninstall-no-receipt" diff --git a/cmd/verdict/usage.go b/cmd/verdict/usage.go index 4c3d53b0a..1866cb01e 100644 --- a/cmd/verdict/usage.go +++ b/cmd/verdict/usage.go @@ -235,7 +235,7 @@ func usageText() string { " Foreground wait: timeout 1s–30m; interval >=1s. Exit 0 on condition,\n" + " exit 1 with wait-timeout or condition-unmet data; no local writes.\n" + "\n" + - " ready [--json] [--check] [--wait-for build --timeout D]\n" + + " ready [--json] [--check] [--filter] [--wait-for build --timeout D]\n" + " advisory only (exit 0, never a gate): CI state, open threads, draft/conflict,\n" + " description completeness. Prints blockers with actions + recommendation\n" + " (review/wait/nudge-author). GitHub and GitLab PR bundles; local bundles refuse\n" + @@ -243,6 +243,9 @@ func usageText() string { " --check opts into gating (same output, machine exits): 0 ready, 1 blockers\n" + " or wait, 2 usage, 3 tool error. Without --check the run stays advisory:\n" + " exit 0 with blockers listed (typed errors exit 1, usage exits 2).\n" + + " --filter applies the closed trigger in .verdict/config.json at the PR base\n" + + " before any live read: a non-matching PR refuses filtered-out naming the\n" + + " predicate; both outcomes are recorded in the bundle's run.json.\n" + " gate [--max-severity critical|important|informational] [--require-head]\n" + " [--live auto|strict|skip] [--coverage require|warn] [--json]\n" + " one composed local gate over the dimensions report already computes: open\n" + diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 774fc1514..29f2f6d74 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -600,6 +600,12 @@ Fix: for provider failures, run `gh auth status` and confirm the repo/PR spellin For context failures, hand over regular Markdown, JSON or `.txt` files — at most 64 files, 1 MiB total and eight path levels — then re-run. +## `filtered-out` + +Cause: `ready --filter` found a trigger predicate in `.verdict/config.json` at the PR base that the PR does not match; the refusal names the failing predicate. + +Fix: review the PR without `--filter`, or change the trigger at the base branch and re-fetch. The decision is recorded in `run.json`. + ## `fix-ceiling` Cause: the fix loop hit its pass ceiling without clearing the sig. @@ -1260,6 +1266,12 @@ Cause: `triage` could not write `findings.json`. Fix: make the bundle directory writable, then re-run. +## `trigger-failed` + +Cause: the trigger could not read its trusted-base policy or record the decision in `run.json`. + +Fix: re-fetch the bundle so its base snapshot is complete, then re-run `ready --filter`. + ## `uninstall-foreign` Cause: the binary is not installer-managed. diff --git a/internal/archtest/imports_test.go b/internal/archtest/imports_test.go index aee99dcdc..0b850fbe5 100644 --- a/internal/archtest/imports_test.go +++ b/internal/archtest/imports_test.go @@ -119,6 +119,7 @@ var facadeAllowedDeps = map[string]bool{ "internal/sig": true, "internal/storelock": true, "internal/strictjson": true, + "internal/trigger": true, "internal/untrusted": true, "internal/validation": true, "pkg/verdict": true, diff --git a/internal/config/runstate.go b/internal/config/runstate.go index c67dc8449..b82facbb5 100644 --- a/internal/config/runstate.go +++ b/internal/config/runstate.go @@ -37,11 +37,21 @@ type RunState struct { Inventory map[string]string `json:"inventory,omitempty"` Evidence Evidence `json:"evidence"` Outcomes []CheckObservation `json:"outcomes,omitempty"` + Trigger *TriggerDecision `json:"trigger,omitempty"` Root RequirementSet `json:"root"` Trusted bool `json:"trusted"` RequiresEvidence bool `json:"requires_evidence,omitempty"` } +// TriggerDecision is the recorded local trigger outcome: whether the PR +// entered review, the predicate that decided it, and the head it decided on. +type TriggerDecision struct { + At string `json:"at"` + Head string `json:"head"` + Run bool `json:"run"` + Reason string `json:"reason"` +} + // StatusUsed marks a snapshot entry whose bytes were captured. const StatusUsed = "used" @@ -158,6 +168,29 @@ func AppendOutcome(dir string, obs *CheckObservation) (bool, error) { return true, nil } +// AppendTrigger records one trigger decision on an existing run bundle. +// Like an outcome, it is an observation rather than a captured input, so the +// inputs digest stays stable. Absent run state appends nothing. +func AppendTrigger(dir string, decision *TriggerDecision) (bool, error) { + rs, found, serr := ReadRunState(dir) + if serr != nil { + return false, serr + } + if !found { + return false, nil + } + rs.Trigger = decision + raw, err := json.MarshalIndent(rs, "", " ") + if err != nil { + return false, &stateError{msg: "could not encode run state for " + rs.Name, cause: err} + } + path := filepath.Join(dir, runStateFileName) + if werr := atomic.WriteFile(path, append(raw, '\n')); werr != nil { + return false, &stateError{msg: "could not write " + path, cause: werr} + } + return true, nil +} + // VerifyRunState loads the run bundle at dir/run.json and binds it to the // controls snapshot beside it, refusing controls-stale on any drift: a // deleted or corrupt bundle, edited captured inputs, or support bytes @@ -261,6 +294,7 @@ func verifyInventory(dir string, rs *RunState, snap *controls.Snapshot) *core.Re func inputsDigest(rs *RunState) string { cp := *rs cp.Outcomes = nil + cp.Trigger = nil cp.InputsSHA256 = "" // The digest binds logical inputs, not volatile serialization: // capture timestamps and temp paths vary run to run for the same diff --git a/internal/core/hint.go b/internal/core/hint.go index 61abab68b..bbfd0b54f 100644 --- a/internal/core/hint.go +++ b/internal/core/hint.go @@ -48,6 +48,8 @@ var hintRules = []HintRule{ "or pass --allow-secret-values to publish it anyway"}, {codes: []string{"fix-patch-secret"}, hint: "try: remove the value from the worktree (rotate it " + "if it was real), then re-run fix"}, + {codes: []string{"filtered-out"}, hint: "try: remove --filter to review every PR, or change the trigger in .verdict/config.json at the PR base"}, + {codes: []string{"trigger-failed"}, hint: "try: re-fetch the bundle so its base-snapshot policy is complete, then re-run"}, { codes: []string{"diff-too-large"}, hint: "try: split the range (--range/--base/--base-sha) or narrow with a paths filter, then re-fetch", diff --git a/internal/core/ready/service.go b/internal/core/ready/service.go index 441219956..cf14eeb6d 100644 --- a/internal/core/ready/service.go +++ b/internal/core/ready/service.go @@ -31,6 +31,7 @@ type Request struct { //nolint:govet // field grouping documents the request sur Timeout string PollRuntime PollRuntime `json:"-"` Check bool + Filter bool `json:"-"` Warnings []string } @@ -246,6 +247,11 @@ func loadState(ctx context.Context, st *state) *core.RefusalError { Message: "missing " + dir + "/PR.json (run fetch " + st.repo + " " + st.pr + " first)", }) } + if st.req.Filter { + if ref := st.filterTrigger(dir); ref != nil { + return ref + } + } if ref := host.live(ctx, st, dir); ref != nil { return ref } diff --git a/internal/core/ready/trigger.go b/internal/core/ready/trigger.go new file mode 100644 index 000000000..38cdb821a --- /dev/null +++ b/internal/core/ready/trigger.go @@ -0,0 +1,73 @@ +package readycore + +import ( + "fmt" + + "github.com/stainless-code/verdict/internal/config" + "github.com/stainless-code/verdict/internal/controls" + "github.com/stainless-code/verdict/internal/core" + "github.com/stainless-code/verdict/internal/readcommon" + "github.com/stainless-code/verdict/internal/trigger" +) + +const ( + codeFilteredOut = "filtered-out" + codeTriggerFail = "trigger-failed" +) + +// filterTrigger evaluates the trusted-base trigger before any live host +// read, and records both outcomes in the bundle's run receipt. The policy +// bytes come from the base snapshot the fetch captured, never the worktree. +func (st *state) filterTrigger(dir string) *core.RefusalError { + predicates, ref := baseTrigger(dir) + if ref != nil { + return st.withWarnings(ref) + } + facts, err := trigger.ReadFacts(dir) + if err != nil { + return st.withWarnings(&core.RefusalError{Code: codeTriggerFail, Message: err.Error()}) + } + run, reason := trigger.Evaluate(facts, predicates) + decision := &config.TriggerDecision{ + At: st.req.TimeUTC(), + Head: readcommon.OpenBundle(dir).Head(), + Run: run, + Reason: reason, + } + written, err := config.AppendTrigger(dir, decision) + if err != nil || !written { + return st.withWarnings(&core.RefusalError{ + Code: codeTriggerFail, + Message: "could not record the trigger decision in " + dir + "/run.json (re-fetch the bundle)", + }) + } + if !run { + return st.withWarnings(&core.RefusalError{ + Code: codeFilteredOut, + Message: fmt.Sprintf("filtered out by %s (policy: %s at the PR base)", reason, config.ConfigPath), + }) + } + return nil +} + +func baseTrigger(dir string) (*core.ConfigTrigger, *core.RefusalError) { + snap, found, ref := controls.GateRecordedPath(dir, config.ConfigPath) + if ref != nil { + return nil, ref + } + if !found { + return nil, nil + } + raw, ok := snap.ReadFile(config.ConfigPath) + if !ok { + return nil, &core.RefusalError{ + Code: codeTriggerFail, + Message: "base snapshot has no readable " + config.ConfigPath + " (re-fetch the bundle)", + } + } + cfg, ref := config.Parse(raw) + if ref != nil { + return nil, ref + } + return cfg.Trigger, nil +} diff --git a/internal/core/ready/trigger_test.go b/internal/core/ready/trigger_test.go new file mode 100644 index 000000000..e1666284f --- /dev/null +++ b/internal/core/ready/trigger_test.go @@ -0,0 +1,107 @@ +package readycore_test + +import ( + "context" + "path/filepath" + "strings" + "testing" + + "github.com/stainless-code/verdict/internal/config" + "github.com/stainless-code/verdict/internal/controls" + "github.com/stainless-code/verdict/internal/core" + readycore "github.com/stainless-code/verdict/internal/core/ready" +) + +func writeTriggerBundle(t *testing.T, cfg string) (string, string) { + t.Helper() + rs := &config.RunState{ + BaseSHA: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + Trusted: true, + } + dir := writeReadyBundle(t, rs) + write(t, filepath.Join(".", ".verdict", "config.json"), cfg) + write(t, filepath.Join(dir, "PR.json"), `{"provider":"github","number":5201,"title":"fix: retry",`+ + `"baseRefName":"main","isDraft":false,"author":{"login":"ana"},"labels":[{"name":"deps"}],`+ + `"files":[{"path":"src/retry.go"}],"headRefOid":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}`) + if _, ref := controls.Resolve(t.Context(), nil, &controls.Request{ + BundleDir: dir, Root: ".", BaseSHA: rs.BaseSHA, AllowHead: true, + }); ref != nil { + t.Fatalf("controls.Resolve: %v", ref) + } + return dir, rs.BaseSHA +} + +func triggerRequest(bundle string) *readycore.Request { + req := &readycore.Request{Filter: true} + req.ReadRequest = core.ReadRequest{ + Runtime: &core.Runtime{Commander: &readyCommander{head: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}}, + Repo: "acme/app", Pr: "5201", + } + return req +} + +func TestReadyFilterRunsMatchingPR(t *testing.T) { + cfg := `{"schema_version":"1","trigger":{"paths_include":["src/**"],"authors":["ana"]}}` + dir, _ := writeTriggerBundle(t, cfg) + write(t, filepath.Join(".", ".verdict", "config.json"), + `{"schema_version":"1","trigger":{"paths_include":["never/**"]}}`) + req := triggerRequest(dir) + resp, ref := readycore.Run(context.Background(), req) + if ref != nil { + t.Fatalf("matching PR refused: %v", ref) + } + if resp.Data == nil || !resp.Data.Ready { + t.Fatalf("matching PR did not reach readiness: %+v", resp.Data) + } + stored, found, serr := config.ReadRunState(dir) + if serr != nil || !found || stored.Trigger == nil { + t.Fatalf("run receipt has no trigger decision (found=%v err=%v)", found, serr) + } + if !stored.Trigger.Run || stored.Trigger.Reason != "matched" { + t.Errorf("receipt = %+v, want run/matched", stored.Trigger) + } +} + +func TestReadyFilterRefusesFilteredOutPR(t *testing.T) { + cfg := `{"schema_version":"1","trigger":{"paths_include":["protected/**"]}}` + dir, _ := writeTriggerBundle(t, cfg) + cmdr := &countingCommander{readyCommander: readyCommander{head: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}} + req := triggerRequest(dir) + req.Runtime.Commander = cmdr + _, ref := readycore.Run(context.Background(), req) + if ref == nil || ref.Code != "filtered-out" { + t.Fatalf("refusal = %+v, want filtered-out", ref) + } + if !strings.Contains(ref.Message, "paths_include") { + t.Errorf("refusal misses the failing predicate: %q", ref.Message) + } + if cmdr.calls != 0 { + t.Errorf("host reads after filter = %d, want 0", cmdr.calls) + } + stored, found, serr := config.ReadRunState(dir) + if serr != nil || !found || stored.Trigger == nil { + t.Fatalf("run receipt has no trigger decision (found=%v err=%v)", found, serr) + } + if stored.Trigger.Run || stored.Trigger.Reason != "paths_include" { + t.Errorf("receipt = %+v, want filtered/paths_include", stored.Trigger) + } +} + +func TestReadyFilterRefusesUnknownBaseConfigKey(t *testing.T) { + cfg := `{"schema_version":"1","trigger":{"bogus":true}}` + dir, _ := writeTriggerBundle(t, cfg) + _, ref := readycore.Run(context.Background(), triggerRequest(dir)) + if ref == nil || ref.Code != config.CodeBadConfig || !strings.Contains(ref.Message, "trigger.bogus") { + t.Fatalf("refusal = %+v, want bad-config naming trigger.bogus", ref) + } +} + +type countingCommander struct { + readyCommander + calls int +} + +func (c *countingCommander) Run(ctx context.Context, cmd *core.Command) (core.Result, error) { + c.calls++ + return c.readyCommander.Run(ctx, cmd) +} diff --git a/internal/core/refusalcodes.go b/internal/core/refusalcodes.go index 5b6113d77..35e5b1bee 100644 --- a/internal/core/refusalcodes.go +++ b/internal/core/refusalcodes.go @@ -122,6 +122,7 @@ var refusalCodes = []string{ "download-failed", "duplicate-payload", "fetch-failed", + "filtered-out", "fix-ceiling", "fix-failed", "fix-patch-secret", @@ -225,6 +226,7 @@ var refusalCodes = []string{ "store-busy", "threads-failed", "triage-failed", + "trigger-failed", "uninstall-foreign", "uninstall-no-receipt", "unknown-agent", diff --git a/internal/core/testdata/hints.golden b/internal/core/testdata/hints.golden index 9508e1097..7b11ab40b 100644 --- a/internal/core/testdata/hints.golden +++ b/internal/core/testdata/hints.golden @@ -95,6 +95,7 @@ diff-too-large try: split the range (--range/--base/--base-sha) or narrow with a download-failed try: address the cause above, then re-run the same command duplicate-payload try: re-run with --force to repost the identical payload — re-fetch if the earlier review was deleted fetch-failed try: run 'gh auth status', confirm the repo/PR spelling, then re-run +filtered-out try: remove --filter to review every PR, or change the trigger in .verdict/config.json at the PR base fix-ceiling try: triage the sig or split the fix — the 3-pass ceiling is hard-coded fix-failed try: address the cause above, then re-run the same command fix-patch-secret try: remove the value from the worktree (rotate it if it was real), then re-run fix @@ -198,6 +199,7 @@ stale-review-artifact try: delete the stale review.json (or let the agent overwr store-busy try: wait for the write holding the store's lock (the OS drops the lock when its holder exits, so a retry after a crash succeeds), then re-run threads-failed try: run 'gh auth status', confirm the repo/PR spelling, then re-run triage-failed try: address the cause above, then re-run the same command +trigger-failed try: re-fetch the bundle so its base-snapshot policy is complete, then re-run uninstall-foreign try: address the cause above, then re-run the same command uninstall-no-receipt try: address the cause above, then re-run the same command unknown-agent try: re-run with one of the known agent names listed above diff --git a/internal/ready/filter_test.go b/internal/ready/filter_test.go new file mode 100644 index 000000000..4abac539a --- /dev/null +++ b/internal/ready/filter_test.go @@ -0,0 +1,17 @@ +package ready + +import ( + "testing" + + readycore "github.com/stainless-code/verdict/internal/core/ready" +) + +func TestParseFilterFlag(t *testing.T) { + q := &readycore.Request{} + if err := parseFlags([]string{"--filter", "--check"}, q); err != nil { + t.Fatalf("parseFlags: %v", err) + } + if !q.Filter || !q.Check { + t.Fatalf("Filter=%v Check=%v, want both true", q.Filter, q.Check) + } +} diff --git a/internal/ready/ready.go b/internal/ready/ready.go index c6e57a6ec..902326a26 100644 --- a/internal/ready/ready.go +++ b/internal/ready/ready.go @@ -18,8 +18,8 @@ import ( // Request wires a ready run. type Request = readcommon.Request -// Run executes `ready [--json, --check]`, returning the -// exit code. +// Run executes `ready [--json, --check, --filter]`, +// returning the exit code. func Run(ctx context.Context, r *cli.Runner, args []string, req Request) int { q := readyRequest(r, req) if err := parseFlags(args, q); err != nil { @@ -68,8 +68,12 @@ func readyRequest(r *cli.Runner, req Request) *readycore.Request { func parseFlags(args []string, q *readycore.Request) *cli.UsageError { for len(args) > 0 { flag := args[0] - if flag == "--check" { - q.Check = true + if flag == "--check" || flag == "--filter" { + if flag == "--check" { + q.Check = true + } else { + q.Filter = true + } args = args[1:] continue } diff --git a/scripts/verdict-complete.sh b/scripts/verdict-complete.sh index 5addb8478..72929cb14 100755 --- a/scripts/verdict-complete.sh +++ b/scripts/verdict-complete.sh @@ -35,7 +35,7 @@ _verdict_flags() { # $1 = verb ingest) printf '%s\n' --sarif --json;; fix) printf '%s\n' --sig --all --selected --min-severity --verify-cmd --paths --json;; pr-wait) printf '%s\n' --until --timeout --once --interval --json --provider ;; - ready) printf '%s\n' --json --check --wait-for --timeout;; + ready) printf '%s\n' --json --check --filter --wait-for --timeout;; gate) printf '%s\n' --max-severity --require-head --live --coverage --json;; mcp) printf '%s\n' --read-only --operator --agent --write --dry-run --json;; serve) printf '%s\n' --root --depth --max-visits --max-repos --rediscover --no-registry --host --port --token --check --max-bundles --interval --sweep --no-watch --json;; From abb3389f33a891e8f8796faed1c97a67d8f1ac04 Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Tue, 17 Nov 2026 09:02:51 +0200 Subject: [PATCH 4/7] docs(evals): cover the base-branch trigger gate --- .changeset/trigger-filter.md | 5 +++ docs/manual.md | 19 ++++++++- evals/README.md | 1 + evals/cases/trigger-filter.sh | 75 +++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 .changeset/trigger-filter.md create mode 100755 evals/cases/trigger-filter.sh diff --git a/.changeset/trigger-filter.md b/.changeset/trigger-filter.md new file mode 100644 index 000000000..cb1eae9b1 --- /dev/null +++ b/.changeset/trigger-filter.md @@ -0,0 +1,5 @@ +--- +"@stainless-code/verdict": minor +--- + +`ready --filter` now gates the review on the closed trigger predicates in the trusted base configuration: matching PRs run and record the decision, non-matching PRs refuse `filtered-out` before any host call, and unusable policy or facts refuse `trigger-failed`. diff --git a/docs/manual.md b/docs/manual.md index 0f705f6b9..2c521e354 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -579,7 +579,7 @@ the artifact, who writes it, and whether a verb gates on it. | Artifact | What it is | Who writes it | Gates? | | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `run.json` | The typed per-run capture: resolved defaults, per-file resolutions, plan, spend and privacy views, trusted-base inventory hashes, base SHA, the `inputs_sha256` digest, and appended check observations. | `fetch`/`fetch-local`; `post` appends observations | Yes, except `config --preview` (see below). `post`, `record`, `triage`, `report`, `ready`, `config --explain`, and `verify --group` verify it; a run bundle deleted while its controls snapshot remains, or an edited captured input, refuses `controls-stale`; a bare bundle dir reads with a warning and no repository requirements. | +| `run.json` | The typed per-run capture: resolved defaults, per-file resolutions, plan, spend and privacy views, trusted-base inventory hashes, base SHA, the `inputs_sha256` digest, appended check observations, and the `trigger` decision from `ready --filter`. | `fetch`/`fetch-local`; `post` appends observations | Yes, except `config --preview` (see below). `post`, `record`, `triage`, `report`, `ready`, `config --explain`, and `verify --group` verify it; a run bundle deleted while its controls snapshot remains, or an edited captured input, refuses `controls-stale`; a bare bundle dir reads with a warning and no repository requirements. | | `validation-.json` | The deep-validation receipt for one plan group: run, inputs digest, group, reviewed and execution heads, canonical root, command, exit, timeout and sandbox flags, status, and a redacted output tail. | `verify --group --command ` | Yes. `post` refuses a binding approval while any `depth: "deep"` group lacks a current receipt; `ready` reports the gap as `profile-work`. | | `filesource.json` | Provenance for the PR file list: `source` (`paged` or `cli`), `cli_cap` (100), `provider_ceiling` (3000), `count`, and `truncated` or `paging_error`. | GitHub `fetch`, when the CLI list arrives at its 100-file cap | No. It is an advisory coverage note: a paging failure warns and the run proceeds with the CLI's first 100 files. | | `full.raw.diff` | The raw pre-exclusion diff, kept as the patch-identity input when a filtered diff would otherwise replace it. No shipped verb writes it today: `fetch` writes the host diff to `full.diff` and records the filtered hash in `run.json` without writing filtered bytes. | Nothing today (a future exclusion pass) | Partly. When present it feeds the identity, and `identity.json`'s `raw_diff_sha256` must reproduce it; a present-but-unreadable file refuses instead of falling back to `full.diff`. Absence is normal. | @@ -974,6 +974,23 @@ missing or stale receipt as required work without executing anything. `ready` stays advisory (exit 0); `ready --check` turns the same list into the machine exits 0 ready, 1 blockers-or-wait, 2 usage, 3 tool error. +### Base-branch trigger gate + +`verdict ready owner/repo 42 --filter` gates the readiness read on the `trigger` +predicates in the trusted `.verdict/config.json` snapshot the bundle fetched, +not the worktree's current config. A configured predicate must match: `authors` +(exact login), `paths_include`, `paths_exclude`, `labels`, `base_branches`, +`title_matches` (Go regular expression), or `draft`. Lists OR within a +predicate and AND across predicates. The decision lands in `run.json` as +`trigger` (`run`, `reason`, `head`, `at`) and leaves the inputs digest alone. + +A match runs the ordinary readiness read. A miss refuses `filtered-out` with +exit code 1, naming the failing predicate before any host command runs; +without `--filter` the same PR is unchanged. Missing or malformed base policy, +facts, or write access refuse `trigger-failed`; an unknown key remains a +`bad-config` parse error naming `trigger.`. `--filter` composes with +`--check` and `--json`. + On GitLab the same blockers read through `glab`: the merge request carries `draft`, `has_conflicts` (plus a `conflict` `detailed_merge_status`), and the `description`, while CI comes from the merge request's pipeline list (a failed diff --git a/evals/README.md b/evals/README.md index 1ac0385dd..7ab1bd269 100644 --- a/evals/README.md +++ b/evals/README.md @@ -195,6 +195,7 @@ the build. | `threads-resolve-github` | Explicit GitHub thread resolve + receipt | A seeded GitHub bundle: `threads resolve --dry-run` lists the verdict-authored addressed thread and makes zero host calls; a real run makes exactly one literal `resolveReviewThread` call, marks the feed resolved, and writes `threads-resolve.json`; a second run skips `already-resolved` with no call; `--thread` on a human thread refuses `not-verdict-authored` and leaves the receipt untouched; calibrate's follow-up read joins the resolved thread to the addressed finding as `fixed`. | | `threads-resolve-gitlab` | Explicit GitLab thread resolve + receipt | A seeded GitLab bundle: `threads resolve --dry-run` lists the verdict-authored addressed discussion and makes zero host calls; a real run makes exactly one literal `glab api ... discussions/d1?resolved=true --method PUT` call, marks the feed resolved, and writes `threads-resolve.json`; a second run skips `already-resolved` with no call; `--thread` on a human discussion refuses `not-verdict-authored` and makes no call. | | `trend-incidents` | `trend --incidents` joins incidents to the stores | Three attached incidents over three bundles: 3 attached, 2 measured, 1 unmeasured (its `findings.json` is gone), 1 hit, 1 miss, `miss_rate: 0.5`, and per-bundle rows with a null `miss_rate` for the unmeasured bundle; the human line reads the literal `incidents: 3 attached, 2 measured, 1 unmeasured — missed 1 of 2 (50.0%)`; the unflagged run never mentions incidents; flipping the caught finding to `false-positive` re-measures 2 misses at `miss_rate: 1`; an absent or empty ledger reads a zero tally and a null `miss_rate`; MCP `trend` with `incidents: true` returns the same object as `--json`; zero host calls | +| `trigger-filter` | `ready --filter`: base-branch trigger gate | A producer-built PR bundle with a base-snapshot trigger: a PR touching `protected/**` runs and records `trigger.reason=matched`; a PR touching only docs refuses `filtered-out` naming `paths_include`, pays zero host calls, and records `run=false` beside that predicate; without `--filter` the same PR still reads live; unknown base keys fail `bad-config`. | | `upgrade` | Explicit `verdict upgrade` + `verdict uninstall` | Local loopback release server serves fake archives/checksums/bundle: installer self-replace updates the binary + receipt after SHA-256/signature/`--version` smoke; latest resolves through the redirect; checksum mismatch leaves the old binary; dry-run makes no request or write; npm delegation runs the exact argv, streams output, and propagates exit 7; project-local/skill/unidentifiable refuse typed with the exact command; uninstall removes only managed files (+ `--remove-path` marker) and refuses foreign channels; checksum-only warns/`VERDICT_REQUIRE_SIGNATURE=1` fails closed | | `usage-adapters` | Optional harness converters outside the binary | `claude-to-usage.sh` and `opencode-to-usage.sh` turn the checked-in transcripts into records that validate against `schemas/usage.schema.json`, preferring a cost-state or session total and deduping repeats; the Stop hook writes a validating usage file; a timestamp-less transcript refuses with nothing written; tokens without a price still convert unpriced; the adapters README documents both as optional; a missing jq is a SKIP, never a pass | | `usage-errors` | The binary owns usage errors (arity matrix, wins-over-faults) | No verb, missing positionals (one leg per verb; bare verbs never fail), and unknown verbs print the full usage block on stderr with exit 2 and no envelope; corrupt settings plus a missing positional still usage-errors (a complete invocation reads settings and refuses typed `bad-settings`); the block names `verdict` and keeps `--json`, the verify codes, and `token-scopes-excess` | diff --git a/evals/cases/trigger-filter.sh b/evals/cases/trigger-filter.sh new file mode 100755 index 000000000..5357b74f7 --- /dev/null +++ b/evals/cases/trigger-filter.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +# +# Trigger gate: ready --filter reads the closed trigger predicates from the +# trusted base snapshot, refuses filtered-out before any host call, and records +# both decisions in run.json. A PR that touches a protected path runs. +set -euo pipefail +HERE="$(cd "$(dirname "$0")" && pwd)" +# shellcheck source=../lib/common.sh +. "$HERE/../lib/common.sh" + +STUB_SAVED="$EVAL_SANDBOX/gh-stub-orig" +cp "$EVAL_STUBBIN/gh" "$STUB_SAVED" +restore_stub() { cp "$STUB_SAVED" "$EVAL_STUBBIN/gh"; } +trap restore_stub EXIT +cat >"$EVAL_STUBBIN/gh" <<'EOF' +#!/usr/bin/env bash +echo "stub-gh $*" >> "${EVAL_STUBLOG:-/dev/null}" +case "$*" in + *"isDraft,mergeable,mergeStateStatus,body,state,headRefOid"*) + printf '%s' '{"isDraft":false,"mergeable":"MERGEABLE","mergeStateStatus":"CLEAN","body":"A long enough description for the readiness check to pass without a blocker.","state":"OPEN","headRefOid":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}' ;; + *"pr checks"*) printf '%s' '[]' ;; + *"graphql"*) printf '%s' '{"data":{"repository":{"pullRequest":{"reviewThreads":{"nodes":[]}}}}}' ;; + *) echo "trigger-eval: unhandled gh call ($*)" >&2; exit 1 ;; +esac +EOF +chmod +x "$EVAL_STUBBIN/gh" + +mkdir -p "$EVAL_SANDBOX/.verdict" +cat >"$EVAL_SANDBOX/.verdict/config.json" <<'EOF' +{"schema_version":"1","trigger":{"paths_include":["protected/**"],"authors":["teammate"]}} +EOF +eval_mk_bundle 42 +PR="$EVAL_SANDBOX/.verdict/state/42" +jq '. + {title:"fix: retry", baseRefName:"main", labels:[{"name":"deps"}], + files:[{path:"protected/key.go"}]}' "$PR/PR.json" >"$PR/PR.json.tmp" +mv "$PR/PR.json.tmp" "$PR/PR.json" +printf '[]' >"$PR/findings.json" + +mark="$(wc -l <"$EVAL_STUBLOG" | tr -d ' ')" +run_review ready acme/app 42 --filter --json +assert_exit "a PR touching a protected path runs" 0 +assert_json_field "the matching PR is still advisory" true '.data.ready' +if [ "$(wc -l <"$EVAL_STUBLOG" | tr -d ' ')" -gt "$mark" ]; then + good "a matching PR reaches the live reads" +else + bad "a matching PR paid no host calls" +fi +assert_file_grep "the receipt records the run" "$PR/run.json" '"reason": "matched"' +assert_file_grep "the receipt records run true" "$PR/run.json" '"run": true' + +jq '.files = [{path:"docs/README.md"}]' "$PR/PR.json" >"$PR/PR.json.tmp" +mv "$PR/PR.json.tmp" "$PR/PR.json" +mark="$(wc -l <"$EVAL_STUBLOG" | tr -d ' ')" +run_review ready acme/app 42 --filter --json +if [ "$EVAL_STATUS" -eq 1 ] && [ "$(jq -r '.error.code' "$EVAL_OUT")" = "filtered-out" ] \ + && jq -e '.error.message | contains("paths_include")' "$EVAL_OUT" >/dev/null; then + good "an untouched PR refuses filtered-out naming paths_include" +else + bad "filtered PR exits $EVAL_STATUS: $(head -c 300 "$EVAL_OUT")" +fi +assert_eq "the gate pays zero host calls" 0 "$(( $(wc -l <"$EVAL_STUBLOG" | tr -d ' ') - mark ))" +assert_file_grep "the receipt records the failing predicate" "$PR/run.json" '"reason": "paths_include"' +assert_file_grep "the receipt records run false" "$PR/run.json" '"run": false' + +mark="$(wc -l <"$EVAL_STUBLOG" | tr -d ' ')" +run_review ready acme/app 42 --json +assert_exit "without --filter the same PR is unchanged" 0 +if [ "$(wc -l <"$EVAL_STUBLOG" | tr -d ' ')" -gt "$mark" ]; then + good "without --filter the readiness read still pays host calls" +else + bad "without --filter no host call was made" +fi + +assert_file_nogrep "no unhandled gh call escaped the stub" "$EVAL_STUBLOG" "unhandled gh call" +eval_end From 7a91ec23f76b8ef4ca07f5b88d0c7c1db3cee94e Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Tue, 17 Nov 2026 09:49:57 +0200 Subject: [PATCH 5/7] fix(trigger): harden the local trigger gate - Gate before build waits and live reads, and keep filtered-out at exit 1 under --check. - Refuse head-policy, damaged, or malformed base snapshots; preserve bad-config for unknown keys. - Treat excluded paths as removable before include matching and skip path predicates on truncated file lists. - Verify and lock the run receipt before appending a decision. - Validate base-branch globs, align PR.json label shape docs and changeset, and tighten tests and surface text. --- .changeset/trigger-filter.md | 2 + cmd/verdict/usage.go | 2 +- docs/manual.md | 28 ++- evals/README.md | 2 +- evals/cases/trigger-filter.sh | 9 + internal/config/build.go | 87 ++++---- internal/config/fields.go | 13 +- internal/config/runstate.go | 42 +++- internal/config/trigger_test.go | 1 + internal/core/configschema.go | 16 +- internal/core/fetch/consts.go | 1 + internal/core/fetch/providers.go | 23 +- internal/core/hint.go | 2 +- internal/core/ready/service.go | 7 +- internal/core/ready/trigger.go | 95 +++++++-- internal/core/ready/trigger_test.go | 197 ++++++++++++++---- internal/core/ready/wait_build.go | 10 +- internal/core/testdata/hints.golden | 2 +- internal/ready/filter_test.go | 79 +++++++ internal/ready/ready.go | 54 +++-- .../testdata/baseline/config.schema.json | 6 +- internal/trigger/trigger.go | 138 +++++++----- internal/trigger/trigger_test.go | 150 +++++++++---- scripts/size-baseline.txt | 10 +- skills/verdict/schemas/config.schema.json | 6 +- 25 files changed, 723 insertions(+), 259 deletions(-) diff --git a/.changeset/trigger-filter.md b/.changeset/trigger-filter.md index cb1eae9b1..0544f6401 100644 --- a/.changeset/trigger-filter.md +++ b/.changeset/trigger-filter.md @@ -3,3 +3,5 @@ --- `ready --filter` now gates the review on the closed trigger predicates in the trusted base configuration: matching PRs run and record the decision, non-matching PRs refuse `filtered-out` before any host call, and unusable policy or facts refuse `trigger-failed`. + +Fetch now requests and records PR labels in `PR.json` for every host, so the `labels` trigger predicate can read them from the bundle. diff --git a/cmd/verdict/usage.go b/cmd/verdict/usage.go index 1866cb01e..a652561e5 100644 --- a/cmd/verdict/usage.go +++ b/cmd/verdict/usage.go @@ -236,7 +236,7 @@ func usageText() string { " exit 1 with wait-timeout or condition-unmet data; no local writes.\n" + "\n" + " ready [--json] [--check] [--filter] [--wait-for build --timeout D]\n" + - " advisory only (exit 0, never a gate): CI state, open threads, draft/conflict,\n" + + " advisory by default (exit 0): CI state, open threads, draft/conflict,\n" + " description completeness. Prints blockers with actions + recommendation\n" + " (review/wait/nudge-author). GitHub and GitLab PR bundles; local bundles refuse\n" + " (no-pr-target) and other providers refuse typed (provider-unimplemented).\n" + diff --git a/docs/manual.md b/docs/manual.md index 2c521e354..d24fee951 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -579,7 +579,7 @@ the artifact, who writes it, and whether a verb gates on it. | Artifact | What it is | Who writes it | Gates? | | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `run.json` | The typed per-run capture: resolved defaults, per-file resolutions, plan, spend and privacy views, trusted-base inventory hashes, base SHA, the `inputs_sha256` digest, appended check observations, and the `trigger` decision from `ready --filter`. | `fetch`/`fetch-local`; `post` appends observations | Yes, except `config --preview` (see below). `post`, `record`, `triage`, `report`, `ready`, `config --explain`, and `verify --group` verify it; a run bundle deleted while its controls snapshot remains, or an edited captured input, refuses `controls-stale`; a bare bundle dir reads with a warning and no repository requirements. | +| `run.json` | The typed per-run capture: resolved defaults, per-file resolutions, plan, spend and privacy views, trusted-base inventory hashes, base SHA, the `inputs_sha256` digest, appended check observations, and the `trigger` decision from `ready --filter`. | `fetch`/`fetch-local`; `post` appends observations, `ready --filter` records the trigger decision | Yes, except `config --preview` (see below). `post`, `record`, `triage`, `report`, `ready`, `config --explain`, and `verify --group` verify it; a run bundle deleted while its controls snapshot remains, or an edited captured input, refuses `controls-stale`; a bare bundle dir reads with a warning and no repository requirements. | | `validation-.json` | The deep-validation receipt for one plan group: run, inputs digest, group, reviewed and execution heads, canonical root, command, exit, timeout and sandbox flags, status, and a redacted output tail. | `verify --group --command ` | Yes. `post` refuses a binding approval while any `depth: "deep"` group lacks a current receipt; `ready` reports the gap as `profile-work`. | | `filesource.json` | Provenance for the PR file list: `source` (`paged` or `cli`), `cli_cap` (100), `provider_ceiling` (3000), `count`, and `truncated` or `paging_error`. | GitHub `fetch`, when the CLI list arrives at its 100-file cap | No. It is an advisory coverage note: a paging failure warns and the run proceeds with the CLI's first 100 files. | | `full.raw.diff` | The raw pre-exclusion diff, kept as the patch-identity input when a filtered diff would otherwise replace it. No shipped verb writes it today: `fetch` writes the host diff to `full.diff` and records the filtered hash in `run.json` without writing filtered bytes. | Nothing today (a future exclusion pass) | Partly. When present it feeds the identity, and `identity.json`'s `raw_diff_sha256` must reproduce it; a present-but-unreadable file refuses instead of falling back to `full.diff`. Absence is normal. | @@ -981,15 +981,21 @@ predicates in the trusted `.verdict/config.json` snapshot the bundle fetched, not the worktree's current config. A configured predicate must match: `authors` (exact login), `paths_include`, `paths_exclude`, `labels`, `base_branches`, `title_matches` (Go regular expression), or `draft`. Lists OR within a -predicate and AND across predicates. The decision lands in `run.json` as -`trigger` (`run`, `reason`, `head`, `at`) and leaves the inputs digest alone. - -A match runs the ordinary readiness read. A miss refuses `filtered-out` with -exit code 1, naming the failing predicate before any host command runs; -without `--filter` the same PR is unchanged. Missing or malformed base policy, -facts, or write access refuse `trigger-failed`; an unknown key remains a -`bad-config` parse error naming `trigger.`. `--filter` composes with -`--check` and `--json`. +predicate and AND across predicates. Excluded paths are removed before +`paths_include` is tested, so one generated file cannot hide a protected +change. A truncated `filesource.json` skips the path predicates with a +warning instead of filtering out on partial evidence. The decision lands in +`run.json` as `trigger` (`run`, `reason`, `head`, `at`) and leaves the inputs +digest alone. + +The gate runs before any live read or `--wait-for build` poll. A match runs +the ordinary readiness read. A miss refuses `filtered-out` with exit code 1, +naming the failing predicate before any host command runs; without `--filter` +the same PR is unchanged. A head-policy snapshot, damaged snapshot, unreadable +facts, corrupt run receipt, or malformed base policy refuses `trigger-failed`; +an unknown key remains a `bad-config` parse error naming `trigger.`. An +absent base config means no trigger. `--filter` composes with `--check` +(the typed miss keeps exit 1) and `--json`. On GitLab the same blockers read through `glab`: the merge request carries `draft`, `has_conflicts` (plus a `conflict` `detailed_merge_status`), and the @@ -3450,7 +3456,7 @@ verdict fetch-local owner/repo slice-b -- # narrow to a fil `fetch --provider gitlab|gitcafe|forgejo|bitbucket|azure` builds the same bundle shape from another host (`PR.json` normalized to one schema: `provider`, -`number/title/body`, `base_ref/head_ref`, `base_sha/head_sha`, `author`, `url`, +`number/title/body`, `base_ref/head_ref`, `base_sha/head_sha`, `author`, `labels`, `url`, `state` as open|closed|merged, `draft`, per-file `additions/deletions`). `write_toc_roles` and the whole validate+merge path run unchanged — anchors validate against `full.diff` hunks regardless of host. `record`, `report`, diff --git a/evals/README.md b/evals/README.md index 7ab1bd269..77c580897 100644 --- a/evals/README.md +++ b/evals/README.md @@ -195,7 +195,7 @@ the build. | `threads-resolve-github` | Explicit GitHub thread resolve + receipt | A seeded GitHub bundle: `threads resolve --dry-run` lists the verdict-authored addressed thread and makes zero host calls; a real run makes exactly one literal `resolveReviewThread` call, marks the feed resolved, and writes `threads-resolve.json`; a second run skips `already-resolved` with no call; `--thread` on a human thread refuses `not-verdict-authored` and leaves the receipt untouched; calibrate's follow-up read joins the resolved thread to the addressed finding as `fixed`. | | `threads-resolve-gitlab` | Explicit GitLab thread resolve + receipt | A seeded GitLab bundle: `threads resolve --dry-run` lists the verdict-authored addressed discussion and makes zero host calls; a real run makes exactly one literal `glab api ... discussions/d1?resolved=true --method PUT` call, marks the feed resolved, and writes `threads-resolve.json`; a second run skips `already-resolved` with no call; `--thread` on a human discussion refuses `not-verdict-authored` and makes no call. | | `trend-incidents` | `trend --incidents` joins incidents to the stores | Three attached incidents over three bundles: 3 attached, 2 measured, 1 unmeasured (its `findings.json` is gone), 1 hit, 1 miss, `miss_rate: 0.5`, and per-bundle rows with a null `miss_rate` for the unmeasured bundle; the human line reads the literal `incidents: 3 attached, 2 measured, 1 unmeasured — missed 1 of 2 (50.0%)`; the unflagged run never mentions incidents; flipping the caught finding to `false-positive` re-measures 2 misses at `miss_rate: 1`; an absent or empty ledger reads a zero tally and a null `miss_rate`; MCP `trend` with `incidents: true` returns the same object as `--json`; zero host calls | -| `trigger-filter` | `ready --filter`: base-branch trigger gate | A producer-built PR bundle with a base-snapshot trigger: a PR touching `protected/**` runs and records `trigger.reason=matched`; a PR touching only docs refuses `filtered-out` naming `paths_include`, pays zero host calls, and records `run=false` beside that predicate; without `--filter` the same PR still reads live; unknown base keys fail `bad-config`. | +| `trigger-filter` | `ready --filter`: base-branch trigger gate | A producer-built PR bundle with a base-snapshot trigger: a PR touching `protected/**` runs and records `trigger.reason=matched`; a PR touching only docs refuses `filtered-out` naming `paths_include`, pays zero host calls, and records `run=false` beside that predicate; a typed miss keeps exit 1 under `--check`; without `--filter` the same PR still reads live. | | `upgrade` | Explicit `verdict upgrade` + `verdict uninstall` | Local loopback release server serves fake archives/checksums/bundle: installer self-replace updates the binary + receipt after SHA-256/signature/`--version` smoke; latest resolves through the redirect; checksum mismatch leaves the old binary; dry-run makes no request or write; npm delegation runs the exact argv, streams output, and propagates exit 7; project-local/skill/unidentifiable refuse typed with the exact command; uninstall removes only managed files (+ `--remove-path` marker) and refuses foreign channels; checksum-only warns/`VERDICT_REQUIRE_SIGNATURE=1` fails closed | | `usage-adapters` | Optional harness converters outside the binary | `claude-to-usage.sh` and `opencode-to-usage.sh` turn the checked-in transcripts into records that validate against `schemas/usage.schema.json`, preferring a cost-state or session total and deduping repeats; the Stop hook writes a validating usage file; a timestamp-less transcript refuses with nothing written; tokens without a price still convert unpriced; the adapters README documents both as optional; a missing jq is a SKIP, never a pass | | `usage-errors` | The binary owns usage errors (arity matrix, wins-over-faults) | No verb, missing positionals (one leg per verb; bare verbs never fail), and unknown verbs print the full usage block on stderr with exit 2 and no envelope; corrupt settings plus a missing positional still usage-errors (a complete invocation reads settings and refuses typed `bad-settings`); the block names `verdict` and keeps `--json`, the verify codes, and `token-scopes-excess` | diff --git a/evals/cases/trigger-filter.sh b/evals/cases/trigger-filter.sh index 5357b74f7..8286da442 100755 --- a/evals/cases/trigger-filter.sh +++ b/evals/cases/trigger-filter.sh @@ -62,6 +62,15 @@ assert_eq "the gate pays zero host calls" 0 "$(( $(wc -l <"$EVAL_STUBLOG" | tr - assert_file_grep "the receipt records the failing predicate" "$PR/run.json" '"reason": "paths_include"' assert_file_grep "the receipt records run false" "$PR/run.json" '"run": false' +mark="$(wc -l <"$EVAL_STUBLOG" | tr -d ' ')" +run_review ready acme/app 42 --filter --check --json +if [ "$EVAL_STATUS" -eq 1 ] && [ "$(jq -r '.error.code' "$EVAL_OUT")" = "filtered-out" ]; then + good "--check keeps the typed miss at exit 1" +else + bad "--check recoded the miss: exit $EVAL_STATUS $(head -c 200 "$EVAL_OUT")" +fi +assert_eq "the --check leg still pays zero host calls" 0 "$(( $(wc -l <"$EVAL_STUBLOG" | tr -d ' ') - mark ))" + mark="$(wc -l <"$EVAL_STUBLOG" | tr -d ' ')" run_review ready acme/app 42 --json assert_exit "without --filter the same PR is unchanged" 0 diff --git a/internal/config/build.go b/internal/config/build.go index 811148573..23e8b5733 100644 --- a/internal/config/build.go +++ b/internal/config/build.go @@ -28,29 +28,39 @@ func buildConfig(root map[string]any) (*core.Config, *core.RefusalError) { if schemaRef, ok := root["$schema"].(string); ok { cfg.SchemaRef = schemaRef } - var ref *core.RefusalError - if cfg.Defaults, ref = presentNested(root, defaultsKey, rootLocation, buildDefaults); ref != nil { + if ref := buildConfigCore(cfg, root); ref != nil { return nil, ref } - if cfg.Requirements, ref = presentNested(root, requirementsKey, rootLocation, buildRequirements); ref != nil { + if ref := buildConfigViews(cfg, root); ref != nil { return nil, ref } - if cfg.Trigger, ref = presentNested(root, "trigger", rootLocation, buildTrigger); ref != nil { - return nil, ref + return cfg, nil +} + +func buildConfigCore(cfg *core.Config, root map[string]any) *core.RefusalError { + var ref *core.RefusalError + if cfg.Defaults, ref = presentNested(root, defaultsKey, rootLocation, buildDefaults); ref != nil { + return ref } + if cfg.Requirements, ref = presentNested(root, requirementsKey, rootLocation, buildRequirements); ref != nil { + return ref + } + cfg.Trigger, ref = presentNested(root, "trigger", rootLocation, buildTrigger) + return ref +} + +func buildConfigViews(cfg *core.Config, root map[string]any) *core.RefusalError { + var ref *core.RefusalError if cfg.Index, ref = presentNested(root, "index", rootLocation, buildIndex); ref != nil { - return nil, ref + return ref } if cfg.SeverityWeights, ref = presentNested(root, "severity_weights", rootLocation, buildSeverityWeights); ref != nil { - return nil, ref + return ref } if cfg.RigorPolicy, ref = presentNested(root, "rigor_policy", rootLocation, buildRigorPolicy); ref != nil { - return nil, ref - } - if ref = buildConfigTail(cfg, root); ref != nil { - return nil, ref + return ref } - return cfg, nil + return buildConfigTail(cfg, root) } // buildConfigTail validates scopes, profiles, the default profile, and @@ -318,56 +328,63 @@ func buildRequirements(raw any, path string) (*core.ConfigRequirements, *core.Re return out, nil } -// triggerStrings reads one non-empty list of single-line predicate values. func triggerStrings(raw any, path string) ([]string, *core.RefusalError) { return panelMembers(raw, path, 1) } -// buildTrigger validates the closed trigger predicate set. Every list is -// non-empty when present, globs stay portable, and title_matches compiles -// here so a bad expression never reaches evaluation. func buildTrigger(raw any, path string) (*core.ConfigTrigger, *core.RefusalError) { obj, ref := asObject(raw, path) if ref != nil { return nil, ref } out := &core.ConfigTrigger{} - if out.Authors, ref = presentStrings(obj, "authors", path, triggerStrings); ref != nil { + if ref := buildTriggerStrings(obj, path, out); ref != nil { return nil, ref } - if out.PathsInclude, ref = presentStrings(obj, "paths_include", path, func(v any, at string) ([]string, *core.RefusalError) { - return globArray(v, at, 1) - }); ref != nil { + if ref := buildTriggerTail(obj, path, out); ref != nil { return nil, ref } - if out.PathsExclude, ref = presentStrings(obj, "paths_exclude", path, func(v any, at string) ([]string, *core.RefusalError) { - return globArray(v, at, 1) - }); ref != nil { - return nil, ref + return out, nil +} + +func buildTriggerStrings(obj map[string]any, path string, out *core.ConfigTrigger) *core.RefusalError { + var ref *core.RefusalError + if out.Authors, ref = presentStrings(obj, "authors", path, triggerStrings); ref != nil { + return ref } - if out.Labels, ref = presentStrings(obj, "labels", path, triggerStrings); ref != nil { - return nil, ref + if out.PathsInclude, ref = presentStrings(obj, "paths_include", path, globArrayOfOne); ref != nil { + return ref } - if out.BaseBranches, ref = presentStrings(obj, "base_branches", path, triggerStrings); ref != nil { - return nil, ref + if out.PathsExclude, ref = presentStrings(obj, "paths_exclude", path, globArrayOfOne); ref != nil { + return ref + } + if out.Labels, ref = presentStrings(obj, "labels", path, triggerStrings); ref != nil { + return ref } + out.BaseBranches, ref = presentStrings(obj, "base_branches", path, globArrayOfOne) + return ref +} + +func globArrayOfOne(raw any, at string) ([]string, *core.RefusalError) { + return globArray(raw, at, 1) +} + +func buildTriggerTail(obj map[string]any, path string, out *core.ConfigTrigger) *core.RefusalError { title, ref := presentValue(obj, "title_matches", path, nonEmptyString) if ref != nil { - return nil, ref + return ref } if title != nil { if ref := checkSingleLine(*title, path+".title_matches"); ref != nil { - return nil, ref + return ref } if _, err := regexp.Compile(*title); err != nil { - return nil, refuse(CodeBadConfig, "%s.title_matches: invalid regexp: %v", path, err) + return refuse(CodeBadConfig, "%s.title_matches: invalid regexp: %v", path, err) } out.TitleMatches = *title } - if out.Draft, ref = presentValue(obj, "draft", path, asBool); ref != nil { - return nil, ref - } - return out, nil + out.Draft, ref = presentValue(obj, "draft", path, asBool) + return ref } // buildClauseList validates an optional non-empty clause array. Absent diff --git a/internal/config/fields.go b/internal/config/fields.go index 90458343b..93b2f6a09 100644 --- a/internal/config/fields.go +++ b/internal/config/fields.go @@ -1,6 +1,8 @@ package config import ( + "strings" + "github.com/stainless-code/verdict/internal/core" ) @@ -238,6 +240,15 @@ func checkProfileUnknown(raw any, path string) *core.RefusalError { return checkKeys(path, prof, profileFields) } +const unknownFieldSuffix = ": unknown field" + +// IsUnknownField reports whether ref is the strict parser's unknown-key +// refusal. The trigger gate keeps that code verbatim and wraps every other +// parse failure as a trigger failure. +func IsUnknownField(ref *core.RefusalError) bool { + return ref != nil && ref.Code == CodeBadConfig && strings.HasSuffix(ref.Message, unknownFieldSuffix) +} + // checkKeys refuses the first key outside the allowed set, naming its path. func checkKeys(path string, obj map[string]any, allowed []string) *core.RefusalError { keep := make(map[string]bool, len(allowed)) @@ -246,7 +257,7 @@ func checkKeys(path string, obj map[string]any, allowed []string) *core.RefusalE } for _, key := range sortedObjectKeys(obj) { if !keep[key] { - return refuse(CodeBadConfig, "%s.%s: unknown field", path, key) + return refuse(CodeBadConfig, "%s.%s%s", path, key, unknownFieldSuffix) } } return nil diff --git a/internal/config/runstate.go b/internal/config/runstate.go index b82facbb5..1d386eb56 100644 --- a/internal/config/runstate.go +++ b/internal/config/runstate.go @@ -12,6 +12,7 @@ import ( "github.com/stainless-code/verdict/internal/atomic" "github.com/stainless-code/verdict/internal/controls" "github.com/stainless-code/verdict/internal/core" + "github.com/stainless-code/verdict/internal/storelock" ) // RunState is the persisted per-run typed bundle: the fetch-time capture @@ -48,8 +49,8 @@ type RunState struct { type TriggerDecision struct { At string `json:"at"` Head string `json:"head"` - Run bool `json:"run"` Reason string `json:"reason"` + Run bool `json:"run"` } // StatusUsed marks a snapshot entry whose bytes were captured. @@ -76,7 +77,7 @@ func WriteRunState(dir string, rs *RunState) *stateError { return &stateError{msg: "could not encode run state for " + rs.Name, cause: err} } path := filepath.Join(dir, runStateFileName) - if _, serr := confineBundleDir(dir); serr != nil { + if serr := confineBundleDir(dir); serr != nil { return serr } if err := os.MkdirAll(dir, stateDirPerm); err != nil { @@ -91,10 +92,10 @@ func WriteRunState(dir string, rs *RunState) *stateError { // confineBundleDir proves the bundle directory stays under a repository // state area: the path must resolve inside a `.verdict/state` parent, so // a caller-supplied bundle path can never escape to an unrelated target. -func confineBundleDir(dir string) (string, *stateError) { +func confineBundleDir(dir string) *stateError { abs, err := filepath.Abs(dir) if err != nil { - return "", &stateError{msg: "could not resolve bundle directory " + dir, cause: err} + return &stateError{msg: "could not resolve bundle directory " + dir, cause: err} } resolvedDir, err := filepath.EvalSymlinks(filepath.Dir(abs)) if err == nil { @@ -104,9 +105,9 @@ func confineBundleDir(dir string) (string, *stateError) { grandparent := filepath.Base(filepath.Dir(filepath.Dir(abs))) if parent != StateDirName || grandparent != VerdictDir { outside := filepath.Join(VerdictDir, StateDirName) - return "", &stateError{msg: "refuses to write run state outside " + outside + ": " + dir, operator: true} + return &stateError{msg: "refuses to write run state outside " + outside + ": " + dir, operator: true} } - return abs, nil + return nil } // ifEmpty defaults an empty root to the working directory, matching @@ -149,6 +150,14 @@ func ReadRunState(dir string) (*RunState, bool, *stateError) { // never captured. The error return is the sealed state error as an // error so verb packages can warn without naming the type. func AppendOutcome(dir string, obs *CheckObservation) (bool, error) { + if _, serr := os.Stat(dir); os.IsNotExist(serr) { + return false, nil + } + unlock, err := lockRunState(dir) + if err != nil { + return false, err + } + defer unlock() rs, found, serr := ReadRunState(dir) if serr != nil { return false, serr @@ -168,10 +177,31 @@ func AppendOutcome(dir string, obs *CheckObservation) (bool, error) { return true, nil } +// lockRunState keeps concurrent ledger appends from overwriting each other. +func lockRunState(dir string) (func(), error) { + if serr := confineBundleDir(dir); serr != nil { + return nil, serr + } + path := filepath.Join(dir, runStateFileName) + unlock, err := storelock.Acquire(path, storelock.DefaultWait) + if err != nil { + return nil, &stateError{msg: "could not lock " + path, cause: err} + } + return unlock, nil +} + // AppendTrigger records one trigger decision on an existing run bundle. // Like an outcome, it is an observation rather than a captured input, so the // inputs digest stays stable. Absent run state appends nothing. func AppendTrigger(dir string, decision *TriggerDecision) (bool, error) { + if _, serr := os.Stat(dir); os.IsNotExist(serr) { + return false, nil + } + unlock, err := lockRunState(dir) + if err != nil { + return false, err + } + defer unlock() rs, found, serr := ReadRunState(dir) if serr != nil { return false, serr diff --git a/internal/config/trigger_test.go b/internal/config/trigger_test.go index 0ef59294d..977e0f408 100644 --- a/internal/config/trigger_test.go +++ b/internal/config/trigger_test.go @@ -44,6 +44,7 @@ func TestParseTriggerRefusals(t *testing.T) { {"unknown key", `{"schema_version":"1","trigger":{"bogus":1}}`, "$.trigger.bogus: unknown field"}, {"bad regexp", `{"schema_version":"1","trigger":{"title_matches":"("}}`, "$.trigger.title_matches"}, {"absolute glob", `{"schema_version":"1","trigger":{"paths_include":["/etc/**"]}}`, "absolute"}, + {"absolute base glob", `{"schema_version":"1","trigger":{"base_branches":["/main"]}}`, "absolute"}, {"wrong draft type", `{"schema_version":"1","trigger":{"draft":"yes"}}`, "$.trigger.draft"}, {"empty authors", `{"schema_version":"1","trigger":{"authors":[]}}`, "$.trigger.authors"}, {"null trigger", `{"schema_version":"1","trigger":null}`, "$.trigger"}, diff --git a/internal/core/configschema.go b/internal/core/configschema.go index ba0779591..87939e1ab 100644 --- a/internal/core/configschema.go +++ b/internal/core/configschema.go @@ -18,7 +18,7 @@ type Config struct { //nolint:govet // field order is the published property ord SchemaVersion string `const:"1" json:"schema_version"` Defaults *ConfigDefaults `json:"defaults,omitempty" nonnull:"true"` Requirements *ConfigRequirements `json:"requirements,omitempty" nonnull:"true"` - Trigger *ConfigTrigger `json:"trigger,omitempty" nonnull:"true"` + Trigger *ConfigTrigger `json:"trigger,omitempty" nonnull:"true"` Scopes []ConfigScope `json:"scopes,omitempty"` Profiles map[string]ConfigProfile `json:"profiles,omitempty"` DefaultProfile string `json:"default_profile,omitempty" nonnull:"true"` @@ -81,13 +81,13 @@ type ConfigRequirements struct { //nolint:govet // field order is the published // within a predicate and AND across predicates; an absent section runs every // PR. All values come from the trusted base configuration, never the PR head. type ConfigTrigger struct { - Authors []string `json:"authors,omitempty" minItems:"1" nonnull:"true"` - PathsInclude []string `json:"paths_include,omitempty" minItems:"1" nonnull:"true"` - PathsExclude []string `json:"paths_exclude,omitempty" minItems:"1" nonnull:"true"` - Labels []string `json:"labels,omitempty" minItems:"1" nonnull:"true"` - TitleMatches string `json:"title_matches,omitempty" nonnull:"true"` - BaseBranches []string `json:"base_branches,omitempty" minItems:"1" nonnull:"true"` - Draft *bool `json:"draft,omitempty" nonnull:"true"` + Draft *bool `json:"draft,omitempty" nonnull:"true"` + TitleMatches string `json:"title_matches,omitempty" nonnull:"true"` + Authors []string `json:"authors,omitempty" minItems:"1" nonnull:"true"` + PathsInclude []string `json:"paths_include,omitempty" minItems:"1" nonnull:"true"` + PathsExclude []string `json:"paths_exclude,omitempty" minItems:"1" nonnull:"true"` + Labels []string `json:"labels,omitempty" minItems:"1" nonnull:"true"` + BaseBranches []string `json:"base_branches,omitempty" minItems:"1" nonnull:"true"` } // BlockClause is one per-source, per-path blocking predicate: an open diff --git a/internal/core/fetch/consts.go b/internal/core/fetch/consts.go index 7278b4f39..3e7a8793b 100644 --- a/internal/core/fetch/consts.go +++ b/internal/core/fetch/consts.go @@ -29,6 +29,7 @@ const ( keyKind = "kind" keyLabels = "labels" keyLogin = "login" + keyName = "name" keyNumber = "number" keyPath = "path" keyPR = "pr" diff --git a/internal/core/fetch/providers.go b/internal/core/fetch/providers.go index a3b657c93..e47b9ed9f 100644 --- a/internal/core/fetch/providers.go +++ b/internal/core/fetch/providers.go @@ -1936,19 +1936,24 @@ func buildPR(spec *prSpec) *jq.Obj { func prLabels(raw any) []any { var out []any for _, entry := range jq.Elements(raw) { - if name, ok := entry.(string); ok && name != "" { + if name, ok := hostLabel(entry); ok { out = append(out, name) - continue - } - if m, ok := jq.AsMap(entry); ok { - if name := normStr(m, "name"); name != "" { - out = append(out, name) - } } } return out } +func hostLabel(entry any) (string, bool) { + if name, ok := entry.(string); ok { + return name, name != "" + } + if m, ok := jq.AsMap(entry); ok { + name := normStr(m, keyName) + return name, name != "" + } + return "", false +} + func prFileRow(path string, status, additions, deletions any) *jq.Obj { return jq.NewObj(keyPath, path, keyStatus, status, keyAdditions, additions, keyDeletions, deletions) @@ -2011,7 +2016,7 @@ func normalizeGitLab(metaRaw, changesRaw string, ns map[string]map[string]int64) Title: normStr(meta, keyTitle), Body: normStr(meta, keyDescription), BaseRef: normStr(meta, "target_branch"), HeadRef: normStr(meta, "source_branch"), BaseSHA: normStr(diffRefs, keyBaseSHA, "start_sha"), HeadSHA: headSHA, - Author: normStr(nestedMap(meta, keyAuthor), "username", "name"), + Author: normStr(nestedMap(meta, keyAuthor), "username", keyName), Labels: prLabels(meta[keyLabels]), URL: normStr(meta, "web_url"), State: providerState(providerGitLab, normStr(meta, keyState)), Draft: meta[keyDraft], Files: gitlabFileRows(changesRaw, ns), @@ -2215,7 +2220,7 @@ func normalizeBitbucket(metaRaw string, ds []any) (*jq.Obj, error) { return buildPR(&prSpec{ Provider: providerBitbucket, Number: meta[keyID], Title: normStr(meta, keyTitle), Body: body, - BaseRef: normStr(nestedMap(dest, modeBranch), "name"), HeadRef: normStr(nestedMap(src, modeBranch), "name"), + BaseRef: normStr(nestedMap(dest, modeBranch), keyName), HeadRef: normStr(nestedMap(src, modeBranch), keyName), BaseSHA: normStr(nestedMap(dest, "commit"), "hash"), HeadSHA: normStr(nestedMap(src, "commit"), "hash"), // The key chain stops at the first string (even ""), so nickname is // authoritative and display_name never wins. diff --git a/internal/core/hint.go b/internal/core/hint.go index bbfd0b54f..b4b50c5dc 100644 --- a/internal/core/hint.go +++ b/internal/core/hint.go @@ -48,7 +48,7 @@ var hintRules = []HintRule{ "or pass --allow-secret-values to publish it anyway"}, {codes: []string{"fix-patch-secret"}, hint: "try: remove the value from the worktree (rotate it " + "if it was real), then re-run fix"}, - {codes: []string{"filtered-out"}, hint: "try: remove --filter to review every PR, or change the trigger in .verdict/config.json at the PR base"}, + {codes: []string{"filtered-out"}, hint: "try: remove --filter to review every PR, or change the trigger at the PR base and re-fetch"}, {codes: []string{"trigger-failed"}, hint: "try: re-fetch the bundle so its base-snapshot policy is complete, then re-run"}, { codes: []string{"diff-too-large"}, diff --git a/internal/core/ready/service.go b/internal/core/ready/service.go index cf14eeb6d..b321c43e7 100644 --- a/internal/core/ready/service.go +++ b/internal/core/ready/service.go @@ -243,15 +243,10 @@ func loadState(ctx context.Context, st *state) *core.RefusalError { } if !isRegular(dir + "/PR.json") { return st.withWarnings(&core.RefusalError{ - Code: "missing-bundle", + Code: codeMissingBundle, Message: "missing " + dir + "/PR.json (run fetch " + st.repo + " " + st.pr + " first)", }) } - if st.req.Filter { - if ref := st.filterTrigger(dir); ref != nil { - return ref - } - } if ref := host.live(ctx, st, dir); ref != nil { return ref } diff --git a/internal/core/ready/trigger.go b/internal/core/ready/trigger.go index 38cdb821a..a7eaa1737 100644 --- a/internal/core/ready/trigger.go +++ b/internal/core/ready/trigger.go @@ -11,13 +11,44 @@ import ( ) const ( - codeFilteredOut = "filtered-out" - codeTriggerFail = "trigger-failed" + codeFilteredOut = "filtered-out" + codeTriggerFail = "trigger-failed" + codeMissingBundle = "missing-bundle" ) -// filterTrigger evaluates the trusted-base trigger before any live host -// read, and records both outcomes in the bundle's run receipt. The policy -// bytes come from the base snapshot the fetch captured, never the worktree. +func filterGate(req *Request) *core.RefusalError { + if !req.Filter { + return nil + } + st := &state{ + req: req, + repo: req.Repo, + pr: req.Pr, + warnings: append([]string{}, req.Warnings...), + } + dir, ref := readcommon.ResolveBundleDir(req.RepoRootPath(), req.Pr) + if ref != nil { + return st.withWarnings(ref) + } + if isLocalDir(dir) { + return st.withWarnings(&core.RefusalError{ + Code: codeNoPRTarget, + Message: "ready needs a PR bundle (fetch owner/repo N first); local bundles have no trigger policy", + }) + } + if !isRegular(dir + "/PR.json") { + return st.withWarnings(&core.RefusalError{ + Code: codeMissingBundle, + Message: "missing " + dir + "/PR.json (run fetch " + st.repo + " " + st.pr + " first)", + }) + } + if ref := st.filterTrigger(dir); ref != nil { + return ref + } + req.Warnings = st.warnings + return nil +} + func (st *state) filterTrigger(dir string) *core.RefusalError { predicates, ref := baseTrigger(dir) if ref != nil { @@ -25,9 +56,15 @@ func (st *state) filterTrigger(dir string) *core.RefusalError { } facts, err := trigger.ReadFacts(dir) if err != nil { - return st.withWarnings(&core.RefusalError{Code: codeTriggerFail, Message: err.Error()}) + return st.withWarnings(triggerFailure("could not read trigger facts", err)) + } + if facts.IncompletePaths { + st.warn("trigger file list is incomplete — path predicates skipped, proceeding with review") } - run, reason := trigger.Evaluate(facts, predicates) + if _, ref := config.VerifyRunState(dir); ref != nil { + return st.withWarnings(triggerFailure("could not verify the run receipt", ref)) + } + run, reason := trigger.Evaluate(&facts, predicates) decision := &config.TriggerDecision{ At: st.req.TimeUTC(), Head: readcommon.OpenBundle(dir).Head(), @@ -36,10 +73,7 @@ func (st *state) filterTrigger(dir string) *core.RefusalError { } written, err := config.AppendTrigger(dir, decision) if err != nil || !written { - return st.withWarnings(&core.RefusalError{ - Code: codeTriggerFail, - Message: "could not record the trigger decision in " + dir + "/run.json (re-fetch the bundle)", - }) + return st.withWarnings(triggerFailure("could not record the trigger decision in "+dir+"/run.json (re-fetch)", err)) } if !run { return st.withWarnings(&core.RefusalError{ @@ -50,24 +84,47 @@ func (st *state) filterTrigger(dir string) *core.RefusalError { return nil } +func triggerFailure(what string, cause error) *core.RefusalError { + if cause == nil { + return &core.RefusalError{Code: codeTriggerFail, Message: what} + } + return &core.RefusalError{Code: codeTriggerFail, Message: what + ": " + cause.Error()} +} + func baseTrigger(dir string) (*core.ConfigTrigger, *core.RefusalError) { - snap, found, ref := controls.GateRecordedPath(dir, config.ConfigPath) + snap, ref := controls.GateRecorded(dir) if ref != nil { + return nil, triggerFailure("could not verify the recorded base snapshot", ref) + } + if ref := checkBasePolicy(snap); ref != nil { return nil, ref } - if !found { + entry := snap.Entry(config.ConfigPath) + if entry == nil || entry.Status != config.StatusUsed { return nil, nil } raw, ok := snap.ReadFile(config.ConfigPath) if !ok { - return nil, &core.RefusalError{ - Code: codeTriggerFail, - Message: "base snapshot has no readable " + config.ConfigPath + " (re-fetch the bundle)", - } + return nil, triggerFailure("base snapshot has no readable "+config.ConfigPath+" (re-fetch)", nil) } + return parseBaseTrigger(raw) +} + +func checkBasePolicy(snap *controls.Snapshot) *core.RefusalError { + if snap.File.Policy != controls.PolicyBase || snap.File.SHA == "" || + snap.File.SHA != snap.File.BaseSHA { + return triggerFailure("trigger needs a base-policy snapshot (re-fetch without --allow-head-controls)", nil) + } + return nil +} + +func parseBaseTrigger(raw []byte) (*core.ConfigTrigger, *core.RefusalError) { cfg, ref := config.Parse(raw) - if ref != nil { + if ref == nil { + return cfg.Trigger, nil + } + if config.IsUnknownField(ref) { return nil, ref } - return cfg.Trigger, nil + return nil, triggerFailure("could not parse the base "+config.ConfigPath, ref) } diff --git a/internal/core/ready/trigger_test.go b/internal/core/ready/trigger_test.go index e1666284f..2c74882be 100644 --- a/internal/core/ready/trigger_test.go +++ b/internal/core/ready/trigger_test.go @@ -2,50 +2,84 @@ package readycore_test import ( "context" + "os" + "os/exec" "path/filepath" "strings" "testing" + "time" "github.com/stainless-code/verdict/internal/config" "github.com/stainless-code/verdict/internal/controls" "github.com/stainless-code/verdict/internal/core" + coreexec "github.com/stainless-code/verdict/internal/core/exec" readycore "github.com/stainless-code/verdict/internal/core/ready" + "github.com/stainless-code/verdict/internal/testenv" ) -func writeTriggerBundle(t *testing.T, cfg string) (string, string) { +const triggerHead = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + +func fixtureGit(t *testing.T, dir string, args ...string) string { t.Helper() - rs := &config.RunState{ - BaseSHA: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", - Trusted: true, - } - dir := writeReadyBundle(t, rs) - write(t, filepath.Join(".", ".verdict", "config.json"), cfg) - write(t, filepath.Join(dir, "PR.json"), `{"provider":"github","number":5201,"title":"fix: retry",`+ - `"baseRefName":"main","isDraft":false,"author":{"login":"ana"},"labels":[{"name":"deps"}],`+ - `"files":[{"path":"src/retry.go"}],"headRefOid":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}`) - if _, ref := controls.Resolve(t.Context(), nil, &controls.Request{ - BundleDir: dir, Root: ".", BaseSHA: rs.BaseSHA, AllowHead: true, + cmd := exec.CommandContext(t.Context(), "git", args...) + cmd.Dir = dir + cmd.Env = testenv.Base(nil) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } + return strings.TrimSpace(string(out)) +} + +// writeTriggerBundle produces a real PR bundle whose snapshot was resolved +// from a committed base, then drifts the worktree config to prove the base +// bytes are the policy the gate reads. +func writeTriggerBundle(t *testing.T, cfg string) (bundle, root, base string) { + t.Helper() + root = t.TempDir() + t.Chdir(root) + mkdir(t, filepath.Join(root, ".verdict")) + write(t, filepath.Join(root, ".verdict", "config.json"), cfg) + fixtureGit(t, root, "init", "-q") + fixtureGit(t, root, "add", ".verdict/config.json") + fixtureGit(t, root, "commit", "-qm", "base") + base = fixtureGit(t, root, "rev-parse", "HEAD") + + bundle = filepath.Join(root, ".verdict", "state", "5201") + mkdir(t, bundle) + write(t, filepath.Join(bundle, "PR.json"), `{"provider":"github","number":5201,`+ + `"title":"fix: retry","baseRefName":"main","head_sha":"`+triggerHead+`",`+ + `"isDraft":false,"author":{"login":"ana"},"labels":[{"name":"deps"}],`+ + `"files":[{"path":"src/retry.go"}],"headRefOid":"`+triggerHead+`",`+ + `"baseRefOid":"`+base+`"}`) + rs := &config.RunState{BaseSHA: base, Trusted: true} + if err := config.WriteRunState(bundle, rs); err != nil { + t.Fatalf("WriteRunState: %v", err) + } + if _, ref := controls.Resolve(t.Context(), coreexec.Runner{}, &controls.Request{ + BundleDir: bundle, Root: root, BaseSHA: base, }); ref != nil { t.Fatalf("controls.Resolve: %v", ref) } - return dir, rs.BaseSHA + write(t, filepath.Join(root, ".verdict", "config.json"), + `{"schema_version":"1","trigger":{"paths_include":["never/**"]}}`) + return bundle, root, base } -func triggerRequest(bundle string) *readycore.Request { +func triggerRequest(root string) *readycore.Request { req := &readycore.Request{Filter: true} req.ReadRequest = core.ReadRequest{ - Runtime: &core.Runtime{Commander: &readyCommander{head: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}}, - Repo: "acme/app", Pr: "5201", + Runtime: &core.Runtime{Commander: &readyCommander{head: triggerHead}}, + RepoRoot: root, + Repo: "acme/app", Pr: "5201", } return req } func TestReadyFilterRunsMatchingPR(t *testing.T) { cfg := `{"schema_version":"1","trigger":{"paths_include":["src/**"],"authors":["ana"]}}` - dir, _ := writeTriggerBundle(t, cfg) - write(t, filepath.Join(".", ".verdict", "config.json"), - `{"schema_version":"1","trigger":{"paths_include":["never/**"]}}`) - req := triggerRequest(dir) + dir, root, _ := writeTriggerBundle(t, cfg) + req := triggerRequest(root) resp, ref := readycore.Run(context.Background(), req) if ref != nil { t.Fatalf("matching PR refused: %v", ref) @@ -53,21 +87,32 @@ func TestReadyFilterRunsMatchingPR(t *testing.T) { if resp.Data == nil || !resp.Data.Ready { t.Fatalf("matching PR did not reach readiness: %+v", resp.Data) } + assertTriggerReceipt(t, dir, true, "matched") +} + +func assertTriggerReceipt(t *testing.T, dir string, wantRun bool, wantReason string) { + t.Helper() stored, found, serr := config.ReadRunState(dir) if serr != nil || !found || stored.Trigger == nil { t.Fatalf("run receipt has no trigger decision (found=%v err=%v)", found, serr) } - if !stored.Trigger.Run || stored.Trigger.Reason != "matched" { - t.Errorf("receipt = %+v, want run/matched", stored.Trigger) + if stored.Trigger.Run != wantRun || stored.Trigger.Reason != wantReason { + t.Errorf("receipt = %+v, want run=%v/%s", stored.Trigger, wantRun, wantReason) + } + if stored.Trigger.Head != triggerHead || stored.Trigger.At == "" { + t.Errorf("receipt = %+v, want the bundle head and an invocation time", stored.Trigger) + } + if _, ref := config.VerifyRunState(dir); ref != nil { + t.Errorf("the appended decision invalidated the run state: %v", ref) } } func TestReadyFilterRefusesFilteredOutPR(t *testing.T) { cfg := `{"schema_version":"1","trigger":{"paths_include":["protected/**"]}}` - dir, _ := writeTriggerBundle(t, cfg) - cmdr := &countingCommander{readyCommander: readyCommander{head: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}} - req := triggerRequest(dir) - req.Runtime.Commander = cmdr + dir, root, _ := writeTriggerBundle(t, cfg) + cmdr := &countingCommander{head: triggerHead} + req := triggerRequest(root) + req.Commander = cmdr _, ref := readycore.Run(context.Background(), req) if ref == nil || ref.Code != "filtered-out" { t.Fatalf("refusal = %+v, want filtered-out", ref) @@ -78,24 +123,106 @@ func TestReadyFilterRefusesFilteredOutPR(t *testing.T) { if cmdr.calls != 0 { t.Errorf("host reads after filter = %d, want 0", cmdr.calls) } - stored, found, serr := config.ReadRunState(dir) - if serr != nil || !found || stored.Trigger == nil { - t.Fatalf("run receipt has no trigger decision (found=%v err=%v)", found, serr) - } - if stored.Trigger.Run || stored.Trigger.Reason != "paths_include" { - t.Errorf("receipt = %+v, want filtered/paths_include", stored.Trigger) - } + assertTriggerReceipt(t, dir, false, "paths_include") } func TestReadyFilterRefusesUnknownBaseConfigKey(t *testing.T) { cfg := `{"schema_version":"1","trigger":{"bogus":true}}` - dir, _ := writeTriggerBundle(t, cfg) - _, ref := readycore.Run(context.Background(), triggerRequest(dir)) + _, root, _ := writeTriggerBundle(t, cfg) + _, ref := readycore.Run(context.Background(), triggerRequest(root)) if ref == nil || ref.Code != config.CodeBadConfig || !strings.Contains(ref.Message, "trigger.bogus") { t.Fatalf("refusal = %+v, want bad-config naming trigger.bogus", ref) } } +func TestReadyFilterRefusesHeadPolicySnapshot(t *testing.T) { + cfg := `{"schema_version":"1","trigger":{"paths_include":["src/**"]}}` + dir, root, base := writeTriggerBundle(t, cfg) + // The PR's own worktree is never the trusted base, even when a fetch + // explicitly allowed a head snapshot. + if _, ref := controls.Resolve(t.Context(), coreexec.Runner{}, &controls.Request{ + BundleDir: dir, Root: root, BaseSHA: base, AllowHead: true, + }); ref != nil { + t.Fatalf("head controls: %v", ref) + } + _, ref := readycore.Run(context.Background(), triggerRequest(root)) + if ref == nil || ref.Code != "trigger-failed" { + t.Fatalf("refusal = %+v, want trigger-failed for a head snapshot", ref) + } +} + +func TestReadyFilterRefusesMissingSnapshot(t *testing.T) { + cfg := `{"schema_version":"1","trigger":{"paths_include":["src/**"]}}` + dir, root, _ := writeTriggerBundle(t, cfg) + if err := os.Remove(filepath.Join(dir, "controls.json")); err != nil { + t.Fatal(err) + } + _, ref := readycore.Run(context.Background(), triggerRequest(root)) + if ref == nil || ref.Code != "trigger-failed" { + t.Fatalf("refusal = %+v, want trigger-failed for a missing snapshot", ref) + } +} + +func TestReadyFilterRefusesMalformedBaseConfig(t *testing.T) { + _, root, _ := writeTriggerBundle(t, `{"schema_version":"1","trigger":{"title_matches":"["}}`) + _, ref := readycore.Run(context.Background(), triggerRequest(root)) + if ref == nil || ref.Code != "trigger-failed" { + t.Fatalf("refusal = %+v, want trigger-failed for a bad regexp", ref) + } +} + +func TestReadyFilterRefusesUnreadableFacts(t *testing.T) { + dir, root, _ := writeTriggerBundle(t, `{"schema_version":"1","trigger":{"paths_include":["src/**"]}}`) + write(t, filepath.Join(dir, "PR.json"), `{"files":[`) + _, ref := readycore.Run(context.Background(), triggerRequest(root)) + if ref == nil || ref.Code != "trigger-failed" { + t.Fatalf("refusal = %+v, want trigger-failed for corrupt PR.json", ref) + } +} + +func TestReadyFilterWaitsOnlyAfterTheGate(t *testing.T) { + cfg := `{"schema_version":"1","trigger":{"paths_include":["protected/**"]}}` + _, root, _ := writeTriggerBundle(t, cfg) + cmdr := &countingCommander{head: triggerHead} + req := triggerRequest(root) + req.Commander = cmdr + req.WaitFor = "build" + req.Timeout = "1s" + req.PollRuntime = readycore.PollRuntime{ + Now: time.Now, + Sleep: func(_ context.Context, _ time.Duration) error { return nil }, + Jitter: func(d time.Duration) time.Duration { return d }, + } + _, ref := readycore.Run(context.Background(), req) + if ref == nil || ref.Code != "filtered-out" { + t.Fatalf("refusal = %+v, want filtered-out before the wait", ref) + } + if cmdr.calls != 0 { + t.Errorf("host calls before the gate = %d, want 0", cmdr.calls) + } +} + +func TestReadyFilterWarnsOnIncompleteFileList(t *testing.T) { + cfg := `{"schema_version":"1","trigger":{"paths_include":["src/**"]}}` + dir, root, _ := writeTriggerBundle(t, cfg) + write(t, filepath.Join(dir, "filesource.json"), + `{"source":"cli","cli_cap":100,"count":100,"truncated":true}`) + req := triggerRequest(root) + resp, ref := readycore.Run(context.Background(), req) + if ref != nil || resp.Data == nil || !resp.Data.Ready { + t.Fatalf("incomplete file list should run the review: resp=%+v ref=%v", resp, ref) + } + found := false + for _, w := range resp.Warnings { + if strings.Contains(w, "incomplete") { + found = true + } + } + if !found { + t.Errorf("warnings = %q, want an incomplete-file-list warning", resp.Warnings) + } +} + type countingCommander struct { readyCommander calls int diff --git a/internal/core/ready/wait_build.go b/internal/core/ready/wait_build.go index d9a163299..930b818b1 100644 --- a/internal/core/ready/wait_build.go +++ b/internal/core/ready/wait_build.go @@ -10,6 +10,14 @@ import ( // Run executes readiness, optionally waiting for CI under one hard deadline. func Run(ctx context.Context, req *Request) (*Response, *core.RefusalError) { + if err := ctx.Err(); err != nil { + return nil, &core.RefusalError{Code: codeCanceled, Message: "canceled before ready completed"} + } + // The gate precedes the wait: a filtered-out PR must never spend a live + // poll, and a wait that times out must not skip the decision receipt. + if ref := filterGate(req); ref != nil { + return nil, ref + } if req.WaitFor == "" && req.Timeout == "" { return runNow(ctx, req) } @@ -95,7 +103,7 @@ func readyWaitBundle(req *Request) *core.RefusalError { return &core.RefusalError{Code: codeNoPRTarget, Message: "ready build waits require a PR bundle"} } if !isRegular(dir + "/PR.json") { - return &core.RefusalError{Code: "missing-bundle", Message: "ready build waits require a fetched PR bundle"} + return &core.RefusalError{Code: codeMissingBundle, Message: "ready build waits require a fetched PR bundle"} } return nil } diff --git a/internal/core/testdata/hints.golden b/internal/core/testdata/hints.golden index 7b11ab40b..91af1898c 100644 --- a/internal/core/testdata/hints.golden +++ b/internal/core/testdata/hints.golden @@ -95,7 +95,7 @@ diff-too-large try: split the range (--range/--base/--base-sha) or narrow with a download-failed try: address the cause above, then re-run the same command duplicate-payload try: re-run with --force to repost the identical payload — re-fetch if the earlier review was deleted fetch-failed try: run 'gh auth status', confirm the repo/PR spelling, then re-run -filtered-out try: remove --filter to review every PR, or change the trigger in .verdict/config.json at the PR base +filtered-out try: remove --filter to review every PR, or change the trigger at the PR base and re-fetch fix-ceiling try: triage the sig or split the fix — the 3-pass ceiling is hard-coded fix-failed try: address the cause above, then re-run the same command fix-patch-secret try: remove the value from the worktree (rotate it if it was real), then re-run fix diff --git a/internal/ready/filter_test.go b/internal/ready/filter_test.go index 4abac539a..94fd312f8 100644 --- a/internal/ready/filter_test.go +++ b/internal/ready/filter_test.go @@ -1,9 +1,20 @@ package ready import ( + "bytes" + "os" + "os/exec" + "path/filepath" + "strings" "testing" + "time" + "github.com/stainless-code/verdict/internal/cli" + "github.com/stainless-code/verdict/internal/config" + "github.com/stainless-code/verdict/internal/controls" + coreexec "github.com/stainless-code/verdict/internal/core/exec" readycore "github.com/stainless-code/verdict/internal/core/ready" + "github.com/stainless-code/verdict/internal/testenv" ) func TestParseFilterFlag(t *testing.T) { @@ -15,3 +26,71 @@ func TestParseFilterFlag(t *testing.T) { t.Fatalf("Filter=%v Check=%v, want both true", q.Filter, q.Check) } } + +// TestReadyFilterCheckKeepsTypedExit proves the --check gate cannot recode a +// typed trigger miss as a tool failure. The fixture fetch resolves a real +// base snapshot, so the refusal is the whole gate, not a stubbed code. +func TestReadyFilterCheckKeepsTypedExit(t *testing.T) { + root := t.TempDir() + t.Chdir(root) + mkdir(t, filepath.Join(root, ".verdict")) + write(t, filepath.Join(root, ".verdict", "config.json"), + `{"schema_version":"1","trigger":{"paths_include":["protected/**"]}}`) + git := func(args ...string) string { + t.Helper() + cmd := exec.CommandContext(t.Context(), "git", args...) + cmd.Dir = root + cmd.Env = testenv.Base(nil) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } + return strings.TrimSpace(string(out)) + } + git("init", "-q") + git("add", ".verdict/config.json") + git("commit", "-qm", "base") + base := git("rev-parse", "HEAD") + + bundle := filepath.Join(root, ".verdict", "state", "5201") + mkdir(t, bundle) + write(t, filepath.Join(bundle, "PR.json"), + `{"provider":"github","title":"fix: retry","head_sha":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",`+ + `"author":{"login":"ana"},"baseRefName":"main","files":[{"path":"docs/README.md"}]}`) + if err := config.WriteRunState(bundle, &config.RunState{BaseSHA: base, Trusted: true}); err != nil { + t.Fatal(err) + } + if _, ref := controls.Resolve(t.Context(), coreexec.Runner{}, &controls.Request{ + BundleDir: bundle, Root: root, BaseSHA: base, + }); ref != nil { + t.Fatalf("controls.Resolve: %v", ref) + } + + var out bytes.Buffer + r := &cli.Runner{ + Out: &out, Human: &out, Diag: &out, Now: time.Now, JSONMode: true, + Cmd: "ready", Repo: "acme/app", Pr: "5201", + } + code := Run(t.Context(), r, []string{"--filter", "--check"}, + Request{RepoRoot: root, Repo: "acme/app", Pr: "5201"}) + if code != 1 { + t.Fatalf("exit = %d, want 1 for filtered-out under --check: %s", code, out.String()) + } + if !strings.Contains(out.String(), `"code": "filtered-out"`) { + t.Errorf("envelope misses the typed refusal: %s", out.String()) + } +} + +func mkdir(t *testing.T, dir string) { + t.Helper() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } +} + +func write(t *testing.T, path, body string) { + t.Helper() + if err := os.WriteFile(path, []byte(body), 0o600); err != nil { + t.Fatal(err) + } +} diff --git a/internal/ready/ready.go b/internal/ready/ready.go index 902326a26..b66fd2e08 100644 --- a/internal/ready/ready.go +++ b/internal/ready/ready.go @@ -30,10 +30,7 @@ func Run(ctx context.Context, r *cli.Runner, args []string, req Request) int { } resp, ref := readycore.Run(ctx, q) if ref != nil { - if q.Check { - return r.RefuseExit(ref, cli.ExitBroke) - } - return r.Refuse(ref) + return refuseReady(r, q, ref) } r.Warnings = resp.Warnings if r.JSONMode { @@ -65,31 +62,44 @@ func readyRequest(r *cli.Runner, req Request) *readycore.Request { } } +func refuseReady(r *cli.Runner, q *readycore.Request, ref *core.RefusalError) int { + if q.Check && ref.Code != "filtered-out" { + return r.RefuseExit(ref, cli.ExitBroke) + } + return r.Refuse(ref) +} + func parseFlags(args []string, q *readycore.Request) *cli.UsageError { + var usage *cli.UsageError for len(args) > 0 { flag := args[0] - if flag == "--check" || flag == "--filter" { - if flag == "--check" { - q.Check = true - } else { - q.Filter = true - } - args = args[1:] - continue - } - if len(args) < 2 { - return &cli.UsageError{Message: "missing ready flag operand: " + flag} - } - value := args[1] - args = args[2:] switch flag { - case "--wait-for": - q.WaitFor = value - case "--timeout": - q.Timeout = value + case "--check": + q.Check = true + args = args[1:] + case "--filter": + q.Filter = true + args = args[1:] + case "--wait-for", "--timeout": + args, usage = valueFlag(args, flag, q) + if usage != nil { + return usage + } default: return &cli.UsageError{Message: "unknown ready flag: " + flag} } } return nil } + +func valueFlag(args []string, flag string, q *readycore.Request) ([]string, *cli.UsageError) { + if len(args) < 2 { + return nil, &cli.UsageError{Message: "missing ready flag operand: " + flag} + } + if flag == "--wait-for" { + q.WaitFor = args[1] + } else { + q.Timeout = args[1] + } + return args[2:], nil +} diff --git a/internal/schemagen/testdata/baseline/config.schema.json b/internal/schemagen/testdata/baseline/config.schema.json index abd7c6a56..436374ee0 100644 --- a/internal/schemagen/testdata/baseline/config.schema.json +++ b/internal/schemagen/testdata/baseline/config.schema.json @@ -120,6 +120,8 @@ "description": "One trigger predicate set: authors, changed-path globs, labels, a title regular expression, base branches, and draft state. title_matches compiles when the configuration is parsed.", "type": "object", "properties": { + "draft": { "type": "boolean" }, + "title_matches": { "type": "string" }, "authors": { "type": "array", "items": { "type": "string" }, @@ -140,13 +142,11 @@ "items": { "type": "string" }, "minItems": 1 }, - "title_matches": { "type": "string" }, "base_branches": { "type": "array", "items": { "type": "string" }, "minItems": 1 - }, - "draft": { "type": "boolean" } + } }, "required": [], "additionalProperties": false diff --git a/internal/trigger/trigger.go b/internal/trigger/trigger.go index a06780be4..538ade413 100644 --- a/internal/trigger/trigger.go +++ b/internal/trigger/trigger.go @@ -4,8 +4,8 @@ package trigger import ( - "fmt" "regexp" + "slices" "github.com/stainless-code/verdict/internal/config" "github.com/stainless-code/verdict/internal/core" @@ -16,65 +16,100 @@ import ( // Facts are the PR inputs a trigger predicate can read. type Facts struct { Author string - Paths []string - Labels []string Title string BaseBranch string + Paths []string + Labels []string Draft bool + // IncompletePaths marks a captured file list the producer flagged as + // truncated or paged with an error. Path predicates cannot decide such a + // PR: skipping the review on partial evidence would be the wrong side to + // fail on. + IncompletePaths bool } // Evaluate reports whether a PR runs the review and names the predicate that // decided it: "matched" for a configured match, "no-trigger" when no predicate // is set, or the failing predicate's key. Lists OR within one predicate and // AND across predicates. -func Evaluate(pr Facts, predicates *core.ConfigTrigger) (bool, string) { - if predicates == nil || empty(*predicates) { +func Evaluate(pr *Facts, predicates *core.ConfigTrigger) (bool, string) { + if predicates == nil || empty(predicates) { return true, "no-trigger" } - if names := predicates.Authors; len(names) != 0 && !contains(names, pr.Author) { + if len(predicates.Authors) != 0 && !slices.Contains(predicates.Authors, pr.Author) { return false, "authors" } - if globs := predicates.PathsInclude; len(globs) != 0 && !anyPath(globs, pr.Paths) { - return false, "paths_include" + if reason := predicateReason(pr, predicates); reason != "" { + return false, reason } - if globs := predicates.PathsExclude; len(globs) != 0 && anyPath(globs, pr.Paths) { - return false, "paths_exclude" + return true, "matched" +} + +func predicateReason(pr *Facts, predicates *core.ConfigTrigger) string { + paths := withoutExcluded(pr.Paths, predicates.PathsExclude) + if reason := pathReason(predicates, paths, pr.IncompletePaths); reason != "" { + return reason + } + for _, check := range []struct { + name string + ok bool + }{ + {"labels", len(predicates.Labels) == 0 || anyContains(predicates.Labels, pr.Labels)}, + {"title_matches", matchTitle(predicates.TitleMatches, pr.Title)}, + {"base_branches", len(predicates.BaseBranches) == 0 || anyGlob(predicates.BaseBranches, pr.BaseBranch)}, + {"draft", predicates.Draft == nil || *predicates.Draft == pr.Draft}, + } { + if !check.ok { + return check.name + } } - if labels := predicates.Labels; len(labels) != 0 && !anyContains(labels, pr.Labels) { - return false, "labels" + return "" +} + +func withoutExcluded(paths, excluded []string) []string { + if len(excluded) == 0 { + return paths } - if pattern := predicates.TitleMatches; pattern != "" { - matched, err := regexp.MatchString(pattern, pr.Title) - if err != nil || !matched { - return false, "title_matches" + kept := make([]string, 0, len(paths)) + for _, path := range paths { + if !anyGlob(excluded, path) { + kept = append(kept, path) } } - if branches := predicates.BaseBranches; len(branches) != 0 && !anyGlob(branches, pr.BaseBranch) { - return false, "base_branches" + return kept +} + +func pathReason(predicates *core.ConfigTrigger, paths []string, incomplete bool) string { + // With an incomplete file list, a path match proves nothing either way. + // The caller warns; here the gate leaves the decision to live review. + if incomplete { + return "" } - if predicates.Draft != nil && *predicates.Draft != pr.Draft { - return false, "draft" + if len(predicates.PathsInclude) != 0 && !anyPath(predicates.PathsInclude, paths) { + return "paths_include" } - return true, "matched" + if len(predicates.PathsExclude) != 0 && len(predicates.PathsInclude) == 0 && len(paths) == 0 { + return "paths_exclude" + } + return "" } -func empty(p core.ConfigTrigger) bool { - return len(p.Authors)+len(p.PathsInclude)+len(p.PathsExclude)+len(p.Labels)+ - len(p.BaseBranches)+len(p.TitleMatches) == 0 && p.Draft == nil +func matchTitle(pattern, title string) bool { + if pattern == "" { + return true + } + matched, err := regexp.MatchString(pattern, title) + return err == nil && matched } -func contains(values []string, want string) bool { - for _, value := range values { - if value == want { - return true - } - } - return false +func empty(p *core.ConfigTrigger) bool { + return len(p.Authors)+len(p.PathsInclude)+len(p.PathsExclude)+len(p.Labels)+ + len(p.BaseBranches)+len(p.TitleMatches) == 0 && p.Draft == nil } func anyContains(values, candidates []string) bool { for _, candidate := range candidates { - if contains(values, candidate) { + if slices.Contains(values, candidate) { return true } } @@ -106,23 +141,24 @@ func anyPath(globs, paths []string) bool { func ReadFacts(dir string) (Facts, error) { raw, err := jq.CappedFile(dir+"/PR.json", jq.MaxPRBytes) if err != nil { - return Facts{}, fmt.Errorf("could not read %s/PR.json: %w", dir, err) + return Facts{}, jq.NewErrf("could not read %s/PR.json: %v", dir, err) } doc, err := jq.Decode(raw) if err != nil { - return Facts{}, fmt.Errorf("could not decode %s/PR.json: %w", dir, err) + return Facts{}, jq.NewErrf("could not decode %s/PR.json: %v", dir, err) } record, ok := jq.AsMap(doc) if !ok { - return Facts{}, fmt.Errorf("%s/PR.json is not an object", dir) + return Facts{}, jq.NewErrf("%s/PR.json is not an object", dir) } return Facts{ - Author: readAuthor(dir, record), - Paths: stringsFrom(jq.Elements(record["files"]), "path"), - Labels: readLabels(record["labels"]), - Title: jq.StrOf(record["title"]), - BaseBranch: firstString(record["baseRefName"], record["base_ref"]), - Draft: readDraft(record), + Author: readAuthor(dir, record), + Title: jq.StrOf(record["title"]), + BaseBranch: firstString(record["baseRefName"], record["base_ref"]), + Paths: stringsFrom(jq.Elements(record["files"]), "path"), + Labels: readLabels(record["labels"]), + Draft: readDraft(record), + IncompletePaths: readcommon.SourceTruncation(dir) != nil, }, nil } @@ -173,15 +209,19 @@ func stringsFrom(entries []any, key string) []string { func readLabels(raw any) []string { var out []string for _, entry := range jq.Elements(raw) { - if value, ok := entry.(string); ok && value != "" { - out = append(out, value) - continue - } - if m, ok := jq.AsMap(entry); ok { - if value := jq.StrOf(m["name"]); value != "" { - out = append(out, value) - } + if name := labelName(entry); name != "" { + out = append(out, name) } } return out } + +func labelName(entry any) string { + if value, ok := entry.(string); ok { + return value + } + if m, ok := jq.AsMap(entry); ok { + return jq.StrOf(m["name"]) + } + return "" +} diff --git a/internal/trigger/trigger_test.go b/internal/trigger/trigger_test.go index dd1e2492a..a53c76ef5 100644 --- a/internal/trigger/trigger_test.go +++ b/internal/trigger/trigger_test.go @@ -33,55 +33,86 @@ func facts() Facts { } } -func TestEvaluate(t *testing.T) { - for _, tc := range []struct { - name string - mutate func(*core.ConfigTrigger, *Facts) - wantRun bool - wantReason string - }{ - {name: "all match", wantRun: true, wantReason: "matched"}, - {name: "no predicates", mutate: func(p *core.ConfigTrigger, _ *Facts) { *p = core.ConfigTrigger{} }, wantRun: true, wantReason: "no-trigger"}, - { - name: "author OR second value matches", - mutate: func(p *core.ConfigTrigger, f *Facts) { - f.Author = "bo" - p.Authors = []string{"ana", "bo"} - }, - wantRun: true, wantReason: "matched", +type evaluateCase struct { + mutate func(*core.ConfigTrigger, *Facts) + name string + wantReason string + wantRun bool +} + +var evaluateCases = []evaluateCase{ + {name: "all match", wantRun: true, wantReason: "matched"}, + {name: "no predicates", mutate: func(p *core.ConfigTrigger, _ *Facts) { *p = core.ConfigTrigger{} }, wantRun: true, wantReason: "no-trigger"}, + { + name: "author OR second value matches", + mutate: func(p *core.ConfigTrigger, f *Facts) { + f.Author = "bo" + p.Authors = []string{"ana", "bo"} + }, + wantRun: true, wantReason: "matched", + }, + {name: "author fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Author = "mallory" }, wantReason: "authors"}, + {name: "path include fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Paths = []string{"docs/README.md"} }, wantReason: "paths_include"}, + { + name: "all excluded paths fail the include", + mutate: func(_ *core.ConfigTrigger, f *Facts) { + f.Paths = []string{"src/generated/retry.go"} + }, + wantReason: "paths_include", + }, + { + name: "an excluded path does not hide a matching one", + mutate: func(_ *core.ConfigTrigger, f *Facts) { + f.Paths = []string{"src/retry.go", "src/generated/retry.go"} }, - {name: "author fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Author = "mallory" }, wantReason: "authors"}, - {name: "path include fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Paths = []string{"docs/README.md"} }, wantReason: "paths_include"}, - { - name: "path exclude wins over include", - mutate: func(_ *core.ConfigTrigger, f *Facts) { - f.Paths = []string{"src/generated/retry.go"} - }, - wantReason: "paths_exclude", + wantRun: true, wantReason: "matched", + }, + { + name: "only excluded paths with no include fail", + mutate: func(p *core.ConfigTrigger, f *Facts) { + p.PathsInclude = nil + f.Paths = []string{"src/generated/retry.go"} }, - {name: "labels fail", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Labels = []string{"docs"} }, wantReason: "labels"}, - {name: "title fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Title = "chore: retry" }, wantReason: "title_matches"}, - {name: "base branch fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.BaseBranch = "dev" }, wantReason: "base_branches"}, - {name: "base branch glob matches", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.BaseBranch = "release/1.2" }, wantRun: true, wantReason: "matched"}, - {name: "draft fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Draft = false }, wantReason: "draft"}, - } { - t.Run(tc.name, func(t *testing.T) { - p, f := predicate(), facts() - if tc.mutate != nil { - tc.mutate(p, &f) - } - run, reason := Evaluate(f, p) - if run != tc.wantRun || reason != tc.wantReason { - t.Errorf("Evaluate = (%v, %q), want (%v, %q)", run, reason, tc.wantRun, tc.wantReason) - } - }) - } - run, reason := Evaluate(facts(), nil) + wantReason: "paths_exclude", + }, + { + name: "an incomplete file list never filters on paths", + mutate: func(_ *core.ConfigTrigger, f *Facts) { + f.Paths = []string{"docs/README.md"} + f.IncompletePaths = true + }, + wantRun: true, wantReason: "matched", + }, + {name: "labels fail", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Labels = []string{"docs"} }, wantReason: "labels"}, + {name: "title fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Title = "chore: retry" }, wantReason: "title_matches"}, + {name: "base branch fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.BaseBranch = "dev" }, wantReason: "base_branches"}, + {name: "base branch glob matches", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.BaseBranch = "release/1.2" }, wantRun: true, wantReason: "matched"}, + {name: "draft fails", mutate: func(_ *core.ConfigTrigger, f *Facts) { f.Draft = false }, wantReason: "draft"}, +} + +func TestEvaluate(t *testing.T) { + for _, tc := range evaluateCases { + t.Run(tc.name, func(t *testing.T) { runEvaluateCase(t, tc) }) + } + f := facts() + run, reason := Evaluate(&f, nil) if !run || reason != "no-trigger" { t.Errorf("Evaluate with nil predicates = (%v, %q), want (true, no-trigger)", run, reason) } } +func runEvaluateCase(t *testing.T, tc evaluateCase) { + t.Helper() + p, f := predicate(), facts() + if tc.mutate != nil { + tc.mutate(p, &f) + } + run, reason := Evaluate(&f, p) + if run != tc.wantRun || reason != tc.wantReason { + t.Errorf("Evaluate = (%v, %q), want (%v, %q)", run, reason, tc.wantRun, tc.wantReason) + } +} + func TestReadFacts(t *testing.T) { dir := t.TempDir() raw := `{"provider":"github","title":"fix: retry","baseRefName":"main","isDraft":true, @@ -106,3 +137,38 @@ func TestReadFacts(t *testing.T) { t.Errorf("ReadFacts = %#v, want %#v", got, want) } } + +func TestReadFactsNormalizedBundle(t *testing.T) { + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "PR.json"), []byte( + `{"provider":"gitlab","title":"fix: retry","base_ref":"release/1.2","draft":false,`+ + `"author":"ana","labels":["security","deps"],"files":[{"path":"src/retry.go"}]}`), 0o600); err != nil { + t.Fatal(err) + } + got, err := ReadFacts(dir) + if err != nil { + t.Fatalf("ReadFacts: %v", err) + } + want := Facts{ + Author: "ana", Paths: []string{"src/retry.go"}, Labels: []string{"security", "deps"}, + Title: "fix: retry", BaseBranch: "release/1.2", Draft: false, + } + if !reflect.DeepEqual(got, want) { + t.Errorf("ReadFacts = %#v, want %#v", got, want) + } +} + +func TestReadFactsMarksTruncatedFileList(t *testing.T) { + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "PR.json"), []byte(`{"files":[{"path":"src/retry.go"}]}`), 0o600); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "filesource.json"), + []byte(`{"source":"cli","cli_cap":100,"count":100,"truncated":true}`), 0o600); err != nil { + t.Fatal(err) + } + got, err := ReadFacts(dir) + if err != nil || !got.IncompletePaths { + t.Errorf("ReadFacts = %#v, err=%v, want IncompletePaths", got, err) + } +} diff --git a/scripts/size-baseline.txt b/scripts/size-baseline.txt index a95da968d..ec62fafd3 100644 --- a/scripts/size-baseline.txt +++ b/scripts/size-baseline.txt @@ -1,7 +1,7 @@ deps 0 -darwin/amd64 17360000 -darwin/arm64 15790000 -linux/amd64 17060000 +darwin/amd64 17380000 +darwin/arm64 15800000 +linux/amd64 17090000 linux/arm64 15540000 -windows/amd64 17450000 -windows/arm64 15570000 +windows/amd64 17480000 +windows/arm64 15590000 diff --git a/skills/verdict/schemas/config.schema.json b/skills/verdict/schemas/config.schema.json index abd7c6a56..436374ee0 100644 --- a/skills/verdict/schemas/config.schema.json +++ b/skills/verdict/schemas/config.schema.json @@ -120,6 +120,8 @@ "description": "One trigger predicate set: authors, changed-path globs, labels, a title regular expression, base branches, and draft state. title_matches compiles when the configuration is parsed.", "type": "object", "properties": { + "draft": { "type": "boolean" }, + "title_matches": { "type": "string" }, "authors": { "type": "array", "items": { "type": "string" }, @@ -140,13 +142,11 @@ "items": { "type": "string" }, "minItems": 1 }, - "title_matches": { "type": "string" }, "base_branches": { "type": "array", "items": { "type": "string" }, "minItems": 1 - }, - "draft": { "type": "boolean" } + } }, "required": [], "additionalProperties": false From 229f2a9ef5504442cdb10d94e924fb1acd9d7d19 Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Tue, 17 Nov 2026 10:05:48 +0200 Subject: [PATCH 6/7] chore(ts): regenerate refusal codes for the trigger gate --- clients/ts/src/refusal-codes.gen.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/ts/src/refusal-codes.gen.ts b/clients/ts/src/refusal-codes.gen.ts index 7aa7cdf57..0e6dff364 100644 --- a/clients/ts/src/refusal-codes.gen.ts +++ b/clients/ts/src/refusal-codes.gen.ts @@ -202,8 +202,8 @@ export type RefusalCode = | "stale-review-artifact" | "store-busy" | "threads-failed" - | "trigger-failed" | "triage-failed" + | "trigger-failed" | "uninstall-foreign" | "uninstall-no-receipt" | "unknown-agent" From 4e0c18646a241380f6f3194834cf8ff8503ffedd Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Tue, 17 Nov 2026 10:12:12 +0200 Subject: [PATCH 7/7] test(evals): record unhandled gh calls in the trigger eval stub --- evals/cases/trigger-filter.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/evals/cases/trigger-filter.sh b/evals/cases/trigger-filter.sh index 8286da442..3d03bc964 100755 --- a/evals/cases/trigger-filter.sh +++ b/evals/cases/trigger-filter.sh @@ -20,7 +20,9 @@ case "$*" in printf '%s' '{"isDraft":false,"mergeable":"MERGEABLE","mergeStateStatus":"CLEAN","body":"A long enough description for the readiness check to pass without a blocker.","state":"OPEN","headRefOid":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}' ;; *"pr checks"*) printf '%s' '[]' ;; *"graphql"*) printf '%s' '{"data":{"repository":{"pullRequest":{"reviewThreads":{"nodes":[]}}}}}' ;; - *) echo "trigger-eval: unhandled gh call ($*)" >&2; exit 1 ;; + *) echo "trigger-eval: unhandled gh call ($*)" >&2 + echo "unhandled gh call ($*)" >> "${EVAL_STUBLOG:-/dev/null}" + exit 1 ;; esac EOF chmod +x "$EVAL_STUBBIN/gh"