@causyne/agent 1.20.112 → 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
@@ -24,13 +24,24 @@ import { contextMeter } from './block.js';
24
24
  import { CHARS_PER_TOKEN } from '../contextManager.js';
25
25
  import { formatAnimatedStatus } from './engineSpinner.js';
26
26
  import { renderRunningLight } from './runningLight.js';
27
+ function nonNegativeFinite(value) {
28
+ const n = Number(value);
29
+ return Number.isFinite(n) && n > 0 ? n : 0;
30
+ }
31
+ function oneLineDisplay(value) {
32
+ return safeDisplay(value).replace(/[\r\n]+/g, ' ');
33
+ }
27
34
  /** 1234 -> "1.2k", 1234567 -> "1.2M". Token counts are read, not summed. */
28
35
  export function compactCount(value) {
29
- const n = Number(value) || 0;
30
- if (n < 1000)
31
- return String(Math.round(n));
32
- if (n < 1000000)
33
- return `${(n / 1000).toFixed(n < 10000 ? 1 : 0)}k`;
36
+ const n = nonNegativeFinite(value);
37
+ const rounded = Math.round(n);
38
+ if (rounded < 1000)
39
+ return String(rounded);
40
+ if (n < 9950)
41
+ return `${(n / 1000).toFixed(1)}k`;
42
+ const thousands = Math.round(n / 1000);
43
+ if (thousands < 1000)
44
+ return `${thousands}k`;
34
45
  return `${(n / 1000000).toFixed(1)}M`;
35
46
  }
36
47
  /**
@@ -40,7 +51,7 @@ export function compactCount(value) {
40
51
  * to look at the cost at all.
41
52
  */
