notesoup 0.56.1 → 0.57.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 (52) hide show
  1. package/README.md +1 -1
  2. package/dist/plugins/pi/runtime.js +8 -8
  3. package/dist/plugins/pi/text.js +1 -1
  4. package/dist/src/history/claude.d.ts +2 -0
  5. package/dist/src/history/claude.js +13 -2
  6. package/dist/src/history/codex-transcript.d.ts +15 -3
  7. package/dist/src/history/codex-transcript.js +57 -21
  8. package/dist/src/history/codex.d.ts +4 -2
  9. package/dist/src/history/codex.js +3 -3
  10. package/dist/src/history/corpus.d.ts +8 -1
  11. package/dist/src/history/corpus.js +17 -1
  12. package/dist/src/history/history.d.ts +16 -10
  13. package/dist/src/history/history.js +9 -9
  14. package/dist/src/history/local.d.ts +2 -0
  15. package/dist/src/history/local.js +35 -38
  16. package/dist/src/history/prefilter.d.ts +5 -0
  17. package/dist/src/history/prefilter.js +95 -0
  18. package/dist/src/history/query.d.ts +29 -27
  19. package/dist/src/history/query.js +82 -34
  20. package/dist/src/history/summary-cache.d.ts +10 -0
  21. package/dist/src/history/summary-cache.js +129 -0
  22. package/dist/src/tools/history.js +31 -20
  23. package/dist/test/agent-loop.test.js +18 -26
  24. package/dist/test/codex-history.test.js +90 -4
  25. package/dist/test/codex-plugin.test.js +6 -2
  26. package/dist/test/history-corpus.test.js +221 -33
  27. package/dist/test/history-prefilter.test.d.ts +1 -0
  28. package/dist/test/history-prefilter.test.js +174 -0
  29. package/dist/test/history-summary-cache.test.d.ts +1 -0
  30. package/dist/test/history-summary-cache.test.js +297 -0
  31. package/dist/test/history.integration.test.js +87 -52
  32. package/dist/test/model-facing-text.test.js +1 -1
  33. package/dist/test/reset-lifecycle.test.js +4 -6
  34. package/docs/history-sessions.md +26 -2
  35. package/docs/reset-lifecycle.md +4 -4
  36. package/docs/tool-results.md +4 -2
  37. package/package.json +1 -1
  38. package/plugins/claude/.claude-plugin/plugin.json +1 -1
  39. package/plugins/claude/dist/claude/helper.js +425 -122
  40. package/plugins/codex/dist/codex/notesoup.js +423 -123
  41. package/plugins/pi/runtime.ts +8 -8
  42. package/plugins/pi/text.ts +1 -1
  43. package/src/history/claude.ts +16 -2
  44. package/src/history/codex-transcript.ts +52 -17
  45. package/src/history/codex.ts +5 -5
  46. package/src/history/corpus.ts +19 -2
  47. package/src/history/history.ts +11 -11
  48. package/src/history/local.ts +34 -38
  49. package/src/history/prefilter.ts +86 -0
  50. package/src/history/query.ts +77 -33
  51. package/src/history/summary-cache.ts +119 -0
  52. package/src/tools/history.ts +33 -20
@@ -557,7 +557,7 @@ test("real AgentSession: manual command spans its tool batch and stops at settle
557
557
  fixture.close();
558
558
  }
559
559
  });
