@causyne/agent 1.20.120 → 1.20.122
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.
- package/README.md +3 -3
- package/package.json +1 -1
- package/src/acp/agentCatalog.js +81 -26
- package/src/acp/agentCatalog.ts +73 -29
- package/src/acp/client.js +210 -53
- package/src/acp/client.ts +202 -52
- package/src/acp/peerProfiles.js +55 -22
- package/src/acp/peerProfiles.ts +51 -22
- package/src/acp/protocol.js +37 -8
- package/src/acp/protocol.ts +30 -8
- package/src/acp/server.js +123 -25
- package/src/acp/server.ts +124 -24
- package/src/agent/errorClassifier.js +117 -0
- package/src/agent/errorClassifier.ts +165 -0
- package/src/agent/promptIntent.js +200 -0
- package/src/agent/promptIntent.ts +203 -0
- package/src/agent/toolArgValidator.js +97 -0
- package/src/agent/toolArgValidator.ts +112 -0
- package/src/agentLoop.js +33 -344
- package/src/agentLoop.ts +51 -352
- package/src/approvalGrants.js +19 -7
- package/src/approvalGrants.ts +20 -7
- package/src/approvalPolicy.js +48 -15
- package/src/approvalPolicy.ts +46 -15
- package/src/autoUpdate.js +116 -10
- package/src/autoUpdate.ts +149 -10
- package/src/browser/causyneBrowser.js +3 -2
- package/src/browser/causyneBrowser.ts +3 -2
- package/src/browser/searchProviders.js +1 -1
- package/src/browser/searchProviders.ts +1 -1
- package/src/cliMode.js +16 -2
- package/src/commands.js +10 -9
- package/src/config.js +288 -272
- package/src/config.ts +342 -0
- package/src/configVault.js +271 -0
- package/src/configVault.ts +273 -0
- package/src/continuum/compactor.js +2 -1
- package/src/continuum/compactor.ts +2 -1
- package/src/continuum/sanitizer.js +10 -3
- package/src/continuum/sanitizer.ts +9 -3
- package/src/continuum/ui/continuumPickerView.js +3 -2
- package/src/continuum/ui/continuumPickerView.ts +3 -2
- package/src/core/contextCompactor.js +70 -11
- package/src/core/contextCompactor.ts +83 -11
- package/src/cortex/vectorDb.js +26 -15
- package/src/cortex/vectorDb.ts +26 -16
- package/src/creative/liveBridge.js +7 -5
- package/src/creative/liveBridge.ts +7 -5
- package/src/creative/studioScripts.js +16 -5
- package/src/creative/studioScripts.ts +14 -5
- package/src/decisionPolicy.js +16 -4
- package/src/decisionPolicy.ts +14 -4
- package/src/documentEngine/antiSlopAuditor.js +10 -5
- package/src/documentEngine/antiSlopAuditor.ts +11 -5
- package/src/documentEngine/documentForge.js +47 -15
- package/src/documentEngine/documentForge.ts +43 -14
- package/src/dynamicCatalogue.js +42 -73
- package/src/dynamicCatalogue.ts +74 -60
- package/src/engines/design.js +2 -1
- package/src/engines/design.ts +2 -1
- package/src/engines/film.js +4 -4
- package/src/engines/film.ts +2 -2
- package/src/engines/light.js +2 -1
- package/src/engines/light.ts +2 -1
- package/src/engines/registry.js +1 -1
- package/src/engines/registry.ts +1 -1
- package/src/engines/reverseHarness.js +4 -3
- package/src/engines/reverseHarness.ts +4 -3
- package/src/entrypoint.js +77 -4
- package/src/headlessCli.js +305 -0
- package/src/headlessCli.ts +381 -0
- package/src/historyPruner.js +3 -2
- package/src/historyPruner.ts +3 -2
- package/src/hooks.js +3 -0
- package/src/hooks.ts +3 -0
- package/src/implementationPlan.js +21 -11
- package/src/implementationPlan.ts +21 -11
- package/src/infra/atomicWriter.js +4 -1
- package/src/infra/atomicWriter.ts +4 -1
- package/src/launchIntent.js +1 -1
- package/src/launchIntent.ts +1 -1
- package/src/mcp/mcpClient.js +38 -11
- package/src/mcp/mcpClient.ts +44 -11
- package/src/mcp/mcpRegistry.js +2 -5
- package/src/mcp/mcpRegistry.ts +2 -5
- package/src/mcp/mcpSecurity.js +189 -0
- package/src/mcp/mcpSecurity.ts +190 -0
- package/src/memory/scopedFacts.js +1 -1
- package/src/memory/scopedFacts.ts +1 -1
- package/src/memory/symbolFolder.js +1 -1
- package/src/memory/symbolFolder.ts +1 -1
- package/src/modelRouter.js +1 -1
- package/src/modelRouter.ts +1 -1
- package/src/observer/evidence.js +10 -4
- package/src/observer/evidence.ts +10 -4
- package/src/osKeychain.js +263 -0
- package/src/osKeychain.ts +287 -0
- package/src/perception.js +3 -2
- package/src/perception.ts +3 -2
- package/src/planCache.js +2 -2
- package/src/planCache.ts +2 -2
- package/src/pluginsFactory/pluginsFactory.js +19 -11
- package/src/pluginsFactory/pluginsFactory.ts +18 -11
- package/src/promptMentions.js +2 -2
- package/src/promptMentions.ts +2 -2
- package/src/providers/acp.js +1 -1
- package/src/providers/acp.ts +1 -1
- package/src/providers/anthropic.js +328 -385
- package/src/providers/anthropic.ts +386 -0
- package/src/providers/antigravity.js +10 -4
- package/src/providers/antigravity.ts +10 -4
- package/src/providers/base.js +221 -319
- package/src/providers/base.ts +340 -0
- package/src/providers/byokHttp.js +397 -0
- package/src/providers/byokHttp.ts +497 -0
- package/src/providers/byokStream.js +748 -0
- package/src/providers/byokStream.ts +863 -0
- package/src/providers/dashscope-byok.js +98 -81
- package/src/providers/dashscope-byok.ts +114 -0
- package/src/providers/deepseek.js +74 -64
- package/src/providers/deepseek.ts +99 -0
- package/src/providers/factory.js +35 -28
- package/src/providers/factory.ts +40 -0
- package/src/providers/gemini.js +229 -253
- package/src/providers/gemini.ts +261 -0
- package/src/providers/loopGuard.js +9 -5
- package/src/providers/loopGuard.ts +10 -5
- package/src/providers/moonshot.js +71 -64
- package/src/providers/moonshot.ts +93 -0
- package/src/providers/muse.js +82 -152
- package/src/providers/muse.ts +122 -0
- package/src/providers/ollama.js +116 -93
- package/src/providers/ollama.ts +135 -0
- package/src/providers/openai-compatible.js +114 -65
- package/src/providers/openai-compatible.ts +139 -0
- package/src/providers/openai.js +22 -15
- package/src/providers/openai.ts +30 -0
- package/src/providers/openrouter.js +194 -98
- package/src/providers/openrouter.ts +202 -97
- package/src/providers/pi.js +6 -0
- package/src/providers/pi.ts +6 -0
- package/src/providers/qwen-byok.js +97 -71
- package/src/providers/qwen-byok.ts +113 -0
- package/src/providers/toolContracts.js +40 -14
- package/src/providers/toolContracts.ts +35 -14
- package/src/providers/wireNormalizer.js +78 -11
- package/src/providers/wireNormalizer.ts +68 -12
- package/src/providers/xai.js +74 -64
- package/src/providers/xai.ts +97 -0
- package/src/reliability/assetEvidence.js +4 -2
- package/src/reliability/assetEvidence.ts +4 -2
- package/src/sandbox.js +486 -530
- package/src/sandbox.ts +552 -0
- package/src/sandboxPolicy.js +1287 -0
- package/src/sandboxPolicy.ts +1381 -0
- package/src/sessionJournal.js +55 -3
- package/src/sessionJournal.ts +58 -3
- package/src/skills/skillResourceSafety.js +19 -8
- package/src/skills/skillResourceSafety.ts +16 -7
- package/src/skillsManager.js +3 -3
- package/src/skillsManager.ts +3 -3
- package/src/smartToolsUse.js +28 -4
- package/src/smartToolsUse.ts +29 -7
- package/src/stallGuard.js +45 -22
- package/src/stallGuard.ts +46 -22
- package/src/subagentJournal.js +6 -4
- package/src/subagentJournal.ts +6 -4
- package/src/textDiff.js +6 -1
- package/src/textDiff.ts +6 -1
- package/src/timeMachine.js +7 -1
- package/src/timeMachine.ts +7 -1
- package/src/tokenTracker.js +43 -28
- package/src/tokenTracker.ts +44 -30
- package/src/toolPacks.js +3 -1
- package/src/toolPacks.ts +3 -1
- package/src/tools/archive.js +245 -35
- package/src/tools/archive.ts +244 -35
- package/src/tools/computerUse.js +7 -3
- package/src/tools/computerUse.ts +7 -3
- package/src/tools/creativeApps.js +13 -8
- package/src/tools/creativeApps.ts +22 -9
- package/src/tools/deferredMcpSearch.js +26 -14
- package/src/tools/deferredMcpSearch.ts +26 -14
- package/src/tools/desktopOpen.js +69 -10
- package/src/tools/desktopOpen.ts +70 -11
- package/src/tools/documentForge.js +167 -82
- package/src/tools/documentForge.ts +169 -84
- package/src/tools/fsEdit.js +101 -30
- package/src/tools/fsEdit.ts +105 -32
- package/src/tools/fsFind.js +8 -5
- package/src/tools/fsFind.ts +7 -4
- package/src/tools/fsRead.js +119 -13
- package/src/tools/fsRead.ts +124 -13
- package/src/tools/fsWrite.js +17 -5
- package/src/tools/fsWrite.ts +17 -7
- package/src/tools/generateMediaViaApi.js +82 -19
- package/src/tools/generateMediaViaApi.ts +80 -19
- package/src/tools/memoryRecall.js +62 -11
- package/src/tools/memoryRecall.ts +70 -13
- package/src/tools/memoryWrite.js +27 -14
- package/src/tools/memoryWrite.ts +30 -14
- package/src/tools/multiEdit.js +100 -30
- package/src/tools/multiEdit.ts +104 -32
- package/src/tools/nativeEngine.js +33 -18
- package/src/tools/nativeEngine.ts +31 -18
- package/src/tools/openSourceToolProvisioner.js +49 -14
- package/src/tools/openSourceToolProvisioner.ts +54 -15
- package/src/tools/osSandbox.js +513 -0
- package/src/tools/osSandbox.ts +588 -0
- package/src/tools/payloadSanitizer.js +40 -8
- package/src/tools/payloadSanitizer.ts +39 -8
- package/src/tools/peerAgent.js +96 -34
- package/src/tools/peerAgent.ts +114 -37
- package/src/tools/persistentShell.js +8 -3
- package/src/tools/pluginsFactory.js +38 -6
- package/src/tools/pluginsFactory.ts +42 -6
- package/src/tools/readNativeSkill.js +10 -8
- package/src/tools/readNativeSkill.ts +10 -8
- package/src/tools/requestDecision.js +10 -3
- package/src/tools/requestDecision.ts +10 -3
- package/src/tools/resolveAutomator.js +91 -17
- package/src/tools/resolveAutomator.ts +102 -17
- package/src/tools/reverseEngineering.js +168 -41
- package/src/tools/reverseEngineering.ts +161 -41
- package/src/tools/subprocessEnv.js +197 -0
- package/src/tools/subprocessEnv.ts +212 -0
- package/src/tools/sysRun.js +108 -35
- package/src/tools/sysRun.ts +119 -31
- package/src/tools/toolDispatch.js +24 -1
- package/src/tools/toolDispatch.ts +21 -1
- package/src/tools/toolOutputStore.js +62 -23
- package/src/tools/toolOutputStore.ts +56 -23
- package/src/tools/toolScanner.js +13 -10
- package/src/tools/toolScanner.ts +13 -10
- package/src/tools/toolSelfRepair.js +94 -28
- package/src/tools/toolSelfRepair.ts +96 -28
- package/src/tools/uiForge.js +71 -6
- package/src/tools/uiForge.ts +74 -6
- package/src/tools/viewImage.js +62 -17
- package/src/tools/viewImage.ts +57 -12
- package/src/tools/visionInspector.js +15 -3
- package/src/tools/visionInspector.ts +15 -3
- package/src/tools/walkerSafety.js +2 -0
- package/src/tools/walkerSafety.ts +1 -0
- package/src/tools/webFetch.js +105 -10
- package/src/tools/webFetch.ts +105 -11
- package/src/tui/banner.js +3 -2
- package/src/tui/banner.ts +3 -2
- package/src/tui/consoleGuard.js +12 -4
- package/src/tui/consoleGuard.ts +9 -4
- package/src/tui/messageInteraction.js +2 -3
- package/src/tui/messageInteraction.ts +2 -3
- package/src/tui/modelPicker.js +1 -0
- package/src/tui/modelPicker.ts +1 -0
- package/src/tui/overlay.js +3 -1
- package/src/tui/overlay.ts +3 -1
- package/src/tui/sessionPicker.js +3 -2
- package/src/tui/sessionPicker.ts +3 -2
- package/src/tui/sessionPickerView.js +7 -1
- package/src/tui/sessionPickerView.ts +6 -1
- package/src/tui/toolOutputNormalizer.js +2 -3
- package/src/tui/toolOutputNormalizer.ts +4 -3
- package/src/tui/viewport.js +4 -4
- package/src/tui/viewport.ts +5 -5
- package/src/tui/workspaceIdentity.js +2 -1
- package/src/tui/workspaceIdentity.ts +2 -1
- package/src/uncertainty.js +8 -3
- package/src/uncertainty.ts +8 -3
- package/src/utils/paths.js +8 -5
- package/src/utils/paths.ts +9 -7
- package/src/vfeTracker.js +9 -3
- package/src/vfeTracker.ts +9 -3
- package/src/config.d.ts +0 -2
- package/src/providers/base.d.ts +0 -94
- package/src/providers/factory.d.ts +0 -1
- package/src/sandbox.d.ts +0 -1
package/src/acp/server.ts
CHANGED
|
@@ -6,6 +6,8 @@ import type { AgentLoopEvent } from '../agentLoop.js';
|
|
|
6
6
|
import { createApprovalScope } from '../approvalGrants.js';
|
|
7
7
|
import type { ApprovalScope } from '../approvalGrants.js';
|
|
8
8
|
import { createSession, getSession, saveSessionMessages, forkSession } from '../sessionJournal.js';
|
|
9
|
+
import { isBlockedWorkspaceRoot } from '../sandboxPolicy.js';
|
|
10
|
+
import { safeDisplay } from '../tui/text.js';
|
|
9
11
|
import {
|
|
10
12
|
ACP_ERROR_CODES,
|
|
11
13
|
ACP_METHODS,
|
|
@@ -75,6 +77,7 @@ export interface AcpServerOptions {
|
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
interface PendingClientRequest {
|
|
80
|
+
sessionId: string;
|
|
78
81
|
resolve: (value: unknown) => void;
|
|
79
82
|
reject: (error: Error) => void;
|
|
80
83
|
timer: NodeJS.Timeout;
|
|
@@ -96,9 +99,9 @@ interface AcpSession {
|
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
const MODE_TO_AUTONOMY: Record<string, 'A' | 'B' | 'C'> = {
|
|
99
|
-
autonomous: '
|
|
102
|
+
autonomous: 'C',
|
|
100
103
|
standard: 'B',
|
|
101
|
-
supervised: '
|
|
104
|
+
supervised: 'A'
|
|
102
105
|
};
|
|
103
106
|
|
|
104
107
|
const MODES: SessionModeState = {
|
|
@@ -112,11 +115,15 @@ const MODES: SessionModeState = {
|
|
|
112
115
|
|
|
113
116
|
const MAX_PROMPT_CHARS = 200_000;
|
|
114
117
|
const MAX_RAW_CHARS = 4000;
|
|
118
|
+
const MAX_JSONRPC_BUFFER_BYTES = 8 * 1024 * 1024;
|
|
119
|
+
const MAX_SESSIONS = 128;
|
|
115
120
|
|
|
116
121
|
function textFromPrompt(prompt: ContentBlock[]): string | null {
|
|
117
122
|
const parts: string[] = [];
|
|
118
123
|
for (const block of prompt) {
|
|
119
|
-
if (block.type !== 'text')
|
|
124
|
+
if (!block || typeof block !== 'object' || block.type !== 'text' || typeof block.text !== 'string') {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
120
127
|
parts.push(block.text);
|
|
121
128
|
}
|
|
122
129
|
return parts.join('\n').slice(0, MAX_PROMPT_CHARS);
|
|
@@ -159,8 +166,8 @@ function bounded(value: unknown, limit = MAX_RAW_CHARS): string {
|
|
|
159
166
|
|
|
160
167
|
function toolTitle(name: string, args: Record<string, unknown>): string {
|
|
161
168
|
const detail = args.path || args.file_path || args.command || args.cmd || args.query || args.url || args.action;
|
|
162
|
-
const
|
|
163
|
-
const title = `${name}${
|
|
169
|
+
const rawSuffix = typeof detail === 'string' && detail.trim() ? ` ${detail.trim()}` : '';
|
|
170
|
+
const title = safeDisplay(`${name}${rawSuffix}`).replace(/\s+/g, ' ').trim();
|
|
164
171
|
return title.length > 200 ? `${title.slice(0, 200)}…` : title;
|
|
165
172
|
}
|
|
166
173
|
|
|
@@ -187,13 +194,25 @@ export class AcpServer {
|
|
|
187
194
|
this.loopFactory = options.loopFactory ?? null;
|
|
188
195
|
this.agentName = options.agentName ?? 'Causyne Agent';
|
|
189
196
|
this.agentVersion = options.agentVersion ?? '0.0.0';
|
|
190
|
-
|
|
197
|
+
const rawTimeout = options.permissionTimeoutMs;
|
|
198
|
+
this.permissionTimeoutMs = typeof rawTimeout === 'number' && Number.isFinite(rawTimeout)
|
|
199
|
+
? Math.max(1_000, Math.min(3_600_000, Math.round(rawTimeout)))
|
|
200
|
+
: 10 * 60 * 1000;
|
|
191
201
|
}
|
|
192
202
|
|
|
193
203
|
start(): void {
|
|
194
204
|
if (!this.input || this.dataListener) return;
|
|
195
205
|
this.dataListener = (chunk: Buffer | string) => {
|
|
196
206
|
this.buffer += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
|
|
207
|
+
if (this.buffer.length > MAX_JSONRPC_BUFFER_BYTES) {
|
|
208
|
+
this.buffer = '';
|
|
209
|
+
this.write({
|
|
210
|
+
jsonrpc: '2.0',
|
|
211
|
+
id: null,
|
|
212
|
+
error: { code: ACP_ERROR_CODES.INVALID_REQUEST, message: 'Request frame exceeds maximum size' }
|
|
213
|
+
});
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
197
216
|
void this.drainBuffer();
|
|
198
217
|
};
|
|
199
218
|
this.endListener = () => { void this.stop(); };
|
|
@@ -209,13 +228,16 @@ export class AcpServer {
|
|
|
209
228
|
if (this.input && this.dataListener) this.input.off('data', this.dataListener);
|
|
210
229
|
if (this.input && this.endListener) this.input.off('end', this.endListener);
|
|
211
230
|
if (this.input && this.errorListener) this.input.off('error', this.errorListener);
|
|
231
|
+
this.buffer = '';
|
|
212
232
|
for (const session of this.sessions.values()) {
|
|
233
|
+
session.cancelled = true;
|
|
213
234
|
try {
|
|
214
235
|
session.loop?.abort();
|
|
215
236
|
} catch {
|
|
216
237
|
// Shutdown never blocks on a misbehaving tool.
|
|
217
238
|
}
|
|
218
239
|
}
|
|
240
|
+
this.sessions.clear();
|
|
219
241
|
for (const [id, pending] of this.pending) {
|
|
220
242
|
clearTimeout(pending.timer);
|
|
221
243
|
pending.reject(new Error('server stopped'));
|
|
@@ -223,20 +245,33 @@ export class AcpServer {
|
|
|
223
245
|
}
|
|
224
246
|
}
|
|
225
247
|
|
|
226
|
-
private
|
|
248
|
+
private drainBuffer(): void {
|
|
249
|
+
const lines: string[] = [];
|
|
227
250
|
let index = this.buffer.indexOf('\n');
|
|
228
251
|
while (index >= 0) {
|
|
229
252
|
const line = this.buffer.slice(0, index).trim();
|
|
230
253
|
this.buffer = this.buffer.slice(index + 1);
|
|
231
|
-
if (line)
|
|
254
|
+
if (line) lines.push(line);
|
|
232
255
|
index = this.buffer.indexOf('\n');
|
|
233
256
|
}
|
|
257
|
+
for (const line of lines) {
|
|
258
|
+
void this.handleLine(line).catch(() => {});
|
|
259
|
+
}
|
|
234
260
|
}
|
|
235
261
|
|
|
236
262
|
async handleLine(line: string): Promise<void> {
|
|
263
|
+
const cleaned = line.replace(/^\uFEFF/, '').replace(/\r$/, '');
|
|
264
|
+
if (cleaned.length > MAX_JSONRPC_BUFFER_BYTES) {
|
|
265
|
+
this.write({
|
|
266
|
+
jsonrpc: '2.0',
|
|
267
|
+
id: null,
|
|
268
|
+
error: { code: ACP_ERROR_CODES.INVALID_REQUEST, message: 'Request frame exceeds maximum size' }
|
|
269
|
+
});
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
237
272
|
let parsed: unknown;
|
|
238
273
|
try {
|
|
239
|
-
parsed = JSON.parse(
|
|
274
|
+
parsed = JSON.parse(cleaned);
|
|
240
275
|
} catch {
|
|
241
276
|
this.write({ jsonrpc: '2.0', id: null, error: { code: ACP_ERROR_CODES.PARSE_ERROR, message: 'Parse error' } });
|
|
242
277
|
return;
|
|
@@ -256,12 +291,19 @@ export class AcpServer {
|
|
|
256
291
|
this.write({ jsonrpc: '2.0', id: null, error: { code: ACP_ERROR_CODES.INVALID_REQUEST, message: 'Invalid request' } });
|
|
257
292
|
}
|
|
258
293
|
|
|
294
|
+
private isPlainParams(value: unknown): boolean {
|
|
295
|
+
return value === undefined || (value !== null && typeof value === 'object' && !Array.isArray(value));
|
|
296
|
+
}
|
|
297
|
+
|
|
259
298
|
private async handleNotification(notification: JsonRpcNotification): Promise<void> {
|
|
299
|
+
if (!this.isPlainParams(notification.params)) return;
|
|
260
300
|
if (notification.method === ACP_METHODS.CANCEL) {
|
|
261
301
|
const params = notification.params as CancelNotification | undefined;
|
|
262
|
-
const
|
|
302
|
+
const sid = params && typeof params.sessionId === 'string' ? params.sessionId.trim() : '';
|
|
303
|
+
const session = sid ? this.sessions.get(sid) : undefined;
|
|
263
304
|
if (session) {
|
|
264
305
|
session.cancelled = true;
|
|
306
|
+
this.rejectPendingForSession(session.id, 'session cancelled');
|
|
265
307
|
try {
|
|
266
308
|
session.loop?.abort();
|
|
267
309
|
} catch {
|
|
@@ -273,6 +315,9 @@ export class AcpServer {
|
|
|
273
315
|
|
|
274
316
|
private async handleRequest(request: JsonRpcRequest): Promise<void> {
|
|
275
317
|
try {
|
|
318
|
+
if (!this.isPlainParams(request.params)) {
|
|
319
|
+
throw this.paramsError('params must be a JSON object');
|
|
320
|
+
}
|
|
276
321
|
switch (request.method) {
|
|
277
322
|
case ACP_METHODS.INITIALIZE:
|
|
278
323
|
this.writeResult(request.id, this.initialize(request.params as InitializeRequest | undefined));
|
|
@@ -308,8 +353,13 @@ export class AcpServer {
|
|
|
308
353
|
}
|
|
309
354
|
|
|
310
355
|
private initialize(params: InitializeRequest | undefined): InitializeResult {
|
|
311
|
-
if (
|
|
312
|
-
|
|
356
|
+
if (
|
|
357
|
+
!params ||
|
|
358
|
+
typeof params.protocolVersion !== 'number' ||
|
|
359
|
+
!Number.isInteger(params.protocolVersion) ||
|
|
360
|
+
params.protocolVersion < 1
|
|
361
|
+
) {
|
|
362
|
+
throw this.paramsError('initialize requires a valid positive integer protocolVersion');
|
|
313
363
|
}
|
|
314
364
|
const capabilities: AgentCapabilities = {
|
|
315
365
|
loadSession: true,
|
|
@@ -349,13 +399,17 @@ export class AcpServer {
|
|
|
349
399
|
|
|
350
400
|
private resolveWorkspace(cwd: unknown): string {
|
|
351
401
|
if (typeof cwd !== 'string' || !cwd.trim()) throw this.paramsError('cwd is required');
|
|
402
|
+
let resolved: string;
|
|
352
403
|
try {
|
|
353
|
-
|
|
404
|
+
resolved = fs.realpathSync(path.resolve(cwd));
|
|
354
405
|
if (!fs.statSync(resolved).isDirectory()) throw new Error('not a directory');
|
|
355
|
-
return resolved;
|
|
356
406
|
} catch {
|
|
357
407
|
throw this.paramsError(`cwd is not an accessible directory: ${cwd}`);
|
|
358
408
|
}
|
|
409
|
+
if (isBlockedWorkspaceRoot(resolved)) {
|
|
410
|
+
throw this.paramsError(`cwd points to a blocked system directory: ${cwd}`);
|
|
411
|
+
}
|
|
412
|
+
return resolved;
|
|
359
413
|
}
|
|
360
414
|
|
|
361
415
|
private bindWorkspace(cwd: string): void {
|
|
@@ -368,10 +422,13 @@ export class AcpServer {
|
|
|
368
422
|
}
|
|
369
423
|
|
|
370
424
|
private addSession(id: string, cwd: string, title: string, modeId: string): AcpSession {
|
|
425
|
+
if (!this.sessions.has(id) && this.sessions.size >= MAX_SESSIONS) {
|
|
426
|
+
throw this.paramsError(`Maximum concurrent sessions (${MAX_SESSIONS}) exceeded`);
|
|
427
|
+
}
|
|
371
428
|
const session: AcpSession = {
|
|
372
429
|
id,
|
|
373
430
|
memorySessionId: id,
|
|
374
|
-
title,
|
|
431
|
+
title: safeDisplay(title).replace(/\s+/g, ' ').trim() || 'Untitled',
|
|
375
432
|
cwd,
|
|
376
433
|
modeId,
|
|
377
434
|
loop: null,
|
|
@@ -428,6 +485,9 @@ export class AcpServer {
|
|
|
428
485
|
}
|
|
429
486
|
|
|
430
487
|
private async runTurn(session: AcpSession, text: string): Promise<StopReason> {
|
|
488
|
+
if (this.closed || !this.sessions.has(session.id)) {
|
|
489
|
+
return 'cancelled';
|
|
490
|
+
}
|
|
431
491
|
session.updatedAt = Date.now();
|
|
432
492
|
session.cancelled = false;
|
|
433
493
|
session.busy = true;
|
|
@@ -470,8 +530,10 @@ export class AcpServer {
|
|
|
470
530
|
} finally {
|
|
471
531
|
session.loop = null;
|
|
472
532
|
session.busy = false;
|
|
473
|
-
|
|
474
|
-
|
|
533
|
+
if (this.sessions.has(session.id)) {
|
|
534
|
+
const record = getSession(session.memorySessionId);
|
|
535
|
+
if (record) session.title = safeDisplay(record.title).replace(/\s+/g, ' ').trim() || session.title;
|
|
536
|
+
}
|
|
475
537
|
if (session.cancelled) stopReason = 'cancelled';
|
|
476
538
|
}
|
|
477
539
|
return stopReason;
|
|
@@ -504,8 +566,12 @@ export class AcpServer {
|
|
|
504
566
|
toolCall,
|
|
505
567
|
options: permissionOptions
|
|
506
568
|
}).then((response) => {
|
|
507
|
-
if (
|
|
508
|
-
const
|
|
569
|
+
if (session.cancelled || !this.sessions.has(session.id)) return null;
|
|
570
|
+
const outcome = response && typeof response.outcome === 'object' && response.outcome !== null
|
|
571
|
+
? response.outcome
|
|
572
|
+
: null;
|
|
573
|
+
if (!outcome || outcome.outcome !== 'selected' || typeof outcome.optionId !== 'string') return null;
|
|
574
|
+
const optionId = outcome.optionId;
|
|
509
575
|
const label = labels.get(optionId);
|
|
510
576
|
if (!label) return null;
|
|
511
577
|
return { optionId, label };
|
|
@@ -628,8 +694,18 @@ export class AcpServer {
|
|
|
628
694
|
toolCall,
|
|
629
695
|
options
|
|
630
696
|
}).then((response) => {
|
|
631
|
-
|
|
632
|
-
|
|
697
|
+
if (session.cancelled || !this.sessions.has(session.id)) {
|
|
698
|
+
session.loop?.handleApprovalResponse(toolCallId, false);
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
const outcome = response && typeof response.outcome === 'object' && response.outcome !== null
|
|
702
|
+
? response.outcome
|
|
703
|
+
: null;
|
|
704
|
+
const selected = outcome && outcome.outcome === 'selected' && typeof outcome.optionId === 'string'
|
|
705
|
+
? outcome.optionId
|
|
706
|
+
: '';
|
|
707
|
+
const canonicalTool = tool.replace(/\./g, '_');
|
|
708
|
+
if (selected === 'allow_always') session.approvalScope.grantAlways(canonicalTool);
|
|
633
709
|
const approved = selected === 'allow_once' || selected === 'allow_always';
|
|
634
710
|
session.loop?.handleApprovalResponse(toolCallId, approved);
|
|
635
711
|
}).catch(() => {
|
|
@@ -671,8 +747,17 @@ export class AcpServer {
|
|
|
671
747
|
return {};
|
|
672
748
|
}
|
|
673
749
|
|
|
750
|
+
private canonicalizePath(targetPath: string): string {
|
|
751
|
+
const resolved = path.resolve(targetPath);
|
|
752
|
+
try {
|
|
753
|
+
return fs.realpathSync(resolved);
|
|
754
|
+
} catch {
|
|
755
|
+
return resolved;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
674
759
|
private listSessions(params: ListSessionsRequest | undefined): ListSessionsResponse {
|
|
675
|
-
const filter = params && typeof params.cwd === 'string' ?
|
|
760
|
+
const filter = params && typeof params.cwd === 'string' ? this.canonicalizePath(params.cwd) : null;
|
|
676
761
|
const sessions: SessionInfo[] = [];
|
|
677
762
|
for (const session of this.sessions.values()) {
|
|
678
763
|
if (filter && session.cwd !== filter) continue;
|
|
@@ -690,6 +775,7 @@ export class AcpServer {
|
|
|
690
775
|
private closeSession(params: CloseSessionRequest | undefined): Record<string, never> {
|
|
691
776
|
const session = this.requireSession(params?.sessionId);
|
|
692
777
|
session.cancelled = true;
|
|
778
|
+
this.rejectPendingForSession(session.id, 'session closed');
|
|
693
779
|
try {
|
|
694
780
|
session.loop?.abort();
|
|
695
781
|
} catch {
|
|
@@ -704,6 +790,9 @@ export class AcpServer {
|
|
|
704
790
|
if (!params || typeof params.sessionId !== 'string' || !params.sessionId) {
|
|
705
791
|
throw this.paramsError('session/load requires sessionId');
|
|
706
792
|
}
|
|
793
|
+
if (Array.isArray(params.mcpServers) && params.mcpServers.length > 0) {
|
|
794
|
+
throw this.paramsError('MCP servers are not supported by this agent');
|
|
795
|
+
}
|
|
707
796
|
const live = this.sessions.get(params.sessionId);
|
|
708
797
|
if (live) {
|
|
709
798
|
this.bindWorkspace(live.cwd);
|
|
@@ -714,7 +803,7 @@ export class AcpServer {
|
|
|
714
803
|
if (!record) throw this.notFound(`Unknown session: ${params.sessionId}`);
|
|
715
804
|
const requested = params.cwd ?? record.workspace;
|
|
716
805
|
const cwd = this.resolveWorkspace(requested);
|
|
717
|
-
if (record.workspace &&
|
|
806
|
+
if (record.workspace && this.canonicalizePath(record.workspace) !== cwd) {
|
|
718
807
|
throw this.paramsError('session workspace does not match cwd');
|
|
719
808
|
}
|
|
720
809
|
this.bindWorkspace(cwd);
|
|
@@ -739,7 +828,7 @@ export class AcpServer {
|
|
|
739
828
|
const requested = params.cwd ?? source?.cwd ?? sourceRecord?.workspace;
|
|
740
829
|
const cwd = this.resolveWorkspace(requested);
|
|
741
830
|
if (source && source.cwd !== cwd) throw this.paramsError('fork must stay in the source workspace');
|
|
742
|
-
if (sourceRecord?.workspace &&
|
|
831
|
+
if (sourceRecord?.workspace && this.canonicalizePath(sourceRecord.workspace) !== cwd) {
|
|
743
832
|
throw this.paramsError('fork must stay in the source workspace');
|
|
744
833
|
}
|
|
745
834
|
this.bindWorkspace(cwd);
|
|
@@ -769,6 +858,15 @@ export class AcpServer {
|
|
|
769
858
|
return error;
|
|
770
859
|
}
|
|
771
860
|
|
|
861
|
+
private rejectPendingForSession(sessionId: string, reason: string): void {
|
|
862
|
+
for (const [id, pending] of this.pending) {
|
|
863
|
+
if (pending.sessionId !== sessionId) continue;
|
|
864
|
+
clearTimeout(pending.timer);
|
|
865
|
+
this.pending.delete(id);
|
|
866
|
+
pending.reject(new Error(reason));
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
|
|
772
870
|
private sendClientRequest<T>(session: AcpSession, method: string, params: Record<string, unknown>): Promise<T> {
|
|
773
871
|
const id = `server-${this.nextClientRequestId++}`;
|
|
774
872
|
return new Promise<T>((resolve, reject) => {
|
|
@@ -776,7 +874,9 @@ export class AcpServer {
|
|
|
776
874
|
this.pending.delete(id);
|
|
777
875
|
reject(new Error(`${method} timed out`));
|
|
778
876
|
}, this.permissionTimeoutMs);
|
|
877
|
+
if (typeof timer.unref === 'function') timer.unref();
|
|
779
878
|
this.pending.set(id, {
|
|
879
|
+
sessionId: session.id,
|
|
780
880
|
resolve: (value) => resolve(value as T),
|
|
781
881
|
reject,
|
|
782
882
|
timer
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Agent — Error Classifier & Retry Guidance (Sector 3)
|
|
3
|
+
*
|
|
4
|
+
* Extracted from `AgentLoop` into a standalone Strict TypeScript module so error
|
|
5
|
+
* classification and retry hints are independently testable and honor structured
|
|
6
|
+
* HTTP/network metadata (`ProviderHttpError.status`, `code: 'ETIMEDOUT'`, partial usage).
|
|
7
|
+
*/
|
|
8
|
+
export function classifyAgentError(error) {
|
|
9
|
+
const errObj = typeof error === 'object' && error !== null ? error : null;
|
|
10
|
+
if (errObj?.code === 'PROVIDER_NO_PROGRESS')
|
|
11
|
+
return 'fatal';
|
|
12
|
+
if (errObj?.code === 'AGY_NATIVE_ACTION_DENIED' ||
|
|
13
|
+
errObj?.code === 'AGY_NATIVE_ACTION_BUDGET' ||
|
|
14
|
+
errObj?.code === 'AGY_DESKTOP_TASK_LIMIT') {
|
|
15
|
+
return 'fatal';
|
|
16
|
+
}
|
|
17
|
+
// A provider may have charged for completed steps before the turn failed.
|
|
18
|
+
// Replaying that turn can repeat actions and spend the same tokens again.
|
|
19
|
+
const posTokens = (v) => {
|
|
20
|
+
const n = Number(v);
|
|
21
|
+
return Number.isFinite(n) && n > 0 ? n : 0;
|
|
22
|
+
};
|
|
23
|
+
const spent = posTokens(errObj?.partialUsage?.promptTokens) +
|
|
24
|
+
posTokens(errObj?.partialUsage?.completionTokens);
|
|
25
|
+
if (spent > 0)
|
|
26
|
+
return 'fatal';
|
|
27
|
+
const msg = (typeof error === 'string'
|
|
28
|
+
? error
|
|
29
|
+
: typeof errObj?.message === 'string'
|
|
30
|
+
? errObj.message
|
|
31
|
+
: '').toLowerCase();
|
|
32
|
+
if (errObj?.name === 'OpenRouterPrivacyConstraintError' ||
|
|
33
|
+
msg.includes('matching the privacy policy') ||
|
|
34
|
+
msg.includes('data_collection=')) {
|
|
35
|
+
return 'fatal';
|
|
36
|
+
}
|
|
37
|
+
if (msg.includes('free usage limit') ||
|
|
38
|
+
msg.includes('subscribe to opencode') ||
|
|
39
|
+
msg.includes('opencode.ai/go') ||
|
|
40
|
+
msg.includes('insufficient_quota') ||
|
|
41
|
+
msg.includes('credit balance is too low')) {
|
|
42
|
+
return 'fatal';
|
|
43
|
+
}
|
|
44
|
+
// Honor structured HTTP status codes and transport error codes directly
|
|
45
|
+
const status = typeof errObj?.status === 'number' ? errObj.status : null;
|
|
46
|
+
if (status === 429)
|
|
47
|
+
return 'rate_limit';
|
|
48
|
+
if (status === 401 || status === 403)
|
|
49
|
+
return 'auth_error';
|
|
50
|
+
if (status === 400)
|
|
51
|
+
return 'invalid_request_error';
|
|
52
|
+
if (status !== null && status >= 500 && status <= 599)
|
|
53
|
+
return 'network_error';
|
|
54
|
+
if (errObj?.code === 'ETIMEDOUT' ||
|
|
55
|
+
errObj?.code === 'ECONNREFUSED' ||
|
|
56
|
+
errObj?.code === 'ECONNRESET') {
|
|
57
|
+
return 'network_error';
|
|
58
|
+
}
|
|
59
|
+
if (msg.includes('syntax') || msg.includes('unexpected token') || msg.includes('invalid json')) {
|
|
60
|
+
return 'syntax_error';
|
|
61
|
+
}
|
|
62
|
+
if (msg.includes('enoent') ||
|
|
63
|
+
msg.includes('no such file') ||
|
|
64
|
+
msg.includes('not found') ||
|
|
65
|
+
msg.includes('does not exist')) {
|
|
66
|
+
return 'path_not_found';
|
|
67
|
+
}
|
|
68
|
+
if (msg.includes('econnrefused') ||
|
|
69
|
+
msg.includes('econnreset') ||
|
|
70
|
+
msg.includes('etimedout') ||
|
|
71
|
+
msg.includes('fetch failed') ||
|
|
72
|
+
msg.includes('network')) {
|
|
73
|
+
return 'network_error';
|
|
74
|
+
}
|
|
75
|
+
if (msg.includes('rate limit') ||
|
|
76
|
+
msg.includes('429') ||
|
|
77
|
+
msg.includes('too many requests') ||
|
|
78
|
+
msg.includes('quota')) {
|
|
79
|
+
return 'rate_limit';
|
|
80
|
+
}
|
|
81
|
+
if (msg.includes('eacces') || msg.includes('permission denied') || msg.includes('eperm')) {
|
|
82
|
+
return 'permission_denied';
|
|
83
|
+
}
|
|
84
|
+
if (msg.includes('401') ||
|
|
85
|
+
msg.includes('403') ||
|
|
86
|
+
msg.includes('unauthorized') ||
|
|
87
|
+
msg.includes('api key')) {
|
|
88
|
+
return 'auth_error';
|
|
89
|
+
}
|
|
90
|
+
if (msg.includes('400') ||
|
|
91
|
+
msg.includes('invalid_parameter') ||
|
|
92
|
+
msg.includes('invalid_request') ||
|
|
93
|
+
msg.includes('required property') ||
|
|
94
|
+
msg.includes('bad request')) {
|
|
95
|
+
return 'invalid_request_error';
|
|
96
|
+
}
|
|
97
|
+
if (msg.includes('kill') || msg.includes('abort') || msg.includes('signal') || msg.includes('fatal')) {
|
|
98
|
+
return 'fatal';
|
|
99
|
+
}
|
|
100
|
+
if (error instanceof TypeError || error instanceof ReferenceError || error instanceof RangeError) {
|
|
101
|
+
return 'fatal';
|
|
102
|
+
}
|
|
103
|
+
return 'unknown_error';
|
|
104
|
+
}
|
|
105
|
+
const RETRY_HINTS = {
|
|
106
|
+
syntax_error: 'Check your command syntax or JSON structure. Ensure quotes are balanced.',
|
|
107
|
+
path_not_found: 'The file or directory does not exist. Use fs.list to verify the path first, or create it with fs.write/mkdir.',
|
|
108
|
+
network_error: 'Network connectivity issue. Wait a moment and retry. If persistent, check if the URL is correct.',
|
|
109
|
+
rate_limit: 'API rate limit hit. Wait 5-10 seconds before retrying.',
|
|
110
|
+
permission_denied: 'Permission denied. Check if the path is within allowed workspace paths. Try a different directory.',
|
|
111
|
+
auth_error: 'Authentication failed. Do NOT retry — the API key may be invalid.',
|
|
112
|
+
invalid_request_error: 'API request format error (400 Bad Request). Do NOT retry — check model parameter compliance.',
|
|
113
|
+
unknown_error: 'An unexpected error occurred. Try a different approach entirely.'
|
|
114
|
+
};
|
|
115
|
+
export function getRetryHintForError(errorType) {
|
|
116
|
+
return RETRY_HINTS[errorType] ?? RETRY_HINTS.unknown_error;
|
|
117
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Agent — Error Classifier & Retry Guidance (Sector 3)
|
|
3
|
+
*
|
|
4
|
+
* Extracted from `AgentLoop` into a standalone Strict TypeScript module so error
|
|
5
|
+
* classification and retry hints are independently testable and honor structured
|
|
6
|
+
* HTTP/network metadata (`ProviderHttpError.status`, `code: 'ETIMEDOUT'`, partial usage).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type AgentErrorClass =
|
|
10
|
+
| 'fatal'
|
|
11
|
+
| 'syntax_error'
|
|
12
|
+
| 'path_not_found'
|
|
13
|
+
| 'network_error'
|
|
14
|
+
| 'rate_limit'
|
|
15
|
+
| 'permission_denied'
|
|
16
|
+
| 'auth_error'
|
|
17
|
+
| 'invalid_request_error'
|
|
18
|
+
| 'unknown_error';
|
|
19
|
+
|
|
20
|
+
interface StructuredErrorLike {
|
|
21
|
+
code?: unknown;
|
|
22
|
+
name?: unknown;
|
|
23
|
+
status?: unknown;
|
|
24
|
+
message?: unknown;
|
|
25
|
+
partialUsage?: {
|
|
26
|
+
promptTokens?: unknown;
|
|
27
|
+
completionTokens?: unknown;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function classifyAgentError(error: unknown): AgentErrorClass {
|
|
32
|
+
const errObj: StructuredErrorLike | null =
|
|
33
|
+
typeof error === 'object' && error !== null ? (error as StructuredErrorLike) : null;
|
|
34
|
+
|
|
35
|
+
if (errObj?.code === 'PROVIDER_NO_PROGRESS') return 'fatal';
|
|
36
|
+
if (
|
|
37
|
+
errObj?.code === 'AGY_NATIVE_ACTION_DENIED' ||
|
|
38
|
+
errObj?.code === 'AGY_NATIVE_ACTION_BUDGET' ||
|
|
39
|
+
errObj?.code === 'AGY_DESKTOP_TASK_LIMIT'
|
|
40
|
+
) {
|
|
41
|
+
return 'fatal';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// A provider may have charged for completed steps before the turn failed.
|
|
45
|
+
// Replaying that turn can repeat actions and spend the same tokens again.
|
|
46
|
+
const posTokens = (v: unknown): number => {
|
|
47
|
+
const n = Number(v);
|
|
48
|
+
return Number.isFinite(n) && n > 0 ? n : 0;
|
|
49
|
+
};
|
|
50
|
+
const spent =
|
|
51
|
+
posTokens(errObj?.partialUsage?.promptTokens) +
|
|
52
|
+
posTokens(errObj?.partialUsage?.completionTokens);
|
|
53
|
+
if (spent > 0) return 'fatal';
|
|
54
|
+
|
|
55
|
+
const msg = (
|
|
56
|
+
typeof error === 'string'
|
|
57
|
+
? error
|
|
58
|
+
: typeof errObj?.message === 'string'
|
|
59
|
+
? errObj.message
|
|
60
|
+
: ''
|
|
61
|
+
).toLowerCase();
|
|
62
|
+
|
|
63
|
+
if (
|
|
64
|
+
errObj?.name === 'OpenRouterPrivacyConstraintError' ||
|
|
65
|
+
msg.includes('matching the privacy policy') ||
|
|
66
|
+
msg.includes('data_collection=')
|
|
67
|
+
) {
|
|
68
|
+
return 'fatal';
|
|
69
|
+
}
|
|
70
|
+
if (
|
|
71
|
+
msg.includes('free usage limit') ||
|
|
72
|
+
msg.includes('subscribe to opencode') ||
|
|
73
|
+
msg.includes('opencode.ai/go') ||
|
|
74
|
+
msg.includes('insufficient_quota') ||
|
|
75
|
+
msg.includes('credit balance is too low')
|
|
76
|
+
) {
|
|
77
|
+
return 'fatal';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Honor structured HTTP status codes and transport error codes directly
|
|
81
|
+
const status = typeof errObj?.status === 'number' ? errObj.status : null;
|
|
82
|
+
if (status === 429) return 'rate_limit';
|
|
83
|
+
if (status === 401 || status === 403) return 'auth_error';
|
|
84
|
+
if (status === 400) return 'invalid_request_error';
|
|
85
|
+
if (status !== null && status >= 500 && status <= 599) return 'network_error';
|
|
86
|
+
if (
|
|
87
|
+
errObj?.code === 'ETIMEDOUT' ||
|
|
88
|
+
errObj?.code === 'ECONNREFUSED' ||
|
|
89
|
+
errObj?.code === 'ECONNRESET'
|
|
90
|
+
) {
|
|
91
|
+
return 'network_error';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (msg.includes('syntax') || msg.includes('unexpected token') || msg.includes('invalid json')) {
|
|
95
|
+
return 'syntax_error';
|
|
96
|
+
}
|
|
97
|
+
if (
|
|
98
|
+
msg.includes('enoent') ||
|
|
99
|
+
msg.includes('no such file') ||
|
|
100
|
+
msg.includes('not found') ||
|
|
101
|
+
msg.includes('does not exist')
|
|
102
|
+
) {
|
|
103
|
+
return 'path_not_found';
|
|
104
|
+
}
|
|
105
|
+
if (
|
|
106
|
+
msg.includes('econnrefused') ||
|
|
107
|
+
msg.includes('econnreset') ||
|
|
108
|
+
msg.includes('etimedout') ||
|
|
109
|
+
msg.includes('fetch failed') ||
|
|
110
|
+
msg.includes('network')
|
|
111
|
+
) {
|
|
112
|
+
return 'network_error';
|
|
113
|
+
}
|
|
114
|
+
if (
|
|
115
|
+
msg.includes('rate limit') ||
|
|
116
|
+
msg.includes('429') ||
|
|
117
|
+
msg.includes('too many requests') ||
|
|
118
|
+
msg.includes('quota')
|
|
119
|
+
) {
|
|
120
|
+
return 'rate_limit';
|
|
121
|
+
}
|
|
122
|
+
if (msg.includes('eacces') || msg.includes('permission denied') || msg.includes('eperm')) {
|
|
123
|
+
return 'permission_denied';
|
|
124
|
+
}
|
|
125
|
+
if (
|
|
126
|
+
msg.includes('401') ||
|
|
127
|
+
msg.includes('403') ||
|
|
128
|
+
msg.includes('unauthorized') ||
|
|
129
|
+
msg.includes('api key')
|
|
130
|
+
) {
|
|
131
|
+
return 'auth_error';
|
|
132
|
+
}
|
|
133
|
+
if (
|
|
134
|
+
msg.includes('400') ||
|
|
135
|
+
msg.includes('invalid_parameter') ||
|
|
136
|
+
msg.includes('invalid_request') ||
|
|
137
|
+
msg.includes('required property') ||
|
|
138
|
+
msg.includes('bad request')
|
|
139
|
+
) {
|
|
140
|
+
return 'invalid_request_error';
|
|
141
|
+
}
|
|
142
|
+
if (msg.includes('kill') || msg.includes('abort') || msg.includes('signal') || msg.includes('fatal')) {
|
|
143
|
+
return 'fatal';
|
|
144
|
+
}
|
|
145
|
+
if (error instanceof TypeError || error instanceof ReferenceError || error instanceof RangeError) {
|
|
146
|
+
return 'fatal';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return 'unknown_error';
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const RETRY_HINTS: Record<string, string> = {
|
|
153
|
+
syntax_error: 'Check your command syntax or JSON structure. Ensure quotes are balanced.',
|
|
154
|
+
path_not_found: 'The file or directory does not exist. Use fs.list to verify the path first, or create it with fs.write/mkdir.',
|
|
155
|
+
network_error: 'Network connectivity issue. Wait a moment and retry. If persistent, check if the URL is correct.',
|
|
156
|
+
rate_limit: 'API rate limit hit. Wait 5-10 seconds before retrying.',
|
|
157
|
+
permission_denied: 'Permission denied. Check if the path is within allowed workspace paths. Try a different directory.',
|
|
158
|
+
auth_error: 'Authentication failed. Do NOT retry — the API key may be invalid.',
|
|
159
|
+
invalid_request_error: 'API request format error (400 Bad Request). Do NOT retry — check model parameter compliance.',
|
|
160
|
+
unknown_error: 'An unexpected error occurred. Try a different approach entirely.'
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
export function getRetryHintForError(errorType: string): string {
|
|
164
|
+
return RETRY_HINTS[errorType] ?? RETRY_HINTS.unknown_error!;
|
|
165
|
+
}
|