Repository navigation
Conversation
|
Credit where due: the engineering here is careful. That said, this is a ~900-line addition that introduces a new trust boundary into the core Concretely I'd want to see this split: (1) the SDK-level hook primitive and its tests on its own, with a design discussion about whether |
fe3fc8c to
20ded3f
Compare
|
Restructured per this review thanks, the split is the right call. This branch is now the SDK primitive alone: one commit, 5 files (hook stage, its The PR body is now the design discussion you asked for. The option worth your attention is C: On whether the extension point should exist at all: I think yes, on the terms stated in the body — host-supplied only, never repo-discovered, bounded, and structurally unable to block or fail a tool. Happy to drop it if the product call goes the other way. |
20ded3f to
867781b
Compare
A host can now give CodebuffClient a terminalCommandHook: the user's own
command, run before each Bash tool call, which may rewrite the command the
model wrote. The stage sits in run_terminal_command before anything is
spawned, so a rewrite reaches every Bash call instead of depending on the
model prefixing commands.
It speaks Claude Code's PreToolUse rewrite contract on purpose -- the hook
gets {"tool_name":"Bash","tool_input":{"command":...},"cwd":...} on stdin
and answers with hookSpecificOutput.updatedInput.command -- so a hook
already written for that ecosystem works unchanged. Only that field counts:
a permission decision or any other answer leaves the command alone.
A hook can never break the tool. A non-zero exit, malformed JSON, an
unusable rewrite, a refused spawn, an aborted run, or the hard timeout
(2000 ms by default, capped at 10000 ms) runs the command as the model
wrote it. FREEBUFF_DISABLE_BASH_HOOK=1 skips the hook for every command.
Hook stderr is bounded, goes only to the host's diagnostics through
onHookStderr, and never enters the tool result. A rewrite makes the result
carry originalCommand beside command, so what actually ran stays visible.
The SDK never reads a hooks file itself: the host supplies the hook, so
nothing in a cloned repository can turn one on.
Co-Authored-By: Codebuff <noreply@codebuff.com>
867781b to
afb3838
Compare
|
Reduced to the smallest shape that still does the job:
The design question is unchanged and still open: A ( |
Scope changed in response to the review below. This PR is now the SDK primitive alone: one commit, 4 files, 436 insertions, 1 deletion — and no existing code is refactored. The hook is a new module; the only edit to a running code path is one line at the single Bash call site. The CLI settings/wiring/transcript work and the docs are parked on a stacked branch, not deleted (see What moved out).
Refs #1760 (deliberately not
Closes: the user-facing opt-in still needs the CLI half).What this adds
A host can hand
CodebuffClientaterminalCommandHook: the user's own command, run before a Bash tool call, which may return a replacement command. It runs at the one place the SDK dispatchesrun_terminal_command, before anything is spawned, so a rewrite reaches every Bash call instead of depending on the model remembering to prefix one.The wire contract is Claude Code's
PreToolUserewrite contract, adopted rather than invented:Only
hookSpecificOutput.updatedInput.commandis honored. A permission decision, another field, a non-zero exit, malformed JSON or no answer leaves the command exactly as the model wrote it. Reusing that contract means a hook already written for that ecosystem works here unchanged, and nothing in the stage is specific to one wrapper.The design question
Four placements were available. This PR takes A; I would like a verdict before anything else is built on top.
A.
CodebuffClientOptions.terminalCommandHook— this PR. The hook is host policy for the whole client, sitting next toterminalCommandBroker, which is already a client-level "how this host runs commands" option. Cost: client-wide lifetime, so a host that wanted a different hook per agent turn could not express that.B.
RunOptions.hooks— arguably better scoping, since a hook is a property of a session or turn rather than of a client. Cheap to move: the value threads throughrunOnce→handleToolCall, so relocating it is roughly a ten-line change. If you would rather scope it per run, say so and I will switch it before merge.C. No SDK change — do it in
overrideTools.run_terminal_command. This is the option actually worth arguing about, because the override dispatch inhandleToolCallcan interceptrun_terminal_commandtoday, so a host could already do something like this. It is not sufficient in practice:nulredirect rewriting, output bounding and the broker path — the several hundred lines that are hardest to get right — in order to add six lines of policy.cli/src/utils/codebuff-client.tssetsterminalCommandBroker. That broker is used insiderunTerminalCommand(ownsShell, PTY, tree cleanup). An override returns its ownToolResultOutput[]and never reaches that path, so a hook written as an override would also drop the host's process boundary for every command — the opposite of what the CLI wants.So the hook has to sit inside the existing terminal pipeline. A is the recommendation; B is a reasonable narrower alternative I will take if preferred.
D. A
hooksmap ({ 'bash.before-execute': ... }) instead of a single field — reasonable eventual shape, but with exactly one hook site it is speculative generality. Add the second entry point first, generalize then.On the broader question: I think the extension point is worth having on these terms, all enforced here — off unless the host supplies it, never discovered from a repository file, hard-bounded so it cannot stall a tool call, and unable to block or fail a tool.
Safety properties
onStderrcallback for the host's log, and never enters the tool result or model context.FREEBUFF_DISABLE_BASH_HOOK=1skips it for every command.The hook is the user's own program with the user's privileges, so it runs with the run's environment as-is; the credential scrubbing that applies to model-written commands is not extended to it.
Size
tools/terminal-command-hook.ts— new file, self-contained: the type, three constants, the timeout clamp, the output parser, andapplyTerminalCommandHook.run.ts— 29 lines and the single changed one: option onCodebuffClientOptions, threaded through the two existing hops, and applied at therun_terminal_commanddispatch site.index.ts— 10 lines of exports.runTerminalCommandand its output schema are untouched, so nothing about command execution, the broker, backgrounding or existing consumers changes. Earlier drafts wrapped the spawn path and added anoriginalCommandfield to the shared output schema; both are gone, and rewrite observability now travels with the CLI half, where it is actually rendered.What moved out
The CLI half —
bashCommandHookinsettings.json,loadBashCommandHook, client wiring, transcript rendering and the docs section — is preserved onyki/bash-command-hook-cli, stacked on this branch and pushed to my fork. It is not part of this PR; I will open it separately once this primitive is accepted, and I am happy to change its settings shape or the rendered transcript note to whatever you prefer in the meantime.Verification
Covers: a rewrite is returned and the payload arrives on stdin with the absolute cwd; non-zero exit leaves the command alone; non-answer output leaves it alone; a timeout leaves it alone and does not wait for the hook; an aborted run never starts the hook;
FREEBUFF_DISABLE_BASH_HOOK=1skips it entirely; hook stderr reaches the host's log but never the result;parseTerminalCommandHookOutputhonors onlyupdatedInput.command; the timeout clamp.tsc --noEmit -p sdk/tsconfig.json— clean.eslinton the changed files — 0 errors (pre-existing warnings only).Not verified here: a live authenticated CLI agent turn, since that needs the CLI half.
Open questions
terminalCommandHookfield, or ahooksmap later.