pi-fast-resume 1.1.2 → 1.2.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 +6 -6
- package/fast-resume.ts +7 -6
- package/package.json +9 -9
- package/src/scanner.ts +302 -76
- package/src/search.ts +5 -4
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
**Instant session picker for [pi](https://github.com/earendil-works/pi-coding-agent)**
|
|
6
6
|
|
|
7
|
-
_Reads
|
|
7
|
+
_Reads just enough of each file to show the title row — first results in **6ms**._
|
|
8
8
|
|
|
9
9
|
[](https://github.com/earendil-works/pi-coding-agent)
|
|
10
10
|
[](./LICENSE)
|
|
@@ -16,7 +16,7 @@ _Reads 16KB per file instead of the full JSONL — first results in **6ms**._
|
|
|
16
16
|
> **`/resume` takes 5.6 seconds** when you have 1,700+ sessions.
|
|
17
17
|
> pi-fast-resume's `/fast-resume` takes **6 milliseconds**.
|
|
18
18
|
|
|
19
|
-
Same picker UI and keybindings as `/resume`. The difference is pi-fast-resume never
|
|
19
|
+
Same picker UI and keybindings as `/resume`. The difference is pi-fast-resume never parses the full JSONL. For each file it streams complete lines forward just until the first user message (the title), then reads a bounded tail near EOF to recover the latest session name (which pi appends on `/rename`). Everything between is full message history the picker never shows. Search matches against the first message only (see [Known Limitations](#known-limitations)).
|
|
20
20
|
|
|
21
21
|
```
|
|
22
22
|
──────────────────────────────────────────────────────────
|
|
@@ -54,7 +54,7 @@ Tested with **1,771 sessions, 1.46 GB** of JSONL data on disk.
|
|
|
54
54
|
| ------------------------------------------- | --------- | ------------------------------------------ |
|
|
55
55
|
| `SessionManager.listAll()` (current) | ~5,600 ms | Full parse of every file |
|
|
56
56
|
| DuckDB `read_ndjson` full query | ~2,560 ms | Still reads all 1.46 GB, but multithreaded |
|
|
57
|
-
| Node.js partial read (
|
|
57
|
+
| Node.js streaming partial read (this extension) | ~370 ms | All ~2,550 sessions; stops at first user message + bounded tail |
|
|
58
58
|
| DuckDB persistent index (query all) | ~49 ms | After one-time build |
|
|
59
59
|
| `node:sqlite` persistent index (query) | ~52 ms | Zero external deps |
|
|
60
60
|
| **pi-fast-resume, first 30 sessions** | **~6 ms** | **Streaming display** |
|
|
@@ -165,7 +165,7 @@ Press `Tab` to switch to **all sessions** — shows every session pi knows about
|
|
|
165
165
|
## How it works
|
|
166
166
|
|
|
167
167
|
```
|
|
168
|
-
stat() all .jsonl files ──────► sort by mtime ──────►
|
|
168
|
+
stat() all .jsonl files ──────► sort by mtime ──────► stream top 30 forward
|
|
169
169
|
(~100ms) (recent first) (~6ms)
|
|
170
170
|
│
|
|
171
171
|
▼
|
|
@@ -180,7 +180,7 @@ stat() all .jsonl files ──────► sort by mtime ──────
|
|
|
180
180
|
|
|
181
181
|
1. **`stat()` all session files** — collect paths and mtimes (~100 ms for 1,700 files)
|
|
182
182
|
2. **Sort by mtime descending** — most recent sessions first
|
|
183
|
-
3. **
|
|
183
|
+
3. **Stream each file forward** line by line until the first user message, then read a bounded tail at EOF for the latest rename name (~6 ms)
|
|
184
184
|
4. **Show picker** — user can navigate, filter, and select immediately
|
|
185
185
|
5. **Background load** — remaining sessions stream in batches of 50, non-blocking
|
|
186
186
|
6. **Tab to switch scope** — filter to current project or show everything
|
|
@@ -248,7 +248,7 @@ None optimize the `/resume` picker itself — they either still fully parse ever
|
|
|
248
248
|
|
|
249
249
|
## Known Limitations
|
|
250
250
|
|
|
251
|
-
The
|
|
251
|
+
The partial-read tradeoff that gives pi-fast-resume its speed comes with functional gaps vs. the built-in `/resume`. The forward stream reads exactly as many bytes as the first user message needs (no fixed window), so oversized first messages — `<skill>` injections, long pastes, base64 images — are parsed correctly. A rename is recovered only if it lives within a bounded tail near EOF (32 KB by default); a rename buried under more continued activity than that falls back to `firstMessage`.
|
|
252
252
|
|
|
253
253
|
| Area | Built-in `/resume` | pi-fast-resume | Impact |
|
|
254
254
|
| ---- | ------------------ | -------------- | ------ |
|
package/fast-resume.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* pi-fast-resume — Fast session picker for pi
|
|
3
3
|
*
|
|
4
|
-
* Reads only
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Reads only enough of each session file to render the title row — streaming
|
|
5
|
+
* forward line by line until the first user message, plus a bounded tail near
|
|
6
|
+
* EOF for the latest rename name — instead of parsing the entire JSONL. Shows
|
|
7
|
+
* results instantly with incremental background loading.
|
|
7
8
|
*
|
|
8
9
|
* Mirrors the exact TUI layout and keybindings of pi's built-in /resume.
|
|
9
10
|
*
|
|
@@ -36,10 +37,10 @@
|
|
|
36
37
|
* exact phrase "node cve" case-insensitive substring
|
|
37
38
|
* regex re:<pattern> RegExp search (case-insensitive)
|
|
38
39
|
*
|
|
39
|
-
* Note on search depth: pi-fast-resume
|
|
40
|
-
*
|
|
40
|
+
* Note on search depth: pi-fast-resume stops reading each file at the first
|
|
41
|
+
* user message, so search matches against id + name + firstMessage + cwd.
|
|
41
42
|
* Upstream /resume matches against all messages (allMessagesText). This
|
|
42
|
-
* tradeoff is by design — the
|
|
43
|
+
* tradeoff is by design — the fast load time depends on partial reads.
|
|
43
44
|
*/
|
|
44
45
|
|
|
45
46
|
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-fast-resume",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Fast session picker for pi — reads headers + first messages from 16KB partial reads instead of full-file parsing",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Tom X Nguyen",
|
|
@@ -29,13 +29,6 @@
|
|
|
29
29
|
"src/",
|
|
30
30
|
"README.md"
|
|
31
31
|
],
|
|
32
|
-
"scripts": {
|
|
33
|
-
"test": "vitest run",
|
|
34
|
-
"test:watch": "vitest",
|
|
35
|
-
"test:coverage": "vitest run --coverage",
|
|
36
|
-
"typecheck": "tsc --noEmit",
|
|
37
|
-
"lint:dead": "knip --no-gitignore"
|
|
38
|
-
},
|
|
39
32
|
"devDependencies": {
|
|
40
33
|
"@earendil-works/pi-coding-agent": "0.79.4",
|
|
41
34
|
"@earendil-works/pi-tui": "0.79.4",
|
|
@@ -52,5 +45,12 @@
|
|
|
52
45
|
},
|
|
53
46
|
"overrides": {
|
|
54
47
|
"brace-expansion": "5.0.6"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:watch": "vitest",
|
|
52
|
+
"test:coverage": "vitest run --coverage",
|
|
53
|
+
"typecheck": "tsc --noEmit",
|
|
54
|
+
"lint:dead": "knip --no-gitignore"
|
|
55
55
|
}
|
|
56
|
-
}
|
|
56
|
+
}
|
package/src/scanner.ts
CHANGED
|
@@ -21,7 +21,75 @@ export interface SessionFileMeta {
|
|
|
21
21
|
size: number;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
// I/O granularity for the streaming forward reader. This is pure performance
|
|
25
|
+
// tuning — it does NOT affect correctness. The reader assembles complete lines
|
|
26
|
+
// across chunks (a line is never truncated mid-JSON), so a line of any size is
|
|
27
|
+
// parsed correctly regardless of this value. It only controls how many bytes
|
|
28
|
+
// each readSync call fetches; smaller = more syscalls for big reads, larger =
|
|
29
|
+
// over-read for tiny sessions that stop early. 16KB is a reasonable middle.
|
|
30
|
+
const READ_CHUNK_SIZE = 16_384;
|
|
31
|
+
|
|
32
|
+
// Tail read for recovering the latest session_info (the rename name) near EOF.
|
|
33
|
+
//
|
|
34
|
+
// Why a tail read exists at all: pi appends session_info as a new line on every
|
|
35
|
+
// /rename (appendSessionInfo → appendFileSync). The forward pass stops at the
|
|
36
|
+
// first user message, which can be very early in a large file — so the latest
|
|
37
|
+
// rename often lives past the forward pass's stop point and must be recovered
|
|
38
|
+
// from the end of the file.
|
|
39
|
+
//
|
|
40
|
+
// Why the bound is this size and not "scan to EOF": scanning the whole file
|
|
41
|
+
// backward to find a session_info that may not exist (98% of sessions are never
|
|
42
|
+
// renamed) collapses pi-fast-resume's perf to pi-core's (~100× slower). So the
|
|
43
|
+
// tail is bounded. The bound only needs to cover "the rename line itself plus
|
|
44
|
+
// any continued activity written after the rename before reopening."
|
|
45
|
+
//
|
|
46
|
+
// Measurement (across 47 real renamed sessions on this system): the latest
|
|
47
|
+
// session_info was at EOF in 100% of cases — the rename was the last write
|
|
48
|
+
// before the session was reopened, every single time. 32KB therefore covers all
|
|
49
|
+
// observed renames with ~32,000× margin, and also covers a rename followed by
|
|
50
|
+
// up to ~32KB of continued activity (dozens of typical message turns) before
|
|
51
|
+
// reopening. A rename followed by more than this much continued activity is
|
|
52
|
+
// missed and falls back to firstMessage — the documented tradeoff vs a
|
|
53
|
+
// full-file scan.
|
|
54
|
+
const TAIL_READ_SIZE = 32_768;
|
|
55
|
+
|
|
56
|
+
// Defensive guard against a single pathological line with no newline (e.g. a
|
|
57
|
+
// corrupted file). Real pi sessions are newline-terminated JSONL, so this never
|
|
58
|
+
// triggers on valid input. It only caps memory for malformed input.
|
|
59
|
+
const MAX_LINE_BYTES = 256 * 1024 * 1024;
|
|
60
|
+
|
|
61
|
+
// Result of scanning a file tail for session_info entries.
|
|
62
|
+
// found: false → no session_info seen in the tail; fall back to the forward name.
|
|
63
|
+
// found: true → a session_info was seen (later in file order than anything
|
|
64
|
+
// the forward pass saw, since the tail starts past the forward
|
|
65
|
+
// stop); name is undefined if that entry explicitly cleared it.
|
|
66
|
+
export interface TailSessionInfo {
|
|
67
|
+
found: boolean;
|
|
68
|
+
name?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Accumulator state while processing complete session entries line by line.
|
|
72
|
+
// Shared by the pure parseSessionFromBuffer and the streaming loadSessionHeader
|
|
73
|
+
// so the per-entry logic exists in exactly one place.
|
|
74
|
+
interface SessionAccumulator {
|
|
75
|
+
header: { id: string; timestamp: string; cwd?: string; parentSession?: string } | null;
|
|
76
|
+
firstUserMessage: string;
|
|
77
|
+
messageCount: number;
|
|
78
|
+
name: string | undefined;
|
|
79
|
+
lastActivityTime: number | undefined;
|
|
80
|
+
foundFirstUser: boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function newAccumulator(): SessionAccumulator {
|
|
84
|
+
return {
|
|
85
|
+
header: null,
|
|
86
|
+
firstUserMessage: "",
|
|
87
|
+
messageCount: 0,
|
|
88
|
+
name: undefined,
|
|
89
|
+
lastActivityTime: undefined,
|
|
90
|
+
foundFirstUser: false,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
25
93
|
|
|
26
94
|
function extractTextFromContent(
|
|
27
95
|
content: string | Array<{ type: string; text?: string }>,
|
|
@@ -33,101 +101,218 @@ function extractTextFromContent(
|
|
|
33
101
|
.join(" ");
|
|
34
102
|
}
|
|
35
103
|
|
|
104
|
+
// Process one complete entry line. Pure: mutates only `acc`. Malformed JSON (a
|
|
105
|
+
// line truncated at a read boundary, or genuinely corrupt input) is swallowed
|
|
106
|
+
// by the try/catch — callers only ever feed complete lines, so a parse failure
|
|
107
|
+
// here means the line is malformed and skipping it is correct.
|
|
108
|
+
function processEntry(acc: SessionAccumulator, line: string): void {
|
|
109
|
+
const trimmed = line.trim();
|
|
110
|
+
if (!trimmed) return;
|
|
111
|
+
let entry: any;
|
|
112
|
+
try {
|
|
113
|
+
entry = JSON.parse(trimmed);
|
|
114
|
+
} catch {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (typeof entry !== "object" || entry === null) return;
|
|
118
|
+
|
|
119
|
+
if (entry.type === "session") {
|
|
120
|
+
acc.header = entry;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (entry.type === "session_info") {
|
|
124
|
+
// Latest session_info in file order wins, including explicit clears
|
|
125
|
+
// (empty/whitespace name → undefined). Matches pi-core's getSessionName().
|
|
126
|
+
acc.name = entry.name?.trim() || undefined;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (entry.type === "message") {
|
|
130
|
+
acc.messageCount++;
|
|
131
|
+
|
|
132
|
+
// Track last activity time. Matches pi-core's getMessageActivityTime
|
|
133
|
+
// priority: message.timestamp (number) > entry.timestamp (date string).
|
|
134
|
+
const msg = entry.message;
|
|
135
|
+
if (msg?.role === "user" || msg?.role === "assistant") {
|
|
136
|
+
const msgTimestamp = msg.timestamp;
|
|
137
|
+
if (typeof msgTimestamp === "number" && msgTimestamp > 0) {
|
|
138
|
+
acc.lastActivityTime = Math.max(acc.lastActivityTime ?? 0, msgTimestamp);
|
|
139
|
+
} else if (typeof entry.timestamp === "string") {
|
|
140
|
+
const t = Date.parse(entry.timestamp);
|
|
141
|
+
if (!Number.isNaN(t)) {
|
|
142
|
+
acc.lastActivityTime = Math.max(acc.lastActivityTime ?? 0, t);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!acc.foundFirstUser && msg?.role === "user") {
|
|
148
|
+
acc.firstUserMessage = extractTextFromContent(msg.content);
|
|
149
|
+
acc.foundFirstUser = true;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Build the SessionHeader from an accumulator. `reachedEof` is whether the
|
|
155
|
+
// forward pass consumed all input — when false (it stopped early at the first
|
|
156
|
+
// user message), lastActivityTime only reflects entries seen and is unreliable,
|
|
157
|
+
// so stat mtime is used instead (pi updates it on every append, so it tracks
|
|
158
|
+
// the true last write time). `tailInfo`, if present, carries the latest
|
|
159
|
+
// session_info from a tail read and wins over the forward name (later in file
|
|
160
|
+
// order), including explicit name clears.
|
|
161
|
+
function buildHeader(
|
|
162
|
+
acc: SessionAccumulator,
|
|
163
|
+
filePath: string,
|
|
164
|
+
mtimeMs: number,
|
|
165
|
+
reachedEof: boolean,
|
|
166
|
+
tailInfo?: TailSessionInfo,
|
|
167
|
+
): SessionHeader | null {
|
|
168
|
+
const header = acc.header;
|
|
169
|
+
if (!header) return null;
|
|
170
|
+
|
|
171
|
+
const name = tailInfo?.found ? tailInfo.name : acc.name;
|
|
172
|
+
|
|
173
|
+
const headerTime = Date.parse(header.timestamp);
|
|
174
|
+
let modified: Date;
|
|
175
|
+
if (!reachedEof) {
|
|
176
|
+
// Partial read — stat mtime is the only reliable signal.
|
|
177
|
+
modified = new Date(mtimeMs);
|
|
178
|
+
} else if (typeof acc.lastActivityTime === "number" && acc.lastActivityTime > 0) {
|
|
179
|
+
modified = new Date(acc.lastActivityTime);
|
|
180
|
+
} else if (!Number.isNaN(headerTime)) {
|
|
181
|
+
modified = new Date(headerTime);
|
|
182
|
+
} else {
|
|
183
|
+
modified = new Date(mtimeMs);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
path: filePath,
|
|
188
|
+
id: header.id,
|
|
189
|
+
cwd: header.cwd ?? "",
|
|
190
|
+
parentSessionPath: header.parentSession || undefined,
|
|
191
|
+
created: new Date(header.timestamp),
|
|
192
|
+
modified,
|
|
193
|
+
messageCount: acc.messageCount,
|
|
194
|
+
firstMessage: acc.firstUserMessage || "(no messages)",
|
|
195
|
+
name,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Pure parser over a buffer containing complete (or complete-prefix) entry
|
|
200
|
+
// lines. Splits on \n and runs processEntry on each line; the last line may be
|
|
201
|
+
// truncated at the buffer boundary (its JSON.parse fails and it is skipped).
|
|
202
|
+
// `partial` means the buffer does not contain the whole file — when true,
|
|
203
|
+
// modified time falls back to stat mtime (the buffer's lastActivityTime only
|
|
204
|
+
// reflects the prefix). `tailInfo` carries a tail-read latest session_info that
|
|
205
|
+
// overrides the buffer's name.
|
|
206
|
+
//
|
|
207
|
+
// Kept as a pure, synchronous, fd-free function for direct testing and
|
|
208
|
+
// callers that already have the bytes. loadSessionHeader (the production path)
|
|
209
|
+
// uses the streaming reader below so it never truncates a line mid-JSON.
|
|
36
210
|
export function parseSessionFromBuffer(
|
|
37
211
|
buf: Buffer,
|
|
38
212
|
bytesRead: number,
|
|
39
213
|
filePath: string,
|
|
40
214
|
mtimeMs: number,
|
|
41
215
|
partial = false,
|
|
216
|
+
tailInfo?: TailSessionInfo,
|
|
42
217
|
): SessionHeader | null {
|
|
43
218
|
const decoder = new StringDecoder("utf8");
|
|
44
219
|
const text = decoder.write(buf.subarray(0, bytesRead)) + decoder.end();
|
|
45
220
|
const lines = text.split("\n");
|
|
221
|
+
const acc = newAccumulator();
|
|
222
|
+
for (const line of lines) processEntry(acc, line);
|
|
223
|
+
return buildHeader(acc, filePath, mtimeMs, !partial, tailInfo);
|
|
224
|
+
}
|
|
46
225
|
|
|
47
|
-
|
|
48
|
-
|
|
226
|
+
// Scan a tail chunk (read from near EOF) for session_info entries and return
|
|
227
|
+
// the latest name, matching pi-core's getSessionName() semantics: the latest
|
|
228
|
+
// session_info in file order wins, including explicit clears (empty name).
|
|
229
|
+
// The tail's first line may be a partial line cut off at the read-start
|
|
230
|
+
// boundary — it is skipped naturally when JSON.parse fails.
|
|
231
|
+
export function scanTailForSessionInfo(
|
|
232
|
+
buf: Buffer,
|
|
233
|
+
bytesRead: number,
|
|
234
|
+
): TailSessionInfo {
|
|
235
|
+
const decoder = new StringDecoder("utf8");
|
|
236
|
+
const text = decoder.write(buf.subarray(0, bytesRead)) + decoder.end();
|
|
237
|
+
const lines = text.split("\n");
|
|
238
|
+
let found = false;
|
|
49
239
|
let name: string | undefined;
|
|
50
|
-
let msgCount = 0;
|
|
51
|
-
let lastActivityTime: number | undefined;
|
|
52
|
-
|
|
53
240
|
for (const line of lines) {
|
|
54
241
|
const trimmed = line.trim();
|
|
55
242
|
if (!trimmed) continue;
|
|
56
243
|
try {
|
|
57
244
|
const entry = JSON.parse(trimmed);
|
|
58
|
-
if (entry.type === "
|
|
59
|
-
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
if (entry.type === "session_info") {
|
|
245
|
+
if (typeof entry === "object" && entry !== null && entry.type === "session_info") {
|
|
246
|
+
found = true;
|
|
63
247
|
name = entry.name?.trim() || undefined;
|
|
64
248
|
}
|
|
65
|
-
if (entry.type === "message") {
|
|
66
|
-
msgCount++;
|
|
67
|
-
|
|
68
|
-
// Track last activity time from user/assistant messages
|
|
69
|
-
// Matches pi-core's getMessageActivityTime priority:
|
|
70
|
-
// message.timestamp (number) > entry.timestamp (date string)
|
|
71
|
-
const msg = entry.message;
|
|
72
|
-
if (msg?.role === "user" || msg?.role === "assistant") {
|
|
73
|
-
const msgTimestamp = msg.timestamp;
|
|
74
|
-
if (typeof msgTimestamp === "number" && msgTimestamp > 0) {
|
|
75
|
-
lastActivityTime = Math.max(lastActivityTime ?? 0, msgTimestamp);
|
|
76
|
-
} else if (typeof entry.timestamp === "string") {
|
|
77
|
-
const t = new Date(entry.timestamp).getTime();
|
|
78
|
-
if (!Number.isNaN(t)) {
|
|
79
|
-
lastActivityTime = Math.max(lastActivityTime ?? 0, t);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (!firstUserMsg && msg?.role === "user") {
|
|
85
|
-
try {
|
|
86
|
-
firstUserMsg = extractTextFromContent(msg.content);
|
|
87
|
-
} catch {
|
|
88
|
-
// Malformed content, skip
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
249
|
} catch {
|
|
93
|
-
//
|
|
250
|
+
// Partial line at tail boundary — skip
|
|
94
251
|
}
|
|
95
252
|
}
|
|
253
|
+
return { found, name };
|
|
254
|
+
}
|
|
96
255
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
256
|
+
// Read complete lines forward from `fd` starting at offset 0. Calls onLine for
|
|
257
|
+
// each complete (newline-terminated) line. Reading stops when onLine returns
|
|
258
|
+
// false, or at EOF. Returns whether EOF was reached (i.e. the caller did not
|
|
259
|
+
// stop early) and the byte offset just past the last emitted line's newline —
|
|
260
|
+
// the caller uses this as the lower bound for any tail scan so the tail never
|
|
261
|
+
// re-reads already-covered bytes.
|
|
262
|
+
//
|
|
263
|
+
// Chunk-based I/O with a StringDecoder so multi-byte UTF-8 sequences split
|
|
264
|
+
// across chunk boundaries decode correctly. The in-memory line buffer grows to
|
|
265
|
+
// fit the longest single line (bounded by MAX_LINE_BYTES against malformed
|
|
266
|
+
// input); real entries are newline-terminated so this is unbounded only for
|
|
267
|
+
// corrupt files.
|
|
268
|
+
function forEachLineForward(
|
|
269
|
+
fd: number,
|
|
270
|
+
size: number,
|
|
271
|
+
onLine: (line: string) => boolean | void,
|
|
272
|
+
): { reachedEof: boolean; consumedBytes: number } {
|
|
273
|
+
const decoder = new StringDecoder("utf8");
|
|
274
|
+
const chunk = Buffer.alloc(READ_CHUNK_SIZE);
|
|
275
|
+
let lineBuf = "";
|
|
276
|
+
let offset = 0;
|
|
277
|
+
let consumedBytes = 0;
|
|
119
278
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
cwd: header.cwd ?? "",
|
|
124
|
-
parentSessionPath: header.parentSession || undefined,
|
|
125
|
-
created: new Date(header.timestamp),
|
|
126
|
-
modified,
|
|
127
|
-
messageCount: msgCount,
|
|
128
|
-
firstMessage: firstUserMsg || "(no messages)",
|
|
129
|
-
name,
|
|
279
|
+
const flushLine = (line: string): boolean | void => {
|
|
280
|
+
consumedBytes += Buffer.byteLength(line, "utf8") + 1; // +1 for \n
|
|
281
|
+
return onLine(line);
|
|
130
282
|
};
|
|
283
|
+
|
|
284
|
+
while (offset < size) {
|
|
285
|
+
const toRead = Math.min(READ_CHUNK_SIZE, size - offset);
|
|
286
|
+
const bytesRead = readSync(fd, chunk, 0, toRead, offset);
|
|
287
|
+
if (bytesRead <= 0) break;
|
|
288
|
+
offset += bytesRead;
|
|
289
|
+
|
|
290
|
+
const text = decoder.write(chunk.subarray(0, bytesRead));
|
|
291
|
+
let start = 0;
|
|
292
|
+
let nl: number;
|
|
293
|
+
while ((nl = text.indexOf("\n", start)) !== -1) {
|
|
294
|
+
const line = lineBuf + text.slice(start, nl);
|
|
295
|
+
lineBuf = "";
|
|
296
|
+
if (flushLine(line) === false) {
|
|
297
|
+
return { reachedEof: false, consumedBytes };
|
|
298
|
+
}
|
|
299
|
+
start = nl + 1;
|
|
300
|
+
}
|
|
301
|
+
lineBuf += text.slice(start);
|
|
302
|
+
|
|
303
|
+
// Defensive: bound memory for a single pathological line.
|
|
304
|
+
if (lineBuf.length > MAX_LINE_BYTES) {
|
|
305
|
+
lineBuf = "";
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// Flush decoder + any trailing line without a final newline.
|
|
309
|
+
const tail = decoder.end();
|
|
310
|
+
if (tail) lineBuf += tail;
|
|
311
|
+
if (lineBuf.length > 0) {
|
|
312
|
+
consumedBytes += Buffer.byteLength(lineBuf, "utf8"); // no trailing \n
|
|
313
|
+
onLine(lineBuf);
|
|
314
|
+
}
|
|
315
|
+
return { reachedEof: true, consumedBytes };
|
|
131
316
|
}
|
|
132
317
|
|
|
133
318
|
const HOME = homedir();
|
|
@@ -212,16 +397,57 @@ export function scanSessionDir(
|
|
|
212
397
|
return results;
|
|
213
398
|
}
|
|
214
399
|
|
|
400
|
+
// Load a session header from disk using a streaming forward read plus a bounded
|
|
401
|
+
// tail read — no fixed head window.
|
|
402
|
+
//
|
|
403
|
+
// Forward pass: reads complete lines from the start and stops at the first user
|
|
404
|
+
// message (which is all the title row needs). This reads exactly as many bytes
|
|
405
|
+
// as the first user message requires — a few KB for a normal session, ~19KB
|
|
406
|
+
// for a <skill> injection, more for a base64 image — and never truncates a line
|
|
407
|
+
// mid-JSON the way a fixed byte window would. So oversized first user messages
|
|
408
|
+
// (the cases that used to show "(no messages)") are now parsed correctly.
|
|
409
|
+
//
|
|
410
|
+
// Tail pass (only when the forward pass stopped before EOF): reads up to
|
|
411
|
+
// TAIL_READ_SIZE bytes from EOF and recovers the latest session_info (the
|
|
412
|
+
// rename name), bounded below by the forward stop offset so it never re-reads
|
|
413
|
+
// covered bytes. See TAIL_READ_SIZE for the documented tradeoff.
|
|
215
414
|
export function loadSessionHeader(
|
|
216
415
|
meta: SessionFileMeta,
|
|
217
416
|
): SessionHeader | null {
|
|
218
417
|
let fd: number | undefined;
|
|
219
418
|
try {
|
|
220
419
|
fd = openSync(meta.path, "r");
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
420
|
+
const acc = newAccumulator();
|
|
421
|
+
|
|
422
|
+
// Forward pass: read complete lines, stopping at the first user message.
|
|
423
|
+
const { reachedEof: forwardReachedEof, consumedBytes } = forEachLineForward(
|
|
424
|
+
fd,
|
|
425
|
+
meta.size,
|
|
426
|
+
(line) => {
|
|
427
|
+
processEntry(acc, line);
|
|
428
|
+
if (acc.header && acc.foundFirstUser) return false;
|
|
429
|
+
return true;
|
|
430
|
+
},
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
// If the forward pass stopped before EOF, recover the latest session_info
|
|
434
|
+
// from a bounded tail at EOF. Bounded below by consumedBytes so it never
|
|
435
|
+
// re-parses already-seen entries; the tail wins over the forward name
|
|
436
|
+
// (later in file order). A failure here falls back to the forward name.
|
|
437
|
+
let tailInfo: TailSessionInfo | undefined;
|
|
438
|
+
if (!forwardReachedEof) {
|
|
439
|
+
try {
|
|
440
|
+
const tailReadSize = Math.min(TAIL_READ_SIZE, meta.size - consumedBytes);
|
|
441
|
+
const tailBuf = Buffer.alloc(tailReadSize);
|
|
442
|
+
const tailOffset = meta.size - tailReadSize;
|
|
443
|
+
const tailBytesRead = readSync(fd, tailBuf, 0, tailReadSize, tailOffset);
|
|
444
|
+
tailInfo = scanTailForSessionInfo(tailBuf, tailBytesRead);
|
|
445
|
+
} catch {
|
|
446
|
+
// Tail read failed — fall back to forward-only name
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return buildHeader(acc, meta.path, meta.mtimeMs, forwardReachedEof, tailInfo);
|
|
225
451
|
} catch {
|
|
226
452
|
return null;
|
|
227
453
|
} finally {
|
|
@@ -287,4 +513,4 @@ export function matchQuery(
|
|
|
287
513
|
if (session.cwd.toLowerCase().includes(q)) return true;
|
|
288
514
|
if (session.id.toLowerCase().includes(q)) return true;
|
|
289
515
|
return false;
|
|
290
|
-
}
|
|
516
|
+
}
|
package/src/search.ts
CHANGED
|
@@ -38,10 +38,11 @@ function normalizeWhitespaceLower(text: string): string {
|
|
|
38
38
|
* Build the searchable text for a session.
|
|
39
39
|
*
|
|
40
40
|
* IMPORTANT LIMITATION: pi-core's /resume builds `allMessagesText` from the
|
|
41
|
-
* full file, so search matches against every message in the session. We
|
|
42
|
-
*
|
|
43
|
-
* queries like `fix oauth` won't match a session where
|
|
44
|
-
* the 5th message but not the 1st. Name, id, and cwd
|
|
41
|
+
* full file, so search matches against every message in the session. We stop
|
|
42
|
+
* reading at the first user message, so we search against `firstMessage`
|
|
43
|
+
* instead. This means queries like `fix oauth` won't match a session where
|
|
44
|
+
* that phrase appears in the 5th message but not the 1st. Name, id, and cwd
|
|
45
|
+
* matches are unaffected.
|
|
45
46
|
*
|
|
46
47
|
* See README "Known Limitations" section for the user-facing explanation.
|
|
47
48
|
*/
|