diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 2fa7d3e92..5306090d7 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1522,6 +1522,16 @@ func (d *Daemon) acquireTaskContainer( d.parkRunningTask(ctx, task.ID, reason+": "+err.Error(), taskstate.ParkTransient) } + // A watched-repositories binding or the operator chat trigger assigns + // the pull request generically through Task.Inputs (its declared + // pr_number input); archie's own PRs sets PRNumber directly and never + // goes through either path. Resolving here, before the brief is + // written, means task.json and the prefetch below always agree with + // what every later reader (stagePRIntake, output posting) sees. + if task.Workflow == "pr-review" { + task.PRNumber = task.EffectivePRNumber() + } + if err := writeTaskBrief(workDir, task); err != nil { park("task.json write failed", err) return nil, nil, false diff --git a/internal/domain/workflow/definition.go b/internal/domain/workflow/definition.go index b90c3809c..f81543c4e 100644 --- a/internal/domain/workflow/definition.go +++ b/internal/domain/workflow/definition.go @@ -193,11 +193,32 @@ func ShippedDefinitions() WorkflowDefinitionCollection { for _, stage := range wf.Stages { fmt.Fprintf(&builder, " - type: %s.%s\n", id, stage.Name) } + // Only pr-review declares inputs today; every other builtin's + // generated YAML stays byte-identical to before this field existed. + if len(wf.Interface.Inputs) > 0 { + builder.WriteString("inputs:\n") + for _, name := range sortedInputNames(wf.Interface.Inputs) { + spec := wf.Interface.Inputs[name] + fmt.Fprintf(&builder, " %s:\n type: %s\n required: %t\n", name, spec.Type, spec.Required) + } + } collection.Definitions = append(collection.Definitions, WorkflowDefinitionEntry{ID: id, YAML: builder.String()}) } return collection } +// sortedInputNames returns a Workflow.Interface's declared input names in a +// deterministic order, so the generated YAML (and its digest) never depends +// on map iteration order. +func sortedInputNames(inputs map[string]task.InputSpec) []string { + names := make([]string, 0, len(inputs)) + for name := range inputs { + names = append(names, name) + } + sort.Strings(names) + return names +} + func legacyBuiltinWorkflows() Registry { return Registry{ "bootstrap": Bootstrap(), "implement": Implement(), "tdd": TDD(), diff --git a/internal/domain/workflow/definition_test.go b/internal/domain/workflow/definition_test.go index 81f469785..0b8b31e2b 100644 --- a/internal/domain/workflow/definition_test.go +++ b/internal/domain/workflow/definition_test.go @@ -3,6 +3,8 @@ package workflow import ( "strings" "testing" + + "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) func TestParseDefinitionRejectsUnsafeDefinitions(t *testing.T) { @@ -31,6 +33,34 @@ func TestParseDefinitionAllowsRepeatingAnOperation(t *testing.T) { } } +func TestShippedDefinitionsEmitDeclaredInputsForPRReview(t *testing.T) { + entry, ok := ShippedDefinitions().DefinitionByID("pr-review") + if !ok { + t.Fatal("missing shipped workflow \"pr-review\"") + } + iface, err := task.ParseWorkflowInterface(entry.YAML) + if err != nil { + t.Fatalf("ParseWorkflowInterface: %v", err) + } + spec, ok := iface.Inputs["pr_number"] + if !ok { + t.Fatal("pr-review does not declare a pr_number input") + } + if spec.Type != "number" || !spec.Required { + t.Fatalf("pr_number input = %+v, want {Type: number, Required: true}", spec) + } +} + +func TestShippedDefinitionsOmitInputsBlockForWorkflowsThatDeclareNone(t *testing.T) { + entry, ok := ShippedDefinitions().DefinitionByID("implement") + if !ok { + t.Fatal("missing shipped workflow \"implement\"") + } + if strings.Contains(entry.YAML, "inputs:") { + t.Fatalf("implement's generated YAML unexpectedly carries an inputs: block:\n%s", entry.YAML) + } +} + func TestShippedDefinitionsPreserveStageOrder(t *testing.T) { registry := BuiltinStepRegistry() for id, legacy := range legacyBuiltinWorkflows() { diff --git a/internal/domain/workflow/prreview_stages.go b/internal/domain/workflow/prreview_stages.go index c4b467ef5..12f73ff25 100644 --- a/internal/domain/workflow/prreview_stages.go +++ b/internal/domain/workflow/prreview_stages.go @@ -149,7 +149,18 @@ const prReviewConcurrency = 8 // merge gate), both off by default and each gated by its own config flag. func PRReview() Workflow { return Workflow{ - Name: "pr-review", + Name: "pr-review", + // pr_number is required so a binding (watched-repos) or a chat task + // (the operator trigger) can only target pr-review by actually + // naming a pull request; stagePRIntake reads it from Task.Inputs + // when Task.PRNumber is unset (archie's-own-PRs sets PRNumber + // directly and never goes through binding validation, so this + // declaration does not affect that path). + Interface: task.WorkflowInterface{ + Inputs: map[string]task.InputSpec{ + "pr_number": {Type: "number", Required: true}, + }, + }, Stages: append(prReviewDecisionStages(), stagePROutput()), } } @@ -184,6 +195,17 @@ func stagePRIntake() Stage { if tc.PRSource == nil { return fmt.Errorf("pr-review: no PRSource configured") } + // A watched-repositories binding or the operator chat trigger + // assigns the pull request through the declared pr_number input, + // not PRNumber directly; the daemon's container-acquisition path + // already resolves this before dispatch, but an in-process or + // subprocess run that bypasses it still needs it resolved here. + if tc.Task.PRNumber == 0 { + tc.Task.PRNumber = tc.Task.EffectivePRNumber() + } + if tc.Task.PRNumber == 0 { + return fmt.Errorf("pr-review: no pull request number (set directly or via the pr_number input)") + } meta, err := tc.PRSource.Metadata(ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.PRNumber) if err != nil { return fmt.Errorf("fetch pull request metadata: %w", err) diff --git a/internal/domain/workflow/prreview_stages_test.go b/internal/domain/workflow/prreview_stages_test.go index 4f6865ea2..0707c0f61 100644 --- a/internal/domain/workflow/prreview_stages_test.go +++ b/internal/domain/workflow/prreview_stages_test.go @@ -170,6 +170,32 @@ func TestStagePRIntakeFailsWithNoPRSource(t *testing.T) { } } +func TestStagePRIntakeResolvesPRNumberFromInputsWhenUnset(t *testing.T) { + tc := baseTaskContext(t) + tc.Task.PRNumber = 0 + tc.Task.Inputs = map[string]any{"pr_number": json.Number("77")} + tc.PRSource = &fakePRSource{metadata: PRMetadata{Title: "t", Body: "b"}, diff: smallDiff} + tc.Agent = &concurrentAgentRunner{byStage: func(string) (agentexec.Result, error) { + return captureResult("score_ai_generated", map[string]any{"confidence": 0.1}), nil + }} + + if err := stagePRIntake().Run(context.Background(), tc); err != nil { + t.Fatalf("intake: %v", err) + } + if tc.Task.PRNumber != 77 { + t.Errorf("PRNumber = %d, want 77 (resolved from Inputs)", tc.Task.PRNumber) + } +} + +func TestStagePRIntakeFailsWithNoPRNumberAnywhere(t *testing.T) { + tc := baseTaskContext(t) + tc.Task.PRNumber = 0 + tc.PRSource = &fakePRSource{metadata: PRMetadata{Title: "t", Body: "b"}, diff: smallDiff} + if err := stagePRIntake().Run(context.Background(), tc); err == nil { + t.Fatal("want an error with no PR number set directly or via Inputs") + } +} + func TestStagePRAnatomyBuildsSnapshotBlastRadiusAndNarrative(t *testing.T) { tc := baseTaskContext(t) tc.prReview = &prReviewState{ diff --git a/internal/domain/workflow/task/task.go b/internal/domain/workflow/task/task.go index b5caa5f7e..225ea41a5 100644 --- a/internal/domain/workflow/task/task.go +++ b/internal/domain/workflow/task/task.go @@ -9,6 +9,7 @@ package task import ( "context" + "encoding/json" "time" "github.com/samcharles93/archie-core/internal/domain/org" @@ -151,6 +152,34 @@ func (t Task) IsForgeBacked() bool { return t.Source != SourceChat } +// EffectivePRNumber resolves the pull request a pr-review task targets: +// PRNumber when a producer already set it directly (archie's own PRs, which +// never goes through binding/chat input assignment), otherwise the pr_number +// input a binding (watched repositories) or a chat task (the operator +// trigger) assigned generically through Inputs. Returns 0 when neither is +// set. DecodeInputs (the store's round-trip) decodes a JSON number as +// json.Number; a caller that built Inputs by hand in Go may use float64 or +// int instead, so all three are accepted. +func (t Task) EffectivePRNumber() int { + if t.PRNumber != 0 { + return t.PRNumber + } + switch n := t.Inputs["pr_number"].(type) { + case json.Number: + i, err := n.Int64() + if err != nil { + return 0 + } + return int(i) + case float64: + return int(n) + case int: + return n + default: + return 0 + } +} + // Store is the narrow, consumer-owned subset of the State Store contract // that workflow stages call mid-run. The PostgreSQL store and staterpc.Client // satisfy it; this is the interface archie-agent uses over the gRPC State diff --git a/internal/domain/workflow/task/task_test.go b/internal/domain/workflow/task/task_test.go new file mode 100644 index 000000000..f649e576b --- /dev/null +++ b/internal/domain/workflow/task/task_test.go @@ -0,0 +1,29 @@ +package task + +import ( + "encoding/json" + "testing" +) + +func TestEffectivePRNumber(t *testing.T) { + for _, test := range []struct { + name string + task Task + want int + }{ + {"PRNumber set directly wins", Task{PRNumber: 7, Inputs: map[string]any{"pr_number": json.Number("99")}}, 7}, + {"json.Number from a store round trip", Task{Inputs: map[string]any{"pr_number": json.Number("42")}}, 42}, + {"float64 from a hand-built map", Task{Inputs: map[string]any{"pr_number": float64(13)}}, 13}, + {"int from a hand-built map", Task{Inputs: map[string]any{"pr_number": 5}}, 5}, + {"missing input", Task{Inputs: map[string]any{}}, 0}, + {"nil Inputs", Task{}, 0}, + {"wrong type", Task{Inputs: map[string]any{"pr_number": "not a number"}}, 0}, + {"malformed json.Number", Task{Inputs: map[string]any{"pr_number": json.Number("not-a-number")}}, 0}, + } { + t.Run(test.name, func(t *testing.T) { + if got := test.task.EffectivePRNumber(); got != test.want { + t.Errorf("EffectivePRNumber() = %d, want %d", got, test.want) + } + }) + } +} diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index 9a7222552..e9a0f7a72 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -261,6 +261,14 @@ type Stage struct { type Workflow struct { Name string Stages []Stage + // Interface declares this workflow's inputs, repository mode and agent + // profile to whatever starts it -- a binding's CheckWorkflow reads it the + // same way it reads a YAML-defined workflow's WorkflowInterface. Zero + // value (no declared inputs) is the default every builtin had before + // pr-review's triggers needed one; ShippedDefinitions only emits an + // inputs: block when this is non-empty, so every other builtin's + // generated YAML is byte-identical to before. + Interface task.WorkflowInterface } // Registry maps workflow names to definitions.