@causyne/agent 1.20.8 → 1.20.10

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 (74) hide show
  1. package/package.json +2 -1
  2. package/src/agentLoop.js +291 -103
  3. package/src/agentLoop.ts +322 -109
  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/compiledSkills.js +7330 -319
  8. package/src/computerUse/types.js +8 -0
  9. package/src/computerUse/types.ts +121 -0
  10. package/src/computerUse/visionGrounding.js +87 -29
  11. package/src/computerUse/visionGrounding.ts +108 -36
  12. package/src/contextManager.js +16 -0
  13. package/src/decisionPolicy.js +7 -9
  14. package/src/decisionPolicy.ts +7 -9
  15. package/src/documentEngine/antiSlopAuditor.js +13 -43
  16. package/src/documentEngine/antiSlopAuditor.ts +15 -47
  17. package/src/documentEngine/documentForge.js +3 -3
  18. package/src/documentEngine/documentForge.ts +3 -3
  19. package/src/launchIntent.js +3 -3
  20. package/src/launchIntent.ts +3 -3
  21. package/src/planCache.js +4 -14
  22. package/src/planCache.ts +4 -14
  23. package/src/providers/acp.js +4 -2
  24. package/src/providers/acp.ts +4 -2
  25. package/src/providers/antigravity.js +191 -28
  26. package/src/providers/antigravity.ts +192 -27
  27. package/src/providers/base.d.ts +2 -0
  28. package/src/providers/pi.js +3 -1
  29. package/src/providers/pi.ts +4 -2
  30. package/src/provisioning/capabilityProvisioner.js +2 -7
  31. package/src/provisioning/npmInstall.js +19 -0
  32. package/src/provisioning/npmInstall.ts +19 -0
  33. package/src/sandbox.js +7 -10
  34. package/src/smartToolsUse.js +223 -0
  35. package/src/smartToolsUse.ts +284 -0
  36. package/src/stallGuard.js +356 -0
  37. package/src/stallGuard.ts +432 -0
  38. package/src/taskShape.js +7 -7
  39. package/src/taskShape.ts +7 -7
  40. package/src/toolPacks.js +5 -5
  41. package/src/tools/blenderAutomator.js +198 -11
  42. package/src/tools/browserAutomator.js +3 -3
  43. package/src/tools/browserAutomator.ts +3 -3
  44. package/src/tools/documentForge.js +1 -1
  45. package/src/tools/documentForge.ts +1 -1
  46. package/src/tools/fsRead.js +91 -80
  47. package/src/tools/fsRead.ts +125 -0
  48. package/src/tools/peerAgent.js +1 -1
  49. package/src/tools/peerAgent.ts +1 -1
  50. package/src/tools/screenshot.js +5 -1
  51. package/src/tools/screenshot.ts +5 -1
  52. package/src/tools/toolScanner.js +19 -0
  53. package/src/tools/toolScanner.ts +17 -0
  54. package/src/tools/toolSelfRepair.js +63 -1
  55. package/src/tools/toolSelfRepair.ts +61 -1
  56. package/src/tools/visionInspector.js +80 -142
  57. package/src/tools/visionInspector.ts +110 -0
  58. package/src/tui/chatboxClipboard.js +152 -0
  59. package/src/tui/chatboxClipboard.ts +201 -0
  60. package/src/tui/clipboardSync.js +38 -7
  61. package/src/tui/clipboardSync.ts +39 -7
  62. package/src/tui/codeProgress.js +89 -18
  63. package/src/tui/codeProgress.ts +102 -17
  64. package/src/tui/codeStreamRenderer.js +7 -2
  65. package/src/tui/codeStreamRenderer.ts +7 -2
  66. package/src/tui/composerControls.js +9 -0
  67. package/src/tui/composerControls.ts +9 -0
  68. package/src/tui/dialogs.js +8 -3
  69. package/src/tui/diff.d.ts +1 -0
  70. package/src/tui/lineEditor.js +166 -14
  71. package/src/tui/mouseHover.js +6 -2
  72. package/src/tui/mouseHover.ts +7 -2
  73. package/src/tui/prompt.js +96 -19
  74. package/src/tui/stageSurface.js +3 -1
