onionskin-cli 0.0.5 → 0.0.6

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 (48) hide show
  1. package/CHANGELOG.md +98 -0
  2. package/README.md +4 -4
  3. package/package.json +1 -1
  4. package/scripts/checker/check.py +24 -5
  5. package/scripts/checker/requirements.txt +482 -5
  6. package/scripts/requirements.txt +482 -5
  7. package/scripts/rewriter/apple/main.swift +1 -1
  8. package/scripts/rewriter/apple/onionskin-fm +0 -0
  9. package/scripts/rewriter/apple/onionskin-fm.json +1 -1
  10. package/skills/onionskin/SKILL.md +4 -4
  11. package/skills/onionskin/reference/ops.md +1 -1
  12. package/src/agent-runner.js +94 -15
  13. package/src/checker.js +238 -33
  14. package/src/cli.js +173 -33
  15. package/src/doctor.js +12 -3
  16. package/src/docx.js +130 -63
  17. package/src/fsutil.js +13 -5
  18. package/src/history.js +11 -2
  19. package/src/html-render.js +74 -15
  20. package/src/html-utf8.js +1 -1
  21. package/src/html.js +3 -1
  22. package/src/merge-package.js +82 -26
  23. package/src/offload.js +25 -6
  24. package/src/ops/docx-table.js +5 -3
  25. package/src/ops/pptx.js +8 -2
  26. package/src/ops/xlsx.js +114 -8
  27. package/src/package.js +17 -2
  28. package/src/pptx.js +244 -8
  29. package/src/question.js +10 -3
  30. package/src/quick.js +1 -1
  31. package/src/redline-check.js +12 -2
  32. package/src/rewriter.js +38 -8
  33. package/src/server-entry.js +4 -3
  34. package/src/server.js +97 -28
  35. package/src/session.js +13 -2
  36. package/src/setup.js +20 -2
  37. package/src/update.js +248 -19
  38. package/src/voice-guide.js +12 -7
  39. package/src/voice-learn.js +11 -1
  40. package/src/voice-store.js +11 -4
  41. package/src/voice.js +2 -1
  42. package/src/xlsx.js +25 -5
  43. package/src/zip-write.js +8 -4
  44. package/src/zip.js +64 -13
  45. package/web/dist/assets/{index-DytKkMdi.js → index-CjsTf5gj.js} +108 -106
  46. package/web/dist/assets/index-DnbLtHhG.css +1 -0
  47. package/web/dist/index.html +2 -2
  48. package/web/dist/assets/index-DkNx0LSH.css +0 -1
