@dynamicagents/plugins 0.11.0 → 0.12.0
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 +12 -13
- package/dist/arc-agi/analysis.d.ts +16 -30
- package/dist/arc-agi/analysis.js +20 -47
- package/dist/arc-agi/client.d.ts +3 -2
- package/dist/arc-agi/game-tools.d.ts +5 -6
- package/dist/arc-agi/index.d.ts +6 -8
- package/dist/arc-agi/index.js +3 -3
- package/dist/arc-agi/main-agent.d.ts +5 -8
- package/dist/arc-agi/main-agent.js +5 -8
- package/dist/arc-agi/recipe.d.ts +17 -20
- package/dist/arc-agi/recipe.js +17 -20
- package/dist/arc-agi/soul.d.ts +16 -27
- package/dist/arc-agi/soul.js +16 -27
- package/dist/arc-agi/store.d.ts +4 -5
- package/dist/arc-agi/store.js +4 -5
- package/dist/arc-agi/tools.d.ts +1 -1
- package/dist/arc-agi/tools.js +26 -19
- package/dist/arc-agi/types.d.ts +6 -7
- package/dist/claude-code/config.d.ts +88 -11
- package/dist/claude-code/config.js +11 -2
- package/dist/claude-code/credentials.d.ts +21 -9
- package/dist/claude-code/credentials.js +43 -9
- package/dist/claude-code/egress.d.ts +7 -13
- package/dist/claude-code/egress.js +9 -10
- package/dist/claude-code/events.d.ts +47 -0
- package/dist/claude-code/events.js +96 -10
- package/dist/claude-code/index.d.ts +31 -13
- package/dist/claude-code/index.js +62 -15
- package/dist/claude-code/run.d.ts +135 -41
- package/dist/claude-code/run.js +366 -61
- package/dist/computer/advisory.d.ts +31 -18
- package/dist/computer/advisory.js +30 -5
- package/dist/computer/context.d.ts +23 -0
- package/dist/computer/context.js +225 -0
- package/dist/computer/file-lock.d.ts +5 -0
- package/dist/computer/file-lock.js +39 -0
- package/dist/computer/gate.d.ts +8 -8
- package/dist/computer/gate.js +7 -7
- package/dist/computer/host/ca-trust.d.ts +88 -0
- package/dist/computer/host/ca-trust.js +303 -0
- package/dist/computer/host/container-deps.d.ts +54 -0
- package/dist/computer/host/container-deps.js +224 -0
- package/dist/computer/host/container-fault.d.ts +38 -0
- package/dist/computer/host/container-fault.js +58 -0
- package/dist/computer/host/git-host.d.ts +62 -0
- package/dist/computer/host/git-host.js +156 -0
- package/dist/computer/host/git-identity.d.ts +57 -0
- package/dist/computer/host/git-identity.js +147 -0
- package/dist/computer/host/install-job.d.ts +121 -0
- package/dist/computer/host/install-job.js +766 -0
- package/dist/computer/host/sync.d.ts +111 -0
- package/dist/computer/host/sync.js +124 -0
- package/dist/computer/host/wake.d.ts +19 -0
- package/dist/computer/host/wake.js +10 -0
- package/dist/computer/host/workspace.d.ts +424 -0
- package/dist/computer/host/workspace.js +1258 -0
- package/dist/computer/index.d.ts +70 -50
- package/dist/computer/index.js +79 -767
- package/dist/computer/install.d.ts +8 -12
- package/dist/computer/install.js +6 -9
- package/dist/computer/paths.d.ts +21 -16
- package/dist/computer/paths.js +40 -38
- package/dist/computer/read.d.ts +18 -22
- package/dist/computer/read.js +24 -27
- package/dist/computer/render.d.ts +28 -0
- package/dist/computer/render.js +34 -1
- package/dist/computer/shell.d.ts +45 -0
- package/dist/computer/shell.js +80 -0
- package/dist/computer/tools-exec.d.ts +3 -0
- package/dist/computer/tools-exec.js +269 -0
- package/dist/computer/tools-file.d.ts +4 -0
- package/dist/computer/tools-file.js +124 -0
- package/dist/computer/tools-find.d.ts +3 -0
- package/dist/computer/tools-find.js +216 -0
- package/dist/recall/index.d.ts +9 -3
- package/dist/recall/index.js +22 -12
- package/dist/repo/checkout.d.ts +34 -4
- package/dist/repo/checkout.js +36 -1
- package/dist/repo/context.d.ts +109 -0
- package/dist/repo/context.js +530 -0
- package/dist/repo/index.d.ts +6 -29
- package/dist/repo/index.js +17 -1027
- package/dist/repo/tools-clone.d.ts +4 -0
- package/dist/repo/tools-clone.js +192 -0
- package/dist/repo/tools-forge.d.ts +4 -0
- package/dist/repo/tools-forge.js +179 -0
- package/dist/repo/tools-review.d.ts +3 -0
- package/dist/repo/tools-review.js +373 -0
- package/dist/repo/tools-worktree.d.ts +4 -0
- package/dist/repo/tools-worktree.js +221 -0
- package/dist/triage/index.d.ts +6 -1
- package/dist/triage/index.js +32 -23
- package/dist/workspace/index.d.ts +3 -3
- package/dist/workspace/index.js +3 -3
- package/package.json +21 -15
package/dist/computer/index.js
CHANGED
|
@@ -50,14 +50,14 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
|
|
|
50
50
|
var e = new Error(message);
|
|
51
51
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
52
|
});
|
|
53
|
-
import {
|
|
54
|
-
import {
|
|
55
|
-
import {
|
|
56
|
-
import {
|
|
57
|
-
import {
|
|
58
|
-
import {
|
|
59
|
-
import {
|
|
60
|
-
import {
|
|
53
|
+
import { definePlugin } from "@dynamicagents/core";
|
|
54
|
+
import { getWorkspace } from "@cloudflare/computer";
|
|
55
|
+
import { cancelledNote } from "./render.js";
|
|
56
|
+
import { computerContext, DEFAULT_CWD, DEFAULT_TIMEOUT_MS } from "./context.js";
|
|
57
|
+
import { withShell } from "./shell.js";
|
|
58
|
+
import { execTools } from "./tools-exec.js";
|
|
59
|
+
import { fileTools } from "./tools-file.js";
|
|
60
|
+
import { findTools } from "./tools-find.js";
|
|
61
61
|
/**
|
|
62
62
|
* `@dynamicagents/plugins/computer` — a Linux container whose filesystem outlives it.
|
|
63
63
|
*
|
|
@@ -71,18 +71,13 @@ import { execGate, execLostNote, writeGate } from "./gate.js";
|
|
|
71
71
|
* filesystem plugin — an agent holding two gives the model no way to know which
|
|
72
72
|
* one a path refers to.
|
|
73
73
|
*
|
|
74
|
-
* ##
|
|
74
|
+
* ## The dependency tree is not in the workspace
|
|
75
75
|
*
|
|
76
|
-
* The one thing to internalise before reading further.
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
* So dependencies live in the container and die with it, while source and `.git`
|
|
83
|
-
* are durable. Two consequences run through everything below: an install has to
|
|
84
|
-
* be re-run on a cold container, and `sb_read` cannot see a path under
|
|
85
|
-
* `node_modules` even though a shell in the same container can.
|
|
76
|
+
* The one thing to internalise before reading further. A package root's
|
|
77
|
+
* `node_modules` is a bind mount of the container's disk, so installs never
|
|
78
|
+
* sync, and a new container reinstalls — see `./host/container-deps.ts`. The file tools read
|
|
79
|
+
* the workspace, so they refuse it; `sb_exec` reaches it. `paths.ts` owns what a
|
|
80
|
+
* path may be and what walks drop.
|
|
86
81
|
*
|
|
87
82
|
* Requires the Workers **Paid** plan (containers) and a Durable Object binding
|
|
88
83
|
* whose class owns the workspace — see the README for the wrangler block.
|
|
@@ -105,8 +100,8 @@ export { DEFAULT_INSTALL_PLAN, installFingerprint, resolveInstallCommand } from
|
|
|
105
100
|
* `verify:exports` treats `./computer` as one realm, and everything below stays
|
|
106
101
|
* inside `dist/computer/`.
|
|
107
102
|
*/
|
|
108
|
-
export {
|
|
109
|
-
export { cancelledNote, packBlocks, renderGrepMatches, renderResult, truncateOutput } from "./render.js";
|
|
103
|
+
export { isGitInternal, WALK_SKIPS } from "./paths.js";
|
|
104
|
+
export { cancelledNote, packBlocks, renderGrepMatches, renderResult, syncPendingNote, truncateOutput } from "./render.js";
|
|
110
105
|
export { execGate, needsDependencies, writeGate } from "./gate.js";
|
|
111
106
|
/**
|
|
112
107
|
* The workspace-advisory vocabulary, which a host needs all of: the type to
|
|
@@ -159,49 +154,7 @@ export function workspaceNameFromRuntime(runtime) {
|
|
|
159
154
|
const value = runtime?.[WORKSPACE_RUNTIME_KEY];
|
|
160
155
|
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
161
156
|
}
|
|
162
|
-
|
|
163
|
-
* How much command output the model is allowed to see, in characters.
|
|
164
|
-
*
|
|
165
|
-
* One `npm install` prints more than a small context window holds, and the
|
|
166
|
-
* interesting part of a failing build is the first error and the last summary —
|
|
167
|
-
* never the middle. So output is truncated from the middle rather than the end,
|
|
168
|
-
* which is what a naive `slice` would do and would drop the exit summary that
|
|
169
|
-
* says what actually failed.
|
|
170
|
-
*/
|
|
171
|
-
const DEFAULT_MAX_OUTPUT_CHARS = 16_000;
|
|
172
|
-
/**
|
|
173
|
-
* How many directory entries one `sb_ls` returns.
|
|
174
|
-
*
|
|
175
|
-
* A bound on the *listing*, not on the rendered text — `maxOutputChars` still
|
|
176
|
-
* applies on top, and the order matters. Bounding only the text means `readdir`
|
|
177
|
-
* returns every entry and the isolate holds all of them before the ceiling
|
|
178
|
-
* discards the tail; a real `node_modules` is 22,470 files, and a misbehaving
|
|
179
|
-
* build is exactly when someone lists one. `readdir` takes a `limit`, so the
|
|
180
|
-
* bound is applied where the entries are read.
|
|
181
|
-
*/
|
|
182
|
-
const DEFAULT_MAX_ENTRIES = 1000;
|
|
183
|
-
/**
|
|
184
|
-
* How many matches one `sb_grep` returns.
|
|
185
|
-
*
|
|
186
|
-
* Bounded at the source like {@link DEFAULT_MAX_ENTRIES}, and for a sharper reason
|
|
187
|
-
* than a listing: without a `limit`, `fs.grep` reads *every* file under the path
|
|
188
|
-
* looking for more. `.git` is in the workspace, so an unbounded search of
|
|
189
|
-
* `/workspace` streams every loose object through the isolate before answering.
|
|
190
|
-
* The limit is what stops that walk early.
|
|
191
|
-
*/
|
|
192
|
-
const DEFAULT_MAX_MATCHES = 200;
|
|
193
|
-
/** A command that has not finished in this long is a hung command. */
|
|
194
|
-
const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000;
|
|
195
|
-
/** Where checkouts live, in the container and in the workspace alike. */
|
|
196
|
-
const DEFAULT_CWD = "/workspace";
|
|
197
|
-
/**
|
|
198
|
-
* How often the install gate re-reads the install's status.
|
|
199
|
-
*
|
|
200
|
-
* An interval rather than a subscription because the status lives in another
|
|
201
|
-
* Durable Object with no event to wait on. Never slept past the gate itself —
|
|
202
|
-
* see `awaitInstall`.
|
|
203
|
-
*/
|
|
204
|
-
const INSTALL_POLL_MS = 3_000;
|
|
157
|
+
export { withShell, withShellTranscript } from "./shell.js";
|
|
205
158
|
/**
|
|
206
159
|
* Open the workspace behind a Durable Object stub.
|
|
207
160
|
*
|
|
@@ -213,706 +166,39 @@ const INSTALL_POLL_MS = 3_000;
|
|
|
213
166
|
* of the class's private brand: runtime-compatible, type-incompatible. This is
|
|
214
167
|
* the only place in the plugin that gap is crossed.
|
|
215
168
|
*/
|
|
216
|
-
function openWorkspace(host) {
|
|
169
|
+
export function openWorkspace(host) {
|
|
217
170
|
return getWorkspace(host);
|
|
218
171
|
}
|
|
219
172
|
/**
|
|
220
|
-
* The
|
|
221
|
-
*
|
|
222
|
-
* One quoted argument, not string concatenation: the command is model-authored
|
|
223
|
-
* and routinely contains quotes of its own (`git commit -m "…"`), so anything
|
|
224
|
-
* less than `shellQuote` re-parses the model's quoting and mangles it.
|
|
225
|
-
*
|
|
226
|
-
* ## Why `-o pipefail`
|
|
227
|
-
*
|
|
228
|
-
* Without it a pipeline's exit status is its **last** stage's, so
|
|
229
|
-
* `npm run check | tail -100` reports `exit 0` for a gate that failed outright —
|
|
230
|
-
* and models pipe into `tail` constantly. A tool that reports success for a
|
|
231
|
-
* failed build is worse than one that reports nothing. It also costs a re-run
|
|
232
|
-
* every time: a model that cannot trust a piped exit code runs the whole thing
|
|
233
|
-
* again unpiped to get one.
|
|
234
|
-
*
|
|
235
|
-
* The trade is worth stating. `pipefail` surfaces SIGPIPE too, so
|
|
236
|
-
* `ls big-dir | head -1` reports 141 rather than 0 — noisy, but *visible*, where
|
|
237
|
-
* the alternative is a failing gate that looks clean. The tool description tells
|
|
238
|
-
* the model not to pipe into `head`/`tail` at all, since output is already
|
|
239
|
-
* truncated with both ends kept.
|
|
240
|
-
*
|
|
241
|
-
* Requires a shell that implements it — `bash`, `zsh`, `ksh`, not `sh`/dash. A
|
|
242
|
-
* host setting {@link ComputerConfig.shell} is choosing that shell explicitly.
|
|
243
|
-
*/
|
|
244
|
-
function wrapped(command, shell) {
|
|
245
|
-
return `${shell} -o pipefail -c ${shellQuote(command)}`;
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Run a command under {@link ComputerConfig.shell} with its two output streams
|
|
249
|
-
* left as they are, or hand it back untouched when no shell is configured.
|
|
250
|
-
*
|
|
251
|
-
* This is the variant for a caller that **reads the result in code**: `stdout` is
|
|
252
|
-
* a data channel it compares or parses, and `stderr` is a separate diagnostic.
|
|
253
|
-
* {@link computerExec} is that caller, on behalf of `@dynamicagents/plugins/repo`,
|
|
254
|
-
* which asks git questions like `symbolic-ref --short refs/remotes/origin/HEAD`
|
|
255
|
-
* and `rev-list --count` and needs the answer alone.
|
|
256
|
-
*
|
|
257
|
-
* Two functions rather than one with a flag, because the difference is a change
|
|
258
|
-
* to the *output contract* and a boolean hides it at the call site. Merging the
|
|
259
|
-
* streams here would give every `/repo` git command an empty `stderr` and a
|
|
260
|
-
* `stdout` that is a transcript rather than an answer — which fails silently,
|
|
261
|
-
* since the commands `/repo` parses are quiet ones and a single `warning:` is
|
|
262
|
-
* enough to skip the empty-branch guard.
|
|
263
|
-
*/
|
|
264
|
-
export function withShell(command, shell) {
|
|
265
|
-
return shell ? wrapped(command, shell) : command;
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Run a command under {@link ComputerConfig.shell} with its two output streams
|
|
269
|
-
* merged into one transcript, in the order they were written.
|
|
270
|
-
*
|
|
271
|
-
* This is the variant for a caller whose consumer is **a model reading output**.
|
|
272
|
-
* `sb_exec` is that caller. A project's check is a chain — `wrangler types &&
|
|
273
|
-
* prettier && eslint && tsc` — and *which tool spoke last* is how you know which
|
|
274
|
-
* one failed. A stdout block and a separate stderr block destroy that ordering,
|
|
275
|
-
* and a model that wants it back re-runs the whole gate as
|
|
276
|
-
* `npm run check > /tmp/out 2>&1; cat /tmp/out`. This is that workaround, done
|
|
277
|
-
* once, for free.
|
|
173
|
+
* The same workspace, opened for filesystem work alone.
|
|
278
174
|
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
* of its own.
|
|
175
|
+
* `getWorkspace` calls exactly one method on what it is handed, so the host is
|
|
176
|
+
* wrapped in an object answering it from the other side of the seam — see
|
|
177
|
+
* {@link WorkspaceHost.__getWorkspaceFsStub}. Everything past that is identical.
|
|
283
178
|
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
* That is not a gap: {@link renderResult} renders them as a labelled
|
|
287
|
-
* `--- stderr ---` block for exactly this case. The transcript is the better
|
|
288
|
-
* answer, not the only supported one.
|
|
179
|
+
* **Only the file tools take this.** `sb_exec` and {@link computerExec} need the
|
|
180
|
+
* container started and its CA installed, so they open it the other way.
|
|
289
181
|
*/
|
|
290
|
-
export function
|
|
291
|
-
return
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Wait for a disposable resource without outliving `signal`.
|
|
295
|
-
*
|
|
296
|
-
* `withAbort` can abandon the wait but not the resource, and one that arrives
|
|
297
|
-
* after its caller has given up has nobody left to dispose it. So it is released
|
|
298
|
-
* on arrival instead — disposed by default, or handed to `release` where
|
|
299
|
-
* disposing alone would leave work running.
|
|
300
|
-
*/
|
|
301
|
-
function acquire(signal, opening, release = (resource) => resource[Symbol.dispose]()) {
|
|
302
|
-
return withAbort(signal, opening, () => {
|
|
303
|
-
void opening.then(release).catch(() => { });
|
|
182
|
+
export function openWorkspaceFs(host) {
|
|
183
|
+
return getWorkspace({
|
|
184
|
+
__getWorkspaceStub: () => host.__getWorkspaceFsStub()
|
|
304
185
|
});
|
|
305
186
|
}
|
|
306
187
|
/**
|
|
307
|
-
*
|
|
308
|
-
*
|
|
188
|
+
* @param workspace Opens the workspace for a command: container started, CA
|
|
189
|
+
* installed.
|
|
190
|
+
* @param fsWorkspace Opens it for the file tools, which touch nothing but the
|
|
191
|
+
* host's SQLite. Defaults to {@link workspace}, so a host with one way in
|
|
192
|
+
* behaves as before. See {@link openWorkspaceFs}.
|
|
193
|
+
* @param lockScope Names the workspace a write locks within — see
|
|
194
|
+
* {@link file://./file-lock.ts}. Defaults to this tool set alone.
|
|
309
195
|
*/
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
await handle.kill("SIGTERM");
|
|
313
|
-
}
|
|
314
|
-
finally {
|
|
315
|
-
handle[Symbol.dispose]();
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
export function buildComputerTools(workspace, config, advisories) {
|
|
319
|
-
const cwd = config.cwd ?? DEFAULT_CWD;
|
|
320
|
-
const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
321
|
-
const maxChars = config.maxOutputChars ?? DEFAULT_MAX_OUTPUT_CHARS;
|
|
322
|
-
const gateMs = config.installGateMs ?? 90_000;
|
|
323
|
-
const env = config.env;
|
|
324
|
-
/**
|
|
325
|
-
* Wait out anything transient, up to the gate, and report whatever is left.
|
|
326
|
-
*
|
|
327
|
-
* Polled rather than subscribed: the state lives in another Durable Object,
|
|
328
|
-
* there is no event to wait on, and the whole window is under two minutes.
|
|
329
|
-
*
|
|
330
|
-
* **Read for every command, including the ones that need no dependencies.**
|
|
331
|
-
* That is a change of shape, not a slip. The filter that spares `cat
|
|
332
|
-
* README.md` from queueing behind an `npm ci` lives in `execGate`, where it
|
|
333
|
-
* applies per advisory — because a dependency install and a full workspace are
|
|
334
|
-
* not relevant to the same commands, and applying one filter to both is what
|
|
335
|
-
* made a workspace at its ceiling silent for exactly the commands whose writes
|
|
336
|
-
* it was dropping. The cost is one same-colo RPC on commands that previously
|
|
337
|
-
* skipped it, against a class of silent data loss.
|
|
338
|
-
*/
|
|
339
|
-
const awaitAdvisories = async (command, signal) => {
|
|
340
|
-
if (!advisories)
|
|
341
|
-
return {};
|
|
342
|
-
// Fails **open**, and that belongs here rather than at the call site: this
|
|
343
|
-
// is a read of another Durable Object's state. An error must not take out a
|
|
344
|
-
// working shell — running the command is exactly what would have happened
|
|
345
|
-
// before the gate existed, and a spurious blockage would strand the
|
|
346
|
-
// subagent.
|
|
347
|
-
const read = async () => {
|
|
348
|
-
try {
|
|
349
|
-
signal?.throwIfAborted();
|
|
350
|
-
return await withAbort(signal, advisories());
|
|
351
|
-
}
|
|
352
|
-
catch (err) {
|
|
353
|
-
// Open on a failed read, never on a cancel: failing open here would go on
|
|
354
|
-
// to run the command the caller just gave up on.
|
|
355
|
-
if (signal?.aborted)
|
|
356
|
-
throw err;
|
|
357
|
-
return [];
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
// Clamped to whatever is left of the gate, not the bare interval. A fixed
|
|
361
|
-
// sleep makes `installGateMs` a floor rather than the ceiling it is
|
|
362
|
-
// documented to be: at a 100 ms gate the loop still slept the full three
|
|
363
|
-
// seconds before looking again, so a public timeout overshot by 30×.
|
|
364
|
-
const deadline = Date.now() + gateMs;
|
|
365
|
-
let current = await read();
|
|
366
|
-
// Keyed on the gate's own verdict rather than on a state name, so "what is
|
|
367
|
-
// worth waiting for" has one definition and it is the one that will decide
|
|
368
|
-
// the outcome.
|
|
369
|
-
while (execGate(current, command).block && Date.now() < deadline) {
|
|
370
|
-
const wait = Math.min(INSTALL_POLL_MS, deadline - Date.now());
|
|
371
|
-
// Throws on a cancel rather than returning the verdict so far: a gate that
|
|
372
|
-
// outlived its caller would otherwise go on to run the command it was holding.
|
|
373
|
-
await withAbort(signal, new Promise((resolve) => setTimeout(resolve, wait)));
|
|
374
|
-
current = await read();
|
|
375
|
-
}
|
|
376
|
-
return execGate(current, command);
|
|
377
|
-
};
|
|
378
|
-
/**
|
|
379
|
-
* Whether a write can succeed at all, asked once and not polled.
|
|
380
|
-
*
|
|
381
|
-
* Separate from {@link awaitAdvisories} because the question is different:
|
|
382
|
-
* there is no command to classify and nothing worth waiting for. An install in
|
|
383
|
-
* flight does not stop a file being written, and the one thing that does —
|
|
384
|
-
* a workspace that no longer accepts writes — never resolves on its own.
|
|
385
|
-
*
|
|
386
|
-
* Fails **open** for the same reason the exec gate does: a read of another
|
|
387
|
-
* Durable Object's state must not take out a working tool.
|
|
388
|
-
*/
|
|
389
|
-
const refuseWrite = async () => {
|
|
390
|
-
if (!advisories)
|
|
391
|
-
return undefined;
|
|
392
|
-
try {
|
|
393
|
-
return writeGate(await advisories());
|
|
394
|
-
}
|
|
395
|
-
catch {
|
|
396
|
-
return undefined;
|
|
397
|
-
}
|
|
398
|
-
};
|
|
399
|
-
/**
|
|
400
|
-
* Host-supplied environment, with the undefined entries dropped.
|
|
401
|
-
*
|
|
402
|
-
* `RuntimeExecOptions.env` is `Record<string, string>`, and a config thunk
|
|
403
|
-
* that reads straight off `env` will hand back `undefined` for anything
|
|
404
|
-
* unset — which would otherwise arrive in the container as the string
|
|
405
|
-
* "undefined".
|
|
406
|
-
*/
|
|
407
|
-
const definedEnv = () => {
|
|
408
|
-
if (!env)
|
|
409
|
-
return undefined;
|
|
410
|
-
const entries = Object.entries(env()).filter((entry) => entry[1] !== undefined);
|
|
411
|
-
return entries.length > 0 ? Object.fromEntries(entries) : undefined;
|
|
412
|
-
};
|
|
413
|
-
/**
|
|
414
|
-
* Open the workspace, run one file operation in it, and own the failure.
|
|
415
|
-
*
|
|
416
|
-
* Five lines every file tool would otherwise repeat with only the verb changed
|
|
417
|
-
* — five chances for the seventh to be written without them. The catch is the
|
|
418
|
-
* point: a failed file operation is the model's to recover from, and it can
|
|
419
|
-
* only recover from what it is told, so this returns the sentence rather than
|
|
420
|
-
* throwing it.
|
|
421
|
-
*
|
|
422
|
-
* `sb_exec` deliberately does not use it. That one recognises `EEXEC_LOST`,
|
|
423
|
-
* logs, and carries an install warning through the failure path — a bespoke
|
|
424
|
-
* catch saying something this cannot.
|
|
425
|
-
*/
|
|
426
|
-
const inWorkspace = async (gerund, subject, body) => {
|
|
427
|
-
try {
|
|
428
|
-
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
429
|
-
try {
|
|
430
|
-
const ws = __addDisposableResource(env_1, await workspace(), false);
|
|
431
|
-
return await body(ws.fs);
|
|
432
|
-
}
|
|
433
|
-
catch (e_1) {
|
|
434
|
-
env_1.error = e_1;
|
|
435
|
-
env_1.hasError = true;
|
|
436
|
-
}
|
|
437
|
-
finally {
|
|
438
|
-
__disposeResources(env_1);
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
catch (err) {
|
|
442
|
-
return `error ${gerund} ${subject}: ${String(err)}`;
|
|
443
|
-
}
|
|
444
|
-
};
|
|
196
|
+
export function buildComputerTools(workspace, config, advisories, fsWorkspace = workspace, lockScope) {
|
|
197
|
+
const ctx = computerContext(workspace, config, advisories, fsWorkspace, lockScope);
|
|
445
198
|
return {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
*
|
|
450
|
-
* A description that leads with "output is truncated if it is large" and
|
|
451
|
-
* gives no exit code hands the model two independent reasons to re-run a
|
|
452
|
-
* command and capture the output "properly" — at 60 seconds a go on a real
|
|
453
|
-
* gate, whose few hundred characters were never near the ceiling anyway.
|
|
454
|
-
*
|
|
455
|
-
* Two of these sentences are only true when a shell is configured, so the
|
|
456
|
-
* description says whichever is. `withShellTranscript` is a no-op without
|
|
457
|
-
* {@link ComputerConfig.shell}: no `2>&1`, so the streams arrive separate
|
|
458
|
-
* and {@link renderResult} labels them; no `-o pipefail`, so `/bin/sh`
|
|
459
|
-
* reports a pipeline's *last* stage. Promising a transcript and
|
|
460
|
-
* first-failure semantics there would be the lie `wrapped` explains the
|
|
461
|
-
* cost of.
|
|
462
|
-
*/
|
|
463
|
-
description: "Run a shell command in the container and return its output. Use this for builds, tests, package installs, git, and anything else a terminal can do. " +
|
|
464
|
-
(config.shell
|
|
465
|
-
? "The result is the command's full transcript — stdout and stderr interleaved in the order they were written — followed by a line reporting the exit code, e.g. `--- exit 0 ---`. A command killed at the time limit says so on that line. " +
|
|
466
|
-
"You do not need to append `echo $?`, add `2>&1`, or redirect to a file to see any of this. " +
|
|
467
|
-
"Do not pipe into `head` or `tail` to shorten output: long output is already truncated from the middle, keeping the beginning and the end, and piping costs you the parts you wanted. Pipelines report the first failing stage, so a piped command reports its real failure rather than the pipe's — but `cmd | head` may report 141 when `head` closes the pipe early, which is not a failure of `cmd`. "
|
|
468
|
-
: "The result is the command's stdout, then any stderr under a `--- stderr ---` heading, then a line reporting the exit code, e.g. `--- exit 0 ---`. A command killed at the time limit says so on that line. " +
|
|
469
|
-
"You do not need to append `echo $?` to see the exit code. Add `2>&1` yourself if you need the two streams in the order they were written. " +
|
|
470
|
-
"Do not pipe into `head` or `tail` to shorten output: long output is already truncated from the middle, keeping the beginning and the end, and piping costs you the parts you wanted. A pipeline reports its **last** stage, so check the exit code of the command you care about rather than the pipe's. ") +
|
|
471
|
-
"Prefer targeted commands over ones that print everything.",
|
|
472
|
-
inputSchema: z.object({
|
|
473
|
-
command: z.string().describe("The shell command, e.g. 'npm test'"),
|
|
474
|
-
cwd: z
|
|
475
|
-
.string()
|
|
476
|
-
.optional()
|
|
477
|
-
.describe(`Working directory (default: ${cwd})`)
|
|
478
|
-
}),
|
|
479
|
-
execute: async ({ command, cwd: overrideCwd }, { abortSignal }) => {
|
|
480
|
-
/**
|
|
481
|
-
* One line per command, and it is the only view of where a task's wall
|
|
482
|
-
* clock actually goes.
|
|
483
|
-
*
|
|
484
|
-
* Everything on the Worker side of a container command is an `await`, so
|
|
485
|
-
* Workers Observability records the invocation at ~0% CPU and a long wall
|
|
486
|
-
* time and cannot say what ran. Diagnosing a 59-minute task on 2026-08-11
|
|
487
|
-
* meant inferring the shape of each command from the *gaps between AI
|
|
488
|
-
* Gateway calls*, because nothing logged the command itself. The three
|
|
489
|
-
* numbers below — how long the install gate held, how long the command
|
|
490
|
-
* took, what it exited with — would have answered it directly.
|
|
491
|
-
*
|
|
492
|
-
* Timed around the gate as well as the command, since a subagent blocked
|
|
493
|
-
* waiting for `npm ci` and one running a slow test suite are
|
|
494
|
-
* indistinguishable from the outside and want opposite fixes.
|
|
495
|
-
*/
|
|
496
|
-
const startedAtMs = Date.now();
|
|
497
|
-
/**
|
|
498
|
-
* What the model is told when this call stops before the command
|
|
499
|
-
* finishes. Said plainly, because the likeliest next move after a bare
|
|
500
|
-
* error is the same command again — and whether that is right depends on
|
|
501
|
-
* where the call stopped. A command that outran the limit once will again;
|
|
502
|
-
* one that never started because the workspace did not answer may not.
|
|
503
|
-
*
|
|
504
|
-
* `sb_exec` reads its call's signal rather than leaving the wait to core,
|
|
505
|
-
* and this is why: core's abandonment can say only that the command may
|
|
506
|
-
* still be running. Core's `TOOL_CALL_GRACE_MS` is the window this answer
|
|
507
|
-
* has to arrive in, and it covers sending the kill below.
|
|
508
|
-
*/
|
|
509
|
-
const stopped = (gateMs, started) => {
|
|
510
|
-
const timedOut = abortSignal?.reason?.name ===
|
|
511
|
-
"TimeoutError";
|
|
512
|
-
console.info("[computer] sb_exec stopped", {
|
|
513
|
-
command,
|
|
514
|
-
gateMs,
|
|
515
|
-
durationMs: Date.now() - startedAtMs - gateMs,
|
|
516
|
-
started,
|
|
517
|
-
reason: timedOut ? "time limit" : "cancelled"
|
|
518
|
-
});
|
|
519
|
-
if (!timedOut)
|
|
520
|
-
return "the command was stopped because this call was cancelled. Anything it changed before then is still changed.";
|
|
521
|
-
return started
|
|
522
|
-
? "the command was stopped: it ran past this call's time limit. Anything it changed before then is still changed. Try something narrower."
|
|
523
|
-
: "the command did not run: this call reached its time limit while the workspace was still getting ready, so nothing was changed. Try again; if it happens again, the workspace is not responding.";
|
|
524
|
-
};
|
|
525
|
-
// Only `sb_exec` waits on an install. The file tools read and write
|
|
526
|
-
// source, which is in the workspace and unaffected by an install in
|
|
527
|
-
// flight — blocking them would stop the subagent doing the reading it
|
|
528
|
-
// could usefully do while it waits. They do consult `writeGate`, which
|
|
529
|
-
// is a different question: not "is the tree ready" but "does a write
|
|
530
|
-
// survive at all".
|
|
531
|
-
let gate;
|
|
532
|
-
try {
|
|
533
|
-
gate = await awaitAdvisories(command, abortSignal);
|
|
534
|
-
}
|
|
535
|
-
catch (err) {
|
|
536
|
-
// Only a cancel gets out of the gate; a failed advisory read opens it.
|
|
537
|
-
if (abortSignal?.aborted)
|
|
538
|
-
return stopped(Date.now() - startedAtMs, false);
|
|
539
|
-
throw err;
|
|
540
|
-
}
|
|
541
|
-
const gateMsWaited = Date.now() - startedAtMs;
|
|
542
|
-
if (gate.block) {
|
|
543
|
-
console.info("[computer] sb_exec blocked by a workspace advisory", {
|
|
544
|
-
command,
|
|
545
|
-
gateMs: gateMsWaited
|
|
546
|
-
});
|
|
547
|
-
return gate.block;
|
|
548
|
-
}
|
|
549
|
-
// Prepended to whatever happens next, success or failure. The warning
|
|
550
|
-
// is context for the output, not a substitute for it — which is why it
|
|
551
|
-
// is carried through the catch as well.
|
|
552
|
-
const note = (body) => gate.warn ? `${gate.warn}\n\n${body}` : body;
|
|
553
|
-
// Set once the exec is sent. See `stopped`.
|
|
554
|
-
let started = false;
|
|
555
|
-
try {
|
|
556
|
-
const env_2 = { stack: [], error: void 0, hasError: false };
|
|
557
|
-
try {
|
|
558
|
-
// A workspace that stops answering would hold the call before any of the
|
|
559
|
-
// cancellation below is reached.
|
|
560
|
-
abortSignal?.throwIfAborted();
|
|
561
|
-
const ws = __addDisposableResource(env_2, await acquire(abortSignal, workspace()), false);
|
|
562
|
-
// Read once per command. This is the *host's* thunk, so calling it
|
|
563
|
-
// twice in the construction of one command's options is two chances to
|
|
564
|
-
// disagree — the check and the value would come from different reads.
|
|
565
|
-
const commandEnv = definedEnv();
|
|
566
|
-
const options = {
|
|
567
|
-
cwd: overrideCwd ?? cwd,
|
|
568
|
-
encoding: "utf8",
|
|
569
|
-
timeoutMs,
|
|
570
|
-
...(commandEnv ? { env: commandEnv } : {})
|
|
571
|
-
};
|
|
572
|
-
// Transcript, not two streams: this result goes to a model, which
|
|
573
|
-
// reads it as a terminal session rather than parsing it.
|
|
574
|
-
// Not started at all on a signal that has already fired: a process that
|
|
575
|
-
// is killed on the next line still ran long enough to change something.
|
|
576
|
-
abortSignal?.throwIfAborted();
|
|
577
|
-
// From here a stop cannot promise nothing ran: the exec may reach the
|
|
578
|
-
// container before its handle reaches this side.
|
|
579
|
-
started = true;
|
|
580
|
-
const handle = __addDisposableResource(env_2, await acquire(abortSignal, ws.runtime.exec(withShellTranscript(command, config.shell), options), killLate), false);
|
|
581
|
-
// The runtime takes no signal, so stopping the wait and stopping the
|
|
582
|
-
// process are two acts. Disposing the handle is neither — it releases
|
|
583
|
-
// this side's attachment and leaves the command running.
|
|
584
|
-
const result = await withAbort(abortSignal, handle.result(), () => handle.kill("SIGTERM"));
|
|
585
|
-
console.info("[computer] sb_exec", {
|
|
586
|
-
command,
|
|
587
|
-
exitCode: result.exitCode,
|
|
588
|
-
// Split so a slow command and a slow *wait* never look alike.
|
|
589
|
-
gateMs: gateMsWaited,
|
|
590
|
-
durationMs: Date.now() - startedAtMs - gateMsWaited,
|
|
591
|
-
// The ceiling this was measured against — a duration sitting on it is
|
|
592
|
-
// a timeout wearing a normal-looking number.
|
|
593
|
-
timeoutMs
|
|
594
|
-
});
|
|
595
|
-
return note(renderResult(result, maxChars));
|
|
596
|
-
}
|
|
597
|
-
catch (e_2) {
|
|
598
|
-
env_2.error = e_2;
|
|
599
|
-
env_2.hasError = true;
|
|
600
|
-
}
|
|
601
|
-
finally {
|
|
602
|
-
__disposeResources(env_2);
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
catch (err) {
|
|
606
|
-
if (abortSignal?.aborted)
|
|
607
|
-
return note(stopped(gateMsWaited, started));
|
|
608
|
-
const lost = execLostNote(err);
|
|
609
|
-
console.warn("[computer] sb_exec failed", {
|
|
610
|
-
command,
|
|
611
|
-
gateMs: gateMsWaited,
|
|
612
|
-
durationMs: Date.now() - startedAtMs - gateMsWaited,
|
|
613
|
-
timeoutMs,
|
|
614
|
-
// Distinguished in the log for the same reason it is distinguished
|
|
615
|
-
// for the model: a container replacement and a broken command want
|
|
616
|
-
// different people looking at them.
|
|
617
|
-
...(lost ? { lost: true } : {}),
|
|
618
|
-
err: String(err)
|
|
619
|
-
});
|
|
620
|
-
// Returned, not thrown: a failed command is usually the model's to
|
|
621
|
-
// recover from, and it can only recover from what it is told.
|
|
622
|
-
return note(lost ?? `error running command: ${String(err)}`);
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
}),
|
|
626
|
-
sb_read: tool({
|
|
627
|
-
description: "Read a file from the workspace. Returns the file's text, or a note if it does not exist. " +
|
|
628
|
-
"A large file comes back with its middle removed and a marker giving the `offset` that reaches the missing part. " +
|
|
629
|
-
"Pass `offset` (and optionally `length`) to read a specific byte window instead — the result states the window it returned and how many bytes follow, so you can page through a file. Byte offsets, not lines: to see the lines around a match, use sb_grep with `context`. " +
|
|
630
|
-
"Files under node_modules are not in the workspace — read those with sb_exec.",
|
|
631
|
-
inputSchema: z.object({
|
|
632
|
-
path: z
|
|
633
|
-
.string()
|
|
634
|
-
.describe("Absolute path, e.g. '/workspace/repo/src/a.ts'"),
|
|
635
|
-
offset: z
|
|
636
|
-
.number()
|
|
637
|
-
.int()
|
|
638
|
-
.min(0)
|
|
639
|
-
.optional()
|
|
640
|
-
.describe("Byte offset to start reading from"),
|
|
641
|
-
length: z
|
|
642
|
-
.number()
|
|
643
|
-
.int()
|
|
644
|
-
.min(1)
|
|
645
|
-
.optional()
|
|
646
|
-
.describe("Maximum bytes to return from `offset`")
|
|
647
|
-
}),
|
|
648
|
-
execute: async ({ path, offset, length }) => {
|
|
649
|
-
const refusal = guardPath(path, "sb_read");
|
|
650
|
-
if (refusal)
|
|
651
|
-
return refusal;
|
|
652
|
-
return inWorkspace("reading", path, async (fs) => {
|
|
653
|
-
// Either knob means the model chose a region; only an unqualified read
|
|
654
|
-
// gets the middle-out guess.
|
|
655
|
-
return offset === undefined && length === undefined
|
|
656
|
-
? await readBounded(fs, path, maxChars)
|
|
657
|
-
: await readWindow(fs, path, offset ?? 0, length ?? maxChars, maxChars);
|
|
658
|
-
});
|
|
659
|
-
}
|
|
660
|
-
}),
|
|
661
|
-
sb_write: tool({
|
|
662
|
-
description: "Create or overwrite a file in the workspace. Parent directories are created for you. For a small change to a large file prefer sb_edit, which does not require sending the whole file back.",
|
|
663
|
-
inputSchema: z.object({
|
|
664
|
-
path: z.string().describe("Absolute path"),
|
|
665
|
-
content: z
|
|
666
|
-
.string()
|
|
667
|
-
.describe("Full file content (overwrites any existing file)")
|
|
668
|
-
}),
|
|
669
|
-
execute: async ({ path, content }) => {
|
|
670
|
-
const refusal = guardPath(path, "sb_write");
|
|
671
|
-
if (refusal)
|
|
672
|
-
return refusal;
|
|
673
|
-
// Before the write, not after: the point is to not report a success that
|
|
674
|
-
// did not happen.
|
|
675
|
-
const lost = await refuseWrite();
|
|
676
|
-
if (lost)
|
|
677
|
-
return lost;
|
|
678
|
-
return inWorkspace("writing", path, async (fs) => {
|
|
679
|
-
const dir = path.slice(0, path.lastIndexOf("/"));
|
|
680
|
-
if (dir)
|
|
681
|
-
await fs.mkdir(dir, { recursive: true });
|
|
682
|
-
await fs.writeFile(path, content);
|
|
683
|
-
// Characters, not bytes. `String.length` counts UTF-16 code units — the
|
|
684
|
-
// distinction `readBounded` documents at length — so calling them bytes
|
|
685
|
-
// was simply wrong for anything outside ASCII. The exact byte count
|
|
686
|
-
// would cost a `TextEncoder` pass over the whole content for a number
|
|
687
|
-
// nobody does arithmetic with, and "characters" is already this module's
|
|
688
|
-
// word for the same count in `truncateOutput`'s omission marker.
|
|
689
|
-
const written = content.length;
|
|
690
|
-
return `wrote ${path} (${written} character${written === 1 ? "" : "s"})`;
|
|
691
|
-
});
|
|
692
|
-
}
|
|
693
|
-
}),
|
|
694
|
-
sb_edit: tool({
|
|
695
|
-
description: "Replace an exact string in a file. The string must be non-empty and must appear exactly once — if it appears zero times or more than once the edit is refused, so include enough surrounding context to make it unique.",
|
|
696
|
-
inputSchema: z.object({
|
|
697
|
-
path: z.string().describe("Absolute path"),
|
|
698
|
-
find: z
|
|
699
|
-
.string()
|
|
700
|
-
// Empty is refused at the schema rather than in the body, because
|
|
701
|
-
// `split("")` does not count empty-string occurrences: on a file of
|
|
702
|
-
// one character it reports none, on a longer one it reports one per
|
|
703
|
-
// character, and on an *empty* file it reports -1 — which slips past
|
|
704
|
-
// both guards below and writes `replace` into the file as if an edit
|
|
705
|
-
// had been found. A write tool must not write what nobody asked for.
|
|
706
|
-
.min(1)
|
|
707
|
-
.describe("Exact text to replace, unique within the file"),
|
|
708
|
-
replace: z.string().describe("Replacement text")
|
|
709
|
-
}),
|
|
710
|
-
execute: async ({ path, find, replace }) => {
|
|
711
|
-
const refusal = guardPath(path, "sb_edit");
|
|
712
|
-
if (refusal)
|
|
713
|
-
return refusal;
|
|
714
|
-
const lost = await refuseWrite();
|
|
715
|
-
if (lost)
|
|
716
|
-
return lost;
|
|
717
|
-
return inWorkspace("editing", path, async (fs) => {
|
|
718
|
-
const content = await fs.readFile(path, "utf8");
|
|
719
|
-
const occurrences = content.split(find).length - 1;
|
|
720
|
-
// Refusing an ambiguous edit is the whole value of this tool over
|
|
721
|
-
// sb_write: a silent first-match replace corrupts the file in a way
|
|
722
|
-
// that surfaces much later, usually as a confusing test failure.
|
|
723
|
-
if (occurrences === 0)
|
|
724
|
-
return `no match for that text in ${path}`;
|
|
725
|
-
if (occurrences > 1)
|
|
726
|
-
return `that text appears ${occurrences} times in ${path} — add surrounding context to make it unique`;
|
|
727
|
-
// A replacer function, not the string itself. `String.replace`
|
|
728
|
-
// interprets `$$`, `$&`, `` $` `` and `$'` in a *string* replacement
|
|
729
|
-
// even when the pattern is a plain string, so the text written is not
|
|
730
|
-
// the text the model sent: `echo $$` becomes `echo $`, and `$'` splices
|
|
731
|
-
// in everything before the match. Those two are not exotic — `$$`
|
|
732
|
-
// escapes a dollar in a Makefile and reads a PID in shell, and `$'…'`
|
|
733
|
-
// is bash ANSI-C quoting. A function's return value is used verbatim.
|
|
734
|
-
await fs.writeFile(path, content.replace(find, () => replace));
|
|
735
|
-
return `edited ${path}`;
|
|
736
|
-
});
|
|
737
|
-
}
|
|
738
|
-
}),
|
|
739
|
-
sb_ls: tool({
|
|
740
|
-
description: "List files in a workspace directory, or find files by name. Without `pattern` it lists one level: directories with a trailing slash, files with their size — check that before reading a large one, since sb_read truncates. " +
|
|
741
|
-
"`pattern` is a glob matched against paths relative to `path`, and searches the whole subtree: `*` stays within one path segment, `**/` crosses directories, `?` matches one character. So `*.ts` finds top-level TypeScript files and `**/*.ts` finds them at any depth. " +
|
|
742
|
-
"A cut listing reports the `offset` that continues it. node_modules is not in the workspace — list it with sb_exec.",
|
|
743
|
-
inputSchema: z.object({
|
|
744
|
-
path: z.string().describe("Absolute directory path"),
|
|
745
|
-
recursive: z
|
|
746
|
-
.boolean()
|
|
747
|
-
.optional()
|
|
748
|
-
.describe("List the whole subtree rather than one level"),
|
|
749
|
-
pattern: z
|
|
750
|
-
.string()
|
|
751
|
-
.optional()
|
|
752
|
-
.describe("Glob relative to path, e.g. '**/*.spec.ts'. Searches the subtree, so `recursive` is not needed with it."),
|
|
753
|
-
offset: z
|
|
754
|
-
.number()
|
|
755
|
-
.int()
|
|
756
|
-
.min(0)
|
|
757
|
-
.optional()
|
|
758
|
-
.describe("Entries to skip — use the offset a cut listing reports")
|
|
759
|
-
}),
|
|
760
|
-
execute: async ({ path, recursive, pattern, offset }) => {
|
|
761
|
-
const refusal = guardPath(path, "sb_ls");
|
|
762
|
-
if (refusal)
|
|
763
|
-
return refusal;
|
|
764
|
-
const from = offset ?? 0;
|
|
765
|
-
return inWorkspace("listing", path, async (fs) => {
|
|
766
|
-
if (recursive || pattern) {
|
|
767
|
-
// `find` rather than `ls`, which took no bound: a prefix scan
|
|
768
|
-
// returned every path in the subtree and the ceiling then threw most
|
|
769
|
-
// of them away — the same read-everything-then-discard shape the
|
|
770
|
-
// `readdir` limit below exists to avoid. Two differences follow from
|
|
771
|
-
// the swap, both improvements: directories appear (rendered like the
|
|
772
|
-
// arm below renders them), and order is the walk's — pre-order, by
|
|
773
|
-
// name — rather than one flat sort.
|
|
774
|
-
//
|
|
775
|
-
// Filtered rather than trusted, and this is the arm where it matters
|
|
776
|
-
// most: at a repo root `.git` is walked *first* and holds thousands
|
|
777
|
-
// of objects, so an unfiltered page is a page of `.git` and nothing
|
|
778
|
-
// else. Four rounds because a `find` retry re-walks dirents, which is
|
|
779
|
-
// cheap SQLite reads rather than file bytes.
|
|
780
|
-
const page = await collectVisible((at, limit) => fs.find(path, pattern, { limit, offset: at }), (e) => e.path, DEFAULT_MAX_ENTRIES, from, 4);
|
|
781
|
-
if (page.items.length === 0)
|
|
782
|
-
return page.crowded
|
|
783
|
-
? `(everything under ${path} from offset ${from} is inside .git — try a \`pattern\`, or a \`path\` inside the working tree)`
|
|
784
|
-
: pattern
|
|
785
|
-
? `(nothing under ${path} matches ${pattern})`
|
|
786
|
-
: `(${path} is empty)`;
|
|
787
|
-
const blocks = page.items
|
|
788
|
-
.slice(0, DEFAULT_MAX_ENTRIES)
|
|
789
|
-
.map((e) => [e.type === "dir" ? `${e.path}/` : e.path]);
|
|
790
|
-
const { body, shown } = packBlocks(blocks, maxChars);
|
|
791
|
-
return body + listingNote(page, shown, "entries", "`pattern`");
|
|
792
|
-
}
|
|
793
|
-
// One over the ceiling: enough to know the listing was cut without a
|
|
794
|
-
// second round trip to find out, and the extra entry is not shown.
|
|
795
|
-
// Unfiltered on purpose — `.git/` shows here exactly as `node_modules`
|
|
796
|
-
// does, because one line naming a directory that is really there is
|
|
797
|
-
// honest, and it is access rather than existence that is refused.
|
|
798
|
-
const entries = await fs.readdir(path, {
|
|
799
|
-
limit: DEFAULT_MAX_ENTRIES + 1,
|
|
800
|
-
offset: from
|
|
801
|
-
});
|
|
802
|
-
if (entries.length === 0)
|
|
803
|
-
return from > 0
|
|
804
|
-
? `(no entries in ${path} past offset ${from})`
|
|
805
|
-
: `(${path} is empty)`;
|
|
806
|
-
const blocks = entries
|
|
807
|
-
.slice(0, DEFAULT_MAX_ENTRIES)
|
|
808
|
-
.map((e) => [
|
|
809
|
-
e.isDirectory ? `${e.name}/` : `${e.name}\t${humanBytes(e.size)}`
|
|
810
|
-
]);
|
|
811
|
-
const { body, shown } = packBlocks(blocks, maxChars);
|
|
812
|
-
const more = entries.length > shown;
|
|
813
|
-
return (body +
|
|
814
|
-
(more
|
|
815
|
-
? `\n… showed ${shown} entries; there are more. Continue with \`offset: ${from + shown}\`, or narrow with \`pattern\`.`
|
|
816
|
-
: ""));
|
|
817
|
-
});
|
|
818
|
-
}
|
|
819
|
-
}),
|
|
820
|
-
/**
|
|
821
|
-
* Search, without the container.
|
|
822
|
-
*
|
|
823
|
-
* The tool this replaces is `sb_exec("grep -rn …")`, and the case for a native
|
|
824
|
-
* one is not that shelling out fails — it is where the search runs. `fs.grep`
|
|
825
|
-
* reads the Durable Object's SQLite, so it answers while the container is
|
|
826
|
-
* being replaced or an install is still running, which is exactly the window
|
|
827
|
-
* the install gate leaves a subagent with nothing to do. It is also why this
|
|
828
|
-
* tool is not gated: see {@link awaitInstall}.
|
|
829
|
-
*
|
|
830
|
-
* Two lesser reasons that still matter. The query arrives as a value rather
|
|
831
|
-
* than through `shellQuote` and a shell that would re-parse it. And the result
|
|
832
|
-
* is bounded by a `limit` at the source instead of being middle-truncated
|
|
833
|
-
* afterwards, which for a match list means losing whole files silently.
|
|
834
|
-
*/
|
|
835
|
-
sb_grep: tool({
|
|
836
|
-
description: "Search file contents across the workspace. Returns matching lines grouped by file, each with its line number. " +
|
|
837
|
-
"The query is matched literally — set `regex` to interpret it as a regular expression. " +
|
|
838
|
-
"Pass `include` to limit which files are searched, e.g. '**/*.ts' — without it every file under `path` is read, which is slower and rarely what you meant. " +
|
|
839
|
-
"A cut result reports the `offset` that continues it. Use `context` to see the lines around a match. " +
|
|
840
|
-
"node_modules is not in the workspace — search it with sb_exec.",
|
|
841
|
-
inputSchema: z.object({
|
|
842
|
-
query: z.string().describe("Text to find, e.g. 'buildComputerTools'"),
|
|
843
|
-
path: z
|
|
844
|
-
.string()
|
|
845
|
-
.optional()
|
|
846
|
-
.describe(`Absolute file or directory to search (default: ${cwd})`),
|
|
847
|
-
include: z
|
|
848
|
-
.string()
|
|
849
|
-
.optional()
|
|
850
|
-
.describe("Glob relative to path limiting which files are searched, e.g. '**/*.ts'"),
|
|
851
|
-
regex: z
|
|
852
|
-
.boolean()
|
|
853
|
-
.optional()
|
|
854
|
-
.describe("Interpret query as a regular expression"),
|
|
855
|
-
ignoreCase: z.boolean().optional().describe("Ignore letter case"),
|
|
856
|
-
context: z
|
|
857
|
-
.number()
|
|
858
|
-
.int()
|
|
859
|
-
.min(0)
|
|
860
|
-
.max(3)
|
|
861
|
-
.optional()
|
|
862
|
-
.describe("Lines of surrounding context to include with each match"),
|
|
863
|
-
offset: z
|
|
864
|
-
.number()
|
|
865
|
-
.int()
|
|
866
|
-
.min(0)
|
|
867
|
-
.optional()
|
|
868
|
-
.describe("Matches to skip — use the offset a cut result reports")
|
|
869
|
-
}),
|
|
870
|
-
execute: async ({ query, path, include, regex, ignoreCase, context, offset }) => {
|
|
871
|
-
const target = path ?? cwd;
|
|
872
|
-
const refusal = guardPath(target, "sb_grep");
|
|
873
|
-
if (refusal)
|
|
874
|
-
return refusal;
|
|
875
|
-
const from = offset ?? 0;
|
|
876
|
-
return inWorkspace("searching", target, async (fs) => {
|
|
877
|
-
// Two rounds, not four: a `grep` retry re-reads and re-scans every file
|
|
878
|
-
// it already looked at, where the `find` retry in `sb_ls` only re-walks
|
|
879
|
-
// dirents. `.git` is also far less likely to flood a page here — its
|
|
880
|
-
// bulk is compressed objects, which a text query does not match.
|
|
881
|
-
const page = await collectVisible((at, limit) => fs.grep(query, target, {
|
|
882
|
-
include,
|
|
883
|
-
regex,
|
|
884
|
-
ignoreCase,
|
|
885
|
-
context,
|
|
886
|
-
limit,
|
|
887
|
-
offset: at
|
|
888
|
-
}), (m) => m.path, DEFAULT_MAX_MATCHES, from, 2);
|
|
889
|
-
if (page.items.length === 0)
|
|
890
|
-
return page.crowded
|
|
891
|
-
? `every match for ${JSON.stringify(query)} from offset ${from} is inside .git, which is not searched. Add \`include\` (e.g. '**/*.ts') to search the working tree instead.`
|
|
892
|
-
: `no matches for ${JSON.stringify(query)} in ${target}${include ? ` (${include})` : ""}${from > 0 ? ` past offset ${from}` : ""}`;
|
|
893
|
-
const { body, shown, capped } = renderGrepMatches(page.items.slice(0, DEFAULT_MAX_MATCHES), maxChars);
|
|
894
|
-
return (body +
|
|
895
|
-
listingNote(page, shown, "matches", "`include`") +
|
|
896
|
-
(capped
|
|
897
|
-
? `\n(Some lines were shortened. Read one in full with \`sb_read\` and an \`offset\`.)`
|
|
898
|
-
: ""));
|
|
899
|
-
});
|
|
900
|
-
}
|
|
901
|
-
}),
|
|
902
|
-
sb_exists: tool({
|
|
903
|
-
description: "Check whether a path exists in the workspace.",
|
|
904
|
-
inputSchema: z.object({ path: z.string().describe("Absolute path") }),
|
|
905
|
-
execute: async ({ path }) => {
|
|
906
|
-
const refusal = guardPath(path, "sb_exists");
|
|
907
|
-
if (refusal)
|
|
908
|
-
return refusal;
|
|
909
|
-
return inWorkspace("checking", path, async (fs) => {
|
|
910
|
-
return (await pathExists(fs, path))
|
|
911
|
-
? `${path} exists`
|
|
912
|
-
: `${path} does not exist`;
|
|
913
|
-
});
|
|
914
|
-
}
|
|
915
|
-
})
|
|
199
|
+
...execTools(ctx),
|
|
200
|
+
...fileTools(ctx),
|
|
201
|
+
...findTools(ctx)
|
|
916
202
|
};
|
|
917
203
|
}
|
|
918
204
|
/**
|
|
@@ -952,13 +238,13 @@ export function buildComputerTools(workspace, config, advisories) {
|
|
|
952
238
|
*/
|
|
953
239
|
export function computerExec(config) {
|
|
954
240
|
return async (command, options) => {
|
|
955
|
-
const
|
|
241
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
956
242
|
try {
|
|
957
243
|
// The caller forwards its execution's runtime state opaquely; only this side
|
|
958
244
|
// knows the key it might carry. That is what lets `/repo` reach a subagent's
|
|
959
245
|
// shared workspace without importing anything from here.
|
|
960
246
|
const name = workspaceNameFromRuntime(options?.runtime) ?? config.workspaceName();
|
|
961
|
-
const ws = __addDisposableResource(
|
|
247
|
+
const ws = __addDisposableResource(env_1, await openWorkspace(config.binding.get(config.binding.idFromName(name))), false);
|
|
962
248
|
const env = options?.env
|
|
963
249
|
? Object.fromEntries(Object.entries(options.env).filter((entry) => entry[1] !== undefined))
|
|
964
250
|
: undefined;
|
|
@@ -970,7 +256,7 @@ export function computerExec(config) {
|
|
|
970
256
|
// decide a tree is clean, and reads a sha out of it to push — all of which
|
|
971
257
|
// need `stdout` to carry the answer alone, with git's diagnostics on the
|
|
972
258
|
// channel git put them on.
|
|
973
|
-
const handle = __addDisposableResource(
|
|
259
|
+
const handle = __addDisposableResource(env_1, await ws.runtime.exec(withShell(command, config.shell), {
|
|
974
260
|
cwd: options?.cwd ?? config.cwd ?? DEFAULT_CWD,
|
|
975
261
|
encoding: "utf8",
|
|
976
262
|
timeoutMs,
|
|
@@ -994,12 +280,12 @@ export function computerExec(config) {
|
|
|
994
280
|
exitCode: result.exitCode
|
|
995
281
|
};
|
|
996
282
|
}
|
|
997
|
-
catch (
|
|
998
|
-
|
|
999
|
-
|
|
283
|
+
catch (e_1) {
|
|
284
|
+
env_1.error = e_1;
|
|
285
|
+
env_1.hasError = true;
|
|
1000
286
|
}
|
|
1001
287
|
finally {
|
|
1002
|
-
__disposeResources(
|
|
288
|
+
__disposeResources(env_1);
|
|
1003
289
|
}
|
|
1004
290
|
};
|
|
1005
291
|
}
|
|
@@ -1007,15 +293,13 @@ export function computer(config) {
|
|
|
1007
293
|
// Resolved per call, not memoized: `workspaceName` is a thunk precisely
|
|
1008
294
|
// because the name is not knowable at construction, and a stale stub would
|
|
1009
295
|
// silently route a second caller's commands into the first caller's files.
|
|
1010
|
-
const
|
|
1011
|
-
|
|
1012
|
-
return config.binding.get(config.binding.idFromName(name));
|
|
1013
|
-
};
|
|
296
|
+
const nameOf = (runtime) => workspaceNameFromRuntime(runtime) ?? config.workspaceName();
|
|
297
|
+
const host = (runtime) => config.binding.get(config.binding.idFromName(nameOf(runtime)));
|
|
1014
298
|
const tools = (runtime) => buildComputerTools(() => openWorkspace(host(runtime)), config,
|
|
1015
299
|
// No try/catch here: `buildComputerTools` fails the gate open itself, so
|
|
1016
300
|
// wrapping again would only make it look like the guarantee lives in two
|
|
1017
301
|
// places.
|
|
1018
|
-
() => host(runtime).advisories());
|
|
302
|
+
() => host(runtime).advisories(), () => openWorkspaceFs(host(runtime)), () => nameOf(runtime));
|
|
1019
303
|
return definePlugin({
|
|
1020
304
|
key: "computer",
|
|
1021
305
|
mainAgentTools: () => tools(),
|
|
@@ -1033,13 +317,13 @@ export function computer(config) {
|
|
|
1033
317
|
// The reason to prefer them is the one thing the model cannot infer from a
|
|
1034
318
|
// tool description: these read the durable workspace directly, so they are
|
|
1035
319
|
// the only tools that still answer while the container is unavailable.
|
|
1036
|
-
"Search with `sb_grep` and `sb_ls` rather than running `grep` or `find` through `sb_exec`. They read the workspace directly, so they keep working while the container is restarting or
|
|
320
|
+
"Search with `sb_grep` and `sb_ls` rather than running `grep` or `find` through `sb_exec`. They read the workspace directly, so they keep working while the container is restarting or an install is still running, and they come back with line numbers and a bounded result instead of a wall of text.",
|
|
1037
321
|
"Command output is truncated from the middle when large, so run targeted commands and read specific files rather than printing everything.",
|
|
1038
322
|
// Both halves of this matter and they pull in opposite directions, which
|
|
1039
323
|
// is why they are stated together rather than left for the model to work
|
|
1040
324
|
// out from a confusing result.
|
|
1041
325
|
"The checkout is durable: it survives between tasks and is still there after the container restarts, so it may already contain work from an earlier task — check before assuming it is empty.",
|
|
1042
|
-
"`node_modules` is
|
|
326
|
+
"`node_modules` is not durable: it lives on the container's disk, so a new container reinstalls it, and the file tools cannot see inside it — use `sb_exec` there. It is a mount point, so `rm -rf node_modules` fails; `npm ci` clears it itself.",
|
|
1043
327
|
// Stated up front rather than left to a refusal, so the model does not spend
|
|
1044
328
|
// a turn discovering it. The destination matters as much as the rule: a
|
|
1045
329
|
// prohibition with nowhere to go gets worked around.
|
|
@@ -1047,3 +331,31 @@ export function computer(config) {
|
|
|
1047
331
|
].join("\n")
|
|
1048
332
|
});
|
|
1049
333
|
}
|
|
334
|
+
// --- the host half ----------------------------------------------------------
|
|
335
|
+
/**
|
|
336
|
+
* The Durable Object everything above talks to.
|
|
337
|
+
*
|
|
338
|
+
* `./host/` is a directory rather than a subpath of its own, and that is the
|
|
339
|
+
* point: one capability is one import path, so a consumer cannot install the
|
|
340
|
+
* tools and miss the object they address. The two halves have opposite bundle
|
|
341
|
+
* costs — an agent that only calls `sb_exec` carries no container backend and no
|
|
342
|
+
* isomorphic-git — and `"sideEffects": false` is what keeps that true, since
|
|
343
|
+
* nothing here references the host unless the consumer does.
|
|
344
|
+
*
|
|
345
|
+
* The dependency runs one way for a mechanical reason: `./host/` imports the
|
|
346
|
+
* leaf modules beside this file, never this barrel, because this barrel
|
|
347
|
+
* re-exports `./host/` and a cycle through a module that builds a class at
|
|
348
|
+
* import time is a base that evaluates `undefined`.
|
|
349
|
+
*/
|
|
350
|
+
export { WorkspaceObjectBase, WORKSPACE_DIR, workspaceName } from "./host/workspace.js";
|
|
351
|
+
// The dependency install, exported because a host that wants to report on one —
|
|
352
|
+
// or a spec that drives it — needs the type, not because anything but
|
|
353
|
+
// `./host/workspace.ts` constructs it.
|
|
354
|
+
export { InstallJob } from "./host/install-job.js";
|
|
355
|
+
// The pull the library owns no alarm for. Its constants are exported because a
|
|
356
|
+
// host deferring to a drain has to back off at the same rate this does.
|
|
357
|
+
export { WorkspaceSync, SYNC_DRAIN_BUDGET_MS, SYNC_DRAIN_RESUME_MS, SYNC_DRAIN_MAX_BACKOFF_MS, syncRetryDelayMs } from "./host/sync.js";
|
|
358
|
+
// `TRUST_CA_COMMAND` is exported so a host can assert what its image will be
|
|
359
|
+
// asked to run — see the file for why this cannot live in an entrypoint.
|
|
360
|
+
export { ContainerTrust, TRUST_CA_COMMAND } from "./host/ca-trust.js";
|
|
361
|
+
export { WorkspaceGitHost } from "./host/git-host.js";
|