@namzu/sdk 35.0.0 → 36.0.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/CHANGELOG.md +38 -0
- package/dist/provider/token-budget.d.ts.map +1 -1
- package/dist/provider/token-budget.js +56 -8
- package/dist/provider/token-budget.js.map +1 -1
- package/dist/public-runtime.d.ts +2 -2
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +1 -1
- package/dist/public-runtime.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +1 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +50 -10
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts +7 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +1 -0
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts +3 -3
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +31 -6
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/context.d.ts +2 -0
- package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/context.js.map +1 -1
- package/dist/runtime/query/iteration/stream-turn.d.ts.map +1 -1
- package/dist/runtime/query/iteration/stream-turn.js +10 -5
- package/dist/runtime/query/iteration/stream-turn.js.map +1 -1
- package/dist/runtime/query/prompt.d.ts.map +1 -1
- package/dist/runtime/query/prompt.js +14 -4
- package/dist/runtime/query/prompt.js.map +1 -1
- package/dist/sandbox/file-walk-program.d.ts +3 -0
- package/dist/sandbox/file-walk-program.d.ts.map +1 -0
- package/dist/sandbox/file-walk-program.js +94 -0
- package/dist/sandbox/file-walk-program.js.map +1 -0
- package/dist/sandbox/file-walk.d.ts +12 -0
- package/dist/sandbox/file-walk.d.ts.map +1 -0
- package/dist/sandbox/file-walk.js +429 -0
- package/dist/sandbox/file-walk.js.map +1 -0
- package/dist/sandbox/index.d.ts +2 -0
- package/dist/sandbox/index.d.ts.map +1 -1
- package/dist/sandbox/index.js +1 -0
- package/dist/sandbox/index.js.map +1 -1
- package/dist/sandbox/provider/local.d.ts.map +1 -1
- package/dist/sandbox/provider/local.js +9 -0
- package/dist/sandbox/provider/local.js.map +1 -1
- package/dist/scheduler/completion-inbox.d.ts +3 -2
- package/dist/scheduler/completion-inbox.d.ts.map +1 -1
- package/dist/scheduler/completion-inbox.js +29 -15
- package/dist/scheduler/completion-inbox.js.map +1 -1
- package/dist/tools/builtins/bash.d.ts.map +1 -1
- package/dist/tools/builtins/bash.js +215 -59
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/glob.d.ts +2 -1
- package/dist/tools/builtins/glob.d.ts.map +1 -1
- package/dist/tools/builtins/glob.js +95 -103
- package/dist/tools/builtins/glob.js.map +1 -1
- package/dist/tools/builtins/grep.d.ts.map +1 -1
- package/dist/tools/builtins/grep.js +155 -111
- package/dist/tools/builtins/grep.js.map +1 -1
- package/dist/tools/builtins/ls.js +2 -2
- package/dist/tools/builtins/ls.js.map +1 -1
- package/dist/types/sandbox/index.d.ts +20 -0
- package/dist/types/sandbox/index.d.ts.map +1 -1
- package/dist/types/sandbox/index.js.map +1 -1
- package/package.json +6 -1
- package/src/provider/token-budget.ts +52 -8
- package/src/public-runtime.ts +2 -1
- package/src/runtime/query/executor.ts +54 -14
- package/src/runtime/query/index.ts +8 -0
- package/src/runtime/query/iteration/index.ts +29 -9
- package/src/runtime/query/iteration/phases/context.ts +2 -0
- package/src/runtime/query/iteration/stream-turn.ts +9 -5
- package/src/runtime/query/prompt.ts +21 -4
- package/src/sandbox/file-walk-program.ts +93 -0
- package/src/sandbox/file-walk.ts +463 -0
- package/src/sandbox/index.ts +2 -0
- package/src/sandbox/provider/local.ts +13 -0
- package/src/scheduler/completion-inbox.ts +29 -19
- package/src/tools/builtins/bash.ts +230 -58
- package/src/tools/builtins/glob.ts +104 -122
- package/src/tools/builtins/grep.ts +166 -132
- package/src/tools/builtins/ls.ts +2 -2
- package/src/types/sandbox/index.ts +21 -0
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { StringDecoder } from 'node:string_decoder'
|
|
3
3
|
import { z } from 'zod'
|
|
4
|
+
import { SANDBOX_KILL_GRACE_MS } from '../../constants/sandbox/index.js'
|
|
4
5
|
import { DANGEROUS_PATTERNS } from '../../constants/tools/index.js'
|
|
6
|
+
import { killTree } from '../../process/kill-tree.js'
|
|
7
|
+
import { subscribeToAbort } from '../../utils/abort.js'
|
|
5
8
|
import { defineTool } from '../defineTool.js'
|
|
6
9
|
import { scrubInheritedEnv } from '../env-scrub.js'
|
|
7
10
|
|
|
8
|
-
const execAsync = promisify(exec)
|
|
9
11
|
// Namzu owns its own bash timeout knob — `NAMZU_BASH_TIMEOUT_MS`.
|
|
10
12
|
// The Vandal fallback (`VANDAL_NAMZU_TIMEOUT_MS`) lived here as a
|
|
11
13
|
// historical bridge while Namzu was carved out of the Vandal repo,
|
|
@@ -67,25 +69,205 @@ const inputSchema = z.object({
|
|
|
67
69
|
|
|
68
70
|
type BashInput = z.infer<typeof inputSchema>
|
|
69
71
|
|
|
72
|
+
const MAX_SHELL_PROGRESS_CHARS = 160
|
|
73
|
+
|
|
74
|
+
/** Keep a bounded tail even when a process never writes a newline. */
|
|
75
|
+
function appendProgressTail(previous: string, chunk: string, start: number, end: number): string {
|
|
76
|
+
return (previous + chunk.slice(Math.max(start, end - MAX_SHELL_PROGRESS_CHARS), end)).slice(
|
|
77
|
+
-MAX_SHELL_PROGRESS_CHARS,
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Clipping a UTF-16 string must not display half of a surrogate pair. */
|
|
82
|
+
function progressLine(value: string): string {
|
|
83
|
+
let line = value.trim()
|
|
84
|
+
const first = line.charCodeAt(0)
|
|
85
|
+
if (first >= 0xdc00 && first <= 0xdfff) line = line.slice(1)
|
|
86
|
+
const last = line.charCodeAt(line.length - 1)
|
|
87
|
+
if (last >= 0xd800 && last <= 0xdbff) line = line.slice(0, -1)
|
|
88
|
+
return line
|
|
89
|
+
}
|
|
90
|
+
|
|
70
91
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* replaces it as the next arrives, so sending a whole chunk sends a wall
|
|
75
|
-
* of text into a slot that shows one line of it. A chunk usually ends
|
|
76
|
-
* mid-line and usually ends with a newline, so the last NON-EMPTY line is
|
|
77
|
-
* the most recent complete thing the command actually said.
|
|
78
|
-
*
|
|
79
|
-
* Progress is capped rather than truncated with an ellipsis: this is
|
|
80
|
-
* glanced at, and a marker in a line nobody reads to the end is noise.
|
|
92
|
+
* One latest line per stream, independent of transport chunk boundaries.
|
|
93
|
+
* Reports stay synchronous: the executor owns coalescing and backpressure.
|
|
94
|
+
* This projection adds neither an output log nor a queue of pending reports.
|
|
81
95
|
*/
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
96
|
+
function shellProgress(report?: (message: string) => void) {
|
|
97
|
+
if (!report) return undefined
|
|
98
|
+
const partial = { stdout: '', stderr: '' }
|
|
99
|
+
let lastMessage = ''
|
|
100
|
+
return ({ stream, data }: { stream: 'stdout' | 'stderr'; data: string }): void => {
|
|
101
|
+
let line = partial[stream]
|
|
102
|
+
let latest = ''
|
|
103
|
+
let start = 0
|
|
104
|
+
// Scan delimiters without allocating an array for arbitrarily chatty output.
|
|
105
|
+
for (const match of data.matchAll(/[\r\n]/g)) {
|
|
106
|
+
line = appendProgressTail(line, data, start, match.index)
|
|
107
|
+
latest = progressLine(line) || latest
|
|
108
|
+
line = ''
|
|
109
|
+
start = match.index + 1
|
|
110
|
+
}
|
|
111
|
+
line = appendProgressTail(line, data, start, data.length)
|
|
112
|
+
partial[stream] = line
|
|
113
|
+
const message = progressLine(line) || latest
|
|
114
|
+
if (!message || message === lastMessage) return
|
|
115
|
+
lastMessage = message
|
|
116
|
+
try {
|
|
117
|
+
report(message)
|
|
118
|
+
} catch {
|
|
119
|
+
// A diagnostic observer cannot break the process output reader.
|
|
120
|
+
}
|
|
87
121
|
}
|
|
88
|
-
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Keep the shell's process group until inherited pipes close. Node's exec
|
|
126
|
+
* timeout/AbortSignal kills only the wrapper and closes its pipes immediately,
|
|
127
|
+
* leaving the command and its descendants running after the promise settles.
|
|
128
|
+
*/
|
|
129
|
+
function execHostShell(
|
|
130
|
+
command: string,
|
|
131
|
+
options: {
|
|
132
|
+
cwd: string
|
|
133
|
+
env: NodeJS.ProcessEnv
|
|
134
|
+
timeout: number
|
|
135
|
+
maxBuffer: number
|
|
136
|
+
signal?: AbortSignal
|
|
137
|
+
onOutput?: ReturnType<typeof shellProgress>
|
|
138
|
+
},
|
|
139
|
+
): Promise<{ stdout: string; stderr: string }> {
|
|
140
|
+
options.signal?.throwIfAborted()
|
|
141
|
+
return new Promise((resolve, reject) => {
|
|
142
|
+
const child = spawn(command, {
|
|
143
|
+
cwd: options.cwd,
|
|
144
|
+
env: options.env,
|
|
145
|
+
shell: true,
|
|
146
|
+
// killTree's negative PID must never target the caller's own group.
|
|
147
|
+
detached: process.platform !== 'win32',
|
|
148
|
+
})
|
|
149
|
+
const captures = {
|
|
150
|
+
stdout: {
|
|
151
|
+
chunks: [] as Buffer[],
|
|
152
|
+
bytes: 0,
|
|
153
|
+
truncated: false,
|
|
154
|
+
decoder: new StringDecoder('utf8'),
|
|
155
|
+
},
|
|
156
|
+
stderr: {
|
|
157
|
+
chunks: [] as Buffer[],
|
|
158
|
+
bytes: 0,
|
|
159
|
+
truncated: false,
|
|
160
|
+
decoder: new StringDecoder('utf8'),
|
|
161
|
+
},
|
|
162
|
+
}
|
|
163
|
+
let cause: 'caller' | 'timeout' | 'maxBuffer' | undefined
|
|
164
|
+
let failure: (Error & { code?: string | number }) | undefined
|
|
165
|
+
let escalation: ReturnType<typeof setTimeout> | undefined
|
|
166
|
+
let disposeAbort: (() => void) | undefined
|
|
167
|
+
let closed = false
|
|
168
|
+
const cancel = (origin: NonNullable<typeof cause>) => {
|
|
169
|
+
if (closed || cause !== undefined) return
|
|
170
|
+
cause = origin
|
|
171
|
+
escalation = setTimeout(() => {
|
|
172
|
+
escalation = undefined
|
|
173
|
+
killTree(child, 'SIGKILL')
|
|
174
|
+
// A descendant can deliberately start a separate session. It is
|
|
175
|
+
// outside this group, but its inherited pipes must not hold the
|
|
176
|
+
// cancelled call forever. Keep what was captured during grace.
|
|
177
|
+
child.stdin?.destroy()
|
|
178
|
+
child.stdout?.destroy()
|
|
179
|
+
child.stderr?.destroy()
|
|
180
|
+
}, SANDBOX_KILL_GRACE_MS)
|
|
181
|
+
escalation.unref?.()
|
|
182
|
+
killTree(child, 'SIGTERM')
|
|
183
|
+
}
|
|
184
|
+
const capture = (stream: 'stdout' | 'stderr', chunk: Buffer) => {
|
|
185
|
+
const state = captures[stream]
|
|
186
|
+
const kept = Math.min(chunk.length, options.maxBuffer - state.bytes)
|
|
187
|
+
if (kept > 0) {
|
|
188
|
+
state.chunks.push(Buffer.from(chunk.subarray(0, kept)))
|
|
189
|
+
state.bytes += kept
|
|
190
|
+
}
|
|
191
|
+
if (kept < chunk.length) {
|
|
192
|
+
state.truncated = true
|
|
193
|
+
if (cause === undefined) {
|
|
194
|
+
failure = Object.assign(new RangeError(`${stream} maxBuffer length exceeded`), {
|
|
195
|
+
code: 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER',
|
|
196
|
+
})
|
|
197
|
+
cancel('maxBuffer')
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (options.onOutput) {
|
|
201
|
+
const data = state.decoder.write(chunk)
|
|
202
|
+
if (data) options.onOutput({ stream, data })
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const onStdout = (chunk: Buffer) => capture('stdout', chunk)
|
|
206
|
+
const onStderr = (chunk: Buffer) => capture('stderr', chunk)
|
|
207
|
+
child.stdout?.on('data', onStdout)
|
|
208
|
+
child.stderr?.on('data', onStderr)
|
|
209
|
+
child.once('error', (error: NodeJS.ErrnoException) => {
|
|
210
|
+
failure ??= error
|
|
211
|
+
})
|
|
212
|
+
child.once('close', (code, signal) => {
|
|
213
|
+
closed = true
|
|
214
|
+
disposeAbort?.()
|
|
215
|
+
clearTimeout(deadline)
|
|
216
|
+
if (escalation !== undefined) {
|
|
217
|
+
clearTimeout(escalation)
|
|
218
|
+
// Pipes closing can precede a descendant which closed its copies.
|
|
219
|
+
// Force the owned group now instead of retaining its PID in a timer.
|
|
220
|
+
killTree(child, 'SIGKILL')
|
|
221
|
+
}
|
|
222
|
+
child.stdout?.off('data', onStdout)
|
|
223
|
+
child.stderr?.off('data', onStderr)
|
|
224
|
+
for (const stream of ['stdout', 'stderr'] as const) {
|
|
225
|
+
const data = captures[stream].decoder.end()
|
|
226
|
+
if (data) options.onOutput?.({ stream, data })
|
|
227
|
+
}
|
|
228
|
+
const decodeCapture = (stream: 'stdout' | 'stderr') => {
|
|
229
|
+
const state = captures[stream]
|
|
230
|
+
const bytes = Buffer.concat(state.chunks, state.bytes)
|
|
231
|
+
// Do not flush an incomplete UTF-8 character cut by our byte cap.
|
|
232
|
+
return state.truncated ? new StringDecoder('utf8').write(bytes) : bytes.toString('utf8')
|
|
233
|
+
}
|
|
234
|
+
const stdout = decodeCapture('stdout')
|
|
235
|
+
const stderr = decodeCapture('stderr')
|
|
236
|
+
if (!failure && cause === undefined && code === 0 && signal === null) {
|
|
237
|
+
resolve({ stdout, stderr })
|
|
238
|
+
return
|
|
239
|
+
}
|
|
240
|
+
const error =
|
|
241
|
+
failure ??
|
|
242
|
+
(cause === 'caller'
|
|
243
|
+
? Object.assign(
|
|
244
|
+
new Error('The operation was aborted', { cause: options.signal?.reason }),
|
|
245
|
+
{
|
|
246
|
+
name: 'AbortError',
|
|
247
|
+
code: 'ABORT_ERR',
|
|
248
|
+
},
|
|
249
|
+
)
|
|
250
|
+
: new Error(`Command failed: ${command}\n${stderr}`))
|
|
251
|
+
reject(
|
|
252
|
+
Object.assign(error, {
|
|
253
|
+
stdout,
|
|
254
|
+
stderr,
|
|
255
|
+
stdoutTruncated: captures.stdout.truncated,
|
|
256
|
+
stderrTruncated: captures.stderr.truncated,
|
|
257
|
+
code: failure?.code ?? (cause === 'caller' ? 'ABORT_ERR' : code),
|
|
258
|
+
killed: cause !== undefined,
|
|
259
|
+
signal,
|
|
260
|
+
timedOut: cause === 'timeout',
|
|
261
|
+
}),
|
|
262
|
+
)
|
|
263
|
+
})
|
|
264
|
+
const deadline = setTimeout(() => cancel('timeout'), options.timeout)
|
|
265
|
+
deadline.unref?.()
|
|
266
|
+
if (options.signal) {
|
|
267
|
+
disposeAbort = subscribeToAbort(options.signal, () => cancel('caller'))
|
|
268
|
+
if (options.signal.aborted) cancel('caller')
|
|
269
|
+
}
|
|
270
|
+
})
|
|
89
271
|
}
|
|
90
272
|
|
|
91
273
|
function isDangerousCommand(command: string): boolean {
|
|
@@ -192,6 +374,7 @@ export const BashTool = defineTool({
|
|
|
192
374
|
// sandbox can be added later as an explicit
|
|
193
375
|
// `SandboxExecOptions.workspaceRelativeCwd` field; the bash
|
|
194
376
|
// builtin doesn't have that requirement today.
|
|
377
|
+
const onOutput = shellProgress(context.report)
|
|
195
378
|
if (context.sandbox) {
|
|
196
379
|
const result = await context.sandbox.exec('/bin/sh', ['-c', input.command], {
|
|
197
380
|
timeout: input.timeout,
|
|
@@ -206,22 +389,9 @@ export const BashTool = defineTool({
|
|
|
206
389
|
// durable transcript — so this is a progress signal, not a
|
|
207
390
|
// second copy of the output. `result.stdout` remains the
|
|
208
391
|
// answer the model is given.
|
|
209
|
-
onOutput
|
|
210
|
-
? ({ data }) => {
|
|
211
|
-
const line = lastNonEmptyLine(data)
|
|
212
|
-
if (line) context.report?.(line)
|
|
213
|
-
}
|
|
214
|
-
: undefined,
|
|
392
|
+
onOutput,
|
|
215
393
|
})
|
|
216
394
|
|
|
217
|
-
if (result.timedOut) {
|
|
218
|
-
return {
|
|
219
|
-
success: false,
|
|
220
|
-
output: '',
|
|
221
|
-
error: `Command timed out after ${input.timeout}ms`,
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
395
|
// The sandbox reports when IT clipped a stream. Dropping those
|
|
226
396
|
// flags meant the model saw a complete-looking result that had
|
|
227
397
|
// silently lost its tail — and the kernel's own convention is
|
|
@@ -235,41 +405,32 @@ export const BashTool = defineTool({
|
|
|
235
405
|
result.stdout ? `STDOUT:\n${result.stdout}` : '',
|
|
236
406
|
result.stderr ? `STDERR:\n${result.stderr}` : '',
|
|
237
407
|
clipped.length > 0
|
|
238
|
-
? `[${clipped.join(' and ')} was truncated by the sandbox output cap
|
|
408
|
+
? `[${clipped.join(' and ')} was truncated by the sandbox output cap. The omitted output is unavailable in this result. Use a saved artifact or a read-only observation; do not repeat a state-changing action to recover its output.]`
|
|
239
409
|
: '',
|
|
240
410
|
]
|
|
241
411
|
.filter(Boolean)
|
|
242
412
|
.join('\n\n')
|
|
243
413
|
|
|
244
414
|
return {
|
|
245
|
-
success: result.exitCode === 0,
|
|
415
|
+
success: !result.timedOut && result.exitCode === 0,
|
|
246
416
|
output: output || '(no output)',
|
|
247
417
|
data: {
|
|
248
418
|
exitCode: result.exitCode,
|
|
249
419
|
sandboxed: true,
|
|
420
|
+
timedOut: result.timedOut,
|
|
250
421
|
stdoutTruncated: result.stdoutTruncated ?? false,
|
|
251
422
|
stderrTruncated: result.stderrTruncated ?? false,
|
|
252
423
|
},
|
|
253
|
-
error: result.
|
|
424
|
+
error: result.timedOut
|
|
425
|
+
? `Command timed out after ${input.timeout}ms. Any captured output before the deadline is above.`
|
|
426
|
+
: result.exitCode !== 0
|
|
427
|
+
? `Command exited with code ${result.exitCode}`
|
|
428
|
+
: undefined,
|
|
254
429
|
}
|
|
255
430
|
}
|
|
256
431
|
|
|
257
|
-
//
|
|
258
|
-
// a
|
|
259
|
-
// and the executor's deadline could only ever DETACH from the tool
|
|
260
|
-
// rather than end the work it started.
|
|
261
|
-
// `exec` REJECTS on a non-zero exit, on its own timeout, and on a
|
|
262
|
-
// kill — and the rejection carries `stdout`, `stderr`, `code` and
|
|
263
|
-
// `killed`. Letting it propagate threw all of that away: the registry
|
|
264
|
-
// turned the throw into a structured failure, so the model was told a
|
|
265
|
-
// command failed and not one word about how.
|
|
266
|
-
//
|
|
267
|
-
// That is the common case, not an edge one. A failing test run and a
|
|
268
|
-
// failing build are the two things an agent runs bash for most, and
|
|
269
|
-
// both exit non-zero WITH the output that explains why. The sandbox
|
|
270
|
-
// branch above already reports all of it; this branch did not, so the
|
|
271
|
-
// same command told the model two different amounts depending on where
|
|
272
|
-
// it happened to run.
|
|
432
|
+
// The owned runner retains stdout/stderr on non-zero exit and timeout,
|
|
433
|
+
// so a failed command still returns the evidence explaining its failure.
|
|
273
434
|
// The inherited half is scrubbed; `context.env` is not. Inheritance is
|
|
274
435
|
// implicit — nobody decided this command should see `process.env` — while
|
|
275
436
|
// a `context.env` key is one a host wrote on purpose. See
|
|
@@ -278,14 +439,14 @@ export const BashTool = defineTool({
|
|
|
278
439
|
const inherited = scrubInheritedEnv()
|
|
279
440
|
|
|
280
441
|
try {
|
|
281
|
-
const { stdout, stderr } = await
|
|
442
|
+
const { stdout, stderr } = await execHostShell(input.command, {
|
|
282
443
|
cwd: context.workingDirectory,
|
|
283
444
|
timeout: input.timeout,
|
|
284
445
|
env: { ...inherited.env, ...context.env },
|
|
285
446
|
maxBuffer: DEFAULT_BASH_MAX_BUFFER_BYTES,
|
|
286
447
|
signal: context.abortSignal,
|
|
448
|
+
onOutput,
|
|
287
449
|
})
|
|
288
|
-
|
|
289
450
|
return {
|
|
290
451
|
success: true,
|
|
291
452
|
output: formatShellOutput(stdout, stderr) || '(no output)',
|
|
@@ -295,25 +456,34 @@ export const BashTool = defineTool({
|
|
|
295
456
|
const failure = err as NodeJS.ErrnoException & {
|
|
296
457
|
stdout?: string
|
|
297
458
|
stderr?: string
|
|
459
|
+
stdoutTruncated?: boolean
|
|
460
|
+
stderrTruncated?: boolean
|
|
298
461
|
code?: number | string
|
|
299
462
|
killed?: boolean
|
|
300
463
|
signal?: string
|
|
464
|
+
timedOut?: boolean
|
|
301
465
|
}
|
|
302
466
|
|
|
303
467
|
// A caller-owned Stop is the caller's, not a command failure.
|
|
304
468
|
if (context.abortSignal?.aborted) throw err
|
|
305
469
|
|
|
306
|
-
//
|
|
307
|
-
//
|
|
308
|
-
|
|
309
|
-
const timedOut = failure.killed === true && failure.signal === 'SIGTERM'
|
|
470
|
+
// A maxBuffer stop or an ordinary signal is not a deadline. The
|
|
471
|
+
// runner latches the first cancellation cause before signalling.
|
|
472
|
+
const timedOut = failure.timedOut === true
|
|
310
473
|
const exitCode = typeof failure.code === 'number' ? failure.code : undefined
|
|
474
|
+
const clipped = [
|
|
475
|
+
failure.stdoutTruncated ? 'stdout' : '',
|
|
476
|
+
failure.stderrTruncated ? 'stderr' : '',
|
|
477
|
+
].filter(Boolean)
|
|
311
478
|
// Only on the failure path. A successful command did not need to know,
|
|
312
479
|
// and appending this to every result would make the common case noisy
|
|
313
480
|
// to buy nothing. A failing one is exactly where "authentication
|
|
314
481
|
// failed" has to be distinguishable from "the variable was withheld".
|
|
315
482
|
const output = [
|
|
316
483
|
formatShellOutput(failure.stdout, failure.stderr),
|
|
484
|
+
clipped.length > 0
|
|
485
|
+
? `[${clipped.join(' and ')} ${clipped.length === 1 ? 'was' : 'were'} truncated by the host output cap. The omitted output is unavailable in this result.]`
|
|
486
|
+
: '',
|
|
317
487
|
describeWithheldEnv(inherited.dropped),
|
|
318
488
|
]
|
|
319
489
|
.filter(Boolean)
|
|
@@ -325,6 +495,8 @@ export const BashTool = defineTool({
|
|
|
325
495
|
data: {
|
|
326
496
|
...(exitCode !== undefined ? { exitCode } : {}),
|
|
327
497
|
timedOut,
|
|
498
|
+
stdoutTruncated: failure.stdoutTruncated ?? false,
|
|
499
|
+
stderrTruncated: failure.stderrTruncated ?? false,
|
|
328
500
|
...(failure.signal ? { signal: failure.signal } : {}),
|
|
329
501
|
},
|
|
330
502
|
error: timedOut
|
|
@@ -1,157 +1,139 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { basename, dirname, relative, resolve, sep } from 'node:path'
|
|
1
|
+
import { isAbsolute, join, relative, sep } from 'node:path'
|
|
3
2
|
import { z } from 'zod'
|
|
4
|
-
import
|
|
5
|
-
import type {
|
|
3
|
+
import { walkFilesLocally } from '../../sandbox/file-walk.js'
|
|
4
|
+
import type { SandboxWalkFilesOptions } from '../../types/sandbox/index.js'
|
|
6
5
|
import { defineTool } from '../defineTool.js'
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { joinPosix, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
6
|
+
import { resolveWithin, resolveWithinAny, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
7
|
+
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
10
8
|
|
|
11
|
-
/**
|
|
12
|
-
* Cap on results, shared by both paths so a sandboxed search and a host
|
|
13
|
-
* search truncate at the same point.
|
|
14
|
-
*/
|
|
15
9
|
const MAX_GLOB_RESULTS = 500
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Enumerate inside the sandbox and match there.
|
|
19
|
-
*
|
|
20
|
-
* This tool read the HOST filesystem through `node:fs` and referenced
|
|
21
|
-
* `context.sandbox` nowhere, so with a container backend wired in the
|
|
22
|
-
* sandbox was not a read boundary at all. The paths it returned were
|
|
23
|
-
* host-relative too, while `read` resolves what it is given INSIDE the
|
|
24
|
-
* sandbox — so every glob-to-read handoff either failed or opened a
|
|
25
|
-
* different file. Every sibling builtin already remembers this branch.
|
|
26
|
-
*
|
|
27
|
-
* `listFiles` returns sandbox-relative paths, which is the coordinate
|
|
28
|
-
* system `read` and `grep` already speak, so a path from here can be
|
|
29
|
-
* handed straight to them.
|
|
30
|
-
*/
|
|
31
|
-
async function globInSandbox(
|
|
32
|
-
input: { pattern: string; path?: string },
|
|
33
|
-
sandbox: Sandbox,
|
|
34
|
-
): Promise<ToolResult> {
|
|
35
|
-
// The sandbox's own coordinate system — see `posix-path`.
|
|
36
|
-
const root = resolveWithinPosix(sandbox.rootDir, input.path)
|
|
37
|
-
const entries = await sandbox.listFiles(root)
|
|
38
|
-
|
|
39
|
-
let pattern = input.pattern
|
|
40
|
-
if (!pattern.includes('/') && !pattern.startsWith('**/')) pattern = `**/${pattern}`
|
|
41
|
-
|
|
42
|
-
const matches: string[] = []
|
|
43
|
-
for (const entry of entries) {
|
|
44
|
-
const relPath = relativePosix(sandbox.rootDir, joinPosix(root, entry.path))
|
|
45
|
-
if (!matchesGlob(relPath, pattern)) continue
|
|
46
|
-
matches.push(`./${relPath}`)
|
|
47
|
-
if (matches.length >= MAX_GLOB_RESULTS) break
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (matches.length === 0) {
|
|
51
|
-
return {
|
|
52
|
-
success: true,
|
|
53
|
-
output: `No files found matching pattern "${input.pattern}" in ${root}`,
|
|
54
|
-
data: { count: 0, files: [], sandboxed: true },
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return {
|
|
58
|
-
success: true,
|
|
59
|
-
output: matches.join('\n'),
|
|
60
|
-
data: { count: matches.length, files: matches, sandboxed: true },
|
|
61
|
-
}
|
|
62
|
-
}
|
|
10
|
+
const MAX_VISITED_ENTRIES = 20_000
|
|
63
11
|
|
|
64
12
|
const inputSchema = z.object({
|
|
65
|
-
pattern: z
|
|
13
|
+
pattern: z
|
|
14
|
+
.string()
|
|
15
|
+
.min(1)
|
|
16
|
+
.max(4096)
|
|
17
|
+
.describe(
|
|
18
|
+
'File glob relative to path. "*" lists immediate files; "**/*.ts" explicitly searches subdirectories. Supports braces and character classes. Known file paths can be read directly without discovery.',
|
|
19
|
+
),
|
|
66
20
|
path: z
|
|
67
21
|
.string()
|
|
68
22
|
.optional()
|
|
69
|
-
.describe(
|
|
23
|
+
.describe(
|
|
24
|
+
'Directory to search in. Defaults to the working directory. Choose the narrowest relevant directory.',
|
|
25
|
+
),
|
|
26
|
+
include_hidden: z
|
|
27
|
+
.boolean()
|
|
28
|
+
.optional()
|
|
29
|
+
.describe(
|
|
30
|
+
'Include hidden files and directories in wildcard matches. Default: false. Explicit dotfile patterns still match.',
|
|
31
|
+
),
|
|
70
32
|
})
|
|
71
33
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (!match || match.index === undefined) {
|
|
80
|
-
return { baseDir: dirname(pattern), relativePattern: basename(pattern) }
|
|
34
|
+
/** Keep the pattern in the selected directory's coordinate system. */
|
|
35
|
+
function relativePattern(pattern: string, root: string, sandboxed: boolean): string {
|
|
36
|
+
let result = sandboxed ? pattern : pattern.split(sep).join('/')
|
|
37
|
+
if (result.split('/').includes('..')) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
'Glob pattern escapes the search directory through "..". Choose a contained path and a relative pattern.',
|
|
40
|
+
)
|
|
81
41
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return { baseDir: '', relativePattern: pattern }
|
|
42
|
+
if (sandboxed ? result.startsWith('/') : isAbsolute(pattern)) {
|
|
43
|
+
const contained = sandboxed ? resolveWithinPosix(root, result) : resolveWithin(root, pattern)
|
|
44
|
+
result = sandboxed
|
|
45
|
+
? relativePosix(root, contained)
|
|
46
|
+
: relative(root, contained).split(sep).join('/')
|
|
88
47
|
}
|
|
48
|
+
while (result.startsWith('./')) result = result.slice(2)
|
|
49
|
+
return result || '.'
|
|
50
|
+
}
|
|
89
51
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return
|
|
52
|
+
/** Keep unusual filenames unambiguous without changing the returned path data. */
|
|
53
|
+
function displayPath(path: string): string {
|
|
54
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: quote filenames containing control bytes instead of creating fake result lines.
|
|
55
|
+
return /[\x00-\x1f\x7f]/.test(path) ? JSON.stringify(path) : path
|
|
94
56
|
}
|
|
95
57
|
|
|
96
58
|
export const GlobTool = defineTool({
|
|
97
59
|
name: 'glob',
|
|
98
|
-
description:
|
|
60
|
+
description:
|
|
61
|
+
'Finds regular files by glob pattern in a bounded search. "*" searches one directory; use "**" explicitly for recursive discovery. Returns at most 500 paths and identifies incomplete searches. Does not follow symlinks.',
|
|
99
62
|
inputSchema,
|
|
100
63
|
category: 'filesystem',
|
|
101
64
|
permissions: ['file_read'],
|
|
102
65
|
readOnly: true,
|
|
103
66
|
destructive: false,
|
|
104
67
|
concurrencySafe: true,
|
|
68
|
+
timeoutMs: 15_000,
|
|
105
69
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Contained, not merely resolved. This was a bare `resolve`, so
|
|
112
|
-
// `path: "../../.."` reached whatever sits above the working
|
|
113
|
-
// directory — with no sandbox needed to make it work.
|
|
114
|
-
const basePath = resolveWithinAny(toolRoots(context), input.path)
|
|
115
|
-
|
|
116
|
-
let searchPath = basePath
|
|
117
|
-
let pattern = input.pattern
|
|
70
|
+
presentCall(input) {
|
|
71
|
+
return { kind: 'generic', label: `${input.pattern} in ${input.path ?? '.'}` }
|
|
72
|
+
},
|
|
118
73
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
74
|
+
async execute(input, context) {
|
|
75
|
+
context.abortSignal?.throwIfAborted()
|
|
76
|
+
const sandbox = context.sandbox
|
|
77
|
+
const lexicalRoot = sandbox
|
|
78
|
+
? resolveWithinPosix(sandbox.rootDir, input.path)
|
|
79
|
+
: resolveWithinAny(toolRoots(context), input.path)
|
|
80
|
+
const root = sandbox ? lexicalRoot : await resolveWithinAnyReal(toolRoots(context), input.path)
|
|
81
|
+
const pattern = relativePattern(input.pattern, lexicalRoot, sandbox !== undefined)
|
|
82
|
+
const walkSandbox = sandbox?.walkFiles?.bind(sandbox)
|
|
83
|
+
if (sandbox && !walkSandbox) {
|
|
84
|
+
return {
|
|
85
|
+
success: false,
|
|
86
|
+
output: '',
|
|
87
|
+
error:
|
|
88
|
+
'This sandbox does not support bounded file discovery (Sandbox.walkFiles). Update its adapter or use an available directory-listing command inside the sandbox.',
|
|
128
89
|
}
|
|
129
90
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
for await (const entry of glob(pattern, { cwd: searchPath })) {
|
|
138
|
-
const absolutePath = resolve(searchPath, entry)
|
|
139
|
-
matches.push(`./${relative(context.workingDirectory, absolutePath)}`)
|
|
140
|
-
if (matches.length >= MAX_GLOB_RESULTS) break
|
|
91
|
+
const options: SandboxWalkFilesOptions = {
|
|
92
|
+
pattern,
|
|
93
|
+
signal: context.abortSignal,
|
|
94
|
+
maxEntries: MAX_GLOB_RESULTS + 1,
|
|
95
|
+
maxVisitedEntries: MAX_VISITED_ENTRIES,
|
|
96
|
+
includeHidden: input.include_hidden ?? false,
|
|
141
97
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
98
|
+
const entries = walkSandbox ? walkSandbox(root, options) : walkFilesLocally(root, options)
|
|
99
|
+
const files: string[] = []
|
|
100
|
+
let truncated = false
|
|
101
|
+
let failure: string | undefined
|
|
102
|
+
try {
|
|
103
|
+
for await (const entry of entries) {
|
|
104
|
+
context.abortSignal?.throwIfAborted()
|
|
105
|
+
const absolute = sandbox
|
|
106
|
+
? resolveWithinPosix(root, entry.path)
|
|
107
|
+
: resolveWithin(root, entry.path)
|
|
108
|
+
if (files.length === MAX_GLOB_RESULTS) {
|
|
109
|
+
truncated = true
|
|
110
|
+
break
|
|
111
|
+
}
|
|
112
|
+
// Readers resolve relative input against the original working path,
|
|
113
|
+
// which may be a symlink. Project back through the selected alias
|
|
114
|
+
// before relativizing so added directories remain reachable.
|
|
115
|
+
const file = sandbox
|
|
116
|
+
? relativePosix(sandbox.rootDir, absolute)
|
|
117
|
+
: relative(context.workingDirectory, join(lexicalRoot, relative(root, absolute)))
|
|
118
|
+
files.push(`./${sandbox ? file : file.split(sep).join('/')}`)
|
|
148
119
|
}
|
|
120
|
+
} catch (error) {
|
|
121
|
+
if (context.abortSignal?.aborted) throw error
|
|
122
|
+
truncated = true
|
|
123
|
+
failure = error instanceof Error ? error.message : String(error)
|
|
149
124
|
}
|
|
150
|
-
|
|
125
|
+
const notice = failure
|
|
126
|
+
? `[Search incomplete: ${failure}]`
|
|
127
|
+
: truncated
|
|
128
|
+
? `[Showing the first ${MAX_GLOB_RESULTS} matching files. Narrow the directory or pattern for the remaining results.]`
|
|
129
|
+
: undefined
|
|
151
130
|
return {
|
|
152
|
-
success:
|
|
153
|
-
output:
|
|
154
|
-
|
|
131
|
+
success: failure === undefined,
|
|
132
|
+
output:
|
|
133
|
+
[...files.map(displayPath), ...(notice ? [notice] : [])].join('\n') ||
|
|
134
|
+
`No files found matching pattern "${input.pattern}" in ${root}`,
|
|
135
|
+
data: { count: files.length, files, truncated, ...(sandbox ? { sandboxed: true } : {}) },
|
|
136
|
+
...(failure ? { error: failure } : {}),
|
|
155
137
|
}
|
|
156
138
|
},
|
|
157
139
|
})
|