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 "context"
5 "encoding/json"
6 "slices"
7 "strconv"
8
9 "amadan.net/oficina/equipo/internal/wire"
10)
11
12// Checks never run outside a Mac's sandbox unless a person chose it, and
13// never on a Mac that holds a model credential (D1, unsandboxedAllowed).
14// A Mac whose sandbox is not proven (its hello says other than seatbelt:
15// probe P2 did not deny every hostile attempt, or the runner tainted
16// itself) or that the instance holds tainted gets no checks: they wait
17// (wire.PlacementSandbox) and run by themselves once its sandbox is
18// proven. Only the task's creator, through the task's own Run outside
19// the sandbox (a page with a confirm, never a coordinator tool), may
20// choose otherwise, for one attempt on one Mac; the choice is recorded
21// on the step (who and when) and audited. The worker refuses too
22// (sandbox_unproven) when a dispatch carries no such choice.
23
24// sandboxProven reports whether checks may run on r in its sandbox: not
25// a Mac, or a Mac whose hello says seatbelt and that the instance holds
26// untainted.
27func (r runnerInfo) sandboxProven() bool {
28 if !slices.Contains(r.tags(), wire.TagMacOS) {
29 return true
30 }
31 return r.Hello.Sandbox == wire.SandboxSeatbelt && !r.Tainted
32}
33
34// unsandboxedChosen reports whether the task's creator chose to run the
35// step's current attempt on runner outside the sandbox.
36func unsandboxedChosen(s stepRecord, runner string) bool {
37 return s.UnsandboxedBy != "" && s.UnsandboxedAt != nil && s.UnsandboxedAttempt == s.Attempt && s.UnsandboxedRunner == runner && runner != ""
38}
39
40// unsandboxedAllowed is D1 on the instance (Paul, 2026-11-28): once a Mac
41// holds a Claude credential, its checks run sandboxed or not at all. The
42// creator may choose to run checks on runner outside its sandbox only
43// while the instance holds no model credential there (from connecting
44// until the Mac says its files and Keychain items are gone). A lookup
45// error denies. If Paul rules otherwise, this and the worker's
46// unsandboxedAllowed are the change (and the warning names the Claude
47// connection).
48func (t *txn) unsandboxedAllowed(runner string) (bool, error) {
49 held, err := t.macCredentialHeld(runner)
50 return err == nil && !held, err
51}
52
53// withdrawUnsandboxed withdraws every choice to run checks on runner
54// outside the sandbox that an attempt not yet started there carries, now
55// that the instance holds runner as having a model credential (D1): the
56// choice is cleared and audited, an attempt already dispatched there is
57// taken back (it can never start; the next waits for the sandbox), and
58// the creator is told once, in the task's lane. An attempt that started
59// is left: the Mac tainted itself before it ran, so it takes no
60// credential.
61func (t *txn) withdrawUnsandboxed(runner string) error {
62 var steps []stepRecord
63 if err := t.db.Where("unsandboxed_runner = ? AND unsandboxed_at IS NOT NULL AND unsandboxed_attempt = attempt AND specialist = ? AND state = ? AND started_at IS NULL", runner, wire.SpecialistChecks, wire.StepQueued).Find(&steps).Error; err != nil {
64 return err
65 }
66 for _, st := range steps {
67 k, err := t.loadTask(st.TaskID)
68 if err != nil {
69 return err
70 }
71 attempt := st.Attempt
72 st.UnsandboxedBy, st.UnsandboxedAt, st.UnsandboxedAttempt, st.UnsandboxedRunner = "", nil, 0, ""
73 if t.s.authority.Audit != nil {
74 if err := t.s.authority.Audit(t.db, "instance", "task.checks_unsandboxed_withdrawn", st.ID+"/"+strconv.FormatInt(attempt, 10)+"@"+runner); err != nil {
75 return err
76 }
77 }
78 if st.RunnerID == runner {
79 // Dispatched with the choice and not started: it never starts.
80 if err := t.withdraw(&k, &st); err != nil {
81 return err
82 }
83 } else if err := t.saveStep(&st, &k); err != nil {
84 return err
85 }
86 if err := t.tellUnsandboxedWithdrawn(k, st.ID, attempt, runner); err != nil {
87 return err
88 }
89 }
90 return nil
91}
92
93// tellUnsandboxedWithdrawn tells the task's creator, once per attempt,
94// that their choice to run its checks on runner outside the sandbox went
95// away, and why.
96func (t *txn) tellUnsandboxedWithdrawn(k taskRecord, step string, attempt int64, runner string) error {
97 text := t.words("work.decide.unsandboxed_withdrawn", mdLabel(k.Title), TaskLink(k.ID), t.runnerLabel(runner))
98 if text == "" {
99 return nil
100 }
101 idem := "unsandboxed-withdrawn:" + step + ":" + strconv.FormatInt(attempt, 10)
102 var n int64
103 if err := t.db.Model(&messageRecord{}).Where("person = ? AND idempotency_key = ?", k.Creator, idem).Count(&n).Error; err != nil || n > 0 {
104 return err
105 }
106 m := messageRecord{ID: newID("m_"), Person: k.Creator, IdempotencyKey: idem, Role: wire.RoleCoordinator, Text: text, Revision: 1, CreatedAt: t.now(), Lane: k.Lane}
107 if err := t.db.Create(&m).Error; err != nil {
108 return err
109 }
110 return t.messageEvent(m)
111}
112
113// unsandboxedOK reports whether the step's current attempt may run on
114// runner outside the sandbox: the creator chose it, and D1 allows it.
115func (t *txn) unsandboxedOK(s stepRecord, runner string) (bool, error) {
116 if !unsandboxedChosen(s, runner) {
117 return false, nil
118 }
119 return t.unsandboxedAllowed(runner)
120}
121
122// stepRunnerLabel names where a step's attempt runs, or, while it is
123// placed nowhere, the runner it waits for: a Mac's sandbox, a Mac's
124// Claude connection or update (or the primary's update), the base for
125// that Mac, or that Mac back online.
126func (t *txn) stepRunnerLabel(s stepRecord) string {
127 if s.Placement == wire.PlacementSandbox || s.RunnerID == "" && s.SandboxRunner != "" {
128 return t.runnerLabel(s.SandboxRunner)
129 }
130 return t.runnerLabel(s.RunnerID)
131}
132
133// stepWaitView adds to a step's view what the runner it waits for says
134// now: while it waits for a Mac's Claude connection, that Mac's
135// Engineering status (read when rendered, never stored); while checks
136// wait for its sandbox, whether the creator may run them outside (D1);
137// and the worker Engineering ran on.
138func (t *txn) stepWaitView(sv *wire.Step, s stepRecord) {
139 sv.RunnerLabel = t.stepRunnerLabel(s)
140 if s.RunnerID == "" {
141 // Placed nowhere: the runner it waits for, by id (RunnerLabel
142 // names it).
143 sv.WaitingFor = s.SandboxRunner
144 }
145 if s.Specialist == wire.SpecialistEngineering && s.RunnerID != "" && s.StartedAt != nil && s.OnWorker {
146 // Made on a worker, as its dispatch recorded: named, with what it
147 // got of Xcode (never the main runner: house rule 2026-11-07.8).
148 sv.MadeOn, sv.XcodeAccess = sv.RunnerLabel, s.XcodeAccess
149 }
150 if s.Placement == wire.PlacementSandbox && s.SandboxRunner != "" && s.Specialist == wire.SpecialistChecks {
151 // Whether its creator may run it outside the sandbox (D1); unknown
152 // (a lookup error) offers nothing.
153 if ok, err := t.unsandboxedAllowed(s.SandboxRunner); err == nil {
154 sv.SandboxChoice = map[bool]string{true: wire.SandboxChoiceOffered, false: wire.SandboxChoiceHeld}[ok]
155 }
156 }
157 if s.Placement != wire.PlacementMacConnection || s.SandboxRunner == "" {
158 return
159 }
160 var sess runnerSessionRecord
161 if t.db.Where("runner_id = ?", s.SandboxRunner).First(&sess).Error != nil || sess.Hello == "" {
162 return
163 }
164 var h wire.Hello
165 if json.Unmarshal([]byte(sess.Hello), &h) != nil || h.Capabilities == nil {
166 return
167 }
168 status := wire.EngineeringStatus{State: wire.EngineeringNoConnection}
169 if e := h.Capabilities.Engineering; e != nil {
170 status = *e
171 }
172 sv.EngineeringStatus = &status
173}
174
175// xcodeAccess is what Engineering on runner gets of Xcode's services
176// (spec §8.2): sandboxed only on a Mac the instance holds sandbox-proven
177// (its hello says seatbelt, so P2 denied, and it is not tainted) whose
178// probe F5 is denied too. Otherwise none: the change is built and tested
179// by Checks.
180func (t *txn) xcodeAccess(person, runner string) (string, error) {
181 rs, err := t.runners(person)
182 if err != nil {
183 return "", err
184 }
185 for _, r := range rs {
186 if r.ID != runner || !slices.Contains(r.tags(), wire.TagMacOS) || !r.sandboxProven() || r.Hello.Capabilities == nil {
187 continue
188 }
189 for _, p := range r.Hello.Capabilities.Probes {
190 if p.ID == "F5" && p.Verdict == wire.ProbeDenied {
191 return wire.XcodeAccessSandboxed, nil
192 }
193 }
194 }
195 return "", nil
196}
197
198// helloSandbox is the sandbox a stored hello says ("" when none).
199func helloSandbox(raw string) string {
200 var h struct {
201 Sandbox string `json:"sandbox"`
202 }
203 if raw == "" || json.Unmarshal([]byte(raw), &h) != nil {
204 return ""
205 }
206 return h.Sandbox
207}
208
209// sandboxUnproven records a worker's word that its sandbox is not proven
210// (it refused checks, sandbox_unproven): its stored hello no longer says
211// seatbelt, so placement waits until its next hello says so again.
212func (t *txn) sandboxUnproven(runner string) error {
213 var r runnerSessionRecord
214 if err := t.db.Where("runner_id = ?", runner).First(&r).Error; err != nil {
215 return err
216 }
217 if helloSandbox(r.Hello) != wire.SandboxSeatbelt {
218 return nil
219 }
220 var h wire.Hello
221 if err := json.Unmarshal([]byte(r.Hello), &h); err != nil {
222 return err
223 }
224 h.Sandbox = wire.SandboxNone
225 raw, err := json.Marshal(h)
226 if err != nil {
227 return err
228 }
229 return t.db.Model(&runnerSessionRecord{}).Where("runner_id = ?", runner).Update("hello", string(raw)).Error
230}
231
232// RunUnsandboxed is the task creator's choice, from the task's Run
233// outside the sandbox and its confirm, to run the checks waiting for a
234// Mac's sandbox on that Mac outside it: only that step's attempt, only on
235// that Mac, recorded (who and when) and audited in the same write. The
236// Mac is then tainted by the run. Nothing else (no coordinator tool,
237// automatic retry, fix round or housekeeping) ever records it.
238func (s *Store) RunUnsandboxed(ctx context.Context, person, id, stepID string, attempt int64) error {
239 return s.tx(ctx, func(t *txn) error {
240 if err := s.authority.CurrentPerson(ctx, t.db); err != nil {
241 return err
242 }
243 if err := t.hkForbid("unsandboxed"); err != nil {
244 return err
245 }
246 if err := t.isOwner(person); err != nil {
247 return err
248 }
249 k, err := t.loadTask(id)
250 if err != nil {
251 return err
252 }
253 if k.Creator != person {
254 return ErrForbidden
255 }
256 if wire.FinalStage(k.Stage) || k.CancelRequested {
257 return ErrTaskEnded
258 }
259 st, found, err := t.latestStep(k.ID)
260 if err != nil {
261 return err
262 }
263 // Only the attempt the person saw waiting: a choice never carries
264 // to another attempt or another Mac.
265 if !found || st.ID != stepID || st.Attempt != attempt || st.Specialist != wire.SpecialistChecks || st.State != wire.StepQueued || st.Placement != wire.PlacementSandbox || st.SandboxRunner == "" || st.Person != person {
266 return ErrNotRetryable
267 }
268 // D1: not on a Mac the instance holds as having a model credential.
269 if ok, err := t.unsandboxedAllowed(st.SandboxRunner); err != nil {
270 return err
271 } else if !ok {
272 return ErrNotRetryable
273 }
274 now := t.now()
275 st.UnsandboxedBy, st.UnsandboxedAt, st.UnsandboxedAttempt, st.UnsandboxedRunner = person, &now, st.Attempt, st.SandboxRunner
276 if s.authority.Audit != nil {
277 if err := s.authority.Audit(t.db, person, "task.checks_unsandboxed", st.ID+"/"+strconv.FormatInt(st.Attempt, 10)+"@"+st.SandboxRunner); err != nil {
278 return err
279 }
280 }
281 since := st.WaitingSince
282 if err := t.dispatchStep(&k, &st); err != nil {
283 return err
284 }
285 if st.Placement != "" {
286 st.WaitingSince = since
287 }
288 return t.saveStep(&st, &k)
289 })
290}
291