| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo" |
| 11 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // macHello is a Mac worker's hello at protocol 7 with sandbox (seatbelt |
| 16 | // when probe P2 denied every hostile attempt, none otherwise). |
| 17 | func macHello(generation int64, label, sandbox string) wire.Hello { |
| 18 | h := WorkerHello(generation) |
| 19 | h.Protocol, h.Label, h.Sandbox = wire.ProtocolSandboxConsent, label, sandbox |
| 20 | return h |
| 21 | } |
| 22 | |
| 23 | // newMac enrols and connects a protocol 7 Mac worker saying sandbox. |
| 24 | func (r *runnersRig) newMac(label, sandbox string) *peer { |
| 25 | r.t.Helper() |
| 26 | token := r.inst.EnrolRunner(r.t).Token |
| 27 | fr, err := DialRunnerHello(r.ctx, r.inst.URL, token, macHello(0, label, sandbox)) |
| 28 | if err != nil { |
| 29 | r.t.Fatal(err) |
| 30 | } |
| 31 | r.t.Cleanup(fr.Close) |
| 32 | r.creds[fr] = [2]string{token, label} |
| 33 | return &peer{t: r.t, r: r.toolRig, fr: fr} |
| 34 | } |
| 35 | |
| 36 | func (r *runnersRig) taintedRunner(id string) bool { |
| 37 | r.t.Helper() |
| 38 | var n int |
| 39 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runners WHERE id = ? AND tainted_at IS NOT NULL", id).Scan(&n); err != nil { |
| 40 | r.t.Fatal(err) |
| 41 | } |
| 42 | return n == 1 |
| 43 | } |
| 44 | |
| 45 | // waitsForSandbox checks a task's checks wait for the Mac's sandbox at |
| 46 | // attempt, nothing sent anywhere: never a failure. |
| 47 | func (r *runnersRig) waitsForSandbox(w *peer, task string, attempt int64) wire.Step { |
| 48 | r.t.Helper() |
| 49 | s := r.checksStep(task) |
| 50 | k, err := r.client.Task(r.ctx, task) |
| 51 | if err != nil { |
| 52 | r.t.Fatal(err) |
| 53 | } |
| 54 | if s.State != wire.StepQueued || s.Placement != wire.PlacementSandbox || s.RunnerID != "" || s.Attempt != attempt || s.RunnerLabel == "" || k.NeedsYou != wire.NeedsMacSandbox || k.Checks == wire.ChecksFailed { |
| 55 | r.t.Fatalf("checks not waiting for the Mac's sandbox: %+v needs %q", s, k.NeedsYou) |
| 56 | } |
| 57 | w.none(wire.FrameDispatch, 200*time.Millisecond) |
| 58 | r.primary.none(wire.FrameSendPack, 0) |
| 59 | return s |
| 60 | } |
| 61 | |
| 62 | // The staging sequence (task tks6kwzzdngr, 2026-11-11): a Mac re-check's |
| 63 | // attempt ends runner_restarted, the Mac's P2 no longer passes (hello |
| 64 | // sandbox none), and then the coordinator's start_task and the task's |
| 65 | // Retry come. Nothing is dispatched, nothing runs outside the sandbox and |
| 66 | // the Mac stays untainted: the checks wait for the Mac's sandbox, past |
| 67 | // the offline limit, never a failure. Only the creator's own choice (the |
| 68 | // page's Run outside the sandbox, its confirm, for that attempt) sends |
| 69 | // them, carrying the consent, audited; a teammate, a stale attempt or a |
| 70 | // coordinator cannot. Running them taints the Mac. |
| 71 | func TestContractChecksWaitForTheMacsSandbox(t *testing.T) { |
| 72 | r := newRunnersRig(t) |
| 73 | w := r.newMac("Tito Studio", wire.SandboxSeatbelt) |
| 74 | task, sha := r.candidate("iOS swipe", true) |
| 75 | _, body := w.expect(wire.FrameDispatch) |
| 76 | d := body.(*wire.DispatchCommand) |
| 77 | if d.Unsandboxed { |
| 78 | t.Fatalf("a proven Mac's dispatch carried consent: %+v", d) |
| 79 | } |
| 80 | r.relay(w, []byte("PACK swipe")) |
| 81 | if !w.lease(d.StepID, 1) { |
| 82 | t.Fatal("start refused") |
| 83 | } |
| 84 | 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{}}) |
| 85 | w.heartbeat(macHello(0, "Tito Studio", wire.SandboxNone)) |
| 86 | |
| 87 | if res := r.call("again", wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}); !res.IsError { |
| 88 | t.Fatalf("start_task on a started task: %+v", res) |
| 89 | } |
| 90 | if acc, err := r.client.RetryTask(r.ctx, task); err != nil || acc.Attempt != 2 { |
| 91 | t.Fatalf("retry %+v %v", acc, err) |
| 92 | } |
| 93 | s := r.waitsForSandbox(w, task, 2) |
| 94 | if s.RunnerLabel != "Tito Studio" || r.taintedRunner(w.fr.Welcome.RunnerID) { |
| 95 | t.Fatalf("waits for %q, or waiting tainted the Mac", s.RunnerLabel) |
| 96 | } |
| 97 | clock := time.Now().UTC().Add(chat.OfflineLimit + time.Minute) |
| 98 | r.inst.Store.Chat().SetClock(func() time.Time { return clock }) |
| 99 | w.heartbeat(macHello(0, "Tito Studio", wire.SandboxNone)) |
| 100 | if err := r.inst.Store.Chat().Place(r.ctx); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | r.waitsForSandbox(w, task, 2) |
| 104 | r.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() }) |
| 105 | w.heartbeat(macHello(0, "Tito Studio", wire.SandboxNone)) |
| 106 | |
| 107 | // Nobody but the creator, for exactly the attempt shown, from the page. |
| 108 | owner := r.inst.Cookie() |
| 109 | personToolRig(t, r.toolRig, "mate@example.com", equipo.RoleOwner) |
| 110 | mate := r.inst.SessionFor(t, "mate@example.com") |
| 111 | choose := func(who *http.Cookie, attempt string) int { |
| 112 | status, _ := r.post(who, "/conversation/tasks/"+task+"/unsandboxed", url.Values{"from": {"task"}, "step": {s.ID}, "attempt": {attempt}}) |
| 113 | return status |
| 114 | } |
| 115 | if status := choose(mate, "2"); status != 403 { |
| 116 | t.Fatalf("a teammate chose for the creator: %d", status) |
| 117 | } |
| 118 | if status := choose(owner, "1"); status != 409 { |
| 119 | t.Fatalf("a stale attempt's choice: %d", status) |
| 120 | } |
| 121 | r.waitsForSandbox(w, task, 2) |
| 122 | if r.taintedRunner(w.fr.Welcome.RunnerID) { |
| 123 | t.Fatal("waiting tainted the Mac") |
| 124 | } |
| 125 | |
| 126 | // The task page says it waits and offers the choice behind a warning. |
| 127 | page := r.get(owner, "/tasks/"+task, 200) |
| 128 | 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."} { |
| 129 | if !strings.Contains(page, want) { |
| 130 | t.Fatalf("task page lacks %q: %s", want, page) |
| 131 | } |
| 132 | } |
| 133 | if status := choose(owner, "2"); status != 303 { |
| 134 | t.Fatalf("run outside the sandbox from the page: %d", status) |
| 135 | } |
| 136 | _, body = w.expect(wire.FrameDispatch) |
| 137 | d = body.(*wire.DispatchCommand) |
| 138 | if d.StepID != s.ID || d.Attempt != 2 || !d.Unsandboxed || d.CandidateSHA != sha { |
| 139 | t.Fatalf("chosen dispatch %+v", d) |
| 140 | } |
| 141 | var by, runner string |
| 142 | var attempt int64 |
| 143 | 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 { |
| 144 | t.Fatalf("choice not recorded: %q %d %q %v", by, attempt, runner, err) |
| 145 | } |
| 146 | var audits int |
| 147 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM audit_events WHERE kind = 'task.checks_unsandboxed'").Scan(&audits); err != nil || audits != 1 { |
| 148 | t.Fatalf("choice not audited: %d %v", audits, err) |
| 149 | } |
| 150 | r.relay(w, []byte("PACK swipe again")) |
| 151 | if !w.lease(d.StepID, 2) { |
| 152 | t.Fatal("chosen attempt refused") |
| 153 | } |
| 154 | pass := passed(task, d.StepID, 2, sha) |
| 155 | pass.ChecksSandbox = wire.SandboxNone |
| 156 | w.send(wire.FrameStep, pass) |
| 157 | r.expect(wire.FrameDispatch) // review on the primary |
| 158 | if !r.taintedRunner(w.fr.Welcome.RunnerID) { |
| 159 | t.Fatal("checks run outside the sandbox did not taint the Mac") |
| 160 | } |
| 161 | if k, _ := r.client.Task(r.ctx, task); k.ChecksSandbox != wire.SandboxNone { |
| 162 | t.Fatalf("not labelled outside the sandbox: %+v", k) |
| 163 | } |
| 164 | // Tainted now: the next task's checks wait, whatever the hello says. |
| 165 | w.heartbeat(macHello(0, "Tito Studio", wire.SandboxSeatbelt)) |
| 166 | next, _ := r.candidate("iOS next", true) |
| 167 | r.waitsForSandbox(w, next, 1) |
| 168 | } |
| 169 | |
| 170 | // When the Mac's P2 passes, checks waiting for its sandbox run in it on |
| 171 | // their own, without consent. A Mac that refuses unproven checks |
| 172 | // (sandbox_unproven, its hello out of date) puts the step back to wait: |
| 173 | // a new attempt, no failure, no taint, and its next hello saying it is |
| 174 | // proven sends it again. |
| 175 | func TestContractChecksRunOnceTheSandboxIsProven(t *testing.T) { |
| 176 | r := newRunnersRig(t) |
| 177 | w := r.newMac("Tito Studio", wire.SandboxNone) |
| 178 | task, sha := r.candidate("iOS swipe", true) |
| 179 | r.waitsForSandbox(w, task, 1) |
| 180 | w.heartbeat(macHello(0, "Tito Studio", wire.SandboxSeatbelt)) |
| 181 | _, body := w.expect(wire.FrameDispatch) |
| 182 | d := body.(*wire.DispatchCommand) |
| 183 | if d.TaskID != task || d.Attempt != 1 || d.Unsandboxed || d.CandidateSHA != sha { |
| 184 | t.Fatalf("dispatch once proven %+v", d) |
| 185 | } |
| 186 | r.relay(w, []byte("PACK swipe")) |
| 187 | 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{}}) |
| 188 | r.waitsForSandbox(w, task, 2) |
| 189 | if s := r.checksStep(task); s.Reason != "" || r.taintedRunner(w.fr.Welcome.RunnerID) { |
| 190 | t.Fatalf("the refusal counted or tainted: %+v", s) |
| 191 | } |
| 192 | w.heartbeat(macHello(0, "Tito Studio", wire.SandboxSeatbelt)) |
| 193 | _, body = w.expect(wire.FrameDispatch) |
| 194 | if d := body.(*wire.DispatchCommand); d.Attempt != 2 || d.Unsandboxed { |
| 195 | t.Fatalf("dispatch once proven again %+v", d) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // The automatic Mac re-check never fires for a Mac whose sandbox is not |
| 200 | // proven: enrolling and coming back place no step; the first hello saying |
| 201 | // it is proven re-checks. |
| 202 | func TestContractMacRecheckWaitsForTheSandbox(t *testing.T) { |
| 203 | r := newRunnersRigAt(t, wire.ProtocolInjectionDefences) |
| 204 | task, sha, _ := r.waitingForMac("Swipe back on iOS") |
| 205 | w := r.newMac("Tito Studio", wire.SandboxNone) |
| 206 | w.none(wire.FrameDispatch, 200*time.Millisecond) |
| 207 | w = r.reconnectPeerHello(w, macHello(w.fr.Welcome.Generation, "Tito Studio", wire.SandboxNone)) |
| 208 | w.none(wire.FrameDispatch, 200*time.Millisecond) |
| 209 | if runs := r.checksRuns(task); len(runs) != 1 { |
| 210 | t.Fatalf("an unproven Mac re-ran the checks: %+v", runs) |
| 211 | } |
| 212 | if r.taintedRunner(w.fr.Welcome.RunnerID) { |
| 213 | t.Fatal("tainted") |
| 214 | } |
| 215 | w.heartbeat(macHello(0, "Tito Studio", wire.SandboxSeatbelt)) |
| 216 | d := macRecheck(t, w, task, sha) |
| 217 | if d.Unsandboxed { |
| 218 | t.Fatalf("re-check carried consent %+v", d) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // reconnectPeerHello dials a runner again with its credential and hello. |
| 223 | func (r *runnersRig) reconnectPeerHello(p *peer, h wire.Hello) *peer { |
| 224 | r.t.Helper() |
| 225 | c, ok := r.creds[p.fr] |
| 226 | if !ok { |
| 227 | r.t.Fatal("unknown peer") |
| 228 | } |
| 229 | fr, err := DialRunnerHello(r.ctx, r.inst.URL, c[0], h) |
| 230 | if err != nil { |
| 231 | r.t.Fatal(err) |
| 232 | } |
| 233 | r.t.Cleanup(fr.Close) |
| 234 | r.creds[fr] = c |
| 235 | return &peer{t: r.t, r: r.toolRig, fr: fr} |
| 236 | } |
| 237 | |