toolnexus 0.19.0 → 0.21.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 +40 -0
- package/dist/agents/home.js +4 -2
- package/dist/agents/runtime.d.ts +12 -3
- package/dist/agents/runtime.js +4 -3
- package/dist/batteries.d.ts +223 -0
- package/dist/batteries.js +488 -0
- package/dist/builtin.d.ts +32 -2
- package/dist/builtin.js +324 -46
- package/dist/classifier.d.ts +14 -0
- package/dist/classifier.js +37 -2
- package/dist/client.d.ts +35 -17
- package/dist/client.js +153 -67
- package/dist/index.d.ts +3 -0
- package/dist/index.js +6 -0
- package/dist/judge.d.ts +98 -0
- package/dist/judge.js +199 -0
- package/package.json +2 -2
- package/src/agents/home.ts +4 -2
- package/src/agents/runtime.ts +12 -5
- package/src/batteries.ts +614 -0
- package/src/builtin.ts +361 -46
- package/src/classifier.ts +39 -2
- package/src/client.ts +166 -67
- package/src/index.ts +26 -0
- package/src/judge.ts +305 -0
package/src/builtin.ts
CHANGED
|
@@ -7,19 +7,196 @@
|
|
|
7
7
|
* ToolResult{isError:true}, never a thrown exception across the boundary. Paths
|
|
8
8
|
* resolve relative to the process working directory unless absolute.
|
|
9
9
|
*/
|
|
10
|
-
import { spawn } from "node:child_process"
|
|
10
|
+
import { spawn, spawnSync } from "node:child_process"
|
|
11
11
|
import fs from "node:fs/promises"
|
|
12
|
-
import { existsSync, readdirSync } from "node:fs"
|
|
12
|
+
import { existsSync, readdirSync, realpathSync } from "node:fs"
|
|
13
13
|
import { compareCodePoints, sortEntriesByName, toPosixPath } from "./order.js"
|
|
14
14
|
import path from "node:path"
|
|
15
15
|
import type { JSONSchema, Tool, ToolContext, ToolResult } from "./types.js"
|
|
16
16
|
import { pending } from "./types.js"
|
|
17
17
|
import { mediaTypeFor } from "./content.js"
|
|
18
18
|
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Config for the single global builtin toggle (mirrors MCP isEnabled precedence),
|
|
21
|
+
* plus the host boundary: which interpreter `bash` runs, what a relative path
|
|
22
|
+
* resolves against, and whether paths leaving it are refused (SPEC §4A, ADR 0034).
|
|
23
|
+
*/
|
|
20
24
|
export type BuiltinsConfig =
|
|
21
25
|
| boolean
|
|
22
|
-
| {
|
|
26
|
+
| {
|
|
27
|
+
enabled?: boolean
|
|
28
|
+
disabled?: boolean
|
|
29
|
+
tools?: Record<string, boolean>
|
|
30
|
+
/**
|
|
31
|
+
* The argv prefix `bash` runs a command with — `["sh","-c"]`,
|
|
32
|
+
* `["cmd","/d","/s","/c"]`, `["powershell","-NoProfile","-Command"]`. Set,
|
|
33
|
+
* it is used verbatim. Absent, an interpreter is DETECTED at toolkit
|
|
34
|
+
* construction (POSIX `sh -c`; Windows %COMSPEC% → pwsh → powershell → bash).
|
|
35
|
+
*/
|
|
36
|
+
shell?: string[]
|
|
37
|
+
/**
|
|
38
|
+
* What a RELATIVE path means, for every builtin that touches the
|
|
39
|
+
* filesystem — including the paths inside apply_patch's patch text, and as
|
|
40
|
+
* bash's default workdir. Empty ⇒ the process cwd, i.e. today's behaviour.
|
|
41
|
+
*/
|
|
42
|
+
baseDir?: string
|
|
43
|
+
/**
|
|
44
|
+
* Refuse any path whose CANONICAL form leaves baseDir (symlinks, Windows
|
|
45
|
+
* junctions and short names resolved on both sides), and Windows reserved
|
|
46
|
+
* device names. Default off. A guarantee about path resolution in the file
|
|
47
|
+
* builtins, NOT a sandbox — `bash` still reaches the whole filesystem.
|
|
48
|
+
*/
|
|
49
|
+
confineToBaseDir?: boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* How long a job gets between "please stop" and "stop". Fixed, and identical in
|
|
54
|
+
* every port, so a timeout means the same thing everywhere.
|
|
55
|
+
*/
|
|
56
|
+
const KILL_GRACE_MS = 2000
|
|
57
|
+
|
|
58
|
+
const WIN_RESERVED = new Set([
|
|
59
|
+
"CON", "PRN", "AUX", "NUL",
|
|
60
|
+
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
|
|
61
|
+
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
|
|
62
|
+
])
|
|
63
|
+
|
|
64
|
+
/** Does `name` resolve as an executable on PATH? (PATHEXT-aware on Windows.) */
|
|
65
|
+
function resolvesOnPath(name: string): boolean {
|
|
66
|
+
if (name.includes(path.sep) || name.includes("/")) return existsSync(name)
|
|
67
|
+
const dirs = (process.env.PATH ?? "").split(path.delimiter).filter(Boolean)
|
|
68
|
+
const exts =
|
|
69
|
+
process.platform === "win32"
|
|
70
|
+
? (process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)
|
|
71
|
+
: [""]
|
|
72
|
+
for (const dir of dirs) {
|
|
73
|
+
for (const ext of exts) {
|
|
74
|
+
if (existsSync(path.join(dir, name + ext))) return true
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The interpreters tried when the host names none. %COMSPEC% leads on Windows
|
|
82
|
+
* because PowerShell is routinely blocked by execution or application-control
|
|
83
|
+
* policy, while %COMSPEC% is always present.
|
|
84
|
+
*/
|
|
85
|
+
function shellCandidates(): string[][] {
|
|
86
|
+
// `/bin/sh` first, then a PATH lookup: `shell: true` means literally
|
|
87
|
+
// `/bin/sh`, while spawning the bare name `sh` is a PATH lookup — two
|
|
88
|
+
// different binaries on a machine that has both. Naming the absolute path
|
|
89
|
+
// first is what makes dropping `shell: true` byte-identical here.
|
|
90
|
+
if (process.platform !== "win32") return [["/bin/sh", "-c"], ["sh", "-c"]]
|
|
91
|
+
const out: string[][] = []
|
|
92
|
+
if (process.env.COMSPEC) out.push([process.env.COMSPEC, "/d", "/s", "/c"])
|
|
93
|
+
out.push(
|
|
94
|
+
["cmd.exe", "/d", "/s", "/c"],
|
|
95
|
+
["pwsh", "-NoProfile", "-Command"],
|
|
96
|
+
["powershell", "-NoProfile", "-Command"],
|
|
97
|
+
["bash", "-lc"],
|
|
98
|
+
)
|
|
99
|
+
return out
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The host boundary the builtins are allowed to know about. Constructed once,
|
|
104
|
+
* at toolkit construction: a missing interpreter is a configuration fact, and
|
|
105
|
+
* turn fourteen of a paid run is the expensive place to learn it.
|
|
106
|
+
*/
|
|
107
|
+
class BuiltinEnv {
|
|
108
|
+
readonly shell: string[]
|
|
109
|
+
readonly shellError?: Error
|
|
110
|
+
readonly baseDir: string
|
|
111
|
+
readonly confine: boolean
|
|
112
|
+
|
|
113
|
+
constructor(cfg?: BuiltinsConfig) {
|
|
114
|
+
const obj = typeof cfg === "object" && cfg !== null ? cfg : {}
|
|
115
|
+
this.baseDir = obj.baseDir ?? ""
|
|
116
|
+
this.confine = obj.confineToBaseDir === true
|
|
117
|
+
if (obj.shell && obj.shell.length > 0) {
|
|
118
|
+
this.shell = obj.shell
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
const tried: string[] = []
|
|
122
|
+
for (const argv of shellCandidates()) {
|
|
123
|
+
tried.push(argv[0])
|
|
124
|
+
if (resolvesOnPath(argv[0])) {
|
|
125
|
+
this.shell = argv
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
this.shell = []
|
|
130
|
+
this.shellError = new Error(
|
|
131
|
+
`no shell interpreter found (tried: ${tried.join(", ")}); set builtins.shell, or disable the bash builtin with builtins.tools.bash = false`,
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The interpreter argv, or the detection error when there is none. */
|
|
136
|
+
shellArgv(): string[] {
|
|
137
|
+
if (this.shellError) throw this.shellError
|
|
138
|
+
return this.shell
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get shellLabel(): string {
|
|
142
|
+
return this.shell.join(" ")
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The directory a command or a walk starts from. */
|
|
146
|
+
dir(): string {
|
|
147
|
+
return this.baseDir || process.cwd()
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Map a tool-supplied path onto the filesystem: relative to baseDir (or, with
|
|
152
|
+
* none, exactly as before), and refused when confinement is on and the
|
|
153
|
+
* canonical target is outside the base.
|
|
154
|
+
*/
|
|
155
|
+
resolvePath(p: string): string {
|
|
156
|
+
if (this.confine && !this.baseDir) {
|
|
157
|
+
throw new Error("confineToBaseDir is set but baseDir is empty")
|
|
158
|
+
}
|
|
159
|
+
const full = this.baseDir && !path.isAbsolute(p) ? path.join(this.baseDir, p) : p
|
|
160
|
+
if (!this.confine) return full
|
|
161
|
+
if (process.platform === "win32") {
|
|
162
|
+
const base = path.basename(full).split(".")[0].trim().toUpperCase()
|
|
163
|
+
if (WIN_RESERVED.has(base)) {
|
|
164
|
+
throw new Error(`${p} names a reserved device, which is not a file inside ${this.baseDir}`)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const canonBase = canonicalPath(this.baseDir)
|
|
168
|
+
const canonTarget = canonicalPath(full)
|
|
169
|
+
const rel = path.relative(canonBase, canonTarget)
|
|
170
|
+
if (rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
|
|
171
|
+
throw new Error(`${p} resolves outside baseDir ${this.baseDir}`)
|
|
172
|
+
}
|
|
173
|
+
return full
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Resolve a path for comparison. Symlinks — and, on Windows, directory
|
|
179
|
+
* junctions and 8.3 short names — are resolved on the DEEPEST EXISTING
|
|
180
|
+
* ancestor and the remaining segments re-attached, because a file `write` is
|
|
181
|
+
* about to create has no real path, and a check that only works on existing
|
|
182
|
+
* files is not a check for `write`.
|
|
183
|
+
*/
|
|
184
|
+
function canonicalPath(p: string): string {
|
|
185
|
+
const abs = path.resolve(p)
|
|
186
|
+
let cur = abs
|
|
187
|
+
let tail = ""
|
|
188
|
+
for (;;) {
|
|
189
|
+
try {
|
|
190
|
+
const resolved = realpathSync.native(cur)
|
|
191
|
+
return tail ? path.join(resolved, tail) : resolved
|
|
192
|
+
} catch {
|
|
193
|
+
const parent = path.dirname(cur)
|
|
194
|
+
if (parent === cur) return abs
|
|
195
|
+
tail = tail ? path.join(path.basename(cur), tail) : path.basename(cur)
|
|
196
|
+
cur = parent
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
23
200
|
|
|
24
201
|
/**
|
|
25
202
|
* Whether the builtin source is on. Default ON. Same precedence as MCP:
|
|
@@ -42,9 +219,13 @@ export function builtinsEnabled(cfg: BuiltinsConfig | undefined): boolean {
|
|
|
42
219
|
export function selectBuiltins(cfg: BuiltinsConfig | undefined): Tool[] {
|
|
43
220
|
if (!builtinsEnabled(cfg)) return []
|
|
44
221
|
const map = typeof cfg === "object" ? cfg.tools : undefined
|
|
45
|
-
const all = createBuiltinTools()
|
|
46
|
-
|
|
47
|
-
|
|
222
|
+
const all = createBuiltinTools(cfg)
|
|
223
|
+
const selected = map ? all.filter((t) => map[t.name] !== false) : all
|
|
224
|
+
// A missing interpreter is a construction-time failure, and only when `bash`
|
|
225
|
+
// survived the toggles: a host that disabled it should run fine on a box with
|
|
226
|
+
// no shell at all (ADR 0034 D1).
|
|
227
|
+
if (selected.some((t) => t.name === "bash")) builtinShell(cfg)
|
|
228
|
+
return selected
|
|
48
229
|
}
|
|
49
230
|
|
|
50
231
|
const err = (output: string, metadata?: Record<string, unknown>): ToolResult => ({ output, isError: true, metadata })
|
|
@@ -139,7 +320,7 @@ function walkFiles(root: string): string[] {
|
|
|
139
320
|
// individual tools
|
|
140
321
|
// ---------------------------------------------------------------------------
|
|
141
322
|
|
|
142
|
-
function bashTool(): Tool {
|
|
323
|
+
function bashTool(env: BuiltinEnv): Tool {
|
|
143
324
|
return builtin(
|
|
144
325
|
"bash",
|
|
145
326
|
"Run a shell command and return its combined stdout+stderr. Non-zero exit is an error.",
|
|
@@ -154,38 +335,134 @@ function bashTool(): Tool {
|
|
|
154
335
|
required: ["command"],
|
|
155
336
|
additionalProperties: false,
|
|
156
337
|
},
|
|
157
|
-
(args) =>
|
|
338
|
+
(args, ctx) =>
|
|
158
339
|
new Promise<ToolResult>((resolve) => {
|
|
159
340
|
const command = String(args.command ?? "")
|
|
160
341
|
if (!command) return resolve(err("bash: command is required"))
|
|
161
|
-
|
|
342
|
+
let argv: string[]
|
|
343
|
+
let workdir: string
|
|
344
|
+
try {
|
|
345
|
+
argv = env.shellArgv()
|
|
346
|
+
workdir = args.workdir ? env.resolvePath(String(args.workdir)) : env.dir()
|
|
347
|
+
} catch (e) {
|
|
348
|
+
return resolve(err(`bash: ${(e as Error).message}`))
|
|
349
|
+
}
|
|
162
350
|
const timeout = typeof args.timeout === "number" ? args.timeout : 60_000
|
|
163
|
-
const
|
|
351
|
+
const meta: Record<string, unknown> = { shell: env.shellLabel }
|
|
352
|
+
|
|
353
|
+
// NOT `shell: true`: on Windows that silently means cmd.exe while every
|
|
354
|
+
// other port fails loudly, and it hides WHICH interpreter ran. `detached`
|
|
355
|
+
// puts the child in its own process group so a kill reaches the command
|
|
356
|
+
// AND everything it started — without it the kill reaches the
|
|
357
|
+
// interpreter only and the real work runs on, reparented (ADR 0034 D4).
|
|
358
|
+
const child = spawn(argv[0], [...argv.slice(1), command], {
|
|
359
|
+
cwd: workdir,
|
|
360
|
+
detached: process.platform !== "win32",
|
|
361
|
+
})
|
|
164
362
|
let out = ""
|
|
165
|
-
let
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
363
|
+
let stopped: "" | "timeout" | "cancelled" = ""
|
|
364
|
+
let killedTree = false
|
|
365
|
+
let settled = false
|
|
366
|
+
|
|
367
|
+
// Captured once, at spawn: `detached` makes the child a process-group
|
|
368
|
+
// LEADER, so its pid is the pgid. Reading it later from `child` would
|
|
369
|
+
// risk signalling by a pid the runtime has already let go of.
|
|
370
|
+
const pgid = child.pid
|
|
371
|
+
|
|
372
|
+
const killJob = (graceful: boolean): boolean => {
|
|
373
|
+
// Nothing may be signalled once this call has settled: a stale timer
|
|
374
|
+
// firing after the fact would aim at a pid the OS may have reissued,
|
|
375
|
+
// and a NEGATED pid is a whole process group.
|
|
376
|
+
if (settled) return false
|
|
377
|
+
if (typeof pgid !== "number" || !Number.isInteger(pgid) || pgid <= 1) return false
|
|
378
|
+
try {
|
|
379
|
+
if (process.platform === "win32") {
|
|
380
|
+
// Windows has NO graceful termination for a console process, and
|
|
381
|
+
// asking anyway is worse than not asking: measured on a native
|
|
382
|
+
// Windows box, `taskkill /T` without `/F` refuses every console
|
|
383
|
+
// process in the tree ("can only be terminated forcefully") while
|
|
384
|
+
// still being able to take the parent down — which reparents the
|
|
385
|
+
// grandchild and leaves it running. So the job is terminated at
|
|
386
|
+
// once there, and the grace window is a POSIX effect (ADR 0034 D4).
|
|
387
|
+
const r = spawnSync("taskkill", ["/T", "/F", "/PID", String(pgid)])
|
|
388
|
+
return r.status === 0
|
|
389
|
+
}
|
|
390
|
+
// Never signal a REAPED child by negated pid: that pid may belong to
|
|
391
|
+
// something else by now, and the blast radius is a whole process
|
|
392
|
+
// GROUP. (On Windows above, taskkill targets one pid and a dead pid
|
|
393
|
+
// is merely an error, so the same guard is not needed there — and
|
|
394
|
+
// must not be applied, because the tree can outlive the child.)
|
|
395
|
+
if (child.exitCode !== null || child.signalCode !== null) return false
|
|
396
|
+
process.kill(-pgid, graceful ? "SIGTERM" : "SIGKILL")
|
|
397
|
+
return true
|
|
398
|
+
} catch {
|
|
399
|
+
return false
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Ask, wait out the grace window, then insist. A runner that gets
|
|
404
|
+
// SIGTERM removes its temp directories; one that gets SIGKILL does not.
|
|
405
|
+
const stop = (why: "timeout" | "cancelled") => {
|
|
406
|
+
if (stopped) return
|
|
407
|
+
stopped = why
|
|
408
|
+
killedTree = killJob(true)
|
|
409
|
+
graceTimer = setTimeout(() => {
|
|
410
|
+
if (killJob(false)) killedTree = true
|
|
411
|
+
}, KILL_GRACE_MS)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
let graceTimer: NodeJS.Timeout | undefined
|
|
415
|
+
const timer = setTimeout(() => stop("timeout"), timeout)
|
|
416
|
+
const signal = ctx?.signal
|
|
417
|
+
const onAbort = () => stop("cancelled")
|
|
418
|
+
signal?.addEventListener?.("abort", onAbort, { once: true })
|
|
419
|
+
|
|
420
|
+
const finish = (result: ToolResult) => {
|
|
421
|
+
if (settled) return
|
|
422
|
+
settled = true
|
|
423
|
+
clearTimeout(timer)
|
|
424
|
+
if (graceTimer) clearTimeout(graceTimer)
|
|
425
|
+
signal?.removeEventListener?.("abort", onAbort)
|
|
426
|
+
resolve(result)
|
|
427
|
+
}
|
|
428
|
+
|
|
170
429
|
child.stdout?.on("data", (d) => (out += d.toString()))
|
|
171
430
|
child.stderr?.on("data", (d) => (out += d.toString()))
|
|
172
|
-
child.on("error", (e) => {
|
|
173
|
-
clearTimeout(timer)
|
|
174
|
-
resolve(err(`bash: ${e.message}`))
|
|
175
|
-
})
|
|
431
|
+
child.on("error", (e) => finish(err(`bash: ${e.message}`, meta)))
|
|
176
432
|
child.on("close", (code) => {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
433
|
+
if (stopped) {
|
|
434
|
+
// WINDOWS ONLY, and the platform guard is the point. There, the
|
|
435
|
+
// direct child closing does not mean the job is gone — a detached
|
|
436
|
+
// grandchild outlives it — so the tree is terminated here by pid.
|
|
437
|
+
//
|
|
438
|
+
// On POSIX this must NOT run. `close` only fires once the stdio the
|
|
439
|
+
// grandchild inherited has closed, so the job is already gone; and
|
|
440
|
+
// signalling `-pid` after the child has been reaped aims at whatever
|
|
441
|
+
// process now owns that pid. PIDs recycle fast on a busy CI box, and
|
|
442
|
+
// the blast radius of a negated pid is a whole process GROUP — which
|
|
443
|
+
// is how this killed sibling test workers on the GitHub runner while
|
|
444
|
+
// passing on macOS, in a container, and on its own.
|
|
445
|
+
if (!killedTree && process.platform === "win32") killedTree = killJob(false)
|
|
446
|
+
meta.timedOut = stopped === "timeout"
|
|
447
|
+
meta.killedTree = killedTree
|
|
448
|
+
return finish(
|
|
449
|
+
err(
|
|
450
|
+
stopped === "timeout"
|
|
451
|
+
? `bash: command timed out after ${timeout}ms\n${out}`
|
|
452
|
+
: `bash: command cancelled\n${out}`,
|
|
453
|
+
meta,
|
|
454
|
+
),
|
|
455
|
+
)
|
|
181
456
|
}
|
|
182
|
-
|
|
457
|
+
meta.exitCode = code
|
|
458
|
+
if (code !== 0) return finish(err(`${out}\nbash: command exited with code ${code}`, meta))
|
|
459
|
+
finish(ok(out, meta))
|
|
183
460
|
})
|
|
184
461
|
}),
|
|
185
462
|
)
|
|
186
463
|
}
|
|
187
464
|
|
|
188
|
-
function readTool(): Tool {
|
|
465
|
+
function readTool(env: BuiltinEnv): Tool {
|
|
189
466
|
return builtin(
|
|
190
467
|
"read",
|
|
191
468
|
"Read a file. Recognised media (png/jpg/jpeg/gif/webp/pdf/mp3/wav) comes back as a content part; anything else is read as UTF-8 text, with offset/limit returning that line window.",
|
|
@@ -203,14 +480,16 @@ function readTool(): Tool {
|
|
|
203
480
|
const p = String(args.path ?? "")
|
|
204
481
|
if (!p) return err("read: path is required")
|
|
205
482
|
let bytes: Buffer
|
|
483
|
+
let full: string
|
|
206
484
|
try {
|
|
207
|
-
|
|
485
|
+
full = env.resolvePath(p)
|
|
486
|
+
bytes = await fs.readFile(full)
|
|
208
487
|
} catch (e) {
|
|
209
488
|
return err(`read: ${e instanceof Error ? e.message : String(e)}`)
|
|
210
489
|
}
|
|
211
490
|
// §6: a recognised media extension comes back as a part. The table is fixed — no magic-byte
|
|
212
491
|
// sniffing and no platform mime database, whose contents vary per machine.
|
|
213
|
-
const media = mediaTypeFor(
|
|
492
|
+
const media = mediaTypeFor(full)
|
|
214
493
|
if (media) {
|
|
215
494
|
return {
|
|
216
495
|
output: `${p} (${media.mimeType}, ${bytes.length} bytes)`,
|
|
@@ -237,7 +516,7 @@ function readTool(): Tool {
|
|
|
237
516
|
)
|
|
238
517
|
}
|
|
239
518
|
|
|
240
|
-
function writeTool(): Tool {
|
|
519
|
+
function writeTool(env: BuiltinEnv): Tool {
|
|
241
520
|
return builtin(
|
|
242
521
|
"write",
|
|
243
522
|
"Write content to a file (create/overwrite), creating parent directories.",
|
|
@@ -254,15 +533,21 @@ function writeTool(): Tool {
|
|
|
254
533
|
const p = String(args.path ?? "")
|
|
255
534
|
if (!p) return err("write: path is required")
|
|
256
535
|
const content = typeof args.content === "string" ? args.content : String(args.content ?? "")
|
|
257
|
-
|
|
258
|
-
|
|
536
|
+
let full: string
|
|
537
|
+
try {
|
|
538
|
+
full = env.resolvePath(p)
|
|
539
|
+
} catch (e) {
|
|
540
|
+
return err(`write: ${e instanceof Error ? e.message : String(e)}`)
|
|
541
|
+
}
|
|
542
|
+
await fs.mkdir(path.dirname(path.resolve(full)), { recursive: true })
|
|
543
|
+
await fs.writeFile(full, content, "utf8")
|
|
259
544
|
const bytes = Buffer.byteLength(content, "utf8")
|
|
260
545
|
return ok(`Wrote ${bytes} bytes to ${p}`, { bytes })
|
|
261
546
|
},
|
|
262
547
|
)
|
|
263
548
|
}
|
|
264
549
|
|
|
265
|
-
function editTool(): Tool {
|
|
550
|
+
function editTool(env: BuiltinEnv): Tool {
|
|
266
551
|
return builtin(
|
|
267
552
|
"edit",
|
|
268
553
|
"Exact-string replace in a file. Default replaces a single unique occurrence; replaceAll replaces all.",
|
|
@@ -286,8 +571,10 @@ function editTool(): Tool {
|
|
|
286
571
|
const oldString = args.oldString
|
|
287
572
|
const newString = typeof args.newString === "string" ? args.newString : String(args.newString ?? "")
|
|
288
573
|
let content: string
|
|
574
|
+
let full: string
|
|
289
575
|
try {
|
|
290
|
-
|
|
576
|
+
full = env.resolvePath(p)
|
|
577
|
+
content = await fs.readFile(full, "utf8")
|
|
291
578
|
} catch (e) {
|
|
292
579
|
return err(`edit: ${e instanceof Error ? e.message : String(e)}`)
|
|
293
580
|
}
|
|
@@ -308,7 +595,7 @@ function editTool(): Tool {
|
|
|
308
595
|
)
|
|
309
596
|
}
|
|
310
597
|
|
|
311
|
-
function grepTool(): Tool {
|
|
598
|
+
function grepTool(env: BuiltinEnv): Tool {
|
|
312
599
|
return builtin(
|
|
313
600
|
"grep",
|
|
314
601
|
"Search file contents by regex under a directory. Output is file:line:text matches.",
|
|
@@ -332,7 +619,12 @@ function grepTool(): Tool {
|
|
|
332
619
|
} catch (e) {
|
|
333
620
|
return err(`grep: invalid regex: ${e instanceof Error ? e.message : String(e)}`)
|
|
334
621
|
}
|
|
335
|
-
|
|
622
|
+
let root: string
|
|
623
|
+
try {
|
|
624
|
+
root = args.path ? env.resolvePath(String(args.path)) : env.dir()
|
|
625
|
+
} catch (e) {
|
|
626
|
+
return err(`grep: ${e instanceof Error ? e.message : String(e)}`)
|
|
627
|
+
}
|
|
336
628
|
const include = args.include ? String(args.include) : undefined
|
|
337
629
|
const limit = typeof args.limit === "number" ? args.limit : 100
|
|
338
630
|
// Was the worse of the two builtins: capped MID-WALK and never sorted at all, so both the
|
|
@@ -361,7 +653,7 @@ function grepTool(): Tool {
|
|
|
361
653
|
)
|
|
362
654
|
}
|
|
363
655
|
|
|
364
|
-
function globTool(): Tool {
|
|
656
|
+
function globTool(env: BuiltinEnv): Tool {
|
|
365
657
|
return builtin(
|
|
366
658
|
"glob",
|
|
367
659
|
"List files matching a glob under a directory. Output is newline-joined relative paths.",
|
|
@@ -378,7 +670,12 @@ function globTool(): Tool {
|
|
|
378
670
|
async (args) => {
|
|
379
671
|
const pattern = String(args.pattern ?? "")
|
|
380
672
|
if (!pattern) return err("glob: pattern is required")
|
|
381
|
-
|
|
673
|
+
let root: string
|
|
674
|
+
try {
|
|
675
|
+
root = args.path ? env.resolvePath(String(args.path)) : env.dir()
|
|
676
|
+
} catch (e) {
|
|
677
|
+
return err(`glob: ${e instanceof Error ? e.message : String(e)}`)
|
|
678
|
+
}
|
|
382
679
|
const limit = typeof args.limit === "number" ? args.limit : 100
|
|
383
680
|
// Collect ALL matches, order them, THEN cap (A24/A25): breaking the walk at the cap let
|
|
384
681
|
// the filesystem decide WHICH files the model sees, not merely their order. The emitted
|
|
@@ -617,7 +914,7 @@ function applyUpdate(content: string, body: string[]): string {
|
|
|
617
914
|
return result
|
|
618
915
|
}
|
|
619
916
|
|
|
620
|
-
function applyPatchTool(): Tool {
|
|
917
|
+
function applyPatchTool(env: BuiltinEnv): Tool {
|
|
621
918
|
return builtin(
|
|
622
919
|
"apply_patch",
|
|
623
920
|
"Apply a patch (Begin/End Patch grammar: Add/Update/Delete File). Atomic — a non-matching hunk aborts with no writes.",
|
|
@@ -638,6 +935,14 @@ function applyPatchTool(): Tool {
|
|
|
638
935
|
} catch (e) {
|
|
639
936
|
return err(`apply_patch: ${e instanceof Error ? e.message : String(e)}`)
|
|
640
937
|
}
|
|
938
|
+
// The paths live INSIDE the patch text, not in the arguments, so a host
|
|
939
|
+
// cannot rewrite them from a hook — this is the one case that genuinely
|
|
940
|
+
// needs the base directory to be library-side (ADR 0034 D2).
|
|
941
|
+
try {
|
|
942
|
+
for (const op of ops) op.path = env.resolvePath(op.path)
|
|
943
|
+
} catch (e) {
|
|
944
|
+
return err(`apply_patch: ${e instanceof Error ? e.message : String(e)}`)
|
|
945
|
+
}
|
|
641
946
|
// Stage every write/delete first; only touch the filesystem once all hunks apply.
|
|
642
947
|
const writes: Array<{ path: string; content: string }> = []
|
|
643
948
|
const deletes: string[] = []
|
|
@@ -678,17 +983,27 @@ function applyPatchTool(): Tool {
|
|
|
678
983
|
* for parity: bash, read, write, edit, grep, glob, webfetch, question,
|
|
679
984
|
* apply_patch, todowrite.
|
|
680
985
|
*/
|
|
681
|
-
export function createBuiltinTools(): Tool[] {
|
|
986
|
+
export function createBuiltinTools(cfg?: BuiltinsConfig): Tool[] {
|
|
987
|
+
const env = new BuiltinEnv(cfg)
|
|
682
988
|
return [
|
|
683
|
-
bashTool(),
|
|
684
|
-
readTool(),
|
|
685
|
-
writeTool(),
|
|
686
|
-
editTool(),
|
|
687
|
-
grepTool(),
|
|
688
|
-
globTool(),
|
|
989
|
+
bashTool(env),
|
|
990
|
+
readTool(env),
|
|
991
|
+
writeTool(env),
|
|
992
|
+
editTool(env),
|
|
993
|
+
grepTool(env),
|
|
994
|
+
globTool(env),
|
|
689
995
|
webfetchTool(),
|
|
690
996
|
questionTool(),
|
|
691
|
-
applyPatchTool(),
|
|
997
|
+
applyPatchTool(env),
|
|
692
998
|
todowriteTool(),
|
|
693
999
|
]
|
|
694
1000
|
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* The interpreter the builtins resolved for `bash` — what a host prints, and
|
|
1004
|
+
* what `metadata.shell` carries on every bash result. Throws the same error
|
|
1005
|
+
* `selectBuiltins` fails construction with when nothing resolves.
|
|
1006
|
+
*/
|
|
1007
|
+
export function builtinShell(cfg?: BuiltinsConfig): string[] {
|
|
1008
|
+
return new BuiltinEnv(cfg).shellArgv()
|
|
1009
|
+
}
|
package/src/classifier.ts
CHANGED
|
@@ -23,6 +23,8 @@ export const DEFAULT_CLASSIFIER_MODEL = "jev-latest"
|
|
|
23
23
|
export const DEFAULT_CLASSIFIER_API_KEY_ENV = "TYPESAFE_API_KEY"
|
|
24
24
|
/** Bounds ONE request; a classifier has no loop to bound. */
|
|
25
25
|
export const DEFAULT_CLASSIFIER_TIMEOUT_MS = 10_000
|
|
26
|
+
/** `evaluateBatch`: at most this many states in flight by default. */
|
|
27
|
+
export const DEFAULT_BATCH_CONCURRENCY = 16
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
30
|
* A named BACKEND: `baseUrl`, `model` and `apiKeyEnv` are only jointly valid, so they travel as
|
|
@@ -123,7 +125,7 @@ export function choiceOver(instructions: string, items: Record<string, string>):
|
|
|
123
125
|
}
|
|
124
126
|
|
|
125
127
|
/** The wire form of one question — the object itself, minus an absent `criteria`. */
|
|
126
|
-
function questionWire(q: Question): Record<string, unknown> {
|
|
128
|
+
export function questionWire(q: Question): Record<string, unknown> {
|
|
127
129
|
switch (q.type) {
|
|
128
130
|
case "noul":
|
|
129
131
|
return q.criteria === undefined
|
|
@@ -564,6 +566,39 @@ export class Classifier {
|
|
|
564
566
|
return decision
|
|
565
567
|
}
|
|
566
568
|
|
|
569
|
+
/**
|
|
570
|
+
* The same questions over many states (SPEC §8B "Batch"). Each state goes through
|
|
571
|
+
* `evaluate`, at most `concurrency` (default 16) in flight; decisions come back in STATE
|
|
572
|
+
* order. Fails closed: the first failing state's index is named and no decisions return.
|
|
573
|
+
* No states is an error and sends nothing.
|
|
574
|
+
*/
|
|
575
|
+
async evaluateBatch(
|
|
576
|
+
states: readonly unknown[],
|
|
577
|
+
questions: Record<string, Question>,
|
|
578
|
+
opts: { signal?: AbortSignal; concurrency?: number } = {},
|
|
579
|
+
): Promise<Decision[]> {
|
|
580
|
+
if (!states || states.length === 0) throw new Error("classifier: no states to evaluate")
|
|
581
|
+
const limit = Math.max(1, opts.concurrency ?? DEFAULT_BATCH_CONCURRENCY)
|
|
582
|
+
const out: Decision[] = new Array(states.length)
|
|
583
|
+
const errs: unknown[] = new Array(states.length)
|
|
584
|
+
let next = 0
|
|
585
|
+
const worker = async () => {
|
|
586
|
+
while (next < states.length) {
|
|
587
|
+
const i = next++
|
|
588
|
+
try {
|
|
589
|
+
out[i] = await this.evaluate(states[i], questions, opts.signal)
|
|
590
|
+
} catch (e) {
|
|
591
|
+
errs[i] = e
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
await Promise.all(Array.from({ length: Math.min(limit, states.length) }, worker))
|
|
596
|
+
for (let i = 0; i < states.length; i++) {
|
|
597
|
+
if (i in errs) throw new Error(`classifier: state ${i}: ${errText(errs[i])}`, { cause: errs[i] })
|
|
598
|
+
}
|
|
599
|
+
return out
|
|
600
|
+
}
|
|
601
|
+
|
|
567
602
|
// ---------------------------------------------------------------- degenerate
|
|
568
603
|
|
|
569
604
|
/**
|
|
@@ -650,8 +685,10 @@ export class Classifier {
|
|
|
650
685
|
let lastErr: unknown
|
|
651
686
|
for (let attempt = 0; ; attempt++) {
|
|
652
687
|
const timer = new AbortController()
|
|
688
|
+
// NOT unref'd: this watchdog is the only thing that will end a request an
|
|
689
|
+
// injected `fetch` never settles, and an unref'd timer cannot keep the
|
|
690
|
+
// loop alive long enough to fire. It is cleared on every exit path below.
|
|
653
691
|
const t = setTimeout(() => timer.abort(new Error(`classifier: request timeout after ${this.timeoutMs}ms`)), this.timeoutMs)
|
|
654
|
-
;(t as any).unref?.()
|
|
655
692
|
const onOuter = () => timer.abort(signal!.reason)
|
|
656
693
|
if (signal) {
|
|
657
694
|
if (signal.aborted) {
|