@join-pantheon/cli 0.38.1 → 0.38.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.
package/bin/pantheon.js CHANGED
@@ -37,9 +37,24 @@ if (!existsSync(entry)) {
37
37
  process.exit(1);
38
38
  }
39
39
 
40
+ // After an upgrade, a release on probation is watched from here, before any of
41
+ // its own code loads, and the kept version is put back when it keeps failing
42
+ // (upgrade-guard.js, #124). The guard never stops pantheon starting.
43
+ let guarded = { entry, importFailed: () => null };
44
+ try { guarded = (await import("./upgrade-guard.js")).guard(root); } catch { /* no guard: start as before */ }
45
+
40
46
  // A path is not a URL, and on Windows that is fatal rather than pedantic: an
41
47
  // absolute POSIX path happens to work as an import specifier, while `D:\...`
42
48
  // is read as the scheme "d:" and Node refuses it — so `pantheon --version`,
43
49
  // the first command anyone runs, died with ERR_UNSUPPORTED_ESM_URL_SCHEME on
44
50
  // every Windows machine. A real Windows runner is what said so.
45
- await import(pathToFileURL(entry).href);
51
+ try {
52
+ await import(pathToFileURL(guarded.entry).href);
53
+ } catch (e) {
54
+ // A release whose code will not even load: counted, and when that was the
55
+ // last failure allowed, the version put back runs this command instead — in
56
+ // a process of its own, since this one has the failed release's modules
57
+ // loaded under the same paths (upgrade-guard.js rerun).
58
+ if (!guarded.importFailed()) throw e;
59
+ process.exitCode = (await import("./upgrade-guard.js")).rerun(process.argv.slice(1));
60
+ }
@@ -0,0 +1,273 @@
1
+ /**
2
+ * The probation after an upgrade, and the way back (issue #124).
3
+ *
4
+ * A release that answers `--version` but whose worker throws at start, or can
5
+ * never finish the handshake, used to crash-loop on every machine that took it:
6
+ * launchd's KeepAlive, systemd's Restart=always and the Windows host restarted it
7
+ * for ever, and a broken worker can never fetch the release that fixes it.
8
+ *
9
+ * So the upgrade (src/upgrade.ts stagedManagedUpgrade) leaves a mark beside the
10
+ * runtime, runtime/upgrade-probation.json, naming the version it put in place
11
+ * and the one it kept in runtime/cli.prev. Until that version has been seen to
12
+ * work (a worker that reached the relay and stayed a minute: src/crash-loop.ts),
13
+ * or two hours have passed, this file watches it:
14
+ *
15
+ * - a worker run that ends with a code no stop gives (0, 129, 130, 143), or
16
+ * that vanished without ending (killed hard, crashed in native code), counts;
17
+ * - so does any command whose code cannot even be loaded, or that dies of an
18
+ * exception nobody caught (a Kiro prompt hook, say);
19
+ * - so does a worker that the relay answers but that never came online
20
+ * (src/upgrade-probation.ts writes `never_online`).
21
+ *
22
+ * FAILS_TO_ROLL_BACK of those, and the next `pantheon` to start puts the kept
23
+ * version back — two renames, as the upgrade made them — writes
24
+ * runtime/upgrade-held.json so that release is not taken again on this machine
25
+ * (a newer one is), and carries on as the version put back. Nothing here needs
26
+ * the relay, the network or any of the release's own compiled code: it runs in
27
+ * bin/pantheon.js, before dist/cli.js is loaded, from the stable entry the
28
+ * launcher, launchd, systemd and the Windows host all start.
29
+ *
30
+ * Anything that goes wrong in here is swallowed: the guard must never be the
31
+ * reason pantheon does not start.
32
+ *
33
+ * Plain JavaScript, no dependencies, only node: built-ins, sync file calls.
34
+ * The file names and fields are shared with src/upgrade-probation.ts (a test
35
+ * holds the two together).
36
+ */
37
+ import { spawnSync } from "node:child_process";
38
+ import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
39
+ import { homedir } from "node:os";
40
+ import { basename, dirname, join } from "node:path";
41
+
42
+ export const PROBATION_FILE = "upgrade-probation.json";
43
+ export const HELD_FILE = "upgrade-held.json";
44
+ export const PROBATION_MS = 2 * 3600_000;
45
+ export const FAILS_TO_ROLL_BACK = 3;
46
+ /** exit codes a run ends with when it was told to stop (crash-loop.ts EXPECTED_EXIT_CODES) */
47
+ export const EXPECTED_EXIT_CODES = [0, 129, 130, 143];
48
+ const KEEP_RUNS = 20;
49
+
50
+ const pidAlive = (pid) => {
51
+ if (!(pid > 0)) return false;
52
+ try { process.kill(pid, 0); return true; } catch (e) { return e && e.code === "EPERM"; }
53
+ };
54
+ const pause = (ms) => { try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); } catch { /* */ } };
55
+ const readJson = (f) => { try { const v = JSON.parse(readFileSync(f, "utf8")); return v && typeof v === "object" ? v : null; } catch { return null; } };
56
+ const writeJson = (f, v) => {
57
+ const tmp = `${f}.${process.pid}.${Math.random().toString(16).slice(2, 10)}.tmp`;
58
+ writeFileSync(tmp, JSON.stringify(v, null, 2) + "\n", { mode: 0o600 });
59
+ try { renameSync(tmp, f); } catch (e) { try { rmSync(tmp, { force: true }); } catch { /* */ } throw e; }
60
+ };
61
+ const versionOf = (dir) => { const p = readJson(join(dir, "package.json")); return p && typeof p.version === "string" ? p.version : null; };
62
+ /** a whole CLI folder: its manifest, its entry and its build (upgrade.ts `whole`) */
63
+ const whole = (dir) => ["package.json", join("bin", "pantheon.js"), join("dist", "cli.js")].every((f) => existsSync(join(dir, f)));
64
+ const isTime = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
65
+
66
+ /**
67
+ * The runtime folder a managed install's CLI sits in, from the CLI's own folder,
68
+ * or null when this is any other install (npm's global, a checkout):
69
+ * <runtime>/node/lib/node_modules/@join-pantheon/cli macOS, Linux
70
+ * <runtime>\node\node_modules\@join-pantheon\cli Windows
71
+ */
72
+ export function runtimeOf(pkgDir) {
73
+ const scope = dirname(pkgDir), nm = dirname(scope);
74
+ if (basename(pkgDir) !== "cli" || basename(scope) !== "@join-pantheon" || basename(nm) !== "node_modules") return null;
75
+ const up = dirname(nm);
76
+ const node = basename(up) === "lib" ? dirname(up) : up;
77
+ const runtime = dirname(node);
78
+ return basename(node) === "node" && basename(runtime) === "runtime" ? runtime : null;
79
+ }
80
+
81
+ /** The probation file, every field checked (anything odd: no probation). */
82
+ export function readProbation(runtime) {
83
+ const p = readJson(join(runtime, PROBATION_FILE));
84
+ if (!p || typeof p.to !== "string" || !isTime(p.at)) return null;
85
+ return {
86
+ v: 1, to: p.to, at: p.at,
87
+ ...(typeof p.from === "string" ? { from: p.from } : {}),
88
+ runs: Array.isArray(p.runs) ? p.runs.filter((r) => r && Number.isInteger(r.pid) && isTime(r.at)).slice(-KEEP_RUNS) : [],
89
+ fails: Array.isArray(p.fails) ? p.fails.filter(isTime).slice(-KEEP_RUNS) : [],
90
+ ...(isTime(p.never_online) ? { never_online: p.never_online } : {}),
91
+ ...(isTime(p.stop_expected_at) ? { stop_expected_at: p.stop_expected_at } : {}),
92
+ ...(isTime(p.stuck) ? { stuck: p.stuck, ...(typeof p.stuck_why === "string" ? { stuck_why: p.stuck_why } : {}) } : {}),
93
+ };
94
+ }
95
+
96
+ /** Read, changed, written back; null when there is no probation (or it could not be written). */
97
+ function change(runtime, fn) {
98
+ const p = readProbation(runtime);
99
+ if (!p) return null;
100
+ fn(p);
101
+ try { writeJson(join(runtime, PROBATION_FILE), p); } catch { return null; }
102
+ return p;
103
+ }
104
+
105
+ /**
106
+ * Worker runs that vanished without saying how they ended — their process gone,
107
+ * started before no expected stop — become failures.
108
+ */
109
+ function foldLost(p, self, alive) {
110
+ for (const r of p.runs) {
111
+ if (r.end !== undefined || r.pid === self || alive(r.pid)) continue;
112
+ r.end = "lost";
113
+ if (!(isTime(p.stop_expected_at) && p.stop_expected_at >= r.at)) p.fails.push(r.at);
114
+ }
115
+ }
116
+
117
+ /** Take the upgrade's own lock folder (upgrade.ts realSwapIo.lock): an upgrade under way is never raced. */
118
+ function takeLock(dir, alive) {
119
+ for (let i = 0; i < 2; i++) {
120
+ try { mkdirSync(dir); writeFileSync(join(dir, "pid"), String(process.pid)); return true; }
121
+ catch (e) {
122
+ if (!e || e.code !== "EEXIST") return false;
123
+ let pid = 0, age = 0;
124
+ try { pid = Number(readFileSync(join(dir, "pid"), "utf8").trim()); } catch { /* */ }
125
+ try { age = Date.now() - statSync(dir).mtimeMs; } catch { continue; }
126
+ if (alive(pid) ? age < 20 * 60_000 : !pid && age < 10_000) return false;
127
+ const gone = `${dir}.stale-${process.pid}`;
128
+ try { renameSync(dir, gone); rmSync(gone, { recursive: true, force: true }); } catch { /* the next try says */ }
129
+ }
130
+ }
131
+ return false;
132
+ }
133
+ function dropLock(dir) {
134
+ try { if (Number(readFileSync(join(dir, "pid"), "utf8").trim()) === process.pid) rmSync(dir, { recursive: true, force: true }); } catch { /* */ }
135
+ }
136
+
137
+ /** A rename retried for a few seconds: Windows refuses one while a scanner holds a file. */
138
+ function renameRetry(a, b, io) {
139
+ for (let i = 0; i < 6; i++) {
140
+ try { io.rename(a, b); return true; } catch { if (i < 5) io.sleep(300); }
141
+ }
142
+ return false;
143
+ }
144
+
145
+ /** The crash-loop record (crash-loop.ts) told the worker's next start is not a failure: the start after a way back is a fresh one. */
146
+ function clearWorkerStarts(home) {
147
+ try {
148
+ const f = join(home, "service-starts.json");
149
+ const s = readJson(f);
150
+ if (s && s.worker && Array.isArray(s.worker.starts) && s.worker.starts.length) { s.worker.starts = []; writeJson(f, s); }
151
+ } catch { /* counted once more, at worst */ }
152
+ }
153
+
154
+ /**
155
+ * Put the kept version back. Returns the path of its dist/cli.js when it is in
156
+ * place, else null (and the version on probation keeps running; the next start
157
+ * tries again, or says why it cannot).
158
+ */
159
+ export function rollBack(pkgDir, runtime, p, o = {}) {
160
+ const io = { rename: renameSync, sleep: pause, ...(o.io || {}) };
161
+ const alive = o.alive || pidAlive;
162
+ const now = (o.now || Date.now)();
163
+ const log = o.log || ((line) => { try { process.stderr.write(`${line}\n`); } catch { /* */ } });
164
+ const prev = join(runtime, "cli.prev"), failed = join(runtime, "cli.failed"), lock = join(runtime, ".upgrade.lock");
165
+ const held = join(runtime, HELD_FILE);
166
+ if (!takeLock(lock, alive)) return null;
167
+ try {
168
+ const back = whole(prev) ? versionOf(prev) : null;
169
+ if (!back || back === p.to) {
170
+ // Nothing to go back to: said once (here and in the file doctor reads), and not tried again.
171
+ const why = !back ? "no previous version is kept beside it" : "the version kept beside it is the same release";
172
+ change(runtime, (q) => { q.stuck = now; q.stuck_why = why; });
173
+ log(`! pantheon ${p.to} is not working on this computer and cannot go back by itself (${why}). Run the installer again to repair it`);
174
+ return null;
175
+ }
176
+ // Written first: cut off between the renames below, the launcher (or the
177
+ // Windows host) moves cli.prev into place by itself, and this says why.
178
+ try { writeJson(held, { v: 1, version: p.to, back_to: back, at: now, why: p.never_online ? "never_online" : "failed_starts" }); } catch { /* the swap still helps */ }
179
+ if (existsSync(failed)) { try { rmSync(failed, { recursive: true, force: true }); } catch { /* */ } }
180
+ if (existsSync(failed) || !renameRetry(pkgDir, failed, io)) {
181
+ try { rmSync(held, { force: true }); } catch { /* */ }
182
+ return null;
183
+ }
184
+ if (!renameRetry(prev, pkgDir, io)) {
185
+ // The release on probation goes back where it was; if even that is refused,
186
+ // the folder is missing and the launcher puts cli.prev in on the next start.
187
+ if (renameRetry(failed, pkgDir, io)) { try { rmSync(held, { force: true }); } catch { /* */ } }
188
+ return null;
189
+ }
190
+ try { rmSync(join(runtime, PROBATION_FILE), { force: true }); } catch { /* */ }
191
+ try { rmSync(failed, { recursive: true, force: true }); } catch { /* the next upgrade removes it */ }
192
+ clearWorkerStarts(o.home || process.env.PANTHEON_HOME || join(homedir(), ".pantheon"));
193
+ log(`! pantheon ${p.to} did not work on this computer, so it went back to ${back} by itself. It updates again when a newer release is out`);
194
+ return join(pkgDir, "dist", "cli.js");
195
+ } finally { dropLock(lock); }
196
+ }
197
+
198
+ /**
199
+ * Called by bin/pantheon.js before the CLI is loaded. Returns:
200
+ * entry the dist/cli.js to load (the version put back, after a way back)
201
+ * importFailed() the CLI would not load: counted; true when that was the last
202
+ * failure allowed and the version put back is in place (run it
203
+ * with rerun(): never in this process)
204
+ */
205
+ export function guard(pkgDir, argv = process.argv.slice(2), o = {}) {
206
+ const own = join(pkgDir, "dist", "cli.js");
207
+ const none = { entry: own, importFailed: () => false };
208
+ const runtime = o.runtime === undefined ? runtimeOf(pkgDir) : o.runtime;
209
+ if (!runtime || !existsSync(join(runtime, PROBATION_FILE))) return none;
210
+ const alive = o.alive || pidAlive;
211
+ const now = o.now || Date.now;
212
+ const p0 = readProbation(runtime);
213
+ const version = versionOf(pkgDir);
214
+ if (!p0 || !version || p0.to !== version) return none; // not the release on probation (the one put back, or a later one)
215
+ if (p0.stuck) return none;
216
+ if (now() - p0.at > PROBATION_MS) { try { rmSync(join(runtime, PROBATION_FILE), { force: true }); } catch { /* */ } return none; }
217
+ const due = (p) => !!p.never_online || p.fails.length >= FAILS_TO_ROLL_BACK;
218
+ const back = (p) => rollBack(pkgDir, runtime, p, o);
219
+
220
+ const p = change(runtime, (q) => foldLost(q, process.pid, alive)) || p0;
221
+ if (due(p)) { const e = back(p); if (e) return { entry: e, importFailed: () => false }; }
222
+
223
+ const worker = argv[0] === "worker" && argv[1] !== "token";
224
+ let counted = false;
225
+ const fail = () => { if (counted) return; counted = true; change(runtime, (q) => { q.fails.push(now()); }); };
226
+ if (worker) change(runtime, (q) => { q.runs.push({ pid: process.pid, at: now() }); q.runs = q.runs.slice(-KEEP_RUNS); });
227
+ if (!o.passive) {
228
+ let crashed = false;
229
+ try { process.on("uncaughtExceptionMonitor", () => { crashed = true; }); } catch { /* */ }
230
+ process.on("exit", (code) => {
231
+ try {
232
+ const expected = EXPECTED_EXIT_CODES.includes(code) || globalThis.__pantheonExpectedExit === true;
233
+ if (worker) {
234
+ change(runtime, (q) => {
235
+ const r = q.runs.find((x) => x.pid === process.pid && x.end === undefined);
236
+ if (r) r.end = code;
237
+ if (!counted && (crashed || !expected)) { counted = true; q.fails.push(now()); }
238
+ });
239
+ } else if (crashed && !counted) fail();
240
+ } catch { /* never in the way of an exit */ }
241
+ });
242
+ }
243
+ return {
244
+ entry: own,
245
+ importFailed: () => {
246
+ try {
247
+ fail();
248
+ const q = readProbation(runtime);
249
+ if (q && q.to === version && due(q)) return !!back(q);
250
+ } catch { /* */ }
251
+ return false;
252
+ },
253
+ };
254
+ }
255
+
256
+ /** Services the service manager starts again by itself: ended, so it starts the version put back. */
257
+ const SERVICES = ["worker", "notifier", "host", "menubar", "relay"];
258
+
259
+ /**
260
+ * The command again, on the version put back, after the release failed to load
261
+ * in this process. Node keeps a module that failed — and every module of that
262
+ * release that did load — under its path, which is now the version put back's:
263
+ * loading it here would mix the two. A service ends instead (its manager starts
264
+ * it again, on the version put back); any other command — an agent's hook that
265
+ * would otherwise block every prompt — runs once more as a child, its input and
266
+ * output its own. Returns the exit code.
267
+ */
268
+ export function rerun(argv, o = {}) {
269
+ const cmd = argv[1];
270
+ if (SERVICES.includes(cmd)) return 1;
271
+ const r = (o.spawn || spawnSync)(process.execPath, [...process.execArgv, ...argv], { stdio: "inherit", windowsHide: true });
272
+ return typeof r.status === "number" ? r.status : 1;
273
+ }
package/dist/cli.js CHANGED
@@ -2553,8 +2553,27 @@ let workerAlerts = null;
2553
2553
  let workerAlertsSaid = false;
