@clagentic/console 1.6.0 → 1.7.0-beta.1

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
@@ -9,6 +9,15 @@ var { writeHandoff, buildHandoffPreamble } = require("./loop-handoff");
9
9
 
10
10
  var LOOP_ID_RE = /^loop_[A-Za-z0-9_-]+$/;
11
11
 
12
+ // lr-7025: pendingUserMessages is unbounded — handleLoopMessage() pushed to
13
+ // it with no cap on queue length or per-message size, and saveLoopState()
14
+ // persists the growing array to disk on every push. These caps bound both
15
+ // in-memory growth and the on-disk state file. Chosen policy: reject (not
16
+ // drop-oldest) once the queue is full, so a caller retrying doesn't silently
17
+ // lose an earlier message the loop hasn't seen yet.
18
+ var LOOP_MESSAGE_MAX_QUEUE_LENGTH = 20;
19
+ var LOOP_MESSAGE_MAX_TEXT_LENGTH = 4000;
20
+
12
21
  /**
13
22
  * Validate a loop ID before using it as a path component.
14
23
  *
@@ -69,6 +78,12 @@ function attachLoop(ctx) {
69
78
  // Lets stopLoop() snapshot the identity at stop-time and guard its
70
79
  // fallback timer against a concurrently restarted loop. (lr-e823)
71
80
  _loopSeq: 0,
81
+ // Human messages sent while an iteration is in flight, queued for
82
+ // delivery at the next iteration boundary (lr-e31b). Each entry:
83
+ // { text, from, fromName, ts }. Cleared once consumed by
84
+ // runNextIteration(), and on stop/finish so nothing leaks into a
85
+ // future run.
86
+ pendingUserMessages: [],
72
87
  };
73
88
 
74
89
  function loopDir() {
@@ -101,6 +116,7 @@ function attachLoop(ctx) {
101
116
  startedAt: loopState.startedAt,
102
117
  loopId: loopState.loopId,
103
118
  loopFilesId: loopState.loopFilesId || null,
119
+ pendingUserMessages: loopState.pendingUserMessages || [],
104
120
  };
105
121
  store.writeJsonAt(_loopStatePath, data).catch(function (err) {
106
122
  console.error("[ralph-loop] Failed to save state:", err && err.message ? err.message : err);
@@ -121,6 +137,7 @@ function attachLoop(ctx) {
121
137
  loopState.startedAt = data.startedAt || null;
122
138
  loopState.loopId = data.loopId || null;
123
139
  loopState.loopFilesId = data.loopFilesId || null;
140
+ loopState.pendingUserMessages = Array.isArray(data.pendingUserMessages) ? data.pendingUserMessages : [];
124
141
  // SDK sessions cannot survive daemon restart
125
142
  loopState.currentSessionId = null;
126
143
  loopState.judgeSessionId = null;
@@ -191,6 +208,7 @@ function attachLoop(ctx) {
191
208
  loopState.startedAt = null;
192
209
  loopState.loopId = null;
193
210
  loopState.loopFilesId = null;
211
+ loopState.pendingUserMessages = [];
194
212
  saveLoopState();
195
213
  }
196
214
 
@@ -264,6 +282,10 @@ function attachLoop(ctx) {
264
282
  saveLoopState();
265
283
 
266
284
  // Parse recommended title from crafting session conversation
285
+ // lr-2ea2a7: backward scan over the heap tail — crafting sessions are
286
+ // short single-turn conversations that never approach HISTORY_INMEM_MAX,
287
+ // so this always sees the full conversation; tolerant of a trimmed head
288
+ // regardless (a title marker in trimmed content would just not be found).
267
289
  if (loopState.craftingSessionId && loopState.loopId) {
268
290
  var craftSess = sm.sessions.get(loopState.craftingSessionId);
269
291
  if (craftSess && craftSess.history) {
@@ -419,6 +441,7 @@ function attachLoop(ctx) {
419
441
  loopState.name = loopOpts.name || null;
420
442
  loopState.settings = loopConfig.settings || null;
421
443
  loopState.startedAt = Date.now();
444
+ loopState.pendingUserMessages = [];
422
445
  saveLoopState();
423
446
 
424
447
  stopClaudeDirWatch();
@@ -467,6 +490,9 @@ function attachLoop(ctx) {
467
490
  console.log("[ralph-loop] Coder #" + loopState.iteration + " onQueryComplete fired, history length: " + completedSession.history.length);
468
491
  if (!loopState.active) { console.log("[ralph-loop] Coder: loopState.active is false, skipping"); return; }
469
492
  // Check if session ended with error
493
+ // lr-2ea2a7: slice(-3) on the heap tail — tolerant of a trimmed head by
494
+ // construction (only ever wants the LAST 3 entries, which are always
495
+ // in the retained tail window regardless of how much was trimmed).
470
496
  var lastItems = completedSession.history.slice(-3);
471
497
  var hadError = false;
472
498
  for (var i = 0; i < lastItems.length; i++) {
@@ -536,8 +562,39 @@ function attachLoop(ctx) {
536
562
  ? _handoffPreamble + loopState.promptText
537
563
  : loopState.promptText;
538
564
 
565
+ // lr-e31b: Deliver human messages queued during the prior iteration at this
566
+ // boundary. Ephemeral loop-iteration sessions are single-turn (their SDK
567
+ // input stream is closed immediately after the first push — see
568
+ // yoke/adapters/claude.js endInput()), so mid-flight injection into a live
569
+ // iteration is not viable without a broader architecture change. Boundary
570
+ // delivery reuses the same session.history / user_message path a normal
571
+ // chat message takes, so the human's text is visible in the transcript and
572
+ // the coder session actually reads it (folded into the iteration prompt).
573
+ var _queuedMsgs = loopState.pendingUserMessages || [];
574
+ if (_queuedMsgs.length > 0) {
575
+ var _queuedBlock = ["## Message from user (sent during the previous iteration)", ""];
576
+ for (var _qi = 0; _qi < _queuedMsgs.length; _qi++) {
577
+ var _qm = _queuedMsgs[_qi];
578
+ _queuedBlock.push(_qm.text);
579
+ // Record as its own history entry so the transcript shows exactly what
580
+ // the human sent and when, distinct from the coder's own prompt text.
581
+ var _qUserMsg = { type: "user_message", text: _qm.text, _ts: _qm.ts || Date.now() };
582
+ if (_qm.from) { _qUserMsg.from = _qm.from; _qUserMsg.fromName = _qm.fromName || ""; }
583
+ // lr-2ea2a7: routes through the shared history-cap helper (see
584
+ // grep-guard test) — Ralph/crew loop sessions are exactly the
585
+ // long-running isProcessing sessions this fix bounds.
586
+ sm.recordHistoryEntry(session, _qUserMsg, true);
587
+ sm.appendToSessionFile(session, _qUserMsg);
588
+ }
589
+ _queuedBlock.push("", "---", "");
590
+ _iterationPrompt = _queuedBlock.join("\n") + _iterationPrompt;
591
+ loopState.pendingUserMessages = [];
592
+ saveLoopState();
593
+ send({ type: "loop_pending_messages", messages: [] });
594
+ }
595
+
539
596
  var userMsg = { type: "user_message", text: _iterationPrompt };
540
- session.history.push(userMsg);
597
+ sm.recordHistoryEntry(session, userMsg, true);
541
598
  sm.appendToSessionFile(session, userMsg);
542
599
 
543
600
  session.isProcessing = true;
@@ -660,7 +717,8 @@ function attachLoop(ctx) {
660
717
  }, 10 * 60 * 1000);
661
718
 
662
719
  var userMsg = { type: "user_message", text: judgePrompt };
663
- judgeSession.history.push(userMsg);
720
+ // lr-2ea2a7: routes through the shared history-cap helper (see grep-guard test).
721
+ sm.recordHistoryEntry(judgeSession, userMsg, true);
664
722
  sm.appendToSessionFile(judgeSession, userMsg);
665
723
 
666
724
  judgeSession.isProcessing = true;
@@ -672,6 +730,10 @@ function attachLoop(ctx) {
672
730
  sdk.startQuery(judgeSession, judgePrompt, undefined, getLinuxUserForSession(judgeSession));
673
731
  }
674
732
 
733
+ // lr-2ea2a7: forward-accumulates delta/text across the FULL heap array —
734
+ // safe because judge sessions are singleTurn (project-loop.js's
735
+ // runJudge()) and short, never approaching HISTORY_INMEM_MAX; the heap
736
+ // array is always the complete judge conversation, never trimmed.
675
737
  function parseJudgeVerdict(session) {
676
738
  var text = "";
677
739
  for (var i = 0; i < session.history.length; i++) {
@@ -715,6 +777,10 @@ function attachLoop(ctx) {
715
777
  loopState.stopping = false;
716
778
  loopState.currentSessionId = null;
717
779
  loopState.judgeSessionId = null;
780
+ // Drop any messages queued but never delivered to an iteration — the loop
781
+ // is done, so there is no future boundary to deliver them at (lr-e31b).
782
+ var _hadUndelivered = loopState.pendingUserMessages && loopState.pendingUserMessages.length > 0;
783
+ loopState.pendingUserMessages = [];
718
784
  saveLoopState();
719
785
 
720
786
  send({
@@ -723,6 +789,9 @@ function attachLoop(ctx) {
723
789
  iterations: loopState.iteration,
724
790
  results: loopState.results,
725
791
  });
792
+ if (_hadUndelivered) {
793
+ send({ type: "loop_pending_messages", messages: [] });
794
+ }
726
795
 
727
796
  // Record result in loop registry
728
797
  if (loopState.loopId) {
@@ -882,6 +951,69 @@ function attachLoop(ctx) {
882
951
  return true;
883
952
  }
884
953
 
954
+ // lr-e31b: Queue a human message for delivery at the next iteration
955
+ // boundary. See the comment in runNextIteration() for why this is
956
+ // boundary-delivered rather than injected into the live iteration.
957
+ if (msg.type === "loop_message") {
958
+ if (!loopState.active) {
959
+ sendTo(ws, { type: "loop_message_error", text: "No loop is currently running." });
960
+ return true;
961
+ }
962
+ var _lmText = (msg.text || "").trim();
963
+ if (!_lmText) return true;
964
+ if (_lmText.length > LOOP_MESSAGE_MAX_TEXT_LENGTH) {
965
+ sendTo(ws, {
966
+ type: "loop_message_error",
967
+ text: "Message too long (" + _lmText.length + " chars, max " + LOOP_MESSAGE_MAX_TEXT_LENGTH + "). Not queued.",
968
+ });
969
+ return true;
970
+ }
971
+ if (loopState.pendingUserMessages.length >= LOOP_MESSAGE_MAX_QUEUE_LENGTH) {
972
+ sendTo(ws, {
973
+ type: "loop_message_error",
974
+ text: "Message queue is full (max " + LOOP_MESSAGE_MAX_QUEUE_LENGTH + " pending). Wait for the next iteration boundary before sending more.",
975
+ });
976
+ return true;
977
+ }
978
+ // lr-4a9c: stable per-message id so a queued message can be targeted
979
+ // for removal (loop_message_remove below) independent of its array
980
+ // position, which shifts as other messages are queued/delivered.
981
+ var _lmEntry = { id: crypto.randomBytes(8).toString("hex"), text: _lmText, ts: Date.now() };
982
+ if (ws && ws._clayUser) {
983
+ _lmEntry.from = ws._clayUser.id;
984
+ _lmEntry.fromName = ws._clayUser.displayName || ws._clayUser.username || "";
985
+ }
986
+ loopState.pendingUserMessages.push(_lmEntry);
987
+ saveLoopState();
988
+ send({ type: "loop_pending_messages", messages: loopState.pendingUserMessages });
989
+ return true;
990
+ }
991
+
992
+ // lr-4a9c: Remove a single queued message before it is delivered at the
993
+ // next iteration boundary. Mirrors loop_message's validation shape and
994
+ // reuses the same pendingUserMessages / loop_pending_messages plumbing —
995
+ // no parallel queue or broadcast mechanism.
996
+ if (msg.type === "loop_message_remove") {
997
+ if (!loopState.active) {
998
+ sendTo(ws, { type: "loop_message_error", text: "No loop is currently running." });
999
+ return true;
1000
+ }
1001
+ var _lmrId = msg.id;
1002
+ if (!_lmrId) {
1003
+ sendTo(ws, { type: "loop_message_error", text: "No message id given to remove." });
1004
+ return true;
1005
+ }
1006
+ var _lmrIdx = loopState.pendingUserMessages.findIndex(function (m) { return m.id === _lmrId; });
1007
+ if (_lmrIdx === -1) {
1008
+ sendTo(ws, { type: "loop_message_error", text: "Queued message not found (already delivered or removed)." });
1009
+ return true;
1010
+ }
1011
+ loopState.pendingUserMessages.splice(_lmrIdx, 1);
1012
+ saveLoopState();
1013
+ send({ type: "loop_pending_messages", messages: loopState.pendingUserMessages });
1014
+ return true;
1015
+ }
1016
+
885
1017
  if (msg.type === "ralph_wizard_complete") {
886
1018
  var wData = msg.data || {};
887
1019
  var maxIter = wData.maxIterations || null;
@@ -971,9 +1103,15 @@ function attachLoop(ctx) {
971
1103
 
972
1104
  startClaudeDirWatch();
973
1105
 
974
- judgeCraftSession.history.push({ type: "user_message", text: judgeCraftPrompt });
975
- sm.appendToSessionFile(judgeCraftSession, { type: "user_message", text: judgeCraftPrompt });
976
- sendToSession(judgeCraftSession.localId, { type: "user_message", text: judgeCraftPrompt });
1106
+ // lr-2ea2a7: routes through the shared history-cap helper (see
1107
+ // grep-guard test); a single object instance now backs both the
1108
+ // heap entry and the durable append (previously two separate
1109
+ // literals — harmless before, but recordHistoryEntry mutates the
1110
+ // object it's given, so a shared reference is the correct shape).
1111
+ var judgeCraftUserMsg = { type: "user_message", text: judgeCraftPrompt };
1112
+ sm.recordHistoryEntry(judgeCraftSession, judgeCraftUserMsg, true);
1113
+ sm.appendToSessionFile(judgeCraftSession, judgeCraftUserMsg);
1114
+ sendToSession(judgeCraftSession.localId, judgeCraftUserMsg);
977
1115
  judgeCraftSession.isProcessing = true;
978
1116
  onProcessingChanged();
979
1117
  judgeCraftSession.sentToolResults = {};
@@ -1044,9 +1182,13 @@ function attachLoop(ctx) {
1044
1182
  startClaudeDirWatch();
1045
1183
 
1046
1184
  // Send crafting prompt and start the conversation with Claude.
1047
- craftingSession.history.push({ type: "user_message", text: craftingPrompt });
1048
- sm.appendToSessionFile(craftingSession, { type: "user_message", text: craftingPrompt });
1049
- sendToSession(craftingSession.localId, { type: "user_message", text: craftingPrompt });
1185
+ // lr-2ea2a7: routes through the shared history-cap helper (see
1186
+ // grep-guard test); single object instance shared across the heap
1187
+ // entry, the durable append, and the client send.
1188
+ var craftingUserMsg = { type: "user_message", text: craftingPrompt };
1189
+ sm.recordHistoryEntry(craftingSession, craftingUserMsg, true);
1190
+ sm.appendToSessionFile(craftingSession, craftingUserMsg);
1191
+ sendToSession(craftingSession.localId, craftingUserMsg);
1050
1192
  craftingSession.isProcessing = true;
1051
1193
  onProcessingChanged();
1052
1194
  craftingSession.sentToolResults = {};
@@ -1371,6 +1513,12 @@ function attachLoop(ctx) {
1371
1513
  name: loopState.name || null,
1372
1514
  });
1373
1515
 
1516
+ // lr-e31b: Restore the queued-message indicator on reconnect so a
1517
+ // client that joins mid-iteration sees what is already queued.
1518
+ if (loopState.active && loopState.pendingUserMessages && loopState.pendingUserMessages.length > 0) {
1519
+ sendTo(ws, { type: "loop_pending_messages", messages: loopState.pendingUserMessages });
1520
+ }
1521
+
1374
1522
  // Ralph phase state
1375
1523
  // Derive source from wizardData for reconnect (so client can distinguish ralph vs task)
1376
1524
  var _connSource = loopState.wizardData ? (loopState.wizardData.source || null) : null;
@@ -1418,4 +1566,9 @@ function attachLoop(ctx) {
1418
1566
  };
1419
1567
  }
1420
1568
 
1421
- module.exports = { attachLoop: attachLoop, validateLoopId: validateLoopId };
1569
+ module.exports = {
1570
+ attachLoop: attachLoop,
1571
+ validateLoopId: validateLoopId,
1572
+ LOOP_MESSAGE_MAX_QUEUE_LENGTH: LOOP_MESSAGE_MAX_QUEUE_LENGTH,
1573
+ LOOP_MESSAGE_MAX_TEXT_LENGTH: LOOP_MESSAGE_MAX_TEXT_LENGTH,
1574
+ };
@@ -120,16 +120,41 @@ function attachSessions(ctx) {
120
120
  if (!session || typeof msg.before !== "number") return true;
121
121
  var before = msg.before;
122
122
  var targetFrom = typeof msg.target === "number" ? msg.target : before - sm.HISTORY_PAGE_SIZE;
123
- var from = sm.findTurnBoundary(session.history, Math.max(0, targetFrom));
124
- // Extend backward turn-boundary by turn-boundary (bounded — see
125
- // extendWindowForVisibility) until the page contains at least one
126
- // visibly-rendering event. Otherwise a page landing entirely on
127
- // invisible-yield events (todo/task bookkeeping, hidden plan tools,
128
- // state events, thinking deltas) advances historyFrom but renders
129
- // nothing — "Load earlier" appears to do nothing (lr-c24b).
130
- from = sm.extendWindowForVisibility(session.history, from, before);
131
- var to = before;
132
- var items = session.history.slice(from, to).map(hydrateImageRefs);
123
+ var baseIndex = session._historyBaseIndex || 0;
124
+ // lr-2ea2a7: `before`/`targetFrom` are ABSOLUTE indices per the wire
125
+ // contract (client only ever sees absolute indices from history_meta /
126
+ // a prior history_prepend). Once the heap array can be trimmed, this
127
+ // window can land at or above baseIndex (served from heap, translating
128
+ // absolute<->heap-relative around the existing turn-boundary helpers)
129
+ // or below it (no longer in heap — bounded disk read of the full
130
+ // on-disk history, using the SAME turn-boundary/visibility helpers
131
+ // against that array with absolute indices directly, since index 0 of
132
+ // a fresh disk read IS absolute index 0). Either path is a rare
133
+ // user-initiated scroll-up action — an O(file) disk read is acceptable
134
+ // here and does not retain anything in session.history/heap state.
135
+ var from, to, items;
136
+ if (Math.max(0, targetFrom) >= baseIndex) {
137
+ var heapTargetFrom = Math.max(0, targetFrom) - baseIndex;
138
+ var heapBefore = before - baseIndex;
139
+ var heapFrom = sm.findTurnBoundary(session.history, heapTargetFrom);
140
+ // Extend backward turn-boundary by turn-boundary (bounded — see
141
+ // extendWindowForVisibility) until the page contains at least one
142
+ // visibly-rendering event. Otherwise a page landing entirely on
143
+ // invisible-yield events (todo/task bookkeeping, hidden plan tools,
144
+ // state events, thinking deltas) advances historyFrom but renders
145
+ // nothing — "Load earlier" appears to do nothing (lr-c24b).
146
+ heapFrom = sm.extendWindowForVisibility(session.history, heapFrom, heapBefore);
147
+ from = baseIndex + heapFrom;
148
+ to = before;
149
+ items = session.history.slice(heapFrom, heapBefore).map(hydrateImageRefs);
150
+ } else {
151
+ var diskHistory = sm.readSessionHistoryFromDisk(session);
152
+ var diskFrom = sm.findTurnBoundary(diskHistory, Math.max(0, targetFrom));
153
+ diskFrom = sm.extendWindowForVisibility(diskHistory, diskFrom, before);
154
+ from = diskFrom;
155
+ to = before;
156
+ items = diskHistory.slice(diskFrom, before).map(hydrateImageRefs);
157
+ }
133
158
  sendTo(ws, {
134
159
  type: "history_prepend",
135
160
  items: items,
@@ -521,11 +546,6 @@ function attachSessions(ctx) {
521
546
  return true;
522
547
  }
523
548
 
524
- if (msg.type === "set_mate_dm") {
525
- // set_mate_dm is a no-op stub (mate feature removed, lr-316f)
526
- return true;
527
- }
528
-
529
549
  if (msg.type === "delete_session") {
530
550
  if (ws._clayUser) {
531
551
  var sdPerms = usersModule.getEffectivePermissions(ws._clayUser, osUsers);
@@ -909,6 +929,17 @@ function attachSessions(ctx) {
909
929
 
910
930
  // Conversation rollback (skip for files-only mode)
911
931
  if (mode !== "files") {
932
+ // lr-2ea2a7: messageUUIDs[].historyIndex is an ABSOLUTE index and
933
+ // the rewind target may be older than the current heap tail
934
+ // (session._historyBaseIndex > 0) — loadFullSessionHistory()
935
+ // materializes the complete on-disk history into session.history
936
+ // first (cap-exempt), so every index below is safe to use as a
937
+ // direct session.history offset without base-index translation.
938
+ // retrimHistory() re-applies the bounded cap once the trim/save
939
+ // below is done so this session doesn't permanently hold
940
+ // unbounded history in heap after a rewind.
941
+ sm.loadFullSessionHistory(session);
942
+
912
943
  var targetIdx = -1;
913
944
  for (var i = 0; i < session.messageUUIDs.length; i++) {
914
945
  if (session.messageUUIDs[i].uuid === msg.uuid) {
@@ -934,11 +965,16 @@ function attachSessions(ctx) {
934
965
  }
935
966
  }
936
967
  session.history = session.history.slice(0, trimTo);
968
+ session._historyBaseIndex = 0; // full history now starts at absolute 0 again
937
969
  session.messageUUIDs = session.messageUUIDs.slice(0, targetIdx);
938
970
  // Reset digest checkpoint if it points past the trimmed history
939
971
  if (typeof session._dmLastDigestedIndex === "number" && session._dmLastDigestedIndex > trimTo) {
940
972
  session._dmLastDigestedIndex = trimTo;
941
973
  }
974
+ // Note (lr-f940): this truncation changes session.history.length,
975
+ // which sm.saveSessionFile()'s historyMatchesDisk() check detects
976
+ // automatically — the save below still does a full rewrite, no
977
+ // extra bookkeeping needed here.
942
978
  }
943
979
 
944
980
  // Notify adapter of conversation rollback (e.g. Codex thread/rollback)
@@ -952,6 +988,9 @@ function attachSessions(ctx) {
952
988
 
953
989
  var kept = session.messageUUIDs;
954
990
  session.lastRewindUuid = kept.length > 0 ? kept[kept.length - 1].uuid : null;
991
+ // lr-2ea2a7: re-apply the bounded tail cap now that the trim/save
992
+ // is complete — see loadFullSessionHistory() call above.
993
+ sm.retrimHistory(session);
955
994
  }
956
995
 
957
996
  if (session.abortController) {
@@ -993,6 +1032,14 @@ function attachSessions(ctx) {
993
1032
 
994
1033
  sdk.forkSession(session, msg.uuid).then(function(result) {
995
1034
  if (result.useLocalHistory) {
1035
+ // lr-2ea2a7: the fork target (messageUUIDs[].historyIndex, an
1036
+ // ABSOLUTE index) may be older than the current heap tail — load
1037
+ // the complete on-disk history first (cap-exempt) so the source
1038
+ // session's indices are safe to use as direct offsets, and the
1039
+ // forked prefix can include everything up to the target uuid even
1040
+ // if it was already trimmed out of the source session's heap.
1041
+ sm.loadFullSessionHistory(session);
1042
+
996
1043
  // Copy local history up to the target UUID
997
1044
  var targetIdx = -1;
998
1045
  for (var fi = 0; fi < session.messageUUIDs.length; fi++) {
@@ -1005,17 +1052,33 @@ function attachSessions(ctx) {
1005
1052
  } else {
1006
1053
  forkHistory = session.history.slice();
1007
1054
  }
1055
+ // Source session's heap is restored to its bounded cap now that
1056
+ // the full prefix has been copied out for the fork.
1057
+ sm.retrimHistory(session);
1058
+
1008
1059
  var forked = sm.createSession({ vendor: session.vendor, ownerId: session.ownerId || null }, ws);
1009
1060
  forked.cliSessionId = result.sessionId;
1010
1061
  forked.title = forkTitle;
1011
1062
  forked.history = forkHistory;
1063
+ forked._historyBaseIndex = 0; // forked history starts fresh at absolute 0
1012
1064
  forked.messageUUIDs = [];
1013
1065
  for (var hi = 0; hi < forkHistory.length; hi++) {
1014
1066
  if (forkHistory[hi].type === "message_uuid") {
1015
1067
  forked.messageUUIDs.push({ uuid: forkHistory[hi].uuid, type: forkHistory[hi].messageType, historyIndex: hi });
1016
1068
  }
1017
1069
  }
1070
+ // Note (lr-f940): forked.history was just assigned directly, which
1071
+ // sm.saveSessionFile()'s historyMatchesDisk() check detects via the
1072
+ // length mismatch against the freshly-created session's persisted
1073
+ // count — the save below still does a full rewrite automatically.
1074
+ // The FULL forkHistory (pre-trim) is what gets written to disk here
1075
+ // — saveSessionFile() serializes session.history as of this call,
1076
+ // which is still the untrimmed forkHistory; retrimHistory() below
1077
+ // only bounds the in-heap copy after the durable write completes.
1018
1078
  sm.saveSessionFile(forked);
1079
+ // lr-2ea2a7: bound the forked session's heap too — a fork target
1080
+ // deep into a long conversation can itself exceed HISTORY_INMEM_MAX.
1081
+ sm.retrimHistory(forked);
1019
1082
  sm.switchSession(forked.localId, ws, hydrateImageRefs);
1020
1083
  sendTo(ws, { type: "fork_complete", sessionId: forked.localId });
1021
1084
  sm.broadcastSessionList();
@@ -1053,7 +1116,8 @@ function attachSessions(ctx) {
1053
1116
  // any other user input.
1054
1117
  var answerText = formatAskUserAnswerAsMessage(pending.input, answers);
1055
1118
  var userMsg = { type: "user_message", text: answerText };
1056
- session.history.push(userMsg);
1119
+ // lr-2ea2a7: routes through the shared history-cap helper (see grep-guard test).
1120
+ sm.recordHistoryEntry(session, userMsg, true);
1057
1121
  sm.appendToSessionFile(session, userMsg);
1058
1122
  sendToSession(session.localId, userMsg);
1059
1123
 
@@ -1200,7 +1264,8 @@ function attachSessions(ctx) {
1200
1264
  var newSession = sm.createSession(null, ws);
1201
1265
  // Send the plan as the first user message (with planContent for UI rendering)
1202
1266
  var userMsg = { type: "user_message", text: planPrompt, planContent: clientPlanContent || null };
1203
- newSession.history.push(userMsg);
1267
+ // lr-2ea2a7: routes through the shared history-cap helper (see grep-guard test).
1268
+ sm.recordHistoryEntry(newSession, userMsg, true);
1204
1269
  sm.appendToSessionFile(newSession, userMsg);
1205
1270
  newSession.title = "Plan execution (cleared context)";
1206
1271
  sm.saveSessionFile(newSession);
@@ -1233,7 +1298,8 @@ function attachSessions(ctx) {
1233
1298
  if (feedback) {
1234
1299
  setTimeout(function () {
1235
1300
  var userMsg = { type: "user_message", text: feedback };
1236
- session.history.push(userMsg);
1301
+ // lr-2ea2a7: routes through the shared history-cap helper (see grep-guard test).
1302
+ sm.recordHistoryEntry(session, userMsg, true);
1237
1303
  sm.appendToSessionFile(session, userMsg);
1238
1304
  sendToSession(session.localId, userMsg);
1239
1305
 
@@ -1429,14 +1495,14 @@ function attachSessions(ctx) {
1429
1495
  var wtDirName = (msg.dirName || "").trim() || wtBranch.replace(/\//g, "-");
1430
1496
  var wtBase = (msg.baseBranch || "").trim() || null;
1431
1497
  if (!wtBranch || !/^[a-zA-Z0-9_\/.@-]+$/.test(wtBranch)) {
1432
- sendTo(ws, { type: "create_worktree_result", ok: false, error: "Invalid branch name" });
1498
+ sendTo(ws, { type: "create_worktree_result", ok: false, dirName: wtDirName, error: "Invalid branch name" });
1433
1499
  return true;
1434
1500
  }
1435
1501
  if (typeof onCreateWorktree === "function") {
1436
1502
  var wtResult = onCreateWorktree(slug, wtBranch, wtDirName, wtBase);
1437
- sendTo(ws, { type: "create_worktree_result", ok: wtResult.ok, slug: wtResult.slug, error: wtResult.error });
1503
+ sendTo(ws, { type: "create_worktree_result", ok: wtResult.ok, dirName: wtDirName, slug: wtResult.slug, error: wtResult.error });
1438
1504
  } else {
1439
- sendTo(ws, { type: "create_worktree_result", ok: false, error: "Not supported" });
1505
+ sendTo(ws, { type: "create_worktree_result", ok: false, dirName: wtDirName, error: "Not supported" });
1440
1506
  }
1441
1507
  return true;
1442
1508
  }
@@ -127,7 +127,8 @@ function attachUserMention(ctx) {
127
127
  if (msg.pastes && msg.pastes.length > 0) entry.pastes = msg.pastes;
128
128
  if (imageRefs.length > 0) entry.imageRefs = imageRefs;
129
129
 
130
- session.history.push(entry);
130
+ // lr-2ea2a7: routes through the shared history-cap helper (see grep-guard test).
131
+ sm.recordHistoryEntry(session, entry, true);
131
132
  sm.appendToSessionFile(session, entry);
132
133
 
133
134
  // Hydrate image refs for live broadcast (clients want urls, not file names).
@@ -22,7 +22,7 @@ var fs = require("fs");
22
22
  * startFileWatch, stopFileWatch,
23
23
  * scheduleMessage, cancelScheduledMessage,
24
24
  * loadContextSources, saveContextSources,
25
- * digestDmTurn, gateMemory,
25
+ * digestDmTurn,
26
26
  * adapter - YOKE adapter instance
27
27
  */
