@causyne/agent 1.20.13 → 1.20.15

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 (71) hide show
  1. package/package.json +1 -1
  2. package/src/SmartTools.js +33 -0
  3. package/src/SmartTools.ts +37 -0
  4. package/src/acp/client.js +157 -4
  5. package/src/acp/client.ts +152 -5
  6. package/src/agentLoop.js +857 -178
  7. package/src/agentLoop.ts +865 -183
  8. package/src/browser/browserRuntime.js +8 -1
  9. package/src/browser/browserRuntime.ts +8 -1
  10. package/src/browser/causyneBrowser.js +20 -4
  11. package/src/browser/causyneBrowser.ts +20 -4
  12. package/src/browser/urlPolicy.js +35 -0
  13. package/src/browser/urlPolicy.ts +34 -0
  14. package/src/cliMode.js +6 -2
  15. package/src/commands.js +14 -0
  16. package/src/contextManager.d.ts +1 -0
  17. package/src/core/contextCompactor.js +277 -0
  18. package/src/core/contextCompactor.ts +353 -0
  19. package/src/decideTools.js +157 -0
  20. package/src/decideTools.ts +195 -0
  21. package/src/decisionPolicy.js +5 -3
  22. package/src/decisionPolicy.ts +5 -3
  23. package/src/implementationPlan.js +284 -0
  24. package/src/implementationPlan.ts +275 -0
  25. package/src/observer/coldObserver.js +10 -4
  26. package/src/observer/coldObserver.ts +15 -7
  27. package/src/observer/evidence.js +11 -5
  28. package/src/observer/evidence.ts +13 -6
  29. package/src/observer.js +196 -0
  30. package/src/observer.ts +287 -0
  31. package/src/providers/acp.js +362 -109
  32. package/src/providers/acp.ts +359 -111
  33. package/src/providers/antigravity.js +133 -105
  34. package/src/providers/antigravity.ts +130 -109
  35. package/src/providers/base.d.ts +3 -0
  36. package/src/providers/budgetEnforcer.js +23 -2
  37. package/src/providers/budgetEnforcer.ts +20 -5
  38. package/src/provisioning/capabilityProvisioner.js +14 -10
  39. package/src/sandbox.js +4 -2
  40. package/src/saveTools.js +146 -0
  41. package/src/saveTools.ts +169 -0
  42. package/src/session/cachePressure.js +77 -0
  43. package/src/session/cachePressure.ts +122 -0
  44. package/src/smartToolsUse.js +96 -15
  45. package/src/smartToolsUse.ts +133 -15
  46. package/src/taskShape.js +7 -7
  47. package/src/taskShape.ts +7 -7
  48. package/src/timeMachine.js +55 -20
  49. package/src/timeMachine.ts +64 -20
  50. package/src/toolPacks.js +217 -310
  51. package/src/toolPacks.ts +505 -0
  52. package/src/tools/deferredMcpSearch.js +21 -13
  53. package/src/tools/deferredMcpSearch.ts +23 -15
  54. package/src/tools/updatePlan.js +2 -2
  55. package/src/tools/updatePlan.ts +2 -2
  56. package/src/toolsList.js +203 -0
  57. package/src/toolsList.ts +224 -0
  58. package/src/tui/chatRenderer.js +49 -14
  59. package/src/tui/codeStreamRenderer.js +16 -5
  60. package/src/tui/codeStreamRenderer.ts +14 -4
  61. package/src/tui/prompt.js +4 -2
  62. package/src/tui/sessionSidebar.js +10 -2
  63. package/src/tui/sessionSidebar.ts +10 -3
  64. package/src/tui/stageSurface.js +27 -1
  65. package/src/tui/text.d.ts +1 -0
  66. package/src/tui/turnRail.js +274 -0
  67. package/src/tui/turnRail.ts +334 -0
  68. package/src/types/contracts.ts +103 -0
  69. package/src/verificationPlan.js +141 -0
  70. package/src/verificationPlan.ts +166 -0
  71. package/src/toolPacks.d.ts +0 -10
@@ -3,21 +3,36 @@ import { failureClass } from './planCache.js';
3
3
  import { isVerificationCommand, type ToolOutcome } from './observer/evidence.js';
4
4
 
5
5
  export const TIME_MACHINE_MAX_ATTEMPTS = 6;
6
- export const TIME_MACHINE_REPLAN_AT = 3;
6
+ /**
7
+ * After this many failed attempts at the same issue the agent must stop and ask
8
+ * the user before trying again. Attempts beyond it are three more rounds of
9
+ * similar edits and checks, which cost real time and tokens, so they are the
10
+ * user's call and never the agent's.
11
+ */
12
+ export const TIME_MACHINE_CONSENT_AT = 3;
7
13
 
