Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions bin/cli-proxy-workroots.test.mjs
Original file line number Diff line number Diff line change
@@ -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/<sha256(key)[:16]> 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/<first 16 hex of sha256(key)> 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');
}
});
25 changes: 17 additions & 8 deletions home/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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/<sha256(key)[:16]> 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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
];
};
Expand Down Expand Up @@ -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" ];
Expand Down