| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/copydesk" |
| 5 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 6 | "amadan.net/oficina/equipo/internal/wire" |
| 7 | "encoding/json" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestCopyClassifiesRealHostedFailure(t *testing.T) { |
| 15 | for _, fixture := range []string{"failure", "sidebar", "parallel", "references"} { |
| 16 | t.Run(fixture, func(t *testing.T) { copyClassifiesFixture(t, fixture) }) |
| 17 | } |
| 18 | } |
| 19 | func copyClassifiesFixture(t *testing.T, fixture string) { |
| 20 | r, row, _ := newCopyRig(t) |
| 21 | row.Copy.References = []copydesk.Reference{{Path: "explorations/team-personality/README.md", Lines: []int{35}, Reason: "Retain the reviewed runtime reference."}} |
| 22 | raw, err := os.ReadFile("testdata/copy-hosted-" + fixture + ".json") |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | var st forge.CIStatus |
| 27 | if err = json.Unmarshal(raw, &st); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | st.SHA = row.CandidateSHA |
| 31 | ci := &fakeCI{on: true, statuses: []forge.CIStatus{st}, failed: &st} |
| 32 | r.e.CI = ci |
| 33 | row.Specialist = wire.SpecialistChecks |
| 34 | rep, ok := r.e.hostedVerdict(r.ctx, row, judgeReport(row), st) |
| 35 | if !ok { |
| 36 | t.Fatal("no hosted verdict") |
| 37 | } |
| 38 | if len(strings.Split(rep.Output, "\n")) < 60 { |
| 39 | t.Fatal("fixture must exercise long hosted output") |
| 40 | } |
| 41 | got := r.e.classifyChecks(r.ctx, row, rep) |
| 42 | if got.Failure == nil || !got.Failure.CopyOnly() || !copydesk.SameFailure(*got.Failure, *got.Failure) { |
| 43 | t.Fatalf("complete real failure not routed: %+v", got.Failure) |
| 44 | } |
| 45 | if _, err := r.e.recoveryEvidence(r.ctx, row); err != nil { |
| 46 | t.Fatal("real recovery refused:", err) |
| 47 | } |
| 48 | // A different run's log cannot complete the observed run's signature. |
| 49 | changed := st |
| 50 | changed.UpdatedAt++ |
| 51 | ci.failed = &changed |
| 52 | got = r.e.classifyChecks(r.ctx, row, rep) |
| 53 | if got.Failure != nil && got.Failure.Complete { |
| 54 | t.Fatal("moved run classified as complete") |
| 55 | } |
| 56 | mixed := st |
| 57 | mixed.Steps = append(append([]forge.CIStep{}, st.Steps...), forge.CIStep{Name: "product-tests", State: wire.HostedFailure, Log: "--- FAIL: TestProductLogic (0.1s)\nFAIL\n"}) |
| 58 | ci.failed = &mixed |
| 59 | got = r.e.classifyChecks(r.ctx, row, rep) |
| 60 | if got.Failure == nil || !got.Failure.Complete || got.Failure.CopyOnly() { |
| 61 | t.Fatalf("mixed product failure misrouted: %+v", got.Failure) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestCopyGenericOnlySuggests(t *testing.T) { |
| 66 | r, row, seed := newCopyRigWith(t, func(seed string) { |
| 67 | if err := os.Remove(filepath.Join(seed, ".equipo/copy.json")); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | }) |
| 71 | before := gitOut(t, seed, "rev-parse", "HEAD") |
| 72 | rep := r.e.Exec(r.ctx, row, func(wire.StepReport) {}) |
| 73 | if rep.State != wire.StepDone || rep.CandidateSHA != before { |
| 74 | t.Fatalf("generic Copy edited or failed: %+v", rep) |
| 75 | } |
| 76 | if rep.Copy == nil || len(rep.Copy.Review.Strings) == 0 || rep.Copy.Review.Strings[0].Text != "Save changes" { |
| 77 | t.Fatal("generic suggestion lost") |
| 78 | } |
| 79 | if got := gitOut(t, r.e.Forge.(*hubForge).dir, "rev-parse", row.Branch); got != before { |
| 80 | t.Fatal("generic Copy published edits") |
| 81 | } |
| 82 | } |
| 83 | func TestCopyGenericReadsOnlyChangedSupportedFiles(t *testing.T) { |
| 84 | r, row, _ := newCopyRigWith(t, func(seed string) { |
| 85 | os.Remove(filepath.Join(seed, ".equipo/copy.json")) |
| 86 | if err := putCopyFile(seed, "untouched.go", []byte("not valid Go")); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | }) |
| 90 | tree, _, err := r.e.judgeTree(r.ctx, row) |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | defer os.RemoveAll(tree.Dir) |
| 95 | defer os.RemoveAll(tree.GitDir) |
| 96 | w, err := r.e.prepareCopy(r.ctx, row, tree) |
| 97 | defer os.RemoveAll(w.root) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | if _, found := w.before["untouched.go"]; found { |
| 102 | t.Fatal("read unchanged source") |
| 103 | } |
| 104 | w.before["image.bin"] = []byte{0, 1, 2} |
| 105 | if _, err := r.e.copyExtract(r.ctx, row, w); err != nil { |
| 106 | t.Fatal("unsupported changed file failed review:", err) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestCopyRulesOnlyAndPreflight(t *testing.T) { |
| 111 | for _, large := range []bool{false, true} { |
| 112 | t.Run(map[bool]string{false: "rules-only", true: "oversized"}[large], func(t *testing.T) { |
| 113 | r, row, _ := newCopyRigWith(t, func(seed string) { |
| 114 | rules := "Use plain words." |
| 115 | if large { |
| 116 | rules = strings.Repeat("words ", 6000) |
| 117 | } |
| 118 | if err := putCopyFile(seed, "AGENTS.md", []byte("## Build\n"+map[bool]string{false: strings.Repeat("unrelated build rules ", 8000), true: ""}[large]+"\n## Copy\n"+rules+"\n## Tests\nnot copy rules")); err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | }) |
| 122 | launched := false |
| 123 | r.e.beforeAdmit = func(string) { launched = true } |
| 124 | rep := r.e.Exec(r.ctx, row, func(wire.StepReport) {}) |
| 125 | if large { |
| 126 | if launched || rep.State != wire.StepFailed { |
| 127 | t.Fatalf("oversized payload launched: %v %+v", launched, rep) |
| 128 | } |
| 129 | } else if rep.State != wire.StepDone || !launched { |
| 130 | t.Fatalf("unrelated guidance blocked realistic pass: %+v", rep) |
| 131 | } |
| 132 | }) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestCopyKeepsValidReviewDespiteUsage(t *testing.T) { |
| 137 | for _, kind := range []string{"overrun", "missing"} { |
| 138 | t.Run(kind, func(t *testing.T) { |
| 139 | r, row, _ := newCopyRigWith(t, func(seed string) { |
| 140 | if err := putCopyFile(seed, "AGENTS.md", []byte("## Copy\ncopy-usage-"+kind)); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | }) |
| 144 | rep := r.e.Exec(r.ctx, row, func(wire.StepReport) {}) |
| 145 | if rep.State != wire.StepDone || rep.Copy == nil || len(rep.Copy.Review.Strings) != 1 { |
| 146 | t.Fatalf("valid review lost on %s: %+v", kind, rep) |
| 147 | } |
| 148 | if rep.Copy.Tokens < copydesk.PassTokens { |
| 149 | t.Fatalf("%s not charged: %+v", kind, rep.Copy) |
| 150 | } |
| 151 | }) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func TestCopyRejectsAddedLonePercent(t *testing.T) { |
| 156 | r, row, _ := newCopyRigWith(t, func(seed string) { |
| 157 | if err := putCopyFile(seed, "AGENTS.md", []byte("## Copy\ncopy-lone-percent")); err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | }) |
| 161 | rep := r.e.Exec(r.ctx, row, func(wire.StepReport) {}) |
| 162 | if rep.State == wire.StepDone || rep.Pushed { |
| 163 | t.Fatal("model added lone percent and published it") |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestCopyRealisticPassEstimate(t *testing.T) { |
| 168 | raw, err := os.ReadFile("testdata/copy-realistic-pass.json") |
| 169 | if err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | var ex copydesk.Extraction |
| 173 | if err = json.Unmarshal(raw, &ex); err != nil { |
| 174 | t.Fatal(err) |
| 175 | } |
| 176 | guidance, err := os.ReadFile("../../../AGENTS.md") |
| 177 | if err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | prompt, estimate := copyPrompt(ex, nil, guidance) |
| 181 | t.Logf("real batch: %d strings, %d prompt bytes, %d estimated tokens, allowance %d", len(ex.Strings), len(prompt), estimate, copydesk.PassTokens) |
| 182 | if estimate > copydesk.PassTokens || estimate < int64(len(prompt)) { |
| 183 | t.Fatal("realistic pass does not fit conservative estimate") |
| 184 | } |
| 185 | if strings.Contains(prompt, "Tailscale") { |
| 186 | t.Fatal("unrelated rules leaked into prompt") |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func TestCopyClassifierWithoutContractReadsNoSources(t *testing.T) { |
| 191 | r, row, _ := newCopyRigWith(t, func(seed string) { |
| 192 | if err := os.Remove(filepath.Join(seed, ".equipo/copy.json")); err != nil { |
| 193 | t.Fatal(err) |
| 194 | } |
| 195 | }) |
| 196 | dir := t.TempDir() |
| 197 | log := filepath.Join(dir, "reads.jsonl") |
| 198 | script := filepath.Join(dir, "git") |
| 199 | logJSON, _ := json.Marshal(log) |
| 200 | body := "#!/usr/bin/python3\nimport os,sys,json\nif 'show' in sys.argv:\n with open(" + string(logJSON) + ",'a') as f: f.write(json.dumps(sys.argv)+'\\n')\nos.execv('/usr/bin/git',['git']+sys.argv[1:])\n" |
| 201 | if err := os.WriteFile(script, []byte(body), 0700); err != nil { |
| 202 | t.Fatal(err) |
| 203 | } |
| 204 | r.e.O.Runner.GitPath = script |
| 205 | row.Specialist = wire.SpecialistChecks |
| 206 | r.e.classifyChecks(r.ctx, row, wire.StepReport{State: wire.StepDone, Checks: wire.ChecksFailed, Gate: "tests", Output: "failed"}) |
| 207 | raw, err := os.ReadFile(log) |
| 208 | if err != nil { |
| 209 | t.Fatal(err) |
| 210 | } |
| 211 | if strings.Contains(string(raw), "internal/equipo/copy.go") || strings.Count(string(raw), ".equipo/copy.json") != 1 { |
| 212 | t.Fatalf("classification read source without contract: %s", raw) |
| 213 | } |
| 214 | } |
| 215 | func TestCopyTruncatedEvidenceStaysUnknown(t *testing.T) { |
| 216 | r, row, _ := newCopyRig(t) |
| 217 | row.Specialist = wire.SpecialistChecks |
| 218 | st := forge.CIStatus{SHA: row.CandidateSHA, State: wire.HostedFailure, UpdatedAt: 1, Steps: []forge.CIStep{{Name: "copy-release-check", State: wire.HostedFailure, Log: "--- FAIL: TestReleaseCopyReviewed (0.1s)\nFAIL\n" + strings.Repeat("x\n", 3<<19)}}} |
| 219 | r.e.CI = &fakeCI{on: true, statuses: []forge.CIStatus{st}, failed: &st} |
| 220 | rep := wire.StepReport{State: wire.StepDone, Checks: wire.ChecksFailed, Hosted: &wire.HostedResult{SHA: st.SHA, ReportedAt: 1}} |
| 221 | rep = r.e.classifyChecks(r.ctx, row, rep) |
| 222 | if rep.Failure == nil || rep.Failure.Complete { |
| 223 | t.Fatalf("cut evidence became complete: %+v", rep.Failure) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | func TestCopyUnclassifiedUnitDoesNotRoute(t *testing.T) { |
| 228 | r, row, _ := newCopyRig(t) |
| 229 | row.Specialist = wire.SpecialistChecks |
| 230 | for _, log := range []string{"", "app.go:2: undefined: missing\nFAIL [build failed]\n"} { |
| 231 | st := forge.CIStatus{SHA: row.CandidateSHA, State: wire.HostedFailure, UpdatedAt: 1, Steps: []forge.CIStep{{Name: "copy-release-check", State: wire.HostedFailure, Log: log}}} |
| 232 | r.e.CI = &fakeCI{on: true, statuses: []forge.CIStatus{st}, failed: &st} |
| 233 | rep := r.e.classifyChecks(r.ctx, row, wire.StepReport{State: wire.StepDone, Checks: wire.ChecksFailed, Hosted: &wire.HostedResult{SHA: st.SHA, ReportedAt: 1}}) |
| 234 | if rep.Failure != nil && rep.Failure.CopyOnly() { |
| 235 | t.Fatalf("unit name granted copy-only classification without diagnostic: %q", log) |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |