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

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 (52) hide show
  1. package/dist/agent-outage.js +165 -35
  2. package/dist/agent-token.js +5 -3
  3. package/dist/agents.js +6 -4
  4. package/dist/approvals-list.js +87 -0
  5. package/dist/board.js +491 -88
  6. package/dist/brief.js +17 -0
  7. package/dist/cli.js +687 -95
  8. package/dist/client.js +159 -19
  9. package/dist/config.js +3 -2
  10. package/dist/continued.js +56 -1
  11. package/dist/decision-ui.js +5 -3
  12. package/dist/doctor.js +84 -16
  13. package/dist/file-lock.js +40 -5
  14. package/dist/flow-stats.js +18 -1
  15. package/dist/home-guard.js +336 -54
  16. package/dist/home-safety.js +2 -0
  17. package/dist/hud-queue.js +91 -0
  18. package/dist/install.js +15 -1
  19. package/dist/mcp-instructions.js +67 -28
  20. package/dist/mcp.js +129 -24
  21. package/dist/notice-linux.js +15 -4
  22. package/dist/notice-windows.js +2 -2
  23. package/dist/notifier.js +2 -1
  24. package/dist/notify.js +28 -9
  25. package/dist/open-in.js +4 -3
  26. package/dist/pickup.js +17 -0
  27. package/dist/plain.js +139 -3
  28. package/dist/profile.js +12 -8
  29. package/dist/protocol.js +1 -1
  30. package/dist/question-goal.js +74 -0
  31. package/dist/readiness-alert.js +24 -3
  32. package/dist/refusal.js +2 -2
  33. package/dist/relay.js +143 -28
  34. package/dist/removed.js +4 -4
  35. package/dist/run-control.js +4 -3
  36. package/dist/run-failure.js +67 -10
  37. package/dist/setup-changes.js +7 -6
  38. package/dist/signed-out.js +144 -21
  39. package/dist/spend.js +5 -4
  40. package/dist/tray.js +24 -6
  41. package/dist/update-check.js +2 -1
  42. package/dist/uri-handler.js +10 -2
  43. package/dist/which.js +2 -0
  44. package/dist/worker-policy.js +212 -10
  45. package/dist/worker.js +942 -133
  46. package/dist/worklist.js +31 -1
  47. package/helper/macos/main.swift +42 -3
  48. package/helper/macos/menubar.swift +482 -87
  49. package/helper/macos/prebuilt/manifest.json +5 -5
  50. package/helper/macos/prebuilt/pantheon-hud +0 -0
  51. package/helper/macos/prebuilt/pantheon-tray +0 -0
  52. package/package.json +1 -1
@@ -1,9 +1,10 @@
1
- import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
1
+ import { closeSync, fsyncSync, mkdirSync, openSync, renameSync, rmSync, writeFileSync, writeSync } from "node:fs";
2
2
  import { dirname, join } from "node:path";
3
3
  import { CONFIG_PATH } from "./config.js";
4
4
  import { trustWrite } from "./home-guard.js";
5
5
  import { parseJsonText } from "./text-file.js";
6
6
  import { compareVersions, isVersion } from "./installer.js";
7
+ import { readStateText, readStateBytes, withFileLock } from "./file-lock.js";
7
8
  export const WORK_MODES = ["autopilot", "here"];
8
9
  export const MODE_LABELS = {
9
10
  autopilot: "My agent handles everything",
@@ -145,13 +146,69 @@ function notAfterNow(m) {
145
146
  const now = Date.now();
146
147
  return Object.fromEntries(Object.entries(m).map(([k, v]) => [k, typeof v === "number" && v > now ? now : v]));
147
148
  }
149
+ const isTime = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
150
+ function deniedOf(v) {
151
+ if (!isRecord(v))
152
+ return {};
153
+ const out = {};
154
+ for (const [k, at] of Object.entries(v)) {
155
+ if (isTime(at))
156
+ out[k] = at;
157
+ else if (k === "unsandboxed_runs" && at !== undefined && at !== null && at !== false)
158
+ out[k] = 1;
159
+ }
160
+ return out;
161
+ }
162
+ const readFrom = new WeakMap();
163
+ const closed = new WeakSet();
148
164
  export function loadPolicy(home) {
165
+ const file = policyPath(home);
166
+ for (let attempt = 0;; attempt++) {
167
+ let text;
168
+ try {
169
+ text = readStateText(file);
170
+ }
171
+ catch (e) {
172
+ if (e.code === "ENOENT")
173
+ return { grants: {}, asks: {}, denied: {}, attempts: {} };
174
+ if (attempt < 3) {
175
+ pause(15);
176
+ continue;
177
+ }
178
+ return closedPolicy();
179
+ }
180
+ const p = parsePolicy(text);
181
+ if (p) {
182
+ readFrom.set(p, text);
183
+ return p;
184
+ }
185
+ if (attempt < 3) {
186
+ pause(15);
187
+ continue;
188
+ }
189
+ return closedPolicy();
190
+ }
191
+ }
192
+ const pause = (ms) => { try {
193
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
194
+ }
195
+ catch { } };
196
+ function closedPolicy() {
197
+ const now = Date.now();
198
+ const p = { grants: {}, asks: {}, denied: { run_commands: now, self_upgrade: now, unsandboxed_runs: now }, attempts: {}, finalNo: ["cap:run_commands"], wakers: {} };
199
+ closed.add(p);
200
+ return p;
201
+ }
202
+ export function policyUnreadable(p) { return closed.has(p); }
203
+ function parsePolicy(text) {
149
204
  try {
150
- const raw = parseJsonText(readFileSync(policyPath(home), "utf8"));
205
+ const raw = parseJsonText(text);
206
+ if (!isRecord(raw))
207
+ return null;
151
208
  const obj = (v) => (isRecord(v) ? v : {});
152
209
  const wakers = readWakers(raw.wakers);
153
210
  return {
154
- grants: obj(raw.grants), asks: obj(raw.asks), denied: notAfterNow(obj(raw.denied)), attempts: obj(raw.attempts),
211
+ grants: obj(raw.grants), asks: obj(raw.asks), denied: notAfterNow(deniedOf(raw.denied)), attempts: obj(raw.attempts),
155
212
  ...(wakers ? { wakers } : {}),
156
213
  ...(isRecord(raw.wakersCarried) ? { wakersCarried: raw.wakersCarried } : {}),
157
214
  ...(isRecord(raw.wakersDeferred) ? { wakersDeferred: raw.wakersDeferred } : {}),
@@ -163,7 +220,7 @@ export function loadPolicy(home) {
163
220
  return [k, Array.isArray(refs) ? { ...rest, refs: refs.filter((r) => typeof r === "string") } : rest];
164
221
  })) } : {}),
165
222
  ...(isRecord(raw.wakeOnce) ? { wakeOnce: Object.fromEntries(Object.entries(raw.wakeOnce).filter(([, v]) => typeof v === "number" && v > 0 && v < 100)) } : {}),
166
- ...(isRecord(raw.wakeNo) ? { wakeNo: notAfterNow(Object.fromEntries(Object.entries(raw.wakeNo).filter(([, v]) => typeof v === "number"))) } : {}),
223
+ ...(isRecord(raw.wakeNo) ? { wakeNo: notAfterNow(Object.fromEntries(Object.entries(raw.wakeNo).filter(([, v]) => isTime(v)))) } : {}),
167
224
  ...(isRecord(raw.wakeTeamFlag) ? { wakeTeamFlag: Object.fromEntries(Object.entries(raw.wakeTeamFlag).filter(([, v]) => typeof v === "number")) } : {}),
168
225
  ...(isRecord(raw.upgradeHeld) && typeof raw.upgradeHeld.version === "string" && typeof raw.upgradeHeld.node === "string"
169
226
  ? { upgradeHeld: { version: raw.upgradeHeld.version, node: raw.upgradeHeld.node, at: Number(raw.upgradeHeld.at) || 0, why: String(raw.upgradeHeld.why ?? "") } } : {}),
@@ -173,10 +230,11 @@ export function loadPolicy(home) {
173
230
  ...(typeof raw.modeToldAt === "number" ? { modeToldAt: raw.modeToldAt } : {}),
174
231
  ...(isRecord(raw.stuckReports) ? { stuckReports: Object.fromEntries(Object.entries(raw.stuckReports).filter(([, v]) => isRecord(v) && typeof v.ref === "string" && typeof v.team === "string" && typeof v.at === "number")) } : {}),
175
232
  ...(Array.isArray(raw.finalNo) ? { finalNo: raw.finalNo.filter((k) => typeof k === "string") } : {}),
233
+ ...(isRecord(raw.safetyNotes) ? { safetyNotes: Object.fromEntries(Object.entries(raw.safetyNotes).filter(([, v]) => isRecord(v) && typeof v.says === "string" && typeof v.fix === "string" && typeof v.at === "number" && typeof v.team === "string")) } : {}),
176
234
  };
177
235
  }
178
236
  catch {
179
- return { grants: {}, asks: {}, denied: {}, attempts: {} };
237
+ return null;
180
238
  }
181
239
  }