560
- test("real AgentSession: /empty text is carried verbatim and answered after close-out", async () => {
560
+ test("real AgentSession: /empty text is close-out context and stops after reset", async () => {
561
561
  let fixture;
562
562
  const prompt = 'AFTER_CLEAR_PROMPT_SENTINEL /cm "literal text" \nsecond line';
563
563
  fixture = await openFixture({
@@ -569,21 +569,18 @@ test("real AgentSession: /empty text is carried verbatim and answered after clos
569
569
  assert.ok(text(context).includes(JSON.stringify(promptBlock(`/empty ${prompt}`)).slice(1, -1)));
570
570
  return assistant(fixture, [{ type: "toolCall", id: "prompt-wipe", name: "empty_brain", arguments: { last_word: "saved" } }], "toolUse");
571
571
  }
572
- assert.equal(request, 3);
573
- assertFreshRequest(fixture, 2, "PROMPT_RESET_OLD_SENTINEL");
574
- assertUserContent(context, [{ type: "text", text: prompt }]);
575
- return assistant(fixture, [{ type: "text", text: "prompt handled" }]);
572
+ throw new Error("command argument must not start fresh-window work");
576
573
  },
577
574
  });
578
575
  try {
579
576
  await fixture.session.prompt("PROMPT_RESET_OLD_SENTINEL");
580
577
  await fixture.session.prompt(`/empty ${prompt}`);
581
578
  await fixture.session.waitForIdle();
582
- assert.equal(fixture.requests.length, 3);
579
+ assert.equal(fixture.requests.length, 2);
583
580
  assert.equal(resetMarkers(fixture).length, 1);
584
581
  const branch = fixture.sessionManager.getBranch();
585
- const continuationIndex = branch.findIndex((entry) => entry.type === "custom_message" && entry.customType === CONTINUATION_TYPE);
586
- assert.deepEqual(branch.slice(continuationIndex + 1, continuationIndex + 3).map((entry) => entry.type === "custom_message" ? entry.customType : entry.type), ["notesoup/reset-last-word", CARRIED_INPUT_TYPE]);
582
+ assert.equal(branch.some((entry) => entry.type === "custom_message" && entry.customType === CARRIED_INPUT_TYPE), false);
583
+ assert.equal(JSON.stringify(fixture.sessionManager.buildSessionProjection().messages).includes("AFTER_CLEAR_PROMPT_SENTINEL"), false);
587
584
  assert.equal(fixture.notices.some(isResetSuccessNotice), false);
588
585
  }
589
586
  finally {
@@ -625,16 +622,12 @@ test("real AgentSession: streaming clear prompt keeps the first request", async
625
622
  if (request === 1)
626
623
  assert.equal(text(context).includes("FIRST_RESET_PROMPT"), false, "the prompt belongs to the close-out, not the turn already running");
627
624
  // The close-out embeds the human's command line verbatim, so the closing turn sees
628
- // what the next window was asked to do. Only the accepted command appears.
625
+ // the command context. Only the accepted command appears.
629
626
  if (request === 2) {
630
- assert.ok(text(context).includes("FIRST_RESET_PROMPT"), "the close-out carries the command line");
627
+ assert.ok(text(context).includes(JSON.stringify(promptBlock("/fresh-window FIRST_RESET_PROMPT")).slice(1, -1)), "the close-out carries the original command line");
631
628
  assert.equal(text(context).includes("IGNORED_RESET_PROMPT"), false, "the refused repeat never enters the close-out");
632
629
  }
633
- if (request === 3) {
634
- assert.ok(text(context).includes("FIRST_RESET_PROMPT"), "the fresh window still receives the prompt");
635
- assert.equal(text(context).includes("IGNORED_RESET_PROMPT"), false);
636
- }
637
- assert.ok(request <= 3);
630
+ assert.ok(request <= 2);
638
631
  return assistant(fixture, [{ type: "text", text: "done" }]);
639
632
  },
640
633
  });
@@ -646,7 +639,8 @@ test("real AgentSession: streaming clear prompt keeps the first request", async
646
639
  release();
647
640
  await running;
648
641
  await fixture.session.waitForIdle();
649
- assert.equal(fixture.requests.length, 3);
642
+ assert.equal(fixture.requests.length, 2);
643
+ assert.equal(fixture.sessionManager.getBranch().some((entry) => entry.type === "custom_message" && entry.customType === CARRIED_INPUT_TYPE), false);
650
644
  assert.equal(resetMarkers(fixture).length, 1);
651
645
  assert.ok(fixture.notices.some((notice) => notice.includes("Brain already being emptied")));
652
646
  }
@@ -698,15 +692,13 @@ test("real AgentSession: manual hard-reserve reset stops clean and mixed-tool tu
698
692
  }
699
693
  }
700
694
  });
