@causyne/agent 1.20.111 → 1.20.116

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 (151) hide show
  1. package/docs/ARCHITECTURE.md +29 -0
  2. package/package.json +1 -1
  3. package/src/agentLoop.js +72 -37
  4. package/src/agentLoop.ts +70 -38
  5. package/src/browser/browserRuntime.js +13 -8
  6. package/src/browser/browserRuntime.ts +16 -8
  7. package/src/browser/causyneBrowser.js +7 -2
  8. package/src/browser/causyneBrowser.ts +8 -2
  9. package/src/browser/searchProviders.js +18 -7
  10. package/src/browser/searchProviders.ts +17 -5
  11. package/src/browser/snapshotWindow.js +1 -1
  12. package/src/browser/snapshotWindow.ts +1 -1
  13. package/src/browser/urlPolicy.js +21 -10
  14. package/src/browser/urlPolicy.ts +19 -10
  15. package/src/browser/workspacePreview.js +13 -1
  16. package/src/browser/workspacePreview.ts +11 -1
  17. package/src/computerUse/visionGrounding.js +31 -27
  18. package/src/computerUse/visionGrounding.ts +27 -27
  19. package/src/continuum/ui/continuumPicker.js +14 -6
  20. package/src/continuum/ui/continuumPicker.ts +13 -6
  21. package/src/continuum/ui/continuumPickerView.js +24 -15
  22. package/src/continuum/ui/continuumPickerView.ts +24 -16
  23. package/src/observer/advisory.js +32 -22
  24. package/src/observer/advisory.ts +30 -18
  25. package/src/observer/coldObserver.js +21 -33
  26. package/src/observer/coldObserver.ts +20 -33
  27. package/src/observer/evidence.js +20 -13
  28. package/src/observer/evidence.ts +21 -13
  29. package/src/observer.js +47 -12
  30. package/src/observer.ts +50 -18
  31. package/src/pluginsFactory/discovery.js +28 -16
  32. package/src/pluginsFactory/discovery.ts +29 -16
  33. package/src/pluginsFactory/pluginsFactory.js +213 -38
  34. package/src/pluginsFactory/pluginsFactory.ts +218 -39
  35. package/src/pluginsFactory/strategy.js +26 -16
  36. package/src/pluginsFactory/strategy.ts +34 -19
  37. package/src/pluginsFactory/templates.js +183 -62
  38. package/src/pluginsFactory/templates.ts +174 -54
  39. package/src/timeMachine.js +43 -22
  40. package/src/timeMachine.ts +44 -22
  41. package/src/tool-scripts/desktop_act.applescript +1 -0
  42. package/src/tools/computerUse.js +23 -10
  43. package/src/tools/computerUse.ts +19 -8
  44. package/src/tools/desktopAct.js +12 -1
  45. package/src/tools/pluginsFactory.js +108 -5
  46. package/src/tools/pluginsFactory.ts +115 -5
  47. package/src/tui/CausyneRotor.js +24 -10
  48. package/src/tui/CausyneRotor.ts +25 -10
  49. package/src/tui/activityLayout.js +9 -5
  50. package/src/tui/activityLayout.ts +9 -5
  51. package/src/tui/ask.js +34 -14
  52. package/src/tui/banner.js +55 -18
  53. package/src/tui/banner.ts +52 -17
  54. package/src/tui/block.js +47 -20
  55. package/src/tui/block.ts +54 -27
  56. package/src/tui/chatRenderer.js +135 -83
  57. package/src/tui/chatboxClipboard.js +12 -5
  58. package/src/tui/chatboxClipboard.ts +10 -4
  59. package/src/tui/clipboardImage.js +52 -42
  60. package/src/tui/clipboardImage.ts +50 -39
  61. package/src/tui/clipboardSync.js +12 -1
  62. package/src/tui/clipboardSync.ts +9 -1
  63. package/src/tui/codeProgress.js +57 -26
  64. package/src/tui/codeProgress.ts +57 -25
  65. package/src/tui/codeStreamRenderer.js +59 -14
  66. package/src/tui/codeStreamRenderer.ts +58 -14
  67. package/src/tui/commandPresentation.js +9 -2
  68. package/src/tui/commandPresentation.ts +9 -2
  69. package/src/tui/completion.js +45 -23
  70. package/src/tui/composerControls.js +21 -10
  71. package/src/tui/composerControls.ts +18 -10
  72. package/src/tui/composerGeometry.js +6 -4
  73. package/src/tui/composerGeometry.ts +6 -4
  74. package/src/tui/consoleGuard.js +29 -7
  75. package/src/tui/consoleGuard.ts +28 -7
  76. package/src/tui/dialogs.js +134 -71
  77. package/src/tui/diff.js +8 -4
  78. package/src/tui/history.js +6 -5
  79. package/src/tui/home.js +25 -13
  80. package/src/tui/homeView.js +37 -18
  81. package/src/tui/homeView.ts +38 -18
  82. package/src/tui/inlineSlash.js +6 -5
  83. package/src/tui/inlineSlash.ts +6 -5
  84. package/src/tui/keys.js +33 -17
  85. package/src/tui/layout.js +27 -9
  86. package/src/tui/layout.ts +26 -9
  87. package/src/tui/lineEditor.js +49 -23
  88. package/src/tui/markdownMath.js +64 -14
  89. package/src/tui/markdownMath.ts +69 -13
  90. package/src/tui/messageActionPlacement.js +28 -13
  91. package/src/tui/messageActionPlacement.ts +28 -13
  92. package/src/tui/messageInteraction.js +111 -46
  93. package/src/tui/messageInteraction.ts +115 -47
  94. package/src/tui/modelPicker.js +44 -15
  95. package/src/tui/modelPicker.ts +48 -15
  96. package/src/tui/observerPicker.js +56 -13
  97. package/src/tui/observerPicker.ts +73 -30
  98. package/src/tui/overlay.js +79 -35
  99. package/src/tui/overlay.ts +81 -34
  100. package/src/tui/prompt.js +162 -94
  101. package/src/tui/runningLight.js +1 -1
  102. package/src/tui/runningLight.ts +1 -1
  103. package/src/tui/scrollInteraction.js +64 -36
  104. package/src/tui/scrollInteraction.ts +70 -37
  105. package/src/tui/selectList.js +60 -32
  106. package/src/tui/sessionLayout.js +11 -3
  107. package/src/tui/sessionLayout.ts +10 -3
  108. package/src/tui/sessionPicker.js +74 -46
  109. package/src/tui/sessionPicker.ts +73 -48
  110. package/src/tui/sessionPickerView.js +32 -19
  111. package/src/tui/sessionPickerView.ts +31 -18
  112. package/src/tui/sessionSidebar.js +87 -57
  113. package/src/tui/sessionSidebar.ts +90 -54
  114. package/src/tui/setup.js +28 -3
  115. package/src/tui/setup.ts +27 -3
  116. package/src/tui/skillAuthoring.js +11 -5
  117. package/src/tui/skillAuthoring.ts +11 -5
  118. package/src/tui/smartLinkClick.js +75 -23
  119. package/src/tui/smartLinkClick.ts +86 -26
  120. package/src/tui/stage.js +47 -16
  121. package/src/tui/stage.ts +53 -18
  122. package/src/tui/stageSurface.js +120 -49
  123. package/src/tui/statusLine.js +85 -53
  124. package/src/tui/statusLine.ts +82 -46
  125. package/src/tui/terminalInputFramer.js +5 -2
  126. package/src/tui/text.js +117 -46
  127. package/src/tui/text.ts +113 -43
  128. package/src/tui/textLayout.js +6 -4
  129. package/src/tui/textLayout.ts +11 -5
  130. package/src/tui/theme.js +17 -8
  131. package/src/tui/thinkingBox.js +95 -95
  132. package/src/tui/thinkingBox.ts +105 -80
  133. package/src/tui/toolActivityPolicy.js +8 -1
  134. package/src/tui/toolActivityPolicy.ts +7 -1
  135. package/src/tui/toolDisplay.js +15 -6
  136. package/src/tui/toolDisplay.ts +16 -6
  137. package/src/tui/toolOutputNormalizer.js +16 -3
  138. package/src/tui/toolOutputNormalizer.ts +13 -3
  139. package/src/tui/transcript.js +15 -9
  140. package/src/tui/turnRail.js +156 -67
  141. package/src/tui/turnRail.ts +155 -58
  142. package/src/tui/usageCard.js +80 -45
  143. package/src/tui/usageCard.ts +81 -46
  144. package/src/tui/viewport.js +48 -23
  145. package/src/tui/viewport.ts +48 -27
  146. package/src/tui/viewportAnchor.js +2 -2
  147. package/src/tui/viewportAnchor.ts +2 -2
  148. package/src/tui/workspaceFileIndex.js +8 -6
  149. package/src/tui/workspaceFileIndex.ts +8 -6
  150. package/src/tui/workspaceSwitch.js +13 -10
  151. package/src/tui/workspaceSwitch.ts +11 -7
