Files
oh-my-pi/docs/bash-tool-runtime.md
can1357 b279532036 feat(coding-agent)!: folded artifact:// spills into local://
Spill N (truncated tool output, shaped originals, shake placeholders,
minimized bash raw output) is now <localRoot>/N.log, read as local://N.log,
an ordinary writable, listed, completed local:// file. Ids stay a per-session
counter shared by the subagent tree; each id's file is claimed exclusively so
a model-written N.log is never clobbered. Sessions without a session file
spill under the tmp local:// root instead of memory.

spillUrl/parseSpillUrl/spillUrlIds in pi-utils are the only code that spells
the URL. The artifact:// protocol, its per-id shell mounts and the in-memory
spill path are removed; line-page reads of local:// files get the old
artifact page budget (SchemeSpec.unspilledPages). omp stats still reads old
sessions' artifact://N. No migration for old sessions.
2026-11-18 14:04:05 +01:00

42 KiB
Raw Permalink Blame History

Bash tool runtime

This document describes the bash tool runtime path used by agent tool calls, from command normalization to execution, truncation/artifacts, and rendering.

It also calls out where behavior diverges in interactive TUI, print mode, RPC mode, and user-initiated bang (!) shell execution.

Scope and runtime surfaces

There are two different bash execution surfaces in coding-agent:

  1. Tool-call surface (toolName: "bash"): used when the model calls the bash tool.
    • Entry point: BashTool.execute().
    • Parameters include command, optional timeout, cwd, pty, and, when async.enabled is true, async. With launch.enabled (default true), name and ready select managed service execution. notify is present with either. There is no model-facing env parameter.
  2. User bang-command surface (!cmd from interactive input or RPC bash command): session-level helper path.
    • Entry point: AgentSession.executeBash().

Both eventually use executeBash() in src/exec/bash-executor.ts for non-PTY execution, but only the tool-call path runs normalization/interception, optional managed background-job handling, and tool renderer logic.

Set bash.enabled: false in settings to remove the model-facing bash tool from the active tool registry. This does not disable user-initiated bang commands or RPC bash requests.

End-to-end tool-call pipeline

1) Input handling and mode selection

BashTool.execute() currently handles input as follows:

  • trims service names and normalizes empty readiness fields,
  • extracts a leading single-line cd <path> && ... into cwd when cwd was not supplied, unless the path needs shell expansion,
  • treats a nonblank notify regex without a service name as async: true, and rejects either when async.enabled is false,
  • rejects a notify regex that does not compile, or a session that cannot take its notices, before running anything,
  • passes notify with a service name to the launch broker, which reports the service's matching output,
  • defaults finite-command timeout to 300 seconds; 0 explicitly disables the command deadline,
  • ignores async: true and any supplied timeout in named-service mode, adding a notice for each; readiness uses ready.timeout,
  • ignores ready without a nonblank name, adding a notice.

There are no structured head or tail parameters. Command text is never rewritten for internal URLs. The embedded shell and its in-process coreutils resolve scheme:// paths through an injected async filesystem (InternalUrlFilesystem) at the moment of each operation, so URLs built from variables, redirections, globs, cd, and a URL cwd all work. File-backed schemes operate on their backing files, and ln -s from one to a host path stores the backing file's path; rendered resources are read-only. An external program has no view of that filesystem, so the program path, every argument (skill://example-skill/tool.py, --index=skill://example-skill/data), and the working directory it is spawned with are translated to the files they alias. Translation covers whole arguments and --option=URL values only; a bare scheme root (local://), a root followed by words (skill:// external command), and URLs embedded in other text stay literal, as do schemes the filesystem does not route (https://, s3://) and arguments naming a scheme that aliases no file (omp://, history://). A URL the provider rejects (an unknown skill, a path escaping its root) fails the command with the provider's error. External programs cannot start in a scheme that aliases no file, nor in a read-only one (skill://), since the child could write anywhere under its working directory; read-only is otherwise enforced only for builtins, so an external program handed a translated read-only path can still write it. xargs, find -exec/-execdir, and ifne run their commands through the shell's own dispatch in a subshell, so … | xargs cat reaches the in-process cat and its URL arguments. The finite-command routes also load configured direnv/devenv changes. SDK callers of the executor can supply an env overlay; the bash tool itself uses shell assignments for per-command variables.

