@clagentic/console 1.6.0 → 1.7.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 (85) hide show
  1. package/bin/cli.js +20 -102
  2. package/deploy/clagentic-console.service +10 -1
  3. package/lib/agents-favorites.js +0 -1
  4. package/lib/agents.js +0 -10
  5. package/lib/cli-sessions.js +0 -12
  6. package/lib/codex-defaults.js +4 -1
  7. package/lib/config.js +0 -8
  8. package/lib/daemon.js +88 -9
  9. package/lib/dm.js +0 -1
  10. package/lib/ipc.js +129 -59
  11. package/lib/loop-handoff.js +5 -1
  12. package/lib/memory-limits.js +214 -36
  13. package/lib/memory-shed.js +207 -0
  14. package/lib/notes.js +0 -20
  15. package/lib/os-users.js +0 -3
  16. package/lib/project-connection.js +31 -5
  17. package/lib/project-external-trigger.js +2 -1
  18. package/lib/project-filesystem.js +18 -11
  19. package/lib/project-knowledge.js +0 -8
  20. package/lib/project-loop.js +162 -9
  21. package/lib/project-sessions.js +87 -21
  22. package/lib/project-user-mention.js +2 -1
  23. package/lib/project-user-message.js +16 -5
  24. package/lib/project.js +14 -25
  25. package/lib/public/app.js +1 -2
  26. package/lib/public/css/channel-layout.css +25 -158
  27. package/lib/public/css/diagnostics.css +17 -1
  28. package/lib/public/css/icon-strip.css +1 -32
  29. package/lib/public/css/input.css +0 -56
  30. package/lib/public/css/loop.css +94 -0
  31. package/lib/public/css/messages.css +23 -4
  32. package/lib/public/css/mobile-nav.css +0 -124
  33. package/lib/public/index.html +11 -11
  34. package/lib/public/modules/app-connection.js +2 -1
  35. package/lib/public/modules/app-favicon.js +1 -1
  36. package/lib/public/modules/app-loop-ui.js +140 -4
  37. package/lib/public/modules/app-messages.js +17 -11
  38. package/lib/public/modules/app-projects.js +1 -1
  39. package/lib/public/modules/custom-icons.js +15 -1
  40. package/lib/public/modules/dom-refs.js +1 -2
  41. package/lib/public/modules/emoji-picker.js +6 -1
  42. package/lib/public/modules/filebrowser.js +33 -9
  43. package/lib/public/modules/input.js +39 -0
  44. package/lib/public/modules/scheduler.js +20 -6
  45. package/lib/public/modules/sidebar-mobile.js +0 -64
  46. package/lib/public/modules/sidebar-projects.js +90 -25
  47. package/lib/public/modules/sidebar-sessions.js +80 -13
  48. package/lib/public/modules/stt.js +2 -6
  49. package/lib/public/modules/tool-palette.js +0 -1
  50. package/lib/public/modules/tools.js +2 -6
  51. package/lib/public/style.css +0 -1
  52. package/lib/scheduler.js +0 -1
  53. package/lib/sdk-bridge.js +126 -194
  54. package/lib/sdk-message-processor.js +24 -9
  55. package/lib/sdk-permission-ownership.js +51 -0
  56. package/lib/server-auth.js +25 -18
  57. package/lib/server-dm.js +1 -1
  58. package/lib/server-skills.js +9 -1
  59. package/lib/server.js +18 -3
  60. package/lib/session-search.js +4 -299
  61. package/lib/sessions.js +467 -82
  62. package/lib/smtp.js +0 -15
  63. package/lib/user-presence.js +0 -12
  64. package/lib/users-preferences.js +2 -2
  65. package/lib/users.js +0 -5
  66. package/lib/utils.js +56 -0
  67. package/lib/ws-schema.js +3 -14
  68. package/lib/yoke/adapters/codex.js +96 -10
  69. package/lib/yoke/codex-local-image.js +79 -0
  70. package/package.json +1 -1
  71. package/scripts/postinstall.js +16 -11
  72. package/lib/ask-user-mcp-server.js +0 -121
  73. package/lib/project-memory.js +0 -39
  74. package/lib/public/console-icon.png +0 -0
  75. package/lib/public/css/debate.css +0 -1610
  76. package/lib/public/favicon-banded-32.png +0 -0
  77. package/lib/public/favicon-dark.svg +0 -32
  78. package/lib/public/favicon.svg +0 -32
  79. package/lib/public/icon-banded-96.png +0 -0
  80. package/lib/public/icons/email/gmail.svg +0 -7
  81. package/lib/public/icons/email/outlook.svg +0 -35
  82. package/lib/public/icons/email/yahoo.svg +0 -1
  83. package/lib/public/modules/events.js +0 -21
  84. package/lib/public/modules/state.js +0 -3
  85. package/lib/sdk-worker.js +0 -730
package/lib/sessions.js CHANGED
@@ -6,6 +6,19 @@ var users = require("./users");
6
6
  var { CODEX_DEFAULTS } = require("./codex-defaults");
7
7
  var { isVisibleHistoryEvent } = require("./history-visibility");
8
8
 
