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,605 @@
1
+ import assert from "node:assert/strict";
2
+ import { mkdtempSync, rmSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import test from "node:test";
6
+ import { SessionManager as Manager } from "@earendil-works/pi-coding-agent";
7
+ import notesoup from "../src/pi/extension.js";
8
+ import { BOOT_TYPE, CONTINUATION_TYPE, GUIDANCE_TYPE, RESET_MARKER_TYPE } from "../src/pi/entries.js";
9
+ import { MANUAL_WIPE_TYPE } from "../src/pi/entries.js";
10
+ import { toolContext } from "./helpers/extension.js";
11
+ import { WARNING_CONTENT } from "../src/pi/reset/text.js";
12
+ import { initialResetControl, reduceResetControl, registerResetLifecycle, } from "../src/pi/reset/lifecycle.js";
13
+ const previousNotesHome = process.env.PI_NOTES_HOME;
14
+ const testNotesHome = mkdtempSync(join(tmpdir(), "notesoup-lifecycle-notes-"));
15
+ process.env.PI_NOTES_HOME = testNotesHome;
16
+ test.after(() => {
17
+ if (previousNotesHome === undefined)
18
+ delete process.env.PI_NOTES_HOME;
19
+ else
20
+ process.env.PI_NOTES_HOME = previousNotesHome;
21
+ rmSync(testNotesHome, { recursive: true, force: true });
22
+ });
23
+ function harness() {
24
+ const sessionManager = Manager.inMemory("/private/tmp/notesoup-lifecycle-test");
25
+ const handlers = new Map();
26
+ const tools = new Map();
27
+ const commands = new Map();
28
+ const sent = [];
29
+ const notices = [];
30
+ const api = {
31
+ on(name, handler) {
32
+ const list = handlers.get(name) ?? [];
33
+ list.push(handler);
34
+ handlers.set(name, list);
35
+ return () => { };
36
+ },
37
+ registerTool(tool) { tools.set(tool.name, tool); },
38
+ registerCommand(name, command) { commands.set(name, command); },
39
+ registerFlag() { },
40
+ appendEntry(customType, data) { sessionManager.appendCustomEntry(customType, data); },
41
+ sendMessage(message, options) {
42
+ sent.push({ customType: message.customType, details: message.details, triggerTurn: options?.triggerTurn });
43
+ sessionManager.appendCustomMessageEntry(message.customType, message.content, message.display, message.details);
44
+ },
45
+ };
46
+ notesoup(api);
47
+ const ctx = {
48
+ sessionManager,
49
+ cwd: "/private/tmp",
50
+ model: undefined,
51
+ isIdle: () => true,
52
+ hasPendingMessages: () => false,
53
+ signal: undefined,
54
+ getContextUsage: () => undefined,
55
+ compact: () => assert.fail("the reset path must not call ctx.compact()"),
56
+ abort: () => { },
57
+ isProjectTrusted: () => true,
58
+ ui: { notify: (message, type) => notices.push({ message, type }) },
59
+ };
60
+ const emit = async (name, event) => {
61
+ const results = [];
62
+ for (const handler of handlers.get(name) ?? [])
63
+ results.push(await handler(event, ctx));
64
+ return results;
65
+ };
66
+ const runCommand = async (name, args = "") => {
67
+ const command = commands.get(name);
68
+ assert.ok(command, `${name} command is registered`);
69
+ const commandCtx = Object.assign({}, ctx, {
70
+ waitForIdle: async () => { },
71
+ ui: { notify: (message, type) => notices.push({ message, type }) },
72
+ });
73
+ await command.handler(args, commandCtx);
74
+ };
75
+ const callTool = async (name) => {
76
+ const tool = tools.get(name);
77
+ assert.ok(tool, `${name} is registered`);
78
+ // Pi 1.0 hands a tool the session context plus the nested-call surface; toolContext builds it.
79
+ return tool.execute("call", {}, new AbortController().signal, () => { }, toolContext(ctx));
80
+ };
81
+ return { sessionManager, handlers, sent, notices, emit, runCommand, callTool };
82
+ }
83
+ function resultEntries(results) {
84
+ let entries = [];
85
+ let shouldContinue = false;
86
+ for (const result of results) {
87
+ if (!result || typeof result !== "object")
88
+ continue;
89
+ const value = result;
90
+ if (value.entries)
91
+ entries = value.entries;
92
+ if (value.continue !== undefined)
93
+ shouldContinue = value.continue;
94
+ }
95
+ return { entries, continue: shouldContinue };
96
+ }
97
+ function appendDrafts(sessionManager, entries) {
98
+ for (const entry of entries) {
99
+ switch (entry.type) {
100
+ case "custom":
101
+ sessionManager.appendCustomEntry(entry.customType, entry.data);
102
+ break;
103
+ case "custom_message":
104
+ sessionManager.appendCustomMessageEntry(entry.customType, entry.content, entry.display, entry.details);
105
+ break;
106
+ case "context_edit":
107
+ sessionManager.appendContextEdit(entry.targetId, entry.replacement);
108
+ break;
109
+ case "compaction":
110
+ sessionManager.appendCompaction(entry.summary, entry.firstKeptEntryId, 0, entry.details, true, entry.usage);
111
+ break;
112
+ }
113
+ }
114
+ }
115
+ function turnEndFacts(overrides = {}) {
116
+ return {
117
+ windowId: "pcw:test:window",
118
+ aborted: false,
119
+ overflow: false,
120
+ failed: false,
121
+ enabled: true,
122
+ queued: false,
123
+ automaticResetEnabled: true,
124
+ hardReserveDue: false,
125
+ ...overrides,
126
+ };
127
+ }
128
+ function beforeSettleFacts(overrides = {}) {
129
+ return {
130
+ windowId: "pcw:test:window",
131
+ queued: false,
132
+ enabled: true,
133
+ automaticResetEnabled: true,
134
+ aborted: false,
135
+ failed: false,
136
+ ...overrides,
137
+ };
138
+ }
139
+ function fakeBoundaryEvent(entries = [], message) {
140
+ return {
141
+ type: "turn_end",
142
+ entries,
143
+ continue: false,
144
+ context: { contextEntries: [], contextMessages: [], llmMessages: [], pendingMessages: [], canContinue: true },
145
+ outcome: "completed",
146
+ turnIndex: 0,
147
+ message: message ?? { role: "assistant", content: [], timestamp: Date.now() },
148
+ toolResults: [],
149
+ messageEntryId: "assistant-entry",
150
+ toolResultEntryIds: [],
151
+ };
152
+ }
153
+ test("public reset boundary drafts one marker, one boot, and one continuation after a tool batch", async () => {
154
+ const h = harness();
155
+ h.sessionManager.appendMessage({ role: "user", content: [{ type: "text", text: "before reset" }], timestamp: Date.now() });
156
+ const first = await h.callTool("clear_memory");
157
+ const second = await h.callTool("clear_memory");
158
+ assert.ok(first.content.length > 0 && second.content.length > 0, "both tool calls return normally");
159
+ const toolBatch = [{ type: "custom_message", customType: "foreign/tool-batch", content: "tool finished", display: false }];
160
+ const boundary = resultEntries(await h.emit("turn_end", fakeBoundaryEvent(toolBatch)));
161
+ assert.equal(boundary.continue, true, "the whole tool batch continues only after the boundary is committed");
162
+ const markerDrafts = boundary.entries.filter((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE);
163
+ const bootDrafts = boundary.entries.filter((entry) => entry.type === "custom_message" && entry.customType === BOOT_TYPE);
164
+ assert.equal(markerDrafts.length, 1, "duplicate wipe requests in one turn dedupe");
165
+ assert.equal(bootDrafts.length, 1);
166
+ assert.equal(boundary.entries[0]?.type, "custom_message", "ordinary tool-batch entries precede the reset drafts");
167
+ assert.equal(boundary.entries[1]?.type, "compaction", "native retain-none checkpoint precedes the reset marker");
168
+ assert.equal(boundary.entries[2]?.type, "custom");
169
+ assert.equal(boundary.entries[3]?.type, "custom_message");
170
+ const windowId = markerDrafts[0].data.windowId;
171
+ assert.match(windowId, /^pcw:/);
172
+ assert.equal(bootDrafts[0].details.windowId, windowId);
173
+ const continuationDrafts = boundary.entries.filter((entry) => entry.type === "custom_message" && entry.customType === CONTINUATION_TYPE);
174
+ assert.equal(continuationDrafts.length, 1, "the boundary persists exactly one reset message");
175
+ assert.equal(boundary.entries[4]?.type, "custom_message", "the continuation closes the ordered reset shape");
176
+ appendDrafts(h.sessionManager, boundary.entries);
177
+ const branch = h.sessionManager.getBranch();
178
+ assert.deepEqual(branch.filter((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE).map((entry) => entry.type === "custom" ? entry.data : undefined), [{ windowId }]);
179
+ assert.equal(branch.filter((entry) => entry.type === "custom_message" && entry.customType === BOOT_TYPE).length, 1);
180
+ });
181
+ test("manual /clear-memory spans tool turns and commits the stop at settlement", async () => {
182
+ const h = harness();
183
+ await h.runCommand("clear-memory");
184
+ assert.ok(h.sent.some((message) => message.customType === MANUAL_WIPE_TYPE && message.triggerTurn === true), "the idle close-out warning starts a turn");
185
+ const toolCallMessage = {
186
+ role: "assistant",
187
+ content: [{ type: "toolCall", id: "call-1", name: "notes_write", arguments: {} }],
188
+ timestamp: Date.now(),
189
+ };
190
+ const midBatch = resultEntries(await h.emit("turn_end", fakeBoundaryEvent([], toolCallMessage)));
191
+ assert.equal(midBatch.entries.length, 0, "a tool-call turn does not commit a manual close-out");
192
+ const cleanTurn = resultEntries(await h.emit("turn_end", fakeBoundaryEvent()));
193
+ assert.equal(cleanTurn.entries.length, 0, "even a clean turn end only keeps the close-out armed");
194
+ const settle = resultEntries(await h.emit("agent_before_settle", {
195
+ type: "agent_before_settle",
196
+ entries: [],
197
+ outcome: "completed",
198
+ context: { contextEntries: [], contextMessages: [], llmMessages: [], pendingMessages: [], canContinue: true },
199
+ }));
200
+ const markers = settle.entries.filter((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE);
201
+ assert.equal(markers.length, 1, "the reset commits at settlement");
202
+ assert.equal(settle.continue, false, "a manual wipe does not continue after the reset");
203
+ });
204
+ test("off stops future automatic/manual reset requests while an existing marker remains authoritative", async () => {
205
+ const h = harness();
206
+ await h.callTool("clear_memory");
207
+ const first = resultEntries(await h.emit("turn_end", fakeBoundaryEvent()));
208
+ assert.ok(first.entries.some((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE));
209
+ appendDrafts(h.sessionManager, first.entries);
210
+ await h.runCommand("notesoup", "off");
211
+ const before = await h.emit("session_before_compact", {
212
+ type: "session_before_compact",
213
+ reason: "manual",
214
+ willRetry: false,
215
+ branchEntries: h.sessionManager.getBranch(),
216
+ preparation: { tokensBefore: 100, firstKeptEntryId: null, keptMessages: [], droppedMessages: [] },
217
+ signal: new AbortController().signal,
218
+ });
219
+ assert.deepEqual(before.at(-1), { cancel: true }, "/compact is canceled when an existing marker would expose old canonical history");
220
+ const afterOff = resultEntries(await h.emit("turn_end", fakeBoundaryEvent()));
221
+ assert.equal(afterOff.entries.length, 0, "off does not create another reset");
222
+ await h.runCommand("notesoup", "on");
223
+ await h.runCommand("clear-memory");
224
+ assert.equal(h.sent.length, 1, "/clear-memory sends one hidden close-out warning and starts a normal turn");
225
+ assert.equal(h.sent[0]?.triggerTurn, true);
226
+ assert.equal(h.sent[0]?.customType, MANUAL_WIPE_TYPE);
227
+ const warning = h.sessionManager.getBranch().find((entry) => entry.type === "custom_message" && entry.customType === MANUAL_WIPE_TYPE);
228
+ assert.ok(warning && warning.type === "custom_message");
229
+ assert.equal(warning.content, WARNING_CONTENT);
230
+ const markers = h.sessionManager.getBranch().filter((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE);
231
+ assert.equal(markers.length, 1, "manual command waits for an agent boundary; it does not persist an immediate reset");
232
+ });
233
+ test("reset construction failure preserves incoming and budget drafts without continuation", async () => {
234
+ const sessionManager = Manager.inMemory("/private/tmp/notesoup-reset-failure-test");
235
+ const handlers = new Map();
236
+ const notices = [];
237
+ const api = {
238
+ on(name, handler) {
239
+ const list = handlers.get(name) ?? [];
240
+ list.push(handler);
241
+ handlers.set(name, list);
242
+ return () => { };
243
+ },
244
+ };
245
+ const ctx = {
246
+ sessionManager,
247
+ model: undefined,
248
+ signal: undefined,
249
+ hasPendingMessages: () => false,
250
+ ui: { notify: (message, type) => notices.push({ message, type }) },
251
+ };
252
+ const budgetDraft = { type: "custom_message", customType: GUIDANCE_TYPE, content: "budget draft", display: false };
253
+ const lifecycle = registerResetLifecycle(api, {
254
+ isEnabled: () => true,
255
+ budget: {
256
+ automaticResetEnabled: () => true,
257
+ hardReserveDue: () => false,
258
+ consumeTurnEnd: () => [budgetDraft],
259
+ clear: () => { },
260
+ },
261
+ buildReset: (_ctx, _isCurrent, prompt) => { assert.equal(prompt, "AFTER_RESET"); throw new Error("synthetic reset construction failure"); },
262
+ });
263
+ lifecycle.closeOut(`pcw:${sessionManager.getSessionId().slice(0, 8)}:root`, "manual", "AFTER_RESET");
264
+ const incoming = { type: "custom_message", customType: "foreign/boundary", content: "foreign draft", display: false };
265
+ const results = [];
266
+ for (const handler of handlers.get("turn_end") ?? [])
267
+ results.push(await handler(fakeBoundaryEvent([incoming]), ctx));
268
+ for (const handler of handlers.get("agent_before_settle") ?? [])
269
+ results.push(await handler(fakeBoundaryEvent([]), ctx));
270
+ const result = resultEntries(results);
271
+ assert.deepEqual(result.entries, [incoming, budgetDraft], "already-built drafts survive reset construction failure");
272
+ assert.equal(result.continue, false, "a failed reset does not request continuation");
273
+ assert.equal(notices.at(-1)?.type, "warning");
274
+ assert.match(notices.at(-1)?.message ?? "", /could not build reset/);
275
+ assert.match(notices.at(-1)?.message ?? "", /follow-up prompt was not executed/);
276
+ });
277
+ test("a stale async reset is discarded after a lifecycle switch without staging a notice", async () => {
278
+ const sessionManager = Manager.inMemory("/private/tmp/notesoup-stale-reset-test");
279
+ const handlers = new Map();
280
+ const api = {
281
+ on(name, handler) {
282
+ const list = handlers.get(name) ?? [];
283
+ list.push(handler);
284
+ handlers.set(name, list);
285
+ return () => { };
286
+ },
287
+ };
288
+ const ctx = {
289
+ sessionManager,
290
+ model: undefined,
291
+ signal: undefined,
292
+ hasPendingMessages: () => false,
293
+ ui: { notify() { } },
294
+ };
295
+ let resolveBuild;
296
+ const pendingBuild = new Promise((resolve) => { resolveBuild = resolve; });
297
+ let readyCount = 0;
298
+ const lifecycle = registerResetLifecycle(api, {
299
+ isEnabled: () => true,
300
+ budget: {
301
+ automaticResetEnabled: () => true,
302
+ hardReserveDue: () => false,
303
+ consumeTurnEnd: () => [],
304
+ clear: () => { },
305
+ },
306
+ buildReset: () => pendingBuild,
307
+ onResetReady: () => { readyCount++; },
308
+ });
309
+ const windowId = `pcw:${sessionManager.getSessionId().slice(0, 8)}:root`;
310
+ lifecycle.request(windowId);
311
+ const incoming = { type: "custom_message", customType: "foreign/boundary", content: "preserve me", display: false };
312
+ const turnEnd = handlers.get("turn_end")?.[0];
313
+ const sessionTree = handlers.get("session_tree")?.[0];
314
+ assert.ok(turnEnd && sessionTree);
315
+ const pending = turnEnd(fakeBoundaryEvent([incoming]), ctx);
316
+ await sessionTree({}, ctx);
317
+ resolveBuild([{ type: "custom_message", customType: BOOT_TYPE, content: "stale", display: false }]);
318
+ const result = resultEntries([await pending]);
319
+ assert.deepEqual(result.entries, [incoming], "a switched lifecycle keeps incoming drafts but discards stale reset drafts");
320
+ assert.equal(result.continue, false);
321
+ assert.equal(readyCount, 0, "stale reset work cannot stage a success notice");
322
+ });
323
+ test("a queued success clears an overflow failure before settle recovery can reset", async () => {
324
+ const sessionManager = Manager.inMemory("/private/tmp/notesoup-queued-overflow-test");
325
+ const handlers = new Map();
326
+ const api = {
327
+ on(name, handler) {
328
+ const list = handlers.get(name) ?? [];
329
+ list.push(handler);
330
+ handlers.set(name, list);
331
+ return () => { };
332
+ },
333
+ };
334
+ const ctx = {
335
+ sessionManager,
336
+ model: { contextWindow: 100_000, maxTokens: 4_096 },
337
+ signal: undefined,
338
+ hasPendingMessages: () => false,
339
+ ui: { notify() { } },
340
+ };
341
+ let resetCount = 0;
342
+ const lifecycle = registerResetLifecycle(api, {
343
+ isEnabled: () => true,
344
+ budget: {
345
+ automaticResetEnabled: () => true,
346
+ hardReserveDue: () => false,
347
+ consumeTurnEnd: () => [],
348
+ clear: () => { },
349
+ },
350
+ buildReset: () => {
351
+ resetCount += 1;
352
+ return [];
353
+ },
354
+ });
355
+ const failed = fakeBoundaryEvent();
356
+ failed.message = { ...failed.message, stopReason: "error", errorMessage: "Prompt too long: context exceeds maximum context length" };
357
+ const turnEnd = handlers.get("turn_end")?.[0];
358
+ const beforeSettle = handlers.get("agent_before_settle")?.[0];
359
+ assert.ok(turnEnd && beforeSettle);
360
+ await turnEnd(failed, ctx);
361
+ const queued = {
362
+ ...failed,
363
+ type: "agent_before_settle",
364
+ outcome: "error",
365
+ context: { ...failed.context, pendingMessages: [{ role: "user", content: [{ type: "text", text: "queued success" }], timestamp: Date.now() }] },
366
+ };
367
+ assert.equal(await beforeSettle(queued, ctx), undefined, "a queued message defers overflow recovery");
368
+ const success = fakeBoundaryEvent();
369
+ await turnEnd(success, ctx);
370
+ const settled = { ...queued, context: { ...queued.context, pendingMessages: [] }, outcome: "completed" };
371
+ assert.equal(await beforeSettle(settled, ctx), undefined, "the successful queued turn clears the stale recovery");
372
+ assert.equal(resetCount, 0);
373
+ });
374
+ test("reset-control: close-out phases deduplicate, span turns, and a tool call only acknowledges a manual close-out", () => {
375
+ const windowId = "pcw:test:window";
376
+ const first = reduceResetControl(initialResetControl(), { type: "close_out", windowId, source: "manual" });
377
+ assert.equal(first.effect, "close-out-armed");
378
+ assert.deepEqual(first.state.request, { phase: "close-out", windowId, source: "manual" });
379
+ const duplicate = reduceResetControl(first.state, { type: "close_out", windowId, source: "manual" });
380
+ assert.equal(duplicate.effect, "already-pending");
381
+ const noteTurn = reduceResetControl(first.state, { type: "turn_end", facts: turnEndFacts() });
382
+ assert.equal(noteTurn.effect, "none");
383
+ assert.deepEqual(noteTurn.state.request, first.state.request, "note/tool turns do not consume close-out");
384
+ const tool = reduceResetControl(noteTurn.state, { type: "tool_request", windowId });
385
+ assert.equal(tool.effect, "already-pending", "the agent's wipe call must not convert a manual close-out into a turn-end commit pi can overrun");
386
+ assert.deepEqual(tool.state.request, { phase: "close-out", windowId, source: "manual" });
387
+ const commit = reduceResetControl(tool.state, { type: "before_settle", facts: beforeSettleFacts() });
388
+ assert.equal(commit.effect, "commit-boundary-stop", "a manual close-out commits at settlement and stops");
389
+ assert.deepEqual(commit.state, initialResetControl());
390
+ });
391
+ test("reset-control: manual close-out converts a pending tool request into a settle stop", () => {
392
+ const windowId = "pcw:test:window";
393
+ const tool = reduceResetControl(initialResetControl(), { type: "tool_request", windowId });
394
+ const manual = reduceResetControl(tool.state, { type: "close_out", windowId, source: "manual" });
395
+ assert.equal(manual.effect, "close-out-armed", "the user's stop intent outranks the pending rollover");
396
+ assert.deepEqual(manual.state.request, { phase: "close-out", windowId, source: "manual" });
397
+ assert.equal(reduceResetControl(manual.state, { type: "turn_end", facts: turnEndFacts() }).effect, "none", "the converted request no longer commits at turn end");
398
+ assert.equal(reduceResetControl(manual.state, { type: "before_settle", facts: beforeSettleFacts() }).effect, "commit-boundary-stop");
399
+ const automatic = reduceResetControl(tool.state, { type: "close_out", windowId, source: "automatic" });
400
+ assert.equal(automatic.effect, "already-pending", "an automatic close-out must not displace a pending tool commit");
401
+ });
402
+ test("reset-control: a tool call upgrades a budget close-out for a turn-end commit", () => {
403
+ const windowId = "pcw:test:window";
404
+ const budget = reduceResetControl(initialResetControl(), { type: "close_out", windowId, source: "automatic" }).state;
405
+ const upgraded = reduceResetControl(budget, { type: "tool_request", windowId });
406
+ assert.equal(upgraded.effect, "close-out-armed");
407
+ assert.deepEqual(upgraded.state.request, { phase: "tool-requested", windowId, source: "automatic" }, "context pressure cannot wait for settlement");
408
+ const commit = reduceResetControl(upgraded.state, { type: "turn_end", facts: turnEndFacts() });
409
+ assert.equal(commit.effect, "commit-boundary", "the budget rollover continues in the fresh window");
410
+ });
411
+ test("reset-control: fallback is normal-stop only; hard reserve remains safety", () => {
412
+ const windowId = "pcw:test:window";
413
+ const manual = reduceResetControl(initialResetControl(), { type: "close_out", windowId, source: "manual" }).state;
414
+ const fallback = reduceResetControl(manual, { type: "before_settle", facts: beforeSettleFacts() });
415
+ assert.equal(fallback.effect, "commit-boundary-stop", "manual close-out stops after the reset is committed");
416
+ assert.deepEqual(fallback.state, initialResetControl());
417
+ const automatic = reduceResetControl(initialResetControl(), { type: "close_out", windowId, source: "automatic" }).state;
418
+ const normalStop = reduceResetControl(automatic, { type: "before_settle", facts: beforeSettleFacts() });
419
+ assert.equal(normalStop.effect, "none", "ignoring a budget warning and answering normally must not wipe memory");
420
+ assert.deepEqual(normalStop.state, initialResetControl());
421
+ const disabledAuto = reduceResetControl(automatic, { type: "before_settle", facts: beforeSettleFacts({ automaticResetEnabled: false }) });
422
+ assert.equal(disabledAuto.effect, "none");
423
+ assert.deepEqual(disabledAuto.state, initialResetControl());
424
+ const safety = reduceResetControl(initialResetControl(), { type: "turn_end", facts: turnEndFacts({ hardReserveDue: true }) });
425
+ assert.equal(safety.effect, "commit-boundary");
426
+ for (const outcome of [{ failed: true }, { aborted: true }]) {
427
+ const notNormalStop = reduceResetControl(manual, { type: "before_settle", facts: beforeSettleFacts(outcome) });
428
+ assert.equal(notNormalStop.effect, "none");
429
+ assert.deepEqual(notNormalStop.state, initialResetControl());
430
+ }
431
+ assert.deepEqual(reduceResetControl(manual, { type: "settled" }).state, initialResetControl());
432
+ });
433
+ test("reset-control: manual stop survives a hard-reserve reset across windows without a second wipe", () => {
434
+ const windowId = "pcw:test:window";
435
+ const manual = reduceResetControl(initialResetControl(), { type: "close_out", windowId, source: "manual" }).state;
436
+ const safety = reduceResetControl(manual, { type: "turn_end", facts: turnEndFacts({ hardReserveDue: true }) });
437
+ assert.equal(safety.effect, "commit-boundary-stop");
438
+ assert.deepEqual(safety.state.request, { phase: "stop-pending", windowId });
439
+ const freshWindowId = "pcw:test:fresh";
440
+ assert.equal(reduceResetControl(safety.state, { type: "tool_request", windowId: freshWindowId }).effect, "already-pending");
441
+ assert.equal(reduceResetControl(safety.state, { type: "close_out", windowId: freshWindowId, source: "automatic" }).effect, "already-pending");
442
+ const queued = reduceResetControl(safety.state, { type: "before_settle", facts: beforeSettleFacts({ windowId: freshWindowId, queued: true }) });
443
+ assert.deepEqual(queued.state, safety.state);
444
+ const freshTurn = reduceResetControl(queued.state, { type: "turn_end", facts: turnEndFacts({ windowId: freshWindowId, hardReserveDue: true }) });
445
+ assert.equal(freshTurn.effect, "none");
446
+ const stop = reduceResetControl(freshTurn.state, { type: "before_settle", facts: beforeSettleFacts({ windowId: freshWindowId }) });
447
+ assert.equal(stop.effect, "stop", "the safety boundary already wiped; settlement must only stop");
448
+ assert.deepEqual(stop.state, initialResetControl());
449
+ assert.equal(reduceResetControl(safety.state, { type: "before_settle", facts: beforeSettleFacts() }).effect, "commit-boundary-stop", "a failed safety construction may be retried before settlement");
450
+ assert.deepEqual(reduceResetControl(safety.state, { type: "abort" }).state, initialResetControl());
451
+ });
452
+ test("reset-control: manual overflow keeps stop intent while queued work supersedes the failure", () => {
453
+ const windowId = "pcw:test:window";
454
+ const manual = reduceResetControl(initialResetControl(), { type: "close_out", windowId, source: "manual" }).state;
455
+ const overflow = reduceResetControl(manual, { type: "turn_end", facts: turnEndFacts({ overflow: true, failed: true, queued: true, automaticResetEnabled: false }) });
456
+ assert.deepEqual(overflow.state.request, manual.request);
457
+ const deferred = reduceResetControl(overflow.state, { type: "before_settle", facts: beforeSettleFacts({ failed: true, queued: true, automaticResetEnabled: false }) });
458
+ assert.deepEqual(deferred.state, overflow.state);
459
+ assert.equal(reduceResetControl(overflow.state, { type: "before_settle", facts: beforeSettleFacts({ failed: true, automaticResetEnabled: false }) }).effect, "commit-boundary-stop", "manual overflow resets but does not retry the model");
460
+ const success = reduceResetControl(deferred.state, { type: "turn_end", facts: turnEndFacts() });
461
+ assert.deepEqual(success.state.request, manual.request, "a successful queued turn clears only the overflow, not the manual wipe");
462
+ assert.equal(success.state.overflow, "idle");
463
+ assert.equal(reduceResetControl(success.state, { type: "before_settle", facts: beforeSettleFacts() }).effect, "commit-boundary-stop");
464
+ });
465
+ test("reset-control: abort and lifecycle transitions clear every pending phase", () => {
466
+ const windowId = "pcw:test:window";
467
+ const pending = reduceResetControl(initialResetControl(), { type: "close_out", windowId, source: "manual" }).state;
468
+ const aborted = reduceResetControl(pending, { type: "turn_end", facts: turnEndFacts({ aborted: true }) });
469
+ assert.equal(aborted.effect, "none");
470
+ assert.deepEqual(aborted.state, initialResetControl());
471
+ assert.deepEqual(reduceResetControl(pending, { type: "clear" }).state, initialResetControl());
472
+ assert.deepEqual(reduceResetControl(pending, { type: "settled" }).state, initialResetControl());
473
+ });
474
+ test("reset-control: overflow recovery is armed at turn_end and spent exactly once at settle", () => {
475
+ const armed = reduceResetControl(initialResetControl(), { type: "turn_end", facts: turnEndFacts({ overflow: true, failed: true }) });
476
+ assert.deepEqual(armed.state, { request: { phase: "none" }, overflow: "pending" });
477
+ const recovered = reduceResetControl(armed.state, { type: "before_settle", facts: beforeSettleFacts() });
478
+ assert.equal(recovered.effect, "recover-overflow", "the first settle commits the bounded recovery");
479
+ assert.deepEqual(recovered.state, { request: { phase: "none" }, overflow: "spent" });
480
+ const repeated = reduceResetControl(recovered.state, { type: "before_settle", facts: beforeSettleFacts() });
481
+ assert.equal(repeated.effect, "none", "a spent recovery is never retried");
482
+ const rearmed = reduceResetControl(recovered.state, { type: "turn_end", facts: turnEndFacts({ overflow: true, failed: true }) });
483
+ assert.deepEqual(rearmed.state, { request: { phase: "none" }, overflow: "pending-spent" });
484
+ const bounded = reduceResetControl(rearmed.state, { type: "before_settle", facts: beforeSettleFacts() });
485
+ assert.equal(bounded.effect, "none", "a second failure chain stays bounded to the spent attempt");
486
+ assert.deepEqual(bounded.state, { request: { phase: "none" }, overflow: "spent" });
487
+ });
488
+ test("reset-control: a queued turn defers recovery and its success supersedes the failure", () => {
489
+ const armed = reduceResetControl(initialResetControl(), { type: "turn_end", facts: turnEndFacts({ overflow: true, failed: true }) }).state;
490
+ const deferred = reduceResetControl(armed, { type: "before_settle", facts: beforeSettleFacts({ queued: true }) });
491
+ assert.equal(deferred.effect, "none");
492
+ assert.deepEqual(deferred.state, armed, "a queued message leaves the overflow chain armed");
493
+ const success = reduceResetControl(armed, { type: "turn_end", facts: turnEndFacts() });
494
+ assert.deepEqual(success.state, initialResetControl(), "a successful queued turn clears the stale failure");
495
+ });
496
+ test("reset-control: disabled mode and explicit aborts disarm overflow without a recovery", () => {
497
+ const armed = reduceResetControl(initialResetControl(), { type: "turn_end", facts: turnEndFacts({ overflow: true, failed: true }) }).state;
498
+ const disabled = reduceResetControl(armed, { type: "before_settle", facts: beforeSettleFacts({ enabled: false }) });
499
+ assert.equal(disabled.effect, "none");
500
+ assert.deepEqual(disabled.state, { request: { phase: "none" }, overflow: "idle" });
501
+ const automaticOff = reduceResetControl(armed, { type: "before_settle", facts: beforeSettleFacts({ automaticResetEnabled: false }) });
502
+ assert.equal(automaticOff.effect, "none");
503
+ assert.deepEqual(automaticOff.state, { request: { phase: "none" }, overflow: "idle" });
504
+ const aborted = reduceResetControl(armed, { type: "before_settle", facts: beforeSettleFacts({ aborted: true }) });
505
+ assert.equal(aborted.effect, "none");
506
+ assert.deepEqual(aborted.state, { request: { phase: "none" }, overflow: "idle" });
507
+ });
508
+ test("reset-control: lazy guards preserve overflow policy ordering", () => {
509
+ const calls = { queued: 0, automatic: 0, hardReserve: 0 };
510
+ const facts = {
511
+ windowId: "pcw:test:window",
512
+ aborted: false,
513
+ overflow: true,
514
+ failed: true,
515
+ enabled: true,
516
+ get queued() { calls.queued += 1; return false; },
517
+ get automaticResetEnabled() { calls.automatic += 1; return true; },
518
+ get hardReserveDue() { calls.hardReserve += 1; return true; },
519
+ };
520
+ reduceResetControl(initialResetControl(), { type: "turn_end", facts });
521
+ assert.deepEqual(calls, { queued: 1, automatic: 1, hardReserve: 0 }, "overflow checks never consult hard-reserve policy");
522
+ const settleCalls = { queued: 0, enabled: 0, automatic: 0 };
523
+ const settleFacts = {
524
+ windowId: "pcw:test:window",
525
+ get queued() { settleCalls.queued += 1; return true; },
526
+ get enabled() { settleCalls.enabled += 1; return true; },
527
+ get automaticResetEnabled() { settleCalls.automatic += 1; return true; },
528
+ aborted: false,
529
+ failed: false,
530
+ };
531
+ const pendingOverflow = reduceResetControl(initialResetControl(), { type: "turn_end", facts: turnEndFacts({ overflow: true, failed: true }) }).state;
532
+ reduceResetControl(pendingOverflow, { type: "before_settle", facts: settleFacts });
533
+ assert.deepEqual(settleCalls, { queued: 1, enabled: 0, automatic: 0 }, "queued settle checks no further overflow policy");
534
+ });
535
+ test("a committed reset places incoming and budget drafts before marker -> boot -> continuation", async () => {
536
+ const sessionManager = Manager.inMemory("/private/tmp/notesoup-reset-ordering-test");
537
+ const handlers = new Map();
538
+ const api = {
539
+ on(name, handler) {
540
+ const list = handlers.get(name) ?? [];
541
+ list.push(handler);
542
+ handlers.set(name, list);
543
+ return () => { };
544
+ },
545
+ };
546
+ const ctx = {
547
+ sessionManager,
548
+ model: undefined,
549
+ signal: undefined,
550
+ hasPendingMessages: () => false,
551
+ ui: { notify() { } },
552
+ };
553
+ const windowId = "pcw:ordering:test";
554
+ const budgetDraft = { type: "custom_message", customType: GUIDANCE_TYPE, content: "budget draft", display: false };
555
+ const lifecycle = registerResetLifecycle(api, {
556
+ isEnabled: () => true,
557
+ budget: {
558
+ automaticResetEnabled: () => true,
559
+ hardReserveDue: () => false,
560
+ consumeTurnEnd: () => [budgetDraft],
561
+ clear: () => { },
562
+ },
563
+ buildReset: () => [
564
+ { type: "compaction", summary: "", firstKeptEntryId: null },
565
+ { type: "custom", customType: RESET_MARKER_TYPE, data: { windowId } },
566
+ { type: "custom_message", customType: BOOT_TYPE, content: "boot", display: false, details: { windowId } },
567
+ { type: "custom_message", customType: CONTINUATION_TYPE, content: "continuation", display: false },
568
+ ],
569
+ });
570
+ lifecycle.request(`pcw:${sessionManager.getSessionId().slice(0, 8)}:root`);
571
+ const incoming = { type: "custom_message", customType: "foreign/boundary", content: "incoming", display: false };
572
+ const results = [];
573
+ for (const handler of handlers.get("turn_end") ?? [])
574
+ results.push(await handler(fakeBoundaryEvent([incoming]), ctx));
575
+ const result = resultEntries(results);
576
+ assert.equal(result.continue, true);
577
+ const customTypes = (entries) => entries.map((entry) => {
578
+ if (entry.type === "compaction")
579
+ return "native-compaction";
580
+ assert.ok(entry.type === "custom" || entry.type === "custom_message", "the boundary only carries expected reset drafts here");
581
+ return entry.customType;
582
+ });
583
+ assert.deepEqual(customTypes(result.entries), [
584
+ "foreign/boundary",
585
+ GUIDANCE_TYPE,
586
+ "native-compaction",
587
+ RESET_MARKER_TYPE,
588
+ BOOT_TYPE,
589
+ CONTINUATION_TYPE,
590
+ ], "ordinary and budget drafts precede the closed reset shape");
591
+ appendDrafts(sessionManager, result.entries);
592
+ const branch = sessionManager.getBranch();
593
+ const markerIndex = branch.findIndex((entry) => entry.type === "custom" && entry.customType === RESET_MARKER_TYPE);
594
+ assert.ok(markerIndex > 0);
595
+ assert.equal(branch[markerIndex - 1]?.type, "compaction", "the checkpoint is directly before the marker");
596
+ const checkpoint = branch[markerIndex - 1];
597
+ assert.ok(checkpoint?.type === "compaction");
598
+ assert.equal(checkpoint.summary, "");
599
+ assert.equal(checkpoint.firstKeptEntryId, checkpoint.id, "Pi materializes null as retain-none");
600
+ assert.deepEqual(customTypes(branch.slice(markerIndex)), [
601
+ RESET_MARKER_TYPE,
602
+ BOOT_TYPE,
603
+ CONTINUATION_TYPE,
604
+ ], "the persisted reset shape remains marker -> boot -> continuation");
605
+ });