8
14
  export interface RecoveryDecision {
9
- kind: 'continue' | 'replan' | 'stop' | 'human' | 'resolved' | 'none';
15
+ /**
16
+ * `consent` means the attempt budget the agent may spend alone is used up:
17
+ * the caller must ask the user and call `grantExtension` only on approval.
18
+ */
19
+ kind: 'continue' | 'replan' | 'consent' | 'stop' | 'human' | 'resolved' | 'none';
10
20
  attempt: number;
11
21
  maxAttempts: number;
12
22
  failureClass: string;
13
23
  lastGoodTool: string | null;
14
24
  guidance: string;
25
+ /** Opaque identifier of the failing issue, for `grantExtension`. Empty when nothing failed. */
26
+ issueKey: string;
15
27
  }
16
28
 
17
29
  export function recoveryUserMessage(decision: RecoveryDecision, tool: string): string {
18
30
  if (decision.kind === 'human') {
19
31
  return `${tool} needs approval or authentication. Review the last tool error, then provide the needed access or choose another route. No success is claimed.`;
20
32
  }
33
+ if (decision.attempt <= TIME_MACHINE_CONSENT_AT && decision.kind === 'stop') {
34
+ return `${tool} failed ${decision.attempt} times (${decision.failureClass}) and more attempts were not approved. The task stopped without verified success.`;
35
+ }
21
36
  return `${tool} still failed after ${decision.attempt} attempts (${decision.failureClass}). The task stopped without verified success.`;
22
37
  }
23
38
 
@@ -44,9 +59,12 @@ function operationFamily(args: Record<string, unknown>): string {
44
59
  return (words[0] || '').replace(/^.*[\\/]/, '').toLowerCase().slice(0, 32);
45
60
  }
46
61
 