9
+ // lr-2ea2a7: hard cap on how many history entries a session may hold in the
10
+ // heap array, regardless of isProcessing state. On-disk JSONL remains the
11
+ // single source of truth for full history; the in-heap array is always a
12
+ // bounded TAIL window, never the full record, for every session including
13
+ // an actively-processing loop that runs for hours. Without this, the
14
+ // LRU_HISTORY_LIMIT isProcessing-skip (below) left a never-terminating loop
15
+ // session's heap history completely unbounded — the root cause diagnosed in
16
+ // lr-2ea2a7 (4th daemon OOM incident). Trim-to has hysteresis (trims well
17
+ // below the cap) so a session sitting exactly at the boundary doesn't
18
+ // re-trigger a trim on every single append.
19
+ var HISTORY_INMEM_MAX_DEFAULT = 1000;
20
+ var HISTORY_INMEM_TRIM_TO_DEFAULT = 800;
21
+
9
22
  function createSessionManager(opts) {
10
23
  var cwd = opts.cwd;
11
24
  var send = opts.send; // function(obj) - broadcast to all clients
@@ -14,6 +27,17 @@ function createSessionManager(opts) {
14
27
  var sendAndRecord = null; // set after init via setSendAndRecord
15
28
  var onSessionDone = opts.onSessionDone || function () {};
16
29
 
30
+ // daemon.json knob `historyInMemMax` (lr-2ea2a7): operators may override the
31
+ // in-heap tail cap. TRIM_TO is derived as 80% of the configured max so the
32
+ // hysteresis gap scales with the cap instead of a fixed 200-entry offset
33
+ // that would be meaningless (or negative) for a small custom override.
34
+ var HISTORY_INMEM_MAX = (typeof opts.historyInMemMax === "number" && opts.historyInMemMax > 0)
35
+ ? opts.historyInMemMax
36
+ : HISTORY_INMEM_MAX_DEFAULT;
37
+ var HISTORY_INMEM_TRIM_TO = (typeof opts.historyInMemMax === "number" && opts.historyInMemMax > 0)
38
+ ? Math.max(1, Math.floor(HISTORY_INMEM_MAX * 0.8))
39
+ : HISTORY_INMEM_TRIM_TO_DEFAULT;
40
+
17
41
  // --- LRU eviction limit: max sessions with in-memory history ---
18
42
  var LRU_HISTORY_LIMIT = 50;
19
43
  // Access order for loaded sessions: most-recently-accessed localId is last.
@@ -43,6 +67,147 @@ function createSessionManager(opts) {
43
67
  return path.join(sessionsDir, cliSessionId + ".jsonl");
44
68
  }
45
69
 
70
+ function buildMetaLine(session) {
71
+ var metaObj = {
72
+ type: "meta",
73
+ localId: session.localId,
74
+ cliSessionId: session.cliSessionId,
75
+ title: session.title,
76
+ createdAt: session.createdAt,
77
+ };
78
+ if (session.ownerId) metaObj.ownerId = session.ownerId;
79
+ if (session.vendor) metaObj.vendor = session.vendor;
80
+ if (session.sessionVisibility) metaObj.sessionVisibility = session.sessionVisibility;
81
+ if (session.bookmarked) metaObj.bookmarked = true;
82
+ if (typeof session.favoriteOrder === "number") metaObj.favoriteOrder = session.favoriteOrder;
83
+ if (session.lastRewindUuid) metaObj.lastRewindUuid = session.lastRewindUuid;
84
+ if (session.agentName) metaObj.agentName = session.agentName;
85
+ if (session.loop) metaObj.loop = session.loop;
86
+ // Persist per-session "allow for session" tool grants (lr-8b2e) so they
87
+ // survive daemon restart / resume-by-cliSessionId rehydration instead of
88
+ // silently re-prompting. Only written when non-empty — grants that were
89
+ // never given must stay absent, not round-trip as {}.
90
+ if (session.allowedTools && Object.keys(session.allowedTools).length > 0) {
91
+ metaObj.allowedTools = session.allowedTools;
92
+ }
93
+ return JSON.stringify(metaObj);
94
+ }
95
+
96
+ // Rewrite only the first (meta) line of an on-disk session file in place,
97
+ // leaving every subsequent history line untouched. Used when a session's
98
+ // history has not been loaded into memory (lr-79c6) — reading the whole
99
+ // history back into memory just to rewrite a title/flag would be wasteful,
100
+ // and serializing session.history (which is [] while unloaded) would
101
+ // truncate the file to the meta line alone, destroying the conversation.
102
+ // Returns true on success, false if the rewrite could not be performed
103
+ // (e.g. no file on disk yet, or the existing line 0 is not actually a meta
104
+ // record — see lr-f940 below) so the caller can fall back to a full write.
105
+ function rewriteMetaLineInPlace(session) {
106
+ var sfPath = sessionFilePath(session.cliSessionId);
107
+ var content;
108
+ try {
109
+ content = fs.readFileSync(sfPath, "utf8");
110
+ } catch (e) {
111
+ return false;
112
+ }
113
+ var nl = content.indexOf("\n");
114
+ var firstLine = nl !== -1 ? content.slice(0, nl) : content;
115
+ // lr-f940 (N1): a session can reach its FIRST-EVER saveSessionFile() call
116
+ // after already having buffered-appended history lines to disk via
117
+ // appendToSessionFile (e.g. sendAndRecord calls before any save has run)
118
+ // — in that case line 0 on disk is a history record, not a meta line, and
119
+ // blindly overwriting it here would silently destroy that first history
120
+ // entry. Only take the in-place path when line 0 is verifiably a meta
121
+ // record; otherwise fall through to the full write below, which is safe
122
+ // (it serializes the in-memory history, which is present since this can
123
+ // only happen while _historyLoaded is true).
124
+ var firstParsed;
125
+ try { firstParsed = JSON.parse(firstLine); } catch (e) { return false; }
126
+ if (!firstParsed || firstParsed.type !== "meta") return false;
127
+ var rest = nl !== -1 ? content.slice(nl + 1) : "";
128
+ var meta = buildMetaLine(session);
129
+ var tmpPath = sfPath + ".tmp." + process.pid;
130
+ fs.writeFileSync(tmpPath, meta + "\n" + rest);
131
+ if (process.platform !== "win32") {
132
+ try { fs.chmodSync(tmpPath, 0o600); } catch (chmodErr) {}
133
+ }
134
+ fs.renameSync(tmpPath, sfPath);
135
+ session._fileModeSet = true;
136
+ return true;
137
+ }
138
+
139
+ // lr-f940 (N1, top-3): has session.history changed since the last time it
140
+ // was durably written to disk in full (either by a full saveSessionFile
141
+ // rewrite, or by the normal doSendAndRecord + appendToSessionFile buffered
142
+ // append path, which keeps disk in sync one entry at a time)?
143
+ //
144
+ // Tracked via length comparison rather than an opt-in flag callers must
145
+ // remember to set: session._historyPersistedLength records how many
146
+ // entries are known to be reflected on disk. doSendAndRecord increments it
147
+ // by exactly one per durably-appended entry; a full rewrite here sets it to
148
+ // session.history.length. ANY other direct mutation of session.history
149
+ // (push/pop/slice/splice/reassignment — e.g. rewind trimming or fork
150
+ // copying a new session's history wholesale) changes session.history.length
151
+ // without updating the counter, so the mismatch is detected automatically
152
+ // and a full rewrite still runs — no caller needs to opt in, and no
153
+ // pre-existing "loaded session save always does a full rewrite" contract
154
+ // (lr-79c6's own regression test) is broken by adding this fast path.
155
+ //
156
+ // lr-2ea2a7: _historyPersistedLength is now the ABSOLUTE persisted count
157
+ // (how many entries exist on disk total), not the heap array length —
158
+ // once recordHistoryEntry() starts trimming the heap tail,
159
+ // session.history.length alone under-counts what's actually durable.
160
+ // _historyBaseIndex (count of entries trimmed off the head) plus the
161
+ // current heap length recovers the absolute count for comparison. A
162
+ // session that has never been trimmed keeps _historyBaseIndex === 0, so
163
+ // this is exactly the pre-lr-2ea2a7 comparison for every existing caller
164
+ // and regression test (lr-79c6, lr-f940).
165
+ function historyMatchesDisk(session) {
166
+ return session._historyPersistedLength === (session._historyBaseIndex || 0) + session.history.length;
167
+ }
168
+
169
+ // lr-2ea2a7: single choke point for every session.history.push() in the
170
+ // codebase (grep-guarded by test/session-history-cap-lr-2ea2a7.test.js —
171
+ // no other file may call session.history.push directly). Responsible for:
172
+ // (a) pushing the entry when history is loaded in memory,
173
+ // (b) maintaining _historyPersistedLength as callers expect (callers that
174
+ // pair this with appendToSessionFile — the overwhelmingly common
175
+ // case — get the same "already durable" accounting doSendAndRecord
176
+ // always did; see the `persisted` param below for the rare direct
177
+ // push that is NOT paired with a durable append),
178
+ // (c) enforcing the bounded in-heap tail cap with hysteresis, advancing
179
+ // _historyBaseIndex by however many entries were trimmed off the head
180
+ // so absolute indices (messageUUIDs[].historyIndex, history_meta
181
+ // total/from, load_more_history before/target) stay valid.
182
+ //
183
+ // Only entries already durable on disk may ever be trimmed off the heap —
184
+ // trimming is driven purely by HISTORY_INMEM_MAX against the CURRENT heap
185
+ // length, and every heap entry at trim time was pushed here in call order,
186
+ // so entries older than the trim point were appended (and, for the
187
+ // doSendAndRecord path, buffer-flushed synchronously on every
188
+ // loadSessionHistory/saveSessionFile call) before this trim ever runs.
189
+ //
190
+ // @param {object} session
191
+ // @param {object} obj - the history entry to record
192
+ // @param {boolean} [persisted=true] - whether this push is paired with a
193
+ // durable disk append (appendToSessionFile) in the same call. Pass false
194
+ // for the rare direct-to-heap push (e.g. context_preview cards) that
195
+ // relies on a LATER full saveSessionFile() rewrite instead — matches the
196
+ // pre-existing lr-f940 contract where such a push is detected via the
197
+ // historyMatchesDisk() length mismatch, not counted as already-durable.
198
+ function recordHistoryEntry(session, obj, persisted) {
199
+ if (!session._historyLoaded) return;
200
+ session.history.push(obj);
201
+ if (persisted !== false) {
202
+ session._historyPersistedLength = (session._historyPersistedLength || 0) + 1;
203
+ }
204
+ if (session.history.length > HISTORY_INMEM_MAX) {
205
+ var trimCount = session.history.length - HISTORY_INMEM_TRIM_TO;
206
+ session.history.splice(0, trimCount);
207
+ session._historyBaseIndex = (session._historyBaseIndex || 0) + trimCount;
208
+ }
209
+ }
210
+
46
211
  function saveSessionFile(session) {
47
212
  if (!session.cliSessionId) return;
48
213
  // Flush any pending async-append buffer before rewriting the file so that
@@ -50,29 +215,29 @@ function createSessionManager(opts) {
50
215
  // timer fires after the atomic rename completes (lr-e0de).
51
216
  flushSessionBuffer(session);
52
217
  try {
53
- var metaObj = {
54
- type: "meta",
55
- localId: session.localId,
56
- cliSessionId: session.cliSessionId,
57
- title: session.title,
58
- createdAt: session.createdAt,
59
- };
60
- if (session.ownerId) metaObj.ownerId = session.ownerId;
61
- if (session.vendor) metaObj.vendor = session.vendor;
62
- if (session.sessionVisibility) metaObj.sessionVisibility = session.sessionVisibility;
63
- if (session.bookmarked) metaObj.bookmarked = true;
64
- if (typeof session.favoriteOrder === "number") metaObj.favoriteOrder = session.favoriteOrder;
65
- if (session.lastRewindUuid) metaObj.lastRewindUuid = session.lastRewindUuid;
66
- if (session.agentName) metaObj.agentName = session.agentName;
67
- if (session.loop) metaObj.loop = session.loop;
68
- // Persist per-session "allow for session" tool grants (lr-8b2e) so they
69
- // survive daemon restart / resume-by-cliSessionId rehydration instead of
70
- // silently re-prompting. Only written when non-empty — grants that were
71
- // never given must stay absent, not round-trip as {}.
72
- if (session.allowedTools && Object.keys(session.allowedTools).length > 0) {
73
- metaObj.allowedTools = session.allowedTools;
218
+ // lr-f940 (N1, top-3): saveSessionFile() used to re-serialize the ENTIRE
219
+ // in-memory history on every metadata-only mutation (title rename,
220
+ // bookmark/favorite toggle, owner transfer, visibility change, agent
221
+ // change, vendor bind) whenever the session's history happened to be
222
+ // loaded (_historyLoaded) — the overwhelmingly common case for an
223
+ // active/processing session, since that session is never LRU-evicted
224
+ // (_lruEvictIfNeeded skips the active session and any isProcessing
225
+ // session). That is an O(n) full-file rewrite (CPU + a second full
226
+ // string in memory) for a change that only ever touches the meta line.
227
+ //
228
+ // The existing !_historyLoaded branch below already rewrites just the
229
+ // meta line in place (lr-79c6) — extend the same fast path to loaded
230
+ // sessions too, but only when history is verifiably unchanged since the
231
+ // last durable write (historyMatchesDisk). Any direct history mutation
232
+ // is caught automatically by the length check, so this still falls
233
+ // through to a full rewrite whenever needed.
234
+ if (!session._historyLoaded || historyMatchesDisk(session)) {
235
+ if (rewriteMetaLineInPlace(session)) return;
236
+ // No file exists yet to patch in place (e.g. never persisted) — fall
237
+ // through to the full write below, which is safe since there is no
238
+ // on-disk history to lose.
74
239
  }
75
- var meta = JSON.stringify(metaObj);
240
+ var meta = buildMetaLine(session);
76
241
  var lines = [meta];
77
242
  for (var i = 0; i < session.history.length; i++) {
78
243
  lines.push(JSON.stringify(session.history[i]));
@@ -90,6 +255,9 @@ function createSessionManager(opts) {
90
255
  fs.renameSync(tmpPath, sfPath);
91
256
  // Mark that the file mode has been set so appendToSessionFile skips chmod.
92
257
  session._fileModeSet = true;
258
+ // The full rewrite above just persisted the current in-memory history
259
+ // in full, so it now matches disk exactly (lr-f940).
260
+ session._historyPersistedLength = session.history.length;
93
261
  } catch(e) {
94
262
  console.error("[session] Failed to save session file:", e.message);
95
263
  }
@@ -121,7 +289,24 @@ function createSessionManager(opts) {
121
289
  buf.lines = [];
122
290
  }
123
291
 
124
- // Flush asynchronously (non-blocking hot path).
292
+ // Flush from the batching timer/threshold (called "async" because it is
293
+ // invoked off the hot path -- the 50ms timer or the 20-line threshold in
294
+ // appendToSessionFile -- rather than synchronously inline with every
295
+ // append). The write itself uses appendFileSync rather than
296
+ // fs.promises.appendFile.
297
+ //
298
+ // This used to use fs.promises.appendFile. Because that write is not
299
+ // awaited by its caller, a session-end/process-exit synchronous flush
300
+ // (flushSessionBuffer) or a saveSessionFile() atomic rename could run
301
+ // before the pending promise's write actually landed on disk -- appending
302
+ // after a rename writes into a file that no longer exists at the recorded
303
+ // path (silently lost), or interleaves with saveSessionFile's rewritten
304
+ // content, corrupting replay order (lr-1bdb item D). Using appendFileSync
305
+ // here removes the race outright: since Node's event loop is
306
+ // single-threaded, a synchronous fs call cannot be interleaved with any
307
+ // other JS (including another flush) -- every write for a given session is
308
+ // trivially serialized in call order, with no in-flight promise that a
309
+ // later synchronous flush could race.
125
310
  function _flushBufferAsync(session) {
126
311
  var buf = _writeBuffers[session.localId];
127
312
  if (!buf || buf.lines.length === 0) return;
@@ -135,10 +320,12 @@ function createSessionManager(opts) {
135
320
  }
136
321
  var toWrite = buf.lines.join("");
137
322
  buf.lines = [];
138
- var afPath = sessionFilePath(session.cliSessionId);
139
- fs.promises.appendFile(afPath, toWrite).catch(function(e) {
323
+ try {
324
+ var afPath = sessionFilePath(session.cliSessionId);
325
+ fs.appendFileSync(afPath, toWrite);
326
+ } catch (e) {
140
327
  console.error("[session] Failed to async-flush session buffer:", e.message);
141
- });
328
+ }
142
329
  }
143
330
 
144
331
  function appendToSessionFile(session, obj) {
@@ -185,6 +372,26 @@ function createSessionManager(opts) {
185
372
  lruOrder.push(localId);
186
373
  }
187
374
 
375
+ // Unload a single session's history from memory (disk file remains the
376
+ // source of truth) and drop it from the LRU order. Shared by the
377
+ // one-at-a-time post-load eviction (_lruEvictIfNeeded) and the
378
+ // pressure-driven bulk eviction (forceEvictToLimit, lr-5e70).
379
+ function _evictSession(candidate) {
380
+ candidate.history = [];
381
+ candidate._historyLoaded = false;
382
+ // _historyLoaded=false alone already forces the meta-only save path on
383
+ // the next saveSessionFile() regardless of _historyPersistedLength;
384
+ // reset it for clarity so a future reload starts from a clean known
385
+ // state (lr-f940 — see loadSessionHistory, which sets it correctly).
386
+ candidate._historyPersistedLength = 0;
387
+ // lr-2ea2a7: reset alongside _historyPersistedLength — the next
388
+ // loadSessionHistory() tail-load recomputes this from the disk scan;
389
+ // a stale non-zero value here would corrupt absolute-index math
390
+ // (historyMatchesDisk, replay from/total) before that reload happens.
391
+ candidate._historyBaseIndex = 0;
392
+ _lruRemove(candidate.localId);
393
+ }
394
+
188
395
  // Evict the least-recently-used loaded session if we are over the limit.
189
396
  // Never evicts the active session or one that is currently processing.
190
397
  function _lruEvictIfNeeded() {
@@ -194,16 +401,81 @@ function createSessionManager(opts) {
194
401
  if (candidateId === activeSessionId) continue;
195
402
  var candidate = sessions.get(candidateId);
196
403
  if (!candidate || candidate.isProcessing) continue;
197
- // Unload history from memory; disk file is the source of truth.
198
- candidate.history = [];
199
- candidate._historyLoaded = false;
200
- _lruRemove(candidateId);
404
+ _evictSession(candidate);
201
405
  return;
202
406
  }
203
407
  }
204
408
 
205
- // Synchronously load a session's history from disk if it has not been loaded.
206
- // Reconstructs messageUUIDs from the history lines.
409
+ // lr-5e70: force-evict loaded sessions down to an arbitrary pressure limit,
410
+ // driven by a MemoryHigh watermark crossing rather than the normal
411
+ // post-load LRU check. Unlike _lruEvictIfNeeded (which evicts at most one
412
+ // session per call, since it runs on every loadSessionHistory), this loops
413
+ // until the limit is reached or no further candidate remains — a single
414
+ // shedding pass may need to evict many sessions at once to bring memory
415
+ // down quickly. Same exemptions as the normal LRU path: never evicts the
416
+ // active session or an isProcessing session (skip-active-session and
417
+ // skip-processing are both load-bearing invariants, not incidental).
418
+ //
419
+ // @param {number} limit — target max loaded-session count after this call
420
+ // @returns {number} count of sessions actually evicted
421
+ function forceEvictToLimit(limit) {
422
+ var evicted = 0;
423
+ var guard = lruOrder.length; // upper bound on loop iterations — no infinite loop
424
+ while (lruOrder.length > limit && guard-- > 0) {
425
+ var evictedOne = false;
426
+ for (var ei = 0; ei < lruOrder.length; ei++) {
427
+ var candidateId = lruOrder[ei];
428
+ if (candidateId === activeSessionId) continue;
429
+ var candidate = sessions.get(candidateId);
430
+ if (!candidate || candidate.isProcessing) continue;
431
+ _evictSession(candidate);
432
+ evicted++;
433
+ evictedOne = true;
434
+ break;
435
+ }
436
+ if (!evictedOne) break; // no further evictable candidate — stop
437
+ }
438
+ return evicted;
439
+ }
440
+
441
+ // Parse every history line in a session's on-disk JSONL file (skipping the
442
+ // meta line 0). Returns { lines: [parsedEntry, ...], messageUUIDs: [...] }
443
+ // where messageUUIDs carries ABSOLUTE historyIndex values (position within
444
+ // the full on-disk sequence, not any in-heap subset) — the wire contract
445
+ // (messageUUIDs[].historyIndex, rewind/fork targeting) depends on this
446
+ // being correct regardless of how much of `lines` a caller keeps in heap.
447
+ // Shared by loadSessionHistory (tail-only) and loadFullSessionHistory
448
+ // (keeps everything) so both stay consistent with the same parse pass.
449
+ function _parseSessionFileLines(session) {
450
+ var filePath = sessionFilePath(session.cliSessionId);
451
+ var content;
452
+ try { content = fs.readFileSync(filePath, "utf8"); } catch { content = ""; }
453
+ var rawLines = content.trim().split("\n");
454
+ var lines = [];
455
+ var messageUUIDs = [];
456
+ // Skip line 0 — it's the meta record, already loaded at startup.
457
+ for (var j = 1; j < rawLines.length; j++) {
458
+ if (!rawLines[j]) continue;
459
+ var entry;
460
+ try { entry = JSON.parse(rawLines[j]); } catch { continue; }
461
+ if (entry.type === "message_uuid") {
462
+ messageUUIDs.push({ uuid: entry.uuid, type: entry.messageType, historyIndex: lines.length });
463
+ }
464
+ lines.push(entry);
465
+ }
466
+ return { lines: lines, messageUUIDs: messageUUIDs };
467
+ }
468
+
469
+ // Synchronously load a session's history from disk if it has not been
470
+ // loaded. lr-2ea2a7: loads only the TAIL (last HISTORY_INMEM_TRIM_TO
471
+ // entries) into the heap array — on-disk JSONL, not the heap, is the
472
+ // source of truth for full history. messageUUIDs is still reconstructed
473
+ // in FULL (uuid + type + absolute historyIndex per entry is small — no
474
+ // reason to cap it) by scanning every line, so rewind/fork targeting by
475
+ // uuid keeps working for any point in the conversation, not just the tail.
476
+ // session._historyBaseIndex records how many entries were skipped so
477
+ // absolute-index math (historyMatchesDisk, history_meta total/from,
478
+ // load_more_history) stays correct.
207
479
  function loadSessionHistory(session) {
208
480
  if (session._historyLoaded) {
209
481
  _lruTouch(session.localId);
@@ -218,30 +490,58 @@ function createSessionManager(opts) {
218
490
  // Flush any buffered appends first so we read the full on-disk state.
219
491
  flushSessionBuffer(session);
220
492
 
221
- var filePath = sessionFilePath(session.cliSessionId);
222
- var content;
223
- try { content = fs.readFileSync(filePath, "utf8"); } catch { content = ""; }
224
- var lines = content.trim().split("\n");
225
- var history = [];
226
- var messageUUIDs = [];
227
- // Skip line 0 — it's the meta record, already loaded at startup.
228
- for (var j = 1; j < lines.length; j++) {
229
- if (!lines[j]) continue;
230
- var entry;
231
- try { entry = JSON.parse(lines[j]); } catch { continue; }
232
- if (entry.type === "message_uuid") {
233
- messageUUIDs.push({ uuid: entry.uuid, type: entry.messageType, historyIndex: history.length });
234
- }
235
- history.push(entry);
236
- }
237
- session.history = history;
238
- session.messageUUIDs = messageUUIDs;
493
+ var parsed = _parseSessionFileLines(session);
494
+ var allLines = parsed.lines;
495
+ var skipCount = Math.max(0, allLines.length - HISTORY_INMEM_TRIM_TO);
496
+ session.history = allLines.slice(skipCount);
497
+ session._historyBaseIndex = skipCount;
498
+ session.messageUUIDs = parsed.messageUUIDs;
239
499
  session._historyLoaded = true;
500
+ // lr-f940 (N1): freshly loaded from disk — history exactly matches what
501
+ // is durably persisted, so a subsequent metadata-only save can safely
502
+ // take the meta-only fast path until something changes history.length.
503
+ // lr-2ea2a7: persisted length is the ABSOLUTE on-disk count, not the
504
+ // (possibly tail-trimmed) heap length — see historyMatchesDisk().
505
+ session._historyPersistedLength = allLines.length;
240
506
 
241
507
  _lruTouch(session.localId);
242
508
  _lruEvictIfNeeded();
243
509
  }
244
510
 
511
+ // lr-2ea2a7: cap-exempt full materialization. Used by rewind/fork, which
512
+ // may target ANY point in the conversation — including one older than the
513
+ // current tail-loaded window — and need the complete history in heap to
514
+ // trim/copy/rewrite correctly. Callers MUST pair this with retrimHistory()
515
+ // once done (or a save/switch), so the heap does not permanently hold an
516
+ // unbounded history after the operation completes.
517
+ function loadFullSessionHistory(session) {
518
+ if (!session.cliSessionId) {
519
+ session._historyLoaded = true;
520
+ return;
521
+ }
522
+ flushSessionBuffer(session);
523
+ var parsed = _parseSessionFileLines(session);
524
+ session.history = parsed.lines;
525
+ session._historyBaseIndex = 0;
526
+ session.messageUUIDs = parsed.messageUUIDs;
527
+ session._historyLoaded = true;
528
+ session._historyPersistedLength = parsed.lines.length;
529
+ _lruTouch(session.localId);
530
+ }
531
+
532
+ // lr-2ea2a7: re-apply the bounded tail cap after a loadFullSessionHistory()
533
+ // call (or any other operation that grew the heap array past the cap
534
+ // outside the normal recordHistoryEntry() append path — e.g. rewind's
535
+ // truncation or fork's wholesale copy can each still legally exceed the
536
+ // cap if the pre-trim conversation itself was long). Idempotent / no-op
537
+ // when history is already within bounds.
538
+ function retrimHistory(session) {
539
+ if (session.history.length <= HISTORY_INMEM_MAX) return;
540
+ var trimCount = session.history.length - HISTORY_INMEM_TRIM_TO;
541
+ session.history.splice(0, trimCount);
542
+ session._historyBaseIndex = (session._historyBaseIndex || 0) + trimCount;
543
+ }
544
+
245
545
  function loadSessions() {
246
546
  var files;
247
547
  try { files = fs.readdirSync(sessionsDir); } catch { return; }
@@ -306,6 +606,11 @@ function createSessionManager(opts) {
306
606
  // History is not loaded yet; populated lazily by loadSessionHistory().
307
607
  history: [],
308
608
  _historyLoaded: false,
609
+ // lr-2ea2a7: count of entries trimmed off the head of the in-heap
610
+ // history array; absolute index = _historyBaseIndex + heapOffset.
611
+ // Set for real by loadSessionHistory() once history is actually
612
+ // loaded (tail-only load skips this many entries from disk).
613
+ _historyBaseIndex: 0,
309
614
  messageUUIDs: [],
310
615
  lastRewindUuid: m.lastRewindUuid || null,
311
616
  // File mode already correct on disk — skip chmod on first append.
@@ -425,6 +730,7 @@ function createSessionManager(opts) {
425
730
  lastActivity: Date.now(),
426
731
  history: [],
427
732
  _historyLoaded: true, // new in-memory session — nothing to load from disk
733
+ _historyBaseIndex: 0, // lr-2ea2a7: nothing trimmed yet
428
734
  messageUUIDs: [],
429
735
  ownerId: (sessionOpts && sessionOpts.ownerId) || null,
430
736
  sessionVisibility: (sessionOpts && sessionOpts.sessionVisibility) || "shared",
@@ -459,6 +765,7 @@ function createSessionManager(opts) {
459
765
  lastActivity: Date.now(),
460
766
  history: [],
461
767
  _historyLoaded: true, // new in-memory session — nothing to load from disk
768
+ _historyBaseIndex: 0, // lr-2ea2a7: nothing trimmed yet
462
769
  messageUUIDs: [],
463
770
  ownerId: (sessionOpts && sessionOpts.ownerId) || null,
464
771
  sessionVisibility: (sessionOpts && sessionOpts.sessionVisibility) || "shared",
@@ -540,33 +847,47 @@ function createSessionManager(opts) {
540
847
  return from;
541
848
  }
542
849
 
850
+ // fromIndex (when passed) is an ABSOLUTE index per the wire contract — no
851
+ // current caller passes one (see call sites below), but the contract is
852
+ // preserved for future callers. Internally this function works with the
853
+ // heap array (session.history) using HEAP-RELATIVE offsets, translating to
854
+ // absolute only at the boundary (history_meta.total/from and each
855
+ // messageUUIDs-adjacent index the client already tracks) — lr-2ea2a7.
543
856
  function replayHistory(session, fromIndex, targetWs, transform) {
544
857
  // Ensure history is in memory before replaying.
545
858
  loadSessionHistory(session);
546
859
 
547
860
  var _send = (targetWs && sendTo) ? function (obj) { sendTo(targetWs, obj); } : send;
548
- var total = session.history.length;
549
- if (typeof fromIndex !== "number") {
550
- if (total <= HISTORY_PAGE_SIZE) {
551
- fromIndex = 0;
552
- } else {
553
- // Start from the most recent complete turn within the PAGE_SIZE window.
554
- // max() ensures we never go past the hard floor even if the last turn
555
- // start somehow lands below it.
556
- var hardFloor = Math.max(0, total - HISTORY_PAGE_SIZE);
557
- var lastTurn = findLastTurnStart(session.history);
558
- fromIndex = Math.max(hardFloor, lastTurn);
559
- // Same yield guarantee as load_more_history (lr-c24b): a turn
560
- // dominated by invisible-yield events (todo/task bookkeeping, hidden
561
- // plan tools, thinking deltas) can otherwise replay zero visible
562
- // bubbles on initial resume.
563
- fromIndex = extendWindowForVisibility(session.history, fromIndex, total);
564
- }
861
+ var baseIndex = session._historyBaseIndex || 0;
862
+ var heapLen = session.history.length;
863
+ var total = baseIndex + heapLen; // absolute total per the wire contract
864
+ var heapFrom;
865
+ if (typeof fromIndex === "number") {
866
+ // Caller-supplied fromIndex is absolute; translate to heap-relative and
867
+ // clamp to the trimmed head (a caller cannot request replay from below
868
+ // baseIndex — that range no longer exists in heap; see
869
+ // load_more_history in project-sessions.js for the disk-backed path
870
+ // that serves ranges below baseIndex).
871
+ heapFrom = Math.max(0, fromIndex - baseIndex);
872
+ } else if (heapLen <= HISTORY_PAGE_SIZE) {
873
+ heapFrom = 0;
874
+ } else {
875
+ // Start from the most recent complete turn within the PAGE_SIZE window.
876
+ // max() ensures we never go past the hard floor even if the last turn
877
+ // start somehow lands below it.
878
+ var hardFloor = Math.max(0, heapLen - HISTORY_PAGE_SIZE);
879
+ var lastTurn = findLastTurnStart(session.history);
880
+ heapFrom = Math.max(hardFloor, lastTurn);
881
+ // Same yield guarantee as load_more_history (lr-c24b): a turn
882
+ // dominated by invisible-yield events (todo/task bookkeeping, hidden
883
+ // plan tools, thinking deltas) can otherwise replay zero visible
884
+ // bubbles on initial resume.
885
+ heapFrom = extendWindowForVisibility(session.history, heapFrom, heapLen);
565
886
  }
566
887
 
567
- _send({ type: "history_meta", total: total, from: fromIndex });
888
+ _send({ type: "history_meta", total: total, from: baseIndex + heapFrom });
568
889
 
569
- for (var i = fromIndex; i < total; i++) {
890
+ for (var i = heapFrom; i < heapLen; i++) {
570
891
  var _item = session.history[i];
571
892
  // Skip internal bookkeeping entries not meant for the UI
572
893
  if (_item && _item.type === "digest_checkpoint") continue;
@@ -578,7 +899,7 @@ function createSessionManager(opts) {
578
899
  var lastModelUsage = null;
579
900
  var lastCost = null;
580
901
  var lastStreamInputTokens = null;
581
- for (var j = total - 1; j >= 0; j--) {
902
+ for (var j = heapLen - 1; j >= 0; j--) {
582
903
  if (session.history[j].type === "result") {
583
904
  var r = session.history[j];
584
905
  lastUsage = r.usage || null;
@@ -818,9 +1139,14 @@ function createSessionManager(opts) {
818
1139
  // If history has not been loaded from disk yet, do not trigger a load just
819
1140
  // to append — the append path writes directly to the file buffer. History
820
1141
  // will be loaded lazily on the next switchSession/replayHistory call.
821
- if (session._historyLoaded) {
822
- session.history.push(obj);
823
- }
1142
+ // lr-f940 (N1): this entry is queued for a durable append (below) that
1143
+ // saveSessionFile's flushSessionBuffer() call always drains before
1144
+ // checking historyMatchesDisk() — safe to count it as persisted now so
1145
+ // a later metadata-only save can still take the meta-only fast path.
1146
+ // lr-2ea2a7: routes through recordHistoryEntry() so this session — even
1147
+ // when isProcessing keeps it permanently exempt from LRU eviction below —
1148
+ // still has its in-heap history bounded to a tail window.
1149
+ recordHistoryEntry(session, obj, true);
824
1150
  appendToSessionFile(session, obj);
825
1151
  if (sendEach) {
826
1152
  // Multi-user: send to clients whose active session matches this one
@@ -892,6 +1218,10 @@ function createSessionManager(opts) {
892
1218
  lastActivity: Date.now(),
893
1219
  history: cliHistory,
894
1220
  _historyLoaded: true, // caller provided history directly
1221
+ // lr-2ea2a7: resumeSession's cliHistory is caller-provided full history
1222
+ // (readCliSessionHistory / fork's forkHistory slice) with absolute
1223
+ // index 0 as its own start — no entries have been trimmed off it yet.
1224
+ _historyBaseIndex: 0,
895
1225
  messageUUIDs: [],
896
1226
  bookmarked: false,
897
1227
  favoriteOrder: null,
@@ -919,6 +1249,22 @@ function createSessionManager(opts) {
919
1249
  activeSessionId = mostRecent.localId;
920
1250
  }
921
1251
 
1252
+ // lr-2ea2a7: read a session's FULL history straight from disk without
1253
+ // touching any in-memory session state (session.history, _historyLoaded,
1254
+ // messageUUIDs, LRU order). Used by search, which previously called
1255
+ // loadSessionHistory() and so permanently loaded full history for up to 50
1256
+ // sessions into heap on every content search — itself an unbounded-growth
1257
+ // path this fix closes, not just the isProcessing leak. For a session
1258
+ // whose history IS already loaded, disk is still queried directly (rather
1259
+ // than reading session.history) so search results are identical regardless
1260
+ // of a session's current load state — no special-casing needed here, and
1261
+ // no risk of a stale/trimmed heap view producing incomplete search hits.
1262
+ function readSessionHistoryFromDisk(session) {
1263
+ if (!session.cliSessionId) return [];
1264
+ flushSessionBuffer(session);
1265
+ return _parseSessionFileLines(session).lines;
1266
+ }
1267
+
922
1268
  function searchSessions(query) {
923
1269
  if (!query) return [];
924
1270
  var q = query.toLowerCase();
@@ -927,13 +1273,12 @@ function createSessionManager(opts) {
927
1273
  var titleMatch = (session.title || "New Session").toLowerCase().indexOf(q) !== -1;
928
1274
  var contentMatch = false;
929
1275
  if (titleMatch) {
930
- // Skip content search when title already matches — avoids loading history
931
- // for sessions that are guaranteed to appear in results anyway.
1276
+ // Skip content search when title already matches — avoids a disk
1277
+ // read for sessions that are guaranteed to appear in results anyway.
932
1278
  } else {
933
- // Lazy-load history to enable content search.
934
- loadSessionHistory(session);
935
- for (var i = 0; i < session.history.length; i++) {
936
- var entry = session.history[i];
1279
+ var diskHistory = readSessionHistoryFromDisk(session);
1280
+ for (var i = 0; i < diskHistory.length; i++) {
1281
+ var entry = diskHistory[i];
937
1282
  if ((entry.type === "delta" || entry.type === "user_message" || entry.type === "mention_user" || entry.type === "mention_response") && entry.text) {
938
1283
  if (entry.text.toLowerCase().indexOf(q) !== -1) {
939
1284
  contentMatch = true;
@@ -962,12 +1307,14 @@ function createSessionManager(opts) {
962
1307
  var session = sessions.get(localId);
963
1308
  if (!session) return { hits: [], total: 0 };
964
1309
 
965
- // Lazy-load history for the target session before searching.
966
- loadSessionHistory(session);
1310
+ // lr-2ea2a7: streaming disk scan — does NOT call loadSessionHistory() and
1311
+ // does NOT mutate session.history/_historyLoaded/LRU state. historyIndex
1312
+ // values below are already absolute (disk line order), matching the
1313
+ // wire contract with no translation needed.
1314
+ var history = readSessionHistoryFromDisk(session);
967
1315
 
968
1316
  var q = query.toLowerCase();
969
1317
  var qLen = query.length;
970
- var history = session.history;
971
1318
  var hits = [];
972
1319
 
973
1320
  // Assistant turns can consist of many streaming deltas (especially Codex,
@@ -1147,6 +1494,21 @@ function createSessionManager(opts) {
1147
1494
  saveSessionFile: saveSessionFile,
1148
1495
  appendToSessionFile: appendToSessionFile,
1149
1496
  loadSessionHistory: loadSessionHistory,
1497
+ // lr-2ea2a7: cap-exempt full materialization + re-trim, for rewind/fork
1498
+ // consumers in project-sessions.js that may target a point older than
1499
+ // the current tail-loaded window.
1500
+ loadFullSessionHistory: loadFullSessionHistory,
1501
+ retrimHistory: retrimHistory,
1502
+ // lr-2ea2a7: the single choke point every session.history.push() site
1503
+ // must route through — see the grep-guard test.
1504
+ recordHistoryEntry: recordHistoryEntry,
1505
+ HISTORY_INMEM_MAX: HISTORY_INMEM_MAX,
1506
+ HISTORY_INMEM_TRIM_TO: HISTORY_INMEM_TRIM_TO,
1507
+ LRU_HISTORY_LIMIT: LRU_HISTORY_LIMIT,
1508
+ // lr-5e70: pressure-driven bulk eviction consumed by lib/memory-shed.js
1509
+ // on a MemoryHigh watermark crossing — see forceEvictToLimit above for
1510
+ // why this is distinct from the normal one-at-a-time _lruEvictIfNeeded.
1511
+ forceEvictToLimit: forceEvictToLimit,
1150
1512
  flushSessionBuffer: flushSessionBuffer,
1151
1513
  sendAndRecord: doSendAndRecord,
1152
1514
  sendToSession: doSendToSession,
@@ -1156,6 +1518,29 @@ function createSessionManager(opts) {
1156
1518
  replayHistory: replayHistory,
1157
1519
  searchSessions: searchSessions,
1158
1520
  searchSessionContent: searchSessionContent,
1521
+ // lr-2ea2a7: streaming disk read with no heap retention — shared by
1522
+ // search internally and exposed for other full-history-scan consumers
1523
+ // (project-filesystem.js's fs_file_history) so they don't have to call
1524
+ // loadSessionHistory()/mutate session state just to scan for a match.
1525
+ readSessionHistoryFromDisk: readSessionHistoryFromDisk,
1526
+ // lr-2ea2a7 observability: per-session heap footprint + top-5 by
1527
+ // heapEntries, consumed by project-sessions.js's process_stats handler
1528
+ // so a future incident is diagnosable without a heap snapshot.
1529
+ getHistoryStats: function () {
1530
+ var perSession = [];
1531
+ sessions.forEach(function (s) {
1532
+ perSession.push({
1533
+ id: s.localId,
1534
+ heapEntries: s.history ? s.history.length : 0,
1535
+ baseIndex: s._historyBaseIndex || 0,
1536
+ isProcessing: !!s.isProcessing,
1537
+ });
1538
+ });
1539
+ var top5 = perSession.slice().sort(function (a, b) {
1540
+ return b.heapEntries - a.heapEntries;
1541
+ }).slice(0, 5);
1542
+ return { sessions: perSession, top5: top5 };
1543
+ },
1159
1544
  setResolveLoopInfo: setResolveLoopInfo,
1160
1545
  migrateSessionTitles: migrateSessionTitles,
1161
1546
  setSessionVisibility: function (localId, visibility) {