From 08c1c4073cf608fe5d00d36149d9f50470322053 Mon Sep 17 00:00:00 2001 From: Amir Fathi Date: Thu, 13 Aug 2026 01:18:08 +0000 Subject: [PATCH] fix(lib): remove unused cost-estimate.js duplicate rate table cost-estimate.js carries its own copy of the stale Opus/Haiku/Sonnet rate table already reported in #2574, but grepping every .js/.json/.md file outside node_modules turns up zero callers besides its own test. It was added in 940135e alongside the statusline observability hooks and never wired into any of them. The maintainer's comment on #2656 named two acceptable outcomes: remove the unused duplicate, or share one rate source with the live tracker. cost-tracker.js's own fix (#2574) has not landed yet, so sharing its table now would import numbers that are still wrong. Removing the dead file is the smaller, immediately-correct step. Fixes #2656 --- scripts/lib/cost-estimate.js | 32 --------- tests/lib/cost-estimate.test.js | 114 -------------------------------- 2 files changed, 146 deletions(-) delete mode 100644 scripts/lib/cost-estimate.js delete mode 100644 tests/lib/cost-estimate.test.js diff --git a/scripts/lib/cost-estimate.js b/scripts/lib/cost-estimate.js deleted file mode 100644 index a1651a8c9..000000000 --- a/scripts/lib/cost-estimate.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict'; - -/** - * Shared cost estimation for ECC hooks. - * - * Approximate per-1M-token blended rates (conservative defaults). - */ - -const RATE_TABLE = { - haiku: { in: 0.8, out: 4.0 }, - sonnet: { in: 3.0, out: 15.0 }, - opus: { in: 15.0, out: 75.0 } -}; - -/** - * Estimate USD cost from token counts. - * @param {string} model - Model name (may contain "haiku", "sonnet", or "opus") - * @param {number} inputTokens - * @param {number} outputTokens - * @returns {number} Estimated cost in USD (rounded to 6 decimal places) - */ -function estimateCost(model, inputTokens, outputTokens) { - const normalized = String(model || '').toLowerCase(); - let rates = RATE_TABLE.sonnet; - if (normalized.includes('haiku')) rates = RATE_TABLE.haiku; - if (normalized.includes('opus')) rates = RATE_TABLE.opus; - - const cost = (inputTokens / 1_000_000) * rates.in + (outputTokens / 1_000_000) * rates.out; - return Math.round(cost * 1e6) / 1e6; -} - -module.exports = { estimateCost, RATE_TABLE }; diff --git a/tests/lib/cost-estimate.test.js b/tests/lib/cost-estimate.test.js deleted file mode 100644 index bcb5906bc..000000000 --- a/tests/lib/cost-estimate.test.js +++ /dev/null @@ -1,114 +0,0 @@ -/** - * 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);