Skip to main content

append_cwd_capture

Function append_cwd_capture 

Source
fn append_cwd_capture(command_block: &str) -> String
Expand description

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.

The temp file is suffixed with $$ (this bash’s own PID), not a fixed name — reagentx P2 on this PR: two Bash tool calls for the same agent/instance can run concurrently (Claude issues parallel tool calls), and a shared fixed temp name meant two concurrent writers could interleave writes to the same temp file before either renamed it, corrupting the persisted cwd. Per-PID temp names make each writer’s temp-then-rename independent; the only remaining race is which completed rename lands last, which is an inherent, expected ambiguity for genuinely concurrent cds (no different from two parallel shells racing to cd a shared session) — not data corruption.