| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 5 | "amadan.net/oficina/equipo/internal/runner/planflow" |
| 6 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 7 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 8 | "context" |
| 9 | "encoding/json" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestPlanWorkflowSpecialistOrder(t *testing.T) { |
| 15 | for _, action := range []string{"scope", "copy", "design", "implement", "review", "checks", "publish"} { |
| 16 | b := runnerproto.StepBrief{PlanRevision: 2, StepKey: "custom-" + action, SpecialistKey: "custom", Action: action, SpecialistRevision: 3} |
| 17 | p, err := planflow.Resolve(b) |
| 18 | want := map[string]string{"scope": "submit_acceptance", "copy": "submit_candidate", "design": "submit_candidate", "implement": "submit_candidate", "review": "submit_review", "checks": "", "publish": ""}[action] |
| 19 | if p.Submission != want { |
| 20 | t.Fatal("action mapped to wrong submission", action, p.Submission) |
| 21 | } |
| 22 | if err != nil || p.Action != action { |
| 23 | t.Fatalf("lost persisted action: %+v %v", p, err) |
| 24 | } |
| 25 | } |
| 26 | if _, err := planflow.Resolve(runnerproto.StepBrief{PlanRevision: 1, StepKey: "implement", Action: "deploy"}); err == nil { |
| 27 | t.Fatal("unknown action accepted") |
| 28 | } |
| 29 | } |
| 30 | func TestPlanWorkflowNoMidStepSwitch(t *testing.T) { |
| 31 | c := RunClaim{Assignment: runnerproto.Assignment{StepKey: "build", Selection: runnerproto.Selection{ConnectionID: "one"}}, ClaimResponse: runnerproto.ClaimResponse{Brief: runnerproto.RunnerBrief{Step: runnerproto.StepBrief{PlanRevision: 1, StepKey: "build", SpecialistKey: "engineering", SpecialistRevision: 1, Action: "implement"}, Selection: runnerproto.Selection{ConnectionID: "two"}}}} |
| 32 | if _, err := planflow.Validate(c.Assignment, c.Brief); err == nil { |
| 33 | t.Fatal("changed account accepted") |
| 34 | } |
| 35 | c.Brief.Selection = c.Assignment.Selection |
| 36 | c.Brief.Step.StepKey = "different" |
| 37 | if _, err := planflow.Validate(c.Assignment, c.Brief); err == nil { |
| 38 | t.Fatal("changed step accepted") |
| 39 | } |
| 40 | } |
| 41 | func TestPlanWorkflowFrozenArtifacts(t *testing.T) { |
| 42 | raw := []byte(`{"expected_symptom":"acceptance"}`) |
| 43 | a := team.Artifact{Role: team.RoleAcceptance, Payload: raw, Hash: team.ContentDigest(raw)} |
| 44 | b := runnerproto.RunnerBrief{Step: runnerproto.StepBrief{PlanRevision: 1, ArtifactHashes: []string{a.Hash}}, ArtifactHashes: []string{a.Hash}} |
| 45 | if err := planflow.Artifacts(b, []team.Artifact{a}); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | a.Payload = []byte(`{}`) |
| 49 | if err := planflow.Artifacts(b, []team.Artifact{a}); err == nil { |
| 50 | t.Fatal("changed frozen artifact accepted") |
| 51 | } |
| 52 | } |
| 53 | func TestPlanWorkflowToolPayloads(t *testing.T) { |
| 54 | for name, raw := range map[string]string{ |
| 55 | "submit_answer": `{"body":"answer","sources":[{"kind":"project","id":"1","hash":"` + strings.Repeat("a", 64) + `","revision":1}]}`, |
| 56 | "submit_acceptance": `{"expected_symptom":"missing behaviour","files":[{"path":"new_test.go","bytes":"dGVzdA=="}],"test_names":[{"package":"fixture","name":"TestNew"}]}`, |
| 57 | "submit_render": `{"commit":"` + strings.Repeat("a", 40) + `"}`, |
| 58 | } { |
| 59 | if err := validateToolArguments(name, json.RawMessage(raw)); err != nil { |
| 60 | t.Fatalf("%s: %v", name, err) |
| 61 | } |
| 62 | raw = strings.TrimSuffix(raw, "}") + `,"attestation":"model-claim"}` |
| 63 | if err := validateToolArguments(name, json.RawMessage(raw)); err == nil { |
| 64 | t.Fatalf("%s accepted fabricated attestation", name) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestPlanWorkflowRenderPair(t *testing.T) { |
| 70 | b := directToolFixture(t) |
| 71 | b.Tools = []string{"submit_candidate", "submit_render"} |
| 72 | proxy, err := OpenToolProxy(context.Background(), b) |
| 73 | mustJournal(t, err) |
| 74 | defer proxy.Close() |
| 75 | args := json.RawMessage(`{"commit":"` + strings.Repeat("a", 40) + `"}`) |
| 76 | mustJournal(t, proxy.submit(context.Background(), "submit_candidate", "candidate", args)) |
| 77 | mustJournal(t, proxy.submit(context.Background(), "submit_render", "render", args)) |
| 78 | if err = proxy.submit(context.Background(), "submit_render", "another", args); err == nil { |
| 79 | t.Fatal("duplicate render accepted") |
| 80 | } |
| 81 | } |
| 82 | func TestPlanWorkflowNoMidStepSwitchNamespaces(t *testing.T) { |
| 83 | TestDirectCancelWaitsForNamespacesUnproven(t) |
| 84 | } |
| 85 | |
| 86 | func TestPlanWorkflowAnswerRecovery(t *testing.T) { |
| 87 | s, r, _, _ := cleanupFixture(t) |
| 88 | r.Claim.Brief.Target.Kind = team.TargetProjectRead |
| 89 | r.Phase = "delivering-answer" |
| 90 | w, err := NewWitness(s.journal) |
| 91 | mustJournal(t, err) |
| 92 | spec := team.ArtifactSpec{Role: team.RoleAnswer, MediaType: "application/json", Payload: []byte(`{"body":"answer","sources":[]}`)} |
| 93 | mustJournal(t, w.record(context.Background(), "read-answer", attemptKey(r.Claim.Scope, r.Claim.Handle), spec)) |
| 94 | calls := 0 |
| 95 | s.api = apiFuncR3(func(_ context.Context, op string, q runnerproto.RunnerRequest, _ any) error { |
| 96 | calls++ |
| 97 | if op != "artifacts" || q.Artifact == nil || string(q.Artifact.Payload) != string(spec.Payload) { |
| 98 | t.Fatal("recovery replayed something other than the recorded answer", op) |
| 99 | } |
| 100 | return nil |
| 101 | }) |
| 102 | mustJournal(t, s.recoverReadAnswer(context.Background(), r)) |
| 103 | if calls != 1 || r.Phase != "completed" { |
| 104 | t.Fatal("answer was not recovered without a provider", calls, r.Phase) |
| 105 | } |
| 106 | } |
| 107 | func TestPlanWorkflowProviderPoliciesBroker(t *testing.T) { |
| 108 | for _, action := range []string{"scope", "recommend", "copy", "design", "review"} { |
| 109 | t.Run(action, func(t *testing.T) { |
| 110 | b := directToolFixture(t) |
| 111 | profile, err := provider.Profile(action) |
| 112 | mustJournal(t, err) |
| 113 | b.Tools = profile.Tools() |
| 114 | proxy, err := OpenToolProxy(context.Background(), b) |
| 115 | mustJournal(t, err) |
| 116 | defer proxy.Close() |
| 117 | q := ToolSubmission{InstanceID: b.InstanceID, TaskID: b.TaskID, Attempt: b.Attempt, Fence: b.Fence, Agent: b.Agent, RequestID: "submission", Name: "submit_candidate", Arguments: json.RawMessage(`{"commit":"` + strings.Repeat("a", 40) + `"}`)} |
| 118 | if profile.Submission != "submit_candidate" { |
| 119 | if proxy.ValidateApproval(context.Background(), q) == nil { |
| 120 | t.Fatal("approval broadened tool inventory") |
| 121 | } |
| 122 | } else { |
| 123 | mustJournal(t, proxy.ValidateApproval(context.Background(), q)) |
| 124 | } |
| 125 | q.Name = profile.Submission |
| 126 | q.Arguments = json.RawMessage(`{"commit":"` + strings.Repeat("a", 40) + `","attestation":"claim"}`) |
| 127 | if proxy.ValidateApproval(context.Background(), q) == nil { |
| 128 | t.Fatal("broker approved invalid arguments") |
| 129 | } |
| 130 | }) |
| 131 | } |
| 132 | } |
| 133 | |