The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
Merge PR #3416: route hook state through Claude profiles
haelyra7753a55d0845
diff --cc scripts/lib/agent-data-home.jsindex 98cd0a84b,8362c31b3..48d9b4294--- a/scripts/lib/agent-data-home.js+++ b/scripts/lib/agent-data-home.js@@@ -92,8 -93,15 +93,10 @@@ function getDefaultCursorAgentDataHome(}function getDefaultClaudeAgentDataHome() {- const fromClaudeConfigDir = expandHomePath(process.env.CLAUDE_CONFIG_DIR);- // Claude Code points every profile-local path at CLAUDE_CONFIG_DIR when it is- // set, so a session launched under a second profile must not keep reading and- // writing the default ~/.claude. This moves the trusted root with it, which is- // the same trust ECC_AGENT_DATA_HOME already gets: both come from whoever- // launched the session, not from the project.- const configDir = process.env[CLAUDE_CONFIG_DIR_ENV];- if (configDir && String(configDir).trim().length > 0) {- return expandHomePath(String(configDir).trim(), process.cwd());- }++ // Launcher-owned CLAUDE_CONFIG_DIR moves the Claude trusted root with its++ // profile. Project-provided paths must still remain inside that root.++ const fromClaudeConfigDir = expandHomePath(process.env[CLAUDE_CONFIG_DIR_ENV]);+ if (fromClaudeConfigDir) return fromClaudeConfigDir;return path.join(getHomeDirFromEnv(), DEFAULT_CLAUDE_DIR_NAME);}diff --cc tests/hooks/claude-config-dir-routing.test.jsindex 000000000,8583cd83a..d82f41204mode 000000,100644..100644--- a/tests/hooks/claude-config-dir-routing.test.js+++ b/tests/hooks/claude-config-dir-routing.test.js@@@ -1,0 -1,282 +1,282 @@@+ /**+ * Tests that the hooks route profile state through CLAUDE_CONFIG_DIR+ *+ * Run with: node tests/hooks/claude-config-dir-routing.test.js+ *+ * These spawn the hooks rather than calling the shared resolver. The four hooks+ * below cannot require scripts/lib/agent-data-home.js: managed installs ship a+ * top-level scripts/ without scripts/lib/ (#3259), so each carries its own copy+ * of the resolver. A resolver test therefore cannot show where a hook writes.+ */++ const assert = require('assert');+ const path = require('path');+ const fs = require('fs');+ const os = require('os');+ const { spawnSync } = require('child_process');++ const hooksDir = path.join(__dirname, '..', '..', 'scripts', 'hooks');+ const libSessions = path.join(__dirname, '..', '..', 'scripts', 'lib', 'plan-canvas', 'sessions.js');++ function test(name, fn) {+ try {+ fn();+ console.log(` ✓ ${name}`);+ return true;+ } catch (err) {+ console.log(` ✗ ${name}`);+ console.log(` Error: ${err.message}`);+ return false;+ }+ }++ function makeTempDir() {- return fs.mkdtempSync(path.join(os.tmpdir(), 'config-dir-routing-'));++ return fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'config-dir-routing-')));+ }++ /**+ * A profile that is not the home directory, so a hook that ignored+ * CLAUDE_CONFIG_DIR would write somewhere this returns.+ */+ function makeProfile() {+ const root = makeTempDir();+ return {+ home: path.join(root, 'home'),+ profile: path.join(root, 'profile', '.claude-work'),+ };+ }++ // No ECC_* override is passed anywhere here: an override would route the hook+ // before it reached the CLAUDE_CONFIG_DIR fallback under test.+ function runHook(hookFile, { profile, home, stdin = '{}', args = [], env = {} }) {+ fs.mkdirSync(home, { recursive: true });+ return spawnSync('node', [path.join(hooksDir, hookFile), ...args], {+ input: stdin,+ encoding: 'utf8',+ env: {+ ...process.env,+ HOME: home,+ USERPROFILE: home,+ CLAUDE_CONFIG_DIR: profile,+ ECC_PLAN_CANVAS_STATE_DIR: undefined,+ ECC_MCP_HEALTH_STATE_PATH: undefined,+ ...env,+ },+ });+ }++ /**+ * plan-canvas-pending.js exports run() and is required by the lifecycle hook+ * bootstrap rather than spawned, so it is driven here in a child process. The+ * child is what makes CLAUDE_CONFIG_DIR meaningful: requiring it in-process,+ * the way tests/hooks/plan-canvas-pending-hook.test.js does with+ * ECC_PLAN_CANVAS_STATE_DIR, cannot reach the fallback under test.+ */+ function runPendingHook({ profile, home, payload }) {+ fs.mkdirSync(home, { recursive: true });+ const result = spawnSync('node', ['-e', `+ require(${JSON.stringify(path.join(hooksDir, 'plan-canvas-pending.js'))})+ .run(${JSON.stringify(JSON.stringify(payload))})+ .then(out => process.stdout.write(out.stdout));+ `], {+ encoding: 'utf8',+ env: {+ ...process.env,+ HOME: home,+ USERPROFILE: home,+ CLAUDE_CONFIG_DIR: profile,+ ECC_PLAN_CANVAS_STATE_DIR: undefined,+ },+ });+ assert.strictEqual(result.status, 0, `pending hook failed: ${result.stderr}`);+ return result.stdout;+ }++ function writeSessions(stateDir, sessions) {+ fs.mkdirSync(stateDir, { recursive: true });+ fs.writeFileSync(path.join(stateDir, 'sessions.json'), JSON.stringify({ sessions }, null, 2));+ }++ function runTests() {+ console.log('\n🧪 Testing CLAUDE_CONFIG_DIR routing in hooks\n');+ let passed = 0;+ let failed = 0;+ const record = ok => { if (ok) passed++; else failed++; };++ console.log('post-bash-command-log.js');++ record(test('writes the audit log under CLAUDE_CONFIG_DIR', () => {+ const { home, profile } = makeProfile();+ runHook('post-bash-command-log.js', {+ home,+ profile,+ args: ['audit'],+ stdin: JSON.stringify({ tool_name: 'Bash', tool_input: { command: 'echo routed' } }),+ });+ const log = path.join(profile, 'bash-commands.log');+ assert.ok(fs.existsSync(log), `expected ${log} to exist`);+ assert.match(fs.readFileSync(log, 'utf8'), /echo routed/);+ assert.ok(+ !fs.existsSync(path.join(home, '.claude', 'bash-commands.log')),+ 'the log must not also land in ~/.claude'+ );+ }));++ record(test('writes the cost log under CLAUDE_CONFIG_DIR', () => {+ const { home, profile } = makeProfile();+ runHook('post-bash-command-log.js', {+ home,+ profile,+ args: ['cost'],+ stdin: JSON.stringify({ tool_name: 'Bash', tool_input: { command: 'echo costed' } }),+ });+ assert.match(fs.readFileSync(path.join(profile, 'cost-tracker.log'), 'utf8'), /echo costed/);+ }));++ record(test('falls back to ~/.claude when CLAUDE_CONFIG_DIR is unset', () => {+ const { home } = makeProfile();+ fs.mkdirSync(home, { recursive: true });+ spawnSync('node', [path.join(hooksDir, 'post-bash-command-log.js'), 'audit'], {+ input: JSON.stringify({ tool_name: 'Bash', tool_input: { command: 'echo default' } }),+ encoding: 'utf8',+ env: { ...process.env, HOME: home, USERPROFILE: home, CLAUDE_CONFIG_DIR: undefined },+ });+ assert.match(+ fs.readFileSync(path.join(home, '.claude', 'bash-commands.log'), 'utf8'),+ /echo default/+ );+ }));++ record(test('expands a tilde profile instead of creating a "~" directory', () => {+ // path.resolve('~/.claude-work') would make ./~/.claude-work under the cwd,+ // splitting the hook off from scripts/lib/agent-data-home.js, which expands.+ const { home } = makeProfile();+ runHook('post-bash-command-log.js', {+ home,+ profile: '~/.claude-work',+ args: ['audit'],+ stdin: JSON.stringify({ tool_name: 'Bash', tool_input: { command: 'echo tilde' } }),+ });+ assert.match(+ fs.readFileSync(path.join(home, '.claude-work', 'bash-commands.log'), 'utf8'),+ /echo tilde/+ );+ }));++ console.log('\nmcp-health-check.js');++ record(test('writes the health cache under CLAUDE_CONFIG_DIR', () => {+ const { home, profile } = makeProfile();+ fs.mkdirSync(home, { recursive: true });+ // Without a configured server the hook skips the probe and never writes.+ // The command does not exist, so the probe fails and the result is cached.+ fs.writeFileSync(path.join(home, '.claude.json'), JSON.stringify({+ mcpServers: { routing_probe: { command: 'ecc-no-such-mcp-binary', args: [] } },+ }));+ runHook('mcp-health-check.js', {+ home,+ profile,+ stdin: JSON.stringify({ tool_name: 'mcp__routing_probe__ping', tool_input: {} }),+ });+ const cache = path.join(profile, 'mcp-health-cache.json');+ assert.ok(fs.existsSync(cache), `expected ${cache} to exist`);+ assert.ok(+ !fs.existsSync(path.join(home, '.claude', 'mcp-health-cache.json')),+ 'the cache must not also land in ~/.claude'+ );+ }));++ console.log('\nplan-canvas-sessions.js (SessionStart)');++ record(test('reports open sessions recorded under CLAUDE_CONFIG_DIR', () => {+ const { home, profile } = makeProfile();+ writeSessions(path.join(profile, 'plan-canvas'), {+ k1: { file: '/work/plan.md', status: 'open', pendingFeedback: [] },+ });+ const result = runHook('plan-canvas-sessions.js', { home, profile });+ assert.match(result.stdout, /\/work\/plan\.md/);+ }));++ record(test('does not read ~/.claude when a profile is set', () => {+ const { home, profile } = makeProfile();+ // Only the home copy holds a session. A hook that ignored the profile+ // would announce it; the right answer here is silence.+ writeSessions(path.join(home, '.claude', 'plan-canvas'), {+ k1: { file: '/work/leaked.md', status: 'open', pendingFeedback: [] },+ });+ const result = runHook('plan-canvas-sessions.js', { home, profile });+ // Silence is the assertion here, and a hook that crashed before writing is+ // also silent, so the exit status has to be checked first.+ assert.strictEqual(result.status, 0, `SessionStart failed: ${result.stderr}`);+ assert.strictEqual(result.stdout.trim(), '');+ }));++ console.log('\nplan-canvas-pending.js (Stop)');++ record(test('blocks on feedback queued under CLAUDE_CONFIG_DIR', () => {+ const { home, profile } = makeProfile();+ const cwd = makeTempDir();+ const artifact = path.join(cwd, 'plan.md');+ fs.writeFileSync(artifact, '# plan\n');+ writeSessions(path.join(profile, 'plan-canvas'), {+ k1: {+ key: 'k1',+ file: artifact,+ status: 'feedback',+ pendingFeedback: [{ kind: 'verdict', verdict: 'request-changes', text: 'tighten step 2' }],+ },+ });+ const decision = JSON.parse(runPendingHook({ home, profile, payload: { cwd } }));+ assert.strictEqual(decision.decision, 'block');+ assert.match(decision.reason, /tighten step 2/);+ }));++ console.log('\nproducer to consumer');++ record(test('a session the library records is found by both hooks', () => {+ // The regression this guards: the library wrote ~/.claude/plan-canvas while+ // the hooks read CLAUDE_CONFIG_DIR/plan-canvas, so under a second profile+ // SessionStart missed open reviews and Stop let a turn end with feedback+ // still queued. Both ends are exercised with no state-directory override.+ const { home, profile } = makeProfile();+ const cwd = makeTempDir();+ const artifact = path.join(cwd, 'design.md');+ fs.writeFileSync(artifact, '# design\n');+ fs.mkdirSync(home, { recursive: true });++ const producer = spawnSync('node', ['-e', `+ const store = require(${JSON.stringify(libSessions)}).createSessionStore();+ const { session } = store.open(${JSON.stringify(artifact)});+ store.queueFeedback(session.key, [{ kind: 'verdict', verdict: 'request-changes', text: 'needs a rollback plan' }]);+ `], {+ encoding: 'utf8',+ env: {+ ...process.env,+ HOME: home,+ USERPROFILE: home,+ CLAUDE_CONFIG_DIR: profile,+ ECC_PLAN_CANVAS_STATE_DIR: undefined,+ },+ });+ assert.strictEqual(producer.status, 0, `producer failed: ${producer.stderr}`);+ assert.ok(+ fs.existsSync(path.join(profile, 'plan-canvas', 'sessions.json')),+ 'the library must record the session under the profile'+ );++ const sessionStart = runHook('plan-canvas-sessions.js', { home, profile });+ assert.match(sessionStart.stdout, /design\.md/, 'SessionStart must see the open review');++ const decision = JSON.parse(runPendingHook({ home, profile, payload: { cwd } }));+ assert.strictEqual(decision.decision, 'block');+ assert.match(decision.reason, /needs a rollback plan/);+ }));++ console.log(`\n${'='.repeat(50)}`);+ console.log(`Passed: ${passed}`);+ console.log(`Failed: ${failed}`);+ console.log('='.repeat(50));+ if (failed > 0) process.exit(1);+ }++ runTests();diff --cc tests/lib/agent-data-home.test.jsindex 0c507c1dd,82a744b77..f4d255d59--- a/tests/lib/agent-data-home.test.js+++ b/tests/lib/agent-data-home.test.js@@@ -570,6 -497,105 +570,107 @@@ function runTests()});})) passed++; else failed++;+ if (test('getDefaultClaudeAgentDataHome follows CLAUDE_CONFIG_DIR', () => {+ // Claude Code points every profile-local path at CLAUDE_CONFIG_DIR. Ignoring+ // it writes this profile's session data, logs and metrics into the default+ // ~/.claude, where another profile reads them back.+ withIsolatedCwd(() => {+ withEnv({+ CLAUDE_CONFIG_DIR: path.join(os.tmpdir(), 'ecc-profile-x'),+ ECC_AGENT_DATA_HOME: undefined,+ }, () => {+ const agentDataHome = require('../../scripts/lib/agent-data-home');+ assert.strictEqual(+ agentDataHome.getDefaultClaudeAgentDataHome(),+ path.join(os.tmpdir(), 'ecc-profile-x')+ );+ });+ });+ })) passed++; else failed++;++ if (test('getDefaultClaudeAgentDataHome falls back to ~/.claude without the env', () => {+ withIsolatedCwd(() => {+ withEnv({ CLAUDE_CONFIG_DIR: undefined, ECC_AGENT_DATA_HOME: undefined }, () => {+ const agentDataHome = require('../../scripts/lib/agent-data-home');+ assert.strictEqual(+ agentDataHome.getDefaultClaudeAgentDataHome(),+ path.join(os.homedir(), '.claude')+ );+ });+ });+ })) passed++; else failed++;++ if (test('a blank CLAUDE_CONFIG_DIR is ignored rather than resolving to cwd', () => {+ // An empty or whitespace value would otherwise resolve to the process cwd and+ // scatter profile data through whatever directory the session started in.+ withIsolatedCwd(() => {+ withEnv({ CLAUDE_CONFIG_DIR: ' ', ECC_AGENT_DATA_HOME: undefined }, () => {+ const agentDataHome = require('../../scripts/lib/agent-data-home');+ assert.strictEqual(+ agentDataHome.getDefaultClaudeAgentDataHome(),+ path.join(os.homedir(), '.claude')+ );+ });+ });+ })) passed++; else failed++;+- if (test('security: the trusted root moves with CLAUDE_CONFIG_DIR but still confines the project config', () => {++ if (test('security: CLAUDE_CONFIG_DIR adds a trusted profile root while preserving explicit sharing', () => {+ // getDefaultClaudeAgentDataHome() is not only a default: it is one of the two+ // roots resolveAllowedProjectConfigHome() will accept. Following+ // CLAUDE_CONFIG_DIR moves that boundary with the profile, so the containment+ // it enforces has to hold at the new location and nowhere else.+ const projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-agent-data-home-cfgdir-'));+ const homeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-agent-data-home-cfgdir-user-'));+ const profileDir = path.join(homeDir, '.claude-profile-x');+ const configPath = path.join(projectDir, '.cursor', 'ecc-agent-data.json');+ fs.mkdirSync(path.dirname(configPath), { recursive: true });++ try {+ withEnv({+ ECC_AGENT_DATA_HOME: undefined,+ CLAUDE_CONFIG_DIR: profileDir,+ HOME: homeDir,+ USERPROFILE: undefined,+ }, () => {+ const agentDataHome = require('../../scripts/lib/agent-data-home');++ // Inside the relocated root: accepted.+ fs.writeFileSync(+ configPath,+ JSON.stringify({ agentDataHome: path.join(profileDir, 'nested') }),+ 'utf8'+ );+ // readProjectConfigAt canonicalizes, and macOS reaches os.tmpdir()+ // through a symlink, so compare against the resolved home.+ assert.strictEqual(+ agentDataHome.readProjectConfigAt(configPath),+ path.join(fs.realpathSync(homeDir), '.claude-profile-x', 'nested')+ );+- // The old default is no longer a root, and neither is anything outside- // the new one. A traversal out of the relocated root stays rejected.++ // Preserve the explicit ~/.claude sharing root shipped in #3479.++ // The profile root adds a trusted location; arbitrary home paths and++ // traversal out of that profile still stay rejected.++ fs.writeFileSync(configPath, JSON.stringify({ agentDataHome: path.join(homeDir, '.claude') }), 'utf8');++ assert.strictEqual(agentDataHome.readProjectConfigAt(configPath), path.join(fs.realpathSync(homeDir), '.claude'));+ const outside = [- path.join(homeDir, '.claude'),+ path.join(profileDir, '..', 'escaped'),+ path.join(homeDir, 'arbitrary-agent-data'),+ ];+ for (const candidate of outside) {+ fs.writeFileSync(configPath, JSON.stringify({ agentDataHome: candidate }), 'utf8');+ const { result, messages } = captureConsoleErrors(+ () => agentDataHome.readProjectConfigAt(configPath)+ );+ assert.strictEqual(result, null, `expected rejection for ${candidate}`);+ assert.ok(messages.some(message => message.includes('Ignoring unsafe agent data project config')));+ }+ });+ } finally {+ fs.rmSync(projectDir, { recursive: true, force: true });+ fs.rmSync(homeDir, { recursive: true, force: true });+ }+ })) passed++; else failed++;+console.log(`\n=== Test Results ===\nPassed: ${passed}\nFailed: ${failed}\n`);if (failed > 0) process.exit(1);}