package thintest import ( "net/http" "net/url" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/chat" "amadan.net/oficina/equipo/internal/wire" ) // macHello is a Mac worker's hello at protocol 7 with sandbox (seatbelt // when probe P2 denied every hostile attempt, none otherwise). func macHello(generation int64, label, sandbox string) wire.Hello { h := WorkerHello(generation) h.Protocol, h.Label, h.Sandbox = wire.ProtocolSandboxConsent, label, sandbox return h } // newMac enrols and connects a protocol 7 Mac worker saying sandbox. func (r *runnersRig) newMac(label, sandbox string) *peer { r.t.Helper() token := r.inst.EnrolRunner(r.t).Token fr, err := DialRunnerHello(r.ctx, r.inst.URL, token, macHello(0, label, sandbox)) if err != nil { r.t.Fatal(err) } r.t.Cleanup(fr.Close) r.creds[fr] = [2]string{token, label} return &peer{t: r.t, r: r.toolRig, fr: fr} } func (r *runnersRig) taintedRunner(id string) bool { r.t.Helper() var n int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runners WHERE id = ? AND tainted_at IS NOT NULL", id).Scan(&n); err != nil { r.t.Fatal(err) } return n == 1 } // waitsForSandbox checks a task's checks wait for the Mac's sandbox at // attempt, nothing sent anywhere: never a failure. func (r *runnersRig) waitsForSandbox(w *peer, task string, attempt int64) wire.Step { r.t.Helper() s := r.checksStep(task) k, err := r.client.Task(r.ctx, task) if err != nil { r.t.Fatal(err) } if s.State != wire.StepQueued || s.Placement != wire.PlacementSandbox || s.RunnerID != "" || s.Attempt != attempt || s.RunnerLabel == "" || k.NeedsYou != wire.NeedsMacSandbox || k.Checks == wire.ChecksFailed { r.t.Fatalf("checks not waiting for the Mac's sandbox: %+v needs %q", s, k.NeedsYou) } w.none(wire.FrameDispatch, 200*time.Millisecond) r.primary.none(wire.FrameSendPack, 0) return s } // The staging sequence (task tks6kwzzdngr, 2026-11-11): a Mac re-check's // attempt ends runner_restarted, the Mac's P2 no longer passes (hello // sandbox none), and then the coordinator's start_task and the task's // Retry come. Nothing is dispatched, nothing runs outside the sandbox and // the Mac stays untainted: the checks wait for the Mac's sandbox, past // the offline limit, never a failure. Only the creator's own choice (the // page's Run outside the sandbox, its confirm, for that attempt) sends // them, carrying the consent, audited; a teammate, a stale attempt or a // coordinator cannot. Running them taints the Mac. func TestContractChecksWaitForTheMacsSandbox(t *testing.T) { r := newRunnersRig(t) w := r.newMac("Tito Studio", wire.SandboxSeatbelt) task, sha := r.candidate("iOS swipe", true) _, body := w.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) if d.Unsandboxed { t.Fatalf("a proven Mac's dispatch carried consent: %+v", d) } r.relay(w, []byte("PACK swipe")) if !w.lease(d.StepID, 1) { t.Fatal("start refused") } w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "runner_restarted", Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}}) w.heartbeat(macHello(0, "Tito Studio", wire.SandboxNone)) if res := r.call("again", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}); !res.IsError { t.Fatalf("start_task on a started task: %+v", res) } if acc, err := r.client.RetryTask(r.ctx, task); err != nil || acc.Attempt != 2 { t.Fatalf("retry %+v %v", acc, err) } s := r.waitsForSandbox(w, task, 2) if s.RunnerLabel != "Tito Studio" || r.taintedRunner(w.fr.Welcome.RunnerID) { t.Fatalf("waits for %q, or waiting tainted the Mac", s.RunnerLabel) } clock := time.Now().UTC().Add(chat.OfflineLimit + time.Minute) r.inst.Store.Chat().SetClock(func() time.Time { return clock }) w.heartbeat(macHello(0, "Tito Studio", wire.SandboxNone)) if err := r.inst.Store.Chat().Place(r.ctx); err != nil { t.Fatal(err) } r.waitsForSandbox(w, task, 2) r.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() }) w.heartbeat(macHello(0, "Tito Studio", wire.SandboxNone)) // Nobody but the creator, for exactly the attempt shown, from the page. owner := r.inst.Cookie() personToolRig(t, r.toolRig, "mate@example.com", equipo.RoleOwner) mate := r.inst.SessionFor(t, "mate@example.com") choose := func(who *http.Cookie, attempt string) int { status, _ := r.post(who, "/conversation/tasks/"+task+"/unsandboxed", url.Values{"from": {"task"}, "step": {s.ID}, "attempt": {attempt}}) return status } if status := choose(mate, "2"); status != 403 { t.Fatalf("a teammate chose for the creator: %d", status) } if status := choose(owner, "1"); status != 409 { t.Fatalf("a stale attempt's choice: %d", status) } r.waitsForSandbox(w, task, 2) if r.taintedRunner(w.fr.Welcome.RunnerID) { t.Fatal("waiting tainted the Mac") } // The task page says it waits and offers the choice behind a warning. page := r.get(owner, "/tasks/"+task, 200) for _, want := range []string{"Waiting for the Mac", "data-task-unsandboxed", `action="/conversation/tasks/` + task + `/unsandboxed"`, "This runs the checks on Tito Studio outside the sandbox. They can read anything that account can. The Mac is then marked tainted."} { if !strings.Contains(page, want) { t.Fatalf("task page lacks %q: %s", want, page) } } if status := choose(owner, "2"); status != 303 { t.Fatalf("run outside the sandbox from the page: %d", status) } _, body = w.expect(wire.FrameDispatch) d = body.(*wire.DispatchCommand) if d.StepID != s.ID || d.Attempt != 2 || !d.Unsandboxed || d.CandidateSHA != sha { t.Fatalf("chosen dispatch %+v", d) } var by, runner string var attempt int64 if err := r.inst.DB.Writer().QueryRow("SELECT unsandboxed_by, unsandboxed_attempt, unsandboxed_runner FROM chat_steps WHERE id = ? AND unsandboxed_at IS NOT NULL", s.ID).Scan(&by, &attempt, &runner); err != nil || by != Owner || attempt != 2 || runner != w.fr.Welcome.RunnerID { t.Fatalf("choice not recorded: %q %d %q %v", by, attempt, runner, err) } var audits int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM audit_events WHERE kind = 'task.checks_unsandboxed'").Scan(&audits); err != nil || audits != 1 { t.Fatalf("choice not audited: %d %v", audits, err) } r.relay(w, []byte("PACK swipe again")) if !w.lease(d.StepID, 2) { t.Fatal("chosen attempt refused") } pass := passed(task, d.StepID, 2, sha) pass.ChecksSandbox = wire.SandboxNone w.send(wire.FrameStep, pass) r.expect(wire.FrameDispatch) // review on the primary if !r.taintedRunner(w.fr.Welcome.RunnerID) { t.Fatal("checks run outside the sandbox did not taint the Mac") } if k, _ := r.client.Task(r.ctx, task); k.ChecksSandbox != wire.SandboxNone { t.Fatalf("not labelled outside the sandbox: %+v", k) } // Tainted now: the next task's checks wait, whatever the hello says. w.heartbeat(macHello(0, "Tito Studio", wire.SandboxSeatbelt)) next, _ := r.candidate("iOS next", true) r.waitsForSandbox(w, next, 1) } // When the Mac's P2 passes, checks waiting for its sandbox run in it on // their own, without consent. A Mac that refuses unproven checks // (sandbox_unproven, its hello out of date) puts the step back to wait: // a new attempt, no failure, no taint, and its next hello saying it is // proven sends it again. func TestContractChecksRunOnceTheSandboxIsProven(t *testing.T) { r := newRunnersRig(t) w := r.newMac("Tito Studio", wire.SandboxNone) task, sha := r.candidate("iOS swipe", true) r.waitsForSandbox(w, task, 1) w.heartbeat(macHello(0, "Tito Studio", wire.SandboxSeatbelt)) _, body := w.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) if d.TaskID != task || d.Attempt != 1 || d.Unsandboxed || d.CandidateSHA != sha { t.Fatalf("dispatch once proven %+v", d) } r.relay(w, []byte("PACK swipe")) w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: wire.ReasonSandboxUnproven, Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}}) r.waitsForSandbox(w, task, 2) if s := r.checksStep(task); s.Reason != "" || r.taintedRunner(w.fr.Welcome.RunnerID) { t.Fatalf("the refusal counted or tainted: %+v", s) } w.heartbeat(macHello(0, "Tito Studio", wire.SandboxSeatbelt)) _, body = w.expect(wire.FrameDispatch) if d := body.(*wire.DispatchCommand); d.Attempt != 2 || d.Unsandboxed { t.Fatalf("dispatch once proven again %+v", d) } } // The automatic Mac re-check never fires for a Mac whose sandbox is not // proven: enrolling and coming back place no step; the first hello saying // it is proven re-checks. func TestContractMacRecheckWaitsForTheSandbox(t *testing.T) { r := newRunnersRigAt(t, wire.ProtocolInjectionDefences) task, sha, _ := r.waitingForMac("Swipe back on iOS") w := r.newMac("Tito Studio", wire.SandboxNone) w.none(wire.FrameDispatch, 200*time.Millisecond) w = r.reconnectPeerHello(w, macHello(w.fr.Welcome.Generation, "Tito Studio", wire.SandboxNone)) w.none(wire.FrameDispatch, 200*time.Millisecond) if runs := r.checksRuns(task); len(runs) != 1 { t.Fatalf("an unproven Mac re-ran the checks: %+v", runs) } if r.taintedRunner(w.fr.Welcome.RunnerID) { t.Fatal("tainted") } w.heartbeat(macHello(0, "Tito Studio", wire.SandboxSeatbelt)) d := macRecheck(t, w, task, sha) if d.Unsandboxed { t.Fatalf("re-check carried consent %+v", d) } } // reconnectPeerHello dials a runner again with its credential and hello. func (r *runnersRig) reconnectPeerHello(p *peer, h wire.Hello) *peer { r.t.Helper() c, ok := r.creds[p.fr] if !ok { r.t.Fatal("unknown peer") } fr, err := DialRunnerHello(r.ctx, r.inst.URL, c[0], h) if err != nil { r.t.Fatal(err) } r.t.Cleanup(fr.Close) r.creds[fr] = c return &peer{t: r.t, r: r.toolRig, fr: fr} }