package/src/tui/prompt.js CHANGED
@@ -42,6 +42,7 @@ import { sidebarVisible, sessionMainWidth } from './sessionSidebar.js';
42
42
  import { isStartupDialog, startupDialogView } from './homeView.js';
43
43
  import { handleMessageMouse, handleMessageActionMenuMouse, isMouseMotion } from './messageInteraction.js';
44
44
  import { handleTurnRailMouse } from './turnRail.js';
45
+ import { runWithStageWrite } from './consoleGuard.js';
45
46
  import {
46
47
  resetTurnInterruptState,
47
48
  shouldAcceptStatusUpdate,
@@ -190,6 +191,13 @@ export class ChatPrompt {
190
191
  // Lifecycle
191
192
  // -------------------------------------------------------------------------
192
193
 
194
+ writeControl(sequence) {
195
+ if (!sequence) return;
196
+ runWithStageWrite(() => {
197
+ this.out.write(sequence);
198
+ });
199
+ }
200
+
193
201
  start() {
194
202
  if (this.active) return;
195
203
  this.active = true;
@@ -197,11 +205,11 @@ export class ChatPrompt {
197
205
  this.input.setRawMode(true);
198
206
  this.input.resume();
199
207
  this.input.on('data', this.handleData);
200
- this.out.write(bracketedPaste(true));
208
+ this.writeControl(bracketedPaste(true));
201
209
  // Node's normal exit does not guarantee ChatPrompt.stop() is called.
202
210
  // Always restore bracketed paste and raw stdin when the process exits.
203
211
  this.exitHook = () => {
204
- try { this.out.write(bracketedPaste(false)); } catch { /* terminal already closed */ }
212
+ try { this.writeControl(bracketedPaste(false)); } catch { /* terminal already closed */ }
205
213
  try { this.input.setRawMode(false); } catch { /* stdin already closed */ }
206
214
  };
207
215
  process.once('exit', this.exitHook);
@@ -214,6 +222,11 @@ export class ChatPrompt {
214
222
  stop() {
215
223
  if (!this.active) return;
216
224
  this.active = false;
225
+ if (this.layer) {
226
+ const layer = this.layer;
227
+ this.layer = null;
228
+ if (typeof layer.answer === 'function') layer.answer(false);
229
+ }
217
230
  this.stopTick();
218
231
  if (this.inputFrameTimer) { clearTimeout(this.inputFrameTimer); this.inputFrameTimer = null; }
219
232
  if (this.decoderRecoveryTimer) { clearTimeout(this.decoderRecoveryTimer); this.decoderRecoveryTimer = null; }
@@ -225,7 +238,7 @@ export class ChatPrompt {
225
238
  // session is flushed into real terminal scrollback, so leaving Causyne leaves
226
239
  // a transcript behind exactly as it always has.
227
240
  if (this.surface) this.surface.close();
228
- this.out.write(bracketedPaste(false));
241
+ this.writeControl(bracketedPaste(false));
229
242
  if (typeof this.out.removeListener === 'function') this.out.removeListener('resize', this.handleResize);
230
243
  this.input.off('data', this.handleData);
231
244
  try { this.input.setRawMode(false); } catch (e) { /* already closed */ }
@@ -297,6 +310,35 @@ export class ChatPrompt {
297
310
  }
298
311
  };
299
312
 
313
+ flushCharBatch(charBatch) {
314
+ if (!charBatch) return '';
315
+ this.prepareKeyboardState(true);
316
+ this.editor.insert(charBatch);
317
+ if (charBatch.length < 60 && !charBatch.includes('\n')) {
318
+ this.refreshCompletion();
319
+ } else {
320
+ this.completion = null;
321
+ }
322
+ return '';
323
+ }
324
+
325
+ prepareKeyboardState(clearNotice) {
326
+ if (clearNotice && this.notice && !this.layer) this.notice = '';
327
+ if (this.surface && typeof this.surface.captureMouse === 'function') {
328
+ if (this.surface.captureMouse() && typeof this.notice === 'string' && this.notice.startsWith('top of this view')) {
329
+ this.notice = '';
330
+ }
331
+ }
332
+ if (this.surface) {
333
+ this.surface.turnRailHoverSuppressed = false;
334
+ if (this.surface.hoveredTurnIndex !== null && this.surface.hoveredTurnIndex !== undefined) {
335
+ this.surface.hoveredTurnIndex = null;
336
+ this.surface.hoveredTurnAnchorY = null;
337
+ this.scheduleDraw();
338
+ }
339
+ }
340
+ }
341
+
300
342
  handleFramedData = (chunk) => {
301
343
  let motionOnly = true;
302
344
  const str = typeof chunk === 'string' ? chunk : Buffer.isBuffer(chunk) ? chunk.toString('utf8') : '';
@@ -338,13 +380,7 @@ export class ChatPrompt {
338
380
  continue;
339
381
  }
340
382
  if (charBatch.length > 0) {
341
- this.editor.insert(charBatch);
342
- if (charBatch.length < 60 && !charBatch.includes('\n')) {
343
- this.refreshCompletion();
344
- } else {
345
- this.completion = null;
346
- }
347
- charBatch = '';
383
+ charBatch = this.flushCharBatch(charBatch);
348
384
  }
349
385
  if (key.name !== 'mouse' || !isMouseMotion(key.sequence)) motionOnly = false;
350
386
  try {
@@ -358,13 +394,7 @@ export class ChatPrompt {
358
394
  }
359
395
 
360
396
  if (charBatch.length > 0) {
361
- this.editor.insert(charBatch);
362
- if (charBatch.length < 60 && !charBatch.includes('\n')) {
363
- this.refreshCompletion();
364
- } else {
365
- this.completion = null;
366
- }
367
- charBatch = '';
397
+ charBatch = this.flushCharBatch(charBatch);
368
398
  }
369
399
 
370
400
  // All-motion chunks repaint only when the hovered message actually changes.
@@ -962,28 +992,15 @@ export class ChatPrompt {
962
992
  // -------------------------------------------------------------------------
963
993
 
964
994
  handleKey(key) {
965
- // A notice reports the result of the last action. Once the user edits the
966
- // line again it is stale, and leaving it up made "copied ..." follow every
967
- // later keystroke for the rest of the session.
968
- if (this.notice && !this.layer
969
- && (key.name === 'char' || key.name === 'paste' || key.name === 'backspace' || key.name === 'delete')) {
995
+ const clearNotice = key.name === 'char' || key.name === 'paste' || key.name === 'backspace' || key.name === 'delete';
996
+ if (clearNotice && this.notice && !this.layer) {
970
997
  this.notice = '';
971
998
  }
972
999
 
973
1000
  // The wheel was handed to the terminal at the top of the band; the moment
974
1001
  // the user types again they are driving this prompt, so take it back.
975
1002
  if (key.name !== 'wheel' && key.name !== 'mouse') {
976
- if (this.surface && typeof this.surface.captureMouse === 'function') {
977
- if (this.surface.captureMouse() && this.notice.startsWith('top of this view')) this.notice = '';
978
- }
979
- if (this.surface) {
980
- this.surface.turnRailHoverSuppressed = false;
981
- if (this.surface.hoveredTurnIndex !== null && this.surface.hoveredTurnIndex !== undefined) {
982
- this.surface.hoveredTurnIndex = null;
983
- this.surface.hoveredTurnAnchorY = null;
984
- this.scheduleDraw();
985
- }
986
- }
1003
+ this.prepareKeyboardState(false);
987
1004
  }
988
1005
 
989
1006
  if (key.ctrl && key.name === 'c') {
@@ -1050,7 +1067,7 @@ export class ChatPrompt {
1050
1067
  this.scheduleDraw();
1051
1068
  } else {
1052
1069
  Promise.resolve(control()).catch(error => {
1053
- this.notice = error instanceof Error ? error.message : String(error);
1070
+ this.notice = this.sanitizeSingleLine(error instanceof Error ? error.message : String(error));
1054
1071
  this.scheduleDraw();
1055
1072
  });
1056
1073
  }
@@ -1060,7 +1077,7 @@ export class ChatPrompt {
1060
1077
  switch (key.name) {
1061
1078
  case 'escape': return this.handleEscape();
1062
1079
  case 'enter':
1063
- if (key.alt || key.shift) { this.editor.newline(); return; }
1080
+ if (key.alt || key.shift) { this.editor.newline(); return this.refreshCompletion(); }
1064
1081
  return this.submit();
1065
1082
  case 'tab':
1066
1083
  if (key.shift) return;
@@ -1093,20 +1110,12 @@ export class ChatPrompt {
1093
1110
  switch (key.name) {
1094
1111
  case 'a': return this.editor.moveHome();
1095
1112
  case 'e': return this.editor.moveEnd();
1096
- case 'k': return this.editor.killToEnd();
1097
- case 'u': return this.editor.killToStart();
1113
+ case 'k': this.editor.killToEnd(); return this.refreshCompletion();
1114
+ case 'u': this.editor.killToStart(); return this.refreshCompletion();
1098
1115
  case 'w': this.editor.killWordLeft(); return this.refreshCompletion();
1099
1116
  case 'y': this.editor.yank(); return this.refreshCompletion();
1100
1117
  case 'x': {
1101
- if (this.editor.hasSelection) {
1102
- const text = this.editor.getSelectedText();
1103
- writeClipboardSync(text);
1104
- this.editor.killRing = text;
1105
- this.editor.deleteSelection();
1106
- this.notice = `cut selection (${text.length} chars) to clipboard`;
1107
- this.scheduleDraw();
1108
- return;
1109
- }
1118
+ if (this.cutSelection()) return;
1110
1119
  break;
1111
1120
  }
1112
1121
  case 'p': return this.openPalette();
@@ -1123,15 +1132,7 @@ export class ChatPrompt {
1123
1132
  return;
1124
1133
  }
1125
1134
  if (key.alt && key.name === 'x') {
1126
- if (this.editor.hasSelection) {
1127
- const text = this.editor.getSelectedText();
1128
- writeClipboardSync(text);
1129
- this.editor.killRing = text;
1130
- this.editor.deleteSelection();
1131
- this.notice = `cut selection (${text.length} chars) to clipboard`;
1132
- this.scheduleDraw();
1133
- return;
1134
- }
1135
+ if (this.cutSelection()) return;
1135
1136
  }
1136
1137
  if (key.alt && (key.name === 'c' || key.name === 'w')) {
1137
1138
  const text = this.editor.hasSelection
@@ -1207,13 +1208,20 @@ export class ChatPrompt {
1207
1208
 
1208
1209
  if (!viewport) return false;
1209
1210
 
1211
+ const clearTurnRailFocus = () => {
1212
+ if (!this.surface) return;
1213
+ this.surface.activeTurnIndex = null;
1214
+ this.surface.hoveredTurnIndex = null;
1215
+ this.surface.hoveredTurnAnchorY = null;
1216
+ };
1217
+
1210
1218
  if (key.name === 'pageup') {
1211
- if (this.surface) this.surface.activeTurnIndex = null;
1219
+ clearTurnRailFocus();
1212
1220
  if (key.shift) viewport.toTop(); else viewport.pageUp();
1213
1221
  return true;
1214
1222
  }
1215
1223
  if (key.name === 'pagedown') {
1216
- if (this.surface) this.surface.activeTurnIndex = null;
1224
+ clearTurnRailFocus();
1217
1225
  // Shift+PgDn is the way back to following, and it is spelled out in the
1218
1226
  // affordance the surface draws while the view is held away.
1219
1227
  if (key.shift) viewport.toBottom(); else viewport.pageDown();
@@ -1221,23 +1229,23 @@ export class ChatPrompt {
1221
1229
  }
1222
1230
  if (key.shift && key.name === 'up') {
1223
1231
  if (!this.editor.isEmpty) return false;
1224
- if (this.surface) this.surface.activeTurnIndex = null;
1232
+ clearTurnRailFocus();
1225
1233
  viewport.lineUp();
1226
1234
  return true;
1227
1235
  }
1228
1236
  if (key.shift && key.name === 'down') {
1229
1237
  if (!this.editor.isEmpty) return false;
1230
- if (this.surface) this.surface.activeTurnIndex = null;
1238
+ clearTurnRailFocus();
1231
1239
  viewport.lineDown();
1232
1240
  return true;
1233
1241
  }
1234
1242
  if (key.alt && key.ctrl && key.name === 'u') {
1235
- if (this.surface) this.surface.activeTurnIndex = null;
1243
+ clearTurnRailFocus();
1236
1244
  viewport.halfPageUp();
1237
1245
  return true;
1238
1246
  }
1239
1247
  if (key.alt && key.ctrl && key.name === 'd') {
1240
- if (this.surface) this.surface.activeTurnIndex = null;
1248
+ clearTurnRailFocus();
1241
1249
  viewport.halfPageDown();
1242
1250
  return true;
1243
1251
  }
@@ -1251,14 +1259,19 @@ export class ChatPrompt {
1251
1259
  // entitled to type; the plan's `[`/`]` could not be bound at all, and
1252
1260
  // alt+[ is worse than unbound — ESC [ IS the CSI introducer, so it is
1253
1261
  // indistinguishable from the start of an escape sequence.
1254
- if (key.alt && key.name === ',') { viewport.jump(-1, ['step']); return true; }
1255
- if (key.alt && key.name === '.') { viewport.jump(1, ['step']); return true; }
1262
+ const jumpViewport = (direction, kinds) => {
1263
+ clearTurnRailFocus();
1264
+ viewport.jump(direction, kinds);
1265
+ return true;
1266
+ };
1267
+ if (key.alt && key.name === ',') return jumpViewport(-1, ['step']);
1268
+ if (key.alt && key.name === '.') return jumpViewport(1, ['step']);
1256
1269
  // Exchanges are marked by the USER's message, not by the turn footer. Both
1257
1270
  // sit at almost the same seam — a footer is immediately followed by the next
1258
1271
  // question — so accepting either made the jump land twice two rows apart
1259
1272
  // instead of once per exchange.
1260
- if ((key.alt && key.name === '[') || (key.alt && key.name === '<')) { viewport.jump(-1, ['user']); return true; }
1261
- if ((key.alt && key.name === ']') || (key.alt && key.name === '>')) { viewport.jump(1, ['user']); return true; }
1273
+ if ((key.alt && key.name === '[') || (key.alt && key.name === '<')) return jumpViewport(-1, ['user']);
1274
+ if ((key.alt && key.name === ']') || (key.alt && key.name === '>')) return jumpViewport(1, ['user']);
1262
1275
 
1263
1276
  if (key.alt && key.name === 'u') { this.offerUndo(); return true; }
1264
1277
 
@@ -1301,8 +1314,9 @@ export class ChatPrompt {
1301
1314
  const mark = onScreen.length ? onScreen[onScreen.length - 1] : (above.pop() || marks[0]);
1302
1315
  const meta = mark.meta || {};
1303
1316
 
1304
- const where = meta.path ? shortenHome(meta.path) : 'files changed this task';
1305
- const step = meta.step == null ? '' : ` at step ${meta.step}`;
1317
+ const where = meta.path ? this.sanitizeSingleLine(shortenHome(meta.path)) : 'files changed this task';
1318
+ const rawStep = meta.step == null ? '' : this.sanitizeSingleLine(meta.step);
1319
+ const step = rawStep ? ` at step ${rawStep}` : '';
1306
1320
 
1307
1321
  if (this.editor.isEmpty) {
1308
1322
  this.editor.setText('/undo');
@@ -1313,6 +1327,21 @@ export class ChatPrompt {
1313
1327
  }
1314
1328
  }
1315
1329
 
1330
+ sanitizeSingleLine(value) {
1331
+ return value == null ? '' : safeDisplay(String(value)).replace(/\s+/g, ' ').trim();
1332
+ }
1333
+
1334
+ cutSelection() {
1335
+ if (!this.editor.hasSelection) return false;
1336
+ const text = this.editor.getSelectedText();
1337
+ writeClipboardSync(text);
1338
+ this.editor.killRing = text;
1339
+ this.editor.deleteSelection();
1340
+ this.notice = `cut selection (${text.length} chars) to clipboard`;
1341
+ this.refreshCompletion();
1342
+ return true;
1343
+ }
1344
+
1316
1345
  handleCtrlC() {
1317
1346
  handlePromptCtrlC(
1318
1347
  this,
@@ -1353,7 +1382,7 @@ export class ChatPrompt {
1353
1382
  this.erase();
1354
1383
  // The one clear Causyne may perform: the user pressed ctrl+L and asked for it.
1355
1384
  // UX-01 forbids clearing the terminal on the app's own initiative.
1356
- this.out.write('\x1b[2J\x1b[H');
1385
+ this.writeControl('\x1b[2J\x1b[H');
1357
1386
  this.drawnRows = 0;
1358
1387
  this.cursorRow = 0;
1359
1388
  }
@@ -1385,7 +1414,7 @@ export class ChatPrompt {
1385
1414
  if (!trigger || trigger.kind !== 'file') return;
1386
1415
  this.openCompletion(false);
1387
1416
  if (index.truncated) this.notice = 'Workspace file index reached its safety limit; narrow the project root.';
1388
- else if (this.notice === 'Indexing workspace files…') this.notice = null;
1417
+ else if (this.notice === 'Indexing workspace files…') this.notice = '';
1389
1418
  this.scheduleDraw();
1390
1419
  });
1391
1420
  void index.ensure();
@@ -1489,21 +1518,40 @@ export class ChatPrompt {
1489
1518
  return true;
1490
1519
  }
1491
1520
  if (key.name === 'mouse') {
1492
- if (key.mouse && key.mouse.pressed && key.mouse.button === 0) {
1521
+ if (key.mouse && key.mouse.pressed && key.mouse.button === 0 && !isMouseMotion(key.sequence)) {
1493
1522
  let item = null;
1494
- if (this.pickerLayout && Number.isFinite(key.mouse.row)) {
1495
- const stageHeight = this.surface?.stage?.height || process.stdout.rows || 24;
1496
- const promptTop = Math.max(1, stageHeight - this.pickerLayout.totalPromptRows + 1);
1523
+ const hasZeroY = Number.isFinite(key.mouse.y);
1524
+ const rawMouseRow = hasZeroY
1525
+ ? Math.floor(key.mouse.y)
1526
+ : (Number.isFinite(key.mouse.row) ? Math.floor(key.mouse.row) : NaN);
1527
+ if (this.pickerLayout && Number.isFinite(rawMouseRow)) {
1528
+ const stageHeight = this.surface?.stage?.height || this.out?.rows || process.stdout.rows || 24;
1529
+ const termRows = Number.isFinite(this.out?.rows) && this.out.rows > 0
1530
+ ? Math.floor(this.out.rows)
1531
+ : stageHeight;
1532
+ // KeyDecoder emits 0-based x/y screen coordinates; legacy unit tests pass 1-based row/col.
1533
+ const promptTop = hasZeroY
1534
+ ? Math.max(0, termRows - this.pickerLayout.totalPromptRows)
1535
+ : Math.max(1, stageHeight - this.pickerLayout.totalPromptRows + 1);
1497
1536
  const pickerTop = promptTop + this.pickerLayout.pickerStart;
1498
1537
  const pickerBottom = pickerTop + this.pickerLayout.pickerCount - 1;
1538
+ const mouseX = Number.isFinite(key.mouse.x) ? Math.floor(key.mouse.x) : NaN;
1539
+ const mainW = this.surface
1540
+ ? sessionMainWidth(this.surface.width, Boolean(this.surface.homeInfo), this.surface.env)
1541
+ : Infinity;
1542
+ const inMainColumn = !Number.isFinite(mouseX) || (mouseX >= 0 && mouseX < mainW);
1499
1543
 
1500
- if (key.mouse.row >= pickerTop && key.mouse.row <= pickerBottom) {
1544
+ if (inMainColumn && rawMouseRow >= pickerTop && rawMouseRow <= pickerBottom) {
1501
1545
  // The picker renders a scrolled window (renderPicker); a click maps
1502
1546
  // to the item under the cursor, not to the first page of items.
1503
- const total = this.completion.items ? this.completion.items.length : 0;
1547
+ const items = Array.isArray(this.completion.items) ? this.completion.items : [];
1548
+ const total = items.length;
1504
1549
  const windowStart = Math.max(0, Math.min(this.selected - MAX_VISIBLE + 1, total - MAX_VISIBLE));
1505
- const index = windowStart + (key.mouse.row - pickerTop);
1506
- item = this.completion.items && this.completion.items[index];
1550
+ const index = windowStart + (rawMouseRow - pickerTop);
1551
+ if (index >= 0 && index < total) {
1552
+ this.selected = index;
1553
+ item = items[index];
1554
+ }
1507
1555
  } else {
1508
1556
  // Click outside picker closes it
1509
1557
  this.completion = null;
@@ -1511,7 +1559,8 @@ export class ChatPrompt {
1511
1559
  return true;
1512
1560
  }
1513
1561
  } else {
1514
- item = this.completion.items && this.completion.items[this.selected];
1562
+ const items = Array.isArray(this.completion.items) ? this.completion.items : [];
1563
+ item = items[this.selected];
1515
1564
  }
1516
1565
 
1517
1566
  if (item) {
@@ -1592,13 +1641,14 @@ export class ChatPrompt {
1592
1641
  }
1593
1642
 
1594
1643
  moveSelection(direction) {
1595
- const count = this.completion.items.length;
1596
- this.selected = (this.selected + direction + count) % count;
1644
+ const count = Array.isArray(this.completion?.items) ? this.completion.items.length : 0;
1645
+ if (count === 0) return;
1646
+ this.selected = (((this.selected + direction) % count) + count) % count;
1597
1647
  }
1598
1648
 
1599
1649
  /** Move without wrapping: a wheel or page key stops at either end. */
1600
1650
  moveSelectionClamped(delta) {
1601
- const count = this.completion.items.length;
1651
+ const count = Array.isArray(this.completion?.items) ? this.completion.items.length : 0;
1602
1652
  if (count === 0) return;
1603
1653
  this.selected = Math.max(0, Math.min(count - 1, this.selected + delta));
1604
1654
  }
@@ -1853,7 +1903,8 @@ export class ChatPrompt {
1853
1903
  totalPromptRows: 0
1854
1904
  } : null;
1855
1905
  const queuedRows = this.queuedPreviewRows(width);
1856
- const noticeRows = this.notice ? [dim(` ${this.notice}`)] : [];
1906
+ const cleanNotice = this.notice ? this.sanitizeSingleLine(this.notice) : '';
1907
+ const noticeRows = cleanNotice ? [dim(` ${cleanNotice}`)] : [];
1857
1908
 
1858
1909
  // And a row of air between the box and the footer. The picker and a notice
1859
1910
  // belong to the input and stay welded to it — opencode floats its
@@ -1870,7 +1921,7 @@ export class ChatPrompt {
1870
1921
  // Park the cursor where the user is typing, not at the end of the picker.
1871
1922
  const availableWidth = this.surface?.composeAvailableWidth
1872
1923
  ? this.surface.composeAvailableWidth()
1873
- : sessionMainWidth(this.surface?.width || terminalWidth());
1924
+ : sessionMainWidth(this.surface?.width || terminalWidth(), false, this.surface?.env);
1874
1925
  const positioned = this.surface
1875
1926
  ? (this.surface.homeInfo ? centerRows(all, this.surface.width) : sessionRows(all, availableWidth))
1876
1927
  : { rows: all, inset: 0 };
@@ -1888,23 +1939,27 @@ export class ChatPrompt {
1888
1939
  * nothing of ours is on the terminal for erase() to walk back over.
1889
1940
  */
1890
1941
  present(all, caretRow, caretCol) {
1942
+ const rows = Array.isArray(all) ? all : [];
1943
+ const maxRow = Math.max(0, rows.length - 1);
1944
+ const safeRow = Number.isFinite(caretRow) ? Math.max(0, Math.min(maxRow, Math.floor(caretRow))) : 0;
1945
+ const safeCol = Number.isFinite(caretCol) ? Math.max(0, Math.floor(caretCol)) : 0;
1891
1946
  const hideCursor = Boolean(this.busy && this.editor.isEmpty);
1892
1947
  if (this.surface) {
1893
1948
  this.drawnRows = 0;
1894
1949
  this.cursorRow = 0;
1895
- this.surface.present(all, { row: caretRow, col: caretCol, hideCursor,
1950
+ this.surface.present(rows, { row: safeRow, col: safeCol, hideCursor,
1896
1951
  modal: Boolean(this.layer), inline: Boolean(this.layer && !isStartupDialog(this.surface)) });
1897
1952
  return;
1898
1953
  }
1899
1954
 
1900
- this.out.write(all.join('\n'));
1901
- this.drawnRows = all.length;
1955
+ this.out.write(rows.join('\n'));
1956
+ this.drawnRows = rows.length;
1902
1957
 
1903
- const below = all.length - 1 - caretRow;
1958
+ const below = rows.length - 1 - safeRow;
1904
1959
  if (below > 0) this.out.write(`\x1b[${below}A`);
1905
- this.cursorRow = caretRow;
1960
+ this.cursorRow = safeRow;
1906
1961
  this.out.write('\r');
1907
- if (caretCol > 0) this.out.write(`\x1b[${caretCol}C`);
1962
+ if (safeCol > 0) this.out.write(`\x1b[${safeCol}C`);
1908
1963
  if (hideCursor) {
1909
1964
  this.out.write('\x1b[?25l');
1910
1965
  } else {
@@ -1941,13 +1996,18 @@ export class ChatPrompt {
1941
1996
 
1942
1997
  /** The live region while a dialog is up: the panel, then the status row. */
1943
1998
  drawLayer(width) {
1999
+ const preferredHeight = typeof this.layer.preferredHeight === 'function'
2000
+ ? this.layer.preferredHeight()
2001
+ : 14;
1944
2002
  if (this.layer.messageActions && this.surface?.presentMessageActions) {
1945
- const availableWidth = sessionMainWidth(this.surface.width);
2003
+ const availableWidth = this.surface.composeAvailableWidth
2004
+ ? this.surface.composeAvailableWidth()
2005
+ : sessionMainWidth(this.surface.width, false, this.surface.env);
1946
2006
  const requestedWidth = typeof this.layer.preferredWidth === 'function'
1947
2007
  ? this.layer.preferredWidth(Math.max(8, availableWidth - 3))
1948
2008
  : (this.layer.width || 64);
1949
2009
  const panelWidth = dialogWidth(Math.max(8, availableWidth - 3), requestedWidth);
1950
- const panel = panelRows(this.layer, panelWidth, this.layer.preferredHeight());
2010
+ const panel = panelRows(this.layer, panelWidth, preferredHeight);
1951
2011
  this.surface.presentMessageActions(panel, this.layer);
1952
2012
  return;
1953
2013
  }
@@ -1956,12 +2016,16 @@ export class ChatPrompt {
1956
2016
  return this.present(view.rows, view.caretRow, view.caretCol);
1957
2017
  }
1958
2018
  const inline = this.surface && !this.surface.homeInfo;
1959
- const availableWidth = inline ? sessionMainWidth(this.surface.width) : width;
2019
+ const availableWidth = inline
2020
+ ? (this.surface.composeAvailableWidth
2021
+ ? this.surface.composeAvailableWidth()
2022
+ : sessionMainWidth(this.surface.width, false, this.surface.env))
2023
+ : width;
1960
2024
  const panelWidth = dialogWidth(availableWidth, this.layer.width || 64);
1961
2025
  const stageMax = this.surface?.stage?.maxHeight || this.out?.rows || 24;
1962
2026
  const statusReserve = 2; // status row + margin
1963
2027
  const availableDialogRows = Math.max(4, stageMax - statusReserve);
1964
- const maxHeight = dialogHeight(availableDialogRows, this.layer.preferredHeight());
2028
+ const maxHeight = dialogHeight(availableDialogRows, preferredHeight);
1965
2029
  const panel = panelRows(this.layer, panelWidth, maxHeight);
1966
2030
  const status = this.statusRow(width);
1967
2031
  const all = this.clampRows(panel.concat(status), width);
@@ -1970,9 +2034,13 @@ export class ChatPrompt {
1970
2034
  const cursor = this.layer.cursor;
1971
2035
  const centered = this.surface ? centerRows(all, availableWidth) : { rows: all, inset: 0 };
1972
2036
  const stageHeight = this.surface?.stage?.height || this.out?.rows || all.length;
2037
+ const outRows = Number.isFinite(this.out?.rows) && this.out.rows > 0
2038
+ ? Math.floor(this.out.rows)
2039
+ : stageHeight;
1973
2040
  this.layer.panelGeometry = {
1974
2041
  inset: centered.inset,
1975
- optionScreenRow: stageHeight - status.length - 1
2042
+ // Anchor at the panel's last body row above the bottom border and status rows (0-based screen row).
2043
+ optionScreenRow: Math.max(0, outRows - status.length - 2)
1976
2044
  };
1977
2045
  this.present(centered.rows, cursor ? 1 + cursor.y : 0,
1978
2046
  (cursor ? 2 + cursor.x : 0) + centered.inset);
@@ -74,7 +74,7 @@ export function resolveRunningLightSpeed(speed = 'medium') {
74
74
  * - For `runesLen >= 25`: bidirectional ping-pong sweep at `longStepMs`.
75
75
  */
76
76
  export function runningLightWindow(runesLen, nowMs, speed = 'medium') {
77
- const len = Math.max(0, Math.floor(runesLen));
77
+ const len = Number.isFinite(runesLen) ? Math.max(0, Math.floor(runesLen)) : 0;
78
78
  const { shortStepMs, longStepMs } = resolveRunningLightSpeed(speed);
79
79
  if (len <= 0 || shortStepMs <= 0 || longStepMs <= 0) {
80
80
  return { start: 0, width: 0, active: false };
@@ -110,7 +110,7 @@ export function runningLightWindow(
110
110
  nowMs: number,
111
111
  speed: RunningLightSpeed = 'medium'
112
112
  ): RunningLightWindow {
113
- const len = Math.max(0, Math.floor(runesLen));
113
+ const len = Number.isFinite(runesLen) ? Math.max(0, Math.floor(runesLen)) : 0;
114
114
  const { shortStepMs, longStepMs } = resolveRunningLightSpeed(speed);
115
115
  if (len <= 0 || shortStepMs <= 0 || longStepMs <= 0) {
116
116
  return { start: 0, width: 0, active: false };