@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
@@ -25,29 +25,36 @@ export class AdaptiveWheelDampener {
25
25
  minTickInterval;
26
26
  burstWindowMs;
27
27
  maxBurstNotches;
28
- lastTick = 0;
28
+ lastTick = null;
29
29
  lastDirection = null;
30
30
  rowAccumulator = 0;
31
31
  recentBursts = [];
32
32
  constructor(options = {}) {
33
- this.minDamping = options.minDamping ?? 0.28;
34
- this.thresholdMs = options.thresholdMs ?? 85;
35
- this.minTickInterval = options.minTickInterval ?? 6;
36
- this.burstWindowMs = options.burstWindowMs ?? 120;
37
- this.maxBurstNotches = options.maxBurstNotches ?? 5.0;
33
+ const finiteNonNeg = (value, fallback) => value !== undefined && Number.isFinite(value) && value >= 0 ? value : fallback;
34
+ const rawMinDamping = options.minDamping;
35
+ this.minDamping = rawMinDamping !== undefined && Number.isFinite(rawMinDamping)
36
+ ? Math.min(1, Math.max(0, rawMinDamping))
37
+ : 0.28;
38
+ this.thresholdMs = finiteNonNeg(options.thresholdMs, 85);
39
+ this.minTickInterval = finiteNonNeg(options.minTickInterval, 6);
40
+ this.burstWindowMs = finiteNonNeg(options.burstWindowMs, 120);
41
+ this.maxBurstNotches = finiteNonNeg(options.maxBurstNotches, 5.0);
38
42
  }
39
43
  /**
40
44
  * Compute adaptive fractional notch multiplier for viewports that possess
41
45
  * their own sub-row accumulator (e.g. Viewport).
42
46
  */
43
47
  computeNotches(direction, now = Date.now()) {
48
+ if (!Number.isFinite(now)) {
49
+ return 1.0;
50
+ }
44
51
  if (this.lastDirection && this.lastDirection !== direction) {
45
52
  this.recentBursts = [];
46
- this.lastTick = 0;
53
+ this.lastTick = null;
47
54
  this.rowAccumulator = 0;
48
55
  }
49
56
  this.lastDirection = direction;
50
- const gap = this.lastTick ? Math.max(0, now - this.lastTick) : Infinity;
57
+ const gap = this.lastTick !== null ? Math.max(0, now - this.lastTick) : Infinity;
51
58
  this.lastTick = now;
52
59
  if (gap === Infinity || gap > 150) {
53
60
  this.recentBursts = [];
@@ -56,7 +63,8 @@ export class AdaptiveWheelDampener {
56
63
  if (gap < this.minTickInterval) {
57
64
  return 1.0;
58
65
  }
59
- let damping = Math.max(this.minDamping, Math.min(1.0, gap / this.thresholdMs));
66
+ const ratio = this.thresholdMs > 0 ? gap / this.thresholdMs : 1.0;
67
+ let damping = Math.max(this.minDamping, Math.min(1.0, ratio));
60
68
  const windowStart = now - this.burstWindowMs;
61
69
  this.recentBursts = this.recentBursts.filter(b => b.time >= windowStart);
62
70
  const windowNotches = this.recentBursts.reduce((sum, b) => sum + b.notches, 0);
@@ -71,6 +79,9 @@ export class AdaptiveWheelDampener {
71
79
  * (e.g. Code panel and Thinking box previews).
72
80
  */
73
81
  stepRows(direction, baseSpeed = 3, now = Date.now()) {
82
+ if (!Number.isFinite(baseSpeed) || baseSpeed <= 0) {
83
+ return 0;
84
+ }
74
85
  const notch = this.computeNotches(direction, now);
75
86
  this.rowAccumulator += baseSpeed * notch;
76
87
  const whole = Math.trunc(this.rowAccumulator);
@@ -81,7 +92,7 @@ export class AdaptiveWheelDampener {
81
92
  return 0;
82
93
  }
83
94
  reset() {
84
- this.lastTick = 0;
95
+ this.lastTick = null;
85
96
  this.lastDirection = null;
86
97
  this.rowAccumulator = 0;
87
98
  this.recentBursts = [];
@@ -101,13 +112,15 @@ export function handleViewportWheel(key, viewport, surface, host, now = Date.now
101
112
  // so the session stays responsive and never freezes.
102
113
  if (surface && typeof surface.captureMouse === 'function' && surface.detached) {
103
114
  surface.captureMouse();
104
- if (host.notice.startsWith('top of this view')) {
115
+ if (typeof host.notice === 'string' && host.notice.startsWith('top of this view')) {
105
116
  host.notice = '';
106
117
  }
107
118
  }
108
119
  // Clear manual turn override on wheel scroll so active indicator syncs with viewport
109
120
  if (surface) {
110
121
  surface.activeTurnIndex = null;
122
+ surface.hoveredTurnIndex = null;
123
+ surface.hoveredTurnAnchorY = null;
111
124
  }
112
125
  const dir = key.wheel.direction === 'down' ? 'down' : 'up';
113
126
  const notches = defaultViewportDampener.computeNotches(dir, now);
@@ -130,38 +143,51 @@ export function handleScrollbarMouse(key, viewport, surface, host) {
130
143
  return false;
131
144
  if (key.mouse.button !== 0)
132
145
  return false; // Left click only
133
- if (surface) {
134
- surface.activeTurnIndex = null;
135
- }
146
+ if (!Number.isFinite(key.mouse.x) || !Number.isFinite(key.mouse.y))
147
+ return false;
148
+ const mouseX = Math.floor(key.mouse.x);
149
+ const mouseY = Math.floor(key.mouse.y);
150
+ const rawTermRows = Number(host.out?.rows);
151
+ const termRows = Number.isFinite(rawTermRows) && rawTermRows > 0 ? Math.floor(rawTermRows) : 24;
152
+ const rawStageH = Number(surface.stage?.height);
153
+ const stageH = Number.isFinite(rawStageH) && rawStageH > 0 ? Math.floor(rawStageH) : termRows;
154
+ const stageTop = Math.max(0, termRows - stageH);
136
155
  // If completion picker is active, check if click was on the picker
137
- if (host.pickerLayout && Number.isFinite(key.mouse.y)) {
138
- const termRows = host.out?.rows || 24;
139
- const stageH = surface.stage?.height || termRows;
140
- const promptTop = Math.max(1, stageH - host.pickerLayout.totalPromptRows + 1);
141
- const pickerTop = promptTop + host.pickerLayout.pickerStart - 1;
142
- const pickerBottom = pickerTop + host.pickerLayout.pickerCount;
143
- if (key.mouse.y >= pickerTop && key.mouse.y <= pickerBottom) {
144
- return false; // Leave to completion handler
156
+ if (host.pickerLayout) {
157
+ const { pickerStart, pickerCount, totalPromptRows } = host.pickerLayout;
158
+ if (Number.isFinite(pickerStart) &&
159
+ Number.isFinite(pickerCount) &&
160
+ Number.isFinite(totalPromptRows) &&
161
+ pickerCount > 0) {
162
+ const promptTop = Math.max(1, stageH - Math.floor(totalPromptRows) + 1);
163
+ const pickerTop = stageTop + promptTop + Math.floor(pickerStart) - 1;
164
+ const pickerBottom = pickerTop + Math.floor(pickerCount) - 1;
165
+ if (mouseY >= pickerTop && mouseY <= pickerBottom) {
166
+ return false; // Leave to completion handler
167
+ }
145
168
  }
146
169
  }
147
- const termRows = host.out?.rows || 24;
148
- const stageH = surface.stage?.height || termRows;
149
- const stageTop = Math.max(0, termRows - stageH);
150
- const viewportH = viewport.height;
151
- const stageW = typeof surface.stage?.width === 'function' ? surface.stage.width() : (surface.width || 80);
152
- const mainW = sessionMainWidth(stageW);
170
+ const rawViewportH = Number(viewport.height);
171
+ const viewportH = Number.isFinite(rawViewportH) && rawViewportH > 0 ? Math.floor(rawViewportH) : 1;
172
+ const rawStageW = Number(typeof surface.stage?.width === 'function' ? surface.stage.width() : surface.width);
173
+ const stageW = Number.isFinite(rawStageW) && rawStageW > 0 ? Math.floor(rawStageW) : 80;
174
+ const mainW = sessionMainWidth(stageW, Boolean(surface.homeInfo), surface.env);
153
175
  const hasRail = typeof surface.railEnabled === 'function' ? surface.railEnabled(mainW) : true;
154
- const contentW = hasRail ? Math.max(20, mainW - 3) : mainW;
155
- const mouseX = key.mouse.x;
156
- const mouseY = key.mouse.y;
157
- // Rail occupies column contentW - 1 through mainW
158
- const isRailColumn = mouseX >= contentW - 1 && mouseX <= mainW;
176
+ if (!hasRail)
177
+ return false;
178
+ const contentW = Math.max(20, mainW - 3);
179
+ // Rail occupies column mainW - 3 through mainW (spacer, glyph column, outer margin)
180
+ const isRailColumn = mouseX >= Math.max(contentW, mainW - 3) && mouseX <= mainW;
159
181
  if (isRailColumn) {
182
+ surface.activeTurnIndex = null;
183
+ surface.hoveredTurnIndex = null;
184
+ surface.hoveredTurnAnchorY = null;
160
185
  // Auto-heal if detached
161
186
  if (typeof surface.captureMouse === 'function' && surface.detached) {
162
187
  surface.captureMouse();
163
- if (host.notice.startsWith('top of this view'))
188
+ if (typeof host.notice === 'string' && host.notice.startsWith('top of this view')) {
164
189
  host.notice = '';
190
+ }
165
191
  }
166
192
  // Boundary clamping:
167
193
  // Clicks preceding/above chat box clamp to top (0)
@@ -180,8 +206,10 @@ export function handleScrollbarMouse(key, viewport, surface, host) {
180
206
  const relY = mouseY - stageTop;
181
207
  const fraction = viewportH > 1 ? relY / (viewportH - 1) : 0;
182
208
  const clampedFraction = Math.max(0, Math.min(1, fraction));
183
- const target = Math.round(clampedFraction * viewport.maxScrollTop());
184
- if (target >= viewport.maxScrollTop()) {
209
+ const rawMax = Number(viewport.maxScrollTop());
210
+ const maxScroll = Number.isFinite(rawMax) && rawMax > 0 ? Math.floor(rawMax) : 0;
211
+ const target = Math.round(clampedFraction * maxScroll);
212
+ if (target >= maxScroll) {
185
213
  viewport.toBottom();
186
214
  }
187
215
  else {
@@ -61,6 +61,8 @@ export interface SurfaceContract {
61
61
  };
62
62
  viewport?: ViewportContract;
63
63
  width?: number;
64
+ env?: { CAUSYNE_SIDEBAR?: string };
65
+ homeInfo?: unknown;
64
66
  mouseCaptured?: boolean;
65
67
  detached?: boolean;
66
68
  releaseMouse?(): boolean;
@@ -106,17 +108,22 @@ export class AdaptiveWheelDampener {
106
108
  readonly minTickInterval: number;
107
109
  readonly burstWindowMs: number;
108
110
  readonly maxBurstNotches: number;
109
- private lastTick = 0;
111
+ private lastTick: number | null = null;
110
112
  private lastDirection: 'up' | 'down' | null = null;
111
113
  private rowAccumulator = 0;
112
114
  private recentBursts: Array<{ time: number; notches: number }> = [];
113
115
 
114
116
  constructor(options: WheelDampenerOptions = {}) {
115
- this.minDamping = options.minDamping ?? 0.28;
116
- this.thresholdMs = options.thresholdMs ?? 85;
117
- this.minTickInterval = options.minTickInterval ?? 6;
118
- this.burstWindowMs = options.burstWindowMs ?? 120;
119
- this.maxBurstNotches = options.maxBurstNotches ?? 5.0;
117
+ const finiteNonNeg = (value: number | undefined, fallback: number): number =>
118
+ value !== undefined && Number.isFinite(value) && value >= 0 ? value : fallback;
119
+ const rawMinDamping = options.minDamping;
120
+ this.minDamping = rawMinDamping !== undefined && Number.isFinite(rawMinDamping)
121
+ ? Math.min(1, Math.max(0, rawMinDamping))
122
+ : 0.28;
123
+ this.thresholdMs = finiteNonNeg(options.thresholdMs, 85);
124
+ this.minTickInterval = finiteNonNeg(options.minTickInterval, 6);
125
+ this.burstWindowMs = finiteNonNeg(options.burstWindowMs, 120);
126
+ this.maxBurstNotches = finiteNonNeg(options.maxBurstNotches, 5.0);
120
127
  }
121
128
 
122
129
  /**
@@ -124,14 +131,17 @@ export class AdaptiveWheelDampener {
124
131
  * their own sub-row accumulator (e.g. Viewport).
125
132
  */
126
133
  computeNotches(direction: 'up' | 'down', now: number = Date.now()): number {
134
+ if (!Number.isFinite(now)) {
135
+ return 1.0;
136
+ }
127
137
  if (this.lastDirection && this.lastDirection !== direction) {
128
138
  this.recentBursts = [];
129
- this.lastTick = 0;
139
+ this.lastTick = null;
130
140
  this.rowAccumulator = 0;
131
141
  }
132
142
  this.lastDirection = direction;
133
143
 
134
- const gap = this.lastTick ? Math.max(0, now - this.lastTick) : Infinity;
144
+ const gap = this.lastTick !== null ? Math.max(0, now - this.lastTick) : Infinity;
135
145
  this.lastTick = now;
136
146
 
137
147
  if (gap === Infinity || gap > 150) {
@@ -143,7 +153,8 @@ export class AdaptiveWheelDampener {
143
153
  return 1.0;
144
154
  }
145
155
 
146
- let damping = Math.max(this.minDamping, Math.min(1.0, gap / this.thresholdMs));
156
+ const ratio = this.thresholdMs > 0 ? gap / this.thresholdMs : 1.0;
157
+ let damping = Math.max(this.minDamping, Math.min(1.0, ratio));
147
158
 
148
159
  const windowStart = now - this.burstWindowMs;
149
160
  this.recentBursts = this.recentBursts.filter(b => b.time >= windowStart);
@@ -162,6 +173,9 @@ export class AdaptiveWheelDampener {
162
173
  * (e.g. Code panel and Thinking box previews).
163
174
  */
164
175
  stepRows(direction: 'up' | 'down', baseSpeed: number = 3, now: number = Date.now()): number {
176
+ if (!Number.isFinite(baseSpeed) || baseSpeed <= 0) {
177
+ return 0;
178
+ }
165
179
  const notch = this.computeNotches(direction, now);
166
180
  this.rowAccumulator += baseSpeed * notch;
167
181
  const whole = Math.trunc(this.rowAccumulator);
@@ -173,7 +187,7 @@ export class AdaptiveWheelDampener {
173
187
  }
174
188
 
175
189
  reset(): void {
176
- this.lastTick = 0;
190
+ this.lastTick = null;
177
191
  this.lastDirection = null;
178
192
  this.rowAccumulator = 0;
179
193
  this.recentBursts = [];
@@ -200,7 +214,7 @@ export function handleViewportWheel(
200
214
  // so the session stays responsive and never freezes.
201
215
  if (surface && typeof surface.captureMouse === 'function' && surface.detached) {
202
216
  surface.captureMouse();
203
- if (host.notice.startsWith('top of this view')) {
217
+ if (typeof host.notice === 'string' && host.notice.startsWith('top of this view')) {
204
218
  host.notice = '';
205
219
  }
206
220
  }
@@ -208,6 +222,8 @@ export function handleViewportWheel(
208
222
  // Clear manual turn override on wheel scroll so active indicator syncs with viewport
209
223
  if (surface) {
210
224
  (surface as any).activeTurnIndex = null;
225
+ (surface as any).hoveredTurnIndex = null;
226
+ (surface as any).hoveredTurnAnchorY = null;
211
227
  }
212
228
 
213
229
  const dir: 'up' | 'down' = key.wheel.direction === 'down' ? 'down' : 'up';
@@ -235,43 +251,58 @@ export function handleScrollbarMouse(
235
251
  ): boolean {
236
252
  if (!viewport || !surface || !key.mouse || !key.mouse.pressed) return false;
237
253
  if (key.mouse.button !== 0) return false; // Left click only
254
+ if (!Number.isFinite(key.mouse.x) || !Number.isFinite(key.mouse.y)) return false;
238
255
 
239
- if (surface) {
240
- (surface as any).activeTurnIndex = null;
241
- }
256
+ const mouseX = Math.floor(key.mouse.x);
257
+ const mouseY = Math.floor(key.mouse.y);
258
+
259
+ const rawTermRows = Number(host.out?.rows);
260
+ const termRows = Number.isFinite(rawTermRows) && rawTermRows > 0 ? Math.floor(rawTermRows) : 24;
261
+ const rawStageH = Number(surface.stage?.height);
262
+ const stageH = Number.isFinite(rawStageH) && rawStageH > 0 ? Math.floor(rawStageH) : termRows;
263
+ const stageTop = Math.max(0, termRows - stageH);
242
264
 
243
265
  // If completion picker is active, check if click was on the picker
244
- if (host.pickerLayout && Number.isFinite(key.mouse.y)) {
245
- const termRows = host.out?.rows || 24;
246
- const stageH = surface.stage?.height || termRows;
247
- const promptTop = Math.max(1, stageH - host.pickerLayout.totalPromptRows + 1);
248
- const pickerTop = promptTop + host.pickerLayout.pickerStart - 1;
249
- const pickerBottom = pickerTop + host.pickerLayout.pickerCount;
250
- if (key.mouse.y >= pickerTop && key.mouse.y <= pickerBottom) {
251
- return false; // Leave to completion handler
266
+ if (host.pickerLayout) {
267
+ const { pickerStart, pickerCount, totalPromptRows } = host.pickerLayout;
268
+ if (
269
+ Number.isFinite(pickerStart) &&
270
+ Number.isFinite(pickerCount) &&
271
+ Number.isFinite(totalPromptRows) &&
272
+ pickerCount > 0
273
+ ) {
274
+ const promptTop = Math.max(1, stageH - Math.floor(totalPromptRows) + 1);
275
+ const pickerTop = stageTop + promptTop + Math.floor(pickerStart) - 1;
276
+ const pickerBottom = pickerTop + Math.floor(pickerCount) - 1;
277
+ if (mouseY >= pickerTop && mouseY <= pickerBottom) {
278
+ return false; // Leave to completion handler
279
+ }
252
280
  }
253
281
  }
254
282
 
255
- const termRows = host.out?.rows || 24;
256
- const stageH = surface.stage?.height || termRows;
257
- const stageTop = Math.max(0, termRows - stageH);
258
- const viewportH = viewport.height;
259
- const stageW = typeof surface.stage?.width === 'function' ? surface.stage.width() : (surface.width || 80);
260
- const mainW = sessionMainWidth(stageW);
261
- const hasRail = typeof surface.railEnabled === 'function' ? surface.railEnabled(mainW) : true;
262
- const contentW = hasRail ? Math.max(20, mainW - 3) : mainW;
263
283
 
264
- const mouseX = key.mouse.x;
265
- const mouseY = key.mouse.y;
284
+ const rawViewportH = Number(viewport.height);
285
+ const viewportH = Number.isFinite(rawViewportH) && rawViewportH > 0 ? Math.floor(rawViewportH) : 1;
286
+ const rawStageW = Number(typeof surface.stage?.width === 'function' ? surface.stage.width() : surface.width);
287
+ const stageW = Number.isFinite(rawStageW) && rawStageW > 0 ? Math.floor(rawStageW) : 80;
288
+ const mainW = sessionMainWidth(stageW, Boolean(surface.homeInfo), surface.env);
289
+ const hasRail = typeof surface.railEnabled === 'function' ? surface.railEnabled(mainW) : true;
290
+ if (!hasRail) return false;
291
+ const contentW = Math.max(20, mainW - 3);
266
292
 
267
- // Rail occupies column contentW - 1 through mainW
268
- const isRailColumn = mouseX >= contentW - 1 && mouseX <= mainW;
293
+ // Rail occupies column mainW - 3 through mainW (spacer, glyph column, outer margin)
294
+ const isRailColumn = mouseX >= Math.max(contentW, mainW - 3) && mouseX <= mainW;
269
295
 
270
296
  if (isRailColumn) {
297
+ (surface as any).activeTurnIndex = null;
298
+ (surface as any).hoveredTurnIndex = null;
299
+ (surface as any).hoveredTurnAnchorY = null;
271
300
  // Auto-heal if detached
272
301
  if (typeof surface.captureMouse === 'function' && surface.detached) {
273
302
  surface.captureMouse();
274
- if (host.notice.startsWith('top of this view')) host.notice = '';
303
+ if (typeof host.notice === 'string' && host.notice.startsWith('top of this view')) {
304
+ host.notice = '';
305
+ }
275
306
  }
276
307
 
277
308
  // Boundary clamping:
@@ -293,9 +324,11 @@ export function handleScrollbarMouse(
293
324
  const relY = mouseY - stageTop;
294
325
  const fraction = viewportH > 1 ? relY / (viewportH - 1) : 0;
295
326
  const clampedFraction = Math.max(0, Math.min(1, fraction));
296
- const target = Math.round(clampedFraction * viewport.maxScrollTop());
327
+ const rawMax = Number(viewport.maxScrollTop());
328
+ const maxScroll = Number.isFinite(rawMax) && rawMax > 0 ? Math.floor(rawMax) : 0;
329
+ const target = Math.round(clampedFraction * maxScroll);
297
330
 
298
- if (target >= viewport.maxScrollTop()) {
331
+ if (target >= maxScroll) {
299
332
  viewport.toBottom();
300
333
  } else {
301
334
  viewport.scrollTo(target);
@@ -18,6 +18,10 @@ import { paint, bold, dim, GLYPHS } from './theme.js';
18
18
  import { truncate, visibleWidth, safeDisplay } from './text.js';
19
19
  import { readClipboardSync } from './clipboardSync.js';
20
20
 
21
+ const cleanSelectText = (value) => (
22
+ value == null ? '' : safeDisplay(String(value)).replace(/\s+/g, ' ').trim()
23
+ );
24
+
21
25
  /**
22
26
  * Score `query` against `text` as a fuzzy subsequence.
23
27
  *
@@ -69,11 +73,12 @@ export function fuzzyScore(query, text) {
69
73
 
70
74
  /** Filter and rank items. Items keep their original order when the query is empty. */
71
75
  export function fuzzyFilter(items, query) {
72
- if (!query) return items.map((item, index) => ({ item, score: 0, positions: [], index }));
76
+ const safeItems = Array.isArray(items) ? items.filter(Boolean) : [];
77
+ if (!query) return safeItems.map((item, index) => ({ item, score: 0, positions: [], index }));
73
78
 
74
79
  const scored = [];
75
- for (let index = 0; index < items.length; index++) {
76
- const item = items[index];
80
+ for (let index = 0; index < safeItems.length; index++) {
81
+ const item = safeItems[index];
77
82
  const subject = `${item.label || ''} ${item.searchText || ''} ${item.category || ''}`.trim();
78
83
  const result = fuzzyScore(query, subject);
79
84
  if (result) scored.push({ item, score: result.score, positions: result.positions, index });
@@ -88,13 +93,14 @@ export class SelectList {
88
93
  * items: { label, description, category, hint, value, searchText, disabled }
89
94
  */
90
95
  constructor(items = [], options = {}) {
96
+ const opts = options && typeof options === 'object' ? options : {};
91
97
  this.setItems(items);
92
98
  this.query = '';
93
99
  this.selected = 0;
94
100
  this.offset = 0;
95
- this.placeholder = options.placeholder || 'type to filter';
96
- this.emptyText = options.emptyText || 'no matches';
97
- this.showFilter = options.showFilter !== false;
101
+ this.placeholder = opts.placeholder || 'type to filter';
102
+ this.emptyText = opts.emptyText || 'no matches';
103
+ this.showFilter = opts.showFilter !== false;
98
104
  }
99
105
 
100
106
  setItems(items) {
@@ -116,17 +122,19 @@ export class SelectList {
116
122
  }
117
123
 
118
124
  move(delta, visibleRows) {
119
- if (this.filtered.length === 0) return this;
125
+ const count = this.filtered.length;
126
+ if (count === 0 || !Number.isFinite(delta)) return this;
127
+ const step = Math.trunc(delta);
120
128
  // Wrapping is what makes a short list feel responsive: pressing up on the
121
129
  // first row lands on the last, instead of doing nothing.
122
- this.selected = (this.selected + delta + this.filtered.length) % this.filtered.length;
130
+ this.selected = (((this.selected + step) % count) + count) % count;
123
131
  this.ensureVisible(visibleRows);
124
132
  return this;
125
133
  }
126
134
 
127
135
  moveTo(index, visibleRows) {
128
- if (this.filtered.length === 0) return this;
129
- this.selected = Math.max(0, Math.min(index, this.filtered.length - 1));
136
+ if (this.filtered.length === 0 || !Number.isFinite(index)) return this;
137
+ this.selected = Math.max(0, Math.min(Math.floor(index), this.filtered.length - 1));
130
138
  this.ensureVisible(visibleRows);
131
139
  return this;
132
140
  }
@@ -160,7 +168,7 @@ export class SelectList {
160
168
  }
161
169
 
162
170
  ensureVisible(visibleRows) {
163
- const rows = Math.max(1, visibleRows || 10);
171
+ const rows = Number.isFinite(visibleRows) && visibleRows > 0 ? Math.max(1, Math.floor(visibleRows)) : 10;
164
172
  if (this.filtered.length === 0) { this.offset = 0; return this; }
165
173
 
166
174
  if (this.selected < this.offset) this.offset = this.selected;
@@ -191,11 +199,17 @@ export class SelectList {
191
199
  */
192
200
  handleKey(key, visibleRows) {
193
201
  if (!key) return null;
194
- const rows = Math.max(1, visibleRows || 10);
202
+ const rows = Number.isFinite(visibleRows) && visibleRows > 0 ? Math.max(1, Math.floor(visibleRows)) : 10;
195
203
 
196
204
  switch (key.name) {
197
205
  case 'up': this.move(-1, rows); return 'handled';
198
206
  case 'down': this.move(1, rows); return 'handled';
207
+ case 'wheel': {
208
+ const direction = key.wheel && key.wheel.direction;
209
+ if (direction === 'up') this.moveTo(this.selected - 1, rows);
210
+ else if (direction === 'down') this.moveTo(this.selected + 1, rows);
211
+ return 'handled';
212
+ }
199
213
  // A page is the number of ITEMS on screen, not the number of rows.
200
214
  // Category headers spend rows without being selectable, so paging by the
201
215
  // row budget jumped further than a screenful and left a band of items that
@@ -225,7 +239,7 @@ export class SelectList {
225
239
  }
226
240
  return 'cancel';
227
241
  case 'paste': {
228
- const clean = String(key.text || '').replace(/[\r\n]+/g, ' ').trim();
242
+ const clean = cleanSelectText(key.text);
229
243
  if (clean) {
230
244
  this.query += clean;
231
245
  this.refilter();
@@ -243,7 +257,7 @@ export class SelectList {
243
257
  if (key.name === 'v') {
244
258
  const text = readClipboardSync();
245
259
  if (text) {
246
- const clean = String(text).replace(/[\r\n]+/g, ' ').trim();
260
+ const clean = cleanSelectText(text);
247
261
  if (clean) {
248
262
  this.query += clean;
249
263
  this.refilter();
@@ -254,9 +268,15 @@ export class SelectList {
254
268
  return null;
255
269
  }
256
270
 
257
- if (key.sequence && key.sequence.length === 1 && key.sequence >= ' ' && !key.meta) {
258
- this.query += key.sequence;
259
- this.refilter();
271
+ const typed = key.name === 'char' && !key.ctrl && !key.alt && !key.meta && typeof key.text === 'string'
272
+ ? key.text
273
+ : (key.sequence && key.sequence.length === 1 && key.sequence >= ' ' && !key.meta ? key.sequence : '');
274
+ if (typed) {
275
+ const safe = safeDisplay(typed).replace(/[\r\n\t]+/g, ' ');
276
+ if (safe) {
277
+ this.query += safe;
278
+ this.refilter();
279
+ }
260
280
  return 'handled';
261
281
  }
262
282
  return null;
@@ -269,11 +289,14 @@ export class SelectList {
269
289
  * is the only place a caret belongs in a picker.
270
290
  */
271
291
  render(width, height) {
292
+ const safeWidth = Number.isFinite(width) && width > 0 ? Math.floor(width) : 8;
293
+ const safeHeight = Number.isFinite(height) && height > 0 ? Math.floor(height) : 4;
272
294
  const rows = [];
273
- const inner = Math.max(4, width);
295
+ const inner = Math.max(4, safeWidth);
296
+ this.lastInnerWidth = inner;
274
297
 
275
298
  if (this.showFilter) {
276
- const shown = this.query || dim(this.placeholder);
299
+ const shown = cleanSelectText(this.query) || dim(cleanSelectText(this.placeholder));
277
300
  rows.push(`${paint('accent', GLYPHS.arrow)} ${truncate(shown, inner - 2, GLYPHS.ellipsis)}`);
278
301
  rows.push(paint('rule', GLYPHS.hLine.repeat(inner)));
279
302
  }
@@ -286,7 +309,7 @@ export class SelectList {
286
309
  // Letting the loop fill that row meant the counter could clobber the very
287
310
  // item the marker was on, which is a second, independent way for the
288
311
  // selection to vanish.
289
- const itemRows = Math.max(1, height - rows.length - 1);
312
+ const itemRows = Math.max(1, safeHeight - rows.length - 1);
290
313
 
291
314
  // Remember what the renderer actually budgeted, so key handling scrolls by
292
315
  // the same number of rows it draws. SelectLayer used to derive its own
@@ -296,14 +319,17 @@ export class SelectList {
296
319
  this.ensureVisible(itemRows);
297
320
 
298
321
  if (this.filtered.length === 0) {
299
- rows.push('');
300
- rows.push(` ${dim(this.emptyText)}`);
322
+ if (rows.length + 1 < safeHeight) rows.push('');
323
+ if (rows.length < safeHeight) {
324
+ const cleanEmpty = truncate(cleanSelectText(this.emptyText), Math.max(1, inner - 2), GLYPHS.ellipsis);
325
+ rows.push(` ${dim(cleanEmpty)}`);
326
+ }
301
327
  return { rows, listRows: itemRows };
302
328
  }
303
329
 
304
330
  const window = this.filtered.slice(this.offset, this.offset + itemRows);
305
331
  // The last row belongs to the position counter whenever one is shown.
306
- const ceiling = this.filtered.length > itemRows ? height - 1 : height;
332
+ const ceiling = this.filtered.length > itemRows ? Math.max(rows.length + 1, safeHeight - 1) : safeHeight;
307
333
  let lastCategory = null;
308
334
 
309
335
  for (let index = 0; index < window.length; index++) {
@@ -312,15 +338,16 @@ export class SelectList {
312
338
  const isSelected = this.offset + index === this.selected;
313
339
 
314
340
  if (item.category && item.category !== lastCategory) {
341
+ if (rows.length + 2 > ceiling) break;
315
342
  lastCategory = item.category;
316
- rows.push(dim(` ${safeDisplay(item.category)}`));
317
- if (rows.length >= ceiling) break;
343
+ rows.push(truncate(dim(` ${cleanSelectText(item.category)}`), inner, GLYPHS.ellipsis));
318
344
  }
319
345
 
320
346
  const marker = isSelected ? paint('accent', GLYPHS.arrow) : ' ';
321
- const label = safeDisplay(item.label || '');
322
- const hint = item.hint ? dim(` ${safeDisplay(item.hint)}`) : '';
323
- const description = item.description ? safeDisplay(item.description) : '';
347
+ const label = cleanSelectText(item.label || '');
348
+ const cleanHint = cleanSelectText(item.hint || '');
349
+ const hint = cleanHint ? dim(` ${cleanHint}`) : '';
350
+ const description = item.description ? cleanSelectText(item.description) : '';
324
351
 
325
352
  // The hint (a keybinding) is right-aligned, which is what makes a palette
326
353
  // scannable: the names line up on the left, the keys on the right.
@@ -333,15 +360,15 @@ export class SelectList {
333
360
  : '';
334
361
  const padding = Math.max(0, inner - visibleWidth(left) - visibleWidth(middle) - hintWidth);
335
362
 
336
- rows.push(left + middle + ' '.repeat(padding) + hint);
363
+ rows.push(truncate(left + middle + ' '.repeat(padding) + hint, inner, GLYPHS.ellipsis));
337
364
  if (rows.length >= ceiling) break;
338
365
  }
339
366
 
340
367
  // A count is the cheapest possible scroll indicator and it never lies.
341
368
  if (this.filtered.length > itemRows) {
342
369
  const position = `${this.selected + 1}/${this.filtered.length}`;
343
- const line = dim(position.padStart(inner));
344
- if (rows.length >= height) rows[height - 1] = line;
370
+ const line = dim(truncate(position.padStart(inner), inner, GLYPHS.ellipsis));
371
+ if (rows.length >= safeHeight) rows[safeHeight - 1] = line;
345
372
  else rows.push(line);
346
373
  }
347
374
 
@@ -350,6 +377,7 @@ export class SelectList {
350
377
 
351
378
  /** Column the caret sits at within the filter row. */
352
379
  cursorColumn() {
353
- return 2 + visibleWidth(this.query);
380
+ const maxCol = Math.max(2, (this.lastInnerWidth || 40) - 1);
381
+ return Math.min(maxCol, 2 + visibleWidth(cleanSelectText(this.query)));
354
382
  }
355
383
  }
@@ -1,11 +1,19 @@
1
+ function clampedRows(value, limit) {
2
+ if (value === Number.POSITIVE_INFINITY)
3
+ return limit;
4
+ if (!Number.isFinite(value))
5
+ return 0;
6
+ return Math.max(0, Math.floor(value));
7
+ }
1
8
  /** Keep an opened conversation anchored to the terminal bottom at every width. */
2
9
  export function sessionStageHeight(maxHeight, promptRows, contentRows, fullSession) {
3
- const limit = Math.max(1, Math.floor(maxHeight));
4
- return fullSession ? limit : Math.min(limit, Math.max(1, promptRows + contentRows));
10
+ const limit = Number.isFinite(maxHeight) ? Math.max(1, Math.floor(maxHeight)) : 1;
11
+ const total = clampedRows(promptRows, limit) + clampedRows(contentRows, limit);
12
+ return fullSession ? limit : Math.min(limit, Math.max(1, total));
5
13
  }
6
14
  /** Place a short transcript beside the input; spare space belongs above it. */
7
15
  export function alignTranscriptRows(rows, contentRows) {
8
- const visible = Math.max(0, Math.min(rows.length, Math.floor(contentRows)));
16
+ const visible = Math.min(rows.length, clampedRows(contentRows, rows.length));
9
17
  if (visible === rows.length)
10
18
  return rows;
11
19
  return Array(rows.length - visible).fill('').concat(rows.slice(0, visible));
@@ -1,12 +1,19 @@
1
+ function clampedRows(value: number, limit: number): number {
2
+ if (value === Number.POSITIVE_INFINITY) return limit;
3
+ if (!Number.isFinite(value)) return 0;
4
+ return Math.max(0, Math.floor(value));
5
+ }
6
+
1
7
  /** Keep an opened conversation anchored to the terminal bottom at every width. */
2
8
  export function sessionStageHeight(maxHeight: number, promptRows: number, contentRows: number, fullSession: boolean): number {
3
- const limit = Math.max(1, Math.floor(maxHeight));
4
- return fullSession ? limit : Math.min(limit, Math.max(1, promptRows + contentRows));
9
+ const limit = Number.isFinite(maxHeight) ? Math.max(1, Math.floor(maxHeight)) : 1;
10
+ const total = clampedRows(promptRows, limit) + clampedRows(contentRows, limit);
11
+ return fullSession ? limit : Math.min(limit, Math.max(1, total));
5
12
  }
6
13
 
7
14
  /** Place a short transcript beside the input; spare space belongs above it. */
8
15
  export function alignTranscriptRows(rows: string[], contentRows: number): string[] {
9
- const visible = Math.max(0, Math.min(rows.length, Math.floor(contentRows)));
16
+ const visible = Math.min(rows.length, clampedRows(contentRows, rows.length));
10
17
  if (visible === rows.length) return rows;
11
18
  return Array<string>(rows.length - visible).fill('').concat(rows.slice(0, visible));
12
19
  }