Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

Merge PR #3490: reserve lifecycle time for Stop persistence

haelyrad05a507c8bc2
diff --cc tests/hooks/claude-config-dir-routing.test.js
index d82f41204,000000000..7f41435e7
mode 100644,000000..100644
--- a/tests/hooks/claude-config-dir-routing.test.js
+++ b/tests/hooks/claude-config-dir-routing.test.js
@@@ -1,282 -1,0 +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.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');
++ console.log('\nTesting 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/hooks/session-end.test.js
index 05e74eeac,5b745705c..93692e38a
--- a/tests/hooks/session-end.test.js
+++ b/tests/hooks/session-end.test.js
@@@ -237,6 -245,114 +245,114 @@@ function runTests()
}
}) ? passed++ : failed++);
+ (test('persists mechanical resume state when the LLM budget is exhausted', () => {
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-session-budget-'));
+ try {
+ const uuid = '12345678-1234-4234-8234-123456789abc';
+ const transcript = path.join(home, `${uuid}.jsonl`);
+ fs.writeFileSync(transcript, JSON.stringify({ type: 'user', content: 'Keep this task resumable' }) + '\n');
+ const bin = path.join(home, 'empty-bin');
+ fs.mkdirSync(bin);
+ const res = runHook(home, transcript, {
+ ECC_LLM_SUMMARY_INTERVAL: '1', ECC_HOOK_DEADLINE_MS: '1',
+ ECC_SKIP_LLM_SUMMARY: '', ECC_LLM_SUMMARY_SUBPROCESS: '', PATH: bin
+ });
+ assert.strictEqual(res.status, 0, res.stderr);
+ assert.match(res.stderr, /LLM summary skipped.*insufficient lifecycle time budget/);
+ assert.doesNotMatch(res.stderr, /LLM summary failed/);
+ assert.match(fs.readFileSync(sessionFileFor(home, uuid), 'utf8'), /Keep this task resumable/);
+ } finally {
+ fs.rmSync(home, { recursive: true, force: true });
+ }
+ }) ? passed++ : failed++);
+
+ (test('logs an explicit LLM disable as skipped and persists mechanical resume state', () => {
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-session-skip-'));
+ try {
+ const uuid = '12345678-1234-4234-8234-123456789abc';
+ const transcript = path.join(home, `${uuid}.jsonl`);
+ const bin = path.join(home, 'empty-bin');
+ fs.mkdirSync(bin);
+ fs.writeFileSync(transcript, JSON.stringify({ type: 'user', content: 'Save context with LLM disabled' }) + '\n');
+ const res = runHook(home, transcript, {
+ ECC_LLM_SUMMARY_INTERVAL: '1', ECC_SKIP_LLM_SUMMARY: '1',
+ ECC_LLM_SUMMARY_SUBPROCESS: '', PATH: bin
+ });
+ assert.strictEqual(res.status, 0, res.stderr);
+ assert.match(res.stderr, /LLM summary skipped.*ECC_SKIP_LLM_SUMMARY/);
+ assert.doesNotMatch(res.stderr, /LLM summary failed/);
+ assert.match(fs.readFileSync(sessionFileFor(home, uuid), 'utf8'), /Save context with LLM disabled/);
+ } finally {
+ fs.rmSync(home, { recursive: true, force: true });
+ }
+ }) ? passed++ : failed++);
+
+ for (const inherited of ['', String(Date.now() + 300000), '1']) {
+ (test(`direct legacy runner bounds the deadline ${inherited || '(unset)'}`, () => {
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-runner-budget-'));
+ try {
+ const relative = 'legacy.js';
+ fs.writeFileSync(path.join(home, relative), "process.stdout.write(JSON.stringify({deadline:Number(process.env.ECC_HOOK_DEADLINE_MS),now:Date.now()}));\n");
+ const runner = path.resolve(__dirname, '../../scripts/hooks/run-with-flags.js');
+ const res = spawnSync(process.execPath, [runner, 'session:stop:session-end', relative, 'standard'], {
+ input: '{}', encoding: 'utf8', timeout: 5000,
+ env: {
+ ...isolatedHomeEnv(home), CLAUDE_PLUGIN_ROOT: home,
- ECC_HOOK_PROFILE: 'standard', ECC_DISABLED_HOOKS: '', ECC_HOOK_DRY_RUN: '',
++ ECC_HOOK_PROFILE: 'standard', ECC_DISABLED_HOOKS: '', ECC_DRY_RUN: '0', ECC_HOOKS_ENABLED: 'true',
+ ECC_HOOK_DEADLINE_MS: inherited
+ }
+ });
+ assert.strictEqual(res.status, 0, res.stderr);
+ if (inherited === '1') {
+ assert.strictEqual(res.stdout, '');
+ assert.match(res.stderr, /time budget exhausted/);
+ } else {
+ const result = JSON.parse(res.stdout);
+ assert.ok(result.deadline > result.now);
+ assert.ok(result.deadline - result.now <= 30000, 'Inherited far-future deadlines must not extend the runner budget');
+ }
+ } finally {
+ fs.rmSync(home, { recursive: true, force: true });
+ }
+ }) ? passed++ : failed++);
+ }
+
+ // A local stand-in exercises the actual bootstrap -> runner -> Stop hook ->
+ // child timeout chain without invoking Claude or using authentication.
+ if (process.platform !== 'win32') {
+ (test('times out a slow summarizer before lifecycle expiry and saves fallback', () => {
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-session-budget-'));
+ try {
+ const uuid = '12345678-1234-4234-8234-123456789abc';
+ const transcript = path.join(home, `${uuid}.jsonl`);
+ const bin = path.join(home, 'bin');
+ fs.mkdirSync(bin);
+ fs.writeFileSync(transcript, JSON.stringify({ type: 'user', content: 'Persist after a slow summary' }) + '\n');
+ fs.writeFileSync(path.join(bin, 'claude'), `#!${process.execPath}\nrequire('fs').writeFileSync(${JSON.stringify(path.join(home, 'started'))}, 'started');\nsetTimeout(() => console.log('late summary'), 5000);\n`, { mode: 0o755 });
+ const root = path.resolve(__dirname, '../..');
+ const bootstrap = path.join(root, 'scripts/hooks/lifecycle-hook-bootstrap.js');
+ const res = spawnSync(process.execPath, [bootstrap, 'session:stop:session-end', 'scripts/hooks/session-end.js', 'minimal,standard,strict', '2500'], {
+ encoding: 'utf8', input: JSON.stringify({ transcript_path: transcript }),
+ env: {
+ ...isolatedHomeEnv(home), PATH: bin,
+ CLAUDE_PLUGIN_ROOT: root, ECC_HOOK_PROFILE: 'standard',
+ ECC_LLM_SUMMARY_INTERVAL: '1', ECC_SKIP_LLM_SUMMARY: '',
+ ECC_LLM_SUMMARY_SUBPROCESS: '', ECC_HOOK_DEADLINE_MS: '1',
- ECC_DISABLED_HOOKS: '', ECC_HOOK_DRY_RUN: ''
++ ECC_DISABLED_HOOKS: '', ECC_DRY_RUN: '0', ECC_HOOKS_ENABLED: 'true'
+ },
+ timeout: 7000
+ });
+ assert.strictEqual(res.status, 0, res.stderr);
+ assert.ok(fs.existsSync(path.join(home, 'started')), 'Fresh bootstrap must replace an inherited expired deadline');
+ assert.match(res.stderr, /LLM summary failed; falling back/);
+ assert.doesNotMatch(res.stderr, /lifecycle runner failed/);
+ assert.match(fs.readFileSync(sessionFileFor(home, uuid), 'utf8'), /Persist after a slow summary/);
+ } finally {
+ fs.rmSync(home, { recursive: true, force: true });
+ }
+ }) ? passed++ : failed++);
+ }
+
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);
}