oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "net/url"
5 "strconv"
6 "strings"
7 "testing"
8 "time"
9
10 "amadan.net/oficina/equipo/internal/sealbox"
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14// D1 (Paul, 2026-11-28): once a Mac holds a Claude credential, its checks
15// run sandboxed or not at all. While the instance holds a Mac as having
16// one (from connecting until the Mac says its files and Keychain items
17// are gone), the task page offers no Run outside the sandbox for checks
18// waiting for that Mac and says why; the choice is refused if posted, and
19// nothing is dispatched outside the sandbox. A choice already recorded on
20// a waiting attempt is withdrawn when a connection starts, and its
21// creator is told.
22
23// d1Mac is a Mac worker at 37 whose sandbox is not proven (hello sandbox
24// none), with its seal key, saying its whole inventory.
25func (r *runnersRig) d1Mac(label string) (*peer, sealbox.Keys) {
26 r.t.Helper()
27 keys := sealKeys(r.t)
28 w := r.newMacEngineer(label, wire.EngineeringNoConnection)
29 w.heartbeat(d1Hello(keys, nil))
30 return w, keys
31}
32
33// d1Hello is that Mac's full hello, sandbox none; change sets its hold.
34func d1Hello(keys sealbox.Keys, change func(*wire.Capabilities), conns ...wire.HeldConnection) wire.Hello {
35 h := inventoryHello(keys.PublicString(), change, conns...)
36 h.Sandbox = wire.SandboxNone
37 return h
38}
39
40// sandboxChoice is what the task page shows its creator for checks
41// waiting for the Mac's sandbox: the offer, or the line saying the Mac
42// holds Claude; and the step's word on it.
43func (r *runnersRig) sandboxChoice(task string) (offered, held bool, choice string) {
44 r.t.Helper()
45 page := r.get(r.inst.Cookie(), "/tasks/"+task, 200)
46 return strings.Contains(page, "data-task-unsandboxed"), strings.Contains(page, "data-sandbox-held"), r.checksStep(task).SandboxChoice
47}
48
49// chooseUnsandboxed posts the creator's Run outside the sandbox for the
50// checks step's attempt, as the page's confirm does.
51func (r *runnersRig) chooseUnsandboxed(task string, s wire.Step) int {
52 r.t.Helper()
53 status, _ := r.post(r.inst.Cookie(), "/conversation/tasks/"+task+"/unsandboxed", url.Values{"from": {"task"}, "step": {s.ID}, "attempt": {strconv.FormatInt(s.Attempt, 10)}})
54 return status
55}
56
57func TestUnsandboxedNotOfferedWithCredential(t *testing.T) {
58 r := newRunnersRig(t)
59 w, keys := r.d1Mac("Tito Studio")
60 wid := w.fr.Welcome.RunnerID
61 task, _ := r.candidate("iOS swipe", true)
62 s := r.waitsForSandbox(w, task, 1)
63 machinesSay := func() bool {
64 t.Helper()
65 return strings.Contains(r.get(r.inst.Cookie(), "/conversation/settings/machines", 200), "data-holds-claude")
66 }
67 offeredNow := func(when string) {
68 t.Helper()
69 if offered, held, choice := r.sandboxChoice(task); !offered || held || choice != wire.SandboxChoiceOffered || machinesSay() {
70 t.Fatalf("%s: offered %v held line %v choice %q machines page says held %v", when, offered, held, choice, machinesSay())
71 }
72 }
73 notOffered := func(when string) {
74 t.Helper()
75 if !r.heldNow(wid) {
76 t.Fatalf("%s: rig: the instance does not hold the Mac", when)
77 }
78 if offered, held, choice := r.sandboxChoice(task); offered || !held || choice != wire.SandboxChoiceHeld || !machinesSay() {
79 t.Fatalf("%s: offered %v held line %v choice %q machines page says held %v", when, offered, held, choice, machinesSay())
80 }
81 if status := r.chooseUnsandboxed(task, r.checksStep(task)); status != 409 {
82 t.Fatalf("%s: the choice was taken: %d", when, status)
83 }
84 w.none(wire.FrameDispatch, 100*time.Millisecond)
85 var by string
86 if err := r.inst.DB.Writer().QueryRow("SELECT coalesce(unsandboxed_by, '') FROM chat_steps WHERE id = ?", r.checksStep(task).ID).Scan(&by); err != nil || by != "" {
87 t.Fatalf("%s: a choice recorded: %q %v", when, by, err)
88 }
89 }
90 offeredNow("nothing held")
91
92 // Connecting, before and after the Mac has the frame.
93 a, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, RunnerID: wid})
94 if err != nil {
95 t.Fatal(err)
96 }
97 notOffered("connecting")
98 w.expect(wire.FrameConnect)
99 w.heartbeat(d1Hello(keys, nil))
100 notOffered("connecting, the frame taken")
101
102 // A failed connect that left a Keychain item.
103 w.send(wire.FrameConnection, wire.ConnectionReport{Connection: a.ID, Person: Owner, Provider: wire.ProviderClaude, State: wire.ConnError, Reason: "provider_refused"})
104 w.heartbeat(d1Hello(keys, func(c *wire.Capabilities) {
105 c.ModelHold, c.HoldingItems = true, []string{"Claude Code-credentials-1a2b3c4d"}
106 }))
107 notOffered("a failed connect's Keychain item")
108
109 // The Mac says everything is gone: offered again.
110 w.heartbeat(d1Hello(keys, nil))
111 offeredNow("the item gone")
112
113 // Connected, then signed out: held until the Mac says the files are
114 // gone too.
115 b, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, Kind: wire.KindToken, RunnerID: wid})
116 if err != nil {
117 t.Fatal(err)
118 }
119 w.expect(wire.FrameConnect)
120 w.send(wire.FrameConnection, wire.ConnectionReport{Connection: b.ID, Person: Owner, LocalID: "claude-1", Provider: wire.ProviderClaude, State: wire.ConnHealthy, Label: "w@example.com"})
121 held := wire.HeldConnection{LocalID: "claude-1", Person: Owner, Provider: wire.ProviderClaude, Kind: wire.KindToken, Label: "w@example.com", State: wire.ConnHealthy}
122 w.heartbeat(d1Hello(keys, func(c *wire.Capabilities) { c.ModelHold = true }, held))
123 notOffered("connected")
124 if _, err := r.client.SignOut(r.ctx, b.ID); err != nil {
125 t.Fatal(err)
126 }
127 w.expect(wire.FrameDisconnect)
128 w.heartbeat(d1Hello(keys, func(c *wire.Capabilities) { c.ModelHold = true }))
129 notOffered("signed out, its files not yet gone")
130 w.heartbeat(d1Hello(keys, nil))
131 offeredNow("its files gone")
132
133 // Free again, the creator's choice runs as before.
134 if status := r.chooseUnsandboxed(task, s); status != 303 {
135 t.Fatalf("the choice once nothing is held: %d", status)
136 }
137 _, body := w.expect(wire.FrameDispatch)
138 if d := body.(*wire.DispatchCommand); !d.Unsandboxed || d.Attempt != 1 {
139 t.Fatalf("dispatch %+v", d)
140 }
141}
142
143// TestUnsandboxedChoiceWithdrawnOnConnect: the creator chose to run the
144// checks outside the sandbox and the attempt has not started on the Mac
145// when a Claude connection starts there. The choice is withdrawn (its
146// dispatch can no longer start, the next attempt waits for the sandbox
147// with no choice on it, audited) and the creator is told once, in the
148// task's lane. When the hold lifts the choice does not come back: the
149// creator may choose again.
150func TestUnsandboxedChoiceWithdrawnOnConnect(t *testing.T) {
151 r := newRunnersRig(t)
152 w, keys := r.d1Mac("Tito Studio")
153 wid := w.fr.Welcome.RunnerID
154 task, _ := r.candidate("iOS swipe", true)
155 s := r.waitsForSandbox(w, task, 1)
156 if status := r.chooseUnsandboxed(task, s); status != 303 {
157 t.Fatalf("choose: %d", status)
158 }
159 _, body := w.expect(wire.FrameDispatch)
160 d := body.(*wire.DispatchCommand)
161 if !d.Unsandboxed {
162 t.Fatalf("rig: dispatch %+v", d)
163 }
164
165 a, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, RunnerID: wid})
166 if err != nil {
167 t.Fatal(err)
168 }
169 if w.lease(d.StepID, d.Attempt) {
170 t.Fatal("the withdrawn choice's attempt started")
171 }
172 next := r.waitsForSandbox(w, task, 2)
173 if next.SandboxChoice != wire.SandboxChoiceHeld {
174 t.Fatalf("next attempt %+v", next)
175 }
176 var by string
177 if err := r.inst.DB.Writer().QueryRow("SELECT coalesce(unsandboxed_by, '') FROM chat_steps WHERE id = ?", s.ID).Scan(&by); err != nil || by != "" {
178 t.Fatalf("the choice stayed recorded: %q %v", by, err)
179 }
180 var audits int
181 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM audit_events WHERE kind = 'task.checks_unsandboxed_withdrawn'").Scan(&audits); err != nil || audits != 1 {
182 t.Fatalf("withdrawal audited %d times: %v", audits, err)
183 }
184 told := func() int {
185 t.Helper()
186 var n int
187 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_messages WHERE person = ? AND role = ? AND text LIKE ? AND text LIKE ?", Owner, wire.RoleCoordinator, "%Tito Studio%", "%"+task+"%").Scan(&n); err != nil {
188 t.Fatal(err)
189 }
190 return n
191 }
192 if told() != 1 {
193 t.Fatalf("the creator was told %d times", told())
194 }
195
196 // The Mac takes the frame, the attempt ends, the hold lifts: nothing
197 // runs by itself outside the sandbox, nobody is told again, and the
198 // choice is the creator's to make again.
199 w.expect(wire.FrameConnect)
200 w.send(wire.FrameConnection, wire.ConnectionReport{Connection: a.ID, Person: Owner, Provider: wire.ProviderClaude, State: wire.ConnError, Reason: "provider_refused"})
201 w.heartbeat(d1Hello(keys, nil))
202 w.none(wire.FrameDispatch, 150*time.Millisecond)
203 if r.heldNow(wid) {
204 t.Fatal("rig: still held")
205 }
206 if offered, _, choice := r.sandboxChoice(task); !offered || choice != wire.SandboxChoiceOffered {
207 t.Fatalf("not offered again once free: %v %q", offered, choice)
208 }
209 if told() != 1 {
210 t.Fatalf("told again: %d", told())
211 }
212}
213
214// TestUnsandboxedChoiceNotDispatchedWhileHeld: placement and the dispatch
215// read D1 themselves. A choice on the current attempt with the hold
216// already set (written by an instance from before D1, say: nothing here
217// withdrew it) never sends the checks to the Mac outside its sandbox;
218// once the Mac's sandbox is proven they go, without the choice.
219func TestUnsandboxedChoiceNotDispatchedWhileHeld(t *testing.T) {
220 r := newRunnersRig(t)
221 w, keys := r.d1Mac("Tito Studio")
222 wid := w.fr.Welcome.RunnerID
223 task, _ := r.candidate("iOS swipe", true)
224 s := r.waitsForSandbox(w, task, 1)
225 if _, err := r.inst.DB.Writer().Exec("UPDATE chat_steps SET unsandboxed_by = ?, unsandboxed_at = ?, unsandboxed_attempt = ?, unsandboxed_runner = ? WHERE id = ?", Owner, time.Now().UTC(), s.Attempt, wid, s.ID); err != nil {
226 t.Fatal(err)
227 }
228 if _, err := r.inst.DB.Writer().Exec("UPDATE runners SET credential_held_at = ? WHERE id = ?", time.Now().UTC(), wid); err != nil {
229 t.Fatal(err)
230 }
231 w.heartbeat(d1Hello(keys, func(c *wire.Capabilities) { c.ModelHold = true }))
232 if err := r.inst.Store.Chat().Place(r.ctx); err != nil {
233 t.Fatal(err)
234 }
235 r.waitsForSandbox(w, task, 1)
236 // Proven now: placed, sandboxed, the choice not carried.
237 h := d1Hello(keys, func(c *wire.Capabilities) { c.ModelHold = true })
238 h.Sandbox = wire.SandboxSeatbelt
239 w.heartbeat(h)
240 _, body := w.expect(wire.FrameDispatch)
241 if d := body.(*wire.DispatchCommand); d.StepID != s.ID || d.Unsandboxed {
242 t.Fatalf("dispatch while held %+v", d)
243 }
244}
245