worktrust 0.10.31 → 0.10.33

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/log-session.mjs CHANGED
@@ -60,7 +60,7 @@
60
60
  * honoured here too, matched against the transcript's project directory, before a file is read.
61
61
  */
62
62
  import { execFileSync, spawn, spawnSync } from "node:child_process";
63
- import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync, lstatSync, openSync, closeSync, fsyncSync, linkSync, unlinkSync, constants } from "node:fs";
63
+ import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync, lstatSync, openSync, closeSync, fsyncSync, fstatSync, linkSync, unlinkSync, readSync, constants } from "node:fs";
64
64
  import { homedir } from "node:os";
65
65
  import { dirname, join, resolve, sep } from "node:path";
66
66
  import { fileURLToPath } from "node:url";
@@ -159,7 +159,15 @@ const DERIVED = new WeakMap();
159
159
  * Running more often costs nothing extra: the watermark means a run with nothing new to say opens
160
160
  * no file, and a day still being worked in is never sent twice.
161
161
  */
162
- const HOOK_EVENTS = ["SessionEnd", "SessionStart", "PreCompact"];
162
+ const HOOK_EVENTS = ["SessionEnd", "SessionStart", "PreCompact", "Stop"];
163
+ /**
164
+ * STOP, AT MOST ONCE AN HOUR (0.10.32, after the owner's session of 2026-10-04 to 10-28). A VS Code session that stays open
165
+ * for weeks fires SessionStart once and compacts rarely, and the live door takes a day for 48 hours only: every finished
166
+ * day arrived late. Claude Code's Stop fires after every answer; the hook answers it at once unless an hour has passed
167
+ * since its last Stop sweep, and then sweeps in a detached copy of itself so the answer is never held. The same hook the
168
+ * person agreed to (the "session hook" of consent.json), on one more of Claude Code's moments.
169
+ */
170
+ const STOP_SWEEP_MS = 3600_000;
163
171
 
164
172
  const STATE = join(homedir(), ".worktrust", "sessions.json");
165
173
  const readState = () => { try { return JSON.parse(readFileSync(STATE, "utf8")); } catch { return {}; } }, writeState = (state) => { mkdirSync(dirname(STATE), { recursive: true }); writeFileSync(STATE, JSON.stringify(state, null, 1)); };
@@ -222,10 +230,10 @@ const excluded = (path) => excludes.some((text) => text && path.includes(text));
222
230
  * reader), the database clients in session-databases.mjs. The install copies both into ~/.worktrust with this hook.
223
231
  * Without them this hook still reads Claude Code; the never-matching patterns keep every other client out.
224
232
  */
225
- const { CODEX_ROLLOUT = /(?!)/, codexIdOf = (file) => String(file).split(/[\\/]/).at(-1).replace(/\.jsonl$/, ""), antigravityIdOf = (file) => String(file).split(/[\\/]/).at(-4), codexLines, codexRolloutFiles = function* () {}, codexCwd = () => null, ANTIGRAVITY_TRANSCRIPT = /(?!)/, antigravityLines, antigravityRoots = () => [], antigravityTranscripts = function* () {}, antigravityContext = () => ({}), rememberAntigravity = () => null, codexLive = () => null, sessionKindReader = () => ({ note() {}, kind: () => "unknown" }) } = (await import("./transcript-readers.mjs").catch(() => null)) ?? {};
233
+ const { CODEX_ROLLOUT = /(?!)/, codexIdOf = (file) => String(file).split(/[\\/]/).at(-1).replace(/\.jsonl$/, ""), antigravityIdOf = (file) => String(file).split(/[\\/]/).at(-4), codexLines, codexRolloutFiles = function* () {}, codexCwd = () => null, ANTIGRAVITY_TRANSCRIPT = /(?!)/, antigravityLines, antigravityRoots = () => [], antigravityTranscripts = function* () {}, antigravityContext = () => ({}), rememberAntigravity = () => null, codexLive = () => null, sessionKindReader = () => ({ note() {}, kind: () => "unknown" }), turnByPerson = () => null, timerSchedule = () => null } = (await import("./transcript-readers.mjs").catch(() => null)) ?? {};
226
234
  // THE STRETCH'S DERIVED RECORD (0.7.4: split from this file): tool calls named by kind, verification, recovery, delegation,
227
235
  // context and routing, for the local archive only. A copy without the module still measures and sends exactly as before.
228
- const { callOf = (block) => ({ id: block.id, kinds: [], family: String(block.name), digest: "" }), deriveStretch = () => ({}), complexityOf = null, resultOf = (block) => ({ id: block.tool_use_id, failed: block.is_error === true, refusal: null }) } = (await import("./stretch-evidence.mjs").catch(() => null)) ?? {};
236
+ const { callOf = (block) => ({ id: block.id, kinds: [], family: String(block.name), digest: "" }), deriveStretch = () => ({}), complexityOf = null, recoveryOf = null, resultOf = (block) => ({ id: block.tool_use_id, failed: block.is_error === true, refusal: null }) } = (await import("./stretch-evidence.mjs").catch(() => null)) ?? {};
229
237
  const { DATABASE_SESSION = /(?!)/, databaseLines = () => null, databaseSessions = function* () {} } = (await import("./session-databases.mjs").catch(() => null)) ?? {};
230
238
  // A WEB CHAT'S DATA EXPORT AS HISTORY (0.10.10, owner 2026-10-10): chat-exports.mjs reads a ChatGPT or Claude export (the counter
231
239
  // imports the same reader); `--history --export <path>` sends its conversations as RECONSTRUCTED days, never measured, never tokens.
@@ -266,15 +274,97 @@ function* antigravityConversations() {
266
274
  if (!(cwd && excluded(cwd))) yield { id: `antigravity:${conversationId}`, file };
267
275
  }
268
276
  }
269
- /** A transcript's lines as objects, whichever client wrote it; null when the file cannot be read. */
277
+ /**
278
+ * A FILE'S LINES, READ IN CHUNKS (0.10.32). A whole-file `readFileSync(file, "utf8")` throws past the longest string Node
279
+ * makes (about 512 MB), and that throw was read as "this file cannot be read": the owner's VS Code session passed it on
280
+ * 2026-10-18 (797 MB by 10-28) and from then on measured no day at all, live or re-measured. Read 8 MB at a time, one
281
+ * line at a time; re-iterable (each pass opens the file again); null only when the file cannot be opened.
282
+ */
283
+ const CHUNK_BYTES = 8 << 20;
284
+ /**
285
+ * A READ THAT FAILS IS COUNTED, NEVER SILENT (0.10.32): a transcript that exists and cannot be opened or read to its end
286
+ * is one failure. The run keeps the count in sessions.json (`npx worktrust status` says it) and tells WorkTrust the
287
+ * count alone, never which file or what was in it (reportUnreadable).
288
+ */
289
+ const READ_FAILURES = { n: 0 };
290
+ /** The bytes this run read from transcripts (the resume's own measure, below): said on stderr under WORKTRUST_HOOK_DEBUG, never sent. */
291
+ const READ_BYTES = { n: 0 };
292
+ /**
293
+ * What a resume trusts of the bytes it does not read again: eight 4 KB samples before the offset, the first and the last of
294
+ * them at its edges and six spread evenly between, hashed with the offset (32 KB read, whatever the file's length).
295
+ */
296
+ const EDGE_BYTES = 4096, EDGE_SAMPLES = 8;
297
+ const edgesOf = (fd, end) => {
298
+ const hash = createHash("sha256").update(`worktrust-read-position|${end}|`), buffer = Buffer.allocUnsafe(EDGE_BYTES);
299
+ const starts = end <= EDGE_BYTES ? [0] : [...new Set(Array.from({ length: EDGE_SAMPLES }, (_, k) => Math.floor((k * (end - EDGE_BYTES)) / (EDGE_SAMPLES - 1))))];
300
+ for (const from of starts) {
301
+ const want = Math.min(EDGE_BYTES, end - from);
302
+ let got = 0;
303
+ while (got < want) { const n = readSync(fd, buffer, got, want - got, from + got); if (n <= 0) break; got += n; }
304
+ READ_BYTES.n += got;
305
+ hash.update(buffer.subarray(0, got));
306
+ }
307
+ return hash.digest("base64url").slice(0, 22);
308
+ };
309
+ /**
310
+ * The lines from byte `from` (a line boundary), split on the newline BYTE, which no UTF-8 sequence holds, and decoded line by
311
+ * line. `at`, when given, says where each yielded line starts (`at.line`) and, read through the same descriptor the lines came
312
+ * from: the file's identity (`at.dev`, `at.ino`), the offset after its last newline (`at.end`, set before an unterminated last
313
+ * line is yielded) and the hash of its edges before that offset (`at.hash`). `at.failed` is a read that failed (counted, as
314
+ * always); `at.moved` a file that is not the one `expect` names (another identity, shorter than its offset, other bytes at its
315
+ * edges): nothing is yielded and nothing counted, and the caller reads it whole.
316
+ */
317
+ function* chunkedLines(file, from = 0, at = null, expect = null) {
318
+ let fd;
319
+ try { fd = openSync(file, "r"); } catch { READ_FAILURES.n += 1; if (at) at.failed = true; return; }
320
+ try {
321
+ if (at || expect) {
322
+ let stat; try { stat = fstatSync(fd); } catch { READ_FAILURES.n += 1; if (at) at.failed = true; return; }
323
+ if (at) { at.dev = String(stat.dev); at.ino = String(stat.ino); }
324
+ if (expect) {
325
+ let same = false;
326
+ try { same = String(stat.dev) === expect.dev && String(stat.ino) === expect.ino && stat.size >= expect.end && expect.end >= from && (expect.end === 0 ? expect.hash === null : edgesOf(fd, expect.end) === expect.hash); } catch { same = false; }
327
+ if (!same) { if (at) at.moved = true; return; }
328
+ }
329
+ }
330
+ const buffer = Buffer.allocUnsafe(CHUNK_BYTES);
331
+ let pending = [], position = from, lineStart = from;
332
+ for (;;) {
333
+ let n;
334
+ try { n = readSync(fd, buffer, 0, CHUNK_BYTES, position); } catch { READ_FAILURES.n += 1; if (at) at.failed = true; return; }
335
+ if (n <= 0) break;
336
+ READ_BYTES.n += n;
337
+ const chunk = buffer.subarray(0, n);
338
+ let start = 0;
339
+ for (let nl = chunk.indexOf(10); nl !== -1; nl = chunk.indexOf(10, start)) {
340
+ const text = pending.length > 0 ? Buffer.concat([...pending, chunk.subarray(start, nl)]).toString("utf8") : chunk.toString("utf8", start, nl);
341
+ pending = [];
342
+ if (at) at.line = lineStart;
343
+ lineStart = position + nl + 1; start = nl + 1;
344
+ yield text;
345
+ }
346
+ if (start < n) pending.push(Buffer.from(chunk.subarray(start)));
347
+ position += n;
348
+ }
349
+ if (at) { at.end = lineStart; try { at.hash = lineStart > 0 ? edgesOf(fd, lineStart) : null; } catch { READ_FAILURES.n += 1; at.failed = true; } }
350
+ const rest = pending.length > 0 ? Buffer.concat(pending).toString("utf8") : "";
351
+ if (rest) { if (at) at.line = lineStart; yield rest; }
352
+ } finally { closeSync(fd); }
353
+ }
354
+ /** A Claude Code transcript's lines as objects from byte `from`, `at` and `expect` as chunkedLines takes them. */
355
+ function* claudeLinesFrom(file, from, at, expect) { for (const raw of chunkedLines(file, from, at, expect)) { if (!raw.trim()) continue; let line; try { line = JSON.parse(raw); } catch { continue; } yield line; } }
356
+ const fileLines = (file) => {
357
+ try { closeSync(openSync(file, "r")); } catch (error) { if (error?.code !== "ENOENT") READ_FAILURES.n += 1; return null; }
358
+ return { [Symbol.iterator]: () => chunkedLines(file) };
359
+ };
360
+ /** A transcript's lines as objects, whichever client wrote it; null when the file cannot be read. A Claude Code transcript is parsed lazily, as it is read. */
270
361
  const parsedLines = (file) => {
271
362
  if (DATABASE_SESSION.test(file)) return databaseLines(file);
272
- let lines; try { lines = readFileSync(file, "utf8").split("\n"); } catch { return null; }
363
+ const lines = fileLines(file);
364
+ if (!lines) return null;
273
365
  if (CODEX_ROLLOUT.test(file)) return [...codexLines(lines)];
274
366
  if (ANTIGRAVITY_TRANSCRIPT.test(file)) { const id = antigravityIdOf(file); return [...antigravityLines(lines, { id, ...antigravityContext(id) })]; }
275
- const out = [];
276
- for (const raw of lines) { if (!raw.trim()) continue; try { out.push(JSON.parse(raw)); } catch { /* a torn line */ } }
277
- return out;
367
+ return { *[Symbol.iterator]() { for (const raw of lines) { if (!raw.trim()) continue; try { yield JSON.parse(raw); } catch { /* a torn line */ } } } };
278
368
  };
