@causyne/agent 1.20.9 → 1.20.11

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 (47) hide show
  1. package/package.json +2 -1
  2. package/src/agentLoop.js +127 -72
  3. package/src/agentLoop.ts +137 -75
  4. package/src/browser/causyneBrowser.js +36 -0
  5. package/src/browser/causyneBrowser.ts +36 -1
  6. package/src/commands.js +25 -0
  7. package/src/contextManager.js +16 -0
  8. package/src/providers/antigravity.js +63 -11
  9. package/src/providers/antigravity.ts +62 -12
  10. package/src/provisioning/capabilityProvisioner.js +2 -7
  11. package/src/provisioning/npmInstall.js +19 -0
  12. package/src/provisioning/npmInstall.ts +19 -0
  13. package/src/smartToolsUse.js +20 -0
  14. package/src/smartToolsUse.ts +21 -0
  15. package/src/stallGuard.js +347 -0
  16. package/src/stallGuard.ts +423 -0
  17. package/src/tools/browserAutomator.js +3 -3
  18. package/src/tools/browserAutomator.ts +3 -3
  19. package/src/tools/fsRead.js +91 -80
  20. package/src/tools/fsRead.ts +125 -0
  21. package/src/tools/peerAgent.js +1 -1
  22. package/src/tools/peerAgent.ts +1 -1
  23. package/src/tools/screenshot.js +5 -1
  24. package/src/tools/screenshot.ts +5 -1
  25. package/src/tools/toolScanner.js +19 -0
  26. package/src/tools/toolScanner.ts +17 -0
  27. package/src/tools/toolSelfRepair.js +63 -1
  28. package/src/tools/toolSelfRepair.ts +61 -1
  29. package/src/tools/visionInspector.js +80 -142
  30. package/src/tools/visionInspector.ts +110 -0
  31. package/src/tui/chatboxClipboard.js +152 -0
  32. package/src/tui/chatboxClipboard.ts +201 -0
  33. package/src/tui/clipboardSync.js +38 -7
  34. package/src/tui/clipboardSync.ts +39 -7
  35. package/src/tui/codeProgress.js +89 -18
  36. package/src/tui/codeProgress.ts +102 -17
  37. package/src/tui/codeStreamRenderer.js +7 -2
  38. package/src/tui/codeStreamRenderer.ts +7 -2
  39. package/src/tui/composerControls.js +9 -0
  40. package/src/tui/composerControls.ts +9 -0
  41. package/src/tui/dialogs.js +8 -3
  42. package/src/tui/diff.d.ts +1 -0
  43. package/src/tui/lineEditor.js +166 -14
  44. package/src/tui/mouseHover.js +6 -2
  45. package/src/tui/mouseHover.ts +7 -2
  46. package/src/tui/prompt.js +96 -19
  47. package/src/tui/stageSurface.js +3 -1
@@ -3,7 +3,7 @@ import { safeDisplay, truncate, padEnd } from './text.js';
3
3
  // The terminal theme is still a standalone JavaScript module in the TS migration.
4
4
  // @ts-expect-error theme.js has no TypeScript declaration yet.
5
5
  import { COLOR_DEPTH, IS_LIGHT, paint } from './theme.js';
6
- import { diffLines } from './diff.js';
6
+ import { diffLines, MAX_SIDE_LINES } from './diff.js';
7
7
  import fs from 'node:fs';
8
8
  import path from 'node:path';
9
9
  export const CODE_PANEL_LINES = 10;
@@ -13,40 +13,56 @@ const FILE_TOOLS = {
13
13
  'fs.write': 'Creating', 'fs.edit': 'Editing',
14
14
  fs_delete: 'Removing', fs_move: 'Moving',
15
15
  write_file: 'Creating', edit_file: 'Editing', delete_file: 'Removing',
16
+ create_file: 'Creating', new_file: 'Creating',
17
+ remove_file: 'Removing', unlink: 'Removing', move_file: 'Moving',
16
18
  replace_file_content: 'Editing', multi_replace_file_content: 'Editing',
19
+ apply_patch: 'Editing', patch: 'Editing', str_replace_editor: 'Editing',
17
20
  WriteFile: 'Creating', EditFile: 'Editing', DeleteFile: 'Removing',
21
+ CreateFile: 'Creating', RemoveFile: 'Removing', MoveFile: 'Moving',
18
22
  ReplaceFileContent: 'Editing', MultiReplaceFileContent: 'Editing'
19
23
  };
20
24
  function stringField(value) {
21
25
  return typeof value === 'string' ? value : '';
22
26
  }
