package thintest import ( "net/url" "strconv" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/sealbox" "amadan.net/oficina/equipo/internal/wire" ) // D1 (Paul, 2026-11-28): once a Mac holds a Claude credential, its checks // run sandboxed or not at all. While the instance holds a Mac as having // one (from connecting until the Mac says its files and Keychain items // are gone), the task page offers no Run outside the sandbox for checks // waiting for that Mac and says why; the choice is refused if posted, and // nothing is dispatched outside the sandbox. A choice already recorded on // a waiting attempt is withdrawn when a connection starts, and its // creator is told. // d1Mac is a Mac worker at 37 whose sandbox is not proven (hello sandbox // none), with its seal key, saying its whole inventory. func (r *runnersRig) d1Mac(label string) (*peer, sealbox.Keys) { r.t.Helper() keys := sealKeys(r.t) w := r.newMacEngineer(label, wire.EngineeringNoConnection) w.heartbeat(d1Hello(keys, nil)) return w, keys } // d1Hello is that Mac's full hello, sandbox none; change sets its hold. func d1Hello(keys sealbox.Keys, change func(*wire.Capabilities), conns ...wire.HeldConnection) wire.Hello { h := inventoryHello(keys.PublicString(), change, conns...) h.Sandbox = wire.SandboxNone return h } // sandboxChoice is what the task page shows its creator for checks // waiting for the Mac's sandbox: the offer, or the line saying the Mac // holds Claude; and the step's word on it. func (r *runnersRig) sandboxChoice(task string) (offered, held bool, choice string) { r.t.Helper() page := r.get(r.inst.Cookie(), "/tasks/"+task, 200) return strings.Contains(page, "data-task-unsandboxed"), strings.Contains(page, "data-sandbox-held"), r.checksStep(task).SandboxChoice } // chooseUnsandboxed posts the creator's Run outside the sandbox for the // checks step's attempt, as the page's confirm does. func (r *runnersRig) chooseUnsandboxed(task string, s wire.Step) int { r.t.Helper() status, _ := r.post(r.inst.Cookie(), "/conversation/tasks/"+task+"/unsandboxed", url.Values{"from": {"task"}, "step": {s.ID}, "attempt": {strconv.FormatInt(s.Attempt, 10)}}) return status } func TestUnsandboxedNotOfferedWithCredential(t *testing.T) { r := newRunnersRig(t) w, keys := r.d1Mac("Tito Studio") wid := w.fr.Welcome.RunnerID task, _ := r.candidate("iOS swipe", true) s := r.waitsForSandbox(w, task, 1) machinesSay := func() bool { t.Helper() return strings.Contains(r.get(r.inst.Cookie(), "/conversation/settings/machines", 200), "data-holds-claude") } offeredNow := func(when string) { t.Helper() if offered, held, choice := r.sandboxChoice(task); !offered || held || choice != wire.SandboxChoiceOffered || machinesSay() { t.Fatalf("%s: offered %v held line %v choice %q machines page says held %v", when, offered, held, choice, machinesSay()) } } notOffered := func(when string) { t.Helper() if !r.heldNow(wid) { t.Fatalf("%s: rig: the instance does not hold the Mac", when) } if offered, held, choice := r.sandboxChoice(task); offered || !held || choice != wire.SandboxChoiceHeld || !machinesSay() { t.Fatalf("%s: offered %v held line %v choice %q machines page says held %v", when, offered, held, choice, machinesSay()) } if status := r.chooseUnsandboxed(task, r.checksStep(task)); status != 409 { t.Fatalf("%s: the choice was taken: %d", when, status) } w.none(wire.FrameDispatch, 100*time.Millisecond) var by string 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 != "" { t.Fatalf("%s: a choice recorded: %q %v", when, by, err) } } offeredNow("nothing held") // Connecting, before and after the Mac has the frame. a, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, RunnerID: wid}) if err != nil { t.Fatal(err) } notOffered("connecting") w.expect(wire.FrameConnect) w.heartbeat(d1Hello(keys, nil)) notOffered("connecting, the frame taken") // A failed connect that left a Keychain item. w.send(wire.FrameConnection, wire.ConnectionReport{Connection: a.ID, Person: Owner, Provider: wire.ProviderClaude, State: wire.ConnError, Reason: "provider_refused"}) w.heartbeat(d1Hello(keys, func(c *wire.Capabilities) { c.ModelHold, c.HoldingItems = true, []string{"Claude Code-credentials-1a2b3c4d"} })) notOffered("a failed connect's Keychain item") // The Mac says everything is gone: offered again. w.heartbeat(d1Hello(keys, nil)) offeredNow("the item gone") // Connected, then signed out: held until the Mac says the files are // gone too. b, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, Kind: wire.KindToken, RunnerID: wid}) if err != nil { t.Fatal(err) } w.expect(wire.FrameConnect) w.send(wire.FrameConnection, wire.ConnectionReport{Connection: b.ID, Person: Owner, LocalID: "claude-1", Provider: wire.ProviderClaude, State: wire.ConnHealthy, Label: "w@example.com"}) held := wire.HeldConnection{LocalID: "claude-1", Person: Owner, Provider: wire.ProviderClaude, Kind: wire.KindToken, Label: "w@example.com", State: wire.ConnHealthy} w.heartbeat(d1Hello(keys, func(c *wire.Capabilities) { c.ModelHold = true }, held)) notOffered("connected") if _, err := r.client.SignOut(r.ctx, b.ID); err != nil { t.Fatal(err) } w.expect(wire.FrameDisconnect) w.heartbeat(d1Hello(keys, func(c *wire.Capabilities) { c.ModelHold = true })) notOffered("signed out, its files not yet gone") w.heartbeat(d1Hello(keys, nil)) offeredNow("its files gone") // Free again, the creator's choice runs as before. if status := r.chooseUnsandboxed(task, s); status != 303 { t.Fatalf("the choice once nothing is held: %d", status) } _, body := w.expect(wire.FrameDispatch) if d := body.(*wire.DispatchCommand); !d.Unsandboxed || d.Attempt != 1 { t.Fatalf("dispatch %+v", d) } } // TestUnsandboxedChoiceWithdrawnOnConnect: the creator chose to run the // checks outside the sandbox and the attempt has not started on the Mac // when a Claude connection starts there. The choice is withdrawn (its // dispatch can no longer start, the next attempt waits for the sandbox // with no choice on it, audited) and the creator is told once, in the // task's lane. When the hold lifts the choice does not come back: the // creator may choose again. func TestUnsandboxedChoiceWithdrawnOnConnect(t *testing.T) { r := newRunnersRig(t) w, keys := r.d1Mac("Tito Studio") wid := w.fr.Welcome.RunnerID task, _ := r.candidate("iOS swipe", true) s := r.waitsForSandbox(w, task, 1) if status := r.chooseUnsandboxed(task, s); status != 303 { t.Fatalf("choose: %d", status) } _, body := w.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) if !d.Unsandboxed { t.Fatalf("rig: dispatch %+v", d) } a, err := r.client.Connect(r.ctx, wire.Connect{Provider: wire.ProviderClaude, RunnerID: wid}) if err != nil { t.Fatal(err) } if w.lease(d.StepID, d.Attempt) { t.Fatal("the withdrawn choice's attempt started") } next := r.waitsForSandbox(w, task, 2) if next.SandboxChoice != wire.SandboxChoiceHeld { t.Fatalf("next attempt %+v", next) } var by string if err := r.inst.DB.Writer().QueryRow("SELECT coalesce(unsandboxed_by, '') FROM chat_steps WHERE id = ?", s.ID).Scan(&by); err != nil || by != "" { t.Fatalf("the choice stayed recorded: %q %v", by, err) } var audits int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM audit_events WHERE kind = 'task.checks_unsandboxed_withdrawn'").Scan(&audits); err != nil || audits != 1 { t.Fatalf("withdrawal audited %d times: %v", audits, err) } told := func() int { t.Helper() var n int 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 { t.Fatal(err) } return n } if told() != 1 { t.Fatalf("the creator was told %d times", told()) } // The Mac takes the frame, the attempt ends, the hold lifts: nothing // runs by itself outside the sandbox, nobody is told again, and the // choice is the creator's to make again. w.expect(wire.FrameConnect) w.send(wire.FrameConnection, wire.ConnectionReport{Connection: a.ID, Person: Owner, Provider: wire.ProviderClaude, State: wire.ConnError, Reason: "provider_refused"}) w.heartbeat(d1Hello(keys, nil)) w.none(wire.FrameDispatch, 150*time.Millisecond) if r.heldNow(wid) { t.Fatal("rig: still held") } if offered, _, choice := r.sandboxChoice(task); !offered || choice != wire.SandboxChoiceOffered { t.Fatalf("not offered again once free: %v %q", offered, choice) } if told() != 1 { t.Fatalf("told again: %d", told()) } } // TestUnsandboxedChoiceNotDispatchedWhileHeld: placement and the dispatch // read D1 themselves. A choice on the current attempt with the hold // already set (written by an instance from before D1, say: nothing here // withdrew it) never sends the checks to the Mac outside its sandbox; // once the Mac's sandbox is proven they go, without the choice. func TestUnsandboxedChoiceNotDispatchedWhileHeld(t *testing.T) { r := newRunnersRig(t) w, keys := r.d1Mac("Tito Studio") wid := w.fr.Welcome.RunnerID task, _ := r.candidate("iOS swipe", true) s := r.waitsForSandbox(w, task, 1) 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 { t.Fatal(err) } if _, err := r.inst.DB.Writer().Exec("UPDATE runners SET credential_held_at = ? WHERE id = ?", time.Now().UTC(), wid); err != nil { t.Fatal(err) } w.heartbeat(d1Hello(keys, func(c *wire.Capabilities) { c.ModelHold = true })) if err := r.inst.Store.Chat().Place(r.ctx); err != nil { t.Fatal(err) } r.waitsForSandbox(w, task, 1) // Proven now: placed, sandboxed, the choice not carried. h := d1Hello(keys, func(c *wire.Capabilities) { c.ModelHold = true }) h.Sandbox = wire.SandboxSeatbelt w.heartbeat(h) _, body := w.expect(wire.FrameDispatch) if d := body.(*wire.DispatchCommand); d.StepID != s.ID || d.Unsandboxed { t.Fatalf("dispatch while held %+v", d) } }