notesoup 0.53.1 → 0.54.0

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 (153) hide show
  1. package/README.md +2 -2
  2. package/dist/plugins/claude/src/helper.js +2 -1
  3. package/dist/plugins/claude/src/history.js +2 -3
  4. package/dist/plugins/claude/src/thresholds.js +4 -2
  5. package/dist/plugins/codex/src/hooks.js +1 -1
  6. package/dist/plugins/codex/src/identity.d.ts +1 -1
  7. package/dist/plugins/codex/src/identity.js +3 -3
  8. package/dist/plugins/codex/src/mcp.js +2 -1
  9. package/dist/plugins/codex/src/text.d.ts +1 -1
  10. package/dist/plugins/codex/src/text.js +2 -2
  11. package/dist/plugins/dsh/engine.js +2 -2
  12. package/dist/plugins/dsh/tools.js +8 -7
  13. package/dist/plugins/dsh/window.d.ts +1 -0
  14. package/dist/plugins/dsh/window.js +7 -3
  15. package/dist/plugins/pi/entries.d.ts +1 -0
  16. package/dist/plugins/pi/entries.js +1 -0
  17. package/dist/plugins/pi/history-tools.d.ts +1 -1
  18. package/dist/plugins/pi/history-tools.js +4 -4
  19. package/dist/plugins/pi/marker.d.ts +9 -0
  20. package/dist/plugins/pi/marker.js +36 -0
  21. package/dist/plugins/pi/notes/tools.js +8 -6
  22. package/dist/plugins/pi/render.js +7 -4
  23. package/dist/plugins/pi/reset/artifacts.d.ts +2 -2
  24. package/dist/plugins/pi/reset/artifacts.js +2 -2
  25. package/dist/plugins/pi/runtime.js +11 -8
  26. package/dist/plugins/pi/text.d.ts +2 -0
  27. package/dist/plugins/pi/text.js +4 -0
  28. package/dist/plugins/pi/window.js +21 -7
  29. package/dist/src/boot/render.js +7 -7
  30. package/dist/src/boot/snapshot.d.ts +7 -7
  31. package/dist/src/boot/snapshot.js +17 -17
  32. package/dist/src/boot/text.d.ts +2 -2
  33. package/dist/src/boot/text.js +7 -5
  34. package/dist/src/dream/git.d.ts +2 -11
  35. package/dist/src/dream/git.js +6 -23
  36. package/dist/src/dream/lock.d.ts +2 -2
  37. package/dist/src/dream/lock.js +3 -3
  38. package/dist/src/index.d.ts +2 -1
  39. package/dist/src/index.js +3 -2
  40. package/dist/src/notes/address.d.ts +8 -8
  41. package/dist/src/notes/address.js +13 -13
  42. package/dist/src/notes/errors.d.ts +1 -1
  43. package/dist/src/notes/frontmatter.js +2 -2
  44. package/dist/src/notes/index.d.ts +1 -1
  45. package/dist/src/notes/index.js +1 -1
  46. package/dist/src/notes/paths.d.ts +6 -6
  47. package/dist/src/notes/paths.js +9 -9
  48. package/dist/src/notes/{project-home.d.ts → project-namespace.d.ts} +7 -7
  49. package/dist/src/notes/{project-home.js → project-namespace.js} +67 -25
  50. package/dist/src/notes/store.d.ts +4 -4
  51. package/dist/src/notes/store.js +55 -52
  52. package/dist/src/tools/dream.d.ts +21 -3
  53. package/dist/src/tools/dream.js +54 -16
  54. package/dist/src/tools/notes-context.d.ts +10 -0
  55. package/dist/src/tools/notes-context.js +27 -0
  56. package/dist/src/tools/notes.d.ts +69 -20
  57. package/dist/src/tools/notes.js +40 -31
  58. package/dist/test/agent-loop.test.js +8 -8
  59. package/dist/test/boot.integration.test.js +5 -5
  60. package/dist/test/claude-history.test.js +26 -0
  61. package/dist/test/claude-mod.test.js +257 -86
  62. package/dist/test/codex-plugin.test.js +18 -1
  63. package/dist/test/context-edit-budget.test.js +121 -0
  64. package/dist/test/dependencies.test.js +71 -4
  65. package/dist/test/dream-tools.test.js +153 -8
  66. package/dist/test/dsh.test.js +62 -15
  67. package/dist/test/helpers/dsh.d.ts +4 -1
  68. package/dist/test/helpers/dsh.js +20 -4
  69. package/dist/test/helpers/extension.d.ts +5 -0
  70. package/dist/test/helpers/extension.js +33 -2
  71. package/dist/test/host-neutral.test.js +17 -15
  72. package/dist/test/marker.test.js +78 -0
  73. package/dist/test/model-facing-text.test.js +12 -8
  74. package/dist/test/notes-cwd.test.d.ts +1 -0
  75. package/dist/test/notes-cwd.test.js +114 -0
  76. package/dist/test/notes-library.test.js +4 -4
  77. package/dist/test/notes-query-namespaces.test.d.ts +1 -0
  78. package/dist/test/{notes-query-homes.test.js → notes-query-namespaces.test.js} +13 -13
  79. package/dist/test/notes.integration.test.js +72 -23
  80. package/dist/test/notes.test.js +50 -20
  81. package/dist/test/pi-render.test.js +33 -1
  82. package/dist/test/project-namespace.test.d.ts +1 -0
  83. package/dist/test/project-namespace.test.js +324 -0
  84. package/dist/test/reset-lifecycle.test.js +12 -0
  85. package/docs/architecture.md +13 -8
  86. package/docs/claude-code-mod.md +7 -7
  87. package/docs/codex.md +4 -4
  88. package/docs/dsh.md +8 -8
  89. package/docs/notes-library.md +9 -5
  90. package/docs/skills.md +3 -3
  91. package/docs/tool-results.md +6 -2
  92. package/package.json +4 -6
  93. package/playbook.md +6 -6
  94. package/plugins/claude/.claude-plugin/plugin.json +1 -1
  95. package/plugins/claude/dist/claude/helper.js +681 -617
  96. package/plugins/claude/hooks/register.ts +61 -65
  97. package/plugins/claude/playbook.md +6 -6
  98. package/plugins/claude/skills/memory/SKILL.md +4 -4
  99. package/plugins/claude/src/helper.ts +3 -2
  100. package/plugins/claude/src/history.ts +2 -3
  101. package/plugins/claude/src/thresholds.ts +4 -2
  102. package/plugins/codex/README.md +1 -1
  103. package/plugins/codex/dist/codex/notesoup.js +877 -815
  104. package/plugins/codex/src/hooks.ts +1 -1
  105. package/plugins/codex/src/identity.ts +3 -3
  106. package/plugins/codex/src/mcp.ts +2 -1
  107. package/plugins/codex/src/text.ts +2 -2
  108. package/plugins/dsh/engine.ts +2 -2
  109. package/plugins/dsh/tools.ts +8 -7
  110. package/plugins/dsh/window.ts +8 -3
  111. package/plugins/paseo/OVERVIEW.md +4 -4
  112. package/plugins/paseo/README.md +1 -1
  113. package/plugins/paseo/client/brain-empty-card.tsx +3 -4
  114. package/plugins/paseo/client/markdown.tsx +179 -0
  115. package/plugins/paseo/client/notes-op-card.tsx +35 -34
  116. package/plugins/paseo/client/notes-op-transformer.ts +17 -68
  117. package/plugins/paseo/client/paper-card.tsx +8 -19
  118. package/plugins/paseo/client/paper-palette.ts +40 -0
  119. package/plugins/paseo/client/sticky-card.tsx +6 -4
  120. package/plugins/paseo/package.json +3 -0
  121. package/plugins/paseo/paseo-plugin.json +2 -2
  122. package/plugins/paseo/shared/notes-op.ts +13 -9
  123. package/plugins/paseo/test/notes-op.test.ts +52 -73
  124. package/plugins/paseo/test/paper-palette.test.ts +54 -0
  125. package/plugins/pi/entries.ts +1 -0
  126. package/plugins/pi/history-tools.ts +4 -4
  127. package/plugins/pi/marker.ts +39 -0
  128. package/plugins/pi/notes/tools.ts +8 -6
  129. package/plugins/pi/render.ts +6 -4
  130. package/plugins/pi/reset/artifacts.ts +2 -2
  131. package/plugins/pi/runtime.ts +9 -8
  132. package/plugins/pi/text.ts +5 -0
  133. package/plugins/pi/window.ts +22 -7
  134. package/skills/memory/SKILL.md +4 -4
  135. package/src/boot/render.ts +8 -8
  136. package/src/boot/snapshot.ts +20 -20
  137. package/src/boot/text.ts +7 -5
  138. package/src/dream/git.ts +7 -24
  139. package/src/dream/lock.ts +3 -3
  140. package/src/index.ts +4 -3
  141. package/src/notes/address.ts +13 -13
  142. package/src/notes/errors.ts +1 -1
  143. package/src/notes/frontmatter.ts +2 -2
  144. package/src/notes/index.ts +1 -1
  145. package/src/notes/paths.ts +9 -9
  146. package/src/notes/{project-home.ts → project-namespace.ts} +59 -28
  147. package/src/notes/store.ts +61 -58
  148. package/src/tools/dream.ts +63 -16
  149. package/src/tools/notes-context.ts +29 -0
  150. package/src/tools/notes.ts +39 -29
  151. package/dist/test/project-home.test.js +0 -134
  152. /package/dist/test/{notes-query-homes.test.d.ts → context-edit-budget.test.d.ts} +0 -0
  153. /package/dist/test/{project-home.test.d.ts → marker.test.d.ts} +0 -0
