@causyne/agent 1.20.124 → 1.20.128
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/bin/causyne.js +1 -1
- package/docs/THIRD_PARTY_NOTICES.md +36 -2
- package/package.json +6 -4
- package/src/acp/protocol.ts +19 -1
- package/src/acp/server.js +282 -8
- package/src/acp/server.ts +284 -8
- package/src/agent/abortableDelay.js +2 -2
- package/src/agent/abortableDelay.ts +2 -2
- package/src/agent/errorClassifier.js +9 -1
- package/src/agent/errorClassifier.ts +8 -1
- package/src/agentLoop.js +142 -53
- package/src/agentLoop.ts +151 -50
- package/src/approvalPolicy.js +34 -10
- package/src/approvalPolicy.ts +42 -11
- package/src/browser/causyneBrowser.js +61 -3
- package/src/browser/causyneBrowser.ts +65 -3
- package/src/browser/urlPolicy.js +14 -1
- package/src/browser/urlPolicy.ts +12 -1
- package/src/cliMode.js +21 -9
- package/src/cliMode.ts +20 -8
- package/src/commands.js +365 -45
- package/src/commands.ts +403 -45
- package/src/config.js +19 -4
- package/src/config.ts +18 -4
- package/src/core/contextCompactor.js +54 -7
- package/src/core/contextCompactor.ts +63 -7
- package/src/core/goalLoop.js +257 -0
- package/src/core/goalLoop.ts +310 -0
- package/src/entrypoint.js +135 -3
- package/src/entrypoint.ts +136 -3
- package/src/game/causyneCoreBridge.js +686 -0
- package/src/game/causyneCoreBridge.ts +702 -0
- package/src/game/gameLauncher.js +287 -0
- package/src/game/gameLauncher.ts +352 -0
- package/src/game/gameProvisioner.js +425 -0
- package/src/game/gameProvisioner.ts +488 -0
- package/src/headlessCli.js +19 -12
- package/src/headlessCli.ts +22 -8
- package/src/infra/atomicWriter.js +40 -4
- package/src/infra/atomicWriter.ts +36 -4
- package/src/launchIntent.js +73 -3
- package/src/launchIntent.ts +76 -3
- package/src/lsp/lspClient.js +83 -12
- package/src/lsp/lspClient.ts +75 -12
- package/src/mcp/mcpRegistry.js +75 -8
- package/src/mcp/mcpRegistry.ts +70 -7
- package/src/memory/contextBudget.js +2 -0
- package/src/memory/contextBudget.ts +1 -0
- package/src/memory/episodicStore.js +40 -0
- package/src/memory/episodicStore.ts +46 -0
- package/src/memory.js +67 -0
- package/src/memory.ts +65 -0
- package/src/modelVariants.js +45 -0
- package/src/modelVariants.ts +54 -0
- package/src/outputRecovery.js +3 -2
- package/src/outputRecovery.ts +4 -2
- package/src/permissionPresets.js +327 -0
- package/src/permissionPresets.ts +361 -0
- package/src/personalization.js +365 -0
- package/src/personalization.ts +471 -0
- package/src/projectInstructions.js +30 -10
- package/src/projectInstructions.ts +31 -12
- package/src/providers/acp.js +3 -2
- package/src/providers/acp.ts +3 -2
- package/src/providers/antigravity.js +64 -9
- package/src/providers/antigravity.ts +75 -9
- package/src/providers/byokHttp.js +7 -1
- package/src/providers/byokHttp.ts +6 -1
- package/src/providers/byokStream.js +21 -8
- package/src/providers/byokStream.ts +17 -7
- package/src/providers/dashscope-byok.js +21 -5
- package/src/providers/dashscope-byok.ts +29 -11
- package/src/providers/moonshot.js +26 -7
- package/src/providers/moonshot.ts +34 -13
- package/src/providers/muse.js +6 -0
- package/src/providers/muse.ts +8 -0
- package/src/providers/ollama.js +39 -7
- package/src/providers/ollama.ts +36 -7
- package/src/providers/openai-compatible.js +16 -3
- package/src/providers/openai-compatible.ts +13 -3
- package/src/providers/openai.js +62 -4
- package/src/providers/openai.ts +50 -4
- package/src/providers/openrouter.js +19 -4
- package/src/providers/openrouter.ts +21 -4
- package/src/providers/qwen-byok.js +21 -5
- package/src/providers/qwen-byok.ts +29 -11
- package/src/providers/xai.js +26 -10
- package/src/providers/xai.ts +34 -16
- package/src/reporting.js +4 -2
- package/src/reporting.ts +4 -2
- package/src/sandbox.js +34 -0
- package/src/sandbox.ts +43 -0
- package/src/scheduler/scheduler.js +372 -0
- package/src/scheduler/scheduler.ts +433 -0
- package/src/sessionSearchIndex.js +267 -0
- package/src/sessionSearchIndex.ts +311 -0
- package/src/skillsManager.js +2 -3
- package/src/skillsManager.ts +2 -3
- package/src/toolPacks.js +6 -0
- package/src/toolPacks.ts +6 -0
- package/src/tools/applyPatch.js +1 -1
- package/src/tools/applyPatch.ts +1 -1
- package/src/tools/generateMediaViaApi.js +4 -3
- package/src/tools/generateMediaViaApi.ts +4 -3
- package/src/tools/memoryWrite.js +15 -1
- package/src/tools/memoryWrite.ts +16 -1
- package/src/tools/nativeEngine.js +2 -2
- package/src/tools/nativeEngine.ts +2 -2
- package/src/tools/openSourceToolProvisioner.js +2 -2
- package/src/tools/openSourceToolProvisioner.ts +2 -2
- package/src/tools/osSandbox.js +1 -1
- package/src/tools/osSandbox.ts +1 -1
- package/src/tools/payloadSanitizer.js +0 -1
- package/src/tools/payloadSanitizer.ts +0 -1
- package/src/tools/pdfReader.js +122 -20
- package/src/tools/pdfReader.ts +111 -20
- package/src/tools/presentDeliverable.js +103 -0
- package/src/tools/presentDeliverable.ts +127 -0
- package/src/tools/ptcRuntime.js +444 -0
- package/src/tools/ptcRuntime.ts +490 -0
- package/src/tools/ptyTerminal.js +1108 -0
- package/src/tools/ptyTerminal.ts +1245 -0
- package/src/tools/registry.js +21 -12
- package/src/tools/registry.ts +21 -12
- package/src/tools/toolOutputStore.js +26 -4
- package/src/tools/toolOutputStore.ts +22 -4
- package/src/tools/webFetch.js +158 -11
- package/src/tools/webFetch.ts +145 -11
- package/src/tools/webSearch.js +123 -36
- package/src/tools/webSearch.ts +124 -33
- package/src/tui/banner.js +5 -1
- package/src/tui/banner.ts +6 -1
- package/src/tui/chatRenderer.js +1 -2
- package/src/tui/chatRenderer.ts +1 -2
- package/src/tui/completion.js +1 -0
- package/src/tui/completion.ts +1 -0
- package/src/tui/composerControls.js +111 -27
- package/src/tui/composerControls.ts +124 -28
- package/src/tui/dialogs.js +3 -16
- package/src/tui/dialogs.ts +3 -16
- package/src/tui/home.js +1 -0
- package/src/tui/home.ts +1 -0
- package/src/tui/invocation.js +1 -2
- package/src/tui/invocation.ts +1 -2
- package/src/tui/modelPicker.js +22 -12
- package/src/tui/modelPicker.ts +22 -12
- package/src/tui/overlay.js +2 -5
- package/src/tui/overlay.ts +2 -5
- package/src/tui/prompt.js +6 -4
- package/src/tui/prompt.ts +6 -4
- package/src/tui/selectList.js +4 -12
- package/src/tui/selectList.ts +4 -12
- package/src/tui/setup.js +22 -9
- package/src/tui/setup.ts +26 -9
- package/src/tui/statusLine.ts +2 -0
- package/src/web/client/browserPanel.js +905 -0
- package/src/web/client/browserPanel.ts +872 -0
- package/src/web/client/chatStream.js +321 -0
- package/src/web/client/chatStream.ts +297 -0
- package/src/web/client/cliPanel.js +165 -0
- package/src/web/client/cliPanel.ts +171 -0
- package/src/web/client/composer.js +204 -0
- package/src/web/client/composer.ts +209 -0
- package/src/web/client/diffPanel.js +176 -0
- package/src/web/client/diffPanel.ts +203 -0
- package/src/web/client/dom.js +271 -0
- package/src/web/client/dom.ts +272 -0
- package/src/web/client/filesAndCanvas.js +233 -0
- package/src/web/client/filesAndCanvas.ts +240 -0
- package/src/web/client/gameLauncherUi.js +78 -0
- package/src/web/client/gameLauncherUi.ts +78 -0
- package/src/web/client/main.js +316 -0
- package/src/web/client/main.ts +391 -0
- package/src/web/client/markdown.js +364 -0
- package/src/web/client/markdown.ts +373 -0
- package/src/web/client/modelPicker.js +455 -0
- package/src/web/client/modelPicker.ts +451 -0
- package/src/web/client/permissionPresetsUi.js +307 -0
- package/src/web/client/permissionPresetsUi.ts +333 -0
- package/src/web/client/sessions.js +353 -0
- package/src/web/client/sessions.ts +372 -0
- package/src/web/client/settingsModal.js +824 -0
- package/src/web/client/settingsModal.ts +959 -0
- package/src/web/client/state.js +280 -0
- package/src/web/client/state.ts +259 -0
- package/src/web/client/streamSmoother.js +127 -0
- package/src/web/client/streamSmoother.ts +119 -0
- package/src/web/client/studioLayout.js +89 -0
- package/src/web/client/studioLayout.ts +103 -0
- package/src/web/client/studioShell.js +660 -0
- package/src/web/client/studioShell.ts +672 -0
- package/src/web/client/studioStart.js +77 -0
- package/src/web/client/studioStart.ts +89 -0
- package/src/web/client/terminalPanel.js +606 -0
- package/src/web/client/terminalPanel.ts +632 -0
- package/src/web/client/terminalTheme.js +249 -0
- package/src/web/client/terminalTheme.ts +258 -0
- package/src/web/client/turnView.js +934 -0
- package/src/web/client/turnView.ts +981 -0
- package/src/web/client/types.js +1 -0
- package/src/web/client/types.ts +322 -0
- package/src/web/client/validators.js +147 -0
- package/src/web/client/validators.ts +140 -0
- package/src/web/server/activeRuns.js +152 -0
- package/src/web/server/activeRuns.ts +171 -0
- package/src/web/server/routesBrowser.js +409 -0
- package/src/web/server/routesBrowser.ts +435 -0
- package/src/web/server/routesChat.js +304 -0
- package/src/web/server/routesChat.ts +347 -0
- package/src/web/server/routesCore.js +472 -0
- package/src/web/server/routesCore.ts +522 -0
- package/src/web/server/routesSettings.js +344 -0
- package/src/web/server/routesSettings.ts +397 -0
- package/src/web/server/routesTerminal.js +162 -0
- package/src/web/server/routesTerminal.ts +182 -0
- package/src/web/server/routesWorkspace.js +510 -0
- package/src/web/server/routesWorkspace.ts +546 -0
- package/src/web/server/security.js +98 -0
- package/src/web/server/security.ts +108 -0
- package/src/web/server/sessionTranscript.js +586 -0
- package/src/web/server/sessionTranscript.ts +566 -0
- package/src/web/server/staticAssets.js +104 -0
- package/src/web/server/staticAssets.ts +116 -0
- package/src/web/server/types.js +1 -0
- package/src/web/server/types.ts +135 -0
- package/src/web/server/workspaceFiles.js +368 -0
- package/src/web/server/workspaceFiles.ts +363 -0
- package/src/web/skybridgeServer.js +78 -2448
- package/src/web/skybridgeServer.ts +120 -2600
- package/src/web/skybridgeUi.js +20 -450
- package/src/web/skybridgeUi.ts +24 -455
- package/src/web/styles/agent.css +380 -0
- package/src/web/styles/composer.css +193 -0
- package/src/web/styles/overlays.css +935 -0
- package/src/web/styles/shell.css +387 -0
- package/src/web/styles/studio.css +951 -0
- package/src/web/styles/thread.css +221 -0
- package/src/web/styles/tokens.css +94 -0
- package/src/web/ui/brandMark.js +73 -0
- package/src/web/ui/brandMark.ts +82 -0
- package/src/web/ui/modalsTemplate.js +388 -0
- package/src/web/ui/modalsTemplate.ts +388 -0
- package/src/web/ui/sidebarTemplate.js +63 -0
- package/src/web/ui/sidebarTemplate.ts +64 -0
- package/src/web/ui/studioTemplate.js +256 -0
- package/src/web/ui/studioTemplate.ts +256 -0
- package/src/web/ui/workspaceTemplate.js +72 -0
- package/src/web/ui/workspaceTemplate.ts +73 -0
- package/src/worktreeManager.js +16 -1
- package/src/worktreeManager.ts +22 -1
- package/src/web/skybridgeClientScript.js +0 -3951
- package/src/web/skybridgeClientScript.ts +0 -3952
- package/src/web/skybridgeStyles.js +0 -827
- package/src/web/skybridgeStyles.ts +0 -828
|
@@ -58,7 +58,7 @@ export function atomicRenameSync(
|
|
|
58
58
|
): void {
|
|
59
59
|
const platform = options.platform ?? process.platform;
|
|
60
60
|
const maxRetries = platform === 'win32'
|
|
61
|
-
? Math.max(0, options.maxRetries ?? DEFAULT_RENAME_BACKOFF_MS.length)
|
|
61
|
+
? (Number.isSafeInteger(options.maxRetries) ? Math.min(100, Math.max(0, options.maxRetries ?? 0)) : DEFAULT_RENAME_BACKOFF_MS.length)
|
|
62
62
|
: 0;
|
|
63
63
|
|
|
64
64
|
let attempt = 0;
|
|
@@ -88,7 +88,7 @@ export async function atomicRename(
|
|
|
88
88
|
): Promise<void> {
|
|
89
89
|
const platform = options.platform ?? process.platform;
|
|
90
90
|
const maxRetries = platform === 'win32'
|
|
91
|
-
? Math.max(0, options.maxRetries ?? DEFAULT_RENAME_BACKOFF_MS.length)
|
|
91
|
+
? (Number.isSafeInteger(options.maxRetries) ? Math.min(100, Math.max(0, options.maxRetries ?? 0)) : DEFAULT_RENAME_BACKOFF_MS.length)
|
|
92
92
|
: 0;
|
|
93
93
|
|
|
94
94
|
let attempt = 0;
|
|
@@ -130,7 +130,12 @@ export function atomicWriteFileSync(
|
|
|
130
130
|
: content;
|
|
131
131
|
const fd = fs.openSync(tempPath, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_WRONLY, mode);
|
|
132
132
|
try {
|
|
133
|
-
|
|
133
|
+
let offset = 0;
|
|
134
|
+
while (offset < payload.length) {
|
|
135
|
+
const written = fs.writeSync(fd, payload, offset, payload.length - offset);
|
|
136
|
+
if (written <= 0) throw new Error('Atomic file write made no progress');
|
|
137
|
+
offset += written;
|
|
138
|
+
}
|
|
134
139
|
fs.fsyncSync(fd);
|
|
135
140
|
} finally {
|
|
136
141
|
fs.closeSync(fd);
|
|
@@ -151,5 +156,32 @@ export async function atomicWriteFile(
|
|
|
151
156
|
content: string | Buffer,
|
|
152
157
|
options: AtomicWriteOptions = {}
|
|
153
158
|
): Promise<void> {
|
|
154
|
-
|
|
159
|
+
const resolved = path.resolve(targetPath);
|
|
160
|
+
const dir = path.dirname(resolved);
|
|
161
|
+
const mode = options.mode ?? 0o644;
|
|
162
|
+
const dirMode = options.mode !== undefined
|
|
163
|
+
? (options.mode | ((options.mode & 0o444) >> 2))
|
|
164
|
+
: 0o755;
|
|
165
|
+
const payload = typeof content === 'string'
|
|
166
|
+
? Buffer.from(content, options.encoding ?? 'utf8')
|
|
167
|
+
: content;
|
|
168
|
+
await fsPromises.mkdir(dir, { recursive: true, mode: dirMode });
|
|
169
|
+
const randomSuffix = `${process.pid}.${crypto.randomBytes(6).toString('hex')}`;
|
|
170
|
+
const tempPath = path.join(dir, `.${path.basename(resolved)}.${randomSuffix}.tmp`);
|
|
171
|
+
let ownsTemp = false;
|
|
172
|
+
try {
|
|
173
|
+
const handle = await fsPromises.open(tempPath, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_WRONLY, mode);
|
|
174
|
+
ownsTemp = true;
|
|
175
|
+
try {
|
|
176
|
+
await handle.writeFile(payload);
|
|
177
|
+
await handle.sync();
|
|
178
|
+
} finally {
|
|
179
|
+
await handle.close();
|
|
180
|
+
}
|
|
181
|
+
await atomicRename(tempPath, resolved);
|
|
182
|
+
ownsTemp = false;
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (ownsTemp) await fsPromises.unlink(tempPath).catch(() => {});
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
155
187
|
}
|
package/src/launchIntent.js
CHANGED
|
@@ -26,10 +26,80 @@ const SCOPED_TEST_PROHIBITION = /\b(?:don'?t|do\s+not|never|without)\s+(?:run|ex
|
|
|
26
26
|
const SCOPED_BACKTICK_EXCLUSION = /\b(?:don'?t|do\s+not|never|without)\s+(?:modify|modifying|mutate|mutating|touch|touching|write|writing|change|changing|edit|editing|delete|deleting|alter|altering|update|updating)\s+`[^`\r\n]{1,160}`(?:\s+(?:or|and)\s+`[^`\r\n]{1,160}`){0,4}/gi;
|
|
27
27
|
const SCOPED_PATH_EXCLUSION = /\b(?:don'?t|do\s+not|never|without)\s+(?:modify|modifying|mutate|mutating|touch|touching|write|writing|change|changing|edit|editing|delete|deleting|alter|altering|update|updating)\s+(?:the\s+)?(?:[A-Za-z0-9_.-]{1,64}\/[A-Za-z0-9_./-]{1,128}|[A-Za-z0-9_-]{1,64}\.[A-Za-z0-9]{1,10})(?:\s+(?:or|and)\s+(?:the\s+)?(?:[A-Za-z0-9_.-]{1,64}\/[A-Za-z0-9_./-]{1,128}|[A-Za-z0-9_-]{1,64}\.[A-Za-z0-9]{1,10})){0,4}/gi;
|
|
28
28
|
const SCOPED_ARTIFACT_EXCLUSION = /\b(?:don'?t|do\s+not|never|without)\s+(?:modify|modifying|mutate|mutating|touch|touching|write|writing|change|changing|edit|editing|delete|deleting|alter|altering|update|updating)\s+(?:the\s+|existing\s+|other\s+|unrelated\s+|public\s+|protected\s+|original\s+)?(?:tests?|test\s+files?|specs?|fixtures?|configs?|config\s+files?|lockfiles?|migrations?|schemas?|exports?|public\s+api|signatures?|interfaces?|headers?|comments?|formatting|unrelated\s+files?|protected\s+files?)\b/gi;
|
|
29
|
+
const STRUCTURED_HARNESS_TAGS = [
|
|
30
|
+
'openmotion-workspace-context',
|
|
31
|
+
'openmotion-task-instructions',
|
|
32
|
+
'openmotion-referenced-conversations',
|
|
33
|
+
'openmotion-browser-annotations',
|
|
34
|
+
'workspace-context',
|
|
35
|
+
'system-reminder'
|
|
36
|
+
];
|
|
37
|
+
function stripTaggedBlockLinear(input, tagName) {
|
|
38
|
+
const openTag = `<${tagName}`;
|
|
39
|
+
const closeTag = `</${tagName}>`;
|
|
40
|
+
let cursor = 0;
|
|
41
|
+
let out = '';
|
|
42
|
+
while (cursor < input.length) {
|
|
43
|
+
const start = input.indexOf(openTag, cursor);
|
|
44
|
+
if (start < 0) {
|
|
45
|
+
out += input.slice(cursor);
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
const openEnd = input.indexOf('>', start + openTag.length);
|
|
49
|
+
if (openEnd < 0) {
|
|
50
|
+
out += input.slice(cursor);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
out += input.slice(cursor, start);
|
|
54
|
+
const end = input.indexOf(closeTag, openEnd + 1);
|
|
55
|
+
if (end < 0) {
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
cursor = end + closeTag.length;
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Extract the user-authored portion of a prompt when an external host (such as
|
|
64
|
+
* Causyne Game Super Agent) wraps a user message with multi-paragraph system
|
|
65
|
+
* instructions or XML context blocks. Uses purely linear index scans (ReDoS-free).
|
|
66
|
+
*/
|
|
67
|
+
function extractUserAuthoredIntent(prompt) {
|
|
68
|
+
let text = prompt;
|
|
69
|
+
for (const tag of STRUCTURED_HARNESS_TAGS) {
|
|
70
|
+
if (text.includes(`<${tag}`)) {
|
|
71
|
+
text = stripTaggedBlockLinear(text, tag);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// If this is a structured harness envelope containing Super Agent workflow rules,
|
|
75
|
+
// isolate the trailing user request block so system guidance ("read-only reference",
|
|
76
|
+
// "without edits", "do not call ...") never trips the user-intent read-only guard.
|
|
77
|
+
const hasHarnessDirectives = text.includes('openmotion_read_workflow')
|
|
78
|
+
|| text.includes('openmotion_game_')
|
|
79
|
+
|| text.includes('openmotion_assets_use')
|
|
80
|
+
|| text.includes('openmotion_get_project')
|
|
81
|
+
|| text.includes('openmotion://workflow/');
|
|
82
|
+
if (hasHarnessDirectives) {
|
|
83
|
+
const userMarkers = ['\nUser request:', '\nUser message:', '\n## User', '\nUser:'];
|
|
84
|
+
for (const marker of userMarkers) {
|
|
85
|
+
const idx = text.lastIndexOf(marker);
|
|
86
|
+
if (idx >= 0) {
|
|
87
|
+
return text.slice(idx + marker.length).trim();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// No separate user marker found in a system-directed harness prompt: do not
|
|
91
|
+
// misclassify host system rules as a user "explain only / no execution" order.
|
|
92
|
+
return '';
|
|
93
|
+
}
|
|
94
|
+
return text;
|
|
95
|
+
}
|
|
29
96
|
export function isNoExecutionPrompt(prompt) {
|
|
30
97
|
if (!prompt || typeof prompt !== 'string')
|
|
31
98
|
return false;
|
|
32
|
-
const
|
|
99
|
+
const userText = extractUserAuthoredIntent(prompt);
|
|
100
|
+
if (!userText.trim())
|
|
101
|
+
return false;
|
|
102
|
+
const cleaned = userText
|
|
33
103
|
.replace(SCOPED_TEST_PROHIBITION, ' ')
|
|
34
104
|
.replace(SCOPED_BACKTICK_EXCLUSION, ' ')
|
|
35
105
|
.replace(SCOPED_PATH_EXCLUSION, ' ')
|
|
@@ -39,7 +109,7 @@ export function isNoExecutionPrompt(prompt) {
|
|
|
39
109
|
export function isReadOnlyOrExplanatoryPrompt(prompt) {
|
|
40
110
|
if (!prompt || typeof prompt !== 'string')
|
|
41
111
|
return false;
|
|
42
|
-
const trimmed = prompt.trim();
|
|
112
|
+
const trimmed = extractUserAuthoredIntent(prompt).trim();
|
|
43
113
|
if (trimmed.length === 0)
|
|
44
114
|
return false;
|
|
45
115
|
if (isNoExecutionPrompt(trimmed))
|
|
@@ -73,7 +143,7 @@ export function readOnlyScopeError(tool, args, prompt) {
|
|
|
73
143
|
const p = prompt || '';
|
|
74
144
|
const noExecution = isNoExecutionPrompt(p);
|
|
75
145
|
if (noExecution) {
|
|
76
|
-
if (tool === 'fs_read' || tool === 'doc_read')
|
|
146
|
+
if (tool === 'fs_read' || tool === 'doc_read' || tool === 'tool_search')
|
|
77
147
|
return null;
|
|
78
148
|
return 'The user explicitly requested an explanation without action execution; modifying files or executing commands is not authorized.';
|
|
79
149
|
}
|
package/src/launchIntent.ts
CHANGED
|
@@ -30,9 +30,82 @@ const SCOPED_BACKTICK_EXCLUSION = /\b(?:don'?t|do\s+not|never|without)\s+(?:modi
|
|
|
30
30
|
const SCOPED_PATH_EXCLUSION = /\b(?:don'?t|do\s+not|never|without)\s+(?:modify|modifying|mutate|mutating|touch|touching|write|writing|change|changing|edit|editing|delete|deleting|alter|altering|update|updating)\s+(?:the\s+)?(?:[A-Za-z0-9_.-]{1,64}\/[A-Za-z0-9_./-]{1,128}|[A-Za-z0-9_-]{1,64}\.[A-Za-z0-9]{1,10})(?:\s+(?:or|and)\s+(?:the\s+)?(?:[A-Za-z0-9_.-]{1,64}\/[A-Za-z0-9_./-]{1,128}|[A-Za-z0-9_-]{1,64}\.[A-Za-z0-9]{1,10})){0,4}/gi;
|
|
31
31
|
const SCOPED_ARTIFACT_EXCLUSION = /\b(?:don'?t|do\s+not|never|without)\s+(?:modify|modifying|mutate|mutating|touch|touching|write|writing|change|changing|edit|editing|delete|deleting|alter|altering|update|updating)\s+(?:the\s+|existing\s+|other\s+|unrelated\s+|public\s+|protected\s+|original\s+)?(?:tests?|test\s+files?|specs?|fixtures?|configs?|config\s+files?|lockfiles?|migrations?|schemas?|exports?|public\s+api|signatures?|interfaces?|headers?|comments?|formatting|unrelated\s+files?|protected\s+files?)\b/gi;
|
|
32
32
|
|
|
33
|
+
const STRUCTURED_HARNESS_TAGS = [
|
|
34
|
+
'openmotion-workspace-context',
|
|
35
|
+
'openmotion-task-instructions',
|
|
36
|
+
'openmotion-referenced-conversations',
|
|
37
|
+
'openmotion-browser-annotations',
|
|
38
|
+
'workspace-context',
|
|
39
|
+
'system-reminder'
|
|
40
|
+
] as const;
|
|
41
|
+
|
|
42
|
+
function stripTaggedBlockLinear(input: string, tagName: string): string {
|
|
43
|
+
const openTag = `<${tagName}`;
|
|
44
|
+
const closeTag = `</${tagName}>`;
|
|
45
|
+
let cursor = 0;
|
|
46
|
+
let out = '';
|
|
47
|
+
while (cursor < input.length) {
|
|
48
|
+
const start = input.indexOf(openTag, cursor);
|
|
49
|
+
if (start < 0) {
|
|
50
|
+
out += input.slice(cursor);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
const openEnd = input.indexOf('>', start + openTag.length);
|
|
54
|
+
if (openEnd < 0) {
|
|
55
|
+
out += input.slice(cursor);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
out += input.slice(cursor, start);
|
|
59
|
+
const end = input.indexOf(closeTag, openEnd + 1);
|
|
60
|
+
if (end < 0) {
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
cursor = end + closeTag.length;
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Extract the user-authored portion of a prompt when an external host (such as
|
|
70
|
+
* Causyne Game Super Agent) wraps a user message with multi-paragraph system
|
|
71
|
+
* instructions or XML context blocks. Uses purely linear index scans (ReDoS-free).
|
|
72
|
+
*/
|
|
73
|
+
function extractUserAuthoredIntent(prompt: string): string {
|
|
74
|
+
let text = prompt;
|
|
75
|
+
for (const tag of STRUCTURED_HARNESS_TAGS) {
|
|
76
|
+
if (text.includes(`<${tag}`)) {
|
|
77
|
+
text = stripTaggedBlockLinear(text, tag);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// If this is a structured harness envelope containing Super Agent workflow rules,
|
|
81
|
+
// isolate the trailing user request block so system guidance ("read-only reference",
|
|
82
|
+
// "without edits", "do not call ...") never trips the user-intent read-only guard.
|
|
83
|
+
const hasHarnessDirectives =
|
|
84
|
+
text.includes('openmotion_read_workflow')
|
|
85
|
+
|| text.includes('openmotion_game_')
|
|
86
|
+
|| text.includes('openmotion_assets_use')
|
|
87
|
+
|| text.includes('openmotion_get_project')
|
|
88
|
+
|| text.includes('openmotion://workflow/');
|
|
89
|
+
if (hasHarnessDirectives) {
|
|
90
|
+
const userMarkers = ['\nUser request:', '\nUser message:', '\n## User', '\nUser:'];
|
|
91
|
+
for (const marker of userMarkers) {
|
|
92
|
+
const idx = text.lastIndexOf(marker);
|
|
93
|
+
if (idx >= 0) {
|
|
94
|
+
return text.slice(idx + marker.length).trim();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// No separate user marker found in a system-directed harness prompt: do not
|
|
98
|
+
// misclassify host system rules as a user "explain only / no execution" order.
|
|
99
|
+
return '';
|
|
100
|
+
}
|
|
101
|
+
return text;
|
|
102
|
+
}
|
|
103
|
+
|
|
33
104
|
export function isNoExecutionPrompt(prompt?: string | null): boolean {
|
|
34
105
|
if (!prompt || typeof prompt !== 'string') return false;
|
|
35
|
-
const
|
|
106
|
+
const userText = extractUserAuthoredIntent(prompt);
|
|
107
|
+
if (!userText.trim()) return false;
|
|
108
|
+
const cleaned = userText
|
|
36
109
|
.replace(SCOPED_TEST_PROHIBITION, ' ')
|
|
37
110
|
.replace(SCOPED_BACKTICK_EXCLUSION, ' ')
|
|
38
111
|
.replace(SCOPED_PATH_EXCLUSION, ' ')
|
|
@@ -42,7 +115,7 @@ export function isNoExecutionPrompt(prompt?: string | null): boolean {
|
|
|
42
115
|
|
|
43
116
|
export function isReadOnlyOrExplanatoryPrompt(prompt?: string | null): boolean {
|
|
44
117
|
if (!prompt || typeof prompt !== 'string') return false;
|
|
45
|
-
const trimmed = prompt.trim();
|
|
118
|
+
const trimmed = extractUserAuthoredIntent(prompt).trim();
|
|
46
119
|
if (trimmed.length === 0) return false;
|
|
47
120
|
if (isNoExecutionPrompt(trimmed)) return true;
|
|
48
121
|
if (!INQUIRY_WORDS.test(trimmed)) return false;
|
|
@@ -78,7 +151,7 @@ export function readOnlyScopeError(
|
|
|
78
151
|
const noExecution = isNoExecutionPrompt(p);
|
|
79
152
|
|
|
80
153
|
if (noExecution) {
|
|
81
|
-
if (tool === 'fs_read' || tool === 'doc_read') return null;
|
|
154
|
+
if (tool === 'fs_read' || tool === 'doc_read' || tool === 'tool_search') return null;
|
|
82
155
|
return 'The user explicitly requested an explanation without action execution; modifying files or executing commands is not authorized.';
|
|
83
156
|
}
|
|
84
157
|
|
package/src/lsp/lspClient.js
CHANGED
|
@@ -139,6 +139,7 @@ export class LspSession {
|
|
|
139
139
|
diagnosticWaiters = new Map();
|
|
140
140
|
initPromise = null;
|
|
141
141
|
closed = false;
|
|
142
|
+
stderrTail = '';
|
|
142
143
|
lastUsedAt = Date.now();
|
|
143
144
|
serverCapabilities = {};
|
|
144
145
|
constructor(options) {
|
|
@@ -169,7 +170,12 @@ export class LspSession {
|
|
|
169
170
|
});
|
|
170
171
|
this.proc = proc;
|
|
171
172
|
proc.stdin.on('error', () => { });
|
|
172
|
-
proc.stderr.on('data', () => {
|
|
173
|
+
proc.stderr.on('data', (chunk) => {
|
|
174
|
+
const text = chunk.toString('utf8');
|
|
175
|
+
if (text) {
|
|
176
|
+
this.stderrTail = (this.stderrTail + text).slice(-16 * 1024);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
173
179
|
proc.stdout.on('data', (chunk) => {
|
|
174
180
|
try {
|
|
175
181
|
const messages = this.parser.feed(chunk);
|
|
@@ -182,23 +188,28 @@ export class LspSession {
|
|
|
182
188
|
}
|
|
183
189
|
});
|
|
184
190
|
proc.on('error', (err) => {
|
|
185
|
-
this.
|
|
191
|
+
const tailNote = this.stderrTail.trim() ? ` | stderr: ${safeDisplay(this.stderrTail.trim()).slice(-400)}` : '';
|
|
192
|
+
this.rejectAllPending(new Error(`LSP server "${this.serverId}" process error: ${err.message}${tailNote}`));
|
|
186
193
|
this.proc = null;
|
|
187
194
|
this.initPromise = null;
|
|
188
195
|
});
|
|
189
196
|
proc.on('exit', (code) => {
|
|
190
|
-
this.
|
|
197
|
+
const tailNote = this.stderrTail.trim() ? ` | stderr: ${safeDisplay(this.stderrTail.trim()).slice(-400)}` : '';
|
|
198
|
+
this.rejectAllPending(new Error(`LSP server "${this.serverId}" exited (code ${String(code)})${tailNote}.`));
|
|
191
199
|
this.proc = null;
|
|
192
200
|
this.initPromise = null;
|
|
193
201
|
});
|
|
194
202
|
const rootUri = filePathToUri(this.workspaceRoot);
|
|
195
203
|
const initResult = await this.sendRequest('initialize', {
|
|
196
|
-
processId:
|
|
204
|
+
processId: null,
|
|
197
205
|
clientInfo: { name: 'causyne-agent', version: '1.0' },
|
|
198
206
|
rootUri,
|
|
199
207
|
rootPath: this.workspaceRoot,
|
|
200
208
|
workspaceFolders: [{ uri: rootUri, name: path.basename(this.workspaceRoot) }],
|
|
201
209
|
capabilities: {
|
|
210
|
+
general: {
|
|
211
|
+
positionEncodings: ['utf-16']
|
|
212
|
+
},
|
|
202
213
|
textDocument: {
|
|
203
214
|
synchronization: { didSave: true },
|
|
204
215
|
definition: { linkSupport: true },
|
|
@@ -219,6 +230,10 @@ export class LspSession {
|
|
|
219
230
|
}
|
|
220
231
|
});
|
|
221
232
|
if (initResult && typeof initResult.capabilities === 'object' && initResult.capabilities !== null) {
|
|
233
|
+
const posEnc = initResult.capabilities.positionEncoding;
|
|
234
|
+
if (typeof posEnc === 'string' && posEnc.toLowerCase() !== 'utf-16') {
|
|
235
|
+
throw new Error(`LSP server "${this.serverId}" negotiated unsupported positionEncoding "${safeDisplay(posEnc)}" (expected utf-16).`);
|
|
236
|
+
}
|
|
222
237
|
this.serverCapabilities = initResult.capabilities;
|
|
223
238
|
}
|
|
224
239
|
this.sendNotification('initialized', {});
|
|
@@ -246,20 +261,48 @@ export class LspSession {
|
|
|
246
261
|
}
|
|
247
262
|
return;
|
|
248
263
|
}
|
|
249
|
-
// Server -> client request
|
|
264
|
+
// Server -> client request: answer safe read-only lifecycle queries accurately,
|
|
265
|
+
// and explicitly reject mutating or unsupported requests (e.g. workspace/applyEdit).
|
|
250
266
|
if (msg.id !== undefined && msg.id !== null && typeof msg.method === 'string') {
|
|
251
|
-
let result = null;
|
|
252
267
|
if (msg.method === 'workspace/configuration') {
|
|
253
268
|
const items = (msg.params && typeof msg.params === 'object' && Array.isArray(msg.params.items))
|
|
254
269
|
? msg.params.items
|
|
255
270
|
: [];
|
|
256
|
-
|
|
271
|
+
try {
|
|
272
|
+
this.writeFrame({ jsonrpc: '2.0', id: msg.id, result: items.map(() => null) });
|
|
273
|
+
}
|
|
274
|
+
catch { /* ignore */ }
|
|
275
|
+
return;
|
|
257
276
|
}
|
|
258
|
-
|
|
259
|
-
|
|
277
|
+
if (msg.method === 'workspace/workspaceFolders') {
|
|
278
|
+
try {
|
|
279
|
+
this.writeFrame({
|
|
280
|
+
jsonrpc: '2.0',
|
|
281
|
+
id: msg.id,
|
|
282
|
+
result: [{ uri: filePathToUri(this.workspaceRoot), name: path.basename(this.workspaceRoot) }]
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
catch { /* ignore */ }
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
if (msg.method === 'window/workDoneProgress/create'
|
|
289
|
+
|| msg.method === 'client/registerCapability'
|
|
290
|
+
|| msg.method === 'client/unregisterCapability') {
|
|
291
|
+
try {
|
|
292
|
+
this.writeFrame({ jsonrpc: '2.0', id: msg.id, result: null });
|
|
293
|
+
}
|
|
294
|
+
catch { /* ignore */ }
|
|
295
|
+
return;
|
|
260
296
|
}
|
|
261
297
|
try {
|
|
262
|
-
this.writeFrame({
|
|
298
|
+
this.writeFrame({
|
|
299
|
+
jsonrpc: '2.0',
|
|
300
|
+
id: msg.id,
|
|
301
|
+
error: {
|
|
302
|
+
code: -32601,
|
|
303
|
+
message: `Client does not support server request "${safeDisplay(msg.method)}".`
|
|
304
|
+
}
|
|
305
|
+
});
|
|
263
306
|
}
|
|
264
307
|
catch { /* ignore */ }
|
|
265
308
|
return;
|
|
@@ -317,16 +360,43 @@ export class LspSession {
|
|
|
317
360
|
...(params !== undefined ? { params } : {})
|
|
318
361
|
});
|
|
319
362
|
}
|
|
320
|
-
sendRequest(method, params, timeoutMs = this.timeoutMs) {
|
|
363
|
+
sendRequest(method, params, timeoutMs = this.timeoutMs, signal) {
|
|
321
364
|
this.lastUsedAt = Date.now();
|
|
365
|
+
if (signal?.aborted) {
|
|
366
|
+
return Promise.reject(new Error(`LSP request "${method}" aborted.`));
|
|
367
|
+
}
|
|
322
368
|
const id = this.nextId++;
|
|
323
369
|
return new Promise((resolve, reject) => {
|
|
370
|
+
const cleanup = () => {
|
|
371
|
+
signal?.removeEventListener('abort', onAbort);
|
|
372
|
+
};
|
|
373
|
+
const sendCancel = () => {
|
|
374
|
+
try {
|
|
375
|
+
this.sendNotification('$/cancelRequest', { id });
|
|
376
|
+
}
|
|
377
|
+
catch { /* ignore */ }
|
|
378
|
+
};
|
|
379
|
+
const onAbort = () => {
|
|
380
|
+
if (!this.pending.has(id))
|
|
381
|
+
return;
|
|
382
|
+
this.pending.delete(id);
|
|
383
|
+
clearTimeout(timer);
|
|
384
|
+
sendCancel();
|
|
385
|
+
reject(new Error(`LSP request "${method}" aborted.`));
|
|
386
|
+
};
|
|
324
387
|
const timer = setTimeout(() => {
|
|
325
388
|
this.pending.delete(id);
|
|
389
|
+
cleanup();
|
|
390
|
+
sendCancel();
|
|
326
391
|
reject(new Error(`LSP request "${method}" timed out after ${timeoutMs}ms.`));
|
|
327
392
|
}, timeoutMs);
|
|
328
393
|
timer.unref?.();
|
|
329
|
-
|
|
394
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
395
|
+
this.pending.set(id, {
|
|
396
|
+
resolve: (val) => { cleanup(); resolve(val); },
|
|
397
|
+
reject: (err) => { cleanup(); reject(err); },
|
|
398
|
+
timer
|
|
399
|
+
});
|
|
330
400
|
try {
|
|
331
401
|
this.writeFrame({
|
|
332
402
|
jsonrpc: '2.0',
|
|
@@ -338,6 +408,7 @@ export class LspSession {
|
|
|
338
408
|
catch (err) {
|
|
339
409
|
this.pending.delete(id);
|
|
340
410
|
clearTimeout(timer);
|
|
411
|
+
cleanup();
|
|
341
412
|
reject(err instanceof Error ? err : new Error(String(err)));
|
|
342
413
|
}
|
|
343
414
|
});
|
package/src/lsp/lspClient.ts
CHANGED
|
@@ -205,6 +205,7 @@ export class LspSession {
|
|
|
205
205
|
private diagnosticWaiters = new Map<string, Array<() => void>>();
|
|
206
206
|
private initPromise: Promise<void> | null = null;
|
|
207
207
|
private closed = false;
|
|
208
|
+
private stderrTail = '';
|
|
208
209
|
lastUsedAt = Date.now();
|
|
209
210
|
serverCapabilities: Record<string, unknown> = {};
|
|
210
211
|
|
|
@@ -238,7 +239,12 @@ export class LspSession {
|
|
|
238
239
|
this.proc = proc;
|
|
239
240
|
|
|
240
241
|
proc.stdin.on('error', () => { /* ignore broken pipe during shutdown */ });
|
|
241
|
-
proc.stderr.on('data', () => {
|
|
242
|
+
proc.stderr.on('data', (chunk: Buffer) => {
|
|
243
|
+
const text = chunk.toString('utf8');
|
|
244
|
+
if (text) {
|
|
245
|
+
this.stderrTail = (this.stderrTail + text).slice(-16 * 1024);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
242
248
|
|
|
243
249
|
proc.stdout.on('data', (chunk: Buffer) => {
|
|
244
250
|
try {
|
|
@@ -252,25 +258,30 @@ export class LspSession {
|
|
|
252
258
|
});
|
|
253
259
|
|
|
254
260
|
proc.on('error', (err: Error) => {
|
|
255
|
-
this.
|
|
261
|
+
const tailNote = this.stderrTail.trim() ? ` | stderr: ${safeDisplay(this.stderrTail.trim()).slice(-400)}` : '';
|
|
262
|
+
this.rejectAllPending(new Error(`LSP server "${this.serverId}" process error: ${err.message}${tailNote}`));
|
|
256
263
|
this.proc = null;
|
|
257
264
|
this.initPromise = null;
|
|
258
265
|
});
|
|
259
266
|
|
|
260
267
|
proc.on('exit', (code: number | null) => {
|
|
261
|
-
this.
|
|
268
|
+
const tailNote = this.stderrTail.trim() ? ` | stderr: ${safeDisplay(this.stderrTail.trim()).slice(-400)}` : '';
|
|
269
|
+
this.rejectAllPending(new Error(`LSP server "${this.serverId}" exited (code ${String(code)})${tailNote}.`));
|
|
262
270
|
this.proc = null;
|
|
263
271
|
this.initPromise = null;
|
|
264
272
|
});
|
|
265
273
|
|
|
266
274
|
const rootUri = filePathToUri(this.workspaceRoot);
|
|
267
275
|
const initResult = await this.sendRequest('initialize', {
|
|
268
|
-
processId:
|
|
276
|
+
processId: null,
|
|
269
277
|
clientInfo: { name: 'causyne-agent', version: '1.0' },
|
|
270
278
|
rootUri,
|
|
271
279
|
rootPath: this.workspaceRoot,
|
|
272
280
|
workspaceFolders: [{ uri: rootUri, name: path.basename(this.workspaceRoot) }],
|
|
273
281
|
capabilities: {
|
|
282
|
+
general: {
|
|
283
|
+
positionEncodings: ['utf-16']
|
|
284
|
+
},
|
|
274
285
|
textDocument: {
|
|
275
286
|
synchronization: { didSave: true },
|
|
276
287
|
definition: { linkSupport: true },
|
|
@@ -292,6 +303,10 @@ export class LspSession {
|
|
|
292
303
|
}) as { capabilities?: Record<string, unknown> } | null;
|
|
293
304
|
|
|
294
305
|
if (initResult && typeof initResult.capabilities === 'object' && initResult.capabilities !== null) {
|
|
306
|
+
const posEnc = initResult.capabilities.positionEncoding;
|
|
307
|
+
if (typeof posEnc === 'string' && posEnc.toLowerCase() !== 'utf-16') {
|
|
308
|
+
throw new Error(`LSP server "${this.serverId}" negotiated unsupported positionEncoding "${safeDisplay(posEnc)}" (expected utf-16).`);
|
|
309
|
+
}
|
|
295
310
|
this.serverCapabilities = initResult.capabilities;
|
|
296
311
|
}
|
|
297
312
|
this.sendNotification('initialized', {});
|
|
@@ -320,19 +335,43 @@ export class LspSession {
|
|
|
320
335
|
return;
|
|
321
336
|
}
|
|
322
337
|
|
|
323
|
-
// Server -> client request
|
|
338
|
+
// Server -> client request: answer safe read-only lifecycle queries accurately,
|
|
339
|
+
// and explicitly reject mutating or unsupported requests (e.g. workspace/applyEdit).
|
|
324
340
|
if (msg.id !== undefined && msg.id !== null && typeof msg.method === 'string') {
|
|
325
|
-
let result: unknown = null;
|
|
326
341
|
if (msg.method === 'workspace/configuration') {
|
|
327
342
|
const items = (msg.params && typeof msg.params === 'object' && Array.isArray((msg.params as { items?: unknown }).items))
|
|
328
343
|
? (msg.params as { items: unknown[] }).items
|
|
329
344
|
: [];
|
|
330
|
-
result
|
|
331
|
-
|
|
332
|
-
|
|
345
|
+
try { this.writeFrame({ jsonrpc: '2.0', id: msg.id, result: items.map(() => null) }); } catch { /* ignore */ }
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (msg.method === 'workspace/workspaceFolders') {
|
|
349
|
+
try {
|
|
350
|
+
this.writeFrame({
|
|
351
|
+
jsonrpc: '2.0',
|
|
352
|
+
id: msg.id,
|
|
353
|
+
result: [{ uri: filePathToUri(this.workspaceRoot), name: path.basename(this.workspaceRoot) }]
|
|
354
|
+
});
|
|
355
|
+
} catch { /* ignore */ }
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (
|
|
359
|
+
msg.method === 'window/workDoneProgress/create'
|
|
360
|
+
|| msg.method === 'client/registerCapability'
|
|
361
|
+
|| msg.method === 'client/unregisterCapability'
|
|
362
|
+
) {
|
|
363
|
+
try { this.writeFrame({ jsonrpc: '2.0', id: msg.id, result: null }); } catch { /* ignore */ }
|
|
364
|
+
return;
|
|
333
365
|
}
|
|
334
366
|
try {
|
|
335
|
-
this.writeFrame({
|
|
367
|
+
this.writeFrame({
|
|
368
|
+
jsonrpc: '2.0',
|
|
369
|
+
id: msg.id,
|
|
370
|
+
error: {
|
|
371
|
+
code: -32601,
|
|
372
|
+
message: `Client does not support server request "${safeDisplay(msg.method)}".`
|
|
373
|
+
}
|
|
374
|
+
});
|
|
336
375
|
} catch { /* ignore */ }
|
|
337
376
|
return;
|
|
338
377
|
}
|
|
@@ -396,16 +435,39 @@ export class LspSession {
|
|
|
396
435
|
});
|
|
397
436
|
}
|
|
398
437
|
|
|
399
|
-
sendRequest(method: string, params?: unknown, timeoutMs = this.timeoutMs): Promise<unknown> {
|
|
438
|
+
sendRequest(method: string, params?: unknown, timeoutMs = this.timeoutMs, signal?: AbortSignal): Promise<unknown> {
|
|
400
439
|
this.lastUsedAt = Date.now();
|
|
440
|
+
if (signal?.aborted) {
|
|
441
|
+
return Promise.reject(new Error(`LSP request "${method}" aborted.`));
|
|
442
|
+
}
|
|
401
443
|
const id = this.nextId++;
|
|
402
444
|
return new Promise((resolve, reject) => {
|
|
445
|
+
const cleanup = (): void => {
|
|
446
|
+
signal?.removeEventListener('abort', onAbort);
|
|
447
|
+
};
|
|
448
|
+
const sendCancel = (): void => {
|
|
449
|
+
try { this.sendNotification('$/cancelRequest', { id }); } catch { /* ignore */ }
|
|
450
|
+
};
|
|
451
|
+
const onAbort = (): void => {
|
|
452
|
+
if (!this.pending.has(id)) return;
|
|
453
|
+
this.pending.delete(id);
|
|
454
|
+
clearTimeout(timer);
|
|
455
|
+
sendCancel();
|
|
456
|
+
reject(new Error(`LSP request "${method}" aborted.`));
|
|
457
|
+
};
|
|
403
458
|
const timer = setTimeout(() => {
|
|
404
459
|
this.pending.delete(id);
|
|
460
|
+
cleanup();
|
|
461
|
+
sendCancel();
|
|
405
462
|
reject(new Error(`LSP request "${method}" timed out after ${timeoutMs}ms.`));
|
|
406
463
|
}, timeoutMs);
|
|
407
464
|
timer.unref?.();
|
|
408
|
-
|
|
465
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
466
|
+
this.pending.set(id, {
|
|
467
|
+
resolve: (val) => { cleanup(); resolve(val); },
|
|
468
|
+
reject: (err) => { cleanup(); reject(err); },
|
|
469
|
+
timer
|
|
470
|
+
});
|
|
409
471
|
try {
|
|
410
472
|
this.writeFrame({
|
|
411
473
|
jsonrpc: '2.0',
|
|
@@ -416,6 +478,7 @@ export class LspSession {
|
|
|
416
478
|
} catch (err: unknown) {
|
|
417
479
|
this.pending.delete(id);
|
|
418
480
|
clearTimeout(timer);
|
|
481
|
+
cleanup();
|
|
419
482
|
reject(err instanceof Error ? err : new Error(String(err)));
|
|
420
483
|
}
|
|
421
484
|
});
|