@@ -0,0 +1,356 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { isMutatingToolCall } from './decisionPolicy.js';
3
+ export const STALL_GUARD_REPEATABLE_TOOLS = new Set([
4
+ 'process_list',
5
+ 'sys_run_poll',
6
+ 'proc_term'
7
+ ]);
8
+ export const FAILURE_TOLERANT_TOOL_NAMES = new Set([
9
+ 'sysRun',
10
+ 'sysRunBg',
11
+ 'terminal_persistent_shell',
12
+ 'grep_search',
13
+ 'fs_search',
14
+ 'code_searcher',
15
+ 'codebase_search',
16
+ 'web_search'
17
+ ]);
18
+ export const IDEMPOTENT_TOOL_NAMES = new Set([
19
+ 'fs_read',
20
+ 'fs_list',
21
+ 'fs_find',
22
+ 'fs_search',
23
+ 'code_search_ast',
24
+ 'code_searcher',
25
+ 'codebase_search',
26
+ 'pdf_read',
27
+ 'clipboard_read',
28
+ 'read_native_skill',
29
+ 'desktop_open',
30
+ 'check_system_specs',
31
+ 'web_search',
32
+ 'web_fetch',
33
+ 'grep_search',
34
+ 'dir_list',
35
+ 'view_image',
36
+ 'vision_inspect_ui',
37
+ 'ax_snapshot',
38
+ 'browser_snapshot',
39
+ 'browser_console',
40
+ 'browser_get_dom',
41
+ 'read_browser_page',
42
+ 'deferred_mcp_search'
43
+ ]);
44
+ export const MUTATING_TOOL_NAMES = new Set([
45
+ 'fs_write',
46
+ 'fs_edit',
47
+ 'MultiEdit',
48
+ 'fs_delete',
49
+ 'fs_move',
50
+ 'archive_extract',
51
+ 'game_build_html5',
52
+ 'tool_forge',
53
+ 'create_skill',
54
+ 'manage_local_dependencies',
55
+ 'creative_batch',
56
+ 'blender_act',
57
+ 'godot_act'
58
+ ]);
59
+ export const STALL_GUARD_IDENTICAL_CALL_THRESHOLD = 3;
60
+ export const STALL_GUARD_IDENTICAL_CALL_HALT = 5;
61
+ export const STALL_GUARD_MAX_CYCLE_PERIOD = 4;
62
+ export const STALL_GUARD_CYCLE_HISTORY = 64;
63
+ export const IDENTICAL_RESULT_STUB_MIN_CHARS = 512;
64
+ export const RESEARCH_INSPECTION_NUDGE_AFTER = 25;
65
+ function stableSortObject(value) {
66
+ if (value === null || typeof value !== 'object') {
67
+ return value;
68
+ }
69
+ if (Array.isArray(value)) {
70
+ return value.map(stableSortObject);
71
+ }
72
+ const sorted = {};
73
+ for (const key of Object.keys(value).sort()) {
74
+ sorted[key] = stableSortObject(value[key]);
75
+ }
76
+ return sorted;
77
+ }
78
+ export function canonicalToolArgs(args) {
79
+ if (!args || typeof args !== 'object') {
80
+ return JSON.stringify(args ?? {});
81
+ }
82
+ return JSON.stringify(stableSortObject(args));
83
+ }
84
+ export function buildToolCallSignature(toolName, args) {
85
+ const canonical = canonicalToolArgs(args);
86
+ const argsHash = createHash('sha256').update(canonical).digest('hex').slice(0, 16);
87
+ return { toolName, argsHash };
88
+ }
89
+ export function computeResultHash(result) {
90
+ if (result === null || result === undefined)
91
+ return '';
92
+ const str = typeof result === 'string' ? result : JSON.stringify(result);
93
+ return createHash('sha256').update(str).digest('hex').slice(0, 24);
94
+ }
95
+ function ordinal(n) {
96
+ const v = n % 100;
97
+ if (v >= 11 && v <= 13)
98
+ return `${n}th`;
99
+ switch (n % 10) {
100
+ case 1: return `${n}st`;
101
+ case 2: return `${n}nd`;
102
+ case 3: return `${n}rd`;
103
+ default: return `${n}th`;
104
+ }
105
+ }
106
+ export class StallGuardController {
107
+ config;
108
+ _identicalStreakSig = null;
109
+ _identicalStreakResultHash = '';
110
+ _identicalStreakCount = 0;
111
+ _identicalStreakFirstCallId = '';
112
+ _callHistory = [];
113
+ _exactFailureCounts = new Map();
114
+ _distinctInspectionCount = 0;
115
+ _researchNudgeEmitted = false;
116
+ _haltDecision = null;
117
+ constructor(config) {
118
+ this.config = {
119
+ warningsEnabled: config?.warningsEnabled ?? true,
120
+ hardStopEnabled: config?.hardStopEnabled ?? false,
121
+ identicalCallWarnAfter: config?.identicalCallWarnAfter ?? STALL_GUARD_IDENTICAL_CALL_THRESHOLD,
122
+ identicalCallHaltAfter: config?.identicalCallHaltAfter ?? STALL_GUARD_IDENTICAL_CALL_HALT,
123
+ cycleWarnAfter: config?.cycleWarnAfter ?? STALL_GUARD_IDENTICAL_CALL_THRESHOLD,
124
+ cycleHaltAfter: config?.cycleHaltAfter ?? STALL_GUARD_IDENTICAL_CALL_HALT,
125
+ researchNudgeAfter: config?.researchNudgeAfter ?? RESEARCH_INSPECTION_NUDGE_AFTER,
126
+ stubMinChars: config?.stubMinChars ?? IDENTICAL_RESULT_STUB_MIN_CHARS
127
+ };
128
+ }
129
+ get haltDecision() {
130
+ return this._haltDecision;
131
+ }
132
+ get distinctInspectionCount() {
133
+ return this._distinctInspectionCount;
134
+ }
135
+ reset() {
136
+ this._identicalStreakSig = null;
137
+ this._identicalStreakResultHash = '';
138
+ this._identicalStreakCount = 0;
139
+ this._identicalStreakFirstCallId = '';
140
+ this._callHistory = [];
141
+ this._exactFailureCounts.clear();
142
+ this._distinctInspectionCount = 0;
143
+ this._researchNudgeEmitted = false;
144
+ this._haltDecision = null;
145
+ }
146
+ isRepeatable(toolName, args = {}) {
147
+ if (STALL_GUARD_REPEATABLE_TOOLS.has(toolName))
148
+ return true;
149
+ if (toolName.endsWith('_poll') || toolName.endsWith('_status') || toolName.endsWith('_get_result'))
150
+ return true;
151
+ if (toolName === 'sysRun' || toolName === 'sysRunBg') {
152
+ const cmd = String(args.command ?? '').trim();
153
+ if (/^(?:ps\s+aux|pgrep|lsof|netstat|ss\s+-)\b/i.test(cmd))
154
+ return true;
155
+ }
156
+ return false;
157
+ }
158
+ isMutating(toolName, args = {}) {
159
+ if (MUTATING_TOOL_NAMES.has(toolName))
160
+ return true;
161
+ return isMutatingToolCall(toolName, args);
162
+ }
163
+ isFailureTolerant(toolName) {
164
+ return FAILURE_TOLERANT_TOOL_NAMES.has(toolName);
165
+ }
166
+ recordMutation() {
167
+ this._exactFailureCounts.clear();
168
+ this._distinctInspectionCount = 0;
169
+ this._researchNudgeEmitted = false;
170
+ }
171
+ beforeCall(toolName, args = {}, context) {
172
+ const signature = buildToolCallSignature(toolName, args);
173
+ const sigKey = `${signature.toolName}:${signature.argsHash}`;
174
+ if (this._haltDecision) {
175
+ return this._haltDecision;
176
+ }
177
+ const failureCount = this._exactFailureCounts.get(sigKey) || 0;
178
+ if (this.config.hardStopEnabled && failureCount >= this.config.identicalCallHaltAfter) {
179
+ const decision = {
180
+ action: 'halt',
181
+ code: 'repeated_exact_failure_halt',
182
+ message: `Halted: identical call to '${toolName}' failed ${failureCount} times with unchanged arguments. Change strategy or explain the blocker.`,
183
+ toolName,
184
+ count: failureCount,
185
+ signature
186
+ };
187
+ this._haltDecision = decision;
188
+ return decision;
189
+ }
190
+ const repeatable = this.isRepeatable(toolName, args);
191
+ if (!repeatable && this._identicalStreakSig !== null) {
192
+ const isStreakMatch = this._identicalStreakSig.toolName === signature.toolName
193
+ && this._identicalStreakSig.argsHash === signature.argsHash;
194
+ if (isStreakMatch && this.config.hardStopEnabled && this._identicalStreakCount >= this.config.identicalCallHaltAfter) {
195
+ const decision = {
196
+ action: 'halt',
197
+ code: 'identical_call_limit_halt',
198
+ message: `Halted: tool '${toolName}' was invoked identically ${this._identicalStreakCount} times without progress.`,
199
+ toolName,
200
+ count: this._identicalStreakCount,
201
+ signature
202
+ };
203
+ this._haltDecision = decision;
204
+ return decision;
205
+ }
206
+ }
207
+ if (context?.isAuthoringTask && !this.isMutating(toolName, args) && this._distinctInspectionCount >= 24) {
208
+ const decision = {
209
+ action: 'halt',
210
+ code: 'read_only_loop_halt',
211
+ message: `FATAL ERROR: Read-Only Verification Loop Detected. Agent performed ${this._distinctInspectionCount} consecutive inspection steps without writing any implementation code. Forcibly aborting task to save tokens.`,
212
+ toolName,
213
+ count: this._distinctInspectionCount,
214
+ signature
215
+ };
216
+ this._haltDecision = decision;
217
+ return decision;
218
+ }
219
+ return {
220
+ action: 'allow',
221
+ code: 'allow',
222
+ message: '',
223
+ toolName,
224
+ count: 0,
225
+ signature
226
+ };
227
+ }
228
+ observeCall(toolName, args = {}, result, toolCallId = '', failed = false) {
229
+ const signature = buildToolCallSignature(toolName, args);
230
+ const sigKey = `${signature.toolName}:${signature.argsHash}`;
231
+ const resultStr = typeof result === 'string' ? result : JSON.stringify(result ?? {});
232
+ const rHash = computeResultHash(resultStr);
233
+ const repeatable = this.isRepeatable(toolName, args);
234
+ const mutating = this.isMutating(toolName, args);
235
+ if (mutating && !failed) {
236
+ this.recordMutation();
237
+ }
238
+ else if (!mutating) {
239
+ this._distinctInspectionCount++;
240
+ }
241
+ if (failed) {
242
+ const count = (this._exactFailureCounts.get(sigKey) || 0) + 1;
243
+ this._exactFailureCounts.set(sigKey, count);
244
+ this._identicalStreakSig = null;
245
+ this._identicalStreakResultHash = '';
246
+ this._identicalStreakCount = 0;
247
+ let notice = null;
248
+ if (this.config.warningsEnabled && count >= this.config.identicalCallWarnAfter) {
249
+ notice = `[STALL GUARD: Tool '${toolName}' with identical arguments failed ${count} times. Stop retrying unchanged; change arguments or adopt an alternative approach.]`;
250
+ }
251
+ return { notice, stub: null };
252
+ }
253
+ const matchesStreak = this._identicalStreakSig !== null
254
+ && this._identicalStreakSig.toolName === signature.toolName
255
+ && this._identicalStreakSig.argsHash === signature.argsHash
256
+ && this._identicalStreakResultHash === rHash;
257
+ if (matchesStreak) {
258
+ this._identicalStreakCount++;
259
+ }
260
+ else {
261
+ this._identicalStreakSig = signature;
262
+ this._identicalStreakResultHash = rHash;
263
+ this._identicalStreakCount = 1;
264
+ this._identicalStreakFirstCallId = toolCallId || '';
265
+ }
266
+ const count = this._identicalStreakCount;
267
+ let stub = null;
268
+ let notice = null;
269
+ let decision = null;
270
+ if (count >= 2 && !failed && resultStr.length >= this.config.stubMinChars) {
271
+ const argsPreview = canonicalToolArgs(args).slice(0, 100);
272
+ const priorId = this._identicalStreakFirstCallId ? `prior call ${this._identicalStreakFirstCallId}` : 'previous call';
273
+ stub = JSON.stringify({
274
+ success: true,
275
+ unchanged: true,
276
+ stub: `[Duplicate result identical to ${priorId}: ${toolName}(${argsPreview})]`
277
+ });
278
+ }
279
+ if (!repeatable && count >= this.config.identicalCallWarnAfter) {
280
+ notice = `[STALL GUARD: This is the ${ordinal(count)} consecutive identical call to '${toolName}' with identical arguments returning the unchanged result. Do not repeat it — change arguments, use a different tool, or proceed with what you have.]`;
281
+ if (this.config.hardStopEnabled && count >= this.config.identicalCallHaltAfter && !this._haltDecision) {
282
+ decision = {
283
+ action: 'halt',
284
+ code: 'identical_call_streak_halt',
285
+ message: `Stopped ${toolName}: the same call with identical arguments returned the same result ${count} times in a row. Stop repeating it unchanged; use the result already provided or change strategy.`,
286
+ toolName,
287
+ count,
288
+ signature
289
+ };
290
+ this._haltDecision = decision;
291
+ }
292
+ }
293
+ this._callHistory.push({ sig: signature, resultHash: rHash, repeatable });
294
+ if (this._callHistory.length > STALL_GUARD_CYCLE_HISTORY) {
295
+ this._callHistory.shift();
296
+ }
297
+ if (!notice) {
298
+ const cycle = this.detectIdenticalCycle();
299
+ if (cycle) {
300
+ const { period, laps } = cycle;
301
+ notice = `[STALL GUARD: The last ${laps} rounds repeated the same cycle of ${period} tool calls (ending with '${toolName}') with identical arguments and identical results. Do not repeat the batch — change arguments, synthesize findings, or proceed.]`;
302
+ if (this.config.hardStopEnabled && laps >= this.config.cycleHaltAfter && !this._haltDecision) {
303
+ decision = {
304
+ action: 'halt',
305
+ code: 'identical_cycle_halt',
306
+ message: `Stopped ${toolName}: repeating cycle of ${period} tool calls with identical arguments and results has run ${laps} times. Repeating the batch unchanged is not progress; use the results already provided or change strategy.`,
307
+ toolName,
308
+ count: laps,
309
+ period,
310
+ signature
311
+ };
312
+ this._haltDecision = decision;
313
+ }
314
+ }
315
+ }
316
+ if (!notice && !this._researchNudgeEmitted && this._distinctInspectionCount >= this.config.researchNudgeAfter) {
317
+ this._researchNudgeEmitted = true;
318
+ notice = `[RESEARCH PROGRESS NOTICE: You have performed ${this._distinctInspectionCount} consecutive inspection steps across the workspace. If sufficient context has been gathered, consider proceeding to implementation or updating the plan.]`;
319
+ }
320
+ return { notice, stub, decision };
321
+ }
322
+ detectIdenticalCycle() {
323
+ const history = this._callHistory;
324
+ for (let period = 2; period <= STALL_GUARD_MAX_CYCLE_PERIOD; period++) {
325
+ if (history.length < period * this.config.cycleWarnAfter) {
326
+ continue;
327
+ }
328
+ let laps = 1;
329
+ while (true) {
330
+ const base = history.length - period * (laps + 1);
331
+ if (base < 0)
332
+ break;
333
+ let lapEqual = true;
334
+ for (let i = 0; i < period; i++) {
335
+ const past = history[base + i];
336
+ const curr = history[history.length - period + i];
337
+ if (!past || !curr || past.sig.toolName !== curr.sig.toolName || past.sig.argsHash !== curr.sig.argsHash || past.resultHash !== curr.resultHash) {
338
+ lapEqual = false;
339
+ break;
340
+ }
341
+ }
342
+ if (!lapEqual)
343
+ break;
344
+ laps++;
345
+ }
346
+ if (laps >= this.config.cycleWarnAfter) {
347
+ const tail = history.slice(history.length - period);
348
+ if (tail.every(item => item.repeatable)) {
349
+ continue;
350
+ }
351
+ return { period, laps };
352
+ }
353
+ }
354
+ return null;
355
+ }
356
+ }