From 65df6226acb940dd62eea80a97f9bbe005383164 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 08:36:03 -0500 Subject: [PATCH] fix(proxies): move shared sessions.json to ~/.local/state/cli-proxies (#3067) The commandcode + claude-code session map was anchored to PROXY_DIR (bin/sessions.json), so the #2782 workdir move to ~/.local/state/cli-proxies missed it and the tracked checkout kept dirtying with every session write, blocking pm-git-sync. - commandcode-proxy: add COMMANDCODE_PROXY_SESSION_FILE override (mirrors claude-code pattern); default resolves to the shared state root sessions.json. - claude-code-proxy: SESSION_FILE default moves from PROXY_DIR to the same shared state-root path; env override kept. Both proxies share one map (mirrored key space) so both must resolve to the same file. - home/common: pin COMMANDCODE_PROXY_SESSION_FILE and CLAUDE_CODE_PROXY_SESSION_FILE to %h/.local/state/cli-proxies/sessions.json on both units, alongside the existing *_PROXY_WORKDIR pins. Codex audit: bin/codex-proxy.mjs already resolves sessions.json via sessionStateDir() = $XDG_STATE_HOME/codex-proxy or ~/.local/state/codex-proxy -- outside the checkout, no change needed. muse-proxy uses warm-sessions.json under WORKDIR (moved by #2782), untouched. No .gitignore entry for bin/sessions.json, by design: after this fix nothing may write there, and dirt in the primary checkout is the regression tripwire. Verified: node --check on both proxies; node --test bin/commandcode-proxy.test.mjs (1 pass) and bin/claude-code-proxy.test.mjs (5 pass); nix-instantiate --parse home/common/default.nix rc=0; default path expression resolves to /home/user/.local/state/cli-proxies/sessions.json. --- bin/claude-code-proxy.mjs | 17 +++++++++++++++-- bin/commandcode-proxy.mjs | 15 ++++++++++++++- home/common/default.nix | 10 ++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/bin/claude-code-proxy.mjs b/bin/claude-code-proxy.mjs index 221148ea15..744851da38 100644 --- a/bin/claude-code-proxy.mjs +++ b/bin/claude-code-proxy.mjs @@ -13,7 +13,8 @@ // // Conversations are keyed like commandcode-proxy keys them (explicit // session headers / body ids, else a hash of the first user message), and -// the key -> CLI session map persists to sessions.json in the proxy dir, +// the key -> CLI session map persists to sessions.json in the shared +// CLI-proxy state root (~/.local/state/cli-proxies, #3067), // so a proxy restart re-attaches with `--resume ` instead of // re-bootstrapping. A request whose history no longer matches the mapped // session (diverged prefix, extra mid-history user turns, lost session) @@ -49,7 +50,19 @@ const PROXY_DIR = path.dirname(fileURLToPath(import.meta.url)); const WORKROOT = process.env.CLAUDE_CODE_PROXY_WORKDIR || path.join(PROXY_DIR, 'work'); fs.mkdirSync(WORKROOT, { recursive: true }); -const SESSION_FILE = process.env.CLAUDE_CODE_PROXY_SESSION_FILE || path.join(PROXY_DIR, 'sessions.json'); +// Session map: SHARED with commandcode-proxy (mirrored key space), so +// both proxies must resolve to the SAME path -- the shared CLI-proxy +// state root, not the script's directory: bin/ is the tracked checkout, +// and every write there dirtied the primary clone and blocked +// pm-git-sync (#3067; missed by the #2782 workdir move). +const SESSION_FILE = + process.env.CLAUDE_CODE_PROXY_SESSION_FILE || + path.join( + process.env.XDG_STATE_HOME || path.join(os.homedir(), '.local', 'state'), + 'cli-proxies', + 'sessions.json' + ); +fs.mkdirSync(path.dirname(SESSION_FILE), { recursive: true }); const MAX_LIVE_SESSIONS = 32; const IDLE_TTL_MS = 2 * 60 * 60 * 1000; const TURN_TIMEOUT_MS = 300 * 1000; diff --git a/bin/commandcode-proxy.mjs b/bin/commandcode-proxy.mjs index 0c8d17d20c..3b48f9b69c 100644 --- a/bin/commandcode-proxy.mjs +++ b/bin/commandcode-proxy.mjs @@ -425,7 +425,20 @@ const childEnv = () => { // change: the automatic multi-turn key hashes the first USER message, // not messages[0] — OpenCode's messages[0] is its identical system // prompt for every session, which would collide all conversations. -const SESSION_FILE = path.join(PROXY_DIR, 'sessions.json'); +// Session map: SHARED with claude-code-proxy (mirrored key space), so +// both proxies must resolve to the SAME path. It lives in the shared +// CLI-proxy state root, not beside the script: bin/ is the tracked +// checkout, and every write there dirtied the primary clone and blocked +// pm-git-sync. #3067 completes the state-dir move of #2782, which +// covered the WORKDIR state (bin/work) but missed this PROXY_DIR anchor. +const SESSION_FILE = + process.env.COMMANDCODE_PROXY_SESSION_FILE || + path.join( + process.env.XDG_STATE_HOME || path.join(os.homedir(), '.local', 'state'), + 'cli-proxies', + 'sessions.json' + ); +fs.mkdirSync(path.dirname(SESSION_FILE), { recursive: true }); const MAX_SESSIONS = 200; const sessionStore = new Map(); diff --git a/home/common/default.nix b/home/common/default.nix index a1c0ce1db7..ffa66f5472 100644 --- a/home/common/default.nix +++ b/home/common/default.nix @@ -717,6 +717,14 @@ in # (warm-session maps, per-conversation workdirs) lives outside the # checkout. The scripts' default (bin/work inside ~/code) dirtied # the primary checkout with every session and blocked pm-git-sync. + # + # The commandcode + claude-code units additionally pin their shared + # key->session map via *_PROXY_SESSION_FILE to + # ~/.local/state/cli-proxies/sessions.json (#3067): that map was + # anchored to the scripts' own directory (bin/sessions.json) rather + # than the workroot, so the #2782 move missed it and the primary + # checkout kept dirtying. Both units MUST name the same file -- the + # two proxies share one map (mirrored key space). # Command Code CLI Local Proxy Bridge for OpenCode (Go plan: the hosted # Provider API rejects the Go plan, so OpenCode talks to the CLI via @@ -750,6 +758,7 @@ in "COMMANDCODE_PROXY_PORT=${toString commandcodeProxyPort}" "COMMANDCODE_PROXY_HOST=127.0.0.1" "COMMANDCODE_PROXY_WORKDIR=%h/.local/state/cli-proxies" + "COMMANDCODE_PROXY_SESSION_FILE=%h/.local/state/cli-proxies/sessions.json" "COMMANDCODE_PROXY_MAX_TURNS=1" "COMMANDCODE_PROXY_AGENCY_MAX_TURNS=8" ]; @@ -779,6 +788,7 @@ in "CLAUDE_CODE_PROXY_PORT=${toString claudeCodeProxyPort}" "CLAUDE_CODE_PROXY_HOST=127.0.0.1" "CLAUDE_CODE_PROXY_WORKDIR=%h/.local/state/cli-proxies" + "CLAUDE_CODE_PROXY_SESSION_FILE=%h/.local/state/cli-proxies/sessions.json" ]; }; Install.WantedBy = [ "default.target" ];