mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-05-17 06:13:08 +08:00
feat: add ECC statusline observability hooks
Salvages the useful statusline/context monitor work from stale PR #1504 while preserving the current continuous-learning hook runner wiring. Adds the metrics bridge, context monitor, statusline script, shared cost/session bridge utilities, and tests. Fixes the reviewed false loop-detection hash collision for non-file tools, avoids default-session cost inflation, sanitizes statusline task lookup, and records hook payload session IDs in cost-tracker.
This commit is contained in:
committed by
Affaan Mustafa
parent
e9c8845833
commit
940135ea47
114
tests/lib/cost-estimate.test.js
Normal file
114
tests/lib/cost-estimate.test.js
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Tests for scripts/lib/cost-estimate.js
|
||||
*
|
||||
* Run with: node tests/lib/cost-estimate.test.js
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const { estimateCost, RATE_TABLE } = require('../../scripts/lib/cost-estimate');
|
||||
|
||||
// Test helper
|
||||
function test(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(` \u2713 ${name}`);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.log(` \u2717 ${name}`);
|
||||
console.log(` Error: ${err.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function runTests() {
|
||||
console.log('\n=== Testing cost-estimate.js ===\n');
|
||||
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
|
||||
// RATE_TABLE structure
|
||||
console.log('RATE_TABLE:');
|
||||
|
||||
if (
|
||||
test('RATE_TABLE has haiku, sonnet, opus keys', () => {
|
||||
assert.ok(RATE_TABLE.haiku, 'Missing haiku');
|
||||
assert.ok(RATE_TABLE.sonnet, 'Missing sonnet');
|
||||
assert.ok(RATE_TABLE.opus, 'Missing opus');
|
||||
assert.strictEqual(typeof RATE_TABLE.haiku.in, 'number');
|
||||
assert.strictEqual(typeof RATE_TABLE.haiku.out, 'number');
|
||||
assert.strictEqual(typeof RATE_TABLE.sonnet.in, 'number');
|
||||
assert.strictEqual(typeof RATE_TABLE.sonnet.out, 'number');
|
||||
assert.strictEqual(typeof RATE_TABLE.opus.in, 'number');
|
||||
assert.strictEqual(typeof RATE_TABLE.opus.out, 'number');
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
// estimateCost tests
|
||||
console.log('\nestimateCost:');
|
||||
|
||||
if (
|
||||
test('opus 1M/1M tokens returns 90', () => {
|
||||
const cost = estimateCost('opus', 1_000_000, 1_000_000);
|
||||
assert.strictEqual(cost, 90);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('sonnet 1M/1M tokens returns 18', () => {
|
||||
const cost = estimateCost('sonnet', 1_000_000, 1_000_000);
|
||||
assert.strictEqual(cost, 18);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('haiku 1M/1M tokens returns 4.8', () => {
|
||||
const cost = estimateCost('haiku', 1_000_000, 1_000_000);
|
||||
assert.strictEqual(cost, 4.8);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('null model with 0 tokens returns 0', () => {
|
||||
const cost = estimateCost(null, 0, 0);
|
||||
assert.strictEqual(cost, 0);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('full model name claude-opus-4-6 uses opus rates', () => {
|
||||
const cost = estimateCost('claude-opus-4-6', 500, 200);
|
||||
// (500 / 1_000_000) * 15 + (200 / 1_000_000) * 75 = 0.0075 + 0.015 = 0.0225
|
||||
const expected = Math.round(0.0225 * 1e6) / 1e6;
|
||||
assert.strictEqual(cost, expected);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('unknown model falls back to sonnet rates', () => {
|
||||
const cost = estimateCost('unknown-model', 1_000_000, 1_000_000);
|
||||
assert.strictEqual(cost, 18);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
// Summary
|
||||
console.log(`\nResults: ${passed} passed, ${failed} failed\n`);
|
||||
return { passed, failed };
|
||||
}
|
||||
|
||||
const { failed } = runTests();
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
174
tests/lib/session-bridge.test.js
Normal file
174
tests/lib/session-bridge.test.js
Normal file
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* Tests for scripts/lib/session-bridge.js
|
||||
*
|
||||
* Run with: node tests/lib/session-bridge.test.js
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
|
||||
const { sanitizeSessionId, getBridgePath, readBridge, writeBridgeAtomic, resolveSessionId, MAX_SESSION_ID_LENGTH } = require('../../scripts/lib/session-bridge');
|
||||
|
||||
// Test helper
|
||||
function test(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(` \u2713 ${name}`);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.log(` \u2717 ${name}`);
|
||||
console.log(` Error: ${err.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function runTests() {
|
||||
console.log('\n=== Testing session-bridge.js ===\n');
|
||||
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
|
||||
// sanitizeSessionId tests
|
||||
console.log('sanitizeSessionId:');
|
||||
|
||||
if (
|
||||
test('valid ID passes through', () => {
|
||||
assert.strictEqual(sanitizeSessionId('abc-123'), 'abc-123');
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('path traversal returns null', () => {
|
||||
assert.strictEqual(sanitizeSessionId('../etc/passwd'), null);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('forward slash returns null', () => {
|
||||
assert.strictEqual(sanitizeSessionId('/tmp/evil'), null);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('backslash returns null', () => {
|
||||
assert.strictEqual(sanitizeSessionId('a\\b'), null);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('null input returns null', () => {
|
||||
assert.strictEqual(sanitizeSessionId(null), null);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('empty string returns null', () => {
|
||||
assert.strictEqual(sanitizeSessionId(''), null);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('long string is truncated to MAX_SESSION_ID_LENGTH', () => {
|
||||
const longId = 'a'.repeat(100);
|
||||
const result = sanitizeSessionId(longId);
|
||||
assert.ok(result, 'Should not return null for valid chars');
|
||||
assert.strictEqual(result.length, MAX_SESSION_ID_LENGTH);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
// getBridgePath tests
|
||||
console.log('\ngetBridgePath:');
|
||||
|
||||
if (
|
||||
test('returns path containing ecc-metrics-', () => {
|
||||
const p = getBridgePath('test-session');
|
||||
assert.ok(p.includes('ecc-metrics-'), `Expected ecc-metrics- in path, got: ${p}`);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
// writeBridgeAtomic + readBridge roundtrip
|
||||
console.log('\nwriteBridgeAtomic / readBridge:');
|
||||
|
||||
if (
|
||||
test('roundtrip write then read returns same data', () => {
|
||||
const testId = `test-bridge-${Date.now()}`;
|
||||
const data = { session_id: testId, tool_count: 42 };
|
||||
try {
|
||||
writeBridgeAtomic(testId, data);
|
||||
const result = readBridge(testId);
|
||||
assert.deepStrictEqual(result, data);
|
||||
} finally {
|
||||
// Clean up
|
||||
try {
|
||||
fs.unlinkSync(getBridgePath(testId));
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('readBridge with nonexistent session returns null', () => {
|
||||
const result = readBridge('nonexistent-session-id-999');
|
||||
assert.strictEqual(result, null);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
// resolveSessionId tests
|
||||
console.log('\nresolveSessionId:');
|
||||
|
||||
if (
|
||||
test('resolveSessionId uses ECC_SESSION_ID env var', () => {
|
||||
const original = process.env.ECC_SESSION_ID;
|
||||
try {
|
||||
process.env.ECC_SESSION_ID = 'env-session-42';
|
||||
const result = resolveSessionId();
|
||||
assert.strictEqual(result, 'env-session-42');
|
||||
} finally {
|
||||
if (original === undefined) {
|
||||
delete process.env.ECC_SESSION_ID;
|
||||
} else {
|
||||
process.env.ECC_SESSION_ID = original;
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('MAX_SESSION_ID_LENGTH is 64', () => {
|
||||
assert.strictEqual(MAX_SESSION_ID_LENGTH, 64);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
// Summary
|
||||
console.log(`\nResults: ${passed} passed, ${failed} failed\n`);
|
||||
return { passed, failed };
|
||||
}
|
||||
|
||||
const { failed } = runTests();
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
Reference in New Issue
Block a user