@join-pantheon/cli 0.35.0-rc.2 → 0.35.1-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/agent-outage.js +156 -36
  2. package/dist/agent-token.js +5 -3
  3. package/dist/approvals-list.js +87 -0
  4. package/dist/board.js +472 -87
  5. package/dist/brief.js +17 -0
  6. package/dist/cli.js +687 -95
  7. package/dist/client.js +159 -19
  8. package/dist/config.js +3 -2
  9. package/dist/continued.js +56 -1
  10. package/dist/decision-ui.js +5 -3
  11. package/dist/doctor.js +81 -15
  12. package/dist/file-lock.js +39 -4
  13. package/dist/flow-stats.js +18 -1
  14. package/dist/home-guard.js +317 -54
  15. package/dist/home-safety.js +2 -0
  16. package/dist/hud-queue.js +91 -0
  17. package/dist/install.js +15 -1
  18. package/dist/mcp-instructions.js +55 -20
  19. package/dist/mcp.js +114 -24
  20. package/dist/notice-linux.js +15 -4
  21. package/dist/notice-windows.js +2 -2
  22. package/dist/notifier.js +2 -1
  23. package/dist/notify.js +8 -4
  24. package/dist/open-in.js +4 -3
  25. package/dist/pickup.js +17 -0
  26. package/dist/plain.js +139 -3
  27. package/dist/profile.js +12 -8
  28. package/dist/protocol.js +1 -1
  29. package/dist/question-goal.js +74 -0
  30. package/dist/readiness-alert.js +24 -3
  31. package/dist/refusal.js +2 -2
  32. package/dist/relay.js +142 -28
  33. package/dist/removed.js +4 -4
  34. package/dist/run-control.js +4 -3
  35. package/dist/run-failure.js +67 -10
  36. package/dist/setup-changes.js +7 -6
  37. package/dist/signed-out.js +144 -21
  38. package/dist/spend.js +2 -2
  39. package/dist/tray.js +24 -6
  40. package/dist/update-check.js +2 -1
  41. package/dist/uri-handler.js +10 -2
  42. package/dist/which.js +2 -0
  43. package/dist/worker-policy.js +212 -10
  44. package/dist/worker.js +847 -114
  45. package/dist/worklist.js +31 -1
  46. package/helper/macos/main.swift +42 -3
  47. package/helper/macos/menubar.swift +482 -87
  48. package/helper/macos/prebuilt/manifest.json +5 -5
  49. package/helper/macos/prebuilt/pantheon-hud +0 -0
  50. package/helper/macos/prebuilt/pantheon-tray +0 -0
  51. package/package.json +1 -1
@@ -1,9 +1,12 @@
1
1
  import { createHash } from "node:crypto";