@@ -13,10 +13,19 @@ import { PROTOCOL_WIPE_SENTENCE, renderProtocolBlock } from "../src/boot/text.js
13
13
  import { CLAUDE_WARNING_PROMPT } from "../plugins/claude/src/text.js";
14
14
  import { COUNTDOWN_LINE, FORCED_CONTINUATIONS, GUIDANCE_CLOSE_TAG, NORMAL_CONTINUATIONS, warningNotice } from "../src/budget/text.js";
15
15
  import { createNotesStore } from "../src/notes/index.js";
16
- import { resolveProjectHome } from "../src/notes/project-home.js";
16
+ import { resolveProjectNamespace } from "../src/notes/project-namespace.js";
17
17
  import { notesIdentityFromPi } from "../plugins/pi/notes/adapter.js";
18
18
  import { context, manager } from "./helpers/extension.js";
19
19
  const identity = { home: mkdtempSync(join(tmpdir(), "claude-mod-")), sessionId: "session", cwd: process.cwd(), agent: "claude", model: "claude" };
20
+ /** Every fake completion satisfies the host contract; success is explicit, never an omitted reason. */
21
+ function turnComplete(overrides = {}) {
22
+ const reason = overrides.reason ?? "answer";
23
+ return {
24
+ answer: reason === "answer" ? "done" : "", durationMs: 1, turnId: "turn-1",
25
+ ...overrides, reason, isAborted: reason === "aborted",
26
+ ...(reason === "refusal" ? { refusal: overrides.refusal ?? { category: null, explanation: null } } : {}),
27
+ };
28
+ }
20
29
  async function callTool(tool, params) {
21
30
  return await dispatch({ op: "tool", tool, identity, params });
22
31
  }
@@ -55,7 +64,7 @@ function fakeEngine(home) {
55
64
  env: { get: async (name) => name === "NOTESOUP_HOME" ? home : undefined },
56
65
  session: { id: async () => "hook-session", cwd: async () => home, model: async () => "claude", messages: async () => [], usage: async () => ({ context: {} }), compact: async () => ({ skip: "test default" }) },
57
66
  settings: { read: async () => ({}) },
58
- command: { register: async (spec) => ({ command: spec.name }) },
67
+ command: { run: async () => ({}), register: async (spec) => ({ command: spec.name }) },
59
68
  clock: { after: () => { throw new Error("test has no pending manual timer"); } },
60
69
  prompt: { submit: async () => undefined },
61
70
  tool: { register: async (spec) => { registered.push(structuredClone(spec)); return { tool: `mcp__notesoup__${spec.name}` }; } },
@@ -107,6 +116,20 @@ test("Claude helper delivers only shared rendered text and success", async () =>
107
116
  assert.deepEqual(Object.keys(missing).sort(), ["ok", "text"]);
108
117
  assert.match(missing.text, /^error: not_found: /, "the refusal renders as the shared error line");
109
118
  });
