Skip to content

perf(status): tj status ran 12 analyzers inline for its footer line - #694

Merged
anshss merged 1 commit into
mainfrom
status-perf
Aug 4, 2026
Merged

anshss merged 1 commit into
mainfrom
status-perf

Conversation

@anshss

@anshss anshss commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

tj status spent about a minute running analyzers

On a real corpus (304 agents), tj status took 55-59 seconds at ~95% CPU. Compute-bound, not IO-wait.

The status table itself is cheap. The cost was the one-line recoverable-savings footer at the bottom of the screen: _recoverable_teaser in tokenjam/cli/cmd_status.py called build_report(findings=list(COST_ANALYZERS)) live, uncached, on every plain tj status. That dispatches the whole cost analyzer set over the entire corpus, including the deliberately unbounded relearn scan, so the user waits a minute for a table that was ready in a second.

This is a failure mode the codebase already documented

tokenjam/core/optimize/report_store.py was written to end exactly this, after the same inline scan on an HTTP request thread hung the dashboard's recoverable-waste panel and the Budget-at-risk card for minutes. Its module docstring states the rule outright:

no request path runs an analyzer. Analyzer runs happen in exactly three places, all of them off the request path and all of them landing here.

A status command a user types to read a table is the same kind of path. The CLI caller had simply never adopted the store.

The fix

_recoverable_teaser now reads report_store.stored_report(config). Nothing else about the figure changed: it is still the largest single analyzer estimate rather than a sum (the analyzers price overlapping angles on the same spans, so summing would print an inflated non-additive headline), still gated at $1, still shown regardless of billing mode. The COST_ANALYZERS scoping the teaser has always claimed is now applied on the read, since the store holds every analyzer's finding rather than just the cost rail's.

Analyzer runs stay where they belong: daemon boot, the daemon's schedule, a user rescan, or an explicit tj optimize.

A cold store renders no figure at all. Not zero, not $0.00, not "nothing to recover". A scan that never ran cannot support an absence claim, and $0.00 recoverable reads as reassurance. The cold line names the command that would produce a figure and states no quantity:

Recoverable waste has not been scanned yet: run tj optimize.

The teaser no longer needs a db argument, which also fixes a smaller bug: it used to go silent whenever the daemon held the write lock (API-shim mode, no .conn), because it had no connection to scan with. That is precisely the mode where the store is warm, so the figure now renders there.

Measured

Same machine, same corpus, same command:

wall cpu
before 58.4s 44.2s user + 11.7s system, 95% CPU
after 1.6s 2.9s user + 0.4s system

Both footer states, rendered from the real CLI against the same corpus:

# warm store
$295 / 62.1M tokens recoverable (largest single fix): run tj optimize

# cold store
Recoverable waste has not been scanned yet: run tj optimize.

Tests

  • tests/unit/test_cmd_status_teaser.py rewritten against the store rather than a monkeypatched build_report.
  • The test that pinned the teaser's silence without a direct DB connection is inverted, not deleted: it now pins that the stored figure renders in that mode.
  • New: cold store renders a command and no figure; the COST_ANALYZERS scoping cannot be bypassed by a finding stored under another name.
  • New regression pin: the status module cannot reach build_report, asserted both statically against the source and dynamically with build_report trapped to raise.

pytest tests/unit -k "status or optimize or report_store" -> 402 passed.
tests/unit/test_advertised_commands_are_invocable.py -> 12 passed (the footer still advertises tj optimize, which resolves).

Sweep

Every other build_report / COST_ANALYZERS / direct-analyzer call site in tokenjam/cli/ is a command whose entire purpose is the analysis the user asked for, so none is the same defect:

  • cmd_optimize.py - the analyzer command itself
  • cmd_report.py - tj report --trim / --reuse, single scoped analyzer per invocation
  • cmd_route.py - tj route, findings=["downsize"]
  • cmd_quickstart.py - the zero-install demo whose output is the figure
  • data_access.py / cmd_quota_audit.py - audit_opus_quota, reached only from tj quota-audit

tj status was the only plain command path running an analyzer as a side effect.

🤖 Generated with Claude Code

`tj status` took about a minute of CPU on a real corpus (measured 55-59s,
~95% CPU, 300+ agents). The cause was the one-line recoverable-savings
footer: `_recoverable_teaser` called `build_report` live on every plain
run, dispatching the whole cost analyzer set over the entire corpus,
including the deliberately unbounded relearn scan.

`core/optimize/report_store.py` exists precisely to end this failure mode
on the HTTP side, where the same inline scan hung the dashboard's
recoverable-waste panel for minutes: "no request path runs an analyzer".
A status command a user types to read a table is the same kind of path;
the CLI caller had simply never adopted the store.

The teaser now reads `report_store.stored_report`, scoping to
COST_ANALYZERS on the read since the store holds every analyzer's finding.
Analyzer runs stay where they belong: daemon boot, the daemon's schedule,
a user rescan, or an explicit `tj optimize`.

A cold store renders no figure at all. Not zero, not $0.00, not "nothing
to recover" - a scan that never ran cannot support an absence claim - just
a line naming the command that would produce one.

Same measurement after the change: 1.6s wall.

The teaser no longer takes a `db` argument, so the test that pinned its
silence in API-shim mode is inverted rather than dropped: with no local DB
handle the stored figure now renders, which is exactly the mode where the
store is warm. Added a regression pin that the status module cannot reach
`build_report` at all, plus coverage for the cold store and the
COST_ANALYZERS scoping.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@anshss
anshss requested a review from anilmurty as a code owner August 4, 2026 17:42
@greptile-apps

greptile-apps Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR removes an expensive inline analyzer pass from tj status and reads the persisted optimize report instead.

  • Shows the largest stored estimate scoped to cost analyzers.
  • Distinguishes a never-written store from a genuine zero result.
  • Preserves fail-soft status behavior and adds focused regression coverage.

Confidence Score: 5/5

The PR appears safe to merge with no concrete changed-code defect identified.

The status footer now reads the same persisted analyzer report produced by established scan paths, handles cold and failed reads without breaking the command, and retains the intended aggregation and scoping behavior.

Important Files Changed

Filename Overview
tokenjam/cli/cmd_status.py Replaces inline report generation with a fail-soft stored-report read while preserving cost scoping and largest-estimate rendering.
tests/unit/test_cmd_status_teaser.py Updates teaser tests for stored reports and adds cold-store, API-shim, analyzer-scoping, and no-inline-analysis regression coverage.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    Scan["Daemon scan / tj optimize"] --> Store["Persisted optimize report"]
    Status["tj status"] --> Read["Read stored report"]
    Store --> Read
    Read -->|Warm| Filter["Filter cost analyzers"]
    Filter --> Teaser["Render largest estimate"]
    Read -->|Cold| Cold["Suggest tj optimize without a figure"]
Loading

Reviews (1): Last reviewed commit: "perf(status): read the stored analyzer r..." | Re-trigger Greptile

@anshss
anshss merged commit 2a548ab into main Aug 4, 2026
7 checks passed
@anilmurty
anilmurty deleted the status-perf branch September 18, 2026 21:03
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.

1 participant