182
240
  export function clearWakeNo(p, teams, member) {
@@ -303,15 +361,101 @@ export function isApprovedWaker(p, teams, member) {
303
361
  return teams.some((t) => { const list = t ? p.wakers?.[t] : undefined; return Array.isArray(list) && list.includes(member); });
304
362
  }
305
363
  export function savePolicy(p, home) {
364
+ if (closed.has(p))
365
+ throw new Error(`${policyPath(home)} could not be read, so nothing was saved over it — run pantheon policy repair`);
306
366
  const file = policyPath(home);
307
367
  mkdirSync(dirname(file), { recursive: true });
308
- let prev = null;
368
+ const write = () => {
369
+ let prev = null;
370
+ try {
371
+ prev = readStateBytes(file);
372
+ }
373
+ catch { }
374
+ const base = readFrom.get(p);
375
+ let next = p;
376
+ if (base !== undefined && prev && prev.toString("utf8") !== base) {
377
+ const theirs = parsePolicy(prev.toString("utf8"));
378
+ const was = parsePolicy(base);
379
+ if (theirs && was)
380
+ next = merge3(was, p, theirs);
381
+ }
382
+ const text = JSON.stringify(next, null, 2) + "\n";
383
+ atomicWrite(file, text);
384
+ readFrom.set(p, text);
385
+ if (next !== p) {
386
+ for (const k of Object.keys(p))
387
+ if (!(k in next))
388
+ delete p[k];
389
+ Object.assign(p, next);
390
+ }
391
+ trustWrite(file, prev);
392
+ };
393
+ let ran = false;
309
394
  try {
310
- prev = readFileSync(file);
395
+ withFileLock(file, () => { ran = true; write(); }, 3_000);
396
+ }
397
+ catch (e) {
398
+ if (ran)
399
+ throw e;
400
+ write();
311
401
  }
312
- catch { }
313
- writeFileSync(file, JSON.stringify(p, null, 2) + "\n", { mode: 0o600 });
314
- trustWrite(file, prev);
402
+ }
403
+ export function atomicWrite(file, text, o = {}) {
404
+ const tmp = `${file}.${process.pid}.${Date.now().toString(36)}.tmp`;
405
+ const rename = o.rename ?? renameSync;
406
+ const win = (o.platform ?? process.platform) === "win32";
407
+ try {
408
+ const fd = openSync(tmp, "wx", 0o600);
409
+ try {
410
+ writeSync(fd, text);
411
+ fsyncSync(fd);
412
+ }
413
+ finally {
414
+ closeSync(fd);
415
+ }
416
+ for (let i = 0;; i++) {
417
+ try {
418
+ rename(tmp, file);
419
+ break;
420
+ }
421
+ catch (e) {
422
+ const code = e.code;
423
+ if (!win || i >= 6 || !(code === "EPERM" || code === "EBUSY" || code === "EACCES"))
424
+ throw e;
425
+ pause(10 * 2 ** i);
426
+ }
427
+ }
428
+ }
429
+ catch (e) {
430
+ try {
431
+ rmSync(tmp, { force: true });
432
+ }
433
+ catch { }
434
+ throw e;
435
+ }
436
+ }
437
+ function merge3(base, ours, theirs) {
438
+ const eq = (a, b) => JSON.stringify(a) === JSON.stringify(b);
439
+ if (eq(ours, base))
440
+ return theirs;
441
+ if (eq(theirs, base))
442
+ return ours;
443
+ if (isRecord(base) && isRecord(ours) && isRecord(theirs)) {
444
+ const out = {};
445
+ for (const k of new Set([...Object.keys(base), ...Object.keys(ours), ...Object.keys(theirs)])) {
446
+ const v = merge3(base[k], ours[k], theirs[k]);
447
+ if (v !== undefined)
448
+ out[k] = v;
449
+ }
450
+ return out;
451
+ }
452
+ const flat = (v) => Array.isArray(v) && v.every((x) => typeof x !== "object" || x === null);
453
+ if (flat(ours) && flat(theirs) && (base === undefined || flat(base))) {
454
+ const b = base ?? [], o = ours, t = theirs;
455
+ const dropped = b.filter((x) => !o.includes(x));
456
+ return [...t, ...o.filter((x) => !b.includes(x) && !t.includes(x))].filter((x) => !dropped.includes(x));
457
+ }
458
+ return ours;
315
459
  }
316
460
  export function useGrant(p, cap) {
317
461
  const g = p.grants[cap];
@@ -376,6 +520,8 @@ export function applyLocalAnswer(refActId, body, home, env = process.env) {
376
520
  const verdict = parseGrantAnswer(body);
377
521
  if (!verdict)
378
522
  return null;
523
+ if (verdict === "allow-once" && p.finalNo?.includes(finalKey.wake(wa.team, wa.member)))
524
+ return { applied: false, why: supersededWords(wa.member) };
379
525
  const k = wakeKey(wa.team, wa.member);
380
526
  if (verdict === "allow-always")
381
527
  setWaker(p, wa.team, wa.member, true);
@@ -404,6 +550,45 @@ export function applyLocalAnswer(refActId, body, home, env = process.env) {
404
550
  savePolicy(p, home);
405
551
  return { applied: true, ...r };
406
552
  }
553
+ const supersededWords = (m) => `you said no to ${m} on this computer since this was asked (pantheon wake deny ${m}), and that stands — "just this once" would not change it. To let ${m} start work here: pantheon wake allow ${m}`;
554
+ export function supersededAnswer(pending, body, home) {
555
+ if (!pending || !("wake" in pending) || parseGrantAnswer(body) !== "allow-once")
556
+ return null;
557
+ const { team, member } = pending.wake;
558
+ return loadPolicy(home).finalNo?.includes(finalKey.wake(team, member)) ? supersededWords(member) : null;
559
+ }
560
+ export function wakeSettled(p, teams, member) {
561
+ const keys = teams.filter((t) => !!t);
562
+ if (keys.some((t) => p.finalNo?.includes(finalKey.wake(t, member))))
563
+ return "no";
564
+ return isApprovedWaker(p, keys, member) ? "yes" : null;
565
+ }
566
+ export function pendingLocalQuestion(refActId, home) {
567
+ if (!refActId)
568
+ return null;
569
+ const p = loadPolicy(home);
570
+ const wa = p.wakeAsks?.[refActId];
571
+ if (wa)
572
+ return { wake: { ...wa } };
573
+ const ask = Object.entries(p.asks).find(([, a]) => a?.act === refActId);
574
+ return ask?.[1] ? { ask: [ask[0], { ...ask[1] }] } : null;
575
+ }
576
+ export function applyLocalAnswerAfterSend(refActId, body, before, home, env = process.env) {
577
+ const g = applyLocalAnswer(refActId, body, home, env);
578
+ if (g || !before || !refActId || inWokenRun(env) || !parseGrantAnswer(body))
579
+ return g;
580
+ const p = loadPolicy(home);
581
+ if ("wake" in before)
582
+ (p.wakeAsks ??= {})[refActId] = before.wake;
583
+ else {
584
+ const [cap, ask] = before.ask;
585
+ if (p.asks[cap])
586
+ return applyLocalAnswer(p.asks[cap].act, body, home, env);
587
+ p.asks[cap] = ask;
588
+ }
589
+ savePolicy(p, home);
590
+ return applyLocalAnswer(refActId, body, home, env);
591
+ }
407
592
  export function describeLocalAnswer(g) {
408
593
  if (!g.applied)
409
594
  return `not granted: ${g.why}`;
@@ -462,4 +647,21 @@ export function isNewer(a, b) {
462
647
  }
463
648
  return false;
464
649
  }
650
+ export function safetyNotesLine(home) {
651
+ const notes = Object.values(loadPolicy(home).safetyNotes ?? {});
652
+ if (!notes.length)
653
+ return null;
654
+ const first = notes.sort((a, b) => a.at - b.at)[0];
655
+ return `${first.says}. What to do: ${first.fix}${notes.length > 1 ? ` (and ${notes.length - 1} more: pantheon status)` : ""} — once handled: pantheon status --clear-safety`;
656
+ }
657
+ export function salvagePolicyText(text) {
658
+ const out = [];
659
+ for (const m of text.matchAll(/"cap:([a-z_]+)"/g))
660
+ out.push(`no to ${CAPABILITY_ARGS[m[1]] ?? m[1]} — pantheon policy revoke ${CAPABILITY_ARGS[m[1]] ?? m[1]}`);
661
+ for (const m of text.matchAll(/"wake:[^"|]+\|([^"]+)"/g))
662
+ out.push(`no to ${m[1]} waking this machine — pantheon wake deny ${m[1]}`);
663
+ for (const m of text.matchAll(/"(run_commands|self_upgrade|unsandboxed_runs)":\s*\{\s*"mode"/g))
664
+ out.push(`allowed ${CAPABILITY_ARGS[m[1]]} — pantheon policy grant ${CAPABILITY_ARGS[m[1]]}`);
665
+ return [...new Set(out)];
666
+ }
465
667
  //# sourceMappingURL=worker-policy.js.map