ECC/scripts/hooks
Jamkris 4f21ed2acf fix(hooks): use last cumulative row for session cost in metrics bridge
`ecc-metrics-bridge.js#readSessionCost` summed the
`estimated_cost_usd`, `input_tokens`, and `output_tokens` of
every matching row in `~/.claude/metrics/costs.jsonl`. That breaks
the documented contract of `scripts/hooks/cost-tracker.js`, which
explicitly states (in its module docblock):

  Cumulative behavior: Stop fires per assistant response, not
  per session. Each row therefore represents the cumulative
  session total up to that point. To get per-session cost, take
  the last row per session_id.

Summing N cumulative rows over-counts by roughly (N+1)/2 ×. For a
session with 3 rows at 0.01, 0.02, 0.03 USD (true running total
0.03), the bridge today reports 0.06 USD. The over-counted value
feeds `ecc-context-monitor.js`, which then trips its
COST_NOTICE_USD / COST_WARNING_USD / COST_CRITICAL_USD thresholds
on phantom spend AND injects the inflated number as
`additionalContext` into the live model turn — so the agent
itself is told a wrong cost.

Reproduced on `main` before this commit:

  $ cat > /tmp/eccc/.claude/metrics/costs.jsonl <<EOF
  {"session_id":"S1","estimated_cost_usd":0.01,"input_tokens":333,"output_tokens":166}
  {"session_id":"S1","estimated_cost_usd":0.02,"input_tokens":666,"output_tokens":333}
  {"session_id":"S1","estimated_cost_usd":0.03,"input_tokens":1000,"output_tokens":500}
  EOF

  $ HOME=/tmp/eccc node -e 'const m = require("./scripts/hooks/ecc-metrics-bridge.js"); \
      console.log(JSON.stringify(m.readSessionCost("S1")))'
  {"totalCost":0.06,"totalIn":1999,"totalOut":999}

Expected: `{"totalCost":0.03,"totalIn":1000,"totalOut":500}` (the
last cumulative row).
Actual: 2× over-count.

Fix: replace `+=` with `=` in the matching branch so the assigned
values reflect the most recent row encountered. The iteration
order is file order, which is also event time order, so the last
assignment wins — exactly the contract cost-tracker writes
against.

After this commit the reproduction above returns
`{"totalCost":0.03,"totalIn":1000,"totalOut":500}`.

Regression test in `tests/hooks/ecc-metrics-bridge.test.js`:
`readSessionCost returns the LAST cumulative row, not the sum
(cost-tracker contract)`. The existing
`readSessionCost does not include unrelated default-session rows`
test happened to pass even with the bug because it only had one
target-session row — single-row sessions are coincidentally
correct under both formulas. The new test uses three rows so the
two formulas diverge.

