wherever-dev 0.10.14 → 0.11.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/dist/commands/install.d.ts +21 -0
- package/dist/commands/install.d.ts.map +1 -1
- package/dist/commands/install.js +70 -3
- package/dist/commands/install.js.map +1 -1
- package/dist/conversation-search.d.ts +117 -0
- package/dist/conversation-search.d.ts.map +1 -0
- package/dist/conversation-search.js +295 -0
- package/dist/conversation-search.js.map +1 -0
- package/dist/index.js +279 -32
- package/dist/index.js.map +1 -1
- package/dist/protocol.d.ts +3 -0
- package/dist/protocol.d.ts.map +1 -1
- package/dist/session-pool.d.ts +64 -32
- package/dist/session-pool.d.ts.map +1 -1
- package/dist/session-pool.js +184 -352
- package/dist/session-pool.js.map +1 -1
- package/dist/session-transcript.d.ts +120 -0
- package/dist/session-transcript.d.ts.map +1 -0
- package/dist/session-transcript.js +813 -0
- package/dist/session-transcript.js.map +1 -0
- package/package.json +2 -1
- package/public/_app/immutable/assets/0.B76aBayq.css +1 -0
- package/public/_app/immutable/chunks/{ClTMu0mO.js → BLQ6nRXO.js} +2 -2
- package/public/_app/immutable/chunks/{ClTMu0mO.js.map → BLQ6nRXO.js.map} +1 -1
- package/public/_app/immutable/chunks/{BzFqVyJ_.js → CKmxwmEQ.js} +2 -2
- package/public/_app/immutable/chunks/{BzFqVyJ_.js.map → CKmxwmEQ.js.map} +1 -1
- package/public/_app/immutable/chunks/{CDbw105l.js → DDXW2gm3.js} +2 -2
- package/public/_app/immutable/chunks/{CDbw105l.js.map → DDXW2gm3.js.map} +1 -1
- package/public/_app/immutable/entry/{app.CqLyltZH.js → app.BXjvSIVv.js} +3 -3
- package/public/_app/immutable/entry/{app.CqLyltZH.js.map → app.BXjvSIVv.js.map} +1 -1
- package/public/_app/immutable/entry/start.BY0WA8dA.js +2 -0
- package/public/_app/immutable/entry/start.BY0WA8dA.js.map +1 -0
- package/public/_app/immutable/nodes/{0.D_7-7cgp.js → 0.HhOK-JKB.js} +2 -2
- package/public/_app/immutable/nodes/{0.D_7-7cgp.js.map → 0.HhOK-JKB.js.map} +1 -1
- package/public/_app/immutable/nodes/{1.CDxNOU-Q.js → 1.BWYloVJt.js} +2 -2
- package/public/_app/immutable/nodes/{1.CDxNOU-Q.js.map → 1.BWYloVJt.js.map} +1 -1
- package/public/_app/immutable/nodes/2.Dq6JhX_v.js +104 -0
- package/public/_app/immutable/nodes/2.Dq6JhX_v.js.map +1 -0
- package/public/_app/version.json +1 -1
- package/public/index.html +11 -11
- package/public/service-worker.js +1 -1
- package/public/_app/immutable/assets/0.BLdSNDwj.css +0 -1
- package/public/_app/immutable/entry/start.CQ4WvoOm.js +0 -2
- package/public/_app/immutable/entry/start.CQ4WvoOm.js.map +0 -1
- package/public/_app/immutable/nodes/2.D1gQZgOb.js +0 -95
- package/public/_app/immutable/nodes/2.D1gQZgOb.js.map +0 -1
|
@@ -0,0 +1,813 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded-memory readers for session `.jsonl` transcripts.
|
|
3
|
+
*
|
|
4
|
+
* A pi session file is an append-only JSONL transcript, and on a real machine
|
|
5
|
+
* that corpus is GIGABYTES (measured: 3,831 files / 2.0 GB, single files up to
|
|
6
|
+
* 62 MB). Every reader in this server therefore has ONE hard rule:
|
|
7
|
+
*
|
|
8
|
+
* **Never materialize a whole transcript.**
|
|
9
|
+
*
|
|
10
|
+
* That rule is not a micro-optimization, it is the difference between a server
|
|
11
|
+
* that idles at tens of MB and one systemd-oomd kills. The two paths that used
|
|
12
|
+
* to break it, and what they cost, are:
|
|
13
|
+
*
|
|
14
|
+
* - the `/sessions` listing scan, which did `readFile(utf8)` + `split('\n')` +
|
|
15
|
+
* `JSON.parse` per line into a RETAINED `entries[]` array, per file. One
|
|
16
|
+
* 62 MB transcript becomes a 62 MB string, a ~62 MB array of line strings and
|
|
17
|
+
* a few hundred MB of parsed objects, all live at once. Startup (which warms
|
|
18
|
+
* the listing cache) peaked at ~1.0 GB RSS and settled at ~460 MB.
|
|
19
|
+
* - every history read (`session_load`, `history_page`, `cli_register`), which
|
|
20
|
+
* went through pi's `SessionManager.open()` (which loads and parses the WHOLE
|
|
21
|
+
* file TWICE) and then mapped EVERY entry into a `HistoryMessage[]`
|
|
22
|
+
* (including base64 tool images) only to slice the last 60. Repeated for the
|
|
23
|
+
* life of the process, so RSS ratchets up and never comes back down.
|
|
24
|
+
*
|
|
25
|
+
* Both are replaced by streaming passes here, in three layers:
|
|
26
|
+
*
|
|
27
|
+
* 1. newlines are found in the BYTES of a pooled 64 KB chunk, so nothing is
|
|
28
|
+
* decoded just to be looked at;
|
|
29
|
+
* 2. each line is classified from a bounded 512-byte HEAD, and a line the
|
|
30
|
+
* caller does not want is DISCARDED without ever becoming a string;
|
|
31
|
+
* 3. only the wanted lines are decoded and parsed, and the parse result is
|
|
32
|
+
* dropped unless it belongs in the answer.
|
|
33
|
+
*
|
|
34
|
+
* So peak memory is bounded by the largest line we actually WANT (tool results,
|
|
35
|
+
* which are most of a transcript's bytes, are never assembled), and the retained
|
|
36
|
+
* result is bounded by what the caller asked for: one small listing record, or
|
|
37
|
+
* one window of history.
|
|
38
|
+
*
|
|
39
|
+
* The readers are async and yield between chunks so a multi-MB transcript can
|
|
40
|
+
* never pin the event loop (which is what made "Loading session..." hang before
|
|
41
|
+
* the listing scan was made async).
|
|
42
|
+
*
|
|
43
|
+
* Measured on that 2.0 GB corpus (built server, same method on both sides):
|
|
44
|
+
* startup peak RSS 990 MB -> 208 MB; opening the largest session (59 MB) 401 ms
|
|
45
|
+
* of blocked loop and ~130 MB per open -> 71 ms non-blocking, plateauing at
|
|
46
|
+
* ~340 MB over 150 consecutive opens with live heap flat at 39 MB.
|
|
47
|
+
* `server/test/bench/` holds the harnesses; re-measure there before trusting a
|
|
48
|
+
* change to this file.
|
|
49
|
+
*/
|
|
50
|
+
import fs from 'node:fs';
|
|
51
|
+
/**
|
|
52
|
+
* Chunk size for the streaming line reader. Deliberately modest: the buffer is
|
|
53
|
+
* allocated per open file and the reader is async, so several can be in flight;
|
|
54
|
+
* bigger chunks buy nothing here (the parse dominates) and cost resident bytes.
|
|
55
|
+
*/
|
|
56
|
+
const READ_CHUNK_BYTES = 64 * 1024;
|
|
57
|
+
/**
|
|
58
|
+
* Cap on a single line the LISTING scan is willing to hand to `JSON.parse`. A
|
|
59
|
+
* parsed object graph costs several times its source text, and the listing only
|
|
60
|
+
* ever needs a 160-char preview and a timestamp out of a line, so an absurd one
|
|
61
|
+
* (a multi-MB pasted payload) falls back to bounded text probes instead. The
|
|
62
|
+
* HISTORY reader has no such cap: there, the line's content IS the answer, and
|
|
63
|
+
* refusing to parse it would silently drop a message from the transcript.
|
|
64
|
+
*/
|
|
65
|
+
const MAX_LISTING_PARSE_BYTES = 4 * 1024 * 1024;
|
|
66
|
+
/**
|
|
67
|
+
* Max length of the first-message PREVIEW shipped in the /sessions list. The
|
|
68
|
+
* sidebar only renders a ~40-char snippet and filters on the text, so the full
|
|
69
|
+
* (often huge: pasted prompts, PRDs, specs) first message must never cross the
|
|
70
|
+
* wire for every session. This caps each entry, which is the dominant factor in
|
|
71
|
+
* the /sessions payload size.
|
|
72
|
+
*/
|
|
73
|
+
const FIRST_MESSAGE_PREVIEW_MAX = 160;
|
|
74
|
+
/**
|
|
75
|
+
* Force a string to be an independent, flat copy.
|
|
76
|
+
*
|
|
77
|
+
* V8 represents `big.slice(0, 160)` as a SlicedString that keeps its PARENT
|
|
78
|
+
* alive. That is free when the preview is transient, but the listing cache
|
|
79
|
+
* holds one preview per session for the process's lifetime, so an un-flattened
|
|
80
|
+
* slice would pin every full first message (pasted PRDs, specs) in memory:
|
|
81
|
+
* measured at ~33 MB of retained parents for ~2800 sessions whose visible
|
|
82
|
+
* previews total under 1 MB. The Buffer round-trip allocates a fresh string.
|
|
83
|
+
*/
|
|
84
|
+
export function flattenString(s) {
|
|
85
|
+
return s.length === 0 ? s : Buffer.from(s, 'utf8').toString('utf8');
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Collapse whitespace and cap to a short preview. Keeps the /sessions payload
|
|
89
|
+
* tens-of-KB instead of multi-MB while preserving what the sidebar displays and
|
|
90
|
+
* filters on. The result is flattened so caching it cannot retain the (possibly
|
|
91
|
+
* huge) message it was sliced from.
|
|
92
|
+
*/
|
|
93
|
+
export function previewText(text) {
|
|
94
|
+
const collapsed = (text || '').replace(/\s+/g, ' ').trim();
|
|
95
|
+
return flattenString(collapsed.length > FIRST_MESSAGE_PREVIEW_MAX
|
|
96
|
+
? collapsed.slice(0, FIRST_MESSAGE_PREVIEW_MAX) + '\u2026'
|
|
97
|
+
: collapsed);
|
|
98
|
+
}
|
|
99
|
+
/** Yield to the event loop between chunks of a large transcript. */
|
|
100
|
+
function yieldToEventLoop() {
|
|
101
|
+
return new Promise((resolve) => setImmediate(resolve));
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Read buffers, reused across files.
|
|
105
|
+
*
|
|
106
|
+
* A 64 KB `Buffer.allocUnsafe` is above V8's pooling threshold, so each one is
|
|
107
|
+
* its own external allocation that only goes away at the next GC. Allocating a
|
|
108
|
+
* fresh one per file meant ~240 MB of external churn for a single pass over a
|
|
109
|
+
* 3,700-file sessions directory, and the process RSS stayed at that high-water
|
|
110
|
+
* mark long after the bytes were dead (glibc does not hand the arena back).
|
|
111
|
+
* Reusing them caps the buffers in play at the number of CONCURRENT readers,
|
|
112
|
+
* which is a handful.
|
|
113
|
+
*/
|
|
114
|
+
const bufferPool = [];
|
|
115
|
+
const BUFFER_POOL_MAX = 8;
|
|
116
|
+
function takeBuffer() {
|
|
117
|
+
return bufferPool.pop() ?? Buffer.allocUnsafe(READ_CHUNK_BYTES);
|
|
118
|
+
}
|
|
119
|
+
function releaseBuffer(buf) {
|
|
120
|
+
if (bufferPool.length < BUFFER_POOL_MAX)
|
|
121
|
+
bufferPool.push(buf);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* How much of a line a `skip` probe gets to look at, in BYTES. Enough to cover a
|
|
125
|
+
* session entry's fixed header keys (`type`, `id`, `parentId`, `timestamp`, and
|
|
126
|
+
* the opening of `message`), which is all any classification here needs. A head
|
|
127
|
+
* cut mid-character decodes with a replacement char at its end; every probe is
|
|
128
|
+
* anchored on ASCII structure, so that is harmless.
|
|
129
|
+
*/
|
|
130
|
+
const PROBE_HEAD_BYTES = 512;
|
|
131
|
+
/**
|
|
132
|
+
* Stream a `.jsonl` file line by line, holding at most one chunk plus the line
|
|
133
|
+
* currently being assembled. Returns false when the file could not be opened.
|
|
134
|
+
*
|
|
135
|
+
* Newlines are found in the BYTES, and only the parts we actually want are
|
|
136
|
+
* decoded to strings: a `skip` probe sees a bounded head (a few hundred bytes),
|
|
137
|
+
* and a claimed line is never decoded at all. Decoding every chunk instead --
|
|
138
|
+
* the obvious implementation -- turns the entire corpus into string garbage
|
|
139
|
+
* (2 GB of transcripts is 2 GB of throwaway strings), which is exactly the churn
|
|
140
|
+
* that keeps a long-lived process's RSS pinned at its high-water mark.
|
|
141
|
+
*
|
|
142
|
+
* The strings handed to the callbacks are TRANSIENT. Anything retained past the
|
|
143
|
+
* callback must be flattened (`flattenString` / `previewText`), or it pins the
|
|
144
|
+
* chunk it came from.
|
|
145
|
+
*/
|
|
146
|
+
const NEWLINE = 0x0a;
|
|
147
|
+
export async function forEachJsonlLine(filePath, handler) {
|
|
148
|
+
const reader = typeof handler === 'function' ? { line: handler } : handler;
|
|
149
|
+
let fh;
|
|
150
|
+
try {
|
|
151
|
+
fh = await fs.promises.open(filePath, 'r');
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
const buffer = takeBuffer();
|
|
157
|
+
try {
|
|
158
|
+
// Pieces of the line currently being assembled, as COPIES: `buffer` is
|
|
159
|
+
// overwritten by the next read, so anything that must outlive this chunk has
|
|
160
|
+
// to be copied out. Only lines that span a chunk boundary pay for this; a
|
|
161
|
+
// line contained in one chunk is decoded straight from `buffer`.
|
|
162
|
+
let pieces = [];
|
|
163
|
+
let piecesLen = 0;
|
|
164
|
+
// Head bytes of the current line, for the classification probe.
|
|
165
|
+
const head = Buffer.allocUnsafe(PROBE_HEAD_BYTES);
|
|
166
|
+
let headLen = 0;
|
|
167
|
+
// undefined = not classified yet, true = discard the rest of this line.
|
|
168
|
+
let skipping = reader.skip ? undefined : false;
|
|
169
|
+
let chunks = 0;
|
|
170
|
+
let stopped = false;
|
|
171
|
+
const classify = () => {
|
|
172
|
+
if (skipping !== undefined || !reader.skip)
|
|
173
|
+
return skipping;
|
|
174
|
+
skipping = reader.skip(head.toString('utf8', 0, headLen));
|
|
175
|
+
if (skipping) {
|
|
176
|
+
pieces = [];
|
|
177
|
+
piecesLen = 0;
|
|
178
|
+
}
|
|
179
|
+
return skipping;
|
|
180
|
+
};
|
|
181
|
+
const resetLine = () => {
|
|
182
|
+
pieces = [];
|
|
183
|
+
piecesLen = 0;
|
|
184
|
+
headLen = 0;
|
|
185
|
+
skipping = reader.skip ? undefined : false;
|
|
186
|
+
};
|
|
187
|
+
/** Take bytes [from, to) of `buf` into the line being assembled. */
|
|
188
|
+
const addSegment = (buf, from, to) => {
|
|
189
|
+
if (skipping === true || to <= from)
|
|
190
|
+
return;
|
|
191
|
+
if (headLen < PROBE_HEAD_BYTES) {
|
|
192
|
+
const n = Math.min(PROBE_HEAD_BYTES - headLen, to - from);
|
|
193
|
+
buf.copy(head, headLen, from, from + n);
|
|
194
|
+
headLen += n;
|
|
195
|
+
if (headLen >= PROBE_HEAD_BYTES && classify() === true)
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
pieces.push(Buffer.from(buf.subarray(from, to)));
|
|
199
|
+
piecesLen += to - from;
|
|
200
|
+
};
|
|
201
|
+
/** Complete the current line and hand it over (unless it was claimed). */
|
|
202
|
+
const finishLine = () => {
|
|
203
|
+
classify();
|
|
204
|
+
if (!skipping && piecesLen > 0) {
|
|
205
|
+
const line = pieces.length === 1 ? pieces[0] : Buffer.concat(pieces, piecesLen);
|
|
206
|
+
if (reader.line(line.toString('utf8')) === false)
|
|
207
|
+
stopped = true;
|
|
208
|
+
}
|
|
209
|
+
resetLine();
|
|
210
|
+
};
|
|
211
|
+
for (;;) {
|
|
212
|
+
const { bytesRead } = await fh.read(buffer, 0, buffer.length, null);
|
|
213
|
+
if (bytesRead === 0)
|
|
214
|
+
break;
|
|
215
|
+
// Yield periodically so a 60 MB transcript cannot pin the loop.
|
|
216
|
+
if (++chunks % 32 === 0)
|
|
217
|
+
await yieldToEventLoop();
|
|
218
|
+
const view = buffer.subarray(0, bytesRead);
|
|
219
|
+
let start = 0;
|
|
220
|
+
for (;;) {
|
|
221
|
+
const nl = view.indexOf(NEWLINE, start);
|
|
222
|
+
if (nl === -1)
|
|
223
|
+
break;
|
|
224
|
+
if (pieces.length === 0 && headLen === 0) {
|
|
225
|
+
// Fast path: the whole line is in this chunk, so it can be classified
|
|
226
|
+
// and decoded in place -- no copy, no per-chunk decode.
|
|
227
|
+
if (reader.skip) {
|
|
228
|
+
const headEnd = Math.min(nl, start + PROBE_HEAD_BYTES);
|
|
229
|
+
skipping = reader.skip(view.toString('utf8', start, headEnd));
|
|
230
|
+
}
|
|
231
|
+
if (!skipping && nl > start) {
|
|
232
|
+
if (reader.line(view.toString('utf8', start, nl)) === false)
|
|
233
|
+
stopped = true;
|
|
234
|
+
}
|
|
235
|
+
resetLine();
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
addSegment(view, start, nl);
|
|
239
|
+
finishLine();
|
|
240
|
+
}
|
|
241
|
+
if (stopped)
|
|
242
|
+
return true;
|
|
243
|
+
start = nl + 1;
|
|
244
|
+
}
|
|
245
|
+
if (start < bytesRead)
|
|
246
|
+
addSegment(view, start, bytesRead);
|
|
247
|
+
}
|
|
248
|
+
// Trailing line with no final newline.
|
|
249
|
+
if (piecesLen > 0 || headLen > 0)
|
|
250
|
+
finishLine();
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
releaseBuffer(buffer);
|
|
254
|
+
await fh.close();
|
|
255
|
+
}
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
/** `JSON.parse` a line, tolerating a malformed one (pi skips those too). */
|
|
259
|
+
function parseLine(line, maxBytes = Infinity) {
|
|
260
|
+
if (line.length > maxBytes)
|
|
261
|
+
return null;
|
|
262
|
+
try {
|
|
263
|
+
return JSON.parse(line);
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Decode a bounded prefix of the first JSON string value found after `"text":`
|
|
271
|
+
* or `"content":` in a line HEAD. Used only for the listing preview of a
|
|
272
|
+
* message too large to parse, so a pasted-1000-page first message still shows a
|
|
273
|
+
* sensible snippet instead of nothing. Stops at the first unescaped quote or
|
|
274
|
+
* `maxChars` decoded characters, whichever comes first.
|
|
275
|
+
*/
|
|
276
|
+
function probeTextPrefix(head, maxChars = 400) {
|
|
277
|
+
const at = (() => {
|
|
278
|
+
const t = head.indexOf('"text":"');
|
|
279
|
+
if (t !== -1)
|
|
280
|
+
return t + 8;
|
|
281
|
+
const c = head.indexOf('"content":"');
|
|
282
|
+
return c === -1 ? -1 : c + 11;
|
|
283
|
+
})();
|
|
284
|
+
if (at === -1)
|
|
285
|
+
return '';
|
|
286
|
+
let out = '';
|
|
287
|
+
for (let i = at; i < head.length && out.length < maxChars; i++) {
|
|
288
|
+
const ch = head[i];
|
|
289
|
+
if (ch === '"')
|
|
290
|
+
break;
|
|
291
|
+
if (ch !== '\\') {
|
|
292
|
+
out += ch;
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const esc = head[++i];
|
|
296
|
+
if (esc === 'n')
|
|
297
|
+
out += '\n';
|
|
298
|
+
else if (esc === 't')
|
|
299
|
+
out += '\t';
|
|
300
|
+
else if (esc === 'r')
|
|
301
|
+
out += '\r';
|
|
302
|
+
else if (esc === 'u') {
|
|
303
|
+
const code = parseInt(head.slice(i + 1, i + 5), 16);
|
|
304
|
+
if (Number.isFinite(code))
|
|
305
|
+
out += String.fromCharCode(code);
|
|
306
|
+
i += 4;
|
|
307
|
+
}
|
|
308
|
+
else if (esc !== undefined)
|
|
309
|
+
out += esc;
|
|
310
|
+
}
|
|
311
|
+
return out;
|
|
312
|
+
}
|
|
313
|
+
// ---------------------------------------------------------------------------
|
|
314
|
+
// Cheap structural probes
|
|
315
|
+
// ---------------------------------------------------------------------------
|
|
316
|
+
//
|
|
317
|
+
// pi writes each entry with `type` FIRST and, for a message entry, the fixed
|
|
318
|
+
// header keys (`id`, `parentId`, `timestamp`) before the `message` object:
|
|
319
|
+
//
|
|
320
|
+
// {"type":"message","id":"..","parentId":"..","timestamp":"..","message":{"role":"..
|
|
321
|
+
//
|
|
322
|
+
// So the FIRST occurrence of these patterns in a line is always the structural
|
|
323
|
+
// one (everything before it is fixed, simple values), even if the payload text
|
|
324
|
+
// happens to contain the same bytes later. That lets us classify a line, and
|
|
325
|
+
// skip parsing it, without trusting user content. Probes only ever look at a
|
|
326
|
+
// bounded HEAD of the line, so a 30 MB tool result costs a slice, not a parse.
|
|
327
|
+
const TYPE_PROBE = /^\{"type":"([a-zA-Z_]+)"/;
|
|
328
|
+
const TIMESTAMP_PROBE = /"timestamp":"([^"]{1,64})"/;
|
|
329
|
+
const ROLE_PROBE = /"message":\{"role":"([a-zA-Z]+)"/;
|
|
330
|
+
function lineType(head) {
|
|
331
|
+
const m = TYPE_PROBE.exec(head);
|
|
332
|
+
return m ? m[1] : null;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Role of a `message` entry, from the structural probe. Returns null when the
|
|
336
|
+
* head does not contain the `message.role` pair (unexpected shape), in which
|
|
337
|
+
* case the caller must fall back to a real parse.
|
|
338
|
+
*/
|
|
339
|
+
function messageRole(head) {
|
|
340
|
+
const m = ROLE_PROBE.exec(head);
|
|
341
|
+
return m ? m[1] : null;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Read ONLY the header (first line) of a session `.jsonl`. Costs one 8 KB read
|
|
345
|
+
* regardless of file size. Returns null when the file is missing, empty, or
|
|
346
|
+
* does not start with a `session` header.
|
|
347
|
+
*/
|
|
348
|
+
export async function readSessionHeader(filePath) {
|
|
349
|
+
try {
|
|
350
|
+
const fh = await fs.promises.open(filePath, 'r');
|
|
351
|
+
try {
|
|
352
|
+
const buf = Buffer.alloc(8192);
|
|
353
|
+
const { bytesRead } = await fh.read(buf, 0, buf.length, 0);
|
|
354
|
+
const text = buf.toString('utf8', 0, bytesRead);
|
|
355
|
+
const nl = text.indexOf('\n');
|
|
356
|
+
// No newline in the first 8 KB means the first line is not a header
|
|
357
|
+
// (a session header is a few hundred bytes).
|
|
358
|
+
if (nl === -1 && bytesRead === buf.length)
|
|
359
|
+
return null;
|
|
360
|
+
const firstLine = nl === -1 ? text : text.slice(0, nl);
|
|
361
|
+
const header = JSON.parse(firstLine);
|
|
362
|
+
if (header?.type !== 'session')
|
|
363
|
+
return null;
|
|
364
|
+
return {
|
|
365
|
+
id: typeof header.id === 'string' ? header.id : '',
|
|
366
|
+
cwd: typeof header.cwd === 'string' ? header.cwd : '',
|
|
367
|
+
timestamp: typeof header.timestamp === 'string' ? header.timestamp : undefined,
|
|
368
|
+
parentSession: typeof header.parentSession === 'string' && header.parentSession
|
|
369
|
+
? header.parentSession
|
|
370
|
+
: undefined,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
finally {
|
|
374
|
+
await fh.close();
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
catch {
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Extract the text a listing preview would show from a message payload, using
|
|
383
|
+
* the same rules as the old whole-file parser: a string content, or the
|
|
384
|
+
* concatenation of its parts' `text` fields.
|
|
385
|
+
*/
|
|
386
|
+
function listingText(message) {
|
|
387
|
+
const c = message?.content;
|
|
388
|
+
if (typeof c === 'string')
|
|
389
|
+
return c;
|
|
390
|
+
if (Array.isArray(c)) {
|
|
391
|
+
return c.map((p) => (typeof p?.text === 'string' ? p.text : '')).join('');
|
|
392
|
+
}
|
|
393
|
+
return '';
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Build the listing record for ONE session file in a single streaming pass.
|
|
397
|
+
*
|
|
398
|
+
* Replaces the old `parseDiskSessionInfo(content)`, which needed the whole file
|
|
399
|
+
* as a string AND every line parsed into a retained array. Semantics are
|
|
400
|
+
* preserved exactly, including the subtle ones:
|
|
401
|
+
* - `messageCount` counts EVERY `message` entry (any role);
|
|
402
|
+
* - `modified` is the newest timestamp among user/assistant messages that
|
|
403
|
+
* actually carry text (never a tool result), falling back to the file mtime;
|
|
404
|
+
* - `firstMessage` is the first USER message's text, capped to a preview;
|
|
405
|
+
* - `name` is the LAST `session_info` name seen.
|
|
406
|
+
*
|
|
407
|
+
* Lines that cannot affect any of those (tool results, thinking-level changes,
|
|
408
|
+
* ...) are classified by a bounded probe and never parsed, which is what keeps a
|
|
409
|
+
* 62 MB transcript to a few MB of transient allocation.
|
|
410
|
+
*/
|
|
411
|
+
export async function readSessionListingInfo(filePath, mtime) {
|
|
412
|
+
let header = null;
|
|
413
|
+
let name;
|
|
414
|
+
let messageCount = 0;
|
|
415
|
+
let firstMessage = '';
|
|
416
|
+
let lastMessageTime = 0;
|
|
417
|
+
let sawHeaderLine = false;
|
|
418
|
+
/** Head of the line currently being handed to `line()` (set by `skip`). */
|
|
419
|
+
let currentHead = '';
|
|
420
|
+
/** Whether `skip` already counted the current line as a message. */
|
|
421
|
+
let countedCurrent = false;
|
|
422
|
+
const ok = await forEachJsonlLine(filePath, {
|
|
423
|
+
skip: (head) => {
|
|
424
|
+
currentHead = head;
|
|
425
|
+
countedCurrent = false;
|
|
426
|
+
if (!head.trim())
|
|
427
|
+
return true;
|
|
428
|
+
if (!sawHeaderLine)
|
|
429
|
+
return false; // the header is always read
|
|
430
|
+
const type = lineType(head);
|
|
431
|
+
// Unclassifiable head (a shape this probe does not know: a different key
|
|
432
|
+
// order from another pi version, a truncated line): NEVER skip on a guess.
|
|
433
|
+
// Read it and let the parse decide, which is the old whole-file behaviour.
|
|
434
|
+
if (type === null)
|
|
435
|
+
return false;
|
|
436
|
+
if (type === 'session_info')
|
|
437
|
+
return false;
|
|
438
|
+
if (type !== 'message')
|
|
439
|
+
return true; // model changes etc: nothing to learn
|
|
440
|
+
// From here the line IS a message, so it counts -- whether or not we go on
|
|
441
|
+
// to read it. Counting here is what lets the body be discarded. NOTE: this
|
|
442
|
+
// counts a TRUNCATED trailing message entry (a session killed mid-write)
|
|
443
|
+
// that the old whole-file parser dropped because `JSON.parse` threw. The
|
|
444
|
+
// message is really there and only its tail is missing, so counting it is
|
|
445
|
+
// if anything more accurate; it moves one number in the sidebar, for
|
|
446
|
+
// corrupt files only.
|
|
447
|
+
messageCount++;
|
|
448
|
+
countedCurrent = true;
|
|
449
|
+
// Only user/assistant messages carrying text can move `modified` or supply
|
|
450
|
+
// the first-message preview. Every other role (toolResult, bashExecution)
|
|
451
|
+
// is pure weight for the listing, and tool results (file reads, bash
|
|
452
|
+
// output, base64 images) are most of a transcript's bytes.
|
|
453
|
+
const role = messageRole(head);
|
|
454
|
+
if (role !== null && role !== 'user' && role !== 'assistant')
|
|
455
|
+
return true;
|
|
456
|
+
// User/assistant messages ARE parsed: "does it carry text" cannot be
|
|
457
|
+
// answered from a bounded head (a thinking block can push the text block
|
|
458
|
+
// past it), and `modified` -- which orders the whole sidebar -- depends on
|
|
459
|
+
// that answer. They are also the small ones; the bytes are in what we just
|
|
460
|
+
// skipped.
|
|
461
|
+
return false;
|
|
462
|
+
},
|
|
463
|
+
line: (line) => {
|
|
464
|
+
const head = currentHead;
|
|
465
|
+
if (!sawHeaderLine) {
|
|
466
|
+
sawHeaderLine = true;
|
|
467
|
+
// The header is the first parseable entry and must be a `session` entry;
|
|
468
|
+
// anything else means this is not a pi session file. Parsed WITHOUT the
|
|
469
|
+
// listing cap: a header is a few hundred bytes, and capping it could only
|
|
470
|
+
// ever turn a readable session into an invisible one.
|
|
471
|
+
header = parseLine(line);
|
|
472
|
+
if (header?.type !== 'session') {
|
|
473
|
+
header = null;
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
// Dispatch on the PARSED type, never on the probe: the probe is an
|
|
479
|
+
// optimization for deciding what to read, the parse is the truth.
|
|
480
|
+
const entry = parseLine(line, MAX_LISTING_PARSE_BYTES);
|
|
481
|
+
if (entry?.type === 'session_info') {
|
|
482
|
+
name = (entry.name && String(entry.name).trim()) || undefined;
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (!entry) {
|
|
486
|
+
// Too large (or malformed) to parse: keep what the probes can tell us
|
|
487
|
+
// rather than losing the session's preview and modified time entirely.
|
|
488
|
+
if (messageRole(head) === 'user' && !firstMessage) {
|
|
489
|
+
const probed = probeTextPrefix(head);
|
|
490
|
+
if (probed)
|
|
491
|
+
firstMessage = previewText(probed);
|
|
492
|
+
}
|
|
493
|
+
const tm = TIMESTAMP_PROBE.exec(head);
|
|
494
|
+
const probedTime = tm ? new Date(tm[1]).getTime() : NaN;
|
|
495
|
+
if (Number.isFinite(probedTime))
|
|
496
|
+
lastMessageTime = Math.max(lastMessageTime, probedTime);
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
if (entry.type !== 'message')
|
|
500
|
+
return;
|
|
501
|
+
// A message the probe could not classify (unknown key order) was not
|
|
502
|
+
// counted in `skip`; count it here so the total never depends on the
|
|
503
|
+
// probe recognising the shape.
|
|
504
|
+
if (!countedCurrent)
|
|
505
|
+
messageCount++;
|
|
506
|
+
const message = entry.message;
|
|
507
|
+
const role = message?.role;
|
|
508
|
+
if (role !== 'user' && role !== 'assistant')
|
|
509
|
+
return;
|
|
510
|
+
const textContent = listingText(message);
|
|
511
|
+
if (!textContent)
|
|
512
|
+
return;
|
|
513
|
+
if (!firstMessage && role === 'user')
|
|
514
|
+
firstMessage = previewText(textContent);
|
|
515
|
+
const t = typeof entry.timestamp === 'string' ? new Date(entry.timestamp).getTime() : NaN;
|
|
516
|
+
if (Number.isFinite(t))
|
|
517
|
+
lastMessageTime = Math.max(lastMessageTime, t);
|
|
518
|
+
},
|
|
519
|
+
});
|
|
520
|
+
if (!ok || !header)
|
|
521
|
+
return null;
|
|
522
|
+
const headerTime = typeof header.timestamp === 'string' ? new Date(header.timestamp).getTime() : NaN;
|
|
523
|
+
const created = Number.isFinite(headerTime) ? new Date(headerTime) : new Date(NaN);
|
|
524
|
+
const modified = lastMessageTime > 0 ? new Date(lastMessageTime) : mtime;
|
|
525
|
+
const parentSessionPath = typeof header.parentSession === 'string' && header.parentSession ? header.parentSession : undefined;
|
|
526
|
+
return {
|
|
527
|
+
path: filePath,
|
|
528
|
+
id: typeof header.id === 'string' ? header.id : '',
|
|
529
|
+
cwd: typeof header.cwd === 'string' ? header.cwd : '',
|
|
530
|
+
name,
|
|
531
|
+
created,
|
|
532
|
+
modified,
|
|
533
|
+
messageCount,
|
|
534
|
+
// Store the CAPPED preview, never the raw first message: the cache holds one
|
|
535
|
+
// entry per session on disk, so keeping full first messages (pasted PRDs,
|
|
536
|
+
// specs) resident would cost megabytes of heap for no benefit.
|
|
537
|
+
firstMessage: firstMessage || '(no messages)',
|
|
538
|
+
...(parentSessionPath ? { parentSessionPath } : {}),
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
// ---------------------------------------------------------------------------
|
|
542
|
+
// History (what a client actually reads)
|
|
543
|
+
// ---------------------------------------------------------------------------
|
|
544
|
+
/** Text of a user/assistant message, as the UI and the fork prefill use it. */
|
|
545
|
+
export function extractMessageText(msg) {
|
|
546
|
+
if (msg?.role === 'user' || msg?.role === 'assistant') {
|
|
547
|
+
if (typeof msg.content === 'string')
|
|
548
|
+
return msg.content;
|
|
549
|
+
if (Array.isArray(msg.content)) {
|
|
550
|
+
return msg.content
|
|
551
|
+
.filter((c) => c.type === 'text')
|
|
552
|
+
.map((c) => c.text)
|
|
553
|
+
.join('\n');
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return '';
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Map ONE session entry to the UI-facing history messages it produces (0..N).
|
|
560
|
+
* Pure, so the streaming reader and any caller with entries already in memory
|
|
561
|
+
* produce byte-identical history.
|
|
562
|
+
*/
|
|
563
|
+
export function mapEntryToHistory(entry) {
|
|
564
|
+
const out = [];
|
|
565
|
+
if (entry.type !== 'message')
|
|
566
|
+
return out;
|
|
567
|
+
const msgEntry = entry;
|
|
568
|
+
const msg = msgEntry.message;
|
|
569
|
+
const ts = Date.parse(msgEntry.timestamp);
|
|
570
|
+
if (msg.role === 'user') {
|
|
571
|
+
const content = extractMessageText(msg);
|
|
572
|
+
if (content) {
|
|
573
|
+
// Carry the source entry id so the client can "Fork from here"
|
|
574
|
+
// (fork BEFORE this user entry, pi's default position:'before').
|
|
575
|
+
const entryId = typeof msgEntry.id === 'string' ? msgEntry.id : undefined;
|
|
576
|
+
out.push({ role: 'user', content, timestamp: ts, ...(entryId ? { entryId } : {}) });
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
else if (msg.role === 'assistant') {
|
|
580
|
+
const content = msg.content;
|
|
581
|
+
if (Array.isArray(content)) {
|
|
582
|
+
for (const block of content) {
|
|
583
|
+
if (block.type === 'thinking') {
|
|
584
|
+
const thinking = block.thinking || '';
|
|
585
|
+
if (thinking)
|
|
586
|
+
out.push({ role: 'thinking', content: thinking, timestamp: ts });
|
|
587
|
+
}
|
|
588
|
+
else if (block.type === 'text') {
|
|
589
|
+
const text = block.text || '';
|
|
590
|
+
if (text)
|
|
591
|
+
out.push({ role: 'assistant', content: text, timestamp: ts });
|
|
592
|
+
}
|
|
593
|
+
else if (block.type === 'toolCall') {
|
|
594
|
+
const tc = block;
|
|
595
|
+
const toolName = tc.name || tc.toolName || 'unknown';
|
|
596
|
+
const rawArgs = tc.arguments || tc.args;
|
|
597
|
+
const args = rawArgs ? JSON.stringify(rawArgs) : '';
|
|
598
|
+
const toolCallId = typeof tc.id === 'string' ? tc.id : undefined;
|
|
599
|
+
out.push({
|
|
600
|
+
role: 'tool_call',
|
|
601
|
+
content: args,
|
|
602
|
+
timestamp: ts,
|
|
603
|
+
toolName,
|
|
604
|
+
...(toolCallId ? { toolCallId } : {}),
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
else if (typeof content === 'string' && content) {
|
|
610
|
+
out.push({ role: 'assistant', content, timestamp: ts });
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
else if (msg.role === 'toolResult') {
|
|
614
|
+
const resultMsg = msg;
|
|
615
|
+
const toolName = resultMsg.toolName || 'unknown';
|
|
616
|
+
let resultText = '';
|
|
617
|
+
const resultImages = [];
|
|
618
|
+
if (Array.isArray(resultMsg.content)) {
|
|
619
|
+
resultText = resultMsg.content
|
|
620
|
+
.filter((c) => c.type === 'text')
|
|
621
|
+
.map((c) => c.text || '')
|
|
622
|
+
.join('\n');
|
|
623
|
+
// Pull image blocks (e.g. `read` on an image file) so reloaded history
|
|
624
|
+
// renders them inline, mirroring live tool_end.
|
|
625
|
+
for (const c of resultMsg.content) {
|
|
626
|
+
if (c && c.type === 'image' && typeof c.data === 'string' && c.data) {
|
|
627
|
+
resultImages.push({
|
|
628
|
+
mimeType: typeof c.mimeType === 'string' ? c.mimeType : 'image/png',
|
|
629
|
+
data: c.data,
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
else if (typeof resultMsg.content === 'string') {
|
|
635
|
+
resultText = resultMsg.content;
|
|
636
|
+
}
|
|
637
|
+
const toolCallId = typeof resultMsg.toolCallId === 'string' ? resultMsg.toolCallId : undefined;
|
|
638
|
+
out.push({
|
|
639
|
+
role: 'tool_result',
|
|
640
|
+
content: resultText,
|
|
641
|
+
timestamp: ts,
|
|
642
|
+
toolName,
|
|
643
|
+
isError: !!resultMsg.isError,
|
|
644
|
+
...(toolCallId ? { toolCallId } : {}),
|
|
645
|
+
...(resultImages.length > 0 ? { images: resultImages } : {}),
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
else if (msg.role === 'bashExecution') {
|
|
649
|
+
const bashMsg = msg;
|
|
650
|
+
out.push({
|
|
651
|
+
role: 'tool_call',
|
|
652
|
+
content: bashMsg.command || '',
|
|
653
|
+
timestamp: ts,
|
|
654
|
+
toolName: 'bash',
|
|
655
|
+
// A bashExecution entry is, by definition, a user `!command` (force
|
|
656
|
+
// command), never an agent tool call. Mark it so the web can auto-expand
|
|
657
|
+
// it on reload, mirroring the live tool_start flag.
|
|
658
|
+
forceCommand: true,
|
|
659
|
+
});
|
|
660
|
+
if (bashMsg.output) {
|
|
661
|
+
out.push({
|
|
662
|
+
role: 'tool_result',
|
|
663
|
+
content: bashMsg.output,
|
|
664
|
+
timestamp: ts,
|
|
665
|
+
toolName: 'bash',
|
|
666
|
+
isError: bashMsg.exitCode !== undefined && bashMsg.exitCode !== 0,
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return out;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* One streaming pass over a transcript, materializing ONLY the history messages
|
|
674
|
+
* whose index falls in [`keepFrom`, `keepUntil`).
|
|
675
|
+
*
|
|
676
|
+
* BOTH bounds matter. `keepFrom` is what lets a `toolResult` behind the window
|
|
677
|
+
* be COUNTED from its head and its (often multi-megabyte) body discarded without
|
|
678
|
+
* ever being assembled or parsed -- it always maps to exactly one history
|
|
679
|
+
* message, so counting it needs nothing else. `keepUntil` stops the read as soon
|
|
680
|
+
* as the window is full: without it a "load older" page (`beforeOffset` near the
|
|
681
|
+
* START of a big transcript) would materialize everything from there to EOF and
|
|
682
|
+
* throw most of it away, which is the very failure this module exists to remove.
|
|
683
|
+
* Measured on a 60 MB transcript, a `beforeOffset=5` page went from an 87 MB
|
|
684
|
+
* transient peak to the same ~16 MB as a tail read.
|
|
685
|
+
*
|
|
686
|
+
* Every entry that is not a behind-the-window tool result is read: how many
|
|
687
|
+
* messages it maps to depends on its content.
|
|
688
|
+
*/
|
|
689
|
+
async function scanTranscript(filePath, keepFrom, keepUntil = Number.POSITIVE_INFINITY) {
|
|
690
|
+
let header = null;
|
|
691
|
+
let model = '';
|
|
692
|
+
let total = 0;
|
|
693
|
+
const messages = [];
|
|
694
|
+
let sawHeaderLine = false;
|
|
695
|
+
await forEachJsonlLine(filePath, {
|
|
696
|
+
skip: (head) => {
|
|
697
|
+
if (!head.trim())
|
|
698
|
+
return true;
|
|
699
|
+
if (!sawHeaderLine)
|
|
700
|
+
return false; // the header is always read
|
|
701
|
+
const type = lineType(head);
|
|
702
|
+
// Unclassifiable head: read it rather than guess. Skipping on a guess here
|
|
703
|
+
// would silently drop a message from the history a client is reading.
|
|
704
|
+
if (type === null)
|
|
705
|
+
return false;
|
|
706
|
+
if (type === 'model_change')
|
|
707
|
+
return false;
|
|
708
|
+
if (type !== 'message')
|
|
709
|
+
return true; // contributes no history message
|
|
710
|
+
// Exactly one message, and it is behind the window: count and drop it.
|
|
711
|
+
if (messageRole(head) === 'toolResult' && total < keepFrom) {
|
|
712
|
+
total++;
|
|
713
|
+
return true;
|
|
714
|
+
}
|
|
715
|
+
return false;
|
|
716
|
+
},
|
|
717
|
+
line: (line) => {
|
|
718
|
+
if (!sawHeaderLine) {
|
|
719
|
+
sawHeaderLine = true;
|
|
720
|
+
const parsed = parseLine(line);
|
|
721
|
+
if (parsed?.type !== 'session')
|
|
722
|
+
return false;
|
|
723
|
+
header = {
|
|
724
|
+
id: typeof parsed.id === 'string' ? parsed.id : '',
|
|
725
|
+
cwd: typeof parsed.cwd === 'string' ? parsed.cwd : '',
|
|
726
|
+
timestamp: typeof parsed.timestamp === 'string' ? parsed.timestamp : undefined,
|
|
727
|
+
parentSession: typeof parsed.parentSession === 'string' && parsed.parentSession
|
|
728
|
+
? parsed.parentSession
|
|
729
|
+
: undefined,
|
|
730
|
+
};
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
// Parsed WITHOUT a size cap: unlike the listing, here the content IS the
|
|
734
|
+
// answer, and refusing a big line would silently drop a message from the
|
|
735
|
+
// transcript the client is reading. Memory stays bounded because the parse
|
|
736
|
+
// is transient -- only messages at/after `keepFrom` survive the loop.
|
|
737
|
+
const entry = parseLine(line);
|
|
738
|
+
if (!entry)
|
|
739
|
+
return;
|
|
740
|
+
if (entry.type === 'model_change') {
|
|
741
|
+
if ('provider' in entry && 'modelId' in entry) {
|
|
742
|
+
model = `${entry.provider}:${entry.modelId}`;
|
|
743
|
+
}
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
if (entry.type !== 'message')
|
|
747
|
+
return;
|
|
748
|
+
for (const m of mapEntryToHistory(entry)) {
|
|
749
|
+
if (total >= keepFrom && total < keepUntil)
|
|
750
|
+
messages.push(m);
|
|
751
|
+
total++;
|
|
752
|
+
}
|
|
753
|
+
// Window full: stop reading. The caller that set `keepUntil` already knows
|
|
754
|
+
// the total (it comes from the counting pass), so there is nothing left to
|
|
755
|
+
// learn from the rest of the file.
|
|
756
|
+
if (total >= keepUntil)
|
|
757
|
+
return false;
|
|
758
|
+
},
|
|
759
|
+
});
|
|
760
|
+
return { header, model, total, messages };
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Read a session transcript and return ONE window of history.
|
|
764
|
+
*
|
|
765
|
+
* Windowing matches the old in-memory `all.slice()` exactly:
|
|
766
|
+
* - no `beforeOffset`: the LAST `limit` messages, `offset = totalCount - limit`;
|
|
767
|
+
* - with `beforeOffset`: the `limit` messages ending just before it, clamped to
|
|
768
|
+
* the real total.
|
|
769
|
+
*
|
|
770
|
+
* Done as TWO bounded passes rather than one. Pass 1 reads the WHOLE file and
|
|
771
|
+
* materializes nothing (every tool result is behind the window by construction),
|
|
772
|
+
* which is what makes `header`, `model` -- the LAST `model_change`, so only a
|
|
773
|
+
* full pass can know it -- and `totalCount` authoritative. Pass 2 then reads only
|
|
774
|
+
* as far as the window and materializes only the window itself. Both read from
|
|
775
|
+
* the page cache and neither ever holds more than the window, which is what a
|
|
776
|
+
* "give me 60 messages" request is allowed to cost on a 60 MB transcript. The
|
|
777
|
+
* old path built a `HistoryMessage` for EVERY entry -- base64 tool images
|
|
778
|
+
* included -- and threw all but 60 away, three whole-file loads deep (two of
|
|
779
|
+
* them inside `SessionManager.open`).
|
|
780
|
+
*
|
|
781
|
+
* Everything except `messages` therefore comes from pass 1, deliberately: pass 2
|
|
782
|
+
* stops early, so its own header/model/total would be partial. An append landing
|
|
783
|
+
* between the passes cannot corrupt the answer either -- entries before the
|
|
784
|
+
* append keep their indices, so the window pass 2 reads is exactly the window
|
|
785
|
+
* pass 1 measured, and the reported total/offset describe that same snapshot.
|
|
786
|
+
*/
|
|
787
|
+
export async function readTranscriptWindow(filePath, limit, beforeOffset) {
|
|
788
|
+
// Pass 1: count only.
|
|
789
|
+
const counted = await scanTranscript(filePath, Number.POSITIVE_INFINITY);
|
|
790
|
+
const totalCount = counted.total;
|
|
791
|
+
if (limit <= 0 || totalCount === 0) {
|
|
792
|
+
const end = beforeOffset ?? totalCount;
|
|
793
|
+
return {
|
|
794
|
+
header: counted.header,
|
|
795
|
+
model: counted.model,
|
|
796
|
+
messages: [],
|
|
797
|
+
totalCount,
|
|
798
|
+
offset: Math.max(0, Math.min(end, totalCount)),
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
const end = beforeOffset === undefined ? totalCount : Math.max(0, Math.min(beforeOffset, totalCount));
|
|
802
|
+
const start = Math.max(0, end - limit);
|
|
803
|
+
// Pass 2: materialize the window, and nothing outside it.
|
|
804
|
+
const read = await scanTranscript(filePath, start, end);
|
|
805
|
+
return {
|
|
806
|
+
header: counted.header,
|
|
807
|
+
model: counted.model,
|
|
808
|
+
messages: read.messages,
|
|
809
|
+
totalCount,
|
|
810
|
+
offset: start,
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
//# sourceMappingURL=session-transcript.js.map
|