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.
- package/README.md +1 -1
- package/dist/plugins/pi/runtime.js +8 -8
- package/dist/plugins/pi/text.js +1 -1
- package/dist/src/history/claude.d.ts +2 -0
- package/dist/src/history/claude.js +13 -2
- package/dist/src/history/codex-transcript.d.ts +15 -3
- package/dist/src/history/codex-transcript.js +57 -21
- package/dist/src/history/codex.d.ts +4 -2
- package/dist/src/history/codex.js +3 -3
- package/dist/src/history/corpus.d.ts +8 -1
- package/dist/src/history/corpus.js +17 -1
- package/dist/src/history/history.d.ts +16 -10
- package/dist/src/history/history.js +9 -9
- package/dist/src/history/local.d.ts +2 -0
- package/dist/src/history/local.js +35 -38
- package/dist/src/history/prefilter.d.ts +5 -0
- package/dist/src/history/prefilter.js +95 -0
- package/dist/src/history/query.d.ts +29 -27
- package/dist/src/history/query.js +82 -34
- package/dist/src/history/summary-cache.d.ts +10 -0
- package/dist/src/history/summary-cache.js +129 -0
- package/dist/src/tools/history.js +31 -20
- package/dist/test/agent-loop.test.js +18 -26
- package/dist/test/codex-history.test.js +90 -4
- package/dist/test/codex-plugin.test.js +6 -2
- package/dist/test/history-corpus.test.js +221 -33
- package/dist/test/history-prefilter.test.d.ts +1 -0
- package/dist/test/history-prefilter.test.js +174 -0
- package/dist/test/history-summary-cache.test.d.ts +1 -0
- package/dist/test/history-summary-cache.test.js +297 -0
- package/dist/test/history.integration.test.js +87 -52
- package/dist/test/model-facing-text.test.js +1 -1
- package/dist/test/reset-lifecycle.test.js +4 -6
- package/docs/history-sessions.md +26 -2
- package/docs/reset-lifecycle.md +4 -4
- package/docs/tool-results.md +4 -2
- package/package.json +1 -1
- package/plugins/claude/.claude-plugin/plugin.json +1 -1
- package/plugins/claude/dist/claude/helper.js +425 -122
- package/plugins/codex/dist/codex/notesoup.js +423 -123
- package/plugins/pi/runtime.ts +8 -8
- package/plugins/pi/text.ts +1 -1
- package/src/history/claude.ts +16 -2
- package/src/history/codex-transcript.ts +52 -17
- package/src/history/codex.ts +5 -5
- package/src/history/corpus.ts +19 -2
- package/src/history/history.ts +11 -11
- package/src/history/local.ts +34 -38
- package/src/history/prefilter.ts +86 -0
- package/src/history/query.ts +77 -33
- package/src/history/summary-cache.ts +119 -0
- package/src/tools/history.ts +33 -20
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
-
import { mkdtemp, mkdir, readFile, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
2
|
+
import fs, { mkdtemp, mkdir, readFile, realpath, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
3
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
3
4
|
import { tmpdir } from "node:os";
|
|
4
5
|
import { dirname, join } from "node:path";
|
|
5
6
|
import { test } from "node:test";
|
|
6
|
-
import { zstdCompressSync } from "node:zlib";
|
|
7
|
+
import zlib, { zstdCompressSync } from "node:zlib";
|
|
7
8
|
import { Check } from "typebox/value";
|
|
8
9
|
import { localHistoryCorpus } from "../src/history/local.js";
|
|
9
10
|
import { parseSessionSelector } from "../src/history/corpus.js";
|
|
10
11
|
import { allItems, projectHistory } from "../src/history/history.js";
|
|
11
|
-
import { claudeHistoryFromJsonl, offlineClaudeHead } from "../src/history/claude.js";
|
|
12
|
+
import { claudeHistoryFromJsonl, offlineClaudeHead, offlineClaudeHistoryFromJsonl } from "../src/history/claude.js";
|
|
12
13
|
import { piHistoryFromJsonl } from "../src/history/pi.js";
|
|
13
14
|
import { historyList, historyRead } from "../src/tools/history.js";
|
|
14
15
|
import { TOOL_OUTPUT_MAX_BYTES } from "../src/tools/output.js";
|
|
@@ -54,7 +55,7 @@ async function fixture(t) {
|
|
|
54
55
|
]);
|
|
55
56
|
const codexPath = await put(join(roots.codex, "2026/10/06", `rollout-2026-10-06T01-02-03-${CODEX}.jsonl.zst`), zstdCompressSync(Buffer.from(codexText)), 30);
|
|
56
57
|
const current = descriptor("pi", "current", projection("current"));
|
|
57
|
-
const corpus = localHistoryCorpus({ home, env: {}, current });
|
|
58
|
+
const corpus = localHistoryCorpus({ home, env: {}, summaryCacheRoot: join(home, "history-cache"), current });
|
|
58
59
|
const ctx = { current: () => projection("current"), corpus };
|
|
59
60
|
return { home, roots, put, ctx, corpus, piPath, claudePath, codexPath, codexText };
|
|
60
61
|
}
|
|
@@ -69,7 +70,7 @@ test("session schemas are optional and non-empty, while the current projection k
|
|
|
69
70
|
const page = await historyList.execute({ limit: 1 }, p);
|
|
70
71
|
assert.ok(Check(historyList.outputSchema, page.ok ? page.data : page));
|
|
71
72
|
assert.equal("session" in data(page).items[0], false);
|
|
72
|
-
assert.
|
|
73
|
+
assert.equal(data(page).has_older, true);
|
|
73
74
|
assert.match(historyList.render(page), /^seq 5 \| user/);
|
|
74
75
|
assert.doesNotMatch(historyList.render(page), /session/);
|
|
75
76
|
const read = await historyRead.execute({ seq: 3 }, p);
|
|
@@ -111,16 +112,19 @@ test("all single-session recipes preserve the resolved identity, including folde
|
|
|
111
112
|
{ seq: 4, windowId: "w", role: "assistant", text: "last" },
|
|
112
113
|
] }], 4, new Set([1, 2, 3, 4]));
|
|
113
114
|
const ctx = context([descriptor("claude", "target-session", p)]);
|
|
114
|
-
const page = await historyList.execute({ session: "claude:target", limit: 1, after_seq: 1, before_seq: 5 }, ctx);
|
|
115
|
-
assert.
|
|
115
|
+
const page = await historyList.execute({ session: "claude:target", limit: 1, after_seq: 1, before_seq: 5, from: "newest" }, ctx);
|
|
116
|
+
assert.equal(data(page).has_older, true);
|
|
117
|
+
assert.match(historyList.render(page), /→ older: \{"session":"claude:target-session","before_seq":4\}/);
|
|
116
118
|
const first = await historyList.execute({ session: "target", from: "oldest", limit: 1 }, ctx);
|
|
117
|
-
assert.
|
|
119
|
+
assert.equal(data(first).has_newer, true);
|
|
120
|
+
assert.match(historyList.render(first), /→ newer: \{"session":"claude:target-session","after_seq":1\}/);
|
|
118
121
|
const foldPage = await historyList.execute({ session: "target", limit: 3 }, ctx);
|
|
119
122
|
const expand = /→ expand: (\{[^\n]+\})/.exec(historyList.render(foldPage));
|
|
120
123
|
assert.ok(expand);
|
|
121
124
|
const recipe = JSON.parse(expand[1]);
|
|
122
125
|
assert.equal(recipe.session, "claude:target-session");
|
|
123
|
-
assert.
|
|
126
|
+
assert.equal("from" in recipe, false);
|
|
127
|
+
assert.deepEqual(data(await historyList.execute(recipe, ctx)).items.map((item) => item.seq), [2]);
|
|
124
128
|
assert.match(historyList.render(foldPage), /session claude:target-session \| seq 1 \| user/);
|
|
125
129
|
const read = await historyRead.execute({ session: "target", seq: 3 }, ctx);
|
|
126
130
|
assert.equal(data(read).session, "claude:target-session");
|
|
@@ -129,11 +133,33 @@ test("all single-session recipes preserve the resolved identity, including folde
|
|
|
129
133
|
assert.ok(Check(historyRead.outputSchema, data(read)));
|
|
130
134
|
assert.ok(Check(historyList.outputSchema, data(foldPage)));
|
|
131
135
|
});
|
|
136
|
+
test("recipes expand a fold at seq 1 and read its neighborhood from the start", async () => {
|
|
137
|
+
const p = projectHistory([{ windowId: "w", items: [
|
|
138
|
+
{ seq: 1, windowId: "w", role: "system", text: "context one" },
|
|
139
|
+
{ seq: 2, windowId: "w", role: "system", text: "context two" },
|
|
140
|
+
{ seq: 3, windowId: "w", role: "user", text: "hello" },
|
|
141
|
+
] }], 3, new Set([1, 2, 3]));
|
|
142
|
+
const ctx = context([descriptor("claude", "first-fold", p)]);
|
|
143
|
+
const folded = await historyList.execute({ session: "first-fold" }, ctx);
|
|
144
|
+
const expand = /→ expand: (\{[^\n]+\})/.exec(historyList.render(folded));
|
|
145
|
+
assert.ok(expand);
|
|
146
|
+
const recipe = JSON.parse(expand[1]);
|
|
147
|
+
assert.equal(recipe.after_seq, 0);
|
|
148
|
+
assert.ok(Check(historyList.parameters, recipe));
|
|
149
|
+
assert.deepEqual(data(await historyList.execute(recipe, ctx)).items.map((item) => item.seq), [1, 2]);
|
|
150
|
+
const read = await historyRead.execute({ session: "first-fold", seq: 3 }, ctx);
|
|
151
|
+
const neighborhood = /neighborhood → `history_list`\((\{[^\n]+\})\)/.exec(historyRead.render(read));
|
|
152
|
+
assert.ok(neighborhood);
|
|
153
|
+
const nearby = JSON.parse(neighborhood[1]);
|
|
154
|
+
assert.equal(nearby.after_seq, 0);
|
|
155
|
+
assert.ok(Check(historyList.parameters, nearby));
|
|
156
|
+
assert.ok(data(await historyList.execute(nearby, ctx)).items.length > 0);
|
|
157
|
+
});
|
|
132
158
|
test("wildcard refusals precede discovery, and read accepts no wildcard selector", async () => {
|
|
133
159
|
const ctx = { current: () => { throw new Error("must not load current"); }, corpus: { currentHarness: "pi", sessions: async () => { throw new Error("must not discover"); } } };
|
|
134
160
|
for (const session of ["*", "pi:*", "pre*fix"]) {
|
|
135
161
|
for (const query of [undefined, "", [], ["", ""], "needle"]) {
|
|
136
|
-
for (const bounds of [{ before_seq: 10 }, { after_seq: 1 }, { before_seq: 10, after_seq: 1 }]) {
|
|
162
|
+
for (const bounds of [{ before_seq: 10 }, { after_seq: 0 }, { after_seq: 1 }, { before_seq: 10, after_seq: 1 }]) {
|
|
137
163
|
refusal(await historyList.execute({ session, query, ...bounds }, ctx), "invalid_bounds");
|
|
138
164
|
}
|
|
139
165
|
}
|
|
@@ -172,20 +198,52 @@ test("wildcard query merges every role chronologically, limits from either end,
|
|
|
172
198
|
assert.deepEqual(page.items.map((item) => "role" in item ? [item.session, item.seq, item.role] : null), [
|
|
173
199
|
["pi:pi-one", 1, "user"], ["claude:claude-one", 1, "user"], ["pi:pi-one", 3, "tool"], ["claude:claude-one", 2, "context"], [`codex:${CODEX}`, 2, "assistant"],
|
|
174
200
|
]);
|
|
175
|
-
assert.equal(page.
|
|
176
|
-
assert.equal(page.
|
|
177
|
-
const
|
|
201
|
+
assert.equal(page.has_older, false);
|
|
202
|
+
assert.equal(page.has_newer, false);
|
|
203
|
+
const newestResult = await historyList.execute({ session: "*", query: "needle", limit: 2 }, ctx);
|
|
204
|
+
const newest = data(newestResult);
|
|
178
205
|
assert.deepEqual(newest.items, page.items.slice(-2));
|
|
179
|
-
|
|
206
|
+
assert.equal(newest.has_older, true);
|
|
207
|
+
assert.equal(newest.has_newer, false);
|
|
208
|
+
assert.match(historyList.render(newestResult), /→ more older results: narrow with since\/until, query, roles or the session pattern/);
|
|
209
|
+
assert.doesNotMatch(historyList.render(newestResult), /before_seq|after_seq/);
|
|
210
|
+
const oldestResult = await historyList.execute({ session: "*", query: "needle", limit: 2, from: "oldest" }, ctx);
|
|
211
|
+
const oldest = data(oldestResult);
|
|
180
212
|
assert.deepEqual(oldest.items, page.items.slice(0, 2));
|
|
213
|
+
assert.equal(oldest.has_older, false);
|
|
214
|
+
assert.equal(oldest.has_newer, true);
|
|
215
|
+
assert.match(historyList.render(oldestResult), /→ more newer results:/);
|
|
216
|
+
assert.doesNotMatch(historyList.render(oldestResult), /before_seq|after_seq/);
|
|
181
217
|
const filtered = data(await historyList.execute({ session: "*", query: "needle", roles: ["tool", "context"], since: "2026-10-06T01:02:05", until: "2026-10-06T01:02:07Z" }, ctx));
|
|
182
|
-
assert.deepEqual(filtered.items.map((item) =>
|
|
218
|
+
assert.deepEqual(filtered.items.map((item) => item.role), ["tool"]);
|
|
183
219
|
const harness = data(await historyList.execute({ session: "claude:*", query: "needle" }, ctx));
|
|
184
220
|
assert.ok(harness.items.every((item) => "session" in item && item.session === "claude:claude-one"));
|
|
185
221
|
assert.equal(data(await historyList.execute({ session: "pi:absent*", query: "needle" }, ctx)).items.length, 0);
|
|
186
222
|
assert.ok(Check(historyList.outputSchema, page));
|
|
187
223
|
assert.match(historyList.render(result), /session pi:pi-one \| seq 1 \| user/);
|
|
188
224
|
});
|
|
225
|
+
test("bounded wildcard search scans every session and orders untimed and tied matches", async () => {
|
|
226
|
+
const seen = [];
|
|
227
|
+
const sessions = ["z", "a", "m"].map((id) => ({
|
|
228
|
+
harness: "pi", id, modifiedAt: 0,
|
|
229
|
+
async load() {
|
|
230
|
+
seen.push(id);
|
|
231
|
+
const items = Array.from({ length: 200 }, (_, index) => ({
|
|
232
|
+
seq: index + 1, windowId: "w", role: "user", text: `needle ${index}`,
|
|
233
|
+
...(index < 2 ? {} : { createdAt: AT }),
|
|
234
|
+
}));
|
|
235
|
+
return { projection: projectHistory([{ windowId: "w", items }], 200, new Set(items.map((item) => item.seq))) };
|
|
236
|
+
},
|
|
237
|
+
}));
|
|
238
|
+
const ctx = context(sessions);
|
|
239
|
+
const ids = (page) => page.items.map((item) => "seq" in item ? [item.session, item.seq] : null);
|
|
240
|
+
assert.deepEqual(ids(data(await historyList.execute({ session: "*", query: "needle", from: "oldest", limit: 3 }, ctx))), [["pi:a", 1], ["pi:a", 2], ["pi:m", 1]]);
|
|
241
|
+
assert.deepEqual(seen, ["z", "a", "m"]);
|
|
242
|
+
seen.length = 0;
|
|
243
|
+
assert.deepEqual(ids(data(await historyList.execute({ session: "*", query: "needle", limit: 3 }, ctx))), [["pi:z", 198], ["pi:z", 199], ["pi:z", 200]]);
|
|
244
|
+
assert.deepEqual(seen, ["z", "a", "m"]);
|
|
245
|
+
assert.deepEqual(ids(data(await historyList.execute({ session: "*", query: "needle", from: "oldest", limit: 1, since: AT }, ctx))), [["pi:a", 3]]);
|
|
246
|
+
});
|
|
189
247
|
test("query-less wildcards browse recent sessions, normalize empty queries, and keep canonical IDs", async (t) => {
|
|
190
248
|
const { ctx } = await fixture(t);
|
|
191
249
|
for (const query of [undefined, "", [], ["", ""]]) {
|
|
@@ -194,8 +252,8 @@ test("query-less wildcards browse recent sessions, normalize empty queries, and
|
|
|
194
252
|
assert.deepEqual(page.items.map((item) => "seq" in item ? [item.session, item.seq, item.role] : null), [
|
|
195
253
|
["pi:pi-one", 1, "user"], ["claude:claude-one", 1, "user"], ["pi:current", 1, "user"], [`codex:${CODEX}`, 2, "assistant"],
|
|
196
254
|
]);
|
|
197
|
-
assert.equal(page.
|
|
198
|
-
assert.equal(page.
|
|
255
|
+
assert.equal(page.has_older, false);
|
|
256
|
+
assert.equal(page.has_newer, false);
|
|
199
257
|
assert.ok(Check(historyList.outputSchema, page));
|
|
200
258
|
}
|
|
201
259
|
const harness = data(await historyList.execute({ session: "claude:*" }, ctx));
|
|
@@ -217,16 +275,23 @@ test("session overviews order discovery by mtime, stop at the session limit, the
|
|
|
217
275
|
});
|
|
218
276
|
const ctx = context(sessions);
|
|
219
277
|
const recent = data(await historyList.execute({ session: "*" }, ctx));
|
|
278
|
+
assert.equal(recent.has_older, true);
|
|
279
|
+
assert.equal(recent.has_newer, false);
|
|
220
280
|
assert.deepEqual(loaded, Array.from({ length: 20 }, (_, index) => 24 - index));
|
|
221
281
|
assert.deepEqual(recent.items.map((item) => item.session), loaded.map((index) => `pi:s-${index}`));
|
|
222
282
|
loaded.length = 0;
|
|
223
283
|
const oldest = data(await historyList.execute({ session: "*", from: "oldest", limit: 2 }, ctx));
|
|
284
|
+
assert.equal(oldest.has_older, false);
|
|
285
|
+
assert.equal(oldest.has_newer, true);
|
|
224
286
|
assert.deepEqual(loaded, [0, 1]);
|
|
225
287
|
assert.deepEqual(oldest.items.map((item) => item.session), ["pi:s-1", "pi:s-0"]);
|
|
226
288
|
loaded.length = 0;
|
|
227
289
|
const pattern = data(await historyList.execute({ session: "pi:s-2*", limit: 2 }, ctx));
|
|
228
290
|
assert.deepEqual(loaded, [24, 23]);
|
|
229
291
|
assert.deepEqual(pattern.items.map((item) => item.session), ["pi:s-24", "pi:s-23"]);
|
|
292
|
+
const all = data(await historyList.execute({ session: "*", limit: 25 }, ctx));
|
|
293
|
+
assert.equal(all.has_older, false);
|
|
294
|
+
assert.equal(all.has_newer, false);
|
|
230
295
|
});
|
|
231
296
|
test("session representatives respect visibility, role/time filters, opening users and dialogue/any fallbacks", async () => {
|
|
232
297
|
const at = (seconds) => new Date(Date.parse(AT) + seconds * 1000).toISOString();
|
|
@@ -237,7 +302,7 @@ test("session representatives respect visibility, role/time filters, opening use
|
|
|
237
302
|
];
|
|
238
303
|
const p = projectHistory([{ windowId: "w", items: messages.map(([role, text, seconds], index) => ({ seq: index + 1, windowId: "w", role, text, createdAt: at(seconds) })) }], 8, new Set([1, 2, 3, 4, 5, 6, 7, 8]));
|
|
239
304
|
const ctx = context([descriptor("claude", "topic", p)]);
|
|
240
|
-
const chosen = async (params) => data(await historyList.execute({ session: "*", ...params }, ctx)).items.map((item) =>
|
|
305
|
+
const chosen = async (params) => data(await historyList.execute({ session: "*", ...params }, ctx)).items.map((item) => item.seq);
|
|
241
306
|
assert.deepEqual(await chosen({}), [3]);
|
|
242
307
|
assert.deepEqual(await chosen({ roles: ["user"] }), [5]);
|
|
243
308
|
assert.deepEqual(await chosen({ roles: ["assistant"] }), [4]);
|
|
@@ -267,7 +332,7 @@ test("empty, filtered-out and unavailable sessions do not consume overview slots
|
|
|
267
332
|
const page = data(await historyList.execute({ session: "*", roles: ["user"], limit: 1 }, context(sessions)));
|
|
268
333
|
assert.deepEqual(page.items.map((item) => item.session), ["pi:topic"]);
|
|
269
334
|
assert.ok(loaded.includes("empty") && loaded.includes("filtered") && !loaded.includes("unneeded"));
|
|
270
|
-
assert.deepEqual(data(await historyList.execute({ session: "*" }, context([]))), { items: [],
|
|
335
|
+
assert.deepEqual(data(await historyList.execute({ session: "*" }, context([]))), { items: [], has_older: false, has_newer: false });
|
|
271
336
|
});
|
|
272
337
|
test("wildcard queries reach lazy loaders and pre-filtered misses are not errors", async () => {
|
|
273
338
|
const seen = [];
|
|
@@ -307,19 +372,19 @@ test("root overrides/env homes are hermetic, missing roots are empty, and foreig
|
|
|
307
372
|
const f = await fixture(t);
|
|
308
373
|
const roots = { pi: f.roots.pi, claude: null, codex: null };
|
|
309
374
|
const current = { harness: "dsh", id: "unreadable", load: async () => { throw new Error("current unavailable"); } };
|
|
310
|
-
const ctx = { current: () => { throw new Error("must not read current"); }, corpus: localHistoryCorpus({ roots, env: {}, home: join(f.home, "absent"), current }) };
|
|
375
|
+
const ctx = { current: () => { throw new Error("must not read current"); }, corpus: localHistoryCorpus({ roots, env: {}, summaryCacheRoot: join(f.home, "history-cache"), home: join(f.home, "absent"), current }) };
|
|
311
376
|
assert.equal(data(await historyRead.execute({ session: "pi:pi-one", seq: 1 }, ctx)).session, "pi:pi-one");
|
|
312
377
|
assert.equal(data(await historyList.execute({ session: "pi:*", query: "needle" }, ctx)).items.length, 2);
|
|
313
378
|
const env = { PI_CODING_AGENT_DIR: dirname(f.roots.pi), CLAUDE_CONFIG_DIR: dirname(f.roots.claude), CODEX_HOME: dirname(f.roots.codex) };
|
|
314
|
-
const configured = localHistoryCorpus({ current, home: join(f.home, "absent"), env });
|
|
379
|
+
const configured = localHistoryCorpus({ current, summaryCacheRoot: join(f.home, "history-cache"), home: join(f.home, "absent"), env });
|
|
315
380
|
assert.equal((await configured.sessions()).length, 4);
|
|
316
|
-
const explicit = localHistoryCorpus({ current, home: join(f.home, "absent"), env: { NOTESOUP_HISTORY_PI_ROOT: f.roots.pi, NOTESOUP_HISTORY_CLAUDE_ROOT: f.roots.claude, NOTESOUP_HISTORY_CODEX_ROOT: f.roots.codex } });
|
|
381
|
+
const explicit = localHistoryCorpus({ current, summaryCacheRoot: join(f.home, "history-cache"), home: join(f.home, "absent"), env: { NOTESOUP_HISTORY_PI_ROOT: f.roots.pi, NOTESOUP_HISTORY_CLAUDE_ROOT: f.roots.claude, NOTESOUP_HISTORY_CODEX_ROOT: f.roots.codex } });
|
|
317
382
|
assert.equal((await explicit.sessions()).length, 4);
|
|
318
383
|
refusal(await historyRead.execute({ session: "dsh:unreadable", seq: 1 }, ctx), "history_unavailable");
|
|
319
384
|
});
|
|
320
385
|
test("DSH service descriptors participate in resolution/search only when supplied; a live descriptor wins over disk", async (t) => {
|
|
321
386
|
const f = await fixture(t);
|
|
322
|
-
const corpus = localHistoryCorpus({ home: f.home, env: {}, current: descriptor("pi", "pi-one", projection("live needle")), dshSessions: async () => [descriptor("dsh", "dsh-one", projection("DSH needle", "system"))] });
|
|
387
|
+
const corpus = localHistoryCorpus({ home: f.home, env: {}, summaryCacheRoot: join(f.home, "history-cache"), current: descriptor("pi", "pi-one", projection("live needle")), dshSessions: async () => [descriptor("dsh", "dsh-one", projection("DSH needle", "system"))] });
|
|
323
388
|
const ctx = { current: () => projection(), corpus };
|
|
324
389
|
assert.equal(data(await historyRead.execute({ session: "dsh:dsh", seq: 1 }, ctx)).session, "dsh:dsh-one");
|
|
325
390
|
assert.equal(data(await historyRead.execute({ session: "pi-one", seq: 1 }, ctx)).text, "live needle 1");
|
|
@@ -336,8 +401,8 @@ test("oversized cross-session reads/searches obey both wire budgets and retain c
|
|
|
336
401
|
assert.ok(Buffer.byteLength(JSON.stringify(page)) <= TOOL_OUTPUT_MAX_BYTES);
|
|
337
402
|
assert.ok(Buffer.byteLength(historyList.render(list)) <= TOOL_OUTPUT_MAX_BYTES);
|
|
338
403
|
assert.equal(page.items.length, 1);
|
|
339
|
-
assert.equal(page.
|
|
340
|
-
assert.equal(page.
|
|
404
|
+
assert.equal(page.has_older, true);
|
|
405
|
+
assert.equal(page.has_newer, false);
|
|
341
406
|
const item = page.items[0];
|
|
342
407
|
assert.ok("seq" in item && item.truncated);
|
|
343
408
|
assert.equal(item.session, "codex:long-id");
|
|
@@ -350,15 +415,26 @@ test("oversized cross-session reads/searches obey both wire budgets and retain c
|
|
|
350
415
|
assert.ok(Buffer.byteLength(JSON.stringify(data(overview))) <= TOOL_OUTPUT_MAX_BYTES);
|
|
351
416
|
assert.ok(Buffer.byteLength(historyList.render(overview)) <= TOOL_OUTPUT_MAX_BYTES);
|
|
352
417
|
assert.equal(data(overview).items.length, 1);
|
|
353
|
-
assert.equal(data(overview).
|
|
354
|
-
assert.equal(data(overview).
|
|
418
|
+
assert.equal(data(overview).has_older, false);
|
|
419
|
+
assert.equal(data(overview).has_newer, false);
|
|
420
|
+
});
|
|
421
|
+
test("wildcard byte budgets report retained candidates beyond either end", async () => {
|
|
422
|
+
const ctx = context([descriptor("pi", "large", projection("needle " + "x".repeat(20_000)))]);
|
|
423
|
+
for (const from of ["newest", "oldest"]) {
|
|
424
|
+
const result = await historyList.execute({ session: "*", query: "needle", from, limit: 5, max_chars_per_item: 50_000 }, ctx);
|
|
425
|
+
const page = data(result);
|
|
426
|
+
assert.ok(page.items.length < 5);
|
|
427
|
+
assert.equal(page.has_older, from === "newest");
|
|
428
|
+
assert.equal(page.has_newer, from === "oldest");
|
|
429
|
+
assert.doesNotMatch(historyList.render(result), /before_seq|after_seq/);
|
|
430
|
+
}
|
|
355
431
|
});
|
|
356
432
|
test("qualified lookup and current-harness matches do not inspect unrelated roots", async (t) => {
|
|
357
433
|
const f = await fixture(t);
|
|
358
434
|
const notDirectory = join(f.home, "not-a-directory");
|
|
359
435
|
await writeFile(notDirectory, "unrelated root must not be traversed");
|
|
360
436
|
const current = descriptor("pi", "current");
|
|
361
|
-
const ctx = { current: () => projection(), corpus: localHistoryCorpus({ current, roots: { pi: f.roots.pi, claude: notDirectory, codex: null }, env: {} }) };
|
|
437
|
+
const ctx = { current: () => projection(), corpus: localHistoryCorpus({ current, summaryCacheRoot: join(f.home, "history-cache"), roots: { pi: f.roots.pi, claude: notDirectory, codex: null }, env: {} }) };
|
|
362
438
|
assert.equal(data(await historyRead.execute({ session: "pi-one", seq: 1 }, ctx)).session, "pi:pi-one");
|
|
363
439
|
assert.equal(data(await historyRead.execute({ session: "pi:pi-one", seq: 1 }, ctx)).session, "pi:pi-one");
|
|
364
440
|
assert.equal(data(await historyList.execute({ session: "pi:*", query: "needle" }, ctx)).items.length, 7);
|
|
@@ -399,6 +475,57 @@ test("wildcards skip corrupt and unreadable sessions, while single-session lists
|
|
|
399
475
|
refusal(await historyRead.execute({ session, seq: 1 }, ctx), "history_unavailable");
|
|
400
476
|
}
|
|
401
477
|
});
|
|
478
|
+
test("escaped streaming misses never call whole-file reads, synchronous decompression, or strict JSON parsing", async (t) => {
|
|
479
|
+
const f = await fixture(t), read = fs.readFile, decompress = zlib.zstdDecompressSync, parse = JSON.parse;
|
|
480
|
+
for (const path of [f.piPath, f.claudePath])
|
|
481
|
+
await f.put(path, (await readFile(path, "utf8")).replace(/needle/iu, String.raw `\u006eeedle`));
|
|
482
|
+
const escapedCodex = f.codexText.replace("needle", String.raw `\u006eeedle`);
|
|
483
|
+
await f.put(f.codexPath, zstdCompressSync(Buffer.from(escapedCodex)));
|
|
484
|
+
const fullReads = [];
|
|
485
|
+
let decompressions = 0, parses = 0;
|
|
486
|
+
t.mock.method(fs, "readFile", (...args) => { fullReads.push(String(args[0])); return read(...args); });
|
|
487
|
+
t.mock.method(zlib, "zstdDecompressSync", (...args) => { decompressions++; return decompress(...args); });
|
|
488
|
+
t.mock.method(JSON, "parse", (...args) => { parses++; return parse(...args); });
|
|
489
|
+
syncBuiltinESMExports();
|
|
490
|
+
t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
|
|
491
|
+
for (const compressed of [true, false]) {
|
|
492
|
+
if (!compressed) {
|
|
493
|
+
await rm(f.codexPath);
|
|
494
|
+
await f.put(f.codexPath.replace(/\.zst$/, ""), escapedCodex);
|
|
495
|
+
}
|
|
496
|
+
for (const session of (await f.corpus.sessions()).filter((session) => session.id !== "current"))
|
|
497
|
+
assert.equal(await session.load(["absentword"]), undefined, session.harness);
|
|
498
|
+
}
|
|
499
|
+
assert.deepEqual(fullReads, []);
|
|
500
|
+
assert.equal(decompressions, 0);
|
|
501
|
+
assert.equal(parses, 0);
|
|
502
|
+
});
|
|
503
|
+
test("Codex local query handoff reads each full source once per load, compressed or plain", async (t) => {
|
|
504
|
+
const f = await fixture(t), read = fs.readFile, decompress = zlib.zstdDecompressSync;
|
|
505
|
+
const fullReads = [];
|
|
506
|
+
let decompressions = 0;
|
|
507
|
+
t.mock.method(fs, "readFile", (...args) => { fullReads.push(String(args[0])); return read(...args); });
|
|
508
|
+
t.mock.method(zlib, "zstdDecompressSync", (...args) => { decompressions++; return decompress(...args); });
|
|
509
|
+
syncBuiltinESMExports();
|
|
510
|
+
t.after(() => { t.mock.restoreAll(); syncBuiltinESMExports(); });
|
|
511
|
+
for (const compressed of [true, false]) {
|
|
512
|
+
const path = compressed ? f.codexPath : f.codexPath.replace(/\.zst$/, "");
|
|
513
|
+
const put = (text) => f.put(path, compressed ? zstdCompressSync(Buffer.from(text)) : text, 100);
|
|
514
|
+
await put(f.codexText);
|
|
515
|
+
const session = (await f.corpus.sessions("codex"))[0];
|
|
516
|
+
for (const queries of [undefined, [], ["NEEDLE"]]) {
|
|
517
|
+
fullReads.length = 0;
|
|
518
|
+
decompressions = 0;
|
|
519
|
+
assert.equal(allItems((await session.load(queries)).projection)[0]?.text, "Codex needle");
|
|
520
|
+
assert.deepEqual(fullReads, [await realpath(path)]);
|
|
521
|
+
assert.equal(decompressions, compressed ? 1 : 0);
|
|
522
|
+
}
|
|
523
|
+
await put(f.codexText.replace("Codex needle", "updated needle"));
|
|
524
|
+
fullReads.length = 0;
|
|
525
|
+
assert.equal(allItems((await session.load(["NEEDLE"])).projection)[0]?.text, "updated needle");
|
|
526
|
+
assert.deepEqual(fullReads, [await realpath(path)]);
|
|
527
|
+
}
|
|
528
|
+
});
|
|
402
529
|
test("local query misses skip full decoding for Pi, Claude and compressed/uncompressed Codex", async (t) => {
|
|
403
530
|
const f = await fixture(t);
|
|
404
531
|
for (const session of (await f.corpus.sessions()).filter((session) => session.id !== "current")) {
|
|
@@ -425,7 +552,7 @@ test("local query misses skip full decoding for Pi, Claude and compressed/uncomp
|
|
|
425
552
|
assert.equal(await compressed.load(["needle"]), undefined);
|
|
426
553
|
await assert.rejects(() => compressed.load());
|
|
427
554
|
});
|
|
428
|
-
test("
|
|
555
|
+
test("streaming filtering never loses escaped JSON, Unicode or OR-query matches", async (t) => {
|
|
429
556
|
const f = await fixture(t);
|
|
430
557
|
const content = 'NEEDLE 你好 😀 quote"slash\\ line\nbreak İ LONGſ CASEΣ';
|
|
431
558
|
const escapeText = (text) => text.replaceAll("NEEDLE", "\\u004e\\u0045\\u0045\\u0044\\u004c\\u0045")
|
|
@@ -445,7 +572,7 @@ test("raw filtering never loses escaped JSON, Unicode or OR-query matches", asyn
|
|
|
445
572
|
}
|
|
446
573
|
assert.deepEqual(data(await historyList.execute({ session: "*", query: "absentword" }, f.ctx)).items, []);
|
|
447
574
|
});
|
|
448
|
-
test("
|
|
575
|
+
test("streaming filtering preserves generated tool documents, compact arguments and nested JSON escapes", async (t) => {
|
|
449
576
|
const f = await fixture(t);
|
|
450
577
|
await f.put(f.piPath, rows([
|
|
451
578
|
{ type: "session", id: "pi-one" },
|
|
@@ -468,7 +595,7 @@ test("raw filtering preserves generated tool documents, compact arguments and ne
|
|
|
468
595
|
assert.equal(page.items.length, 1, query);
|
|
469
596
|
}
|
|
470
597
|
});
|
|
471
|
-
test("Codex
|
|
598
|
+
test("Codex streaming prefilter preserves query hits inherited through a split history_base", async (t) => {
|
|
472
599
|
const f = await fixture(t);
|
|
473
600
|
const child = "019a0000-0000-7000-8000-000000000002";
|
|
474
601
|
const parent = rows([
|
|
@@ -476,13 +603,51 @@ test("Codex raw pre-filtering preserves query hits inherited through history_bas
|
|
|
476
603
|
{ type: "response_item", ordinal: 1, timestamp: AT, payload: { type: "message", role: "user", content: [{ type: "input_text", text: "ancestralneedle" }] } },
|
|
477
604
|
]);
|
|
478
605
|
await f.put(f.codexPath, zstdCompressSync(Buffer.from(parent)));
|
|
479
|
-
|
|
606
|
+
const childText = rows([
|
|
480
607
|
{ type: "session_meta", ordinal: 2, timestamp: AT, payload: { id: child, cwd: "/workspace", cli_version: "0.160.1", history_mode: "paginated", history_base: { thread_id: CODEX, end_ordinal_exclusive: 2, end_byte_offset: Buffer.byteLength(parent) }, context_window: { window_id: WINDOW } } },
|
|
481
608
|
{ type: "response_item", ordinal: 3, timestamp: AT, payload: { type: "message", role: "assistant", content: [{ type: "output_text", text: "local only" }] } },
|
|
482
|
-
])
|
|
609
|
+
]);
|
|
610
|
+
// Put the inheritance sentinel across the filesystem stream's 64KiB boundary.
|
|
611
|
+
await f.put(join(dirname(f.codexPath), `rollout-2026-10-06T01-02-03-${child}.jsonl`), " ".repeat(65535 - childText.indexOf('"history_base"')) + childText);
|
|
483
612
|
const page = data(await historyList.execute({ session: `codex:${child}*`, query: "ancestralneedle" }, f.ctx));
|
|
484
613
|
assert.deepEqual(page.items.map((item) => "seq" in item ? [item.session, item.seq] : null), [[`codex:${child}`, 2]]);
|
|
485
614
|
});
|
|
615
|
+
test("streaming search agrees with strict decoding across chunk and event-time boundaries", async (t) => {
|
|
616
|
+
const f = await fixture(t);
|
|
617
|
+
const content = "NEEDLE LONGſ CASEΣ K 𐐀 你好 😀";
|
|
618
|
+
const pad = (text) => " ".repeat(65535 - Buffer.byteLength(text.slice(0, text.indexOf("NEEDLE")))) + text;
|
|
619
|
+
await f.put(f.piPath, pad(rows([
|
|
620
|
+
{ type: "session", id: "pi-one" },
|
|
621
|
+
{ type: "message", id: "u", parentId: null, timestamp: AT, message: { role: "user", content } },
|
|
622
|
+
{ type: "message", id: "a", parentId: "u", message: { role: "assistant", content: "untimed needle" } },
|
|
623
|
+
])));
|
|
624
|
+
await f.put(f.claudePath, pad(rows([
|
|
625
|
+
{ type: "user", uuid: "u", parentUuid: null, sessionId: "claude-one", timestamp: "2026-10-06T01:02:04Z", message: { role: "user", content } },
|
|
626
|
+
])));
|
|
627
|
+
const codexText = pad(f.codexText.replace("Codex needle", content));
|
|
628
|
+
await f.put(f.codexPath, zstdCompressSync(Buffer.from(codexText)));
|
|
629
|
+
const plain = f.codexPath.replace(/\.zst$/, "");
|
|
630
|
+
for (const compressed of [true, false]) {
|
|
631
|
+
if (!compressed) {
|
|
632
|
+
await rm(f.codexPath);
|
|
633
|
+
await f.put(plain, codexText);
|
|
634
|
+
}
|
|
635
|
+
const sessions = await f.corpus.sessions();
|
|
636
|
+
const strict = { current: () => projection(), corpus: { currentHarness: "pi", sessions: async () => sessions.map((session) => ({ ...session, load: () => session.load() })) } };
|
|
637
|
+
for (const query of ["needle", "longs", "caseς", "k", "𐐨", "你好", "😀", "absentword", ["absentword", "needle"]]) {
|
|
638
|
+
for (const bounds of [{}, { since: AT, until: "2026-10-06T01:02:08Z" }, { since: "2026-10-06T01:02:08Z" }, { until: AT }]) {
|
|
639
|
+
const args = { session: "*", query, limit: 20, ...bounds };
|
|
640
|
+
assert.deepEqual(await historyList.execute(args, f.ctx), await historyList.execute(args, strict), JSON.stringify({ compressed, ...args }));
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
test("stream failures skip wildcard sessions but explicit selections still refuse", async (t) => {
|
|
646
|
+
const f = await fixture(t);
|
|
647
|
+
await f.put(f.codexPath, "not-zstd");
|
|
648
|
+
assert.deepEqual(data(await historyList.execute({ session: "codex:*", query: "absentword" }, f.ctx)).items, []);
|
|
649
|
+
refusal(await historyList.execute({ session: `codex:${CODEX}`, query: "absentword" }, f.ctx), "history_unavailable");
|
|
650
|
+
});
|
|
486
651
|
test("offline Claude attachments and retry branches are readable and searchable through session selectors", async (t) => {
|
|
487
652
|
const f = await fixture(t);
|
|
488
653
|
await f.put(f.claudePath, rows([
|
|
@@ -529,3 +694,26 @@ test("offline Claude prefers non-attachment leaves, then transcript order, and k
|
|
|
529
694
|
assert.throws(() => claudeHistoryFromJsonl(branch, { sessionId: "s" }), /no confirmed active head/);
|
|
530
695
|
assert.deepEqual(allItems(claudeHistoryFromJsonl(branch, { sessionId: "s", headUuid: "left" })).map((item) => item.seq), [1, 2]);
|
|
531
696
|
});
|
|
697
|
+
test("offline Claude parses each record once and preserves branch, attachment and gap projection", () => {
|
|
698
|
+
const user = (uuid, parentUuid) => ({ type: "user", uuid, parentUuid, sessionId: "s", message: { role: "user", content: uuid } });
|
|
699
|
+
const records = [user("root", "missing"), user("left", "root"), user("right", "root"),
|
|
700
|
+
{ type: "attachment", uuid: "hook", parentUuid: "root", sessionId: "s" }];
|
|
701
|
+
const text = rows(records);
|
|
702
|
+
const expected = claudeHistoryFromJsonl(text, { sessionId: "s", headUuid: offlineClaudeHead(text) });
|
|
703
|
+
const parse = JSON.parse;
|
|
704
|
+
let calls = 0;
|
|
705
|
+
let actual;
|
|
706
|
+
try {
|
|
707
|
+
JSON.parse = (...args) => { calls++; return parse(...args); };
|
|
708
|
+
actual = offlineClaudeHistoryFromJsonl(text, "s");
|
|
709
|
+
}
|
|
710
|
+
finally {
|
|
711
|
+
JSON.parse = parse;
|
|
712
|
+
}
|
|
713
|
+
assert.equal(calls, records.length);
|
|
714
|
+
assert.deepEqual(actual, expected);
|
|
715
|
+
assert.deepEqual(allItems(actual).map((item) => [item.seq, item.text]), [[1, "root"], [3, "right"]]);
|
|
716
|
+
assert.deepEqual(actual.missingAncestors, ["missing"]);
|
|
717
|
+
assert.deepEqual(offlineClaudeHistoryFromJsonl(text + '{"partial":', "s"), expected);
|
|
718
|
+
assert.throws(() => offlineClaudeHistoryFromJsonl(text + "bad\n", "s"), /malformed Claude transcript JSON/);
|
|
719
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { test } from "node:test";
|
|
6
|
+
import { zstdCompressSync } from "node:zlib";
|
|
7
|
+
import { createHistoryPrefilter, historyFileMayMatch } from "../src/history/prefilter.js";
|
|
8
|
+
async function* chunks(bytes, width) {
|
|
9
|
+
for (let offset = 0; offset < bytes.length; offset += width)
|
|
10
|
+
yield bytes.subarray(offset, offset + width);
|
|
11
|
+
}
|
|
12
|
+
test("streaming tokens preserve Unicode iu matching at every UTF-8 split and across files", async () => {
|
|
13
|
+
const cases = [
|
|
14
|
+
["LONGſ CASEΣ K 𐐀 你好", ["longs caseς k 𐐨 你好"], true],
|
|
15
|
+
["beta --- alpha", ["alpha beta"], true],
|
|
16
|
+
["beta --- alpha", ["alpha beta missing"], false],
|
|
17
|
+
["unrelated --- NEEDLE", ["nohit", "needle"], true],
|
|
18
|
+
["NEEDLE", ["nohit needle", "absentword"], false],
|
|
19
|
+
["NEEDLE", ["need.*le"], true],
|
|
20
|
+
];
|
|
21
|
+
for (const [text, queries, expected] of cases) {
|
|
22
|
+
const plan = createHistoryPrefilter(queries), bytes = Buffer.from(text);
|
|
23
|
+
for (let width = 1; width <= bytes.length; width++)
|
|
24
|
+
assert.equal(await plan(chunks(bytes, width)), expected, `${JSON.stringify(queries)} at ${width}`);
|
|
25
|
+
assert.equal(await plan(chunks(Buffer.from("no match here"), 1)), false, "token bits must not leak between files");
|
|
26
|
+
assert.equal(await plan(chunks(bytes, 1)), expected, "the compiled plan is reusable");
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
test("only EOF proves a miss, while errors after tokens or a fallback still propagate", async () => {
|
|
30
|
+
let finished = false;
|
|
31
|
+
async function* late() {
|
|
32
|
+
yield Buffer.from("unrelated");
|
|
33
|
+
yield Buffer.from("needle");
|
|
34
|
+
finished = true;
|
|
35
|
+
}
|
|
36
|
+
assert.equal(await createHistoryPrefilter(["needle"])(late()), true);
|
|
37
|
+
assert.equal(finished, true);
|
|
38
|
+
async function* broken() { yield Buffer.from("needle"); throw new Error("read failed"); }
|
|
39
|
+
for (const queries of [["absentword"], ["needle"], ["😀"]])
|
|
40
|
+
await assert.rejects(createHistoryPrefilter(queries)(broken()), /read failed/);
|
|
41
|
+
});
|
|
42
|
+
test("incremental Unicode unescaping matches the old prefilter at every byte split", async () => {
|
|
43
|
+
const presentation = `Codex unknown arguments not recorded output nested calls complete incomplete terminal ms image audio items null
|
|
44
|
+
copied subagent inference prefix omitted child-local history starts at native ordinal copied model context
|
|
45
|
+
does not prove original parent event addresses compaction boundary replacement_history is an inference checkpoint
|
|
46
|
+
not new conversation events opaque content omitted not recovered plaintext MCP completion without a recorded raw call
|
|
47
|
+
FunctionCallOutput display without raw output encrypted content unavailable world_state snapshot patch omitted from
|
|
48
|
+
the text projection not a newly spoken message native structured retained context not newly spoken dialogue`;
|
|
49
|
+
const prior = (text, queries) => {
|
|
50
|
+
const normalized = text.replace(/\\+u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(Number.parseInt(hex, 16)));
|
|
51
|
+
return queries.some((query) => (query.match(/[\p{L}\p{N}_-]+/gu) ?? []).every((token) => {
|
|
52
|
+
const parts = /^\d+ms$/iu.test(token) ? [token.slice(0, -2), "ms"] : [token];
|
|
53
|
+
return parts.every((part) => {
|
|
54
|
+
const literal = new RegExp(part, "iu");
|
|
55
|
+
return literal.test(text) || literal.test(normalized) || literal.test(presentation);
|
|
56
|
+
});
|
|
57
|
+
}));
|
|
58
|
+
};
|
|
59
|
+
const cases = [
|
|
60
|
+
[String.raw `{"text":"\u006eeedle"}`, ["needle"]],
|
|
61
|
+
[String.raw `{"text":"\u006eeedle"}`, ["u006eeedle"]],
|
|
62
|
+
[String.raw `{"text":"\u006eeedle"}`, ["needle u006eeedle"]],
|
|
63
|
+
[String.raw `{"text":"\u006eeedle"}`, ["absentword", "needle"]],
|
|
64
|
+
[String.raw `{"text":"\u006eeedle"}`, ["needle absentword"]],
|
|
65
|
+
[String.raw `NEE\u0044LE LONG\u017f CASE\u03a3`, ["needle longs caseς"]],
|
|
66
|
+
[String.raw `\uD801\uDC00needle`, ["𐐨needle"]],
|
|
67
|
+
[String.raw `\uD801\uDC00needle`, ["ud801 𐐨needle"]],
|
|
68
|
+
[String.raw `\u005cu006eeedle`, ["needle"]],
|
|
69
|
+
[String.raw `\u005c\u0075\u0030\u0030\u0036\u0065eedle`, ["needle"]],
|
|
70
|
+
[String.raw `prefix\\u006eeedle`, ["prefixneedle"]],
|
|
71
|
+
[String.raw `\u006e\\u0065edle`, ["needle"]],
|
|
72
|
+
[String.raw `{"arguments":"{\"path\":\"\\u006eeedle\"}"}`, ["needle"]],
|
|
73
|
+
[String.raw `\u00xz needle\u00`, ["needle u00"]],
|
|
74
|
+
[String.raw `need\u00xzle`, ["needle"]],
|
|
75
|
+
[String.raw `\u006e\u00xz`, ["nu00xz"]],
|
|
76
|
+
[String.raw `\u006e\u00xz`, ["u00xz"]],
|
|
77
|
+
[String.raw `\u006e\u00`, ["u00"]],
|
|
78
|
+
[String.raw `\u006e` + "\\", ["absentword"]],
|
|
79
|
+
[String.raw `\U006eeedle`, ["needle"]],
|
|
80
|
+
["LONGſ CASEΣ 你好", ["longs caseς 你好"]],
|
|
81
|
+
];
|
|
82
|
+
for (const [text, queries] of cases) {
|
|
83
|
+
const bytes = Buffer.from(text), plan = createHistoryPrefilter(queries), expected = prior(text, queries);
|
|
84
|
+
for (let cut = 1; cut < bytes.length; cut++) {
|
|
85
|
+
async function* split() { yield bytes.subarray(0, cut); yield bytes.subarray(cut); }
|
|
86
|
+
assert.equal(await plan(split()), expected, `${JSON.stringify({ text, queries })} split ${cut}`);
|
|
87
|
+
}
|
|
88
|
+
assert.equal(await plan(chunks(bytes, 1)), expected, JSON.stringify({ text, queries }));
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
test("only Codex inheritance, not Unicode escapes, forces conservative fallback", async () => {
|
|
92
|
+
for (const text of [String.raw `{"text":"\u006eeedle"}`, String.raw `{"arguments":"{\"value\":\"\\u006eeedle\"}"}`]) {
|
|
93
|
+
for (let width = 1; width < text.length; width++) {
|
|
94
|
+
assert.equal(await createHistoryPrefilter(["needle"])(chunks(Buffer.from(text), width)), true);
|
|
95
|
+
assert.equal(await createHistoryPrefilter(["absentword"])(chunks(Buffer.from(text), width)), false);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const base = Buffer.from('{"history_base":{"thread_id":"parent"}}');
|
|
99
|
+
for (let width = 1; width < base.length; width++) {
|
|
100
|
+
const plan = createHistoryPrefilter(["ancestralneedle"]);
|
|
101
|
+
assert.equal(await plan(chunks(base, width), true), true);
|
|
102
|
+
assert.equal(await plan(chunks(base, width)), false);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
test("generated presentation, compact durations, and queries without safe tokens never false-negative", async () => {
|
|
106
|
+
for (const queries of [[], [""], ["😀"], [".*"], ["Codex unknown arguments"], ["absentword", "😀"]]) {
|
|
107
|
+
assert.equal(await createHistoryPrefilter(queries)(chunks(Buffer.from("unrelated"), 1)), true, JSON.stringify(queries));
|
|
108
|
+
}
|
|
109
|
+
for (const query of ["25ms", "25MS", "output 25ms"]) {
|
|
110
|
+
const plan = createHistoryPrefilter([query]);
|
|
111
|
+
assert.equal(await plan(chunks(Buffer.from('{"duration_ms":25}'), 1)), true, query);
|
|
112
|
+
assert.equal(await plan(chunks(Buffer.from('{"duration_ms":26}'), 1)), false, query);
|
|
113
|
+
}
|
|
114
|
+
assert.equal(await createHistoryPrefilter(["output absentword"])(chunks(Buffer.from("unrelated"), 1)), false);
|
|
115
|
+
});
|
|
116
|
+
test("plain and zstd streams propagate unavailable/malformed input and enforce stored and decoded budgets", async (t) => {
|
|
117
|
+
const home = await mkdtemp(join(tmpdir(), "notesoup-prefilter-"));
|
|
118
|
+
t.after(() => rm(home, { recursive: true, force: true }));
|
|
119
|
+
const plain = join(home, "transcript.jsonl"), compressed = plain + ".zst";
|
|
120
|
+
const text = "unrelated ".repeat(8192);
|
|
121
|
+
await writeFile(plain, text);
|
|
122
|
+
await writeFile(compressed, zstdCompressSync(Buffer.from(text)));
|
|
123
|
+
for (const path of [plain, compressed]) {
|
|
124
|
+
assert.equal(await historyFileMayMatch(path, createHistoryPrefilter(["needle"]), true, text.length), false);
|
|
125
|
+
assert.equal(await historyFileMayMatch(path, createHistoryPrefilter(["unrelated"]), true, text.length), true);
|
|
126
|
+
await assert.rejects(historyFileMayMatch(path, createHistoryPrefilter(["needle"]), true, text.length - 1), /size limit exceeded/);
|
|
127
|
+
await assert.rejects(historyFileMayMatch(path, createHistoryPrefilter(["😀"]), true, text.length - 1), /size limit exceeded/);
|
|
128
|
+
}
|
|
129
|
+
await writeFile(compressed, "not zstd");
|
|
130
|
+
await assert.rejects(historyFileMayMatch(compressed, createHistoryPrefilter(["needle"]), true, text.length));
|
|
131
|
+
await assert.rejects(historyFileMayMatch(home, createHistoryPrefilter(["needle"]), true), /not a regular file/);
|
|
132
|
+
await assert.rejects(historyFileMayMatch(join(home, "missing"), createHistoryPrefilter(["needle"])), /ENOENT/);
|
|
133
|
+
});
|
|
134
|
+
test("arbitrarily long backslash runs retain only bounded normalization input", async (t) => {
|
|
135
|
+
const block = Buffer.alloc(64 * 1024, 92), count = 4096;
|
|
136
|
+
let largest = 0, consumed = 0;
|
|
137
|
+
const replace = RegExp.prototype[Symbol.replace];
|
|
138
|
+
Object.defineProperty(RegExp.prototype, Symbol.replace, { value: function (...args) {
|
|
139
|
+
largest = Math.max(largest, args[0].length);
|
|
140
|
+
return replace.apply(this, args);
|
|
141
|
+
} });
|
|
142
|
+
t.after(() => { Object.defineProperty(RegExp.prototype, Symbol.replace, { value: replace }); });
|
|
143
|
+
async function* run(suffix) {
|
|
144
|
+
yield Buffer.from("n");
|
|
145
|
+
for (let index = 0; index < count; index++) {
|
|
146
|
+
consumed++;
|
|
147
|
+
yield block;
|
|
148
|
+
}
|
|
149
|
+
yield Buffer.from(suffix);
|
|
150
|
+
}
|
|
151
|
+
assert.equal(await createHistoryPrefilter(["needle"])(run("u0065edle")), true);
|
|
152
|
+
assert.equal(await createHistoryPrefilter(["needle"])(run("u00x5edle")), false);
|
|
153
|
+
assert.equal(consumed * block.length, 512 * 1024 * 1024);
|
|
154
|
+
assert.ok(largest <= block.length + 6, `largest normalization input: ${largest}`);
|
|
155
|
+
});
|
|
156
|
+
test("a large miss scans bounded strings rather than a complete transcript or line", async (t) => {
|
|
157
|
+
const block = Buffer.alloc(64 * 1024, 120), count = 4096;
|
|
158
|
+
let consumed = 0, largest = 0;
|
|
159
|
+
const original = RegExp.prototype.test;
|
|
160
|
+
t.mock.method(RegExp.prototype, "test", function (text) {
|
|
161
|
+
if (this.source === "absentword")
|
|
162
|
+
largest = Math.max(largest, text.length);
|
|
163
|
+
return original.call(this, text);
|
|
164
|
+
});
|
|
165
|
+
async function* large() {
|
|
166
|
+
for (let index = 0; index < count; index++) {
|
|
167
|
+
consumed++;
|
|
168
|
+
yield block;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
assert.equal(await createHistoryPrefilter(["absentword"])(large()), false);
|
|
172
|
+
assert.equal(consumed * block.length, 256 * 1024 * 1024);
|
|
173
|
+
assert.ok(largest <= block.length + '"history_base"'.length, `largest regex input: ${largest}`);
|
|
174
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|