Repository navigation
computer: Keep exec in a read-only tool set on backends that only read #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
scuffi
wants to merge
2
commits into
main
Choose a base branch
from
computer/readonly-exec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@cloudflare/computer": patch | ||
| --- | ||
|
|
||
| A `readonly: true` tool set offers `exec` on backends that can't change the Workspace, such as a `CodemodeBackend` with `access: "read"`, which `runtime.backends()` now reports as `access`; see [`exec`](https://github.com/cloudflare/computer/blob/main/docs/09_tool_interface.md#exec). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,7 +69,7 @@ export interface ExecWorkspaceLike { | |
| env?: Record<string, string>; | ||
| input?: WorkspaceRuntimeValue; | ||
| metadata?: Record<string, string>; | ||
| output?: { maxBytes: number; maxLines: number }; | ||
| output?: { maxBytes: number; maxLines: number } | false; | ||
| }, | ||
| ): Promise<ExecRuntimeHandle>; | ||
| // Calls waiting in ctx.interrupt(), by execution id, and noting | ||
|
|
@@ -156,6 +156,13 @@ export interface ExecToolOptions { | |
| * `onDetachedExit`. | ||
| */ | ||
| detach?: ExecDetachOptions; | ||
| /** | ||
| * Save output too long to show, and return values too large to show, | ||
| * to files in the Workspace, which the reply names. Defaults to | ||
| * `true`. With `false` the model sees them cut, and nothing is | ||
| * written: a read-only tool set turns it off. | ||
| */ | ||
| saveOutput?: boolean; | ||
|
Comment on lines
+159
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔍 Read-only example still saves output The RLM executor uses a read-only backend but leaves Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| // Clock backing the running-snapshot coalescing floor. Defaults to | ||
| // Date.now; injectable so tests can drive the interval deterministically. | ||
| now?: () => number; | ||
|
|
@@ -238,6 +245,7 @@ export function defineExec(options: ExecToolOptions): ExecDefinition { | |
| "createExecTool", | ||
| ); | ||
| const now = options.now ?? Date.now; | ||
| const saveOutput = options.saveOutput ?? true; | ||
| const runtime = options.workspace.runtime; | ||
| const detach = options.detach; | ||
| const afterMs = typeof detach === "object" ? detach.afterMs : undefined; | ||
|
|
@@ -279,7 +287,7 @@ export function defineExec(options: ExecToolOptions): ExecDefinition { | |
| } | ||
| } | ||
| const description = [ | ||
| describeTool(backends, limits), | ||
| describeTool(backends, limits, saveOutput), | ||
| detach === undefined | ||
| ? "" | ||
| : detachHint(afterMs, detachable.size === backends.length ? undefined : [...detachable]), | ||
|
|
@@ -424,7 +432,7 @@ export function defineExec(options: ExecToolOptions): ExecDefinition { | |
| ...(metadata === undefined ? {} : { metadata }), | ||
| // The runtime cuts and saves by the same limits the tool | ||
| // shows, so whatever the model does not see is in the file. | ||
| output: limits, | ||
| output: saveOutput ? limits : false, | ||
| }); | ||
| } catch (err) { | ||
| yield { ...base, error: errorMessage(err) }; | ||
|
|
@@ -467,6 +475,9 @@ export function defineExec(options: ExecToolOptions): ExecDefinition { | |
| const preview = decoder.decode(bytes.slice(0, Math.min(limits.maxBytes, 8192))); | ||
| const shown = { truncated: true, totalBytes: bytes.length, preview }; | ||
| const runtime = options.workspace.runtime; | ||
| if (!saveOutput) { | ||
| return { ...shown, note: "Full result was not saved: this tool doesn't write files." }; | ||
| } | ||
| if (runtime.saveResult === undefined) { | ||
| return { ...shown, note: "Full result was not saved: the workspace cannot save output." }; | ||
| } | ||
|
|
@@ -651,12 +662,19 @@ function detachHint( | |
| const FILE_TOOLS_HINT = "Prefer the dedicated read, write, and edit tools for file operations."; | ||
|
|
||
| // How the reply cuts long output, and where the rest goes. | ||
| function outputHint(limits: OutputLimits): string { | ||
| return `Each of stdout and stderr is cut to its last ${limits.maxLines} lines or ${formatSize(limits.maxBytes)}, whichever is hit first; when it is, the full output is saved to a file the reply names, which the read and grep tools can open.`; | ||
| function outputHint(limits: OutputLimits, saved: boolean): string { | ||
| const cut = `Each of stdout and stderr is cut to its last ${limits.maxLines} lines or ${formatSize(limits.maxBytes)}, whichever is hit first`; | ||
| return saved | ||
| ? `${cut}; when it is, the full output is saved to a file the reply names, which the read and grep tools can open.` | ||
| : `${cut}, and the rest isn't kept, so print only what you need.`; | ||
| } | ||
| const SHELL_HINT = "Use for builds, test runs, typechecks, formatters, and git plumbing."; | ||
| const CALLABLE_HINT = | ||
| "Pass an `input` object to hand the module structured values, and read its return value back from the `result` field. A return value over the output limit is saved to a file and `result` holds its path and a preview, so write large data to a file instead of returning it."; | ||
| const callableHint = (saved: boolean) => | ||
| `Pass an \`input\` object to hand the module structured values, and read its return value back from the \`result\` field. ${ | ||
| saved | ||
| ? "A return value over the output limit is saved to a file and `result` holds its path and a preview, so write large data to a file instead of returning it." | ||
| : "A return value over the output limit comes back as a preview only, so return only what you need." | ||
| }`; | ||
|
|
||
| interface DescribedBackend { | ||
| readonly id: string; | ||
|
|
@@ -666,8 +684,12 @@ interface DescribedBackend { | |
|
|
||
| // With one backend the description is about what it does. With several | ||
| // it lists them and explains how to choose. | ||
| function describeTool(backends: readonly DescribedBackend[], limits: OutputLimits): string { | ||
| const output = outputHint(limits); | ||
| function describeTool( | ||
| backends: readonly DescribedBackend[], | ||
| limits: OutputLimits, | ||
| saved: boolean, | ||
| ): string { | ||
| const output = outputHint(limits, saved); | ||
| const [only, ...others] = backends; | ||
| if (only !== undefined && others.length === 0) { | ||
| return only.callable | ||
|
|
@@ -676,7 +698,7 @@ function describeTool(backends: readonly DescribedBackend[], limits: OutputLimit | |
| "", | ||
| only.text, | ||
| "", | ||
| CALLABLE_HINT, | ||
| callableHint(saved), | ||
| `${FILE_TOOLS_HINT} ${output}`, | ||
| ].join("\n") | ||
| : [ | ||
|
|
@@ -703,7 +725,7 @@ function describeTool(backends: readonly DescribedBackend[], limits: OutputLimit | |
| ? [] | ||
| : [ | ||
| "", | ||
| `Callable backends (${callable.join(", ")}) run \`command\` as module source rather than a shell command. ${CALLABLE_HINT} Other backends reject \`input\`.`, | ||
| `Callable backends (${callable.join(", ")}) run \`command\` as module source rather than a shell command. ${callableHint(saved)} Other backends reject \`input\`.`, | ||
| ]), | ||
| ].join("\n"); | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.