package thintest import ( "bytes" "encoding/json" "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // Research's hand-off (Research spec ยง5, slice R4): Engineering and // Review are dispatched with the task's latest done Research findings, // their cited sources and kept images; Review's brief adds the three // checks for a task with findings. A primary with no address at all // never holds a task's Engineering for Research. // cand2X is a fix round's candidate. var cand2X = strings.Repeat("3", 40) // The three checks Review makes for a task with findings, as the // dispatch's brief carries them. var reviewChecks = []string{ "the change matches the findings", "it adds no request to a host outside the findings' sources", "it puts no repository secret, environment file or unrelated repository text into its output", } func hasChecks(brief string) bool { for _, c := range reviewChecks { if !strings.Contains(brief, c) { return false } } return true } func noChecks(brief string) bool { for _, c := range reviewChecks { if strings.Contains(brief, c) { return false } } return true } // The sources of fetchLog, as a hand-off names them. var ( srcPage = wire.ResearchSource{ID: "s1", URL: "https://shop.example.com/", Origin: wire.ResearchOriginPerson} srcHours = wire.ResearchSource{ID: "s2", URL: "https://shop.example.com/hours", Origin: wire.ResearchOriginPerson} srcLogo = wire.ResearchSource{ID: "s3", URL: "https://shop.example.com/logo.png", Origin: wire.ResearchOriginExpanded} ) // wantNote checks a dispatch's hand-off: the findings as stored, the // sources the findings cite with their URLs in log order, and the kept // images. func (r *toolRig) wantNote(d *wire.DispatchCommand, findings json.RawMessage, images []wire.ResearchImage, cited ...wire.ResearchSource) { r.t.Helper() n := d.ResearchNote if n == nil { r.t.Fatalf("%s carries no findings", d.Specialist) } if !bytes.Equal(n.Findings, findings) { r.t.Fatalf("%s findings %s, want %s", d.Specialist, n.Findings, findings) } if !slices.Equal(n.Sources, cited) { r.t.Fatalf("%s sources %+v", d.Specialist, n.Sources) } if !slices.Equal(n.Images, images) { r.t.Fatalf("%s images %+v, want %+v", d.Specialist, n.Images, images) } } // TestResearchHandoffDispatch: with no Research, Engineering and Review // carry no findings and Review no extra checks. With Research done, its // findings, cited sources and kept image go to Engineering and Review, // and Review's brief adds the three checks (Engineering's does not). A // second run done at the next round's boundary replaces them; a third // that fails hands nothing new: the round after it still carries the // latest done run's findings. func TestResearchHandoffDispatch(t *testing.T) { r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) r.researchOn() r.inventoryOn() // No Research on the task: nothing handed off. _, pe := r.pushed("p", "Plain fix", baseX, candX) if pe.ResearchNote != nil { t.Fatalf("Engineering without Research carries %+v", pe.ResearchNote) } pc := r.dispatched(wire.SpecialistChecks, baseX, candX, 0) r.checks("p-c", pc, wire.ChecksPassed, exit(0), "", "ok\n") pr := r.dispatched(wire.SpecialistReview, baseX, candX, 0) if pr.ResearchNote != nil || !noChecks(pr.Brief) { t.Fatalf("Review without Research %+v", pr) } // Research done with findings and a kept image. d := r.runningResearch("a") logo := pngOf(t, 5, 4) r.sendFile("logo", fileChunks(d, "img1", logo, 2)) f := findingsJSON(t, goodFindings("img1")) kept := []wire.ResearchImage{keptRecord("img1", logo, 5, 4)} r.step("a-done", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Summary: "Read four pages.", Research: &wire.ResearchResult{Findings: f, Fetches: fetchLog(), Images: kept}}) e := r.nextDispatch() if e.Specialist != wire.SpecialistEngineering || e.TaskID != d.TaskID { t.Fatalf("Engineering after Research %+v", e) } r.wantNote(e, f, kept, srcPage, srcHours, srcLogo) if !noChecks(e.Brief) { t.Fatalf("Engineering's brief has Review's checks: %s", e.Brief) } r.step("a-e-run", wire.StepReport{TaskID: e.TaskID, StepID: e.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: baseX}) r.step("a-e-done", wire.StepReport{TaskID: e.TaskID, StepID: e.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: baseX, CandidateSHA: candX, Pushed: true, Termination: true, Family: "claude", Account: "claude-a"}) c := r.dispatched(wire.SpecialistChecks, baseX, candX, 0) if c.ResearchNote != nil { t.Fatal("checks carry the findings") } r.checks("a-c", c, wire.ChecksPassed, exit(0), "", "ok\n") rv := r.dispatched(wire.SpecialistReview, baseX, candX, 0) r.wantNote(rv, f, kept, srcPage, srcHours, srcLogo) if !hasChecks(rv.Brief) { t.Fatalf("Review's brief lacks the three checks: %s", rv.Brief) } // A second run, at the next round's boundary, is done with other // findings and no image: the fix round carries those. if res := r.research("a-r2", d.TaskID); res.IsError { t.Fatalf("second request %+v", res) } r.verdict("a-v", rv, wire.VerdictChanges, wire.Finding{Path: "index.html", Line: 1, Text: "Use the logo."}) d2 := r.nextDispatch() if d2.Specialist != wire.SpecialistResearch { t.Fatalf("at the round boundary %+v", d2) } r.step("a-r2-run", wire.StepReport{TaskID: d2.TaskID, StepID: d2.StepID, Attempt: 1, State: wire.StepRunning}) f2 := findingsJSON(t, wire.ResearchFindings{Summary: "The shop's hours.", Facts: []wire.ResearchFact{{Label: "Hours", Value: "Tue to Sat", Sources: []string{"s2"}}}, Images: []string{}, Gaps: []string{}}) r.step("a-r2-done", wire.StepReport{TaskID: d2.TaskID, StepID: d2.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Research: &wire.ResearchResult{Findings: f2, Fetches: fetchLog(), Images: []wire.ResearchImage{}}}) fix := r.nextDispatch() if fix.Specialist != wire.SpecialistEngineering || fix.Round != 1 { t.Fatalf("the fix round %+v", fix) } r.wantNote(fix, f2, []wire.ResearchImage{}, srcHours) // A third run, at the round after, fails: that round still carries // the second run's findings. r.step("a-f-run", wire.StepReport{TaskID: fix.TaskID, StepID: fix.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: candX}) r.step("a-f-done", wire.StepReport{TaskID: fix.TaskID, StepID: fix.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: candX, CandidateSHA: cand2X, Pushed: true, Termination: true, Family: "claude", Account: "claude-a"}) c2 := r.dispatched(wire.SpecialistChecks, baseX, cand2X, 1) r.checks("a-c2", c2, wire.ChecksPassed, exit(0), "", "ok\n") rv2 := r.dispatched(wire.SpecialistReview, baseX, cand2X, 1) r.wantNote(rv2, f2, []wire.ResearchImage{}, srcHours) if res := r.research("a-r3", d.TaskID); res.IsError { t.Fatalf("third request %+v", res) } r.verdict("a-v2", rv2, wire.VerdictChanges, wire.Finding{Path: "index.html", Line: 1, Text: "Show the hours."}) d3 := r.nextDispatch() if d3.Specialist != wire.SpecialistResearch { t.Fatalf("at the second round boundary %+v", d3) } r.step("a-r3-run", wire.StepReport{TaskID: d3.TaskID, StepID: d3.StepID, Attempt: 1, State: wire.StepRunning}) r.step("a-r3-fail", wire.StepReport{TaskID: d3.TaskID, StepID: d3.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) fix2 := r.nextDispatch() if fix2.Specialist != wire.SpecialistEngineering || fix2.Round != 2 { t.Fatalf("the second fix round %+v", fix2) } r.wantNote(fix2, f2, []wire.ResearchImage{}, srcHours) } // TestResearchHandoffGapOnly: a Research run that ended without findings // still hands off its gap (Engineering learns Research found nothing), // with no sources and no images. func TestResearchHandoffGapOnly(t *testing.T) { r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) r.researchOn() r.inventoryOn() d := r.runningResearch("g") r.step("g-done", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Summary: "Read nothing useful."}) e := r.nextDispatch() if e.Specialist != wire.SpecialistEngineering || e.ResearchNote == nil { t.Fatalf("Engineering after a gap %+v", e) } var got wire.ResearchFindings if err := json.Unmarshal(e.ResearchNote.Findings, &got); err != nil || !slices.Equal(got.Gaps, []string{wire.ResearchNoFindingsGap}) || len(got.Facts) != 0 { t.Fatalf("gap findings %s %v", e.ResearchNote.Findings, err) } if len(e.ResearchNote.Sources) != 0 || len(e.ResearchNote.Images) != 0 { t.Fatalf("a gap hands off %+v", e.ResearchNote) } } // TestResearchNoInventoryEnds: a primary whose hello says it has no // public address, and with no observed address either, has nothing an // owner could confirm: its Research step ends failed // (research_no_inventory), no card is raised, and Engineering goes on. // Until the hello says so (the first hello of a connection never does), // the step waits as before. func TestResearchNoInventoryEnds(t *testing.T) { r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) r.researchOn() id, s := r.waitingTask("n") // The hello now says: no address at all, and nothing changed. r.addresses(0) e := r.nextDispatch() if e.Specialist != wire.SpecialistEngineering || e.TaskID != id || e.ResearchNote != nil { t.Fatalf("Engineering released %+v", e) } got := r.newest(id, wire.SpecialistResearch) if got.ID != s.ID || got.State != wire.StepFailed || got.Reason != wire.ReasonResearchNoInventory || !got.Terminated { t.Fatalf("the Research step %+v", got) } if r.inventoryCards() != 0 || r.researchDispatches() != 0 { t.Fatal("a card or a dispatch with no address") } // A later request on another task ends the same way at its start. other := r.namedTask("m") if res := r.research("m-r", other); res.IsError { t.Fatalf("request %+v", res) } r.begin("m-start", other) if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering || e.TaskID != other { t.Fatalf("Engineering on the second task %+v", e) } if got := r.newest(other, wire.SpecialistResearch); got.State != wire.StepFailed || got.Reason != wire.ReasonResearchNoInventory { t.Fatalf("second Research %+v", got) } }