feat(workflow): watched-repos triggers pr-review via the generic binding system
Watched-repos needs no bespoke webhook-parsing code: the existing generic capture+binding system (internal/infrastructure/captureintake, internal/domain/binding.Binding) already turns any inbound webhook into a task on any workflow, via a dashboard-editable matcher, CEL filter and payload-to-input mapping. pr-review just needed to become a binding-targetable workflow. - Workflow gains an Interface field (task.WorkflowInterface) so a Go-literal builtin can declare inputs the same way a YAML-defined one does; ShippedDefinitions emits an inputs: block only when non-empty, so every other builtin's generated YAML is byte-identical to before. - PRReview() declares pr_number as a required number input. - Task gains EffectivePRNumber(): PRNumber when a producer set it directly (archie's own PRs), else the pr_number input a binding assigned generically through Task.Inputs. Handles json.Number (the store's round-trip encoding), float64 and int. - The daemon resolves it once in acquireTaskContainer, before the task brief is written and before the pr-review prefetch runs, so everything downstream agrees. stagePRIntake falls back to the same resolution for any dispatch path that bypasses container acquisition, and fails loudly if neither PRNumber nor the input is set. An operator wires up watched-repos entirely through the dashboard: a Binding matching the webhook's source, a CEL filter for pull_request events, workflow: pr-review, and an Inputs mapping pr_number from the payload. No code change needed for a new repo or a new source. Operator/chat trigger remains open, left as a precise continuation: EnqueueChatTask is part of the State Store's protobuf-generated gRPC contract (proto/state/v1/state.proto), not just a Go interface -- extending it for a generic inputs parameter needs a proto field addition + regen, a new SQL query (StampTaskBinding cannot be reused, since it unconditionally stamps binding_id/version, which a plain chat task must never carry), and updates across staterpc's client/server and ~20 call sites. Real plumbing, not a quick tool rewrite -- scoped correctly as its own pass rather than rushed here.
This commit is contained in:
8 files changed
+176
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user