Skip to content

Add an experimental bash tool - #2185

Draft
fazxes wants to merge 20 commits into
mainfrom
fazxes/add-bash-tool
Draft

fazxes wants to merge 20 commits into
mainfrom
fazxes/add-bash-tool

Conversation

@fazxes

@fazxes fazxes commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • With experimental.bash_tool on, fx offers the model one bash tool in place of its file, search, web, and shell tools. Subagent, toolbox, and vision stay. Turn it on with fx config set experimental.bash_tool true, or for one run with fx ask --config experimental.bash_tool=true. With the setting off, every host offers the same tools as before.
  • Each call runs one bash program. Before the command, the program's own shell runs a script that restores the last program's variables, functions, aliases, and shell options, and after the command it saves them, so state carries over from call to call. errexit, xtrace, posix, and expand_aliases are not carried: each program sets the first three for itself, and alias expansion is always on. The script runs after the user's startup definitions, so a name the program defined wins over one from .bashrc, and the save keeps only definitions that differ from the startup ones, so a later .bashrc edit still applies. fx hands the script to the shell as text, so child processes never load or save the state, and subshells never write it. The save calls every command through builtin or command, so a program's own mv() or wc() cannot change it. A program that sets its own EXIT trap keeps fx's save, and a program's own IFS or shell options do not change it.
  • fx saves the state between programs instead of keeping one live bash process, so no product state exists only in a live shell. Each call stays an ordinary captured command with the same permission review, timeout, and output handling as today's shell commands. Exported variables fx passed in unchanged are not saved, so no credential fx holds reaches the state file, which is private to the user.
  • Each program starts in the folder the last program ended in. Permission review is told that folder, so an approval covers the folder the program really runs in. When a program ended in another folder after the approval, the call is refused and the model sends it again.
  • A bash state over 1 MiB is not saved, and neither is a state whose save fails or a program that ends before fx's save runs, for example one that is killed or replaces itself with exec. The last saved state stays, the call's result says so, and fx writes a debug trace line with the reason. The save leaves a small status record in the session folder, and fx reads it when the program's end is delivered, so the notice reaches the model even when the program ends with exit, is killed, or replaces itself with exec. If the next program cannot start, its failure result carries the notice instead.
  • A call waits up to 30 seconds for its program. A program that is still running keeps going, the result says so, and a call whose command is wait_more waits for it again. One program runs at a time for each agent.
  • Each program is a captured command with the permission class of today's default shell command, dynamic_shell, because a saved function or PATH can change what a command runs. For the same reason, auto mode reviews every bash program, including routine commands such as git status that it runs without review for the shell tool. Configured allow rules and session approvals still match the exact command text, as they do for the shell tool. fx decides from the command alone whether a call is a program or a wait. A wait needs no command review in auto mode, and it meets the same terminal rules as shell's interact.
  • fx mcp call works from a bash program, as it does from the shell tool.
  • The setting applies in fx ask, the TUI, ACP, and their subagents. A new conversation in the TUI (/new, /clear, /reset, resume, fork) or a new ACP session starts with a fresh bash state, and the TUI removes the bash folder when it quits. A subagent's bash state lasts one subagent run. A folder that a killed fx leaves behind is removed by the next bash session in the same temp folder once its owner process is gone. Each reset, including the end of a subagent run, and each sweep writes a debug trace line.
  • Needs Reap the running prompt before libfx/new replaces the session #2282, which makes libfx/new cancel and wait for a running prompt before it replaces the session. Without it, a host that sends libfx/new while a prompt runs could free this change's bash state under the prompt, as it already can free the session runtime on main. Merge Reap the running prompt before libfx/new replaces the session #2282 first.
  • Before each program, fx checks the bash session's folder and its files, never following a link. A folder that a program removed is made again, a load script that is missing or not a regular file is written again, and a saved state or record that is not a regular file is removed or replaced, so a FIFO or a link there cannot make fx or the next program wait or write elsewhere. When saved state is lost this way, the call's result says so, and fx writes a debug trace line for each repair.
  • src/core/bash_session/ keeps the saved state. It imports only the standard library, and other code reaches it only through bash_session.zig, which scripts/check-bash-session-boundary.sh checks in CI.
  • The command runner gains one generic field, after_startup: shell text that the command's own shell runs after the user's startup definitions and before the command, on both the startup-snapshot path and the full-startup path. Only the bash tool sets it. Every other caller passes an empty text, and its scripts and arguments stay byte-identical.
  • Permission admission and the bash tool build a program's environment with the same shell_resolver.bashToolEnvironment. Shell and bash hand off their results through tooling/managed_delivery.zig and Snapshot.failed(). The command limit, the wait time, wait_more, and the variables fx adds to each program come from their owners' constants.
  • tests/tla/bash_tool/ResultStore.tla models the session: one program at a time, wait_more, a saved state that is never left half written, a state over the size cap that is never dropped without a notice, and session files a program damaged that fx heals before the next program, whose result says the state was lost. CI now runs its checker with the other models on every pull request.
  • bash_call_text fuzzes the bash tool's argument decoder.
  • When the saved start folder can no longer be used, for example because a program removed it, the next program starts in the workspace root, and its result and a debug trace line say so.
  • No manual page changes, because the fx.sh manual does not list experimental settings.