23
27
  function codeFromArgs(args) {
24
- const direct = args.content ?? args.Content ?? args.CodeContent ?? args.code ?? args.new_string ?? args.newText ?? args.replaceContent ?? args.ReplacementContent;
28
+ const direct = args.content ?? args.Content ?? args.CodeContent ?? args.codeContent ?? args.code ?? args.Code
29
+ ?? args.new_string ?? args.new_str ?? args.newText ?? args.NewText ?? args.replaceContent ?? args.ReplaceContent
30
+ ?? args.replacementContent ?? args.ReplacementContent ?? args.replacement_content ?? args.text ?? args.Text
31
+ ?? args.after ?? args.After;
25
32
  if (typeof direct === 'string')
26
33
  return direct;
27
- const edits = args.edits ?? args.ReplacementChunks;
34
+ const edits = args.edits ?? args.ReplacementChunks ?? args.chunks ?? args.Chunks;
28
35
  if (Array.isArray(edits)) {
29
36
  return edits.map((item) => {
30
37
  if (!item || typeof item !== 'object')
31
38
  return '';
32
39
  const entry = item;
33
- return stringField(entry.new_string ?? entry.newText ?? entry.replaceContent ?? entry.ReplacementContent ?? entry.content);
40
+ return stringField(entry.new_string ?? entry.new_str ?? entry.newText ?? entry.NewText
41
+ ?? entry.replaceContent ?? entry.ReplaceContent ?? entry.replacementContent
42
+ ?? entry.ReplacementContent ?? entry.replacement_content ?? entry.content ?? entry.Content
43
+ ?? entry.text ?? entry.Text ?? entry.after ?? entry.After);
34
44
  }).join('\n');
35
45
  }
36
46
  return '';
37
47
  }
