@namzu/sdk 74.1.0 → 76.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 +85 -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 +13 -2
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/bridge/acp/update.d.ts.map +1 -1
- package/dist/bridge/acp/update.js +22 -2
- package/dist/bridge/acp/update.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/peers/record.d.ts +3 -3
- 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 +3 -2
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +2 -1
- 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 +25 -4
- 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/session/workspace/git-worktree.d.ts +18 -0
- package/dist/session/workspace/git-worktree.d.ts.map +1 -1
- package/dist/session/workspace/git-worktree.js +100 -1
- package/dist/session/workspace/git-worktree.js.map +1 -1
- package/dist/session/workspace/index.d.ts +1 -1
- package/dist/session/workspace/index.d.ts.map +1 -1
- package/dist/session/workspace/index.js +1 -1
- package/dist/session/workspace/index.js.map +1 -1
- package/dist/store/checkpoint/contract.d.ts.map +1 -1
- package/dist/store/checkpoint/contract.js +5 -2
- package/dist/store/checkpoint/contract.js.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 +118 -25
- 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/admit-facts.d.ts.map +1 -1
- package/dist/tools/admit-facts.js +19 -0
- package/dist/tools/admit-facts.js.map +1 -1
- package/dist/tools/builtins/bash.d.ts +1 -1
- package/dist/tools/builtins/bash.d.ts.map +1 -1
- package/dist/tools/builtins/bash.js +9 -7
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/browser.d.ts +2 -2
- package/dist/tools/builtins/browser.d.ts.map +1 -1
- package/dist/tools/builtins/browser.js +15 -3
- package/dist/tools/builtins/browser.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/computer-use.d.ts +19 -3
- package/dist/tools/builtins/computer-use.d.ts.map +1 -1
- package/dist/tools/builtins/computer-use.js +150 -50
- package/dist/tools/builtins/computer-use.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/coordinator/agent.d.ts.map +1 -1
- package/dist/tools/coordinator/agent.js +6 -0
- package/dist/tools/coordinator/agent.js.map +1 -1
- package/dist/tools/coordinator/index.d.ts.map +1 -1
- package/dist/tools/coordinator/index.js +6 -0
- package/dist/tools/coordinator/index.js.map +1 -1
- package/dist/tools/coordinator/progress.d.ts +9 -0
- package/dist/tools/coordinator/progress.d.ts.map +1 -0
- package/dist/tools/coordinator/progress.js +30 -0
- package/dist/tools/coordinator/progress.js.map +1 -0
- 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/tools/shell-output-window.d.ts +12 -0
- package/dist/tools/shell-output-window.d.ts.map +1 -0
- package/dist/tools/shell-output-window.js +92 -0
- package/dist/tools/shell-output-window.js.map +1 -0
- package/dist/types/acp/index.d.ts +3 -0
- package/dist/types/acp/index.d.ts.map +1 -1
- package/dist/types/browser/index.d.ts +4 -0
- package/dist/types/browser/index.d.ts.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/computer-use/index.d.ts +4 -0
- package/dist/types/computer-use/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/session/checkpoint.d.ts +13 -0
- package/dist/types/session/checkpoint.d.ts.map +1 -1
- package/dist/types/session/checkpoint.js +33 -0
- package/dist/types/session/checkpoint.js.map +1 -1
- package/dist/types/session/events.d.ts +3 -0
- package/dist/types/session/events.d.ts.map +1 -1
- package/dist/types/session/events.js.map +1 -1
- package/dist/types/tool/index.d.ts +17 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/types/tool/presentation.d.ts +8 -0
- package/dist/types/tool/presentation.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/bridge/acp/server.ts +22 -8
- package/src/bridge/acp/update.ts +23 -2
- 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 +3 -0
- package/src/public-tools.ts +1 -0
- package/src/runtime/query/executor.ts +41 -6
- 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/session/workspace/git-worktree.ts +115 -1
- package/src/session/workspace/index.ts +6 -1
- package/src/store/checkpoint/contract.ts +9 -2
- package/src/store/session-index/sqlite.ts +8 -4
- package/src/store/session-log/core.ts +130 -26
- package/src/store/session-log/disk.ts +1 -1
- package/src/tools/admit-facts.ts +20 -0
- package/src/tools/builtins/bash.ts +11 -12
- package/src/tools/builtins/browser.ts +16 -3
- package/src/tools/builtins/code-analysis.ts +6 -1
- package/src/tools/builtins/computer-use.ts +188 -44
- 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/coordinator/agent.ts +6 -0
- package/src/tools/coordinator/index.ts +6 -0
- package/src/tools/coordinator/progress.ts +40 -0
- package/src/tools/paths.ts +49 -7
- package/src/tools/posix-path.ts +9 -0
- package/src/tools/shell-output-window.ts +97 -0
- package/src/types/acp/index.ts +3 -0
- package/src/types/browser/index.ts +2 -0
- package/src/types/code-analysis/index.ts +1 -1
- package/src/types/computer-use/index.ts +17 -0
- package/src/types/provider/errors.ts +3 -5
- package/src/types/session/checkpoint.ts +37 -0
- package/src/types/session/events.ts +3 -0
- package/src/types/tool/index.ts +12 -0
- package/src/types/tool/presentation.ts +12 -1
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
1
2
|
import { createHash } from 'node:crypto'
|
|
2
3
|
import { z } from 'zod'
|
|
3
4
|
import { modelReadsImages, resolveProviderCapabilities } from '../../provider/capabilities.js'
|
|
5
|
+
import type {
|
|
6
|
+
ComputerUseStageTimes,
|
|
7
|
+
ComputerUseTimingStage,
|
|
8
|
+
} from '../../types/computer-use/index.js'
|
|
4
9
|
import type {
|
|
5
10
|
ActionDelivery,
|
|
6
11
|
ActionRetrySafety,
|
|
@@ -75,6 +80,7 @@ async function settleAfterActions(
|
|
|
75
80
|
windowId: string | undefined,
|
|
76
81
|
settleMs: number,
|
|
77
82
|
signal: AbortSignal | undefined,
|
|
83
|
+
wait: (run: () => Promise<void>) => Promise<void>,
|
|
78
84
|
): Promise<void> {
|
|
79
85
|
if (settleMs > 0 && typeof host.settle === 'function') {
|
|
80
86
|
try {
|
|
@@ -87,7 +93,7 @@ async function settleAfterActions(
|
|
|
87
93
|
// Fall through to the fixed wait; a cancelled signal makes it reject at once.
|
|
88
94
|
}
|
|
89
95
|
}
|
|
90
|
-
await sleep(settleMs, signal)
|
|
96
|
+
await wait(() => sleep(settleMs, signal))
|
|
91
97
|
}
|
|
92
98
|
const MAX_LISTED_WINDOWS = 50
|
|
93
99
|
/** Steps after which a batch may end with the window's controls instead of a picture. */
|
|
@@ -153,11 +159,58 @@ const ADVICE_MIN_MARGIN = 0.4
|
|
|
153
159
|
/** The most controls an advisor is asked about at once. */
|
|
154
160
|
const MAX_ADVISED_CANDIDATES = 255
|
|
155
161
|
|
|
162
|
+
/** Operator diagnostics for one computer-use call. */
|
|
163
|
+
export interface ComputerUseTimingRecord {
|
|
164
|
+
readonly app?: string
|
|
165
|
+
readonly elapsedMs: number
|
|
166
|
+
readonly stages: ComputerUseStageTimes
|
|
167
|
+
readonly steps: readonly {
|
|
168
|
+
readonly action: string
|
|
169
|
+
readonly elapsedMs: number
|
|
170
|
+
readonly stages: ComputerUseStageTimes
|
|
171
|
+
}[]
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function timingTail(
|
|
175
|
+
elapsedMs: number,
|
|
176
|
+
stages: ComputerUseStageTimes,
|
|
177
|
+
steps: ComputerUseTimingRecord['steps'],
|
|
178
|
+
): string {
|
|
179
|
+
const totals = { ...stages }
|
|
180
|
+
for (const step of steps) {
|
|
181
|
+
for (const stage of Object.keys(step.stages) as ComputerUseTimingStage[]) {
|
|
182
|
+
totals[stage] = (totals[stage] ?? 0) + (step.stages[stage] ?? 0)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const labels = {
|
|
186
|
+
verify_reads: 'verify',
|
|
187
|
+
settle_wait: 'settle',
|
|
188
|
+
ui_snapshot: 'tree',
|
|
189
|
+
capture_encode: 'capture',
|
|
190
|
+
list_windows: 'windows',
|
|
191
|
+
input_delivery: 'input',
|
|
192
|
+
foreground_gate: 'foreground',
|
|
193
|
+
} satisfies Record<ComputerUseTimingStage, string>
|
|
194
|
+
const detail = (Object.keys(totals) as ComputerUseTimingStage[])
|
|
195
|
+
.sort((a, b) => (totals[b] ?? 0) - (totals[a] ?? 0))
|
|
196
|
+
.slice(0, 3)
|
|
197
|
+
.map((stage) => `${labels[stage]} ${((totals[stage] ?? 0) / 1000).toFixed(1)}`)
|
|
198
|
+
return `took ${(elapsedMs / 1000).toFixed(1)}s${detail.length ? ` (${detail.join(', ')})` : ''}`
|
|
199
|
+
}
|
|
200
|
+
|
|
156
201
|
/**
|
|
157
202
|
* How `createComputerUseTool` sizes screenshots, paces actions and bounds a
|
|
158
203
|
* batch. Every field is optional.
|
|
159
204
|
*/
|
|
160
205
|
export interface ComputerUseToolOptions {
|
|
206
|
+
/** Operator-only timing report. Defaults to on. */
|
|
207
|
+
readonly timings?: boolean | (() => boolean)
|
|
208
|
+
/** Receives one timing record after each call, including failures. */
|
|
209
|
+
readonly onTiming?: (
|
|
210
|
+
record: ComputerUseTimingRecord,
|
|
211
|
+
context: ToolContext,
|
|
212
|
+
) => void | Promise<void>
|
|
213
|
+
|
|
161
214
|
/**
|
|
162
215
|
* A second opinion on which control an intent names; see
|
|
163
216
|
* {@link ComputerUseAdvisor}. With one that `acts`, the tool offers `ui_do`
|
|
@@ -1868,6 +1921,40 @@ export function createComputerUseTool(
|
|
|
1868
1921
|
// passes it so an interrupted turn stops waiting for the desktop.
|
|
1869
1922
|
let callSignal: AbortSignal | undefined
|
|
1870
1923
|
|
|
1924
|
+
const timing = new AsyncLocalStorage<{
|
|
1925
|
+
stages: ComputerUseStageTimes
|
|
1926
|
+
steps: ComputerUseTimingRecord['steps'][number][]
|
|
1927
|
+
}>()
|
|
1928
|
+
const measure = async <T>(stage: ComputerUseTimingStage, run: () => Promise<T>): Promise<T> => {
|
|
1929
|
+
const store = timing.getStore()
|
|
1930
|
+
if (!store) return run()
|
|
1931
|
+
const start = performance.now()
|
|
1932
|
+
try {
|
|
1933
|
+
return await run()
|
|
1934
|
+
} finally {
|
|
1935
|
+
store.stages[stage] = (store.stages[stage] ?? 0) + performance.now() - start
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
const timeStep = async <T>(action: string, run: () => Promise<T>): Promise<T> => {
|
|
1939
|
+
const store = timing.getStore()
|
|
1940
|
+
if (!store) return run()
|
|
1941
|
+
const stages: ComputerUseStageTimes = {}
|
|
1942
|
+
const start = performance.now()
|
|
1943
|
+
try {
|
|
1944
|
+
return await timing.run({ stages, steps: store.steps }, run)
|
|
1945
|
+
} finally {
|
|
1946
|
+
store.steps.push({ action, elapsedMs: performance.now() - start, stages })
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
const settle = (window: string | undefined, signal?: AbortSignal) =>
|
|
1950
|
+
host.withTiming
|
|
1951
|
+
? settleAfterActions(host, window, settings.settleMs, signal, (wait) =>
|
|
1952
|
+
measure('settle_wait', wait),
|
|
1953
|
+
)
|
|
1954
|
+
: measure('settle_wait', () =>
|
|
1955
|
+
settleAfterActions(host, window, settings.settleMs, signal, (wait) => wait()),
|
|
1956
|
+
)
|
|
1957
|
+
|
|
1871
1958
|
/** Capture the display or one explicitly named window, fit it, and number it. */
|
|
1872
1959
|
const capture = async (windowId?: string): Promise<Capture> => {
|
|
1873
1960
|
if (windowId !== undefined) {
|
|
@@ -1879,7 +1966,7 @@ export function createComputerUseTool(
|
|
|
1879
1966
|
windowId,
|
|
1880
1967
|
callSignal,
|
|
1881
1968
|
)
|
|
1882
|
-
const image = await fitPng(shot.data, settings.limits())
|
|
1969
|
+
const image = await measure('capture_encode', () => fitPng(shot.data, settings.limits()))
|
|
1883
1970
|
if (image.sourceWidth !== shot.width || image.sourceHeight !== shot.height)
|
|
1884
1971
|
throw new StepFailure(
|
|
1885
1972
|
'the window PNG dimensions changed during capture; take a new screenshot',
|
|
@@ -1896,7 +1983,7 @@ export function createComputerUseTool(
|
|
|
1896
1983
|
if (result.type !== 'screenshot')
|
|
1897
1984
|
throw new Error(`computer_use: the host answered a screenshot with "${result.type}"`)
|
|
1898
1985
|
const shot = result.result
|
|
1899
|
-
const image = await fitPng(shot.data, settings.limits())
|
|
1986
|
+
const image = await measure('capture_encode', () => fitPng(shot.data, settings.limits()))
|
|
1900
1987
|
const display = shot.display ?? assumedDisplay(image.sourceWidth, image.sourceHeight)
|
|
1901
1988
|
const frame = frames.record(image, display)
|
|
1902
1989
|
noteDigest(frame, image)
|
|
@@ -2974,7 +3061,7 @@ export function createComputerUseTool(
|
|
|
2974
3061
|
fitted = await cropAndFitPng(frame.window.data, rect, settings.limits())
|
|
2975
3062
|
} else if (caps.regionCapture === true && typeof host.captureRegion === 'function') {
|
|
2976
3063
|
const piece = await host.captureRegion(rect, callSignal)
|
|
2977
|
-
fitted = await fitPng(piece.data, settings.limits())
|
|
3064
|
+
fitted = await measure('capture_encode', () => fitPng(piece.data, settings.limits()))
|
|
2978
3065
|
} else {
|
|
2979
3066
|
const result = await host.execute({ type: 'screenshot' }, callSignal)
|
|
2980
3067
|
if (result.type !== 'screenshot')
|
|
@@ -3134,6 +3221,26 @@ export function createComputerUseTool(
|
|
|
3134
3221
|
break
|
|
3135
3222
|
}
|
|
3136
3223
|
try {
|
|
3224
|
+
await timeStep(step.item.type, runStep)
|
|
3225
|
+
} catch (error) {
|
|
3226
|
+
const unknown = error instanceof StepFailure ? error.unknown : undefined
|
|
3227
|
+
const delivery = error instanceof StepFailure ? error.delivery : undefined
|
|
3228
|
+
const retrySafety = error instanceof StepFailure ? error.retrySafety : undefined
|
|
3229
|
+
const message = signal?.aborted ? 'cancelled' : errorText(error)
|
|
3230
|
+
failure = { index, error: message, ...(unknown ? { unknown } : {}) }
|
|
3231
|
+
records.push({
|
|
3232
|
+
label: step.label,
|
|
3233
|
+
status: 'failed',
|
|
3234
|
+
error: message,
|
|
3235
|
+
...(unknown ? { unknown } : {}),
|
|
3236
|
+
...(delivery ? { delivery } : {}),
|
|
3237
|
+
...(retrySafety ? { retrySafety } : {}),
|
|
3238
|
+
})
|
|
3239
|
+
if (delivery === 'delivered_unverified' || unknown) changed = true
|
|
3240
|
+
break
|
|
3241
|
+
}
|
|
3242
|
+
|
|
3243
|
+
async function runStep(): Promise<void> {
|
|
3137
3244
|
const keyboard = step.item.type === 'type_text' || step.item.type === 'key'
|
|
3138
3245
|
if (keyboard && !actionFrame?.window && !frontChecked) {
|
|
3139
3246
|
await refuseTerminalInFront()
|
|
@@ -3167,24 +3274,6 @@ export function createComputerUseTool(
|
|
|
3167
3274
|
postWindowId = actionFrame.window.window.id
|
|
3168
3275
|
records.push({ label: step.label, status: 'done', ...outcome })
|
|
3169
3276
|
if (step.mutating) changed = true
|
|
3170
|
-
} catch (error) {
|
|
3171
|
-
const unknown = error instanceof StepFailure ? error.unknown : undefined
|
|
3172
|
-
const delivery = error instanceof StepFailure ? error.delivery : undefined
|
|
3173
|
-
const retrySafety = error instanceof StepFailure ? error.retrySafety : undefined
|
|
3174
|
-
const message = signal?.aborted ? 'cancelled' : errorText(error)
|
|
3175
|
-
failure = { index, error: message, ...(unknown ? { unknown } : {}) }
|
|
3176
|
-
records.push({
|
|
3177
|
-
label: step.label,
|
|
3178
|
-
status: 'failed',
|
|
3179
|
-
error: message,
|
|
3180
|
-
...(unknown ? { unknown } : {}),
|
|
3181
|
-
...(delivery ? { delivery } : {}),
|
|
3182
|
-
...(retrySafety ? { retrySafety } : {}),
|
|
3183
|
-
})
|
|
3184
|
-
// A step that may have acted changed the desktop even though it failed.
|
|
3185
|
-
if (delivery === 'delivered_unverified') changed = true
|
|
3186
|
-
if (unknown) changed = true
|
|
3187
|
-
break
|
|
3188
3277
|
}
|
|
3189
3278
|
}
|
|
3190
3279
|
for (const step of planned.slice(records.length))
|
|
@@ -3197,7 +3286,7 @@ export function createComputerUseTool(
|
|
|
3197
3286
|
let frameShot: Capture | undefined
|
|
3198
3287
|
if (unsent.length > 0 && !failure && !signal?.aborted) {
|
|
3199
3288
|
try {
|
|
3200
|
-
if (changed) await
|
|
3289
|
+
if (changed) await settle(undefined, signal)
|
|
3201
3290
|
frameShot = await capture()
|
|
3202
3291
|
const before = single
|
|
3203
3292
|
? ''
|
|
@@ -3258,7 +3347,7 @@ export function createComputerUseTool(
|
|
|
3258
3347
|
}
|
|
3259
3348
|
if (treeEnding && postWindowId !== undefined) {
|
|
3260
3349
|
try {
|
|
3261
|
-
await
|
|
3350
|
+
await settle(postWindowId, signal)
|
|
3262
3351
|
const read = await uiSnapshot({
|
|
3263
3352
|
type: 'ui_snapshot',
|
|
3264
3353
|
window_id: postWindowId,
|
|
@@ -3288,7 +3377,7 @@ export function createComputerUseTool(
|
|
|
3288
3377
|
}
|
|
3289
3378
|
if (looks && treeText === undefined && windowClosed === undefined) {
|
|
3290
3379
|
try {
|
|
3291
|
-
await
|
|
3380
|
+
await settle(postWindowId, signal)
|
|
3292
3381
|
shot = await capture(postWindowId)
|
|
3293
3382
|
} catch (error) {
|
|
3294
3383
|
shotError = signal?.aborted ? 'cancelled' : errorText(error)
|
|
@@ -3798,13 +3887,15 @@ export function createComputerUseTool(
|
|
|
3798
3887
|
try {
|
|
3799
3888
|
switch (input.type) {
|
|
3800
3889
|
case 'screenshot':
|
|
3801
|
-
return await
|
|
3890
|
+
return await timeStep(input.type, async () =>
|
|
3891
|
+
firstLookWithTree(await capture(input.window_id), input.window_id),
|
|
3892
|
+
)
|
|
3802
3893
|
case 'zoom':
|
|
3803
|
-
return await zoom(input)
|
|
3894
|
+
return await timeStep(input.type, () => zoom(input))
|
|
3804
3895
|
case 'ui_snapshot':
|
|
3805
|
-
return await uiSnapshot(input)
|
|
3896
|
+
return await timeStep(input.type, () => uiSnapshot(input))
|
|
3806
3897
|
case 'wait_for':
|
|
3807
|
-
return await waitFor(input, context)
|
|
3898
|
+
return await timeStep(input.type, () => waitFor(input, context))
|
|
3808
3899
|
case 'batch':
|
|
3809
3900
|
return await runSteps(input.actions, input.screenshot_id, context, false)
|
|
3810
3901
|
default: {
|
|
@@ -3849,37 +3940,90 @@ export function createComputerUseTool(
|
|
|
3849
3940
|
presentResult: (_input, result) => {
|
|
3850
3941
|
// The first call to work on an application says so, so a person reading the
|
|
3851
3942
|
// conversation can see which applications this conversation has used.
|
|
3852
|
-
const
|
|
3943
|
+
const data = isRecord(result.data) ? result.data : undefined
|
|
3944
|
+
const first = data?.firstUseOf
|
|
3945
|
+
const tail = typeof data?.timingTail === 'string' ? data.timingTail : undefined
|
|
3853
3946
|
if (result.success && typeof first === 'string')
|
|
3854
3947
|
return {
|
|
3855
3948
|
kind: 'generic',
|
|
3856
|
-
label: `Using ${applicationName(first)}`,
|
|
3949
|
+
label: `Using ${applicationName(first)}${tail ? ` · ${tail}` : ''}`,
|
|
3857
3950
|
presentation: 'activity',
|
|
3858
3951
|
}
|
|
3952
|
+
if (tail)
|
|
3953
|
+
return {
|
|
3954
|
+
kind: 'terminal',
|
|
3955
|
+
output: [result.success ? result.output : (result.error ?? result.output), tail]
|
|
3956
|
+
.filter(Boolean)
|
|
3957
|
+
.join('\n'),
|
|
3958
|
+
}
|
|
3859
3959
|
return result.success && ACKNOWLEDGEMENT.test(result.output)
|
|
3860
3960
|
? { kind: 'generic', label: result.output, visibility: 'hidden' }
|
|
3861
3961
|
: undefined
|
|
3862
3962
|
},
|
|
3863
3963
|
|
|
3864
3964
|
async execute(input, context): Promise<ToolResult> {
|
|
3865
|
-
const
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
const
|
|
3870
|
-
const
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3965
|
+
const show =
|
|
3966
|
+
typeof options.timings === 'function' ? options.timings() : options.timings !== false
|
|
3967
|
+
if (!options.onTiming && (!show || !host.withTiming)) return execute()
|
|
3968
|
+
const stages: ComputerUseStageTimes = {}
|
|
3969
|
+
const steps: ComputerUseTimingRecord['steps'][number][] = []
|
|
3970
|
+
const start = performance.now()
|
|
3971
|
+
let elapsedMs: number
|
|
3972
|
+
let result: ToolResult
|
|
3973
|
+
try {
|
|
3974
|
+
result = await timing.run(
|
|
3975
|
+
{ stages, steps },
|
|
3976
|
+
() =>
|
|
3977
|
+
host.withTiming?.((stage, ms) => {
|
|
3978
|
+
const current = timing.getStore()?.stages
|
|
3979
|
+
if (current) current[stage] = (current[stage] ?? 0) + ms
|
|
3980
|
+
}, execute) ?? execute(),
|
|
3981
|
+
)
|
|
3982
|
+
} finally {
|
|
3983
|
+
elapsedMs = performance.now() - start
|
|
3984
|
+
const record = { app: appKnown(input), elapsedMs, stages, steps }
|
|
3985
|
+
try {
|
|
3986
|
+
await options.onTiming?.(
|
|
3987
|
+
{
|
|
3988
|
+
...(record.app ? { app: record.app } : {}),
|
|
3989
|
+
elapsedMs,
|
|
3990
|
+
stages,
|
|
3991
|
+
steps,
|
|
3992
|
+
},
|
|
3993
|
+
context,
|
|
3994
|
+
)
|
|
3995
|
+
} catch {
|
|
3996
|
+
// Diagnostics must never change an input's outcome.
|
|
3997
|
+
}
|
|
3998
|
+
}
|
|
3999
|
+
if (show) {
|
|
3876
4000
|
result.data = {
|
|
3877
4001
|
...(isRecord(result.data) ? result.data : {}),
|
|
3878
|
-
|
|
4002
|
+
timingTail: timingTail(elapsedMs, stages, steps),
|
|
3879
4003
|
}
|
|
3880
4004
|
}
|
|
3881
|
-
await observe(input, before, result, context.toolUseId, app)
|
|
3882
4005
|
return result
|
|
4006
|
+
|
|
4007
|
+
async function execute(): Promise<ToolResult> {
|
|
4008
|
+
const keptOut = await deniedAppRefusal(input, context)
|
|
4009
|
+
if (keptOut) return { success: false, output: '', error: keptOut }
|
|
4010
|
+
const tiered = await appTierRefusal(input, context)
|
|
4011
|
+
if (tiered) return { success: false, output: '', error: tiered }
|
|
4012
|
+
const shotId = 'screenshot_id' in input ? input.screenshot_id : undefined
|
|
4013
|
+
const before = shotId ? frames.get(shotId) : frames.latest()
|
|
4014
|
+
// Only what is already known: acting must not cost the host a call it would not make.
|
|
4015
|
+
const app = appKnown(input)
|
|
4016
|
+
const result = await runAction(input, context)
|
|
4017
|
+
if (app && result.success !== false && !appsUsed.has(app) && !isReadOnlyInput(input)) {
|
|
4018
|
+
appsUsed.add(app)
|
|
4019
|
+
result.data = {
|
|
4020
|
+
...(isRecord(result.data) ? result.data : {}),
|
|
4021
|
+
firstUseOf: app,
|
|
4022
|
+
}
|
|
4023
|
+
}
|
|
4024
|
+
await observe(input, before, result, context.toolUseId, app)
|
|
4025
|
+
return result
|
|
4026
|
+
}
|
|
3883
4027
|
},
|
|
3884
4028
|
})
|
|
3885
4029
|
const browserQuestionPending = (app: string): boolean =>
|
|
@@ -363,25 +363,29 @@ function nearLine(content: string, needle: string): number | undefined {
|
|
|
363
363
|
return undefined
|
|
364
364
|
}
|
|
365
365
|
|
|
366
|
+
function insertionLineEnding(content: string): '\r\n' | '\n' {
|
|
367
|
+
return content.includes('\r\n') ? '\r\n' : '\n'
|
|
368
|
+
}
|
|
369
|
+
|
|
366
370
|
function applyLineInsert(
|
|
367
371
|
content: string,
|
|
368
372
|
input: Extract<NormalizedEditInput, { operation: 'insert' }>,
|
|
369
373
|
): { success: true; content: string; replacements: number } {
|
|
374
|
+
const ending = insertionLineEnding(content)
|
|
370
375
|
const hasTrailingNewline = content.endsWith('\n')
|
|
371
|
-
const lines = content.split(
|
|
376
|
+
const lines = content === '' ? [] : content.split(/\r?\n/)
|
|
372
377
|
if (hasTrailingNewline) lines.pop()
|
|
373
378
|
|
|
374
379
|
const line =
|
|
375
380
|
input.insertLine === 'end'
|
|
376
381
|
? lines.length
|
|
377
382
|
: Math.min(Math.max(input.insertLine, 0), lines.length)
|
|
378
|
-
const inserted = input.newString.
|
|
379
|
-
|
|
380
|
-
: input.newString.split('\n')
|
|
383
|
+
const inserted = input.newString.split(/\r?\n/)
|
|
384
|
+
if (input.newString.endsWith('\n')) inserted.pop()
|
|
381
385
|
lines.splice(line, 0, ...inserted)
|
|
382
386
|
return {
|
|
383
387
|
success: true,
|
|
384
|
-
content: `${lines.join(
|
|
388
|
+
content: `${lines.join(ending)}${hasTrailingNewline ? ending : ''}`,
|
|
385
389
|
replacements: 1,
|
|
386
390
|
}
|
|
387
391
|
}
|
|
@@ -568,12 +572,15 @@ export function replayOperationsWithin(
|
|
|
568
572
|
*/
|
|
569
573
|
function predictOne(content: string, operation: NormalizedEditInput, allowance: number): number {
|
|
570
574
|
if (operation.operation === 'insert') {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
//
|
|
574
|
-
//
|
|
575
|
+
const ending = insertionLineEnding(content)
|
|
576
|
+
const { newString } = operation
|
|
577
|
+
// Existing and inserted breaks are normalized before joining. A nonempty
|
|
578
|
+
// file contributes one join boundary, while the insert's final break is removed.
|
|
575
579
|
return (
|
|
576
|
-
content
|
|
580
|
+
normalizedLineEndingLength(content, ending.length) +
|
|
581
|
+
normalizedLineEndingLength(newString, ending.length) +
|
|
582
|
+
(content === '' ? 0 : ending.length) -
|
|
583
|
+
(newString.endsWith('\n') ? ending.length : 0)
|
|
577
584
|
)
|
|
578
585
|
}
|
|
579
586
|
const { oldString, newString, replace_all } = normalizeLineEndings(content, operation)
|
|
@@ -591,6 +598,14 @@ function predictOne(content: string, operation: NormalizedEditInput, allowance:
|
|
|
591
598
|
return Math.max(content.length + countOccurrences(content, oldString, cap) * growth, 0)
|
|
592
599
|
}
|
|
593
600
|
|
|
601
|
+
function normalizedLineEndingLength(text: string, endingLength: number): number {
|
|
602
|
+
let length = text.length
|
|
603
|
+
for (let index = text.indexOf('\n'); index !== -1; index = text.indexOf('\n', index + 1)) {
|
|
604
|
+
length += endingLength - (text[index - 1] === '\r' ? 2 : 1)
|
|
605
|
+
}
|
|
606
|
+
return length
|
|
607
|
+
}
|
|
608
|
+
|
|
594
609
|
/** Non-overlapping matches, up to `cap`, counted the way `split` counts them. */
|
|
595
610
|
function countOccurrences(content: string, needle: string, cap: number): number {
|
|
596
611
|
// `split('')` yields one part per character; scanning for it would never
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises'
|
|
2
|
-
|
|
3
1
|
import { z } from 'zod'
|
|
4
2
|
import type { ToolContext, ToolResult } from '../../types/tool/index.js'
|
|
5
3
|
import type { ToolFacts } from '../../types/tool/presentation.js'
|
|
6
4
|
import { defineTool } from '../defineTool.js'
|
|
7
|
-
import { resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
5
|
+
import { resolveAllowedFile, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
6
|
+
import { refuseSandboxHomePath } from '../posix-path.js'
|
|
8
7
|
import { atomicWriteFile } from './atomic-write-file.js'
|
|
9
8
|
import { clip, describeChange } from './classifier-input.js'
|
|
10
9
|
import {
|
|
@@ -21,9 +20,11 @@ import {
|
|
|
21
20
|
normalizeEditInput,
|
|
22
21
|
} from './edit-apply.js'
|
|
23
22
|
import { withFileMutationLock } from './file-mutation-lock.js'
|
|
23
|
+
import { decodeFileText, unreadableFileText } from './file-text.js'
|
|
24
24
|
import { jsonStringEscapes } from './json-string-hint.js'
|
|
25
25
|
import { lineDiff } from './line-diff.js'
|
|
26
26
|
import { notebookRefusal } from './notebook-guard.js'
|
|
27
|
+
import { readRegularFileBytes } from './read-regular-file.js'
|
|
27
28
|
import { staleBeforeReview } from './stale-before-review.js'
|
|
28
29
|
|
|
29
30
|
type RawEditInput = {
|
|
@@ -550,12 +551,17 @@ async function refuseEditBeforeReview(
|
|
|
550
551
|
// unread-file refusals are given when the call runs there, so a guest that is
|
|
551
552
|
// not admitted yet, or that holds a file the host has never seen, is not
|
|
552
553
|
// judged from a read made before anyone approved the call.
|
|
553
|
-
if (context.sandbox) return staleBeforeReview(context, parsed.data.path, 'edit')
|
|
554
554
|
let key: string
|
|
555
555
|
let content: string
|
|
556
556
|
try {
|
|
557
|
-
|
|
558
|
-
|
|
557
|
+
const allowed = await resolveAllowedFile(context, parsed.data.path)
|
|
558
|
+
if (context.sandbox && allowed === undefined)
|
|
559
|
+
return staleBeforeReview(context, parsed.data.path, 'edit')
|
|
560
|
+
key = allowed ?? (await resolveWithinAnyReal(toolRoots(context), parsed.data.path))
|
|
561
|
+
const bytes = await readRegularFileBytes(key, 'Edit')
|
|
562
|
+
const decoded = decodeFileText(bytes)
|
|
563
|
+
if (decoded === undefined) return { error: unreadableFileText(bytes.length) }
|
|
564
|
+
content = decoded
|
|
559
565
|
} catch {
|
|
560
566
|
return undefined
|
|
561
567
|
}
|
|
@@ -703,25 +709,31 @@ export const EditTool = defineTool({
|
|
|
703
709
|
}
|
|
704
710
|
}
|
|
705
711
|
|
|
712
|
+
const allowed = await resolveAllowedFile(context, parsed.data.path)
|
|
713
|
+
const sandbox = allowed === undefined ? context.sandbox : undefined
|
|
706
714
|
// Host-side containment, on the host branch only. The sandbox has its
|
|
707
715
|
// own root and its own resolver; canonicalizing a sandbox-relative
|
|
708
716
|
// path against the HOST filesystem asks a question about the wrong
|
|
709
717
|
// machine, and answers it with whatever happens to exist there.
|
|
710
|
-
const filePath =
|
|
711
|
-
|
|
712
|
-
: await resolveWithinAnyReal(toolRoots(context), parsed.data.path)
|
|
718
|
+
const filePath =
|
|
719
|
+
allowed ??
|
|
720
|
+
(sandbox ? undefined : await resolveWithinAnyReal(toolRoots(context), parsed.data.path))
|
|
713
721
|
// Read-modify-write is not atomic on its own: two edits to the same
|
|
714
722
|
// path interleave their reads, and the second write lands on content
|
|
715
723
|
// the first had already replaced — so one edit vanishes and the loser
|
|
716
724
|
// reports "old_string not found", blaming the model for a race. The
|
|
717
725
|
// key spans both branches because sandbox and local are distinct
|
|
718
726
|
// files even when the path string matches.
|
|
719
|
-
const lockKey =
|
|
727
|
+
const lockKey = sandbox ? `sandbox:${parsed.data.path}` : `local:${filePath as string}`
|
|
720
728
|
|
|
721
729
|
return withFileMutationLock(lockKey, async () => {
|
|
722
|
-
if (
|
|
723
|
-
|
|
724
|
-
const
|
|
730
|
+
if (sandbox) {
|
|
731
|
+
refuseSandboxHomePath(parsed.data.path)
|
|
732
|
+
const buffer = await sandbox.readFile(parsed.data.path)
|
|
733
|
+
const content = decodeFileText(buffer)
|
|
734
|
+
if (content === undefined) {
|
|
735
|
+
return { success: false as const, output: '', error: unreadableFileText(buffer.length) }
|
|
736
|
+
}
|
|
725
737
|
const admitted = admitAgainstRead(context, parsed.data.path, content, normalized.operations)
|
|
726
738
|
if (!admitted.success) return admitted
|
|
727
739
|
const result = admitted.result
|
|
@@ -729,7 +741,7 @@ export const EditTool = defineTool({
|
|
|
729
741
|
const notebook = notebookRefusal(parsed.data.path, result.content, content)
|
|
730
742
|
if (notebook) return { success: false as const, output: '', error: notebook }
|
|
731
743
|
|
|
732
|
-
await
|
|
744
|
+
await sandbox.writeFile(parsed.data.path, result.content)
|
|
733
745
|
recordEditedContent(context, parsed.data.path, result.content)
|
|
734
746
|
const changed = editFacts(parsed.data.path, result.replacements, content, result.content)
|
|
735
747
|
return {
|
|
@@ -745,7 +757,11 @@ export const EditTool = defineTool({
|
|
|
745
757
|
}
|
|
746
758
|
|
|
747
759
|
const hostPath = filePath as string
|
|
748
|
-
const
|
|
760
|
+
const buffer = await readRegularFileBytes(hostPath, 'Edit')
|
|
761
|
+
const content = decodeFileText(buffer)
|
|
762
|
+
if (content === undefined) {
|
|
763
|
+
return { success: false as const, output: '', error: unreadableFileText(buffer.length) }
|
|
764
|
+
}
|
|
749
765
|
|
|
750
766
|
const admitted = admitAgainstRead(context, hostPath, content, normalized.operations)
|
|
751
767
|
if (!admitted.success) return admitted
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { humanBytes } from './read-render.js'
|
|
2
|
+
|
|
3
|
+
// Keep a leading BOM in the body so read-modify-write tools can round-trip it.
|
|
4
|
+
export function decodeFileText(bytes: Buffer): string | undefined {
|
|
5
|
+
try {
|
|
6
|
+
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes)
|
|
7
|
+
} catch {
|
|
8
|
+
return undefined
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function unreadableFileText(bytes: number): string {
|
|
13
|
+
return `This file is not readable as text (${humanBytes(bytes)} of binary or non-UTF-8 data). Inspect it with a shell command such as file or xxd, or use a tool made for its format.`
|
|
14
|
+
}
|
|
@@ -5,7 +5,7 @@ import { walkFilesLocally } from '../../sandbox/file-walk.js'
|
|
|
5
5
|
import type { SandboxWalkFilesOptions, SandboxWalkSummary } from '../../types/sandbox/index.js'
|
|
6
6
|
import { defineTool } from '../defineTool.js'
|
|
7
7
|
import { resolveWithin, resolveWithinAny, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
8
|
-
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
8
|
+
import { refuseSandboxHomePath, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
9
9
|
import {
|
|
10
10
|
SEARCH_BUDGET_MS,
|
|
11
11
|
SEARCH_TOOL_TIMEOUT_MS,
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
ignoredHint,
|
|
14
14
|
isEntryLimit,
|
|
15
15
|
isReadDenied,
|
|
16
|
+
missingSearchRoot,
|
|
16
17
|
openSearchBudget,
|
|
17
18
|
plural,
|
|
18
19
|
quoteControlPath,
|
|
@@ -106,10 +107,14 @@ export const GlobTool = defineTool({
|
|
|
106
107
|
async execute(input, context) {
|
|
107
108
|
context.abortSignal?.throwIfAborted()
|
|
108
109
|
const sandbox = context.sandbox
|
|
110
|
+
if (sandbox) refuseSandboxHomePath(input.path)
|
|
109
111
|
const lexicalRoot = sandbox
|
|
110
112
|
? resolveWithinPosix(sandbox.rootDir, input.path)
|
|
111
113
|
: resolveWithinAny(toolRoots(context), input.path)
|
|
112
114
|
const root = sandbox ? lexicalRoot : await resolveWithinAnyReal(toolRoots(context), input.path)
|
|
115
|
+
if (!sandbox && (await missingSearchRoot(root))) {
|
|
116
|
+
return { success: false, output: '', error: `search path does not exist: ${lexicalRoot}` }
|
|
117
|
+
}
|
|
113
118
|
const pattern = relativePattern(input.pattern, lexicalRoot, sandbox !== undefined)
|
|
114
119
|
const walkSandbox = sandbox?.walkFiles?.bind(sandbox)
|
|
115
120
|
if (sandbox && !walkSandbox) {
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
resolveWithinReal,
|
|
15
15
|
toolRoots,
|
|
16
16
|
} from '../paths.js'
|
|
17
|
-
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
17
|
+
import { refuseSandboxHomePath, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
18
18
|
import {
|
|
19
19
|
CAN_REFUSE_LINKS,
|
|
20
20
|
REFUSE_LINK_FLAG,
|
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
ignoredHint,
|
|
31
31
|
isEntryLimit,
|
|
32
32
|
isReadDenied,
|
|
33
|
+
missingSearchRoot,
|
|
33
34
|
openSearchBudget,
|
|
34
35
|
plural,
|
|
35
36
|
readAhead,
|
|
@@ -388,10 +389,14 @@ export const GrepTool = defineTool({
|
|
|
388
389
|
}
|
|
389
390
|
|
|
390
391
|
const sandbox = context.sandbox
|
|
392
|
+
if (sandbox) refuseSandboxHomePath(input.path)
|
|
391
393
|
const lexicalRoot = sandbox
|
|
392
394
|
? resolveWithinPosix(sandbox.rootDir, input.path)
|
|
393
395
|
: resolveWithinAny(toolRoots(context), input.path)
|
|
394
396
|
const root = sandbox ? lexicalRoot : await resolveWithinAnyReal(toolRoots(context), input.path)
|
|
397
|
+
if (!sandbox && (await missingSearchRoot(root))) {
|
|
398
|
+
return { success: false, output: '', error: `search path does not exist: ${lexicalRoot}` }
|
|
399
|
+
}
|
|
395
400
|
const filePattern = relativeInclude(input.include ?? '**/*', lexicalRoot, sandbox !== undefined)
|
|
396
401
|
const walkSandbox = sandbox?.walkFiles?.bind(sandbox)
|
|
397
402
|
if (sandbox && !walkSandbox) {
|
|
@@ -830,10 +835,7 @@ function multilineHits(content: string, regex: RegExp): [number, number][] {
|
|
|
830
835
|
regex.lastIndex = 0
|
|
831
836
|
for (let m = regex.exec(content); m !== null; m = regex.exec(content)) {
|
|
832
837
|
const text = m[0]
|
|
833
|
-
if (text.length === 0)
|
|
834
|
-
regex.lastIndex++
|
|
835
|
-
continue
|
|
836
|
-
}
|
|
838
|
+
if (text.length === 0) regex.lastIndex++
|
|
837
839
|
// A match that ends on a newline covers the lines before it, not the next one.
|
|
838
840
|
const end = m.index + text.length - (text.endsWith('\n') ? 1 : 0)
|
|
839
841
|
hits.push([lineOf(m.index), lineOf(Math.max(m.index, end))])
|
package/src/tools/builtins/ls.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { Sandbox } from '../../types/sandbox/index.js'
|
|
|
5
5
|
import type { ToolResult } from '../../types/tool/index.js'
|
|
6
6
|
import { defineTool } from '../defineTool.js'
|
|
7
7
|
import { resolveWithinAny, toolRoots } from '../paths.js'
|
|
8
|
-
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
8
|
+
import { refuseSandboxHomePath, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
9
9
|
|
|
10
10
|
const inputSchema = z.object({
|
|
11
11
|
path: z.string().default('.').describe('Directory path to list. Defaults to working directory.'),
|
|
@@ -134,6 +134,7 @@ async function listInSandbox(
|
|
|
134
134
|
input: { path: string; all: boolean; recursive: boolean; max_depth: number },
|
|
135
135
|
sandbox: Sandbox,
|
|
136
136
|
): Promise<ToolResult> {
|
|
137
|
+
refuseSandboxHomePath(input.path)
|
|
137
138
|
const root = resolveWithinPosix(sandbox.rootDir, input.path)
|
|
138
139
|
const entries = await sandbox.listFiles(root)
|
|
139
140
|
|