oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "amadan.net/oficina/equipo/internal/copydesk"
5 "amadan.net/oficina/equipo/internal/wire"
6 "encoding/json"
7 "gorm.io/gorm"
8 "strings"
9 "testing"
10)
11
12func copyPendingRig(t *testing.T, state, reason string) *hkRig {
13 r, _ := rosterRig(t)
14 r.task("tkcopy", "Copy", hkOwner)
15 r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return nil }
16 r.must(r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", true, 0))
17 r.exec("UPDATE chat_tasks SET stage='in_review',base_sha=? WHERE id='tkcopy'", strings.Repeat("b", 40))
18 r.must(r.s.db.G.Create(&stepRecord{ID: "eng", TaskID: "tkcopy", Idx: 1, Person: hkOwner, Specialist: wire.SpecialistEngineering, State: wire.StepDone, Pushed: true, CandidateSHA: strings.Repeat("a", 40)}).Error)
19 r.must(r.s.db.G.Create(&stepRecord{ID: "copy", TaskID: "tkcopy", Idx: 2, Person: hkOwner, Specialist: wire.SpecialistCopy, State: state, Reason: reason, Terminated: state == wire.StepFailed, Placement: wire.PlacementWaiting, Attempt: 1, CopyOwner: hkOwner, CopyRevision: 1, CandidateSHA: strings.Repeat("a", 40)}).Error)
20 return r
21}
22func TestCopyRevocationReleasesQueued(t *testing.T) {
23 r := copyPendingRig(t, wire.StepQueued, "")
24 r.must(r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", false, 1))
25 var step stepRecord
26 r.must(r.s.db.G.First(&step, "id='copy'").Error)
27 if step.State == wire.StepQueued || !step.Withdrawn {
28 t.Fatalf("revoked Copy remains queued: %+v", step)
29 }
30 var n int64
31 r.must(r.s.db.G.Model(&stepRecord{}).Where("task_id='tkcopy' AND specialist='checks'").Count(&n).Error)
32 if n != 1 {
33 t.Fatal("switching off stranded task", n)
34 }
35}
36func TestCopyRepeatedRoundTapIsSingle(t *testing.T) {
37 r := copyPendingRig(t, wire.StepDone, "")
38 r.exec("DELETE FROM chat_steps WHERE id='copy'")
39 sig := copydesk.Classify(copydesk.Input{Version: 1, Units: []string{"tests"}}, copydesk.Classification{Version: 1, Complete: true, Failures: []copydesk.Failure{{Unit: "tests", Test: "TestA", Code: "failed", Category: "other"}}}, "adapter")
40 raw, _ := json.Marshal(sig)
41 for i, id := range []string{"check1", "check2"} {
42 r.must(r.s.db.G.Create(&stepRecord{ID: id, TaskID: "tkcopy", Idx: i + 2, Person: hkOwner, Specialist: wire.SpecialistChecks, State: wire.StepDone, Checks: wire.ChecksFailed, CandidateSHA: strings.Repeat("a", 40), FailureSignature: string(raw)}).Error)
43 }
44 if _, err := r.s.RoundTask(r.ctx, hkOwner, "tkcopy"); err != nil {
45 t.Fatal(err)
46 }
47 if _, err := r.s.RoundTask(r.ctx, hkOwner, "tkcopy"); err == nil {
48 t.Fatal("second tap queued another Engineering step")
49 }
50}
51
52func TestCopyUnknownUsageStillDispatches(t *testing.T) {
53 r := copyPendingRig(t, wire.StepQueued, "")
54 hello, _ := json.Marshal(wire.Hello{Protocol: wire.ProtocolCopy})
55 r.must(r.s.db.G.Create(&runnerSessionRecord{RunnerID: "RUNNER1", Hello: string(hello), Protocol: wire.ProtocolCopy}).Error)
56 r.must(r.s.db.G.Create(&copyAttemptRecord{ID: "old/1", TaskID: "tkcopy", StepID: "old", Attempt: 1, Tokens: 7, Known: false}).Error)
57 r.must(r.tx(func(tx *txn) error {
58 k, err := tx.loadTask("tkcopy")
59 if err != nil {
60 return err
61 }
62 var step stepRecord
63 if err := tx.db.First(&step, "id='copy'").Error; err != nil {
64 return err
65 }
66 if err := tx.dispatchStep(&k, &step); err != nil {
67 return err
68 }
69 if step.Reason == "copy_budget_unknown" {
70 t.Fatal("unknown attempt held task forever")
71 }
72 left, known, err := tx.copyBudget(k.ID)
73 if err == nil && (!known || left != copydesk.TaskTokens-2*copydesk.PassTokens) {
74 t.Fatalf("unknown attempt not conservatively charged: %d %v", left, known)
75 }
76 return err
77 }))
78}
79func TestCopyFailureCanContinue(t *testing.T) {
80 for _, reason := range []string{"policy_rejected", "workspace_refused", "copy_question", "copy_budget"} {
81 t.Run(reason, func(t *testing.T) {
82 state := wire.StepFailed
83 if reason == "copy_budget" {
84 state = wire.StepQueued
85 }
86 r := copyPendingRig(t, state, reason)
87 r.must(r.s.ContinueWithoutCopy(r.ctx, hkOwner, "tkcopy"))
88 var n int64
89 r.must(r.s.db.G.Model(&stepRecord{}).Where("task_id='tkcopy' AND specialist='checks'").Count(&n).Error)
90 if n != 1 {
91 t.Fatal("continue stranded task", n)
92 }
93 if err := r.s.ContinueWithoutCopy(r.ctx, hkOwner, "tkcopy"); err == nil {
94 t.Fatal("continue replay changed an active next step")
95 }
96 })
97 }
98}
99
100func TestCopyResultRoutesReviewAndSuggestions(t *testing.T) {
101 for _, kind := range []string{"overrun", "unknown", "suggestions"} {
102 t.Run(kind, func(t *testing.T) {
103 r := copyPendingRig(t, wire.StepQueued, "")
104 r.s.authority.RunnerRole = func(*gorm.DB, string) (string, error) { return wire.RolePrimary, nil }
105 r.exec("UPDATE chat_steps SET runner_id='RUNNER1' WHERE id='copy'")
106 c := &wire.CopyResult{Owner: hkOwner, Delegation: 1, Input: strings.Repeat("a", 40), Compared: strings.Repeat("b", 40), Extraction: copydesk.Extraction{Version: 1, Strings: []copydesk.Literal{{Key: "save", Text: "Old words"}}}, Review: copydesk.Review{Version: 1, Strings: []copydesk.Wording{{Key: "save", Text: "Save changes", Action: "accept", Flag: "Check context"}}}, Tokens: copydesk.PassTokens + 1, UsageKnown: kind != "unknown", Suggestions: kind == "suggestions"}
107 if kind == "suggestions" {
108 c.Tokens = 100
109 }
110 report := wire.StepReport{TaskID: "tkcopy", StepID: "copy", Attempt: 1, State: wire.StepDone, Termination: true, Pushed: true, CandidateSHA: c.Input, Copy: c}
111 r.must(r.tx(func(tx *txn) error {
112 return tx.stepReport(Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCopy}, &report)
113 }))
114 task, err := r.s.Task(r.ctx, hkOwner, "tkcopy")
115 r.must(err)
116 if len(task.CopyFlags) != 1 || task.CopyFlags[0].Text != "Save changes" {
117 t.Fatal("review or suggestion lost on task card")
118 }
119 var n int64
120 r.must(r.s.db.G.Model(&stepRecord{}).Where("task_id='tkcopy' AND specialist='checks'").Count(&n).Error)
121 if n != 1 {
122 t.Fatal("valid report did not advance", n)
123 }
124 if kind == "suggestions" {
125 r.must(r.s.db.G.Model(&copyReviewRecord{}).Count(&n).Error)
126 if n != 0 {
127 t.Fatal("suggestion gained acceptance authority")
128 }
129 }
130 })
131 }
132}
133
134func TestCopyPausedHousekeepingShowsHold(t *testing.T) {
135 r := copyPendingRig(t, wire.StepDone, "")
136 result := wire.CopyResult{Owner: hkOwner, Delegation: 1, Input: strings.Repeat("a", 40), Extraction: copydesk.Extraction{Version: 1}, Review: copydesk.Review{Version: 1}, UsageKnown: true}
137 raw, _ := json.Marshal(result)
138 r.exec("UPDATE chat_steps SET pushed=1,copy_result=? WHERE id='copy'", string(raw))
139 sig := copydesk.Classify(copydesk.Input{Version: 1, Units: []string{"copy"}}, copydesk.Classification{Version: 1, Complete: true, Failures: []copydesk.Failure{{Unit: "copy", Test: "TestCopy", Code: "failed", Category: "copy"}}}, "adapter")
140 raw, _ = json.Marshal(sig)
141 r.must(r.s.db.G.Create(&stepRecord{ID: "checks", TaskID: "tkcopy", Idx: 3, Specialist: wire.SpecialistChecks, Person: hkOwner, State: wire.StepDone, Checks: wire.ChecksFailed, CandidateSHA: strings.Repeat("a", 40), FailureSignature: string(raw)}).Error)
142 settings, err := r.s.Housekeeping(r.ctx, hkOwner)
143 r.must(err)
144 paused := true
145 _, err = r.s.SetHousekeeping(r.ctx, hkOwner, wire.SetHousekeeping{Revision: settings.Revision, Paused: paused})
146 r.must(err)
147 r.must(r.tx(func(tx *txn) error {
148 k, err := tx.loadTask("tkcopy")
149 if err != nil {
150 return err
151 }
152 return tx.advanceTask(&k)
153 }))
154 task, err := r.s.Task(r.ctx, hkOwner, "tkcopy")
155 r.must(err)
156 if !task.CopyHold {
157 t.Fatal("paused Copy hold invisible")
158 }
159}
160
161func TestCopyImportsEarlierOwnerReview(t *testing.T) {
162 r := copyPendingRig(t, wire.StepDone, "")
163 var in CopyImport
164 raw := []byte(`{"action":"approve","review":"earlier-review","generation":1,"strings":[{"id":"save","original":"Save it","text":"Save changes","edited":true,"done":true,"rerolled":false},{"id":"model","original":"Old words","text":"Model words","edited":true,"done":true,"rerolled":true}]}`)
165 r.must(json.Unmarshal(raw, &in))
166 if err := r.s.ImportCopyReview(r.ctx, hkMember, "p1", in); err == nil {
167 t.Fatal("another owner imported teaching edits")
168 }
169 r.must(r.s.ImportCopyReview(r.ctx, hkOwner, "p1", in))
170 r.must(r.s.ImportCopyReview(r.ctx, hkOwner, "p1", in))
171 var rows []copyFeedbackRecord
172 r.must(r.s.db.G.Find(&rows).Error)
173 if len(rows) != 1 || rows[0].Before != "Save it" || rows[0].After != "Save changes" {
174 t.Fatalf("wrong historic evidence: %+v", rows)
175 }
176 in.SourceID += ":changed"
177 in.Strings[0].Before = "not the reviewed original"
178 if err := r.s.ImportCopyReview(r.ctx, hkOwner, "p1", in); err == nil {
179 t.Fatal("mismatched original taught Copy")
180 }
181}
182
183func TestCopyRevocationWaitsForRunningProof(t *testing.T) {
184 r := copyPendingRig(t, wire.StepRunning, "")
185 r.s.authority.RunnerRole = func(*gorm.DB, string) (string, error) { return wire.RolePrimary, nil }
186 r.exec("UPDATE chat_steps SET started_at=?,runner_id='RUNNER1' WHERE id='copy'", r.now)
187 r.must(r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", false, 1))
188 var n int64
189 r.must(r.s.db.G.Model(&stepRecord{}).Where("specialist='checks'").Count(&n).Error)
190 if n != 0 {
191 t.Fatal("revocation advanced without termination proof")
192 }
193 r.must(r.tx(func(tx *txn) error {
194 return tx.stepReport(Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCopy}, &wire.StepReport{TaskID: "tkcopy", StepID: "copy", Attempt: 1, State: wire.StepCancelled, Termination: true, Reason: "cancelled"})
195 }))
196 r.must(r.s.db.G.Model(&stepRecord{}).Where("specialist='checks'").Count(&n).Error)
197 if n != 1 {
198 t.Fatal("revocation stayed blocked after termination proof", n)
199 }
200}
201func TestCopyContinueRequiresCreatorAndStoppedStep(t *testing.T) {
202 r := copyPendingRig(t, wire.StepFailed, "policy_rejected")
203 if err := r.s.ContinueWithoutCopy(r.ctx, hkMember, "tkcopy"); err == nil {
204 t.Fatal("another owner skipped Copy")
205 }
206 r.exec("UPDATE chat_steps SET state='running',terminated=0,started_at=? WHERE id='copy'", r.now)
207 if err := r.s.ContinueWithoutCopy(r.ctx, hkOwner, "tkcopy"); err == nil {
208 t.Fatal("skipped a running Copy attempt")
209 }
210}
211
212func TestCopyAuthorizedHoldCanContinue(t *testing.T) {
213 r := copyPendingRig(t, wire.StepQueued, "copy_budget")
214 r.s.authority.RunnerRole = func(*gorm.DB, string) (string, error) { return wire.RolePrimary, nil }
215 r.exec("UPDATE chat_steps SET started_at=?,runner_id='RUNNER1' WHERE id='copy'", r.now)
216 r.must(r.s.ContinueWithoutCopy(r.ctx, hkOwner, "tkcopy"))
217 var n int64
218 r.must(r.s.db.G.Model(&stepRecord{}).Where("specialist='checks'").Count(&n).Error)
219 if n != 0 {
220 t.Fatal("held authorized attempt advanced before cancellation proof")
221 }
222 r.must(r.tx(func(tx *txn) error {
223 return tx.stepReport(Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCopy}, &wire.StepReport{TaskID: "tkcopy", StepID: "copy", Attempt: 1, State: wire.StepCancelled, Termination: true, Reason: "cancelled"})
224 }))
225 r.must(r.s.db.G.Model(&stepRecord{}).Where("specialist='checks'").Count(&n).Error)
226 if n != 1 {
227 t.Fatal("held attempt did not continue after proof")
228 }
229}
230func TestCopyContinueRepairReturnsToEngineering(t *testing.T) {
231 r := copyPendingRig(t, wire.StepFailed, "policy_rejected")
232 r.exec("UPDATE chat_steps SET idx=3 WHERE id='copy'")
233 sig := copydesk.Classify(copydesk.Input{Version: 1, Units: []string{"copy"}}, copydesk.Classification{Version: 1, Complete: true, Failures: []copydesk.Failure{{Unit: "copy", Test: "TestCopy", Code: "failed", Category: "copy"}}}, "adapter")
234 raw, _ := json.Marshal(sig)
235 r.must(r.s.db.G.Create(&stepRecord{ID: "checks", TaskID: "tkcopy", Idx: 2, Specialist: wire.SpecialistChecks, Person: hkOwner, State: wire.StepDone, Checks: wire.ChecksFailed, CandidateSHA: strings.Repeat("a", 40), FailureSignature: string(raw)}).Error)
236 r.must(r.s.ContinueWithoutCopy(r.ctx, hkOwner, "tkcopy"))
237 var last stepRecord
238 r.must(r.s.db.G.Where("task_id='tkcopy'").Order("idx DESC").First(&last).Error)
239 if last.Specialist != wire.SpecialistEngineering || last.State != wire.StepQueued {
240 t.Fatalf("continue requeued Copy instead of normal repair: %+v", last)
241 }
242}
243