package runner import ( "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runner/planflow" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" "context" "encoding/json" "strings" "testing" ) func TestPlanWorkflowSpecialistOrder(t *testing.T) { for _, action := range []string{"scope", "copy", "design", "implement", "review", "checks", "publish"} { b := runnerproto.StepBrief{PlanRevision: 2, StepKey: "custom-" + action, SpecialistKey: "custom", Action: action, SpecialistRevision: 3} p, err := planflow.Resolve(b) want := map[string]string{"scope": "submit_acceptance", "copy": "submit_candidate", "design": "submit_candidate", "implement": "submit_candidate", "review": "submit_review", "checks": "", "publish": ""}[action] if p.Submission != want { t.Fatal("action mapped to wrong submission", action, p.Submission) } if err != nil || p.Action != action { t.Fatalf("lost persisted action: %+v %v", p, err) } } if _, err := planflow.Resolve(runnerproto.StepBrief{PlanRevision: 1, StepKey: "implement", Action: "deploy"}); err == nil { t.Fatal("unknown action accepted") } } func TestPlanWorkflowNoMidStepSwitch(t *testing.T) { 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"}}}} if _, err := planflow.Validate(c.Assignment, c.Brief); err == nil { t.Fatal("changed account accepted") } c.Brief.Selection = c.Assignment.Selection c.Brief.Step.StepKey = "different" if _, err := planflow.Validate(c.Assignment, c.Brief); err == nil { t.Fatal("changed step accepted") } } func TestPlanWorkflowFrozenArtifacts(t *testing.T) { raw := []byte(`{"expected_symptom":"acceptance"}`) a := team.Artifact{Role: team.RoleAcceptance, Payload: raw, Hash: team.ContentDigest(raw)} b := runnerproto.RunnerBrief{Step: runnerproto.StepBrief{PlanRevision: 1, ArtifactHashes: []string{a.Hash}}, ArtifactHashes: []string{a.Hash}} if err := planflow.Artifacts(b, []team.Artifact{a}); err != nil { t.Fatal(err) } a.Payload = []byte(`{}`) if err := planflow.Artifacts(b, []team.Artifact{a}); err == nil { t.Fatal("changed frozen artifact accepted") } } func TestPlanWorkflowToolPayloads(t *testing.T) { for name, raw := range map[string]string{ "submit_answer": `{"body":"answer","sources":[{"kind":"project","id":"1","hash":"` + strings.Repeat("a", 64) + `","revision":1}]}`, "submit_acceptance": `{"expected_symptom":"missing behaviour","files":[{"path":"new_test.go","bytes":"dGVzdA=="}],"test_names":[{"package":"fixture","name":"TestNew"}]}`, "submit_render": `{"commit":"` + strings.Repeat("a", 40) + `"}`, } { if err := validateToolArguments(name, json.RawMessage(raw)); err != nil { t.Fatalf("%s: %v", name, err) } raw = strings.TrimSuffix(raw, "}") + `,"attestation":"model-claim"}` if err := validateToolArguments(name, json.RawMessage(raw)); err == nil { t.Fatalf("%s accepted fabricated attestation", name) } } } func TestPlanWorkflowRenderPair(t *testing.T) { b := directToolFixture(t) b.Tools = []string{"submit_candidate", "submit_render"} proxy, err := OpenToolProxy(context.Background(), b) mustJournal(t, err) defer proxy.Close() args := json.RawMessage(`{"commit":"` + strings.Repeat("a", 40) + `"}`) mustJournal(t, proxy.submit(context.Background(), "submit_candidate", "candidate", args)) mustJournal(t, proxy.submit(context.Background(), "submit_render", "render", args)) if err = proxy.submit(context.Background(), "submit_render", "another", args); err == nil { t.Fatal("duplicate render accepted") } } func TestPlanWorkflowNoMidStepSwitchNamespaces(t *testing.T) { TestDirectCancelWaitsForNamespacesUnproven(t) } func TestPlanWorkflowAnswerRecovery(t *testing.T) { s, r, _, _ := cleanupFixture(t) r.Claim.Brief.Target.Kind = team.TargetProjectRead r.Phase = "delivering-answer" w, err := NewWitness(s.journal) mustJournal(t, err) spec := team.ArtifactSpec{Role: team.RoleAnswer, MediaType: "application/json", Payload: []byte(`{"body":"answer","sources":[]}`)} mustJournal(t, w.record(context.Background(), "read-answer", attemptKey(r.Claim.Scope, r.Claim.Handle), spec)) calls := 0 s.api = apiFuncR3(func(_ context.Context, op string, q runnerproto.RunnerRequest, _ any) error { calls++ if op != "artifacts" || q.Artifact == nil || string(q.Artifact.Payload) != string(spec.Payload) { t.Fatal("recovery replayed something other than the recorded answer", op) } return nil }) mustJournal(t, s.recoverReadAnswer(context.Background(), r)) if calls != 1 || r.Phase != "completed" { t.Fatal("answer was not recovered without a provider", calls, r.Phase) } } func TestPlanWorkflowProviderPoliciesBroker(t *testing.T) { for _, action := range []string{"scope", "recommend", "copy", "design", "review"} { t.Run(action, func(t *testing.T) { b := directToolFixture(t) profile, err := provider.Profile(action) mustJournal(t, err) b.Tools = profile.Tools() proxy, err := OpenToolProxy(context.Background(), b) mustJournal(t, err) defer proxy.Close() 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) + `"}`)} if profile.Submission != "submit_candidate" { if proxy.ValidateApproval(context.Background(), q) == nil { t.Fatal("approval broadened tool inventory") } } else { mustJournal(t, proxy.ValidateApproval(context.Background(), q)) } q.Name = profile.Submission q.Arguments = json.RawMessage(`{"commit":"` + strings.Repeat("a", 40) + `","attestation":"claim"}`) if proxy.ValidateApproval(context.Background(), q) == nil { t.Fatal("broker approved invalid arguments") } }) } }