notesoup 0.0.0-stage → 0.39.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 (227) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +91 -2
  3. package/dist/src/boot/render.d.ts +16 -0
  4. package/dist/src/boot/render.js +95 -0
  5. package/dist/src/boot/snapshot.d.ts +32 -0
  6. package/dist/src/boot/snapshot.js +36 -0
  7. package/dist/src/boot/text.d.ts +21 -0
  8. package/dist/src/boot/text.js +52 -0
  9. package/dist/src/budget/constants.d.ts +9 -0
  10. package/dist/src/budget/constants.js +9 -0
  11. package/dist/src/budget/policy.d.ts +19 -0
  12. package/dist/src/budget/policy.js +36 -0
  13. package/dist/src/budget/text.d.ts +8 -0
  14. package/dist/src/budget/text.js +10 -0
  15. package/dist/src/claude/helper.d.ts +15 -0
  16. package/dist/src/claude/helper.js +87 -0
  17. package/dist/src/dream/doctor.d.ts +2 -0
  18. package/dist/src/dream/doctor.js +160 -0
  19. package/dist/src/dream/gates.d.ts +10 -0
  20. package/dist/src/dream/gates.js +26 -0
  21. package/dist/src/dream/git.d.ts +21 -0
  22. package/dist/src/dream/git.js +71 -0
  23. package/dist/src/dream/jail.d.ts +11 -0
  24. package/dist/src/dream/jail.js +71 -0
  25. package/dist/src/dream/lock.d.ts +31 -0
  26. package/dist/src/dream/lock.js +99 -0
  27. package/dist/src/dream/playbook.d.ts +1 -0
  28. package/dist/src/dream/playbook.js +2 -0
  29. package/dist/src/dream/report.d.ts +7 -0
  30. package/dist/src/dream/report.js +43 -0
  31. package/dist/src/dream/result.d.ts +9 -0
  32. package/dist/src/dream/result.js +1 -0
  33. package/dist/src/dream/settings.d.ts +10 -0
  34. package/dist/src/dream/settings.js +14 -0
  35. package/dist/src/history/history.d.ts +151 -0
  36. package/dist/src/history/history.js +195 -0
  37. package/dist/src/history/query.d.ts +75 -0
  38. package/dist/src/history/query.js +143 -0
  39. package/dist/src/index.d.ts +1 -0
  40. package/dist/src/index.js +1 -0
  41. package/dist/src/notes/address.d.ts +31 -0
  42. package/dist/src/notes/address.js +123 -0
  43. package/dist/src/notes/constants.d.ts +3 -0
  44. package/dist/src/notes/constants.js +3 -0
  45. package/dist/src/notes/errors.d.ts +11 -0
  46. package/dist/src/notes/errors.js +13 -0
  47. package/dist/src/notes/frontmatter.d.ts +44 -0
  48. package/dist/src/notes/frontmatter.js +145 -0
  49. package/dist/src/notes/fs-error.d.ts +2 -0
  50. package/dist/src/notes/fs-error.js +5 -0
  51. package/dist/src/notes/identity.d.ts +10 -0
  52. package/dist/src/notes/identity.js +33 -0
  53. package/dist/src/notes/index.d.ts +5 -0
  54. package/dist/src/notes/index.js +3 -0
  55. package/dist/src/notes/paths.d.ts +21 -0
  56. package/dist/src/notes/paths.js +113 -0
  57. package/dist/src/notes/store.d.ts +104 -0
  58. package/dist/src/notes/store.js +478 -0
  59. package/dist/src/pi/boot.d.ts +24 -0
  60. package/dist/src/pi/boot.js +56 -0
  61. package/dist/src/pi/budget.d.ts +13 -0
  62. package/dist/src/pi/budget.js +178 -0
  63. package/dist/src/pi/dream/cli.d.ts +10 -0
  64. package/dist/src/pi/dream/cli.js +140 -0
  65. package/dist/src/pi/dream/runner.d.ts +27 -0
  66. package/dist/src/pi/dream/runner.js +90 -0
  67. package/dist/src/pi/dream/settings.d.ts +7 -0
  68. package/dist/src/pi/dream/settings.js +17 -0
  69. package/dist/src/pi/entries.d.ts +7 -0
  70. package/dist/src/pi/entries.js +7 -0
  71. package/dist/src/pi/estimate.d.ts +16 -0
  72. package/dist/src/pi/estimate.js +99 -0
  73. package/dist/src/pi/extension.d.ts +10 -0
  74. package/dist/src/pi/extension.js +24 -0
  75. package/dist/src/pi/history-tools.d.ts +3 -0
  76. package/dist/src/pi/history-tools.js +10 -0
  77. package/dist/src/pi/history.d.ts +4 -0
  78. package/dist/src/pi/history.js +150 -0
  79. package/dist/src/pi/notes/adapter.d.ts +10 -0
  80. package/dist/src/pi/notes/adapter.js +27 -0
  81. package/dist/src/pi/notes/tools.d.ts +3 -0
  82. package/dist/src/pi/notes/tools.js +15 -0
  83. package/dist/src/pi/reset/artifacts.d.ts +24 -0
  84. package/dist/src/pi/reset/artifacts.js +84 -0
  85. package/dist/src/pi/reset/committed.d.ts +7 -0
  86. package/dist/src/pi/reset/committed.js +28 -0
  87. package/dist/src/pi/reset/lifecycle.d.ts +101 -0
  88. package/dist/src/pi/reset/lifecycle.js +277 -0
  89. package/dist/src/pi/reset/text.d.ts +5 -0
  90. package/dist/src/pi/reset/text.js +6 -0
  91. package/dist/src/pi/runtime.d.ts +3 -0
  92. package/dist/src/pi/runtime.js +206 -0
  93. package/dist/src/pi/session-reader.d.ts +5 -0
  94. package/dist/src/pi/session-reader.js +1 -0
  95. package/dist/src/pi/thresholds.d.ts +17 -0
  96. package/dist/src/pi/thresholds.js +33 -0
  97. package/dist/src/pi/tool-names.d.ts +11 -0
  98. package/dist/src/pi/tool-names.js +11 -0
  99. package/dist/src/pi/tool-result.d.ts +15 -0
  100. package/dist/src/pi/tool-result.js +29 -0
  101. package/dist/src/pi/window.d.ts +41 -0
  102. package/dist/src/pi/window.js +127 -0
  103. package/dist/src/settings.d.ts +9 -0
  104. package/dist/src/settings.js +18 -0
  105. package/dist/src/text-match.d.ts +5 -0
  106. package/dist/src/text-match.js +16 -0
  107. package/dist/src/tools/history.d.ts +67 -0
  108. package/dist/src/tools/history.js +251 -0
  109. package/dist/src/tools/notes.d.ts +282 -0
  110. package/dist/src/tools/notes.js +378 -0
  111. package/dist/src/tools/output.d.ts +23 -0
  112. package/dist/src/tools/output.js +39 -0
  113. package/dist/src/tools/result.d.ts +79 -0
  114. package/dist/src/tools/result.js +130 -0
  115. package/dist/src/tools/schema.d.ts +18 -0
  116. package/dist/src/tools/schema.js +31 -0
  117. package/dist/test/agent-loop.test.d.ts +1 -0
  118. package/dist/test/agent-loop.test.js +1497 -0
  119. package/dist/test/boot.integration.test.d.ts +1 -0
  120. package/dist/test/boot.integration.test.js +294 -0
  121. package/dist/test/budget-settings.integration.test.d.ts +1 -0
  122. package/dist/test/budget-settings.integration.test.js +144 -0
  123. package/dist/test/claude-mod.test.d.ts +1 -0
  124. package/dist/test/claude-mod.test.js +404 -0
  125. package/dist/test/dependencies.test.d.ts +1 -0
  126. package/dist/test/dependencies.test.js +101 -0
  127. package/dist/test/doctor.test.d.ts +1 -0
  128. package/dist/test/doctor.test.js +37 -0
  129. package/dist/test/dream-skill.test.d.ts +1 -0
  130. package/dist/test/dream-skill.test.js +72 -0
  131. package/dist/test/dream.test.d.ts +1 -0
  132. package/dist/test/dream.test.js +136 -0
  133. package/dist/test/helpers/dependency-graph.d.ts +5 -0
  134. package/dist/test/helpers/dependency-graph.js +73 -0
  135. package/dist/test/helpers/extension-test-environment.d.ts +1 -0
  136. package/dist/test/helpers/extension-test-environment.js +9 -0
  137. package/dist/test/helpers/extension.d.ts +119 -0
  138. package/dist/test/helpers/extension.js +368 -0
  139. package/dist/test/helpers/notes.d.ts +6 -0
  140. package/dist/test/helpers/notes.js +13 -0
  141. package/dist/test/history.integration.test.d.ts +1 -0
  142. package/dist/test/history.integration.test.js +556 -0
  143. package/dist/test/host-neutral.test.d.ts +1 -0
  144. package/dist/test/host-neutral.test.js +300 -0
  145. package/dist/test/notes-library.test.d.ts +1 -0
  146. package/dist/test/notes-library.test.js +214 -0
  147. package/dist/test/notes.integration.test.d.ts +1 -0
  148. package/dist/test/notes.integration.test.js +497 -0
  149. package/dist/test/notes.test.d.ts +1 -0
  150. package/dist/test/notes.test.js +528 -0
  151. package/dist/test/reset-lifecycle.test.d.ts +1 -0
  152. package/dist/test/reset-lifecycle.test.js +605 -0
  153. package/docs/architecture.md +63 -0
  154. package/docs/claude-code-mod.md +9 -0
  155. package/docs/notes-library.md +74 -0
  156. package/docs/reset-lifecycle.md +75 -0
  157. package/docs/sdk-integration.md +39 -0
  158. package/docs/skills.md +30 -0
  159. package/docs/tool-results.md +88 -0
  160. package/mods/notesoup/.claude-plugin/plugin.json +8 -0
  161. package/mods/notesoup/dist/claude/helper.js +9324 -0
  162. package/mods/notesoup/hooks/hooks.json +4 -0
  163. package/mods/notesoup/hooks/register.ts +241 -0
  164. package/mods/notesoup/playbook.md +34 -0
  165. package/mods/notesoup/skills/dream/SKILL.md +12 -0
  166. package/mods/pi-context/dist/claude/helper.js +9324 -0
  167. package/package.json +89 -5
  168. package/playbook.md +34 -0
  169. package/skills/dream/SKILL.md +12 -0
  170. package/skills/memory/SKILL.md +59 -0
  171. package/src/boot/render.ts +115 -0
  172. package/src/boot/snapshot.ts +46 -0
  173. package/src/boot/text.ts +66 -0
  174. package/src/budget/constants.ts +9 -0
  175. package/src/budget/policy.ts +36 -0
  176. package/src/budget/text.ts +11 -0
  177. package/src/claude/helper.ts +94 -0
  178. package/src/dream/doctor.ts +117 -0
  179. package/src/dream/gates.ts +26 -0
  180. package/src/dream/git.ts +73 -0
  181. package/src/dream/jail.ts +62 -0
  182. package/src/dream/lock.ts +82 -0
  183. package/src/dream/playbook.ts +3 -0
  184. package/src/dream/report.ts +36 -0
  185. package/src/dream/result.ts +2 -0
  186. package/src/dream/settings.ts +16 -0
  187. package/src/history/history.ts +270 -0
  188. package/src/history/query.ts +147 -0
  189. package/src/index.ts +1 -0
  190. package/src/notes/address.ts +110 -0
  191. package/src/notes/constants.ts +3 -0
  192. package/src/notes/errors.ts +15 -0
  193. package/src/notes/frontmatter.ts +166 -0
  194. package/src/notes/fs-error.ts +5 -0
  195. package/src/notes/identity.ts +40 -0
  196. package/src/notes/index.ts +24 -0
  197. package/src/notes/paths.ts +109 -0
  198. package/src/notes/store.ts +497 -0
  199. package/src/pi/boot.ts +88 -0
  200. package/src/pi/budget.ts +178 -0
  201. package/src/pi/dream/cli.ts +92 -0
  202. package/src/pi/dream/runner.ts +91 -0
  203. package/src/pi/dream/settings.ts +17 -0
  204. package/src/pi/entries.ts +7 -0
  205. package/src/pi/estimate.ts +116 -0
  206. package/src/pi/extension.ts +27 -0
  207. package/src/pi/history-tools.ts +12 -0
  208. package/src/pi/history.ts +162 -0
  209. package/src/pi/notes/adapter.ts +33 -0
  210. package/src/pi/notes/tools.ts +18 -0
  211. package/src/pi/reset/artifacts.ts +100 -0
  212. package/src/pi/reset/committed.ts +26 -0
  213. package/src/pi/reset/lifecycle.ts +342 -0
  214. package/src/pi/reset/text.ts +10 -0
  215. package/src/pi/runtime.ts +200 -0
  216. package/src/pi/session-reader.ts +6 -0
  217. package/src/pi/thresholds.ts +50 -0
  218. package/src/pi/tool-names.ts +13 -0
  219. package/src/pi/tool-result.ts +41 -0
  220. package/src/pi/window.ts +133 -0
  221. package/src/settings.ts +22 -0
  222. package/src/text-match.ts +14 -0
  223. package/src/tools/history.ts +273 -0
  224. package/src/tools/notes.ts +394 -0
  225. package/src/tools/output.ts +41 -0
  226. package/src/tools/result.ts +169 -0
  227. package/src/tools/schema.ts +31 -0
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,294 @@
1
+ import { resetBoundaryCommitted } from "../src/pi/reset/committed.js";
2
+ import { inspectResetTail } from "../src/pi/reset/artifacts.js";
3
+ import assert from "node:assert/strict";
4
+ import { rmSync, writeFileSync } from "node:fs";
5
+ import test from "node:test";
6
+ import { historyFromSession } from "../src/pi/history.js";
7
+ import { BOOT_TYPE, GUIDANCE_TYPE, RESET_MARKER_TYPE } from "../src/pi/entries.js";
8
+ import { CONTEXT_WINDOW_OPEN_TAG, CONTEXT_WINDOW_PROTOCOL_OPEN_TAG } from "../src/boot/text.js";
9
+ import { CONTINUATION } from "../src/pi/reset/text.js";
10
+ import { listNotes } from "./helpers/notes.js";
11
+ import { CONTINUATION_TYPE, MANUAL_WIPE_TYPE } from "../src/pi/entries.js";
12
+ import { appendText, call, commitTurnEndBoundary, context, makeExtension, manager, noticesOf, resultData, resultJson, resultRead, runContextHook, runContextWithSystemHook, runHandlers, runManualCompact, runCommand, sentOf, } from "./helpers/extension.js";
13
+ import { installExtensionTestHooks } from "./helpers/extension-test-environment.js";
14
+ const testEnvironment = installExtensionTestHooks("notesoup-integration");
15
+ test("custom reset marker removes old provider context while history remains searchable", async () => {
16
+ const sessionManager = manager();
17
+ const captured = makeExtension(sessionManager);
18
+ const ctx = context(sessionManager);
19
+ const oldUserId = appendText(sessionManager, "user", "OLD-UNIQUE-TRANSCRIPT needle");
20
+ appendText(sessionManager, "assistant", "I will use a tool");
21
+ const toolResultId = appendText(sessionManager, "toolResult", "tool result safely recorded");
22
+ await call(captured, "clear_memory", {}, ctx);
23
+ const boundary = await commitTurnEndBoundary(captured, sessionManager, ctx);
24
+ assert.equal(boundary.continue, true);
25
+ const branch = sessionManager.getBranch();
26
+ const marker = branch.find((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE);
27
+ assert.ok(marker && marker.type === "custom");
28
+ const checkpoint = branch.find((entry) => entry.type === "compaction");
29
+ assert.ok(checkpoint && checkpoint.type === "compaction", "reset persists a native retain-none checkpoint");
30
+ assert.equal(checkpoint.parentId, toolResultId, "checkpoint follows the completed tool result");
31
+ assert.equal(checkpoint.summary, "");
32
+ assert.equal(checkpoint.firstKeptEntryId, checkpoint.id);
33
+ assert.equal(marker.parentId, checkpoint.id, "marker follows the native checkpoint");
34
+ assert.deepEqual(Object.keys(marker.data), ["windowId"]);
35
+ const projected = await runContextWithSystemHook(captured, ctx, sessionManager.buildSessionContext().messages);
36
+ const providerText = JSON.stringify(projected?.messages ?? []);
37
+ assert.equal(providerText.includes("OLD-UNIQUE-TRANSCRIPT"), false);
38
+ assert.equal(providerText.includes(CONTEXT_WINDOW_OPEN_TAG), true);
39
+ assert.equal(providerText.includes(RESET_MARKER_TYPE), false, "plain reset state never reaches the provider");
40
+ const projection = historyFromSession(ctx);
41
+ const windows = projection.windows;
42
+ assert.equal(windows.length, 2);
43
+ const oldWindow = windows[0]?.windowId;
44
+ assert.ok(oldWindow);
45
+ const oldSeq = windows[0]?.items.find((item) => item.text.includes("OLD-UNIQUE-TRANSCRIPT"))?.seq;
46
+ assert.ok(oldSeq);
47
+ const read = resultRead(await call(captured, "history_read", { seq: oldSeq }, ctx));
48
+ assert.match(read.content, /OLD-UNIQUE-TRANSCRIPT/);
49
+ const found = resultData(await call(captured, "history_search", { query: "needle" }, ctx));
50
+ assert.equal(found.items.length, 1);
51
+ assert.equal(found.items[0]?.seq, oldSeq);
52
+ });
53
+ test("the root boot and reset boot carry durable window identity", async () => {
54
+ const sessionManager = manager();
55
+ const captured = makeExtension(sessionManager);
56
+ const ctx = context(sessionManager);
57
+ appendText(sessionManager, "user", "task before reset");
58
+ appendText(sessionManager, "assistant", "working");
59
+ await call(captured, "notes_write", { address: "decisions.md", content: "use terra" }, ctx);
60
+ // Root window: session_start persists the boot block without triggering a turn.
61
+ await runHandlers(captured, "session_start", { reason: "startup" }, ctx);
62
+ assert.equal(captured.sent.length, 1);
63
+ const rootBoot = captured.sent[0];
64
+ assert.equal(rootBoot?.message.customType, BOOT_TYPE);
65
+ assert.equal(rootBoot?.message.display, false, "boot block stays out of the TUI");
66
+ assert.equal(rootBoot?.options?.triggerTurn, false);
67
+ assert.equal(noticesOf(ctx).length, 0, "initial boot is silent");
68
+ await runHandlers(captured, "session_start", { reason: "reload" }, ctx);
69
+ assert.equal(noticesOf(ctx).length, 0, "reload is silent");
70
+ assert.deepEqual(rootBoot?.message.details, { windowId: `pcw:${sessionManager.getSessionId().slice(0, 8)}:root` });
71
+ const rootText = typeof rootBoot?.message.content === "string" ? rootBoot.message.content : "";
72
+ assert.ok(rootText.startsWith(CONTEXT_WINDOW_OPEN_TAG), "root block omits the reset line");
73
+ assert.equal(rootText.includes(CONTINUATION), false, "root startup carries no reset message");
74
+ assert.equal(rootText.includes("Previous context window id:"), false, "root block omits the previous-id line");
75
+ assert.match(rootText, new RegExp(`First context window id: pcw:${sessionManager.getSessionId().slice(0, 8)}:root`));
76
+ assert.match(rootText, new RegExp(`Current context window id: pcw:${sessionManager.getSessionId().slice(0, 8)}:root`));
77
+ assert.ok(rootText.includes("decisions.md"));
78
+ const decisionsMeta = (await listNotes(ctx, { scope: "session" })).find((row) => row.path === "decisions.md")?.meta;
79
+ assert.ok(decisionsMeta);
80
+ assert.match(rootText, /decisions\.md · (?:just now|\d+s ago)/, "boot note metadata carries a relative update time");
81
+ assert.ok(rootText.includes(CONTEXT_WINDOW_PROTOCOL_OPEN_TAG));
82
+ // Reset: the marker and boot are committed together at the turn boundary.
83
+ await call(captured, "clear_memory", {}, ctx);
84
+ assert.equal(noticesOf(ctx).length, 0, "requesting a reset does not announce success");
85
+ const boundary = await commitTurnEndBoundary(captured, sessionManager, ctx);
86
+ assert.equal(boundary.continue, true);
87
+ await runHandlers(captured, "turn_start", {}, ctx);
88
+ await runHandlers(captured, "agent_settled", {}, ctx);
89
+ assert.equal(noticesOf(ctx).filter((notice) => notice.message.includes("memory cleared")).length, 1, "the committed reset is announced once");
90
+ const entries = sessionManager.getBranch();
91
+ const marker = entries.find((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE);
92
+ assert.ok(marker && marker.type === "custom");
93
+ const resetWindowId = marker.data.windowId;
94
+ const resetBoot = entries.find((entry) => entry.type === "custom_message" && entry.customType === BOOT_TYPE && entry.details && typeof entry.details === "object" && entry.details.windowId === resetWindowId);
95
+ assert.ok(resetBoot && resetBoot.type === "custom_message");
96
+ assert.equal(resetBoot.details.windowId, marker.data.windowId);
97
+ const continuation = entries.find((entry) => entry.type === "custom_message" && entry.customType === CONTINUATION_TYPE);
98
+ assert.ok(continuation && continuation.type === "custom_message" && continuation.display === false, "the resumed run is represented by one hidden continuation");
99
+ });
100
+ test("an aborted async boot repair emits no boot, continuation, or incomplete-notes notice", async () => {
101
+ const notesHome = process.env.PI_NOTES_HOME;
102
+ assert.ok(notesHome);
103
+ rmSync(notesHome, { recursive: true, force: true });
104
+ writeFileSync(notesHome, "not a directory");
105
+ const sessionManager = manager();
106
+ const windowId = "pcw:aborted-boot-repair";
107
+ sessionManager.appendCustomEntry(RESET_MARKER_TYPE, { windowId });
108
+ const captured = makeExtension(sessionManager);
109
+ const ctx = context(sessionManager);
110
+ const controller = new AbortController();
111
+ ctx.signal = controller.signal;
112
+ const sessionStart = captured.handlers.get("session_start")?.[0];
113
+ assert.ok(sessionStart);
114
+ const pending = sessionStart({ reason: "startup" }, ctx);
115
+ controller.abort();
116
+ await pending;
117
+ assert.equal(captured.sent.length, 0, "aborted snapshot work cannot append the reset boot or continuation");
118
+ assert.equal(noticesOf(ctx).some((notice) => notice.message.includes("notes index incomplete")), false, "aborted snapshot work cannot notify about missing homes");
119
+ assert.equal(sessionManager.getBranch().some((entry) => entry.type === "custom_message" && [BOOT_TYPE, CONTINUATION_TYPE].includes(entry.customType)), false);
120
+ });
121
+ test("a marker tail with only metadata repairs its missing boot and continuation without moving the boundary", async () => {
122
+ const sessionManager = manager(true);
123
+ const windowId = "pcw:metadata-tail";
124
+ const markerId = sessionManager.appendCustomEntry(RESET_MARKER_TYPE, { windowId });
125
+ const modelChangeId = sessionManager.appendModelChange("openai", "scripted-model");
126
+ sessionManager.appendThinkingLevelChange("low");
127
+ const captured = makeExtension(sessionManager);
128
+ const ctx = context(sessionManager);
129
+ await runHandlers(captured, "session_start", { reason: "startup" }, ctx);
130
+ const branch = sessionManager.getBranch();
131
+ const markerIndex = branch.findIndex((entry) => entry.id === markerId);
132
+ const bootEntries = branch.filter((entry) => entry.type === "custom_message" && entry.customType === BOOT_TYPE && entry.details && typeof entry.details === "object" && entry.details.windowId === windowId);
133
+ const continuationEntries = branch.filter((entry) => entry.type === "custom_message" && entry.customType === CONTINUATION_TYPE);
134
+ assert.equal(bootEntries.length, 1, "an incomplete marker tail gets one repaired boot");
135
+ assert.equal(continuationEntries.length, 1, "the same repair completes the missing continuation");
136
+ assert.ok(branch.findIndex((entry) => entry.id === modelChangeId) > markerIndex, "metadata remains after the marker");
137
+ assert.ok(branch.findIndex((entry) => entry.id === bootEntries[0]?.id) > markerIndex, "the repaired boot remains in the marked window");
138
+ assert.ok(branch.findIndex((entry) => entry.id === continuationEntries[0]?.id) > branch.findIndex((entry) => entry.id === bootEntries[0]?.id), "the continuation follows its boot");
139
+ assert.equal(captured.sent.length, 2, "repair emits only the missing boot and continuation, without a model turn");
140
+ assert.equal(captured.sent[0]?.message.customType, BOOT_TYPE);
141
+ assert.equal(captured.sent[1]?.message.customType, CONTINUATION_TYPE);
142
+ assert.equal(noticesOf(ctx).length, 0, "repairing a reset tail is not a new reset");
143
+ });
144
+ test("a bare marker tail repairs the full ordered reset shape", async () => {
145
+ const sessionManager = manager(true);
146
+ const windowId = "pcw:bare-marker";
147
+ const markerId = sessionManager.appendCustomEntry(RESET_MARKER_TYPE, { windowId });
148
+ const captured = makeExtension(sessionManager);
149
+ const ctx = context(sessionManager);
150
+ await runHandlers(captured, "session_start", { reason: "startup" }, ctx);
151
+ assert.equal(captured.sent.length, 2, "a bare marker emits a boot and a continuation");
152
+ const branch = sessionManager.getBranch();
153
+ const markerIndex = branch.findIndex((entry) => entry.id === markerId);
154
+ const bootIndex = branch.findIndex((entry) => entry.type === "custom_message" && entry.customType === BOOT_TYPE && entry.details?.windowId === windowId);
155
+ const continuationIndex = branch.findIndex((entry) => entry.type === "custom_message" && entry.customType === CONTINUATION_TYPE);
156
+ assert.ok(bootIndex > markerIndex, "the repaired boot follows its marker");
157
+ assert.ok(continuationIndex > bootIndex, "the repaired continuation follows its boot");
158
+ });
159
+ test("a marker tail that already carries a boot repairs only its missing continuation, once", async () => {
160
+ const sessionManager = manager(true);
161
+ const windowId = "pcw:missing-continuation";
162
+ const markerId = sessionManager.appendCustomEntry(RESET_MARKER_TYPE, { windowId });
163
+ sessionManager.appendModelChange("openai", "scripted-model");
164
+ sessionManager.appendCustomMessageEntry(BOOT_TYPE, "already-persisted boot", false, { windowId });
165
+ const captured = makeExtension(sessionManager);
166
+ const ctx = context(sessionManager);
167
+ await runHandlers(captured, "session_start", { reason: "startup" }, ctx);
168
+ assert.equal(captured.sent.length, 1, "only the missing continuation is emitted");
169
+ assert.equal(captured.sent[0]?.message.customType, CONTINUATION_TYPE);
170
+ assert.equal(sessionManager.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === BOOT_TYPE).length, 1, "the existing boot is never duplicated");
171
+ assert.ok(sessionManager.getBranch().findIndex((entry) => entry.id === markerId) >= 0);
172
+ await runHandlers(captured, "session_start", { reason: "reload" }, ctx);
173
+ assert.equal(captured.sent.length, 1, "a complete reset tail is idempotent");
174
+ assert.equal(sessionManager.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === CONTINUATION_TYPE).length, 1);
175
+ });
176
+ test("a marker tail followed by real conversation is never repaired", async () => {
177
+ const sessionManager = manager(true);
178
+ const windowId = "pcw:unsafe-tail";
179
+ sessionManager.appendCustomEntry(RESET_MARKER_TYPE, { windowId });
180
+ appendText(sessionManager, "user", "post-marker work that must not be pushed behind a late boot");
181
+ const captured = makeExtension(sessionManager);
182
+ const ctx = context(sessionManager);
183
+ await runHandlers(captured, "session_start", { reason: "startup" }, ctx);
184
+ assert.equal(captured.sent.length, 0, "an unsafe marker tail emits nothing");
185
+ assert.equal(sessionManager.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === BOOT_TYPE).length, 0, "no boot is appended after real work");
186
+ assert.equal(sessionManager.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === CONTINUATION_TYPE).length, 0, "no continuation is appended after real work");
187
+ });
188
+ test("off preserves an existing marker window and still cancels native compaction", async () => {
189
+ const sessionManager = manager();
190
+ const captured = makeExtension(sessionManager);
191
+ const ctx = context(sessionManager);
192
+ appendText(sessionManager, "user", "old context must stay hidden");
193
+ const windowId = "pcw:test:existing";
194
+ sessionManager.appendCustomEntry(RESET_MARKER_TYPE, { windowId });
195
+ sessionManager.appendCustomMessageEntry(BOOT_TYPE, "fresh boot", false, { windowId });
196
+ await runCommand(captured, "notesoup", "off", ctx);
197
+ const before = await runManualCompact(captured, ctx);
198
+ assert.deepEqual(before, { cancel: true });
199
+ const projected = await runContextWithSystemHook(captured, ctx, sessionManager.buildSessionContext().messages);
200
+ assert.equal(JSON.stringify(projected?.messages ?? []).includes("old context must stay hidden"), false);
201
+ const windows = resultData(await call(captured, "history_windows", {}, ctx));
202
+ assert.equal(windows.windows.at(-1)?.window_id, windowId);
203
+ });
204
+ test("clear_memory uses one turn boundary and never calls ctx.compact", async () => {
205
+ const sessionManager = manager();
206
+ const captured = makeExtension(sessionManager);
207
+ const ctx = context(sessionManager, () => assert.fail("ctx.compact must not be used"));
208
+ appendText(sessionManager, "user", "enough history for the boundary test");
209
+ const result = resultJson(await call(captured, "clear_memory", {}, ctx));
210
+ assert.ok(result.status);
211
+ const boundary = await commitTurnEndBoundary(captured, sessionManager, ctx);
212
+ assert.equal(boundary.continue, true);
213
+ assert.equal(sessionManager.getBranch().filter((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE).length, 1);
214
+ assert.equal(sessionManager.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === BOOT_TYPE && entry.details && typeof entry.details === "object" && "windowId" in entry.details).length, 1);
215
+ });
216
+ test("/clear-memory waits out non-agent busy work before starting its single-turn request", async () => {
217
+ const sessionManager = manager();
218
+ const captured = makeExtension(sessionManager);
219
+ const cmd = captured.commands.get("clear-memory");
220
+ assert.ok(cmd);
221
+ let waits = 0;
222
+ const notices = [];
223
+ const ctx = Object.assign(context(sessionManager, undefined, undefined, false), {
224
+ signal: undefined,
225
+ waitForIdle: async () => { waits++; },
226
+ ui: { notify: (message) => notices.push(message) },
227
+ });
228
+ await cmd.handler("", ctx);
229
+ assert.equal(waits, 1);
230
+ assert.equal(captured.sent.length, 1);
231
+ assert.equal(captured.sent[0]?.options?.triggerTurn, true);
232
+ assert.equal(notices.filter((message) => message.includes("received; the agent closes out its notes")).length, 1);
233
+ });
234
+ test("notesoup command toggles future work, /clear-memory schedules a turn-end reset, and /compact remains disabled", async () => {
235
+ const sessionManager = manager();
236
+ appendText(sessionManager, "user", "hello");
237
+ const captured = makeExtension(sessionManager);
238
+ const low = context(sessionManager, undefined, { tokens: 170_000, contextWindow: 200_000, percent: 85 });
239
+ // On by default: session_start persists the root boot block.
240
+ await runHandlers(captured, "session_start", { reason: "startup" }, low);
241
+ assert.equal(captured.sent.length, 1);
242
+ assert.equal(captured.sent[0]?.message.customType, BOOT_TYPE);
243
+ let notices = await runCommand(captured, "notesoup", "off", low);
244
+ assert.match(notices[0]?.message ?? "", /off/);
245
+ assert.equal(await runContextHook(captured, low), undefined, "no guidance while off");
246
+ assert.equal(sentOf(captured, GUIDANCE_TYPE).length, 0, "no guidance persisted while off");
247
+ await runHandlers(captured, "session_start", { reason: "startup" }, low);
248
+ assert.equal(captured.sent.length, 1, "no new boot block is persisted while off");
249
+ const offResult = resultJson(await call(captured, "clear_memory", {}, low));
250
+ assert.match(offResult.error ?? "", /off/, "clear_memory refuses while off");
251
+ notices = await runCommand(captured, "notesoup", "on", low);
252
+ assert.match(notices[0]?.message ?? "", /on/);
253
+ notices = await runCommand(captured, "clear-memory", "", low);
254
+ assert.equal(notices.length, 1, "the command acknowledges the reset request");
255
+ assert.match(notices[0]?.message ?? "", /closes out its notes/);
256
+ assert.equal(captured.sent.length, 2, "the command persists one hidden warning after the startup boot");
257
+ assert.equal(captured.sent[1]?.options?.triggerTurn, true, "the manual checkpoint prompt starts an ordinary agent turn");
258
+ assert.equal(captured.sent[1]?.message.customType, MANUAL_WIPE_TYPE);
259
+ assert.equal(captured.sent[1]?.message.display, false);
260
+ assert.equal(sessionManager.getBranch().filter((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE).length, 0, "no reset is claimed until close-out completes");
261
+ const markerContext = await runManualCompact(captured, low);
262
+ assert.deepEqual(markerContext, { cancel: true }, "/compact is canceled while context windows are enabled");
263
+ const projected = await runContextWithSystemHook(captured, low, sessionManager.buildSessionContext().messages);
264
+ assert.equal(JSON.stringify(projected?.messages ?? []).includes("hello"), true, "the old window remains active until the close-out commits");
265
+ notices = await runCommand(captured, "notesoup", "maybe", low);
266
+ assert.equal(notices[0]?.type, "error", "unknown argument rejected");
267
+ // Bare command reports current state without changing it.
268
+ notices = await runCommand(captured, "notesoup", "", low);
269
+ assert.match(notices[0]?.message ?? "", /on/);
270
+ });
271
+ test("committed reset confirmation survives later work while repair refuses that suffix", async () => {
272
+ const sessionManager = manager();
273
+ const captured = makeExtension(sessionManager);
274
+ const ctx = context(sessionManager);
275
+ await call(captured, "clear_memory", {}, ctx);
276
+ await commitTurnEndBoundary(captured, sessionManager, ctx);
277
+ const marker = sessionManager.getBranch().find((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE);
278
+ assert.ok(marker && marker.type === "custom");
279
+ const windowId = marker.data.windowId;
280
+ assert.equal(resetBoundaryCommitted(ctx, marker.id, windowId), true);
281
+ appendText(sessionManager, "user", "work after the completed boundary");
282
+ assert.equal(inspectResetTail(ctx, marker.id, windowId), undefined, "conversation refuses suffix repair");
283
+ assert.equal(resetBoundaryCommitted(ctx, marker.id, windowId), true, "later work does not undo a committed native boundary");
284
+ await runHandlers(captured, "agent_settled", {}, ctx);
285
+ await runHandlers(captured, "turn_start", {}, ctx);
286
+ assert.equal(noticesOf(ctx).filter((notice) => notice.message.includes("memory cleared")).length, 1);
287
+ assert.equal(resetBoundaryCommitted(ctx, marker.id, "wrong-window"), false);
288
+ const partial = manager();
289
+ const partialCtx = context(partial);
290
+ const partialMarker = partial.appendCustomEntry(RESET_MARKER_TYPE, { windowId });
291
+ partial.appendCustomMessageEntry(BOOT_TYPE, "boot", false, { windowId });
292
+ partial.appendCustomMessageEntry(CONTINUATION_TYPE, "continuation", false);
293
+ assert.equal(resetBoundaryCommitted(partialCtx, partialMarker, windowId), false, "a bare legacy marker is not evidence of a new checkpoint commit");
294
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,144 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { DEFAULT_REMINDER_MARGIN_TOKENS, DEFAULT_RESERVE_TOKENS } from "../src/budget/constants.js";
4
+ import { GUIDANCE_TYPE, WARNING_TYPE } from "../src/pi/entries.js";
5
+ import { NOTESOUP_SETTINGS_KEY } from "../src/settings.js";
6
+ import { appendText, call, commitTurnEndBoundary, context, makeExtension, manager, noticesOf, resultJson, runContextHook, runHandlers, sentOf, settingsFixture, } from "./helpers/extension.js";
7
+ import { installExtensionTestHooks } from "./helpers/extension-test-environment.js";
8
+ const testEnvironment = installExtensionTestHooks("notesoup-integration");
9
+ test("low-budget guidance and warning persist at turn_end, once per active window", async () => {
10
+ const sessionManager = manager();
11
+ const captured = makeExtension(sessionManager);
12
+ const low = context(sessionManager, undefined, { tokens: 165_000, percent: 82.5, contextWindow: 200_000 });
13
+ assert.equal(await runContextHook(captured, low), undefined, "guidance is staged, not injected into this request");
14
+ assert.equal(captured.sent.length, 0, "guidance does not trigger a detached turn");
15
+ const guidanceBoundary = await commitTurnEndBoundary(captured, sessionManager, low);
16
+ assert.equal(guidanceBoundary.entries.filter((entry) => entry.type === "custom_message" && entry.customType === GUIDANCE_TYPE).length, 1);
17
+ assert.equal(sessionManager.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === GUIDANCE_TYPE).length, 1);
18
+ assert.equal(await runContextHook(captured, low), undefined, "the same window does not repeat guidance");
19
+ const warningContext = context(sessionManager, undefined, { tokens: 199_000, percent: 99.5, contextWindow: 200_000 });
20
+ const warning = await runContextHook(captured, warningContext);
21
+ assert.equal(warning?.messages.length, 1, "the warning is visible in the current provider request");
22
+ assert.equal((warning?.messages[0]).customType, WARNING_TYPE);
23
+ const warningBoundary = await commitTurnEndBoundary(captured, sessionManager, warningContext);
24
+ assert.equal(warningBoundary.entries.filter((entry) => entry.type === "custom_message" && entry.customType === WARNING_TYPE).length, 1);
25
+ assert.equal(sessionManager.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === WARNING_TYPE).length, 1);
26
+ });
27
+ test("early guidance stays silent and the committed final warning notifies once", async () => {
28
+ const sm = manager();
29
+ const captured = makeExtension(sm);
30
+ const early = context(sm, undefined, { tokens: 165_000, percent: 82.5, contextWindow: 200_000 });
31
+ assert.equal(await runContextHook(captured, early), undefined, "early guidance is persisted at turn_end, not injected immediately");
32
+ await commitTurnEndBoundary(captured, sm, early);
33
+ await runHandlers(captured, "agent_settled", {}, early);
34
+ assert.equal(noticesOf(early).filter((notice) => notice.message.includes("Context running low") || notice.message.includes("Context almost full")).length, 0, "early model guidance has no UI toast");
35
+ assert.equal(sm.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === GUIDANCE_TYPE).length, 1, "early guidance remains durable for the model");
36
+ const final = context(sm, undefined, { tokens: 181_000, percent: 90.5, contextWindow: 200_000 });
37
+ const warning = await runContextHook(captured, final);
38
+ assert.equal(warning?.messages[0]?.customType, WARNING_TYPE);
39
+ await commitTurnEndBoundary(captured, sm, final);
40
+ await runHandlers(captured, "turn_start", {}, final);
41
+ await runHandlers(captured, "agent_settled", {}, final);
42
+ assert.equal(noticesOf(final).filter((notice) => notice.message === "notesoup: context almost full; close out the current memory window.").length, 1, "only the committed final warning notifies, exactly once");
43
+ });
44
+ test("the visible countdown ends at the warning line, clamps at zero, and preserves unknown usage", async () => {
45
+ const fixture = settingsFixture({
46
+ reserveTokens: 16_384,
47
+ project: { compaction: { reserveTokens: 32_768 } },
48
+ });
49
+ const sm = manager();
50
+ const captured = makeExtension(sm);
51
+ await runHandlers(captured, "session_tree", {}, context(sm, undefined, undefined, true, fixture.cwd, true));
52
+ const readBudget = async (tokens, trusted = true) => {
53
+ const ctx = context(sm, undefined, { tokens, contextWindow: 200_000, percent: tokens === null ? null : tokens / 2000 }, true, fixture.cwd, trusted);
54
+ return resultJson(await call(captured, "get_context_remaining", {}, ctx)).remaining_tokens;
55
+ };
56
+ assert.equal(await readBudget(72_563), 78_285, "the reported 127437 physical tokens exclude reserve plus runway (49152)");
57
+ assert.equal(await readBudget(167_232), 0, "inside the runway the countdown reads zero");
58
+ assert.equal(await readBudget(190_000), 0, "below the reserve, still zero");
59
+ assert.equal(await readBudget(210_000), 0, "over the physical window");
60
+ assert.equal(await readBudget(null), null, "unknown usage remains unknown");
61
+ const absent = context(sm, undefined, undefined, true, fixture.cwd);
62
+ assert.equal(resultJson(await call(captured, "get_context_remaining", {}, absent)).remaining_tokens, null);
63
+ const untrusted = context(sm, undefined, { tokens: 72_563, contextWindow: 200_000, percent: 36.2815 }, true, fixture.cwd, false);
64
+ await runHandlers(captured, "session_start", {}, untrusted);
65
+ assert.equal(await readBudget(72_563, false), 94_669, "session start reloads the global reserve when the project is untrusted");
66
+ });
67
+ test("absent notesoup key or margins reproduce the default reminder threshold at Pi's default reserve", async () => {
68
+ assert.equal(DEFAULT_RESERVE_TOKENS, 16_384);
69
+ assert.equal(DEFAULT_RESERVE_TOKENS + DEFAULT_REMINDER_MARGIN_TOKENS, 40_960);
70
+ for (const [label, options] of [
71
+ ["absent key", { global: {} }],
72
+ ["absent margins", { global: { [NOTESOUP_SETTINGS_KEY]: {} } }],
73
+ ]) {
74
+ const fixture = settingsFixture(options);
75
+ const sm = manager();
76
+ const captured = makeExtension(sm);
77
+ // Thresholds are resolved once per session and cached; branch navigation clears the
78
+ // cache without emitting a boot block, so the next read uses this fixture.
79
+ await runHandlers(captured, "session_tree", {}, context(sm, undefined, undefined, true, fixture.cwd));
80
+ const window = 200_000;
81
+ const at = (remaining) => context(sm, undefined, { tokens: window - remaining, percent: 0, contextWindow: window }, true, fixture.cwd);
82
+ const first = at(40_961);
83
+ assert.equal(await runContextHook(captured, first), undefined, `${label}: nothing injected above the default reminder`);
84
+ assert.equal(sentOf(captured, GUIDANCE_TYPE).length, 0, `${label}: no guidance above the default reminder`);
85
+ const crossing = at(40_960);
86
+ assert.equal(await runContextHook(captured, crossing), undefined, `${label}: default reminder crossing persists only`);
87
+ await commitTurnEndBoundary(captured, sm, crossing);
88
+ assert.equal(sm.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === GUIDANCE_TYPE).length, 1, `${label}: default reminder fires`);
89
+ assert.equal(noticesOf(first).length, 0, `${label}: valid defaults warn nobody`);
90
+ }
91
+ });
92
+ test("project notesoup reminder margin and reserve override global per key", async () => {
93
+ const fixture = settingsFixture({
94
+ reserveTokens: 20_000,
95
+ global: { [NOTESOUP_SETTINGS_KEY]: { reminderMarginTokens: 30_000 } },
96
+ project: { compaction: { reserveTokens: 50_000 }, [NOTESOUP_SETTINGS_KEY]: { reminderMarginTokens: 40_000 } },
97
+ });
98
+ // Project reserve wins: reminder = 50000 + 40000 (project margin).
99
+ const sm = manager();
100
+ const captured = makeExtension(sm);
101
+ await runHandlers(captured, "session_tree", {}, context(sm, undefined, undefined, true, fixture.cwd));
102
+ const window = 300_000;
103
+ const at = (remaining) => context(sm, undefined, { tokens: window - remaining, percent: 0, contextWindow: window }, true, fixture.cwd);
104
+ assert.equal(await runContextHook(captured, at(90_001)), undefined, "nothing injected above the project-derived reminder");
105
+ assert.equal(sentOf(captured, GUIDANCE_TYPE).length, 0);
106
+ const crossing = at(90_000);
107
+ assert.equal(await runContextHook(captured, crossing), undefined, "project-derived reminder crossing persists only");
108
+ await commitTurnEndBoundary(captured, sm, crossing);
109
+ assert.equal(sm.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === GUIDANCE_TYPE).length, 1, "project reminder margin wins");
110
+ });
111
+ test("an invalid reminder margin degrades to its default with one warning and never throws", async () => {
112
+ const fixture = settingsFixture({ global: { [NOTESOUP_SETTINGS_KEY]: { reminderMarginTokens: 0 } } });
113
+ const sm = manager();
114
+ const captured = makeExtension(sm);
115
+ const ctx = context(sm, undefined, undefined, true, fixture.cwd);
116
+ await assert.doesNotReject(() => runHandlers(captured, "session_start", { reason: "startup" }, ctx));
117
+ const notices = noticesOf(ctx).filter((notice) => notice.type === "warning");
118
+ assert.equal(notices.length, 1, "one warning for the offending key");
119
+ assert.equal(notices[0]?.type, "warning");
120
+ assert.match(notices[0]?.message ?? "", /reminderMarginTokens/);
121
+ assert.match(notices[0]?.message ?? "", /default reminder margin/);
122
+ assert.equal(/\d+\s*k?\s*(remaining|tokens)/i.test(notices[0]?.message ?? ""), false, "UI notices do not expose token counts");
123
+ const window = 200_000;
124
+ const at = (remaining) => context(sm, undefined, { tokens: window - remaining, percent: 0, contextWindow: window }, true, fixture.cwd);
125
+ // The degraded reminder is Pi's default reserve + default margin = 40960.
126
+ assert.equal(await runContextHook(captured, at(40_961)), undefined, "nothing injected above the degraded reminder");
127
+ const crossing = at(40_960);
128
+ assert.equal(await runContextHook(captured, crossing), undefined, "degraded reminder uses its default");
129
+ await commitTurnEndBoundary(captured, sm, crossing);
130
+ assert.equal(sm.getBranch().filter((entry) => entry.type === "custom_message" && entry.customType === GUIDANCE_TYPE).length, 1, "the degraded reminder is persisted once");
131
+ assert.equal(noticesOf(ctx).filter((notice) => notice.type === "warning").length, 1, "warning stays one-time across handler calls");
132
+ });
133
+ test("the warning supersedes the early reminder when usage jumps across both thresholds", async () => {
134
+ const sm = manager();
135
+ appendText(sm, "user", "ongoing work");
136
+ const captured = makeExtension(sm);
137
+ const ctx = context(sm, undefined, { tokens: 199_000, percent: 99.5, contextWindow: 200_000 }, false);
138
+ const warningResult = await runContextHook(captured, ctx);
139
+ assert.deepEqual(warningResult?.messages.map((message) => message.customType), [WARNING_TYPE]);
140
+ await commitTurnEndBoundary(captured, sm, ctx);
141
+ const reloaded = makeExtension(sm);
142
+ await runHandlers(reloaded, "context", {}, ctx);
143
+ assert.equal(reloaded.sent.length, 0, "persisted warning also suppresses a late reminder after reload");
144
+ });
@@ -0,0 +1 @@
1
+ export {};