test(hooks): make context-gate tests portable to Windows (#3106)

The hook proposes a correct absolute checkpoint path on Windows, but the
tests located that line with /^\s+[/\\].*\.md$/, which only matches paths
starting with a slash. Windows paths start with a drive letter (C:\...),
so every case that read the proposed path saw undefined and failed.

- Extract the proposed checkpoint as the indented line that ends in .md
  and satisfies path.isAbsolute, so POSIX and Windows paths both match.
- Include the hook output in the "both fires should propose" assertion
  so a genuine decline is diagnosable from CI logs.
- Write transcript fixtures into private fs.mkdtempSync directories
  instead of predictable names directly under os.tmpdir() (CodeQL
  js/insecure-temporary-file) in context-gate, context-gate-state and
  ecc-context-monitor tests.
This commit is contained in:
haelyra committed 2026-11-16 22:07:01 -05:00
1 parent ae9ce433cd
commit e413166c50
4 files changed
+38 -14

No files matched your search

+9 -3
View File
@@ -54,6 +54,12 @@ function createSandbox() {
return { root, resumeDir, transcript, env };
}
// The proposed checkpoint is the indented line that is an absolute .md path:
// "/..." on POSIX, "C:\\..." (or a UNC path) on Windows.
function isProposedPathLine(line) {
return /^\s+\S/.test(line) && line.endsWith('.md') && path.isAbsolute(line.trim());
}
function fire(sandbox, extra = {}) {
const input = { cwd: '/tmp/owner-a/project', session_id: 'mysess', transcript_path: sandbox.transcript, ...extra };
const result = spawnSync(process.execPath, [RUNNER, 'user-prompt:context-gate', 'scripts/hooks/context-gate.js', 'standard,strict'], {
@@ -66,7 +72,7 @@ function fire(sandbox, extra = {}) {
if (result.error) return { timeout: result.error.code };
const text = JSON.parse(result.stdout).hookSpecificOutput.additionalContext;
const lines = text.split('\n');
const target = lines.find(line => /^\s+[/\\].*\.md$/.test(line));
const target = lines.find(isProposedPathLine);
const marker = lines.find(line => line.includes('<!-- ecc-handoff'));
return { text, target: target && target.trim(), marker: marker && marker.trim() };
}
@@ -118,7 +124,7 @@ function runTests() {
clearResumeDir(sandbox);
const a = fire(sandbox);
const b = fire(sandbox, { cwd: '/tmp/owner-b/project' });
assert.ok(a.target && b.target, 'both fires should propose a destination');
assert.ok(a.target && b.target, `both fires should propose a destination. Got: ${a.target ? b.text : a.text}`);
assert.notStrictEqual(a.target, b.target);
}));
@@ -126,7 +132,7 @@ function runTests() {
clearResumeDir(sandbox);
const a = fire(sandbox, { session_id: 'owner/a' });
const b = fire(sandbox, { session_id: 'owner?a' });
assert.ok(a.target && b.target, 'both fires should propose a destination');
assert.ok(a.target && b.target, `both fires should propose a destination. Got: ${a.target ? b.text : a.text}`);
assert.notStrictEqual(a.target, b.target);
}));
+16 -4
View File
@@ -25,12 +25,24 @@ function test(name, fn) {
}
}
// Private per-run directory for fixtures, so no test writes a predictable name
// directly under the shared OS temp directory.
const FIXTURE_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-context-gate-test-'));
process.on('exit', () => fs.rmSync(FIXTURE_DIR, { recursive: true, force: true }));
let fixtureSeq = 0;
// The proposed checkpoint is the indented line that is an absolute .md path:
// "/..." on POSIX, "C:\\..." (or a UNC path) on Windows.
function isProposedPathLine(line) {
return /^\s+\S/.test(line) && line.endsWith('.md') && path.isAbsolute(line.trim());
}
/**
* Write a minimal transcript JSONL whose latest assistant record reports the
* given context token total against a claude-fable-5 (1M window) model.
*/
function writeTranscript(tokens, model = 'claude-fable-5') {
const file = path.join(os.tmpdir(), `ecc-context-gate-test-${process.pid}-${Math.random().toString(16).slice(2)}.jsonl`);
const file = path.join(FIXTURE_DIR, `transcript-${++fixtureSeq}.jsonl`);
const records = [
{ type: 'user', message: { role: 'user', content: 'hello' } },
{
@@ -219,7 +231,7 @@ function runTests() {
if (
test('missing transcript path fails open to empty output', () => {
assert.strictEqual(run(JSON.stringify({ transcript_path: path.join(os.tmpdir(), 'ecc-context-gate-nope.jsonl') }), { env }), '');
assert.strictEqual(run(JSON.stringify({ transcript_path: path.join(FIXTURE_DIR, 'missing.jsonl') }), { env }), '');
assert.strictEqual(run(JSON.stringify({}), { env }), '');
assert.strictEqual(run('', { env }), '');
})
@@ -476,7 +488,7 @@ function runTests() {
try {
process.env.ECC_AGENT_DATA_HOME = sandbox;
const first = JSON.parse(run(JSON.stringify({ transcript_path: t, cwd: '/x/demoproj', session_id: 'mysess' }), { env: { ECC_CONTEXT_GATE_MODE: 'enforce' } })).hookSpecificOutput.additionalContext;
taken = first.split('\n').find(line => /^\s+[/\\].*\.md$/.test(line)).trim();
taken = first.split('\n').find(isProposedPathLine).trim();
fs.writeFileSync(taken, foreign, 'utf8');
const out = JSON.parse(run(JSON.stringify({ transcript_path: t, cwd: '/x/demoproj', session_id: 'mysess', prompt: 'go' }), { env: { ECC_CONTEXT_GATE_MODE: 'enforce' } }));
const ctx = out.hookSpecificOutput.additionalContext;
@@ -507,7 +519,7 @@ function runTests() {
try {
process.env.ECC_AGENT_DATA_HOME = sandbox;
const first = JSON.parse(run(JSON.stringify({ transcript_path: t, cwd: '/x/demoproj', session_id: 'mysess' }), { env: { ECC_CONTEXT_GATE_MODE: 'enforce' } })).hookSpecificOutput.additionalContext;
own = first.split('\n').find(line => /^\s+[/\\].*\.md$/.test(line)).trim();
own = first.split('\n').find(isProposedPathLine).trim();
const marker = first.split('\n').find(line => line.includes('<!-- ecc-handoff')).trim().replace('<GIT_HEAD>', 'a'.repeat(40));
fs.writeFileSync(own, marker + '\nMINE\n', 'utf8');
const out = JSON.parse(run(JSON.stringify({ transcript_path: t, cwd: '/x/demoproj', session_id: 'mysess', prompt: 'go' }), { env: { ECC_CONTEXT_GATE_MODE: 'enforce' } }));
+4 -4
View File
@@ -148,7 +148,7 @@ function runTests() {
test('run(): readable transcript in the gate band suppresses the context warning', () => {
const sessionId = `ctx-monitor-gate-active-${process.pid}-${Date.now()}`;
const warnPath = path.join(os.tmpdir(), `ecc-ctx-warn-${sessionId}.json`);
const transcript = path.join(os.tmpdir(), `ecc-ctx-monitor-transcript-${sessionId}.jsonl`);
const transcript = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-ctx-monitor-')), 'transcript.jsonl');
// 190k of an assumed 200k window = 95% >= the 90% gate threshold.
fs.writeFileSync(
transcript,
@@ -173,7 +173,7 @@ function runTests() {
else process.env.ECC_CONTEXT_GATE_MODE = originalMode;
fs.rmSync(getBridgePath(sessionId), { force: true });
fs.rmSync(warnPath, { force: true });
fs.rmSync(transcript, { force: true });
fs.rmSync(path.dirname(transcript), { recursive: true, force: true });
}
})
)
@@ -185,7 +185,7 @@ function runTests() {
// Same in-band transcript, but with no ECC_CONTEXT_GATE_MODE the gate is
// inert — the monitor must keep emitting CONTEXT CRITICAL.
const sessionId = `ctx-monitor-gate-advisory-${process.pid}-${Date.now()}`;
const transcript = path.join(os.tmpdir(), `ecc-ctx-monitor-transcript-${sessionId}.jsonl`);
const transcript = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-ctx-monitor-')), 'transcript.jsonl');
fs.writeFileSync(
transcript,
JSON.stringify({
@@ -210,7 +210,7 @@ function runTests() {
if (originalMode === undefined) delete process.env.ECC_CONTEXT_GATE_MODE;
else process.env.ECC_CONTEXT_GATE_MODE = originalMode;
fs.rmSync(getBridgePath(sessionId), { force: true });
fs.rmSync(transcript, { force: true });
fs.rmSync(path.dirname(transcript), { recursive: true, force: true });
}
})
)
+9 -3
View File
@@ -47,8 +47,14 @@ function test(desc, fn) {
}
}
// Private per-run directory for fixtures, so no test writes a predictable name
// directly under the shared OS temp directory.
const FIXTURE_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-gate-state-test-'));
process.on('exit', () => fs.rmSync(FIXTURE_DIR, { recursive: true, force: true }));
let fixtureSeq = 0;
function writeTranscript(tokens, model = 'claude-unknown-x') {
const file = path.join(os.tmpdir(), `ecc-gate-state-test-${process.pid}-${Math.random().toString(16).slice(2)}.jsonl`);
const file = path.join(FIXTURE_DIR, `transcript-${++fixtureSeq}.jsonl`);
const record = JSON.stringify({
type: 'assistant',
message: {
@@ -212,9 +218,9 @@ test('false below the band and with the gate disabled', () => {
test('false when the gate cannot evaluate usage (missing/unreadable transcript)', () => {
// The advisory hooks use false to KEEP their own warnings — a gate that
// cannot fire must never silence its fallbacks.
assert.strictEqual(gateOwnsTranscript(path.join(os.tmpdir(), `ecc-gate-state-nope-${process.pid}.jsonl`), ENFORCE), false);
assert.strictEqual(gateOwnsTranscript(path.join(FIXTURE_DIR, 'missing.jsonl'), ENFORCE), false);
assert.strictEqual(gateOwnsTranscript('', ENFORCE), false);
const broken = path.join(os.tmpdir(), `ecc-gate-state-broken-${process.pid}.jsonl`);
const broken = path.join(FIXTURE_DIR, 'broken.jsonl');
fs.writeFileSync(broken, 'not json\n{broken', 'utf8');
try {
assert.strictEqual(gateOwnsTranscript(broken, ENFORCE), false);