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.
Files changed (46) hide show
  1. package/dist/commands/install.d.ts +21 -0
  2. package/dist/commands/install.d.ts.map +1 -1
  3. package/dist/commands/install.js +70 -3
  4. package/dist/commands/install.js.map +1 -1
  5. package/dist/conversation-search.d.ts +117 -0
  6. package/dist/conversation-search.d.ts.map +1 -0
  7. package/dist/conversation-search.js +295 -0
  8. package/dist/conversation-search.js.map +1 -0
  9. package/dist/index.js +279 -32
  10. package/dist/index.js.map +1 -1
  11. package/dist/protocol.d.ts +3 -0
  12. package/dist/protocol.d.ts.map +1 -1
  13. package/dist/session-pool.d.ts +64 -32
  14. package/dist/session-pool.d.ts.map +1 -1
  15. package/dist/session-pool.js +184 -352
  16. package/dist/session-pool.js.map +1 -1
  17. package/dist/session-transcript.d.ts +120 -0
  18. package/dist/session-transcript.d.ts.map +1 -0
  19. package/dist/session-transcript.js +813 -0
  20. package/dist/session-transcript.js.map +1 -0
  21. package/package.json +2 -1
  22. package/public/_app/immutable/assets/0.B76aBayq.css +1 -0
  23. package/public/_app/immutable/chunks/{ClTMu0mO.js → BLQ6nRXO.js} +2 -2
  24. package/public/_app/immutable/chunks/{ClTMu0mO.js.map → BLQ6nRXO.js.map} +1 -1
  25. package/public/_app/immutable/chunks/{BzFqVyJ_.js → CKmxwmEQ.js} +2 -2
  26. package/public/_app/immutable/chunks/{BzFqVyJ_.js.map → CKmxwmEQ.js.map} +1 -1
  27. package/public/_app/immutable/chunks/{CDbw105l.js → DDXW2gm3.js} +2 -2
  28. package/public/_app/immutable/chunks/{CDbw105l.js.map → DDXW2gm3.js.map} +1 -1
  29. package/public/_app/immutable/entry/{app.CqLyltZH.js → app.BXjvSIVv.js} +3 -3
  30. package/public/_app/immutable/entry/{app.CqLyltZH.js.map → app.BXjvSIVv.js.map} +1 -1
  31. package/public/_app/immutable/entry/start.BY0WA8dA.js +2 -0
  32. package/public/_app/immutable/entry/start.BY0WA8dA.js.map +1 -0
  33. package/public/_app/immutable/nodes/{0.D_7-7cgp.js → 0.HhOK-JKB.js} +2 -2
  34. package/public/_app/immutable/nodes/{0.D_7-7cgp.js.map → 0.HhOK-JKB.js.map} +1 -1
  35. package/public/_app/immutable/nodes/{1.CDxNOU-Q.js → 1.BWYloVJt.js} +2 -2
  36. package/public/_app/immutable/nodes/{1.CDxNOU-Q.js.map → 1.BWYloVJt.js.map} +1 -1
  37. package/public/_app/immutable/nodes/2.Dq6JhX_v.js +104 -0
  38. package/public/_app/immutable/nodes/2.Dq6JhX_v.js.map +1 -0
  39. package/public/_app/version.json +1 -1
  40. package/public/index.html +11 -11
  41. package/public/service-worker.js +1 -1
  42. package/public/_app/immutable/assets/0.BLdSNDwj.css +0 -1
  43. package/public/_app/immutable/entry/start.CQ4WvoOm.js +0 -2
  44. package/public/_app/immutable/entry/start.CQ4WvoOm.js.map +0 -1
  45. package/public/_app/immutable/nodes/2.D1gQZgOb.js +0 -95
  46. package/public/_app/immutable/nodes/2.D1gQZgOb.js.map +0 -1
@@ -2,6 +2,7 @@ import path from 'path';
2
2
  import os from 'node:os';
3
3
  import fs from 'node:fs';
4
4
  import { execFileSync } from 'node:child_process';