Approval policy

The bash tool has the exec approval tier. bash.patterns rules can explicitly allow, deny, or prompt. By default, allow rules must match the entire command and cannot approve shell-control syntax. The opt-in bash.allowCompoundCommands: true additionally recognizes only flat chains of two or more literal commands separated by unquoted &&. It resolves the ordered rules independently against each original raw segment, with the first matching rule winning for that segment.

For allow rules, leading NAME=value assignments are stripped only when NAME exactly matches an entry in bash.allowedEnvVars or the rule's env list; deny and prompt rules check tokenized segments both with and without leading assignments.

Restrictions are combined conservatively across the chain (and across tokenized segments of non-eligible compound commands): any explicit segment or whole-chain deny wins, otherwise any explicit prompt wins. A deny or prompt pattern matching the full chain but no individual segment remains a whole-chain restriction, while later broad restrictions do not override an earlier match for a segment. Operator deny rules from Settings.grantAuthority() also take precedence over project-layer bash.patterns. These restrictions resolve before the existing raw and canonical critical-command checks.

Whole-chain restrictions are scanned with deny precedence, even when a matching prompt appears earlier. The centralized positive shell classifier permits compound recognition only for known POSIX-quoting shells (sh, bash, dash, ash, ksh, and zsh, including .exe names). Cmd, PowerShell, fish, and unknown shells retain legacy approval behavior; merely accepting -c for execution does not establish compatible quoting.

After those checks, the chain receives an explicit write-tier allow only when every segment explicitly resolves to allow. If any segment is unmatched, bash retains its standalone exec tier with no explicit policy, so the generic approval resolver applies tools.approval.bash and then the active mode. Unmatched segments therefore inherit existing policy rather than always prompting.

Literal quoted arguments are accepted, but expansions, assignments, other control operators, redirections, globbing, newlines, malformed syntax, and shell-state-changing builtins (cd, source, eval, and similar) do not qualify. These inputs retain legacy approval behavior. Critical destructive and remote-fetch-and-execute checks still inspect the whole raw and canonical input and its segments, so an allowed prefix cannot conceal a critical later segment. Approval does not rewrite execution: the shell receives the original command, preserving native && short-circuiting.

Pattern approval is not containment. Once approved, a process keeps the shell's ambient filesystem, network, and subprocess access. Interception and approval are also separate mechanisms: interception routes misuse toward dedicated tools; approval governs whether execution may proceed.

These rules govern the bash tool only. They do not constrain shells started through other tools — notably eval, which can spawn a shell via subprocess (subprocess.run(["bash", "-c", ...]), Bun.$, etc.). A bash.patterns deny rule therefore does nothing when the same command is issued through eval. To harden against destructive commands across both surfaces, pair bash.patterns with a tools.approval.eval policy (prompt or deny); see Tool approval mode.

2) Optional interception (blocked-command path)

If bashInterceptor.enabled is true (default false), BashTool loads bashInterceptor.patterns and runs checkBashInterception() against the command — checking both the original and the cwd-normalized form (after a leading cd … && is extracted) when they differ. Rule syntax is unchanged: each rule checks the complete input first, then raw flat command fragments separated by unquoted/unescaped &&, ||, ;, |, |&, &, or newlines, then those fragments with leading NAME=value assignments removed. Fragments that receive piped stdin from | or |& are excluded from the fragment candidates, including across blank/comment continuation lines, because a stdin-consuming stage cannot be replaced by a path-based dedicated tool.

Interception behavior:

  • command is blocked only when:
    • regex rule matches, and
    • the suggested tool is present in ctx.toolNames.
  • invalid regex rules are silently skipped.
  • on block, BashTool throws ToolError with message:
    • Blocked: ...
    • original command included.
  • heredocs, parameter expansion, command substitutions, backticks, grouping, and malformed quoting do not produce extra fragments; they retain only the complete-input check. Interception is best-effort routing to dedicated tools, not a shell-security policy.

