mirror of
https://github.com/glittercowboy/get-shit-done
synced 2026-04-26 01:35:29 +02:00
* fix(hooks): detect Claude Code via stdin session_id, not filtered env (#2520) The #2344 fix assumed `CLAUDECODE` would propagate to hook subprocesses. On Claude Code v2.1.116 it doesn't — Claude Code applies a separate env filter to PreToolUse hook commands that drops bare CLAUDECODE and CLAUDE_SESSION_ID, keeping only CLAUDE_CODE_*-prefixed vars plus CLAUDE_PROJECT_DIR. As a result every Edit/Write on an existing file produced a redundant READ-BEFORE-EDIT advisory inside Claude Code. Use `data.session_id` from the hook's stdin JSON as the primary Claude Code signal (it's part of Claude Code's documented PreToolUse hook-input schema). Keep CLAUDE_CODE_ENTRYPOINT / CLAUDE_CODE_SSE_PORT env checks as propagation-verified fallbacks, and keep the legacy CLAUDE_SESSION_ID / CLAUDECODE checks for back-compat and future-proofing. Add tests/bug-2520-read-guard-hook-subprocess-env.test.cjs, which spawns the hook with an env mirroring the actual Claude Code hook-subprocess filter. Extend the legacy test harnesses to also strip the propagation-verified CLAUDE_CODE_* vars so positive-path tests keep passing when the suite itself runs inside a Claude Code session (same class of leak as #2370 / PR #2375, now covering the new detection signals). Non-Claude-host behavior (OpenCode / MiniMax) is unchanged: with no `session_id` on stdin and no CLAUDE_CODE_* env var, the advisory still fires. Closes #2520 * test(2520): isolate session_id signal from env fallbacks in regression test Per reviewer feedback (Copilot + CodeRabbit on #2521): the session_id isolation test used the helper's default CLAUDE_CODE_ENTRYPOINT / CLAUDE_CODE_SSE_PORT values, so the env fallback would rescue the skip even if the primary `data.session_id` check regressed. Pass an explicit env override that clears those fallbacks, so only the stdin `session_id` signal can trigger the skip. Other cases (env-only fallback, negative / non-Claude host) already override env appropriately. --------- Co-authored-by: forfrossen <forfrossensvart@gmail.com>
102 lines
3.4 KiB
JavaScript
102 lines
3.4 KiB
JavaScript
/**
|
|
* Regression test for bug #2344
|
|
*
|
|
* gsd-read-guard.js checked process.env.CLAUDE_SESSION_ID to detect the
|
|
* Claude Code runtime and skip its advisory. However, Claude Code CLI exports
|
|
* CLAUDECODE=1, not CLAUDE_SESSION_ID. The skip never fired, so the
|
|
* READ-BEFORE-EDIT advisory injected on every Edit/Write call inside Claude
|
|
* Code — producing noise in long-running sessions.
|
|
*
|
|
* Fix: check CLAUDECODE (and CLAUDE_SESSION_ID for back-compat) before
|
|
* emitting the advisory.
|
|
*/
|
|
|
|
process.env.GSD_TEST_MODE = '1';
|
|
|
|
const { test, describe, beforeEach, afterEach } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const { execFileSync } = require('node:child_process');
|
|
|
|
const { createTempDir, cleanup } = require('./helpers.cjs');
|
|
|
|
const HOOK_PATH = path.join(__dirname, '..', 'hooks', 'gsd-read-guard.js');
|
|
|
|
function runHook(payload, envOverrides = {}) {
|
|
const input = JSON.stringify(payload);
|
|
const env = {
|
|
...process.env,
|
|
CLAUDE_SESSION_ID: '',
|
|
CLAUDECODE: '',
|
|
CLAUDE_CODE_ENTRYPOINT: '',
|
|
CLAUDE_CODE_SSE_PORT: '',
|
|
CLAUDE_PROJECT_DIR: '',
|
|
...envOverrides,
|
|
};
|
|
try {
|
|
const stdout = execFileSync(process.execPath, [HOOK_PATH], {
|
|
input,
|
|
encoding: 'utf-8',
|
|
timeout: 5000,
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
env,
|
|
});
|
|
return { exitCode: 0, stdout: stdout.trim(), stderr: '' };
|
|
} catch (err) {
|
|
return {
|
|
exitCode: err.status ?? 1,
|
|
stdout: (err.stdout || '').toString().trim(),
|
|
stderr: (err.stderr || '').toString().trim(),
|
|
};
|
|
}
|
|
}
|
|
|
|
describe('bug #2344: read guard skips on CLAUDECODE env var', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => { tmpDir = createTempDir('gsd-read-guard-2344-'); });
|
|
afterEach(() => { cleanup(tmpDir); });
|
|
|
|
test('skips advisory when CLAUDECODE=1 is set (Claude Code CLI env)', () => {
|
|
const filePath = path.join(tmpDir, 'existing.js');
|
|
fs.writeFileSync(filePath, 'const x = 1;\n');
|
|
|
|
const result = runHook(
|
|
{ tool_name: 'Edit', tool_input: { file_path: filePath, old_string: 'const x = 1;', new_string: 'const x = 2;' } },
|
|
{ CLAUDECODE: '1' }
|
|
);
|
|
|
|
assert.equal(result.exitCode, 0);
|
|
assert.equal(result.stdout, '', 'advisory must not fire when CLAUDECODE=1');
|
|
});
|
|
|
|
test('skips advisory when CLAUDE_SESSION_ID is set (back-compat)', () => {
|
|
const filePath = path.join(tmpDir, 'existing.js');
|
|
fs.writeFileSync(filePath, 'const x = 1;\n');
|
|
|
|
const result = runHook(
|
|
{ tool_name: 'Edit', tool_input: { file_path: filePath, old_string: 'const x = 1;', new_string: 'const x = 2;' } },
|
|
{ CLAUDE_SESSION_ID: 'test-session-123' }
|
|
);
|
|
|
|
assert.equal(result.exitCode, 0);
|
|
assert.equal(result.stdout, '', 'advisory must not fire when CLAUDE_SESSION_ID is set');
|
|
});
|
|
|
|
test('still injects advisory when neither CLAUDECODE nor CLAUDE_SESSION_ID is set', () => {
|
|
const filePath = path.join(tmpDir, 'existing.js');
|
|
fs.writeFileSync(filePath, 'const x = 1;\n');
|
|
|
|
const result = runHook(
|
|
{ tool_name: 'Edit', tool_input: { file_path: filePath, old_string: 'const x = 1;', new_string: 'const x = 2;' } },
|
|
{ CLAUDECODE: '', CLAUDE_SESSION_ID: '' }
|
|
);
|
|
|
|
assert.equal(result.exitCode, 0);
|
|
assert.ok(result.stdout.length > 0, 'advisory should fire on non-Claude-Code runtimes');
|
|
const output = JSON.parse(result.stdout);
|
|
assert.ok(output.hookSpecificOutput?.additionalContext?.includes('Read'));
|
|
});
|
|
});
|