fix: stable logged-cost pricing, local-day bucketing, cache savings - #20
fix: stable logged-cost pricing, local-day bucketing, cache savings#20masked8knights wants to merge 6 commits into
Conversation
- Use API-logged costs for Prime/Pi/OpenCode instead of models.dev estimates so totals match the provider dashboard and never drift on catalog refresh (costMode positive-logged-only). - Bucket usage days in the machine's local timezone, not UTC, so the current local day is not shown as yesterday. - Report cache savings whenever pricing is available, instead of zeroing it for logged-cost-only records. - Mark the OpenCode source skipped when the CLI is missing instead of failing the whole sync, so the dashboard stops reporting a scan failure for hosted-API users. - Skip zero-token/zero-cost assistant messages and guard null usage to avoid phantom $0 rows and scan crashes; cache version bumped to 3.
4a707d5 to
831e605
Compare
|
thanks @masked8knights for raising the PR, will take a look and merge it tomorrow |
# Conflicts: # README.md # collectors.test.ts # collectors.ts # lib/host-json-collector.ts # server.ts
There was a problem hiding this comment.
I resolved the current merge conflicts against main and pushed the resolution in 9d6e7a3.
@masked8knights A few follow-ups before merging:
-
The OpenCode SQL path still buckets and filters by UTC.
date(m.time_created / 1000, "unixepoch")and thestrftimecutoffs inserver.tsdo not use the enrolled host local timezone, whileparseOpenCodepreserves the returned day. This leaves the today-versus-yesterday bug unresolved for OpenCode near timezone boundaries. Please update both the grouping and cutoff and add a boundary regression test. -
The test suite passes in UTC (65/65), but currently fails under
America/Los_Angeles(2 failures) andPacific/Auckland(1 failure) because several local-day tests assert fixed UTC calendar dates. Please make these fixtures and expectations timezone-safe and add explicit positive/negative offset coverage. -
Please move the vendored plugin SDK declaration sync into a separate PR. The functional change is relatively small, while the two generated declaration files account for more than 11,000 changed lines and make this bug-fix PR much harder to review.
It would also be useful to make the intended timezone ownership explicit across the plugin server, enrolled hosts, and browser, since the current history boundary and dashboard filters can be evaluated in different timezones.
Follow-up to #17 (Prime Agent usage support). All build on the PR #17 head.
Problems fixed
Costs drift downward and don't match the provider dashboard. Prime/Pi were priced from the models.dev catalog (
estimated ?? logged), and every 15-minute sync re-priced the same tokens whenever the catalog refreshed (the bundled snapshot and the live models.dev fetch differ 8x for OpenCode Go models). Changed Prime, Pi, and OpenCode topositive-logged-onlyso the API-loggedusage.cost.totalfrom session logs is authoritative and history never changes on refresh.Today's usage shows as yesterday. Days were bucketed in UTC; machines in UTC+10 (or any positive offset) saw local-morning usage land in the previous UTC day. Bucketing now uses the machine's local timezone in the host collector,
collectors.ts,historyStartDay(), the dashboard SQL (date('now','localtime',...)), and the app'srangeDays(). Scan cache version bumped so rows re-parse once.Cache savings disappeared for logged-cost agents.
cacheSavingsUsdwas zeroed whencostModewaspositive-logged-only. Savings is an estimate from models.dev pricing and is now computed whenever pricing is available, regardless of how the cost itself is derived."1 agent scan failed" banner for hosted-API users. The OpenCode collector required the local
opencodeCLI. When the CLI is missing the source is now recorded asskipped(with a clear message) instead ofunavailable, so the banner disappears while hosted usage still arrives via Prime Agent sessions.Phantom $0 rows and scan crashes. Zero-token/zero-cost assistant messages are filtered, and a null-
usageguard fixes a TypeError that failed the whole host scan; failure messages now include the offending file path.Verification
tsc --noEmitclean,bb plugin buildOK.pricing_status: logged, day strings match the local calendar, cache savings > 0, source statusready(prime) /skipped(opencode).