5
+ import { readSessionHeader, readSessionListingInfo, readTranscriptWindow, previewText, extractMessageText, } from './session-transcript.js';
5
6
  /**
6
7
  * Normalize a path to prevent duplicate session folders.
7
8
  * - Removes trailing slashes (except for root "/")
@@ -47,39 +48,6 @@ export function normalizeSessionFile(sessionFile) {
47
48
  function isValidDate(d) {
48
49
  return d instanceof Date && Number.isFinite(d.getTime());
49
50
  }
50
- /**
51
- * Max length of the first-message PREVIEW shipped in the /sessions list. The
52
- * sidebar only renders a ~40-char snippet and filters on the text, so the full
53
- * (often huge: pasted prompts, PRDs, specs) first message must never cross the
54
- * wire for every session. This caps each entry, which is the dominant factor in
55
- * the /sessions payload size.
56
- */
57
- const FIRST_MESSAGE_PREVIEW_MAX = 160;
58
- /**
59
- * Force a string to be an independent, flat copy.
60
- *
61
- * V8 represents `big.slice(0, 160)` as a SlicedString that keeps its PARENT
62
- * alive. That is free when the preview is transient, but the listing cache
63
- * holds one preview per session for the process's lifetime, so an un-flattened
64
- * slice would pin every full first message (pasted PRDs, specs) in memory:
65
- * measured at ~33 MB of retained parents for ~2800 sessions whose visible
66
- * previews total under 1 MB. The Buffer round-trip allocates a fresh string.
67
- */
68
- function flattenString(s) {
69
- return s.length === 0 ? s : Buffer.from(s, 'utf8').toString('utf8');
70
- }
71
- /**
72
- * Collapse whitespace and cap to a short preview. Keeps the /sessions payload
73
- * tens-of-KB instead of multi-MB while preserving what the sidebar displays and
74
- * filters on. The result is flattened so caching it cannot retain the (possibly
75
- * huge) message it was sliced from.
76
- */
77
- function previewText(text) {
78
- const collapsed = (text || '').replace(/\s+/g, ' ').trim();
79
- return flattenString(collapsed.length > FIRST_MESSAGE_PREVIEW_MAX
80
- ? collapsed.slice(0, FIRST_MESSAGE_PREVIEW_MAX) + '\u2026'
81
- : collapsed);
82
- }
83
51
  /**
84
52
  * Convert a Date to an ISO string, tolerating invalid/missing dates.
85
53
  * Belt-and-suspenders guard: a single malformed session timestamp must never
@@ -172,30 +140,14 @@ export function makeIgnoreMatcher(patterns) {
172
140
  * ignored folder before reading its (potentially many, large) file bodies.
173
141
  */
174
142
  async function readSessionCwdFromHeader(filePath) {
175
- try {
176
- const fh = await fs.promises.open(filePath, 'r');
177
- try {
178
- const buf = Buffer.alloc(8192);
179
- const { bytesRead } = await fh.read(buf, 0, buf.length, 0);
180
- const text = buf.toString('utf8', 0, bytesRead);
181
- const nl = text.indexOf('\n');
182
- const firstLine = nl === -1 ? text : text.slice(0, nl);
183
- const header = JSON.parse(firstLine);
184
- return typeof header?.cwd === 'string' ? header.cwd : '';
185
- }
186
- finally {
187
- await fh.close();
188
- }
189
- }
190
- catch {
191
- return '';
192
- }
143
+ const header = await readSessionHeader(filePath);
144
+ return header?.cwd || '';
193
145
  }
194
146
  /**
195
147
  * Resolve a session's raw header cwd into a normalized absolute path, matching
196
148
  * how listSessions() buckets folders (tilde-expand; relative -> under home).
197
149
  */