47
- function issueKey(tool: string, args: Record<string, unknown>, classification: string): string {
62
+ function issueKey(tool: string, args: Record<string, unknown>): string {
48
63
  const family = operationFamily(args);
49
- return createHash('sha256').update(`${tool}\0${family}\0${classification}`).digest('hex');
64
+ const target = String(args.path ?? args.file_path ?? args.filePath ?? '');
65
+ // Changing error text/class does not buy another three attempts at the same
66
+ // operation. Targets are hashed and never included in recovery messages.
67
+ return createHash('sha256').update(`${tool}\0${family}\0${target}`).digest('hex');
50
68
  }
51
69
 
52
70
  function lastGoodTool(outcomes: readonly ToolOutcome[]): string | null {
@@ -73,9 +91,22 @@ function nextDiagnostic(classification: string): string {
73
91
  /** Per-task, deterministic recovery budget. Never replays actions or approvals. */
74
92
  export class TimeMachine {
75
93
  private readonly failures = new Map<string, number>();
94
+ private readonly extended = new Set<string>();
76
95
 
77
96
  reset(): void {
78
97
  this.failures.clear();
98
+ this.extended.clear();
99
+ }
100
+
101
+ /** The user approved attempts beyond the consent point for this issue, up to the hard maximum. */
102
+ grantExtension(issueKeyValue: string): void {
103
+ const count = this.failures.get(issueKeyValue) ?? 0;
104
+ if (count >= TIME_MACHINE_CONSENT_AT && count < TIME_MACHINE_MAX_ATTEMPTS) this.extended.add(issueKeyValue);
105
+ }
106
+
107
+ /** Whether the user already approved further attempts for this issue. */
108
+ isExtended(issueKeyValue: string): boolean {
109
+ return this.extended.has(issueKeyValue);
79
110
  }
80
111
 
81
112
  observe(tool: string, args: Record<string, unknown>, result: unknown,
@@ -83,21 +114,21 @@ export class TimeMachine {
83
114
  const value = record(result);
84
115
  const failed = value.success === false || typeof value.error === 'string';
85
116
  const classification = failed ? failureClass(value) || 'failed' : '';
86
- const key = issueKey(tool, args, classification);
117
+ const key = issueKey(tool, args);
87
118
  const good = lastGoodTool(outcomes);
88
119
 
89
120
  if (!failed) {
90
121
  // A successful check only clears its own failure family. A successful
91
122
  // read/write cannot prove that a failing build or test was repaired.
92
- const possibleKeys = ['failed', 'exit_nonzero', 'syntax', 'invalid_args',
93
- 'not_found', 'timeout', 'network', 'rate_limited', 'permission'];
94
- const hadFailure = possibleKeys.some((item) => this.failures.has(issueKey(tool, args, item)));
123
+ const hadFailure = this.failures.has(key);
95
124
  if (hadFailure) {
96
- for (const item of possibleKeys) this.failures.delete(issueKey(tool, args, item));
125
+ this.failures.delete(key);
126
+ this.extended.delete(key);
97
127
  }
98
128
  return {
99
129
  kind: hadFailure ? 'resolved' : 'none', attempt: 0,
100
130
  maxAttempts: TIME_MACHINE_MAX_ATTEMPTS, failureClass: '',
131
+ issueKey: '',
101
132
  lastGoodTool: good,
102
133
  guidance: hadFailure
103
134
  ? isVerificationCommand(String(args.command || args.cmd || ''))
@@ -113,28 +144,41 @@ export class TimeMachine {
113
144
  this.failures.set(key, attempt);
114
145
  const checkpoint = good ? ` Last successful tool: ${good}.` : '';
115
146
 
116
- if (value.failureKind === 'site_refusal' && attempt < TIME_MACHINE_MAX_ATTEMPTS) {
117
- return { kind: 'replan', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS,
118
- failureClass: classification, lastGoodTool: good,
119
- guidance: `The site refused this fetch. Do not repeat the same URL or bypass the site's restriction. Search for another public source or use an already available browser capability.${checkpoint}` };
120
- }
121
147
 
122
148
  if (needsHuman) {
123
- return { kind: 'human', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS,
149
+ return { kind: 'human', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS, issueKey: key,
124
150
  failureClass: classification, lastGoodTool: good,
125
151
  guidance: `Permission or authentication is required. Stop retrying and explain the exact user action needed.${checkpoint}` };
126
152
  }
127
153
  if (attempt >= TIME_MACHINE_MAX_ATTEMPTS) {
128
- return { kind: 'stop', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS,
154
+ return { kind: 'stop', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS, issueKey: key,
129
155
  failureClass: classification, lastGoodTool: good,
130
156
  guidance: `The same failure class persisted through ${TIME_MACHINE_MAX_ATTEMPTS} attempts. Stop this task. Give a brief evidence-based report, state what remains unresolved, and ask for input only if needed. Put a detailed report in a Markdown file only if the user requests one.${checkpoint}` };
131
157
  }
132
- if (attempt >= TIME_MACHINE_REPLAN_AT) {
133
- return { kind: 'replan', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS,
158
+ // The agent may spend three attempts alone. The fourth through sixth belong
159
+ // to the user: without an explicit grant the budget ends here.
160
+ if (attempt >= TIME_MACHINE_CONSENT_AT && !this.extended.has(key)) {
161
+ if (attempt === TIME_MACHINE_CONSENT_AT) {
162
+ return { kind: 'consent', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS, issueKey: key,
163
+ failureClass: classification, lastGoodTool: good,
164
+ guidance: `Three attempts failed. Ask the user before trying again. If they allow it, change approach completely: review the last successful step and the failing evidence. ${nextDiagnostic(classification)}${checkpoint}` };
165
+ }
166
+ return { kind: 'stop', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS, issueKey: key,
167
+ failureClass: classification, lastGoodTool: good,
168
+ guidance: `Further attempts were not approved. Stop this task. Give a brief evidence-based report and state what remains unresolved.${checkpoint}` };
169
+ }
170
+ if (value.failureKind === 'site_refusal' && attempt < TIME_MACHINE_MAX_ATTEMPTS) {
171
+ return { kind: 'replan', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS, issueKey: key,
172
+ failureClass: classification, lastGoodTool: good,
173
+ guidance: `The site refused this fetch. Do not repeat the same URL or bypass the site's restriction. Search for another public source or use an already available browser capability.${checkpoint}` };
174
+ }
175
+
176
+ if (attempt >= TIME_MACHINE_CONSENT_AT) {
177
+ return { kind: 'replan', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS, issueKey: key,
134
178
  failureClass: classification, lastGoodTool: good,
135
- guidance: `Review the last successful step and the failing evidence. ${nextDiagnostic(classification)} Change approach before another attempt; do not repeat the same command or claim a fix without a passing check.${checkpoint}` };
179
+ guidance: `Review the last successful step and the failing evidence. ${nextDiagnostic(classification)} Change approach before another attempt; do not repeat the same command or claim a fix without a passing check. ${TIME_MACHINE_MAX_ATTEMPTS - attempt} attempt(s) remain.${checkpoint}` };
136
180
  }
137
- return { kind: 'continue', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS,
181
+ return { kind: 'continue', attempt, maxAttempts: TIME_MACHINE_MAX_ATTEMPTS, issueKey: key,
138
182
  failureClass: classification, lastGoodTool: good,
139
183
  guidance: `Inspect this failure before retrying. Attempt ${attempt}/${TIME_MACHINE_MAX_ATTEMPTS}.${checkpoint}` };
140
184
  }