@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/text.js CHANGED
@@ -16,6 +16,9 @@ import os from 'node:os';
16
16
  const CSI_PATTERN = /\x1b\[[0-?]*[ -/]*[@-~]/g;
17
17
  const OSC_PATTERN = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g;
18
18
  const OTHER_ESCAPE_PATTERN = /\x1b[@-Z\\-_]/g;
19
+ const CSI_STICKY = /\x1b\[[0-?]*[ -/]*[@-~]/y;
20
+ const OSC_STICKY = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/y;
21
+ const OTHER_ESCAPE_STICKY = /\x1b[@-Z\\-_]/y;
19
22
  const OSC8_SEQUENCE = /^\x1b\]8;[^;]*;([^\x07\x1b]*)(\x07|\x1b\\)/;
20
23
  /** Everything that is an escape sequence rather than visible text. */
21
24
  export function stripAnsi(text) {
@@ -111,12 +114,40 @@ function detectAmbiguousWide() {
111
114
  export const AMBIGUOUS_IS_WIDE = detectAmbiguousWide();
112
115
  /** Columns one code point occupies, under the active ambiguous-width policy. */
113
116
  function columnsFor(code) {
117
+ if (code < 32 ||
118
+ (code >= 0x7f && code <= 0x9f) ||
119
+ (code >= 0x0300 && code <= 0x036f) ||
120
+ (code >= 0x1ab0 && code <= 0x1aff) ||
121
+ (code >= 0x1dc0 && code <= 0x1dff) ||
122
+ (code >= 0x200b && code <= 0x200f) ||
123
+ (code >= 0x20d0 && code <= 0x20ff) ||
124
+ (code >= 0xfe00 && code <= 0xfe0f) ||
125
+ (code >= 0xfe20 && code <= 0xfe2f) ||
126
+ code === 0xfeff ||
127
+ (code >= 0xe0100 && code <= 0xe01ef)) {
128
+ return 0;
129
+ }
114
130
  if (isWideCodePoint(code))
115
131
  return 2;
116
132
  if (AMBIGUOUS_IS_WIDE && isAmbiguousCodePoint(code))
117
133
  return 2;
118
134
  return 1;
119
135
  }
136
+ function matchEscapeSequenceAt(source, index) {
137
+ if (source[index] !== '\x1b')
138
+ return null;
139
+ OSC_STICKY.lastIndex = index;
140
+ const osc = OSC_STICKY.exec(source);
141
+ if (osc)
142
+ return osc[0];
143
+ CSI_STICKY.lastIndex = index;
144
+ const csi = CSI_STICKY.exec(source);
145
+ if (csi)
146
+ return csi[0];
147
+ OTHER_ESCAPE_STICKY.lastIndex = index;
148
+ const other = OTHER_ESCAPE_STICKY.exec(source);
149
+ return other ? other[0] : null;
150
+ }
120
151
  /** Columns this string will occupy once printed. Escapes count for nothing. */
121
152
  export function visibleWidth(text) {
122
153
  const plain = stripAnsi(text);
@@ -125,10 +156,6 @@ export function visibleWidth(text) {
125
156
  const code = character.codePointAt(0);
126
157
  if (code === undefined)
127
158
  continue;
128
- if (code === 0x200d)
129
- continue; // zero-width joiner
130
- if (code >= 0xfe00 && code <= 0xfe0f)
131
- continue; // variation selectors
132
159
  width += columnsFor(code);
133
160
  }
134
161
  return width;
@@ -143,36 +170,38 @@ const RESET = '\x1b[0m';
143
170
  */
144
171
  export function truncate(text, width, ellipsis = '…') {
145
172
  const source = String(text == null ? '' : text);
146
- if (width <= 0)
173
+ if (width === Number.POSITIVE_INFINITY)
174
+ return source;
175
+ const safeWidth = Number.isFinite(width) ? Math.floor(width) : 0;
176
+ if (safeWidth <= 0)
147
177
  return '';
148
- if (visibleWidth(source) <= width)
178
+ if (visibleWidth(source) <= safeWidth)
149
179
  return source;
150
- const ellipsisWidth = visibleWidth(ellipsis);
151
- const budget = Math.max(0, width - ellipsisWidth);
180
+ const rawEllipsis = String(ellipsis == null ? '' : ellipsis);
181
+ const safeEllipsis = visibleWidth(rawEllipsis) <= safeWidth
182
+ ? rawEllipsis
183
+ : (rawEllipsis ? truncate(rawEllipsis, safeWidth, '') : '');
184
+ const ellipsisWidth = visibleWidth(safeEllipsis);
185
+ const budget = Math.max(0, safeWidth - ellipsisWidth);
152
186
  let out = '';
153
187
  let used = 0;
154
188
  let sawEscape = false;
155
189
  let openOsc8Term = '';
156
190
  let index = 0;
157
191
  while (index < source.length) {
158
- if (source[index] === '\x1b') {
192
+ const esc = matchEscapeSequenceAt(source, index);
193
+ if (esc) {
159
194
  // Copy the whole sequence verbatim; it costs no columns.
160
- const rest = source.slice(index);
161
- const match = rest.match(/^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/)
162
- || rest.match(/^\x1b\[[0-?]*[ -/]*[@-~]/)
163
- || rest.match(/^\x1b[@-Z\\-_]/);
164
- if (match) {
165
- const osc8 = OSC8_SEQUENCE.exec(match[0]);
166
- if (osc8) {
167
- const uri = osc8[1] ?? '';
168
- const term = osc8[2] ?? '\x1b\\';
169
- openOsc8Term = uri.length > 0 ? term : '';
170
- }
171
- out += match[0];
172
- sawEscape = true;
173
- index += match[0].length;
174
- continue;
195
+ const osc8 = OSC8_SEQUENCE.exec(esc);
196
+ if (osc8) {
197
+ const uri = osc8[1] ?? '';
198
+ const term = osc8[2] ?? '\x1b\\';
199
+ openOsc8Term = uri.length > 0 ? term : '';
175
200
  }
201
+ out += esc;
202
+ sawEscape = true;
203
+ index += esc.length;
204
+ continue;
176
205
  }
177
206
  const code = source.codePointAt(index);
178
207
  if (code === undefined)
@@ -186,26 +215,30 @@ export function truncate(text, width, ellipsis = '…') {
186
215
  index += character.length;
187
216
  }
188
217
  const osc8Reset = openOsc8Term ? `\x1b]8;;${openOsc8Term}` : '';
189
- return out + osc8Reset + (sawEscape ? RESET : '') + ellipsis;
218
+ return out + osc8Reset + (sawEscape ? RESET : '') + safeEllipsis;
190
219
  }
191
220
  /** Pad to an exact column count, measuring visible width rather than bytes. */
192
221
  export function padEnd(text, width) {
193
- const gap = width - visibleWidth(text);
194
- return gap > 0 ? String(text) + ' '.repeat(gap) : String(text);
222
+ const source = String(text == null ? '' : text);
223
+ const safeWidth = Number.isFinite(width) ? Math.floor(width) : 0;
224
+ const gap = safeWidth - visibleWidth(source);
225
+ return gap > 0 ? source + ' '.repeat(gap) : source;
195
226
  }
196
227
  export function wrap(text, width, _options) {
197
- if (width <= 0)
198
- return [String(text)];
228
+ const source = String(text == null ? '' : text);
229
+ const safeWidth = Number.isFinite(width) ? Math.floor(width) : (width === Infinity ? Infinity : 0);
230
+ if (safeWidth <= 0)
231
+ return [source];
199
232
  const lines = [];
200
- for (const paragraph of String(text == null ? '' : text).split('\n')) {
201
- if (visibleWidth(paragraph) <= width) {
233
+ for (const paragraph of source.split('\n')) {
234
+ if (visibleWidth(paragraph) <= safeWidth) {
202
235
  lines.push(paragraph);
203
236
  continue;
204
237
  }
205
238
  let current = '';
206
239
  for (const word of paragraph.split(' ')) {
207
240
  const candidate = current ? `${current} ${word}` : word;
208
- if (visibleWidth(candidate) <= width) {
241
+ if (visibleWidth(candidate) <= safeWidth) {
209
242
  current = candidate;
210
243
  continue;
211
244
  }
@@ -214,18 +247,33 @@ export function wrap(text, width, _options) {
214
247
  current = '';
215
248
  }
216
249
  let remainder = word;
217
- while (visibleWidth(remainder) > width) {
218
- lines.push(truncate(remainder, width, ''));
219
- // truncate() gives us the head; drop the same number of characters.
250
+ while (visibleWidth(remainder) > safeWidth) {
251
+ const chunk = truncate(remainder, safeWidth, '');
252
+ // Drop the exact prefix, skipping escapes.
220
253
  let consumed = 0;
221
254
  let cut = 0;
222
- while (cut < remainder.length && consumed < width) {
255
+ while (cut < remainder.length) {
256
+ const esc = matchEscapeSequenceAt(remainder, cut);
257
+ if (esc) {
258
+ cut += esc.length;
259
+ continue;
260
+ }
223
261
  const code = remainder.codePointAt(cut);
224
262
  if (code === undefined)
225
263
  break;
226
- consumed += columnsFor(code);
264
+ const charWidth = columnsFor(code);
265
+ if (consumed + charWidth > safeWidth) {
266
+ if (cut === 0 || consumed === 0) {
267
+ cut += String.fromCodePoint(code).length;
268
+ }
269
+ break;
270
+ }
271
+ consumed += charWidth;
227
272
  cut += String.fromCodePoint(code).length;
228
273
  }
274
+ if (cut === 0)
275
+ break;
276
+ lines.push(visibleWidth(chunk) > 0 ? chunk : remainder.slice(0, cut));
229
277
  remainder = remainder.slice(cut);
230
278
  }
231
279
  current = remainder;
@@ -247,18 +295,22 @@ export function wrap(text, width, _options) {
247
295
  * terminal broke it at the right edge, mid-word, at column 0.
248
296
  */
249
297
  export function wrapBlock(text, width, { hang = 2 } = {}) {
298
+ const safeHang = Number.isFinite(hang) ? Math.max(0, Math.floor(hang)) : 2;
299
+ const safeWidth = Number.isFinite(width) ? Math.floor(width) : 80;
250
300
  const rows = [];
251
- for (const line of String(text == null ? '' : text).split('\n')) {
252
- const indent = (line.match(/^[ \t]*/) || [''])[0];
301
+ for (const rawLine of String(text == null ? '' : text).split('\n')) {
302
+ const line = expandTabs(rawLine.replace(/\r$/, ''));
303
+ const indent = (line.match(/^ */) || [''])[0];
253
304
  const body = line.slice(indent.length);
254
305
  if (!body) {
255
306
  rows.push('');
256
307
  continue;
257
308
  }
258
- const continuation = indent + ' '.repeat(hang);
309
+ const rawContinuation = indent + ' '.repeat(safeHang);
310
+ const continuation = visibleWidth(rawContinuation) < safeWidth ? rawContinuation : indent;
259
311
  // Wrap at the NARROWER of the two budgets so neither the first row nor a
260
312
  // continuation row can exceed `width`.
261
- const budget = Math.max(8, width - visibleWidth(continuation));
313
+ const budget = Math.max(1, safeWidth - visibleWidth(continuation));
262
314
  const parts = wrap(body, budget);
263
315
  rows.push(indent + (parts[0] ?? ''));
264
316
  for (const part of parts.slice(1))
@@ -308,11 +360,23 @@ export function expandTabs(text, tabWidth = TAB_WIDTH) {
308
360
  const source = String(text == null ? '' : text);
309
361
  if (!source.includes('\t'))
310
362
  return source;
363
+ const step = Number.isFinite(tabWidth) && tabWidth >= 1 ? Math.floor(tabWidth) : TAB_WIDTH;
311
364
  let out = '';
312
365
  let column = 0;
313
- for (const char of source) {
366
+ let index = 0;
367
+ while (index < source.length) {
368
+ const esc = matchEscapeSequenceAt(source, index);
369
+ if (esc) {
370
+ out += esc;
371
+ index += esc.length;
372
+ continue;
373
+ }
374
+ const code = source.codePointAt(index);
375
+ if (code === undefined)
376
+ break;
377
+ const char = String.fromCodePoint(code);
314
378
  if (char === '\t') {
315
- const advance = tabWidth - (column % tabWidth);
379
+ const advance = step - (column % step);
316
380
  out += ' '.repeat(advance);
317
381
  column += advance;
318
382
  }
@@ -322,8 +386,9 @@ export function expandTabs(text, tabWidth = TAB_WIDTH) {
322
386
  }
323
387
  else {
324
388
  out += char;
325
- column += 1;
389
+ column += columnsFor(code);
326
390
  }
391
+ index += char.length;
327
392
  }
328
393
  return out;
329
394
  }
@@ -343,8 +408,14 @@ export function sanitize(text) {
343
408
  export function shortenHome(value) {
344
409
  const home = os.homedir();
345
410
  const text = String(value == null ? '' : value);
346
- if (home && text.startsWith(home))
347
- return '~' + text.slice(home.length);
411
+ const cleanHome = home.length > 1 ? home.replace(/[\\/]+$/, '') : home;
412
+ if (cleanHome && cleanHome !== '/') {
413
+ if (text === cleanHome)
414
+ return '~';
415
+ if (text.startsWith(`${cleanHome}/`) || text.startsWith(`${cleanHome}\\`)) {
416
+ return `~${text.slice(cleanHome.length)}`;
417
+ }
418
+ }
348
419
  return text;
349
420
  }
350
421
  const SECRET_PATTERNS = [
package/src/tui/text.ts CHANGED
@@ -18,6 +18,9 @@ import os from 'node:os';
18
18
  const CSI_PATTERN = /\x1b\[[0-?]*[ -/]*[@-~]/g;
19
19
  const OSC_PATTERN = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g;
20
20
  const OTHER_ESCAPE_PATTERN = /\x1b[@-Z\\-_]/g;
21
+ const CSI_STICKY = /\x1b\[[0-?]*[ -/]*[@-~]/y;
22
+ const OSC_STICKY = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/y;
23
+ const OTHER_ESCAPE_STICKY = /\x1b[@-Z\\-_]/y;
21
24
  const OSC8_SEQUENCE = /^\x1b\]8;[^;]*;([^\x07\x1b]*)(\x07|\x1b\\)/;
22
25
 
23
26
  /** Everything that is an escape sequence rather than visible text. */
@@ -119,11 +122,39 @@ export const AMBIGUOUS_IS_WIDE: boolean = detectAmbiguousWide();
119
122
 
120
123
  /** Columns one code point occupies, under the active ambiguous-width policy. */
121
124
  function columnsFor(code: number): number {
125
+ if (
126
+ code < 32 ||
127
+ (code >= 0x7f && code <= 0x9f) ||
128
+ (code >= 0x0300 && code <= 0x036f) ||
129
+ (code >= 0x1ab0 && code <= 0x1aff) ||
130
+ (code >= 0x1dc0 && code <= 0x1dff) ||
131
+ (code >= 0x200b && code <= 0x200f) ||
132
+ (code >= 0x20d0 && code <= 0x20ff) ||
133
+ (code >= 0xfe00 && code <= 0xfe0f) ||
134
+ (code >= 0xfe20 && code <= 0xfe2f) ||
135
+ code === 0xfeff ||
136
+ (code >= 0xe0100 && code <= 0xe01ef)
137
+ ) {
138
+ return 0;
139
+ }
122
140
  if (isWideCodePoint(code)) return 2;
123
141
  if (AMBIGUOUS_IS_WIDE && isAmbiguousCodePoint(code)) return 2;
124
142
  return 1;
125
143
  }
126
144
 
145
+ function matchEscapeSequenceAt(source: string, index: number): string | null {
146
+ if (source[index] !== '\x1b') return null;
147
+ OSC_STICKY.lastIndex = index;
148
+ const osc = OSC_STICKY.exec(source);
149
+ if (osc) return osc[0];
150
+ CSI_STICKY.lastIndex = index;
151
+ const csi = CSI_STICKY.exec(source);
152
+ if (csi) return csi[0];
153
+ OTHER_ESCAPE_STICKY.lastIndex = index;
154
+ const other = OTHER_ESCAPE_STICKY.exec(source);
155
+ return other ? other[0] : null;
156
+ }
157
+
127
158
  /** Columns this string will occupy once printed. Escapes count for nothing. */
128
159
  export function visibleWidth(text: unknown): number {
129
160
  const plain = stripAnsi(text);
@@ -131,8 +162,6 @@ export function visibleWidth(text: unknown): number {
131
162
  for (const character of plain) {
132
163
  const code = character.codePointAt(0);
133
164
  if (code === undefined) continue;
134
- if (code === 0x200d) continue; // zero-width joiner
135
- if (code >= 0xfe00 && code <= 0xfe0f) continue; // variation selectors
136
165
  width += columnsFor(code);
137
166
  }
138
167
  return width;
@@ -149,11 +178,17 @@ const RESET = '\x1b[0m';
149
178
  */
150
179
  export function truncate(text: unknown, width: number, ellipsis = '…'): string {
151
180
  const source = String(text == null ? '' : text);
152
- if (width <= 0) return '';
153
- if (visibleWidth(source) <= width) return source;
154
-
155
- const ellipsisWidth = visibleWidth(ellipsis);
156
- const budget = Math.max(0, width - ellipsisWidth);
181
+ if (width === Number.POSITIVE_INFINITY) return source;
182
+ const safeWidth = Number.isFinite(width) ? Math.floor(width) : 0;
183
+ if (safeWidth <= 0) return '';
184
+ if (visibleWidth(source) <= safeWidth) return source;
185
+
186
+ const rawEllipsis = String(ellipsis == null ? '' : ellipsis);
187
+ const safeEllipsis = visibleWidth(rawEllipsis) <= safeWidth
188
+ ? rawEllipsis
189
+ : (rawEllipsis ? truncate(rawEllipsis, safeWidth, '') : '');
190
+ const ellipsisWidth = visibleWidth(safeEllipsis);
191
+ const budget = Math.max(0, safeWidth - ellipsisWidth);
157
192
 
158
193
  let out = '';
159
194
  let used = 0;
@@ -162,24 +197,19 @@ export function truncate(text: unknown, width: number, ellipsis = '…'): string
162
197
  let index = 0;
163
198
 
164
199
  while (index < source.length) {
165
- if (source[index] === '\x1b') {
200
+ const esc = matchEscapeSequenceAt(source, index);
201
+ if (esc) {
166
202
  // Copy the whole sequence verbatim; it costs no columns.
167
- const rest = source.slice(index);
168
- const match = rest.match(/^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/)
169
- || rest.match(/^\x1b\[[0-?]*[ -/]*[@-~]/)
170
- || rest.match(/^\x1b[@-Z\\-_]/);
171
- if (match) {
172
- const osc8 = OSC8_SEQUENCE.exec(match[0]);
173
- if (osc8) {
174
- const uri = osc8[1] ?? '';
175
- const term = osc8[2] ?? '\x1b\\';
176
- openOsc8Term = uri.length > 0 ? term : '';
177
- }
178
- out += match[0];
179
- sawEscape = true;
180
- index += match[0].length;
181
- continue;
203
+ const osc8 = OSC8_SEQUENCE.exec(esc);
204
+ if (osc8) {
205
+ const uri = osc8[1] ?? '';
206
+ const term = osc8[2] ?? '\x1b\\';
207
+ openOsc8Term = uri.length > 0 ? term : '';
182
208
  }
209
+ out += esc;
210
+ sawEscape = true;
211
+ index += esc.length;
212
+ continue;
183
213
  }
184
214
 
185
215
  const code = source.codePointAt(index);
@@ -194,21 +224,25 @@ export function truncate(text: unknown, width: number, ellipsis = '…'): string
194
224
  }
195
225
 
196
226
  const osc8Reset = openOsc8Term ? `\x1b]8;;${openOsc8Term}` : '';
197
- return out + osc8Reset + (sawEscape ? RESET : '') + ellipsis;
227
+ return out + osc8Reset + (sawEscape ? RESET : '') + safeEllipsis;
198
228
  }
199
229
 
200
230
  /** Pad to an exact column count, measuring visible width rather than bytes. */
201
231
  export function padEnd(text: unknown, width: number): string {
202
- const gap = width - visibleWidth(text);
203
- return gap > 0 ? String(text) + ' '.repeat(gap) : String(text);
232
+ const source = String(text == null ? '' : text);
233
+ const safeWidth = Number.isFinite(width) ? Math.floor(width) : 0;
234
+ const gap = safeWidth - visibleWidth(source);
235
+ return gap > 0 ? source + ' '.repeat(gap) : source;
204
236
  }
205
237
 
206
238
  export function wrap(text: unknown, width: number, _options?: { hang?: number }): string[] {
207
- if (width <= 0) return [String(text)];
239
+ const source = String(text == null ? '' : text);
240
+ const safeWidth = Number.isFinite(width) ? Math.floor(width) : (width === Infinity ? Infinity : 0);
241
+ if (safeWidth <= 0) return [source];
208
242
  const lines: string[] = [];
209
243
 
210
- for (const paragraph of String(text == null ? '' : text).split('\n')) {
211
- if (visibleWidth(paragraph) <= width) {
244
+ for (const paragraph of source.split('\n')) {
245
+ if (visibleWidth(paragraph) <= safeWidth) {
212
246
  lines.push(paragraph);
213
247
  continue;
214
248
  }
@@ -217,7 +251,7 @@ export function wrap(text: unknown, width: number, _options?: { hang?: number })
217
251
  for (const word of paragraph.split(' ')) {
218
252
  const candidate = current ? `${current} ${word}` : word;
219
253
 
220
- if (visibleWidth(candidate) <= width) {
254
+ if (visibleWidth(candidate) <= safeWidth) {
221
255
  current = candidate;
222
256
  continue;
223
257
  }
@@ -227,17 +261,31 @@ export function wrap(text: unknown, width: number, _options?: { hang?: number })
227
261
  }
228
262
 
229
263
  let remainder = word;
230
- while (visibleWidth(remainder) > width) {
231
- lines.push(truncate(remainder, width, ''));
232
- // truncate() gives us the head; drop the same number of characters.
264
+ while (visibleWidth(remainder) > safeWidth) {
265
+ const chunk = truncate(remainder, safeWidth, '');
266
+ // Drop the exact prefix, skipping escapes.
233
267
  let consumed = 0;
234
268
  let cut = 0;
235
- while (cut < remainder.length && consumed < width) {
269
+ while (cut < remainder.length) {
270
+ const esc = matchEscapeSequenceAt(remainder, cut);
271
+ if (esc) {
272
+ cut += esc.length;
273
+ continue;
274
+ }
236
275
  const code = remainder.codePointAt(cut);
237
276
  if (code === undefined) break;
238
- consumed += columnsFor(code);
277
+ const charWidth = columnsFor(code);
278
+ if (consumed + charWidth > safeWidth) {
279
+ if (cut === 0 || consumed === 0) {
280
+ cut += String.fromCodePoint(code).length;
281
+ }
282
+ break;
283
+ }
284
+ consumed += charWidth;
239
285
  cut += String.fromCodePoint(code).length;
240
286
  }
287
+ if (cut === 0) break;
288
+ lines.push(visibleWidth(chunk) > 0 ? chunk : remainder.slice(0, cut));
241
289
  remainder = remainder.slice(cut);
242
290
  }
243
291
  current = remainder;
@@ -260,16 +308,20 @@ export function wrap(text: unknown, width: number, _options?: { hang?: number })
260
308
  * terminal broke it at the right edge, mid-word, at column 0.
261
309
  */
262
310
  export function wrapBlock(text: unknown, width: number, { hang = 2 }: { hang?: number } = {}): string[] {
311
+ const safeHang = Number.isFinite(hang) ? Math.max(0, Math.floor(hang)) : 2;
312
+ const safeWidth = Number.isFinite(width) ? Math.floor(width) : 80;
263
313
  const rows: string[] = [];
264
- for (const line of String(text == null ? '' : text).split('\n')) {
265
- const indent = (line.match(/^[ \t]*/) || [''])[0];
314
+ for (const rawLine of String(text == null ? '' : text).split('\n')) {
315
+ const line = expandTabs(rawLine.replace(/\r$/, ''));
316
+ const indent = (line.match(/^ */) || [''])[0];
266
317
  const body = line.slice(indent.length);
267
318
  if (!body) { rows.push(''); continue; }
268
319
 
269
- const continuation = indent + ' '.repeat(hang);
320
+ const rawContinuation = indent + ' '.repeat(safeHang);
321
+ const continuation = visibleWidth(rawContinuation) < safeWidth ? rawContinuation : indent;
270
322
  // Wrap at the NARROWER of the two budgets so neither the first row nor a
271
323
  // continuation row can exceed `width`.
272
- const budget = Math.max(8, width - visibleWidth(continuation));
324
+ const budget = Math.max(1, safeWidth - visibleWidth(continuation));
273
325
  const parts = wrap(body, budget);
274
326
  rows.push(indent + (parts[0] ?? ''));
275
327
  for (const part of parts.slice(1)) rows.push(continuation + part);
@@ -320,11 +372,22 @@ export function expandTabs(text: unknown, tabWidth = TAB_WIDTH): string {
320
372
  const source = String(text == null ? '' : text);
321
373
  if (!source.includes('\t')) return source;
322
374
 
375
+ const step = Number.isFinite(tabWidth) && tabWidth >= 1 ? Math.floor(tabWidth) : TAB_WIDTH;
323
376
  let out = '';
324
377
  let column = 0;
325
- for (const char of source) {
378
+ let index = 0;
379
+ while (index < source.length) {
380
+ const esc = matchEscapeSequenceAt(source, index);
381
+ if (esc) {
382
+ out += esc;
383
+ index += esc.length;
384
+ continue;
385
+ }
386
+ const code = source.codePointAt(index);
387
+ if (code === undefined) break;
388
+ const char = String.fromCodePoint(code);
326
389
  if (char === '\t') {
327
- const advance = tabWidth - (column % tabWidth);
390
+ const advance = step - (column % step);
328
391
  out += ' '.repeat(advance);
329
392
  column += advance;
330
393
  } else if (char === '\n') {
@@ -332,8 +395,9 @@ export function expandTabs(text: unknown, tabWidth = TAB_WIDTH): string {
332
395
  column = 0;
333
396
  } else {
334
397
  out += char;
335
- column += 1;
398
+ column += columnsFor(code);
336
399
  }
400
+ index += char.length;
337
401
  }
338
402
  return out;
339
403
  }
@@ -355,7 +419,13 @@ export function sanitize(text: unknown): string {
355
419
  export function shortenHome(value: unknown): string {
356
420
  const home = os.homedir();
357
421
  const text = String(value == null ? '' : value);
358
- if (home && text.startsWith(home)) return '~' + text.slice(home.length);
422
+ const cleanHome = home.length > 1 ? home.replace(/[\\/]+$/, '') : home;
423
+ if (cleanHome && cleanHome !== '/') {
424
+ if (text === cleanHome) return '~';
425
+ if (text.startsWith(`${cleanHome}/`) || text.startsWith(`${cleanHome}\\`)) {
426
+ return `~${text.slice(cleanHome.length)}`;
427
+ }
428
+ }
359
429
  return text;
360
430
  }
361
431
 
@@ -1,4 +1,4 @@
1
- import { visibleWidth, wrap } from './text.js';
1
+ import { safeDisplay, truncate, visibleWidth, wrap } from './text.js';
2
2
  // The terminal palette remains a JavaScript module during the TypeScript migration.
3
3
  // @ts-expect-error theme.js does not have a declaration yet.
4
4
  import { paint } from './theme.js';
@@ -16,8 +16,10 @@ export function chatProseWidth(columns) {
16
16
  }
17
17
  /** Keep every line of a labeled message visible when the terminal is resized. */
18
18
  export function wrappedChatRows(message, columns, prefix, tone) {
19
- const inset = visibleWidth(prefix);
20
- const room = Math.max(1, chatTextWidth(columns) - inset);
19
+ const width = chatTextWidth(columns);
20
+ const safePrefix = truncate(String(prefix ?? '').replace(/[\x00-\x1a\x1c-\x1f\x7f-\x9f]/g, ' '), Math.max(0, width - 1), '');
21
+ const inset = visibleWidth(safePrefix);
22
+ const room = Math.max(1, width - inset);
21
23
  const continuation = ' '.repeat(inset);
22
- return wrap(message, room).map((line, index) => `${index === 0 ? prefix : continuation}${paint(tone, line)}`);
24
+ return wrap(safeDisplay(String(message ?? '')), room).map((line, index) => `${index === 0 ? safePrefix : continuation}${paint(tone, line)}`);
23
25
  }
@@ -1,4 +1,4 @@
1
- import { visibleWidth, wrap } from './text.js';
1
+ import { safeDisplay, truncate, visibleWidth, wrap } from './text.js';
2
2
  // The terminal palette remains a JavaScript module during the TypeScript migration.
3
3
  // @ts-expect-error theme.js does not have a declaration yet.
4
4
  import { paint } from './theme.js';
@@ -20,9 +20,15 @@ export function chatProseWidth(columns: number): number {
20
20
 
21
21
  /** Keep every line of a labeled message visible when the terminal is resized. */
22
22
  export function wrappedChatRows(message: string, columns: number, prefix: string, tone: MessageTone): string[] {
23
- const inset = visibleWidth(prefix);
24
- const room = Math.max(1, chatTextWidth(columns) - inset);
23
+ const width = chatTextWidth(columns);
24
+ const safePrefix = truncate(
25
+ String(prefix ?? '').replace(/[\x00-\x1a\x1c-\x1f\x7f-\x9f]/g, ' '),
26
+ Math.max(0, width - 1),
27
+ ''
28
+ );
29
+ const inset = visibleWidth(safePrefix);
30
+ const room = Math.max(1, width - inset);
25
31
  const continuation = ' '.repeat(inset);
26
- return wrap(message, room).map((line: string, index: number) =>
27
- `${index === 0 ? prefix : continuation}${paint(tone, line)}`);
32
+ return wrap(safeDisplay(String(message ?? '')), room).map((line: string, index: number) =>
33
+ `${index === 0 ? safePrefix : continuation}${paint(tone, line)}`);
28
34
  }
package/src/tui/theme.js CHANGED
@@ -233,16 +233,20 @@ export function fill(role, text) {
233
233
 
234
234
  /** A filled badge — used for risk banners, where a fill reads faster than text. */
235
235
  export function badge(role, text) {
236
- if (!HAS_COLOR) return `[${text}]`;
237
- const rgb = PALETTE[role];
238
- const label = ` ${text} `;
236
+ const value = text == null ? '' : String(text);
237
+ const rgb = typeof role === 'string' && Object.prototype.hasOwnProperty.call(PALETTE, role)
238
+ ? PALETTE[role]
239
+ : null;
240
+ if (!HAS_COLOR || !rgb) return `[${value}]`;
241
+ const label = ` ${value} `;
239
242
  if (COLOR_DEPTH >= 16777216) {
240
243
  return `\x1b[48;2;${rgb[0]};${rgb[1]};${rgb[2]}m\x1b[38;2;16;16;20m\x1b[1m${label}${RESET}`;
241
244
  }
242
245
  if (COLOR_DEPTH >= 256) {
243
246
  return `\x1b[48;5;${rgbTo256(rgb[0], rgb[1], rgb[2])}m\x1b[30m\x1b[1m${label}${RESET}`;
244
247
  }
245
- return `\x1b[${(BASIC_CODES[role] || 39) + 10}m\x1b[30m${label}${RESET}`;
248
+ const basicCode = Object.prototype.hasOwnProperty.call(BASIC_CODES, role) ? BASIC_CODES[role] : 39;
249
+ return `\x1b[${basicCode + 10}m\x1b[30m${label}${RESET}`;
246
250
  }
247
251
 
248
252
  /** Glyph set, degrading to ASCII where Unicode is unsafe. */
@@ -364,8 +368,10 @@ export function safeLink(text, url) {
364
368
 
365
369
  /** A file path as a clickable file:// link, absolute as OSC 8 requires. */
366
370
  export function fileLink(text, absolutePath) {
367
- if (!HAS_HYPERLINKS || !absolutePath) return String(text);
368
- return link(text, 'file://' + encodeURI(absolutePath));
371
+ const pathText = typeof absolutePath === 'string' ? absolutePath : '';
372
+ // eslint-disable-next-line no-control-regex
373
+ if (!HAS_HYPERLINKS || !pathText || /[\x00-\x1f\x7f]/.test(pathText)) return String(text);
374
+ return link(text, 'file://' + encodeURI(pathText));
369
375
  }
370
376
 
371
377
  /**
@@ -378,7 +384,9 @@ export function fileLink(text, absolutePath) {
378
384
  * the box is that it is the surface they are typing into.
379
385
  */
380
386
  export function terminalWidth() {
381
- const raw = process.stdout.columns || 80;
387
+ const raw = Number.isFinite(process.stdout.columns) && process.stdout.columns > 0
388
+ ? Math.floor(process.stdout.columns)
389
+ : 80;
382
390
  return Math.max(40, raw);
383
391
  }
384
392
 
@@ -398,7 +406,8 @@ export function terminalWidth() {
398
406
  export const READABLE_MAX = 100;
399
407
 
400
408
  export function readableWidth(available = terminalWidth()) {
401
- return Math.max(40, Math.min(available, READABLE_MAX));
409
+ const width = Number.isFinite(available) ? Math.floor(available) : terminalWidth();
410
+ return Math.max(40, Math.min(width, READABLE_MAX));
402
411
  }
403
412
 
404
413
  export { getEngineFrames, getEngineFrame };