2554
2554
  async function trackServiceStart(kind, noNotify) {
2555
2555
  try {
2556
- const { trackService, composeCrashLoop, loopLine } = await import("./crash-loop.js");
2556
+ const { trackService, composeCrashLoop, loopLine, serviceConnected } = await import("./crash-loop.js");
2557
+ const probation = await import("./upgrade-probation.js");
2558
+ if (kind === "worker") {
2559
+ const relay = (() => { try {
2560
+ return load()?.relay ?? null;
2561
+ }
2562
+ catch {
2563
+ return null;
2564
+ } })();
2565
+ if (relay) {
2566
+ probation.watchNeverOnline({
2567
+ version: VERSION, connected: serviceConnected,
2568
+ relayAnswers: async () => (await (await import("./version-health.js")).fetchRelayPkg(relay, 5000)) !== null,
2569
+ exit: () => { process.exit(1); },
2570
+ log: (line) => console.error(line),
2571
+ });
2572
+ }
2573
+ }
2557
2574
  trackService(kind, {
2575
+ onConnectedHealthy: () => { if (probation.probationPassed(VERSION))
2576
+ console.error(D(` pantheon ${VERSION} works here: the upgrade is kept`)); },
2558
2577
  onLoop: (loop) => {
2559
2578
  console.error(`! ${loopLine(kind, loop)}`);
2560
2579
  if (kind === "worker") {
@@ -3,6 +3,7 @@ import { join } from "node:path";
3
3
  import { readStateText, withFileLock } from "./file-lock.js";
4
4
  import { atomicWrite } from "./worker-policy.js";
5
5
  import { BUDGET } from "./notice.js";
6
+ import { probationStopExpected } from "./upgrade-probation.js";
6
7
  export const WATCHED = ["worker", "notifier"];
7
8
  export const LOOP_WINDOW_MS = 10 * 60_000;
8
9
  export const LOOP_STARTS = 3;
@@ -115,6 +116,12 @@ export function markExpectedExit(kind, o = {}) {
115
116
  export function stopIsExpected(kind, o = {}) {
116
117
  if (kind === "worker" || kind === "notifier")
117
118
  markExpectedExit(kind, o);
119
+ if (kind === "worker") {
120
+ try {
121
+ probationStopExpected();
122
+ }
123
+ catch { }
124
+ }
118
125
  }
119
126
  export function activeLoop(kind, o = {}) {
120
127
  const loop = (o.file ?? readStarts(o.home))[kind]?.loop;
@@ -157,7 +164,7 @@ export function trackService(kind, o = {}) {
157
164
  r = recordStart(kind, { ...(o.home ? { home: o.home } : {}), now: now() });
158
165
  }
159
166
  catch { }
160
- tracking = { kind, ...(o.home ? { home: o.home } : {}), startedAt: now(), healthy: false, expectExit: false, connected: false, timers: [] };
167
+ tracking = { kind, ...(o.home ? { home: o.home } : {}), startedAt: now(), healthy: false, expectExit: false, connected: false, ...(o.onConnectedHealthy ? { onConnectedHealthy: o.onConnectedHealthy } : {}), timers: [] };
161
168
  const t = tracking;
162
169
  if (!o.passive) {
163
170
  const steady = setTimeout(() => healthyNow(t), STEADY_AFTER_MS);
@@ -192,15 +199,25 @@ function healthyNow(t) {
192
199
  }
193
200
  export function noteServiceConnected() {
194
201
  const t = tracking;
195
- if (!t || t.healthy || t.connected)
202
+ if (!t || t.connected)
196
203
  return;
197
204
  t.connected = true;
198
- const timer = setTimeout(() => healthyNow(t), HEALTHY_AFTER_CONNECT_MS);
205
+ const timer = setTimeout(() => {
206
+ healthyNow(t);
207
+ try {
208
+ t.onConnectedHealthy?.();
209
+ }
210
+ catch { }
211
+ }, HEALTHY_AFTER_CONNECT_MS);
199
212
  timer.unref?.();
200
213
  t.timers.push(timer);
201
214
  }
202
- export function expectServiceExit() { if (tracking)
203
- tracking.expectExit = true; }
215
+ export function serviceConnected() { return !!tracking?.connected; }
216
+ export function expectServiceExit() {
217
+ if (tracking)
218
+ tracking.expectExit = true;
219
+ globalThis.__pantheonExpectedExit = true;
220
+ }
204
221
  export function resetServiceTracking() { if (tracking)
205
222
  for (const t of tracking.timers)
206
223
  clearTimeout(t); tracking = null; }
package/dist/file-lock.js CHANGED
@@ -182,6 +182,24 @@ export function brokenStateFix(broken) {
182
182
  parts.push(`and the folder${folders.length === 1 ? "" : "s"} ${folders.join(", ")} in the Pantheon folder (on a Mac: chflags nouchg,nouappnd on ${folders.length === 1 ? "it" : "them"})`);
183
183
  return parts.join("; ");
184
184
  }
185
+ export function removeAside(p, o = {}) {
186
+ const rm = o.rm ?? ((x) => rmSync(x, { force: true }));
187
+ const tries = o.tries ?? 5;
188
+ for (let i = 0; i < tries; i++) {
189
+ try {
190
+ rm(p);
191
+ return true;
192
+ }
193
+ catch (e) {
194
+ const code = e.code;
195
+ if (code !== "EBUSY" && code !== "EPERM" && code !== "EACCES")
196
+ return false;
197
+ if (i < tries - 1)
198
+ (o.sleep ?? sleepSync)(5 * (i + 1));
199
+ }
200
+ }
201
+ return false;
202
+ }
185
203
  export function withFileLock(f, fn, waitMs = WAIT_MS) {
186
204
  mkdirSync(dirname(f), { recursive: true, mode: 0o700 });
187
205
  const lock = `${f}.lock`;
@@ -212,10 +230,7 @@ export function withFileLock(f, fn, waitMs = WAIT_MS) {
212
230
  }
213
231
  }
214
232
  finally {
215
- try {
216
- rmSync(made, { force: true });
217
- }
218
- catch { }
233
+ removeAside(made);
219
234
  }
220
235
  try {
221
236
  return fn();
@@ -521,6 +521,7 @@ export const REFUSAL_WORDS = {
521
521
  SPACE_UNREADABLE: "a team whose files could not be read",
522
522
  STALE_VIEW: "a change made from an out-of-date view",
523
523
  STORAGE: "a write the disk refused",
524
+ STORAGE_BUSY: "the disk was behind on this team's writes",
524
525
  SUMMARY_REQUIRED: "a long message with no one-line summary",
525
526
  TO_REQUIRED: "an ask that named nobody",
526
527
  TOO_MANY_GOALS: "too many open goals",
@@ -64,12 +64,17 @@ export const PANTHEON_HOME_ENTRIES = [
64
64
  { name: "data", what: "the relay's ledger", dir: true, kept: "it is your team's history and may be its only copy; delete it yourself once no relay runs from it" },
65
65
  { name: /^(\.DS_Store|Thumbs\.db|desktop\.ini)$/i, sample: ".DS_Store", what: "the file browser's folder cache" },
66
66
  ];
67
+ const LOCK_DEBRIS = /^(.+?)\.lock\.(?:[0-9a-f]+\.new|break(?:\.\d+\.[0-9a-f]+)?|stale\.\d+\.[0-9a-f]+)$/i;
67
68
  const LOCK_OR_TMP = /^(.+?)(\.lock|(\.[0-9a-f]+)*\.tmp)$/i;
68
69
  export function homeEntryFor(name) {
69
70
  const direct = (n) => PANTHEON_HOME_ENTRIES.find((e) => typeof e.name === "string" ? e.name === n : e.name.test(n)) ?? null;
70
71
  const e = direct(name);
71
72
  if (e)
72
73
  return e;
74
+ const d = LOCK_DEBRIS.exec(name);
75
+ const debrisOf = d ? direct(d[1]) : null;
76
+ if (debrisOf && !debrisOf.dir)
77
+ return { name, what: `a lock's leftover file (${debrisOf.what})` };
73
78
  const m = LOCK_OR_TMP.exec(name);
74
79
  const base = m ? direct(m[1]) : null;
75
80
  return base && !base.dir ? base : null;
package/dist/leftovers.js CHANGED
@@ -2,7 +2,7 @@ import { closeSync, existsSync, fstatSync, openSync, readFileSync, readSync, rea
2
2
  import { join } from "node:path";
3
3
  import { pendingView } from "./disk-writer.js";
4
4
  export const DELETED_NAME_HOLD_MS = 3_600_000;
5
- export const TEAM_FILES = [".members.json", ".jsonl", ".state.json", ".usage.json"];
5
+ export const TEAM_FILES = [".members.json", ".jsonl", ".state.json", ".usage.json", ".closed-goals.json"];
6
6
  const iso = (t) => new Date(t).toISOString().replace(/\.\d+Z$/, "Z");
7
7
  const strs = (x) => (Array.isArray(x) ? x.filter((h) => typeof h === "string") : []);
8
8
  export function tombViewOfText(text) {
package/dist/mcp.js CHANGED
@@ -19,6 +19,7 @@ import { openFailureWhy, openQuestionGoal, questionFailureText, settleFailedQues
19
19
  import { VERSION } from "./version.js";
20
20
  import { installedVersion } from "./mcp-supervisor.js";
21
21
  import { behindStatusLine, localVersion, readVersionHealth, refreshRelayPkg, relayKey, RELAY_PKG_REFRESH_MS } from "./version-health.js";
22
+ import { readRolledBack } from "./upgrade-probation.js";
22
23
  import { loadPolicy } from "./worker-policy.js";
23
24
  import { backendFromLabel } from "./agents.js";
24
25
  import { agentHookStates } from "./notify.js";
@@ -406,7 +407,13 @@ export async function runMcp() {
406
407
  catch {
407
408
  policy = null;
408
409
  }
409
- const line = behindStatusLine({ local: localVersion(), seen, now: Date.now(), policy, state, full: !behindSaid });
410
+ const rolledBack = (() => { try {
411
+ return readRolledBack()?.version ?? null;
412
+ }
413
+ catch {
414
+ return null;
415
+ } })();
416
+ const line = behindStatusLine({ local: localVersion(), seen, now: Date.now(), policy, state, full: !behindSaid, rolledBack });
410
417
  if (line?.startsWith("PANTHEON IS OUT OF DATE"))
411
418
  behindSaid = true;
412
419
  return line;
package/dist/plain.js CHANGED
@@ -129,6 +129,7 @@ export const STEPS = {
129
129
  RESERVED_NAME: "pick another name",
130
130
  SIGNED_ONLY: "run it as the founder: `pantheon usage`",
131
131
  STORAGE: "wait a moment, then check `pantheon history` before sending it again — it may already be kept",
132
+ STORAGE_BUSY: "wait a few seconds, then send it again",
132
133
  SHUTTING_DOWN: "wait a few seconds, then try again",
133
134
  TOO_MANY_REFUSALS: "fix the request that keeps being refused, then reconnect",
134
135
  ID_TAKEN: "it may already be recorded — check `pantheon history` before sending it again",
package/dist/relay.js CHANGED
@@ -439,6 +439,10 @@ const LOOP_WINDOW_MS = 60_000;
439
439
  function loopMs(ns) {
440
440
  return Math.max(0, Math.round((ns / 1e6 - LOOP_RESOLUTION_MS) * 100) / 100);
441
441
  }
442
+ const CLAIM_RATE = 120;
443
+ const ROSTER_EVERY_MS = 500;
444
+ const CLOSE_COMPACT_EVERY_MS = 60_000;
445
+ const OUTBOX_MAX = 5_000;
442
446
  const LINK_CLOSED = "Different words were typed here, so this link is closed. That's all: the 6-digit code in the same email still works. Give it to your coding agent.";
443
447
  const LINK_GONE = "This link has expired (they last 10 minutes), a newer Pantheon email replaced it, or it was already used. Use the code in the newest Pantheon email, or ask your coding agent to send a new one.";
444
448
  const STORAGE_HELD = "the relay received this but has not saved it yet — its disk is refusing writes. It is kept, and reaches the team when the disk recovers; do not send it again.";
@@ -624,6 +628,14 @@ export class Relay {
624
628
  goalView(sp, g, held) {
625
629
  return g.state === "CLOSED" ? g : withLast(g, this.lastsOf(sp).get(g.id), held?.has(g.id) ?? false);
626
630
  }
631
+ welcomeGoal(sp, g, held, aged) {
632
+ if (g.state === "CLOSED" && aged.has(g.id) && g.closed_summary !== undefined) {
633
+ const slim = { ...g };
634
+ delete slim.closed_summary;
635
+ return slim;
636
+ }
637
+ return this.goalView(sp, g, held);
638
+ }
627
639
  heldIn(sp) {
628
640
  return heldGoals(sp.obligations.values(), (id) => sp.ledger.find(id)?.goal);
629
641
  }
@@ -682,6 +694,7 @@ export class Relay {
682
694
  closing = false;
683
695
  fsyncOff = false;
684
696
  compactedAt = new Map();
697
+ get outboxMax() { return this.opts.outboxMax ?? OUTBOX_MAX; }
685
698
  sessionSeq = 0;
686
699
  spaceCreates = new Map();
687
700
  limits;
@@ -839,6 +852,14 @@ export class Relay {
839
852
  await new Promise((r) => this.wss.close(() => r()));
840
853
  await new Promise((r) => setImmediate(r));
841
854
  for (const sp of this.spaces.values()) {
855
+ if (sp.rosterTimer) {
856
+ clearTimeout(sp.rosterTimer);
857
+ sp.rosterTimer = undefined;
858
+ }
859
+ if (sp.closeCompactTimer) {
860
+ clearTimeout(sp.closeCompactTimer);
861
+ sp.closeCompactTimer = undefined;
862
+ }
842
863
  try {
843
864
  sp.store.close(sp.goals, sp.obligations, sp.profiles);
844
865
  }
@@ -2972,7 +2993,7 @@ ${body}`;
2972
2993
  }
2973
2994
  quarantining = new Set();
2974
2995
  moveToQuarantine(stem) {
2975
- const sufs = [".members.json", ".members.json.tmp", ".jsonl", ".jsonl.tmp", ".state.json", ".state.json.tmp", ".usage.json", ".usage.json.tmp"];
2996
+ const sufs = [".members.json", ".members.json.tmp", ".jsonl", ".jsonl.tmp", ".state.json", ".state.json.tmp", ".usage.json", ".usage.json.tmp", ".closed-goals.json", ".closed-goals.json.tmp"];
2976
2997
  let files = [];
2977
2998
  try {
2978
2999
  files = readdirSync(this.dataDir);
@@ -2982,7 +3003,7 @@ ${body}`;
2982
3003
  }
2983
3004
  const mine = [
2984
3005
  ...sufs.flatMap((x) => (files.includes(`${stem}${x}`) ? [`${stem}${x}`] : [])),
2985
- ...files.filter((f) => f.startsWith(`${stem}.usage.json.corrupt-`)),
3006
+ ...files.filter((f) => f.startsWith(`${stem}.usage.json.corrupt-`) || f.startsWith(`${stem}.closed-goals.json.corrupt-`)),
2986
3007
  ];
2987
3008
  if (!mine.length)
2988
3009
  return { ok: false, error: `no files of "${stem}" to move`, moved: [], rolledBack: true };
@@ -3022,7 +3043,7 @@ ${body}`;
3022
3043
  }
3023
3044
  verifyRemoved(stem, attempt) {
3024
3045
  void writerFor(this.dataDir, stem).whenWritten().then(() => {
3025
- const left = [".jsonl", ".state.json", ".members.json", ".usage.json"].filter((x) => existsSync(join(this.dataDir, `${stem}${x}`)));
3046
+ const left = [".jsonl", ".state.json", ".members.json", ".usage.json", ".closed-goals.json"].filter((x) => existsSync(join(this.dataDir, `${stem}${x}`)));
3026
3047
  if (!left.length)
3027
3048
  return;
3028
3049
  const m = this.moveToQuarantine(stem);
@@ -3755,7 +3776,7 @@ ${body}`;
3755
3776
  return out;
3756
3777
  }
3757
3778
  async teamFilesSig(safe) {
3758
- const files = [".jsonl", ".state.json", ".members.json", ".usage.json"].map((suf) => join(this.dataDir, `${safe}${suf}`));
3779
+ const files = [".jsonl", ".state.json", ".members.json", ".usage.json", ".closed-goals.json"].map((suf) => join(this.dataDir, `${safe}${suf}`));
3759
3780
  if (files.some((f) => pendingView(this.dataDir, safe, f) !== undefined))
3760
3781
  return `pending:${Date.now()}:${Math.random()}`;
3761
3782
  const st = await Promise.all(files.map((f) => fsp.stat(f).then((x) => `${x.size}:${x.mtimeMs}`, () => "-")));
@@ -5004,7 +5025,7 @@ pantheon start</code></pre>
5004
5025
  }
5005
5026
  this.send(ws, {
5006
5027
  t: "welcome", member: me.id, session: sessionId, space: sp.name, seq: sp.ledger.head,
5007
- roster: this.roster(sp), now: Date.now(), goals: ((held) => [...sp.goals.values()].map((g) => this.goalView(sp, g, held)))(this.heldIn(sp)),
5028
+ roster: this.roster(sp), now: Date.now(), goals: ((held, aged) => [...sp.goals.values()].map((g) => this.welcomeGoal(sp, g, held, aged)))(this.heldIn(sp), sp.store.agedIds(sp.goals)),
5008
5029
  obligations: [...sp.obligations.values()], members: this.members(sp), profiles: [...sp.profiles.values()],
5009
5030
  space_id: sp.allow.spaceId(), repos: sp.allow.repoList(), features: [...this.features],
5010
5031
  ...(acct ? { plan: this.planView(acct, sp) } : {}),
@@ -5040,6 +5061,9 @@ pantheon start</code></pre>
5040
5061
  if (delegated && m.t !== "act" && m.t !== "open_goal" && m.t !== "claim" && m.t !== "worker_state") {
5041
5062
  return fail("NOT_FOR_A_RUN", `an unattended run cannot ${m.t.replace(/_/g, " ")} — its member does that from their own session`, m.cid);
5042
5063
  }
5064
+ if (m.t !== "worker_state" && (space.outbox?.length ?? 0) >= this.outboxMax) {
5065
+ return fail("STORAGE_BUSY", "the relay's disk is behind on this team's writes, so this was not taken. Wait a few seconds, then send it again.", m.cid);
5066
+ }
5043
5067
  if (m.t === "publish_profile") {
5044
5068
  if (m.profile.member !== me.id) {
5045
5069
  return fail("PROFILE_NOT_YOURS", `you may publish only the profile for ${me.id}`, m.cid);
@@ -5156,7 +5180,7 @@ pantheon start</code></pre>
5156
5180
  me.worker = { state: m.state, ...(detail ? { detail } : {}), ...(until ? { until } : {}), ...(m.idle_ms !== undefined ? { idle_ms: m.idle_ms } : {}),
5157
5181
  ...(reason ? { reason } : {}), ...(m.ref ? { ref: m.ref } : {}), ...(m.goal ? { goal: m.goal } : {}),
5158
5182
  ...(m.mode && this.features.includes("autopilot") ? { mode: m.mode } : {}), at: now };
5159
- this.broadcast(space, () => ({ t: "roster", roster: this.roster(space), now: Date.now() }));
5183
+ this.rosterSoon(space);
5160
5184
  return;
5161
5185
  }
5162
5186
  if (m.t === "delete_space") {
@@ -5291,6 +5315,8 @@ pantheon start</code></pre>
5291
5315
  return;
5292
5316
  }
5293
5317
  if (m.t === "claim") {
5318
+ if (!this.claimOk(me))
5319
+ return fail("RATE_LIMIT", "too many claims too fast — wait a few seconds", m.cid);
5294
5320
  if (role === "worker") {
5295
5321
  const plan = this.spacePlan(space.name);
5296
5322
  if (plan && !plan.limits.wake)
@@ -5379,6 +5405,11 @@ pantheon start</code></pre>
5379
5405
  return fail("NOT_ALLOWED_TO_WAKE", `"${origin.from}" is not permitted to wake a worker on this machine`, m.cid);
5380
5406
  }
5381
5407
  }
5408
+ if (o.claimed_by && o.claimed_by.session === sessionId && o.claimed_by.role === role) {
5409
+ o.claimed_by.at = Date.now();
5410
+ this.send(ws, { t: "ack", ...(m.cid ? { cid: m.cid } : {}), id: o.id, seq: space.ledger.head });
5411
+ return;
5412
+ }
5382
5413
  o.claimed_by = { session: sessionId, role, at: Date.now() };
5383
5414
  space.store.noteFirstClaim({ ob: o.id, ref: o.ref, owner: o.owner, at: o.claimed_by.at, by: role === "worker" ? "worker" : "person" });
5384
5415
  this.broadcast(space, { t: "claimed", obligation: o.id, session: sessionId, member: me.id });
@@ -5550,14 +5581,8 @@ pantheon start</code></pre>
5550
5581
  this.countUsage(sp, (b, now) => { countMessage(b, rec.act, role, now); if (closed)
5551
5582
  countGoal(b, "closed", now); });
5552
5583
  }
5553
- if (wasFull && m.act === "CLOSE") {
5554
- try {
5555
- this.compactSpace(space);
5556
- }
5557
- catch (e) {
5558
- this.noteStorageFailure(`[${space.name}] compacting after a close`, e);
5559
- }
5560
- }
5584
+ if (wasFull && m.act === "CLOSE")
5585
+ this.compactAfterClose(space);
5561
5586
  space.recent.set(fp, { id: rec.id, seq: rec.seq, at: Date.now() });
5562
5587
  for (const [k, v] of space.recent)
5563
5588
  if (Date.now() - v.at > LIMITS.dedupeWindowMs)
@@ -6247,6 +6272,7 @@ pantheon start</code></pre>
6247
6272
  if (goal) {
6248
6273
  goal.state = "CLOSED";
6249
6274
  goal.closed_summary = rec.body;
6275
+ goal.closed_at = rec.ts;
6250
6276
  const dt = rec.meta?.defers_to;
6251
6277
  if (dt)
6252
6278
  goal.defers_to = dt;
@@ -6329,6 +6355,31 @@ pantheon start</code></pre>
6329
6355
  me.stamps.push(now);
6330
6356
  return true;
6331
6357
  }
6358
+ claimOk(me) {
6359
+ const now = Date.now();
6360
+ const st = (me.claimStamps ?? []).filter((t) => now - t < LIMITS.rateWindowMs);
6361
+ me.claimStamps = st;
6362
+ if (st.length >= CLAIM_RATE)
6363
+ return false;
6364
+ st.push(now);
6365
+ return true;
6366
+ }
6367
+ rosterSoon(sp) {
6368
+ if (sp.rosterTimer)
6369
+ return;
6370
+ const since = Date.now() - (sp.rosterAt ?? 0);
6371
+ const go = () => {
6372
+ sp.rosterTimer = undefined;
6373
+ if (this.closing || sp.deleted || this.spaces.get(spaceKey(sp.name)) !== sp)
6374
+ return;
6375
+ sp.rosterAt = Date.now();
6376
+ this.broadcast(sp, () => ({ t: "roster", roster: this.roster(sp), now: Date.now() }), "roster");
6377
+ };
6378
+ if (since >= ROSTER_EVERY_MS)
6379
+ return go();
6380
+ sp.rosterTimer = setTimeout(go, ROSTER_EVERY_MS - since);
6381
+ sp.rosterTimer.unref?.();
6382
+ }
6332
6383
  mayCreateSpace(ip) {
6333
6384
  const now = Date.now();
6334
6385
  if (!this.localTrusted(ip)) {
@@ -6425,11 +6476,11 @@ pantheon start</code></pre>
6425
6476
  }
6426
6477
  deleteSpaceFiles(name) {
6427
6478
  const safe = name.replace(/[^a-z0-9._-]/gi, "_");
6428
- for (const suffix of [".jsonl", ".state.json", ".members.json", ".usage.json"])
6479
+ for (const suffix of [".jsonl", ".state.json", ".members.json", ".usage.json", ".closed-goals.json"])
6429
6480
  writerFor(this.dataDir, safe).remove(join(this.dataDir, `${safe}${suffix}`));
6430
6481
  try {
6431
6482
  for (const f of readdirSync(this.dataDir))
6432
- if (f.startsWith(`${safe}.usage.json.corrupt-`))
6483
+ if (f.startsWith(`${safe}.usage.json.corrupt-`) || f.startsWith(`${safe}.closed-goals.json.corrupt-`))
6433
6484
  writerFor(this.dataDir, safe).remove(join(this.dataDir, f));
6434
6485
  }
6435
6486
  catch { }
@@ -6702,6 +6753,9 @@ pantheon start</code></pre>
6702
6753
  }
6703
6754
  for (const o of sp.obligations.values())
6704
6755
  o.created_at = sp.ledger.find(o.ref)?.ts ?? o.created_at;
6756
+ for (const g of sp.store.closedGoals())
6757
+ if (!sp.goals.has(g.id))
6758
+ sp.goals.set(g.id, g);
6705
6759
  let open = 0;
6706
6760
  for (const g of sp.goals.values()) {
6707
6761
  if (g.state === "CLOSED")
@@ -6737,6 +6791,27 @@ pantheon start</code></pre>
6737
6791
  ...(kindFromOpening(meta?.goal_sig, mm[4]) ? { kind: kindFromOpening(meta?.goal_sig, mm[4]) } : {}),
6738
6792
  };
6739
6793
  }
6794
+ compactAfterClose(sp) {
6795
+ if (sp.closeCompactTimer)
6796
+ return;
6797
+ const run = () => {
6798
+ sp.closeCompactTimer = undefined;
6799
+ if (this.closing || sp.deleted || this.spaces.get(spaceKey(sp.name)) !== sp)
6800
+ return;
6801
+ sp.closeCompactAt = Date.now();
6802
+ try {
6803
+ this.compactSpace(sp);
6804
+ }
6805
+ catch (e) {
6806
+ this.noteStorageFailure(`[${sp.name}] compacting after a close`, e);
6807
+ }
6808
+ };
6809
+ const since = Date.now() - (sp.closeCompactAt ?? 0);
6810
+ if (since >= CLOSE_COMPACT_EVERY_MS)
6811
+ return run();
6812
+ sp.closeCompactTimer = setTimeout(run, CLOSE_COMPACT_EVERY_MS - since);
6813
+ sp.closeCompactTimer.unref?.();
6814
+ }
6740
6815
  compactSpace(sp) {
6741
6816
  this.compactedAt.set(sp.name, Date.now());
6742
6817
  if (sp.unclean)
@@ -6880,7 +6955,7 @@ pantheon start</code></pre>
6880
6955
  const sp = this.spaceOf.get(ws);
6881
6956
  const payload = JSON.stringify(msg);
6882
6957
  const run = () => this.write(ws, payload);
6883
- if (!sp) {
6958
+ if (!sp || ((msg.t === "error" || msg.t === "pong") && (sp.outbox?.length ?? 0) >= this.outboxMax)) {
6884
6959
  run();
6885
6960
  return Buffer.byteLength(payload);
6886
6961
  }
@@ -6903,7 +6978,7 @@ pantheon start</code></pre>
6903
6978
  ws.send(payload);
6904
6979
  return true;
6905
6980
  }
6906
- broadcast(sp, msg) {
6981
+ broadcast(sp, msg, key) {
6907
6982
  this.out(sp, () => {
6908
6983
  const m = typeof msg === "function" ? msg() : msg;
6909
6984
  const payload = JSON.stringify(m);
@@ -6919,15 +6994,19 @@ pantheon start</code></pre>
6919
6994
  if (reached && seq !== null && seq > mem.delivered_seq)
6920
6995
  mem.delivered_seq = seq;
6921
6996
  }
6922
- });
6997
+ }, undefined, key);
6923
6998
  }
6924
- out(sp, run, failed) {
6999
+ out(sp, run, failed, key) {
6925
7000
  const w = busyWriter(this.dataDir, safeName(sp.name));
6926
7001
  if (!sp.outbox?.length && !w)
6927
7002
  return run();
6928
7003
  if (failed && w?.failing)
6929
7004
  return this.sendFailed(failed);
6930
- (sp.outbox ??= []).push({ run, w, ticket: w ? w.ticket : 0, ...(failed ? { failed } : {}) });
7005
+ if (key && sp.outbox?.some((h) => h.key === key))
7006
+ return;
7007
+ if (failed?.hangUp && (sp.outbox?.length ?? 0) >= this.outboxMax)
7008
+ return this.sendFailed(failed);
7009
+ (sp.outbox ??= []).push({ run, w, ticket: w ? w.ticket : 0, ...(failed ? { failed } : {}), ...(key ? { key } : {}) });
6931
7010
  if (w)
6932
7011
  void w.whenWritten(w.ticket).then(() => this.release(sp));
6933
7012
  }
package/dist/store.js CHANGED
@@ -4,6 +4,23 @@ import { emptyRefusals } from "./admin-stats.js";
4
4
  import { pendingView, writerFor } from "./disk-writer.js";
5
5
  import { emptyUsage, parseUsage } from "./usage.js";
6
6
  export const USAGE_SAVE_MS = 5_000;
7
+ export const CLOSED_AGE_MS = 30 * 24 * 60 * 60_000;
8
+ export const CLOSED_KEEP = 200;
9
+ const AGED_RECHECK_MS = 10 * 60_000;
10
+ export function closedAt(g) {
11
+ return typeof g.closed_at === "number" && Number.isFinite(g.closed_at) ? g.closed_at : g.opened_at;
12
+ }
13
+ export function agedClosed(goals, now = Date.now(), age = CLOSED_AGE_MS, keep = CLOSED_KEEP) {
14
+ const closed = [...goals].filter((g) => g.state === "CLOSED");
15
+ if (closed.length <= keep)
16
+ return new Set();
17
+ closed.sort((a, b) => closedAt(b) - closedAt(a));
18
+ const out = new Set();
19
+ for (const g of closed.slice(keep))
20
+ if (now - closedAt(g) > age)
21
+ out.add(g.id);
22
+ return out;
23
+ }
7
24
  export const FIRST_CLAIMS_KEEP_MS = 31 * 86_400_000;
8
25
  export const FIRST_CLAIMS_MAX = 5_000;
9
26
  export function ensurePrivateDir(dir) {
@@ -118,12 +135,58 @@ export class StateStore {
118
135
  usageFile;
119
136
  usageTimer = null;
120
137
  usageDirty = false;
138
+ closedFile;
139
+ closedOnDisk = new Map();
140
+ aged = null;
141
+ closedUnreadable = false;
121
142
  constructor(dir, stem) {
122
143
  this.dir = dir;
123
144
  this.stem = stem;
124
145
  this.file = join(dir, `${stem}.state.json`);
125
146
  this.usageFile = join(dir, `${stem}.usage.json`);
147
+ this.closedFile = join(dir, `${stem}.closed-goals.json`);
148
+ }
149
+ agedIds(goals, now = Date.now()) {
150
+ if (!this.aged || this.aged.size !== goals.size || now - this.aged.at > AGED_RECHECK_MS) {
151
+ this.aged = { ids: agedClosed(goals.values(), now), at: now, size: goals.size };
152
+ }
153
+ return this.aged.ids;
154
+ }
155
+ isAged(goals, g) {
156
+ return g.state === "CLOSED" && this.agedIds(goals).has(g.id);
157
+ }
158
+ loadClosed(quarantine) {
159
+ this.closedOnDisk = new Map();
160
+ this.closedUnreadable = false;
161
+ try {
162
+ const pending = pendingView(this.dir, this.stem, this.closedFile);
163
+ if (pending === null || (pending === undefined && !existsSync(this.closedFile)))
164
+ return this.closedOnDisk;
165
+ const raw = JSON.parse(pending ? pending.toString("utf8") : readFileSync(this.closedFile, "utf8"));
166
+ const all = raw && typeof raw === "object" && raw.goals && typeof raw.goals === "object" && !Array.isArray(raw.goals) ? raw.goals : null;
167
+ if (!all)
168
+ throw new Error("not a closed-goals file");
169
+ for (const [id, v] of Object.entries(all)) {
170
+ const g = v;
171
+ if (g && typeof g === "object" && g.id === id && typeof g.title === "string" && typeof g.closed_summary === "string" && Array.isArray(g.members))
172
+ this.closedOnDisk.set(id, { ...g, state: "CLOSED" });
173
+ }
174
+ }
175
+ catch {
176
+ let moved = false;
177
+ if (quarantine) {
178
+ try {
179
+ renameSync(this.closedFile, `${this.closedFile}.corrupt-${Date.now()}`);
180
+ moved = true;
181
+ }
182
+ catch { }
183
+ }
184
+ this.closedUnreadable = !moved;
185
+ this.closedOnDisk = new Map();
186
+ }
187
+ return this.closedOnDisk;
126
188
  }
189
+ closedGoals() { return [...this.closedOnDisk.values()].map((g) => ({ ...g, members: [...g.members] })); }
127
190
  loadUsage(quarantine) {
128
191
  try {
129
192
  const pending = pendingView(this.dir, this.stem, this.usageFile);
@@ -177,6 +240,8 @@ export class StateStore {
177
240
  this.loadUsage(!!opts.quarantine);
178
241
  const pending = pendingView(this.dir, this.stem, this.file);
179
242
  if (pending === null || (pending === undefined && !existsSync(this.file))) {
243
+ this.loadClosed(!!opts.quarantine);
244
+ this.aged = null;
180
245
  return { goals, obligations, profiles, unclean: opts.expectHistory ? "the state snapshot is missing but the ledger has history" : null };
181
246
  }
182
247
  try {
@@ -188,6 +253,13 @@ export class StateStore {
188
253
  }
189
254
  for (const g of snap.goals ?? [])
190
255
  goals.set(g.id, g);
256
+ const kept = this.loadClosed(!!opts.quarantine);
257
+ for (const [id, whole] of kept) {
258
+ const g = goals.get(id);
259
+ if (g && g.closed_summary === undefined && whole.closed_summary !== undefined)
260
+ goals.set(id, { ...g, closed_summary: whole.closed_summary });
261
+ }
262
+ this.aged = null;
191
263
  for (const o of snap.obligations ?? []) {
192
264
  obligations.set(o.id, o.claimed_by ? { ...o, claimed_by: { ...o.claimed_by, at: Date.now() } } : o);
193
265
  }
@@ -228,6 +300,8 @@ export class StateStore {
228
300
  }
229
301
  catch { }
230
302
  }
303
+ this.loadClosed(!!opts.quarantine);
304
+ this.aged = null;
231
305
  return { goals: new Map(), obligations: new Map(), profiles: new Map(), unclean: why, ...(quarantined ? { quarantined } : {}) };
232
306
  }
233
307
  }
@@ -251,10 +325,34 @@ export class StateStore {
251
325
  }
252
326
  onError;
253
327
  flush(goals, obligations, profiles) {
328
+ let list = [...goals.values()];
329
+ if (!this.closedUnreadable) {
330
+ const aged = this.agedIds(goals);
331
+ const next = new Map(this.closedOnDisk);
332
+ let grew = false;
333
+ if (aged.size) {
334
+ list = list.map((g) => {
335
+ if (g.state !== "CLOSED" || !aged.has(g.id) || g.closed_summary === undefined)
336
+ return g;
337
+ const had = next.get(g.id);
338
+ if (!had || had.closed_summary !== g.closed_summary) {
339
+ next.set(g.id, { ...g, members: [...g.members] });
340
+ grew = true;
341
+ }
342
+ const rest = { ...g };
343
+ delete rest.closed_summary;
344
+ return rest;
345
+ });
346
+ }
347
+ if (grew) {
348
+ writerFor(this.dir, this.stem).replace(this.closedFile, JSON.stringify({ v: 1, goals: Object.fromEntries(next) }) + "\n", 0o600, "first");
349
+ this.closedOnDisk = next;
350
+ }
351
+ }
254
352
  const snap = {
255
353
  ...this.extra,
256
354
  v: 2,
257
- goals: [...goals.values()],
355
+ goals: list,
258
356
  obligations: [...obligations.values()],
259
357
  profiles: [...profiles.values()],
260
358
  ...(this.refusals.total ? { refusals: this.refusals } : {}),
@@ -265,7 +363,7 @@ export class StateStore {
265
363
  ...(this.gapsChecked ? { gaps_checked: this.gapsChecked } : {}),
266
364
  saved_at: Date.now(),
267
365
  };
268
- writerFor(this.dir, this.stem).replace(this.file, JSON.stringify(snap, null, 2) + "\n");
366
+ writerFor(this.dir, this.stem).replace(this.file, JSON.stringify(snap) + "\n");
269
367
  }
270
368
  keptFirstClaims(now = Date.now()) {
271
369
  for (const [k, c] of this.firstClaims)
@@ -0,0 +1,130 @@
1
+ import { readFileSync, rmSync } from "node:fs";
2
+ import { posix, win32 } from "node:path";
3
+ import { managedLayout } from "./managed.js";
4
+ import { atomicWrite } from "./worker-policy.js";
5
+ export const PROBATION_FILE = "upgrade-probation.json";
6
+ export const HELD_FILE = "upgrade-held.json";
7
+ export const PROBATION_MS = 2 * 3600_000;
8
+ export const FAILS_TO_ROLL_BACK = 3;
9
+ export const NEVER_ONLINE_MS = 15 * 60_000;
10
+ const isTime = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
11
+ export function runtimeDir(m) {
12
+ return (/\.exe$/i.test(m.node) ? win32 : posix).dirname(m.prevDir);
13
+ }
14
+ const fileIn = (m, name) => (/\.exe$/i.test(m.node) ? win32 : posix).join(runtimeDir(m), name);
15
+ const readJson = (f) => {
16
+ try {
17
+ const v = JSON.parse(readFileSync(f, "utf8"));
18
+ return v && typeof v === "object" ? v : null;
19
+ }
20
+ catch {
21
+ return null;
22
+ }
23
+ };
24
+ export function readProbation(m = managedLayout()) {
25
+ if (!m)
26
+ return null;
27
+ const p = readJson(fileIn(m, PROBATION_FILE));
28
+ if (!p || typeof p.to !== "string" || !isTime(p.at))
29
+ return null;
30
+ return p;
31
+ }
32
+ export function probationText(from, to, now) {
33
+ return JSON.stringify({ v: 1, ...(from ? { from } : {}), to, at: now, runs: [], fails: [] }, null, 2) + "\n";
34
+ }
35
+ export function onProbation(version, o = {}) {
36
+ const p = readProbation(o.m === undefined ? managedLayout() : o.m);
37
+ return !!p && p.to === version && !p.stuck && (o.now ?? Date.now()) - p.at <= PROBATION_MS;
38
+ }
39
+ export function probationPassed(version, o = {}) {
40
+ const m = o.m === undefined ? managedLayout() : o.m;
41
+ const p = readProbation(m);
42
+ if (!m || !p || p.to !== version)
43
+ return false;
44
+ try {
45
+ rmSync(fileIn(m, PROBATION_FILE), { force: true });
46
+ return true;
47
+ }
48
+ catch {
49
+ return false;
50
+ }
51
+ }
52
+ function changeProbation(m, fn) {
53
+ const p = readProbation(m);
54
+ if (!p)
55
+ return false;
56
+ fn(p);
57
+ try {
58
+ atomicWrite(fileIn(m, PROBATION_FILE), JSON.stringify(p, null, 2) + "\n");
59
+ return true;
60
+ }
61
+ catch {
62
+ return false;
63
+ }
64
+ }
65
+ export function markNeverOnline(version, o = {}) {
66
+ const m = o.m === undefined ? managedLayout() : o.m;
67
+ if (!m || readProbation(m)?.to !== version)
68
+ return false;
69
+ return changeProbation(m, (p) => { p.never_online = o.now ?? Date.now(); });
70
+ }
71
+ export function probationStopExpected(o = {}) {
72
+ const m = o.m === undefined ? managedLayout() : o.m;
73
+ if (!m || !readProbation(m))
74
+ return;
75
+ changeProbation(m, (p) => { p.stop_expected_at = o.now ?? Date.now(); });
76
+ }
77
+ export function readRolledBack(m = managedLayout()) {
78
+ if (!m)
79
+ return null;
80
+ const h = readJson(fileIn(m, HELD_FILE));
81
+ if (!h || typeof h.version !== "string" || typeof h.back_to !== "string")
82
+ return null;
83
+ return { version: h.version, back_to: h.back_to, at: isTime(h.at) ? h.at : 0, why: typeof h.why === "string" ? h.why : "", ...(isTime(h.told_at) ? { told_at: h.told_at } : {}) };
84
+ }
85
+ export function markRolledBackTold(o = {}) {
86
+ const m = o.m === undefined ? managedLayout() : o.m;
87
+ const h = m ? readRolledBack(m) : null;
88
+ if (!m || !h)
89
+ return;
90
+ try {
91
+ atomicWrite(fileIn(m, HELD_FILE), JSON.stringify({ v: 1, ...h, told_at: o.now ?? Date.now() }, null, 2) + "\n");
92
+ }
93
+ catch { }
94
+ }
95
+ export function heldBack(pkg, o = {}) {
96
+ if (!pkg)
97
+ return null;
98
+ const h = readRolledBack(o.m === undefined ? managedLayout() : o.m);
99
+ return h && h.version === pkg ? h : null;
100
+ }
101
+ export function rolledBackSentence(h) {
102
+ const what = h.why === "never_online" ? "could not reach your team" : "kept stopping";
103
+ return `Pantheon ${h.version} ${what} on this computer, so it went back to ${h.back_to} by itself. Nothing to do: it updates again when a newer release is out.`;
104
+ }
105
+ export function watchNeverOnline(o) {
106
+ if (!onProbation(o.version, o.m === undefined ? {} : { m: o.m }))
107
+ return null;
108
+ const t = setTimeout(() => {
109
+ void (async () => {
110
+ if (o.connected() || !onProbation(o.version, o.m === undefined ? {} : { m: o.m }))
111
+ return;
112
+ let up = false;
113
+ try {
114
+ up = await o.relayAnswers();
115
+ }
116
+ catch {
117
+ up = false;
118
+ }
119
+ if (!up || o.connected())
120
+ return;
121
+ if (!markNeverOnline(o.version, o.m === undefined ? {} : { m: o.m }))
122
+ return;
123
+ o.log?.(`! pantheon ${o.version} has not reached the team's relay in ${Math.round((o.afterMs ?? NEVER_ONLINE_MS) / 60_000)} minutes though the relay answers — going back to the previous version`);
124
+ o.exit();
125
+ })();
126
+ }, o.afterMs ?? NEVER_ONLINE_MS);
127
+ t.unref?.();
128
+ return t;
129
+ }
130
+ //# sourceMappingURL=upgrade-probation.js.map
package/dist/upgrade.js CHANGED
@@ -8,6 +8,7 @@ import { proxyEnvFor } from "./network.js";
8
8
  import { relayProc, workerProc, notifierProc, menubarProc } from "./machine.js";
9
9
  import { load } from "./config.js";
10
10
  import { compareVersions, INSTALLER_MIN_CLI, isVersion } from "./installer.js";
11
+ import { HELD_FILE, PROBATION_FILE, probationText } from "./upgrade-probation.js";
11
12
  export const WINDOWS_PROFILE_ROOT_MIN = "0.33.1-0";
12
13
  import { childNodeFlags, installerLineHere, launcherWithRepair, managedLayout, nodeSatisfies, nodeTooOldSentence, runtimeNodeVersion } from "./managed.js";
13
14
  export function repoRoot() {
@@ -400,6 +401,11 @@ export function stagedManagedUpgrade(m, spec, io = realSwapIo(m)) {
400
401
  const staged = startsAs(P.join(stagedPkg, "bin", "pantheon.js"));
401
402
  if (staged !== version)
402
403
  throw new Error(`the new pantheon ${version} did not start (it answered ${staged ?? "nothing"}); the working version was kept`);
404
+ let from = null;
405
+ try {
406
+ from = JSON.parse(io.read(P.join(m.pkgDir, "package.json"))).version ?? null;
407
+ }
408
+ catch { }
403
409
  if (!rename(stagedPkg, ready))
404
410
  throw new Error("the new pantheon's files are still held by another program (a virus scan); nothing was changed, and the next check tries again");
405
411
  if (!inPlace())
@@ -431,6 +437,13 @@ export function stagedManagedUpgrade(m, spec, io = realSwapIo(m)) {
431
437
  ? `pantheon ${version} did not start once in place (it answered ${placed ?? "nothing"}); the previous version was put back`
432
438
  : `pantheon ${version} did not start once in place, and the previous version could not be put back yet. ${inPlace() ? `It is kept at ${prev}; the next upgrade puts it back, or run the installer again` : repairHint()}`);
433
439
  }
440
+ if (from && from !== version && whole(prev)) {
441
+ try {
442
+ io.write(P.join(runtime, PROBATION_FILE), probationText(from, version, Date.now()));
443
+ }
444
+ catch { }
445
+ }
446
+ tidy(P.join(runtime, HELD_FILE));
434
447
  return { version };
435
448
  }
436
449
  finally {
@@ -5,6 +5,7 @@ import { atomicWrite, isNewer, ASK_EXPIRY_MS } from "./worker-policy.js";
5
5
  import { installedVersion } from "./mcp-supervisor.js";
6
6
  import { VERSION } from "./version.js";
7
7
  import { BUDGET } from "./notice.js";
8
+ import { readRolledBack } from "./upgrade-probation.js";
8
9
  export const BEHIND_ALERT_AFTER_MS = 24 * 3_600_000;
9
10
  export const GRANTED_STUCK_AFTER_MS = 72 * 3_600_000;
10
11
  export const RELAY_PKG_REFRESH_MS = 60 * 60_000;
@@ -155,6 +156,8 @@ export function behindVerdict(seen, o) {
155
156
  return { due: false, why: "current" };
156
157
  if (o.state.behind_alerted?.[seen.pkg])
157
158
  return { due: false, why: "said" };
159
+ if (o.rolledBack && o.rolledBack === seen.pkg)
160
+ return { due: false, why: "worker_said" };
158
161
  if (o.state.upgrade_failed?.[seen.pkg]?.alerted_at)
159
162
  return { due: false, why: "worker_said" };
160
163
  if (o.policy?.upgradeHeld?.version === seen.pkg)
@@ -209,11 +212,12 @@ export class VersionWatch {
209
212
  const quiet = [];
210
213
  let running;
211
214
  const workerRunning = () => (running ??= safe(() => false, () => this.o.workerRunning()));
215
+ const rolledBack = safe(() => null, () => (this.o.rolledBack ?? (() => readRolledBack()?.version ?? null))());
212
216
  for (const r of relays) {
213
217
  const seen = state.relays[r];
214
218
  if (!seen)
215
219
  continue;
216
- const v = behindVerdict(seen, { local, now: now(), state, policy, workerRunning });
220
+ const v = behindVerdict(seen, { local, now: now(), state, policy, workerRunning, rolledBack });
217
221
  if (v.due)
218
222
  due.push(seen.pkg);
219
223
  else if (v.why === "worker_said")
@@ -267,6 +271,9 @@ export function behindStatusLine(o) {
267
271
  if (!seen || !isNewer(seen.pkg, o.local))
268
272
  return null;
269
273
  const pkg = seen.pkg;
274
+ if (o.rolledBack && o.rolledBack === pkg) {
275
+ return `pantheon ${o.local} here, the team's relay ${pkg}: ${pkg} did not work on this computer, so Pantheon went back to ${o.local} by itself and updates again when a newer release is out — nothing to do unless your human asks.`;
276
+ }
270
277
  const held = o.policy?.upgradeHeld?.version === pkg ? o.policy.upgradeHeld : null;
271
278
  if (held) {
272
279
  return `PANTHEON IS OUT OF DATE HERE: this computer runs ${o.local}, the team's relay ${pkg}, and the update needs something only your human can do: ${held.why} ` +
package/dist/win-host.js CHANGED
@@ -530,6 +530,7 @@ export function realHostIo(o = {}) {
530
530
  const node = o.node ?? process.execPath;
531
531
  const flags = o.flags ?? [];
532
532
  const cli = o.cli ?? resolve(HERE, "cli.js");
533
+ const childEntry = o.cli ? cli : (() => { const b = resolve(HERE, "..", "bin", "pantheon.js"); return existsSync(b) ? b : cli; })();
533
534
  const pantheon = o.pantheonCommand ?? `"${node}" ${flags.join(" ")} "${cli}"`.replace(/\s+/g, " ");
534
535
  const session = o.session ?? hostSession();
535
536
  const parent = process.ppid;
@@ -617,7 +618,7 @@ export function realHostIo(o = {}) {
617
618
  const d = readDefinition(child, home);
618
619
  if (!d)
619
620
  throw new Error(`no ${child} definition`);
620
- argv = childArgv(child, d.args, node, flags, cli);
621
+ argv = childArgv(child, d.args, node, flags, childEntry);
621
622
  if (existsSync(d.cwd))
622
623
  cwd = d.cwd;
623
624
  }
package/dist/worker.js CHANGED
@@ -21,6 +21,7 @@ import { activeOutages, detectOutage, earliest, isSignOut, outagePredicate, when
21
21
  import { agentSpawn, CmdLineUnsafe } from "./which.js";
22
22
  import { VERSION } from "./version.js";
23
23
  import { composeUpgradeFailed, fetchRelayPkg, recordRelayPkg, upgradeFailedOnce } from "./version-health.js";
24
+ import { heldBack, markRolledBackTold, rolledBackSentence } from "./upgrade-probation.js";
24
25
  import { noteServiceConnected } from "./crash-loop.js";
25
26
  import { applyAnswer, ASK_EXPIRY_MS, policyUnreadable, CAPABILITY_ARGS, CARRY_REASON, carryOverWakers, dropLedgerGrants, hasWakeOnce, isApprovedWaker, mayAskWake, takeWakeOnce, OLD_RELAY_WAKE, relayKnowsAdmitters, loadPolicy, mayAsk, needsSelfUpgrade, parseGrantAnswer, recordAsk, savePolicy, useGrant, WOKEN_RUN_ENV, takeTeamFlagOnYes, unsandboxedAllowed, policyPath, isNewer, RUN_MINUTES, effectiveRunMinutes, effectiveIdleSeconds, readinessRecheckPath, policyHome, describeRunMinutes, parseRunMinutes, currentMode, wakeSettled, effectiveRunModel, describeRunModel, effectiveGoalRuns, claudeRouting, } from "./worker-policy.js";
26
27
  import { readinessAlerts } from "./readiness-alert.js";
@@ -1089,6 +1090,7 @@ export function composePickups(items, space) {
1089
1090
  export const UNSANDBOXED_RUN = `\n\nTHIS RUN HAS NO SANDBOX. Never run code a teammate wrote: their branch, a patch or a script they sent, or a\ntest they changed. Read and review it, then ESCALATE to your human to run it. The repo's own commands on your\nown changes are fine.`;
1090
1091
  export const TASK_POINTER = "Your task for this run is in the file .pantheon-task.md in the current directory. Read that file first and follow it: it is the whole brief.";
1091
1092
  let fleetLock = null;
1093
+ let rolledBackSaid = null;
1092
1094
  export const RUN_TIMEOUT_MS = RUN_MINUTES.default * 60_000;
1093
1095
  export const RUN_TIMEOUT_MAX_MS = RUN_MINUTES.max * 60_000;
1094
1096
  function recordLimitMs(r) {
@@ -6074,6 +6076,32 @@ export class Worker {
6074
6076
  const lastAttempt = policy.attempts.self_upgrade ?? null;
6075
6077
  if (!needsSelfUpgrade(VERSION, relayPkg, lastAttempt))
6076
6078
  return;
6079
+ const back = this.opts.selfUpgrade?.rolledBack ? this.opts.selfUpgrade.rolledBack.read(relayPkg) : heldBack(relayPkg);
6080
+ if (back) {
6081
+ const key = `${back.version}@${back.at}`;
6082
+ if (!back.told_at && rolledBackSaid !== key) {
6083
+ rolledBackSaid = key;
6084
+ const line = rolledBackSentence(back);
6085
+ this.opts.log(`! ${line}`);
6086
+ const canAlert = !!this.opts.notify && decisionAlertsOn() && (() => { try {
6087
+ return !this.opts.canAlert || this.opts.canAlert();
6088
+ }
6089
+ catch {
6090
+ return false;
6091
+ } })();
6092
+ if (canAlert) {
6093
+ try {
6094
+ this.opts.notify?.({ kind: "worker", who: "Pantheon · this computer", heading: `Pantheon went back to ${back.back_to}`, context: this.cfg.space, body: line, ttl: 0, sound: false });
6095
+ }
6096
+ catch { }
6097
+ }
6098
+ if (this.opts.selfUpgrade?.rolledBack)
6099
+ this.opts.selfUpgrade.rolledBack.told();
6100
+ else
6101
+ markRolledBackTold();
6102
+ }
6103
+ return;
6104
+ }
6077
6105
  const nodeNow = this.opts.selfUpgrade?.node ?? (() => runtimeNodeVersion());
6078
6106
  const held = policy.upgradeHeld;
6079
6107
  if (held && held.version === relayPkg && held.node === nodeNow())
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "format": 1,
3
- "built_at": "2026-11-13T10:46:28.280Z",
3
+ "built_at": "2026-11-15T08:35:44.857Z",
4
4
  "swiftc": "Apple Swift version 6.4.2 (swiftlang-6.4.2.1.9 clang-2100.4.1.1)",
5
5
  "helpers": {
6
6
  "pantheon-tray": {
@@ -17,7 +17,7 @@
17
17
  "pantheon-hud": {
18
18
  "source": "main.swift",
19
19
  "source_sha256": "4976c00b5d4bd0d8c509d2d284d9c6e2d70a1b3db952de5f11108dfff4951a82",
20
- "sha256": "6b81963a5d075d034224d5e9181038194e362af14d281ad98d74c2bb9fe1dd6f",
20
+ "sha256": "7ea1f02fcab4d53b1fd081ce7bef33c73bd092fcf310bef1619a10ac8611c808",
21
21
  "archs": [
22
22
  "arm64",
23
23
  "x86_64"
Binary file
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@join-pantheon/cli",
3
- "version": "0.38.1",
3
+ "version": "0.38.2",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@join-pantheon/cli",
9
- "version": "0.38.1",
9
+ "version": "0.38.2",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@modelcontextprotocol/sdk": "^1.32.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@join-pantheon/cli",
3
- "version": "0.38.1",
3
+ "version": "0.38.2",
4
4
  "description": "Let your coding agent collaborate directly with your teammates' agents — ask, agree an interface, and deliver, without a human relaying messages.",
5
5
  "type": "module",
6
6
  "bin": {