Default rule patterns (defined in code) target common misuses:

  • file readers (cat, head, tail, ...)
  • search tools (grep, rg, ...)
  • file finders (find, fd, ...)
  • in-place editors (sed -i, perl -i, awk -i inplace)
  • shell redirection writes (echo ... > file, heredoc redirection)
  • unmanaged background syntax and common services/watchers/debuggers (suggesting bash with name)

Named-service calls omit the rules whose suggested tool is bash, so service commands do not intercept themselves.

Caveat

InterceptionResult includes suggestedTool, but BashTool currently surfaces only the message text (no structured suggested-tool field in details).

3) CWD validation and timeout resolution

A host cwd is resolved relative to session cwd (resolveToCwd) and validated via filesystem stat. A recognized internal-URL cwd is normalized and validated through InternalUrlFilesystem; the embedded shell enters it, and a service's or pty command's shell process starts in the session cwd and enters it from there. Client-terminal routing is skipped for a virtual cwd.

  • missing path -> ToolError("Working directory does not exist: ...")
  • non-directory -> ToolError("Working directory is not a directory: ...")

The default timeout is 300 seconds. timeout: 0 disables the deadline. Other values are clamped to [1, 3600] seconds and by a positive tools.maxTimeout ceiling; a clamp notice and both requested/resolved values are recorded when they differ.

4) Artifact allocation

Local finite-command execution allocates an artifact path/id (best-effort) for truncated output storage; managed jobs allocate inside the job. Service logs use the broker, and client-terminal output uses the final inline-cap recovery path instead.

  • artifact allocation failure is non-fatal (execution continues without artifact spill file),
  • artifact id/path are passed into execution path for full-output persistence on truncation.

5) PTY vs non-PTY execution selection

PTY eligibility is decided by canUseInteractiveBashPty(pty, ctx) (src/tools/bash-pty-selection.ts); the local PTY overlay runs only when all are true:

  • tool input pty === true
  • PI_NO_PTY !== "1"
  • tool context has UI (ctx.hasUI === true and ctx.ui set)

If pty is requested but unavailable, the call falls back to non-PTY and appends a pty requested but unavailable … notice.

A pty command runs as a shell process (src/exec/shell-process.ts): the embedded engine with the tool profile in a process of its own, leading the overlay's pseudo-terminal, so its programs own that terminal (/dev/tty, terminal signals, resize). It starts from the session shell's exports, functions and aliases, and its local:// (spills included), skill:// and attachment:// paths resolve to the session's host files; other schemes are missing there.

Before the local PTY/non-PTY choice, a foreground (async: false) call can route to a managed background job (auto-backgrounding; see below) or — when the session's client advertises a terminal capability (clientBridge.capabilities.terminal + createTerminal, with pty false) — to a client-bridge editor terminal that runs the command remotely (streaming terminalId updates, killing on timeout, mapping a signal kill to exit code 137). Otherwise it uses non-interactive executeBash().

Print mode and non-UI RPC/tool contexts cannot use the local interactive overlay. Named services have their own broker-owned PTY and do not require a TUI.

Managed service execution

A nonblank name routes to startService() before finite-command timeout, async, terminal, or output-sink selection.

  • Names are 1–48 characters: an initial letter or digit, followed by letters, digits, dots, underscores, or hyphens. Names are project-broker scoped; starting an existing live name replaces that process.
  • Services run as a shell process too (the embedded engine with the tool profile, started by the broker), with the configured shell prefix, from the session shell's exports, functions and aliases plus PI_SESSION_ID/PI_SESSION_FILE/PI_TOOL_CALL_ID. Their local:// (spills included), skill:// and attachment:// paths resolve to the owning session's host files; other schemes are missing there.
  • pty defaults to true; no command deadline is imposed.
  • ready must include a log regex or TCP port (1–65535). When both are present, both must pass. TCP host defaults to 127.0.0.1; readiness timeout defaults to 30 seconds and is clamped to 0.05–3600 seconds.
  • Without a readiness condition, startup returns the running service. With one, startup waits for readiness, exit, or the readiness deadline. A readiness timeout is reported without stopping the process. A queued steering message ends the readiness wait early: the call returns starting and the service keeps starting.
  • New services use session lifetime (persistent when launch.persist is on, which print, RPC and ACP modes always set), no automatic restart, and are not detached. Completion notifications are associated with their owning session.
  • read proc://<name> inspects state and logs; write proc://<name> sends input (empty content sends Enter), write proc://<name>/kill stops it, and /mode accepts persist, session, or detached.