38
48
  function previousCodeFromArgs(args) {
39
- const direct = args.old_string ?? args.oldText ?? args.searchContent ?? args.SearchContent;
49
+ const direct = args.old_string ?? args.old_str ?? args.oldText ?? args.OldText
50
+ ?? args.searchContent ?? args.SearchContent ?? args.targetContent ?? args.TargetContent
51
+ ?? args.target_content ?? args.oldContent ?? args.OldContent ?? args.old_content
52
+ ?? args.before ?? args.Before;
40
53
  if (typeof direct === 'string')
41
54
  return direct;
42
- const edits = args.edits ?? args.ReplacementChunks;
55
+ const edits = args.edits ?? args.ReplacementChunks ?? args.chunks ?? args.Chunks;
43
56
  if (!Array.isArray(edits))
44
57
  return undefined;
45
58
  const previous = edits.map((item) => {
46
59
  if (!item || typeof item !== 'object')
47
60
  return undefined;
48
61
  const entry = item;
49
- const value = entry.old_string ?? entry.oldText ?? entry.searchContent ?? entry.SearchContent;
62
+ const value = entry.old_string ?? entry.old_str ?? entry.oldText ?? entry.OldText
63
+ ?? entry.searchContent ?? entry.SearchContent ?? entry.targetContent ?? entry.TargetContent
64
+ ?? entry.target_content ?? entry.oldContent ?? entry.OldContent ?? entry.old_content
65
+ ?? entry.before ?? entry.Before;
50
66
  return typeof value === 'string' ? value : undefined;
51
67
  });
52
68
  return previous.length && previous.every(value => value !== undefined) ? previous.join('\n') : undefined;
@@ -172,7 +188,13 @@ export class NativeCodeObserver {
172
188
  const entry = this.active.get(event.id);
173
189
  const current = this.read(target);
174
190
  if (current !== null && (current !== entry?.previous || event.operation === 'Creating')) {
175
- this.emit({ ...event, code: current, ...(entry?.previous !== null && entry?.previous !== undefined ? { before: entry.previous } : {}) });
191
+ this.emit({
192
+ ...event,
193
+ code: current,
194
+ ...(entry?.previous !== null && entry?.previous !== undefined && event.operation !== 'Creating'
195
+ ? { before: entry.previous }
196
+ : {})
197
+ });
176
198
  }
177
199
  }
178
200
  this.stop(event.id);
@@ -185,7 +207,11 @@ export class NativeCodeObserver {
185
207
  if (current === null || current === entry.previous || current === entry.lastShown)
186
208
  return;
187
209
  entry.lastShown = current;
188
- this.emit({ ...entry.event, code: current, ...(entry.previous !== null ? { before: entry.previous } : {}) });
210
+ this.emit({
211
+ ...entry.event,
212
+ code: current,
213
+ ...(entry.previous !== null && entry.event.operation !== 'Creating' ? { before: entry.previous } : {})
214
+ });
189
215
  }
190
216
  dispose() {
191
217
  for (const id of this.active.keys())
@@ -226,6 +252,17 @@ function splitCodeLines(code) {
226
252
  function previewLines(event) {
227
253
  if (event.code === undefined)
228
254
  return [];
255
+ // When creating or drafting code, every line is new code (addition).
256
+ // Even if a previous file existed on disk, the action is Creating/Drafting.
257
+ if (event.operation === 'Creating' || event.operation === 'Drafting') {
258
+ return splitCodeLines(event.code).map(text => ({ kind: 'add', text }));
259
+ }
260
+ // When removing, all lines are deletions.
261
+ if (event.operation === 'Removing') {
262
+ const text = event.before ?? event.code;
263
+ return splitCodeLines(text).map(text => ({ kind: 'del', text }));
264
+ }
265
+ // When editing with known previous content:
229
266
  if (event.before !== undefined) {
230
267
  const oldLines = splitCodeLines(event.before);
231
268
  const newLines = splitCodeLines(event.code);
@@ -238,24 +275,58 @@ function previewLines(event) {
238
275
  suffix++;
239
276
  const changedOld = oldLines.slice(prefix, oldLines.length - suffix);
240
277
  const changedNew = newLines.slice(prefix, newLines.length - suffix);
278
+ if (changedOld.length === 0) {
279
+ return [
280
+ ...oldLines.slice(0, prefix).map(text => ({ kind: 'ctx', text })),
281
+ ...changedNew.map(text => ({ kind: 'add', text })),
282
+ ...oldLines.slice(oldLines.length - suffix).map(text => ({ kind: 'ctx', text }))
283
+ ];
284
+ }
285
+ if (changedNew.length === 0) {
286
+ return [
287
+ ...oldLines.slice(0, prefix).map(text => ({ kind: 'ctx', text })),
288
+ ...changedOld.map(text => ({ kind: 'del', text })),
289
+ ...oldLines.slice(oldLines.length - suffix).map(text => ({ kind: 'ctx', text }))
290
+ ];
291
+ }
241
292
  const result = diffLines(changedOld.join('\n'), changedNew.join('\n'));
242
- // Diff only the changed middle so a small edit in a long file stays fast.
243
- // A partial middle diff could mislabel lines, so leave that case neutral.
244
- if (result.truncated)
245
- return newLines.map(text => ({ kind: 'ctx', text }));
293
+ const ops = [...result.ops];
294
+ // If diff was truncated due to exceeding MAX_SIDE_LINES, preserve remaining lines as del/add rather than losing them
295
+ if (result.truncated) {
296
+ if (changedOld.length > MAX_SIDE_LINES) {
297
+ for (let i = MAX_SIDE_LINES; i < changedOld.length; i++) {
298
+ ops.push({ kind: 'del', text: changedOld[i] });
299
+ }
300
+ }
301
+ if (changedNew.length > MAX_SIDE_LINES) {
302
+ for (let j = MAX_SIDE_LINES; j < changedNew.length; j++) {
303
+ ops.push({ kind: 'add', text: changedNew[j] });
304
+ }
305
+ }
306
+ }
246
307
  return [
247
308
  ...oldLines.slice(0, prefix).map(text => ({ kind: 'ctx', text })),
248
- ...result.ops,
309
+ ...ops,
249
310
  ...oldLines.slice(oldLines.length - suffix).map(text => ({ kind: 'ctx', text }))
250
311
  ];
251
312
  }
252
- const kind = event.operation === 'Removing' ? 'del'
253
- : event.operation === 'Creating' || event.operation === 'Drafting' ? 'add' : 'ctx';
254
- return splitCodeLines(event.code).map(text => ({ kind, text }));
313
+ // Editing without evidenced 'before': preserve existing test contract by rendering lines as ctx
314
+ return splitCodeLines(event.code).map(text => ({ kind: 'ctx', text }));
255
315
  }
256
316
  export function codePreviewLineCount(event) {
257
317
  return previewLines(event).length;
258
318
  }
319
+ export function defaultPreviewStart(event) {
320
+ const codeLines = previewLines(event);
321
+ const maxStart = Math.max(0, codeLines.length - CODE_PANEL_LINES);
322
+ if (event.operation === 'Editing') {
323
+ const firstChange = codeLines.findIndex(l => l.kind === 'add' || l.kind === 'del');
324
+ if (firstChange >= 0) {
325
+ return Math.max(0, Math.min(maxStart, firstChange - 2));
326
+ }
327
+ }
328
+ return maxStart;
329
+ }
259
330
  function highlightCode(kind, text, width) {
260
331
  const marker = kind === 'add' ? '+' : kind === 'del' ? '-' : ' ';
261
332
  const role = kind === 'add' ? 'success' : kind === 'del' ? 'danger' : 'muted';
@@ -283,7 +354,7 @@ export function renderCodeProgress(event, width, modelNote = '', startLine) {
283
354
  rows.push(truncate(`model note: ${safeDisplay(modelNote)}`, inner));
284
355
  const codeLines = previewLines(event);
285
356
  const maxStart = Math.max(0, codeLines.length - CODE_PANEL_LINES);
286
- const start = startLine === undefined ? maxStart : Math.max(0, Math.min(Math.floor(startLine), maxStart));
357
+ const start = startLine === undefined ? defaultPreviewStart(event) : Math.max(0, Math.min(Math.floor(startLine), maxStart));
287
358
  const visible = codeLines.slice(start, start + CODE_PANEL_LINES);
288
359
  if (visible.length) {
289
360
  rows.push('');
@@ -3,7 +3,7 @@ import { safeDisplay, truncate, padEnd } from './text.js';
3
3
  // The terminal theme is still a standalone JavaScript module in the TS migration.
4
4
  // @ts-expect-error theme.js has no TypeScript declaration yet.
5
5
  import { COLOR_DEPTH, IS_LIGHT, paint } from './theme.js';
6
- import { diffLines } from './diff.js';
6
+ import { diffLines, MAX_SIDE_LINES } from './diff.js';
7
7
  import fs from 'node:fs';
8
8
  import path from 'node:path';
9
9
 
@@ -31,8 +31,12 @@ const FILE_TOOLS: Record<string, string> = {
31
31
  'fs.write': 'Creating', 'fs.edit': 'Editing',
32
32
  fs_delete: 'Removing', fs_move: 'Moving',
33
33
  write_file: 'Creating', edit_file: 'Editing', delete_file: 'Removing',
34
+ create_file: 'Creating', new_file: 'Creating',
35
+ remove_file: 'Removing', unlink: 'Removing', move_file: 'Moving',
34
36
  replace_file_content: 'Editing', multi_replace_file_content: 'Editing',
37
+ apply_patch: 'Editing', patch: 'Editing', str_replace_editor: 'Editing',
35
38
  WriteFile: 'Creating', EditFile: 'Editing', DeleteFile: 'Removing',
39
+ CreateFile: 'Creating', RemoveFile: 'Removing', MoveFile: 'Moving',
36
40
  ReplaceFileContent: 'Editing', MultiReplaceFileContent: 'Editing'
37
41
  };
38
42
 
@@ -41,28 +45,42 @@ function stringField(value: unknown): string {
41
45
  }
42
46
 
43
47
  function codeFromArgs(args: Record<string, unknown>): string {
44
- const direct = args.content ?? args.Content ?? args.CodeContent ?? args.code ?? args.new_string ?? args.newText ?? args.replaceContent ?? args.ReplacementContent;
48
+ const direct = args.content ?? args.Content ?? args.CodeContent ?? args.codeContent ?? args.code ?? args.Code
49
+ ?? args.new_string ?? args.new_str ?? args.newText ?? args.NewText ?? args.replaceContent ?? args.ReplaceContent
50
+ ?? args.replacementContent ?? args.ReplacementContent ?? args.replacement_content ?? args.text ?? args.Text
51
+ ?? args.after ?? args.After;
45
52
  if (typeof direct === 'string') return direct;
46
- const edits = args.edits ?? args.ReplacementChunks;
53
+ const edits = args.edits ?? args.ReplacementChunks ?? args.chunks ?? args.Chunks;
47
54
  if (Array.isArray(edits)) {
48
55
  return edits.map((item: unknown) => {
49
56
  if (!item || typeof item !== 'object') return '';
50
57
  const entry = item as Record<string, unknown>;
51
- return stringField(entry.new_string ?? entry.newText ?? entry.replaceContent ?? entry.ReplacementContent ?? entry.content);
58
+ return stringField(
59
+ entry.new_string ?? entry.new_str ?? entry.newText ?? entry.NewText
60
+ ?? entry.replaceContent ?? entry.ReplaceContent ?? entry.replacementContent
61
+ ?? entry.ReplacementContent ?? entry.replacement_content ?? entry.content ?? entry.Content
62
+ ?? entry.text ?? entry.Text ?? entry.after ?? entry.After
63
+ );
52
64
  }).join('\n');
53
65
  }
54
66
  return '';
55
67
  }
56
68
 
57
69
  function previousCodeFromArgs(args: Record<string, unknown>): string | undefined {
58
- const direct = args.old_string ?? args.oldText ?? args.searchContent ?? args.SearchContent;
70
+ const direct = args.old_string ?? args.old_str ?? args.oldText ?? args.OldText
71
+ ?? args.searchContent ?? args.SearchContent ?? args.targetContent ?? args.TargetContent
72
+ ?? args.target_content ?? args.oldContent ?? args.OldContent ?? args.old_content
73
+ ?? args.before ?? args.Before;
59
74
  if (typeof direct === 'string') return direct;
60
- const edits = args.edits ?? args.ReplacementChunks;
75
+ const edits = args.edits ?? args.ReplacementChunks ?? args.chunks ?? args.Chunks;
61
76
  if (!Array.isArray(edits)) return undefined;
62
77
  const previous = edits.map((item: unknown) => {
63
78
  if (!item || typeof item !== 'object') return undefined;
64
79
  const entry = item as Record<string, unknown>;
65
- const value = entry.old_string ?? entry.oldText ?? entry.searchContent ?? entry.SearchContent;
80
+ const value = entry.old_string ?? entry.old_str ?? entry.oldText ?? entry.OldText
81
+ ?? entry.searchContent ?? entry.SearchContent ?? entry.targetContent ?? entry.TargetContent
82
+ ?? entry.target_content ?? entry.oldContent ?? entry.OldContent ?? entry.old_content
83
+ ?? entry.before ?? entry.Before;
66
84
  return typeof value === 'string' ? value : undefined;
67
85
  });
68
86
  return previous.length && previous.every(value => value !== undefined) ? previous.join('\n') : undefined;
@@ -185,7 +203,13 @@ export class NativeCodeObserver {
185
203
  const entry = this.active.get(event.id);
186
204
  const current = this.read(target);
187
205
  if (current !== null && (current !== entry?.previous || event.operation === 'Creating')) {
188
- this.emit({ ...event, code: current, ...(entry?.previous !== null && entry?.previous !== undefined ? { before: entry.previous } : {}) });
206
+ this.emit({
207
+ ...event,
208
+ code: current,
209
+ ...(entry?.previous !== null && entry?.previous !== undefined && event.operation !== 'Creating'
210
+ ? { before: entry.previous }
211
+ : {})
212
+ });
189
213
  }
190
214
  }
191
215
  this.stop(event.id);
@@ -197,7 +221,11 @@ export class NativeCodeObserver {
197
221
  const current = this.read(entry.path);
198
222
  if (current === null || current === entry.previous || current === entry.lastShown) return;
199
223
  entry.lastShown = current;
200
- this.emit({ ...entry.event, code: current, ...(entry.previous !== null ? { before: entry.previous } : {}) });
224
+ this.emit({
225
+ ...entry.event,
226
+ code: current,
227
+ ...(entry.previous !== null && entry.event.operation !== 'Creating' ? { before: entry.previous } : {})
228
+ });
201
229
  }
202
230
 
203
231
  dispose(): void {
@@ -236,6 +264,20 @@ function splitCodeLines(code: string): string[] {
236
264
 
237
265
  function previewLines(event: CodeProgressEvent): PreviewLine[] {
238
266
  if (event.code === undefined) return [];
267
+
268
+ // When creating or drafting code, every line is new code (addition).
269
+ // Even if a previous file existed on disk, the action is Creating/Drafting.
270
+ if (event.operation === 'Creating' || event.operation === 'Drafting') {
271
+ return splitCodeLines(event.code).map(text => ({ kind: 'add', text }));
272
+ }
273
+
274
+ // When removing, all lines are deletions.
275
+ if (event.operation === 'Removing') {
276
+ const text = event.before ?? event.code;
277
+ return splitCodeLines(text).map(text => ({ kind: 'del', text }));
278
+ }
279
+
280
+ // When editing with known previous content:
239
281
  if (event.before !== undefined) {
240
282
  const oldLines = splitCodeLines(event.before);
241
283
  const newLines = splitCodeLines(event.code);
@@ -244,27 +286,70 @@ function previewLines(event: CodeProgressEvent): PreviewLine[] {
244
286
  let suffix = 0;
245
287
  while (suffix < oldLines.length - prefix && suffix < newLines.length - prefix
246
288
  && oldLines[oldLines.length - suffix - 1] === newLines[newLines.length - suffix - 1]) suffix++;
289
+
247
290
  const changedOld = oldLines.slice(prefix, oldLines.length - suffix);
248
291
  const changedNew = newLines.slice(prefix, newLines.length - suffix);
292
+
293
+ if (changedOld.length === 0) {
294
+ return [
295
+ ...oldLines.slice(0, prefix).map(text => ({ kind: 'ctx' as const, text })),
296
+ ...changedNew.map(text => ({ kind: 'add' as const, text })),
297
+ ...oldLines.slice(oldLines.length - suffix).map(text => ({ kind: 'ctx' as const, text }))
298
+ ];
299
+ }
300
+
301
+ if (changedNew.length === 0) {
302
+ return [
303
+ ...oldLines.slice(0, prefix).map(text => ({ kind: 'ctx' as const, text })),
304
+ ...changedOld.map(text => ({ kind: 'del' as const, text })),
305
+ ...oldLines.slice(oldLines.length - suffix).map(text => ({ kind: 'ctx' as const, text }))
306
+ ];
307
+ }
308
+
249
309
  const result = diffLines(changedOld.join('\n'), changedNew.join('\n'));
250
- // Diff only the changed middle so a small edit in a long file stays fast.
251
- // A partial middle diff could mislabel lines, so leave that case neutral.
252
- if (result.truncated) return newLines.map(text => ({ kind: 'ctx', text }));
310
+ const ops: PreviewLine[] = [...result.ops];
311
+
312
+ // If diff was truncated due to exceeding MAX_SIDE_LINES, preserve remaining lines as del/add rather than losing them
313
+ if (result.truncated) {
314
+ if (changedOld.length > MAX_SIDE_LINES) {
315
+ for (let i = MAX_SIDE_LINES; i < changedOld.length; i++) {
316
+ ops.push({ kind: 'del' as const, text: changedOld[i]! });
317
+ }
318
+ }
319
+ if (changedNew.length > MAX_SIDE_LINES) {
320
+ for (let j = MAX_SIDE_LINES; j < changedNew.length; j++) {
321
+ ops.push({ kind: 'add' as const, text: changedNew[j]! });
322
+ }
323
+ }
324
+ }
325
+
253
326
  return [
254
327
  ...oldLines.slice(0, prefix).map(text => ({ kind: 'ctx' as const, text })),
255
- ...result.ops,
328
+ ...ops,
256
329
  ...oldLines.slice(oldLines.length - suffix).map(text => ({ kind: 'ctx' as const, text }))
257
330
  ];
258
331
  }
259
- const kind = event.operation === 'Removing' ? 'del'
260
- : event.operation === 'Creating' || event.operation === 'Drafting' ? 'add' : 'ctx';
261
- return splitCodeLines(event.code).map(text => ({ kind, text }));
332
+
333
+ // Editing without evidenced 'before': preserve existing test contract by rendering lines as ctx
334
+ return splitCodeLines(event.code).map(text => ({ kind: 'ctx', text }));
262
335
  }
263
336
 
264
337
  export function codePreviewLineCount(event: CodeProgressEvent): number {
265
338
  return previewLines(event).length;
266
339
  }
267
340
 
341
+ export function defaultPreviewStart(event: CodeProgressEvent): number {
342
+ const codeLines = previewLines(event);
343
+ const maxStart = Math.max(0, codeLines.length - CODE_PANEL_LINES);
344
+ if (event.operation === 'Editing') {
345
+ const firstChange = codeLines.findIndex(l => l.kind === 'add' || l.kind === 'del');
346
+ if (firstChange >= 0) {
347
+ return Math.max(0, Math.min(maxStart, firstChange - 2));
348
+ }
349
+ }
350
+ return maxStart;
351
+ }
352
+
268
353
  function highlightCode(kind: PreviewLine['kind'], text: string, width: number): string {
269
354
  const marker = kind === 'add' ? '+' : kind === 'del' ? '-' : ' ';
270
355
  const role = kind === 'add' ? 'success' : kind === 'del' ? 'danger' : 'muted';
@@ -290,7 +375,7 @@ export function renderCodeProgress(event: CodeProgressEvent, width: number, mode
290
375
  if (modelNote) rows.push(truncate(`model note: ${safeDisplay(modelNote)}`, inner));
291
376
  const codeLines = previewLines(event);
292
377
  const maxStart = Math.max(0, codeLines.length - CODE_PANEL_LINES);
293
- const start = startLine === undefined ? maxStart : Math.max(0, Math.min(Math.floor(startLine), maxStart));
378
+ const start = startLine === undefined ? defaultPreviewStart(event) : Math.max(0, Math.min(Math.floor(startLine), maxStart));
294
379
  const visible = codeLines.slice(start, start + CODE_PANEL_LINES);
295
380
  if (visible.length) {
296
381
  rows.push('');
@@ -1,5 +1,5 @@
1
1
  import { ChatRenderer } from './chatRenderer.js';
2
- import { CODE_PANEL_LINES, codePreviewLineCount, renderCodeProgress } from './codeProgress.js';
2
+ import { CODE_PANEL_LINES, codePreviewLineCount, defaultPreviewStart, renderCodeProgress } from './codeProgress.js';
3
3
  function lastStart(event) {
4
4
  return Math.max(0, codePreviewLineCount(event) - CODE_PANEL_LINES);
5
5
  }
@@ -113,7 +113,12 @@ export class CodeStreamRenderer extends ChatRenderer {
113
113
  const last = transcript?.frames.at(-1);
114
114
  const samePanel = last?.meta?.codePreview === true && last.meta.id === event.id;
115
115
  const state = samePanel && last ? this.codePanels.get(last) : undefined;
116
- const panelState = state || { event, modelNote: this.modelNote, start: lastStart(event), followTail: true };
116
+ const panelState = state || {
117
+ event,
118
+ modelNote: this.modelNote,
119
+ start: defaultPreviewStart(event),
120
+ followTail: event.operation !== 'Editing'
121
+ };
117
122
  panelState.event = event;
118
123
  panelState.modelNote = this.modelNote;
119
124
  panelState.start = panelState.followTail ? lastStart(event) : Math.min(panelState.start, lastStart(event));
@@ -1,5 +1,5 @@
1
1
  import { ChatRenderer } from './chatRenderer.js';
2
- import { CODE_PANEL_LINES, codePreviewLineCount, renderCodeProgress, type CodeProgressEvent } from './codeProgress.js';
2
+ import { CODE_PANEL_LINES, codePreviewLineCount, defaultPreviewStart, renderCodeProgress, type CodeProgressEvent } from './codeProgress.js';
3
3
 
4
4
  type RendererEvent = { type: string; [key: string]: unknown };
5
5
  type CodePanelState = { event: CodeProgressEvent; modelNote: string; start: number; followTail: boolean };
@@ -126,7 +126,12 @@ export class CodeStreamRenderer extends ChatRenderer {
126
126
  const last = transcript?.frames.at(-1);
127
127
  const samePanel = last?.meta?.codePreview === true && last.meta.id === event.id;
128
128
  const state = samePanel && last ? this.codePanels.get(last) : undefined;
129
- const panelState: CodePanelState = state || { event, modelNote: this.modelNote, start: lastStart(event), followTail: true };
129
+ const panelState: CodePanelState = state || {
130
+ event,
131
+ modelNote: this.modelNote,
132
+ start: defaultPreviewStart(event),
133
+ followTail: event.operation !== 'Editing'
134
+ };
130
135
  panelState.event = event;
131
136
  panelState.modelNote = this.modelNote;
132
137
  panelState.start = panelState.followTail ? lastStart(event) : Math.min(panelState.start, lastStart(event));
@@ -1,5 +1,6 @@
1
1
  import { loadConfig, saveConfig } from '../config.js';
2
2
  import { getAvailableProvidersOverview } from '../dynamicCatalogue.js';
3
+ import { writeClipboardSync } from './clipboardSync.js';
3
4
  // @ts-expect-error dialogs.js is awaiting TypeScript migration.
4
5
  import { SelectLayer } from './dialogs.js';
5
6
  export function openModeControl(prompt, loop) {
@@ -93,8 +94,16 @@ export function triggerTurnInterrupt(prompt, formatInterruptedRow, options = {})
93
94
  }
94
95
  export function handlePromptCtrlC(prompt, formatInterruptedRow, windowMs = 2000, now = Date.now()) {
95
96
  if (!prompt.editor.isEmpty) {
97
+ const text = prompt.editor.getText();
98
+ if (text) {
99
+ writeClipboardSync(text);
100
+ if ('killRing' in prompt.editor) {
101
+ prompt.editor.killRing = text;
102
+ }
103
+ }
96
104
  prompt.editor.clear();
97
105
  prompt.completion = null;
106
+ prompt.notice = text ? 'cleared and copied to clipboard' : '';
98
107
  return;
99
108
  }
100
109
  if (prompt.busy) {
@@ -1,5 +1,6 @@
1
1
  import { loadConfig, saveConfig } from '../config.js';
2
2
  import { getAvailableProvidersOverview } from '../dynamicCatalogue.js';
3
+ import { writeClipboardSync } from './clipboardSync.js';
3
4
  // @ts-expect-error dialogs.js is awaiting TypeScript migration.
4
5
  import { SelectLayer } from './dialogs.js';
5
6
 
@@ -142,8 +143,16 @@ export function handlePromptCtrlC(
142
143
  now = Date.now()
143
144
  ): void {
144
145
  if (!prompt.editor.isEmpty) {
146
+ const text = prompt.editor.getText();
147
+ if (text) {
148
+ writeClipboardSync(text);
149
+ if ('killRing' in prompt.editor) {
150
+ (prompt.editor as { killRing?: string }).killRing = text;
151
+ }
152
+ }
145
153
  prompt.editor.clear();
146
154
  prompt.completion = null;
155
+ prompt.notice = text ? 'cleared and copied to clipboard' : '';
147
156
  return;
148
157
  }
149
158
  if (prompt.busy) {
@@ -348,6 +348,11 @@ export class ApprovalLayer extends Layer {
348
348
  // Selectable options, modelled on opencode's permission prompt: a row of
349
349
  // chips moved with ←/→ and committed with enter. Typing y/n still answers
350
350
  // directly, so the fast path stays one keystroke rather than two.
351
+ // grant_capability stores consent for the named capability group itself.
352
+ // A standing grant for the *manager tool* would also authorize unrelated
353
+ // package and MCP operations, and adds a redundant second confirmation.
354
+ this.capabilityGrant = this.request.tool === 'manage_local_dependencies'
355
+ && this.request.args?.action === 'grant_capability';
351
356
  this.options = [
352
357
  { id: 'once', label: 'Allow once', answer: true },
353
358
  // Standing approval for the rest of the PROCESS — never written to disk.
@@ -355,7 +360,7 @@ export class ApprovalLayer extends Layer {
355
360
  // and a gate people hammer through is worse than one they chose to lift.
356
361
  // It is not the default and it is confirmed a second time before it takes
357
362
  // effect; see makeApprovalHandler in cliMode.js.
358
- { id: 'always', label: 'Allow always', answer: true },
363
+ ...(!this.capabilityGrant ? [{ id: 'always', label: 'Allow always', answer: true }] : []),
359
364
  { id: 'reject', label: 'Reject', answer: false }
360
365
  ];
361
366
 
@@ -372,7 +377,7 @@ export class ApprovalLayer extends Layer {
372
377
  // authorises anything the agent asks for.
373
378
  this.selected = this.options.findIndex(option => option.id === 'reject');
374
379
 
375
- this.footer = `←→ choose ${GLYPHS.dot} enter confirm ${GLYPHS.dot} y/a/n direct ${GLYPHS.dot} ↑↓ scroll ${GLYPHS.dot} esc reject`;
380
+ this.footer = `←→ choose ${GLYPHS.dot} enter confirm ${GLYPHS.dot} ${this.capabilityGrant ? 'y/n' : 'y/a/n'} direct ${GLYPHS.dot} ↑↓ scroll ${GLYPHS.dot} esc reject`;
376
381
 
377
382
  this.diff = approvalDiff(this.request.tool, this.request.args, { width: this.width - 6, maxLines: 400 });
378
383
  }
@@ -466,7 +471,7 @@ export class ApprovalLayer extends Layer {
466
471
  // dialog could not be approved at all; escape worked, which made it look
467
472
  // responsive. See src/tui/keys.js isChar().
468
473
  if (isChar(event, 'y')) { this.answer(true, 'once'); return 'close'; }
469
- if (isChar(event, 'a')) { this.answer(true, 'always'); return 'close'; }
474
+ if (!this.capabilityGrant && isChar(event, 'a')) { this.answer(true, 'always'); return 'close'; }
470
475
  if (isChar(event, 'n')) { this.answer(false, 'reject'); return 'close'; }
471
476
  if (event.name === 'escape') { this.answer(false, 'reject'); return 'close'; }
472
477
 
package/src/tui/diff.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export type DiffLine = { kind: 'ctx' | 'add' | 'del'; text: string };
2
2
  export function diffLines(before: string, after: string): { ops: DiffLine[]; truncated: boolean };
3
+ export const MAX_SIDE_LINES: number;