anchor-ai-sdk 0.8.9 → 0.8.11
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 +52 -1
- package/dist/tools/batch/cache.d.ts +49 -0
- package/dist/tools/batch/cache.d.ts.map +1 -0
- package/dist/tools/batch/cache.js +118 -0
- package/dist/tools/batch/cache.js.map +1 -0
- package/dist/tools/batch/commands.d.ts +10 -0
- package/dist/tools/batch/commands.d.ts.map +1 -0
- package/dist/tools/batch/commands.js +97 -0
- package/dist/tools/batch/commands.js.map +1 -0
- package/dist/tools/batch/edits.d.ts +33 -0
- package/dist/tools/batch/edits.d.ts.map +1 -0
- package/dist/tools/batch/edits.js +70 -0
- package/dist/tools/batch/edits.js.map +1 -0
- package/dist/tools/batch/index.d.ts +28 -0
- package/dist/tools/batch/index.d.ts.map +1 -0
- package/dist/tools/batch/index.js +491 -0
- package/dist/tools/batch/index.js.map +1 -0
- package/dist/tools/batch/ops.d.ts +75 -0
- package/dist/tools/batch/ops.d.ts.map +1 -0
- package/dist/tools/batch/ops.js +148 -0
- package/dist/tools/batch/ops.js.map +1 -0
- package/dist/tools/batch/render.d.ts +37 -0
- package/dist/tools/batch/render.d.ts.map +1 -0
- package/dist/tools/batch/render.js +69 -0
- package/dist/tools/batch/render.js.map +1 -0
- package/dist/tools/index.d.ts +2 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +1 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/search.d.ts.map +1 -1
- package/dist/tools/search.js +19 -6
- package/dist/tools/search.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -179,6 +179,57 @@ serialize. Omit `repository` only for a command that intentionally spans
|
|
|
179
179
|
repositories — it runs at the workspace root and serializes globally. Effectful
|
|
180
180
|
tools remain serial and ordered.
|
|
181
181
|
|
|
182
|
+
### Batching known steps: `createBatchTool`
|
|
183
|
+
|
|
184
|
+
A model that already knows its next steps can send them as one `batch` call —
|
|
185
|
+
one model turn instead of one per step:
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
import { createBatchTool } from "anchor-ai-sdk";
|
|
189
|
+
|
|
190
|
+
const tools = [...myWrappedTools];
|
|
191
|
+
const batch = createBatchTool({ tools }); // ops run through YOUR tools
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
```jsonc
|
|
195
|
+
{ "phases": [
|
|
196
|
+
{ "ops": [ { "op": "read", "paths": ["src/a.ts", "src/b.ts"] },
|
|
197
|
+
{ "op": "bash", "command": "git diff --stat HEAD~2..HEAD" } ] },
|
|
198
|
+
{ "ops": [ { "op": "edit", "path": "src/a.ts", "old": "…", "new": "…" } ] },
|
|
199
|
+
{ "ops": [ { "op": "bash", "command": "npx vitest run src/a.test.ts" } ] } ] }
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
- Ops: `read` (`path`/`paths`, `offset`, `limit`), `search` (`pattern`, `path`,
|
|
203
|
+
`glob`, `regex`), `bash` (`command`, `repository`, `timeout_secs`), `edit`
|
|
204
|
+
(`path`, `old`, `new`, `replaceAll`), `write` (`path`, `content`). A flat
|
|
205
|
+
`ops` list is one phase. At most 20 ops (each read path counts).
|
|
206
|
+
- A phase of reads/searches runs concurrently; a phase with an edit, write or
|
|
207
|
+
command runs in order. The batch is a serial mutation for the engine, so its
|
|
208
|
+
writes never overlap anything.
|
|
209
|
+
- Every op runs through the tool you passed for it (`read_file`, `search`,
|
|
210
|
+
`bash`, `edit_file`), so your guards hold per op; an op whose tool is absent
|
|
211
|
+
is refused on its own line and the rest still run. `edit`/`write` need
|
|
212
|
+
`edit_file`.
|
|
213
|
+
- Edits are all-or-nothing per file and written once through `edit_file`; a
|
|
214
|
+
bad `old` leaves the file untouched and names the failing edit.
|
|
215
|
+
- A retry of an edit that already applied reports `already applied` (its `old`
|
|
216
|
+
is gone and its `new` is there), as does a `write` whose file already has
|
|
217
|
+
that content; nothing is written and the op is ok. An ambiguous `old`, or
|
|
218
|
+
neither `old` nor `new` present, still fails.
|
|
219
|
+
- After a failed write the batch stops; fix and continue from that op. Once
|
|
220
|
+
the batch has written (an edit, a write, or a `bash` not recognised as a
|
|
221
|
+
read or check), a later failure skips the rest (`stopped after #N: …`);
|
|
222
|
+
before any write, a failure leaves the rest running. An explicit
|
|
223
|
+
`stopOnError` (true or false) overrides both.
|
|
224
|
+
- Results: one label per op (`#3 edit src/a.ts — ok (1 replacement, +4/−2)`),
|
|
225
|
+
each op capped at 300 lines and the batch at 600, with the full log's path
|
|
226
|
+
(`logDir`, default `<scratch>/batch`) in the result. The whole batch is bounded
|
|
227
|
+
by `maxBatchSecs` (600).
|
|
228
|
+
- Reads and searches are cached in the sandbox across turns (`cache.dir`,
|
|
229
|
+
default `<scratch>/batch-cache`, 200 MB LRU): a read is keyed by the file's
|
|
230
|
+
content hash, a search by its scope's file listing, so a changed file is
|
|
231
|
+
always a miss. Commands, edits and writes are never cached.
|
|
232
|
+
|
|
182
233
|
`run_tests` in a multi-repo workspace requires `repository`; its metadata
|
|
183
234
|
includes that repo and a tree hash computed there. LSP servers are rooted per
|
|
184
235
|
repo when you pass `repos` to `createLspTools({ repos })`.
|
|
@@ -702,7 +753,7 @@ and an extraction failure, not the full benchmark or a general superiority claim
|
|
|
702
753
|
|
|
703
754
|
## Status and contributing
|
|
704
755
|
|
|
705
|
-
**Early alpha, 0.x. Source version: 0.
|
|
756
|
+
**Early alpha, 0.x. Source version: 0.8.11; latest published release: v0.8.10.** Pin exact versions and test your
|
|
706
757
|
integration. Provider and runtime features are version-specific; the current
|
|
707
758
|
source may be ahead of the installed package. Daytona, agent teams, context
|
|
708
759
|
reset, MCP, and consumer skill bundles remain roadmap work in the reviewed
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type Sandbox } from "../../sandbox/types.js";
|
|
2
|
+
export interface BatchCacheOptions {
|
|
3
|
+
/** Directory inside the sandbox. Default: `<sandbox.scratch>/batch-cache`. */
|
|
4
|
+
dir?: string;
|
|
5
|
+
/** Evict oldest-used entries past this size. Default 200 MB. */
|
|
6
|
+
maxBytes?: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const BATCH_CACHE_MAX_BYTES: number;
|
|
9
|
+
/** Below Linux's 128 KB limit on one argv string, with room for the helpers. */
|
|
10
|
+
export declare const SCRIPT_MAX_CHARS = 90000;
|
|
11
|
+
export interface CacheQuery {
|
|
12
|
+
/** Caller's handle for this entry. */
|
|
13
|
+
id: string;
|
|
14
|
+
/** Everything but the stamp that decides the result. */
|
|
15
|
+
parts: Record<string, unknown>;
|
|
16
|
+
/** Shell that prints this entry's stamp (a hash) on stdout, or nothing. */
|
|
17
|
+
stamp: string;
|
|
18
|
+
}
|
|
19
|
+
export type CacheLookup = {
|
|
20
|
+
state: "hit";
|
|
21
|
+
content: string;
|
|
22
|
+
} | {
|
|
23
|
+
state: "miss";
|
|
24
|
+
key: string;
|
|
25
|
+
stamp: string;
|
|
26
|
+
script: string;
|
|
27
|
+
} | {
|
|
28
|
+
state: "none";
|
|
29
|
+
};
|
|
30
|
+
/** Stamp: the content hash of one file (nothing when it is not a file). */
|
|
31
|
+
export declare const fileStamp: (path: string) => string;
|
|
32
|
+
/** Stamp: a hash of the scope's file listing. */
|
|
33
|
+
export declare const treeStamp: (scope: string) => string;
|
|
34
|
+
export declare function cacheDir(sandbox: Sandbox, options: BatchCacheOptions): string;
|
|
35
|
+
/** One exec: every query's stamp, and the stored result when its key exists. */
|
|
36
|
+
export declare function lookup(sandbox: Sandbox, dir: string, queries: readonly CacheQuery[]): Promise<Map<string, CacheLookup>>;
|
|
37
|
+
/** Whether a result is worth a slot: complete, self-contained, and small. */
|
|
38
|
+
export declare function cacheable(ok: boolean, content: string): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Store misses whose stamp still holds, then evict past the size bound. Best
|
|
41
|
+
* effort: a store that fails costs a later miss, never a wrong answer.
|
|
42
|
+
*/
|
|
43
|
+
export declare function store(sandbox: Sandbox, dir: string, maxBytes: number, items: ReadonlyArray<{
|
|
44
|
+
key: string;
|
|
45
|
+
stamp: string;
|
|
46
|
+
script: string;
|
|
47
|
+
content: string;
|
|
48
|
+
}>): Promise<void>;
|
|
49
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../../src/tools/batch/cache.ts"],"names":[],"mappings":"AAoBA,OAAO,EAAO,KAAK,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAE3D,MAAM,WAAW,iBAAiB;IAChC,8EAA8E;IAC9E,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,gEAAgE;IAChE,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,eAAO,MAAM,qBAAqB,QAAoB,CAAC;AAIvD,gFAAgF;AAChF,eAAO,MAAM,gBAAgB,QAAS,CAAC;AAEvC,MAAM,WAAW,UAAU;IACzB,sCAAsC;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,wDAAwD;IACxD,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,2EAA2E;IAC3E,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,MAAM,WAAW,GACnB;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC7D;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAQtB,2EAA2E;AAC3E,eAAO,MAAM,SAAS,SAAU,MAAM,WAAgD,CAAC;AAEvF,iDAAiD;AACjD,eAAO,MAAM,SAAS,UAAW,MAAM,WAAsD,CAAC;AAK9F,wBAAgB,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,iBAAiB,GAAG,MAAM,CAE7E;AAED,gFAAgF;AAChF,wBAAsB,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,UAAU,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAoC7H;AAED,6EAA6E;AAC7E,wBAAgB,SAAS,CAAC,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAE/D;AAED;;;GAGG;AACH,wBAAsB,KAAK,CACzB,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,aAAa,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,GACpF,OAAO,CAAC,IAAI,CAAC,CAuBf"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The batch's per-op cache, kept IN the sandbox so it survives turns and dies
|
|
3
|
+
* with the sandbox (one per thread when the consumer keeps one per thread).
|
|
4
|
+
*
|
|
5
|
+
* Only reads and searches are cached. Each entry is keyed by its inputs and a
|
|
6
|
+
* stamp the sandbox computes at lookup time:
|
|
7
|
+
*
|
|
8
|
+
* read the file's content hash (sha256) — a changed byte is a miss
|
|
9
|
+
* search a hash of the scope's file listing (path, size, mtime, ctime),
|
|
10
|
+
* `.git` excluded — an added, removed or touched file is a miss
|
|
11
|
+
*
|
|
12
|
+
* A miss is stored only if the stamp is still the same after the op ran, so a
|
|
13
|
+
* file that changed while it was being read is never filed under the old
|
|
14
|
+
* stamp. Commands, edits and writes are never cached here. Entries are files
|
|
15
|
+
* under one directory, evicted oldest-used first past `maxBytes`.
|
|
16
|
+
*
|
|
17
|
+
* Lookups for a whole phase share one exec, and so do its stores: on a
|
|
18
|
+
* microVM the round trip, not the work, is the cost.
|
|
19
|
+
*/
|
|
20
|
+
import { createHash } from "node:crypto";
|
|
21
|
+
import { shq } from "../../sandbox/types.js";
|
|
22
|
+
export const BATCH_CACHE_MAX_BYTES = 200 * 1024 * 1024;
|
|
23
|
+
/** Larger results are not worth a slot (and would strain one exec's argv). */
|
|
24
|
+
const STORE_MAX_CHARS = 40_000;
|
|
25
|
+
const MARK = "@@anchor-batch-cache ";
|
|
26
|
+
/** Below Linux's 128 KB limit on one argv string, with room for the helpers. */
|
|
27
|
+
export const SCRIPT_MAX_CHARS = 90_000;
|
|
28
|
+
const HELPERS = [
|
|
29
|
+
`__h() { if command -v sha256sum >/dev/null 2>&1; then sha256sum; else shasum -a 256; fi | cut -c1-64; }`,
|
|
30
|
+
// GNU find prints the listing itself; BSD find hands it to stat(1).
|
|
31
|
+
`__l() { if find . -maxdepth 0 -printf '' >/dev/null 2>&1; then find "$1" -name .git -prune -o -type f -printf '%p %s %T@ %C@\\n' 2>/dev/null; else find "$1" -name .git -prune -o -type f -exec stat -f '%N %z %Fm %Fc' {} + 2>/dev/null; fi | LC_ALL=C sort; }`,
|
|
32
|
+
].join("\n");
|
|
33
|
+
/** Stamp: the content hash of one file (nothing when it is not a file). */
|
|
34
|
+
export const fileStamp = (path) => `[ -f ${shq(path)} ] && __h < ${shq(path)}`;
|
|
35
|
+
/** Stamp: a hash of the scope's file listing. */
|
|
36
|
+
export const treeStamp = (scope) => `[ -e ${shq(scope)} ] && __l ${shq(scope)} | __h`;
|
|
37
|
+
const prefixOf = (parts) => createHash("sha256").update(JSON.stringify(parts)).digest("hex").slice(0, 32);
|
|
38
|
+
export function cacheDir(sandbox, options) {
|
|
39
|
+
return options.dir ?? `${sandbox.scratch}/batch-cache`;
|
|
40
|
+
}
|
|
41
|
+
/** One exec: every query's stamp, and the stored result when its key exists. */
|
|
42
|
+
export async function lookup(sandbox, dir, queries) {
|
|
43
|
+
const out = new Map();
|
|
44
|
+
if (queries.length === 0)
|
|
45
|
+
return out;
|
|
46
|
+
const body = queries.map((query, i) => {
|
|
47
|
+
const prefix = prefixOf(query.parts);
|
|
48
|
+
return [
|
|
49
|
+
`__s=$( ${query.stamp} ) && [ -n "$__s" ] && {`,
|
|
50
|
+
` __k=$(printf '%s|%s' ${shq(prefix)} "$__s" | __h)`,
|
|
51
|
+
` if [ -f "$__d/$__k" ]; then touch "$__d/$__k"; printf '\\n${MARK}${i} hit %s %s\\n' "$__k" "$__s"; base64 < "$__d/$__k" | tr -d '\\n';`,
|
|
52
|
+
` else printf '\\n${MARK}${i} miss %s %s\\n' "$__k" "$__s"; fi`,
|
|
53
|
+
`} || printf '\\n${MARK}${i} none\\n'`,
|
|
54
|
+
].join("\n");
|
|
55
|
+
});
|
|
56
|
+
const script = [HELPERS, `__d=${shq(dir)}`, `mkdir -p "$__d" 2>/dev/null`, ...body].join("\n");
|
|
57
|
+
const result = await sandbox.exec(script, { timeoutSecs: 60, concurrent: true }).catch(() => undefined);
|
|
58
|
+
for (const query of queries)
|
|
59
|
+
out.set(query.id, { state: "none" });
|
|
60
|
+
if (!result)
|
|
61
|
+
return out;
|
|
62
|
+
const chunks = result.stdout.split(`\n${MARK}`).slice(1);
|
|
63
|
+
for (const chunk of chunks) {
|
|
64
|
+
const newline = chunk.indexOf("\n");
|
|
65
|
+
const head = (newline === -1 ? chunk : chunk.slice(0, newline)).trim().split(" ");
|
|
66
|
+
const rest = newline === -1 ? "" : chunk.slice(newline + 1).trim();
|
|
67
|
+
const query = queries[Number(head[0])];
|
|
68
|
+
if (!query)
|
|
69
|
+
continue;
|
|
70
|
+
const [, state, key, stamp] = head;
|
|
71
|
+
if (state === "hit" && key) {
|
|
72
|
+
try {
|
|
73
|
+
out.set(query.id, { state: "hit", content: Buffer.from(rest, "base64").toString("utf8") });
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
/* an unreadable entry is a miss without a slot */
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else if (state === "miss" && key && stamp && /^[0-9a-f]{64}$/.test(key)) {
|
|
80
|
+
out.set(query.id, { state: "miss", key, stamp, script: query.stamp });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
/** Whether a result is worth a slot: complete, self-contained, and small. */
|
|
86
|
+
export function cacheable(ok, content) {
|
|
87
|
+
return ok && content.length <= STORE_MAX_CHARS && !content.includes("full output saved:");
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Store misses whose stamp still holds, then evict past the size bound. Best
|
|
91
|
+
* effort: a store that fails costs a later miss, never a wrong answer.
|
|
92
|
+
*/
|
|
93
|
+
export async function store(sandbox, dir, maxBytes, items) {
|
|
94
|
+
if (items.length === 0)
|
|
95
|
+
return;
|
|
96
|
+
const writes = items.map((item) => {
|
|
97
|
+
const b64 = Buffer.from(item.content, "utf8").toString("base64");
|
|
98
|
+
return `if [ "$( ${item.script} )" = ${shq(item.stamp)} ]; then printf %s ${shq(b64)} | base64 -d > "$__d/${item.key}.tmp.$$" && mv -f "$__d/${item.key}.tmp.$$" "$__d/${item.key}"; fi`;
|
|
99
|
+
});
|
|
100
|
+
const maxKb = Math.max(1, Math.floor(maxBytes / 1024));
|
|
101
|
+
const evict = `while [ "$(du -sk "$__d" 2>/dev/null | cut -f1)" -gt ${maxKb} ]; do __f=$(ls -tr "$__d" | head -n 1); [ -n "$__f" ] || break; rm -f "$__d/$__f"; done`;
|
|
102
|
+
// One script is one argv string on the guest, which Linux caps at 128 KB.
|
|
103
|
+
const groups = [[]];
|
|
104
|
+
let size = 0;
|
|
105
|
+
for (const write of writes) {
|
|
106
|
+
if (size + write.length > SCRIPT_MAX_CHARS && groups[groups.length - 1].length > 0) {
|
|
107
|
+
groups.push([]);
|
|
108
|
+
size = 0;
|
|
109
|
+
}
|
|
110
|
+
groups[groups.length - 1].push(write);
|
|
111
|
+
size += write.length;
|
|
112
|
+
}
|
|
113
|
+
for (const [i, group] of groups.entries()) {
|
|
114
|
+
const script = [HELPERS, `__d=${shq(dir)}`, `mkdir -p "$__d"`, ...group, ...(i === groups.length - 1 ? [evict] : [])].join("\n");
|
|
115
|
+
await sandbox.exec(script, { timeoutSecs: 60, concurrent: true }).catch(() => undefined);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../../src/tools/batch/cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,GAAG,EAAgB,MAAM,wBAAwB,CAAC;AAS3D,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AACvD,8EAA8E;AAC9E,MAAM,eAAe,GAAG,MAAM,CAAC;AAC/B,MAAM,IAAI,GAAG,uBAAuB,CAAC;AACrC,gFAAgF;AAChF,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAgBvC,MAAM,OAAO,GAAG;IACd,yGAAyG;IACzG,oEAAoE;IACpE,iQAAiQ;CAClQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAEb,2EAA2E;AAC3E,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,QAAQ,GAAG,CAAC,IAAI,CAAC,eAAe,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;AAEvF,iDAAiD;AACjD,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,QAAQ,GAAG,CAAC,KAAK,CAAC,aAAa,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC;AAE9F,MAAM,QAAQ,GAAG,CAAC,KAA8B,EAAE,EAAE,CAClD,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,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`bash` op change the workspace? Pure, and deliberately
|
|
3
|
+
* conservative: a command is read-only only when every segment is a known
|
|
4
|
+
* read (ls, cat, rg, git status…) or check (tests, typecheck, lint without
|
|
5
|
+
* --fix), with no redirection into a file and no command substitution.
|
|
6
|
+
* Anything unrecognised counts as a write. The batch uses the answer for one
|
|
7
|
+
* thing: once a write has run, a later failure stops the batch by default.
|
|
8
|
+
*/
|
|
9
|
+
export declare function isReadOnlyCommand(command: string): boolean;
|
|
10
|
+
//# sourceMappingURL=commands.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../../../src/tools/batch/commands.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAqEH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAK1D"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Does a batch `bash` op change the workspace? Pure, and deliberately
|
|
3
|
+
* conservative: a command is read-only only when every segment is a known
|
|
4
|
+
* read (ls, cat, rg, git status…) or check (tests, typecheck, lint without
|
|
5
|
+
* --fix), with no redirection into a file and no command substitution.
|
|
6
|
+
* Anything unrecognised counts as a write. The batch uses the answer for one
|
|
7
|
+
* thing: once a write has run, a later failure stops the batch by default.
|
|
8
|
+
*/
|
|
9
|
+
const READ_HEADS = new Set([
|
|
10
|
+
"ls", "cat", "head", "tail", "wc", "grep", "egrep", "fgrep", "rg", "stat", "file", "pwd", "echo",
|
|
11
|
+
"printf", "true", "false", "test", "[", "diff", "cmp", "tree", "du", "df", "which", "type", "uniq",
|
|
12
|
+
"cut", "tr", "jq", "nl", "basename", "dirname", "realpath", "readlink", "date", "uname", "whoami",
|
|
13
|
+
"id", "sha256sum", "shasum", "md5sum", "column", "seq", "sleep", "exit",
|
|
14
|
+
]);
|
|
15
|
+
const GIT_READS = new Set([
|
|
16
|
+
"status", "diff", "log", "show", "ls-files", "ls-tree", "grep", "rev-parse", "blame", "describe",
|
|
17
|
+
"shortlog", "cat-file", "merge-base", "rev-list", "name-rev",
|
|
18
|
+
]);
|
|
19
|
+
/** Test runners, type checkers, linters (their own output dirs aside). */
|
|
20
|
+
const CHECK_HEADS = new Set(["vitest", "jest", "pytest", "mypy", "pyright", "playwright", "eslint", "tsc", "ruff", "prettier", "mocha"]);
|
|
21
|
+
const CHECK_SCRIPT = /^(?:test|vitest|jest|typecheck|check|lint|tsc)(?:$|:)/;
|
|
22
|
+
const PACKAGE_MANAGERS = new Set(["npm", "pnpm", "yarn", "bun"]);
|
|
23
|
+
const RUNNERS = new Set(["npx", "bunx", "pnpx"]);
|
|
24
|
+
const withoutQuoted = (command) => command.replace(/'[^']*'|"(?:\\.|[^"\\])*"/g, "''");
|
|
25
|
+
function checkTool(head, args) {
|
|
26
|
+
if (args.some((arg) => arg === "--fix" || arg === "--write" || arg.startsWith("--fix=")))
|
|
27
|
+
return false;
|
|
28
|
+
if (head === "tsc")
|
|
29
|
+
return args.includes("--noEmit");
|
|
30
|
+
if (head === "ruff")
|
|
31
|
+
return args[0] === "check";
|
|
32
|
+
if (head === "prettier")
|
|
33
|
+
return args.includes("--check");
|
|
34
|
+
return CHECK_HEADS.has(head);
|
|
35
|
+
}
|
|
36
|
+
function readOnlySegment(segment) {
|
|
37
|
+
const words = segment.trim().split(/\s+/).filter(Boolean);
|
|
38
|
+
let i = 0;
|
|
39
|
+
while (/^[A-Za-z_][A-Za-z0-9_]*=/.test(words[i] ?? ""))
|
|
40
|
+
i += 1;
|
|
41
|
+
if (words[i] === "env")
|
|
42
|
+
i += 1;
|
|
43
|
+
if (words[i] === "timeout") {
|
|
44
|
+
i += 1;
|
|
45
|
+
while (words[i]?.startsWith("-"))
|
|
46
|
+
i += 1;
|
|
47
|
+
i += 1;
|
|
48
|
+
}
|
|
49
|
+
const head = words[i]?.split("/").at(-1);
|
|
50
|
+
const args = words.slice(i + 1);
|
|
51
|
+
if (head === undefined)
|
|
52
|
+
return true;
|
|
53
|
+
if (head === "cd")
|
|
54
|
+
return true;
|
|
55
|
+
if (READ_HEADS.has(head))
|
|
56
|
+
return true;
|
|
57
|
+
if (head === "sed")
|
|
58
|
+
return !args.some((arg) => /^-[a-zA-Z]*i/.test(arg) || arg.startsWith("--in-place"));
|
|
59
|
+
if (head === "find")
|
|
60
|
+
return !args.some((arg) => /^-(?:delete|exec|execdir|ok|okdir|fprint\w*|fls)$/.test(arg));
|
|
61
|
+
if (head === "sort")
|
|
62
|
+
return !args.some((arg) => arg === "-o" || arg.startsWith("--output"));
|
|
63
|
+
if (head === "git") {
|
|
64
|
+
const sub = args[0] === "-C" ? args[2] : args[0];
|
|
65
|
+
return sub !== undefined && GIT_READS.has(sub);
|
|
66
|
+
}
|
|
67
|
+
if (PACKAGE_MANAGERS.has(head)) {
|
|
68
|
+
const script = ["run", "run-script"].includes(args[0] ?? "") ? args[1] : args[0];
|
|
69
|
+
if (script && CHECK_SCRIPT.test(script))
|
|
70
|
+
return !args.includes("--fix");
|
|
71
|
+
if (head !== "npm" && ["exec", "x", "dlx"].includes(args[0] ?? ""))
|
|
72
|
+
return checkTool(args[1] ?? "", args.slice(2));
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
if (RUNNERS.has(head)) {
|
|
76
|
+
const rest = args.filter((arg) => arg !== "--yes" && arg !== "-y" && arg !== "--no-install");
|
|
77
|
+
return checkTool(rest[0] ?? "", rest.slice(1));
|
|
78
|
+
}
|
|
79
|
+
if (/^python3?$/.test(head))
|
|
80
|
+
return args[0] === "-m" && ["pytest", "mypy", "unittest"].includes(args[1] ?? "");
|
|
81
|
+
if (head === "go")
|
|
82
|
+
return ["test", "vet"].includes(args[0] ?? "");
|
|
83
|
+
if (head === "cargo")
|
|
84
|
+
return ["test", "check", "clippy"].includes(args[0] ?? "") && !args.includes("--fix");
|
|
85
|
+
if (head === "make")
|
|
86
|
+
return ["test", "check", "lint"].includes(args[0] ?? "");
|
|
87
|
+
return checkTool(head, args);
|
|
88
|
+
}
|
|
89
|
+
export function isReadOnlyCommand(command) {
|
|
90
|
+
if (/\$\(|`/.test(command))
|
|
91
|
+
return false;
|
|
92
|
+
const bare = withoutQuoted(command).replace(/\d*>&\d+|&>\s*\/dev\/null|\d*>>?\s*\/dev\/null/g, " ");
|
|
93
|
+
if (/>/.test(bare) || /\btee\b/.test(bare))
|
|
94
|
+
return false;
|
|
95
|
+
return bare.split(/&&|\|\||[;|&\n]/).every(readOnlySegment);
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=commands.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commands.js","sourceRoot":"","sources":["../../../src/tools/batch/commands.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,UAAU,GAAwB,IAAI,GAAG,CAAC;IAC9C,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAChG,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM;IAClG,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ;IACjG,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM;CACxE,CAAC,CAAC;AAEH,MAAM,SAAS,GAAwB,IAAI,GAAG,CAAC;IAC7C,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,U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,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,iDAAiD,EAAE,GAAG,CAAC,CAAC;IACpG,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IACzD,OAAO,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;AAC9D,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface FileEdit {
|
|
2
|
+
/** The op number, for the failure message. */
|
|
3
|
+
index: number;
|
|
4
|
+
old: string;
|
|
5
|
+
new: string;
|
|
6
|
+
replaceAll: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface EditStat {
|
|
9
|
+
index: number;
|
|
10
|
+
replacements: number;
|
|
11
|
+
added: number;
|
|
12
|
+
removed: number;
|
|
13
|
+
alreadyApplied?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export type EditOutcome = {
|
|
16
|
+
ok: true;
|
|
17
|
+
content: string;
|
|
18
|
+
stats: EditStat[];
|
|
19
|
+
} | {
|
|
20
|
+
ok: false;
|
|
21
|
+
failed: number;
|
|
22
|
+
reason: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Lines a replacement adds and removes, ignoring the lines it shares at the
|
|
26
|
+
* start and end (context the model included to make `old` unique).
|
|
27
|
+
*/
|
|
28
|
+
export declare function lineDelta(oldText: string, newText: string): {
|
|
29
|
+
added: number;
|
|
30
|
+
removed: number;
|
|
31
|
+
};
|
|
32
|
+
export declare function applyEdits(content: string, edits: readonly FileEdit[]): EditOutcome;
|
|
33
|
+
//# sourceMappingURL=edits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edits.d.ts","sourceRoot":"","sources":["../../../src/tools/batch/edits.ts"],"names":[],"mappings":"AAcA,MAAM,WAAW,QAAQ;IACvB,8CAA8C;IAC9C,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,MAAM,WAAW,GACnB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,QAAQ,EAAE,CAAA;CAAE,GAChD;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAElD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAa9F;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,QAAQ,EAAE,GAAG,WAAW,CAoCnF"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All-or-nothing edits for one file, in TS-land where exact semantics are
|
|
3
|
+
* ours. Pure: the caller reads the file once, applies every edit here in
|
|
4
|
+
* order (each sees the previous one's result), and writes once — or, when any
|
|
5
|
+
* `old` fails to match, writes nothing and names the edit that failed.
|
|
6
|
+
*
|
|
7
|
+
* Retry safety: an edit whose `old` is gone but whose `new` is already there
|
|
8
|
+
* (or, when `new` contains `old`, already stands everywhere `old` does) is
|
|
9
|
+
* `alreadyApplied` — a model retrying a partly applied batch must not apply it
|
|
10
|
+
* twice or see a fresh failure. A deletion (`new` empty) has nothing to find,
|
|
11
|
+
* so a missing `old` still fails.
|
|
12
|
+
*/
|
|
13
|
+
import { countOccurrences } from "../edit-file.js";
|
|
14
|
+
/**
|
|
15
|
+
* Lines a replacement adds and removes, ignoring the lines it shares at the
|
|
16
|
+
* start and end (context the model included to make `old` unique).
|
|
17
|
+
*/
|
|
18
|
+
export function lineDelta(oldText, newText) {
|
|
19
|
+
const a = oldText.split("\n");
|
|
20
|
+
const b = newText.split("\n");
|
|
21
|
+
let head = 0;
|
|
22
|
+
while (head < a.length && head < b.length && a[head] === b[head])
|
|
23
|
+
head += 1;
|
|
24
|
+
let tail = 0;
|
|
25
|
+
while (tail < a.length - head && tail < b.length - head && a[a.length - 1 - tail] === b[b.length - 1 - tail])
|
|
26
|
+
tail += 1;
|
|
27
|
+
if (oldText === newText)
|
|
28
|
+
return { added: 0, removed: 0 };
|
|
29
|
+
return {
|
|
30
|
+
added: newText === "" ? 0 : b.length - head - tail,
|
|
31
|
+
removed: oldText === "" ? 0 : a.length - head - tail,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function applyEdits(content, edits) {
|
|
35
|
+
let current = content;
|
|
36
|
+
const stats = [];
|
|
37
|
+
for (const edit of edits) {
|
|
38
|
+
const count = countOccurrences(current, edit.old);
|
|
39
|
+
const present = edit.new === "" ? 0 : countOccurrences(current, edit.new);
|
|
40
|
+
const applied = count === 0
|
|
41
|
+
? present > 0
|
|
42
|
+
: edit.new.includes(edit.old) && present >= count && (count === 1 || edit.replaceAll);
|
|
43
|
+
if (applied) {
|
|
44
|
+
stats.push({ index: edit.index, replacements: 0, added: 0, removed: 0, alreadyApplied: true });
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (count === 0)
|
|
48
|
+
return {
|
|
49
|
+
ok: false,
|
|
50
|
+
failed: edit.index,
|
|
51
|
+
reason: `edit #${edit.index}: "old" not found — read the file and copy the exact text (whitespace included)`,
|
|
52
|
+
};
|
|
53
|
+
if (count > 1 && !edit.replaceAll)
|
|
54
|
+
return {
|
|
55
|
+
ok: false,
|
|
56
|
+
failed: edit.index,
|
|
57
|
+
reason: `edit #${edit.index}: "old" appears ${count} times — include more surrounding lines so it matches once, or set replaceAll`,
|
|
58
|
+
};
|
|
59
|
+
current = edit.replaceAll ? current.split(edit.old).join(edit.new) : current.replace(edit.old, () => edit.new);
|
|
60
|
+
const delta = lineDelta(edit.old, edit.new);
|
|
61
|
+
stats.push({
|
|
62
|
+
index: edit.index,
|
|
63
|
+
replacements: count,
|
|
64
|
+
added: delta.added * count,
|
|
65
|
+
removed: delta.removed * count,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return { ok: true, content: current, stats };
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=edits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edits.js","sourceRoot":"","sources":["../../../src/tools/batch/edits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAsBnD;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,OAAe,EAAE,OAAe;IACxD,MAAM,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC9B,MAAM,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,OAAO,IAAI,GAAG,CAAC,CAAC,MAAM,IAAI,IAAI,GAAG,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;QAAE,IAAI,IAAI,CAAC,CAAC;IAC5E,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,OAAO,IAAI,GAAG,CAAC,CAAC,MAAM,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,CAAC,MAAM,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,GAAG,IAAI,CAA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types.js";
|
|
2
|
+
import { type BatchCacheOptions } from "./cache.js";
|
|
3
|
+
import { type RenderCaps } from "./render.js";
|
|
4
|
+
export interface BatchToolOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The tools ops run through, by name: read → `read_file`, search →
|
|
7
|
+
* `search`, bash → `bash`, edit/write → `edit_file`. Pass the agent's own
|
|
8
|
+
* (wrapped) tools so their guards apply per op. Default: the built-ins.
|
|
9
|
+
*/
|
|
10
|
+
tools?: readonly Tool[];
|
|
11
|
+
/** Map a path the model wrote to the one the tools would act on (cache keys, edits, writes). */
|
|
12
|
+
resolvePath?: (path: string, ctx: ToolContext) => Promise<string>;
|
|
13
|
+
/** Per-op cache for reads and searches; `false` turns it off. */
|
|
14
|
+
cache?: BatchCacheOptions | false;
|
|
15
|
+
/** Where the full log goes. Default `<sandbox.scratch>/batch`. */
|
|
16
|
+
logDir?: string;
|
|
17
|
+
/** Wall clock for the whole batch. Default 600 s. */
|
|
18
|
+
maxBatchSecs?: number;
|
|
19
|
+
caps?: Partial<RenderCaps>;
|
|
20
|
+
}
|
|
21
|
+
export declare function createBatchTool(options?: BatchToolOptions): Tool;
|
|
22
|
+
export { applyEdits, lineDelta } from "./edits.js";
|
|
23
|
+
export { parseBatch, BATCH_MAX_OPS } from "./ops.js";
|
|
24
|
+
export type { BatchUnit, ParsedBatch } from "./ops.js";
|
|
25
|
+
export { allotment, renderBatch, BATCH_RENDER_CAPS } from "./render.js";
|
|
26
|
+
export type { OpReport, RenderCaps } from "./render.js";
|
|
27
|
+
export type { BatchCacheOptions } from "./cache.js";
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/tools/batch/index.ts"],"names":[],"mappings":"AAkCA,OAAO,EAA8B,KAAK,IAAI,EAAE,KAAK,WAAW,EAAmB,MAAM,aAAa,CAAC;AAOvG,OAAO,EAQL,KAAK,iBAAiB,EAGvB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAuE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAEnH,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,KAAK,CAAC,EAAE,SAAS,IAAI,EAAE,CAAC;IACxB,gGAAgG;IAChG,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAClE,iEAAiE;IACjE,KAAK,CAAC,EAAE,iBAAiB,GAAG,KAAK,CAAC;IAClC,kEAAkE;IAClE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;CAC5B;AA+VD,wBAAgB,eAAe,CAAC,OAAO,GAAE,gBAAqB,GAAG,IAAI,CA4JpE;AAED,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACrD,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACxE,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACxD,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
|