The result carries details.service (name, state, ready, timedOut, optional pid), not finite-command timeout/async metadata.

Non-interactive execution engine (executeBash)

Shell session reuse model

executeBash() caches native Shell instances in a process-global map keyed by:

  • shell path,
  • configured command prefix,
  • snapshot path,
  • serialized shell env,
  • optional agent session key,
  • minimizer configuration.

Session-level bang-command executions pass the session ID captured when execution starts, preserving result ownership across a branch or session transition.

Tool-call executions pass sessionKey: this.session.getSessionId?.(), when available, including calls that start in the foreground on the auto-background path, so variables, functions, and shell options carry over between calls. A managed job that starts in the background (async: true, or no foreground wait) uses a per-job <session>:async:<jobId> key instead, so it never holds the session's shell. In both surfaces, a session key isolates shell reuse per session; without one, reuse falls back to shell config/snapshot/env. Concurrent calls never share one Shell: the native session runs one command at a time and Shell.abort() kills every in-flight run on it. executeBash() tracks in-flight keys in shellSessionsInUse; while a key is busy, overlapping calls skip the cache and run in a one-shot Shell.fork() of the session's shell, which starts from its state before the running command. When a fork's run settles, executeBash() hands it to Shell.adopt(): what it set (variables, functions, aliases, options, traps; not the working directory or the hash table) carries back into the session's shell at the end of its running command (at once when it is idle) and into every later fork; a name both set keeps the value of the command that ended last. A fork that left background jobs running is retained while they run, as a released session is. BashResult.fork reports both; BashTool adds a notice only when the state could not be carried back or background jobs stay in the fork. A call that moves to the background keeps the session's shell until it finishes. Only the owning call releases the in-use flag or deletes the cached session in its finally.

exit, errexit (set -e), and a stray top-level return/break end the command, not the native session; a timeout, cancellation, or failed run quarantines the session and the next call starts a fresh one.

A call keeps the session ID it started with, even if the session changes before its shell starts. Leaving a session (new, fork, switch, branch) releases its shells, and a command still running or pending from a left session drops its shell when it settles (isSessionCurrent), so returning to a session starts a fresh shell.

A session can own more than one shell cache entry: surfaces that need their own shell state pass a namespaced key, <label>:<session id> (a /loop condition uses loop-condition:<id>), while the bash tool uses the bare session id. Releasing a session drops every one of them, and a shell released while a command is still running retains any live background jobs once that command settles. Only an identifier-shaped label directly followed by the released session id counts as a namespace, so per-job <session>:async:<jobId> keys and cwd-scoped keys are unaffected.

Native shell visualization

omp-shell owns Raw/Pseudo generation, static analysis, source mapping, replay reduction and last-setter annotations. It retains the actual parser's List, Command and Word nodes as units compile; arithmetic formatting uses the engine's existing arithmetic parser. There is no second TypeScript AST or shfmt conversion in this path.

  • Incremental: only engine-announced syntax is presented as Pseudo. Uncompiled source remains literal; creating or presenting a pending RunTrace does not parse ahead.
  • Live: the executor attaches each immutable snapshot to its actual native RunTrace before publishing it. RunTrace.present(options) borrows native syntax/history; the UI does not serialize and reload an AST on each update.
  • Stored: ShellVisualization(snapshotJson) restores a persisted trace once. Canonical syntax uses a versioned, cached base64 archive of the engine's own AST and parser context. Unsupported archive versions fail explicitly. Legacy traces without an archive restore only their announced units with the native parser.
  • Renderers: ANSI and Tern consume the same semantic rows. TypeScript owns preferences, input dispatch and playback clocks, not shell interpretation. Tern receives neither the syntax archive nor the execution journal.
  • Identity: source-token node keys and UTF-16 ranges drive selection/source links. executionNode identifies the actual command owning a token's outcome and completion, so arguments share one failure mark and 500ms trail. Keys preserve native unit/source/node identities; renderers treat them as opaque.
  • Provenance: values follow real setters and the replay cursor. Unproven local binding identity stays explicitly unknown; a function-entry event is not proof of a dynamically scoped variable's owner.

In-engine children, including literal eval, use actual compiler origins. External OS shells and SSH remain external/untraced: literal bodies may be shown with that label, but never receive invented inner outcomes, variables or output. Visualization does not change executable resolution.

The ordinary Bash card remains a neutral segmented command/output/footer box. Raw/Pseudo and the menu stay on its first code line; replay controls belong to the inspector. original rows are produced only for Pseudo split, and normal live output still comes from the existing output sink; an explicit replay cursor uses native captured output.

Bundled jq compatibility

Unless PI_DISABLE_UUTILS_BUILTINS is truthy, the non-PTY native shell registers a bundled, in-process jq command instead of the system jq. It behaves like jq 1.8.2: the same options, grammar, builtins, output bytes, error messages and exit statuses (jq --version prints jq-1.8.2), and it passes jq 1.8.2's own test suite (tests/*.test, also through --run-tests). It is built on jaq's crates, vendored and patched for jq's semantics (crates/vendor/jaq-*). Setting that flag disables the in-process uutils command set and falls back to system binaries; a path (/usr/bin/jq) runs a system jq for one command.

Known differences from jq 1.8.2: --debug-trace prints no bytecode, unbounded recursion (def f: 1 + f; f) fails with filter recursion too deep instead of exhausting memory, and math builtins jq takes from the C library (gamma, lgamma, j0, …) may differ from it in the last digit.

Shell config, direnv, and snapshot behavior

At each call, the executor loads settings shell config (shell, env, optional prefix) and runs applyDirenvPreflight().

Unless bash.direnv is "off", preflight attempts to load the cwd's direnv/devenv changes within bash.direnvLoadTimeoutMs, additionally bounded by a positive command timeout. Direnv-provided variables are merged below explicit caller env; safe variables removed by direnv are prepended as unset -v .... ACP-terminal and PTY routes run the same preflight before their backend; the non-PTY executor runs it internally. Every BashTool route passes the session identity (PI_SESSION_ID, and PI_SESSION_FILE as an absolute path or empty when unpersisted) and the call's PI_TOOL_CALL_ID as caller env, read with the session ID when the call starts; it is not part of the shell session key, so it never starts a new shell.

Successful exports retain their loaded environment and DIRENV_* state per .envrc directory. Each call still runs direnv export json to check direnv's watched inputs and authorization state, but an unchanged environment avoids re-running .envrc and devenv setup. OMP returns the complete diff relative to its process environment. A change to that process environment, or to the nanosecond timestamp, size, or existence of the .envrc or any direnv-watched path (including direnv's allow/deny files), restarts the load from a clean baseline; so does a warm export that reports any change. This cache does not modify OMP's process environment.

If the selected shell includes bash, it attempts getOrCreateSnapshot():

  • snapshot captures aliases/functions/options from user rc,
  • snapshot creation is best-effort,
  • failure falls back to no snapshot; a failed snapshot is retried after 60 s, or sooner when the rc file's mtime or size or the shell environment changes.

If prefix is configured, it wraps the command after any direnv unset prefix.

The per-command child environment is then built by buildNonInteractiveEnv() (src/exec/non-interactive-env.ts), which layers non-interactive hardening defaults under the caller and direnv overrides:

  • pagers disabled (PAGER=cat, GIT_PAGER=cat, … and LESS=FRX),
  • editor prompts disabled (GIT_EDITOR=true, GIT_SEQUENCE_EDITOR=true, EDITOR=true, VISUAL=true),
  • terminal/credential prompts reduced (TERM=dumb, GIT_TERMINAL_PROMPT=0, GCM_INTERACTIVE=never, SSH_ASKPASS set to the resolved false executable or "false", NO_COLOR=1, CI=true unless PI_BASH_NO_CI/CLAUDE_BASH_NO_CI is set),
  • package-manager/tooling automation flags for non-interactive behavior (npm/pnpm/yarn/pip/cargo/terraform/gh, …),
  • locale pinned to LANG=C.UTF-8/LC_ALL=C.UTF-8 (byte-order collation, C numbers and messages, UTF-8 text) over any inherited locale, unless the caller or direnv env sets LANG or an LC_* variable; macOS ships no C.UTF-8, so there LANG and every LC_* category are C except LC_CTYPE=en_US.UTF-8, under an empty LC_ALL; user ! commands keep the inherited locale, with the UTF-8 default applied on Windows only when none is set,
  • on Windows, Python UTF-8 stdio defaults (PYTHONIOENCODING, PYTHONUTF8) are added when neither is inherited or passed.

Streaming and cancellation

Shell.run() streams chunks to OutputSink and optional onChunk callback.

Cancellation:

  • aborted signal triggers shellSession.abort(...),
  • timeout from native result is mapped to cancelled: true + annotation text,
  • explicit cancellation similarly returns cancelled: true + annotation.
  • on POSIX, cancelling a non-PTY run sends TERM to its owned process groups and descendants, allows up to one second for cleanup, then escalates to KILL; Windows terminates processes without a catchable TERM signal.

No exception is thrown inside executor for timeout/cancel; it returns structured BashResult and lets caller map error semantics.

Interactive PTY path (runInteractiveBashPty)

When PTY is enabled, tool runs runInteractiveBashPty() which opens an overlay console component and drives a native PtySession.

Behavior highlights:

  • xterm-headless virtual terminal renders viewport in overlay,
  • keyboard input is normalized (including Kitty sequences and application cursor mode handling),
  • esc while running kills the PTY session,
  • terminal resize propagates to PTY (session.resize(cols, rows)).

Unlike the non-PTY engine, the interactive PTY path does not apply the non-interactive hardening. The Rust side starts from the process's native environment and applies the env it is handed as overrides; Bun's process.env writes never reach that base, so the PTY is handed the shell spawn environment (getShellConfig().env) minus its non-interactive guards (GIT_EDITOR, GPG_TTY, CI) and NO_COLOR, then a real TERM=xterm-256color so editors, pagers, and TUIs behave like a normal terminal, then the direnv values, which win over both. A key left out keeps the inherited value.

PTY output is normalized (CRLF/CR to LF, sanitizeText) and written into OutputSink, including artifact spill support.

On PTY startup/runtime error, sink receives PTY error: ... line and command finalizes with undefined exit code.

Output handling: streaming, truncation, artifact spill

Both PTY and non-PTY paths use OutputSink.

OutputSink semantics

The bash executor builds the sink with headBytes and maxColumns from settings (resolveOutputSinkHeadBytes / resolveOutputMaxColumns).

  • keeps the inline body within spillThreshold (DEFAULT_MAX_BYTES, 50 KiB by default), using UTF-8-safe boundaries,
  • when headBytes > 0 (tools.artifactHeadBytes, default 20 KiB) it reserves a head window within that same budget and uses the remainder for a rolling tail; head retention is capped at half the total budget, and dump() splices in a middle-elision marker when necessary,
  • per-line column cap: when maxColumns > 0 (tools.outputMaxColumns, default 768 bytes) over-wide lines are ellipsis-truncated at write time and the rest of the line is dropped,
  • tracks total bytes/lines seen,
  • mirrors the sanitized, uncapped text stream to the artifact file when output overflows, a column cap dropped bytes, or the file is already active; the artifact file is capped at tools.artifactMaxBytes (default 16 MB: the first 3 MB plus a rolling tail, joined by an [ARTIFACT TRUNCATED: …] notice; 0 = unbounded),
  • marks truncated on tail overflow, middle elision, column-cap drops, or file spill.

dump() returns:

  • output (possibly annotated prefix),
  • truncated,
  • totalLines/totalBytes,
  • outputLines/outputBytes,
  • elidedBytes/elidedLines when the middle was elided,
  • columnDroppedBytes/columnTruncatedLines when the per-line cap fired,
  • columnMax when the per-line cap fired,
  • artifactId if capture succeeded,
  • artifactError (open, write, flush, or end) when artifact I/O failed; a failed capture withholds its artifact ID.

Long-output caveat

Runtime truncation is byte-threshold based in OutputSink (50 KiB shared head/tail budget by default, plus marker text). It does not enforce a hard line-count cap in this code path.

Kitty direct-image and Sixel frames are extracted before text sanitization/truncation and returned as image content. Their control bytes are not preserved as ordinary artifact text.

Shell output minimizer

Non-PTY execution also passes shell-minimizer settings into the native Shell session. When the minimizer rewrites verbose output, the executor substitutes the minimized text only after the original capture has been saved as a separate spill, referenced by a [raw output: local://<id>.log] footer, and returns { filter, inputBytes, outputBytes, artifactId } as BashResult.minimized, which the tool result keeps as details.meta.minimized and the renderer shows as a Minimized footer fact. If saving returns no artifact ID, the lossless sink output is retained instead and no minimized is recorded.

Live tool updates and async jobs

For non-PTY foreground execution, BashTool passes an onPreview callback to executeBash(), which streams the OutputSink's own inline view (OutputSink.preview()) as onUpdate snapshots while the command is running; there is no separate partial-update buffer.

For PTY execution, live rendering is handled by custom UI overlay, not by onUpdate text chunks.

When async.enabled is true and the call passes async: true, BashTool starts a managed bash job immediately, returns a running result with a job id, and stores completion through the session job manager. Auto-backgrounding (bash.autoBackground.enabled, default true) can also use this path after bash.autoBackground.thresholdMs (default 60,000 ms); it is skipped for PTY and client-bridge terminal routes and falls back to foreground execution when the job manager is at capacity. A queued steering message can background a still-running auto-background candidate early.

Pipe streams (proc://<id>/streams)

Every managed bash job passes a native PipeStreams to the embedded shell (ShellRunOptions.streams). The engine mirrors the bytes written into each | pipe of the run into a per-stream ring (newest 1 MiB; a run keeps up to 16 streams, 4 of them finished) without changing pipe semantics: backpressure, EOF, and an early-exiting consumer (| head) widowing the producer behave as on a plain pipe (omp_shell::bash::Host::pipe_tap, crates/shell/src/bash/mem.rs, crates/shell/src/taps.rs). read proc://<id>/streams lists them (stream n is what stage n reads) and proc://<id>/streams/<n> returns the decoded, sanitized text, so cmd | tail -5 can be followed while tail holds cmd's output back. The streams stay with the job row until it is evicted. User-shell (zsh/fish) and PTY runs are not tapped, and each stage's stderr still goes to the job's own output.

Job widgets

Bash jobs register widgetInputs (src/job-widgets/): an out source fed from the job's output chunks plus one pipe:<n> source per pipe stream, and a brief (the command and the call's i intent). Once a shown job has run 15 s with output, the widget model role (resolves like smol; a request that errors moves on to the role's next model with a credential, from retry.fallbackChains.widget or else the built-in smol defaults, each attempt charged to the cost cap) writes one declarative spec: line rules (linear-time Rust regexes run through the native LineMatcher) whose named groups each feed the field of the same name (a number, or text for the rule's text group or a capture holding no number; key splits a rule's fields into series, count counts its matching lines), and up to four blocks (column chart with target lines, per-key bars, meter, stats, status grid, text). The request carries only the job's distinct sample lines (at most three per line shape at the head and the tail of each source) and caches nothing but the shared system prompt and schema. A subagent's job (its tools source and its own jobs' sources) is only described once its completion probe names a watch hint: a subagent reading and editing code has no output that shows progress. The author may decline with {"skip": "<why>"}, which leaves the job without a widget (a task's job is asked again once its watch hint changes). The spec is validated and dry-run on the samples before it is installed (a field read as a number must capture one; no rule may read the » <tool> <intent> markers of a subagent's tool calls; a constant meter total or spark target must be a number the command, task or output states); common slips are repaired locally instead of costing a retry (an over-long title or label is cut, a meter with no known total becomes a stat, a key or text naming a group the pattern lacks is dropped, or captures the whole match when the pattern has no groups), one retry gets the remaining errors back, and drift (200 unmatched lines on the rules' sources with stale data) or a subagent's late watch hint re-authors, at most three requests per job. From then on every refresh reads only new bytes, runs one native match per source over the rules reading it (column-wise results: match lines, rules and capture spans), folds matches into bounded per-field series, and recomputes the JobWidgetView locally; snapshotJobs attaches it to JobSnapshot.widget, which the wait card, the HUD jobs pill (with its hovercard) and the jobs sheet render. async.widgets turns the feature off.

Result shaping, metadata, and error mapping

After execution:

  1. A cancellation or missing exit status throws a tool error. The client-bridge terminal route also throws ToolError for timeout before structured result shaping.
  2. Local non-PTY and interactive-PTY timeouts return an error result with details.timedOut = true so the renderer can distinguish them from an ordinary failure.
  3. Empty output becomes (no output).
  4. A final inline byte cap protects routes that bypass OutputSink; it reuses the sink artifact when available or saves a bash-original artifact.
  5. Truncation metadata is attached from the sink summary.
  6. A nonzero exit returns an error result with details.exitCode; zero returns success.

Result details can also include resolved/requested timeout, timeoutDisabled, client terminalId, wall time, async job state, and truncation metadata. Truncation includes direction/reason, total and shown line/byte counts, shown range, and artifactId when persistence succeeded.

Built-in tool wrapping appends the model-facing recovery notice automatically, for example Read local://<id>.log for full output.

Rendering paths

Tool-call renderer (bashToolRenderer)

bashToolRenderer is used for tool-call messages (toolCall / toolResult):

  • collapsed mode shows visual-line-truncated preview,
  • expanded mode shows all currently available output text,
  • warning line includes truncation reason and local://<id>.log when truncated,
  • timeout value (from args) is shown in footer metadata line.

Caveat: full artifact expansion

BashRenderContext has isFullOutput, but current renderer context builder does not set it for bash tool results. Expanded view still uses the text already in result content (tail/truncated output) unless another caller provides full artifact content.

User bang-command component (BashExecutionComponent)

BashExecutionComponent is for user ! commands in interactive mode (not model tool calls):

  • streams chunks live,
  • collapsed preview keeps last 20 logical lines,
  • line clamp at 4000 visible columns per line,
  • shows truncation + artifact warnings when metadata is present,
  • marks cancelled/error/exit state separately.

This component is wired by CommandController.handleBashCommand() and fed from AgentSession.executeBash().

Interactive ! calls request configured user-shell execution. zsh/fish commands can run on a headless PTY and replay ANSI output in the component; bash uses the snapshot/embedded-shell path. A simple successful cd command runs through the persistent shell and can relocate the OMP session cwd; this relocation is refused while an agent response is streaming. !! excludes the execution message from model context.

Mode-specific behavior differences

This table covers finite-command execution; named-service routing is described separately above.

Surface Entry path PTY eligible Live output UX Error surfacing
Interactive tool call BashTool.execute Yes, when pty=true and UI exists and PI_NO_PTY!=1 PTY overlay (interactive) or streamed tail updates Tool errors become toolResult.isError
Print mode tool call BashTool.execute No (no UI context) No TUI overlay; output appears in event stream/final assistant text flow Same tool error mapping
RPC tool call (agent tooling) BashTool.execute Usually no UI -> non-PTY Structured tool events/results Same tool error mapping
Interactive bang command (!) AgentSession.executeBash + BashExecutionComponent Headless PTY for supported zsh/fish user shells; not the interactive overlay Dedicated bash execution component with PTY replay when used Controller catches exceptions and shows UI error
RPC bash command rpc-mode -> session.executeBash No Returns BashResult directly Consumer handles returned fields

Operational caveats

  • Interceptor only blocks commands when suggested tool is currently available in context.
  • If artifact allocation fails, truncation still occurs but no local://N.log back-reference is available.
  • Ordinary shell session cache entries are process-scoped with no size-based eviction. Cancelled/broken sessions are removed and quarantined; per-job :async: entries are removed after completion. Shells with live background children can be retained until those children exit.
  • Timeout shaping is backend-specific: local non-PTY and interactive-PTY timeouts return error results with details.timedOut; the client-bridge terminal creation/execution timeout paths throw ToolError. Non-timeout cancellations throw across these tool-call routes.

Implementation files