mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-06-12 11:13:11 +08:00
fix(hooks): prefer fresh harness cost cache (#2054)
Uses a fresh harness cost cache when available and keeps transcript pricing as the fallback. Focused cost-tracker tests passed locally before merge.
This commit is contained in:
@@ -215,6 +215,93 @@ function runTests() {
|
||||
fs.rmSync(tmpHome, { recursive: true, force: true });
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
// 8. Prefers harness-cost cache value over transcript-sum when fresh
|
||||
(test('prefers fresh harness-cost cache over transcript estimate', () => {
|
||||
const tmpHome = makeTempDir();
|
||||
const sessionId = 'harness-fresh-' + Date.now();
|
||||
const transcriptPath = path.join(tmpHome, 'session.jsonl');
|
||||
writeTranscript(transcriptPath, [
|
||||
{
|
||||
type: 'assistant',
|
||||
message: {
|
||||
model: 'claude-opus-4-20250514',
|
||||
usage: {
|
||||
input_tokens: 10000,
|
||||
output_tokens: 5000,
|
||||
cache_creation_input_tokens: 200000,
|
||||
cache_read_input_tokens: 1000000,
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
const harnessCachePath = path.join(os.tmpdir(), `harness-cost-${sessionId}.json`);
|
||||
const nowEpoch = Math.floor(Date.now() / 1000);
|
||||
fs.writeFileSync(
|
||||
harnessCachePath,
|
||||
JSON.stringify({ ts: nowEpoch, cost_usd: 1.23 }),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
try {
|
||||
const result = runScript(
|
||||
{ session_id: sessionId, transcript_path: transcriptPath },
|
||||
withTempHome(tmpHome)
|
||||
);
|
||||
assert.strictEqual(result.code, 0, `Expected exit code 0, got ${result.code}`);
|
||||
|
||||
const metricsFile = path.join(tmpHome, '.claude', 'metrics', 'costs.jsonl');
|
||||
const row = JSON.parse(fs.readFileSync(metricsFile, 'utf8').trim());
|
||||
assert.strictEqual(row.estimated_cost_usd, 1.23, 'Expected harness cost to win');
|
||||
// Token totals still reflect the transcript scan
|
||||
assert.strictEqual(row.input_tokens, 10000, 'Token totals should still come from transcript');
|
||||
assert.strictEqual(row.output_tokens, 5000, 'Token totals should still come from transcript');
|
||||
} finally {
|
||||
try { fs.unlinkSync(harnessCachePath); } catch { /* best-effort */ }
|
||||
fs.rmSync(tmpHome, { recursive: true, force: true });
|
||||
}
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
// 9. Ignores stale harness-cost cache and falls back to transcript estimate
|
||||
(test('ignores stale harness-cost cache (>300s) and uses transcript estimate', () => {
|
||||
const tmpHome = makeTempDir();
|
||||
const sessionId = 'harness-stale-' + Date.now();
|
||||
const transcriptPath = path.join(tmpHome, 'session.jsonl');
|
||||
writeTranscript(transcriptPath, [
|
||||
{
|
||||
type: 'assistant',
|
||||
message: {
|
||||
model: 'claude-sonnet-4-20250514',
|
||||
usage: { input_tokens: 1000, output_tokens: 500 },
|
||||
},
|
||||
},
|
||||
]);
|
||||
const harnessCachePath = path.join(os.tmpdir(), `harness-cost-${sessionId}.json`);
|
||||
const staleEpoch = Math.floor(Date.now() / 1000) - 3600;
|
||||
fs.writeFileSync(
|
||||
harnessCachePath,
|
||||
JSON.stringify({ ts: staleEpoch, cost_usd: 999.99 }),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
try {
|
||||
const result = runScript(
|
||||
{ session_id: sessionId, transcript_path: transcriptPath },
|
||||
withTempHome(tmpHome)
|
||||
);
|
||||
assert.strictEqual(result.code, 0, `Expected exit code 0, got ${result.code}`);
|
||||
|
||||
const metricsFile = path.join(tmpHome, '.claude', 'metrics', 'costs.jsonl');
|
||||
const row = JSON.parse(fs.readFileSync(metricsFile, 'utf8').trim());
|
||||
assert.notStrictEqual(row.estimated_cost_usd, 999.99, 'Stale cache must not win');
|
||||
assert.ok(row.estimated_cost_usd > 0, 'Expected fallback transcript estimate to be positive');
|
||||
// Sonnet rates: 1000/1e6*3 + 500/1e6*15 ≈ $0.011 — well below the 999.99 stale value
|
||||
assert.ok(row.estimated_cost_usd < 1, 'Expected small transcript estimate, not the stale 999.99');
|
||||
} finally {
|
||||
try { fs.unlinkSync(harnessCachePath); } catch { /* best-effort */ }
|
||||
fs.rmSync(tmpHome, { recursive: true, force: true });
|
||||
}
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user