Persist the prompt file so claude --bg sessions can respawn - #9
Open
Iteratrix wants to merge 1 commit into
Open
Conversation
claude-mode wrote the assembled prompt to a mkdtemp directory and removed it once claude exited. That assumed claude reads the file exactly once, while claude-mode is still alive. Background sessions break that assumption: `claude-mode <preset> -- --bg` returns immediately, and the session is then hosted by Claude Code's supervisor, which re-executes the worker with the original argv after idle stops, `claude respawn`, binary updates, and reboot recovery. Every one of those re-reads --system-prompt-file, by then already deleted. Write the prompt to $XDG_STATE_HOME/claude-mode/prompts/<sha256[:16]>.md (default ~/.local/state) and never delete it. Content addressing means reruns of the same mode in the same environment share one file. This also stops build-prompt.ts leaking one /tmp/claude-mode-* directory per call, which it never cleaned up. createCliRunner gains an optional env overlay: under bun, execSync does not see process.env mutations made in the test body.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
claude-modewrites the assembled prompt to amkdtempdirectory, spawnsclaude --system-prompt-file <tmp>, waits for it to exit, thenrmSyncs the directory. That assumes Claude Code reads the file exactly once, whileclaude-modeis still alive.Claude Code's background sessions break that assumption. With
claude-mode <preset> -- --bg ...,claudereturns within a second and hands the session to the background supervisor, which outlivesclaude-modeand re-executes the worker with the original argv:claude respawn <id>(and the automatic respawn after a Claude Code update)Every one of those re-reads
--system-prompt-file, which by then has been deleted. Reproduced on Claude Code 2.1.263: hiding the prompt file and runningclaude respawngivesRestoring the file and respawning again brings the session back. So the file has to outlive the launcher.
(
build-prompt.tshad the opposite problem: it never cleaned up, so every call leaked a/tmp/claude-mode-*directory. I had 77 of them on one machine.)Change
writeTempPromptbecomeswritePromptFile: the prompt goes to$XDG_STATE_HOME/claude-mode/prompts/<sha256[:16]>.md(default~/.local/state/...) and is never deleted. Content addressing means reruns of the same mode in the same environment share one file, so the directory stays small (~16 KB per distinct prompt).cli.tsdrops the post-exit cleanup;build-prompt.tsuses the same path, which also fixes its leak.createCliRunnergains an optionalenvoverlay. Under bun,execSyncdoes not seeprocess.envmutations made in the test body, which the new e2e test needs.I chose the state dir over the cache dir because cache directories are conventionally safe to wipe, and a wiped prompt file turns into a crashed respawn hours or days later. Happy to switch to a different location or add pruning if you'd rather; I kept the change to the minimum that makes
--bgwork.Verification
bunx tsc --noEmitclean;bun test726 pass with the same 4 pre-existing model-fixture failures asmainon my machine (my Claude settings select a different model than the fixtures expect).claude-mode tinker -- --bg -n cm-bgtest "..."starts, the worker's argv points at the state-dir file, the session answers,claude respawnsucceeds, and the negative control above fails exactly as expected when the file is missing.🤖 Generated with Claude Code