28
28
  function attachUserMessage(ctx) {
@@ -284,7 +284,10 @@ function attachUserMessage(ctx) {
284
284
  console.log("[project] Scheduled message sent immediately for session " + nowSession.localId);
285
285
  sm.sendAndRecord(nowSession, { type: "scheduled_message_sent" });
286
286
  var userMsg = { type: "user_message", text: schedText, _ts: Date.now() };
287
- nowSession.history.push(userMsg);
287
+ // lr-2ea2a7: routes through the shared history-cap helper (see grep-guard
288
+ // test) instead of pushing directly, so a long-running session's heap
289
+ // history stays bounded even for scheduled-message injection.
290
+ sm.recordHistoryEntry(nowSession, userMsg, true);
288
291
  sm.appendToSessionFile(nowSession, userMsg);
289
292
  sendToSession(nowSession.localId, userMsg);
290
293
  nowSession.isProcessing = true;
@@ -343,7 +346,9 @@ function attachUserMessage(ctx) {
343
346
  userMsg2.pastes = msg.pastes;
344
347
  }
345
348
  if (!userMsg2._ts) userMsg2._ts = Date.now();
346
- session.history.push(userMsg2);
349
+ // lr-2ea2a7: routes through the shared history-cap helper (see grep-guard
350
+ // test) instead of pushing directly.
351
+ sm.recordHistoryEntry(session, userMsg2, true);
347
352
  sm.appendToSessionFile(session, userMsg2);
348
353
  sendToSessionOthers(ws, session.localId, hydrateImageRefs(userMsg2));
349
354
 
@@ -617,7 +622,14 @@ function attachUserMessage(ctx) {
617
622
  screenshotFile: ss.file
618
623
  }
619
624
  };
620
- session.history.push(previewEntry);
625
+ // lr-2ea2a7: routes through the shared history-cap helper (see
626
+ // grep-guard test) instead of pushing directly.
627
+ // Note (lr-f940): this push is not paired with appendToSessionFile
628
+ // (unlike every other .history.push site in this file), so pass
629
+ // persisted=false — sm.saveSessionFile()'s historyMatchesDisk()
630
+ // length check below will detect the mismatch and still do a
631
+ // full rewrite, exactly as before this fix.
632
+ sm.recordHistoryEntry(session, previewEntry, false);
621
633
  // Send context card to all clients
622
634
  sendToSession(session.localId, {
623
635
  type: "context_preview",
@@ -645,7 +657,6 @@ function attachUserMessage(ctx) {
645
657
 
646
658
  return {
647
659
  handleUserMessage: handleUserMessage,
648
- syncNotesKnowledge: syncNotesKnowledge,
649
660
  };
650
661
  }
651
662
 
package/lib/project.js CHANGED
@@ -11,9 +11,7 @@ var { execFileSync, spawn } = require("child_process");
11
11
  var usersModule = require("./users");
12
12
  var { resolveOsUserInfo, fsAsUser, grantProjectAccess } = require("./os-users");
13
13
  var crisisSafety = require("./crisis-safety");
14
- var sessionSearch = require("./session-search");
15
14
  var userPresence = require("./user-presence");
16
- var { attachMemory } = require("./project-memory");
17
15
  var { attachUserMention } = require("./project-user-mention");
18
16
  var { attachLoop } = require("./project-loop");
19
17
  var { attachFileWatch } = require("./project-file-watch");
@@ -182,6 +180,9 @@ function createProjectContext(opts) {
182
180
  var serverPort = opts.port || 2633;
183
181
  var serverTls = opts.tls || false;
184
182
  var serverAuthToken = opts.authToken || null;
183
+ // lr-2ea2a7: daemon.json knob overriding the in-heap session-history tail
184
+ // cap (default HISTORY_INMEM_MAX_DEFAULT=1000 in lib/sessions.js).
185
+ var historyInMemMax = opts.historyInMemMax;
185
186
  var latestVersion = null;
186
187
  var sessionTitleMigrationScheduled = false;
187
188
 
@@ -431,6 +432,7 @@ function createProjectContext(opts) {
431
432
  }
432
433
  },
433
434
  onSessionDone: onSessionDone,
435
+ historyInMemMax: historyInMemMax,
434
436
  });
435
437
  sm.availableVendors = Object.keys(adapters);
436
438
  sm.defaultVendor = defaultVendor;
@@ -518,7 +520,14 @@ function createProjectContext(opts) {
518
520
  c.send(JSON.stringify({ type: "context_sources_state", active: active }));
519
521
  }
520
522
  }