A second issue in the same function — the 8 KiB tail-only read
silently drops older rows once a session's recent cumulative
totals scroll past that window — is fixed in the next commit.
2026-05-17 21:41:24 -04:00
..
auto-tmux-dev.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
bash-hook-dispatcher.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
block-no-verify.js fix: close block-no-verify bypass holes 2026-05-12 22:28:12 -04:00
check-console-log.js Fix stdin overflow bug in hook scripts - truncate chunks to stay within MAX_STDIN limit 2026-02-18 07:40:12 +00:00
check-hook-enabled.js feat: deliver v1.8.0 harness reliability and parity updates 2026-03-04 14:48:06 -08:00
config-protection.js fix: integrate recent hook and docs PRs (#1905) 2026-05-14 21:37:28 -04:00
cost-tracker.js fix: integrate recent hook and docs PRs (#1905) 2026-05-14 21:37:28 -04:00
design-quality-check.js feat: add web frontend rules and design quality hook 2026-04-02 17:33:17 -07:00
desktop-notify.js fix: salvage remaining stale queue fixes (#1754) 2026-05-11 16:41:08 -04:00
doc-file-warning.js fix(lint): prefix unused options parameter with underscore 2026-03-29 10:14:53 +08:00
ecc-context-monitor.js fix: add context monitor cost warning opt-out 2026-05-17 01:53:57 -04:00
ecc-metrics-bridge.js fix(hooks): use last cumulative row for session cost in metrics bridge 2026-05-17 21:41:24 -04:00
ecc-statusline.js chore: update statusline ANSI color palette 2026-05-15 23:18:01 -04:00
evaluate-session.js Fix stdin overflow bug in hook scripts - truncate chunks to stay within MAX_STDIN limit 2026-02-18 07:40:12 +00:00
gateguard-fact-force.js fix(hooks): close grouped command bypasses in gateguard (#1912) 2026-05-15 01:39:15 -04:00
governance-capture.js fix: fold session manager blockers into one candidate 2026-03-24 23:08:27 -04:00
insaits-security-monitor.py test: cover hook bootstrap and InsAIts monitor 2026-04-29 17:45:22 -04:00
insaits-security-wrapper.js fix: fail open on insaits monitor errors 2026-04-29 18:03:33 -04:00
mcp-health-check.js fix: port Windows hook safety fixes (#1719) 2026-05-11 03:56:51 -04:00
observe-runner.js fix: route continuous learning observe hooks through node 2026-04-29 21:28:59 -04:00
plugin-hook-bootstrap.js fix: bootstrap plugin-installed hook commands safely 2026-04-14 20:24:21 -07:00
post-bash-build-complete.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
post-bash-command-log.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
post-bash-dispatcher.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
post-bash-pr-created.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
post-edit-accumulator.js perf(hooks): batch format+typecheck at Stop instead of per Edit (#746) 2026-03-31 14:12:12 -07:00
post-edit-console-warn.js Merge branch 'affaan-m:main' into main 2026-02-18 18:33:04 +05:30
post-edit-format.js fix: port Windows hook safety fixes (#1719) 2026-05-11 03:56:51 -04:00
post-edit-typecheck.js Fix stdin overflow bug in hook scripts - truncate chunks to stay within MAX_STDIN limit 2026-02-18 07:40:12 +00:00
pre-bash-commit-quality.js fix: run pre-bash linters through windows wrappers 2026-04-29 18:59:10 -04:00
pre-bash-dev-server-block.js fix: integrate recent hook and docs PRs (#1905) 2026-05-14 21:37:28 -04:00
pre-bash-dispatcher.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
pre-bash-git-push-reminder.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
pre-bash-tmux-reminder.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
pre-compact.js fix: nullish coalescing in evaluate-session config, narrow pre-compact glob, add 11 tests 2026-02-13 02:42:01 -08:00
pre-write-doc-warn.js feat: deliver v1.8.0 harness reliability and parity updates 2026-03-04 14:48:06 -08:00
quality-gate.js refactor: deduplicate config lists and unify resolveFormatterBin branches 2026-03-11 10:45:28 +09:00
run-with-flags-shell.sh fix(hooks): pass phase argument from hook ID to observe.sh (#1042) 2026-03-31 14:05:16 -07:00
run-with-flags.js fix: route continuous learning observe hooks through node 2026-04-29 21:28:59 -04:00
session-activity-tracker.js Merge pull request #1384 from KeWang0622/fix/lint-md028-eqeqeq 2026-04-12 23:03:19 -07:00
session-end-marker.js fix: clean up observer sessions on lifecycle end 2026-04-02 18:02:29 -07:00
session-end.js review: broaden CLAUDE_TRANSCRIPT_PATH fallback to cover missing/empty JSON fields 2026-04-19 14:35:21 +09:00
session-start-bootstrap.js fix: salvage remaining stale queue fixes (#1754) 2026-05-11 16:41:08 -04:00
session-start.js fix: scope SessionStart context injection 2026-05-11 22:56:29 -04:00
stop-format-typecheck.js perf(hooks): batch format+typecheck at Stop instead of per Edit (#746) 2026-03-31 14:12:12 -07:00
suggest-compact.js fix: integrate recent hook and docs PRs (#1905) 2026-05-14 21:37:28 -04:00