198
- function resolveSessionCwd(rawCwd) {
150
+ export function resolveSessionCwd(rawCwd) {
199
151
  const raw = rawCwd || '';
200
152
  let cwd = raw;
201
153
  if (raw.startsWith('~')) {
@@ -206,86 +158,6 @@ function resolveSessionCwd(rawCwd) {
206
158
  }
207
159
  return normalizePath(cwd);
208
160
  }
209
- /**
210
- * Parse one session .jsonl body into the subset of fields the listing needs.
211
- * This is the directory-aware path's equivalent of pi's internal
212
- * buildSessionInfo (which is not exported). Pure: the caller does the IO, so
213
- * the result can be cached against the file's (mtime, size) stamp.
214
- * Returns null for a non-session / empty / unparseable file.
215
- */
216
- function parseDiskSessionInfo(filePath, content, mtime) {
217
- try {
218
- const lines = content.trim().split('\n');
219
- const entries = [];
220
- for (const line of lines) {
221
- if (!line.trim())
222
- continue;
223
- try {
224
- entries.push(JSON.parse(line));
225
- }
226
- catch { /* skip malformed line */ }
227
- }
228
- if (entries.length === 0)
229
- return null;
230
- const header = entries[0];
231
- if (header?.type !== 'session')
232
- return null;
233
- const parentSessionPath = typeof header.parentSession === 'string' && header.parentSession
234
- ? header.parentSession
235
- : undefined;
236
- let messageCount = 0;
237
- let firstMessage = '';
238
- let name;
239
- let lastMessageTime = 0;
240
- for (const entry of entries) {
241
- if (entry?.type === 'session_info') {
242
- name = (entry.name && String(entry.name).trim()) || undefined;
243
- }
244
- if (entry?.type !== 'message')
245
- continue;
246
- messageCount++;
247
- const message = entry.message;
248
- const role = message?.role;
249
- if (role !== 'user' && role !== 'assistant')
250
- continue;
251
- let textContent = '';
252
- const c = message?.content;
253
- if (typeof c === 'string') {
254
- textContent = c;
255
- }
256
- else if (Array.isArray(c)) {
257
- textContent = c.map((p) => (typeof p?.text === 'string' ? p.text : '')).join('');
258
- }
259
- if (!textContent)
260
- continue;
261
- if (!firstMessage && role === 'user')
262
- firstMessage = textContent;
263
- const t = typeof entry.timestamp === 'string' ? new Date(entry.timestamp).getTime() : NaN;
264
- if (Number.isFinite(t))
265
- lastMessageTime = Math.max(lastMessageTime, t);
266
- }
267
- const headerTime = typeof header.timestamp === 'string' ? new Date(header.timestamp).getTime() : NaN;
268
- const created = Number.isFinite(headerTime) ? new Date(headerTime) : new Date(NaN);
269
- const modified = lastMessageTime > 0 ? new Date(lastMessageTime) : mtime;
270
- return {
271
- path: filePath,
272
- id: typeof header.id === 'string' ? header.id : '',
273
- cwd: typeof header.cwd === 'string' ? header.cwd : '',
274
- name,
275
- created,
276
- modified,
277
- messageCount,
278
- // Store the CAPPED preview, never the raw first message: the cache holds
279
- // one entry per session on disk, so keeping full first messages (pasted
280
- // PRDs, specs) resident would cost megabytes of heap for no benefit.
281
- firstMessage: previewText(firstMessage) || '(no messages)',
282
- ...(parentSessionPath ? { parentSessionPath } : {}),
283
- };
284
- }
285
- catch {
286
- return null;
287
- }
288
- }
289
161
  /**
290
162
  * Process-wide cache of parsed session-listing info, keyed by absolute file
291
163
  * path. Without it, EVERY `/sessions` request re-read and re-parsed every
@@ -312,11 +184,23 @@ const dirCwdCache = new Map();
312
184
  * the same freshness guarantee a request arriving a moment earlier would get.
313
185
  */
314
186
  const inFlightScans = new Map();
187
+ /**
188
+ * Number of session BODIES read (streamed + parsed) since the last reset. The
189
+ * whole point of the cache is that this stays at 0 on a warm pass, so it is the
190
+ * thing tests assert on. Counting the IO primitive instead would conflate a
191
+ * body read with the cheap header probe, which also opens a file.
192
+ */
193
+ let sessionBodyReads = 0;
194
+ /** Test seam: how many session bodies have been read since the last reset. */
195
+ export function getSessionBodyReadCount() {
196
+ return sessionBodyReads;
197
+ }
315
198
  /** Test seam: drop all cached listing state. */
316
199
  export function clearSessionIndexCache() {
317
200
  diskSessionCache.clear();
318
201
  dirCwdCache.clear();
319
202
  inFlightScans.clear();
203
+ sessionBodyReads = 0;
320
204
  }
321
205
  /**
322
206
  * Yield to the event loop. The scan runs on the same thread that serves the
@@ -326,6 +210,11 @@ export function clearSessionIndexCache() {
326
210
  function yieldToEventLoop() {
327
211
  return new Promise((resolve) => setImmediate(resolve));
328
212
  }
213
+ /**
214
+ * Session count above which startup points at the retention settings. Chosen to
215
+ * be well past "a busy few months" so it is a real signal, not noise.
216
+ */
217
+ const WARN_SESSION_COUNT = 1000;
329
218
  /** How many files to parse between event-loop yields during a cold scan. */
330
219
  const SCAN_YIELD_EVERY = 8;
331
220
  /** Only log scan stats when a pass did real work (avoids a per-request log). */
@@ -335,18 +224,18 @@ const SCAN_LOG_MIN_READS = 10;
335
224
  * passes `folderWanted`. Async, cached and yielding: a warm pass is a `stat`
336
225
  * per file (milliseconds), and even a cold pass never blocks the event loop.
337
226
  */
338
- export function scanDiskSessions(sessionsRoot, folderWanted, label) {
227
+ export function scanDiskSessions(sessionsRoot, folderWanted, label, retention) {
339
228
  const key = `${sessionsRoot}::${label}`;
340
229
  const running = inFlightScans.get(key);
341
230
  if (running)
342
231
  return running;
343
- const scan = runDiskScan(sessionsRoot, folderWanted, label).finally(() => {
232
+ const scan = runDiskScan(sessionsRoot, folderWanted, label, retention).finally(() => {
344
233
  inFlightScans.delete(key);
345
234
  });
346
235
  inFlightScans.set(key, scan);
347
236
  return scan;
348
237
  }
349
- async function runDiskScan(sessionsRoot, folderWanted, label) {
238
+ async function runDiskScan(sessionsRoot, folderWanted, label, retention) {
350
239
  let dirEntries;
351
240
  try {
352
241
  dirEntries = await fs.promises.readdir(sessionsRoot, { withFileTypes: true });
@@ -358,6 +247,9 @@ async function runDiskScan(sessionsRoot, folderWanted, label) {
358
247
  const seenFiles = new Set();
359
248
  const seenDirs = new Set();
360
249
  const infos = [];
250
+ // (path, stat) of every session file that survived the folder pre-filter.
251
+ // Collected first so retention can be decided on metadata alone.
252
+ const candidates = [];
361
253
  let prunedFolders = 0;
362
254
  let readCount = 0;
363
255
  let parsedSinceYield = 0;
@@ -398,34 +290,58 @@ async function runDiskScan(sessionsRoot, folderWanted, label) {
398
290
  catch {
399
291
  continue;
400
292
  }
401
- const cached = diskSessionCache.get(filePath);
402
- let info;
403
- if (cached && cached.mtimeMs === stats.mtimeMs && cached.size === stats.size) {
404
- info = cached.info;
405
- }
406
- else {
407
- let content;
408
- try {
409
- content = await fs.promises.readFile(filePath, 'utf8');
410
- }
411
- catch {
412
- continue;
413
- }
414
- info = parseDiskSessionInfo(filePath, content, stats.mtime);
415
- diskSessionCache.set(filePath, { mtimeMs: stats.mtimeMs, size: stats.size, info });
416
- readCount++;
417
- if (++parsedSinceYield >= SCAN_YIELD_EVERY) {
418
- parsedSinceYield = 0;
419
- await yieldToEventLoop();
420
- }
293
+ candidates.push({ filePath, mtimeMs: stats.mtimeMs, size: stats.size, mtime: stats.mtime });
294
+ }
295
+ }
296
+ // Retention, applied on (cheap) stat metadata BEFORE any body is read.
297
+ let kept = candidates;
298
+ if (retention?.maxAgeDays && retention.maxAgeDays > 0) {
299
+ const cutoff = Date.now() - retention.maxAgeDays * 24 * 60 * 60 * 1000;
300
+ kept = kept.filter((c) => c.mtimeMs >= cutoff);
301
+ }
302
+ if (retention?.maxSessions && retention.maxSessions > 0 && kept.length > retention.maxSessions) {
303
+ kept = [...kept].sort((a, b) => b.mtimeMs - a.mtimeMs).slice(0, retention.maxSessions);
304
+ }
305
+ const droppedByRetention = candidates.length - kept.length;
306
+ for (const c of kept) {
307
+ const cached = diskSessionCache.get(c.filePath);
308
+ let info;
309
+ if (cached && cached.mtimeMs === c.mtimeMs && cached.size === c.size) {
310
+ info = cached.info;
311
+ }
312
+ else {
313
+ // Streaming, bounded-memory read: never materialize the transcript (see
314
+ // session-transcript.ts). This is the pass that used to peak near 1 GB of
315
+ // RSS at startup on a 2 GB sessions directory.
316
+ //
317
+ // ONE bad file must cost ONE session, never the listing. An IO error
318
+ // (EIO on a flaky disk, EACCES on a file another user owns) rejects the
319
+ // read, and this scan's promise is SHARED by every concurrent waiter for
320
+ // this view (inFlightScans), so letting it escape would turn a single
321
+ // unreadable transcript into a 500 for the whole dashboard. Skip the file
322
+ // and do NOT cache the failure: a transient error should be retried on
323
+ // the next pass, unlike an unparseable file (cached as `info: null`).
324
+ try {
325
+ info = await readSessionListingInfo(c.filePath, c.mtime);
421
326
  }
422
- if (!info)
423
- continue;
424
- // Defensive per-session re-check (mixed/edge cases the folder probe missed).
425
- if (!folderWanted(resolveSessionCwd(info.cwd)))
327
+ catch (err) {
328
+ console.error(`[wherever] skipping unreadable session ${c.filePath}:`, err);
426
329
  continue;
427
- infos.push(info);
330
+ }
331
+ diskSessionCache.set(c.filePath, { mtimeMs: c.mtimeMs, size: c.size, info });
332
+ readCount++;
333
+ sessionBodyReads++;
334
+ if (++parsedSinceYield >= SCAN_YIELD_EVERY) {
335
+ parsedSinceYield = 0;
336
+ await yieldToEventLoop();
337
+ }
428
338
  }
339
+ if (!info)
340
+ continue;
341
+ // Defensive per-session re-check (mixed/edge cases the folder probe missed).
342
+ if (!folderWanted(resolveSessionCwd(info.cwd)))
343
+ continue;
344
+ infos.push(info);
429
345
  }
430
346
  // Evict entries whose files/directories are gone (deleted sessions).
431
347
  for (const key of diskSessionCache.keys()) {
@@ -438,7 +354,9 @@ async function runDiskScan(sessionsRoot, folderWanted, label) {
438
354
  }
439
355
  if (readCount >= SCAN_LOG_MIN_READS) {
440
356
  console.log(`[wherever] ${label}: parsed ${readCount} changed session file(s) of ${seenFiles.size}` +
441
- `${prunedFolders > 0 ? `, skipped ${prunedFolders} folder(s)` : ''} in ${Date.now() - started}ms`);
357
+ `${prunedFolders > 0 ? `, skipped ${prunedFolders} folder(s)` : ''}` +
358
+ `${droppedByRetention > 0 ? `, ${droppedByRetention} outside retention` : ''}` +
359
+ ` in ${Date.now() - started}ms`);
442
360
  }
443
361
  return infos;
444
362
  }
@@ -680,8 +598,15 @@ export class SessionPool {
680
598
  // Pending `!sudo ...` commands awaiting a password from the client, keyed by a
681
599
  // one-shot promptId. The password never lives here: only the (password-free)
682
600
  // command and enough context to run it once the client replies. Entries are
683
- // removed as soon as the password arrives or the prompt is cancelled.
601
+ // removed as soon as the password arrives or the prompt is cancelled. A client
602
+ // can also simply vanish (phone sleeps, tab closes) leaving its prompt
603
+ // unanswered forever, so there are two backstops: the entry is dropped when
604
+ // its session is destroyed (idle eviction, so it is bounded by the session's
605
+ // own lifetime), and any entry past the TTL is swept when the next prompt is
606
+ // armed. Small, but this is the one map here with no other bound.
684
607
  pendingSudo = new Map();
608
+ /** How long an unanswered sudo prompt survives, if its session outlives it. */
609
+ static SUDO_PROMPT_TTL_MS = 30 * 60_000;
685
610
  onEvent;
686
611
  constructor(idleTimeoutMs = 300_000) {
687
612
  this.agentDir = getAgentDir();
@@ -716,7 +641,23 @@ export class SessionPool {
716
641
  * it (see inFlightScans).
717
642
  */
718
643
  warmSessionIndex() {
719
- void this.listSessions('default').catch(() => {
644
+ void this.listSessions('default')
645
+ .then((folders) => {
646
+ const count = folders.reduce((n, f) => n + f.sessions.length, 0);
647
+ const rssMb = Math.round(process.memoryUsage().rss / 1024 / 1024);
648
+ console.log(`[wherever] session index warm: ${count} session(s) listed, RSS ${rssMb} MB`);
649
+ // The sessions directory only ever grows, and every cold pass has to
650
+ // look at all of it. Say so ONCE, at startup, with the two levers --
651
+ // rather than leaving an operator to discover the cost when the machine
652
+ // is already thrashing.
653
+ const cfg = getWhereverConfig().sessions;
654
+ if (count >= WARN_SESSION_COUNT && !cfg?.maxAgeDays && !cfg?.maxSessions) {
655
+ console.log(`[wherever] tip: ${count} sessions is a lot to scan. Set sessions.maxAgeDays and/or ` +
656
+ `sessions.maxSessions in ~/.wherever/config.json to bound the listing (nothing is ` +
657
+ `deleted; older sessions stay openable by path/ID). See docs/USAGE.md.`);
658
+ }
659
+ })
660
+ .catch(() => {
720
661
  /* warm-up is best-effort: a real request will retry and report. */
721
662
  });
722
663
  }
@@ -805,32 +746,25 @@ export class SessionPool {
805
746
  sessionId: residentTracked.sessionId,
806
747
  cwd: residentTracked.cwd,
807
748
  model: residentTracked.model,
808
- history: this.getSessionHistoryWindow(resolvedFile, limit),
749
+ history: await this.getSessionHistoryWindow(resolvedFile, limit),
809
750
  readOnly: this.isReadOnlyCwd(residentTracked.cwd),
810
751
  resident: true,
811
752
  };
812
753
  }
813
754
  try {
814
- const sessionManager = SessionManager.open(resolvedFile);
815
- const header = sessionManager.getHeader();
816
- if (!header) {
755
+ // ONE streaming pass over the transcript yields the header, the last
756
+ // model_change and the history window. This used to be three whole-file
757
+ // loads (`SessionManager.open` alone reads and parses the file twice) plus
758
+ // a full `HistoryMessage[]` of every entry, base64 tool images included,
759
+ // just to slice the last 60 off the end.
760
+ const read = await readTranscriptWindow(resolvedFile, limit);
761
+ if (!read.header) {
817
762
  return { error: 'Session file has no header' };
818
763
  }
819
- const sessionId = sessionManager.getSessionId();
820
- const cwd = normalizePath(header.cwd || process.cwd());
821
- let model = '';
822
- if (modelStr) {
823
- model = modelStr;
824
- }
825
- else {
826
- const entries = sessionManager.getEntries();
827
- const modelChange = [...entries].reverse().find((e) => e.type === 'model_change');
828
- if (modelChange && 'provider' in modelChange && 'modelId' in modelChange) {
829
- model = `${modelChange.provider}:${modelChange.modelId}`;
830
- }
831
- }
832
- const all = this.mapEntriesToHistory(sessionManager.getEntries(), sessionId);
833
- const history = this.windowHistory(all, limit);
764
+ const sessionId = read.header.id;
765
+ const cwd = normalizePath(read.header.cwd || process.cwd());
766
+ const model = modelStr || read.model;
767
+ const history = { messages: read.messages, totalCount: read.totalCount, offset: read.offset };
834
768
  return { sessionFile: resolvedFile, sessionId, cwd, model, history, readOnly: this.isReadOnlyCwd(cwd), resident: false };
835
769
  }
836
770
  catch (err) {
@@ -884,10 +818,23 @@ export class SessionPool {
884
818
  }
885
819
  }
886
820
  if (!model && header) {
821
+ // Walk BACKWARDS in place for the last model_change. `[...entries]` +
822
+ // reverse() copied the entire entry array (hundreds of thousands of
823
+ // elements on a long session) to read one field. Note this path DOES
824
+ // hold the whole transcript in memory, unavoidably: `SessionManager`
825
+ // is what the live agent runs on and it needs the full context. That
826
+ // is the AGENT-BUILD path; the VIEW path (readSessionMeta) never loads
827
+ // it, which is why opening a session to read is now cheap even when
828
+ // building its agent is not.
887
829
  const entries = sessionManager.getEntries();
888
- const modelChange = [...entries].reverse().find((e) => e.type === 'model_change');
889
- if (modelChange && 'provider' in modelChange && 'modelId' in modelChange) {
890
- model = this.modelRegistry.find(modelChange.provider, modelChange.modelId);
830
+ for (let i = entries.length - 1; i >= 0; i--) {
831
+ const e = entries[i];
832
+ if (e.type !== 'model_change')
833
+ continue;
834
+ if ('provider' in e && 'modelId' in e) {
835
+ model = this.modelRegistry.find(e.provider, e.modelId);
836
+ }
837
+ break;
891
838
  }
892
839
  }
893
840
  const settingsManager = SettingsManager.create(normalizedCwd, this.agentDir);
@@ -1196,7 +1143,7 @@ export class SessionPool {
1196
1143
  return { error: 'Fork target must be a user message' };
1197
1144
  }
1198
1145
  const userMsg = selectedEntry.message;
1199
- const prefillText = this.extractMessageText(userMsg) || '';
1146
+ const prefillText = extractMessageText(userMsg) || '';
1200
1147
  // pi's position:'before' -> the new branch ends just BEFORE the chosen
1201
1148
  // user message, i.e. at that entry's parent. A null parent means the user
1202
1149
  // message is the very first entry; forking before it yields an empty
@@ -1286,134 +1233,6 @@ export class SessionPool {
1286
1233
  }
1287
1234
  return { conflict: false };
1288
1235
  }
1289
- getSessionHistory(sessionFileOrId) {
1290
- const tracked = this.getSession(sessionFileOrId);
1291
- if (!tracked)
1292
- return [];
1293
- const sessionManager = SessionManager.open(tracked.sessionFile);
1294
- return this.mapEntriesToHistory(sessionManager.getEntries());
1295
- }
1296
- /**
1297
- * Map raw pi session entries to the UI-facing HistoryMessage[]. Shared by the
1298
- * resident path (getSessionHistory) and the cheap cold-read path
1299
- * (readSessionMeta), so both produce identical history regardless of whether a
1300
- * live agent exists. The `sessionId` parameter is accepted for parity/future
1301
- * use; message shape does not depend on it today.
1302
- */
1303
- mapEntriesToHistory(entries, _sessionId) {
1304
- const messages = [];
1305
- for (const entry of entries) {
1306
- if (entry.type === 'message') {
1307
- const msgEntry = entry;
1308
- const msg = msgEntry.message;
1309
- const ts = Date.parse(msgEntry.timestamp);
1310
- if (msg.role === 'user') {
1311
- const content = this.extractMessageText(msg);
1312
- if (content) {
1313
- // Carry the source entry id so the client can "Fork from here"
1314
- // (fork BEFORE this user entry, pi's default position:'before').
1315
- const entryId = typeof msgEntry.id === 'string' ? msgEntry.id : undefined;
1316
- messages.push({ role: 'user', content, timestamp: ts, ...(entryId ? { entryId } : {}) });
1317
- }
1318
- }
1319
- else if (msg.role === 'assistant') {
1320
- const content = msg.content;
1321
- if (Array.isArray(content)) {
1322
- for (const block of content) {
1323
- if (block.type === 'thinking') {
1324
- const thinking = block.thinking || '';
1325
- if (thinking) {
1326
- messages.push({ role: 'thinking', content: thinking, timestamp: ts });
1327
- }
1328
- }
1329
- else if (block.type === 'text') {
1330
- const text = block.text || '';
1331
- if (text) {
1332
- messages.push({ role: 'assistant', content: text, timestamp: ts });
1333
- }
1334
- }
1335
- else if (block.type === 'toolCall') {
1336
- const tc = block;
1337
- const toolName = tc.name || tc.toolName || 'unknown';
1338
- const rawArgs = tc.arguments || tc.args;
1339
- const args = rawArgs ? JSON.stringify(rawArgs) : '';
1340
- const toolCallId = typeof tc.id === 'string' ? tc.id : undefined;
1341
- messages.push({ role: 'tool_call', content: args, timestamp: ts, toolName, ...(toolCallId ? { toolCallId } : {}) });
1342
- }
1343
- }
1344
- }
1345
- else if (typeof content === 'string' && content) {
1346
- messages.push({ role: 'assistant', content, timestamp: ts });
1347
- }
1348
- }
1349
- else if (msg.role === 'toolResult') {
1350
- const resultMsg = msg;
1351
- const toolName = resultMsg.toolName || 'unknown';
1352
- let resultText = '';
1353
- const resultImages = [];
1354
- if (Array.isArray(resultMsg.content)) {
1355
- resultText = resultMsg.content
1356
- .filter((c) => c.type === 'text')
1357
- .map((c) => c.text || '')
1358
- .join('\n');
1359
- // Pull image blocks (e.g. `read` on an image file) so reloaded
1360
- // history renders them inline, mirroring live tool_end.
1361
- for (const c of resultMsg.content) {
1362
- if (c && c.type === 'image' && typeof c.data === 'string' && c.data) {
1363
- resultImages.push({ mimeType: typeof c.mimeType === 'string' ? c.mimeType : 'image/png', data: c.data });
1364
- }
1365
- }
1366
- }
1367
- else if (typeof resultMsg.content === 'string') {
1368
- resultText = resultMsg.content;
1369
- }
1370
- const toolCallId = typeof resultMsg.toolCallId === 'string' ? resultMsg.toolCallId : undefined;
1371
- messages.push({ role: 'tool_result', content: resultText, timestamp: ts, toolName, isError: !!resultMsg.isError, ...(toolCallId ? { toolCallId } : {}), ...(resultImages.length > 0 ? { images: resultImages } : {}) });
1372
- }
1373
- else if (msg.role === 'bashExecution') {
1374
- const bashMsg = msg;
1375
- messages.push({
1376
- role: 'tool_call',
1377
- content: bashMsg.command || '',
1378
- timestamp: ts,
1379
- toolName: 'bash',
1380
- // A bashExecution entry is, by definition, a user `!command` (force
1381
- // command), never an agent tool call. Mark it so the web can
1382
- // auto-expand it on reload, mirroring the live tool_start flag.
1383
- forceCommand: true,
1384
- });
1385
- if (bashMsg.output) {
1386
- messages.push({
1387
- role: 'tool_result',
1388
- content: bashMsg.output,
1389
- timestamp: ts,
1390
- toolName: 'bash',
1391
- isError: bashMsg.exitCode !== undefined && bashMsg.exitCode !== 0,
1392
- });
1393
- }
1394
- }
1395
- }
1396
- }
1397
- return messages;
1398
- }
1399
- /**
1400
- * Pure windowing math over an already-mapped HistoryMessage[]. Returns the
1401
- * last `limit` messages (or the window ending at `beforeOffset`) plus the
1402
- * total count and the offset of the first returned message. Shared by the
1403
- * resident and cold-read paths.
1404
- */
1405
- windowHistory(all, limit, beforeOffset) {
1406
- const totalCount = all.length;
1407
- if (limit <= 0 || totalCount === 0) {
1408
- const end = beforeOffset ?? totalCount;
1409
- return { messages: [], totalCount, offset: Math.max(0, Math.min(end, totalCount)) };
1410
- }
1411
- const end = beforeOffset === undefined
1412
- ? totalCount
1413
- : Math.max(0, Math.min(beforeOffset, totalCount));
1414
- const start = Math.max(0, end - limit);
1415
- return { messages: all.slice(start, end), totalCount, offset: start };
1416
- }
1417
1236
  /**
1418
1237
  * Tail-first windowed history. Returns the last `limit` messages (the most
1419
1238
  * recent), along with the total count and the offset of the first returned
@@ -1421,9 +1240,20 @@ export class SessionPool {
1421
1240
  *
1422
1241
  * `beforeOffset`, when provided, returns the window of `limit` messages
1423
1242
  * ending just before that offset (used for "load older" requests).
1243
+ *
1244
+ * Always read STREAMING from the transcript on disk (see
1245
+ * session-transcript.ts): the file is the source of truth for both resident
1246
+ * and cold sessions, and only the requested window is ever materialized. The
1247
+ * previous implementation mapped EVERY entry of the file into a
1248
+ * `HistoryMessage[]` (base64 tool images included) and threw all but 60 of
1249
+ * them away, on every session open and every "load older" page.
1424
1250
  */
1425
- getSessionHistoryWindow(sessionFileOrId, limit, beforeOffset) {
1426
- return this.windowHistory(this.getSessionHistory(sessionFileOrId), limit, beforeOffset);
1251
+ async getSessionHistoryWindow(sessionFileOrId, limit, beforeOffset) {
1252
+ const tracked = this.getSession(sessionFileOrId);
1253
+ if (!tracked)
1254
+ return { messages: [], totalCount: 0, offset: 0 };
1255
+ const read = await readTranscriptWindow(tracked.sessionFile, limit, beforeOffset);
1256
+ return { messages: read.messages, totalCount: read.totalCount, offset: read.offset };
1427
1257
  }
1428
1258
  /**
1429
1259
  * List session folders.
@@ -1453,7 +1283,7 @@ export class SessionPool {
1453
1283
  return false;
1454
1284
  return view === 'readonly' ? isReadOnly(cwd) : !isReadOnly(cwd);
1455
1285
  };
1456
- const infos = await scanDiskSessions(path.join(this.agentDir, 'sessions'), folderWanted, `/sessions (${view})`);
1286
+ const infos = await scanDiskSessions(path.join(this.agentDir, 'sessions'), folderWanted, `/sessions (${view})`, { maxAgeDays: cfg?.maxAgeDays, maxSessions: cfg?.maxSessions });
1457
1287
  return this.buildFolders(infos, view === 'readonly' ? isReadOnly : undefined);
1458
1288
  }
1459
1289
  /**
@@ -1463,6 +1293,9 @@ export class SessionPool {
1463
1293
  * blocking work just to resolve one deep-linked session ID.
1464
1294
  */
1465
1295
  async findDiskSessionByIdOrName(idOrName) {
1296
+ // Deliberately NOT retention-limited: retention governs what the dashboard
1297
+ // LISTS, not what can be opened, so a deep link to an old session must still
1298
+ // resolve.
1466
1299
  const infos = await scanDiskSessions(path.join(this.agentDir, 'sessions'), () => true, 'session lookup');
1467
1300
  return infos.find((s) => s.id === idOrName || s.name === idOrName);
1468
1301
  }
@@ -1596,10 +1429,16 @@ export class SessionPool {
1596
1429
  // Everything else about the `!command` UX is unchanged.
1597
1430
  if (this.isSudoCommand(command)) {
1598
1431
  const promptId = randomUUID();
1432
+ const now = Date.now();
1433
+ for (const [id, p] of this.pendingSudo) {
1434
+ if (now - p.armedAt > SessionPool.SUDO_PROMPT_TTL_MS)
1435
+ this.pendingSudo.delete(id);
1436
+ }
1599
1437
  this.pendingSudo.set(promptId, {
1600
1438
  sessionFileOrId: tracked.sessionFile,
1601
1439
  command,
1602
1440
  excludeFromContext: isExcluded,
1441
+ armedAt: now,
1603
1442
  });
1604
1443
  if (this.onEvent) {
1605
1444
  this.onEvent(tracked.sessionFile, {
@@ -1958,6 +1797,13 @@ export class SessionPool {
1958
1797
  catch (err) { }
1959
1798
  }
1960
1799
  this.sessions.delete(tracked.sessionFile);
1800
+ // A prompt whose session is gone can never be answered: drop it here so an
1801
+ // abandoned prompt is bounded by the session's own lifetime (idle eviction)
1802
+ // rather than waiting for someone to type the next `!sudo`.
1803
+ for (const [id, p] of this.pendingSudo) {
1804
+ if (p.sessionFileOrId === tracked.sessionFile)
1805
+ this.pendingSudo.delete(id);
1806
+ }
1961
1807
  }
1962
1808
  async registerCliSession(rawSessionFile, cwd, modelStr, cliWs, isStreaming = false) {
1963
1809
  // Canonicalize the CLI-reported path so it keys the same entry the server
@@ -1994,19 +1840,28 @@ export class SessionPool {
1994
1840
  }
1995
1841
  let sessionId = existing?.sessionId;
1996
1842
  if (!sessionId) {
1997
- try {
1998
- const sessionManager = SessionManager.open(sessionFile);
1999
- sessionId = sessionManager.getSessionId();
2000
- }
2001
- catch (err) {
2002
- // Fallback 1: Extract from filename (e.g. some_path/TIMESTAMP_UUID.jsonl)
2003
- const baseName = path.basename(sessionFile);
2004
- const match = baseName.match(/_(.+)\.jsonl$/);
2005
- if (match) {
2006
- sessionId = match[1];
1843
+ // The session id IS the header's `id`, so read ONLY the header (one 8 KB
1844
+ // read). `SessionManager.open()` loads and parses the ENTIRE transcript
1845
+ // twice for this single field, on every CLI bridge register -- on a large
1846
+ // session that is hundreds of MB of transient allocation for 36 bytes.
1847
+ // It stays as the fallback for the cases the header read cannot answer
1848
+ // (file not written yet, no header), so behaviour is unchanged there.
1849
+ const header = await readSessionHeader(sessionFile);
1850
+ sessionId = header?.id || undefined;
1851
+ if (!sessionId) {
1852
+ try {
1853
+ sessionId = SessionManager.open(sessionFile).getSessionId();
2007
1854
  }
2008
- else {
2009
- return { tracked: null, error: `Could not determine a persistent session ID from file path: ${sessionFile}` };
1855
+ catch (err) {
1856
+ // Fallback: extract from filename (e.g. some_path/TIMESTAMP_UUID.jsonl)
1857
+ const baseName = path.basename(sessionFile);
1858
+ const match = baseName.match(/_(.+)\.jsonl$/);
1859
+ if (match) {
1860
+ sessionId = match[1];
1861
+ }
1862
+ else {
1863
+ return { tracked: null, error: `Could not determine a persistent session ID from file path: ${sessionFile}` };
1864
+ }
2010
1865
  }
2011
1866
  }
2012
1867
  }
@@ -2149,29 +2004,6 @@ export class SessionPool {
2149
2004
  }
2150
2005
  });
2151
2006
  }
2152
- extractMessageText(msg) {
2153
- if (msg.role === 'user') {
2154
- if (typeof msg.content === 'string')
2155
- return msg.content;
2156
- if (Array.isArray(msg.content)) {
2157
- return msg.content
2158
- .filter((c) => c.type === 'text')
2159
- .map((c) => c.text)
2160
- .join('\n');
2161
- }
2162
- }
2163
- if (msg.role === 'assistant') {
2164
- if (typeof msg.content === 'string')
2165
- return msg.content;
2166
- if (Array.isArray(msg.content)) {
2167
- return msg.content
2168
- .filter((c) => c.type === 'text')
2169
- .map((c) => c.text)
2170
- .join('\n');
2171
- }
2172
- }
2173
- return '';
2174
- }
2175
2007
  parseModelStr(modelStr) {
2176
2008
  const colonIdx = modelStr.indexOf(':');
2177
2009
  if (colonIdx === -1)