521
- return active;
523
+ // Mirrors watchTab's return value above: this call fans out across
524
+ // every connected client's own per-session context sources, so
525
+ // there is no single "the" active list to report back. The prior
526
+ // code returned the loop-scoped `active` var, which is undefined
527
+ // whenever there are zero connected clients and otherwise just the
528
+ // last-iterated client's list -- neither is meaningful to the
529
+ // caller (lr-1bdb item E). Return a deterministic constant instead.
530
+ return [];
522
531
  },
523
532
  });
524
533
  var mcpConfig = adapter.createToolServer({ name: "clay-browser", version: "1.0.0", tools: browserToolDefs });
@@ -691,7 +700,8 @@ function createProjectContext(opts) {
691
700
  console.log("[project] Scheduled message firing for session " + session.localId);
692
701
  sm.sendAndRecord(session, { type: "scheduled_message_sent" });
693
702
  var schedUserMsg = { type: "user_message", text: text, _ts: Date.now() };
694
- session.history.push(schedUserMsg);
703
+ // lr-2ea2a7: routes through the shared history-cap helper (see grep-guard test).
704
+ sm.recordHistoryEntry(session, schedUserMsg, true);
695
705
  sm.appendToSessionFile(session, schedUserMsg);
696
706
  sendToSession(session.localId, schedUserMsg);
697
707
  session.isProcessing = true;
@@ -805,11 +815,6 @@ function createProjectContext(opts) {
805
815
  // --- Notifications (delegated to project-notifications.js) ---
806
816
  if (_notifications.handleNotificationMessage(ws, msg)) return;
807
817
 
808
- // --- Memory (session digests) management (delegated to project-memory.js) ---
809
- if (msg.type === "memory_list") { _memory.handleMemoryList(ws); return; }
810
- if (msg.type === "memory_search") { _memory.handleMemorySearch(ws, msg); return; }
811
- if (msg.type === "memory_delete") { _memory.handleMemoryDelete(ws, msg); return; }
812
-
813
818
  // --- Sessions, config, project mgmt (delegated to project-sessions.js) ---
814
819
  if (_sessions.handleSessionsMessage(ws, msg)) return;
815
820
 
@@ -826,21 +831,6 @@ function createProjectContext(opts) {
826
831
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
827
832
  }
828
833
 
829
- // --- Memory engine (delegated to project-memory.js) ---
830
- var _memory = attachMemory({
831
- cwd: cwd,
832
- sm: sm,
833
- sdk: sdk,
834
- sendTo: sendTo,
835
- sessionSearch: sessionSearch,
836
- getAllProjectSessions: getAllProjectSessions,
837
- projectOwnerId: projectOwnerId,
838
- handleMessage: handleMessage,
839
- });
840
- var gateMemory = _memory.gateMemory;
841
- var updateMemorySummary = _memory.updateMemorySummary;
842
- var initMemorySummary = _memory.initMemorySummary;
843
-
844
834
  // --- User-to-user mention engine (delegated to project-user-mention.js) ---
845
835
  var _userMention = attachUserMention({
846
836
  slug: slug,
@@ -985,7 +975,6 @@ function createProjectContext(opts) {
985
975
  cancelScheduledMessage: cancelScheduledMessage,
986
976
  loadContextSources: loadContextSources,
987
977
  saveContextSources: saveContextSources,
988
- gateMemory: gateMemory,
989
978
  escapeRegex: escapeRegex,
990
979
  adapter: adapter,
991
980
  getHubSchedules: getHubSchedules,