Skip to main content

Module cwd_state

Module cwd_state 

Source
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 exec invocation.

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 $PWD to.
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 ~/.agentmux state, 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_block that (1) captures its real exit code before running anything else, (2) persists the shell’s ending $PWD to $AGENTMUX_BASHWRAP_CWD_STATE if that env var is set (skipped entirely otherwise — e.g. when CwdState::path was None), 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_block must be a brace group or bare command, not something that already ends in its own exit.
cwd_state_path 🔒
Per-instance state file path. Keyed by AGENTMUX_INSTANCE_SLUG — set by agent-model.ts to a per-launch slug that also seeds that instance’s own unique working directory (${agentmuxHome()}/agents/${instanceSlug}) — NOT AGENTMUX_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 by AGENTMUX_AGENT_ID made two concurrently-running instances of the same definition silently share one cwd file, so one instance’s cd could redirect the other’s next command into the wrong worktree). Falls back to AGENTMUX_AGENT_ID for 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 dir not written for max_age. Never an instance’s own file (no agent mark), and never fatal.