package/src/rewriter.js CHANGED
@@ -71,18 +71,27 @@ export const APPLE_STATES = {
71
71
  /** The probe's words (scripts/rewriter/apple/main.swift --probe) to the state codes above. */
72
72
  export const PROBE_REASONS = { deviceNotEligible: "device_not_eligible", appleIntelligenceNotEnabled: "apple_intelligence_not_enabled", modelNotReady: "model_not_ready" };
73
73
 
74
+ const shaCache = new Map(); // `${bin}\0${mtimeMs}\0${size}\0${ino}` -> sha256
75
+ const quarantineCache = new Map(); // `${bin}\0${mtimeMs}\0${ctimeMs}\0${size}\0${ino}` -> boolean
76
+ const cacheSet = (m, k, v) => {
77
+ if (m.size >= 32) m.clear();
78
+ m.set(k, v);
79
+ return v;
80
+ };
81
+
74
82
  /**
75
83
  * Does the file carry the com.apple.quarantine mark? Asked of /usr/bin/xattr, never by running the file. true | false; anything but a clean
76
84
  * "No such xattr" answer counts as marked (fail closed).
77
85
  */
78
86
  export function isQuarantined(file) {
79
- const r = spawnSync(XATTR, ["-p", QUARANTINE, file], { encoding: "utf8", timeout: 5000 });
87
+ const r = spawnSync(XATTR, ["-p", QUARANTINE, file], { encoding: "utf8", timeout: 5000, env: { ...process.env, LC_ALL: "C" } });
80
88
  if (r.status === 0) return true;
81
89
  return !(r.status === 1 && /No such xattr/.test(r.stderr || ""));
82
90
  }
83
91
  /** Removes the mark from exactly this file. true when the file carries no mark afterwards. */
84
92
  export function clearQuarantine(file) {
85
- spawnSync(XATTR, ["-d", QUARANTINE, file], { encoding: "utf8", timeout: 5000 });
93
+ spawnSync(XATTR, ["-d", QUARANTINE, file], { encoding: "utf8", timeout: 5000, env: { ...process.env, LC_ALL: "C" } });
94
+ for (const k of quarantineCache.keys()) if (k.startsWith(`${file}\0`)) quarantineCache.delete(k);
86
95
  return !isQuarantined(file);
87
96
  }
88
97
 
@@ -112,20 +121,25 @@ export function hostFacts() {
112
121
  export function appleHelperState(o = {}) {
113
122
  const dir = o.dir ?? (env("APPLE_HELPER_DIR", "") || APPLE_HELPER_DIR); // the folder variable is for the browser tests, with a stand-in helper
114
123
  const host = o.host ?? hostFacts();
115
- const marked = o.quarantined ?? isQuarantined;
116
124
  const major = Number(String(host.macos ?? "").split(".")[0]);
117
125
  if (!Number.isFinite(major) || major < 26) return { ok: false, reason: "macos_too_old" };
118
126
  if (!host.appleSilicon) return { ok: false, reason: "device_not_eligible" };
119
127
  const bin = path.join(dir, "onionskin-fm");
120
128
  let rec;
129
+ let st;
121
130
  try {
122
131
  rec = JSON.parse(fs.readFileSync(path.join(dir, "onionskin-fm.json"), "utf8"));
123
- if (!fs.lstatSync(bin).isFile()) return { ok: false, reason: "helper_missing" };
132
+ st = fs.lstatSync(bin);
133
+ if (!st.isFile()) return { ok: false, reason: "helper_missing" };
124
134
  } catch {
125
135
  return { ok: false, reason: "helper_missing" };
126
136
  }
127
- if (typeof rec?.sha256 !== "string" || fileSha256(bin) !== rec.sha256) return { ok: false, reason: "helper_changed" };
128
- if (marked(bin)) return { ok: false, reason: "quarantined", bin };
137
+ const shaKey = `${bin}\0${st.mtimeMs}\0${st.size}\0${st.ino}`;
138
+ const sha = shaCache.get(shaKey) ?? cacheSet(shaCache, shaKey, fileSha256(bin));
139
+ if (typeof rec?.sha256 !== "string" || sha !== rec.sha256) return { ok: false, reason: "helper_changed" };
140
+ const qKey = `${bin}\0${st.mtimeMs}\0${st.ctimeMs}\0${st.size}\0${st.ino}`;
141
+ const isMarked = o.quarantined ? o.quarantined(bin) : (quarantineCache.get(qKey) ?? cacheSet(quarantineCache, qKey, isQuarantined(bin)));
142
+ if (isMarked) return { ok: false, reason: "quarantined", bin };
129
143
  return { ok: true, bin, sha256: rec.sha256 };
130
144
  }
131
145
  /** The person's words for a reason code, or null. helper_changed reads as helper_missing. */
@@ -175,7 +189,7 @@ export function workerEnv() {
175
189
  * Options: idleMs (default 15 minutes), startMs, stuckMs, retryMs.
176
190
  */
177
191
  export function createWorker(launch, opts = {}) {
178
- const o = { idleMs: idleMs(), startMs: START_LIMIT_MS, stuckMs: STUCK_MS, retryMs: RETRY_AFTER_FAILURE_MS, ...opts };
192
+ const o = { idleMs: idleMs(), startMs: START_LIMIT_MS, stuckMs: STUCK_MS, retryMs: RETRY_AFTER_FAILURE_MS, killGraceMs: 500, ...opts };
179
193
  let child = null;
180
194
  let ready = false;
181
195
  let failedAt = 0;
@@ -197,7 +211,23 @@ export function createWorker(launch, opts = {}) {
197
211
  try {
198
212
  c.stdin.end();
199
213
  } catch {}
200
- c.kill("SIGTERM");
214
+ if (c.exitCode === null && c.signalCode === null) {
215
+ try {
216
+ c.kill("SIGTERM");
217
+ } catch {}
218
+ const forceKill = () => {
219
+ try {
220
+ if (c.exitCode === null && c.signalCode === null) c.kill("SIGKILL");
221
+ } catch {}
222
+ };
223
+ const killTimer = setTimeout(forceKill, o.killGraceMs);
224
+ killTimer.unref?.();
225
+ process.once("exit", forceKill);
226
+ c.once("exit", () => {
227
+ clearTimeout(killTimer);
228
+ process.off("exit", forceKill);
229
+ });
230
+ }
201
231
  }
202
232
  if (busy) {
203
233
  busy.settle({ ok: false, code: "worker_exited" });
@@ -1,13 +1,14 @@
1
1
  import { startServer } from "./server.js";
2
2
 
3
+ let srv;
3
4
  try {
4
- await startServer();
5
+ srv = await startServer();
5
6
  process.stdout.write("ready\n"); // the CLI waits for this line instead of polling
6
7
  } catch (err) {
7
8
  if (err?.code === "EALREADY") process.exit(0);
8
9
  console.error(err?.message || err);
9
10
  process.exit(1);
10
11
  }
11
- process.on("SIGTERM", () => process.exit(0));
12
- process.on("SIGINT", () => process.exit(0));
12
+ process.on("SIGTERM", () => srv.shutdown());
13
+ process.on("SIGINT", () => srv.shutdown());
13
14
  process.stdout.on("error", () => {});
package/src/server.js CHANGED
@@ -28,6 +28,7 @@ import { BG_PRESETS, MAX_IMAGE_BYTES, SettingsError, readCustomImage, readSettin
28
28
  const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
29
29
  const WEB_DIST = env("WEB_DIST", path.join(root, "web", "dist"));
30
30
  const MAX_BODY = 300 * 1024 * 1024; // base64 of a 200 MB document; ceiling: whole body buffered in memory. Upgrade path: binary upload route.
31
+ const META_ROUTES = new Set(["/api/claim", "/api/answer", "/api/review", "/api/suggestion", "/api/takeback", "/api/revert", "/api/show-copy", "/api/dismiss-conflict", "/api/close"]);
31
32
  const MIME = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".json": "application/json", ".svg": "image/svg+xml", ".png": "image/png", ".woff2": "font/woff2", ".ico": "image/x-icon", ".map": "application/json" };
32
33
 
33
34
  function secure(res) {
@@ -110,20 +111,44 @@ function serveIndex(res, launchToken) {
110
111
  res.end(raw);
111
112
  }
112
113
 
114
+ function decodeUriPart(s) {
115
+ try {
116
+ return decodeURIComponent(s);
117
+ } catch {
118
+ throw new HttpError(400, "invalid URL encoding");
119
+ }
120
+ }
121
+
113
122
  function serveStatic(res, urlPath) {
114
- const rel = decodeURIComponent(urlPath).replace(/^\/+/, "");
115
- const file = path.resolve(WEB_DIST, rel);
116
- if (file !== WEB_DIST && !file.startsWith(WEB_DIST + path.sep)) return sendText(res, 403, "forbidden");
123
+ const rel = decodeUriPart(urlPath).replace(/^\/+/, "");
124
+ const dist = path.resolve(WEB_DIST);
125
+ const file = path.resolve(dist, rel);
126
+ if (file !== dist && !file.startsWith(dist + path.sep)) return sendText(res, 403, "forbidden");
127
+ let realDist;
128
+ let realFile;
117
129
  let st;
118
130
  try {
119
- st = fs.statSync(file);
131
+ realDist = fs.realpathSync(dist);
132
+ realFile = fs.realpathSync(file);
133
+ } catch {
134
+ return sendText(res, 404, "not found");
135
+ }
136
+ if (realFile !== realDist && !realFile.startsWith(realDist + path.sep)) return sendText(res, 403, "forbidden");
137
+ try {
138
+ st = fs.statSync(realFile);
120
139
  } catch {
121
140
  return sendText(res, 404, "not found");
122
141
  }
123
142
  if (!st.isFile()) return sendText(res, 404, "not found");
124
143
  secure(res);
125
144
  res.writeHead(200, { "Content-Type": MIME[path.extname(file)] || "application/octet-stream", "Content-Length": st.size });
126
- fs.createReadStream(file).pipe(res);
145
+ const stream = fs.createReadStream(realFile);
146
+ stream.on("error", () => {
147
+ if (!res.headersSent) sendText(res, 404, "not found");
148
+ else res.destroy();
149
+ });
150
+ res.on("close", () => stream.destroy());
151
+ stream.pipe(res);
127
152
  }
128
153
 
129
154
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
@@ -143,14 +168,22 @@ async function withStartLock(fn) {
143
168
  break;
144
169
  } catch (err) {
145
170
  if (err.code !== "EEXIST") throw err;
146
- let alive = false;
171
+ let stale = false;
147
172
  try {
148
- process.kill(Number(fs.readFileSync(lock, "utf8")), 0);
149
- alive = true;
150
- } catch (e) {
151
- alive = e.code === "EPERM";
173
+ const pid = Number(fs.readFileSync(lock, "utf8"));
174
+ if (Number.isInteger(pid) && pid > 0) {
175
+ try {
176
+ process.kill(pid, 0);
177
+ } catch (e) {
178
+ stale = e.code !== "EPERM";
179
+ }
180
+ } else {
181
+ stale = Date.now() - fs.statSync(lock).mtimeMs > 2000;
182
+ }
183
+ } catch {
184
+ continue;
152
185
  }
153
- if (!alive) {
186
+ if (stale) {
154
187
  try {
155
188
  fs.unlinkSync(lock);
156
189
  } catch {}
@@ -313,19 +346,27 @@ export async function startServer({ port = 0 } = {}) {
313
346
  }
314
347
  if (req.method === "GET" && p === "/api/stale") return sendJson(res, 200, sessions.staleCheck(key, url.searchParams.get("sha") || ""));
315
348
  if (req.method === "GET" && p === "/api/history") return sendJson(res, 200, await sessions.history(key));
316
- if (req.method === "GET" && p.startsWith("/api/history/")) return sendJson(res, 200, await sessions.historyUpdate(key, decodeURIComponent(p.slice("/api/history/".length)).slice(0, 64)));
349
+ if (req.method === "GET" && p.startsWith("/api/history/")) return sendJson(res, 200, await sessions.historyUpdate(key, decodeUriPart(p.slice("/api/history/".length)).slice(0, 64)));
317
350
  if (req.method !== "POST") return sendJson(res, 405, { error: "method not allowed" });
318
351
  // the preview of a quick change (src/quick-plan.js): read only, so no editor claim; it answers for the file as it is on disk now
319
352
  if (p === "/api/quick") return sendJson(res, 200, await quickPreview(s, String((await readJson(req, 8 * 1024))?.note ?? "")));
320
353
  // "Build my voice guide" (src/voice-learn.js): the editing tab only, with its own body cap (five 20 MB files in base64 plus 1 MB)
321
354
  if (p === "/api/voice/build") {
322
355
  sessions.requireEditor(key, tab);
323
- return sendJson(res, 200, { ok: true, session: (await buildVoiceRequest(sessions, key, await readJson(req, VOICE_BUILD_MAX_BODY)), sessions.view(s)) });
356
+ const body = await readJson(req, VOICE_BUILD_MAX_BODY);
357
+ await buildVoiceRequest(sessions, key, body);
358
+ return sendJson(res, 200, { ok: true, session: sessions.view(s) });
324
359
  }
325
360
  // starts the writing helper of quick fixes if it is cold (src/suggest.js); carries no text, changes nothing
326
361
  if (p === "/api/suggest/warm") return sendJson(res, 200, (await readJson(req, 1024), warmSuggest()));
327
362
  // the export and the agent's buttons take ids only; a draft carries no document, so it is held to the cap of a request's own data
328
- const body = await readJson(req, p === "/api/export-tracked" || p === "/api/agent" ? 1024 : p === "/api/suggest" ? 8 * 1024 : p === "/api/draft" || p === "/api/handover" ? REQUEST_MAX : MAX_BODY, p === "/api/draft" || p === "/api/handover" ? tooLarge("This draft") : null);
363
+ const bodyMax =
364
+ p === "/api/export-tracked" || p === "/api/agent" ? 1024 :
365
+ p === "/api/suggest" ? 8 * 1024 :
366
+ META_ROUTES.has(p) ? 16 * 1024 :
367
+ p === "/api/draft" || p === "/api/handover" ? REQUEST_MAX :
368
+ MAX_BODY;
369
+ const body = await readJson(req, bodyMax, p === "/api/draft" || p === "/api/handover" ? tooLarge("This draft") : null);
329
370
  // a Send or a quick fix's Use carries the document too: their comments, note, typed edits and view state are held to the same cap
330
371
  if ((p === "/api/send" || p === "/api/suggest/use") && requestSize([body.comments, body.note, body.edits, body.viewState, body.htmlEdits]) > REQUEST_MAX) throw tooLarge(p === "/api/send" ? "This Send" : "This request");
331
372
  if (p === "/api/claim" && !/^[\w-]{6,64}$/.test(tab)) return sendJson(res, 400, { error: "a tab id is required" });
@@ -349,8 +390,8 @@ export async function startServer({ port = 0 } = {}) {
349
390
  // a tap on a question card: an index (or null for "Something else"), never option text; checked against what the server stored
350
391
  if (p === "/api/answer") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(key, () => sessions.answer(key, { id: String(body.id || ""), choice: body.choice })), sessions.view(s)) });
351
392
  // review first (src/review.js): a button's kind, then taps on its cards (ids and an integer, never text)
352
- if (p === "/api/review") return sendJson(res, 200, { ok: true, session: (sessions.requestReview(key, body.kind), sessions.view(s)) });
353
- if (p === "/api/suggestion") return sendJson(res, 200, { ok: true, session: (sessions.answerSuggestions(key, { ids: body.ids, choice: body.choice }), sessions.view(s)) });
393
+ if (p === "/api/review") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(s.key, () => sessions.requestReview(key, body.kind)), sessions.view(s)) });
394
+ if (p === "/api/suggestion") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(s.key, () => sessions.answerSuggestions(key, { ids: body.ids, choice: body.choice })), sessions.view(s)) });
354
395
  // quick fixes (src/suggest.js): asking writes nothing; Use names the stored suggestion by id and writes it on this Mac
355
396
  if (p === "/api/suggest") return sendJson(res, 200, await suggest(sessions, key, body));
356
397
  if (p === "/api/suggest/use") return sendJson(res, 200, { ok: true, session: (await useSuggestion(sessions, key, { ...body, tab }), sessions.view(s)) });
@@ -423,27 +464,50 @@ export async function startServer({ port = 0 } = {}) {
423
464
  return sendJson(res, 200, await status());
424
465
  }
425
466
 
467
+ function dropSse(key, res) {
468
+ const set = sse.get(key);
469
+ if (!set) return;
470
+ if (set.delete(res)) tabSeen.set(key, Date.now());
471
+ if (!set.size) sse.delete(key);
472
+ }
473
+ function writeSse(key, res, chunk) {
474
+ try {
475
+ res.write(chunk);
476
+ return true;
477
+ } catch {
478
+ dropSse(key, res);
479
+ try { res.destroy(); } catch {}
480
+ return false;
481
+ }
482
+ }
426
483
  function events(req, res, key, tab) {
427
484
  const s = sessions.get(key);
428
- if (/^[\w-]{6,64}$/.test(tab)) {
429
- sessions.claimEditor(key, tab);
430
- req.on("close", () => sessions.releaseEditor(key, tab));
431
- }
485
+ const hasTab = /^[\w-]{6,64}$/.test(tab);
486
+ if (hasTab) sessions.claimEditor(key, tab);
487
+ let cleaned = false;
488
+ const cleanup = () => {
489
+ if (cleaned) return;
490
+ cleaned = true;
491
+ if (hasTab) sessions.releaseEditor(key, tab);
492
+ dropSse(key, res);
493
+ };
494
+ req.on("close", cleanup);
495
+ res.on("error", cleanup);
496
+ res.on("close", cleanup);
432
497
  secure(res);
433
498
  res.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-store", Connection: "keep-alive" });
434
- res.write(`event: state\ndata: ${JSON.stringify(sessions.view(s))}\n\n`);
435
499
  if (!sse.has(key)) sse.set(key, new Set());
436
500
  sse.get(key).add(res);
437
501
  tabSeen.set(key, Date.now());
438
- req.on("close", () => (sse.get(key)?.delete(res), tabSeen.set(key, Date.now())));
502
+ writeSse(key, res, `event: state\ndata: ${JSON.stringify(sessions.view(s))}\n\n`);
439
503
  }
440
504
  sessions.on("change", (s, event) => {
441
505
  const set = sse.get(s.key);
442
506
  if (!set?.size) return;
443
507
  const view = JSON.stringify(sessions.view(s));
444
- for (const res of set) {
445
- if (event === "reload") res.write(`event: reload\ndata: ${view}\n\n`);
446
- res.write(`event: state\ndata: ${view}\n\n`);
508
+ for (const res of [...set]) {
509
+ if (event === "reload" && !writeSse(s.key, res, `event: reload\ndata: ${view}\n\n`)) continue;
510
+ writeSse(s.key, res, `event: state\ndata: ${view}\n\n`);
447
511
  }
448
512
  });
449
513
 
@@ -504,6 +568,8 @@ export async function startServer({ port = 0 } = {}) {
504
568
  if (req.method === "GET" && p === "/cli/poll") {
505
569
  const f = url.searchParams.get("file") || "";
506
570
  checkBinding(f, url.searchParams.get("holder")); // before the file is looked up or opened
571
+ let handle;
572
+ res.once("close", () => handle?.cancel());
507
573
  let s;
508
574
  try {
509
575
  s = sessions.byFile(f);
@@ -511,8 +577,8 @@ export async function startServer({ port = 0 } = {}) {
511
577
  s = await sessions.openAsync(f); // a poller may start before (or after a server restart without) `open`
512
578
  }
513
579
  await sessions.outlineAhead(s.key, url.searchParams.get("holder") || "agent"); // the outline a delivery carries, built off this thread
514
- const handle = sessions.poll(s.key, { ack: url.searchParams.get("ack") === "1", holder: url.searchParams.get("holder") || "agent", full: url.searchParams.get("full") === "1", compact: url.searchParams.get("compact") === "1" });
515
- res.on("close", () => handle.cancel());
580
+ if (res.destroyed) return;
581
+ handle = sessions.poll(s.key, { ack: url.searchParams.get("ack") === "1", holder: url.searchParams.get("holder") || "agent", full: url.searchParams.get("full") === "1", compact: url.searchParams.get("compact") === "1" });
516
582
  const out = await handle.promise;
517
583
  if (res.destroyed) return;
518
584
  return sendJson(res, 200, out);
@@ -589,13 +655,16 @@ export async function startServer({ port = 0 } = {}) {
589
655
  const timer = setInterval(() => {
590
656
  sessions.tick();
591
657
  agent.tick();
592
- for (const set of sse.values()) for (const r of set) r.write(": ping\n\n");
658
+ for (const [k, set] of [...sse]) for (const r of [...set]) writeSse(k, r, ": ping\n\n");
593
659
  // the background agent's own polls do not keep the server up (it stops after 20 minutes without a tab; then the server may stop too)
594
660
  const busy = [...sessions.map.values()].some((s) => s.pollers.some((p) => !isBgHolder(p.holder)) || s.delivered);
595
661
  if (!busy && ![...sse.values()].some((x) => x.size) && Date.now() - lastActivity > idleMs) shutdown();
596
662
  }, Number(env("TICK_MS", 1000)));
597
663
 
664
+ let shuttingDown = false;
598
665
  function shutdown() {
666
+ if (shuttingDown) return;
667
+ shuttingDown = true;
599
668
  clearInterval(timer);
600
669
  watcher.close();
601
670
  for (const k of [...late.keys()]) cancelLate(k, "the server stopped"); // no soffice outlives the server
package/src/session.js CHANGED
@@ -483,7 +483,7 @@ export class Sessions extends EventEmitter {
483
483
  const j = JSON.parse(fs.readFileSync(f, "utf8"));
484
484
  const s = { ...this.#fresh(key, j.file, j.format, j.baseSha), ...j, pollers: [] };
485
485
  // a hard kill can leave work in flight that no result will ever settle
486
- if (s.delivered?.stage === "checking") s.delivered.stage = "writing";
486
+ if (s.delivered?.stage === "checking") (s.delivered.stage = "writing"), (s.delivered.checkingAt = null);
487
487
  if (s.delivered?.local) {
488
488
  // the local runner died with the server. Not yet writing: the batch waits for an agent, as if handed back. Writing: the lease
489
489
  // expires at the first tick, which records what was written (it needs the snapshots, which open() sets after this)
@@ -1674,7 +1674,16 @@ export class Sessions extends EventEmitter {
1674
1674
 
1675
1675
  /** Called on a timer: a delivered batch whose lease expired goes back to pending (stranded) and the document unlocks. */
1676
1676
  tick() {
1677
- for (const s of this.map.values()) if (s.delivered && s.delivered.lease.expiresAt <= this.now()) this.#expire(s);
1677
+ const now = this.now();
1678
+ const checkMax = Math.max(LEASE_MS(), 5 * 60_000);
1679
+ for (const s of this.map.values()) {
1680
+ if (!s.delivered || s.delivered.lease.expiresAt > now) continue;
1681
+ if (s.delivered.stage === "checking") {
1682
+ const since = s.delivered.checkingAt ?? s.delivered.lease.expiresAt;
1683
+ if (now - since < checkMax) continue;
1684
+ }
1685
+ this.#expire(s);
1686
+ }
1678
1687
  }
1679
1688
 
1680
1689
  /**
@@ -1710,6 +1719,7 @@ export class Sessions extends EventEmitter {
1710
1719
  const s = this.get(key);
1711
1720
  if (!s.delivered || s.delivered.stage === stage) return;
1712
1721
  s.delivered.stage = stage;
1722
+ s.delivered.checkingAt = stage === "checking" ? this.now() : null;
1713
1723
  this.#changed(s);
1714
1724
  }
1715
1725
 
@@ -1854,6 +1864,7 @@ export class Sessions extends EventEmitter {
1854
1864
  if (!s.delivered) throw new HttpError(409, "no batch is in progress for this file (taken back or already finished)", { code: "no_batch" });
1855
1865
  this.#assertLease(s, claim, { staleOnHolder: true });
1856
1866
  this.setStage(key, "checking");
1867
+ this.#renew(s);
1857
1868
  let post;
1858
1869
  let pre;
1859
1870
  try {
package/src/setup.js CHANGED
@@ -184,8 +184,26 @@ export async function runSetup(rest, { home = os.homedir(), source = SKILL_SOURC
184
184
  const tmp = path.join(parent, `.${PRODUCT.bin}.tmp-${process.pid}`);
185
185
  fs.rmSync(tmp, { recursive: true, force: true });
186
186
  copyTree(source, tmp);
187
- if (st) fs.rmSync(target, { recursive: true });
188
- fs.renameSync(tmp, target);
187
+ if (!st) fs.renameSync(tmp, target);
188
+ else {
189
+ const old = path.join(parent, `.${PRODUCT.bin}.old-${process.pid}`);
190
+ // Deliver Ctrl-C after the two synchronous renames, when the target is whole again.
191
+ const interrupt = () => process.kill(process.pid, "SIGINT");
192
+ const defer = process.listenerCount("SIGINT") === 0;
193
+ if (defer) process.once("SIGINT", interrupt);
194
+ try {
195
+ fs.renameSync(target, old);
196
+ try {
197
+ fs.renameSync(tmp, target);
198
+ } catch (err) {
199
+ fs.renameSync(old, target);
200
+ throw err;
201
+ }
202
+ await fs.promises.rm(old, { recursive: true });
203
+ } finally {
204
+ if (defer) process.off("SIGINT", interrupt);
205
+ }
206
+ }
189
207
  }
190
208
  say(`${dry}${target}: ${st ? "replaced" : "installed"}`);
191
209
  }