notesoup 0.47.0 → 0.48.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 (78) hide show
  1. package/README.md +1 -1
  2. package/dist/src/boot/render.d.ts +14 -7
  3. package/dist/src/boot/render.js +29 -16
  4. package/dist/src/boot/text.d.ts +25 -4
  5. package/dist/src/boot/text.js +59 -11
  6. package/dist/src/budget/text.d.ts +29 -9
  7. package/dist/src/budget/text.js +51 -10
  8. package/dist/src/claude/helper.d.ts +2 -3
  9. package/dist/src/claude/helper.js +45 -11
  10. package/dist/src/claude/history.d.ts +9 -0
  11. package/dist/src/claude/history.js +271 -0
  12. package/dist/src/dsh/engine.d.ts +2 -1
  13. package/dist/src/dsh/engine.js +18 -9
  14. package/dist/src/dsh/index.js +9 -4
  15. package/dist/src/dsh/window.d.ts +2 -1
  16. package/dist/src/dsh/window.js +14 -8
  17. package/dist/src/history/query.js +59 -81
  18. package/dist/src/notes/address.d.ts +5 -0
  19. package/dist/src/notes/address.js +20 -21
  20. package/dist/src/notes/store.js +12 -19
  21. package/dist/src/pi/boot.d.ts +1 -1
  22. package/dist/src/pi/boot.js +28 -5
  23. package/dist/src/pi/budget.d.ts +0 -2
  24. package/dist/src/pi/budget.js +13 -53
  25. package/dist/src/pi/entries.d.ts +0 -1
  26. package/dist/src/pi/entries.js +0 -1
  27. package/dist/src/pi/history.d.ts +3 -0
  28. package/dist/src/pi/history.js +4 -0
  29. package/dist/src/pi/reset/artifacts.d.ts +7 -2
  30. package/dist/src/pi/reset/artifacts.js +16 -14
  31. package/dist/src/pi/reset/lifecycle.d.ts +21 -25
  32. package/dist/src/pi/reset/lifecycle.js +49 -73
  33. package/dist/src/pi/runtime.js +19 -18
  34. package/dist/src/pi/window.d.ts +12 -6
  35. package/dist/src/pi/window.js +7 -0
  36. package/dist/test/agent-loop.test.js +65 -42
  37. package/dist/test/boot.integration.test.js +119 -16
  38. package/dist/test/budget-settings.integration.test.js +4 -2
  39. package/dist/test/claude-history.test.d.ts +1 -0
  40. package/dist/test/claude-history.test.js +184 -0
  41. package/dist/test/claude-mod.test.js +243 -49
  42. package/dist/test/dsh.test.js +79 -2
  43. package/dist/test/helpers/extension.d.ts +3 -0
  44. package/dist/test/helpers/extension.js +33 -5
  45. package/dist/test/history.integration.test.js +66 -0
  46. package/dist/test/host-neutral.test.js +1 -1
  47. package/dist/test/model-facing-text.test.d.ts +1 -0
  48. package/dist/test/model-facing-text.test.js +181 -0
  49. package/dist/test/notes.integration.test.js +16 -18
  50. package/dist/test/reset-lifecycle.test.js +202 -47
  51. package/docs/architecture.md +3 -3
  52. package/docs/claude-code-mod.md +15 -3
  53. package/docs/dsh.md +1 -1
  54. package/docs/reset-lifecycle.md +32 -20
  55. package/docs/skills.md +1 -1
  56. package/mods/notesoup/.claude-plugin/plugin.json +1 -1
  57. package/mods/notesoup/dist/claude/helper.js +836 -92
  58. package/mods/notesoup/hooks/register.ts +154 -52
  59. package/package.json +1 -1
  60. package/src/boot/render.ts +41 -23
  61. package/src/boot/text.ts +68 -11
  62. package/src/budget/text.ts +61 -10
  63. package/src/claude/helper.ts +53 -22
  64. package/src/claude/history.ts +228 -0
  65. package/src/dsh/engine.ts +16 -9
  66. package/src/dsh/index.ts +8 -3
  67. package/src/dsh/window.ts +15 -9
  68. package/src/history/query.ts +57 -82
  69. package/src/notes/address.ts +19 -16
  70. package/src/notes/store.ts +9 -14
  71. package/src/pi/boot.ts +27 -5
  72. package/src/pi/budget.ts +13 -50
  73. package/src/pi/entries.ts +0 -1
  74. package/src/pi/history.ts +7 -2
  75. package/src/pi/reset/artifacts.ts +17 -14
  76. package/src/pi/reset/lifecycle.ts +57 -80
  77. package/src/pi/runtime.ts +19 -18
  78. package/src/pi/window.ts +11 -1
