@namzu/sdk 74.1.0 → 75.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 +47 -0
- package/dist/bridge/acp/server.d.ts +2 -0
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +9 -1
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/connector/mcp/adapter.d.ts +1 -1
- package/dist/connector/mcp/adapter.d.ts.map +1 -1
- package/dist/connector/mcp/adapter.js +3 -2
- package/dist/connector/mcp/adapter.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +4 -25
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +32 -32
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/provider/errors.d.ts.map +1 -1
- package/dist/provider/errors.js +13 -18
- package/dist/provider/errors.js.map +1 -1
- package/dist/provider/stream-request-timeout.d.ts +9 -0
- package/dist/provider/stream-request-timeout.d.ts.map +1 -0
- package/dist/provider/stream-request-timeout.js +34 -0
- package/dist/provider/stream-request-timeout.js.map +1 -0
- package/dist/public-runtime.d.ts +1 -0
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +1 -0
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +1 -1
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +1 -1
- package/dist/public-tools.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +3 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +15 -3
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/file-evidence-replay.d.ts +1 -0
- package/dist/runtime/query/file-evidence-replay.d.ts.map +1 -1
- package/dist/runtime/query/file-evidence-replay.js +64 -21
- package/dist/runtime/query/file-evidence-replay.js.map +1 -1
- package/dist/runtime/query/file-evidence-seed.d.ts +7 -0
- package/dist/runtime/query/file-evidence-seed.d.ts.map +1 -1
- package/dist/runtime/query/file-evidence-seed.js +12 -1
- package/dist/runtime/query/file-evidence-seed.js.map +1 -1
- package/dist/runtime/query/index.d.ts +2 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +5 -2
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/path-escalation.d.ts.map +1 -1
- package/dist/runtime/query/path-escalation.js +4 -3
- package/dist/runtime/query/path-escalation.js.map +1 -1
- package/dist/runtime/query/tooling.d.ts +2 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +1 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/sandbox/provider/local.d.ts.map +1 -1
- package/dist/sandbox/provider/local.js +45 -18
- package/dist/sandbox/provider/local.js.map +1 -1
- package/dist/schedules/cron.js +1 -1
- package/dist/schedules/cron.js.map +1 -1
- package/dist/schedules/describe.js +1 -1
- package/dist/schedules/describe.js.map +1 -1
- package/dist/schedules/next-fire.d.ts +2 -2
- package/dist/schedules/next-fire.d.ts.map +1 -1
- package/dist/schedules/next-fire.js +7 -4
- package/dist/schedules/next-fire.js.map +1 -1
- package/dist/schedules/spec.d.ts.map +1 -1
- package/dist/schedules/spec.js +23 -7
- package/dist/schedules/spec.js.map +1 -1
- package/dist/schedules/types.d.ts +2 -0
- package/dist/schedules/types.d.ts.map +1 -1
- package/dist/store/session-index/sqlite.d.ts.map +1 -1
- package/dist/store/session-index/sqlite.js +7 -3
- package/dist/store/session-index/sqlite.js.map +1 -1
- package/dist/store/session-log/core.d.ts.map +1 -1
- package/dist/store/session-log/core.js +88 -24
- package/dist/store/session-log/core.js.map +1 -1
- package/dist/store/session-log/disk.js +1 -1
- package/dist/store/session-log/disk.js.map +1 -1
- package/dist/tools/builtins/code-analysis.d.ts.map +1 -1
- package/dist/tools/builtins/code-analysis.js +4 -1
- package/dist/tools/builtins/code-analysis.js.map +1 -1
- package/dist/tools/builtins/edit-apply.d.ts.map +1 -1
- package/dist/tools/builtins/edit-apply.js +24 -10
- package/dist/tools/builtins/edit-apply.js.map +1 -1
- package/dist/tools/builtins/edit.d.ts.map +1 -1
- package/dist/tools/builtins/edit.js +31 -15
- package/dist/tools/builtins/edit.js.map +1 -1
- package/dist/tools/builtins/file-text.d.ts +3 -0
- package/dist/tools/builtins/file-text.d.ts.map +1 -0
- package/dist/tools/builtins/file-text.js +14 -0
- package/dist/tools/builtins/file-text.js.map +1 -0
- package/dist/tools/builtins/glob.d.ts.map +1 -1
- package/dist/tools/builtins/glob.js +7 -2
- 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 +8 -5
- package/dist/tools/builtins/grep.js.map +1 -1
- package/dist/tools/builtins/ls.d.ts.map +1 -1
- package/dist/tools/builtins/ls.js +2 -1
- package/dist/tools/builtins/ls.js.map +1 -1
- package/dist/tools/builtins/notebook-edit.d.ts.map +1 -1
- package/dist/tools/builtins/notebook-edit.js +19 -10
- package/dist/tools/builtins/notebook-edit.js.map +1 -1
- package/dist/tools/builtins/read-file.d.ts.map +1 -1
- package/dist/tools/builtins/read-file.js +48 -24
- package/dist/tools/builtins/read-file.js.map +1 -1
- package/dist/tools/builtins/read-regular-file.d.ts +5 -0
- package/dist/tools/builtins/read-regular-file.d.ts.map +1 -0
- package/dist/tools/builtins/read-regular-file.js +23 -0
- package/dist/tools/builtins/read-regular-file.js.map +1 -0
- package/dist/tools/builtins/search-support.d.ts +2 -0
- package/dist/tools/builtins/search-support.d.ts.map +1 -1
- package/dist/tools/builtins/search-support.js +14 -0
- package/dist/tools/builtins/search-support.js.map +1 -1
- package/dist/tools/builtins/stale-before-review.d.ts.map +1 -1
- package/dist/tools/builtins/stale-before-review.js +8 -5
- package/dist/tools/builtins/stale-before-review.js.map +1 -1
- package/dist/tools/builtins/write-file.d.ts.map +1 -1
- package/dist/tools/builtins/write-file.js +17 -13
- package/dist/tools/builtins/write-file.js.map +1 -1
- package/dist/tools/paths.d.ts +9 -0
- package/dist/tools/paths.d.ts.map +1 -1
- package/dist/tools/paths.js +39 -9
- package/dist/tools/paths.js.map +1 -1
- package/dist/tools/posix-path.d.ts +2 -14
- package/dist/tools/posix-path.d.ts.map +1 -1
- package/dist/tools/posix-path.js +7 -0
- package/dist/tools/posix-path.js.map +1 -1
- package/dist/types/code-analysis/index.d.ts +1 -1
- package/dist/types/code-analysis/index.d.ts.map +1 -1
- package/dist/types/provider/errors.d.ts.map +1 -1
- package/dist/types/provider/errors.js +3 -5
- package/dist/types/provider/errors.js.map +1 -1
- package/dist/types/tool/index.d.ts +2 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/package.json +1 -1
- package/src/bridge/acp/server.ts +16 -7
- package/src/connector/mcp/adapter.ts +4 -3
- package/src/connector/mcp/client.ts +33 -32
- package/src/provider/errors.ts +12 -19
- package/src/provider/stream-request-timeout.ts +34 -0
- package/src/public-runtime.ts +1 -0
- package/src/public-tools.ts +1 -0
- package/src/runtime/query/executor.ts +29 -3
- package/src/runtime/query/file-evidence-replay.ts +73 -19
- package/src/runtime/query/file-evidence-seed.ts +16 -0
- package/src/runtime/query/index.ts +9 -1
- package/src/runtime/query/path-escalation.ts +4 -3
- package/src/runtime/query/tooling.ts +3 -0
- package/src/sandbox/provider/local.ts +53 -25
- package/src/schedules/cron.ts +1 -1
- package/src/schedules/describe.ts +1 -1
- package/src/schedules/next-fire.ts +7 -4
- package/src/schedules/spec.ts +36 -7
- package/src/schedules/types.ts +2 -0
- package/src/store/session-index/sqlite.ts +8 -4
- package/src/store/session-log/core.ts +96 -25
- package/src/store/session-log/disk.ts +1 -1
- package/src/tools/builtins/code-analysis.ts +6 -1
- package/src/tools/builtins/edit-apply.ts +25 -10
- package/src/tools/builtins/edit.ts +31 -15
- package/src/tools/builtins/file-text.ts +14 -0
- package/src/tools/builtins/glob.ts +6 -1
- package/src/tools/builtins/grep.ts +7 -5
- package/src/tools/builtins/ls.ts +2 -1
- package/src/tools/builtins/notebook-edit.ts +18 -11
- package/src/tools/builtins/read-file.ts +52 -27
- package/src/tools/builtins/read-regular-file.ts +25 -0
- package/src/tools/builtins/search-support.ts +13 -0
- package/src/tools/builtins/stale-before-review.ts +8 -6
- package/src/tools/builtins/write-file.ts +18 -12
- package/src/tools/paths.ts +49 -7
- package/src/tools/posix-path.ts +9 -0
- package/src/types/code-analysis/index.ts +1 -1
- package/src/types/provider/errors.ts +3 -5
- package/src/types/tool/index.ts +2 -0
|
@@ -4,12 +4,14 @@ import { z } from 'zod'
|
|
|
4
4
|
import type { ToolContext, ToolResult } from '../../types/tool/index.js'
|
|
5
5
|
import type { ToolFacts } from '../../types/tool/presentation.js'
|
|
6
6
|
import { defineTool } from '../defineTool.js'
|
|
7
|
-
import { resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
7
|
+
import { resolveAllowedFile, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
8
|
+
import { refuseSandboxHomePath } from '../posix-path.js'
|
|
8
9
|
import { resolveRetainedOutput } from '../retained-output.js'
|
|
9
10
|
import { looksBinary } from '../text-window.js'
|
|
10
11
|
import { toolIsAvailable } from '../tool-is-available.js'
|
|
11
12
|
import { TEXT_MAX_INPUT_BYTES, readBoundedChunks, readBoundedFile } from './bounded-file-read.js'
|
|
12
13
|
import { fingerprintContent } from './content-fingerprint.js'
|
|
14
|
+
import { decodeFileText } from './file-text.js'
|
|
13
15
|
import {
|
|
14
16
|
type MediaRead,
|
|
15
17
|
NOTEBOOK_MAX_BYTES,
|
|
@@ -95,8 +97,10 @@ export const ReadFileTool = defineTool({
|
|
|
95
97
|
// roots and outside any sandbox. It is read here, as a plain window of text,
|
|
96
98
|
// because the result that named it told the model to.
|
|
97
99
|
const retained = await resolveRetainedOutput(context.retainedOutputDirectory, input.path)
|
|
98
|
-
|
|
99
|
-
|
|
100
|
+
const allowed =
|
|
101
|
+
retained === undefined ? await resolveAllowedFile(context, input.path) : undefined
|
|
102
|
+
if (context.sandbox && retained === undefined && allowed === undefined) {
|
|
103
|
+
refuseSandboxHomePath(input.path)
|
|
100
104
|
const sandbox = context.sandbox
|
|
101
105
|
const buffer = await readBoundedChunks(
|
|
102
106
|
(offset, length) =>
|
|
@@ -107,7 +111,7 @@ export const ReadFileTool = defineTool({
|
|
|
107
111
|
)
|
|
108
112
|
const media = await readMediaFile(buffer, input.path, input, context, true)
|
|
109
113
|
if (media) return media
|
|
110
|
-
const binaryGuidance = describeStructuredBinaryRead(input.path, shellAvailable)
|
|
114
|
+
const binaryGuidance = describeStructuredBinaryRead(input.path, true, shellAvailable)
|
|
111
115
|
if (binaryGuidance) {
|
|
112
116
|
return {
|
|
113
117
|
success: false,
|
|
@@ -125,8 +129,8 @@ export const ReadFileTool = defineTool({
|
|
|
125
129
|
},
|
|
126
130
|
}
|
|
127
131
|
}
|
|
128
|
-
const content = buffer
|
|
129
|
-
if (looksBinary(content))
|
|
132
|
+
const content = decodeFileText(buffer)
|
|
133
|
+
if (content === undefined || looksBinary(content))
|
|
130
134
|
return unreadableAsText(input.path, buffer.length, true, shellAvailable)
|
|
131
135
|
const capped = renderCappedRead(content, buffer.length, input, capOptions(input, context))
|
|
132
136
|
if (!capped.ok) return refusedRead(input.path, buffer.length, capped.message, true)
|
|
@@ -149,7 +153,8 @@ export const ReadFileTool = defineTool({
|
|
|
149
153
|
}
|
|
150
154
|
}
|
|
151
155
|
|
|
152
|
-
const filePath =
|
|
156
|
+
const filePath =
|
|
157
|
+
retained ?? allowed ?? (await resolveWithinAnyReal(toolRoots(context), input.path))
|
|
153
158
|
// Pictures, PDFs and notebooks are not text: they have their own size
|
|
154
159
|
// limits and are never numbered, so the text cap below does not apply.
|
|
155
160
|
const namesMediaFile = namesMedia(filePath)
|
|
@@ -165,7 +170,7 @@ export const ReadFileTool = defineTool({
|
|
|
165
170
|
)
|
|
166
171
|
const media = await readMediaFile(buffer, filePath, input, context, false)
|
|
167
172
|
if (media) return media
|
|
168
|
-
const binaryGuidance = describeStructuredBinaryRead(filePath, shellAvailable)
|
|
173
|
+
const binaryGuidance = describeStructuredBinaryRead(filePath, false, shellAvailable)
|
|
169
174
|
if (binaryGuidance) {
|
|
170
175
|
return {
|
|
171
176
|
success: false,
|
|
@@ -182,8 +187,8 @@ export const ReadFileTool = defineTool({
|
|
|
182
187
|
},
|
|
183
188
|
}
|
|
184
189
|
}
|
|
185
|
-
const content = buffer
|
|
186
|
-
if (looksBinary(content))
|
|
190
|
+
const content = decodeFileText(buffer)
|
|
191
|
+
if (content === undefined || looksBinary(content))
|
|
187
192
|
return unreadableAsText(filePath, buffer.length, false, shellAvailable)
|
|
188
193
|
const capped = renderCappedRead(content, buffer.length, input, capOptions(input, context))
|
|
189
194
|
if (!capped.ok) return refusedRead(filePath, buffer.length, capped.message, false)
|
|
@@ -293,6 +298,10 @@ async function readMediaFile(
|
|
|
293
298
|
)
|
|
294
299
|
}
|
|
295
300
|
}
|
|
301
|
+
const notebookText = kind === 'notebook' ? decodeFileText(buffer) : undefined
|
|
302
|
+
if (kind === 'notebook' && notebookText === undefined) {
|
|
303
|
+
return unreadableAsText(path, buffer.length, sandboxed, toolIsAvailable(context, 'bash'))
|
|
304
|
+
}
|
|
296
305
|
const media: MediaRead =
|
|
297
306
|
kind === 'pdf'
|
|
298
307
|
? await readPdfPages(buffer, input.pages, path, context.abortSignal)
|
|
@@ -301,7 +310,7 @@ async function readMediaFile(
|
|
|
301
310
|
// A notebook shown whole is a read like any other: the body is what a later
|
|
302
311
|
// edit of it is checked against, and `notebook_edit` refuses a notebook the
|
|
303
312
|
// session has not read.
|
|
304
|
-
if (
|
|
313
|
+
if (notebookText !== undefined) context.fileReadTracker?.recordRead(path, notebookText)
|
|
305
314
|
const text = media.parts
|
|
306
315
|
.filter((part) => part.type === 'text')
|
|
307
316
|
.map((part) => part.text)
|
|
@@ -475,29 +484,45 @@ function unchangedRead(
|
|
|
475
484
|
}
|
|
476
485
|
}
|
|
477
486
|
|
|
478
|
-
function describeStructuredBinaryRead(
|
|
479
|
-
const ext = extname(path).toLowerCase()
|
|
480
|
-
if (ext === '.docx')
|
|
481
|
-
return buildStructuredBinaryGuidance(path, 'DOCX', 'python-docx', shellAvailable)
|
|
482
|
-
if (ext === '.pptx')
|
|
483
|
-
return buildStructuredBinaryGuidance(path, 'PPTX', 'python-pptx', shellAvailable)
|
|
484
|
-
if (ext === '.xlsx')
|
|
485
|
-
return buildStructuredBinaryGuidance(path, 'XLSX', 'openpyxl', shellAvailable)
|
|
486
|
-
return null
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
function buildStructuredBinaryGuidance(
|
|
487
|
+
function describeStructuredBinaryRead(
|
|
490
488
|
path: string,
|
|
491
|
-
|
|
492
|
-
extractor: string,
|
|
489
|
+
sandboxed: boolean,
|
|
493
490
|
shellAvailable: boolean,
|
|
494
|
-
): string {
|
|
491
|
+
): string | null {
|
|
492
|
+
const format = extname(path).slice(1).toUpperCase()
|
|
493
|
+
const environment = sandboxed ? 'in the sandbox' : 'on the host'
|
|
494
|
+
let extraction: string
|
|
495
|
+
switch (format) {
|
|
496
|
+
case 'DOCX':
|
|
497
|
+
extraction = pythonExtractorGuidance('python-docx', 'docx', environment)
|
|
498
|
+
break
|
|
499
|
+
case 'DOCM':
|
|
500
|
+
extraction = `Use Python's zipfile and XML tools ${environment} to extract text from word/document.xml without running macros.`
|
|
501
|
+
break
|
|
502
|
+
case 'PPTX':
|
|
503
|
+
extraction = pythonExtractorGuidance('python-pptx', 'pptx', environment)
|
|
504
|
+
break
|
|
505
|
+
case 'XLSX':
|
|
506
|
+
case 'XLSM':
|
|
507
|
+
extraction = pythonExtractorGuidance('openpyxl', 'openpyxl', environment)
|
|
508
|
+
break
|
|
509
|
+
case 'ODT':
|
|
510
|
+
extraction = pythonExtractorGuidance('odfpy', 'odf', environment)
|
|
511
|
+
break
|
|
512
|
+
default:
|
|
513
|
+
return null
|
|
514
|
+
}
|
|
495
515
|
const description = `The file "${path}" is a ${format} document package, not UTF-8 text.`
|
|
496
516
|
if (!shellAvailable) return description
|
|
497
517
|
return [
|
|
498
518
|
description,
|
|
499
519
|
'Do not use the read/cat tools as evidence for this raw file.',
|
|
500
|
-
|
|
520
|
+
extraction,
|
|
521
|
+
'Write the extracted text or summary under scratch, then read that text file.',
|
|
501
522
|
'If extraction fails, report the exact filename and extraction error instead of claiming the attachment is unavailable.',
|
|
502
523
|
].join('\n')
|
|
503
524
|
}
|
|
525
|
+
|
|
526
|
+
function pythonExtractorGuidance(pkg: string, module: string, environment: string): string {
|
|
527
|
+
return `Check for the ${pkg} extractor ${environment} with python3 -c "import ${module}". If the module is missing, you may need python3 -m pip install ${pkg} in that Python environment before extracting text.`
|
|
528
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { constants } from 'node:fs'
|
|
2
|
+
import { lstat, open } from 'node:fs/promises'
|
|
3
|
+
|
|
4
|
+
/** Read a canonical mutation target's bytes without blocking on special files. */
|
|
5
|
+
export async function readRegularFileBytes(
|
|
6
|
+
path: string,
|
|
7
|
+
operation: 'Edit' | 'Write',
|
|
8
|
+
): Promise<Buffer> {
|
|
9
|
+
const regularFileError = () => new Error(`${operation} requires a regular file.`)
|
|
10
|
+
if (!(await lstat(path)).isFile()) throw regularFileError()
|
|
11
|
+
|
|
12
|
+
// If the target changes after lstat, a FIFO still cannot block the open.
|
|
13
|
+
const file = await open(path, constants.O_RDONLY | constants.O_NONBLOCK)
|
|
14
|
+
try {
|
|
15
|
+
if (!(await file.stat()).isFile()) throw regularFileError()
|
|
16
|
+
return await file.readFile()
|
|
17
|
+
} finally {
|
|
18
|
+
await file.close()
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The same read as lenient text, for the checks that only compare content (a write's staleness). */
|
|
23
|
+
export async function readRegularFile(path: string, operation: 'Edit' | 'Write'): Promise<string> {
|
|
24
|
+
return (await readRegularFileBytes(path, operation)).toString('utf-8')
|
|
25
|
+
}
|
|
@@ -1,8 +1,21 @@
|
|
|
1
|
+
import { stat } from 'node:fs/promises'
|
|
1
2
|
import { isAbsolute, join as joinPath, posix, relative, sep } from 'node:path'
|
|
2
3
|
|
|
3
4
|
import { subscribeToAbort } from '../../utils/abort.js'
|
|
4
5
|
import { readPositiveIntEnv } from '../../utils/env.js'
|
|
5
6
|
|
|
7
|
+
/** Report a missing host search root separately from an empty search. */
|
|
8
|
+
export async function missingSearchRoot(path: string): Promise<boolean> {
|
|
9
|
+
try {
|
|
10
|
+
await stat(path)
|
|
11
|
+
return false
|
|
12
|
+
} catch (error) {
|
|
13
|
+
const code = (error as NodeJS.ErrnoException).code
|
|
14
|
+
if (code === 'ENOENT' || code === 'ENOTDIR') return true
|
|
15
|
+
throw error
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
6
19
|
/**
|
|
7
20
|
* How long grep and glob search before they return what they have. The tool
|
|
8
21
|
* deadline (budget + 3 s) cancels a call outright and discards its output; a
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises'
|
|
2
|
-
|
|
3
1
|
import type { ToolContext } from '../../types/tool/index.js'
|
|
4
2
|
import type { ToolFacts } from '../../types/tool/presentation.js'
|
|
5
|
-
import { resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
3
|
+
import { resolveAllowedFile, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
4
|
+
import { refuseSandboxHomePath } from '../posix-path.js'
|
|
6
5
|
import { movedSinceRead, staleFileError } from './content-fingerprint.js'
|
|
6
|
+
import { readRegularFile } from './read-regular-file.js'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* The refusal a file mutation would give for a file that moved on disk since
|
|
@@ -26,12 +26,14 @@ export async function staleBeforeReview(
|
|
|
26
26
|
let key: string
|
|
27
27
|
let current: string
|
|
28
28
|
try {
|
|
29
|
-
|
|
29
|
+
const allowed = await resolveAllowedFile(context, path)
|
|
30
|
+
if (context.sandbox && allowed === undefined) {
|
|
31
|
+
refuseSandboxHomePath(path)
|
|
30
32
|
key = path
|
|
31
33
|
current = (await context.sandbox.readFile(path)).toString('utf-8')
|
|
32
34
|
} else {
|
|
33
|
-
key = await resolveWithinAnyReal(toolRoots(context), path)
|
|
34
|
-
current = await
|
|
35
|
+
key = allowed ?? (await resolveWithinAnyReal(toolRoots(context), path))
|
|
36
|
+
current = await readRegularFile(key, operation === 'write' ? 'Write' : 'Edit')
|
|
35
37
|
}
|
|
36
38
|
} catch {
|
|
37
39
|
return undefined
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { mkdir
|
|
1
|
+
import { mkdir } from 'node:fs/promises'
|
|
2
2
|
import { dirname } from 'node:path'
|
|
3
3
|
import { z } from 'zod'
|
|
4
4
|
import type { ToolContext, ToolResult } from '../../types/tool/index.js'
|
|
5
5
|
import type { ToolFacts } from '../../types/tool/presentation.js'
|
|
6
6
|
import { defineTool } from '../defineTool.js'
|
|
7
|
-
import { resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
7
|
+
import { resolveAllowedFile, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
8
|
+
import { refuseSandboxHomePath } from '../posix-path.js'
|
|
8
9
|
import { atomicWriteFile } from './atomic-write-file.js'
|
|
9
10
|
import { clip } from './classifier-input.js'
|
|
10
11
|
import { fingerprintContent, staleFileError } from './content-fingerprint.js'
|
|
@@ -12,6 +13,7 @@ import { withFileMutationLock } from './file-mutation-lock.js'
|
|
|
12
13
|
import { fileWriteResult } from './file-write-result.js'
|
|
13
14
|
import { jsonStringEscapes } from './json-string-hint.js'
|
|
14
15
|
import { notebookRefusal } from './notebook-guard.js'
|
|
16
|
+
import { readRegularFile } from './read-regular-file.js'
|
|
15
17
|
import { staleBeforeReview } from './stale-before-review.js'
|
|
16
18
|
|
|
17
19
|
const inputSchema = z
|
|
@@ -154,9 +156,12 @@ export const WriteFileTool = defineTool({
|
|
|
154
156
|
}
|
|
155
157
|
const valid = parsed.data
|
|
156
158
|
const content = valid.content ?? valid.newStr ?? ''
|
|
159
|
+
const allowed = await resolveAllowedFile(context, valid.path)
|
|
160
|
+
const sandbox = allowed === undefined ? context.sandbox : undefined
|
|
157
161
|
// The sandbox has its own root, so its path is checked as typed. The
|
|
158
162
|
// host path is checked below, once containment has resolved symlinks.
|
|
159
|
-
if (
|
|
163
|
+
if (sandbox) {
|
|
164
|
+
refuseSandboxHomePath(valid.path)
|
|
160
165
|
const notebook = notebookRefusal(valid.path, content)
|
|
161
166
|
if (notebook) return { success: false as const, output: '', error: notebook }
|
|
162
167
|
}
|
|
@@ -164,9 +169,8 @@ export const WriteFileTool = defineTool({
|
|
|
164
169
|
// own root and its own resolver; canonicalizing a sandbox-relative
|
|
165
170
|
// path against the HOST filesystem asks a question about the wrong
|
|
166
171
|
// machine and answers it with whatever happens to exist there.
|
|
167
|
-
const filePath =
|
|
168
|
-
? undefined
|
|
169
|
-
: await resolveWithinAnyReal(toolRoots(context), valid.path)
|
|
172
|
+
const filePath =
|
|
173
|
+
allowed ?? (sandbox ? undefined : await resolveWithinAnyReal(toolRoots(context), valid.path))
|
|
170
174
|
if (filePath !== undefined) {
|
|
171
175
|
const notebook = notebookRefusal(filePath, content)
|
|
172
176
|
if (notebook) return { success: false as const, output: '', error: notebook }
|
|
@@ -174,16 +178,16 @@ export const WriteFileTool = defineTool({
|
|
|
174
178
|
// The exists-check and the write are a check-then-act pair. Unlocked,
|
|
175
179
|
// two writers both see "absent", both skip the read-before-overwrite
|
|
176
180
|
// guard, and the second silently discards the first.
|
|
177
|
-
const lockKey =
|
|
181
|
+
const lockKey = sandbox ? `sandbox:${valid.path}` : `local:${filePath as string}`
|
|
178
182
|
|
|
179
183
|
return withFileMutationLock(lockKey, async () => {
|
|
180
|
-
if (
|
|
184
|
+
if (sandbox) {
|
|
181
185
|
const current = await readSandboxFileIfPresent(context, valid.path)
|
|
182
186
|
if (current !== undefined && current !== null) {
|
|
183
187
|
const guard = enforceFreshOverwrite(context, valid.path, current.toString('utf-8'))
|
|
184
188
|
if (guard) return guard
|
|
185
189
|
}
|
|
186
|
-
await
|
|
190
|
+
await sandbox.writeFile(valid.path, content)
|
|
187
191
|
// This write is now the runtime's newest observation. Recording only
|
|
188
192
|
// the boolean would leave an older content fingerprint in place and
|
|
189
193
|
// make the next same-run write refuse its own predecessor as drift.
|
|
@@ -229,12 +233,14 @@ async function refuseWriteBeforeReview(
|
|
|
229
233
|
// unread-file refusals are given when the call runs there, so a guest that is
|
|
230
234
|
// not admitted yet, or that holds a file the host has never seen, is not
|
|
231
235
|
// judged from a read made before anyone approved the call.
|
|
232
|
-
if (context.sandbox) return staleBeforeReview(context, input.path, 'write')
|
|
233
236
|
const tracker = context.fileReadTracker
|
|
234
237
|
if (!tracker) return undefined
|
|
235
238
|
let key: string
|
|
236
239
|
try {
|
|
237
|
-
|
|
240
|
+
const allowed = await resolveAllowedFile(context, input.path)
|
|
241
|
+
if (context.sandbox && allowed === undefined)
|
|
242
|
+
return staleBeforeReview(context, input.path, 'write')
|
|
243
|
+
key = allowed ?? (await resolveWithinAnyReal(toolRoots(context), input.path))
|
|
238
244
|
if ((await readHostFileIfPresent(key)) === undefined) return undefined
|
|
239
245
|
} catch {
|
|
240
246
|
return undefined
|
|
@@ -288,7 +294,7 @@ function enforceFreshOverwrite(
|
|
|
288
294
|
|
|
289
295
|
async function readHostFileIfPresent(hostPath: string): Promise<string | undefined> {
|
|
290
296
|
try {
|
|
291
|
-
return await
|
|
297
|
+
return await readRegularFile(hostPath, 'Write')
|
|
292
298
|
} catch (error) {
|
|
293
299
|
if (isMissingFileError(error)) return undefined
|
|
294
300
|
throw error
|
package/src/tools/paths.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import { realpath } from 'node:fs/promises'
|
|
2
|
-
import { tmpdir } from 'node:os'
|
|
1
|
+
import { lstat, realpath } from 'node:fs/promises'
|
|
2
|
+
import { homedir, tmpdir } from 'node:os'
|
|
3
3
|
import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path'
|
|
4
4
|
|
|
5
|
+
export function isHomeRelativePath(path: string | undefined): boolean {
|
|
6
|
+
return path === '~' || path?.startsWith('~/') === true
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function expandHomePath(path: string): string {
|
|
10
|
+
return isHomeRelativePath(path) ? join(homedir(), path.slice(1)) : path
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
/**
|
|
6
14
|
* Resolve a caller-supplied path against a root, refusing anything that
|
|
7
15
|
* lands outside it.
|
|
@@ -83,6 +91,36 @@ export function toolRoots(context: {
|
|
|
83
91
|
]
|
|
84
92
|
}
|
|
85
93
|
|
|
94
|
+
/** Match the resolved spelling of one host-supplied absolute filename. */
|
|
95
|
+
export function isAllowedFile(
|
|
96
|
+
context: {
|
|
97
|
+
readonly workingDirectory: string
|
|
98
|
+
readonly allowedFiles?: readonly string[] | undefined
|
|
99
|
+
},
|
|
100
|
+
path: string,
|
|
101
|
+
): boolean {
|
|
102
|
+
if (path === '' || path.includes('\0') || !context.allowedFiles?.length) return false
|
|
103
|
+
const target = resolve(context.workingDirectory, path)
|
|
104
|
+
return context.allowedFiles.some((file) => isAbsolute(file) && resolve(file) === target)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A listed file may be created, but an existing entry must be a regular file. */
|
|
108
|
+
export async function resolveAllowedFile(
|
|
109
|
+
context: Parameters<typeof isAllowedFile>[0],
|
|
110
|
+
path: string,
|
|
111
|
+
): Promise<string | undefined> {
|
|
112
|
+
if (!isAllowedFile(context, path)) return undefined
|
|
113
|
+
const target = resolve(context.workingDirectory, path)
|
|
114
|
+
const entry = await lstat(target).catch((error: NodeJS.ErrnoException) => {
|
|
115
|
+
if (error.code !== 'ENOENT') throw error
|
|
116
|
+
return undefined
|
|
117
|
+
})
|
|
118
|
+
if (entry && !entry.isFile()) {
|
|
119
|
+
throw new Error(`The session file must be a regular file: ${target}.`)
|
|
120
|
+
}
|
|
121
|
+
return canonicalize(target)
|
|
122
|
+
}
|
|
123
|
+
|
|
86
124
|
/**
|
|
87
125
|
* The absolute path `candidate` names when it lies outside every root, or
|
|
88
126
|
* `undefined` when a tool would accept it as it stands.
|
|
@@ -105,7 +143,7 @@ export async function pathOutsideRoots(
|
|
|
105
143
|
await resolveWithinAnyReal(roots, candidate)
|
|
106
144
|
return undefined
|
|
107
145
|
} catch {
|
|
108
|
-
return resolve(
|
|
146
|
+
return resolve(first, expandHomePath(candidate))
|
|
109
147
|
}
|
|
110
148
|
}
|
|
111
149
|
|
|
@@ -122,11 +160,13 @@ function describeRoots(roots: readonly string[]): string {
|
|
|
122
160
|
export function resolveWithinAny(roots: readonly string[], candidate: string | undefined): string {
|
|
123
161
|
const [first, ...rest] = roots
|
|
124
162
|
if (first === undefined) throw new Error('No root to resolve within.')
|
|
125
|
-
if (rest.length === 0)
|
|
163
|
+
if (rest.length === 0) {
|
|
164
|
+
return resolveWithin(first, candidate === undefined ? undefined : expandHomePath(candidate))
|
|
165
|
+
}
|
|
126
166
|
const resolved =
|
|
127
167
|
candidate === undefined || candidate === ''
|
|
128
168
|
? resolve(first)
|
|
129
|
-
: resolve(
|
|
169
|
+
: resolve(first, expandHomePath(candidate))
|
|
130
170
|
for (const root of roots) {
|
|
131
171
|
if (isWithin(root, resolved)) return resolveWithin(root, resolved)
|
|
132
172
|
}
|
|
@@ -142,11 +182,13 @@ export async function resolveWithinAnyReal(
|
|
|
142
182
|
): Promise<string> {
|
|
143
183
|
const [first, ...rest] = roots
|
|
144
184
|
if (first === undefined) throw new Error('No root to resolve within.')
|
|
145
|
-
if (rest.length === 0)
|
|
185
|
+
if (rest.length === 0) {
|
|
186
|
+
return resolveWithinReal(first, candidate === undefined ? undefined : expandHomePath(candidate))
|
|
187
|
+
}
|
|
146
188
|
const resolved =
|
|
147
189
|
candidate === undefined || candidate === ''
|
|
148
190
|
? resolve(first)
|
|
149
|
-
: resolve(
|
|
191
|
+
: resolve(first, expandHomePath(candidate))
|
|
150
192
|
let firstError: unknown
|
|
151
193
|
for (const root of roots) {
|
|
152
194
|
try {
|
package/src/tools/posix-path.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isHomeRelativePath } from './paths.js'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Path arithmetic in the SANDBOX's coordinate system.
|
|
3
5
|
*
|
|
@@ -54,6 +56,13 @@ export function relativePosix(from: string, to: string): string {
|
|
|
54
56
|
return [...up, ...toSegments.slice(shared)].join('/')
|
|
55
57
|
}
|
|
56
58
|
|
|
59
|
+
/** The sandbox adapter does not expose the guest's home directory. */
|
|
60
|
+
export function refuseSandboxHomePath(candidate: string | undefined): void {
|
|
61
|
+
if (isHomeRelativePath(candidate)) {
|
|
62
|
+
throw new Error('Use an explicit sandbox path instead of ~ or ~/.')
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
57
66
|
/**
|
|
58
67
|
* Resolve `candidate` against `root`, refusing anything that lands
|
|
59
68
|
* outside. The sandbox-side twin of the host containment check.
|
|
@@ -206,7 +206,7 @@ export interface FindLiteralRequest extends AnalysisPaging {
|
|
|
206
206
|
readonly text: string
|
|
207
207
|
/** Absolute file or directory to restrict the search to. */
|
|
208
208
|
readonly path?: string
|
|
209
|
-
/** Default `true`. */
|
|
209
|
+
/** Default `true`. Set to `false` for Unicode simple case folding without locale tailoring. */
|
|
210
210
|
readonly caseSensitive?: boolean
|
|
211
211
|
}
|
|
212
212
|
|
|
@@ -497,6 +497,7 @@ const STRUCTURAL_CODES: Readonly<Record<string, ProviderErrorCode>> = {
|
|
|
497
497
|
usage_limit_reached: 'usage_limit',
|
|
498
498
|
usage_limit_exceeded: 'usage_limit',
|
|
499
499
|
billing_error: 'usage_limit',
|
|
500
|
+
credits_required: 'usage_limit',
|
|
500
501
|
overloaded_error: 'overloaded',
|
|
501
502
|
content_filter: 'content_filter',
|
|
502
503
|
content_policy_violation: 'content_filter',
|
|
@@ -507,14 +508,11 @@ const STRUCTURAL_CODES: Readonly<Record<string, ProviderErrorCode>> = {
|
|
|
507
508
|
}
|
|
508
509
|
|
|
509
510
|
/**
|
|
510
|
-
*
|
|
511
|
-
* allowance? The codes are the vendors' identifiers (`insufficient_quota`,
|
|
512
|
-
* `usage_limit_reached`, `usage_limit_exceeded`, `billing_error`); the
|
|
513
|
-
* sentences are the two vendors that word it only in prose: an exhausted
|
|
514
|
-
* prepaid balance and an exhausted plan allowance.
|
|
511
|
+
* Provider wording for exhausted allowances and required credits.
|
|
515
512
|
*/
|
|
516
513
|
const USAGE_LIMIT_DETAIL: readonly RegExp[] = [
|
|
517
514
|
/credit balance is too low/i,
|
|
515
|
+
/credits are required/i,
|
|
518
516
|
/exceeded your current quota/i,
|
|
519
517
|
/usage limit (?:has been )?(?:reached|exceeded)/i,
|
|
520
518
|
]
|
package/src/types/tool/index.ts
CHANGED
|
@@ -692,6 +692,8 @@ export interface ToolContext {
|
|
|
692
692
|
* directory; an absolute path inside any of these is accepted.
|
|
693
693
|
*/
|
|
694
694
|
additionalDirectories?: readonly string[]
|
|
695
|
+
/** Host-owned absolute file paths available to read, write and edit, including in a sandbox. */
|
|
696
|
+
allowedFiles?: readonly string[]
|
|
695
697
|
/**
|
|
696
698
|
* Whether the session's read rules deny an absolute path. The search tools
|
|
697
699
|
* (`grep`, `glob`) call it for every file they would list or open and skip
|