42
53
  export function formatCost(value) {
43
- const n = Number(value) || 0;
54
+ const n = nonNegativeFinite(value);
44
55
  if (n <= 0)
45
56
  return '$0';
46
57
  if (n < 0.01)
@@ -50,9 +61,10 @@ export function formatCost(value) {
50
61
  return `$${n.toFixed(2)}`;
51
62
  }
52
63
  export function formatElapsed(ms) {
53
- const total = Math.max(0, Math.round(ms / 1000));
64
+ const safeMs = nonNegativeFinite(ms);
65
+ const total = Math.round(safeMs / 1000);
54
66
  if (total < 60)
55
- return `${(ms / 1000).toFixed(1)}s`;
67
+ return `${(safeMs / 1000).toFixed(1)}s`;
56
68
  const minutes = Math.floor(total / 60);
57
69
  const seconds = total % 60;
58
70
  if (minutes < 60)
@@ -61,8 +73,12 @@ export function formatElapsed(ms) {
61
73
  }
62
74
  /** Strip a provider prefix so "anthropic/claude-opus-5" reads as the model. */
63
75
  function shortModel(model) {
64
- const name = String(model || '').split('/').pop() || '';
65
- return name.length > 22 ? name.slice(0, 21) + GLYPHS.ellipsis : name;
76
+ const name = oneLineDisplay(model || '').split('/').pop() || '';
77
+ return visibleWidth(name) > 22 ? truncate(name, 22, GLYPHS.ellipsis) : name;
78
+ }
79
+ function finiteWidth(width) {
80
+ const numeric = Number(width ?? 80);
81
+ return Number.isFinite(numeric) ? Math.max(2, Math.floor(numeric)) : 80;
66
82
  }
67
83
  export class StatusLine {
68
84
  model = '';
@@ -79,20 +95,27 @@ export class StatusLine {
79
95
  constructor() {
80
96
  this.reset();
81
97
  }
98
+ applyContextWindow(chars, limit) {
99
+ const safeChars = nonNegativeFinite(chars);
100
+ if (safeChars > 0 || chars === 0)
101
+ this.contextChars = safeChars;
102
+ const safeLimit = nonNegativeFinite(limit);
103
+ if (safeLimit > 0 || limit === 0)
104
+ this.contextLimit = safeLimit;
105
+ }
82
106
  /** Session-lifetime facts, set once from the same info as the header. */
83
107
  setSession(info = {}) {
108
+ if (!info || typeof info !== 'object')
109
+ return this;
84
110
  if (info.model !== undefined)
85
111
  this.model = shortModel(info.model);
86
112
  if (info.provider !== undefined)
87
- this.provider = info.provider ? String(info.provider) : '';
113
+ this.provider = info.provider ? oneLineDisplay(info.provider) : '';
88
114
  if (info.tier !== undefined)
89
- this.tier = info.tier ? String(info.tier) : '';
115
+ this.tier = info.tier ? oneLineDisplay(info.tier) : '';
90
116
  if (info.cwd !== undefined)
91
- this.cwd = info.cwd ? String(info.cwd) : '';
92
- if (Number(info.contextLimit) > 0)
93
- this.contextLimit = Number(info.contextLimit);
94
- if (Number(info.contextChars) > 0)
95
- this.contextChars = Number(info.contextChars);
117
+ this.cwd = info.cwd ? oneLineDisplay(info.cwd) : '';
118
+ this.applyContextWindow(info.contextChars, info.contextLimit);
96
119
  return this;
97
120
  }
98
121
  /** Per-turn counters. */
@@ -107,14 +130,17 @@ export class StatusLine {
107
130
  /**
108
131
  * The idle footer: where you are, and how much context is left.
109
132
  */
110
- renderIdle({ width = 80, hint = '' } = {}) {
111
- const budget = Math.max(10, width - 1);
133
+ renderIdle(options = {}) {
134
+ const { width = 80, hint = '' } = options || {};
135
+ const safeWidth = finiteWidth(width);
136
+ const budget = Math.max(1, safeWidth - 1);
112
137
  const meter = contextMeter(Math.round(this.contextChars / CHARS_PER_TOKEN), Math.round(this.contextLimit / CHARS_PER_TOKEN));
113
138
  const segments = [];
114
139
  if (meter)
115
140
  segments.push(meter);
116
- if (hint)
117
- segments.push(hint);
141
+ const cleanHint = hint ? oneLineDisplay(hint) : '';
142
+ if (cleanHint)
143
+ segments.push(cleanHint);
118
144
  const separator = ` ${GLYPHS.dot} `;
119
145
  const kept = segments.slice();
120
146
  let tail = kept.join(separator);
@@ -125,43 +151,48 @@ export class StatusLine {
125
151
  if (visibleWidth(tail) > budget)
126
152
  tail = '';
127
153
  const room = tail ? Math.max(0, budget - visibleWidth(tail) - visibleWidth(separator)) : budget;
128
- const where = this.cwd ? truncate(shortenHome(this.cwd), room, GLYPHS.ellipsis) : '';
154
+ const where = this.cwd && room > 0 ? truncate(shortenHome(oneLineDisplay(this.cwd)), room, GLYPHS.ellipsis) : '';
129
155
  const left = dim(where);
130
156
  if (!tail)
131
157
  return left;
132
- const gap = Math.max(visibleWidth(separator), width - 1 - visibleWidth(left) - visibleWidth(tail));
158
+ if (!where)
159
+ return dim(tail);
160
+ const gap = Math.max(visibleWidth(separator), safeWidth - 1 - visibleWidth(left) - visibleWidth(tail));
133
161
  return `${left}${' '.repeat(gap)}${dim(tail)}`;
134
162
  }
135
163
  /**
136
164
  * Consume a `usage_update` frame.
137
165
  */
138
166
  onUsage(msg = {}) {
139
- this.turnTokens = Number(msg.taskTokens) || Number(msg.usedTokens) || this.turnTokens;
140
- if (Number(msg.contextChars) > 0)
141
- this.contextChars = Number(msg.contextChars);
142
- if (Number(msg.contextLimitChars) > 0)
143
- this.contextLimit = Number(msg.contextLimitChars);
144
- const responseCost = Number(msg.costUsd) || 0;
145
- const taskCost = Number(msg.taskCostUsd) || 0;
167
+ const usage = msg && typeof msg === 'object' ? msg : {};
168
+ this.turnTokens = nonNegativeFinite(usage.taskTokens) || nonNegativeFinite(usage.usedTokens) || this.turnTokens;
169
+ this.applyContextWindow(usage.contextChars, usage.contextLimitChars);
170
+ const responseCost = nonNegativeFinite(usage.costUsd);
171
+ const taskCost = nonNegativeFinite(usage.taskCostUsd);
146
172
  this.turnCost = taskCost > 0 ? taskCost : this.turnCost + responseCost;
147
- this.cachedTokens = Number(msg.cachedTokens) || 0;
173
+ this.cachedTokens = nonNegativeFinite(usage.cachedTokens);
148
174
  return this;
149
175
  }
150
176
  /**
151
177
  * Build the line.
152
178
  */
153
- render({ message = '', elapsedMs = 0, steps = 0, width = 80, spinnerFrame = '', tick } = {}) {
154
- const head = spinnerFrame
155
- ? (typeof spinnerFrame === 'string' && spinnerFrame.includes('\x1b')
156
- ? `${spinnerFrame} `
157
- : `${paint('accent', spinnerFrame)} `)
158
- : '';
159
- const budget = Math.max(10, width - visibleWidth(head) - 1);
179
+ render(options = {}) {
180
+ const { message = '', elapsedMs = 0, steps = 0, width = 80, spinnerFrame = '', tick } = options || {};
181
+ const safeWidth = finiteWidth(width);
182
+ const safeElapsedMs = nonNegativeFinite(elapsedMs);
183
+ const safeSteps = Math.floor(nonNegativeFinite(steps));
184
+ const safeTick = tick !== undefined && Number.isFinite(tick) ? Math.floor(tick) : undefined;
185
+ const maxTotal = Math.max(1, safeWidth - 1);
186
+ // Even a precolored frame may be supplied by a tool; render only
187
+ // sanitized glyphs so it cannot move the cursor or clear the screen.
188
+ const rawHead = spinnerFrame ? `${paint('accent', oneLineDisplay(spinnerFrame))} ` : '';
189
+ const head = visibleWidth(rawHead) < maxTotal ? rawHead : '';
190
+ const budget = Math.max(1, maxTotal - visibleWidth(head));
160
191
  const segments = [];
161
- if (elapsedMs > 0)
162
- segments.push(formatElapsed(elapsedMs));
163
- if (steps > 0)
164
- segments.push(`step ${steps}`);
192
+ if (safeElapsedMs > 0)
193
+ segments.push(formatElapsed(safeElapsedMs));
194
+ if (safeSteps > 0)
195
+ segments.push(`step ${safeSteps}`);
165
196
  if (this.turnTokens > 0)
166
197
  segments.push(`${compactCount(this.turnTokens)} tok`);
167
198
  if (this.turnCost > 0)
@@ -171,28 +202,29 @@ export class StatusLine {
171
202
  if (this.model)
172
203
  segments.push(this.model);
173
204
  const separator = ` ${GLYPHS.dot} `;
174
- const rawMessage = tick !== undefined
175
- ? formatAnimatedStatus(message || 'working', tick)
205
+ const sepWidth = visibleWidth(separator);
206
+ const rawMessage = safeTick !== undefined
207
+ ? formatAnimatedStatus(message || 'working', safeTick)
176
208
  : (message || (spinnerFrame ? 'working' : ''));
177
- const text = safeDisplay(rawMessage);
209
+ const text = oneLineDisplay(rawMessage);
178
210
  const kept = segments.slice();
179
211
  const wanted = Math.min(visibleWidth(text), Math.max(18, Math.floor(budget / 2)));
180
212
  while (kept.length > 0) {
181
213
  const tail = kept.join(separator);
182
- if (visibleWidth(tail) + separator.length + wanted <= budget)
214
+ if (visibleWidth(tail) + sepWidth + wanted <= budget)
183
215
  break;
184
216
  kept.pop();
185
217
  }
186
218
  const tail = kept.length > 0 ? kept.join(separator) : '';
187
219
  const messageBudget = tail
188
- ? Math.max(1, budget - visibleWidth(tail) - separator.length)
220
+ ? Math.max(1, budget - visibleWidth(tail) - sepWidth)
189
221
  : budget;
190
222
  const body = truncate(text, messageBudget, GLYPHS.ellipsis);
191
- const isAnimating = tick !== undefined || elapsedMs > 0 || Boolean(spinnerFrame);
192
- const shimmerNowMs = elapsedMs > 0 ? elapsedMs : (tick !== undefined ? tick * 80 : 320);
223
+ const isAnimating = safeTick !== undefined || safeElapsedMs > 0 || Boolean(spinnerFrame);
224
+ const shimmerNowMs = safeElapsedMs > 0 ? safeElapsedMs : (safeTick !== undefined ? Math.max(0, safeTick) * 80 : 320);
193
225
  if (isAnimating && this.onTick) {
194
226
  try {
195
- this.onTick(shimmerNowMs, tick);
227
+ this.onTick(shimmerNowMs, safeTick);
196
228
  }
197
229
  catch {
198
230
  // Never allow a tick observer to disrupt status rendering
@@ -204,16 +236,16 @@ export class StatusLine {
204
236
  const left = `${head}${styledBody}`;
205
237
  if (!tail)
206
238
  return left;
207
- const gap = Math.max(visibleWidth(separator), width - 1 - visibleWidth(left) - visibleWidth(tail));
239
+ const gap = Math.max(visibleWidth(separator), safeWidth - 1 - visibleWidth(left) - visibleWidth(tail));
208
240
  return `${left}${' '.repeat(gap)}${dim(tail)}`;
209
241
  }
210
242
  /**
211
243
  * The non-TTY form: no spinner, no elapsed clock, printed once.
212
244
  */
213
245
  renderStatic(message, width = 80) {
214
- const parts = [safeDisplay(message)];
246
+ const parts = [oneLineDisplay(message)];
215
247
  if (this.model)
216
248
  parts.push(this.model);
217
- return truncate(`${GLYPHS.dot} ${parts.join(` ${GLYPHS.dot} `)}`, width, GLYPHS.ellipsis);
249
+ return truncate(`${GLYPHS.dot} ${parts.join(` ${GLYPHS.dot} `)}`, finiteWidth(width), GLYPHS.ellipsis);
218
250
  }
219
251
  }
@@ -26,11 +26,23 @@ import { CHARS_PER_TOKEN } from '../contextManager.js';
26
26
  import { formatAnimatedStatus } from './engineSpinner.js';
27
27
  import { renderRunningLight } from './runningLight.js';
28
28
 
29
+ function nonNegativeFinite(value: unknown): number {
30
+ const n = Number(value);
31
+ return Number.isFinite(n) && n > 0 ? n : 0;
32
+ }
33
+
34
+ function oneLineDisplay(value: unknown): string {
35
+ return safeDisplay(value).replace(/[\r\n]+/g, ' ');
36
+ }
37
+
29
38
  /** 1234 -> "1.2k", 1234567 -> "1.2M". Token counts are read, not summed. */
30
39
  export function compactCount(value: unknown): string {
31
- const n = Number(value) || 0;
32
- if (n < 1000) return String(Math.round(n));
33
- if (n < 1000000) return `${(n / 1000).toFixed(n < 10000 ? 1 : 0)}k`;
40
+ const n = nonNegativeFinite(value);
41
+ const rounded = Math.round(n);
42
+ if (rounded < 1000) return String(rounded);
43
+ if (n < 9950) return `${(n / 1000).toFixed(1)}k`;
44
+ const thousands = Math.round(n / 1000);
45
+ if (thousands < 1000) return `${thousands}k`;
34
46
  return `${(n / 1000000).toFixed(1)}M`;
35
47
  }
36
48
 
@@ -41,7 +53,7 @@ export function compactCount(value: unknown): string {
41
53
  * to look at the cost at all.
42
54
  */
43
55
  export function formatCost(value: unknown): string {
44
- const n = Number(value) || 0;
56
+ const n = nonNegativeFinite(value);
45
57
  if (n <= 0) return '$0';
46
58
  if (n < 0.01) return `$${n.toFixed(4)}`;
47
59
  if (n < 1) return `$${n.toFixed(3)}`;
@@ -49,8 +61,9 @@ export function formatCost(value: unknown): string {
49
61
  }
50
62
 
51
63
  export function formatElapsed(ms: number): string {
52
- const total = Math.max(0, Math.round(ms / 1000));
53
- if (total < 60) return `${(ms / 1000).toFixed(1)}s`;
64
+ const safeMs = nonNegativeFinite(ms);
65
+ const total = Math.round(safeMs / 1000);
66
+ if (total < 60) return `${(safeMs / 1000).toFixed(1)}s`;
54
67
  const minutes = Math.floor(total / 60);
55
68
  const seconds = total % 60;
56
69
  if (minutes < 60) return `${minutes}m${String(seconds).padStart(2, '0')}s`;
@@ -59,8 +72,8 @@ export function formatElapsed(ms: number): string {
59
72
 
60
73
  /** Strip a provider prefix so "anthropic/claude-opus-5" reads as the model. */
61
74
  function shortModel(model: unknown): string {
62
- const name = String(model || '').split('/').pop() || '';
63
- return name.length > 22 ? name.slice(0, 21) + GLYPHS.ellipsis : name;
75
+ const name = oneLineDisplay(model || '').split('/').pop() || '';
76
+ return visibleWidth(name) > 22 ? truncate(name, 22, GLYPHS.ellipsis) : name;
64
77
  }
65
78
 
66
79
  export interface StatusLineSessionInfo {
@@ -86,6 +99,11 @@ export interface StatusLineIdleOptions {
86
99
  hint?: string | undefined;
87
100
  }
88
101
 
102
+ function finiteWidth(width: number | undefined): number {
103
+ const numeric = Number(width ?? 80);
104
+ return Number.isFinite(numeric) ? Math.max(2, Math.floor(numeric)) : 80;
105
+ }
106
+
89
107
  export class StatusLine {
90
108
  model = '';
91
109
  provider = '';
@@ -104,14 +122,21 @@ export class StatusLine {
104
122
  this.reset();
105
123
  }
106
124
 
125
+ private applyContextWindow(chars: unknown, limit: unknown): void {
126
+ const safeChars = nonNegativeFinite(chars);
127
+ if (safeChars > 0 || chars === 0) this.contextChars = safeChars;
128
+ const safeLimit = nonNegativeFinite(limit);
129
+ if (safeLimit > 0 || limit === 0) this.contextLimit = safeLimit;
130
+ }
131
+
107
132
  /** Session-lifetime facts, set once from the same info as the header. */
108
- setSession(info: StatusLineSessionInfo = {}): this {
133
+ setSession(info: StatusLineSessionInfo | null = {}): this {
134
+ if (!info || typeof info !== 'object') return this;
109
135
  if (info.model !== undefined) this.model = shortModel(info.model);
110
- if (info.provider !== undefined) this.provider = info.provider ? String(info.provider) : '';
111
- if (info.tier !== undefined) this.tier = info.tier ? String(info.tier) : '';
112
- if (info.cwd !== undefined) this.cwd = info.cwd ? String(info.cwd) : '';
113
- if (Number(info.contextLimit) > 0) this.contextLimit = Number(info.contextLimit);
114
- if (Number(info.contextChars) > 0) this.contextChars = Number(info.contextChars);
136
+ if (info.provider !== undefined) this.provider = info.provider ? oneLineDisplay(info.provider) : '';
137
+ if (info.tier !== undefined) this.tier = info.tier ? oneLineDisplay(info.tier) : '';
138
+ if (info.cwd !== undefined) this.cwd = info.cwd ? oneLineDisplay(info.cwd) : '';
139
+ this.applyContextWindow(info.contextChars, info.contextLimit);
115
140
  return this;
116
141
  }
117
142
 
@@ -128,15 +153,18 @@ export class StatusLine {
128
153
  /**
129
154
  * The idle footer: where you are, and how much context is left.
130
155
  */
131
- renderIdle({ width = 80, hint = '' }: StatusLineIdleOptions = {}): string {
132
- const budget = Math.max(10, width - 1);
156
+ renderIdle(options: StatusLineIdleOptions | null = {}): string {
157
+ const { width = 80, hint = '' } = options || {};
158
+ const safeWidth = finiteWidth(width);
159
+ const budget = Math.max(1, safeWidth - 1);
133
160
  const meter = contextMeter(
134
161
  Math.round(this.contextChars / CHARS_PER_TOKEN),
135
162
  Math.round(this.contextLimit / CHARS_PER_TOKEN)
136
163
  );
137
164
  const segments: string[] = [];
138
165
  if (meter) segments.push(meter);
139
- if (hint) segments.push(hint);
166
+ const cleanHint = hint ? oneLineDisplay(hint) : '';
167
+ if (cleanHint) segments.push(cleanHint);
140
168
 
141
169
  const separator = ` ${GLYPHS.dot} `;
142
170
  const kept = segments.slice();
@@ -148,13 +176,14 @@ export class StatusLine {
148
176
  if (visibleWidth(tail) > budget) tail = '';
149
177
 
150
178
  const room = tail ? Math.max(0, budget - visibleWidth(tail) - visibleWidth(separator)) : budget;
151
- const where = this.cwd ? truncate(shortenHome(this.cwd), room, GLYPHS.ellipsis) : '';
179
+ const where = this.cwd && room > 0 ? truncate(shortenHome(oneLineDisplay(this.cwd)), room, GLYPHS.ellipsis) : '';
152
180
  const left = dim(where);
153
181
  if (!tail) return left;
182
+ if (!where) return dim(tail);
154
183
 
155
184
  const gap = Math.max(
156
185
  visibleWidth(separator),
157
- width - 1 - visibleWidth(left) - visibleWidth(tail)
186
+ safeWidth - 1 - visibleWidth(left) - visibleWidth(tail)
158
187
  );
159
188
  return `${left}${' '.repeat(gap)}${dim(tail)}`;
160
189
  }
@@ -162,61 +191,68 @@ export class StatusLine {
162
191
  /**
163
192
  * Consume a `usage_update` frame.
164
193
  */
165
- onUsage(msg: Record<string, unknown> = {}): this {
166
- this.turnTokens = Number(msg.taskTokens) || Number(msg.usedTokens) || this.turnTokens;
167
- if (Number(msg.contextChars) > 0) this.contextChars = Number(msg.contextChars);
168
- if (Number(msg.contextLimitChars) > 0) this.contextLimit = Number(msg.contextLimitChars);
169
- const responseCost = Number(msg.costUsd) || 0;
170
- const taskCost = Number(msg.taskCostUsd) || 0;
194
+ onUsage(msg: Record<string, unknown> | null = {}): this {
195
+ const usage = msg && typeof msg === 'object' ? msg : {};
196
+ this.turnTokens = nonNegativeFinite(usage.taskTokens) || nonNegativeFinite(usage.usedTokens) || this.turnTokens;
197
+ this.applyContextWindow(usage.contextChars, usage.contextLimitChars);
198
+ const responseCost = nonNegativeFinite(usage.costUsd);
199
+ const taskCost = nonNegativeFinite(usage.taskCostUsd);
171
200
  this.turnCost = taskCost > 0 ? taskCost : this.turnCost + responseCost;
172
- this.cachedTokens = Number(msg.cachedTokens) || 0;
201
+ this.cachedTokens = nonNegativeFinite(usage.cachedTokens);
173
202
  return this;
174
203
  }
175
204
 
176
205
  /**
177
206
  * Build the line.
178
207
  */
179
- render({ message = '', elapsedMs = 0, steps = 0, width = 80, spinnerFrame = '', tick }: StatusLineRenderOptions = {}): string {
180
- const head = spinnerFrame
181
- ? (typeof spinnerFrame === 'string' && spinnerFrame.includes('\x1b')
182
- ? `${spinnerFrame} `
183
- : `${paint('accent', spinnerFrame)} `)
184
- : '';
185
- const budget = Math.max(10, width - visibleWidth(head) - 1);
208
+ render(options: StatusLineRenderOptions | null = {}): string {
209
+ const { message = '', elapsedMs = 0, steps = 0, width = 80, spinnerFrame = '', tick } = options || {};
210
+ const safeWidth = finiteWidth(width);
211
+ const safeElapsedMs = nonNegativeFinite(elapsedMs);
212
+ const safeSteps = Math.floor(nonNegativeFinite(steps));
213
+ const safeTick = tick !== undefined && Number.isFinite(tick) ? Math.floor(tick) : undefined;
214
+
215
+ const maxTotal = Math.max(1, safeWidth - 1);
216
+ // Even a precolored frame may be supplied by a tool; render only
217
+ // sanitized glyphs so it cannot move the cursor or clear the screen.
218
+ const rawHead = spinnerFrame ? `${paint('accent', oneLineDisplay(spinnerFrame))} ` : '';
219
+ const head = visibleWidth(rawHead) < maxTotal ? rawHead : '';
220
+ const budget = Math.max(1, maxTotal - visibleWidth(head));
186
221
 
187
222
  const segments: string[] = [];
188
- if (elapsedMs > 0) segments.push(formatElapsed(elapsedMs));
189
- if (steps > 0) segments.push(`step ${steps}`);
223
+ if (safeElapsedMs > 0) segments.push(formatElapsed(safeElapsedMs));
224
+ if (safeSteps > 0) segments.push(`step ${safeSteps}`);
190
225
  if (this.turnTokens > 0) segments.push(`${compactCount(this.turnTokens)} tok`);
191
226
  if (this.turnCost > 0) segments.push(formatCost(this.turnCost));
192
227
  if (this.cachedTokens > 0) segments.push(`${compactCount(this.cachedTokens)} cached`);
193
228
  if (this.model) segments.push(this.model);
194
229
 
195
230
  const separator = ` ${GLYPHS.dot} `;
196
- const rawMessage = tick !== undefined
197
- ? formatAnimatedStatus(message || 'working', tick)
231
+ const sepWidth = visibleWidth(separator);
232
+ const rawMessage = safeTick !== undefined
233
+ ? formatAnimatedStatus(message || 'working', safeTick)
198
234
  : (message || (spinnerFrame ? 'working' : ''));
199
- const text = safeDisplay(rawMessage);
235
+ const text = oneLineDisplay(rawMessage);
200
236
 
201
237
  const kept = segments.slice();
202
238
  const wanted = Math.min(visibleWidth(text), Math.max(18, Math.floor(budget / 2)));
203
239
  while (kept.length > 0) {
204
240
  const tail = kept.join(separator);
205
- if (visibleWidth(tail) + separator.length + wanted <= budget) break;
241
+ if (visibleWidth(tail) + sepWidth + wanted <= budget) break;
206
242
  kept.pop();
207
243
  }
208
244
 
209
245
  const tail = kept.length > 0 ? kept.join(separator) : '';
210
246
  const messageBudget = tail
211
- ? Math.max(1, budget - visibleWidth(tail) - separator.length)
247
+ ? Math.max(1, budget - visibleWidth(tail) - sepWidth)
212
248
  : budget;
213
249
  const body = truncate(text, messageBudget, GLYPHS.ellipsis);
214
250
 
215
- const isAnimating = tick !== undefined || elapsedMs > 0 || Boolean(spinnerFrame);
216
- const shimmerNowMs = elapsedMs > 0 ? elapsedMs : (tick !== undefined ? tick * 80 : 320);
251
+ const isAnimating = safeTick !== undefined || safeElapsedMs > 0 || Boolean(spinnerFrame);
252
+ const shimmerNowMs = safeElapsedMs > 0 ? safeElapsedMs : (safeTick !== undefined ? Math.max(0, safeTick) * 80 : 320);
217
253
  if (isAnimating && this.onTick) {
218
254
  try {
219
- this.onTick(shimmerNowMs, tick);
255
+ this.onTick(shimmerNowMs, safeTick);
220
256
  } catch {
221
257
  // Never allow a tick observer to disrupt status rendering
222
258
  }
@@ -229,7 +265,7 @@ export class StatusLine {
229
265
 
230
266
  const gap = Math.max(
231
267
  visibleWidth(separator),
232
- width - 1 - visibleWidth(left) - visibleWidth(tail)
268
+ safeWidth - 1 - visibleWidth(left) - visibleWidth(tail)
233
269
  );
234
270
  return `${left}${' '.repeat(gap)}${dim(tail)}`;
235
271
  }
@@ -238,8 +274,8 @@ export class StatusLine {
238
274
  * The non-TTY form: no spinner, no elapsed clock, printed once.
239
275
  */
240
276
  renderStatic(message: string, width = 80): string {
241
- const parts = [safeDisplay(message)];
277
+ const parts = [oneLineDisplay(message)];
242
278
  if (this.model) parts.push(this.model);
243
- return truncate(`${GLYPHS.dot} ${parts.join(` ${GLYPHS.dot} `)}`, width, GLYPHS.ellipsis);
279
+ return truncate(`${GLYPHS.dot} ${parts.join(` ${GLYPHS.dot} `)}`, finiteWidth(width), GLYPHS.ellipsis);
244
280
  }
245
281
  }
@@ -22,6 +22,7 @@ export class TerminalInputFramer {
22
22
  }
23
23
 
24
24
  push(input) {
25
+ if (input == null) return [];
25
26
  const incoming = Buffer.isBuffer(input) ? input : Buffer.from(String(input), 'utf8');
26
27
  if (incoming.length === 0) return [];
27
28
  const bytes = this.pending.length ? Buffer.concat([this.pending, incoming]) : incoming;
@@ -66,6 +67,7 @@ const SGR = /\x1b\[[0-9;]*m/g;
66
67
  * regression test expects to arrive as a single multiline editor insertion.
67
68
  */
68
69
  export function shouldTreatAsAtomicPaste(raw, decoder) {
70
+ if (raw == null) return false;
69
71
  let text;
70
72
  if (typeof raw === 'string') text = raw;
71
73
  else {
@@ -91,8 +93,9 @@ export function shouldTreatAsAtomicPaste(raw, decoder) {
91
93
 
92
94
  /** Display a short, non-sensitive paste notification (not the pasted content). */
93
95
  export function pasteSummary(text) {
94
- const lines = 1 + (text.match(/\n/g)?.length ?? 0);
95
- const size = Buffer.byteLength(text, 'utf8');
96
+ const normalized = String(text == null ? '' : text).replace(/\r\n/g, '\n').replace(/\r/g, '\n');
97
+ const lines = 1 + (normalized.match(/\n/g)?.length ?? 0);
98
+ const size = Buffer.byteLength(normalized, 'utf8');
96
99
  const sizeLabel = size >= 1024 ? `${Math.round(size / 1024)} KiB` : `${size} B`;
97
100
  return `pasted ${lines} ${lines === 1 ? 'line' : 'lines'} · ${sizeLabel}`;
98
101
  }