@@ -0,0 +1,184 @@
1
+ import assert from "node:assert/strict";
2
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import test from "node:test";
6
+ import { claudeHistoryFromJsonl, readClaudeHistory } from "../src/claude/history.js";
7
+ import { renderBootBlock } from "../src/boot/render.js";
8
+ import { unavailableNotesSnapshot } from "../src/boot/snapshot.js";
9
+ import { DEFAULT_BOOT_TOOL_NAMES } from "../src/boot/text.js";
10
+ import { allItems, eventDocument } from "../src/history/history.js";
11
+ import { historyList, historyRead, historyWindows } from "../src/tools/history.js";
12
+ import { dispatch } from "../src/claude/helper.js";
13
+ const sessionId = "synthetic-session";
14
+ function row(uuid, parentUuid, type, content, extra = {}) {
15
+ return { uuid, parentUuid, type, sessionId, timestamp: "2026-01-01T00:00:00Z", isSidechain: false,
16
+ ...(type === "system" ? { content } : { message: { role: type, content } }), ...extra };
17
+ }
18
+ const user = (uuid, parent, text) => row(uuid, parent, "user", text);
19
+ const assistant = (uuid, parent, content) => row(uuid, parent, "assistant", content);
20
+ const call = (id, name = "bash") => ({ type: "tool_use", id, name, input: { command: `echo ${id}` } });
21
+ const result = (id, text, error = false) => ({ type: "tool_result", tool_use_id: id, content: text, is_error: error });
22
+ const jsonl = (rows) => `${rows.map((row) => JSON.stringify(row)).join("\n")}\n`;
23
+ const project = (rows, headUuid) => claudeHistoryFromJsonl(jsonl(rows), { sessionId, headUuid });
24
+ // Complete boot shape, not a real/private transcript fixture. `context_window` is the pre-rename
25
+ // spelling that transcripts already on disk carry; both spellings must yield the same ordinal.
26
+ const bootText = (ordinal, spelling = "legacy") => spelling === "legacy"
27
+ ? `<context_window>\nAgent name: claude (brain: claude)\n\nclaude:${sessionId}\n</context_window>\n\n<context_window_protocol>\nSynthetic reset protocol\n</context_window_protocol>\nCurrent context window id: claude:${sessionId}:${ordinal}`
28
+ : `<notesoup:window_identity>\nAgent name: claude (brain: claude)\n\nclaude:${sessionId}\n</notesoup:window_identity>\n\n<notesoup:notes_protocol>\nSynthetic reset protocol\n</notesoup:notes_protocol>\nCurrent context window id: claude:${sessionId}:${ordinal}`;
29
+ function compact(uuid, logical, metadata = {}) {
30
+ return row(uuid, null, "system", "Conversation compacted", { subtype: "compact_boundary", logicalParentUuid: logical, compactMetadata: metadata });
31
+ }
32
+ test("Claude append and result arrival preserve call addresses and use the shared paired read", async () => {
33
+ const rows = [user("u", null, "run tools"), assistant("a", "u", [{ type: "thinking", thinking: "private thought" }, { type: "text", text: "working" }, call("one"), call("two")])];
34
+ const pending = project(rows, "a");
35
+ assert.equal(pending.highestSeq, 4);
36
+ assert.equal(allItems(pending).find((event) => event.seq === 3)?.execution?.status, "unfinished");
37
+ assert.doesNotMatch(allItems(pending).map(eventDocument).join("\n"), /private thought/);
38
+ rows.push(row("r", "a", "user", [result("two", "failure", true), result("one", [{ type: "text", text: "output one" }, { type: "thinking", thinking: "hidden result thinking" }])]));
39
+ const completed = project(rows, "r");
40
+ assert.equal(completed.highestSeq, 7);
41
+ const read = await historyRead.execute({ seq: 3 }, completed);
42
+ assert.ok(read.ok);
43
+ assert.equal(read.data.tool_status, "ok");
44
+ assert.match(read.data.text, /bash \{"command":"echo one"\}\n--- output ---\noutput one/);
45
+ assert.doesNotMatch(read.data.text, /hidden/);
46
+ assert.equal(allItems(completed).find((event) => event.seq === 4)?.execution?.status, "error");
47
+ const resultRead = await historyRead.execute({ seq: 7 }, completed);
48
+ assert.ok(!resultRead.ok);
49
+ assert.equal(resultRead.error.code, "not_an_event");
50
+ assert.match(resultRead.error.message, /read seq 3/);
51
+ const search = await historyList.execute({ query: "output one" }, completed);
52
+ assert.ok(search.ok);
53
+ assert.deepEqual(search.data.items.map((item) => "seq" in item ? item.seq : -1), [3]);
54
+ });
55
+ test("Claude duplicate UUID records do not allocate again or move addresses on reload", () => {
56
+ const u = user("u", null, "hello"), a = assistant("a", "u", [call("one")]);
57
+ const original = project([u, a], "a");
58
+ const repeated = [u, a, { ...u, cwd: "/new/provenance" }, { ...a, cwd: "/new/provenance" }];
59
+ const reloaded = project(repeated, "a");
60
+ assert.equal(reloaded.highestSeq, original.highestSeq);
61
+ assert.deepEqual(reloaded, original);
62
+ assert.deepEqual(project(repeated, "a"), reloaded);
63
+ });
64
+ test("Claude branch selection is bound to committed head, not append order or last-prompt", async () => {
65
+ const rows = [user("u", null, "root"), assistant("left", "u", [call("left-call")]), row("left-result", "left", "user", [result("left-call", "left output")]), assistant("right", "u", [{ type: "text", text: "right branch" }]), { type: "last-prompt", leafUuid: "right", sessionId }];
66
+ const left = project(rows, "left-result"), right = project(rows, "right");
67
+ assert.equal(left.highestSeq, right.highestSeq);
68
+ assert.ok(!allItems(left).some((event) => event.text === "right branch"));
69
+ assert.equal(left.pairedResults.get(5), 3);
70
+ assert.deepEqual(allItems(right).map((event) => event.seq), [1, 6]);
71
+ const offBranch = await historyRead.execute({ seq: 3 }, right);
72
+ assert.ok(!offBranch.ok);
73
+ assert.equal(offBranch.error.code, "not_on_branch");
74
+ assert.throws(() => claudeHistoryFromJsonl(jsonl(rows), { sessionId }), /no confirmed active head/);
75
+ assert.throws(() => project(rows, "not-written-yet"), /not persisted yet/);
76
+ });
77
+ test("a persisted boot yields its ordinal under both tag spellings", () => {
78
+ // Three boundaries but one boot: only a recognised boot explains the ordinal.
79
+ const rowsFor = (boot) => [user("u", null, "old request"), assistant("a", "u", [{ type: "text", text: "answered" }]), compact("boundary", "a"), row("boot", "boundary", "user", boot), user("after", "boot", "after reset")];
80
+ const legacy = project(rowsFor(bootText(3)), "after");
81
+ const current = project(rowsFor(bootText(3, "current")), "after");
82
+ assert.equal(allItems(legacy).find((event) => event.text === "after reset")?.windowId, `claude:${sessionId}:3`, "an old-spelling boot numbers the window it opens");
83
+ assert.deepEqual(current.windows.map((window) => window.windowId), legacy.windows.map((window) => window.windowId));
84
+ const rendered = renderBootBlock({
85
+ agentName: "claude",
86
+ modelName: "claude",
87
+ tree: { trunk: `claude:${sessionId}`, windows: [{ shortName: "3", isCurrent: true }] },
88
+ notes: unavailableNotesSnapshot(Date.now()),
89
+ tools: DEFAULT_BOOT_TOOL_NAMES,
90
+ });
91
+ const real = project(rowsFor(`${rendered}\nCurrent context window id: claude:${sessionId}:3`), "after");
92
+ assert.deepEqual(real.windows.map((window) => window.windowId), current.windows.map((window) => window.windowId));
93
+ const partial = `<notesoup:window_identity>\nAgent name: claude (brain: claude)\n\nclaude:${sessionId}\n</notesoup:window_identity>\nCurrent context window id: claude:${sessionId}:3`;
94
+ assert.equal(allItems(project(rowsFor(partial), "after")).find((event) => event.text === "after reset")?.windowId, `claude:${sessionId}:1`, "an identity line without the protocol block is not a trusted boot");
95
+ });
96
+ test("Claude native compaction and reset keep pre-reset history, preserved rewrites and matching boot windows", async () => {
97
+ const old = assistant("old", "u", [{ type: "text", text: "before first reset" }]);
98
+ const reset = row("boot", "boundary", "user", bootText(1));
99
+ const rows = [user("u", null, "old request"), old,
100
+ compact("boundary", "old", { preservedSegment: { headUuid: "old", tailUuid: "old", anchorUuid: "boot" }, preservedMessages: { allUuids: ["old", "virtual-unpersisted-attachment"] } }), reset,
101
+ { ...old, parentUuid: "boot" }, assistant("new", "old", [{ type: "text", text: "after first reset" }]),
102
+ compact("second", "new"), row("boot2", "second", "user", bootText(2)), user("latest", "boot2", "after second reset")];
103
+ const projection = project(rows, "latest");
104
+ assert.deepEqual(projection.windows.map((window) => window.windowId), [0, 1, 2].map((ordinal) => `claude:${sessionId}:${ordinal}`));
105
+ assert.equal(allItems(projection).filter((event) => event.text === "before first reset").length, 1);
106
+ assert.equal(allItems(projection).find((event) => event.text === "before first reset")?.windowId, `claude:${sessionId}:0`);
107
+ assert.equal(allItems(projection).find((event) => event.text === "after first reset")?.windowId, `claude:${sessionId}:1`);
108
+ assert.equal(projection.highestSeq, 8);
109
+ const previous = await historyList.execute({ window_id: "@previous", roles: ["assistant"] }, projection);
110
+ assert.ok(previous.ok);
111
+ assert.deepEqual(previous.data.items.map((item) => "content" in item ? item.content : ""), ["after first reset"]);
112
+ const windows = await historyWindows.execute({}, projection, sessionId);
113
+ assert.ok(windows.ok);
114
+ assert.equal(windows.data.windows[0]?.first_seq, 1);
115
+ assert.deepEqual(project(rows, "latest"), projection, "reload does not need an in-memory seq cache");
116
+ });
117
+ test("Claude reset crosses native turn_duration graph metadata with no message content", async () => {
118
+ const duration = { type: "system", subtype: "turn_duration", uuid: "duration", parentUuid: "old", sessionId, durationMs: 123, messageCount: 2 };
119
+ const rows = [user("u", null, "marker before reset"), assistant("old", "u", [{ type: "text", text: "finished turn" }]), duration,
120
+ compact("boundary", "duration"), row("boot", "boundary", "user", bootText(1)), user("continuation", "boot", "continue")];
121
+ const projection = project(rows, "continuation");
122
+ assert.equal(projection.highestSeq, 5, "duration metadata does not allocate a message address");
123
+ assert.equal(projection.windows.length, 2);
124
+ assert.ok(allItems(projection).some((event) => event.text === "marker before reset"));
125
+ assert.ok((await historyList.execute({ window_id: "@previous" }, projection)).ok);
126
+ assert.ok((await historyRead.execute({ seq: 2 }, projection)).ok);
127
+ assert.ok((await historyWindows.execute({}, projection, sessionId)).ok);
128
+ });
129
+ test("Claude resolves forward persisted ancestry without renumbering or using later rewritten parent versions", () => {
130
+ const notice = row("notice", "attachment", "system", "hook notice");
131
+ const attachment = { type: "attachment", uuid: "attachment", parentUuid: "u", sessionId };
132
+ const rows = [user("u", null, "root"), notice, attachment, compact("boundary", "notice"),
133
+ row("boot", "boundary", "user", bootText(1), { isCompactSummary: true }), { ...attachment, parentUuid: "boot" }, user("new", "attachment", "after reset")];
134
+ const projection = project(rows, "new");
135
+ assert.equal(projection.highestSeq, 6);
136
+ assert.equal(allItems(projection).find((event) => event.text === "hook notice")?.seq, 2);
137
+ assert.equal(allItems(projection).find((event) => event.text === "hook notice")?.windowId, `claude:${sessionId}:0`);
138
+ assert.equal(allItems(projection).find((event) => event.text === "after reset")?.windowId, `claude:${sessionId}:1`);
139
+ assert.deepEqual(project(rows, "new"), projection);
140
+ assert.throws(() => project([notice], "notice"), /missing.*ancestor/);
141
+ assert.equal(project([user("u", null, bootText(9))], "u").windows.length, 1, "ordinary user text is not a trusted retained boot");
142
+ });
143
+ test("Claude attachments expose recorded renderings only and keep non-rendered nodes in ancestry", () => {
144
+ const rows = [user("u", null, "hi"), { type: "attachment", uuid: "raw", parentUuid: "u", sessionId, attachment: { type: "prompt_snapshot", content: "must not invent raw payload" } },
145
+ { type: "attachment", uuid: "rendered", parentUuid: "raw", sessionId, renderedRole: "user", rendered: [{ content: [{ type: "text", text: "visible injection" }] }] }];
146
+ const projection = project(rows, "rendered");
147
+ assert.deepEqual(allItems(projection).map((event) => event.seq), [1, 2, 3]);
148
+ assert.equal(allItems(projection).at(-1)?.role, "context");
149
+ assert.match(allItems(projection).map(eventDocument).join("\n"), /visible injection/);
150
+ assert.doesNotMatch(allItems(projection).map(eventDocument).join("\n"), /must not invent/);
151
+ });
152
+ test("Claude tolerates only the uncommitted JSONL tail, not malformed committed lines or unknown message shapes", () => {
153
+ const text = jsonl([user("u", null, "first")]);
154
+ assert.deepEqual(claudeHistoryFromJsonl(`${text}{\"type\":\"user\"`, { sessionId, headUuid: "u" }), project([user("u", null, "first")], "u"));
155
+ assert.equal(claudeHistoryFromJsonl(text.trimEnd(), { sessionId, headUuid: "u" }).highestSeq, 1);
156
+ assert.throws(() => claudeHistoryFromJsonl(`${text}{broken}\n`, { sessionId, headUuid: "u" }), /malformed.*line 2/);
157
+ assert.throws(() => project([user("u", null, "first"), { type: "new-message-schema", uuid: "unknown" }], "u"), /unsupported.*record type/);
158
+ assert.throws(() => project([row("u", null, "user", [{ type: "new-block-schema" }])], "u"), /unsupported.*content block/);
159
+ assert.throws(() => project([row("u", null, "user", { type: "text", text: "not an array" })], "u"), /message content/);
160
+ assert.throws(() => project([user("u", null, "first"), user("u", null, "conflict")], "u"), /conflicting.*duplicate UUID/);
161
+ assert.throws(() => project([{ ...user("u", null, "first"), isSidechain: true }], "u"), /only the main/);
162
+ assert.throws(() => project([{ ...user("u", null, "first"), sessionId: "another-session" }], "u"), /different session/);
163
+ });
164
+ test("Claude helper history tools reuse shared schemas, outcomes and text rendering from a supplied path", async (t) => {
165
+ const root = mkdtempSync(join(tmpdir(), "claude-history-"));
166
+ t.after(() => rmSync(root, { recursive: true, force: true }));
167
+ const transcriptPath = join(root, "transcript.jsonl"), rows = [user("u", null, "remember this"), assistant("a", "u", [call("one")]), row("r", "a", "user", [result("one", "recorded output")])];
168
+ writeFileSync(transcriptPath, jsonl(rows));
169
+ const identity = { home: join(root, "notes"), sessionId, cwd: root, agent: "claude", model: "claude" };
170
+ const history = { sessionId, transcriptPath, headUuid: "r" };
171
+ const projection = await readClaudeHistory(history);
172
+ const operations = [historyList, historyRead, historyWindows];
173
+ for (const operation of operations) {
174
+ const params = operation.name === "history_read" ? { seq: 3 } : {};
175
+ const actual = await dispatch({ op: "tool", tool: operation.name, params, identity, history });
176
+ const expected = operation.name === "history_read" ? { text: historyRead.render(await historyRead.execute({ seq: 3 }, projection)), ok: true }
177
+ : operation.name === "history_list" ? { text: historyList.render(await historyList.execute({}, projection)), ok: true }
178
+ : { text: historyWindows.render(await historyWindows.execute({}, projection, sessionId)), ok: true };
179
+ assert.deepEqual(actual, expected);
180
+ }
181
+ assert.deepEqual(await dispatch({ op: "history_state", identity, history }), { window: 0 });
182
+ await assert.rejects(dispatch({ op: "tool", tool: "history_windows", params: {}, identity }), /transcript_path is unavailable/);
183
+ await assert.rejects(dispatch({ op: "tool", tool: "history_windows", params: {}, identity, history: { ...history, sessionId: "other" } }), /different session/);
184
+ });