701
- test("real AgentSession: overflowing /empty close-out forces a reset and retries its text only once", async () => {
695
+ test("real AgentSession: overflowing /empty close-out forces a reset and stops", async () => {
702
696
  let fixture;
703
697
  fixture = await openFixture({
704
- script: (request, context) => {
698
+ script: (request) => {
705
699
  if (request === 1)
706
700
  return assistant(fixture, [{ type: "text", text: "before overflow" }]);
707
- assert.ok(request <= 3, "an oversized carried prompt cannot loop");
708
- if (request === 3)
709
- assertUserContent(context, [{ type: "text", text: "OVERSIZED_MANUAL_PROMPT" }]);
701
+ assert.equal(request, 2, "command text must not trigger a retry");
710
702
  return assistant(fixture, [], "error", { errorMessage: "Prompt too long: context exceeds maximum context length" });
711
703
  },
712
704
  });
@@ -714,9 +706,9 @@ test("real AgentSession: overflowing /empty close-out forces a reset and retries
714
706
  await fixture.session.prompt("MANUAL_OVERFLOW_OLD_SENTINEL");
715
707
  await fixture.session.prompt("/empty OVERSIZED_MANUAL_PROMPT");
716
708
  await fixture.session.waitForIdle();
717
- assert.equal(fixture.requests.length, 3);
709
+ assert.equal(fixture.requests.length, 2);
718
710
  assert.equal(resetMarkers(fixture).length, 1);
719
- assertFreshRequest(fixture, 2, "MANUAL_OVERFLOW_OLD_SENTINEL");
711
+ assert.equal(fixture.sessionManager.getBranch().some((entry) => entry.type === "custom_message" && entry.customType === CARRIED_INPUT_TYPE), false);
720
712
  assert.equal(resetMarkers(fixture)[0].data.origin, "forced");
721
713
  assert.ok(fixture.notices.some(isResetSuccessNotice));
722
714
  }
@@ -843,7 +835,7 @@ test("real AgentSession: user abort, synthetic aborted response, and generic err
843
835
  }
844
836
  }
845
837
  });
846
- test("real AgentSession: input during bare /empty is hidden-steered, carried with images, and answered after a safety reset", async () => {
838
+ test("real AgentSession: input during prompted /empty is hidden-steered, carried with images, and answered after a safety reset", async () => {
847
839
  let fixture;
848
840
  let started;
849
841
  let release;
@@ -862,14 +854,14 @@ test("real AgentSession: input during bare /empty is hidden-steered, carried wit
862
854
  assertUserContent(context, [{ type: "text", text: promptBlock(input) }, image]);
863
855
  return assistant(fixture, [{ type: "toolCall", id: "deferred-pressure", name: "notes_write", arguments: { address: "deferred.md", content: "saved" } }], "toolUse", { usage: usage(80_000) });
864
856
  }
865
- assert.equal(request, 3, "carried input continues instead of taking the bare-command stop");
857
+ assert.equal(request, 3, "carried input continues despite command stop policy");
866
858
  assertFreshRequest(fixture, 2, "saving notes");
867
859
  assertUserContent(context, [{ type: "text", text: input }, image]);
868
860
  return assistant(fixture, [{ type: "text", text: "deferred input answered" }]);
869
861
  },
870
862
  });
871
863
  try {
872
- const command = fixture.session.prompt("/empty");
864
+ const command = fixture.session.prompt("/empty CLOSE_OUT_CONTEXT");
873
865
  await ready;
874
866
  await fixture.session.prompt(input, { images: [image], streamingBehavior: "followUp" });
875
867
  release();
@@ -1,11 +1,13 @@
1
1
  import assert from "node:assert/strict";
2
- import { mkdtemp, writeFile, appendFile, mkdir, readFile, rm, stat } from "node:fs/promises";
2
+ import fs, { mkdtemp, writeFile, appendFile, mkdir, readFile, realpath, rm, stat, symlink, truncate } from "node:fs/promises";
3
+ import { syncBuiltinESMExports } from "node:module";
3
4
  import { tmpdir } from "node:os";
4
5
  import { join } from "node:path";
5
- import { zstdCompressSync } from "node:zlib";
6
+ import zlib, { zstdCompressSync } from "node:zlib";
6
7
  import { test } from "node:test";
7
8
  import { readCodexHistory } from "../plugins/codex/src/history.js";
8
9
  import { readCodexTranscriptState } from "../plugins/codex/src/transcript.js";
10
+ import { createCodexTranscriptReader } from "../src/history/codex-transcript.js";
9
11
  import { allItems, eventDocument } from "../src/history/history.js";
10
12
  import { historyList, historyRead } from "../src/tools/history.js";
11
13
  /* Producer-shaped synthetic fixtures, not captured/private transcripts.
@@ -57,6 +59,85 @@ async function fixture(t) {
57
59
  await mkdir(day, { recursive: true });
58
60
  return { home, day, path: join(day, nativeName(ROOT)) };
59
61
  }
62
+ function countSourceReads(t) {
63
+ const files = new Map(), read = fs.readFile, decompress = zlib.zstdDecompressSync;
64
+ let decompressions = 0;
65
+ t.mock.method(fs, "readFile", (...args) => {
66
+ const path = String(args[0]);
67
+ files.set(path, (files.get(path) ?? 0) + 1);
68
+ return read(...args);
69
+ });
70
+ t.mock.method(zlib, "zstdDecompressSync", (...args) => { decompressions++; return decompress(...args); });
71
+ syncBuiltinESMExports();
72
+ t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
73
+ return { files, decompressions: () => decompressions };
74
+ }
75
+ test("request reader reuses the original compressed Buffer by canonical path and rechecks consumption limits", async (t) => {
76
+ const f = await fixture(t), path = f.path + ".zst", alias = join(f.home, "alias.jsonl.zst");
77
+ const text = jsonl([row("session_meta", meta(), 0), row("response_item", message("user", "needle ".repeat(1000)), 1)]);
78
+ const compressed = zstdCompressSync(Buffer.from(text));
79
+ await writeFile(path, compressed);
80
+ await symlink(path, alias);
81
+ const reads = countSourceReads(t), readBytes = createCodexTranscriptReader();
82
+ const [bytes, same] = await Promise.all([readBytes(path), readBytes(alias)]);
83
+ assert.equal(bytes, same);
84
+ assert.equal(bytes.toString("utf8"), text);
85
+ const result = await readCodexHistory({ transcriptPath: alias, threadId: ROOT }, readBytes);
86
+ assert.deepEqual(result.dependencies, [await realpath(path)]);
87
+ assert.equal(allItems(result.projection)[0]?.text, "needle ".repeat(1000));
88
+ await assert.rejects(readBytes(path, compressed.length - 1), /read size limit exceeded/);
89
+ await assert.rejects(readBytes(path, bytes.length - 1), /decoded size limit exceeded/);
90
+ assert.deepEqual([...reads.files.values()], [1]);
91
+ assert.equal(reads.decompressions(), 1);
92
+ // A later load must see appends; no byte cache survives in the default reader.
93
+ await writeFile(path, zstdCompressSync(Buffer.from(text + jsonl([row("response_item", message("assistant", "later"), 2)]))));
94
+ assert.equal(allItems((await readCodexHistory({ transcriptPath: path, threadId: ROOT })).projection).at(-1)?.text, "later");
95
+ assert.deepEqual([...reads.files.values()], [2]);
96
+ assert.equal(reads.decompressions(), 2);
97
+ });
98
+ test("cached source bytes still charge the aggregate decoded lineage budget", async (t) => {
99
+ const f = await fixture(t), path = join(f.day, nativeName(CHILD));
100
+ const ancestor = jsonl([row("session_meta", meta(), 0), row("response_item", message("user", "kept"), 1)]);
101
+ await writeFile(f.path, ancestor);
102
+ await writeFile(path, jsonl([row("session_meta", meta(CHILD, { history_base: { thread_id: ROOT, end_ordinal_exclusive: 2, end_byte_offset: Buffer.byteLength(ancestor) } }), 2)]));
103
+ // Sparse, uncommitted tails count against decoded resource limits, not event count.
104
+ for (const source of [path, f.path])
105
+ await truncate(source, 64 * 1024 * 1024 + 1);
106
+ const reads = countSourceReads(t), readBytes = createCodexTranscriptReader();
107
+ await readBytes(path);
108
+ await readBytes(f.path);
109
+ await assert.rejects(readCodexHistory({ transcriptPath: path, threadId: CHILD }, readBytes), /size limit exceeded/);
110
+ assert.deepEqual([...reads.files.values()], [1, 1]);
111
+ });
112
+ test("preloaded Buffers cannot bypass metadata, cutoff, cycle or containment checks", async (t) => {
113
+ const f = await fixture(t), path = join(f.day, nativeName(CHILD));
114
+ const ancestor = jsonl([row("session_meta", meta(), 0), row("response_item", message("user", "kept"), 1)]);
115
+ const base = { thread_id: ROOT, end_ordinal_exclusive: 2, end_byte_offset: Buffer.byteLength(ancestor) };
116
+ await writeFile(f.path, ancestor);
117
+ await writeFile(path, jsonl([row("session_meta", meta(CHILD, { history_base: { ...base, end_byte_offset: base.end_byte_offset - 1 } }), 2)]));
118
+ let readBytes = createCodexTranscriptReader();
119
+ await readBytes(path);
120
+ await assert.rejects(readCodexHistory({ transcriptPath: path, threadId: ROOT }, readBytes), /not requested threadId/);
121
+ await readBytes(f.path);
122
+ await assert.rejects(readCodexHistory({ transcriptPath: path, threadId: CHILD }, readBytes), /complete decoded JSONL boundary/);
123
+ await writeFile(path, jsonl([row("session_meta", meta(CHILD, { history_base: { ...base, thread_id: CHILD } }), 2)]));
124
+ readBytes = createCodexTranscriptReader();
125
+ await readBytes(path);
126
+ await assert.rejects(readCodexHistory({ transcriptPath: path, threadId: CHILD }, readBytes), /cycle or ancestry depth/);
127
+ // Native discovery ignores symlink files, but a symlinked native directory must
128
+ // still pass canonical containment before a cached ancestor can be consumed.
129
+ const outside = join(f.home, "outside"), archived = join(f.home, "archived_sessions");
130
+ await mkdir(outside);
131
+ const escaped = join(outside, nativeName(ROOT));
132
+ await writeFile(escaped, ancestor);
133
+ await rm(f.path);
134
+ await symlink(outside, archived);
135
+ await writeFile(path, jsonl([row("session_meta", meta(CHILD, { history_base: base }), 2)]));
136
+ readBytes = createCodexTranscriptReader();
137
+ await readBytes(escaped);
138
+ await readBytes(path);
139
+ await assert.rejects(readCodexHistory({ transcriptPath: path, threadId: CHILD }, readBytes), /escapes native storage roots/);
140
+ });
60
141
  test("root: raw messages, top-level envelope metadata, native tool pairing and no display duplicates", async (t) => {
61
142
  const f = await fixture(t);
62
143
  await writeFile(f.path, jsonl([
@@ -131,7 +212,7 @@ test("two native checkpoints retain old raw history; replacement_history is not
131
212
  assert.match(historyList.render(earlier), /^seq 6 \| user\nmiddle text$/);
132
213
  const old = await historyRead.execute({ seq: 2 }, second.projection);
133
214
  assert.match(historyRead.render(old), /old raw text/);
134
- const { projection: _projection, ...facts } = second;
215
+ const { projection: _projection, dependencies: _dependencies, lineageSources: _lineageSources, ...facts } = second;
135
216
  assert.deepEqual(await readCodexTranscriptState({ transcriptPath: f.path, threadId: ROOT }), facts);
136
217
  assert.deepEqual(await readCodexHistory({ transcriptPath: f.path, threadId: ROOT }), second);
137
218
  });
@@ -186,7 +267,12 @@ test("fork + reverted physical lineage resolves only bounded ancestry, including
186
267
  row("response_item", message("assistant", "new branch"), 6),
187
268
  ]));
188
269
  await writeFile(join(f.day, nativeName(CHILD)), "THIS SIBLING MUST NEVER BE PARSED\n");
189
- const result = await readCodexHistory({ transcriptPath: replacementPath, threadId: FORK });
270
+ const reads = countSourceReads(t), readBytes = createCodexTranscriptReader();
271
+ await readBytes(replacementPath); // The local prefilter's root read is handed to strict decode.
272
+ const result = await readCodexHistory({ transcriptPath: replacementPath, threadId: FORK }, readBytes);
273
+ assert.deepEqual(result.dependencies, await Promise.all([replacementPath, forkPath, join(archived, nativeName(ROOT) + ".zst")].map((path) => realpath(path))));
274
+ assert.deepEqual([...reads.files.values()], [1, 1, 1]);
275
+ assert.equal(reads.decompressions(), 1);
190
276
  const text = allItems(result.projection).map(eventDocument).join("\n");
191
277
  assert.match(text, /父前缀 🦈/);
192
278
  assert.match(text, /fork-local kept/);
@@ -178,8 +178,12 @@ test("bundled Codex MCP reads another harness and wildcard results retain their
178
178
  assert.equal(list.isError, undefined, textOf(list));
179
179
  const page = list.structuredContent;
180
180
  assert.deepEqual(new Set(page.items.map((item) => item.session)), new Set([`codex:${ROOT}`, "pi:foreign-pi"]));
181
- assert.equal(page.page_older, null);
182
- assert.equal(page.page_newer, null);
181
+ assert.equal(page.has_older, false);
182
+ assert.equal(page.has_newer, false);
183
+ const limited = await call(client, { name: "history_list", arguments: { session: "*", query: "needle", limit: 1 }, _meta: metaFor(ROOT, ROOT) });
184
+ assert.equal(limited.structuredContent.has_older, true);
185
+ assert.match(textOf(limited), /→ more older results: narrow with since\/until/);
186
+ assert.doesNotMatch(textOf(limited), /before_seq|after_seq/);
183
187
  const invalid = await call(client, { name: "history_read", arguments: { session: "*", seq: 1 }, _meta: metaFor(ROOT, ROOT) });
184
188
  assert.equal(errorCode(invalid), "invalid_session");
185
189
  });