Known limits

  • A program can damage its own bash session in ways that fx reports or repairs but cannot prevent. A program that hard-links an outside file at its own save record makes its own exit save empty that file, as the program could do itself; fx replaces the record before the next program. An old session folder that a program made impossible to remove, for example with a read-only child folder, stays on disk; fx retries its removal at later repairs and when the session ends, and writes a trace line if it is still there. fx keeps at most eight such folders to retry; a ninth is not retried or counted, and the repair's trace line gives only the error name. If the session's current folder cannot be removed when the session ends, it also stays on disk with no trace line. A program's child that keeps running can still put a link or a FIFO at a startup record in the short time after fx checks the folder and before the next program loads it.
  • A saved name that the user's startup files also define is taken from the program's state, and a startup definition that the program removed with unset comes back from the startup files in the next program.

Sample

With the setting on, fx ask --json --config experimental.bash_tool=true offers the model bash, toolbox, and vision. Two calls from a run of the built binary, then the result text of their programs:

bash {"command":"set -u; greet() { echo \"hello-$1\"; }"}
[done, no output]

bash {"command":"greet fx; bash child.sh"}
hello-fx
childok

A program that runs past 30 seconds, then the wait:

bash {"command":"sleep 40; echo finished"}
[still running after 30 s. Send wait_more as the whole program to wait for it.]

bash {"command":"wait_more"}
finished

The notice for a state over the size cap:

[done, no output]
fx: this program's bash state is 1101686 bytes, over the 1048576-byte limit, so it was not saved; the next program starts from the last saved state.

Architecture

Before:

flowchart LR
  model[Model] --> set[builtin_tools.advertisement_set]
  set --> admission[tooling/tool_admission.zig]
  admission --> shell[tools/shell/shell.zig]
  shell --> runtime[execution/managed_execution.zig]
  shell --> delivery_copy[shell's own delivery handoff]
  shell --> bridge[mcp_host/mcp_call_bridge.zig]
Loading

After:

flowchart LR
  model[Model] --> select{experimental.bash_tool}
  select -- off --> set[builtin_tools.advertisement_set]
  select -- on --> bash_set[builtins/bash_tools.zig: bash, subagent, toolbox, vision]
  set --> admission[tooling/tool_admission.zig]
  bash_set --> admission
  admission -- bash host: start folder --> bash[tools/bash/bash.zig]
  admission --> shell[tools/shell/shell.zig]
  env[terminal/shell_resolver.zig bashToolEnvironment] --> admission
  env --> bash
  bash --> session[core/bash_session: folder, load and save script, program id, start folder]
  bash --> runtime[execution/managed_execution.zig]
  shell --> runtime
  runtime -- after_startup --> runner[execution/command_runner.zig and terminal/shell_resolver.zig]
  bash --> delivery[tooling/managed_delivery.zig]
  shell --> delivery
  bash --> bridge[mcp_host/mcp_call_bridge.zig]
  shell --> bridge
Loading

Ship check

  • The TLA+ models in tests/tla/acp/, agent_runtime/, agents_menu/, auth/, managed_execution/, mcp_host/, saved_turn/, subagent/, terminal/, and toolbox/ describe files this change touches, and none of them changes. In those files the change picks the tool set, keeps and resets each agent's bash state, passes the bash start folder to admission, lets an inferred action replace one the model sent, moves shell's delivery handoff into a shared helper without changing it, makes libfx/new reap a running prompt before it replaces the session, carries the empty-by-default after_startup text to the runner, has the snapshot capture pass an empty one, and adds a list of the bridge's variable names. The modeled behavior stays the same: permission modes and speed, the empty-reply nudge, the agent list, login refresh, managed command delivery, the MCP host, message order at step boundaries, steer queues, the startup snapshot owner and hosted terminals, and the toolbox menu. The setting is read once at startup, so the toolbox model's rule that a session's tools never change still holds.
  • The check lists 22 fuzz targets that this change reaches. The 9 direct ones, bash_call_text, request_wrapper_text, session_title, switch_order, agents_menu_selection, subagent_task_line, trace_record_text, trace_review_records, and snapshot_alias_lines, each ran clean for 60 seconds, and zig build fuzz replays every target. The other 13 are reached only through imports, so they are left to the nightly fuzz run. The Fuzz workflow on this branch passes.
  • Built side by side with its base d3399becc9, this change adds 57,648 bytes to the stripped Linux x86_64 ReleaseSafe binary, so the binary-size check shows its informational note (52,429 bytes). Most of it is the two new bash files. In hyperfine runs of 200 and 300 on main 170f50e45d, fx sessions --json and startup took the same time with and without this change (1.5 to 1.7 ms against 1.5 to 1.6 ms, and 505 µs against 558 µs).
  • Eight bash session tests need folder modes to hold, so they skip when the user can write through them (root, or a sandbox user with CAP_DAC_OVERRIDE). CI's runner is not root, so they run there, and they pass in a sandbox with those capabilities dropped.
  • The check flags one line as concurrency: kill(pid, 0) in the dead-folder sweep delivers no signal and only asks whether the process exists. The Thread Sanitizer run on this branch reports 6 warnings, and all 6 match main's latest nightly run. One of them, the thread leak created at shell_snapshot.zig:692, passes through managed_execution.zig, which this change edits only to add the shared failed(), two names, and the after_startup field; main has the same leak at the same site. No warning names bash_session.zig or bash.zig.

@fazxes fazxes added the type: feature Adds a new user-facing capability label Nov 22, 2026
@vercel-internal-security-reviewer

Copy link
Copy Markdown

Security review details

… session when a repair fails, and model the heal
…on folders, and treat hard-linked records as damaged

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature Adds a new user-facing capability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant