Expand description
Where an agent’s Bash calls start: the working directory a call ends in is
kept for the next one, per instance and, for a Claude Code subagent, per
subagent (docs/specs/SPEC_BASHWRAP_PER_AGENT_CWD_2026_10_18.md). Moved out
of bash_wrap.rs, which runs the calls.
Structs§
- CwdState 🔒
- Resolves the cwd-persistence file location and any previously-restored
starting directory once per
execinvocation.
Constants§
- AGENT_
STATE_ 🔒MARK - AGENT_
STATE_ 🔒MAX_ AGE - A subagent’s state file untouched this long belongs to one that ended.
- CWD_
STATE_ 🔒ENV - Env var bashwrap sets on the child bash process (never interpolated
into the script text, so no path-quoting concerns) pointing at the state
file
append_cwd_capture’s appended script writes the ending$PWDto. - CWD_
STATE_ 🔒FILE_ OVERRIDE_ ENV - Optional override of the state file’s own path, read from bashwrap’s own
process env (distinct from
CWD_STATE_ENV, which bashwrap sets rather than reads). Exists so tests can point persistence at a tempdir instead of a real agent’s~/.agentmuxstate, and as an escape hatch for manual debugging.
Functions§
- agent_
state_ 🔒path - A subagent’s state file: the instance’s, with the agent in its name
(
<instance>.agent-<agent>.cwd), so it sorts and sweeps with it. - append_
cwd_ 🔒capture - Appends bookkeeping to
command_blockthat (1) captures its real exit code before running anything else, (2) persists the shell’s ending$PWDto$AGENTMUX_BASHWRAP_CWD_STATEif that env var is set (skipped entirely otherwise — e.g. whenCwdState::pathwasNone), written via a temp-file-then-rename so a mid-write crash can’t leave a half-written state file, and (3) re-exits with the captured code so this bookkeeping never changes the command’s real exit status.command_blockmust be a brace group or bare command, not something that already ends in its ownexit. - cwd_
state_ 🔒path - Per-instance state file path. Keyed by
AGENTMUX_INSTANCE_SLUG— set byagent-model.tsto a per-launch slug that also seeds that instance’s own unique working directory (${agentmuxHome()}/agents/${instanceSlug}) — NOTAGENTMUX_AGENT_ID, which is the definition-wide slug shared by every named instance launched from the same agent definition (codex P1 on this PR: keying byAGENTMUX_AGENT_IDmade two concurrently-running instances of the same definition silently share one cwd file, so one instance’scdcould redirect the other’s next command into the wrong worktree). Falls back toAGENTMUX_AGENT_IDfor older callers that only set that, then to a sanitized form of this process’s own cwd (always the instance’s fixed home directory either way) for the rare case neither is set, e.g. a manual invocation outside AgentMux. - restore_
cwd 🔒 - Read back a previously-persisted cwd, if the file exists, is non-empty, and still names a real directory (it may not — the directory could have been deleted since, or this is the first call for this agent).
- sanitize_
state_ 🔒key - Filesystem-safe form of an arbitrary key for use as a filename component.
- sweep_
agent_ 🔒state_ files - Delete subagents’ state files in
dirnot written formax_age. Never an instance’s own file (no agent mark), and never fatal.