279
369
  /** The coupling Codex holds, when Claude Code holds none: the same door, read from ~/.codex/config.toml. */
280
370
  function codexCoupling() {
@@ -375,7 +465,36 @@ const pathsTouched = (cwd, from, to) => {
375
465
  * left unsaid, and WorkTrust reads an unsaid kind as interactive. A copy without the readers reads every session as unknown.
376
466
  * WorkTrust counts a headless or scheduled session as Agent operations, never as the person's hours.
377
467
  */
378
- function stretchesOf(file, since) {
468
+ /**
469
+ * THE READ POSITION (0.10.32, owner 2026-10-28: the owner's session was 805 MB after 24 days, and every hook run and every
470
+ * hourly Stop sweep read it from its first byte, because the watermark is a TIME filter). Beside a Claude Code transcript's
471
+ * watermark the hook keeps where its last read ended, and the next run reads only what it has to. Per file (the session's
472
+ * own and each of its subagents'):
473
+ * d, i the file's identity (device and inode), read through the descriptor the lines came from;
474
+ * e the offset after the last newline read (an unterminated last line is read again next time);
475
+ * h a hash of eight 4 KB samples before `e` (its edges and six between): a file truncated, rewritten, rotated or compacted
476
+ * fails it, or its identity or length does (a change between the samples of a file the client only appends to is not seen);
477
+ * c / s the CARRY: what a full read would know at `e` of the lines it does not read again (the session's last cwd, the last
478
+ * `steered` a line said, the session-kind reader's state; a subagent's first moment);
479
+ * b the offset of the first line holding a message after `t`: every line before it holds none, so a run whose watermark
480
+ * is `t` or later skips it unread. `b` is at or before the start of the earliest day not yet sent, and a day that spans
481
+ * the offset is therefore read again whole, from its first message: nothing of a stretch is kept between runs.
482
+ * and per transcript: `t` (the watermark `b` was cut for) and `v` (this hook's digest and the measure rules it ran under).
483
+ * A run resumes only with the same `v`, a watermark at or past `t`, and a file whose identity, length (at least `e`) and edges
484
+ * agree; it then reads from `b`, letting the lines between `b` and `e` add messages only and the lines from `e` on add both
485
+ * messages and carry. Anything else is a full read, as before 0.10.32: a new CLI, other measure rules, a state written without
486
+ * positions, a watermark moved back, a file that changed. The re-measure (`--history`, `--history --rebuild`, the offer after
487
+ * an update) never resumes. Proven equal to a full read by scripts/test-cli-read-position.mjs.
488
+ */
489
+ const positionKey = () => (selfDigest ? `${selfDigest.slice(0, 16)}|${DAY_RULES}` : null);
490
+ const resumesHere = (file) => /\.jsonl$/.test(String(file)) && !CODEX_ROLLOUT.test(file) && !ANTIGRAVITY_TRANSCRIPT.test(file) && !DATABASE_SESSION.test(file) && typeof sessionKindReader().state === "function";
491
+ const resumable = (saved, since) => Boolean(saved && typeof saved === "object" && positionKey() !== null && saved.v === positionKey() && (saved.t === null || (Number.isFinite(saved.t) && since !== null && since >= saved.t)));
492
+ const fileMarkOk = (mark) => Boolean(mark && typeof mark === "object" && typeof mark.d === "string" && typeof mark.i === "string" && Number.isSafeInteger(mark.e) && Number.isSafeInteger(mark.b) && mark.b >= 0 && mark.b <= mark.e && (mark.h === null ? mark.e === 0 : typeof mark.h === "string"));
493
+ const expectOf = (mark) => ({ dev: mark.d, ino: mark.i, end: mark.e, hash: mark.h });
494
+ /** The first line holding a message after `t`, among the messages read ([moment, line start]); `end` when there is none. */
495
+ const firstAfter = (pairs, t, end) => { let b = end; for (const [ms, start] of pairs) if ((t === null || ms > t) && start < b) b = start; return b; };
496
+ function stretchesOf(file, since, saved = null) {
497
+ if (resumesHere(file)) return claudeStretchesOf(file, since, resumable(saved, since) ? saved : null);
379
498
  // A file we cannot read is no days, never a null the caller has to remember to check: this
380
499
  // returns a list now, and one caller forgetting that is one silent crash inside a hook.
381
500
  const lines = parsedLines(file);
@@ -397,7 +516,47 @@ function stretchesOf(file, since) {
397
516
  messages.sort((a, b) => a.at - b.at);
398
517
  const byDay = byDayOf(messages);
399
518
  const agents = agentsByDay(file, since), session = kind.kind();
400
- return [...byDay.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([day, ofDay]) => { const stretch = measure(cwd, ofDay); return stretch && { ...stretch, ref: stretchRef(file, day), steered, session, agents: agents.get(day) ?? null, live: liveOf(file, day) }; }).filter(Boolean);
519
+ return [...byDay.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([day, ofDay]) => { const stretch = measure(cwd, ofDay, session); return stretch && { ...stretch, ref: stretchRef(file, day), steered, session, agents: agents.get(day) ?? null, live: liveOf(file, day) }; }).filter(Boolean);
520
+ }
521
+ /** stretchesOf for a Claude Code transcript, from its read position when `saved` holds a usable one; `positionAt(t)` on the list is the position to keep. */
522
+ function claudeStretchesOf(file, since, saved) {
523
+ if (!fileLines(file)) return [];
524
+ const prior = saved && fileMarkOk(saved.f) && saved.f.c && typeof saved.f.c === "object" ? saved.f : null;
525
+ const at = {}, pairs = [], messages = [];
526
+ let cwd = typeof prior?.c.cwd === "string" ? prior.c.cwd : null, lineSteered = typeof prior?.c.steered === "string" ? prior.c.steered : null, carry = null;
527
+ const kind = sessionKindReader(prior?.c.kind ?? null), scanFrom = prior ? prior.e : 0;
528
+ const snap = () => ({ cwd, steered: lineSteered, kind: kind.state() });
529
+ for (const line of claudeLinesFrom(file, prior ? prior.b : 0, at, prior ? expectOf(prior) : null)) {
530
+ if (carry === null && at.end !== undefined) carry = snap(); // the unterminated last line is past `e`: the carry stops before it
531
+ if (at.line >= scanFrom) {
532
+ if (typeof line.cwd === "string") cwd = line.cwd;
533
+ if (typeof line.steered === "string") lineSteered = line.steered;
534
+ kind.note(line);
535
+ }
536
+ if (!line.timestamp || (line.type !== "user" && line.type !== "assistant")) continue;
537
+ const ms = Date.parse(line.timestamp);
538
+ if (!Number.isFinite(ms)) continue;
539
+ if (since && ms <= since) continue;
540
+ messages.push(messageOf(line, ms)); pairs.push([ms, at.line]);
541
+ }
542
+ if (at.moved) return claudeStretchesOf(file, since, null);
543
+ if (carry === null && at.end !== undefined) carry = snap();
544
+ const steered = lineSteered ?? STEERED_HERE;
545
+ let stretches = [], subs = null;
546
+ if (messages.length > 0) {
547
+ messages.sort((a, b) => a.at - b.at);
548
+ const byDay = byDayOf(messages), positions = {};
549
+ const agents = agentsByDay(file, since, saved?.a ?? null, positions), session = kind.kind();
550
+ subs = positions;
551
+ stretches = [...byDay.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([day, ofDay]) => { const stretch = measure(cwd, ofDay, session); return stretch && { ...stretch, ref: stretchRef(file, day), steered, session, agents: agents.get(day) ?? null, live: liveOf(file, day) }; }).filter(Boolean);
552
+ }
553
+ // A read that failed keeps no position: the next run reads the file whole.
554
+ if (!at.failed && at.end !== undefined && carry !== null) {
555
+ stretches.positionAt = (t) => ({ v: positionKey(), t: t ?? null, f: { d: at.dev, i: at.ino, e: at.end, b: firstAfter(pairs, t ?? null, at.end), h: at.hash ?? null, c: carry },
556
+ // Subagents not read this run (no message after the watermark) keep the marks they had: each was cut for a watermark at or before this one.
557
+ a: subs ? Object.fromEntries(Object.entries(subs).map(([name, markAt]) => [name, markAt(t ?? null)]).filter(([, mark]) => mark)) : (saved?.a && typeof saved.a === "object" ? saved.a : {}) });
558
+ }
559
+ return stretches;
401
560
  }
402
561
 
403
562
  /**
@@ -445,6 +604,10 @@ const messageOf = (line, at) => {
445
604
  // A tool that waits for the PERSON (a question, a plan to approve): the gap to its result is the person's, not the tool's. The name is read, never the input.
446
605
  waitsForPerson: line.type === "assistant" && blocks.some((block) => block && block.type === "tool_use" && WAITS_FOR_PERSON.has(String(block.name))),
447
606
  agentic: line.agentic === true, cumulative: line.cumulative === true, // the session's running totals (Hermes, Goose without a ledger)
607
+ // 0.10.32 (owner, 2026-10-28): who wrote a turn on the person's role (true the person, false a program or a timer, null not said),
608
+ // and an answer that did not complete (the app's own API-error line, a refusal), read for the call's outcome and never kept.
609
+ byPerson: line.type === "user" ? turnByPerson(line) : null,
610
+ failedAnswer: line.type === "assistant" && (line.isApiErrorMessage === true || line.message?.stop_reason === "refusal"),
448
611
  // 0.7.1: each tool call's kinds (never its command) and each tool result's outcome, joined on the call's id in `measure`.
449
612
  // 0.7.2: and the call's FAMILY (the tool, and its check kinds) and a digest of its input, compared here and never kept.
450
613
  calls: line.type === "assistant" ? blocks.filter((block) => block && block.type === "tool_use" && typeof block.id === "string").map(callOf) : [],
@@ -453,46 +616,92 @@ const messageOf = (line, at) => {
453
616
  };
454
617
 
455
618
  /**
456
- * SUBAGENTS AS AGENT TIME, NEVER AS HOURS (owner, 2026-10-06). Claude Code writes each subagent's transcript beside its
457
- * session (`<session>/subagents/*.jsonl`: 279 of them beside 48 sessions on the computer this was built on), and the
458
- * sweep read only the sessions, so delegated work was invisible. Each run is read for its clocks: how many runs started
459
- * that day and how many were open at once at the busiest moment (read here for the complexity class). Their SECONDS are
460
- * not computed here since 0.10.20 (no counting rule ships in this file); until a timeline per run travels, the agent
461
- * group is not sent. Only Claude Code keeps subagents this way; every other client answers an empty map.
619
+ * SUBAGENTS AS AGENT TIME, NEVER AS HOURS (owner, 2026-10-06; AGENTS ACTIVE AT ONCE, 0.10.32, owner 2026-10-28). Claude Code
620
+ * writes each subagent's transcript beside its session (`<session>/subagents/*.jsonl`, and a `.meta.json` saying how it was
621
+ * started). Each run that started on a day rides with that day's stretch as one entry of `agents`, by position (the ordinal),
622
+ * never a name, a prompt, a path or an id: its kind (`fork`, else `background`, else `builtin` or the person's `own` type), its
623
+ * start in whole seconds from the stretch's start, its ACTIVE intervals from that start ([from, to, from, to, …]; a gap over
624
+ * 300 s ends one) and its tool failures and recoveries (stretch-evidence's own rule). At most AGENT_RUNS runs a stretch,
625
+ * AGENT_INTERVALS intervals a run and AGENT_INTERVALS_TOTAL in all, the longest kept: a dropped one reads as less, never more.
626
+ * WorkTrust counts a run only inside its parent's steered window (docs/AGENT-HOURS.md). Only Claude Code keeps subagents this way.
462
627
  */
628
+ const AGENT_RUNS = 64, AGENT_INTERVALS = 12, AGENT_INTERVALS_TOTAL = 256, AGENT_GAP_MS = 300_000, BUILTIN_AGENTS = new Set(["general-purpose", "Explore", "Plan", "statusline-setup", "output-style-setup", "claude-code-guide"]);
629
+ const agentKindOf = (meta) => (meta?.isFork === true || meta?.agentType === "fork" ? "fork" : meta?.requestShape === "background" ? "background" : meta?.isBuiltIn === true || !meta || BUILTIN_AGENTS.has(meta.agentType) ? "builtin" : "own");
463
630
  function subagentFiles(file) {
464
631
  if (!/\.jsonl$/.test(String(file)) || CODEX_ROLLOUT.test(file) || ANTIGRAVITY_TRANSCRIPT.test(file) || DATABASE_SESSION.test(file)) return [];
465
632
  const dir = join(String(file).replace(/\.jsonl$/, ""), "subagents");
466
633
  try { return readdirSync(dir).filter((name) => name.endsWith(".jsonl")).map((name) => join(dir, name)); } catch { return []; }
467
634
  }
468
- function agentsByDay(file, since) {
635
+ /** A file whose identity, length and edges are still those of its mark: the bytes before `e` are the ones read then. */
636
+ const unchangedSince = (file, mark) => { let fd; try { fd = openSync(file, "r"); } catch { return false; } try { const stat = fstatSync(fd); return String(stat.dev) === mark.d && String(stat.ino) === mark.i && stat.size >= mark.e && (mark.e === 0 ? mark.h === null : edgesOf(fd, mark.e) === mark.h); } catch { return false; } finally { closeSync(fd); } };
637
+ /** One subagent transcript's lines, from its mark when it has a usable one: its first moment, its stamps and messages after `since`, and the mark to keep. */
638
+ function subagentRead(sub, since, prior) {
639
+ const at = {}, stamps = [], messages = [], pairs = [];
640
+ let started = prior ? (Number.isFinite(prior.s) ? prior.s : Infinity) : Infinity, carry = null;
641
+ for (const line of claudeLinesFrom(sub, prior ? prior.b : 0, at, prior ? expectOf(prior) : null)) {
642
+ if (carry === null && at.end !== undefined) carry = started;
643
+ if (!line.timestamp || (line.type !== "user" && line.type !== "assistant")) continue;
644
+ const ms = Date.parse(line.timestamp);
645
+ if (!Number.isFinite(ms)) continue;
646
+ started = Math.min(started, ms); // a line between `b` and `e` is in the carried minimum already: a minimum taken twice is the same
647
+ if (!(since && ms <= since)) { stamps.push(ms); messages.push(messageOf(line, ms)); pairs.push([ms, at.line]); }
648
+ }
649
+ if (at.moved) return subagentRead(sub, since, null);
650
+ if (carry === null && at.end !== undefined) carry = started;
651
+ const markAt = at.failed || at.end === undefined || carry === null ? () => null : (t) => ({ d: at.dev, i: at.ino, e: at.end, b: firstAfter(pairs, t, at.end), h: at.hash ?? null, s: Number.isFinite(carry) ? carry : null });
652
+ return { started, stamps, messages, markAt };
653
+ }
654
+ function agentsByDay(file, since, saved = null, positions = null) {
469
655
  const byDay = new Map();
470
656
  for (const sub of subagentFiles(file)) {
657
+ const name = String(sub).split(/[\\/]/).pop(), prior = positions && saved && typeof saved === "object" && fileMarkOk(saved[name]) && (saved[name].s === null || Number.isFinite(saved[name].s)) ? saved[name] : null;
658
+ // A RUN THAT STARTED AT OR BEFORE THE WATERMARK is not counted (below), and lines appended since cannot make it start later:
659
+ // a subagent whose kept first moment is at or before the watermark, its bytes unchanged, is not opened again.
660
+ if (prior && since && prior.s !== null && prior.s <= since && unchangedSince(sub, prior)) { positions[name] = () => prior; continue; }
661
+ if (positions ? !fileLines(sub) : false) continue;
662
+ let stamps, messages, started;
663
+ if (positions) { const read = subagentRead(sub, since, prior); ({ stamps, messages, started } = read); positions[name] = read.markAt; }
664
+ else {
471
665
  const lines = parsedLines(sub);
472
666
  if (!lines) continue;
473
- const stamps = [];
474
- let started = Infinity;
667
+ stamps = []; messages = [];
668
+ started = Infinity;
475
669
  for (const line of lines) {
476
670
  if (!line.timestamp || (line.type !== "user" && line.type !== "assistant")) continue;
477
671
  const at = Date.parse(line.timestamp);
478
672
  if (!Number.isFinite(at)) continue;
479
673
  started = Math.min(started, at);
480
- if (!(since && at <= since)) stamps.push(at);
674
+ if (!(since && at <= since)) { stamps.push(at); messages.push(messageOf(line, at)); }
481
675
  }
482
- // A RUN IS COUNTED ONCE, on the side of the watermark where it STARTED (review, 2026-10-06): a run that straddles the
483
- // watermark was sent with the day it began and is not a second run on the next sweep; what it did after the
484
- // watermark stays unsent, which is the safe direction.
676
+ }
677
+ // A RUN IS COUNTED ONCE, on the side of the watermark where it STARTED (review, 2026-10-06), and on the day it started: a run
678
+ // that straddles the watermark was sent with the day it began; what it did after the watermark stays unsent (the safe direction).
485
679
  if (stamps.length === 0 || (since && started <= since)) continue;
486
- stamps.sort((a, b) => a - b);
487
- // A run is counted on the day it started.
680
+ stamps.sort((a, b) => a - b); messages.sort((a, b) => a.at - b.at);
488
681
  const day = new Date(stamps[0]).toISOString().slice(0, 10);
489
- if (!byDay.has(day)) byDay.set(day, { runs: 0, spans: [] });
490
- const entry = byDay.get(day);
682
+ if (!byDay.has(day)) byDay.set(day, { runs: 0, spans: [], list: [] });
683
+ const entry = byDay.get(day), recovery = recoveryOf ? recoveryOf(messages) : null;
684
+ let meta = null; try { meta = JSON.parse(readFileSync(sub.replace(/\.jsonl$/, ".meta.json"), "utf8")); } catch { /* an older client wrote none */ }
491
685
  entry.runs += 1; entry.spans.push([stamps[0], stamps[stamps.length - 1]]);
686
+ entry.list.push({ stamps, kind: agentKindOf(meta), failures: recovery?.failures ?? 0, recovered: recovery?.recovered ?? 0 });
492
687
  }
493
688
  for (const entry of byDay.values()) entry.peak = peakOf(entry.spans);
494
689
  return byDay;
495
690
  }
691
+ /** A day's runs → the payload's `agents`, from the stretch's first moment: whole seconds, the caps applied, null when none ran. */
692
+ function agentRunsOf(list, from) {
693
+ const longest = (items, cap, length) => (items.length <= cap ? items : items.map((item, at) => ({ item, at })).sort((a, b) => length(b.item) - length(a.item) || a.at - b.at).slice(0, cap).sort((a, b) => a.at - b.at).map(({ item }) => item));
694
+ const runs = (list ?? []).map((run) => {
695
+ const spans = [];
696
+ for (const at of run.stamps) { const last = spans.at(-1); if (last && at - last[1] <= AGENT_GAP_MS) last[1] = at; else spans.push([at, at]); }
697
+ return { ...run, spans: spans.map(([lo, hi]) => [Math.max(0, Math.round((lo - from) / 1000)), Math.max(0, Math.round((hi - from) / 1000))]).filter(([lo, hi]) => hi > lo) };
698
+ }).filter((run) => run.spans.length > 0);
699
+ const kept = longest(runs, AGENT_RUNS, (run) => run.spans.reduce((total, [lo, hi]) => total + hi - lo, 0)), length = ([lo, hi]) => hi - lo;
700
+ let per = AGENT_INTERVALS; // the most intervals a run keeps so that all of them together stay inside AGENT_INTERVALS_TOTAL
701
+ while (per > 1 && kept.reduce((total, run) => total + Math.min(per, run.spans.length), 0) > AGENT_INTERVALS_TOTAL) per -= 1;
702
+ return kept.length > 0 ? kept.map((run) => { const spans = longest(run.spans, per, length), start = spans[0][0];
703
+ return { kind: run.kind, start, active: spans.flatMap(([lo, hi]) => [lo - start, hi - start]), failures: run.failures, recovered: Math.min(run.recovered, run.failures) }; }).sort((a, b) => a.start - b.start) : null;
704
+ }
496
705
  /** The most spans open at one instant; a span that ends where another starts does not overlap it. */
497
706
  const peakOf = (spans) => {
498
707
  const events = spans.flatMap(([from, to]) => [[from, 1], [to, -1]]).sort((a, b) => a[0] - b[0] || a[1] - b[1]);
@@ -524,7 +733,7 @@ function isHumanTurn(content) {
524
733
  }
525
734
 
526
735
  /** One day's messages → the stretch: its timeline, its tokens, its model. The server measures it. */
527
- function measure(cwd, messages) {
736
+ function measure(cwd, messages, session = null) {
528
737
  // THE CLOCK IS THE SERVER'S (0.10.20). This hook sends the stretch's timeline and never a duration of its own: every
529
738
  // second WorkTrust counts lies between two recorded timestamps in that timeline, under WorkTrust's rules, and a stretch
530
739
  // too short to time is a stretch WorkTrust answers without a duration. A stretch whose clocks give nothing is still
@@ -534,7 +743,8 @@ function measure(cwd, messages) {
534
743
  const model = [...models.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] ?? null;
535
744
  const tokens = tokensOf(messages);
536
745
  const from = firstOf(messages);
537
- return { cwd, derived: deriveStretch(messages, { isHumanTurn, INTERRUPTED, textOf }), timeline: messages.length > 0 ? timelineOf(messages) : null, ...tokens, cumulative: messages.some((message) => message.cumulative), model, from, to: messages[messages.length - 1].at, offset: offsetAt(from) };
746
+ const unattended = session === "headless" || session === "scheduled";
747
+ return { cwd, derived: deriveStretch(messages, { isHumanTurn, INTERRUPTED, textOf }), timeline: messages.length > 0 ? timelineOf(messages, unattended) : null, outcomes: messages.length > 0 ? callOutcomesOf(messages) : null, lengths: inputLengthsOf(messages, unattended), ...tokens, cumulative: messages.some((message) => message.cumulative), model, from, to: messages[messages.length - 1].at, offset: offsetAt(from) };
538
748
  }
539
749
 
540
750
  /**
@@ -545,16 +755,50 @@ function measure(cwd, messages) {
545
755
  * turns from this with its own rules (private to the server); this file sends no figure of its own, so nothing in it can
546
756
  * move the passport.
547
757
  */
548
- function timelineOf(messages) {
758
+ // 0.10.32: in a run no person started (headless, scheduled) a turn is H only when the app marked it the person's; a program's or
759
+ // a timer's prompt is U, so WorkTrust's clock steers only from a person's input.
760
+ const stepKindOf = (message, unattended) => message.kind === "assistant" ? (message.waitsForPerson ? "W" : message.toolUse ? "T" : "A") : message.kind === "tool_result" ? "R" : INTERRUPTED.test(textOf(message.content).trim()) && !message.meta ? "I" : !message.meta && isHumanTurn(message.content) && (!unattended || message.byPerson === true) ? "H" : "U";
761
+ function timelineOf(messages, unattended = false) {
549
762
  let out = "";
550
763
  for (let i = 0; i < messages.length; i += 1) {
551
764
  const message = messages[i];
552
765
  const gap = i === 0 ? 0 : Math.max(0, Math.round((message.at - messages[i - 1].at) / 1000));
553
- const kind = message.kind === "assistant" ? (message.waitsForPerson ? "W" : message.toolUse ? "T" : "A") : message.kind === "tool_result" ? "R" : INTERRUPTED.test(textOf(message.content).trim()) && !message.meta ? "I" : !message.meta && isHumanTurn(message.content) ? "H" : "U";
766
+ const kind = stepKindOf(message, unattended);
554
767
  out += `${message.agentic ? "G" : ""}${message.bridge ? kind.toLowerCase() : kind}${Math.min(999999, gap)}`;
555
768
  }
556
769
  return out;
557
770
  }
771
+ /**
772
+ * THE PERSON'S INPUTS, AS LENGTH BUCKETS (owner, 2026-10-28; 0.10.32): in a run no person started, one character per H step of
773
+ * the timeline: s up to 80 characters, m up to 400, l longer. Counted here, never sent as text; WorkTrust's steering cap reads it
774
+ * to tell a regular rhythm of short nudges from real steering, as a review signal only.
775
+ */
776
+ function inputLengthsOf(messages, unattended) {
777
+ if (!unattended) return null;
778
+ let out = "";
779
+ for (const message of messages) if (stepKindOf(message, unattended) === "H") { const n = textOf(message.content).trim().length; out += n <= 80 ? "s" : n <= 400 ? "m" : "l"; }
780
+ return out.length > 0 ? out : null;
781
+ }
782
+ /**
783
+ * EACH CALL'S OUTCOME (owner, 2026-10-28; 0.10.32; docs/AGENTIC-OPERATIONS.md "2b"): one character per model answer step of the
784
+ * timeline (A, T, W), in its order. 1: it completed; 0: it did not (a tool result the app marked as an error, a timeout or a
785
+ * refusal among them; an API-error or refusal answer); ?: not known (a tool call without its result, a result no reader could see).
786
+ * A question to the person (W) completed when it was put. Read from the app's own markers, never from a duration; nothing of a
787
+ * message travels.
788
+ */
789
+ function callOutcomesOf(messages) {
790
+ const failed = new Map();
791
+ for (const message of messages) for (const result of message.results ?? []) if (result && typeof result.id === "string") failed.set(result.id, result.failed);
792
+ let out = "";
793
+ for (const message of messages) {
794
+ if (message.kind !== "assistant") continue;
795
+ if (message.failedAnswer) { out += "0"; continue; }
796
+ if (message.waitsForPerson || !message.toolUse) { out += "1"; continue; }
797
+ const outcomes = (message.calls ?? []).map((call) => (failed.has(call.id) ? failed.get(call.id) : null));
798
+ out += outcomes.some((one) => one === true) ? "0" : outcomes.length > 0 && outcomes.every((one) => one === false) ? "1" : "?";
799
+ }
800
+ return /[01]/.test(out) ? out : null;
801
+ }
558
802
  /** A digest of this hook and the readers beside it, as installed: the door records it beside each line as a signal of which code ran, never as proof. */
559
803
  const selfDigest = (() => { try { const here = dirname(fileURLToPath(import.meta.url)); const hash = createHash("sha256"); for (const name of ["log-session.mjs", ...READER_FILES].sort()) { try { hash.update(`${name}\0`).update(readFileSync(join(here, name))).update("\0"); } catch { hash.update(`${name}\0missing\0`); } } return hash.digest("hex"); } catch { return null; } })();
560
804
  const CLI_VERSION_SENT = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,4}$/.test(process.env.WORKTRUST_CLI_VERSION ?? "") ? process.env.WORKTRUST_CLI_VERSION : null; // `worktrust.mjs` hands its own version to the hook it starts
@@ -621,6 +865,7 @@ function payloadFor(stretch) {
621
865
  const coauthored = cwd && touched.shas.length > 0 ? git(cwd, "log", "--no-walk", "--format=%(trailers:key=Co-authored-by,valueonly,separator=%x01)%x00", ...touched.shas.slice(0, 50)).split("\0").filter((entry, index) => index < Math.min(50, touched.shas.length) && AI_COAUTHOR.test(entry)).length : 0;
622
866
  const route = { ...(stretch.derived?.route ?? {}), ...(touched.committed ? gitRouteOf(cwd, touched.shas, changed) : {}) };
623
867
  const derived = stretch.derived ? { ...stretch.derived, ...(Object.keys(route).length > 0 ? { route } : {}), ...(changed.length > 0 ? { changes: { files: changed.length, test_files: tests } } : {}), ...(touched.shas.length > 0 ? { authorship: { commits: Math.min(50, touched.shas.length), ai_coauthored: coauthored } } : {}), ...(contextFilesOf(changed) ? { context_files: contextFilesOf(changed) } : {}), ...(complexityOf ? { complexity: complexityOf(stretch.derived, { layers: Math.max(layers.length, layer ? 1 : 0), agentRuns: stretch.agents?.runs ?? 0, agentPeak: stretch.agents?.peak ?? 0, seconds: null }) } : {}) } : null;
868
+ const agents = agentRunsOf(stretch.agents?.list, stretch.from);
624
869
  const evidence = derived || stretch.live ? { ...(derived ?? {}), ...(stretch.live ? { live: stretch.live } : {}) } : null; // 0.10.15: the mod's counts ride as their own group
625
870
  const payload = {
626
871
  title: layer ? `AI-assisted work · ${layer}` : "AI-assisted work",
@@ -647,11 +892,16 @@ function payloadFor(stretch) {
647
892
  ...(stretch.ref ? { stretch_ref: stretch.ref } : {}), ...(stretch.steered ? { steered_from: stretch.steered } : {}),
648
893
  // HOW THE SESSION WAS STARTED (owner, 2026-10-22): interactive | headless | scheduled, beside the timeline; unknown is unsaid.
649
894
  ...(stretch.timeline && (stretch.session === "interactive" || stretch.session === "headless" || stretch.session === "scheduled") ? { session_kind: stretch.session } : {}),
895
+ // EACH CALL'S OUTCOME and THE TIMER'S SCHEDULE (owner, 2026-10-28; 0.10.32): beside the timeline; a schedule only for a run no person started.
896
+ ...(stretch.timeline && stretch.outcomes ? { call_outcomes: stretch.outcomes } : {}),
897
+ ...(stretch.timeline && stretch.lengths && (stretch.session === "headless" || stretch.session === "scheduled") ? { input_lengths: stretch.lengths } : {}),
898
+ ...(stretch.timeline && stretch.schedule && (stretch.session === "headless" || stretch.session === "scheduled") ? { schedule_cadence: stretch.schedule.cadence, schedule_ref: stretch.schedule.ref } : {}),
899
+ // AGENTS ACTIVE AT ONCE (0.10.32, owner 2026-10-28): the subagent runs that started this day, beside the timeline (agentRunsOf).
900
+ ...(stretch.timeline && agents ? { agents } : {}),
650
901
  // WHICH CODE SENT IT (0.10.20): a version and a digest of this hook's own files, signals never proof.
651
902
  ...(CLI_VERSION_SENT ? { cli_version: CLI_VERSION_SENT } : {}),
652
903
  ...(selfDigest ? { cli_digest: selfDigest } : {}),
653
- // WHEN (0.6.18): the stretch's first moment and the computer's UTC offset then. The interruptions and the mid-task turns are
654
- // read by WorkTrust from the timeline; the subagents' seconds wait for a timeline per run (0.10.20).
904
+ // WHEN (0.6.18): the stretch's first moment and the computer's UTC offset then; the interruptions and mid-task turns are WorkTrust's.
655
905
  started_at: new Date(stretch.from).toISOString(), utc_offset: stretch.offset,
656
906
  // THE STRETCH'S EVIDENCE (0.8.3): what the CLI derived from it here, kinds and counts only (stretch-evidence.mjs); the door keeps it from a CLI key alone.
657
907
  ...(evidence ? { evidence } : {}),
@@ -712,8 +962,13 @@ const noteReceipt = (answer) => {
712
962
  const json = JSON.parse(answer.trim().startsWith("{") ? answer : answer.split("\n").find((row) => row.startsWith("data:"))?.slice(5) ?? "null");
713
963
  const receipt = JSON.parse(json?.result?.content?.[0]?.text ?? "{}");
714
964
  for (const key of Object.keys(RECEIPTS)) RECEIPTS[key] += Number(receipt[key]) || 0;
965
+ // THE QUEUE (0.10.32): the receipt of the batch that completes a run says where the run waits; the last one seen is kept.
966
+ const queued = receipt.queued;
967
+ if (queued && Number.isInteger(queued.position) && queued.position >= 1 && Number.isFinite(Number(queued.eta_minutes)) && Number(queued.eta_minutes) >= 0) QUEUED.at = { position: queued.position, eta_minutes: Math.max(1, Math.ceil(Number(queued.eta_minutes))) };
715
968
  } catch { /* a receipt unread leaves the counts short, never the send */ }
716
969
  };
970
+ /** Where the last completed run waits in WorkTrust's queue (0.10.32): null when no receipt said so (an older server, or not complete yet). */
971
+ const QUEUED = { at: null };
717
972
  /** Only explicit durable receipts authorize a checkpoint. HTTP 200 alone says nothing. */
718
973
  function confirmedReceipt(answer, expectedCount = null) {
719
974
  try {
@@ -726,7 +981,7 @@ function confirmedReceipt(answer, expectedCount = null) {
726
981
  if (!Array.isArray(content) || content.length !== 1 || content[0].type !== "text") throw Error();
727
982
  const receipt = JSON.parse(content[0].text);
728
983
  if (expectedCount === null) {
729
- if (!(receipt.receipt === "received" && ["accepted", "flagged", "pending"].includes(receipt.status)) && !(receipt.receipt === "unchanged" && receipt.reason === "duplicate")) throw Error();
984
+ if (!(receipt.receipt === "received" && ["accepted", "flagged", "pending"].includes(receipt.status)) && !(receipt.receipt === "unchanged" && (receipt.reason === "duplicate" || receipt.reason === "process_day_held"))) throw Error();
730
985
  } else if (receipt.staged === true) {
731
986
  // KEPT ON THE SERVER (0.10.27): a batch of a run WorkTrust holds and rebuilds itself; every line counted as kept or refused.
732
987
  if (receipt.receipt !== "received" || receipt.received !== expectedCount || !Number.isInteger(receipt.kept) || receipt.kept < 0
@@ -739,7 +994,7 @@ function confirmedReceipt(answer, expectedCount = null) {
739
994
  }
740
995
  /** A private metadata journal survives retries, an edited transcript and a later CLI update. */
741
996
  function retainMeasurement(payload, source) {
742
- const fields = ["stretch_ref", "at", "started_at", "timeline", "seconds", "client_seconds", "model_seconds", "tool_seconds", "human_seconds", "idle_seconds", "tokens_in", "tokens_out", "tokens_cache_read", "tokens_cache_write", "exchanges", "interrupts", "steers", "agent_runs", "agent_seconds", "agent_peak", "duration_basis", "token_basis", "turn_basis", "cli_version", "cli_digest"];
997
+ const fields = ["stretch_ref", "at", "started_at", "timeline", "seconds", "client_seconds", "model_seconds", "tool_seconds", "human_seconds", "idle_seconds", "tokens_in", "tokens_out", "tokens_cache_read", "tokens_cache_write", "exchanges", "interrupts", "steers", "agent_runs", "agent_seconds", "agent_peak", "agents", "duration_basis", "token_basis", "turn_basis", "cli_version", "cli_digest"];
743
998
  const metadata = Object.fromEntries(fields.filter(key => Object.hasOwn(payload, key)).map(key => [key, payload[key]]));
744
999
  const body = JSON.stringify({ format: "worktrust-measurement/1", source: source ?? "claude", metadata }) + "\n";
745
1000
  const parent = join(homedir(), ".worktrust"), dir = join(parent, "measurements");
@@ -756,6 +1011,80 @@ function retainMeasurement(payload, source) {
756
1011
  } finally { closeSync(fd); unlinkSync(temporary); }
757
1012
  }
758
1013
  const HISTORY_DELIVERY = { review: 0, waiting: 0, refused: 0 };
1014
+ const HISTORY_PERSISTED = { n: 0 }; // 0.10.32: entries this run put in the outbox on disk (sent now or later, never lost)
1015
+ /**
1016
+ * ONLY THE DAYS THAT CHANGED (0.10.32; owner, 2026-10-28: an identical re-run sends 0 days). Each UTC day of each AI app gets a
1017
+ * digest of its entries exactly as they are sent (the canonical JSON of each, `commits` gone, the CLI's own version and digest
1018
+ * left out, so an update that measures the same sends nothing), under the measure rules the command started this hook with.
1019
+ * Before a re-measure the door is asked, per AI app, which of this computer's days it still holds as current; it answers the
1020
+ * digests this computer sent with them (its own submissions, never the record). A day whose digest is the same is not sent.
1021
+ * An answer that is not one (an error, an older WorkTrust, no network) is NOT KNOWN, and every day goes, as before.
1022
+ */
1023
+ const DAY_RULES = /^[1-9][0-9]{0,3}$/.test(process.env.WORKTRUST_MEASURE_RULES ?? "") ? process.env.WORKTRUST_MEASURE_RULES : "unknown";
1024
+ const canonical = (value) => (Array.isArray(value) ? `[${value.map(canonical).join(",")}]` : value && typeof value === "object" ? `{${Object.keys(value).sort().filter((key) => value[key] !== undefined).map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(",")}}` : JSON.stringify(value) ?? "null");
1025
+ const entryDigest = (entry) => { const { commits: _commits, cli_version: _version, cli_digest: _digest, ...rest } = entry; void _commits; void _version; void _digest; return createHash("sha256").update(canonical(rest)).digest("hex"); };
1026
+ const dayOf = (entry) => new Date(entry.at).toISOString().slice(0, 10);
1027
+ /** One AI app's entries → day → digest, over all of that app's entries of the day (a day cut across two batches has one digest). */
1028
+ function dayDigests(entries) {
1029
+ const byDay = new Map();
1030
+ for (const entry of entries) { const day = dayOf(entry); byDay.set(day, [...(byDay.get(day) ?? []), entryDigest(entry)]); }
1031
+ return new Map([...byDay].map(([day, digests]) => [day, createHash("sha256").update(`worktrust-day/1|rules=${DAY_RULES}|${digests.sort().join("\n")}`).digest("hex")]));
1032
+ }
1033
+ /** The days the door holds as current for this computer and this AI app, or null when that is not known. Never throws. */
1034
+ async function heldDays(target, source) {
1035
+ try {
1036
+ const body = JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "import_history", arguments: { rebuild: true, fingerprint: true } } });
1037
+ const response = await fetch(target.url, { method: "POST", headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${target.token}`, ...clientOf(source, true), ...proofFor(target.url, body) }, body, redirect: "error", signal: AbortSignal.timeout(20_000) });
1038
+ if (!response.ok) return null;
1039
+ const text = await response.text();
1040
+ const json = JSON.parse(text.trim().startsWith("{") ? text : text.split("\n").find((row) => row.startsWith("data:"))?.slice(5) ?? "null");
1041
+ if (json?.jsonrpc !== "2.0" || json.error || !json.result || json.result.isError === true) return null;
1042
+ const answer = JSON.parse(json.result.content?.[0]?.text ?? "null");
1043
+ if (answer?.receipt !== "fingerprint" || !answer.days || typeof answer.days !== "object" || Array.isArray(answer.days)) return null;
1044
+ const held = new Map();
1045
+ for (const [day, digest] of Object.entries(answer.days)) if (/^\d{4}-\d{2}-\d{2}$/.test(day) && typeof digest === "string" && /^[0-9a-f]{64}$/.test(digest)) held.set(day, digest);
1046
+ return held;
1047
+ } catch { return null; }
1048
+ }
1049
+ /**
1050
+ * A READ FAILURE, KEPT AND TOLD (0.10.32): the count lands in sessions.json (`__unreadable`, which `npx worktrust status`
1051
+ * prints) and, at most once a day, reaches WorkTrust as the fingerprint question with the count beside it
1052
+ * (`unreadable: n`): a number, never a file name, a path or a line. A run that read everything clears the count.
1053
+ */
1054
+ async function settleReadFailures(target, held = null) {
1055
+ // `held`: the hook's own state object, which later steps write back whole; the count goes into it, not beside it.
1056
+ const state = held ?? readState(), now = new Date().toISOString();
1057
+ if (READ_FAILURES.n === 0) { if (state.__unreadable?.count) { state.__unreadable = { count: 0, at: now }; writeState(state); } return; }
1058
+ state.__unreadable = { count: READ_FAILURES.n, at: now };
1059
+ const told = Date.parse(state.__unreadableTold ?? "") || 0;
1060
+ if (target?.url && target?.token && Date.now() - told > 24 * 3600_000) {
1061
+ try {
1062
+ const body = JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "import_history", arguments: { rebuild: true, fingerprint: true, unreadable: Math.min(1000, READ_FAILURES.n) } } });
1063
+ const response = await fetch(target.url, { method: "POST", headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${target.token}`, ...clientOf(undefined, true), ...proofFor(target.url, body) }, body, redirect: "error", signal: AbortSignal.timeout(20_000) });
1064
+ if (response.ok) state.__unreadableTold = now;
1065
+ } catch { /* told next time */ }
1066
+ }
1067
+ writeState(state);
1068
+ }
1069
+ /** What the comparison found, for the command's one line: distinct days compared, and of them how many changed. */
1070
+ const COMPARED = { days: 0, changed: 0, asked: false };
1071
+ /** A re-measure's entries, less every (AI app, day) whose digest WorkTrust holds as current. */
1072
+ async function changedOnly(entries) {
1073
+ const bySource = new Map();
1074
+ for (const entry of entries) { const source = entrySource.get(entry) ?? "claude"; bySource.set(source, [...(bySource.get(source) ?? []), entry]); }
1075
+ const keep = new Set(), allDays = new Set(), changedDays = new Set();
1076
+ for (const [source, group] of bySource) {
1077
+ const target = source === "codex" ? codexDoor ?? door : door;
1078
+ const digests = dayDigests(group);
1079
+ const held = target ? await heldDays(target, source) : null;
1080
+ if (held !== null) COMPARED.asked = true;
1081
+ for (const [day, digest] of digests) { allDays.add(day); if (held?.get(day) !== digest) changedDays.add(`${source}|${day}`); }
1082
+ for (const entry of group) if (changedDays.has(`${source}|${dayOf(entry)}`)) keep.add(entry);
1083
+ }
1084
+ COMPARED.days = allDays.size;
1085
+ COMPARED.changed = new Set([...changedDays].map((key) => key.split("|")[1])).size;
1086
+ return entries.filter((entry) => keep.has(entry));
1087
+ }
759
1088
  /**
760
1089
  * ONE RUN PER COUPLING (0.10.27; owner, 2026-10-21: re-measured history is kept on the server). The batches of a re-measure
761
1090
  * to one key are one run: an id, each batch's number and the run's count ride in every batch, and WorkTrust keeps each batch
@@ -774,13 +1103,15 @@ const historyRunId = (bodies) => {
774
1103
  function historyPlan(door, entries, rebuild = false, { byDay = false, withdrawUnknown = false } = {}) {
775
1104
  const groups = [["claude", entries.filter((entry) => !entrySource.has(entry))], ["codex", entries.filter((entry) => entrySource.get(entry) === "codex")], ...["antigravity", ...DATABASE_CLIENTS, ...EXPORT_CLIENTS].map((client) => [client, entries.filter((entry) => entrySource.get(entry) === client)])];
776
1105
  // Imported entries carry no live commit/session join.
777
- const encodeBatch = (batch, run = null) => JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "import_history", arguments: { ...(rebuild ? { rebuild: true } : {}), ...(withdrawUnknown ? { withdraw_unknown: true } : {}), entries: batch.map(({commits, ...rest}) => { void commits; return rest; }), ...(run ? { run } : {}) } } });
1106
+ const encodeBatch = (batch, run = null, days = null) => JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "import_history", arguments: { ...(rebuild ? { rebuild: true } : {}), ...(withdrawUnknown ? { withdraw_unknown: true } : {}), entries: batch.map(({commits, ...rest}) => { void commits; return rest; }), ...(run ? { run } : {}), ...(days ? { days } : {}) } } });
778
1107
  const plan = groups.flatMap(([source, group]) => { const target = source === "codex" ? codexDoor ?? door : door; return group.length > 0 && target ? batchesOf(group, byDay).map((batch) => ({ source, target, batch, run: null })) : []; });
779
1108
  if (rebuild && !byDay) for (const token of new Set(plan.map((item) => item.target.token))) {
780
1109
  const run = plan.filter((item) => item.target.token === token), id = historyRunId(run.map((item) => encodeBatch(item.batch)));
781
1110
  run.forEach((item, index) => { item.run = { id, index, count: run.length }; });
782
1111
  }
783
- for (const item of plan) item.body = encodeBatch(item.batch, item.run);
1112
+ // 0.10.32: each batch of a staged run names its days' digests (of the AI app's whole plan for the day), only the days it carries.
1113
+ const digestsOf = new Map(groups.map(([source, group]) => [source, dayDigests(group)]));
1114
+ for (const item of plan) item.body = encodeBatch(item.batch, item.run, item.run ? daysOf(item.batch, digestsOf.get(item.source)) : null);
784
1115
  return plan;
785
1116
  }
786
1117
  /**
@@ -789,21 +1120,25 @@ function historyPlan(door, entries, rebuild = false, { byDay = false, withdrawUn
789
1120
  * run's batches again, each AI app on its own, under a new run id and count. `tighter` is a batch WorkTrust answered 413 although
790
1121
  * it fit here: the next cut is half of it, so every piece is smaller than what was refused and a refused batch never goes again.
791
1122
  */
1123
+ /** A batch's own days and their digests, from the AI app's day digests. */
1124
+ const daysOf = (batch, digests) => Object.fromEntries([...new Set(batch.map(dayOf))].sort().filter((day) => digests?.has(day)).map((day) => [day, digests.get(day)]));
792
1125
  const outboxCut = {
793
1126
  fits: (entries) => fits(entries),
794
1127
  split(rpc, source, tighter) {
795
1128
  const args = rpc.params.arguments, pieces = batchesOf(args.entries, EXPORT_CLIENTS.includes(source), tighter ? halfOf(tighter) : BATCH_LIMIT);
796
- return pieces.length < 2 ? [] : pieces.map((batch) => JSON.stringify({ ...rpc, params: { ...rpc.params, arguments: { ...args, entries: batch } } }));
1129
+ return pieces.length < 2 ? [] : pieces.map((batch) => { const { days, ...rest } = args; const own = days && typeof days === "object" ? Object.fromEntries(Object.entries(days).filter(([day]) => batch.some((entry) => dayOf(entry) === day))) : null; return JSON.stringify({ ...rpc, params: { ...rpc.params, arguments: { ...rest, entries: batch, ...(own ? { days: own } : {}) } } }); });
797
1130
  },
798
1131
  replan(batches, tighter) {
799
1132
  const limit = tighter ? halfOf(tighter) : BATCH_LIMIT, bySource = new Map();
800
1133
  for (const { rpc, source } of batches) bySource.set(source, [...(bySource.get(source) ?? []), ...rpc.params.arguments.entries]);
801
- const { run: _run, entries: _entries, ...args } = batches[0].rpc.params.arguments; void _run; void _entries;
1134
+ const { run: _run, entries: _entries, days: _days, ...args } = batches[0].rpc.params.arguments; void _run; void _entries; void _days;
802
1135
  const plan = [...bySource].flatMap(([source, group]) => batchesOf(group, false, limit).map((batch) => ({ source, batch })));
803
1136
  if (plan.length > 1000) return [];
804
- const encode = (batch, run = null) => JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "import_history", arguments: { ...args, entries: batch, ...(run ? { run } : {}) } } });
1137
+ // 0.10.32: the days' digests follow the new cut, each batch naming only the days it carries.
1138
+ const digests = new Map([...bySource].map(([source, group]) => [source, dayDigests(group)]));
1139
+ const encode = (batch, run = null, days = null) => JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "import_history", arguments: { ...args, entries: batch, ...(run ? { run } : {}), ...(days ? { days } : {}) } } });
805
1140
  const id = historyRunId(plan.map(({ batch }) => encode(batch)));
806
- return plan.map(({ source, batch }, index) => ({ source, body: encode(batch, { id, index, count: plan.length }) }));
1141
+ return plan.map(({ source, batch }, index) => ({ source, body: encode(batch, { id, index, count: plan.length }, daysOf(batch, digests.get(source))) }));
807
1142
  },
808
1143
  };
809
1144
  async function sendHistory(door, entries, announce = false, rebuild = false, named = false, { byDay = false, withdrawUnknown = false } = {}) {
@@ -813,6 +1148,9 @@ async function sendHistory(door, entries, announce = false, rebuild = false, nam
813
1148
  // Persist the approved plan before sending, so later batches survive removed source files too.
814
1149
  try { for (const item of plan) queuedHistory(item.target, item.body, item.source); }
815
1150
  catch { console.error("History queue is full, busy or unavailable. Resume the saved batches before retrying this import; nothing was marked complete."); HISTORY_DELIVERY.waiting += entries.length; return 0; }
1151
+ // Every entry is now in the outbox, on disk, and goes again by itself until WorkTrust takes it (0.10.32: what the hook's
1152
+ // late days may count on, so a late day not yet filed never holds a transcript's newer days back).
1153
+ HISTORY_PERSISTED.n += entries.length;
816
1154
  const numbers = new Map(), stopped = new Set();
817
1155
  // TOO LARGE FOR ONE CALL (0.10.29): a batch WorkTrust answered 413, or one an earlier run already cut, is handed after the loop to
818
1156
  // the outbox's cut with the rest of its run, never sent again as it was. `stoppedRuns`: a run with a refused batch sends no more.
@@ -910,11 +1248,51 @@ async function send(door, payload) {
910
1248
  retainMeasurement(payload, entrySource.get(payload));
911
1249
  const body = JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "log_work", arguments: payload } });
912
1250
  const response = await fetch(door.url, { method: "POST", headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${door.token}`, ...clientOf(entrySource.get(payload)), ...proofFor(door.url, body) }, body });
1251
+ // A FAIR-USE LIMIT (0.10.33; owner, 2026-10-28): 429 with Retry-After. Nothing was stored; the line is kept and sent again after the delay.
1252
+ if (response.status === 429) throw Object.assign(new Error("door said 429"), { retryAfter: Math.max(1, Math.min(86_400, Number(response.headers.get("retry-after")) || 60)) });
913
1253
  if (!response.ok) throw new Error(`door said ${response.status}`);
914
1254
  const text = await response.text();
915
1255
  confirmedReceipt(text);
916
1256
  return text;
917
1257
  }
1258
+ /**
1259
+ * ONE LINE PER PROCESS PER DAY (0.10.33; owner, 2026-10-28; docs/FAIR-USE.md §1): a finished headless or scheduled run is folded
1260
+ * into its process's UTC day on disk (history-outbox.mjs) and the day goes as ONE line once it is over and quiet: live while the
1261
+ * door takes it, else through the history outbox; a refusal keeps it. A copy without that module sends each run as before.
1262
+ */
1263
+ const PROCESS = await import("./history-outbox.mjs").then((m) => (m.foldProcessRun ? m : null), () => null);
1264
+ const isProcessRun = (stretch) => Boolean(PROCESS && stretch.timeline && (stretch.session === "headless" || stretch.session === "scheduled"));
1265
+ const processKey = (source, stretch, codex) => { const day = new Date(stretch.to).toISOString().slice(0, 10); return { ref: PROCESS.processRefOf({ source, kind: stretch.session, schedule: stretch.schedule?.ref ?? null, cwd: stretch.cwd, day }), day, source: source ?? "claude", kind: stretch.session, cwd: stretch.cwd ?? null, steered: stretch.steered ?? null, schedule: stretch.schedule ?? null, codex }; };
1266
+ // A RUN KEEPS WHAT RIDES ON ITS STEPS (release 9): its call outcomes and input lengths compacted with the timeline
1267
+ // (minuteCompactRun), and its subagent runs as read (wall-clock stamps), so the day's line carries them as each run's did.
1268
+ const processRun = (id, stretch) => { const compact = PROCESS.minuteCompactRun ? PROCESS.minuteCompactRun(stretch.timeline, stretch.from, stretch.outcomes ?? null, stretch.lengths ?? null) : { timeline: PROCESS.minuteCompact(stretch.timeline, stretch.from), outcomes: null, lengths: null };
1269
+ return { id: `${id}|${stretch.from}`, from: stretch.from, to: stretch.to, offset: stretch.offset, timeline: compact.timeline, outcomes: compact.outcomes, lengths: compact.lengths, agents: stretch.agents?.list ?? null, tokensIn: stretch.tokensIn, tokensOut: stretch.tokensOut, cacheRead: stretch.cacheRead, cacheWrite: stretch.cacheWrite, shapeKnown: stretch.shapeKnown, model: stretch.model ?? null }; };
1270
+ function processPayload(group, part, runs) {
1271
+ const payload = { ...payloadFor({ ...PROCESS.combineRuns(runs), cwd: group.cwd && existsSync(group.cwd) ? group.cwd : null, ref: PROCESS.lineRefOf(group.ref, part), steered: group.steered, session: group.kind, schedule: group.schedule ?? null, derived: null, live: null }), process_ref: group.ref, process_runs: runs.length };
1272
+ if (group.source && group.source !== "claude") entrySource.set(payload, group.source);
1273
+ return payload;
1274
+ }
1275
+ const liveWaiting = () => Date.parse(state.__liveRetryAt ?? "") > Date.now();
1276
+ const liveWait = (seconds) => { state.__liveRetryAt = new Date(Date.now() + seconds * 1000).toISOString(); LIVE_NOTED.n += 1; };
1277
+ const LIVE_NOTED = { n: 0 };
1278
+ async function sendProcessDays() {
1279
+ let n = 0;
1280
+ for (const due of PROCESS ? PROCESS.dueProcessDays(Date.now(), IDLE_BEFORE_SWEEP) : []) {
1281
+ const payload = processPayload(due, due.part, due.runs);
1282
+ if (DRY) { console.log(JSON.stringify({ ...payload, client: due.source, route: "process-day" }, null, 1)); continue; }
1283
+ if (Date.now() - Date.parse(payload.at) > WINDOW_HOURS * 3600_000 - IDLE_BEFORE_SWEEP) {
1284
+ const before = HISTORY_PERSISTED.n;
1285
+ await sendHistory(door, [payload]).catch(() => 0);
1286
+ if (HISTORY_PERSISTED.n > before) { PROCESS.markProcessSent(due.ref, due); n += 1; } else PROCESS.deferProcessDay(due.ref, 300);
1287
+ continue;
1288
+ }
1289
+ if (liveWaiting()) continue;
1290
+ // HELD IS NOT TAKEN (0.10.33): a day WorkTrust holds whole against this part keeps its runs here and asks again later.
1291
+ try { const held = /process_day_held/.test(await send(due.codex ? codexDoor ?? door : door, payload)); if (held) PROCESS.deferProcessDay(due.ref, 21_600); else { PROCESS.markProcessSent(due.ref, due); n += 1; } }
1292
+ catch (error) { PROCESS.deferProcessDay(due.ref, error.retryAfter ?? 300); if (error.retryAfter) liveWait(error.retryAfter); }
1293
+ }
1294
+ return n;
1295
+ }
918
1296
 
919
1297
  /**
920
1298
  * THE MONTHS ALREADY ON THIS MACHINE, AS HISTORY.
@@ -931,7 +1309,7 @@ async function send(door, payload) {
931
1309
  * average of one. Younger than the live window is left alone — that is `log_work`'s half.
932
1310
  */
933
1311
  function historyEntries(floor = null, all = false) {
934
- const entries = [];
1312
+ const entries = [], processGroups = new Map();
935
1313
  for (const { file } of allTranscripts()) {
936
1314
  const lines = parsedLines(file);
937
1315
  if (!lines) continue;
@@ -957,7 +1335,15 @@ function historyEntries(floor = null, all = false) {
957
1335
  // The live window belongs to the hook: a stretch that log_work may still report is not history.
958
1336
  if (!all && Date.now() - last < WINDOW_HOURS * 3600_000) continue;
959
1337
  // The same measure as a live day (seconds, layers, tokens, turns, model); history differs only in the door it goes through.
960
- const entry = payloadFor({ ...measure(cwd, messages), ref: stretchRef(file, day), steered, session, agents: agents.get(day) ?? null, live: liveOf(file, day) });
1338
+ const stretch = { ...measure(cwd, messages, session), ref: stretchRef(file, day), steered, session, agents: agents.get(day) ?? null, live: liveOf(file, day) };
1339
+ // ONE LINE PER PROCESS PER DAY (0.10.33), as live: an autonomous run joins its process's day, sent below as one line.
1340
+ if (isProcessRun(stretch)) {
1341
+ const key = processKey(CODEX_ROLLOUT.test(file) ? "codex" : ANTIGRAVITY_TRANSCRIPT.test(file) ? "antigravity" : DATABASE_SESSION.test(file) ? file.slice(0, file.indexOf(":")) : null, stretch, false);
1342
+ const group = processGroups.get(key.ref) ?? { ...key, runs: [], file, from: stretch.from, folder: cwd ? String(cwd).split(/[\\/]/).filter(Boolean).pop() ?? null : null };
1343
+ group.runs.push(processRun(file, stretch)); processGroups.set(key.ref, group);
1344
+ continue;
1345
+ }
1346
+ const entry = payloadFor(stretch);
961
1347
  if (CODEX_ROLLOUT.test(file)) entrySource.set(entry, "codex");
962
1348
  if (ANTIGRAVITY_TRANSCRIPT.test(file)) entrySource.set(entry, "antigravity");
963
1349
  if (DATABASE_SESSION.test(file)) entrySource.set(entry, file.slice(0, file.indexOf(":")));
@@ -965,6 +1351,7 @@ function historyEntries(floor = null, all = false) {
965
1351
  entries.push(entry);
966
1352
  }
967
1353
  }
1354
+ for (const group of processGroups.values()) PROCESS.processParts(group.runs).forEach((runs, i) => { const entry = processPayload(group, i + 1, runs); entryFile.set(entry, { file: group.file, from: group.from, folder: group.folder }); entries.push(entry); });
968
1355
  const seen = new Set(); // ONE SESSION FILE IN TWO PROJECT FOLDERS (a copied folder) IS ONE STRETCH (0.6.16, audit M6), as live and preserve count it
969
1356
  return entries.filter((entry) => !seen.has(entry.stretch_ref) && seen.add(entry.stretch_ref)).sort((a, b) => a.at.localeCompare(b.at));
970
1357
  }
@@ -1350,7 +1737,7 @@ if (flag("history")) {
1350
1737
  const sizes = plan.map((item) => ({ lines: item.batch.length, bytes: Buffer.byteLength(item.body, "utf8"), timeline_bytes: item.batch.reduce((n, entry) => n + timelineOfEntry(entry), 0) }));
1351
1738
  const kinds = { interactive: 0, headless: 0, scheduled: 0, unknown: 0 };
1352
1739
  for (const entry of entries) kinds[entry.session_kind ?? "unknown"] += 1;
1353
- console.log(JSON.stringify({ sessions: new Set(entries.map((entry) => entryFile.get(entry)?.file)).size, days: new Set(entries.map((entry) => entry.at.slice(0, 10))).size, first: months[0] ?? null, last: months.at(-1) ?? null, ...(fromExports ? { exports: fromExports.reports } : {}), ...(fromFolder ? { folder: fromFolder.report } : {}), clients,
1740
+ console.log(JSON.stringify({ sessions: new Set(entries.map((entry) => entryFile.get(entry)?.file)).size, unreadable: READ_FAILURES.n, days: new Set(entries.map((entry) => entry.at.slice(0, 10))).size, first: months[0] ?? null, last: months.at(-1) ?? null, ...(fromExports ? { exports: fromExports.reports } : {}), ...(fromFolder ? { folder: fromFolder.report } : {}), clients,
1354
1741
  batches: { count: sizes.length, largest_bytes: Math.max(0, ...sizes.map((one) => one.bytes)), largest_timeline_bytes: Math.max(0, ...sizes.map((one) => one.timeline_bytes)), largest_lines: Math.max(0, ...sizes.map((one) => one.lines)), each: sizes }, session_kinds: kinds }));
1355
1742
  return await exitFlushed(0);
1356
1743
  }
@@ -1361,17 +1748,34 @@ if (flag("history")) {
1361
1748
  console.log(`\ndry run — nothing sent. Add --history${flag("rebuild") ? " --rebuild" : ""} without --dry-run to ${flag("rebuild") ? "rebuild" : "import"} them.`);
1362
1749
  return await exitFlushed(0);
1363
1750
  }
1751
+ // ONLY THE DAYS THAT CHANGED (0.10.32): a re-measure of this computer's own sessions asks the door first; an export, a copied
1752
+ // folder and a plain history never do. Not known is every day.
1753
+ const staged = flag("rebuild") && !fromExports && !fromFolder;
1754
+ const sending = staged ? await changedOnly(entries) : entries;
1755
+ if (staged && COMPARED.asked && COMPARED.days > COMPARED.changed) console.log(sending.length === 0 ? `nothing changed: WorkTrust holds all ${COMPARED.days} days as this computer measures them` : `${COMPARED.days - COMPARED.changed} of ${COMPARED.days} days unchanged on WorkTrust; sending the ${COMPARED.changed} that changed`);
1364
1756
  // A batch the door refused is a failure the caller must see, not a quiet zero.
1365
- const accepted = await sendHistory(door, entries, true, flag("rebuild"), Boolean(fromFolder), fromExports ? { byDay: true, withdrawUnknown: flag("withdraw-unknown") } : {});
1757
+ const accepted = sending.length === 0 ? 0 : await sendHistory(door, sending, true, flag("rebuild"), Boolean(fromFolder), fromExports ? { byDay: true, withdrawUnknown: flag("withdraw-unknown") } : {});
1366
1758
  // An export that arrived whole is remembered by its digest (never by a conversation), so the bare command offers it no more.
1367
1759
  if (fromExports && accepted === entries.length) for (const report of fromExports.reports) rememberExport(report.digest, { vendor: report.vendor, sent: true, at: new Date().toISOString(), conversations: report.conversations });
1368
1760
  // ONE LINE FOR THE COMMAND TO ASK WITH (2026-10-10): what the door replaced, how many AI-app lines lie on the export's days, how many it withdrew.
1369
- if (fromExports) console.log(JSON.stringify({ export_sent: accepted, of: entries.length, ...RECEIPTS })); else console.log(JSON.stringify({ history_sent: accepted, of: entries.length, ...RECEIPTS, delivery: HISTORY_DELIVERY })); // the same line for a history send: what the door did with it, and WorkTrust's measuredSeconds for what it stored
1370
- return await exitFlushed(accepted === entries.length ? 0 : 1);
1761
+ if (!fromExports && !fromFolder) await settleReadFailures(door ?? codexDoor); // 0.10.32: a transcript that could not be read is counted and told
1762
+ if (fromExports) console.log(JSON.stringify({ export_sent: accepted, of: entries.length, ...RECEIPTS })); else console.log(JSON.stringify({ history_sent: accepted, of: sending.length, unreadable: READ_FAILURES.n, ...RECEIPTS, delivery: HISTORY_DELIVERY, ...(staged ? { days: { compared: COMPARED.asked ? COMPARED.days : null, changed: COMPARED.asked ? COMPARED.changed : null }, queued: QUEUED.at } : {}) })); // the same line for a history send: what the door did with it, and WorkTrust's measuredSeconds for what it stored
1763
+ return await exitFlushed(accepted === sending.length ? 0 : 1);
1371
1764
  }
1372
1765
 
1373
1766
  const input = hookInput();
1374
1767
  const state = readState();
1768
+ if (input?.hook_event_name === "Stop" && !process.env.WORKTRUST_STOP_CHILD) {
1769
+ if (Date.now() - (Date.parse(state.__stopSweep ?? "") || 0) < STOP_SWEEP_MS) return await exitFlushed(0);
1770
+ if (!DRY) { state.__stopSweep = new Date().toISOString(); writeState(state); }
1771
+ // The sweep itself in a detached copy, so the answer that fired Stop is not held while a large transcript is read.
1772
+ if (!DRY && !process.env.WORKTRUST_STOP_INLINE && existsSync(join(RELEASE_DIR, "log-session.mjs"))) {
1773
+ try {
1774
+ spawn(moduleExecutable(RELEASE_DIR), moduleArgs(RELEASE_DIR, "log-session.mjs", []), { detached: true, stdio: ["ignore", "ignore", "ignore"], env: { ...process.env, WORKTRUST_STOP_CHILD: "1", WORKTRUST_HOOK_INPUT: JSON.stringify(input) } }).unref();
1775
+ return await exitFlushed(0);
1776
+ } catch { /* no detached copy: the sweep runs here */ }
1777
+ }
1778
+ }
1375
1779
  const work = [];
1376
1780
  if (input?.transcript_path && existsSync(input.transcript_path) && !excluded(input.transcript_path)) {
1377
1781
  work.push({ id: String(input.session_id ?? input.transcript_path), file: input.transcript_path });
@@ -1395,7 +1799,14 @@ if (floorOf(state, "__from") === null) {
1395
1799
  if (!DRY) writeState(state);
1396
1800
  }
1397
1801
  const floor = floorOf(state, "__from");
1398
- let sent = 0, filed = 0;
1802
+ // THE TIMER THAT STARTED THIS RUN (0.10.32): read once, for the session the hook was called for, and kept beside its watermark, so a
1803
+ // day sent by a later run (the day was not finished) still carries it. A run no timer started reads none.
1804
+ let scheduleNoted = false;
1805
+ if (work.length > 0 && input?.transcript_path && work[0].file === input.transcript_path) {
1806
+ const found = timerSchedule(), secret = found ? repoCodeSecret() : null, timer = found && secret ? { cadence: found.cadence, ref: createHmac("sha256", secret).update(`worktrust-schedule-ref|${found.ref}`).digest("base64url") } : null; // the label's hash keyed with this computer's secret, as repo_code (L4)
1807
+ if (timer &&state[work[0].id]?.schedule?.ref !== timer.ref) { state[work[0].id] = { ...(state[work[0].id] ?? {}), schedule: timer }; scheduleNoted = true; }
1808
+ }
1809
+ let sent = 0, filed = 0, folded = 0, positioned = false;
1399
1810
  const late = []; // measured days the live door will no longer take: they are not dropped, they are filed
1400
1811
  const heldBack = new Map(); // per transcript with a late day: its state before that day, put back unless history took every late day
1401
1812
  for (const entry of work) {
@@ -1404,7 +1815,8 @@ for (const entry of work) {
1404
1815
  const since = mark !== null && floor !== null ? Math.max(mark, floor) : mark ?? floor;
1405
1816
  // Nothing written since we last read this one: it is not opened. This keeps a sweep over every transcript to one `stat` each.
1406
1817
  if (since !== null && entry.writtenAt && entry.writtenAt <= since) continue;
1407
- const stretches = stretchesOf(entry.file, since);
1818
+ // THE READ POSITION (0.10.32, at stretchesOf): a Claude Code transcript is read from where the last run left it, when nothing says otherwise.
1819
+ const stretches = stretchesOf(entry.file, since, state[entry.id]?.pos ?? null);
1408
1820
  // THE WATERMARK ADVANCES OVER WHAT WAS ACCEPTED, AND STOPS AT THE FIRST REFUSAL. A door that is
1409
1821
  // down must never cost the person a day, so the loop breaks rather than skipping ahead.
1410
1822
  let through = null, before, reported = state[entry.id]?.reported;
@@ -1413,31 +1825,53 @@ for (const entry of work) {
1413
1825
  // every day but today, on whichever run comes first — it no longer waits for its own end.
1414
1826
  if (Date.now() - stretch.to < IDLE_BEFORE_SWEEP) break;
1415
1827
  const total = grownBy(stretch, reported);
1416
- const payload = payloadFor(stretch);
1828
+ // THE TIMER'S SCHEDULE (0.10.32) travels with the run, so a scheduled process-day is its own process and its line names it.
1829
+ const timed = { ...stretch, schedule: state[entry.id]?.schedule ?? null };
1830
+ // ONE LINE PER PROCESS PER DAY (0.10.33): a finished autonomous run waits on disk for its day's line; the mark passes it once it is there.
1831
+ if (isProcessRun(timed)) {
1832
+ if (DRY) { console.log(JSON.stringify({ process_run: true, session_kind: timed.session, stretch_ref: timed.ref ?? null, at: new Date(timed.to).toISOString() })); through = timed.to; reported = total; continue; }
1833
+ try { PROCESS.foldProcessRun(processKey(LIVE_SOURCE.exec(String(entry.id))?.[1], timed, String(entry.id).startsWith("codex:")), processRun(entry.id, timed)); through = timed.to; reported = total; folded += 1; continue; } catch { break; }
1834
+ }
1835
+ const payload = payloadFor(timed);
1417
1836
  const source = LIVE_SOURCE.exec(String(entry.id))?.[1];
1418
1837
  if (source) entrySource.set(payload, source);
1419
1838
  if (DRY) { console.log(JSON.stringify({ ...payload, client: source ?? "claude", route: Date.now() - stretch.to > WINDOW_HOURS * 3600_000 ? "history" : "log_work" }, null, 1)); through = stretch.to; reported = total; continue; }
1420
1839
  // MEASURED TIME IS NEVER DROPPED. The live door refuses anything older than two days, which is right, and used to mean a machine closed
1421
1840
  // for a fortnight lost the fortnight. The same numbers go through the history door instead, standing as history stands. Late is not untrue.
1422
1841
  if (Date.now() - stretch.to > WINDOW_HOURS * 3600_000) { before ??= { mark: through === null ? state[entry.id] : { ...state[entry.id], through: new Date(through).toISOString() } }; late.push(payload); through = stretch.to; reported = total; continue; }
1842
+ // A LIMIT SAID WHEN (0.10.33): while WorkTrust asked to wait, nothing is sent; the mark stays and the next run sends it.
1843
+ if (liveWaiting()) break;
1423
1844
  try { await send(doorFor(entry), payload); through = stretch.to; reported = total; sent += 1; } catch (error) {
1845
+ if (error.retryAfter) liveWait(error.retryAfter);
1424
1846
  if (process.env.WORKTRUST_HOOK_DEBUG) console.error(`worktrust: ${error.message}`);
1425
1847
  break;
1426
1848
  }
1427
1849
  }
1428
1850
  if (before && !heldBack.has(entry.id)) heldBack.set(entry.id, before.mark);
1429
- if (through !== null) state[entry.id] = { through: new Date(through).toISOString(), ...(reported ? { reported } : {}) };
1851
+ // The position is cut for the watermark kept beside it: the new one, or the one this run read under.
1852
+ const pos = stretches.positionAt?.(through ?? since) ?? null;
1853
+ if (through !== null) state[entry.id] = { through: new Date(through).toISOString(), ...(reported ? { reported } : {}), ...(state[entry.id]?.schedule ? { schedule: state[entry.id].schedule } : {}), ...(pos ? { pos } : {}) };
1854
+ else if (pos && !DRY && canonical(pos) !== canonical(state[entry.id]?.pos ?? null)) { state[entry.id] = { ...(state[entry.id] ?? {}), pos }; positioned = true; }
1430
1855
  }
1431
1856
  // The history door, in batches of two hundred as `--history` sends them. A resend FILLS a line that was missing something, never a twin.
1432
1857
  if (!DRY && late.length > 0) {
1433
1858
  try { filed = await sendHistory(door, late); } catch (error) { if (process.env.WORKTRUST_HOOK_DEBUG) console.error(`worktrust: ${error.message}`); }
1434
- // A LATE DAY THE HISTORY DOOR REFUSED IS NOT PASSED: its transcript's mark goes back to before it, so the next run sends it again.
1435
- if (filed < late.length) for (const [id, mark] of heldBack) { if (mark) state[id] = mark; else delete state[id]; }
1859
+ // A LATE DAY THAT IS NOWHERE IS NOT PASSED: when the outbox could not keep it, its transcript's mark goes back to before it, so
1860
+ // the next run reads it again. A late day the outbox KEEPS is passed (0.10.32): the outbox sends it again by itself until
1861
+ // WorkTrust takes it, and putting the mark back made every newer day of a long session late too, a day at a time, so a
1862
+ // session whose first late day waited (the owner's, from 2026-10-18) never moved again.
1863
+ if (filed < late.length && HISTORY_PERSISTED.n < late.length) for (const [id, mark] of heldBack) { if (mark) state[id] = mark; else delete state[id]; }
1436
1864
  }
1437
- if (!DRY && (sent > 0 || filed > 0)) writeState(state);
1438
- // What an earlier history run left on this computer because the server never received it: a few batches a run, by itself; not in a
1439
- // run that just sent late days itself (a batch it just tried waits for the next run, as it always did).
1865
+ // THE PROCESS-DAYS THAT ARE OVER (0.10.33): one line each; what WorkTrust does not take now stays on disk for the next run.
1866
+ await sendProcessDays();
1867
+ if (!DRY && (sent > 0 || filed > 0 || folded > 0 || scheduleNoted || positioned || LIVE_NOTED.n > 0 || (late.length > 0 && HISTORY_PERSISTED.n >= late.length))) writeState(state);
1868
+ // What an earlier run left in the outbox because the server never received it: a few batches a run, by itself; not in a run
1869
+ // that just sent late days itself (a batch it just tried waits for the next run). Since 0.10.32 the mark passes a late day the
1870
+ // outbox keeps, so the next run has no late day of its own and asks; before, a long session always had one and never asked.
1440
1871
  if (!DRY && late.length === 0) await resendPending(4);
1872
+ // A transcript that could not be read is counted here and told as a count (0.10.32), never skipped in silence.
1873
+ if (!DRY) await settleReadFailures(door, state);
1874
+ if (process.env.WORKTRUST_HOOK_DEBUG) console.error(`worktrust: read ${READ_BYTES.n} transcript bytes`);
1441
1875
 
1442
1876
  // The counter, at most once a day, detached: reading every transcript takes longer than a hook
1443
1877
  // may hold a session's exit, and its result waits for a click either way. `--stage` and never