Skip to content

fix(extract): persist CLI token usage in cost.json (#1769)#1772

Open
TPAteeq wants to merge 1 commit into
Graphify-Labs:v8from
TPAteeq:fix/1769-cost-ledger
Open

fix(extract): persist CLI token usage in cost.json (#1769)#1772
TPAteeq wants to merge 1 commit into
Graphify-Labs:v8from
TPAteeq:fix/1769-cost-ledger

Conversation

@TPAteeq

@TPAteeq TPAteeq commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Fixes #1769.

Problem

graphify extract calculated and printed token usage after each run, but never
persisted it to graphify-out/cost.json. The only ledger writer lived in the
agent-driven skill pipeline, so direct CLI refreshes left the original entry
and cumulative totals unchanged.

The labeling usage added in #1694 was similarly available to cluster-only and
label, but was not persisted.

Fix

  • Add a shared record_cost_run() ledger writer using the existing
    cost.json schema.
  • Append a run after every successful clustered or --no-cluster extraction,
    including unchanged zero-token incremental refreshes.
  • Persist cluster-only and label token usage after the command completes
    successfully.
  • Recompute cumulative totals from run history, repairing missing or stale
    total fields.
  • Serialize concurrent writers with a cross-process lock and use atomic file
    replacement so interrupted writes cannot truncate the ledger.
  • Preserve existing file permissions and respect the process umask when
    creating a new ledger.
  • Treat ledger failures as telemetry warnings rather than failing an otherwise
    successful graph build.

Testing

Regression coverage includes:

  • appending to an existing ledger across incremental extraction runs;
  • clustered, --no-cluster, and unchanged incremental exits;
  • labeling usage appended to an existing ledger;
  • cumulative-total recovery from run history;
  • concurrent writers;
  • atomic replacement failure preserving the original file;
  • existing permission preservation and new-file umask handling.

Full suite: 3093 passed, 30 skipped.

Additional checks:

  • ruff check clean
  • pyright graphify/cost.py clean
  • python -m tools.skillgen --check clean
  • graphify update . completed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cost.json never appends after the initial run — extract prints token counts but doesn't persist them

1 participant