fix: add context monitor cost warning opt-out

This commit is contained in:
Affaan Mustafa
2026-05-17 01:43:50 -04:00
committed by Affaan Mustafa
parent 471dee27ec
commit b47dfa95a3
5 changed files with 140 additions and 23 deletions

View File

@@ -5,8 +5,12 @@
*/
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { run, evaluateConditions, detectLoop, severityLabel } = require('../../scripts/hooks/ecc-context-monitor');
const { run, evaluateConditions, detectLoop, severityLabel, costWarningsEnabled } = require('../../scripts/hooks/ecc-context-monitor');
const { getBridgePath, writeBridgeAtomic } = require('../../scripts/lib/session-bridge');
// Test helper
function test(name, fn) {
@@ -21,6 +25,18 @@ function test(name, fn) {
}
}
function withEnv(name, value, fn) {
const original = process.env[name];
try {
if (value === undefined) delete process.env[name];
else process.env[name] = value;
return fn();
} finally {
if (original === undefined) delete process.env[name];
else process.env[name] = original;
}
}
function runTests() {
console.log('\n=== Testing ecc-context-monitor.js ===\n');
@@ -113,6 +129,53 @@ function runTests() {
passed++;
else failed++;
if (
test('cost warnings can be suppressed without hiding context warnings', () => {
const warnings = evaluateConditions({ total_cost_usd: 55, context_remaining_pct: 20 }, { costWarnings: false });
assert.strictEqual(warnings.find(w => w.type === 'cost'), undefined);
const ctx = warnings.find(w => w.type === 'context');
assert.ok(ctx, 'Expected context warning to remain enabled');
assert.strictEqual(ctx.severity, 3);
})
)
passed++;
else failed++;
if (
test('ECC_CONTEXT_MONITOR_COST_WARNINGS=off disables only run-time cost warnings', () => {
const sessionId = `ctx-monitor-cost-off-${process.pid}-${Date.now()}`;
const input = JSON.stringify({ session_id: sessionId, tool_name: 'Bash' });
const warnPath = path.join(os.tmpdir(), `ecc-ctx-warn-${sessionId}.json`);
try {
writeBridgeAtomic(sessionId, {
context_remaining_pct: 20,
total_cost_usd: 55,
last_timestamp: new Date().toISOString()
});
const result = withEnv('ECC_CONTEXT_MONITOR_COST_WARNINGS', 'off', () => JSON.parse(run(input)));
const message = result.hookSpecificOutput.additionalContext;
assert.ok(message.includes('CONTEXT CRITICAL'), 'Expected context warning to remain');
assert.ok(!message.includes('COST CRITICAL'), 'Expected cost warning to be suppressed');
} finally {
fs.rmSync(getBridgePath(sessionId), { force: true });
fs.rmSync(warnPath, { force: true });
}
})
)
passed++;
else failed++;
if (
test('cost warning env defaults on and accepts false-like values', () => {
assert.strictEqual(withEnv('ECC_CONTEXT_MONITOR_COST_WARNINGS', undefined, () => costWarningsEnabled()), true);
assert.strictEqual(withEnv('ECC_CONTEXT_MONITOR_COST_WARNINGS', 'false', () => costWarningsEnabled()), false);
assert.strictEqual(withEnv('ECC_CONTEXT_MONITOR_COST_WARNINGS', '0', () => costWarningsEnabled()), false);
assert.strictEqual(withEnv('ECC_CONTEXT_MONITOR_COST_WARNINGS', 'yes', () => costWarningsEnabled()), true);
})
)
passed++;
else failed++;
// evaluateConditions — scope warnings
console.log('\nevaluateConditions (scope):');