@ai-hero/sandcastle 0.6.5 → 0.6.6
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 +123 -54
- package/dist/{MountConfig.d.ts → MountConfig-CmXclHA5.d.ts} +3 -2
- package/dist/{SandboxProvider.d.ts → SandboxProvider-EkSMuBp8.d.ts} +25 -39
- package/dist/chunk-72UVAC7B.js +99 -0
- package/dist/chunk-72UVAC7B.js.map +1 -0
- package/dist/chunk-BIWNFKGV.js +22 -0
- package/dist/chunk-BIWNFKGV.js.map +1 -0
- package/dist/chunk-NGBM7T3E.js +76 -0
- package/dist/chunk-NGBM7T3E.js.map +1 -0
- package/dist/chunk-Q5W3WQVU.js +25569 -0
- package/dist/chunk-Q5W3WQVU.js.map +1 -0
- package/dist/chunk-UPDEQ2U7.js +362 -0
- package/dist/chunk-UPDEQ2U7.js.map +1 -0
- package/dist/chunk-Z7O2WNRU.js +26934 -0
- package/dist/chunk-Z7O2WNRU.js.map +1 -0
- package/dist/index.d.ts +920 -22
- package/dist/index.js +3212 -9
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts +0 -2
- package/dist/main.js +19256 -13
- package/dist/main.js.map +1 -1
- package/dist/mountUtils-CCA-bbpK.d.ts +25 -0
- package/dist/sandboxes/daytona.d.ts +8 -5
- package/dist/sandboxes/daytona.js +118 -124
- package/dist/sandboxes/daytona.js.map +1 -1
- package/dist/sandboxes/docker.d.ts +10 -8
- package/dist/sandboxes/docker.js +8 -255
- package/dist/sandboxes/docker.js.map +1 -1
- package/dist/sandboxes/no-sandbox.d.ts +7 -4
- package/dist/sandboxes/no-sandbox.js +6 -114
- package/dist/sandboxes/no-sandbox.js.map +1 -1
- package/dist/sandboxes/podman.d.ts +10 -8
- package/dist/sandboxes/podman.js +287 -297
- package/dist/sandboxes/podman.js.map +1 -1
- package/dist/sandboxes/vercel.d.ts +7 -4
- package/dist/sandboxes/vercel.js +144 -165
- package/dist/sandboxes/vercel.js.map +1 -1
- package/package.json +15 -14
- package/dist/AgentProvider.d.ts +0 -134
- package/dist/AgentProvider.d.ts.map +0 -1
- package/dist/AgentProvider.js +0 -647
- package/dist/AgentProvider.js.map +0 -1
- package/dist/AgentStreamEmitter.d.ts +0 -36
- package/dist/AgentStreamEmitter.d.ts.map +0 -1
- package/dist/AgentStreamEmitter.js +0 -21
- package/dist/AgentStreamEmitter.js.map +0 -1
- package/dist/CopyToWorktree.d.ts +0 -15
- package/dist/CopyToWorktree.d.ts.map +0 -1
- package/dist/CopyToWorktree.js +0 -60
- package/dist/CopyToWorktree.js.map +0 -1
- package/dist/Display.d.ts +0 -58
- package/dist/Display.d.ts.map +0 -1
- package/dist/Display.js +0 -142
- package/dist/Display.js.map +0 -1
- package/dist/DockerLifecycle.d.ts +0 -54
- package/dist/DockerLifecycle.d.ts.map +0 -1
- package/dist/DockerLifecycle.js +0 -123
- package/dist/DockerLifecycle.js.map +0 -1
- package/dist/EnvResolver.d.ts +0 -11
- package/dist/EnvResolver.d.ts.map +0 -1
- package/dist/EnvResolver.js +0 -63
- package/dist/EnvResolver.js.map +0 -1
- package/dist/ErrorHandler.d.ts +0 -15
- package/dist/ErrorHandler.d.ts.map +0 -1
- package/dist/ErrorHandler.js +0 -85
- package/dist/ErrorHandler.js.map +0 -1
- package/dist/InitService.d.ts +0 -92
- package/dist/InitService.d.ts.map +0 -1
- package/dist/InitService.js +0 -836
- package/dist/InitService.js.map +0 -1
- package/dist/MountConfig.d.ts.map +0 -1
- package/dist/MountConfig.js +0 -7
- package/dist/MountConfig.js.map +0 -1
- package/dist/Orchestrator.d.ts +0 -56
- package/dist/Orchestrator.d.ts.map +0 -1
- package/dist/Orchestrator.js +0 -293
- package/dist/Orchestrator.js.map +0 -1
- package/dist/Output.d.ts +0 -107
- package/dist/Output.d.ts.map +0 -1
- package/dist/Output.js +0 -95
- package/dist/Output.js.map +0 -1
- package/dist/PodmanLifecycle.d.ts +0 -17
- package/dist/PodmanLifecycle.d.ts.map +0 -1
- package/dist/PodmanLifecycle.js +0 -45
- package/dist/PodmanLifecycle.js.map +0 -1
- package/dist/PromptArgumentSubstitution.d.ts +0 -32
- package/dist/PromptArgumentSubstitution.d.ts.map +0 -1
- package/dist/PromptArgumentSubstitution.js +0 -104
- package/dist/PromptArgumentSubstitution.js.map +0 -1
- package/dist/PromptPreprocessor.d.ts +0 -15
- package/dist/PromptPreprocessor.d.ts.map +0 -1
- package/dist/PromptPreprocessor.js +0 -55
- package/dist/PromptPreprocessor.js.map +0 -1
- package/dist/PromptResolver.d.ts +0 -21
- package/dist/PromptResolver.d.ts.map +0 -1
- package/dist/PromptResolver.js +0 -27
- package/dist/PromptResolver.js.map +0 -1
- package/dist/RecoveryMessage.d.ts +0 -15
- package/dist/RecoveryMessage.d.ts.map +0 -1
- package/dist/RecoveryMessage.js +0 -81
- package/dist/RecoveryMessage.js.map +0 -1
- package/dist/SandboxFactory.d.ts +0 -90
- package/dist/SandboxFactory.d.ts.map +0 -1
- package/dist/SandboxFactory.js +0 -324
- package/dist/SandboxFactory.js.map +0 -1
- package/dist/SandboxLifecycle.d.ts +0 -65
- package/dist/SandboxLifecycle.d.ts.map +0 -1
- package/dist/SandboxLifecycle.js +0 -296
- package/dist/SandboxLifecycle.js.map +0 -1
- package/dist/SandboxProvider.d.ts.map +0 -1
- package/dist/SandboxProvider.js +0 -28
- package/dist/SandboxProvider.js.map +0 -1
- package/dist/SessionStore.d.ts +0 -110
- package/dist/SessionStore.d.ts.map +0 -1
- package/dist/SessionStore.js +0 -330
- package/dist/SessionStore.js.map +0 -1
- package/dist/TextDeltaBuffer.d.ts +0 -24
- package/dist/TextDeltaBuffer.d.ts.map +0 -1
- package/dist/TextDeltaBuffer.js +0 -68
- package/dist/TextDeltaBuffer.js.map +0 -1
- package/dist/WorktreeManager.d.ts +0 -79
- package/dist/WorktreeManager.d.ts.map +0 -1
- package/dist/WorktreeManager.js +0 -283
- package/dist/WorktreeManager.js.map +0 -1
- package/dist/boundedTail.d.ts +0 -48
- package/dist/boundedTail.d.ts.map +0 -1
- package/dist/boundedTail.js +0 -64
- package/dist/boundedTail.js.map +0 -1
- package/dist/cli.d.ts +0 -30
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js +0 -340
- package/dist/cli.js.map +0 -1
- package/dist/createSandbox.d.ts +0 -154
- package/dist/createSandbox.d.ts.map +0 -1
- package/dist/createSandbox.js +0 -476
- package/dist/createSandbox.js.map +0 -1
- package/dist/createWorktree.d.ts +0 -154
- package/dist/createWorktree.d.ts.map +0 -1
- package/dist/createWorktree.js +0 -391
- package/dist/createWorktree.js.map +0 -1
- package/dist/errors.d.ts +0 -227
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -81
- package/dist/errors.js.map +0 -1
- package/dist/extractStructuredOutput.d.ts +0 -23
- package/dist/extractStructuredOutput.d.ts.map +0 -1
- package/dist/extractStructuredOutput.js +0 -102
- package/dist/extractStructuredOutput.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/interactive.d.ts +0 -74
- package/dist/interactive.d.ts.map +0 -1
- package/dist/interactive.js +0 -279
- package/dist/interactive.js.map +0 -1
- package/dist/main.d.ts.map +0 -1
- package/dist/mergeProviderEnv.d.ts +0 -13
- package/dist/mergeProviderEnv.d.ts.map +0 -1
- package/dist/mergeProviderEnv.js +0 -23
- package/dist/mergeProviderEnv.js.map +0 -1
- package/dist/mountUtils.d.ts +0 -146
- package/dist/mountUtils.d.ts.map +0 -1
- package/dist/mountUtils.js +0 -301
- package/dist/mountUtils.js.map +0 -1
- package/dist/raceAbortSignal.d.ts +0 -18
- package/dist/raceAbortSignal.d.ts.map +0 -1
- package/dist/raceAbortSignal.js +0 -32
- package/dist/raceAbortSignal.js.map +0 -1
- package/dist/resolveCwd.d.ts +0 -24
- package/dist/resolveCwd.d.ts.map +0 -1
- package/dist/resolveCwd.js +0 -32
- package/dist/resolveCwd.js.map +0 -1
- package/dist/resumePrecheck.d.ts +0 -26
- package/dist/resumePrecheck.d.ts.map +0 -1
- package/dist/resumePrecheck.js +0 -40
- package/dist/resumePrecheck.js.map +0 -1
- package/dist/run.d.ts +0 -216
- package/dist/run.d.ts.map +0 -1
- package/dist/run.js +0 -313
- package/dist/run.js.map +0 -1
- package/dist/sandboxExec.d.ts +0 -12
- package/dist/sandboxExec.d.ts.map +0 -1
- package/dist/sandboxExec.js +0 -26
- package/dist/sandboxExec.js.map +0 -1
- package/dist/sandboxes/daytona.d.ts.map +0 -1
- package/dist/sandboxes/docker.d.ts.map +0 -1
- package/dist/sandboxes/no-sandbox.d.ts.map +0 -1
- package/dist/sandboxes/podman.d.ts.map +0 -1
- package/dist/sandboxes/test-bind-mount.d.ts +0 -17
- package/dist/sandboxes/test-bind-mount.d.ts.map +0 -1
- package/dist/sandboxes/test-bind-mount.js +0 -92
- package/dist/sandboxes/test-bind-mount.js.map +0 -1
- package/dist/sandboxes/test-isolated.d.ts +0 -17
- package/dist/sandboxes/test-isolated.d.ts.map +0 -1
- package/dist/sandboxes/test-isolated.js +0 -98
- package/dist/sandboxes/test-isolated.js.map +0 -1
- package/dist/sandboxes/vercel.d.ts.map +0 -1
- package/dist/shutdownRegistry.d.ts +0 -30
- package/dist/shutdownRegistry.d.ts.map +0 -1
- package/dist/shutdownRegistry.js +0 -73
- package/dist/shutdownRegistry.js.map +0 -1
- package/dist/startSandbox.d.ts +0 -50
- package/dist/startSandbox.d.ts.map +0 -1
- package/dist/startSandbox.js +0 -117
- package/dist/startSandbox.js.map +0 -1
- package/dist/syncIn.d.ts +0 -24
- package/dist/syncIn.d.ts.map +0 -1
- package/dist/syncIn.js +0 -107
- package/dist/syncIn.js.map +0 -1
- package/dist/syncOut.d.ts +0 -27
- package/dist/syncOut.d.ts.map +0 -1
- package/dist/syncOut.js +0 -271
- package/dist/syncOut.js.map +0 -1
- package/dist/templates.d.ts +0 -2
- package/dist/templates.d.ts.map +0 -1
- package/dist/templates.js +0 -26
- package/dist/templates.js.map +0 -1
- package/dist/terminalCleanup.d.ts +0 -30
- package/dist/terminalCleanup.d.ts.map +0 -1
- package/dist/terminalCleanup.js +0 -37
- package/dist/terminalCleanup.js.map +0 -1
- package/dist/testSandbox.d.ts +0 -8
- package/dist/testSandbox.d.ts.map +0 -1
- package/dist/testSandbox.js +0 -109
- package/dist/testSandbox.js.map +0 -1
- package/dist/testSetup.d.ts +0 -2
- package/dist/testSetup.d.ts.map +0 -1
- package/dist/testSetup.js +0 -29
- package/dist/testSetup.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,10 +1,3213 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { NodeContext_exports, NodeFileSystem_exports, formatErrorMessage } from './chunk-Q5W3WQVU.js';
|
|
3
|
+
import { Context_exports, CwdError, Effect_exports, resolveCwd, getCurrentBranch, generateTempBranchName, Layer_exports, FileDisplay, ClackDisplay, WorktreeDockerSandboxFactory, SandboxConfig, Display, pruneStale, create, copyToWorktree, runHostHooks, startSandbox, resolveGitMounts, SANDBOX_REPO_DIR, remove, makeSandboxFromHandle, syncOut, withSandboxLifecycle, hasUncommittedChanges, patchGitMountsForWindows, registerShutdown, SandboxFactory, PromptError, FileSystem_exports, SessionCaptureError, Clock_exports, Duration_exports, Option_exports, PromptExpansionTimeoutError, Deferred_exports, AgentError, Ref_exports, SilentDisplay, AgentIdleTimeoutError, Fiber_exports } from './chunk-Z7O2WNRU.js';
|
|
4
|
+
export { createBindMountSandboxProvider, createIsolatedSandboxProvider } from './chunk-BIWNFKGV.js';
|
|
5
|
+
import { noSandbox } from './chunk-72UVAC7B.js';
|
|
6
|
+
import './chunk-NGBM7T3E.js';
|
|
7
|
+
import path, { join, posix, dirname, relative } from 'path';
|
|
8
|
+
import { styleText } from 'util';
|
|
9
|
+
import * as clack from '@clack/prompts';
|
|
10
|
+
import { readdir, access, readFile, mkdir, writeFile, rm } from 'fs/promises';
|
|
11
|
+
import { tmpdir } from 'os';
|
|
12
|
+
|
|
13
|
+
createRequire(import.meta.url);
|
|
14
|
+
|
|
15
|
+
// src/resumePrecheck.ts
|
|
16
|
+
var assertResumeSessionExists = async (params) => {
|
|
17
|
+
const { provider, sandboxTag, hostRepoDir, resumeSession } = params;
|
|
18
|
+
if (!provider.sessionStorage) {
|
|
19
|
+
throw new Error(`${provider.name} does not support resumeSession`);
|
|
20
|
+
}
|
|
21
|
+
if (sandboxTag === "none") {
|
|
22
|
+
const found = await provider.sessionStorage.findByIdOnHost(resumeSession);
|
|
23
|
+
if (!found.path) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
`resumeSession "${resumeSession}" not found under ${found.searchedRoot}`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const exists = await provider.sessionStorage.existsOnHost(
|
|
31
|
+
hostRepoDir,
|
|
32
|
+
resumeSession
|
|
33
|
+
);
|
|
34
|
+
if (!exists) {
|
|
35
|
+
const sessionPath = provider.sessionStorage.hostSessionFilePath(
|
|
36
|
+
hostRepoDir,
|
|
37
|
+
resumeSession
|
|
38
|
+
);
|
|
39
|
+
throw new Error(
|
|
40
|
+
sessionPath ? `resumeSession "${resumeSession}" not found: expected session file at ${sessionPath}` : `resumeSession "${resumeSession}" not found`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/AgentStreamEmitter.ts
|
|
46
|
+
var AgentStreamEmitter = class extends Context_exports.Tag("AgentStreamEmitter")() {
|
|
47
|
+
};
|
|
48
|
+
var agentStreamEmitterLayer = (onEvent) => Layer_exports.succeed(AgentStreamEmitter, {
|
|
49
|
+
emit: onEvent ? (event) => Effect_exports.sync(() => {
|
|
50
|
+
try {
|
|
51
|
+
onEvent(event);
|
|
52
|
+
} catch {
|
|
53
|
+
}
|
|
54
|
+
}) : () => Effect_exports.void
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// src/PromptPreprocessor.ts
|
|
58
|
+
var PROMPT_EXPANSION_TIMEOUT_MS = 3e4;
|
|
59
|
+
var SHELL_BLOCK_MARKER = "";
|
|
60
|
+
var MARKED_SHELL_BLOCK_PATTERN = new RegExp(
|
|
61
|
+
`!${SHELL_BLOCK_MARKER}\`([^\`]+)\``,
|
|
62
|
+
"g"
|
|
63
|
+
);
|
|
64
|
+
var preprocessPrompt = (prompt, sandbox, cwd) => {
|
|
65
|
+
const matches = [...prompt.matchAll(MARKED_SHELL_BLOCK_PATTERN)];
|
|
66
|
+
if (matches.length === 0) {
|
|
67
|
+
return Effect_exports.succeed(prompt.replaceAll(SHELL_BLOCK_MARKER, ""));
|
|
68
|
+
}
|
|
69
|
+
return Effect_exports.gen(function* () {
|
|
70
|
+
const display = yield* Display;
|
|
71
|
+
return yield* display.taskLog(
|
|
72
|
+
"Expanding shell expressions",
|
|
73
|
+
(message) => Effect_exports.gen(function* () {
|
|
74
|
+
const results = yield* Effect_exports.all(
|
|
75
|
+
matches.map((match) => {
|
|
76
|
+
const command = match[1];
|
|
77
|
+
return Effect_exports.gen(function* () {
|
|
78
|
+
const start = yield* Clock_exports.currentTimeMillis;
|
|
79
|
+
const maybeResult = yield* sandbox.exec(command, { cwd }).pipe(
|
|
80
|
+
Effect_exports.timeoutOption(
|
|
81
|
+
Duration_exports.millis(PROMPT_EXPANSION_TIMEOUT_MS)
|
|
82
|
+
)
|
|
83
|
+
);
|
|
84
|
+
if (Option_exports.isNone(maybeResult)) {
|
|
85
|
+
const elapsedMs = (yield* Clock_exports.currentTimeMillis) - start;
|
|
86
|
+
return yield* Effect_exports.fail(
|
|
87
|
+
new PromptExpansionTimeoutError({
|
|
88
|
+
message: `Shell expression \`${command}\` timed out after ${elapsedMs}ms`,
|
|
89
|
+
timeoutMs: PROMPT_EXPANSION_TIMEOUT_MS,
|
|
90
|
+
expression: command,
|
|
91
|
+
elapsedMs
|
|
92
|
+
})
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
const execResult = maybeResult.value;
|
|
96
|
+
if (execResult.exitCode !== 0) {
|
|
97
|
+
return yield* Effect_exports.fail(
|
|
98
|
+
new PromptError({
|
|
99
|
+
message: `Command \`${command}\` exited with code ${execResult.exitCode}: ${execResult.stderr}`,
|
|
100
|
+
exitCode: execResult.exitCode
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
return execResult.stdout.trimEnd();
|
|
105
|
+
});
|
|
106
|
+
}),
|
|
107
|
+
{ concurrency: "unbounded" }
|
|
108
|
+
);
|
|
109
|
+
for (let i = 0; i < matches.length; i++) {
|
|
110
|
+
const command = matches[i][1];
|
|
111
|
+
const tokens = Math.ceil(results[i].length / 4);
|
|
112
|
+
message(`${command} \u2192 ~${tokens} tokens`);
|
|
113
|
+
}
|
|
114
|
+
let result = prompt;
|
|
115
|
+
for (let i = matches.length - 1; i >= 0; i--) {
|
|
116
|
+
const match = matches[i];
|
|
117
|
+
const index = match.index;
|
|
118
|
+
result = result.slice(0, index) + results[i] + result.slice(index + match[0].length);
|
|
119
|
+
}
|
|
120
|
+
return result.replaceAll(SHELL_BLOCK_MARKER, "");
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// src/TextDeltaBuffer.ts
|
|
127
|
+
var LENGTH_THRESHOLD = 80;
|
|
128
|
+
var DEBOUNCE_MS = 50;
|
|
129
|
+
var SENTENCE_BOUNDARY_RE = /[.!?] $/;
|
|
130
|
+
var TextDeltaBuffer = class {
|
|
131
|
+
buffer = "";
|
|
132
|
+
timer = null;
|
|
133
|
+
onFlush;
|
|
134
|
+
constructor(onFlush) {
|
|
135
|
+
this.onFlush = onFlush;
|
|
136
|
+
}
|
|
137
|
+
write(text2) {
|
|
138
|
+
if (text2.length === 0) return;
|
|
139
|
+
this.buffer += text2;
|
|
140
|
+
this.clearTimer();
|
|
141
|
+
if (this.shouldFlush()) {
|
|
142
|
+
this.doFlush();
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
this.timer = setTimeout(() => {
|
|
146
|
+
this.doFlush();
|
|
147
|
+
}, DEBOUNCE_MS);
|
|
148
|
+
}
|
|
149
|
+
/** Force-flush any buffered text. */
|
|
150
|
+
flush() {
|
|
151
|
+
this.clearTimer();
|
|
152
|
+
this.doFlush();
|
|
153
|
+
}
|
|
154
|
+
/** Flush remaining buffer and clean up. */
|
|
155
|
+
dispose() {
|
|
156
|
+
this.flush();
|
|
157
|
+
}
|
|
158
|
+
shouldFlush() {
|
|
159
|
+
if (this.buffer.includes("\n")) return true;
|
|
160
|
+
if (SENTENCE_BOUNDARY_RE.test(this.buffer)) return true;
|
|
161
|
+
if (this.buffer.length >= LENGTH_THRESHOLD) return true;
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
doFlush() {
|
|
165
|
+
if (this.buffer.length === 0) return;
|
|
166
|
+
const text2 = this.buffer;
|
|
167
|
+
this.buffer = "";
|
|
168
|
+
this.onFlush(text2);
|
|
169
|
+
}
|
|
170
|
+
clearTimer() {
|
|
171
|
+
if (this.timer !== null) {
|
|
172
|
+
clearTimeout(this.timer);
|
|
173
|
+
this.timer = null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// src/Orchestrator.ts
|
|
179
|
+
var IDLE_WARNING_INTERVAL_MS = 6e4;
|
|
180
|
+
var invokeAgent = (sandbox, sandboxRepoDir, prompt, provider, idleTimeoutMs, completionTimeoutMs, completionSignals, onText, onToolCall, onIdleWarning, onCompletionTimeout, idleWarningIntervalMs = IDLE_WARNING_INTERVAL_MS, resumeSession, forkSession, signal) => Effect_exports.gen(function* () {
|
|
181
|
+
let resultText = "";
|
|
182
|
+
let sessionId;
|
|
183
|
+
let usage;
|
|
184
|
+
let accumulatedOutput = "";
|
|
185
|
+
const timeoutSignal = yield* Deferred_exports.make();
|
|
186
|
+
const completionTimeoutDeferred = yield* Deferred_exports.make();
|
|
187
|
+
let timeoutFiber = null;
|
|
188
|
+
let completionDetected = false;
|
|
189
|
+
let warningFiber = null;
|
|
190
|
+
let idleMinuteCounter = 0;
|
|
191
|
+
const interruptFiber = (fiber) => {
|
|
192
|
+
if (fiber !== null) Effect_exports.runFork(Fiber_exports.interrupt(fiber));
|
|
193
|
+
};
|
|
194
|
+
const startWarningInterval = () => {
|
|
195
|
+
interruptFiber(warningFiber);
|
|
196
|
+
idleMinuteCounter = 0;
|
|
197
|
+
warningFiber = Effect_exports.runFork(
|
|
198
|
+
Effect_exports.gen(function* () {
|
|
199
|
+
while (true) {
|
|
200
|
+
yield* Effect_exports.sleep(Duration_exports.millis(idleWarningIntervalMs));
|
|
201
|
+
idleMinuteCounter++;
|
|
202
|
+
onIdleWarning(idleMinuteCounter);
|
|
203
|
+
}
|
|
204
|
+
})
|
|
205
|
+
);
|
|
206
|
+
};
|
|
207
|
+
const resetTimer = () => {
|
|
208
|
+
interruptFiber(timeoutFiber);
|
|
209
|
+
if (completionDetected) {
|
|
210
|
+
timeoutFiber = Effect_exports.runFork(
|
|
211
|
+
Effect_exports.gen(function* () {
|
|
212
|
+
yield* Effect_exports.sleep(Duration_exports.millis(completionTimeoutMs));
|
|
213
|
+
onCompletionTimeout(completionTimeoutMs);
|
|
214
|
+
yield* Deferred_exports.succeed(completionTimeoutDeferred, {
|
|
215
|
+
result: resultText || accumulatedOutput,
|
|
216
|
+
sessionId,
|
|
217
|
+
usage
|
|
218
|
+
});
|
|
219
|
+
})
|
|
220
|
+
);
|
|
221
|
+
} else {
|
|
222
|
+
timeoutFiber = Effect_exports.runFork(
|
|
223
|
+
Effect_exports.gen(function* () {
|
|
224
|
+
yield* Effect_exports.sleep(Duration_exports.millis(idleTimeoutMs));
|
|
225
|
+
yield* Deferred_exports.fail(
|
|
226
|
+
timeoutSignal,
|
|
227
|
+
new AgentIdleTimeoutError({
|
|
228
|
+
message: `Agent idle for ${idleTimeoutMs / 1e3} seconds \u2014 no output received. Consider increasing the idle timeout with --idle-timeout.`,
|
|
229
|
+
timeoutMs: idleTimeoutMs
|
|
230
|
+
})
|
|
231
|
+
);
|
|
232
|
+
})
|
|
233
|
+
);
|
|
234
|
+
startWarningInterval();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const abortDeferred = yield* Deferred_exports.make();
|
|
238
|
+
let abortCleanup = null;
|
|
239
|
+
if (signal) {
|
|
240
|
+
if (signal.aborted) {
|
|
241
|
+
return yield* Effect_exports.die(signal.reason);
|
|
242
|
+
}
|
|
243
|
+
const onAbort = () => {
|
|
244
|
+
Effect_exports.runFork(Deferred_exports.die(abortDeferred, signal.reason));
|
|
245
|
+
};
|
|
246
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
247
|
+
abortCleanup = () => signal.removeEventListener("abort", onAbort);
|
|
248
|
+
}
|
|
249
|
+
resetTimer();
|
|
250
|
+
const execEffect = Effect_exports.gen(function* () {
|
|
251
|
+
const printCmd = provider.buildPrintCommand({
|
|
252
|
+
prompt,
|
|
253
|
+
dangerouslySkipPermissions: true,
|
|
254
|
+
resumeSession,
|
|
255
|
+
forkSession
|
|
256
|
+
});
|
|
257
|
+
const execResult = yield* sandbox.exec(printCmd.command, {
|
|
258
|
+
onLine: (line) => {
|
|
259
|
+
for (const parsed of provider.parseStreamLine(line)) {
|
|
260
|
+
if (parsed.type === "text") {
|
|
261
|
+
onText(parsed.text);
|
|
262
|
+
accumulatedOutput += parsed.text;
|
|
263
|
+
} else if (parsed.type === "result") {
|
|
264
|
+
resultText = parsed.result;
|
|
265
|
+
accumulatedOutput += parsed.result;
|
|
266
|
+
} else if (parsed.type === "tool_call") {
|
|
267
|
+
onToolCall(parsed.name, parsed.args);
|
|
268
|
+
} else if (parsed.type === "session_id") {
|
|
269
|
+
sessionId = parsed.sessionId;
|
|
270
|
+
} else if (parsed.type === "usage") {
|
|
271
|
+
usage = parsed.usage;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (!completionDetected && completionSignals.some((sig) => accumulatedOutput.includes(sig))) {
|
|
275
|
+
completionDetected = true;
|
|
276
|
+
interruptFiber(warningFiber);
|
|
277
|
+
warningFiber = null;
|
|
278
|
+
}
|
|
279
|
+
resetTimer();
|
|
280
|
+
},
|
|
281
|
+
cwd: sandboxRepoDir,
|
|
282
|
+
stdin: printCmd.stdin
|
|
283
|
+
});
|
|
284
|
+
if (execResult.exitCode !== 0) {
|
|
285
|
+
let errorDetail = execResult.stderr;
|
|
286
|
+
if (!errorDetail.trim()) {
|
|
287
|
+
errorDetail = resultText;
|
|
288
|
+
}
|
|
289
|
+
if (!errorDetail.trim()) {
|
|
290
|
+
const lines = execResult.stdout.split("\n").filter((l) => l.trim());
|
|
291
|
+
errorDetail = lines.slice(-20).join("\n");
|
|
292
|
+
}
|
|
293
|
+
return yield* Effect_exports.fail(
|
|
294
|
+
new AgentError({
|
|
295
|
+
message: `${provider.name} exited with code ${execResult.exitCode}:
|
|
296
|
+
${errorDetail}`
|
|
297
|
+
})
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
return { result: resultText || execResult.stdout, sessionId, usage };
|
|
301
|
+
}).pipe(
|
|
302
|
+
Effect_exports.ensuring(
|
|
303
|
+
Effect_exports.sync(() => {
|
|
304
|
+
interruptFiber(timeoutFiber);
|
|
305
|
+
timeoutFiber = null;
|
|
306
|
+
interruptFiber(warningFiber);
|
|
307
|
+
warningFiber = null;
|
|
308
|
+
})
|
|
309
|
+
)
|
|
310
|
+
);
|
|
311
|
+
let raced = Effect_exports.raceFirst(execEffect, Deferred_exports.await(timeoutSignal));
|
|
312
|
+
raced = Effect_exports.raceFirst(raced, Deferred_exports.await(completionTimeoutDeferred));
|
|
313
|
+
if (signal) {
|
|
314
|
+
raced = Effect_exports.raceFirst(
|
|
315
|
+
raced,
|
|
316
|
+
Deferred_exports.await(abortDeferred)
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
return yield* raced.pipe(
|
|
320
|
+
Effect_exports.ensuring(
|
|
321
|
+
Effect_exports.sync(() => {
|
|
322
|
+
abortCleanup?.();
|
|
323
|
+
interruptFiber(timeoutFiber);
|
|
324
|
+
timeoutFiber = null;
|
|
325
|
+
interruptFiber(warningFiber);
|
|
326
|
+
warningFiber = null;
|
|
327
|
+
})
|
|
328
|
+
)
|
|
329
|
+
);
|
|
330
|
+
});
|
|
331
|
+
var DEFAULT_COMPLETION_SIGNAL = "<promise>COMPLETE</promise>";
|
|
332
|
+
var DEFAULT_IDLE_TIMEOUT_SECONDS = 10 * 60;
|
|
333
|
+
var DEFAULT_COMPLETION_TIMEOUT_SECONDS = 60;
|
|
334
|
+
var orchestrate = (options) => {
|
|
335
|
+
const idleTimeoutMs = (options.idleTimeoutSeconds ?? DEFAULT_IDLE_TIMEOUT_SECONDS) * 1e3;
|
|
336
|
+
const completionTimeoutMs = (options.completionTimeoutSeconds ?? DEFAULT_COMPLETION_TIMEOUT_SECONDS) * 1e3;
|
|
337
|
+
return Effect_exports.gen(function* () {
|
|
338
|
+
const factory = yield* SandboxFactory;
|
|
339
|
+
const display = yield* Display;
|
|
340
|
+
const streamEmitter = yield* AgentStreamEmitter;
|
|
341
|
+
const { hostRepoDir, iterations, hooks, prompt, branch, provider } = options;
|
|
342
|
+
let completionSignals;
|
|
343
|
+
if (options.completionSignal === void 0) {
|
|
344
|
+
completionSignals = [DEFAULT_COMPLETION_SIGNAL];
|
|
345
|
+
} else if (Array.isArray(options.completionSignal)) {
|
|
346
|
+
completionSignals = options.completionSignal;
|
|
347
|
+
} else {
|
|
348
|
+
completionSignals = [options.completionSignal];
|
|
349
|
+
}
|
|
350
|
+
const label = (msg) => options.name ? `[${options.name}] ${msg}` : msg;
|
|
351
|
+
const allCommits = [];
|
|
352
|
+
const allIterations = [];
|
|
353
|
+
let allStdout = "";
|
|
354
|
+
let resolvedBranch = "";
|
|
355
|
+
let iterationPreservedPath;
|
|
356
|
+
const checkAbort = () => options.signal?.aborted ? Effect_exports.die(options.signal.reason) : Effect_exports.void;
|
|
357
|
+
for (let i = 1; i <= iterations; i++) {
|
|
358
|
+
yield* checkAbort();
|
|
359
|
+
yield* display.status(label(`Iteration ${i}/${iterations}`), "info");
|
|
360
|
+
const sandboxResult = yield* factory.withSandbox(
|
|
361
|
+
({ hostWorktreePath, sandboxRepoPath, applyToHost, bindMountHandle }, sandbox) => withSandboxLifecycle(
|
|
362
|
+
{
|
|
363
|
+
hostRepoDir,
|
|
364
|
+
sandboxRepoDir: sandboxRepoPath,
|
|
365
|
+
hooks,
|
|
366
|
+
branch,
|
|
367
|
+
hostWorktreePath,
|
|
368
|
+
applyToHost,
|
|
369
|
+
signal: options.signal,
|
|
370
|
+
timeouts: options.timeouts
|
|
371
|
+
},
|
|
372
|
+
sandbox,
|
|
373
|
+
(ctx) => Effect_exports.gen(function* () {
|
|
374
|
+
const iterationResumeSession = i === 1 ? options.resumeSession : void 0;
|
|
375
|
+
const iterationForkSession = i === 1 ? options.forkSession : void 0;
|
|
376
|
+
if (iterationResumeSession && bindMountHandle && provider.sessionStorage) {
|
|
377
|
+
yield* display.status(label("Resuming session"), "info");
|
|
378
|
+
yield* Effect_exports.tryPromise({
|
|
379
|
+
try: () => provider.sessionStorage.resumeIntoSandbox({
|
|
380
|
+
hostCwd: hostRepoDir,
|
|
381
|
+
sandboxCwd: ctx.sandboxRepoDir,
|
|
382
|
+
sessionId: iterationResumeSession,
|
|
383
|
+
handle: bindMountHandle
|
|
384
|
+
}),
|
|
385
|
+
catch: (e) => new SessionCaptureError({
|
|
386
|
+
message: `Session resume failed: ${e instanceof Error ? e.message : String(e)}`,
|
|
387
|
+
sessionId: iterationResumeSession
|
|
388
|
+
})
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
const fullPrompt = options.skipPromptExpansion ? prompt : yield* preprocessPrompt(
|
|
392
|
+
prompt,
|
|
393
|
+
ctx.sandbox,
|
|
394
|
+
ctx.sandboxRepoDir
|
|
395
|
+
);
|
|
396
|
+
yield* display.status(label("Agent started"), "success");
|
|
397
|
+
const textBuffer = new TextDeltaBuffer((chunk) => {
|
|
398
|
+
Effect_exports.runPromise(display.text(chunk));
|
|
399
|
+
Effect_exports.runPromise(
|
|
400
|
+
streamEmitter.emit({
|
|
401
|
+
type: "text",
|
|
402
|
+
message: chunk,
|
|
403
|
+
iteration: i,
|
|
404
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
405
|
+
})
|
|
406
|
+
);
|
|
407
|
+
});
|
|
408
|
+
const onText = (text2) => {
|
|
409
|
+
textBuffer.write(text2);
|
|
410
|
+
};
|
|
411
|
+
const onToolCall = (name, formattedArgs) => {
|
|
412
|
+
textBuffer.flush();
|
|
413
|
+
Effect_exports.runPromise(display.toolCall(name, formattedArgs));
|
|
414
|
+
Effect_exports.runPromise(
|
|
415
|
+
streamEmitter.emit({
|
|
416
|
+
type: "toolCall",
|
|
417
|
+
name,
|
|
418
|
+
formattedArgs,
|
|
419
|
+
iteration: i,
|
|
420
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
421
|
+
})
|
|
422
|
+
);
|
|
423
|
+
};
|
|
424
|
+
const onIdleWarning = (minutes) => {
|
|
425
|
+
const msg = minutes === 1 ? "Agent idle for 1 minute" : `Agent idle for ${minutes} minutes`;
|
|
426
|
+
Effect_exports.runPromise(display.status(label(msg), "warn"));
|
|
427
|
+
};
|
|
428
|
+
const onCompletionTimeout = (timeoutMs) => {
|
|
429
|
+
Effect_exports.runPromise(
|
|
430
|
+
display.status(
|
|
431
|
+
label(
|
|
432
|
+
`Completion signal seen but agent process is hanging \u2014 force-completing after ${timeoutMs / 1e3}s grace window.`
|
|
433
|
+
),
|
|
434
|
+
"warn"
|
|
435
|
+
)
|
|
436
|
+
);
|
|
437
|
+
};
|
|
438
|
+
const {
|
|
439
|
+
result: agentOutput,
|
|
440
|
+
sessionId,
|
|
441
|
+
usage: streamUsage
|
|
442
|
+
} = yield* invokeAgent(
|
|
443
|
+
ctx.sandbox,
|
|
444
|
+
ctx.sandboxRepoDir,
|
|
445
|
+
fullPrompt,
|
|
446
|
+
provider,
|
|
447
|
+
idleTimeoutMs,
|
|
448
|
+
completionTimeoutMs,
|
|
449
|
+
completionSignals,
|
|
450
|
+
onText,
|
|
451
|
+
onToolCall,
|
|
452
|
+
onIdleWarning,
|
|
453
|
+
onCompletionTimeout,
|
|
454
|
+
options._idleWarningIntervalMs,
|
|
455
|
+
iterationResumeSession,
|
|
456
|
+
iterationForkSession,
|
|
457
|
+
options.signal
|
|
458
|
+
);
|
|
459
|
+
textBuffer.dispose();
|
|
460
|
+
yield* display.status(label("Agent stopped"), "info");
|
|
461
|
+
let sessionFilePath;
|
|
462
|
+
let usage = streamUsage;
|
|
463
|
+
if (provider.captureSessions && provider.sessionStorage && sessionId && bindMountHandle) {
|
|
464
|
+
yield* display.status(label("Capturing session"), "info");
|
|
465
|
+
yield* Effect_exports.tryPromise({
|
|
466
|
+
try: () => provider.sessionStorage.captureToHost({
|
|
467
|
+
hostCwd: hostRepoDir,
|
|
468
|
+
sandboxCwd: ctx.sandboxRepoDir,
|
|
469
|
+
sessionId,
|
|
470
|
+
handle: bindMountHandle
|
|
471
|
+
}),
|
|
472
|
+
catch: (e) => new SessionCaptureError({
|
|
473
|
+
message: `Session capture failed: ${e instanceof Error ? e.message : String(e)}`,
|
|
474
|
+
sessionId
|
|
475
|
+
})
|
|
476
|
+
});
|
|
477
|
+
sessionFilePath = provider.sessionStorage.hostSessionFilePath(
|
|
478
|
+
hostRepoDir,
|
|
479
|
+
sessionId
|
|
480
|
+
);
|
|
481
|
+
if (provider.parseSessionUsage) {
|
|
482
|
+
const content = yield* Effect_exports.promise(
|
|
483
|
+
() => provider.sessionStorage.readHostSession(hostRepoDir, sessionId).catch(() => void 0)
|
|
484
|
+
);
|
|
485
|
+
if (content) {
|
|
486
|
+
const parsedUsage = provider.parseSessionUsage(content);
|
|
487
|
+
if (parsedUsage) usage = parsedUsage;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
const matchedSignal = completionSignals.find(
|
|
492
|
+
(sig) => agentOutput.includes(sig)
|
|
493
|
+
);
|
|
494
|
+
return {
|
|
495
|
+
completionSignal: matchedSignal,
|
|
496
|
+
stdout: agentOutput,
|
|
497
|
+
sessionId,
|
|
498
|
+
sessionFilePath,
|
|
499
|
+
usage
|
|
500
|
+
};
|
|
501
|
+
})
|
|
502
|
+
)
|
|
503
|
+
);
|
|
504
|
+
const lifecycleResult = sandboxResult.value;
|
|
505
|
+
iterationPreservedPath = sandboxResult.preservedWorktreePath;
|
|
506
|
+
allCommits.push(...lifecycleResult.commits);
|
|
507
|
+
allStdout += lifecycleResult.result.stdout;
|
|
508
|
+
resolvedBranch = lifecycleResult.branch;
|
|
509
|
+
allIterations.push({
|
|
510
|
+
sessionId: lifecycleResult.result.sessionId,
|
|
511
|
+
sessionFilePath: lifecycleResult.result.sessionFilePath,
|
|
512
|
+
usage: lifecycleResult.result.usage
|
|
513
|
+
});
|
|
514
|
+
if (lifecycleResult.result.completionSignal !== void 0) {
|
|
515
|
+
yield* display.status(
|
|
516
|
+
label(`Agent signaled completion after ${i} iteration(s).`),
|
|
517
|
+
"success"
|
|
518
|
+
);
|
|
519
|
+
return {
|
|
520
|
+
iterations: allIterations,
|
|
521
|
+
completionSignal: lifecycleResult.result.completionSignal,
|
|
522
|
+
stdout: allStdout,
|
|
523
|
+
commits: allCommits,
|
|
524
|
+
branch: resolvedBranch,
|
|
525
|
+
preservedWorktreePath: iterationPreservedPath
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
yield* display.status(
|
|
530
|
+
label(`Reached max iterations (${iterations}).`),
|
|
531
|
+
"info"
|
|
532
|
+
);
|
|
533
|
+
return {
|
|
534
|
+
iterations: allIterations,
|
|
535
|
+
completionSignal: void 0,
|
|
536
|
+
stdout: allStdout,
|
|
537
|
+
commits: allCommits,
|
|
538
|
+
branch: resolvedBranch,
|
|
539
|
+
preservedWorktreePath: iterationPreservedPath
|
|
540
|
+
};
|
|
541
|
+
});
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
// src/PromptResolver.ts
|
|
545
|
+
var resolvePrompt = (options) => {
|
|
546
|
+
const { prompt, promptFile } = options;
|
|
547
|
+
if (prompt !== void 0 && promptFile !== void 0) {
|
|
548
|
+
return Effect_exports.fail(
|
|
549
|
+
new PromptError({
|
|
550
|
+
message: "Cannot provide both --prompt and --prompt-file"
|
|
551
|
+
})
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
if (prompt !== void 0) {
|
|
555
|
+
return Effect_exports.succeed({ text: prompt, source: "inline" });
|
|
556
|
+
}
|
|
557
|
+
if (promptFile === void 0) {
|
|
558
|
+
return Effect_exports.fail(
|
|
559
|
+
new PromptError({
|
|
560
|
+
message: "Must provide either prompt or promptFile. Pass prompt: '...' or promptFile: './.sandcastle/prompt.md' to run()."
|
|
561
|
+
})
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
return Effect_exports.gen(function* () {
|
|
565
|
+
const fs = yield* FileSystem_exports.FileSystem;
|
|
566
|
+
const text2 = yield* fs.readFileString(promptFile).pipe(
|
|
567
|
+
Effect_exports.catchAll(
|
|
568
|
+
(e) => Effect_exports.fail(
|
|
569
|
+
new PromptError({
|
|
570
|
+
message: `Failed to read prompt from ${promptFile}: ${e}`
|
|
571
|
+
})
|
|
572
|
+
)
|
|
573
|
+
)
|
|
574
|
+
);
|
|
575
|
+
return { text: text2, source: "template" };
|
|
576
|
+
});
|
|
577
|
+
};
|
|
578
|
+
var parseEnvFile = (filePath) => Effect_exports.gen(function* () {
|
|
579
|
+
const fs = yield* FileSystem_exports.FileSystem;
|
|
580
|
+
const content = yield* fs.readFileString(filePath).pipe(Effect_exports.catchAll(() => Effect_exports.succeed(null)));
|
|
581
|
+
if (content === null) return {};
|
|
582
|
+
const vars = {};
|
|
583
|
+
for (const line of content.split("\n")) {
|
|
584
|
+
const trimmed = line.trim();
|
|
585
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
586
|
+
const eqIndex = trimmed.indexOf("=");
|
|
587
|
+
if (eqIndex === -1) continue;
|
|
588
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
589
|
+
let value = trimmed.slice(eqIndex + 1).trim();
|
|
590
|
+
const isDoubleQuoted = value.length >= 2 && value[0] === '"' && value[value.length - 1] === '"';
|
|
591
|
+
const isSingleQuoted = value.length >= 2 && value[0] === "'" && value[value.length - 1] === "'";
|
|
592
|
+
if (isDoubleQuoted || isSingleQuoted) {
|
|
593
|
+
value = value.slice(1, -1);
|
|
594
|
+
}
|
|
595
|
+
if (isDoubleQuoted) {
|
|
596
|
+
value = value.replace(/\\([nrt\\])/g, (_, ch) => {
|
|
597
|
+
const escapes = {
|
|
598
|
+
n: "\n",
|
|
599
|
+
r: "\r",
|
|
600
|
+
t: " ",
|
|
601
|
+
"\\": "\\"
|
|
602
|
+
};
|
|
603
|
+
return escapes[ch] ?? ch;
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
vars[key] = value;
|
|
607
|
+
}
|
|
608
|
+
return vars;
|
|
609
|
+
});
|
|
610
|
+
var resolveEnv = (repoDir) => Effect_exports.gen(function* () {
|
|
611
|
+
const sandcastleEnv = yield* parseEnvFile(
|
|
612
|
+
join(repoDir, ".sandcastle", ".env")
|
|
613
|
+
);
|
|
614
|
+
const result = {};
|
|
615
|
+
for (const key of Object.keys(sandcastleEnv)) {
|
|
616
|
+
const value = sandcastleEnv[key] || process.env[key];
|
|
617
|
+
if (value) {
|
|
618
|
+
result[key] = value;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
return result;
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
// src/mergeProviderEnv.ts
|
|
625
|
+
var mergeProviderEnv = (options) => {
|
|
626
|
+
const { resolvedEnv, agentProviderEnv, sandboxProviderEnv } = options;
|
|
627
|
+
const agentKeys = Object.keys(agentProviderEnv);
|
|
628
|
+
const sandboxKeys = new Set(Object.keys(sandboxProviderEnv));
|
|
629
|
+
const overlapping = agentKeys.filter((k) => sandboxKeys.has(k));
|
|
630
|
+
if (overlapping.length > 0) {
|
|
631
|
+
throw new Error(
|
|
632
|
+
`Overlapping env keys between agent provider and sandbox provider: ${overlapping.join(", ")}`
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
return {
|
|
636
|
+
...resolvedEnv,
|
|
637
|
+
...sandboxProviderEnv,
|
|
638
|
+
...agentProviderEnv
|
|
639
|
+
};
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// src/PromptArgumentSubstitution.ts
|
|
643
|
+
var SHELL_BLOCK_PATTERN = /!`([^`]+)`/g;
|
|
644
|
+
var BUILT_IN_PROMPT_ARG_KEYS = [
|
|
645
|
+
"SOURCE_BRANCH",
|
|
646
|
+
"TARGET_BRANCH"
|
|
647
|
+
];
|
|
648
|
+
var PLACEHOLDER_PATTERN = /\{\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*\}\}/g;
|
|
649
|
+
var validateNoArgsWithInlinePrompt = (args) => {
|
|
650
|
+
if (Object.keys(args).length === 0) return Effect_exports.void;
|
|
651
|
+
return Effect_exports.fail(
|
|
652
|
+
new PromptError({
|
|
653
|
+
message: 'promptArgs is only supported with promptFile. Inline prompts (prompt: "...") are passed to the agent as-is \u2014 interpolate values directly in JavaScript, or switch to promptFile to use {{KEY}} substitution.'
|
|
654
|
+
})
|
|
655
|
+
);
|
|
656
|
+
};
|
|
657
|
+
var validateNoBuiltInArgOverride = (args) => {
|
|
658
|
+
for (const key of BUILT_IN_PROMPT_ARG_KEYS) {
|
|
659
|
+
if (key in args) {
|
|
660
|
+
return Effect_exports.fail(
|
|
661
|
+
new PromptError({
|
|
662
|
+
message: `"${key}" is a built-in prompt argument and cannot be overridden via promptArgs`
|
|
663
|
+
})
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return Effect_exports.void;
|
|
668
|
+
};
|
|
669
|
+
var findMissingPromptArgKeys = (prompt, providedArgs) => {
|
|
670
|
+
const matches = [...prompt.matchAll(PLACEHOLDER_PATTERN)];
|
|
671
|
+
const builtInSet = new Set(BUILT_IN_PROMPT_ARG_KEYS);
|
|
672
|
+
const seen = /* @__PURE__ */ new Set();
|
|
673
|
+
const missing = [];
|
|
674
|
+
for (const m of matches) {
|
|
675
|
+
const key = m[1];
|
|
676
|
+
if (seen.has(key)) continue;
|
|
677
|
+
seen.add(key);
|
|
678
|
+
if (builtInSet.has(key)) continue;
|
|
679
|
+
if (key in providedArgs) continue;
|
|
680
|
+
missing.push(key);
|
|
681
|
+
}
|
|
682
|
+
return missing;
|
|
683
|
+
};
|
|
684
|
+
var substitutePromptArgs = (prompt, args, silentKeys) => {
|
|
685
|
+
const markedPrompt = prompt.replaceAll(SHELL_BLOCK_MARKER, "").replace(SHELL_BLOCK_PATTERN, `!${SHELL_BLOCK_MARKER}\`$1\``);
|
|
686
|
+
const sanitizedArgs = Object.fromEntries(
|
|
687
|
+
Object.entries(args).map(([key, value]) => [
|
|
688
|
+
key,
|
|
689
|
+
typeof value === "string" ? value.replaceAll(SHELL_BLOCK_MARKER, "") : value
|
|
690
|
+
])
|
|
691
|
+
);
|
|
692
|
+
const matches = [...markedPrompt.matchAll(PLACEHOLDER_PATTERN)];
|
|
693
|
+
if (matches.length === 0 && Object.keys(sanitizedArgs).length === 0) {
|
|
694
|
+
return Effect_exports.succeed(markedPrompt);
|
|
695
|
+
}
|
|
696
|
+
return Effect_exports.gen(function* () {
|
|
697
|
+
const display = yield* Display;
|
|
698
|
+
const referencedKeys = new Set(matches.map((m) => m[1]));
|
|
699
|
+
for (const key of referencedKeys) {
|
|
700
|
+
if (!(key in sanitizedArgs)) {
|
|
701
|
+
return yield* Effect_exports.fail(
|
|
702
|
+
new PromptError({
|
|
703
|
+
message: `Prompt argument "{{${key}}}" has no matching value in promptArgs`
|
|
704
|
+
})
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
for (const key of Object.keys(sanitizedArgs)) {
|
|
709
|
+
if (!referencedKeys.has(key) && !silentKeys?.has(key)) {
|
|
710
|
+
yield* display.status(
|
|
711
|
+
`Prompt argument "${key}" was provided but not referenced in the prompt`,
|
|
712
|
+
"warn"
|
|
713
|
+
);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
const result = markedPrompt.replace(
|
|
717
|
+
PLACEHOLDER_PATTERN,
|
|
718
|
+
(_match, key) => sanitizedArgs[key].toString()
|
|
719
|
+
);
|
|
720
|
+
return result;
|
|
721
|
+
});
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
// src/Output.ts
|
|
725
|
+
var Output = {
|
|
726
|
+
/**
|
|
727
|
+
* Declare an object-typed structured output extracted from an XML tag in
|
|
728
|
+
* the agent's stdout. The tag contents are JSON-parsed (with fence-aware
|
|
729
|
+
* unwrapping) and validated against the provided Standard Schema validator.
|
|
730
|
+
*/
|
|
731
|
+
object: (opts) => ({
|
|
732
|
+
_tag: "object",
|
|
733
|
+
tag: opts.tag,
|
|
734
|
+
schema: opts.schema
|
|
735
|
+
}),
|
|
736
|
+
/**
|
|
737
|
+
* Declare a string-typed structured output extracted from an XML tag in
|
|
738
|
+
* the agent's stdout. The tag contents are whitespace-trimmed and returned
|
|
739
|
+
* as a plain string — no JSON parsing, no schema validation.
|
|
740
|
+
*/
|
|
741
|
+
string: (opts) => ({
|
|
742
|
+
_tag: "string",
|
|
743
|
+
tag: opts.tag
|
|
744
|
+
})
|
|
745
|
+
};
|
|
746
|
+
var StructuredOutputError = class extends Error {
|
|
747
|
+
tag;
|
|
748
|
+
rawMatched;
|
|
749
|
+
cause;
|
|
750
|
+
commits;
|
|
751
|
+
branch;
|
|
752
|
+
preservedWorktreePath;
|
|
753
|
+
/** Session ID of the iteration that produced the bad output, when available. */
|
|
754
|
+
sessionId;
|
|
755
|
+
/** Host path to the captured session JSONL, when the session was captured. */
|
|
756
|
+
sessionFilePath;
|
|
757
|
+
constructor(message, options) {
|
|
758
|
+
super(message);
|
|
759
|
+
this.name = "StructuredOutputError";
|
|
760
|
+
this.tag = options.tag;
|
|
761
|
+
this.rawMatched = options.rawMatched;
|
|
762
|
+
this.cause = options.cause;
|
|
763
|
+
this.commits = options.commits;
|
|
764
|
+
this.branch = options.branch;
|
|
765
|
+
this.preservedWorktreePath = options.preservedWorktreePath;
|
|
766
|
+
this.sessionId = options.sessionId;
|
|
767
|
+
this.sessionFilePath = options.sessionFilePath;
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
// src/extractStructuredOutput.ts
|
|
772
|
+
var extractStructuredOutput = async (stdout, definition, context) => {
|
|
773
|
+
if (definition._tag === "object") {
|
|
774
|
+
return extractObject(
|
|
775
|
+
stdout,
|
|
776
|
+
definition,
|
|
777
|
+
context
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
return extractString(stdout, definition, context);
|
|
781
|
+
};
|
|
782
|
+
var extractObject = async (stdout, definition, context) => {
|
|
783
|
+
const raw = findLastTagContent(stdout, definition.tag);
|
|
784
|
+
if (raw === void 0) {
|
|
785
|
+
throw new StructuredOutputError(
|
|
786
|
+
`Structured output tag <${definition.tag}> not found in agent output`,
|
|
787
|
+
{ tag: definition.tag, rawMatched: void 0, ...context }
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
const unwrapped = unwrapFences(raw.trim());
|
|
791
|
+
let parsed;
|
|
792
|
+
try {
|
|
793
|
+
parsed = JSON.parse(unwrapped);
|
|
794
|
+
} catch (cause) {
|
|
795
|
+
throw new StructuredOutputError(
|
|
796
|
+
`Structured output tag <${definition.tag}> contains invalid JSON`,
|
|
797
|
+
{ tag: definition.tag, rawMatched: raw, cause, ...context }
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
const result = await definition.schema["~standard"].validate(parsed);
|
|
801
|
+
if (result.issues) {
|
|
802
|
+
throw new StructuredOutputError(
|
|
803
|
+
`Structured output tag <${definition.tag}> failed schema validation`,
|
|
804
|
+
{
|
|
805
|
+
tag: definition.tag,
|
|
806
|
+
rawMatched: raw,
|
|
807
|
+
cause: result.issues,
|
|
808
|
+
...context
|
|
809
|
+
}
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
return result.value;
|
|
813
|
+
};
|
|
814
|
+
var extractString = (stdout, definition, context) => {
|
|
815
|
+
const raw = findLastTagContent(stdout, definition.tag);
|
|
816
|
+
if (raw === void 0) {
|
|
817
|
+
throw new StructuredOutputError(
|
|
818
|
+
`Structured output tag <${definition.tag}> not found in agent output`,
|
|
819
|
+
{ tag: definition.tag, rawMatched: void 0, ...context }
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
return raw.trim();
|
|
823
|
+
};
|
|
824
|
+
var findLastTagContent = (text2, tag) => {
|
|
825
|
+
const openTag = `<${tag}>`;
|
|
826
|
+
const closeTag = `</${tag}>`;
|
|
827
|
+
let lastContent;
|
|
828
|
+
let searchFrom = 0;
|
|
829
|
+
while (true) {
|
|
830
|
+
const openIdx = text2.indexOf(openTag, searchFrom);
|
|
831
|
+
if (openIdx === -1) break;
|
|
832
|
+
const contentStart = openIdx + openTag.length;
|
|
833
|
+
const closeIdx = text2.indexOf(closeTag, contentStart);
|
|
834
|
+
if (closeIdx === -1) break;
|
|
835
|
+
lastContent = text2.slice(contentStart, closeIdx);
|
|
836
|
+
searchFrom = closeIdx + closeTag.length;
|
|
837
|
+
}
|
|
838
|
+
return lastContent;
|
|
839
|
+
};
|
|
840
|
+
var unwrapFences = (text2) => {
|
|
841
|
+
const fenceMatch = text2.match(/^```(?:json)?\s*\n([\s\S]*?)\n\s*```\s*$/);
|
|
842
|
+
if (fenceMatch) {
|
|
843
|
+
return fenceMatch[1].trim();
|
|
844
|
+
}
|
|
845
|
+
return text2;
|
|
846
|
+
};
|
|
847
|
+
|
|
848
|
+
// src/run.ts
|
|
849
|
+
var DEFAULT_MAX_ITERATIONS = 1;
|
|
850
|
+
var sanitizeBranchForFilename = (branch) => branch.replace(/[/\\:*?"<>|]/g, "-");
|
|
851
|
+
var printFileDisplayStartup = (options) => {
|
|
852
|
+
const name = options.agentName ?? "Agent";
|
|
853
|
+
const label = styleText("bold", `[${name}]`);
|
|
854
|
+
const branchPart = options.branch ? ` on branch ${options.branch}` : "";
|
|
855
|
+
const hostRepoDir = options.hostRepoDir ?? process.cwd();
|
|
856
|
+
const displayLogPath = hostRepoDir === process.cwd() ? path.relative(process.cwd(), options.logPath) : options.logPath;
|
|
857
|
+
console.log(`${label} Started${branchPart}`);
|
|
858
|
+
console.log(styleText("dim", ` tail -f ${displayLogPath}`));
|
|
859
|
+
};
|
|
860
|
+
var buildLogFilename = (resolvedBranch, targetBranch, name) => {
|
|
861
|
+
const sanitized = sanitizeBranchForFilename(resolvedBranch);
|
|
862
|
+
const nameSuffix = name ? `-${name.toLowerCase().replace(/[^a-z0-9_.-]/g, "-")}` : "";
|
|
863
|
+
if (targetBranch) {
|
|
864
|
+
return `${sanitizeBranchForFilename(targetBranch)}-${sanitized}${nameSuffix}.log`;
|
|
865
|
+
}
|
|
866
|
+
return `${sanitized}${nameSuffix}.log`;
|
|
867
|
+
};
|
|
868
|
+
var buildRunSummaryRows = (options) => ({
|
|
869
|
+
Agent: options.name ?? options.agentName,
|
|
870
|
+
Sandbox: options.sandboxName,
|
|
871
|
+
"Max iterations": String(options.maxIterations),
|
|
872
|
+
Branch: options.branch
|
|
873
|
+
});
|
|
874
|
+
var buildCompletionMessage = (completionSignal, iterationsRun) => {
|
|
875
|
+
if (completionSignal !== void 0) {
|
|
876
|
+
return {
|
|
877
|
+
message: `Run complete: agent finished after ${iterationsRun} iteration(s).`,
|
|
878
|
+
severity: "success"
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
return {
|
|
882
|
+
message: `Run complete: reached ${iterationsRun} iteration(s) without completion signal.`,
|
|
883
|
+
severity: "warn"
|
|
884
|
+
};
|
|
885
|
+
};
|
|
886
|
+
var formatContextWindowSize = (usage) => {
|
|
887
|
+
const total = usage.inputTokens + usage.cacheCreationInputTokens + usage.cacheReadInputTokens;
|
|
888
|
+
return `${Math.ceil(total / 1e3)}k`;
|
|
889
|
+
};
|
|
890
|
+
var buildContextWindowLines = (iterations) => iterations.filter((it) => it.usage !== void 0).map((it) => `Context window: ${formatContextWindowSize(it.usage)}`);
|
|
891
|
+
async function run(options) {
|
|
892
|
+
options.signal?.throwIfAborted();
|
|
893
|
+
const {
|
|
894
|
+
prompt,
|
|
895
|
+
promptFile,
|
|
896
|
+
maxIterations = DEFAULT_MAX_ITERATIONS,
|
|
897
|
+
hooks,
|
|
898
|
+
agent: provider
|
|
899
|
+
} = options;
|
|
900
|
+
const branchStrategy = options.branchStrategy ?? (options.sandbox.tag === "isolated" ? { type: "merge-to-head" } : { type: "head" });
|
|
901
|
+
const effectiveBranchType = branchStrategy.type;
|
|
902
|
+
if (effectiveBranchType === "head" && options.sandbox.tag === "isolated") {
|
|
903
|
+
throw new Error(
|
|
904
|
+
"head branch strategy is not supported with isolated providers"
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
if (effectiveBranchType === "head" && options.copyToWorktree && options.copyToWorktree.length > 0) {
|
|
908
|
+
throw new Error(
|
|
909
|
+
"copyToWorktree is not supported with head branch strategy. In head mode the host working directory is bind-mounted directly."
|
|
910
|
+
);
|
|
911
|
+
}
|
|
912
|
+
if (options.resumeSession && maxIterations > 1) {
|
|
913
|
+
throw new Error(
|
|
914
|
+
"resumeSession cannot be combined with maxIterations > 1. Resume applies to iteration 1 only; multi-iteration resume semantics are not supported."
|
|
915
|
+
);
|
|
916
|
+
}
|
|
917
|
+
if (options.forkSession && !options.resumeSession) {
|
|
918
|
+
throw new Error(
|
|
919
|
+
"forkSession requires resumeSession. Use RunResult.fork(prompt) to fork the last captured session."
|
|
920
|
+
);
|
|
921
|
+
}
|
|
922
|
+
if (options.output && maxIterations !== 1) {
|
|
923
|
+
throw new Error(
|
|
924
|
+
"output requires maxIterations to be 1. Structured output is only supported for single-iteration runs."
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
const branch = branchStrategy.type === "branch" ? branchStrategy.branch : void 0;
|
|
928
|
+
const hostRepoDir = await Effect_exports.runPromise(
|
|
929
|
+
resolveCwd(options.cwd).pipe(Effect_exports.provide(NodeContext_exports.layer))
|
|
930
|
+
);
|
|
931
|
+
if (options.resumeSession) {
|
|
932
|
+
await assertResumeSessionExists({
|
|
933
|
+
provider,
|
|
934
|
+
sandboxTag: options.sandbox.tag,
|
|
935
|
+
hostRepoDir,
|
|
936
|
+
resumeSession: options.resumeSession
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
const resolved = await Effect_exports.runPromise(
|
|
940
|
+
resolvePrompt({ prompt, promptFile }).pipe(
|
|
941
|
+
Effect_exports.provide(NodeContext_exports.layer)
|
|
942
|
+
)
|
|
943
|
+
);
|
|
944
|
+
const rawPrompt = resolved.text;
|
|
945
|
+
const isInlinePrompt = resolved.source === "inline";
|
|
946
|
+
if (options.output) {
|
|
947
|
+
const openTag = `<${options.output.tag}>`;
|
|
948
|
+
if (!rawPrompt.includes(openTag)) {
|
|
949
|
+
throw new Error(
|
|
950
|
+
`output tag <${options.output.tag}> not found in the resolved prompt. The caller must instruct the agent to emit the configured tag.`
|
|
951
|
+
);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
const agentName = provider.name;
|
|
955
|
+
const resolvedEnv = await Effect_exports.runPromise(
|
|
956
|
+
resolveEnv(hostRepoDir).pipe(Effect_exports.provide(NodeContext_exports.layer))
|
|
957
|
+
);
|
|
958
|
+
const env = mergeProviderEnv({
|
|
959
|
+
resolvedEnv,
|
|
960
|
+
agentProviderEnv: provider.env,
|
|
961
|
+
sandboxProviderEnv: options.sandbox.env
|
|
962
|
+
});
|
|
963
|
+
const currentHostBranch = await Effect_exports.runPromise(
|
|
964
|
+
getCurrentBranch(hostRepoDir)
|
|
965
|
+
);
|
|
966
|
+
const resolvedBranch = effectiveBranchType === "head" ? currentHostBranch : branch ?? generateTempBranchName(options.name);
|
|
967
|
+
const targetBranch = effectiveBranchType === "merge-to-head" ? currentHostBranch : void 0;
|
|
968
|
+
const resolvedLogging = options.logging ?? {
|
|
969
|
+
type: "file",
|
|
970
|
+
path: join(
|
|
971
|
+
hostRepoDir,
|
|
972
|
+
".sandcastle",
|
|
973
|
+
"logs",
|
|
974
|
+
buildLogFilename(resolvedBranch, targetBranch, options.name)
|
|
975
|
+
)
|
|
976
|
+
};
|
|
977
|
+
const displayLayer = resolvedLogging.type === "file" ? (() => {
|
|
978
|
+
printFileDisplayStartup({
|
|
979
|
+
logPath: resolvedLogging.path,
|
|
980
|
+
agentName: options.name,
|
|
981
|
+
branch: resolvedBranch,
|
|
982
|
+
hostRepoDir
|
|
983
|
+
});
|
|
984
|
+
return Layer_exports.provide(
|
|
985
|
+
FileDisplay.layer(resolvedLogging.path),
|
|
986
|
+
NodeFileSystem_exports.layer
|
|
987
|
+
);
|
|
988
|
+
})() : ClackDisplay.layer;
|
|
989
|
+
const factoryLayer = Layer_exports.provide(
|
|
990
|
+
WorktreeDockerSandboxFactory.layer,
|
|
991
|
+
Layer_exports.mergeAll(
|
|
992
|
+
Layer_exports.succeed(SandboxConfig, {
|
|
993
|
+
env,
|
|
994
|
+
hostRepoDir,
|
|
995
|
+
copyToWorktree: options.copyToWorktree,
|
|
996
|
+
name: options.name,
|
|
997
|
+
sandboxProvider: options.sandbox,
|
|
998
|
+
branchStrategy,
|
|
999
|
+
hooks,
|
|
1000
|
+
signal: options.signal,
|
|
1001
|
+
timeouts: options.timeouts
|
|
1002
|
+
}),
|
|
1003
|
+
NodeFileSystem_exports.layer,
|
|
1004
|
+
displayLayer
|
|
1005
|
+
)
|
|
1006
|
+
);
|
|
1007
|
+
const streamEmitterLayer = agentStreamEmitterLayer(
|
|
1008
|
+
resolvedLogging.type === "file" ? resolvedLogging.onAgentStreamEvent : void 0
|
|
1009
|
+
);
|
|
1010
|
+
const runLayer = Layer_exports.mergeAll(
|
|
1011
|
+
factoryLayer,
|
|
1012
|
+
displayLayer,
|
|
1013
|
+
streamEmitterLayer
|
|
1014
|
+
);
|
|
1015
|
+
const baseEffect = Effect_exports.gen(function* () {
|
|
1016
|
+
const d = yield* Display;
|
|
1017
|
+
yield* d.intro(options.name ?? "sandcastle");
|
|
1018
|
+
const rows = buildRunSummaryRows({
|
|
1019
|
+
name: options.name,
|
|
1020
|
+
agentName,
|
|
1021
|
+
sandboxName: options.sandbox.name,
|
|
1022
|
+
maxIterations,
|
|
1023
|
+
branch: resolvedBranch
|
|
1024
|
+
});
|
|
1025
|
+
yield* d.summary("Sandcastle Run", rows);
|
|
1026
|
+
const userArgs = options.promptArgs ?? {};
|
|
1027
|
+
let resolvedPrompt;
|
|
1028
|
+
if (isInlinePrompt) {
|
|
1029
|
+
yield* validateNoArgsWithInlinePrompt(userArgs);
|
|
1030
|
+
resolvedPrompt = rawPrompt;
|
|
1031
|
+
} else {
|
|
1032
|
+
yield* validateNoBuiltInArgOverride(userArgs);
|
|
1033
|
+
const effectiveArgs = {
|
|
1034
|
+
SOURCE_BRANCH: resolvedBranch,
|
|
1035
|
+
TARGET_BRANCH: currentHostBranch,
|
|
1036
|
+
...userArgs
|
|
1037
|
+
};
|
|
1038
|
+
const builtInArgKeysSet = new Set(BUILT_IN_PROMPT_ARG_KEYS);
|
|
1039
|
+
resolvedPrompt = yield* substitutePromptArgs(
|
|
1040
|
+
rawPrompt,
|
|
1041
|
+
effectiveArgs,
|
|
1042
|
+
builtInArgKeysSet
|
|
1043
|
+
);
|
|
1044
|
+
}
|
|
1045
|
+
const orchestrateBranch = effectiveBranchType === "head" ? currentHostBranch : branch;
|
|
1046
|
+
const orchestrateResult = yield* orchestrate({
|
|
1047
|
+
hostRepoDir,
|
|
1048
|
+
iterations: maxIterations,
|
|
1049
|
+
hooks,
|
|
1050
|
+
prompt: resolvedPrompt,
|
|
1051
|
+
branch: orchestrateBranch,
|
|
1052
|
+
provider,
|
|
1053
|
+
completionSignal: options.completionSignal,
|
|
1054
|
+
idleTimeoutSeconds: options.idleTimeoutSeconds,
|
|
1055
|
+
completionTimeoutSeconds: options.completionTimeoutSeconds,
|
|
1056
|
+
name: options.name,
|
|
1057
|
+
resumeSession: options.resumeSession,
|
|
1058
|
+
forkSession: options.forkSession,
|
|
1059
|
+
signal: options.signal,
|
|
1060
|
+
skipPromptExpansion: isInlinePrompt,
|
|
1061
|
+
timeouts: options.timeouts
|
|
1062
|
+
});
|
|
1063
|
+
const completion = buildCompletionMessage(
|
|
1064
|
+
orchestrateResult.completionSignal,
|
|
1065
|
+
orchestrateResult.iterations.length
|
|
1066
|
+
);
|
|
1067
|
+
yield* d.status(completion.message, completion.severity);
|
|
1068
|
+
for (const line of buildContextWindowLines(orchestrateResult.iterations)) {
|
|
1069
|
+
yield* d.text(line);
|
|
1070
|
+
}
|
|
1071
|
+
return orchestrateResult;
|
|
1072
|
+
});
|
|
1073
|
+
const withErrorLog = resolvedLogging.type === "file" ? baseEffect.pipe(
|
|
1074
|
+
Effect_exports.tapError(
|
|
1075
|
+
(error) => Effect_exports.gen(function* () {
|
|
1076
|
+
const d = yield* Display;
|
|
1077
|
+
yield* d.status(
|
|
1078
|
+
formatErrorMessage(error),
|
|
1079
|
+
"error"
|
|
1080
|
+
);
|
|
1081
|
+
})
|
|
1082
|
+
)
|
|
1083
|
+
) : baseEffect;
|
|
1084
|
+
let result;
|
|
1085
|
+
try {
|
|
1086
|
+
result = await Effect_exports.runPromise(
|
|
1087
|
+
withErrorLog.pipe(Effect_exports.provide(runLayer))
|
|
1088
|
+
);
|
|
1089
|
+
} catch (error) {
|
|
1090
|
+
options.signal?.throwIfAborted();
|
|
1091
|
+
throw error;
|
|
1092
|
+
}
|
|
1093
|
+
const baseResult = {
|
|
1094
|
+
...result,
|
|
1095
|
+
logFilePath: resolvedLogging.type === "file" ? resolvedLogging.path : void 0,
|
|
1096
|
+
resume: async (prompt2, resumeOptions) => {
|
|
1097
|
+
const lastIteration = result.iterations.at(-1);
|
|
1098
|
+
if (!lastIteration?.sessionId) {
|
|
1099
|
+
throw new Error("Cannot resume: no sessionId was captured");
|
|
1100
|
+
}
|
|
1101
|
+
return run({
|
|
1102
|
+
...options,
|
|
1103
|
+
...resumeOptions,
|
|
1104
|
+
prompt: prompt2,
|
|
1105
|
+
promptFile: void 0,
|
|
1106
|
+
maxIterations: 1,
|
|
1107
|
+
resumeSession: lastIteration.sessionId
|
|
1108
|
+
});
|
|
1109
|
+
},
|
|
1110
|
+
fork: async (prompt2, forkOptions) => {
|
|
1111
|
+
const lastIteration = result.iterations.at(-1);
|
|
1112
|
+
if (!lastIteration?.sessionId) {
|
|
1113
|
+
throw new Error("Cannot fork: no sessionId was captured");
|
|
1114
|
+
}
|
|
1115
|
+
return run({
|
|
1116
|
+
...options,
|
|
1117
|
+
...forkOptions,
|
|
1118
|
+
prompt: prompt2,
|
|
1119
|
+
promptFile: void 0,
|
|
1120
|
+
maxIterations: 1,
|
|
1121
|
+
resumeSession: lastIteration.sessionId,
|
|
1122
|
+
forkSession: true
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
if (options.output) {
|
|
1127
|
+
const lastIteration = baseResult.iterations.at(-1);
|
|
1128
|
+
const output = await extractStructuredOutput(
|
|
1129
|
+
baseResult.stdout,
|
|
1130
|
+
options.output,
|
|
1131
|
+
{
|
|
1132
|
+
commits: baseResult.commits,
|
|
1133
|
+
branch: baseResult.branch,
|
|
1134
|
+
preservedWorktreePath: baseResult.preservedWorktreePath,
|
|
1135
|
+
sessionId: lastIteration?.sessionId,
|
|
1136
|
+
sessionFilePath: lastIteration?.sessionFilePath
|
|
1137
|
+
}
|
|
1138
|
+
);
|
|
1139
|
+
return { ...baseResult, output };
|
|
1140
|
+
}
|
|
1141
|
+
return baseResult;
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
// src/raceAbortSignal.ts
|
|
1145
|
+
var raceAbortSignal = (effect, signal) => {
|
|
1146
|
+
if (!signal) return effect;
|
|
1147
|
+
return Effect_exports.gen(function* () {
|
|
1148
|
+
if (signal.aborted) {
|
|
1149
|
+
return yield* Effect_exports.die(signal.reason);
|
|
1150
|
+
}
|
|
1151
|
+
const abortDeferred = yield* Deferred_exports.make();
|
|
1152
|
+
const onAbort = () => {
|
|
1153
|
+
Effect_exports.runPromise(Deferred_exports.die(abortDeferred, signal.reason)).catch(
|
|
1154
|
+
() => {
|
|
1155
|
+
}
|
|
1156
|
+
);
|
|
1157
|
+
};
|
|
1158
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1159
|
+
return yield* Effect_exports.raceFirst(
|
|
1160
|
+
effect,
|
|
1161
|
+
Deferred_exports.await(abortDeferred)
|
|
1162
|
+
).pipe(
|
|
1163
|
+
Effect_exports.ensuring(
|
|
1164
|
+
Effect_exports.sync(() => signal.removeEventListener("abort", onAbort))
|
|
1165
|
+
)
|
|
1166
|
+
);
|
|
1167
|
+
});
|
|
1168
|
+
};
|
|
1169
|
+
|
|
1170
|
+
// src/interactive.ts
|
|
1171
|
+
var interactive = async (options) => {
|
|
1172
|
+
options.signal?.throwIfAborted();
|
|
1173
|
+
const { prompt, promptFile, hooks, agent: provider } = options;
|
|
1174
|
+
const resolvedSandbox = options.sandbox ?? noSandbox();
|
|
1175
|
+
const branchStrategy = options.branchStrategy ?? (resolvedSandbox.tag === "isolated" ? { type: "merge-to-head" } : { type: "head" });
|
|
1176
|
+
if (branchStrategy.type === "head" && resolvedSandbox.tag === "isolated") {
|
|
1177
|
+
throw new Error(
|
|
1178
|
+
"head branch strategy is not supported with isolated providers"
|
|
1179
|
+
);
|
|
1180
|
+
}
|
|
1181
|
+
if (branchStrategy.type === "head" && options.copyToWorktree && options.copyToWorktree.length > 0) {
|
|
1182
|
+
throw new Error(
|
|
1183
|
+
"copyToWorktree is not supported with head branch strategy. In head mode the host working directory is bind-mounted directly."
|
|
1184
|
+
);
|
|
1185
|
+
}
|
|
1186
|
+
if (!provider.buildInteractiveArgs) {
|
|
1187
|
+
throw new Error(
|
|
1188
|
+
`Agent provider "${provider.name}" does not support buildInteractiveArgs, required for interactive sessions.`
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
const branch = branchStrategy.type === "branch" ? branchStrategy.branch : void 0;
|
|
1192
|
+
const isHeadMode = branchStrategy.type === "head";
|
|
1193
|
+
const sandboxProvider = resolvedSandbox;
|
|
1194
|
+
const inner = Effect_exports.gen(function* () {
|
|
1195
|
+
const hostRepoDir = yield* resolveCwd(options.cwd);
|
|
1196
|
+
const d = yield* Display;
|
|
1197
|
+
const hasPromptSource = prompt !== void 0 || promptFile !== void 0;
|
|
1198
|
+
const resolved = hasPromptSource ? yield* resolvePrompt({ prompt, promptFile }) : void 0;
|
|
1199
|
+
const rawPrompt = resolved?.text ?? "";
|
|
1200
|
+
const isInlinePrompt = resolved?.source === "inline";
|
|
1201
|
+
const resolvedEnv = yield* resolveEnv(hostRepoDir);
|
|
1202
|
+
const env = mergeProviderEnv({
|
|
1203
|
+
resolvedEnv,
|
|
1204
|
+
agentProviderEnv: provider.env,
|
|
1205
|
+
sandboxProviderEnv: sandboxProvider.env
|
|
1206
|
+
});
|
|
1207
|
+
const effectiveEnv = { ...env, ...options.env ?? {} };
|
|
1208
|
+
const currentHostBranch = yield* getCurrentBranch(hostRepoDir);
|
|
1209
|
+
const resolvedBranch = branchStrategy.type === "head" ? currentHostBranch : branch ?? generateTempBranchName(options.name);
|
|
1210
|
+
let substitutedPrompt = rawPrompt;
|
|
1211
|
+
if (hasPromptSource && !isInlinePrompt) {
|
|
1212
|
+
const userArgs = options.promptArgs ?? {};
|
|
1213
|
+
yield* validateNoBuiltInArgOverride(userArgs);
|
|
1214
|
+
const missingKeys = findMissingPromptArgKeys(rawPrompt, userArgs);
|
|
1215
|
+
const collectedArgs = {};
|
|
1216
|
+
for (const key of missingKeys) {
|
|
1217
|
+
const value = yield* Effect_exports.promise(
|
|
1218
|
+
() => clack.text({
|
|
1219
|
+
message: `Enter value for {{${key}}}`,
|
|
1220
|
+
validate: (v) => {
|
|
1221
|
+
if (!v) return `A value is required for {{${key}}}`;
|
|
1222
|
+
}
|
|
1223
|
+
})
|
|
1224
|
+
);
|
|
1225
|
+
if (clack.isCancel(value)) {
|
|
1226
|
+
clack.cancel("Prompt arg collection cancelled.");
|
|
1227
|
+
return yield* Effect_exports.fail(
|
|
1228
|
+
new Error("User cancelled prompt arg collection")
|
|
1229
|
+
);
|
|
1230
|
+
}
|
|
1231
|
+
collectedArgs[key] = value;
|
|
1232
|
+
}
|
|
1233
|
+
const mergedUserArgs = { ...userArgs, ...collectedArgs };
|
|
1234
|
+
const effectiveArgs = {
|
|
1235
|
+
SOURCE_BRANCH: resolvedBranch,
|
|
1236
|
+
TARGET_BRANCH: currentHostBranch,
|
|
1237
|
+
...mergedUserArgs
|
|
1238
|
+
};
|
|
1239
|
+
const builtInArgKeysSet = new Set(BUILT_IN_PROMPT_ARG_KEYS);
|
|
1240
|
+
substitutedPrompt = yield* substitutePromptArgs(
|
|
1241
|
+
rawPrompt,
|
|
1242
|
+
effectiveArgs,
|
|
1243
|
+
builtInArgKeysSet
|
|
1244
|
+
);
|
|
1245
|
+
} else if (isInlinePrompt) {
|
|
1246
|
+
const userArgs = options.promptArgs ?? {};
|
|
1247
|
+
yield* validateNoArgsWithInlinePrompt(userArgs);
|
|
1248
|
+
}
|
|
1249
|
+
const lifecycleBranch = isHeadMode ? currentHostBranch : branch;
|
|
1250
|
+
yield* d.intro(options.name ?? "sandcastle interactive");
|
|
1251
|
+
yield* d.summary("Interactive Session", {
|
|
1252
|
+
Agent: options.name ?? provider.name,
|
|
1253
|
+
Sandbox: sandboxProvider.name,
|
|
1254
|
+
Branch: resolvedBranch
|
|
1255
|
+
});
|
|
1256
|
+
let worktreeInfo;
|
|
1257
|
+
if (!isHeadMode) {
|
|
1258
|
+
worktreeInfo = yield* d.taskLog(
|
|
1259
|
+
"Creating worktree",
|
|
1260
|
+
() => pruneStale(hostRepoDir).pipe(
|
|
1261
|
+
Effect_exports.catchAll(() => Effect_exports.void),
|
|
1262
|
+
Effect_exports.andThen(
|
|
1263
|
+
branch ? create(hostRepoDir, { branch }) : create(hostRepoDir, { name: options.name })
|
|
1264
|
+
)
|
|
1265
|
+
)
|
|
1266
|
+
);
|
|
1267
|
+
}
|
|
1268
|
+
const handle = yield* Effect_exports.gen(function* () {
|
|
1269
|
+
if (!isHeadMode) {
|
|
1270
|
+
if ((sandboxProvider.tag === "bind-mount" || sandboxProvider.tag === "none") && options.copyToWorktree && options.copyToWorktree.length > 0) {
|
|
1271
|
+
yield* d.taskLog(
|
|
1272
|
+
"Copying files to worktree",
|
|
1273
|
+
() => copyToWorktree(
|
|
1274
|
+
options.copyToWorktree,
|
|
1275
|
+
hostRepoDir,
|
|
1276
|
+
worktreeInfo.path,
|
|
1277
|
+
options.timeouts?.copyToWorktreeMs
|
|
1278
|
+
)
|
|
1279
|
+
);
|
|
1280
|
+
}
|
|
1281
|
+
if (hooks?.host?.onWorktreeReady?.length) {
|
|
1282
|
+
yield* runHostHooks(hooks.host.onWorktreeReady, worktreeInfo.path);
|
|
1283
|
+
}
|
|
1284
|
+
} else if (hooks?.host?.onWorktreeReady?.length) {
|
|
1285
|
+
yield* runHostHooks(hooks.host.onWorktreeReady, hostRepoDir);
|
|
1286
|
+
}
|
|
1287
|
+
if (sandboxProvider.tag === "none") {
|
|
1288
|
+
const worktreePath = isHeadMode ? hostRepoDir : worktreeInfo.path;
|
|
1289
|
+
return yield* Effect_exports.promise(
|
|
1290
|
+
() => sandboxProvider.create({
|
|
1291
|
+
worktreePath,
|
|
1292
|
+
env: effectiveEnv
|
|
1293
|
+
})
|
|
1294
|
+
);
|
|
1295
|
+
} else if (sandboxProvider.tag === "isolated") {
|
|
1296
|
+
const startResult = yield* d.taskLog(
|
|
1297
|
+
"Starting sandbox",
|
|
1298
|
+
() => startSandbox({
|
|
1299
|
+
provider: sandboxProvider,
|
|
1300
|
+
hostRepoDir: worktreeInfo.path,
|
|
1301
|
+
env: effectiveEnv,
|
|
1302
|
+
copyPaths: options.copyToWorktree
|
|
1303
|
+
})
|
|
1304
|
+
);
|
|
1305
|
+
return startResult.handle;
|
|
1306
|
+
} else {
|
|
1307
|
+
const gitPath = join(hostRepoDir, ".git");
|
|
1308
|
+
const gitMounts = yield* resolveGitMounts(gitPath);
|
|
1309
|
+
const startResult = yield* d.taskLog(
|
|
1310
|
+
"Starting sandbox",
|
|
1311
|
+
() => startSandbox({
|
|
1312
|
+
provider: sandboxProvider,
|
|
1313
|
+
hostRepoDir,
|
|
1314
|
+
env: effectiveEnv,
|
|
1315
|
+
worktreeOrRepoPath: isHeadMode ? hostRepoDir : worktreeInfo.path,
|
|
1316
|
+
gitMounts,
|
|
1317
|
+
repoDir: SANDBOX_REPO_DIR
|
|
1318
|
+
})
|
|
1319
|
+
);
|
|
1320
|
+
return startResult.handle;
|
|
1321
|
+
}
|
|
1322
|
+
}).pipe(
|
|
1323
|
+
Effect_exports.tapError(
|
|
1324
|
+
() => worktreeInfo ? remove(worktreeInfo.path).pipe(
|
|
1325
|
+
Effect_exports.catchAll(() => Effect_exports.void)
|
|
1326
|
+
) : Effect_exports.void
|
|
1327
|
+
)
|
|
1328
|
+
);
|
|
1329
|
+
return yield* Effect_exports.gen(function* () {
|
|
1330
|
+
if (!handle.interactiveExec) {
|
|
1331
|
+
throw new Error(
|
|
1332
|
+
`Sandbox provider does not support interactiveExec. The provider must implement the optional interactiveExec method to use interactive().`
|
|
1333
|
+
);
|
|
1334
|
+
}
|
|
1335
|
+
const interactiveExecFn = handle.interactiveExec.bind(handle);
|
|
1336
|
+
const sandbox = makeSandboxFromHandle(handle);
|
|
1337
|
+
const worktreePath = handle.worktreePath;
|
|
1338
|
+
const applyToHost = sandboxProvider.tag === "isolated" && worktreeInfo ? () => syncOut(worktreeInfo.path, handle) : () => Effect_exports.void;
|
|
1339
|
+
const lifecycleEffect = withSandboxLifecycle(
|
|
1340
|
+
{
|
|
1341
|
+
hostRepoDir,
|
|
1342
|
+
sandboxRepoDir: worktreePath,
|
|
1343
|
+
hooks,
|
|
1344
|
+
branch: lifecycleBranch,
|
|
1345
|
+
hostWorktreePath: isHeadMode ? hostRepoDir : worktreeInfo?.path,
|
|
1346
|
+
applyToHost,
|
|
1347
|
+
timeouts: options.timeouts
|
|
1348
|
+
},
|
|
1349
|
+
sandbox,
|
|
1350
|
+
(ctx) => Effect_exports.gen(function* () {
|
|
1351
|
+
const fullPrompt = !hasPromptSource || isInlinePrompt ? substitutedPrompt : yield* preprocessPrompt(
|
|
1352
|
+
substitutedPrompt,
|
|
1353
|
+
ctx.sandbox,
|
|
1354
|
+
ctx.sandboxRepoDir
|
|
1355
|
+
);
|
|
1356
|
+
const interactiveArgs = provider.buildInteractiveArgs({
|
|
1357
|
+
prompt: fullPrompt,
|
|
1358
|
+
dangerouslySkipPermissions: sandboxProvider.tag !== "none"
|
|
1359
|
+
});
|
|
1360
|
+
const result2 = yield* raceAbortSignal(
|
|
1361
|
+
Effect_exports.promise(
|
|
1362
|
+
() => interactiveExecFn(interactiveArgs, {
|
|
1363
|
+
stdin: process.stdin,
|
|
1364
|
+
stdout: process.stdout,
|
|
1365
|
+
stderr: process.stderr,
|
|
1366
|
+
cwd: worktreePath
|
|
1367
|
+
})
|
|
1368
|
+
),
|
|
1369
|
+
options.signal
|
|
1370
|
+
);
|
|
1371
|
+
return result2.exitCode;
|
|
1372
|
+
})
|
|
1373
|
+
);
|
|
1374
|
+
const lifecycleResult = yield* lifecycleEffect;
|
|
1375
|
+
const exitCode = lifecycleResult.result;
|
|
1376
|
+
let preservedWorktreePath;
|
|
1377
|
+
if (worktreeInfo) {
|
|
1378
|
+
const hasUncommitted = yield* hasUncommittedChanges(
|
|
1379
|
+
worktreeInfo.path
|
|
1380
|
+
).pipe(Effect_exports.catchAll(() => Effect_exports.succeed(false)));
|
|
1381
|
+
if (hasUncommitted) {
|
|
1382
|
+
preservedWorktreePath = worktreeInfo.path;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
if (worktreeInfo && !preservedWorktreePath) {
|
|
1386
|
+
yield* remove(worktreeInfo.path).pipe(
|
|
1387
|
+
Effect_exports.catchAll(() => Effect_exports.void)
|
|
1388
|
+
);
|
|
1389
|
+
}
|
|
1390
|
+
yield* d.summary("Session Complete", {
|
|
1391
|
+
Commits: String(lifecycleResult.commits.length),
|
|
1392
|
+
Branch: lifecycleResult.branch,
|
|
1393
|
+
"Exit code": String(exitCode),
|
|
1394
|
+
...preservedWorktreePath ? { "Preserved worktree": preservedWorktreePath } : {}
|
|
1395
|
+
});
|
|
1396
|
+
return {
|
|
1397
|
+
commits: lifecycleResult.commits,
|
|
1398
|
+
branch: lifecycleResult.branch,
|
|
1399
|
+
preservedWorktreePath,
|
|
1400
|
+
exitCode
|
|
1401
|
+
};
|
|
1402
|
+
}).pipe(
|
|
1403
|
+
// On error, always clean up worktree (on success, handled above with preserve check)
|
|
1404
|
+
Effect_exports.tapError(
|
|
1405
|
+
() => worktreeInfo ? remove(worktreeInfo.path).pipe(
|
|
1406
|
+
Effect_exports.catchAll(() => Effect_exports.void)
|
|
1407
|
+
) : Effect_exports.void
|
|
1408
|
+
),
|
|
1409
|
+
// Always close sandbox handle
|
|
1410
|
+
Effect_exports.ensuring(Effect_exports.promise(() => handle.close().catch(() => {
|
|
1411
|
+
})))
|
|
1412
|
+
);
|
|
1413
|
+
});
|
|
1414
|
+
let result;
|
|
1415
|
+
try {
|
|
1416
|
+
result = await Effect_exports.runPromise(
|
|
1417
|
+
inner.pipe(
|
|
1418
|
+
Effect_exports.provide(ClackDisplay.layer),
|
|
1419
|
+
Effect_exports.provide(NodeContext_exports.layer),
|
|
1420
|
+
Effect_exports.provide(NodeFileSystem_exports.layer)
|
|
1421
|
+
)
|
|
1422
|
+
);
|
|
1423
|
+
} catch (error) {
|
|
1424
|
+
options.signal?.throwIfAborted();
|
|
1425
|
+
throw error;
|
|
1426
|
+
}
|
|
1427
|
+
return result;
|
|
1428
|
+
};
|
|
1429
|
+
var buildSandboxHandle = (ctx, close) => {
|
|
1430
|
+
const {
|
|
1431
|
+
branch,
|
|
1432
|
+
worktreePath,
|
|
1433
|
+
hostRepoDir,
|
|
1434
|
+
sandboxRepoDir,
|
|
1435
|
+
sandbox,
|
|
1436
|
+
providerHandle,
|
|
1437
|
+
applyToHost,
|
|
1438
|
+
timeouts
|
|
1439
|
+
} = ctx;
|
|
1440
|
+
const sandboxHandle = {
|
|
1441
|
+
branch,
|
|
1442
|
+
worktreePath,
|
|
1443
|
+
run: async (runOptions) => {
|
|
1444
|
+
runOptions.signal?.throwIfAborted();
|
|
1445
|
+
const {
|
|
1446
|
+
agent: provider,
|
|
1447
|
+
prompt,
|
|
1448
|
+
promptFile,
|
|
1449
|
+
maxIterations = 1
|
|
1450
|
+
} = runOptions;
|
|
1451
|
+
const resolved = await Effect_exports.runPromise(
|
|
1452
|
+
resolvePrompt({ prompt, promptFile }).pipe(
|
|
1453
|
+
Effect_exports.provide(NodeContext_exports.layer)
|
|
1454
|
+
)
|
|
1455
|
+
);
|
|
1456
|
+
const rawPrompt = resolved.text;
|
|
1457
|
+
const isInlinePrompt = resolved.source === "inline";
|
|
1458
|
+
const userArgs = runOptions.promptArgs ?? {};
|
|
1459
|
+
const currentHostBranch = await Effect_exports.runPromise(
|
|
1460
|
+
getCurrentBranch(hostRepoDir)
|
|
1461
|
+
);
|
|
1462
|
+
const displayRef = Ref_exports.unsafeMake([]);
|
|
1463
|
+
const silentDisplayLayer = SilentDisplay.layer(displayRef);
|
|
1464
|
+
const resolvedPrompt = await Effect_exports.runPromise(
|
|
1465
|
+
Effect_exports.gen(function* () {
|
|
1466
|
+
if (isInlinePrompt) {
|
|
1467
|
+
yield* validateNoArgsWithInlinePrompt(userArgs);
|
|
1468
|
+
return rawPrompt;
|
|
1469
|
+
}
|
|
1470
|
+
yield* validateNoBuiltInArgOverride(userArgs);
|
|
1471
|
+
const effectiveArgs = {
|
|
1472
|
+
SOURCE_BRANCH: branch,
|
|
1473
|
+
TARGET_BRANCH: currentHostBranch,
|
|
1474
|
+
...userArgs
|
|
1475
|
+
};
|
|
1476
|
+
const builtInArgKeysSet = new Set(BUILT_IN_PROMPT_ARG_KEYS);
|
|
1477
|
+
return yield* substitutePromptArgs(
|
|
1478
|
+
rawPrompt,
|
|
1479
|
+
effectiveArgs,
|
|
1480
|
+
builtInArgKeysSet
|
|
1481
|
+
);
|
|
1482
|
+
}).pipe(Effect_exports.provide(silentDisplayLayer))
|
|
1483
|
+
);
|
|
1484
|
+
const resolvedLogging = runOptions.logging ?? {
|
|
1485
|
+
type: "file",
|
|
1486
|
+
path: join(
|
|
1487
|
+
hostRepoDir,
|
|
1488
|
+
".sandcastle",
|
|
1489
|
+
"logs",
|
|
1490
|
+
buildLogFilename(branch, void 0, runOptions.name)
|
|
1491
|
+
)
|
|
1492
|
+
};
|
|
1493
|
+
const runDisplayLayer = resolvedLogging.type === "file" ? (() => {
|
|
1494
|
+
printFileDisplayStartup({
|
|
1495
|
+
logPath: resolvedLogging.path,
|
|
1496
|
+
agentName: runOptions.name,
|
|
1497
|
+
branch
|
|
1498
|
+
});
|
|
1499
|
+
return Layer_exports.provide(
|
|
1500
|
+
FileDisplay.layer(resolvedLogging.path),
|
|
1501
|
+
NodeFileSystem_exports.layer
|
|
1502
|
+
);
|
|
1503
|
+
})() : silentDisplayLayer;
|
|
1504
|
+
const reuseFactoryLayer = Layer_exports.succeed(SandboxFactory, {
|
|
1505
|
+
withSandbox: (makeEffect) => makeEffect(
|
|
1506
|
+
{
|
|
1507
|
+
hostWorktreePath: worktreePath,
|
|
1508
|
+
sandboxRepoPath: sandboxRepoDir,
|
|
1509
|
+
applyToHost
|
|
1510
|
+
},
|
|
1511
|
+
sandbox
|
|
1512
|
+
).pipe(
|
|
1513
|
+
Effect_exports.map((value) => ({
|
|
1514
|
+
value,
|
|
1515
|
+
preservedWorktreePath: void 0
|
|
1516
|
+
}))
|
|
1517
|
+
)
|
|
1518
|
+
});
|
|
1519
|
+
const streamEmitterLayer = agentStreamEmitterLayer(
|
|
1520
|
+
resolvedLogging.type === "file" ? resolvedLogging.onAgentStreamEvent : void 0
|
|
1521
|
+
);
|
|
1522
|
+
const runLayer = Layer_exports.mergeAll(
|
|
1523
|
+
reuseFactoryLayer,
|
|
1524
|
+
runDisplayLayer,
|
|
1525
|
+
streamEmitterLayer
|
|
1526
|
+
);
|
|
1527
|
+
let result;
|
|
1528
|
+
try {
|
|
1529
|
+
result = await Effect_exports.runPromise(
|
|
1530
|
+
Effect_exports.gen(function* () {
|
|
1531
|
+
const display = yield* Display;
|
|
1532
|
+
yield* display.intro(runOptions.name ?? "sandcastle");
|
|
1533
|
+
const orchestrateResult = yield* orchestrate({
|
|
1534
|
+
hostRepoDir,
|
|
1535
|
+
iterations: maxIterations,
|
|
1536
|
+
prompt: resolvedPrompt,
|
|
1537
|
+
branch,
|
|
1538
|
+
provider,
|
|
1539
|
+
completionSignal: runOptions.completionSignal,
|
|
1540
|
+
idleTimeoutSeconds: runOptions.idleTimeoutSeconds,
|
|
1541
|
+
completionTimeoutSeconds: runOptions.completionTimeoutSeconds,
|
|
1542
|
+
name: runOptions.name,
|
|
1543
|
+
signal: runOptions.signal,
|
|
1544
|
+
skipPromptExpansion: isInlinePrompt,
|
|
1545
|
+
timeouts
|
|
1546
|
+
});
|
|
1547
|
+
const completion = buildCompletionMessage(
|
|
1548
|
+
orchestrateResult.completionSignal,
|
|
1549
|
+
orchestrateResult.iterations.length
|
|
1550
|
+
);
|
|
1551
|
+
yield* display.status(completion.message, completion.severity);
|
|
1552
|
+
for (const line of buildContextWindowLines(
|
|
1553
|
+
orchestrateResult.iterations
|
|
1554
|
+
)) {
|
|
1555
|
+
yield* display.text(line);
|
|
1556
|
+
}
|
|
1557
|
+
return orchestrateResult;
|
|
1558
|
+
}).pipe(Effect_exports.provide(runLayer))
|
|
1559
|
+
);
|
|
1560
|
+
} catch (error) {
|
|
1561
|
+
runOptions.signal?.throwIfAborted();
|
|
1562
|
+
throw error;
|
|
1563
|
+
}
|
|
1564
|
+
return {
|
|
1565
|
+
iterations: result.iterations,
|
|
1566
|
+
completionSignal: result.completionSignal,
|
|
1567
|
+
stdout: result.stdout,
|
|
1568
|
+
commits: result.commits,
|
|
1569
|
+
logFilePath: resolvedLogging.type === "file" ? resolvedLogging.path : void 0
|
|
1570
|
+
};
|
|
1571
|
+
},
|
|
1572
|
+
interactive: async (interactiveOptions) => {
|
|
1573
|
+
interactiveOptions.signal?.throwIfAborted();
|
|
1574
|
+
const { agent: provider, prompt, promptFile } = interactiveOptions;
|
|
1575
|
+
if (!provider.buildInteractiveArgs) {
|
|
1576
|
+
throw new Error(
|
|
1577
|
+
`Agent provider "${provider.name}" does not support buildInteractiveArgs, required for interactive sessions.`
|
|
1578
|
+
);
|
|
1579
|
+
}
|
|
1580
|
+
if (!providerHandle?.interactiveExec) {
|
|
1581
|
+
throw new Error(
|
|
1582
|
+
`Sandbox provider does not support interactiveExec. The provider must implement the optional interactiveExec method to use interactive().`
|
|
1583
|
+
);
|
|
1584
|
+
}
|
|
1585
|
+
const interactiveExecFn = providerHandle.interactiveExec.bind(providerHandle);
|
|
1586
|
+
let lifecycleResult;
|
|
1587
|
+
try {
|
|
1588
|
+
lifecycleResult = await Effect_exports.runPromise(
|
|
1589
|
+
Effect_exports.gen(function* () {
|
|
1590
|
+
const resolved = yield* resolvePrompt({ prompt, promptFile });
|
|
1591
|
+
const rawPrompt = resolved.text;
|
|
1592
|
+
const isInlinePrompt = resolved.source === "inline";
|
|
1593
|
+
const userArgs = interactiveOptions.promptArgs ?? {};
|
|
1594
|
+
const currentHostBranch = yield* getCurrentBranch(hostRepoDir);
|
|
1595
|
+
let resolvedPrompt;
|
|
1596
|
+
if (isInlinePrompt) {
|
|
1597
|
+
yield* validateNoArgsWithInlinePrompt(userArgs);
|
|
1598
|
+
resolvedPrompt = rawPrompt;
|
|
1599
|
+
} else {
|
|
1600
|
+
yield* validateNoBuiltInArgOverride(userArgs);
|
|
1601
|
+
const effectiveArgs = {
|
|
1602
|
+
SOURCE_BRANCH: branch,
|
|
1603
|
+
TARGET_BRANCH: currentHostBranch,
|
|
1604
|
+
...userArgs
|
|
1605
|
+
};
|
|
1606
|
+
const builtInArgKeysSet = new Set(
|
|
1607
|
+
BUILT_IN_PROMPT_ARG_KEYS
|
|
1608
|
+
);
|
|
1609
|
+
resolvedPrompt = yield* substitutePromptArgs(
|
|
1610
|
+
rawPrompt,
|
|
1611
|
+
effectiveArgs,
|
|
1612
|
+
builtInArgKeysSet
|
|
1613
|
+
);
|
|
1614
|
+
}
|
|
1615
|
+
return yield* withSandboxLifecycle(
|
|
1616
|
+
{
|
|
1617
|
+
hostRepoDir,
|
|
1618
|
+
sandboxRepoDir,
|
|
1619
|
+
branch,
|
|
1620
|
+
hostWorktreePath: worktreePath,
|
|
1621
|
+
applyToHost,
|
|
1622
|
+
timeouts
|
|
1623
|
+
},
|
|
1624
|
+
sandbox,
|
|
1625
|
+
(ctx2) => Effect_exports.gen(function* () {
|
|
1626
|
+
const fullPrompt = isInlinePrompt ? resolvedPrompt : yield* preprocessPrompt(
|
|
1627
|
+
resolvedPrompt,
|
|
1628
|
+
ctx2.sandbox,
|
|
1629
|
+
ctx2.sandboxRepoDir
|
|
1630
|
+
);
|
|
1631
|
+
const interactiveArgs = provider.buildInteractiveArgs({
|
|
1632
|
+
prompt: fullPrompt,
|
|
1633
|
+
dangerouslySkipPermissions: true
|
|
1634
|
+
});
|
|
1635
|
+
const execPromise = interactiveExecFn(interactiveArgs, {
|
|
1636
|
+
stdin: process.stdin,
|
|
1637
|
+
stdout: process.stdout,
|
|
1638
|
+
stderr: process.stderr,
|
|
1639
|
+
cwd: sandboxRepoDir
|
|
1640
|
+
});
|
|
1641
|
+
const signal = interactiveOptions.signal;
|
|
1642
|
+
const result = yield* Effect_exports.promise(() => {
|
|
1643
|
+
if (!signal) return execPromise;
|
|
1644
|
+
if (signal.aborted) return Promise.reject(signal.reason);
|
|
1645
|
+
return new Promise(
|
|
1646
|
+
(resolve, reject) => {
|
|
1647
|
+
const onAbort = () => reject(signal.reason);
|
|
1648
|
+
signal.addEventListener("abort", onAbort, {
|
|
1649
|
+
once: true
|
|
1650
|
+
});
|
|
1651
|
+
execPromise.then(
|
|
1652
|
+
(r) => {
|
|
1653
|
+
signal.removeEventListener("abort", onAbort);
|
|
1654
|
+
resolve(r);
|
|
1655
|
+
},
|
|
1656
|
+
(e) => {
|
|
1657
|
+
signal.removeEventListener("abort", onAbort);
|
|
1658
|
+
reject(e);
|
|
1659
|
+
}
|
|
1660
|
+
);
|
|
1661
|
+
}
|
|
1662
|
+
);
|
|
1663
|
+
});
|
|
1664
|
+
return result.exitCode;
|
|
1665
|
+
})
|
|
1666
|
+
);
|
|
1667
|
+
}).pipe(
|
|
1668
|
+
Effect_exports.provide(ClackDisplay.layer),
|
|
1669
|
+
Effect_exports.provide(NodeContext_exports.layer)
|
|
1670
|
+
)
|
|
1671
|
+
);
|
|
1672
|
+
} catch (error) {
|
|
1673
|
+
interactiveOptions.signal?.throwIfAborted();
|
|
1674
|
+
throw error;
|
|
1675
|
+
}
|
|
1676
|
+
return {
|
|
1677
|
+
commits: lifecycleResult.commits,
|
|
1678
|
+
exitCode: lifecycleResult.result
|
|
1679
|
+
};
|
|
1680
|
+
},
|
|
1681
|
+
close: async () => close(),
|
|
1682
|
+
[Symbol.asyncDispose]: async () => {
|
|
1683
|
+
await sandboxHandle.close();
|
|
1684
|
+
}
|
|
1685
|
+
};
|
|
1686
|
+
return sandboxHandle;
|
|
1687
|
+
};
|
|
1688
|
+
var createSandboxFromWorktree = async (options) => {
|
|
1689
|
+
const { branch, worktreePath, hostRepoDir } = options;
|
|
1690
|
+
const isTestMode = !!options._test?.buildSandbox;
|
|
1691
|
+
if (options.copyToWorktree && options.copyToWorktree.length > 0 && options.sandbox.tag !== "isolated") {
|
|
1692
|
+
await Effect_exports.runPromise(
|
|
1693
|
+
copyToWorktree(
|
|
1694
|
+
options.copyToWorktree,
|
|
1695
|
+
hostRepoDir,
|
|
1696
|
+
worktreePath,
|
|
1697
|
+
options.timeouts?.copyToWorktreeMs
|
|
1698
|
+
)
|
|
1699
|
+
);
|
|
1700
|
+
}
|
|
1701
|
+
let providerHandle;
|
|
1702
|
+
let sandbox;
|
|
1703
|
+
let sandboxRepoDir;
|
|
1704
|
+
const isIsolated = options.sandbox.tag === "isolated";
|
|
1705
|
+
if (isTestMode) {
|
|
1706
|
+
sandbox = options._test.buildSandbox(worktreePath);
|
|
1707
|
+
sandboxRepoDir = worktreePath;
|
|
1708
|
+
} else {
|
|
1709
|
+
const resolvedEnv = await Effect_exports.runPromise(
|
|
1710
|
+
resolveEnv(hostRepoDir).pipe(Effect_exports.provide(NodeContext_exports.layer))
|
|
1711
|
+
);
|
|
1712
|
+
const env = mergeProviderEnv({
|
|
1713
|
+
resolvedEnv,
|
|
1714
|
+
agentProviderEnv: {},
|
|
1715
|
+
sandboxProviderEnv: options.sandbox.env
|
|
1716
|
+
});
|
|
1717
|
+
const provider = options.sandbox;
|
|
1718
|
+
let startEffect;
|
|
1719
|
+
if (provider.tag === "isolated") {
|
|
1720
|
+
startEffect = startSandbox({
|
|
1721
|
+
provider,
|
|
1722
|
+
hostRepoDir: worktreePath,
|
|
1723
|
+
env,
|
|
1724
|
+
copyPaths: options.copyToWorktree
|
|
1725
|
+
});
|
|
1726
|
+
} else if (provider.tag === "none") {
|
|
1727
|
+
startEffect = startSandbox({
|
|
1728
|
+
provider,
|
|
1729
|
+
hostRepoDir,
|
|
1730
|
+
env,
|
|
1731
|
+
worktreeOrRepoPath: worktreePath
|
|
1732
|
+
});
|
|
1733
|
+
} else {
|
|
1734
|
+
startEffect = resolveGitMounts(join(hostRepoDir, ".git")).pipe(
|
|
1735
|
+
Effect_exports.provide(NodeFileSystem_exports.layer),
|
|
1736
|
+
Effect_exports.catchAll(() => Effect_exports.succeed([])),
|
|
1737
|
+
// Patch git mounts for Windows worktree compatibility (ADR-0006)
|
|
1738
|
+
Effect_exports.flatMap(
|
|
1739
|
+
(gitMounts) => Effect_exports.tryPromise({
|
|
1740
|
+
try: () => patchGitMountsForWindows(
|
|
1741
|
+
gitMounts,
|
|
1742
|
+
worktreePath,
|
|
1743
|
+
SANDBOX_REPO_DIR
|
|
1744
|
+
),
|
|
1745
|
+
catch: (e) => new Error(
|
|
1746
|
+
`Failed to patch git mounts: ${e instanceof Error ? e.message : String(e)}`
|
|
1747
|
+
)
|
|
1748
|
+
})
|
|
1749
|
+
),
|
|
1750
|
+
Effect_exports.flatMap(
|
|
1751
|
+
(gitMounts) => startSandbox({
|
|
1752
|
+
provider,
|
|
1753
|
+
hostRepoDir,
|
|
1754
|
+
env,
|
|
1755
|
+
worktreeOrRepoPath: worktreePath,
|
|
1756
|
+
gitMounts,
|
|
1757
|
+
repoDir: SANDBOX_REPO_DIR
|
|
1758
|
+
})
|
|
1759
|
+
)
|
|
1760
|
+
);
|
|
1761
|
+
}
|
|
1762
|
+
const startResult = await Effect_exports.runPromise(startEffect);
|
|
1763
|
+
providerHandle = startResult.handle;
|
|
1764
|
+
sandbox = startResult.sandbox;
|
|
1765
|
+
sandboxRepoDir = startResult.worktreePath;
|
|
1766
|
+
}
|
|
1767
|
+
const sandboxOnReady = options.hooks?.sandbox?.onSandboxReady;
|
|
1768
|
+
const hostOnReady = options.hooks?.host?.onSandboxReady;
|
|
1769
|
+
if (sandboxOnReady?.length || hostOnReady?.length) {
|
|
1770
|
+
await Effect_exports.runPromise(
|
|
1771
|
+
Effect_exports.gen(function* () {
|
|
1772
|
+
yield* sandbox.exec(
|
|
1773
|
+
`git config --global --add safe.directory "${sandboxRepoDir}"`
|
|
1774
|
+
);
|
|
1775
|
+
const sandboxEffects = (sandboxOnReady ?? []).map(
|
|
1776
|
+
(hook) => sandbox.exec(hook.command, {
|
|
1777
|
+
cwd: sandboxRepoDir,
|
|
1778
|
+
sudo: hook.sudo
|
|
1779
|
+
})
|
|
1780
|
+
);
|
|
1781
|
+
const allEffects = [...sandboxEffects];
|
|
1782
|
+
if (hostOnReady?.length) {
|
|
1783
|
+
allEffects.push(runHostHooks(hostOnReady, worktreePath));
|
|
1784
|
+
}
|
|
1785
|
+
yield* Effect_exports.all(allEffects, {
|
|
1786
|
+
concurrency: "unbounded"
|
|
1787
|
+
});
|
|
1788
|
+
})
|
|
1789
|
+
);
|
|
1790
|
+
}
|
|
1791
|
+
const applyToHost = isIsolated && providerHandle ? () => syncOut(worktreePath, providerHandle) : () => Effect_exports.void;
|
|
1792
|
+
let closed = false;
|
|
1793
|
+
return buildSandboxHandle(
|
|
1794
|
+
{
|
|
1795
|
+
branch,
|
|
1796
|
+
worktreePath,
|
|
1797
|
+
hostRepoDir,
|
|
1798
|
+
sandboxRepoDir,
|
|
1799
|
+
sandbox,
|
|
1800
|
+
providerHandle,
|
|
1801
|
+
applyToHost,
|
|
1802
|
+
timeouts: options.timeouts
|
|
1803
|
+
},
|
|
1804
|
+
async () => {
|
|
1805
|
+
if (closed) return { preservedWorktreePath: void 0 };
|
|
1806
|
+
closed = true;
|
|
1807
|
+
if (providerHandle) await providerHandle.close();
|
|
1808
|
+
return { preservedWorktreePath: void 0 };
|
|
1809
|
+
}
|
|
1810
|
+
);
|
|
1811
|
+
};
|
|
1812
|
+
var createSandbox = async (options) => {
|
|
1813
|
+
const { branch } = options;
|
|
1814
|
+
const isTestMode = !!options._test?.buildSandbox;
|
|
1815
|
+
const isIsolated = options.sandbox.tag === "isolated";
|
|
1816
|
+
const { hostRepoDir, worktreePath, providerHandle, sandbox, sandboxRepoDir } = await Effect_exports.runPromise(
|
|
1817
|
+
Effect_exports.gen(function* () {
|
|
1818
|
+
const hostRepoDir2 = yield* resolveCwd(options.cwd);
|
|
1819
|
+
yield* pruneStale(hostRepoDir2).pipe(
|
|
1820
|
+
Effect_exports.catchAll(() => Effect_exports.void)
|
|
1821
|
+
);
|
|
1822
|
+
const { path: worktreePath2 } = yield* create(
|
|
1823
|
+
hostRepoDir2,
|
|
1824
|
+
{ branch, baseBranch: options.baseBranch }
|
|
1825
|
+
);
|
|
1826
|
+
const prepared = yield* Effect_exports.gen(function* () {
|
|
1827
|
+
if (options.copyToWorktree && options.copyToWorktree.length > 0 && options.sandbox.tag !== "isolated") {
|
|
1828
|
+
yield* copyToWorktree(
|
|
1829
|
+
options.copyToWorktree,
|
|
1830
|
+
hostRepoDir2,
|
|
1831
|
+
worktreePath2,
|
|
1832
|
+
options.timeouts?.copyToWorktreeMs
|
|
1833
|
+
);
|
|
1834
|
+
}
|
|
1835
|
+
if (options.hooks?.host?.onWorktreeReady?.length) {
|
|
1836
|
+
yield* runHostHooks(
|
|
1837
|
+
options.hooks.host.onWorktreeReady,
|
|
1838
|
+
worktreePath2
|
|
1839
|
+
);
|
|
1840
|
+
}
|
|
1841
|
+
let providerHandle2;
|
|
1842
|
+
let sandbox2;
|
|
1843
|
+
let sandboxRepoDir2;
|
|
1844
|
+
if (isTestMode) {
|
|
1845
|
+
sandbox2 = options._test.buildSandbox(worktreePath2);
|
|
1846
|
+
sandboxRepoDir2 = worktreePath2;
|
|
1847
|
+
} else {
|
|
1848
|
+
const resolvedEnv = yield* resolveEnv(hostRepoDir2);
|
|
1849
|
+
const env = mergeProviderEnv({
|
|
1850
|
+
resolvedEnv,
|
|
1851
|
+
agentProviderEnv: {},
|
|
1852
|
+
sandboxProviderEnv: options.sandbox.env
|
|
1853
|
+
});
|
|
1854
|
+
const provider = options.sandbox;
|
|
1855
|
+
const startResult = yield* provider.tag === "isolated" ? startSandbox({
|
|
1856
|
+
provider,
|
|
1857
|
+
hostRepoDir: worktreePath2,
|
|
1858
|
+
env,
|
|
1859
|
+
copyPaths: options.copyToWorktree
|
|
1860
|
+
}) : provider.tag === "none" ? startSandbox({
|
|
1861
|
+
provider,
|
|
1862
|
+
hostRepoDir: hostRepoDir2,
|
|
1863
|
+
env,
|
|
1864
|
+
worktreeOrRepoPath: worktreePath2
|
|
1865
|
+
}) : resolveGitMounts(join(hostRepoDir2, ".git")).pipe(
|
|
1866
|
+
Effect_exports.provide(NodeFileSystem_exports.layer),
|
|
1867
|
+
Effect_exports.catchAll(() => Effect_exports.succeed([])),
|
|
1868
|
+
// Patch git mounts for Windows worktree compatibility (ADR-0006)
|
|
1869
|
+
Effect_exports.flatMap(
|
|
1870
|
+
(gitMounts) => Effect_exports.tryPromise({
|
|
1871
|
+
try: () => patchGitMountsForWindows(
|
|
1872
|
+
gitMounts,
|
|
1873
|
+
worktreePath2,
|
|
1874
|
+
SANDBOX_REPO_DIR
|
|
1875
|
+
),
|
|
1876
|
+
catch: (e) => new Error(
|
|
1877
|
+
`Failed to patch git mounts: ${e instanceof Error ? e.message : String(e)}`
|
|
1878
|
+
)
|
|
1879
|
+
})
|
|
1880
|
+
),
|
|
1881
|
+
Effect_exports.flatMap(
|
|
1882
|
+
(gitMounts) => startSandbox({
|
|
1883
|
+
provider,
|
|
1884
|
+
hostRepoDir: hostRepoDir2,
|
|
1885
|
+
env,
|
|
1886
|
+
worktreeOrRepoPath: worktreePath2,
|
|
1887
|
+
gitMounts,
|
|
1888
|
+
repoDir: SANDBOX_REPO_DIR
|
|
1889
|
+
})
|
|
1890
|
+
)
|
|
1891
|
+
);
|
|
1892
|
+
providerHandle2 = startResult.handle;
|
|
1893
|
+
sandbox2 = startResult.sandbox;
|
|
1894
|
+
sandboxRepoDir2 = startResult.worktreePath;
|
|
1895
|
+
}
|
|
1896
|
+
const sandboxOnReady = options.hooks?.sandbox?.onSandboxReady;
|
|
1897
|
+
const hostOnReady = options.hooks?.host?.onSandboxReady;
|
|
1898
|
+
if (sandboxOnReady?.length || hostOnReady?.length) {
|
|
1899
|
+
yield* Effect_exports.gen(function* () {
|
|
1900
|
+
yield* sandbox2.exec(
|
|
1901
|
+
`git config --global --add safe.directory "${sandboxRepoDir2}"`
|
|
1902
|
+
);
|
|
1903
|
+
const sandboxEffects = (sandboxOnReady ?? []).map(
|
|
1904
|
+
(hook) => sandbox2.exec(hook.command, {
|
|
1905
|
+
cwd: sandboxRepoDir2,
|
|
1906
|
+
sudo: hook.sudo
|
|
1907
|
+
})
|
|
1908
|
+
);
|
|
1909
|
+
const allEffects = [...sandboxEffects];
|
|
1910
|
+
if (hostOnReady?.length) {
|
|
1911
|
+
allEffects.push(runHostHooks(hostOnReady, worktreePath2));
|
|
1912
|
+
}
|
|
1913
|
+
yield* Effect_exports.all(allEffects, { concurrency: "unbounded" });
|
|
1914
|
+
}).pipe(
|
|
1915
|
+
Effect_exports.onError(
|
|
1916
|
+
() => providerHandle2 ? Effect_exports.promise(
|
|
1917
|
+
() => providerHandle2.close().catch(() => {
|
|
1918
|
+
})
|
|
1919
|
+
) : Effect_exports.void
|
|
1920
|
+
)
|
|
1921
|
+
);
|
|
1922
|
+
}
|
|
1923
|
+
return { providerHandle: providerHandle2, sandbox: sandbox2, sandboxRepoDir: sandboxRepoDir2 };
|
|
1924
|
+
}).pipe(
|
|
1925
|
+
Effect_exports.onError(
|
|
1926
|
+
() => remove(worktreePath2).pipe(
|
|
1927
|
+
Effect_exports.catchAll(() => Effect_exports.void)
|
|
1928
|
+
)
|
|
1929
|
+
)
|
|
1930
|
+
);
|
|
1931
|
+
return { hostRepoDir: hostRepoDir2, worktreePath: worktreePath2, ...prepared };
|
|
1932
|
+
}).pipe(Effect_exports.provide(NodeContext_exports.layer))
|
|
1933
|
+
);
|
|
1934
|
+
const applyToHost = isIsolated && providerHandle ? () => syncOut(worktreePath, providerHandle) : () => Effect_exports.void;
|
|
1935
|
+
let closed = false;
|
|
1936
|
+
const forceCleanup = () => {
|
|
1937
|
+
console.error(`
|
|
1938
|
+
Worktree preserved at ${worktreePath}`);
|
|
1939
|
+
console.error(` To review: cd ${worktreePath}`);
|
|
1940
|
+
console.error(` To clean up: git worktree remove --force ${worktreePath}`);
|
|
1941
|
+
};
|
|
1942
|
+
const unregisterShutdown = registerShutdown(forceCleanup);
|
|
1943
|
+
const doClose = async () => {
|
|
1944
|
+
if (closed) return { preservedWorktreePath: void 0 };
|
|
1945
|
+
closed = true;
|
|
1946
|
+
return Effect_exports.runPromise(
|
|
1947
|
+
Effect_exports.gen(function* () {
|
|
1948
|
+
if (providerHandle) {
|
|
1949
|
+
yield* Effect_exports.promise(() => providerHandle.close());
|
|
1950
|
+
}
|
|
1951
|
+
const isDirty = yield* hasUncommittedChanges(
|
|
1952
|
+
worktreePath
|
|
1953
|
+
).pipe(Effect_exports.catchAll(() => Effect_exports.succeed(false)));
|
|
1954
|
+
if (isDirty) {
|
|
1955
|
+
return { preservedWorktreePath: worktreePath };
|
|
1956
|
+
}
|
|
1957
|
+
yield* remove(worktreePath).pipe(
|
|
1958
|
+
Effect_exports.catchAll(() => Effect_exports.void)
|
|
1959
|
+
);
|
|
1960
|
+
return { preservedWorktreePath: void 0 };
|
|
1961
|
+
})
|
|
1962
|
+
);
|
|
1963
|
+
};
|
|
1964
|
+
return buildSandboxHandle(
|
|
1965
|
+
{
|
|
1966
|
+
branch,
|
|
1967
|
+
worktreePath,
|
|
1968
|
+
hostRepoDir,
|
|
1969
|
+
sandboxRepoDir,
|
|
1970
|
+
sandbox,
|
|
1971
|
+
providerHandle,
|
|
1972
|
+
applyToHost,
|
|
1973
|
+
timeouts: options.timeouts
|
|
1974
|
+
},
|
|
1975
|
+
async () => {
|
|
1976
|
+
unregisterShutdown();
|
|
1977
|
+
return doClose();
|
|
1978
|
+
}
|
|
1979
|
+
);
|
|
1980
|
+
};
|
|
1981
|
+
var createWorktree = async (options) => {
|
|
1982
|
+
const branch = options.branchStrategy.type === "branch" ? options.branchStrategy.branch : void 0;
|
|
1983
|
+
const baseBranch = options.branchStrategy.type === "branch" ? options.branchStrategy.baseBranch : void 0;
|
|
1984
|
+
const { hostRepoDir, worktreeInfo } = await Effect_exports.gen(function* () {
|
|
1985
|
+
const hostRepoDir2 = yield* resolveCwd(options.cwd);
|
|
1986
|
+
yield* pruneStale(hostRepoDir2).pipe(
|
|
1987
|
+
Effect_exports.catchAll(() => Effect_exports.void)
|
|
1988
|
+
);
|
|
1989
|
+
const info = yield* create(hostRepoDir2, {
|
|
1990
|
+
branch,
|
|
1991
|
+
baseBranch
|
|
1992
|
+
});
|
|
1993
|
+
if (options.copyToWorktree && options.copyToWorktree.length > 0) {
|
|
1994
|
+
yield* copyToWorktree(
|
|
1995
|
+
options.copyToWorktree,
|
|
1996
|
+
hostRepoDir2,
|
|
1997
|
+
info.path,
|
|
1998
|
+
options.timeouts?.copyToWorktreeMs
|
|
1999
|
+
);
|
|
2000
|
+
}
|
|
2001
|
+
if (options.hooks?.host?.onWorktreeReady?.length) {
|
|
2002
|
+
yield* runHostHooks(options.hooks.host.onWorktreeReady, info.path);
|
|
2003
|
+
}
|
|
2004
|
+
return { hostRepoDir: hostRepoDir2, worktreeInfo: info };
|
|
2005
|
+
}).pipe(Effect_exports.provide(NodeContext_exports.layer), Effect_exports.runPromise);
|
|
2006
|
+
let closed = false;
|
|
2007
|
+
const close = async () => {
|
|
2008
|
+
if (closed) return { preservedWorktreePath: void 0 };
|
|
2009
|
+
closed = true;
|
|
2010
|
+
return Effect_exports.gen(function* () {
|
|
2011
|
+
const isDirty = yield* hasUncommittedChanges(
|
|
2012
|
+
worktreeInfo.path
|
|
2013
|
+
).pipe(Effect_exports.catchAll(() => Effect_exports.succeed(false)));
|
|
2014
|
+
if (isDirty) {
|
|
2015
|
+
return { preservedWorktreePath: worktreeInfo.path };
|
|
2016
|
+
}
|
|
2017
|
+
yield* remove(worktreeInfo.path).pipe(
|
|
2018
|
+
Effect_exports.catchAll(() => Effect_exports.void)
|
|
2019
|
+
);
|
|
2020
|
+
return { preservedWorktreePath: void 0 };
|
|
2021
|
+
}).pipe(Effect_exports.runPromise);
|
|
2022
|
+
};
|
|
2023
|
+
const worktreeInteractive = async (opts) => {
|
|
2024
|
+
opts.signal?.throwIfAborted();
|
|
2025
|
+
const { prompt, promptFile, hooks, agent: provider } = opts;
|
|
2026
|
+
const resolvedSandbox = opts.sandbox ?? noSandbox();
|
|
2027
|
+
if (!provider.buildInteractiveArgs) {
|
|
2028
|
+
throw new Error(
|
|
2029
|
+
`Agent provider "${provider.name}" does not support buildInteractiveArgs, required for interactive sessions.`
|
|
2030
|
+
);
|
|
2031
|
+
}
|
|
2032
|
+
const inner = Effect_exports.gen(function* () {
|
|
2033
|
+
const d = yield* Display;
|
|
2034
|
+
const hasPromptSource = prompt !== void 0 || promptFile !== void 0;
|
|
2035
|
+
const resolved = hasPromptSource ? yield* resolvePrompt({ prompt, promptFile }) : void 0;
|
|
2036
|
+
const rawPrompt = resolved?.text ?? "";
|
|
2037
|
+
const isInlinePrompt = resolved?.source === "inline";
|
|
2038
|
+
const resolvedEnv = yield* resolveEnv(hostRepoDir);
|
|
2039
|
+
const env = mergeProviderEnv({
|
|
2040
|
+
resolvedEnv,
|
|
2041
|
+
agentProviderEnv: provider.env,
|
|
2042
|
+
sandboxProviderEnv: resolvedSandbox.env
|
|
2043
|
+
});
|
|
2044
|
+
const effectiveEnv = { ...env, ...opts.env ?? {} };
|
|
2045
|
+
let substitutedPrompt = rawPrompt;
|
|
2046
|
+
if (hasPromptSource && !isInlinePrompt) {
|
|
2047
|
+
const userArgs = opts.promptArgs ?? {};
|
|
2048
|
+
yield* validateNoBuiltInArgOverride(userArgs);
|
|
2049
|
+
const effectiveArgs = {
|
|
2050
|
+
SOURCE_BRANCH: worktreeInfo.branch,
|
|
2051
|
+
TARGET_BRANCH: worktreeInfo.branch,
|
|
2052
|
+
...userArgs
|
|
2053
|
+
};
|
|
2054
|
+
const builtInArgKeysSet = new Set(BUILT_IN_PROMPT_ARG_KEYS);
|
|
2055
|
+
substitutedPrompt = yield* substitutePromptArgs(
|
|
2056
|
+
rawPrompt,
|
|
2057
|
+
effectiveArgs,
|
|
2058
|
+
builtInArgKeysSet
|
|
2059
|
+
);
|
|
2060
|
+
} else if (isInlinePrompt) {
|
|
2061
|
+
yield* validateNoArgsWithInlinePrompt(opts.promptArgs ?? {});
|
|
2062
|
+
}
|
|
2063
|
+
yield* d.intro(opts.name ?? "sandcastle interactive");
|
|
2064
|
+
yield* d.summary("Interactive Session", {
|
|
2065
|
+
Agent: opts.name ?? provider.name,
|
|
2066
|
+
Sandbox: resolvedSandbox.name,
|
|
2067
|
+
Branch: worktreeInfo.branch
|
|
2068
|
+
});
|
|
2069
|
+
let handle;
|
|
2070
|
+
if (resolvedSandbox.tag === "none") {
|
|
2071
|
+
handle = yield* Effect_exports.promise(
|
|
2072
|
+
() => resolvedSandbox.create({
|
|
2073
|
+
worktreePath: worktreeInfo.path,
|
|
2074
|
+
env: effectiveEnv
|
|
2075
|
+
})
|
|
2076
|
+
);
|
|
2077
|
+
} else if (resolvedSandbox.tag === "isolated") {
|
|
2078
|
+
const startResult = yield* d.taskLog(
|
|
2079
|
+
"Starting sandbox",
|
|
2080
|
+
() => startSandbox({
|
|
2081
|
+
provider: resolvedSandbox,
|
|
2082
|
+
hostRepoDir: worktreeInfo.path,
|
|
2083
|
+
env: effectiveEnv
|
|
2084
|
+
})
|
|
2085
|
+
);
|
|
2086
|
+
handle = startResult.handle;
|
|
2087
|
+
} else {
|
|
2088
|
+
const gitPath = join(hostRepoDir, ".git");
|
|
2089
|
+
const gitMounts = yield* resolveGitMounts(gitPath);
|
|
2090
|
+
const startResult = yield* d.taskLog(
|
|
2091
|
+
"Starting sandbox",
|
|
2092
|
+
() => startSandbox({
|
|
2093
|
+
provider: resolvedSandbox,
|
|
2094
|
+
hostRepoDir,
|
|
2095
|
+
env: effectiveEnv,
|
|
2096
|
+
worktreeOrRepoPath: worktreeInfo.path,
|
|
2097
|
+
gitMounts,
|
|
2098
|
+
repoDir: SANDBOX_REPO_DIR
|
|
2099
|
+
})
|
|
2100
|
+
);
|
|
2101
|
+
handle = startResult.handle;
|
|
2102
|
+
}
|
|
2103
|
+
return yield* Effect_exports.gen(function* () {
|
|
2104
|
+
if (!handle.interactiveExec) {
|
|
2105
|
+
throw new Error(
|
|
2106
|
+
`Sandbox provider does not support interactiveExec. The provider must implement the optional interactiveExec method to use interactive().`
|
|
2107
|
+
);
|
|
2108
|
+
}
|
|
2109
|
+
const interactiveExecFn = handle.interactiveExec.bind(handle);
|
|
2110
|
+
const sandbox = makeSandboxFromHandle(handle);
|
|
2111
|
+
const worktreePath = handle.worktreePath;
|
|
2112
|
+
const applyToHost = resolvedSandbox.tag === "isolated" ? () => syncOut(worktreeInfo.path, handle) : () => Effect_exports.void;
|
|
2113
|
+
const lifecycleEffect = withSandboxLifecycle(
|
|
2114
|
+
{
|
|
2115
|
+
hostRepoDir,
|
|
2116
|
+
sandboxRepoDir: worktreePath,
|
|
2117
|
+
hooks,
|
|
2118
|
+
branch: worktreeInfo.branch,
|
|
2119
|
+
hostWorktreePath: worktreeInfo.path,
|
|
2120
|
+
applyToHost,
|
|
2121
|
+
timeouts: options.timeouts
|
|
2122
|
+
},
|
|
2123
|
+
sandbox,
|
|
2124
|
+
(ctx) => Effect_exports.gen(function* () {
|
|
2125
|
+
const fullPrompt = !hasPromptSource || isInlinePrompt ? substitutedPrompt : yield* preprocessPrompt(
|
|
2126
|
+
substitutedPrompt,
|
|
2127
|
+
ctx.sandbox,
|
|
2128
|
+
ctx.sandboxRepoDir
|
|
2129
|
+
);
|
|
2130
|
+
const interactiveArgs = provider.buildInteractiveArgs({
|
|
2131
|
+
prompt: fullPrompt,
|
|
2132
|
+
dangerouslySkipPermissions: resolvedSandbox.tag !== "none"
|
|
2133
|
+
});
|
|
2134
|
+
const result = yield* raceAbortSignal(
|
|
2135
|
+
Effect_exports.promise(
|
|
2136
|
+
() => interactiveExecFn(interactiveArgs, {
|
|
2137
|
+
stdin: process.stdin,
|
|
2138
|
+
stdout: process.stdout,
|
|
2139
|
+
stderr: process.stderr,
|
|
2140
|
+
cwd: worktreePath
|
|
2141
|
+
})
|
|
2142
|
+
),
|
|
2143
|
+
opts.signal
|
|
2144
|
+
);
|
|
2145
|
+
return result.exitCode;
|
|
2146
|
+
})
|
|
2147
|
+
);
|
|
2148
|
+
const lifecycleResult = yield* lifecycleEffect;
|
|
2149
|
+
const exitCode = lifecycleResult.result;
|
|
2150
|
+
yield* d.summary("Session Complete", {
|
|
2151
|
+
Commits: String(lifecycleResult.commits.length),
|
|
2152
|
+
Branch: lifecycleResult.branch,
|
|
2153
|
+
"Exit code": String(exitCode)
|
|
2154
|
+
});
|
|
2155
|
+
return {
|
|
2156
|
+
commits: lifecycleResult.commits,
|
|
2157
|
+
branch: lifecycleResult.branch,
|
|
2158
|
+
preservedWorktreePath: void 0,
|
|
2159
|
+
exitCode
|
|
2160
|
+
};
|
|
2161
|
+
}).pipe(
|
|
2162
|
+
// Always close sandbox handle
|
|
2163
|
+
Effect_exports.ensuring(Effect_exports.promise(() => handle.close().catch(() => {
|
|
2164
|
+
})))
|
|
2165
|
+
);
|
|
2166
|
+
});
|
|
2167
|
+
try {
|
|
2168
|
+
return await Effect_exports.runPromise(
|
|
2169
|
+
inner.pipe(
|
|
2170
|
+
Effect_exports.provide(ClackDisplay.layer),
|
|
2171
|
+
Effect_exports.provide(NodeContext_exports.layer),
|
|
2172
|
+
Effect_exports.provide(NodeFileSystem_exports.layer)
|
|
2173
|
+
)
|
|
2174
|
+
);
|
|
2175
|
+
} catch (error) {
|
|
2176
|
+
opts.signal?.throwIfAborted();
|
|
2177
|
+
throw error;
|
|
2178
|
+
}
|
|
2179
|
+
};
|
|
2180
|
+
const worktreeRun = async (opts) => {
|
|
2181
|
+
opts.signal?.throwIfAborted();
|
|
2182
|
+
const { prompt, promptFile, hooks, agent: provider } = opts;
|
|
2183
|
+
const sandboxProvider = opts.sandbox;
|
|
2184
|
+
const maxIterations = opts.maxIterations ?? 1;
|
|
2185
|
+
if (opts.resumeSession && maxIterations > 1) {
|
|
2186
|
+
throw new Error(
|
|
2187
|
+
"resumeSession cannot be combined with maxIterations > 1. Resume applies to iteration 1 only; multi-iteration resume semantics are not supported."
|
|
2188
|
+
);
|
|
2189
|
+
}
|
|
2190
|
+
if (opts.resumeSession) {
|
|
2191
|
+
await assertResumeSessionExists({
|
|
2192
|
+
provider,
|
|
2193
|
+
sandboxTag: sandboxProvider.tag,
|
|
2194
|
+
hostRepoDir,
|
|
2195
|
+
resumeSession: opts.resumeSession
|
|
2196
|
+
});
|
|
2197
|
+
}
|
|
2198
|
+
const inner = Effect_exports.gen(function* () {
|
|
2199
|
+
const resolved = yield* resolvePrompt({ prompt, promptFile });
|
|
2200
|
+
const rawPrompt = resolved.text;
|
|
2201
|
+
const isInlinePrompt = resolved.source === "inline";
|
|
2202
|
+
const resolvedEnv = yield* resolveEnv(hostRepoDir);
|
|
2203
|
+
const env = mergeProviderEnv({
|
|
2204
|
+
resolvedEnv,
|
|
2205
|
+
agentProviderEnv: provider.env,
|
|
2206
|
+
sandboxProviderEnv: sandboxProvider.env
|
|
2207
|
+
});
|
|
2208
|
+
const effectiveEnv = { ...env, ...opts.env ?? {} };
|
|
2209
|
+
const userArgs = opts.promptArgs ?? {};
|
|
2210
|
+
let resolvedPrompt;
|
|
2211
|
+
if (isInlinePrompt) {
|
|
2212
|
+
yield* validateNoArgsWithInlinePrompt(userArgs);
|
|
2213
|
+
resolvedPrompt = rawPrompt;
|
|
2214
|
+
} else {
|
|
2215
|
+
yield* validateNoBuiltInArgOverride(userArgs);
|
|
2216
|
+
const effectiveArgs = {
|
|
2217
|
+
SOURCE_BRANCH: worktreeInfo.branch,
|
|
2218
|
+
TARGET_BRANCH: worktreeInfo.branch,
|
|
2219
|
+
...userArgs
|
|
2220
|
+
};
|
|
2221
|
+
const builtInArgKeysSet = new Set(BUILT_IN_PROMPT_ARG_KEYS);
|
|
2222
|
+
resolvedPrompt = yield* substitutePromptArgs(
|
|
2223
|
+
rawPrompt,
|
|
2224
|
+
effectiveArgs,
|
|
2225
|
+
builtInArgKeysSet
|
|
2226
|
+
);
|
|
2227
|
+
}
|
|
2228
|
+
let handle;
|
|
2229
|
+
let sandboxRepoDir;
|
|
2230
|
+
if (sandboxProvider.tag === "isolated") {
|
|
2231
|
+
const startResult = yield* startSandbox({
|
|
2232
|
+
provider: sandboxProvider,
|
|
2233
|
+
hostRepoDir: worktreeInfo.path,
|
|
2234
|
+
env: effectiveEnv
|
|
2235
|
+
});
|
|
2236
|
+
handle = startResult.handle;
|
|
2237
|
+
sandboxRepoDir = startResult.worktreePath;
|
|
2238
|
+
} else if (sandboxProvider.tag === "none") {
|
|
2239
|
+
const startResult = yield* startSandbox({
|
|
2240
|
+
provider: sandboxProvider,
|
|
2241
|
+
hostRepoDir,
|
|
2242
|
+
env: effectiveEnv,
|
|
2243
|
+
worktreeOrRepoPath: worktreeInfo.path
|
|
2244
|
+
});
|
|
2245
|
+
handle = startResult.handle;
|
|
2246
|
+
sandboxRepoDir = startResult.worktreePath;
|
|
2247
|
+
} else {
|
|
2248
|
+
const gitPath = join(hostRepoDir, ".git");
|
|
2249
|
+
const gitMounts = yield* resolveGitMounts(gitPath);
|
|
2250
|
+
const startResult = yield* startSandbox({
|
|
2251
|
+
provider: sandboxProvider,
|
|
2252
|
+
hostRepoDir,
|
|
2253
|
+
env: effectiveEnv,
|
|
2254
|
+
worktreeOrRepoPath: worktreeInfo.path,
|
|
2255
|
+
gitMounts,
|
|
2256
|
+
repoDir: SANDBOX_REPO_DIR
|
|
2257
|
+
});
|
|
2258
|
+
handle = startResult.handle;
|
|
2259
|
+
sandboxRepoDir = startResult.worktreePath;
|
|
2260
|
+
}
|
|
2261
|
+
const sandbox = makeSandboxFromHandle(handle);
|
|
2262
|
+
const applyToHost = sandboxProvider.tag === "isolated" ? () => syncOut(worktreeInfo.path, handle) : () => Effect_exports.void;
|
|
2263
|
+
const resolvedLogging = opts.logging ?? {
|
|
2264
|
+
type: "file",
|
|
2265
|
+
path: join(
|
|
2266
|
+
hostRepoDir,
|
|
2267
|
+
".sandcastle",
|
|
2268
|
+
"logs",
|
|
2269
|
+
buildLogFilename(worktreeInfo.branch, void 0, opts.name)
|
|
2270
|
+
)
|
|
2271
|
+
};
|
|
2272
|
+
const runDisplayLayer = resolvedLogging.type === "file" ? (() => {
|
|
2273
|
+
printFileDisplayStartup({
|
|
2274
|
+
logPath: resolvedLogging.path,
|
|
2275
|
+
agentName: opts.name,
|
|
2276
|
+
branch: worktreeInfo.branch
|
|
2277
|
+
});
|
|
2278
|
+
return Layer_exports.provide(
|
|
2279
|
+
FileDisplay.layer(resolvedLogging.path),
|
|
2280
|
+
NodeFileSystem_exports.layer
|
|
2281
|
+
);
|
|
2282
|
+
})() : ClackDisplay.layer;
|
|
2283
|
+
const reuseFactoryLayer = Layer_exports.succeed(SandboxFactory, {
|
|
2284
|
+
withSandbox: (makeEffect) => makeEffect(
|
|
2285
|
+
{
|
|
2286
|
+
hostWorktreePath: worktreeInfo.path,
|
|
2287
|
+
sandboxRepoPath: sandboxRepoDir,
|
|
2288
|
+
applyToHost
|
|
2289
|
+
},
|
|
2290
|
+
sandbox
|
|
2291
|
+
).pipe(
|
|
2292
|
+
Effect_exports.map((value) => ({
|
|
2293
|
+
value,
|
|
2294
|
+
preservedWorktreePath: void 0
|
|
2295
|
+
}))
|
|
2296
|
+
)
|
|
2297
|
+
});
|
|
2298
|
+
const streamEmitterLayer = agentStreamEmitterLayer(
|
|
2299
|
+
resolvedLogging.type === "file" ? resolvedLogging.onAgentStreamEvent : void 0
|
|
2300
|
+
);
|
|
2301
|
+
const runLayer = Layer_exports.mergeAll(
|
|
2302
|
+
reuseFactoryLayer,
|
|
2303
|
+
runDisplayLayer,
|
|
2304
|
+
streamEmitterLayer
|
|
2305
|
+
);
|
|
2306
|
+
const result = yield* Effect_exports.gen(function* () {
|
|
2307
|
+
const display = yield* Display;
|
|
2308
|
+
yield* display.intro(opts.name ?? "sandcastle");
|
|
2309
|
+
const orchestrateResult = yield* orchestrate({
|
|
2310
|
+
hostRepoDir,
|
|
2311
|
+
iterations: maxIterations,
|
|
2312
|
+
hooks,
|
|
2313
|
+
prompt: resolvedPrompt,
|
|
2314
|
+
branch: worktreeInfo.branch,
|
|
2315
|
+
provider,
|
|
2316
|
+
completionSignal: opts.completionSignal,
|
|
2317
|
+
idleTimeoutSeconds: opts.idleTimeoutSeconds,
|
|
2318
|
+
completionTimeoutSeconds: opts.completionTimeoutSeconds,
|
|
2319
|
+
name: opts.name,
|
|
2320
|
+
resumeSession: opts.resumeSession,
|
|
2321
|
+
signal: opts.signal,
|
|
2322
|
+
skipPromptExpansion: isInlinePrompt,
|
|
2323
|
+
timeouts: options.timeouts
|
|
2324
|
+
});
|
|
2325
|
+
const completion = buildCompletionMessage(
|
|
2326
|
+
orchestrateResult.completionSignal,
|
|
2327
|
+
orchestrateResult.iterations.length
|
|
2328
|
+
);
|
|
2329
|
+
yield* display.status(completion.message, completion.severity);
|
|
2330
|
+
for (const line of buildContextWindowLines(
|
|
2331
|
+
orchestrateResult.iterations
|
|
2332
|
+
)) {
|
|
2333
|
+
yield* display.text(line);
|
|
2334
|
+
}
|
|
2335
|
+
return orchestrateResult;
|
|
2336
|
+
}).pipe(
|
|
2337
|
+
Effect_exports.provide(runLayer),
|
|
2338
|
+
// Always close sandbox handle
|
|
2339
|
+
Effect_exports.ensuring(Effect_exports.promise(() => handle.close().catch(() => {
|
|
2340
|
+
})))
|
|
2341
|
+
);
|
|
2342
|
+
return {
|
|
2343
|
+
iterations: result.iterations,
|
|
2344
|
+
completionSignal: result.completionSignal,
|
|
2345
|
+
stdout: result.stdout,
|
|
2346
|
+
commits: result.commits,
|
|
2347
|
+
branch: result.branch,
|
|
2348
|
+
logFilePath: resolvedLogging.type === "file" ? resolvedLogging.path : void 0
|
|
2349
|
+
};
|
|
2350
|
+
});
|
|
2351
|
+
try {
|
|
2352
|
+
return await Effect_exports.runPromise(
|
|
2353
|
+
inner.pipe(
|
|
2354
|
+
Effect_exports.provide(ClackDisplay.layer),
|
|
2355
|
+
Effect_exports.provide(NodeContext_exports.layer),
|
|
2356
|
+
Effect_exports.provide(NodeFileSystem_exports.layer)
|
|
2357
|
+
)
|
|
2358
|
+
);
|
|
2359
|
+
} catch (error) {
|
|
2360
|
+
opts.signal?.throwIfAborted();
|
|
2361
|
+
throw error;
|
|
2362
|
+
}
|
|
2363
|
+
};
|
|
2364
|
+
const worktreeCreateSandbox = async (opts) => {
|
|
2365
|
+
return createSandboxFromWorktree({
|
|
2366
|
+
branch: worktreeInfo.branch,
|
|
2367
|
+
worktreePath: worktreeInfo.path,
|
|
2368
|
+
hostRepoDir,
|
|
2369
|
+
sandbox: opts.sandbox,
|
|
2370
|
+
hooks: opts.hooks,
|
|
2371
|
+
copyToWorktree: opts.copyToWorktree,
|
|
2372
|
+
timeouts: opts.timeouts,
|
|
2373
|
+
_test: opts._test
|
|
2374
|
+
});
|
|
2375
|
+
};
|
|
2376
|
+
return {
|
|
2377
|
+
branch: worktreeInfo.branch,
|
|
2378
|
+
worktreePath: worktreeInfo.path,
|
|
2379
|
+
run: worktreeRun,
|
|
2380
|
+
interactive: worktreeInteractive,
|
|
2381
|
+
createSandbox: worktreeCreateSandbox,
|
|
2382
|
+
close,
|
|
2383
|
+
async [Symbol.asyncDispose]() {
|
|
2384
|
+
await close();
|
|
2385
|
+
}
|
|
2386
|
+
};
|
|
2387
|
+
};
|
|
2388
|
+
var pathExists = async (path2) => {
|
|
2389
|
+
try {
|
|
2390
|
+
await access(path2);
|
|
2391
|
+
return true;
|
|
2392
|
+
} catch {
|
|
2393
|
+
return false;
|
|
2394
|
+
}
|
|
2395
|
+
};
|
|
2396
|
+
var encodeProjectPath = (cwd) => {
|
|
2397
|
+
const isRoot = cwd === "/" || /^[A-Za-z]:[\\/]?$/.test(cwd);
|
|
2398
|
+
const normalized = isRoot ? cwd : cwd.replace(/[\\/]+$/, "");
|
|
2399
|
+
return normalized.replace(/^([A-Za-z]):/, "$1").replace(/[\\/]/g, "-");
|
|
2400
|
+
};
|
|
2401
|
+
var claudeHostSessionPath = (cwd, id, projectsDir) => {
|
|
2402
|
+
const base = projectsDir ?? join(process.env.HOME ?? "~", ".claude", "projects");
|
|
2403
|
+
return join(base, encodeProjectPath(cwd), `${id}.jsonl`);
|
|
2404
|
+
};
|
|
2405
|
+
var claudeSandboxSessionPath = (cwd, id, projectsDir) => posix.join(projectsDir, encodeProjectPath(cwd), `${id}.jsonl`);
|
|
2406
|
+
var findClaudeSessionOnHost = async (id, projectsDir) => {
|
|
2407
|
+
const root = projectsDir ?? join(process.env.HOME ?? "~", ".claude", "projects");
|
|
2408
|
+
let entries;
|
|
2409
|
+
try {
|
|
2410
|
+
entries = await readdir(root, { withFileTypes: true });
|
|
2411
|
+
} catch {
|
|
2412
|
+
return { path: void 0, searchedRoot: root };
|
|
2413
|
+
}
|
|
2414
|
+
for (const entry of entries) {
|
|
2415
|
+
if (!entry.isDirectory()) continue;
|
|
2416
|
+
const candidate = join(root, entry.name, `${id}.jsonl`);
|
|
2417
|
+
if (await pathExists(candidate)) {
|
|
2418
|
+
return { path: candidate, searchedRoot: root };
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
return { path: void 0, searchedRoot: root };
|
|
2422
|
+
};
|
|
2423
|
+
var rewriteSessionCwd = (content, fromCwd, toCwd) => {
|
|
2424
|
+
if (content === "") return "";
|
|
2425
|
+
return content.split("\n").map((line) => {
|
|
2426
|
+
if (line === "") return line;
|
|
2427
|
+
const entry = JSON.parse(line);
|
|
2428
|
+
if (typeof entry.cwd === "string" && entry.cwd === fromCwd) {
|
|
2429
|
+
entry.cwd = toCwd;
|
|
2430
|
+
}
|
|
2431
|
+
if (entry.type === "session_meta" && typeof entry.payload === "object" && entry.payload !== null && typeof entry.payload.cwd === "string" && entry.payload.cwd === fromCwd) {
|
|
2432
|
+
entry.payload.cwd = toCwd;
|
|
2433
|
+
}
|
|
2434
|
+
return JSON.stringify(entry);
|
|
2435
|
+
}).join("\n");
|
|
2436
|
+
};
|
|
2437
|
+
var transferClaudeSession = (jsonl, fromCwd, toCwd) => rewriteSessionCwd(jsonl, fromCwd, toCwd);
|
|
2438
|
+
var isCodexSessionFilename = (filename, id) => filename.startsWith("rollout-") && filename.endsWith(`-${id}.jsonl`);
|
|
2439
|
+
var findCodexSessionPath = async (rootDir, id) => {
|
|
2440
|
+
const visit = async (dir) => {
|
|
2441
|
+
let entries;
|
|
2442
|
+
try {
|
|
2443
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
2444
|
+
} catch {
|
|
2445
|
+
return void 0;
|
|
2446
|
+
}
|
|
2447
|
+
for (const entry of entries) {
|
|
2448
|
+
const child = join(dir, entry.name);
|
|
2449
|
+
if (entry.isFile() && isCodexSessionFilename(entry.name, id)) {
|
|
2450
|
+
return child;
|
|
2451
|
+
}
|
|
2452
|
+
if (entry.isDirectory()) {
|
|
2453
|
+
const found = await visit(child);
|
|
2454
|
+
if (found) return found;
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
return void 0;
|
|
2458
|
+
};
|
|
2459
|
+
return visit(rootDir);
|
|
2460
|
+
};
|
|
2461
|
+
var findCodexSessionOnHost = async (id, sessionsDir) => {
|
|
2462
|
+
const root = sessionsDir ?? join(process.env.HOME ?? "~", ".codex", "sessions");
|
|
2463
|
+
const path2 = await findCodexSessionPath(root, id);
|
|
2464
|
+
return { path: path2, searchedRoot: root };
|
|
2465
|
+
};
|
|
2466
|
+
var locateCodexHostSession = async (id, sessionsDir) => {
|
|
2467
|
+
const root = sessionsDir ?? join(process.env.HOME ?? "~", ".codex", "sessions");
|
|
2468
|
+
const path2 = await findCodexSessionPath(root, id);
|
|
2469
|
+
if (!path2) throw new Error(`session ${id} not found in ${root}`);
|
|
2470
|
+
return { path: path2, relativePath: relative(root, path2) };
|
|
2471
|
+
};
|
|
2472
|
+
var locateCodexSandboxSession = async (id, handle, sessionsDir) => {
|
|
2473
|
+
const result = await handle.exec(
|
|
2474
|
+
`find ${JSON.stringify(sessionsDir)} -type f -name ${JSON.stringify(`rollout-*-${id}.jsonl`)} -print -quit`
|
|
2475
|
+
);
|
|
2476
|
+
const path2 = result.stdout.trim().split("\n")[0];
|
|
2477
|
+
if (result.exitCode !== 0 || !path2) {
|
|
2478
|
+
throw new Error(`session ${id} not found in ${sessionsDir}`);
|
|
2479
|
+
}
|
|
2480
|
+
return { path: path2, relativePath: posix.relative(sessionsDir, path2) };
|
|
2481
|
+
};
|
|
2482
|
+
var transferCodexSession = (jsonl, fromCwd, toCwd) => rewriteSessionCwd(jsonl, fromCwd, toCwd);
|
|
2483
|
+
var encodePiSessionDir = (cwd) => {
|
|
2484
|
+
const stripped = cwd.replace(/^[/\\]/, "");
|
|
2485
|
+
const replaced = stripped.replace(/[/\\:]/g, "-");
|
|
2486
|
+
return `--${replaced}--`;
|
|
2487
|
+
};
|
|
2488
|
+
var piSessionDirPath = (cwd, sessionsDir) => {
|
|
2489
|
+
const base = sessionsDir ?? join(process.env.HOME ?? "~", ".pi", "agent", "sessions");
|
|
2490
|
+
return join(base, encodePiSessionDir(cwd));
|
|
2491
|
+
};
|
|
2492
|
+
var isPiSessionFilename = (filename, id) => filename.endsWith(`_${id}.jsonl`);
|
|
2493
|
+
var findPiSessionPath = async (rootDir, id) => {
|
|
2494
|
+
let entries;
|
|
2495
|
+
try {
|
|
2496
|
+
entries = await readdir(rootDir, { withFileTypes: true });
|
|
2497
|
+
} catch {
|
|
2498
|
+
return void 0;
|
|
2499
|
+
}
|
|
2500
|
+
for (const entry of entries) {
|
|
2501
|
+
if (!entry.isDirectory()) continue;
|
|
2502
|
+
const dirAbs = join(rootDir, entry.name);
|
|
2503
|
+
let files;
|
|
2504
|
+
try {
|
|
2505
|
+
files = await readdir(dirAbs);
|
|
2506
|
+
} catch {
|
|
2507
|
+
continue;
|
|
2508
|
+
}
|
|
2509
|
+
const match = files.find((name) => isPiSessionFilename(name, id));
|
|
2510
|
+
if (match) {
|
|
2511
|
+
return {
|
|
2512
|
+
path: join(dirAbs, match),
|
|
2513
|
+
relativePath: join(entry.name, match)
|
|
2514
|
+
};
|
|
2515
|
+
}
|
|
2516
|
+
}
|
|
2517
|
+
return void 0;
|
|
2518
|
+
};
|
|
2519
|
+
var findPiSessionOnHost = async (id, sessionsDir) => {
|
|
2520
|
+
const root = sessionsDir ?? join(process.env.HOME ?? "~", ".pi", "agent", "sessions");
|
|
2521
|
+
const found = await findPiSessionPath(root, id);
|
|
2522
|
+
return { path: found?.path, searchedRoot: root };
|
|
2523
|
+
};
|
|
2524
|
+
var locatePiHostSession = async (id, sessionsDir) => {
|
|
2525
|
+
const root = sessionsDir ?? join(process.env.HOME ?? "~", ".pi", "agent", "sessions");
|
|
2526
|
+
const found = await findPiSessionPath(root, id);
|
|
2527
|
+
if (!found) throw new Error(`session ${id} not found in ${root}`);
|
|
2528
|
+
return found;
|
|
2529
|
+
};
|
|
2530
|
+
var locatePiSandboxSession = async (id, handle, sessionsDir) => {
|
|
2531
|
+
const result = await handle.exec(
|
|
2532
|
+
`find ${JSON.stringify(sessionsDir)} -type f -name ${JSON.stringify(`*_${id}.jsonl`)} -print -quit`
|
|
2533
|
+
);
|
|
2534
|
+
const path2 = result.stdout.trim().split("\n")[0];
|
|
2535
|
+
if (result.exitCode !== 0 || !path2) {
|
|
2536
|
+
throw new Error(`session ${id} not found in ${sessionsDir}`);
|
|
2537
|
+
}
|
|
2538
|
+
return { path: path2, relativePath: posix.relative(sessionsDir, path2) };
|
|
2539
|
+
};
|
|
2540
|
+
var transferPiSession = (jsonl, fromCwd, toCwd) => {
|
|
2541
|
+
if (jsonl === "") return "";
|
|
2542
|
+
return jsonl.split("\n").map((line) => {
|
|
2543
|
+
if (line === "") return line;
|
|
2544
|
+
try {
|
|
2545
|
+
const entry = JSON.parse(line);
|
|
2546
|
+
if (entry.type === "session" && typeof entry.cwd === "string" && entry.cwd === fromCwd) {
|
|
2547
|
+
entry.cwd = toCwd;
|
|
2548
|
+
return JSON.stringify(entry);
|
|
2549
|
+
}
|
|
2550
|
+
return line;
|
|
2551
|
+
} catch {
|
|
2552
|
+
return line;
|
|
2553
|
+
}
|
|
2554
|
+
}).join("\n");
|
|
2555
|
+
};
|
|
2556
|
+
|
|
2557
|
+
// src/CwdError.ts
|
|
2558
|
+
var CwdError2 = CwdError;
|
|
2559
|
+
var fileExists = async (path2) => {
|
|
2560
|
+
try {
|
|
2561
|
+
await access(path2);
|
|
2562
|
+
return true;
|
|
2563
|
+
} catch {
|
|
2564
|
+
return false;
|
|
2565
|
+
}
|
|
2566
|
+
};
|
|
2567
|
+
var shellEscape = (s) => "'" + s.replace(/'/g, "'\\''") + "'";
|
|
2568
|
+
var TOOL_ARG_FIELDS = {
|
|
2569
|
+
Bash: "command",
|
|
2570
|
+
WebSearch: "query",
|
|
2571
|
+
WebFetch: "url",
|
|
2572
|
+
Agent: "description"
|
|
2573
|
+
};
|
|
2574
|
+
var extractErrorMessage = (obj) => {
|
|
2575
|
+
const err = obj.error;
|
|
2576
|
+
if (typeof err === "string") return err;
|
|
2577
|
+
if (typeof err === "object" && err !== null) {
|
|
2578
|
+
if (typeof err.message === "string") return err.message;
|
|
2579
|
+
if (typeof err.data?.message === "string") return err.data.message;
|
|
2580
|
+
}
|
|
2581
|
+
if (typeof obj.message === "string") return obj.message;
|
|
2582
|
+
return void 0;
|
|
2583
|
+
};
|
|
2584
|
+
var parseStreamJsonLine = (line) => {
|
|
2585
|
+
if (!line.startsWith("{")) return [];
|
|
2586
|
+
try {
|
|
2587
|
+
const obj = JSON.parse(line);
|
|
2588
|
+
if (obj.type === "assistant" && Array.isArray(obj.message?.content)) {
|
|
2589
|
+
const events = [];
|
|
2590
|
+
const texts = [];
|
|
2591
|
+
for (const block of obj.message.content) {
|
|
2592
|
+
if (block.type === "text" && typeof block.text === "string") {
|
|
2593
|
+
texts.push(block.text);
|
|
2594
|
+
} else if (block.type === "tool_use" && typeof block.name === "string" && block.input !== void 0) {
|
|
2595
|
+
const argField = TOOL_ARG_FIELDS[block.name];
|
|
2596
|
+
if (argField === void 0) continue;
|
|
2597
|
+
const argValue = block.input[argField];
|
|
2598
|
+
if (typeof argValue !== "string") continue;
|
|
2599
|
+
if (texts.length > 0) {
|
|
2600
|
+
events.push({ type: "text", text: texts.join("") });
|
|
2601
|
+
texts.length = 0;
|
|
2602
|
+
}
|
|
2603
|
+
events.push({
|
|
2604
|
+
type: "tool_call",
|
|
2605
|
+
name: block.name,
|
|
2606
|
+
args: argValue
|
|
2607
|
+
});
|
|
2608
|
+
}
|
|
2609
|
+
}
|
|
2610
|
+
if (texts.length > 0) {
|
|
2611
|
+
events.push({ type: "text", text: texts.join("") });
|
|
2612
|
+
}
|
|
2613
|
+
return events;
|
|
2614
|
+
}
|
|
2615
|
+
if (obj.type === "result" && typeof obj.result === "string") {
|
|
2616
|
+
return [{ type: "result", result: obj.result }];
|
|
2617
|
+
}
|
|
2618
|
+
if (obj.type === "system" && obj.subtype === "init" && typeof obj.session_id === "string") {
|
|
2619
|
+
return [{ type: "session_id", sessionId: obj.session_id }];
|
|
2620
|
+
}
|
|
2621
|
+
} catch {
|
|
2622
|
+
}
|
|
2623
|
+
return [];
|
|
2624
|
+
};
|
|
2625
|
+
var CURSOR_PRINT_PROMPT_MAX_BYTES = 120 * 1024;
|
|
2626
|
+
function assertCursorPrintPromptFitsArgv(prompt) {
|
|
2627
|
+
const n = Buffer.byteLength(prompt, "utf8");
|
|
2628
|
+
if (n > CURSOR_PRINT_PROMPT_MAX_BYTES) {
|
|
2629
|
+
throw new Error(
|
|
2630
|
+
`Cursor print-mode prompt is ${n} bytes (max ${CURSOR_PRINT_PROMPT_MAX_BYTES} bytes). The Cursor CLI accepts the prompt only as a command-line argument; shorten the prompt or split the work. Other Sandcastle providers use stdin for large prompts.`
|
|
2631
|
+
);
|
|
2632
|
+
}
|
|
2633
|
+
}
|
|
2634
|
+
var parseCursorToolCallStarted = (obj) => {
|
|
2635
|
+
if (obj.type !== "tool_call" || obj.subtype !== "started") return [];
|
|
2636
|
+
const toolCall = obj.tool_call;
|
|
2637
|
+
if (!toolCall || typeof toolCall !== "object") return [];
|
|
2638
|
+
const tc = toolCall;
|
|
2639
|
+
const readToolCall = tc.readToolCall;
|
|
2640
|
+
if (readToolCall?.args && typeof readToolCall.args.path === "string") {
|
|
2641
|
+
return [{ type: "tool_call", name: "Read", args: readToolCall.args.path }];
|
|
2642
|
+
}
|
|
2643
|
+
const writeToolCall = tc.writeToolCall;
|
|
2644
|
+
if (writeToolCall?.args && typeof writeToolCall.args.path === "string") {
|
|
2645
|
+
return [
|
|
2646
|
+
{ type: "tool_call", name: "Write", args: writeToolCall.args.path }
|
|
2647
|
+
];
|
|
2648
|
+
}
|
|
2649
|
+
const fn = tc.function;
|
|
2650
|
+
if (fn && typeof fn.name === "string") {
|
|
2651
|
+
const rawArgs = typeof fn.arguments === "string" ? fn.arguments : "";
|
|
2652
|
+
if (rawArgs) {
|
|
2653
|
+
try {
|
|
2654
|
+
const parsedArgs = JSON.parse(rawArgs);
|
|
2655
|
+
if (typeof parsedArgs.command === "string") {
|
|
2656
|
+
return [
|
|
2657
|
+
{ type: "tool_call", name: "Bash", args: parsedArgs.command }
|
|
2658
|
+
];
|
|
2659
|
+
}
|
|
2660
|
+
} catch {
|
|
2661
|
+
}
|
|
2662
|
+
return [{ type: "tool_call", name: fn.name, args: rawArgs }];
|
|
2663
|
+
}
|
|
2664
|
+
return [{ type: "tool_call", name: fn.name, args: "" }];
|
|
2665
|
+
}
|
|
2666
|
+
return [];
|
|
2667
|
+
};
|
|
2668
|
+
var parseCursorStreamLine = (line) => {
|
|
2669
|
+
if (!line.startsWith("{")) return [];
|
|
2670
|
+
let obj;
|
|
2671
|
+
try {
|
|
2672
|
+
obj = JSON.parse(line);
|
|
2673
|
+
} catch {
|
|
2674
|
+
return [];
|
|
2675
|
+
}
|
|
2676
|
+
if (obj.type === "tool_call") {
|
|
2677
|
+
return parseCursorToolCallStarted(obj);
|
|
2678
|
+
}
|
|
2679
|
+
return parseStreamJsonLine(line);
|
|
2680
|
+
};
|
|
2681
|
+
var readSandboxFile = async (handle, sandboxPath, tag) => {
|
|
2682
|
+
const tmpPath = join(
|
|
2683
|
+
tmpdir(),
|
|
2684
|
+
`sandcastle-${tag}-${Date.now()}-${Math.random().toString(36).slice(2)}.jsonl`
|
|
2685
|
+
);
|
|
2686
|
+
await handle.copyFileOut(sandboxPath, tmpPath);
|
|
2687
|
+
try {
|
|
2688
|
+
return await readFile(tmpPath, "utf-8");
|
|
2689
|
+
} finally {
|
|
2690
|
+
await rm(tmpPath, { force: true }).catch(() => {
|
|
2691
|
+
});
|
|
2692
|
+
}
|
|
2693
|
+
};
|
|
2694
|
+
var writeSandboxFile = async (handle, sandboxPath, content, tag) => {
|
|
2695
|
+
const tmpPath = join(
|
|
2696
|
+
tmpdir(),
|
|
2697
|
+
`sandcastle-${tag}-${Date.now()}-${Math.random().toString(36).slice(2)}.jsonl`
|
|
2698
|
+
);
|
|
2699
|
+
await writeFile(tmpPath, content);
|
|
2700
|
+
try {
|
|
2701
|
+
await handle.exec(`mkdir -p ${JSON.stringify(posix.dirname(sandboxPath))}`);
|
|
2702
|
+
await handle.copyFileIn(tmpPath, sandboxPath);
|
|
2703
|
+
} finally {
|
|
2704
|
+
await rm(tmpPath, { force: true }).catch(() => {
|
|
2705
|
+
});
|
|
2706
|
+
}
|
|
2707
|
+
};
|
|
2708
|
+
var makeClaudeSessionStorage = (options) => {
|
|
2709
|
+
const hostProjectsDir = options?.sessionStorage?.hostProjectsDir;
|
|
2710
|
+
const sandboxProjectsDir = options?.sessionStorage?.sandboxProjectsDir ?? "/home/agent/.claude/projects";
|
|
2711
|
+
return {
|
|
2712
|
+
hostSessionFilePath: (cwd, id) => claudeHostSessionPath(cwd, id, hostProjectsDir),
|
|
2713
|
+
existsOnHost: (cwd, id) => fileExists(claudeHostSessionPath(cwd, id, hostProjectsDir)),
|
|
2714
|
+
readHostSession: async (cwd, id) => {
|
|
2715
|
+
const path2 = claudeHostSessionPath(cwd, id, hostProjectsDir);
|
|
2716
|
+
if (!await fileExists(path2)) return void 0;
|
|
2717
|
+
return readFile(path2, "utf-8");
|
|
2718
|
+
},
|
|
2719
|
+
captureToHost: async ({ hostCwd, sandboxCwd, sessionId, handle }) => {
|
|
2720
|
+
const sandboxPath = claudeSandboxSessionPath(
|
|
2721
|
+
sandboxCwd,
|
|
2722
|
+
sessionId,
|
|
2723
|
+
sandboxProjectsDir
|
|
2724
|
+
);
|
|
2725
|
+
const jsonl = await readSandboxFile(handle, sandboxPath, "claude-cap");
|
|
2726
|
+
const rewritten = transferClaudeSession(jsonl, sandboxCwd, hostCwd);
|
|
2727
|
+
const hostPath = claudeHostSessionPath(
|
|
2728
|
+
hostCwd,
|
|
2729
|
+
sessionId,
|
|
2730
|
+
hostProjectsDir
|
|
2731
|
+
);
|
|
2732
|
+
await mkdir(dirname(hostPath), { recursive: true });
|
|
2733
|
+
await writeFile(hostPath, rewritten);
|
|
2734
|
+
},
|
|
2735
|
+
resumeIntoSandbox: async ({ hostCwd, sandboxCwd, sessionId, handle }) => {
|
|
2736
|
+
const hostPath = claudeHostSessionPath(
|
|
2737
|
+
hostCwd,
|
|
2738
|
+
sessionId,
|
|
2739
|
+
hostProjectsDir
|
|
2740
|
+
);
|
|
2741
|
+
const jsonl = await readFile(hostPath, "utf-8");
|
|
2742
|
+
const rewritten = transferClaudeSession(jsonl, hostCwd, sandboxCwd);
|
|
2743
|
+
const sandboxPath = claudeSandboxSessionPath(
|
|
2744
|
+
sandboxCwd,
|
|
2745
|
+
sessionId,
|
|
2746
|
+
sandboxProjectsDir
|
|
2747
|
+
);
|
|
2748
|
+
await writeSandboxFile(handle, sandboxPath, rewritten, "claude-res");
|
|
2749
|
+
},
|
|
2750
|
+
findByIdOnHost: (id) => findClaudeSessionOnHost(id, hostProjectsDir)
|
|
2751
|
+
};
|
|
2752
|
+
};
|
|
2753
|
+
var makeCodexSessionStorage = (options) => {
|
|
2754
|
+
const hostSessionsDir = options?.sessionStorage?.hostSessionsDir;
|
|
2755
|
+
const sandboxSessionsDir = options?.sessionStorage?.sandboxSessionsDir ?? posix.join("/home/agent", ".codex", "sessions");
|
|
2756
|
+
const capturedPaths = /* @__PURE__ */ new Map();
|
|
2757
|
+
return {
|
|
2758
|
+
hostSessionFilePath: (_cwd, id) => capturedPaths.get(id),
|
|
2759
|
+
existsOnHost: async (_cwd, id) => {
|
|
2760
|
+
const found = await findCodexSessionOnHost(id, hostSessionsDir);
|
|
2761
|
+
return found.path !== void 0;
|
|
2762
|
+
},
|
|
2763
|
+
readHostSession: async (_cwd, id) => {
|
|
2764
|
+
const found = await findCodexSessionOnHost(id, hostSessionsDir);
|
|
2765
|
+
if (!found.path) return void 0;
|
|
2766
|
+
return readFile(found.path, "utf-8");
|
|
2767
|
+
},
|
|
2768
|
+
captureToHost: async ({ hostCwd, sandboxCwd, sessionId, handle }) => {
|
|
2769
|
+
const located = await locateCodexSandboxSession(
|
|
2770
|
+
sessionId,
|
|
2771
|
+
handle,
|
|
2772
|
+
sandboxSessionsDir
|
|
2773
|
+
);
|
|
2774
|
+
const jsonl = await readSandboxFile(handle, located.path, "codex-cap");
|
|
2775
|
+
const rewritten = transferCodexSession(jsonl, sandboxCwd, hostCwd);
|
|
2776
|
+
const root = hostSessionsDir ?? join(process.env.HOME ?? "~", ".codex", "sessions");
|
|
2777
|
+
const target = join(root, located.relativePath);
|
|
2778
|
+
await mkdir(dirname(target), { recursive: true });
|
|
2779
|
+
await writeFile(target, rewritten);
|
|
2780
|
+
capturedPaths.set(sessionId, target);
|
|
2781
|
+
},
|
|
2782
|
+
resumeIntoSandbox: async ({ hostCwd, sandboxCwd, sessionId, handle }) => {
|
|
2783
|
+
const located = await locateCodexHostSession(sessionId, hostSessionsDir);
|
|
2784
|
+
const jsonl = await readFile(located.path, "utf-8");
|
|
2785
|
+
const rewritten = transferCodexSession(jsonl, hostCwd, sandboxCwd);
|
|
2786
|
+
const target = posix.join(sandboxSessionsDir, located.relativePath);
|
|
2787
|
+
await writeSandboxFile(handle, target, rewritten, "codex-res");
|
|
2788
|
+
},
|
|
2789
|
+
findByIdOnHost: (id) => findCodexSessionOnHost(id, hostSessionsDir)
|
|
2790
|
+
};
|
|
2791
|
+
};
|
|
2792
|
+
var makePiSessionStorage = (options) => {
|
|
2793
|
+
const hostSessionsDir = options?.sessionStorage?.hostSessionsDir;
|
|
2794
|
+
const sandboxSessionsDir = options?.sessionStorage?.sandboxSessionsDir ?? posix.join("/home/agent", ".pi", "agent", "sessions");
|
|
2795
|
+
return {
|
|
2796
|
+
hostSessionFilePath: (cwd, _id) => piSessionDirPath(cwd, hostSessionsDir),
|
|
2797
|
+
existsOnHost: async (_cwd, id) => {
|
|
2798
|
+
const found = await findPiSessionOnHost(id, hostSessionsDir);
|
|
2799
|
+
return found.path !== void 0;
|
|
2800
|
+
},
|
|
2801
|
+
readHostSession: async (_cwd, id) => {
|
|
2802
|
+
const found = await findPiSessionOnHost(id, hostSessionsDir);
|
|
2803
|
+
if (!found.path) return void 0;
|
|
2804
|
+
return readFile(found.path, "utf-8");
|
|
2805
|
+
},
|
|
2806
|
+
captureToHost: async ({ hostCwd, sandboxCwd, sessionId, handle }) => {
|
|
2807
|
+
const located = await locatePiSandboxSession(
|
|
2808
|
+
sessionId,
|
|
2809
|
+
handle,
|
|
2810
|
+
sandboxSessionsDir
|
|
2811
|
+
);
|
|
2812
|
+
const jsonl = await readSandboxFile(handle, located.path, "pi-cap");
|
|
2813
|
+
const rewritten = transferPiSession(jsonl, sandboxCwd, hostCwd);
|
|
2814
|
+
const filename = posix.basename(located.path);
|
|
2815
|
+
const target = join(piSessionDirPath(hostCwd, hostSessionsDir), filename);
|
|
2816
|
+
await mkdir(dirname(target), { recursive: true });
|
|
2817
|
+
await writeFile(target, rewritten);
|
|
2818
|
+
},
|
|
2819
|
+
resumeIntoSandbox: async ({ hostCwd, sandboxCwd, sessionId, handle }) => {
|
|
2820
|
+
const located = await locatePiHostSession(sessionId, hostSessionsDir);
|
|
2821
|
+
const jsonl = await readFile(located.path, "utf-8");
|
|
2822
|
+
const rewritten = transferPiSession(jsonl, hostCwd, sandboxCwd);
|
|
2823
|
+
const filename = located.relativePath.split(/[\\/]/).pop();
|
|
2824
|
+
const target = posix.join(
|
|
2825
|
+
sandboxSessionsDir,
|
|
2826
|
+
encodePiSessionDir(sandboxCwd),
|
|
2827
|
+
filename
|
|
2828
|
+
);
|
|
2829
|
+
await writeSandboxFile(handle, target, rewritten, "pi-res");
|
|
2830
|
+
},
|
|
2831
|
+
findByIdOnHost: (id) => findPiSessionOnHost(id, hostSessionsDir)
|
|
2832
|
+
};
|
|
2833
|
+
};
|
|
2834
|
+
var parsePiStreamLine = (line) => {
|
|
2835
|
+
if (!line.startsWith("{")) return [];
|
|
2836
|
+
try {
|
|
2837
|
+
const obj = JSON.parse(line);
|
|
2838
|
+
if (obj.type === "session" && typeof obj.id === "string") {
|
|
2839
|
+
return [{ type: "session_id", sessionId: obj.id }];
|
|
2840
|
+
}
|
|
2841
|
+
if (obj.type === "message_update" && obj.assistantMessageEvent) {
|
|
2842
|
+
const evt = obj.assistantMessageEvent;
|
|
2843
|
+
if (evt.type === "text_delta" && typeof evt.delta === "string") {
|
|
2844
|
+
return [{ type: "text", text: evt.delta }];
|
|
2845
|
+
}
|
|
2846
|
+
return [];
|
|
2847
|
+
}
|
|
2848
|
+
if (obj.type === "tool_execution_start") {
|
|
2849
|
+
const toolName = obj.toolName;
|
|
2850
|
+
if (typeof toolName !== "string") return [];
|
|
2851
|
+
const argField = TOOL_ARG_FIELDS[toolName];
|
|
2852
|
+
if (argField === void 0) return [];
|
|
2853
|
+
const args = obj.args;
|
|
2854
|
+
if (!args) return [];
|
|
2855
|
+
const argValue = args[argField];
|
|
2856
|
+
if (typeof argValue !== "string") return [];
|
|
2857
|
+
return [{ type: "tool_call", name: toolName, args: argValue }];
|
|
2858
|
+
}
|
|
2859
|
+
if (obj.type === "agent_error" || obj.type === "error") {
|
|
2860
|
+
const msg = extractErrorMessage(obj);
|
|
2861
|
+
return msg ? [{ type: "result", result: msg }] : [];
|
|
2862
|
+
}
|
|
2863
|
+
if (obj.type === "agent_end" && Array.isArray(obj.messages)) {
|
|
2864
|
+
const messages = obj.messages;
|
|
2865
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
2866
|
+
const msg = messages[i];
|
|
2867
|
+
if (msg?.role === "assistant") {
|
|
2868
|
+
const texts = [];
|
|
2869
|
+
for (const block of msg.content) {
|
|
2870
|
+
if (block.type === "text" && typeof block.text === "string") {
|
|
2871
|
+
texts.push(block.text);
|
|
2872
|
+
}
|
|
2873
|
+
}
|
|
2874
|
+
if (texts.length > 0) {
|
|
2875
|
+
return [{ type: "result", result: texts.join("") }];
|
|
2876
|
+
}
|
|
2877
|
+
break;
|
|
2878
|
+
}
|
|
2879
|
+
}
|
|
2880
|
+
return [];
|
|
2881
|
+
}
|
|
2882
|
+
} catch {
|
|
2883
|
+
}
|
|
2884
|
+
return [];
|
|
2885
|
+
};
|
|
2886
|
+
var pi = (model, options) => ({
|
|
2887
|
+
name: "pi",
|
|
2888
|
+
env: options?.env ?? {},
|
|
2889
|
+
captureSessions: options?.captureSessions ?? true,
|
|
2890
|
+
sessionStorage: makePiSessionStorage(options),
|
|
2891
|
+
buildPrintCommand({
|
|
2892
|
+
prompt,
|
|
2893
|
+
resumeSession
|
|
2894
|
+
}) {
|
|
2895
|
+
const thinkingFlag = options?.thinking ? ` --thinking ${options.thinking}` : "";
|
|
2896
|
+
const sessionFlag = resumeSession ? ` --session ${shellEscape(resumeSession)}` : "";
|
|
2897
|
+
return {
|
|
2898
|
+
command: `pi -p --mode json --model ${shellEscape(model)}${thinkingFlag}${sessionFlag}`,
|
|
2899
|
+
stdin: prompt
|
|
2900
|
+
};
|
|
2901
|
+
},
|
|
2902
|
+
buildInteractiveArgs({ prompt }) {
|
|
2903
|
+
const args = ["pi", "--model", model];
|
|
2904
|
+
if (prompt) args.push(prompt);
|
|
2905
|
+
return args;
|
|
2906
|
+
},
|
|
2907
|
+
parseStreamLine(line) {
|
|
2908
|
+
return parsePiStreamLine(line);
|
|
2909
|
+
}
|
|
2910
|
+
});
|
|
2911
|
+
var parseCodexUsage = (usage) => {
|
|
2912
|
+
if (typeof usage !== "object" || usage === null) return void 0;
|
|
2913
|
+
const u = usage;
|
|
2914
|
+
if (typeof u.input_tokens !== "number" || typeof u.cached_input_tokens !== "number" || typeof u.output_tokens !== "number") {
|
|
2915
|
+
return void 0;
|
|
2916
|
+
}
|
|
2917
|
+
return {
|
|
2918
|
+
inputTokens: u.input_tokens - u.cached_input_tokens,
|
|
2919
|
+
cacheCreationInputTokens: 0,
|
|
2920
|
+
cacheReadInputTokens: u.cached_input_tokens,
|
|
2921
|
+
outputTokens: u.output_tokens
|
|
2922
|
+
};
|
|
2923
|
+
};
|
|
2924
|
+
var parseCodexStreamLine = (line) => {
|
|
2925
|
+
if (!line.startsWith("{")) return [];
|
|
2926
|
+
try {
|
|
2927
|
+
const obj = JSON.parse(line);
|
|
2928
|
+
if (obj.type === "thread.started" && typeof obj.thread_id === "string") {
|
|
2929
|
+
return [{ type: "session_id", sessionId: obj.thread_id }];
|
|
2930
|
+
}
|
|
2931
|
+
if (obj.type === "item.completed" && obj.item?.type === "agent_message" && typeof obj.item.text === "string") {
|
|
2932
|
+
const text2 = obj.item.text;
|
|
2933
|
+
return [
|
|
2934
|
+
{ type: "text", text: text2 },
|
|
2935
|
+
{ type: "result", result: text2 }
|
|
2936
|
+
];
|
|
2937
|
+
}
|
|
2938
|
+
if (obj.type === "item.started" && obj.item?.type === "command_execution" && typeof obj.item.command === "string") {
|
|
2939
|
+
return [{ type: "tool_call", name: "Bash", args: obj.item.command }];
|
|
2940
|
+
}
|
|
2941
|
+
if (obj.type === "error") {
|
|
2942
|
+
const msg = extractErrorMessage(obj);
|
|
2943
|
+
return msg ? [{ type: "result", result: msg }] : [];
|
|
2944
|
+
}
|
|
2945
|
+
if (obj.type === "turn.completed") {
|
|
2946
|
+
const usage = parseCodexUsage(obj.usage);
|
|
2947
|
+
return usage ? [{ type: "usage", usage }] : [];
|
|
2948
|
+
}
|
|
2949
|
+
} catch {
|
|
2950
|
+
}
|
|
2951
|
+
return [];
|
|
2952
|
+
};
|
|
2953
|
+
var codex = (model, options) => ({
|
|
2954
|
+
name: "codex",
|
|
2955
|
+
env: options?.env ?? {},
|
|
2956
|
+
captureSessions: options?.captureSessions ?? true,
|
|
2957
|
+
sessionStorage: makeCodexSessionStorage(options),
|
|
2958
|
+
buildPrintCommand({
|
|
2959
|
+
prompt,
|
|
2960
|
+
resumeSession,
|
|
2961
|
+
forkSession
|
|
2962
|
+
}) {
|
|
2963
|
+
const effortFlag = options?.effort ? ` -c ${shellEscape(`model_reasoning_effort="${options.effort}"`)}` : "";
|
|
2964
|
+
let base;
|
|
2965
|
+
if (resumeSession && forkSession) {
|
|
2966
|
+
base = `codex exec fork ${shellEscape(resumeSession)}`;
|
|
2967
|
+
} else if (resumeSession) {
|
|
2968
|
+
base = `codex exec resume ${shellEscape(resumeSession)}`;
|
|
2969
|
+
} else {
|
|
2970
|
+
base = "codex exec";
|
|
2971
|
+
}
|
|
2972
|
+
const stdinArg = resumeSession ? " -" : "";
|
|
2973
|
+
return {
|
|
2974
|
+
command: `${base} --json --dangerously-bypass-approvals-and-sandbox -m ${shellEscape(model)}${effortFlag}${stdinArg}`,
|
|
2975
|
+
stdin: prompt
|
|
2976
|
+
};
|
|
2977
|
+
},
|
|
2978
|
+
buildInteractiveArgs({ prompt }) {
|
|
2979
|
+
const args = ["codex", "--model", model];
|
|
2980
|
+
if (prompt) args.push(prompt);
|
|
2981
|
+
return args;
|
|
2982
|
+
},
|
|
2983
|
+
parseStreamLine(line) {
|
|
2984
|
+
return parseCodexStreamLine(line);
|
|
2985
|
+
}
|
|
2986
|
+
});
|
|
2987
|
+
var cursor = (model, options) => ({
|
|
2988
|
+
name: "cursor",
|
|
2989
|
+
env: options?.env ?? {},
|
|
2990
|
+
captureSessions: false,
|
|
2991
|
+
// Cursor has no filesystem-backed session storage (captureSessions: false, no
|
|
2992
|
+
// sessionStorage), so it is non-resumable per ADR 0012/0016. resumeSession is
|
|
2993
|
+
// ignored here — like pi and opencode — rather than wired to --resume.
|
|
2994
|
+
buildPrintCommand({
|
|
2995
|
+
prompt,
|
|
2996
|
+
dangerouslySkipPermissions
|
|
2997
|
+
}) {
|
|
2998
|
+
assertCursorPrintPromptFitsArgv(prompt);
|
|
2999
|
+
const forceFlag = dangerouslySkipPermissions ? " --force" : "";
|
|
3000
|
+
return {
|
|
3001
|
+
command: `agent --print --output-format stream-json --model ${shellEscape(model)} ${forceFlag} ${shellEscape(prompt)}`
|
|
3002
|
+
};
|
|
3003
|
+
},
|
|
3004
|
+
buildInteractiveArgs({
|
|
3005
|
+
prompt,
|
|
3006
|
+
dangerouslySkipPermissions
|
|
3007
|
+
}) {
|
|
3008
|
+
const args = ["agent", "--model", model];
|
|
3009
|
+
if (dangerouslySkipPermissions) args.push("--force");
|
|
3010
|
+
if (prompt) args.push(prompt);
|
|
3011
|
+
return args;
|
|
3012
|
+
},
|
|
3013
|
+
parseStreamLine(line) {
|
|
3014
|
+
return parseCursorStreamLine(line);
|
|
3015
|
+
}
|
|
3016
|
+
});
|
|
3017
|
+
var OPENCODE_TOOL_ARG_FIELDS = {
|
|
3018
|
+
bash: "command",
|
|
3019
|
+
webfetch: "url",
|
|
3020
|
+
task: "description"
|
|
3021
|
+
};
|
|
3022
|
+
var parseOpenCodeStreamLine = (line) => {
|
|
3023
|
+
if (!line.startsWith("{")) return [];
|
|
3024
|
+
try {
|
|
3025
|
+
const obj = JSON.parse(line);
|
|
3026
|
+
const part = obj.part;
|
|
3027
|
+
if (obj.type === "step_start" && typeof obj.sessionID === "string") {
|
|
3028
|
+
return [{ type: "session_id", sessionId: obj.sessionID }];
|
|
3029
|
+
}
|
|
3030
|
+
if (obj.type === "text" && part?.type === "text" && typeof part.text === "string") {
|
|
3031
|
+
return [
|
|
3032
|
+
{ type: "text", text: part.text },
|
|
3033
|
+
{ type: "result", result: part.text }
|
|
3034
|
+
];
|
|
3035
|
+
}
|
|
3036
|
+
if (obj.type === "tool_use" && part?.type === "tool") {
|
|
3037
|
+
if (typeof part.tool !== "string") return [];
|
|
3038
|
+
const state = part.state;
|
|
3039
|
+
if (state?.status !== "completed") return [];
|
|
3040
|
+
const input = state.input;
|
|
3041
|
+
if (!input) return [];
|
|
3042
|
+
const argField = OPENCODE_TOOL_ARG_FIELDS[part.tool];
|
|
3043
|
+
const argValue = argField !== void 0 ? input[argField] : void 0;
|
|
3044
|
+
const args = typeof argValue === "string" ? argValue : JSON.stringify(input);
|
|
3045
|
+
return [{ type: "tool_call", name: part.tool, args }];
|
|
3046
|
+
}
|
|
3047
|
+
if (obj.type === "error") {
|
|
3048
|
+
const msg = extractErrorMessage(obj);
|
|
3049
|
+
return msg ? [{ type: "result", result: msg }] : [];
|
|
3050
|
+
}
|
|
3051
|
+
} catch {
|
|
3052
|
+
}
|
|
3053
|
+
return [];
|
|
3054
|
+
};
|
|
3055
|
+
var opencode = (model, options) => ({
|
|
3056
|
+
name: "opencode",
|
|
3057
|
+
env: options?.env ?? {},
|
|
3058
|
+
captureSessions: false,
|
|
3059
|
+
buildPrintCommand({
|
|
3060
|
+
prompt,
|
|
3061
|
+
dangerouslySkipPermissions
|
|
3062
|
+
}) {
|
|
3063
|
+
const variantFlag = options?.variant ? ` --variant ${shellEscape(options.variant)}` : "";
|
|
3064
|
+
const agentFlag = options?.agent ? ` --agent ${shellEscape(options.agent)}` : "";
|
|
3065
|
+
const permissionsFlag = dangerouslySkipPermissions ? " --dangerously-skip-permissions" : "";
|
|
3066
|
+
return {
|
|
3067
|
+
command: `opencode run --format json --model ${shellEscape(model)}${variantFlag}${agentFlag}${permissionsFlag} ${shellEscape(prompt)}`
|
|
3068
|
+
};
|
|
3069
|
+
},
|
|
3070
|
+
buildInteractiveArgs({ prompt }) {
|
|
3071
|
+
const args = ["opencode", "--model", model];
|
|
3072
|
+
if (options?.agent) args.push("--agent", options.agent);
|
|
3073
|
+
if (prompt) args.push("-p", prompt);
|
|
3074
|
+
return args;
|
|
3075
|
+
},
|
|
3076
|
+
parseStreamLine(line) {
|
|
3077
|
+
return parseOpenCodeStreamLine(line);
|
|
3078
|
+
}
|
|
3079
|
+
});
|
|
3080
|
+
var COPILOT_PRINT_PROMPT_MAX_BYTES = 120 * 1024;
|
|
3081
|
+
function assertCopilotPrintPromptFitsArgv(prompt) {
|
|
3082
|
+
const n = Buffer.byteLength(prompt, "utf8");
|
|
3083
|
+
if (n > COPILOT_PRINT_PROMPT_MAX_BYTES) {
|
|
3084
|
+
throw new Error(
|
|
3085
|
+
`Copilot print-mode prompt is ${n} bytes (max ${COPILOT_PRINT_PROMPT_MAX_BYTES} bytes). This provider passes the prompt as a command-line argument; shorten the prompt or split the work. Other Sandcastle providers use stdin for large prompts.`
|
|
3086
|
+
);
|
|
3087
|
+
}
|
|
3088
|
+
}
|
|
3089
|
+
var parseCopilotStreamLine = (line) => {
|
|
3090
|
+
if (!line.startsWith("{")) return [];
|
|
3091
|
+
try {
|
|
3092
|
+
const obj = JSON.parse(line);
|
|
3093
|
+
if (obj.type === "assistant.message_delta" && typeof obj.data?.deltaContent === "string") {
|
|
3094
|
+
return [{ type: "text", text: obj.data.deltaContent }];
|
|
3095
|
+
}
|
|
3096
|
+
if (obj.type === "tool.execution_start") {
|
|
3097
|
+
const rawName = obj.data?.toolName;
|
|
3098
|
+
if (typeof rawName !== "string") return [];
|
|
3099
|
+
const toolName = rawName === "bash" ? "Bash" : rawName;
|
|
3100
|
+
const argField = TOOL_ARG_FIELDS[toolName];
|
|
3101
|
+
if (argField === void 0) return [];
|
|
3102
|
+
const args = obj.data?.arguments;
|
|
3103
|
+
if (!args) return [];
|
|
3104
|
+
const argValue = args[argField];
|
|
3105
|
+
if (typeof argValue !== "string") return [];
|
|
3106
|
+
return [{ type: "tool_call", name: toolName, args: argValue }];
|
|
3107
|
+
}
|
|
3108
|
+
if (obj.type === "assistant.message" && typeof obj.data?.content === "string" && obj.data.content.length > 0) {
|
|
3109
|
+
return [{ type: "result", result: obj.data.content }];
|
|
3110
|
+
}
|
|
3111
|
+
if (obj.type === "result" && typeof obj.sessionId === "string") {
|
|
3112
|
+
return [{ type: "session_id", sessionId: obj.sessionId }];
|
|
3113
|
+
}
|
|
3114
|
+
if (obj.type === "error" || obj.type === "agent_error") {
|
|
3115
|
+
const msg = extractErrorMessage(obj);
|
|
3116
|
+
return msg ? [{ type: "result", result: msg }] : [];
|
|
3117
|
+
}
|
|
3118
|
+
} catch {
|
|
3119
|
+
}
|
|
3120
|
+
return [];
|
|
3121
|
+
};
|
|
3122
|
+
var copilot = (model, options) => ({
|
|
3123
|
+
name: "copilot",
|
|
3124
|
+
env: options?.env ?? {},
|
|
3125
|
+
captureSessions: false,
|
|
3126
|
+
// Copilot CLI does expose `--resume <id>`, but its session state is indexed by
|
|
3127
|
+
// a SQLite database alongside the JSONL files in ~/.copilot/session-state/, so
|
|
3128
|
+
// transferring a single session file between host and sandbox is not enough to
|
|
3129
|
+
// make resume work (see ADR 0016). Until the round-trip is verified end-to-end,
|
|
3130
|
+
// copilot is non-resumable: captureSessions is false, there is no sessionStorage,
|
|
3131
|
+
// and resumeSession is ignored here — like cursor, pi, and opencode.
|
|
3132
|
+
buildPrintCommand({
|
|
3133
|
+
prompt,
|
|
3134
|
+
dangerouslySkipPermissions
|
|
3135
|
+
}) {
|
|
3136
|
+
assertCopilotPrintPromptFitsArgv(prompt);
|
|
3137
|
+
const allowAll = dangerouslySkipPermissions ? " --allow-all-tools" : "";
|
|
3138
|
+
const effortFlag = options?.effort ? ` --effort ${options.effort}` : "";
|
|
3139
|
+
return {
|
|
3140
|
+
command: `copilot -p ${shellEscape(prompt)} --output-format json --model ${shellEscape(model)}${allowAll}${effortFlag}`
|
|
3141
|
+
};
|
|
3142
|
+
},
|
|
3143
|
+
buildInteractiveArgs({ prompt }) {
|
|
3144
|
+
const args = ["copilot", "--model", model];
|
|
3145
|
+
if (prompt) args.push("-i", prompt);
|
|
3146
|
+
return args;
|
|
3147
|
+
},
|
|
3148
|
+
parseStreamLine(line) {
|
|
3149
|
+
return parseCopilotStreamLine(line);
|
|
3150
|
+
}
|
|
3151
|
+
});
|
|
3152
|
+
var claudeCode = (model, options) => ({
|
|
3153
|
+
name: "claude-code",
|
|
3154
|
+
env: options?.env ?? {},
|
|
3155
|
+
captureSessions: options?.captureSessions ?? true,
|
|
3156
|
+
sessionStorage: makeClaudeSessionStorage(options),
|
|
3157
|
+
buildPrintCommand({
|
|
3158
|
+
prompt,
|
|
3159
|
+
dangerouslySkipPermissions,
|
|
3160
|
+
resumeSession,
|
|
3161
|
+
forkSession
|
|
3162
|
+
}) {
|
|
3163
|
+
const skipPerms = dangerouslySkipPermissions ? " --dangerously-skip-permissions" : "";
|
|
3164
|
+
const effortFlag = options?.effort ? ` --effort ${options.effort}` : "";
|
|
3165
|
+
const resumeFlag = resumeSession ? ` --resume ${shellEscape(resumeSession)}` : "";
|
|
3166
|
+
const forkFlag = resumeSession && forkSession ? " --fork-session" : "";
|
|
3167
|
+
return {
|
|
3168
|
+
command: `claude --print --verbose${skipPerms} --output-format stream-json --model ${shellEscape(model)}${effortFlag}${resumeFlag}${forkFlag} -p -`,
|
|
3169
|
+
stdin: prompt
|
|
3170
|
+
};
|
|
3171
|
+
},
|
|
3172
|
+
buildInteractiveArgs({
|
|
3173
|
+
prompt,
|
|
3174
|
+
dangerouslySkipPermissions
|
|
3175
|
+
}) {
|
|
3176
|
+
const args = ["claude"];
|
|
3177
|
+
if (dangerouslySkipPermissions) args.push("--dangerously-skip-permissions");
|
|
3178
|
+
args.push("--model", model);
|
|
3179
|
+
if (options?.effort) args.push("--effort", options.effort);
|
|
3180
|
+
if (prompt) args.push(prompt);
|
|
3181
|
+
return args;
|
|
3182
|
+
},
|
|
3183
|
+
parseStreamLine(line) {
|
|
3184
|
+
return parseStreamJsonLine(line);
|
|
3185
|
+
},
|
|
3186
|
+
parseSessionUsage(content) {
|
|
3187
|
+
const lines = content.split("\n");
|
|
3188
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
3189
|
+
const line = lines[i];
|
|
3190
|
+
if (!line.startsWith("{")) continue;
|
|
3191
|
+
try {
|
|
3192
|
+
const obj = JSON.parse(line);
|
|
3193
|
+
if (obj.type === "assistant" && obj.message?.usage) {
|
|
3194
|
+
const u = obj.message.usage;
|
|
3195
|
+
if (typeof u.input_tokens === "number" && typeof u.cache_creation_input_tokens === "number" && typeof u.cache_read_input_tokens === "number" && typeof u.output_tokens === "number") {
|
|
3196
|
+
return {
|
|
3197
|
+
inputTokens: u.input_tokens,
|
|
3198
|
+
cacheCreationInputTokens: u.cache_creation_input_tokens,
|
|
3199
|
+
cacheReadInputTokens: u.cache_read_input_tokens,
|
|
3200
|
+
outputTokens: u.output_tokens
|
|
3201
|
+
};
|
|
3202
|
+
}
|
|
3203
|
+
}
|
|
3204
|
+
} catch {
|
|
3205
|
+
}
|
|
3206
|
+
}
|
|
3207
|
+
return void 0;
|
|
3208
|
+
}
|
|
3209
|
+
});
|
|
3210
|
+
|
|
3211
|
+
export { CwdError2 as CwdError, Output, StructuredOutputError, claudeCode, claudeHostSessionPath, claudeSandboxSessionPath, codex, copilot, createSandbox, createWorktree, cursor, encodeProjectPath, findClaudeSessionOnHost, findCodexSessionOnHost, interactive, opencode, pi, run, transferClaudeSession, transferCodexSession };
|
|
3212
|
+
//# sourceMappingURL=index.js.map
|
|
10
3213
|
//# sourceMappingURL=index.js.map
|