@causyne/agent 1.20.15 → 1.20.16

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.
@@ -74,8 +74,72 @@ export function collectSessionTurns(transcript, contentWidth = 80) {
74
74
  }
75
75
  return turns;
76
76
  }
77
+ /**
78
+ * Resolve which turn is currently active based on viewport scroll position.
79
+ */
80
+ export function resolveActiveTurnIndex(turns, scrollTop, viewportHeight = 20, isAtBottom = false) {
81
+ if (!turns || turns.length === 0)
82
+ return 0;
83
+ if (isAtBottom) {
84
+ return turns[turns.length - 1].turnIndex;
85
+ }
86
+ if (scrollTop <= 0) {
87
+ return turns[0].turnIndex;
88
+ }
89
+ let active = turns[turns.length - 1].turnIndex;
90
+ const targetRow = Math.max(0, scrollTop + Math.floor(viewportHeight / 3));
91
+ for (let i = turns.length - 1; i >= 0; i--) {
92
+ const t = turns[i];
93
+ if (t.startRow <= targetRow) {
94
+ active = t.turnIndex;
95
+ break;
96
+ }
97
+ }
98
+ return active;
99
+ }
100
+ /**
101
+ * ANSI-safe column slicing: skips the first startCol visible columns while preserving active styles.
102
+ */
103
+ export function sliceFromColumn(source, startCol) {
104
+ let col = 0;
105
+ let index = 0;
106
+ let activeStyles = '';
107
+ while (index < source.length) {
108
+ if (source[index] === '\x1b') {
109
+ const rest = source.slice(index);
110
+ const match = rest.match(/^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/)
111
+ || rest.match(/^\x1b\[[0-?]*[ -/]*[@-~]/)
112
+ || rest.match(/^\x1b[@-Z\\-_]/);
113
+ if (match) {
114
+ if (col < startCol) {
115
+ if (match[0] === '\x1b[0m' || match[0] === '\x1b[m') {
116
+ activeStyles = '';
117
+ }
118
+ else if (match[0].startsWith('\x1b[')) {
119
+ activeStyles += match[0];
120
+ }
121
+ }
122
+ index += match[0].length;
123
+ continue;
124
+ }
125
+ }
126
+ if (col >= startCol) {
127
+ return activeStyles + source.slice(index);
128
+ }
129
+ const code = source.codePointAt(index);
130
+ if (code === undefined)
131
+ break;
132
+ const char = String.fromCodePoint(code);
133
+ col += visibleWidth(char);
134
+ index += char.length;
135
+ }
136
+ return '';
137
+ }
77
138
  /**
78
139
  * Build the vertical column of turn rail cells.
140
+ * Matches the canonical Turn Rail UX from Screen Recording 2026-10-30 at 02.42.17:
141
+ * - Consecutive vertical pillar when turns fit within viewport height.
142
+ * - Proportional scaling when turn count exceeds available height.
79
143
  */
80
144
  export function buildTurnRailCells(turns, height, activeTurnIndex, hoveredTurnIndex) {
81
145
  const cells = new Array(Math.max(0, height)).fill(' ');
@@ -85,7 +149,7 @@ export function buildTurnRailCells(turns, height, activeTurnIndex, hoveredTurnIn
85
149
  return { cells, layout: { dotRows, rowToTurn } };
86
150
  }
87
151
  const count = turns.length;
88
- // If count fits comfortably, stack consecutive dots centered or top-aligned
152
+ // If count fits comfortably, stack consecutive dots centered or upper-aligned
89
153
  const startRow = count <= height
90
154
  ? Math.max(0, Math.floor((height - count) / 3))
91
155
  : 0;
@@ -105,16 +169,16 @@ export function buildTurnRailCells(turns, height, activeTurnIndex, hoveredTurnIn
105
169
  dotRows.set(turnNum, rowIdx);
106
170
  rowToTurn.set(rowIdx, turnNum);
107
171
  const isHovered = hoveredTurnIndex === turnNum;
108
- const isActive = activeTurnIndex === turnNum || (turnNum === count && !hoveredTurnIndex);
172
+ const isActive = activeTurnIndex === turnNum;
109
173
  if (isHovered) {
110
174
  // Hovered dot: bright silver highlighted circle
111
175
  cells[rowIdx] = '\x1b[1m\x1b[38;2;230;235;245m●\x1b[0m';
112
176
  }
113
- else if (isActive) {
177
+ else if (isActive && !cells[rowIdx]?.includes('230;235;245m')) {
114
178
  // Active / current turn: bright white circle
115
179
  cells[rowIdx] = '\x1b[1m\x1b[38;2;250;250;250m●\x1b[0m';
116
180
  }
117
- else {
181
+ else if (!cells[rowIdx]?.includes('●')) {
118
182
  // Muted inactive turns: subtle gray dot
119
183
  cells[rowIdx] = '\x1b[38;2;115;115;120m•\x1b[0m';
120
184
  }
@@ -189,8 +253,8 @@ export function overlayTurnPreviewCard(viewportRows, cardRows, anchorY, railX, s
189
253
  const cardWidth = visibleWidth(cardRows[0] || '');
190
254
  // Clamp card placement horizontally: preferably to the left of the rail, or to the right
191
255
  let x = railX - cardWidth - 1;
192
- if (x < 0 || (railX <= 4 && railX + 2 + cardWidth <= screenWidth)) {
193
- x = Math.max(0, Math.min(screenWidth - cardWidth - 1, railX + 2));
256
+ if (x < 0 || (railX <= 4 && railX + 3 + cardWidth <= screenWidth)) {
257
+ x = Math.max(0, Math.min(screenWidth - cardWidth - 1, railX + 3));
194
258
  }
195
259
  // Center vertically around anchorY
196
260
  const startY = Math.max(0, Math.min(viewportRows.length - cardRows.length, anchorY - Math.floor(cardRows.length / 2)));
@@ -203,7 +267,7 @@ export function overlayTurnPreviewCard(viewportRows, cardRows, anchorY, railX, s
203
267
  const cardRow = cardRows[i] || '';
204
268
  // Composite cardRow onto baseRow at column x
205
269
  const beforePart = padEnd(truncate(baseRow, x, ''), x);
206
- const afterPart = baseRow.length > x + cardWidth ? baseRow.slice(x + cardWidth) : '';
270
+ const afterPart = sliceFromColumn(baseRow, x + cardWidth);
207
271
  out[targetY] = `${beforePart}${cardRow}${afterPart}`;
208
272
  }
209
273
  return out;
@@ -223,8 +287,12 @@ export function handleTurnRailMouse(event, surface, host) {
223
287
  const mouseY = event.mouse.y;
224
288
  const stageTop = layout.stageTop ?? 0;
225
289
  const relY = mouseY - stageTop;
226
- const isRail = mouseX >= layout.railX - 1 && mouseX <= layout.railX + 1;
290
+ // Rail hit box: if railX <= 2, check columns 0..3 (left margin); otherwise check [railX - 1, railX + 2]
291
+ const isRail = layout.railX <= 2
292
+ ? (mouseX >= 0 && mouseX <= Math.max(3, layout.railX + 2))
293
+ : (mouseX >= layout.railX - 1 && mouseX <= layout.railX + 2);
227
294
  if (!isRail || relY < 0 || relY >= layout.viewportHeight) {
295
+ surface.turnRailHoverSuppressed = false;
228
296
  if (surface.hoveredTurnIndex !== null && surface.hoveredTurnIndex !== undefined) {
229
297
  surface.hoveredTurnIndex = null;
230
298
  surface.hoveredTurnAnchorY = null;
@@ -235,12 +303,35 @@ export function handleTurnRailMouse(event, surface, host) {
235
303
  }
236
304
  return false;
237
305
  }
238
- const turnNum = layout.rowToTurn?.get(relY);
306
+ // If hover is temporarily suppressed after a click, swallow rail mouse events until suppression ends
307
+ if (surface.turnRailHoverSuppressed) {
308
+ return true;
309
+ }
310
+ let turnNum = layout.rowToTurn?.get(relY);
311
+ let anchorY = relY;
312
+ // If not an exact row hit, resolve closest dot within proximity
313
+ if (!turnNum && layout.dotRows && layout.dotRows.size > 0) {
314
+ let minDistance = Infinity;
315
+ let closestTurn;
316
+ let closestRow;
317
+ for (const [tNum, row] of layout.dotRows.entries()) {
318
+ const dist = Math.abs(relY - row);
319
+ if (dist < minDistance) {
320
+ minDistance = dist;
321
+ closestTurn = tNum;
322
+ closestRow = row;
323
+ }
324
+ }
325
+ if (minDistance <= 2 && closestTurn !== undefined && closestRow !== undefined) {
326
+ turnNum = closestTurn;
327
+ anchorY = closestRow;
328
+ }
329
+ }
239
330
  if (turnNum) {
240
331
  if (isMouseMotion(event.sequence)) {
241
332
  if (surface.hoveredTurnIndex !== turnNum) {
242
333
  surface.hoveredTurnIndex = turnNum;
243
- surface.hoveredTurnAnchorY = relY;
334
+ surface.hoveredTurnAnchorY = anchorY;
244
335
  if (typeof host?.scheduleDraw === 'function')
245
336
  host.scheduleDraw();
246
337
  else if (typeof host?.draw === 'function')
@@ -249,17 +340,31 @@ export function handleTurnRailMouse(event, surface, host) {
249
340
  return true;
250
341
  }
251
342
  if (event.mouse.button === 0 && event.mouse.pressed) {
252
- const turn = layout.turns?.find((t) => t.turnIndex === turnNum);
253
- if (turn && surface.viewport && typeof surface.viewport.scrollTo === 'function') {
343
+ const turns = layout.turns || [];
344
+ const turn = turns.find((t) => t.turnIndex === turnNum);
345
+ const isLatestTurn = turnNum === turns.length || (turn && turns.length > 0 && turn.turnIndex === turns[turns.length - 1].turnIndex);
346
+ if (isLatestTurn && surface.viewport && typeof surface.viewport.toBottom === 'function') {
347
+ surface.viewport.toBottom();
348
+ }
349
+ else if (turn && surface.viewport && typeof surface.viewport.scrollTo === 'function') {
254
350
  surface.viewport.scrollTo(turn.startRow);
255
- surface.hoveredTurnIndex = null;
256
- surface.hoveredTurnAnchorY = null;
257
- if (typeof host?.draw === 'function')
258
- host.draw();
259
- else if (typeof host?.scheduleDraw === 'function')
260
- host.scheduleDraw();
261
- return true;
262
351
  }
352
+ surface.activeTurnIndex = turnNum;
353
+ // Immediately dismiss preview card and activate temporary suppression cooldown
354
+ surface.hoveredTurnIndex = null;
355
+ surface.hoveredTurnAnchorY = null;
356
+ surface.turnRailHoverSuppressed = true;
357
+ if (surface._turnRailSuppressTimer) {
358
+ clearTimeout(surface._turnRailSuppressTimer);
359
+ }
360
+ surface._turnRailSuppressTimer = setTimeout(() => {
361
+ surface.turnRailHoverSuppressed = false;
362
+ }, 400);
363
+ if (typeof host?.draw === 'function')
364
+ host.draw();
365
+ else if (typeof host?.scheduleDraw === 'function')
366
+ host.scheduleDraw();
367
+ return true;
263
368
  }
264
369
  }
265
370
  else if (surface.hoveredTurnIndex !== null && surface.hoveredTurnIndex !== undefined) {
@@ -101,8 +101,81 @@ export function collectSessionTurns(transcript: any, contentWidth = 80): Session
101
101
  return turns;
102
102
  }
103
103
 
104
+ /**
105
+ * Resolve which turn is currently active based on viewport scroll position.
106
+ */
107
+ export function resolveActiveTurnIndex(
108
+ turns: SessionTurn[],
109
+ scrollTop: number,
110
+ viewportHeight = 20,
111
+ isAtBottom = false
112
+ ): number {
113
+ if (!turns || turns.length === 0) return 0;
114
+ if (isAtBottom) {
115
+ return turns[turns.length - 1]!.turnIndex;
116
+ }
117
+ if (scrollTop <= 0) {
118
+ return turns[0]!.turnIndex;
119
+ }
120
+
121
+ let active = turns[turns.length - 1]!.turnIndex;
122
+ const targetRow = Math.max(0, scrollTop + Math.floor(viewportHeight / 3));
123
+ for (let i = turns.length - 1; i >= 0; i--) {
124
+ const t = turns[i]!;
125
+ if (t.startRow <= targetRow) {
126
+ active = t.turnIndex;
127
+ break;
128
+ }
129
+ }
130
+ return active;
131
+ }
132
+
133
+ /**
134
+ * ANSI-safe column slicing: skips the first startCol visible columns while preserving active styles.
135
+ */
136
+ export function sliceFromColumn(source: string, startCol: number): string {
137
+ let col = 0;
138
+ let index = 0;
139
+ let activeStyles = '';
140
+
141
+ while (index < source.length) {
142
+ if (source[index] === '\x1b') {
143
+ const rest = source.slice(index);
144
+ const match = rest.match(/^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/)
145
+ || rest.match(/^\x1b\[[0-?]*[ -/]*[@-~]/)
146
+ || rest.match(/^\x1b[@-Z\\-_]/);
147
+ if (match) {
148
+ if (col < startCol) {
149
+ if (match[0] === '\x1b[0m' || match[0] === '\x1b[m') {
150
+ activeStyles = '';
151
+ } else if (match[0].startsWith('\x1b[')) {
152
+ activeStyles += match[0];
153
+ }
154
+ }
155
+ index += match[0].length;
156
+ continue;
157
+ }
158
+ }
159
+
160
+ if (col >= startCol) {
161
+ return activeStyles + source.slice(index);
162
+ }
163
+
164
+ const code = source.codePointAt(index);
165
+ if (code === undefined) break;
166
+ const char = String.fromCodePoint(code);
167
+ col += visibleWidth(char);
168
+ index += char.length;
169
+ }
170
+
171
+ return '';
172
+ }
173
+
104
174
  /**
105
175
  * Build the vertical column of turn rail cells.
176
+ * Matches the canonical Turn Rail UX from Screen Recording 2026-10-30 at 02.42.17:
177
+ * - Consecutive vertical pillar when turns fit within viewport height.
178
+ * - Proportional scaling when turn count exceeds available height.
106
179
  */
107
180
  export function buildTurnRailCells(
108
181
  turns: SessionTurn[],
@@ -119,7 +192,7 @@ export function buildTurnRailCells(
119
192
  }
120
193
 
121
194
  const count = turns.length;
122
- // If count fits comfortably, stack consecutive dots centered or top-aligned
195
+ // If count fits comfortably, stack consecutive dots centered or upper-aligned
123
196
  const startRow = count <= height
124
197
  ? Math.max(0, Math.floor((height - count) / 3))
125
198
  : 0;
@@ -141,15 +214,15 @@ export function buildTurnRailCells(
141
214
  rowToTurn.set(rowIdx, turnNum);
142
215
 
143
216
  const isHovered = hoveredTurnIndex === turnNum;
144
- const isActive = activeTurnIndex === turnNum || (turnNum === count && !hoveredTurnIndex);
217
+ const isActive = activeTurnIndex === turnNum;
145
218
 
146
219
  if (isHovered) {
147
220
  // Hovered dot: bright silver highlighted circle
148
221
  cells[rowIdx] = '\x1b[1m\x1b[38;2;230;235;245m●\x1b[0m';
149
- } else if (isActive) {
222
+ } else if (isActive && !cells[rowIdx]?.includes('230;235;245m')) {
150
223
  // Active / current turn: bright white circle
151
224
  cells[rowIdx] = '\x1b[1m\x1b[38;2;250;250;250m●\x1b[0m';
152
- } else {
225
+ } else if (!cells[rowIdx]?.includes('●')) {
153
226
  // Muted inactive turns: subtle gray dot
154
227
  cells[rowIdx] = '\x1b[38;2;115;115;120m•\x1b[0m';
155
228
  }
@@ -244,8 +317,8 @@ export function overlayTurnPreviewCard(
244
317
  const cardWidth = visibleWidth(cardRows[0] || '');
245
318
  // Clamp card placement horizontally: preferably to the left of the rail, or to the right
246
319
  let x = railX - cardWidth - 1;
247
- if (x < 0 || (railX <= 4 && railX + 2 + cardWidth <= screenWidth)) {
248
- x = Math.max(0, Math.min(screenWidth - cardWidth - 1, railX + 2));
320
+ if (x < 0 || (railX <= 4 && railX + 3 + cardWidth <= screenWidth)) {
321
+ x = Math.max(0, Math.min(screenWidth - cardWidth - 1, railX + 3));
249
322
  }
250
323
 
251
324
  // Center vertically around anchorY
@@ -263,7 +336,7 @@ export function overlayTurnPreviewCard(
263
336
 
264
337
  // Composite cardRow onto baseRow at column x
265
338
  const beforePart = padEnd(truncate(baseRow, x, ''), x);
266
- const afterPart = baseRow.length > x + cardWidth ? baseRow.slice(x + cardWidth) : '';
339
+ const afterPart = sliceFromColumn(baseRow, x + cardWidth);
267
340
  out[targetY] = `${beforePart}${cardRow}${afterPart}`;
268
341
  }
269
342
 
@@ -290,8 +363,13 @@ export function handleTurnRailMouse(
290
363
  const stageTop = layout.stageTop ?? 0;
291
364
  const relY = mouseY - stageTop;
292
365
 
293
- const isRail = mouseX >= layout.railX - 1 && mouseX <= layout.railX + 1;
366
+ // Rail hit box: if railX <= 2, check columns 0..3 (left margin); otherwise check [railX - 1, railX + 2]
367
+ const isRail = layout.railX <= 2
368
+ ? (mouseX >= 0 && mouseX <= Math.max(3, layout.railX + 2))
369
+ : (mouseX >= layout.railX - 1 && mouseX <= layout.railX + 2);
370
+
294
371
  if (!isRail || relY < 0 || relY >= layout.viewportHeight) {
372
+ surface.turnRailHoverSuppressed = false;
295
373
  if (surface.hoveredTurnIndex !== null && surface.hoveredTurnIndex !== undefined) {
296
374
  surface.hoveredTurnIndex = null;
297
375
  surface.hoveredTurnAnchorY = null;
@@ -301,27 +379,71 @@ export function handleTurnRailMouse(
301
379
  return false;
302
380
  }
303
381
 
304
- const turnNum = layout.rowToTurn?.get(relY);
382
+ // If hover is temporarily suppressed after a click, swallow rail mouse events until suppression ends
383
+ if (surface.turnRailHoverSuppressed) {
384
+ return true;
385
+ }
386
+
387
+ let turnNum = layout.rowToTurn?.get(relY);
388
+ let anchorY = relY;
389
+
390
+ // If not an exact row hit, resolve closest dot within proximity
391
+ if (!turnNum && layout.dotRows && layout.dotRows.size > 0) {
392
+ let minDistance = Infinity;
393
+ let closestTurn: number | undefined;
394
+ let closestRow: number | undefined;
395
+
396
+ for (const [tNum, row] of layout.dotRows.entries()) {
397
+ const dist = Math.abs(relY - row);
398
+ if (dist < minDistance) {
399
+ minDistance = dist;
400
+ closestTurn = tNum;
401
+ closestRow = row;
402
+ }
403
+ }
404
+
405
+ if (minDistance <= 2 && closestTurn !== undefined && closestRow !== undefined) {
406
+ turnNum = closestTurn;
407
+ anchorY = closestRow;
408
+ }
409
+ }
410
+
305
411
  if (turnNum) {
306
412
  if (isMouseMotion(event.sequence)) {
307
413
  if (surface.hoveredTurnIndex !== turnNum) {
308
414
  surface.hoveredTurnIndex = turnNum;
309
- surface.hoveredTurnAnchorY = relY;
415
+ surface.hoveredTurnAnchorY = anchorY;
310
416
  if (typeof host?.scheduleDraw === 'function') host.scheduleDraw();
311
417
  else if (typeof host?.draw === 'function') host.draw();
312
418
  }
313
419
  return true;
314
420
  }
315
421
  if (event.mouse.button === 0 && event.mouse.pressed) {
316
- const turn = layout.turns?.find((t: SessionTurn) => t.turnIndex === turnNum);
317
- if (turn && surface.viewport && typeof surface.viewport.scrollTo === 'function') {
422
+ const turns: SessionTurn[] = layout.turns || [];
423
+ const turn = turns.find((t: SessionTurn) => t.turnIndex === turnNum);
424
+ const isLatestTurn = turnNum === turns.length || (turn && turns.length > 0 && turn.turnIndex === turns[turns.length - 1]!.turnIndex);
425
+
426
+ if (isLatestTurn && surface.viewport && typeof surface.viewport.toBottom === 'function') {
427
+ surface.viewport.toBottom();
428
+ } else if (turn && surface.viewport && typeof surface.viewport.scrollTo === 'function') {
318
429
  surface.viewport.scrollTo(turn.startRow);
319
- surface.hoveredTurnIndex = null;
320
- surface.hoveredTurnAnchorY = null;
321
- if (typeof host?.draw === 'function') host.draw();
322
- else if (typeof host?.scheduleDraw === 'function') host.scheduleDraw();
323
- return true;
324
430
  }
431
+ surface.activeTurnIndex = turnNum;
432
+
433
+ // Immediately dismiss preview card and activate temporary suppression cooldown
434
+ surface.hoveredTurnIndex = null;
435
+ surface.hoveredTurnAnchorY = null;
436
+ surface.turnRailHoverSuppressed = true;
437
+ if (surface._turnRailSuppressTimer) {
438
+ clearTimeout(surface._turnRailSuppressTimer);
439
+ }
440
+ surface._turnRailSuppressTimer = setTimeout(() => {
441
+ surface.turnRailHoverSuppressed = false;
442
+ }, 400);
443
+
444
+ if (typeof host?.draw === 'function') host.draw();
445
+ else if (typeof host?.scheduleDraw === 'function') host.scheduleDraw();
446
+ return true;
325
447
  }
326
448
  } else if (surface.hoveredTurnIndex !== null && surface.hoveredTurnIndex !== undefined) {
327
449
  surface.hoveredTurnIndex = null;
@@ -332,3 +454,4 @@ export function handleTurnRailMouse(
332
454
 
333
455
  return false;
334
456
  }
457
+