diff --git a/bin/cli-proxy-workroots.test.mjs b/bin/cli-proxy-workroots.test.mjs new file mode 100644 index 0000000000..8f560fabfb --- /dev/null +++ b/bin/cli-proxy-workroots.test.mjs @@ -0,0 +1,70 @@ +// Tests for the per-proxy CLI workroot isolation (#2785). +// Run: node --test bin/cli-proxy-workroots.test.mjs +// +// home/common/default.nix points all three CLI proxies at one shared +// WORKDIR before this fix: commandcode and claude-code derive identical +// sessions/ workdirs for overlapping keys, and the +// muse proxy uses the shared root itself as its session cwd and +// workspaceRoot. The units must give each proxy its own subtree while +// the commandcode/claude-code session map stays intentionally shared +// (#3067) — it is anchored to the state root, not to either workroot. +import assert from 'node:assert/strict'; +import crypto from 'node:crypto'; +import fs from 'node:fs'; +import path from 'node:path'; +import test from 'node:test'; +import { fileURLToPath } from 'node:url'; + +const binDir = path.dirname(fileURLToPath(import.meta.url)); +const nix = fs.readFileSync(path.join(binDir, '..', 'home', 'common', 'default.nix'), 'utf8'); + +const envValue = (name) => { + const m = nix.match(new RegExp(`"${name}=([^"]+)"`)); + assert.ok(m, `default.nix should set ${name}`); + return m[1]; +}; + +const WORKDIRS = { + commandcode: envValue('COMMANDCODE_PROXY_WORKDIR'), + claudeCode: envValue('CLAUDE_CODE_PROXY_WORKDIR'), + muse: envValue('MUSE_PROXY_WORKDIR'), +}; + +test('each CLI proxy unit gets a distinct workroot subtree', () => { + const values = Object.values(WORKDIRS); + assert.equal(new Set(values).size, 3, `workroots must be pairwise distinct: ${values}`); + for (const [proxy, dir] of Object.entries(WORKDIRS)) { + assert.ok( + dir.startsWith('%h/.local/state/cli-proxies/'), + `${proxy} workroot stays under the cli-proxies state root: ${dir}`, + ); + assert.notEqual( + dir, + '%h/.local/state/cli-proxies', + `${proxy} workroot must not be the shared root itself`, + ); + } +}); + +test('an overlapping conversation key derives disjoint session workdirs', () => { + // Both proxies derive sessions/ under + // their WORKROOT (commandcode-proxy.mjs / claude-code-proxy.mjs + // sessionWorkdirFor). Distinct workroots make the derived paths + // disjoint even when the key — and therefore the hash — is identical. + const key = 'an overlapping conversation key'; + const hash = crypto.createHash('sha256').update(key).digest('hex').slice(0, 16); + const ccDir = path.join(WORKDIRS.commandcode, 'sessions', hash); + const claudeDir = path.join(WORKDIRS.claudeCode, 'sessions', hash); + assert.notEqual(ccDir, claudeDir); +}); + +test('the shared commandcode/claude session map is unchanged (#3067)', () => { + const cc = envValue('COMMANDCODE_PROXY_SESSION_FILE'); + const claude = envValue('CLAUDE_CODE_PROXY_SESSION_FILE'); + assert.equal(cc, claude, 'both units must name the same session file'); + assert.equal(cc, '%h/.local/state/cli-proxies/sessions.json'); + // The shared map lives at the state root, outside every workroot. + for (const dir of Object.values(WORKDIRS)) { + assert.ok(!cc.startsWith(dir), 'session file must not sit inside a per-proxy workroot'); + } +}); diff --git a/home/common/default.nix b/home/common/default.nix index d559cd2962..6a71e60845 100644 --- a/home/common/default.nix +++ b/home/common/default.nix @@ -712,11 +712,20 @@ in # proxy, per the normal flake service pattern (cf. mcpx/openchamber). # CLI proxy workroots (muse / commandcode / claude-code): the three - # units below point their *_PROXY_WORKDIR overrides at the shared - # state dir ~/.local/state/cli-proxies, so runtime session state - # (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. + # units below point their *_PROXY_WORKDIR overrides at per-proxy + # subtrees of the state dir ~/.local/state/cli-proxies + # (cli-proxies/commandcode, cli-proxies/claude-code, + # cli-proxies/muse), so runtime session state (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 subtrees + # are per-proxy (#2785): commandcode and claude-code derive identical + # sessions/ workdirs for overlapping keys, and + # muse uses its workroot as the session cwd/workspaceRoot, so one + # shared root let the proxies mutate each other's session state. + # Only the workroots moved: pre-#2785 state under the shared root is + # left in place (unattributable between the proxies by construction) + # and each proxy re-warms fresh state in its own subtree. # # The commandcode + claude-code units additionally pin their shared # key->session map via *_PROXY_SESSION_FILE to @@ -757,7 +766,7 @@ in Environment = [ "COMMANDCODE_PROXY_PORT=${toString commandcodeProxyPort}" "COMMANDCODE_PROXY_HOST=127.0.0.1" - "COMMANDCODE_PROXY_WORKDIR=%h/.local/state/cli-proxies" + "COMMANDCODE_PROXY_WORKDIR=%h/.local/state/cli-proxies/commandcode" "COMMANDCODE_PROXY_SESSION_FILE=%h/.local/state/cli-proxies/sessions.json" "COMMANDCODE_PROXY_MAX_TURNS=1" "COMMANDCODE_PROXY_AGENCY_MAX_TURNS=8" @@ -787,7 +796,7 @@ in Environment = [ "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_WORKDIR=%h/.local/state/cli-proxies/claude-code" "CLAUDE_CODE_PROXY_SESSION_FILE=%h/.local/state/cli-proxies/sessions.json" ]; }; @@ -825,7 +834,7 @@ in Environment = [ "MUSE_PROXY_PORT=${toString museProxyPort}" "MUSE_PROXY_HOST=127.0.0.1" - "MUSE_PROXY_WORKDIR=%h/.local/state/cli-proxies" + "MUSE_PROXY_WORKDIR=%h/.local/state/cli-proxies/muse" ]; }; Install.WantedBy = [ "default.target" ];