2
- import { chmodSync, lstatSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
3
- import { join, resolve } from "node:path";
2
+ import { chmodSync, lstatSync, readFileSync, readlinkSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
3
+ import { execFileSync } from "node:child_process";
4
+ import { dirname, join, resolve } from "node:path";
4
5
  import { parseJsonText } from "./text-file.js";
5
6
  import { withFileLock } from "./file-lock.js";
6
7
  import { writeWhole } from "./pins.js";
8
+ import { WORKER_MARK, markedHere, ownReleases } from "./continued.js";
9
+ import { HUD_QUEUE_DIR, sweepActionQueue } from "./hud-queue.js";
7
10
  export const POLICY_FILE = "worker-policy.json";
8
11
  export const PROFILE_FILE = "profile.json";
9
12
  export const HUD_FILE = join("bin", ".hud-action.json");
@@ -21,7 +24,9 @@ export const RUN_CONTROL_FILE = "run-control.json";
21
24
  export const HUD_PREF_FILE = join("bin", ".hud-pref.json");
22
25
  export const AGENT_TOKENS_FILE = "agent-tokens.json";
23
26
  export const SIGNED_OUT_FILE = "signed-out.json";
24
- export const GUARDED_FILES = [AGENT_TOKENS_FILE, SIGNED_OUT_FILE, POLICY_FILE, CONFIG_FILE, PROFILE_FILE, HUD_FILE, CONTINUED_FILE, CURSOR_FILE, PINS_FILE, NONCES_FILE, REMOVED_FILE, RECHECK_FILE, READINESS_ALERT_FILE, CHANGES_LOG_FILE, PENDING_CHANGES_FILE, RUN_CONTROL_FILE, HUD_PREF_FILE];
27
+ export const SIGNED_OUT_SALT_FILE = "signed-out.salt";
28
+ export const APPROVALS_LIST_FILE = "approvals-last.json";
29
+ export const GUARDED_FILES = [AGENT_TOKENS_FILE, SIGNED_OUT_FILE, SIGNED_OUT_SALT_FILE, POLICY_FILE, CONFIG_FILE, PROFILE_FILE, HUD_FILE, CONTINUED_FILE, CURSOR_FILE, PINS_FILE, NONCES_FILE, REMOVED_FILE, RECHECK_FILE, READINESS_ALERT_FILE, CHANGES_LOG_FILE, PENDING_CHANGES_FILE, RUN_CONTROL_FILE, HUD_PREF_FILE, APPROVALS_LIST_FILE];
25
30
  export function narrowRunControl(before, now) {
26
31
  const list = (x) => (Array.isArray(obj(x).stops) ? obj(x).stops.filter((e) => !!e && typeof e === "object") : []);
27
32
  const b = list(before), n = list(now);
@@ -98,14 +103,64 @@ export function trustWrite(file, prev) {
98
103
  set.delete(set.values().next().value);
99
104
  trusted.set(k, set);
100
105
  }
106
+ const linkOf = (f) => { try {
107
+ return lstatSync(f).isSymbolicLink() ? readlinkSync(f) : undefined;
108
+ }
109
+ catch {
110
+ return undefined;
111
+ } };
101
112
  export function snapshotHome(home, files = GUARDED_FILES) {
102
- const s = { home, at: Date.now(), files: files.map((rel) => { const b = read(join(home, rel)); return { rel, bytes: b, hash: b ? sha(b) : null }; }) };
113
+ const s = { home, at: Date.now(), files: files.map((rel) => { const b = read(join(home, rel)); const l = linkOf(join(home, rel)); return { rel, bytes: b, hash: b ? sha(b) : null, ...(l !== undefined ? { link: l } : {}) }; }) };
103
114
  for (const f of s.files)
104
115
  runBaseline.set(resolve(join(home, f.rel)), f.hash);
105
116
  return s;
106
117
  }
107
118
  export const RUN_GUARD_FILE = "worker-run-guard.json";
108
119
  export const RUN_GUARD_TMP = `${RUN_GUARD_FILE}.tmp`;
120
+ export function ownWrites() {
121
+ return { wake: [...askedHere.wake], cap: [...askedHere.cap], stuck: [...askedHere.stuck], safety: [...askedHere.safety], withdrawn: [...withdrawnHere], noes: [...noesHere], marks: [...markedHere] };
122
+ }
123
+ export function resetOwn() {
124
+ for (const m of Object.values(askedHere))
125
+ m.clear();
126
+ withdrawnHere.clear();
127
+ noesHere.clear();
128
+ markedHere.clear();
129
+ }
130
+ export function adoptOwn(o) {
131
+ if (!o)
132
+ return;
133
+ for (const [k, v] of o.wake)
134
+ askedHere.wake.set(k, v);
135
+ for (const [k, v] of o.cap)
136
+ askedHere.cap.set(k, v);
137
+ for (const [k, v] of o.stuck)
138
+ askedHere.stuck.set(k, v);
139
+ for (const [k, v] of o.safety ?? [])
140
+ askedHere.safety.set(k, v);
141
+ for (const k of o.withdrawn)
142
+ withdrawnHere.add(k);
143
+ for (const k of o.noes)
144
+ noesHere.add(k);
145
+ for (const k of o.marks)
146
+ markedHere.add(k);
147
+ }
148
+ const pairs = (v) => Array.isArray(v) ? v.filter((x) => Array.isArray(x) && typeof x[0] === "string" && typeof x[1] === "string") : [];
149
+ const strs = (v) => Array.isArray(v) ? v.filter((x) => typeof x === "string") : [];
150
+ function amendGuard(home, patch) {
151
+ try {
152
+ const f = join(home, RUN_GUARD_FILE);
153
+ const o = obj(JSON.parse(readFileSync(f, "utf8")));
154
+ if (!Array.isArray(o.files))
155
+ return;
156
+ const tmp = join(home, RUN_GUARD_TMP);
157
+ writeFileSync(tmp, JSON.stringify({ ...o, ...patch }), { mode: 0o600 });
158
+ renameSync(tmp, f);
159
+ }
160
+ catch { }
161
+ }
162
+ export function persistOwn(home) { amendGuard(home, { own: ownWrites() }); }
163
+ export function persistPending(home, rels) { amendGuard(home, { pending: rels, own: ownWrites() }); }
109
164
  export function configSecretPaths(cfg) {
110
165
  const c = obj(cfg);
111
166
  const out = [];
@@ -173,7 +228,8 @@ function unredactConfig(before, now) {
173
228
  const HOLDS_SECRETS = new Set([CONFIG_FILE, AGENT_TOKENS_FILE]);
174
229
  export function persistSnapshot(s, o = {}) {
175
230
  const file = join(s.home, RUN_GUARD_FILE);
176
- const files = s.files.map((f) => {
231
+ const files = s.files.map((f) => ({ ...persisted(f), ...(f.link !== undefined ? { link: f.link } : {}) }));
232
+ function persisted(f) {
177
233
  if (!HOLDS_SECRETS.has(f.rel) || !f.bytes)
178
234
  return { rel: f.rel, bytes: f.bytes ? f.bytes.toString("base64") : null, hash: f.hash };
179
235
  if (f.rel === AGENT_TOKENS_FILE)
@@ -186,7 +242,7 @@ export function persistSnapshot(s, o = {}) {
186
242
  safe = null;
187
243
  }
188
244
  return { rel: f.rel, bytes: safe === null ? null : Buffer.from(safe).toString("base64"), hash: f.hash, redacted: true };
189
- });
245
+ }
190
246
  const body = { v: 2, worker: o.worker ?? process.pid, ...(o.workerId ? { worker_id: o.workerId } : {}), at: s.at ?? Date.now(), ...(o.limit !== undefined ? { limit: o.limit } : {}), attempts: o.attempts ?? 0, files };
191
247
  const tmp = join(s.home, RUN_GUARD_TMP);
192
248
  writeFileSync(tmp, JSON.stringify(body), { mode: 0o600 });
@@ -216,6 +272,11 @@ export function loadPersistedGuard(home) {
216
272
  out.limit = o.limit;
217
273
  if (Array.isArray(o.restored))
218
274
  out.restored = o.restored.filter((x) => typeof x === "string");
275
+ if (Array.isArray(o.pending))
276
+ out.pending = strs(o.pending).filter((r) => GUARDED_FILES.includes(r));
277
+ const ow = obj(o.own);
278
+ if (Object.keys(ow).length)
279
+ out.own = { wake: pairs(ow.wake), cap: pairs(ow.cap), stuck: pairs(ow.stuck), safety: pairs(ow.safety), withdrawn: strs(ow.withdrawn), noes: strs(ow.noes), marks: strs(ow.marks) };
219
280
  if (Array.isArray(o.files)) {
220
281
  const files = [];
221
282
  for (const f of o.files) {
@@ -223,12 +284,13 @@ export function loadPersistedGuard(home) {
223
284
  if (typeof x.rel !== "string" || !GUARDED_FILES.includes(x.rel))
224
285
  continue;
225
286
  const bytes = typeof x.bytes === "string" ? Buffer.from(x.bytes, "base64") : null;
287
+ const link = typeof x.link === "string" ? { link: x.link } : {};
226
288
  if (HOLDS_SECRETS.has(x.rel)) {
227
289
  const hash = typeof x.hash === "string" && /^[0-9a-f]{64}$/.test(x.hash) ? x.hash : null;
228
- files.push({ rel: x.rel, bytes, hash, redacted: true });
290
+ files.push({ rel: x.rel, bytes, hash, redacted: true, ...link });
229
291
  continue;
230
292
  }
231
- files.push({ rel: x.rel, bytes, hash: bytes ? sha(bytes) : null });
293
+ files.push({ rel: x.rel, bytes, hash: bytes ? sha(bytes) : null, ...link });
232
294
  }
233
295
  out.snapshot = { home, at: o.at, files };
234
296
  }
@@ -266,8 +328,16 @@ export function changedKeys(a, b, prefix = "", depth = 0) {
266
328
  }
267
329
  const obj = (x) => (x && typeof x === "object" && !Array.isArray(x) ? x : {});
268
330
  const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
269
- export const askedHere = new Set();
331
+ export const askedHere = { wake: new Map(), cap: new Map(), stuck: new Map(), safety: new Map() };
332
+ export function canon(v) {
333
+ return JSON.stringify(v, (_k, x) => x && typeof x === "object" && !Array.isArray(x)
334
+ ? Object.fromEntries(Object.keys(x).sort().map((k) => [k, x[k]])) : x);
335
+ }
336
+ export const withdrawnHere = new Set();
337
+ export const noesHere = new Set();
338
+ const isTime = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
270
339
  export function narrowPolicy(before, now, keepAsks = askedHere) {
340
+ const mine = (kind, key, v) => keepAsks[kind].get(key) === canon(v);
271
341
  const b = obj(before);
272
342
  const keys = [];
273
343
  const out = { ...now };
@@ -299,31 +369,62 @@ export function narrowPolicy(before, now, keepAsks = askedHere) {
299
369
  }
300
370
  if (Object.keys(wakers).length || "wakers" in now)
301
371
  out.wakers = wakers;
302
- const bd = obj(b.denied), nd = obj(now.denied);
303
- const denied = { ...nd };
304
- const capAt = (v, was) => typeof v === "number" && v > Date.now() && v !== was ? Date.now() : v;
305
- for (const [cap, at] of Object.entries(nd))
306
- denied[cap] = capAt(at, bd[cap]);
307
- for (const [cap, at] of Object.entries(bd))
308
- if (!(cap in nd)) {
309
- denied[cap] = at;
310
- keys.push(`denied.${cap} removed`);
311
- }
312
- out.denied = denied;
372
+ const dated = (field, lifted, keepAdded = () => false) => {
373
+ const bv = obj(b[field]), nv = obj(now[field]);
374
+ const res = {};
375
+ for (const [k, at] of Object.entries(bv))
376
+ if (isTime(at))
377
+ res[k] = at;
378
+ for (const [k, at] of Object.entries(nv)) {
379
+ if (k in res) {
380
+ if (same(at, res[k]))
381
+ continue;
382
+ keys.push(`${field}.${k} ${!isTime(at) ? lifted : at < res[k] ? (field === "denied" ? "backdated" : lifted) : "moved later"}`);
383
+ continue;
384
+ }
385
+ if ((keepAdded(k) || noesHere.has(`${field}:${k}`)) && isTime(at)) {
386
+ res[k] = Math.min(at, Date.now());
387
+ continue;
388
+ }
389
+ if (k in bv && same(bv[k], at))
390
+ continue;
391
+ keys.push(`${field}.${k} added (taken back)`);
392
+ }
393
+ for (const k of Object.keys(res))
394
+ if (!(k in nv))
395
+ keys.push(`${field}.${k} ${field === "denied" ? "removed" : lifted}`);
396
+ return res;
397
+ };
398
+ out.denied = dated("denied", "backdated", (cap) => cap === "unsandboxed_runs");
313
399
  const bk = obj(b.asks), nk = obj(now.asks);
314
400
  const pending = {};
401
+ const askAct = (q) => (q && typeof q === "object" && typeof q.act === "string" ? q.act : "");
315
402
  for (const [cap, q] of Object.entries(nk)) {
316
403
  if (!(cap in bk)) {
404
+ if (mine("cap", cap, q)) {
405
+ pending[cap] = q;
406
+ continue;
407
+ }
317
408
  keys.push(`asks.${cap} added`);
318
409
  continue;
319
410
  }
320
411
  if (!same(bk[cap], q)) {
412
+ if (mine("cap", cap, q)) {
413
+ pending[cap] = q;
414
+ continue;
415
+ }
321
416
  keys.push(`asks.${cap} changed`);
322
417
  pending[cap] = bk[cap];
323
418
  continue;
324
419
  }
325
420
  pending[cap] = q;
326
421
  }
422
+ for (const [cap, q] of Object.entries(bk)) {
423
+ if (cap in nk || withdrawnHere.has(askAct(q)))
424
+ continue;
425
+ pending[cap] = q;
426
+ keys.push(`asks.${cap} removed`);
427
+ }
327
428
  if ("asks" in now || Object.keys(pending).length)
328
429
  out.asks = pending;
329
430
  const bo = obj(b.wakeOnce), no = obj(now.wakeOnce);
@@ -340,21 +441,12 @@ export function narrowPolicy(before, now, keepAsks = askedHere) {
340
441
  }
341
442
  if ("wakeOnce" in now || Object.keys(once).length)
342
443
  out.wakeOnce = once;
343
- const bn = obj(b.wakeNo), nn = obj(now.wakeNo);
344
- const wakeNo = { ...nn };
345
- for (const [k, at] of Object.entries(nn))
346
- wakeNo[k] = capAt(at, bn[k]);
347
- for (const [k, at] of Object.entries(bn)) {
348
- if (!(k in nn) || (typeof at === "number" && typeof nn[k] === "number" && nn[k] < at)) {
349
- wakeNo[k] = at;
350
- keys.push(`wakeNo.${k} lifted`);
351
- }
352
- }
444
+ const wakeNo = dated("wakeNo", "lifted");
353
445
  const ba = obj(b.wakeAsks), na = obj(now.wakeAsks);
354
446
  const asks = {};
355
447
  for (const [id, q] of Object.entries(na)) {
356
448
  if (!(id in ba)) {
357
- if (keepAsks.has(id)) {
449
+ if (mine("wake", id, q)) {
358
450
  asks[id] = q;
359
451
  continue;
360
452
  }
@@ -362,12 +454,22 @@ export function narrowPolicy(before, now, keepAsks = askedHere) {
362
454
  continue;
363
455
  }
364
456
  if (!same(ba[id], q)) {
457
+ if (mine("wake", id, q)) {
458
+ asks[id] = q;
459
+ continue;
460
+ }
365
461
  keys.push(`wakeAsks.${id} changed`);
366
462
  asks[id] = ba[id];
367
463
  continue;
368
464
  }
369
465
  asks[id] = q;
370
466
  }
467
+ for (const [id, q] of Object.entries(ba)) {
468
+ if (id in na || withdrawnHere.has(id))
469
+ continue;
470
+ asks[id] = q;
471
+ keys.push(`wakeAsks.${id} removed`);
472
+ }
371
473
  if ("wakeAsks" in now || Object.keys(asks).length)
372
474
  out.wakeAsks = asks;
373
475
  for (const f of ["wakersCarried", "wakersDeferred"]) {
@@ -398,15 +500,24 @@ export function narrowPolicy(before, now, keepAsks = askedHere) {
398
500
  if ("wakeNo" in now || Object.keys(wakeNo).length)
399
501
  out.wakeNo = wakeNo;
400
502
  const bfn = new Set(Array.isArray(b.finalNo) ? b.finalNo.filter((k) => typeof k === "string") : []);
401
- if (Array.isArray(now.finalNo)) {
402
- const fn = now.finalNo.filter((k) => typeof k === "string");
403
- out.finalNo = fn.filter((k) => bfn.has(k));
404
- }
503
+ const fn = Array.isArray(now.finalNo) ? now.finalNo.filter((k) => typeof k === "string") : [];
504
+ const keptFinal = fn.filter((k) => bfn.has(k));
505
+ for (const k of new Set(fn.filter((k) => !bfn.has(k))))
506
+ keys.push(`finalNo.${k} added (taken back)`);
507
+ const lostFinal = [...bfn].filter((k) => !fn.includes(k));
508
+ for (const k of lostFinal)
509
+ keys.push(`finalNo.${k} removed`);
510
+ if (Array.isArray(now.finalNo) || bfn.size)
511
+ out.finalNo = [...keptFinal, ...lostFinal];
405
512
  const bs = obj(b.stuckReports), ns = obj(now.stuckReports);
406
513
  if ("stuckReports" in now) {
407
514
  const kept = {};
408
515
  for (const [id, r] of Object.entries(ns)) {
409
516
  if (!(id in bs)) {
517
+ if (mine("stuck", id, r)) {
518
+ kept[id] = r;
519
+ continue;
520
+ }
410
521
  keys.push(`stuckReports.${id} added`);
411
522
  continue;
412
523
  }
@@ -419,6 +530,26 @@ export function narrowPolicy(before, now, keepAsks = askedHere) {
419
530
  }
420
531
  out.stuckReports = kept;
421
532
  }
533
+ {
534
+ const bsn = obj(b.safetyNotes), nsn = obj(now.safetyNotes);
535
+ const notes = {};
536
+ for (const [k, v] of Object.entries(nsn)) {
537
+ if (k in bsn ? same(bsn[k], v) : mine("safety", k, v)) {
538
+ notes[k] = v;
539
+ continue;
540
+ }
541
+ keys.push(`safetyNotes.${k} ${k in bsn ? "changed" : "added"}`);
542
+ if (k in bsn)
543
+ notes[k] = bsn[k];
544
+ }
545
+ for (const [k, v] of Object.entries(bsn))
546
+ if (!(k in nsn)) {
547
+ notes[k] = v;
548
+ keys.push(`safetyNotes.${k} removed`);
549
+ }
550
+ if ("safetyNotes" in now || Object.keys(notes).length)
551
+ out.safetyNotes = notes;
552
+ }
422
553
  const minutes = (x) => (typeof x === "number" && Number.isInteger(x) && x >= RUN_MINUTES_MIN && x <= RUN_MINUTES_MAX ? x : RUN_MINUTES_DEFAULT);
423
554
  if (!same(b.runMinutes, now.runMinutes) && minutes(now.runMinutes) > minutes(b.runMinutes)) {
424
555
  if ("runMinutes" in b)
@@ -594,7 +725,7 @@ export function narrowPins(before, now) {
594
725
  }
595
726
  return { value: { ...n, v: 1, teams }, keys };
596
727
  }
597
- export function narrowNonces(before, now) {
728
+ export function narrowNonces(before, now, released = new Set()) {
598
729
  const b = obj(before), n = obj(now);
599
730
  const out = { ...n };
600
731
  let lost = 0;
@@ -602,8 +733,10 @@ export function narrowNonces(before, now) {
602
733
  const cur = { ...obj(n[team]) };
603
734
  const forward = (c, w) => same(c, w) || (Array.isArray(c) && Array.isArray(w) && c[0] === w[0] && c[1] === w[1]
604
735
  && (c[2] ?? null) === (w[2] ?? null) && typeof c[3] === "number" && (w[3] === undefined || w[3] === c[3]));
736
+ const ownTakeBack = (c, w) => Array.isArray(c) && Array.isArray(w) && typeof w[0] === "string" && released.has(`${team}|${w[0]}`)
737
+ && c[0] === w[0] && c[1] === w[1] && (c[2] ?? null) === (w[2] ?? null) && c[3] === undefined;
605
738
  for (const [k, v] of Object.entries(obj(entries)))
606
- if (!(k in cur) || !forward(cur[k], v)) {
739
+ if (!(k in cur) || (!forward(cur[k], v) && !ownTakeBack(cur[k], v))) {
607
740
  cur[k] = v;
608
741
  lost++;
609
742
  }
@@ -669,8 +802,41 @@ export function restoreHome(s) {
669
802
  }
670
803
  for (const f of s.files)
671
804
  runBaseline.delete(resolve(join(s.home, f.rel)));
805
+ const releasedOwn = (() => {
806
+ const snap = s.files.find((f) => f.rel === CONTINUED_FILE);
807
+ try {
808
+ return ownReleases(parse(snap?.bytes ?? null), parse(read(join(s.home, CONTINUED_FILE))), s.at ?? Date.now());
809
+ }
810
+ catch {
811
+ return new Set();
812
+ }
813
+ })();
814
+ if (s.at !== undefined) {
815
+ const gone = sweepActionQueue(s.home, { maxAgeMs: 30_000, since: s.at - 1_000 });
816
+ if (gone)
817
+ out.push({ rel: HUD_QUEUE_DIR, what: "removed", keys: [`${gone} queued menu-bar action${gone === 1 ? "" : "s"}`] });
818
+ }
672
819
  for (const f of s.files) {
673
820
  const file = join(s.home, f.rel);
821
+ if (f.rel.includes("/"))
822
+ makeWritable(dirname(file), false);
823
+ const linkNow = linkOf(file);
824
+ if (linkNow !== f.link) {
825
+ try {
826
+ const through = read(file);
827
+ makeWritable(file);
828
+ rmSync(file, { force: true });
829
+ if (f.link !== undefined)
830
+ symlinkSync(f.link, file);
831
+ else if (through)
832
+ putWhole(file, through);
833
+ out.push({ rel: f.rel, what: "reverted", keys: [linkNow !== undefined ? "a link in place of the file" : "its link replaced by a file"] });
834
+ }
835
+ catch {
836
+ out.push({ rel: f.rel, what: "reported", keys: ["(could not be put back — unattended runs are held until it is writable again)"], failed: true });
837
+ continue;
838
+ }
839
+ }
674
840
  const now = read(file);
675
841
  const hashNow = now ? sha(now) : null;
676
842
  if (hashNow === f.hash)
@@ -678,6 +844,7 @@ export function restoreHome(s) {
678
844
  const alwaysNarrow = f.rel === POLICY_FILE || f.rel === PROFILE_FILE || f.rel === CONFIG_FILE || f.rel === CONTINUED_FILE || f.rel === CURSOR_FILE || f.rel === PINS_FILE || f.rel === NONCES_FILE || f.rel === PENDING_CHANGES_FILE || f.rel === CHANGES_LOG_FILE || f.rel === RUN_CONTROL_FILE || f.rel === HUD_PREF_FILE;
679
845
  if (!alwaysNarrow && trusted.get(resolve(file))?.has(hashNow ?? "absent"))
680
846
  continue;
847
+ makeWritable(file);
681
848
  try {
682
849
  if (f.rel === HUD_FILE) {
683
850
  if (now) {
@@ -691,7 +858,7 @@ export function restoreHome(s) {
691
858
  rmSync(file, { recursive: true, force: true });
692
859
  }
693
860
  catch { }
694
- if (f.rel === AGENT_TOKENS_FILE || f.rel === SIGNED_OUT_FILE) {
861
+ if (f.rel === AGENT_TOKENS_FILE || f.rel === SIGNED_OUT_FILE || f.rel === SIGNED_OUT_SALT_FILE) {
695
862
  try {
696
863
  if (lstatSync(file).isSymbolicLink())
697
864
  rmSync(file, { force: true });
@@ -706,7 +873,7 @@ export function restoreHome(s) {
706
873
  writeWhole(file, f.bytes.toString("utf8"));
707
874
  else
708
875
  rmSync(file, { force: true });
709
- out.push({ rel: f.rel, what: f.bytes ? "reverted" : "removed", keys: f.rel === AGENT_TOKENS_FILE ? ["the worker's Claude token"] : [], ...(f.rel === SIGNED_OUT_FILE ? { quiet: true } : {}) });
876
+ out.push({ rel: f.rel, what: f.bytes ? "reverted" : "removed", keys: f.rel === AGENT_TOKENS_FILE ? ["the worker's Claude token"] : [], ...(f.rel === SIGNED_OUT_FILE || f.rel === SIGNED_OUT_SALT_FILE ? { quiet: true } : {}) });
710
877
  continue;
711
878
  }
712
879
  if (f.rel === CHANGES_LOG_FILE) {
@@ -723,7 +890,7 @@ export function restoreHome(s) {
723
890
  const added = (is.startsWith(was) ? is.slice(was.length).split("\n") : is.split("\n").filter((l) => !old.has(l))).filter((l) => l.trim());
724
891
  const planted = added.filter(firstSetup).length;
725
892
  if (f.bytes)
726
- writeFileSync(file, f.bytes, { mode: 0o600 });
893
+ putWhole(file, f.bytes);
727
894
  else
728
895
  rmSync(file, { force: true });
729
896
  trustWrite(file);
@@ -753,16 +920,35 @@ export function restoreHome(s) {
753
920
  writeWhole(file, JSON.stringify(next));
754
921
  }
755
922
  catch {
756
- writeFileSync(file, JSON.stringify(next), { mode: 0o600 });
923
+ putWhole(file, JSON.stringify(next));
757
924
  }
758
925
  trustWrite(file);
759
926
  out.push({ rel: f.rel, what: "reverted", keys: ["where questions open"] });
760
927
  continue;
761
928
  }
929
+ if (f.rel === APPROVALS_LIST_FILE) {
930
+ try {
931
+ if (lstatSync(file).isSymbolicLink() || !statSync(file).isFile())
932
+ rmSync(file, { recursive: true, force: true });
933
+ }
934
+ catch { }
935
+ const bl = obj(obj(a).lists), nl = obj(obj(parse(read(file))).lists);
936
+ const kept = Object.fromEntries(Object.entries(nl).filter(([k, l]) => k in bl && same(bl[k], l)));
937
+ const dropped = Object.keys(nl).length - Object.keys(kept).length;
938
+ try {
939
+ writeWhole(file, JSON.stringify({ v: 2, lists: kept }) + "\n");
940
+ }
941
+ catch {
942
+ putWhole(file, JSON.stringify({ v: 2, lists: kept }) + "\n");
943
+ }
944
+ trustWrite(file);
945
+ out.push({ rel: f.rel, what: "reverted", keys: dropped ? [`${dropped} numbered approvals list${dropped === 1 ? "" : "s"} — list again`] : [], quiet: true });
946
+ continue;
947
+ }
762
948
  if (f.rel === RUN_CONTROL_FILE) {
763
949
  const n = narrowRunControl(a, b);
764
950
  if (n.keys.length) {
765
- writeFileSync(file, JSON.stringify(n.value, null, 2) + "\n", { mode: 0o600 });
951
+ putWhole(file, JSON.stringify(n.value, null, 2) + "\n");
766
952
  trustWrite(file);
767
953
  out.push({ rel: f.rel, what: "reverted", keys: n.keys });
768
954
  }
@@ -771,7 +957,7 @@ export function restoreHome(s) {
771
957
  if (f.rel === PENDING_CHANGES_FILE) {
772
958
  const n = narrowPending(a, b);
773
959
  if (n.keys.length) {
774
- writeFileSync(file, JSON.stringify(n.value, null, 2) + "\n", { mode: 0o600 });
960
+ putWhole(file, JSON.stringify(n.value, null, 2) + "\n");
775
961
  trustWrite(file);
776
962
  out.push({ rel: f.rel, what: "reverted", keys: n.keys });
777
963
  }
@@ -780,17 +966,23 @@ export function restoreHome(s) {
780
966
  if (f.rel === POLICY_FILE) {
781
967
  if (!b) {
782
968
  if (f.bytes) {
783
- writeFileSync(file, f.bytes, { mode: 0o600 });
969
+ putWhole(file, f.bytes);
784
970
  out.push({ rel: f.rel, what: "reverted", keys: ["(the whole file)"] });
785
971
  }
786
972
  continue;
787
973
  }
788
974
  const n = narrowPolicy(a, b);
789
- if (n.keys.length) {
790
- writeFileSync(file, JSON.stringify(n.policy, null, 2) + "\n", { mode: 0o600 });
975
+ if (n.keys.length || !same(n.policy, b)) {
976
+ try {
977
+ if (lstatSync(file).isSymbolicLink())
978
+ rmSync(file, { force: true });
979
+ }
980
+ catch { }
981
+ putWhole(file, JSON.stringify(n.policy, null, 2) + "\n");
791
982
  trustWrite(file);
792
- out.push({ rel: f.rel, what: "reverted", keys: n.keys });
793
983
  }
984
+ if (n.keys.length)
985
+ out.push({ rel: f.rel, what: "reverted", keys: n.keys });
794
986
  continue;
795
987
  }
796
988
  const locked = (fn) => {
@@ -810,7 +1002,7 @@ export function restoreHome(s) {
810
1002
  writeWhole(file, text);
811
1003
  }
812
1004
  catch {
813
- writeFileSync(file, text, { mode: 0o600 });
1005
+ putWhole(file, text);
814
1006
  } };
815
1007
  if (f.rel === RECHECK_FILE || f.rel === READINESS_ALERT_FILE) {
816
1008
  try {
@@ -839,7 +1031,7 @@ export function restoreHome(s) {
839
1031
  if (f.rel === PINS_FILE || f.rel === NONCES_FILE) {
840
1032
  locked(() => {
841
1033
  const cur = parse(read(file));
842
- const n = f.rel === PINS_FILE ? narrowPins(a, cur) : narrowNonces(a, cur);
1034
+ const n = f.rel === PINS_FILE ? narrowPins(a, cur) : narrowNonces(a, cur, releasedOwn);
843
1035
  if (n.keys.length) {
844
1036
  put(JSON.stringify(n.value, null, 2) + "\n");
845
1037
  trustWrite(file);
@@ -861,14 +1053,33 @@ export function restoreHome(s) {
861
1053
  }
862
1054
  }
863
1055
  else if (Array.isArray(v)) {
1056
+ if (k.endsWith(" inflight"))
1057
+ continue;
864
1058
  const cur = Array.isArray(now[k]) ? now[k] : [];
865
- const back = v.filter((x) => !cur.includes(x));
1059
+ const back = v.filter((x) => !cur.includes(x) && !(typeof x === "string" && x.startsWith("handled:") && releasedOwn.has(`${k}|${x.slice("handled:".length)}`)));
866
1060
  if (back.length) {
867
1061
  merged[k] = [...back, ...cur];
868
1062
  lost += back.length;
869
1063
  }
870
1064
  }
871
1065
  }
1066
+ let planted = 0;
1067
+ if (f.rel === CONTINUED_FILE)
1068
+ for (const [k, v] of Object.entries(merged)) {
1069
+ if (!Array.isArray(v))
1070
+ continue;
1071
+ const was = new Set(Array.isArray(before[k]) ? before[k] : []);
1072
+ const kept = v.filter((x) => !(typeof x === "string" && WORKER_MARK.test(x) && !was.has(x) && !markedHere.has(x)));
1073
+ if (kept.length !== v.length) {
1074
+ merged[k] = kept;
1075
+ planted += v.length - kept.length;
1076
+ }
1077
+ }
1078
+ if (planted) {
1079
+ put(JSON.stringify(merged) + "\n");
1080
+ trustWrite(file);
1081
+ out.push({ rel: f.rel, what: "reverted", keys: [`${planted} worker mark${planted === 1 ? "" : "s"} added during the run`] });
1082
+ }
872
1083
  let sentDropped = 0;
873
1084
  if (f.rel === CONTINUED_FILE)
874
1085
  for (const [k, v] of Object.entries(merged)) {
@@ -898,7 +1109,7 @@ export function restoreHome(s) {
898
1109
  continue;
899
1110
  const n = narrowProfile(a, b);
900
1111
  if (n.keys.length) {
901
- writeFileSync(file, JSON.stringify(n.profile, null, 2) + "\n", { mode: 0o600 });
1112
+ putWhole(file, JSON.stringify(n.profile, null, 2) + "\n");
902
1113
  trustWrite(file);
903
1114
  out.push({ rel: f.rel, what: "reverted", keys: n.keys });
904
1115
  }
@@ -916,7 +1127,7 @@ export function restoreHome(s) {
916
1127
  n.keys.push(said);
917
1128
  }
918
1129
  if (n.keys.length) {
919
- writeFileSync(file, JSON.stringify(n.config, null, 2) + "\n", { mode: 0o600 });
1130
+ putWhole(file, JSON.stringify(n.config, null, 2) + "\n");
920
1131
  trustWrite(file);
921
1132
  out.push({ rel: f.rel, what: "reverted", keys: n.keys });
922
1133
  }
@@ -928,7 +1139,7 @@ export function restoreHome(s) {
928
1139
  out.push({ rel: f.rel, what: "reported", keys: ["unreadable since the run — Pantheon could not put it back; check it (pantheon status)"] });
929
1140
  }
930
1141
  else if (a && !b && now) {
931
- writeFileSync(file, f.bytes, { mode: 0o600 });
1142
+ putWhole(file, f.bytes);
932
1143
  trustWrite(file);
933
1144
  out.push({ rel: f.rel, what: "reverted", keys: ["(the whole file)"] });
934
1145
  }
@@ -936,11 +1147,63 @@ export function restoreHome(s) {
936
1147
  out.push({ rel: f.rel, what: "reported", keys: [] });
937
1148
  }
938
1149
  catch {
939
- out.push({ rel: f.rel, what: "reported", keys: ["(could not be checked)"] });
1150
+ out.push({ rel: f.rel, what: "reported", keys: ["(could not be put back — unattended runs are held until it is writable again)"], failed: true });
940
1151
  }
941
1152
  }
942
1153
  return out;
943
1154
  }
1155
+ function putWhole(file, data) {
1156
+ const tmp = `${file}.${process.pid}.${Date.now().toString(36)}.guard.tmp`;
1157
+ try {
1158
+ writeFileSync(tmp, data, { mode: 0o600, flag: "wx" });
1159
+ renameSync(tmp, file);
1160
+ }
1161
+ catch {
1162
+ try {
1163
+ rmSync(tmp, { force: true });
1164
+ }
1165
+ catch { }
1166
+ writeFileSync(file, data, { mode: 0o600 });
1167
+ }
1168
+ }
1169
+ function makeWritable(file, deep = true) {
1170
+ let st;
1171
+ try {
1172
+ st = lstatSync(file);
1173
+ }
1174
+ catch {
1175
+ return;
1176
+ }
1177
+ if (st.isSymbolicLink())
1178
+ return;
1179
+ const dir = st.isDirectory() && deep;
1180
+ const quiet = { stdio: "ignore", timeout: 5_000 };
1181
+ if (process.platform === "darwin") {
1182
+ try {
1183
+ execFileSync("chflags", [...(dir ? ["-R"] : []), "nouchg,nouappnd", file], quiet);
1184
+ }
1185
+ catch { }
1186
+ try {
1187
+ execFileSync("chmod", [...(dir ? ["-R"] : []), "-N", file], quiet);
1188
+ }
1189
+ catch { }
1190
+ }
1191
+ else if (process.platform === "linux") {
1192
+ try {
1193
+ execFileSync("setfacl", [...(dir ? ["-R"] : []), "-b", file], quiet);
1194
+ }
1195
+ catch { }
1196
+ }
1197
+ try {
1198
+ execFileSync("chmod", [...(dir ? ["-R"] : []), "u+rwX", file], quiet);
1199
+ }
1200
+ catch {
1201
+ try {
1202
+ chmodSync(file, st.isDirectory() ? 0o700 : 0o600);
1203
+ }
1204
+ catch { }
1205
+ }
1206
+ }
944
1207
  export function describeChange(c) {
945
1208
  if (c.what === "removed")
946
1209
  return `${c.rel} was written while the run could write it — removed, not acted on`;
@@ -26,12 +26,14 @@ export const PANTHEON_HOME_ENTRIES = [
26
26
  { name: "approvals-last.json", what: "the order of the decisions pantheon approvals last listed, so a number answers that one" },
27
27
  { name: "agent-tokens.json", what: "the long-lived Claude Code token your worker's runs use (pantheon worker token)" },
28
28
  { name: "signed-out.json", what: "the agents a run here found signed out" },
29
+ { name: "signed-out.salt", what: "this machine's salt for the signed-out marks, which hash the worker's sign-in variables (never their values)" },
29
30
  { name: "readiness-alert.json", what: "what you were told about this machine taking requests (signed out, no agent)" },
30
31
  { name: "worker-run.json", what: "the worker's record of a run in flight" },
31
32
  { name: "worker-run-guard.json", what: "Pantheon's own files as they were before the run in flight, put back if its worker goes mid-run" },
32
33
  { name: "held-deliveries.json", what: "deliveries woken runs held until their branch was on origin (pantheon leave and uninstall name any not yet sent)" },
33
34
  { name: /^held-deliveries\.json\.corrupt-\d+$/, sample: "held-deliveries.json.corrupt-1760000000000", what: "a record of held deliveries that could not be read, set aside" },
34
35
  { name: "run-slot.json", what: "the worker's run slot" },
36
+ { name: "person-answering.json", what: "the mark that you are answering a question here, so no run starts until you are done" },
35
37
  { name: "worker.pid", what: "the worker's pid file" },
36
38
  { name: "notifier.pid", what: "the alerts' pid file" },
37
39
  { name: /^notifier-state(-.+)?\.json$/, sample: "notifier-state-team%2Fone.json", what: "the alerts' state" },