119
+ test("Claude resolves relative cwd against the request identity, not the helper process", async (t) => {
120
+ const base = join(identity.home, "base"), other = join(identity.home, "other");
121
+ mkdirSync(base);
122
+ mkdirSync(other);
123
+ t.after(() => { rmSync(base, { recursive: true, force: true }); rmSync(other, { recursive: true, force: true }); });
124
+ const requestIdentity = { ...identity, cwd: base };
125
+ const written = await dispatch({ op: "tool", tool: "notes_write", identity: requestIdentity, params: { address: "@project/selected", content: "from Claude", cwd: "../other" } });
126
+ assert.equal(written.ok, true, written.text);
127
+ const project = resolveProjectNamespace(other, identity.home);
128
+ assert.match(readFileSync(join(project.dir, "selected.md"), "utf8"), /from Claude/);
129
+ const read = await dispatch({ op: "tool", tool: "notes_read", identity: requestIdentity, params: { address: "@project/selected", cwd: "../other" } });
130
+ assert.equal(read.ok, true, read.text);
131
+ assert.match(read.text, /from Claude/);
132
+ });
110
133
  test("Claude helper rejects malformed or unknown requests", async () => {
111
134
  const malformed = JSON.parse(await handleLine("not json"));
112
135
  assert.equal(malformed.ok, false);
@@ -172,7 +195,7 @@ function limitPoints(facts) {
172
195
  const limits = resolveClaudeLimits(facts);
173
196
  return { source: limits.source, unit: limits.unit, autoCompact: limits.autoCompact, native: limits.native, reset: limits.reset, warn: limits.warn };
174
197
  }
175
- // Claude compacts at min(floor((W-20000)*pct/100), W-20000-13000); notesoup resets 8192 below
198
+ // Claude compacts at min(floor((W-20000)*pct/100), W-20000-13000); warning math uses the old reset intermediate 8192 below
176
199
  // that and warns 16384 earlier. These are the numbers, not prose.
177
200
  const CLAUDE_200K = { native: 167_000, reset: 158_808, warn: 142_424 };
178
201
  test("Claude limits derive from Claude's own auto-compact point, never a fixed percent", () => {
@@ -192,8 +215,18 @@ test("Claude limits derive from Claude's own auto-compact point, never a fixed p
192
215
  env: { autoCompactWindow: "400000" },
193
216
  settings: { autoCompactWindow: 500_000, modelSettings: { "claude-opus-4-6": { autoCompactWindow: 300_000 } } },
194
217
  }).native, 267_000, "the smallest configured window wins");
218
+ for (const [pctOverride, native, warn] of [["65", 117_000, 92_424], ["75", 135_000, 110_424]]) {
219
+ assert.equal(resolveClaudeLimits({ ...fromBreakdown, env: { pctOverride } }).warn, warn);
220
+ const fallback = resolveClaudeLimits({ usage: { window: 200_000 }, env: { pctOverride, autoCompactWindow: "200000" } });
221
+ assert.equal(fallback.source, "window");
222
+ assert.equal(fallback.native, native);
223
+ assert.equal(fallback.warn, warn);
224
+ }
225
+ for (const pctOverride of ["0", "101", "abc", "", "96"])
226
+ assert.equal(resolveClaudeLimits({ usage: { window: 200_000 }, env: { pctOverride } }).native, 167_000);
195
227
  // Auto-compact off: the point moves to the hard window, and the margins stay.
196
228
  const offSettings = { usage: { tokens: 171_808, window: 200_000 }, settings: { autoCompactEnabled: false } };
229
+ assert.deepEqual(limitPoints({ ...offSettings, env: { pctOverride: "65" } }), { source: "window", unit: "tokens", autoCompact: false, native: 180_000, reset: 171_808, warn: 155_424 }, "disabled auto-compaction ignores pct");
197
230
  assert.deepEqual(limitPoints(offSettings), { source: "window", unit: "tokens", autoCompact: false, native: 180_000, reset: 171_808, warn: 155_424 });
198
231
  assert.deepEqual(limitPoints({ usage: { window: 200_000 }, env: { disableAutoCompact: "1" } }), { source: "window", unit: "tokens", autoCompact: false, native: 180_000, reset: 171_808, warn: 155_424 });
199
232
  assert.deepEqual(limitPoints({ usage: { window: 200_000 }, breakdown: { isAutoCompactEnabled: false } }), { source: "window", unit: "tokens", autoCompact: false, native: 180_000, reset: 171_808, warn: 155_424 });
@@ -350,10 +383,9 @@ test("Claude Mod with NOTESOUP_DISABLED registers nothing and passes every event
350
383
  const native = { skip: "engine default" };
351
384
  const compact = await handlers.get("session.compact")(host, { trigger: "auto" }, async () => native);
352
385
  assert.equal(compact, native, "native compaction runs exactly as if the mod were absent");
353
- const completed = await handlers.get("turn.complete")(host, {}, async () => ({ done: true }));
386
+ const completed = await handlers.get("turn.complete")(host, turnComplete(), async () => ({ done: true }));
354
387
  assert.deepEqual(completed, { done: true }, "turn.complete passes through without a reset");
355
- const measured = await handlers.get("session.measure")(host, {}, async () => ({ marks: true }));
356
- assert.deepEqual(measured, { marks: true }, "session.measure passes through without a reset");
388
+ assert.equal(handlers.has("session.measure"), false, "measure has no reset hook");
357
389
  assert.equal(helperCalls, 0, "the helper never runs while disabled");
358
390
  const event = { text: "continue", context: ["upstream"] };
359
391
  assert.equal(await handlers.get("prompt.submit")(host, event, async (e) => e), event);
@@ -476,14 +508,15 @@ test("Claude Mod hook delivers the helper's shared text, and marks only a refusa
476
508
  assert.equal(crashed.isError, true);
477
509
  assert.match(String(crashed.result), /^notesoup: helper execution failed: Error: spawn timed out after 10000ms$/, "the host's own reason survives the catch");
478
510
  });
479
- test("Claude Mod resets at Claude's resolved point after the turn, retains one fresh boot and deduplicates measure", async (t) => {
511
+ test("Claude Mod crosses old reset thresholds without clearing and retains one fresh boot on request", async (t) => {
480
512
  const home = mkdtempSync(join(tmpdir(), "claude-reset-home-"));
481
513
  t.after(() => rmSync(home, { recursive: true, force: true }));
482
514
  const { host, registered, notifications } = fakeEngine(home);
483
515
  const handlers = new Map();
484
516
  (await loadHookModule(t)).register((event, handler) => handlers.set(event, handler));
485
517
  const invoke = (name, event = {}) => handlers.get(name)(host, event, async () => ({ sentinel: name }));
486
- // A 200K window resolves to Claude's 167 000 point, so notesoup resets 8 192 below it.
518
+ assert.equal(handlers.has("session.measure"), false, "measure has no reset hook");
519
+ // The old proactive point was 158808; crossing it must no longer clear.
487
520
  let tokens = 140_000;
488
521
  let compactions = 0;
489
522
  let kept = [];
@@ -502,24 +535,30 @@ test("Claude Mod resets at Claude's resolved point after the turn, retains one f
502
535
  assert.equal(initial.blocks.length, 1, "below the warn point no warning joins the boot");
503
536
  assert.match(initial.blocks[0].text, /Current context window id: claude:hook-session:0/);
504
537
  await invoke("turn.start");
505
- await invoke("session.measure");
506
- await invoke("turn.complete");
538
+ await invoke("turn.complete", turnComplete());
507
539
  assert.equal(compactions, 0, "140000 is below every resolved point");
508
540
  host.session.usage = async () => { throw new Error("usage unavailable"); };
509
- await invoke("session.measure");
510
- await invoke("turn.complete");
511
- assert.equal(notifications.filter((text) => text.includes("Brain fullness unknown")).length, 1, "usage failure logs once per session");
541
+ await invoke("turn.complete", turnComplete());
542
+ await invoke("tool.call", { tool: "Read" });
543
+ assert.equal(notifications.filter((text) => text.includes("Brain fullness unknown")).length, 1, "warning usage failure logs once per session");
512
544
  host.session.usage = async () => measured();
513
545
  await dispatch({ op: "tool", tool: "notes_write", identity: { ...identity, home, cwd: home, sessionId: "hook-session" }, params: { address: "@human/MAP.md", content: "FRESH_NOTES_AT_RESET" } });
514
546
  await invoke("turn.start");
515
547
  tokens = 158_807;
516
- await invoke("turn.complete");
548
+ await invoke("turn.complete", turnComplete());
517
549
  assert.equal(compactions, 0, "one token below the resolved reset point");
518
550
  await invoke("turn.start");
519
551
  tokens = 158_808;
520
- await invoke("session.measure");
521
552
  assert.equal(compactions, 0, "measure cannot compact under a running turn");
522
- assert.deepEqual(await invoke("turn.complete"), { sentinel: "turn.complete" });
553
+ assert.deepEqual(await invoke("turn.complete", turnComplete()), { sentinel: "turn.complete" });
554
+ assert.equal(compactions, 0, "the old reset point no longer clears at turn completion");
555
+ tokens = 190_000;
556
+ await invoke("turn.start");
557
+ await invoke("turn.complete", turnComplete());
558
+ assert.equal(compactions, 0, "even beyond the native threshold, only a native auto event or explicit request resets");
559
+ await invoke("turn.start");
560
+ await invoke("command.run", { command: "empty" });
561
+ await invoke("turn.complete", turnComplete());
523
562
  assert.equal(compactions, 1);
524
563
  assert.equal(kept.length, 2, "the reset is the notes boot followed by the one reset message");
525
564
  assert.equal(kept[0].role, "user");
@@ -528,44 +567,132 @@ test("Claude Mod resets at Claude's resolved point after the turn, retains one f
528
567
  assert.match(kept[0].text, /FRESH_NOTES_AT_RESET/);
529
568
  assert.doesNotMatch(kept[0].text, /OLD_SECRET/);
530
569
  assert.ok(FORCED_CONTINUATIONS.includes(kept[1].text), "the handoff comes from the shared forced pool");
531
- await invoke("session.measure");
532
570
  assert.equal(compactions, 1, "invalidated tokens cannot immediately reset twice");
533
571
  const upstream = { blocks: [{ name: "unrelated", text: "leave this alone" }], sentinel: "kept" };
534
572
  assert.deepEqual(await handlers.get("prompt.context")(host, {}, async () => upstream), upstream, "the retained boot is not injected a second time");
535
573
  });
536
- test("Claude Mod headless detection skips compaction and logs once", async (t) => {
574
+ test("Claude Mod preserves context and cancels a queued reset after an unsuccessful turn", async (t) => {
575
+ for (const reason of ["aborted", "error", "refusal"]) {
576
+ await t.test(reason, async (t) => {
577
+ const home = mkdtempSync(join(tmpdir(), `claude-reset-${reason}-`));
578
+ t.after(() => rmSync(home, { recursive: true, force: true }));
579
+ const { host, notifications } = fakeEngine(home);
580
+ const handlers = new Map();
581
+ (await loadHookModule(t)).register((event, handler) => handlers.set(event, handler));
582
+ const invoke = (name, event = {}) => handlers.get(name)(host, event, async () => ({ sentinel: name }));
583
+ let compactions = 0;
584
+ host.session.compact = async () => { compactions++; return { skip: "reset must not run" }; };
585
+ await invoke("session.start", { cwd: home, isInteractive: true });
586
+ await invoke("turn.start");
587
+ await invoke("tool.call", { tool: "mcp__notesoup__empty_brain", tool_use_id: `reset-${reason}`, last_word: "DO_NOT_KEEP" });
588
+ await invoke("turn.complete", turnComplete({ reason }));
589
+ assert.equal(compactions, 0, `${reason} never submits the queued reset`);
590
+ assert.ok(notifications.some(text => text.includes("cancelled") && text.includes(reason)), `${reason} reports that its request was cancelled`);
591
+ await invoke("turn.start");
592
+ await invoke("turn.complete", turnComplete());
593
+ assert.equal(compactions, 0, `${reason} leaves no pending reset for a later successful turn`);
594
+ });
595
+ }
596
+ });
597
+ test("Claude headless resets through its own manual command, retries failure and keeps user compact vetoed", async (t) => {
537
598
  const home = mkdtempSync(join(tmpdir(), "claude-headless-home-"));
538
599
  t.after(() => rmSync(home, { recursive: true, force: true }));
539
600
  const { host, notifications } = fakeEngine(home);
540
601
  const handlers = new Map();
541
602
  (await loadHookModule(t)).register((event, handler) => handlers.set(event, handler));
542
- const invoke = (name, e = {}) => handlers.get(name)(host, e, async () => ({ sentinel: name }));
543
- let tokens;
603
+ const invoke = (name, e = {}) => handlers.get(name)(host, e, async () => ({}));
604
+ let kept = [];
544
605
  let attempts = 0;
606
+ let reject = true;
607
+ host.session.messages = async () => kept;
608
+ host.session.compact = async () => { throw new Error("headless must not call session.compact"); };
609
+ host.command.run = async ({ command }) => {
610
+ attempts++;
611
+ assert.equal(command, "compact");
612
+ if (reject)
613
+ throw new Error("command unavailable");
614
+ return { text: "Not compacted" };
615
+ };
616
+ // Exercise the user veto before the internally owned command is in flight.
617
+ await invoke("session.start", { cwd: home, isInteractive: false });
618
+ assert.match(JSON.stringify(await invoke("session.compact", { trigger: "manual" })), /use \/fresh-window/);
619
+ await invoke("tool.call", { tool: "mcp__notesoup__empty_brain", last_word: "STICKY", tool_use_id: "reset" });
620
+ await invoke("turn.complete", turnComplete());
621
+ assert.equal(attempts, 1);
622
+ assert.ok(notifications.some(x => x.includes("command unavailable")));
623
+ reject = false;
624
+ await invoke("turn.complete", turnComplete());
625
+ assert.equal(attempts, 2, "failed requests remain pending");
626
+ assert.ok(notifications.some(x => x.includes("Brain not emptied")));
627
+ // Core dispatches the admitted manual event within our command hook.
628
+ host.command.run = async ({ command }) => {
629
+ attempts++;
630
+ const user = await handlers.get("command.run")(host, { command, origin: { kind: "user" } }, async () => invoke("session.compact", { trigger: "manual" }));
631
+ assert.match(JSON.stringify(user), /use \/fresh-window/, "a pending reset does not admit the user's command");
632
+ await handlers.get("command.run")(host, { command, origin: { kind: "plugin", name: "notesoup" } }, async () => {
633
+ kept = (await invoke("session.compact", { trigger: "manual" })).messages;
634
+ return {};
635
+ });
636
+ return {};
637
+ };
638
+ await invoke("turn.complete", turnComplete());
639
+ assert.match(kept[0].text, /claude:hook-session:1/);
640
+ assert.equal(kept[2].text, "*You peel a sticky note off your forehead.*\nSTICKY");
641
+ await invoke("turn.complete", turnComplete());
642
+ assert.equal(attempts, 3, "success clears pending exactly once");
643
+ assert.match(JSON.stringify(await invoke("session.compact", { trigger: "manual" })), /use \/fresh-window/);
644
+ await invoke("tool.call", { tool: "mcp__notesoup__empty_brain", tool_use_id: "reset2" });
645
+ await invoke("turn.complete", turnComplete());
646
+ assert.match(kept[0].text, /claude:hook-session:2/);
647
+ assert.equal(kept.length, 2, "sticky is consumed once");
648
+ });
649
+ test("Claude headless warns without proactive resets and supports both manual aliases", async (t) => {
650
+ const home = mkdtempSync(join(tmpdir(), "claude-headless-auto-"));
651
+ t.after(() => rmSync(home, { recursive: true, force: true }));
652
+ const { host } = fakeEngine(home);
653
+ const handlers = new Map();
654
+ (await loadHookModule(t)).register((event, handler) => handlers.set(event, handler));
655
+ const invoke = (name, e = {}) => handlers.get(name)(host, e, async () => ({}));
656
+ let tokens = CLAUDE_200K.warn;
657
+ let attempts = 0, prompts = 0;
658
+ const timers = [];
545
659
  host.session.usage = async () => ({ context: { tokens, window: 200_000 } });
546
- host.session.compact = async () => { attempts++; throw new Error("not available in a headless (-p / SDK) session yet"); };
660
+ host.clock.after = (_ms, callback) => { timers.push(callback); };
661
+ host.prompt.submit = async () => { prompts++; await invoke("turn.start"); await invoke("turn.complete", turnComplete()); };
662
+ host.command.run = async ({ command }) => {
663
+ attempts++;
664
+ await handlers.get("command.run")(host, { command, origin: { kind: "plugin", name: "notesoup" } }, async () => {
665
+ const result = await invoke("session.compact", { trigger: "manual" });
666
+ assert.match(result.messages[0].text, new RegExp(`claude:hook-session:${attempts}`));
667
+ tokens = undefined;
668
+ return {};
669
+ });
670
+ return {};
671
+ };
547
672
  await invoke("session.start", { cwd: home, isInteractive: false });
673
+ const warning = await handlers.get("prompt.submit")(host, { text: "continue" }, async (e) => e);
674
+ assert.deepEqual(warning.context, [CLAUDE_WARNING_PROMPT]);
675
+ tokens = CLAUDE_200K.reset;
548
676
  await invoke("turn.start");
549
- await invoke("turn.complete");
550
- await invoke("session.measure");
551
- assert.equal(attempts, 0, "no measured tokens means no automatic reset");
552
- tokens = 190_000;
553
- await invoke("turn.start");
554
- assert.deepEqual(await invoke("turn.complete"), { sentinel: "turn.complete" }, "a rejection does not break the turn chain");
555
- await invoke("session.measure");
556
- await invoke("turn.complete");
557
- assert.equal(attempts, 0, "headless sessions skip the unsupported capability from startup");
558
- assert.deepEqual(notifications, ["🥣 No brain when headless"]);
559
- const manual = await invoke("command.run", { command: "empty" });
560
- assert.match(manual.text, /No brain when headless/);
561
- const tool = await invoke("tool.call", { tool: "mcp__notesoup__empty_brain", tool_use_id: "headless" });
562
- assert.equal(tool.isError, true);
563
- assert.match(tool.result, /No brain when headless/);
564
- assert.equal(notifications.length, 1);
565
- const boot = await handlers.get("prompt.context")(host, {}, async () => ({ blocks: [] }));
566
- assert.match(boot.blocks[0].text, /Current context window id: claude:hook-session:0/);
567
- const event = { text: "continue" };
568
- assert.equal(await handlers.get("prompt.submit")(host, event, async (e) => e), event, "headless prompts never warn");
677
+ assert.equal(attempts, 0);
678
+ await invoke("turn.complete", turnComplete());
679
+ assert.equal(attempts, 0, "the old reset threshold and turn completion do not clear");
680
+ await invoke("tool.call", { tool: "mcp__notesoup__empty_brain", tool_use_id: "explicit" });
681
+ await invoke("turn.complete", turnComplete());
682
+ assert.equal(attempts, 1);
683
+ for (const command of ["empty", "fresh-window"]) {
684
+ assert.deepEqual(await invoke("command.run", { command }), { text: "🥣 Dumping Brain" });
685
+ await Promise.resolve();
686
+ await timers.shift()();
687
+ }
688
+ assert.equal(attempts, 3);
689
+ assert.equal(prompts, 2, "each idle alias schedules one close-out, not a summarizer");
690
+ host.prompt.submit = async () => { throw new Error("close-out unavailable"); };
691
+ await invoke("command.run", { command: "empty" });
692
+ await Promise.resolve();
693
+ await timers.shift()();
694
+ await invoke("turn.complete", turnComplete());
695
+ assert.equal(attempts, 4, "a failed close-out also leaves the reset pending");
569
696
  });
570
697
  test("Claude Mod keeps its window numbering from a boot written before the tag rename", async (t) => {
571
698
  const home = mkdtempSync(join(tmpdir(), "claude-legacy-boot-"));
@@ -605,12 +732,13 @@ test("Claude Mod refuses to wipe on boot failure and can retry without advancing
605
732
  let result = {};
606
733
  host.session.compact = async ({ instructions }) => result = await invoke("session.compact", { trigger: "plugin", instructions });
607
734
  await invoke("turn.start");
608
- await invoke("turn.complete");
735
+ await invoke("command.run", { command: "empty" });
736
+ await invoke("turn.complete", turnComplete());
609
737
  assert.match(result.skip, /notes didn't load/);
610
738
  assert.equal(result.messages, undefined, "a failed builder must veto rather than fall through to summary");
611
739
  failBoot = false;
612
740
  await invoke("turn.start");
613
- await invoke("turn.complete");
741
+ await invoke("turn.complete", turnComplete());
614
742
  assert.equal(result.skip, undefined);
615
743
  assert.equal(result.messages.length, 2);
616
744
  assert.match(result.messages[0].text, /Current context window id: claude:hook-session:1/, "a failed reset never consumes a window ordinal");
@@ -638,7 +766,7 @@ test("Claude Mod manual reset uses one hidden close-out turn, then recovers its
638
766
  host.prompt.submit = async ({ text }) => {
639
767
  prompts.push(text);
640
768
  await invoke("turn.start");
641
- await invoke("turn.complete");
769
+ await invoke("turn.complete", turnComplete());
642
770
  };
643
771
  (await loadHookModule(t)).register((event, handler) => handlers.set(event, handler));
644
772
  await invoke("session.start", { cwd: home, isInteractive: true });
@@ -646,7 +774,7 @@ test("Claude Mod manual reset uses one hidden close-out turn, then recovers its
646
774
  assert.deepEqual(await invoke("command.run", { command: "empty-brain" }), {}, "the retired spelling passes through instead of resetting");
647
775
  assert.equal(timers.length, 0);
648
776
  const command = await invoke("command.run", { command: "empty" });
649
- assert.match(command.text, /Brain being emptied/);
777
+ assert.equal(command.text, "🥣 Dumping Brain");
650
778
  await Promise.resolve();
651
779
  assert.equal(compactions, 0, "command.run must not call compact under its held turn");
652
780
  assert.equal(timers.length, 1);
@@ -664,7 +792,7 @@ test("Claude Mod manual reset uses one hidden close-out turn, then recovers its
664
792
  assert.deepEqual(await handlers.get("prompt.context")(host, {}, async () => ({ blocks: [] })), { blocks: [] }, "reload does not duplicate the already-retained boot");
665
793
  await invoke("turn.start");
666
794
  await invoke("command.run", { command: "empty" });
667
- await invoke("turn.complete");
795
+ await invoke("turn.complete", turnComplete());
668
796
  assert.equal(prompts.length, 1, "a busy manual request reuses the current turn instead of submitting another");
669
797
  assert.match(kept[0].text, /Current context window id: claude:hook-session:2/);
670
798
  });
@@ -697,6 +825,36 @@ test("Claude Mod delivers one warning per window through main tools or prompt su
697
825
  assert.equal(await tool(), foreign, "prompt delivery wins over the later tool route");
698
826
  assert.deepEqual((await context()).blocks, [], "no notesoup:warning block is ever added to prompt context");
699
827
  });
828
+ test("Claude warnings read the public nested breakdown and apply pct even without its threshold", async (t) => {
829
+ for (const threshold of [167_000, undefined]) {
830
+ const { host, invoke } = await retainedFixture(t);
831
+ const env = host.env.get;
832
+ host.env.get = async (name) => name === "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE" ? "65" : name === "CLAUDE_CODE_AUTO_COMPACT_WINDOW" ? "200000" : env(name);
833
+ let tokens = 92_423;
834
+ host.session.usage = async (options) => ({ context: {
835
+ tokens, window: 200_000,
836
+ ...(options?.breakdown ? { breakdown: { autoCompactThreshold: threshold, isAutoCompactEnabled: true, model: "claude" } } : {}),
837
+ } });
838
+ const run = host.process.run;
839
+ host.process.run = async (argv, init) => {
840
+ const request = JSON.parse(init?.stdin ?? "{}");
841
+ if (request.op === "limits")
842
+ assert.deepEqual(request.facts.breakdown, { ...(threshold === undefined ? {} : { autoCompactThreshold: threshold }), isAutoCompactEnabled: true, model: "claude" });
843
+ return run(argv, init);
844
+ };
845
+ let resets = 0;
846
+ host.session.compact = async () => { resets++; return {}; };
847
+ const submit = () => invoke("prompt.submit", { text: "continue" }, {}, async (e) => e);
848
+ assert.equal((await submit()).context, undefined);
849
+ tokens++;
850
+ assert.deepEqual((await submit()).context, [CLAUDE_WARNING_PROMPT]);
851
+ assert.equal((await submit()).context, undefined, "one warning per window");
852
+ await invoke("turn.complete", turnComplete());
853
+ assert.equal(resets, 0, "warning and turn completion never proactively clear");
854
+ assert.equal((await invoke("session.compact", { trigger: "auto" })).messages.length, 2);
855
+ assert.deepEqual((await submit()).context, [CLAUDE_WARNING_PROMPT], "native auto starts a new warning window");
856
+ }
857
+ });
700
858
  test("Claude Mod consumes a warning only after an accepted delivery and ignores stale generations", async (t) => {
701
859
  const { host, invoke } = await retainedFixture(t);
702
860
  const texts = await dispatch({ op: "texts" });
@@ -728,7 +886,7 @@ test("Claude Mod consumes a warning only after an accepted delivery and ignores
728
886
  assert.deepEqual(delivered.context, [...event.context, texts.warning], "an old delivery cannot mark the new generation warned");
729
887
  assert.equal(await invoke("prompt.submit", event, event), event, "only an accepted delivery consumes the new window");
730
888
  });
731
- test("Claude Mod moves the reset point to the hard window when auto-compact is off", async (t) => {
889
+ test("Claude Mod keeps warning timing when auto-compact is off without clearing at the old reset point", async (t) => {
732
890
  const home = mkdtempSync(join(tmpdir(), "claude-off-home-"));
733
891
  t.after(() => rmSync(home, { recursive: true, force: true }));
734
892
  const { host, notifications } = fakeEngine(home);
@@ -736,21 +894,25 @@ test("Claude Mod moves the reset point to the hard window when auto-compact is o
736
894
  const handlers = new Map();
737
895
  (await loadHookModule(t)).register((event, handler) => handlers.set(event, handler));
738
896
  const invoke = (name, e = {}) => handlers.get(name)(host, e, async () => ({}));
739
- let tokens = 171_807;
897
+ let tokens = 155_423;
740
898
  let compactions = 0;
741
899
  host.session.usage = async () => ({ context: { tokens, window: 200_000 } });
742
900
  host.session.compact = async ({ instructions }) => { compactions++; return await invoke("session.compact", { trigger: "plugin", instructions }); };
743
901
  await invoke("session.start", { cwd: home, isInteractive: true });
744
902
  await invoke("turn.start");
745
- await invoke("turn.complete");
746
- assert.equal(compactions, 0, "171807 is below the hard-window point");
903
+ await invoke("turn.complete", turnComplete());
904
+ const submit = () => handlers.get("prompt.submit")(host, { text: "continue" }, async (e) => e);
905
+ assert.equal((await submit()).context, undefined);
906
+ tokens = 155_424;
907
+ assert.deepEqual((await submit()).context, [CLAUDE_WARNING_PROMPT], "the hard-window warning point stays 180000 - 8192 - 16384");
908
+ assert.equal(compactions, 0);
747
909
  await invoke("turn.start");
748
910
  tokens = 171_808;
749
- await invoke("turn.complete");
750
- assert.equal(compactions, 1, "200000 - 20000 - 8192");
911
+ await invoke("turn.complete", turnComplete());
912
+ assert.equal(compactions, 0, "the former 200000 - 20000 - 8192 reset point is not a clear trigger");
751
913
  assert.deepEqual(notifications.filter((text) => text.includes("unavailable") || text.includes("Couldn't")), [], "a resolvable configuration logs no failure");
752
914
  });
753
- test("Claude Mod stops the automatic reset and the manual command under DISABLE_COMPACT", async (t) => {
915
+ test("Claude Mod suppresses warnings and explicit reset requests under DISABLE_COMPACT", async (t) => {
754
916
  const home = mkdtempSync(join(tmpdir(), "claude-disabled-home-"));
755
917
  t.after(() => rmSync(home, { recursive: true, force: true }));
756
918
  const { host, notifications } = fakeEngine(home);
@@ -773,15 +935,13 @@ test("Claude Mod stops the automatic reset and the manual command under DISABLE_
773
935
  assert.match(tool.result, /DISABLE_COMPACT/);
774
936
  assert.equal(timers.length, 0, "no close-out turn is queued");
775
937
  await invoke("turn.start");
776
- await invoke("session.measure");
777
- await invoke("turn.complete");
778
- await invoke("session.measure");
779
- assert.equal(compactions, 0, "the automatic reset is off and does not retry");
938
+ await invoke("turn.complete", turnComplete());
939
+ assert.equal(compactions, 0, "disabled reset requests never run");
780
940
  assert.equal(notifications.length, 1, "detection logs once, not per turn");
781
941
  const upstream = { result: "upstream", context: ["other plugin"] };
782
942
  assert.equal(await handlers.get("tool.call")(host, { tool: "Read" }, async () => upstream), upstream, "DISABLE_COMPACT also suppresses warnings");
783
943
  });
784
- test("Claude Mod skips the automatic reset when thresholds cannot be resolved", async (t) => {
944
+ test("Claude Mod skips the warning when thresholds cannot be resolved", async (t) => {
785
945
  const home = mkdtempSync(join(tmpdir(), "claude-nolimits-home-"));
786
946
  t.after(() => rmSync(home, { recursive: true, force: true }));
787
947
  const { host, notifications } = fakeEngine(home);
@@ -794,9 +954,8 @@ test("Claude Mod skips the automatic reset when thresholds cannot be resolved",
794
954
  host.session.compact = async () => { compactions++; return {}; };
795
955
  await invoke("session.start", { cwd: home, isInteractive: true });
796
956
  await invoke("turn.start");
797
- await invoke("session.measure");
798
- await invoke("turn.complete");
799
- await invoke("session.measure");
957
+ await invoke("turn.complete", turnComplete());
958
+ await invoke("tool.call", { tool: "Read" });
800
959
  assert.equal(compactions, 0, "an unresolvable point never resets");
801
960
  assert.equal(notifications.filter((text) => text.includes("Brain size unknown")).length, 1, "one line, not one per turn");
802
961
  });
@@ -820,11 +979,11 @@ test("Claude Mod keeps the turn latch through subagent turns", async (t) => {
820
979
  host.session.compact = async ({ instructions }) => { compactions++; return await invoke("session.compact", { trigger: "plugin", instructions }); };
821
980
  await invoke("session.start", { cwd: home, isInteractive: true });
822
981
  await invoke("turn.start");
982
+ await invoke("tool.call", { tool: "mcp__notesoup__empty_brain", tool_use_id: "explicit" });
823
983
  await invoke("turn.start", { agentId: "sub-1" });
824
- await invoke("turn.complete", { agentId: "sub-1" });
825
- await invoke("session.measure");
984
+ await invoke("turn.complete", turnComplete({ agentId: "sub-1" }));
826
985
  assert.equal(compactions, 0, "a subagent turn cannot release the main turn's latch");
827
- await invoke("turn.complete");
986
+ await invoke("turn.complete", turnComplete());
828
987
  assert.equal(compactions, 1, "the main turn still resets at its own end");
829
988
  });
830
989
  test("Claude Mod treats DISABLE_COMPACT=off as not disabled", async (t) => {
@@ -841,10 +1000,11 @@ test("Claude Mod treats DISABLE_COMPACT=off as not disabled", async (t) => {
841
1000
  await invoke("session.start", { cwd: home, isInteractive: true });
842
1001
  assert.deepEqual(notifications.filter((text) => text.includes("DISABLE_COMPACT")), [], "an explicit off is not a disable");
843
1002
  await invoke("turn.start");
844
- await invoke("turn.complete");
845
- assert.equal(compactions, 1, "the automatic reset still runs");
1003
+ await invoke("tool.call", { tool: "mcp__notesoup__empty_brain", tool_use_id: "explicit" });
1004
+ await invoke("turn.complete", turnComplete());
1005
+ assert.equal(compactions, 1, "the explicit reset still runs");
846
1006
  });
847
- test("Claude Mod retries an unresolvable threshold inside the same window", async (t) => {
1007
+ test("Claude Mod retries an unresolvable warning threshold inside the same window", async (t) => {
848
1008
  const home = mkdtempSync(join(tmpdir(), "claude-retry-home-"));
849
1009
  t.after(() => rmSync(home, { recursive: true, force: true }));
850
1010
  const { host } = fakeEngine(home);
@@ -864,13 +1024,16 @@ test("Claude Mod retries an unresolvable threshold inside the same window", asyn
864
1024
  host.session.compact = async ({ instructions }) => { compactions++; return await invoke("session.compact", { trigger: "plugin", instructions }); };
865
1025
  await invoke("session.start", { cwd: home, isInteractive: true });
866
1026
  await invoke("turn.start");
867
- await invoke("turn.complete");
1027
+ await invoke("turn.complete", turnComplete());
1028
+ await handlers.get("prompt.submit")(host, { text: "continue" }, async (e) => e);
868
1029
  assert.equal(limitsCalls, 1, "the first limits round trip failed");
869
1030
  assert.equal(compactions, 0, "one failed resolution does not disable the window");
870
1031
  await invoke("turn.start");
871
- await invoke("turn.complete");
1032
+ await invoke("turn.complete", turnComplete());
1033
+ const retried = await handlers.get("prompt.submit")(host, { text: "continue" }, async (e) => e);
1034
+ assert.deepEqual(retried.context, [CLAUDE_WARNING_PROMPT]);
872
1035
  assert.equal(limitsCalls, 2, "the retry runs in the same window");
873
- assert.equal(compactions, 1, "the retried resolution resets at the turn's end");
1036
+ assert.equal(compactions, 0, "warning threshold resolution never resets");
874
1037
  });
875
1038
  /** Start from a real retained boot, as after notesoup reset or a plugin reload. */
876
1039
  async function retainedFixture(t) {
@@ -913,35 +1076,43 @@ test("Claude empty_brain defers until main turn completion, carries last_word, a
913
1076
  assert.equal(accepted.isError, undefined);
914
1077
  assert.match(accepted.result, /End your turn now/);
915
1078
  await call({ last_word: "must not replace the first request" });
916
- await invoke("session.measure");
917
- await invoke("turn.complete", { agentId: "child" });
1079
+ await invoke("turn.complete", turnComplete({ agentId: "child" }));
918
1080
  assert.equal(resets.length, 0, "neither tool execution nor a child completion resets a running main turn");
919
- await invoke("turn.complete");
1081
+ await invoke("turn.complete", turnComplete());
920
1082
  assert.equal(resets.length, 1, "the tool requests a reset even far below the budget threshold");
921
1083
  assert.deepEqual(origins, ["normal"]);
922
1084
  assert.ok(NORMAL_CONTINUATIONS.includes(resets[0].messages[1].text));
923
1085
  assert.deepEqual(resets[0].messages.slice(2).map((row) => row.text), ["*You peel a sticky note off your forehead.*\nnext move: test"]);
924
1086
  assert.match(resets[0].messages[0].text, /mcp__notesoup__empty_brain/);
925
1087
  await invoke("turn.start");
926
- await invoke("turn.complete");
1088
+ await invoke("turn.complete", turnComplete());
927
1089
  assert.equal(resets.length, 1, "the successful tool request does not leak into the next window");
928
1090
  const forced = await invoke("session.compact", { trigger: "auto" });
929
1091
  assert.deepEqual(origins, ["normal", "forced"]);
930
1092
  assert.equal(forced.messages.length, 2, "the sticky note does not leak into the next reset");
931
1093
  assert.ok(FORCED_CONTINUATIONS.includes(forced.messages[1].text));
932
1094
  });
1095
+ test("Claude empty_brain accepts 500 code points", async (t) => {
1096
+ const { host, invoke } = await retainedFixture(t);
1097
+ host.session.usage = async () => ({ context: { tokens: 100, window: 200_000 } });
1098
+ const sticky = "🐟".repeat(500);
1099
+ const accepted = await invoke("tool.call", { tool: "mcp__notesoup__empty_brain", tool_use_id: "limit-wipe", last_word: ` ${sticky} ` });
1100
+ assert.equal(accepted.isError, undefined);
1101
+ const result = await invoke("session.compact", { trigger: "auto" });
1102
+ assert.equal(result.messages[2].text, `*You peel a sticky note off your forehead.*\n${sticky}`);
1103
+ });
933
1104
  test("Claude empty_brain refuses unsupported calls without queuing a reset", async (t) => {
934
1105
  const { host, invoke } = await retainedFixture(t);
935
1106
  let resets = 0;
936
1107
  host.session.compact = async () => { resets++; return {}; };
937
1108
  await invoke("turn.start");
938
1109
  const call = (params) => invoke("tool.call", { tool: "mcp__notesoup__empty_brain", tool_use_id: "bad-wipe", ...params });
939
- for (const [params, reason] of [[{ agentId: "child" }, /only the main agent/], [{ last_word: "x".repeat(301) }, /Shorten it/], [{ last_word: 7 }, /must be a string/]]) {
1110
+ for (const [params, reason] of [[{ agentId: "child" }, /only the main agent/], [{ last_word: "x".repeat(501) }, /Shorten it/], [{ last_word: 7 }, /must be a string/]]) {
940
1111
  const refused = await call(params);
941
1112
  assert.equal(refused.isError, true);
942
1113
  assert.match(refused.result, reason);
943
1114
  }
944
- await invoke("turn.complete");
1115
+ await invoke("turn.complete", turnComplete());
945
1116
  assert.equal(resets, 0);
946
1117
  });
947
1118
  test("Claude asks for one handoff per compaction and reuses it when the same window retries", async (t) => {
@@ -956,9 +1127,8 @@ test("Claude asks for one handoff per compaction and reuses it when the same win
956
1127
  };
957
1128
  // Every read path stays pure: no boot, warning, context, or measure draws a handoff.
958
1129
  await invoke("prompt.context", {}, { blocks: [] });
959
- await invoke("session.measure");
960
1130
  await invoke("turn.start");
961
- await invoke("turn.complete");
1131
+ await invoke("turn.complete", turnComplete());
962
1132
  assert.equal(draws.length, 0, "only a reset draws, never a read");
963
1133
  const timers = [];
964
1134
  const lines = [];
@@ -972,10 +1142,10 @@ test("Claude asks for one handoff per compaction and reuses it when the same win
972
1142
  }
973
1143
  return { skip: "test default" };
974
1144
  };
975
- assert.match((await invoke("command.run", { command: "fresh-window", args: "" })).text, /Brain being emptied/);
1145
+ assert.equal((await invoke("command.run", { command: "fresh-window", args: "" })).text, "🥣 Dumping Brain");
976
1146
  await Promise.resolve();
977
1147
  await timers.shift()();
978
- await invoke("turn.complete");
1148
+ await invoke("turn.complete", turnComplete());
979
1149
  assert.equal(draws.length, 1, "a retried compaction adds no second draw");
980
1150
  assert.equal(lines.length, 2, "both attempts returned a handoff");
981
1151
  assert.equal(lines[0], lines[1], "both attempts carry the same artifact's paragraph");
@@ -996,7 +1166,7 @@ test("Claude session.end gives /clear, /resume and /branch a fresh boot and iden
996
1166
  // Clear the old running-turn and queued-reset state, not just the retained boot.
997
1167
  let compactions = 0;
998
1168
  host.session.compact = async () => { compactions++; return {}; };
999
- await invoke("turn.complete");
1169
+ await invoke("turn.complete", turnComplete());
1000
1170
  assert.equal(compactions, 0, "the old queued reset cannot spill into the new session");
1001
1171
  const fresh = await context();
1002
1172
  assert.deepEqual(fresh.blocks.map((block) => block.name), ["notesoup:boot"]);
@@ -1008,7 +1178,7 @@ test("Claude session.end gives /clear, /resume and /branch a fresh boot and iden
1008
1178
  host.process.run = async (argv, init) => { requests.push(JSON.parse(init.stdin)); return run(argv, init); };
1009
1179
  const written = await invoke("tool.call", { tool: "mcp__notesoup__notes_write", tool_use_id: "new-session", address: "session.md", content: action });
1010
1180
  assert.equal(written.result, "created session.md");
1011
- assert.equal(requests[0].identity?.sessionId, `after-${action}`, "tools also use the new session home");
1181
+ assert.equal(requests[0].identity?.sessionId, `after-${action}`, "tools also use the new session notebook");
1012
1182
  });
1013
1183
  }
1014
1184
  });
@@ -1120,9 +1290,9 @@ test("Claude identity uses the documented current model and cwd, with a safe mod
1120
1290
  await write("@model/model.md", "fallback");
1121
1291
  assert.equal(seen.at(-1).identity.model, "claude");
1122
1292
  });
1123
- test("Claude current-cwd project resolution shares Pi's remote home across checkout spellings", async (t) => {
1293
+ test("Claude current-cwd project resolution shares Pi's remote notebook across checkout spellings", async (t) => {
1124
1294
  const { host, home, invoke } = await retainedFixture(t);
1125
- const root = realpathSync(mkdtempSync(join(tmpdir(), "claude-project-home-")));
1295
+ const root = realpathSync(mkdtempSync(join(tmpdir(), "claude-project-notebook-")));
1126
1296
  t.after(() => rmSync(root, { recursive: true, force: true }));
1127
1297
  const first = join(root, "api");
1128
1298
  const second = join(root, "renamed");
@@ -1145,7 +1315,7 @@ test("Claude current-cwd project resolution shares Pi's remote home across check
1145
1315
  assert.equal(written.result, "overwrote @project/shared.md (project api)");
1146
1316
  assert.equal((await createNotesStore(piIdentity).read("@project/shared"))?.body, "Claude body");
1147
1317
  assert.deepEqual(JSON.parse(readFileSync(join(home, "project/api/.project"), "utf8")), { id: "host/team/backend", path: second });
1148
- assert.equal(resolveProjectHome(second, home).name, piIdentity.projectKey);
1318
+ assert.equal(resolveProjectNamespace(second, home).name, piIdentity.projectKey);
1149
1319
  });
1150
1320
  test("Claude session.end cannot be undone by an in-flight old reset", async (t) => {
1151
1321
  const { host, invoke, context } = await retainedFixture(t);
@@ -1160,7 +1330,8 @@ test("Claude session.end cannot be undone by an in-flight old reset", async (t)
1160
1330
  await hold;
1161
1331
  return result;
1162
1332
  };
1163
- const finishing = invoke("turn.complete");
1333
+ await invoke("tool.call", { tool: "mcp__notesoup__empty_brain", tool_use_id: "in-flight" });
1334
+ const finishing = invoke("turn.complete", turnComplete());
1164
1335
  await ready;
1165
1336
  await invoke("session.end", { reason: "clear", sessionId: "hook-session" });
1166
1337
  host.session.id = async () => "after-flight";