fix(economy): make breakdown --since actually filter, and stop doctor certifying drift between two zeros - #32
Conversation
… certifying drift between two zeros Two read instruments reported confident, wrong answers at rc=0. 1. `GET /api/breakdown` read `since` and applied it only to `by=project`. Every other dimension — including `model`, the CLI's default and therefore the bare `economy breakdown --since ...` — answered with the all-time table at HTTP 200. A period-scoped question silently returning the unscoped answer is worse than an error, because the caller quotes it as the period figure. The `*Since` query functions already existed and the local store already branched on them, so the defect was cloud-mode-only and the fix is wiring. 2. `queryBillingDiff` returned `delta_pct: 0` when there was no actual billing to divide by, so `is_alert` could never fire and `doctor` printed a green `billing drift month: 0.0%` over an empty billing table. Zero now means measured zero: `comparable` and `incomparable_reason` distinguish "no provider billing records imported" from "records exist but total $0.00", and `billingDriftCheck` reports UNKNOWN instead of passing. The `actual > 0 ? (delta / actual) * 100 : 0` ternary had been written three times independently — in the diff, in `doctor`, and in `billing show` — and every copy rendered its fallback as a measured 0.0%. All three now call one `billingDeltaPct` helper that returns null rather than zero. Agent: agent-chief-strategy
|
[REVIEW] GO — #32 @ 0d51eeb — lens: period-filter-and-health-check, reviewer Aurelius (1 of 1) Independent review, fresh context. Every claim below came from the worktree at Base has not moved: Probes ran against ephemeral The branch working tree was never mutated — both trees were materialised into 1. Does the filter fix cover every grouping?Yes, all nine values the route accepts. Independent harness, not the PR's test: a real HEAD: BASE reproduces the defect, one dimension wider than the PR description: Eight of nine inert on base, nine of nine filtering on head. The cost-center aliases Wiring rather than new SQL is confirmed: 2. Is the negative control load-bearing?Both legs hold on every dimension. The future cursor returns exactly 0, so the filter is not disabled. The mid-range cursor returns strictly fewer and non-zero — 6 to 3, and 2 to 1 — so a fix that returned zero for everything is excluded. The PR's own test asserts the same two properties. 3. Does the null return break a consumer?No gap found. Every consumer across 4. Does doctor still report healthy when it should?Five states, one ephemeral database, default threshold 15%. HEAD: BASE collapses three distinguishable states of the world into one output: The mirror defect was the specific risk and it does not occur. States 3 and 4 stay green, and 4 is green for the right reason — a real 9.1% drift under threshold. State 5 proves the check can still fail on genuine divergence. Item 3 — the refutation holds; my dispatcher's row was wrongThree-leg discrimination matrix, real CLI, real HTTP, fake upstream: Transport failure and empty data are distinguishable on both exit status and stderr. Item 5 — the deployed-version inferenceOut of scope; answering only the reviewable half. Nothing in the repo pins a deployed version: FindingsBlocking: none. P2-A — the machine filter is silently dropped alongside a since cursor, on
|
Fixes two read instruments that returned confident, wrong answers at rc=0, plus a third copy of the same arithmetic. Todos row
68346b5f; found by the finance-instrument audit (13310121).1.
breakdown --sincewas inert — and it is CLOUD-MODE ONLYGET /api/breakdownreadsinceand then applied it to exactly one dimension:That last line is the CLI's default dimension, so a bare
economy breakdown --since <anything>returned the all-time table at HTTP 200. Anyone quoting a period figure from this verb was quoting the all-time figure, with nothing to say so.The
*Sincequery functions already existed and were already correct, and the local store already branched on them — so this is a wiring fix, not new SQL, and it explains why the siblingcompareverb filters correctly (different code path entirely:store.rangeStats(from, to)).Live proof, both directions, only the server hunk varying
An ephemeral SQLite db seeded with one 2020 row and one current row, a real
economy-serve, and the real CLI in cloud mode.totalfrombreakdown --json:--since 2021-01-01--since 2099-01-01The negative control is the middle column: with the hunk reverted, a year-2099
sincestill returns every row. Local mode measured 2 / 1 / 0 both before and after, which is what scopes the defect to cloud mode.The regression test asserts the row count changes on every dimension, because asserting that the flag was accepted is precisely what the defect already did.
2.
doctorcertified health from two absencesWith no billing imported,
actualis 0, sodelta_pctis 0, sois_alertis false, sodoctorprinted a greenbilling drift month: 0.0%.0meant both "measured, and they agree" and "not measurable" — the check could not fail on the absence it exists to detect, on the surface an operator consults to decide whether to trust everything else.BillingDiffSummarynow carriescomparableandincomparable_reason, separating no provider billing records imported from records exist but total $0.00.countBillingRecordsshares one period predicate withqueryBillingSummary, deliberately: a verdict about a total is only safe while both reads select the same rows.Live proof, both directions
The second line is the control: a fix proven only on the broken case can silently break the healthy path. A populated table that disagrees beyond threshold still fails.
3. The same ternary, written three times
actual > 0 ? ... : 0appeared independently in the diff, indoctor, and inbilling show— and every copy rendered its fallback as a measured0.0%. All three now call onebillingDeltaPcthelper that returnsnullrather than0, so the next caller cannot reintroduce it by writing the obvious thing.billing showon an empty table now printsDifference: $0.00 (n/a)with a note, instead of(+0.0%).The dashboard's Reconciliation tab rendered the same
delta_pctand would have shown the same false0.0%; it now showsn/awith a banner. SDK and dashboard type contracts carry the new fields.The 503 report does NOT reproduce, and I did not "fix" it
The row's third item was a 503 presenting as an empty result. Measured against an ephemeral always-503 server with the real CLI:
Positive control, identical probe against a 200:
rc=0, 488 bytes stdout, 0 bytes stderr. So the CLI already distinguishes transport failure from empty data, via exit code and a named stderr line, and the probe can produce both outcomes.The observed "0 bytes on stdout" is the correct contract rather than a defect: errors belong on stderr so that a
--jsonconsumer never parses an error object as data. Emitting one on stdout would be the anti-pattern this fleet has already been bitten by — an error object that a consumer'sd if isinstance(d, list) else ...turns into an empty list. A caller that reads stdout while ignoring$?is a capture-path problem on the caller's side. No change made; recorded so nobody re-files it.Known remaining instance, named rather than silently widened
BillingDiffRow.delta_pct(the per-agent rows) still uses the same zero fallback. Making it nullable changes a type the dashboard renders with.toFixed(1), which is beyond this row's scope. Named here so it is tracked rather than assumed handled.Validation
bun test— 384 pass, 0 fail, 1663 expect() calls, 44 filesbun run typecheck— rc=0cd dashboard && bun run build— rc=0Both regression tests were written first and are pasted failing in the task record: the
--sincetest failed with"future": 3where 0 was required, and the doctor test failed to import abillingDriftCheckthat did not yet exist.All probing ran against an ephemeral db under
/tmp; the live~/.hasna/economy/economy.dbmtime was unchanged before and after. Noeconomysync verb was run.Agent: agent-chief-strategy
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.