Repository navigation
fix(parser): range-filter calls inside turns instead of dropping day-spanning turns - #853
Conversation
…spanning turns parseProviderSources keyed the dateRange check on a turn's first call timestamp, so a long autonomous turn starting before midnight was excluded from the next day's view entirely and every post-midnight call in it was lost. Filter calls inside the turn instead and keep the turn when any call falls in range. Fixes getagentseal#852
Extends the per-call range filter so multi-day periods stop losing usage (review on getagentseal#857): - Re-anchor a sliced turn's timestamp to its first surviving call in parseProviderSources, so every turn slicer shares one split rule. - scanProjectDirs (Claude Code path): slice per call instead of dropping the whole turn on its first assistant timestamp; category/ subCategory/retries/hasEdits stay classified from the full turn. - aggregateProjectsIntoDays: bucket cost/calls/tokens (and the model, project, provider-slice rollups built from them) under each call's own day; turn-level stats (categories, editTurns, oneShotTurns) stay turn-anchored. This is the conservation fix: cache (<= yesterday) + live (today) unions now sum to the whole range for straddling turns. - buildJsonReport's dailyMap fallback follows the same per-call rule so the no-durable path can't diverge from durable.days. - filterProjectsByDateRange (dashboard) and filterProjectsByDays (menubar/history) slice per call instead of dropping whole turns. Adds the straddling-turn buildDurablePeriod case to the durable-totals parity suite (day-N + day-N+1 == whole-range calls/cost/tokens, verified to fail without the fix), covers the today view and the surface filters, and makes the suite hermetic on machines with real provider data.
|
@iamtoruk Thanks for the thorough review on #857 — this update (7591e68) lands the conservation-correct version here, point by point:
Verification: |
…rovider path The Codex/OTel/network provider path classified a date-sliced turn from only its in-range calls (cachedTurnToClassified(slicedTurn)), while the Claude path kept the full-turn classification. So a midnight-straddling turn was categorized and edit-counted differently by provider — a Read at 23:59 plus an Edit at 00:05, sliced to the 23:59 side, read as coding/hasEdits on Claude but exploration/no-edit on Codex. This contradicted the documented intent that category/hasEdits/retries are whole-exchange judgments, not per-call sums. Classify the full turn, then trim its calls to the range (mirroring the Claude path's classifiedTurnSlicedToRange). Cost/calls still come from the retained calls, so conservation is unchanged; only the turn-level judgments are now provider-independent.
iamtoruk
left a comment
There was a problem hiding this comment.
Reviewed on 6c411ad. This is the conservation-correct fix for #852, and I verified it end to end.
Conservation (the hard part) holds. Driven through the real buildDurablePeriod cache+period path with a turn straddling local midnight (call1 23:58 $0.006, call2 00:10 $0.012): today = 1 call/$0.012, week = month = 2 calls/$0.018, days split correctly (07-28: $0.006, 07-29: $0.012). No call is lost or double-counted. The per-call bucketing in day-aggregator.ts and main.ts (the layers a parse-only slice misses) is what makes the union exact.
Cross-surface parity holds. turnIsInDateRange is gone and filterProjectsByDateRange (dashboard) and filterProjectsByDays (menubar) now slice per-call, so headline / dashboard breakdown / menubar all agree (today 1, week 2). An adversarial pass also confirmed: cost/calls/tokens conserve across single-midnight, two-midnight, DST, and malformed-timestamp cases; the spawn-anchor change is a genuine fix (it recovers parent spend main was dropping); and the same-day common case is byte-identical to main.
One thing I fixed and pushed (5a12dcc). The provider path classified a sliced turn from only its in-range calls (cachedTurnToClassified(slicedTurn)) while the Claude path kept the full-turn classification, so a straddling turn's category/hasEdits differed by provider — contradicting this PR's own comment that those are whole-exchange judgments. I changed the two provider loops to classify the full turn then trim (mirroring the Claude path); cost/calls are unchanged, only the turn-level judgments are now provider-independent. Added a regression test (provider-turn-grouping, mutation-checked: it fails on the pre-fix classification and passes now). Also merged main to resolve the 7-commit lag (clean).
The editTurns sum-across-disjoint-reports inflation is documented/accepted and cost-safe (the persisted durable path parses the window once and doesn't inflate). tsc clean; full suite green apart from the two pre-existing parser.test.ts failures that reproduce on main. Merging this and closing #857 in its favor.
…he all-provider view buildDurablePeriod derived the today slice of the multi-day, all-provider headline from the unsliced whole-range parse, so a turn spanning local midnight kept its category and turn count anchored on its yesterday start. The per-call cost and calls bucketed onto today correctly, but By Activity and the JSON daily turn count lost the post-midnight half — categories summed to only the pre-midnight cost while the headline, By Model and By Project were right. Slice the today parse with filterProjectsByDays first, which re-anchors the straddling turn to its surviving today calls, so today's category cost lands on today. Category cost is the sum of the slice's own calls, so day-N + day-N+1 still equals the whole-range total (no over-count); the per-day turn-count split matches the cache side and the documented per-day semantics. Adds a regression test in the straddling-turn conservation suite (mutation-checked: fails on the pre-fix code). Also fills in the CHANGELOG Unreleased entries for the batch (#853, #856, #872, #846/#859, #866/#867, #833).
Fixes #852.
Bug:
parseProviderSourcesrange-filtered turns by the turn's first call timestamp (turn.calls[0]?.timestamp). A long autonomous turn that starts before midnight (e.g. a 30h Codex run) was therefore dropped wholesale from the next day's view —today/ single-day exports lost every post-midnight call in the turn.Fix: filter calls inside the turn against the range instead. A turn is skipped only when none of its calls fall in range; otherwise it is kept with just the in-range calls, and downstream derivation (classified turn, project, sessionId, projectPath) uses the filtered turn. Applied to both identical filter blocks (served-sources loop and durable-orphans loop) in
src/parser.ts.Tests:
tests/provider-turn-grouping.test.ts: a synthetic Codex rollout whose single turn hastoken_countevents straddling midnight; querying the next day's range now keeps the turn with only the post-midnight call. Verified the test fails without the fix and passes with it.tsc --noEmitandtsupbuild are green; parser-related suites (parser, session-cache, codex, date-range, export, durable-totals) pass. Full suite diffed againstmain: identical failure sets (pre-existing Windows-environment issues — missing jsdom, 5s-timeout flakes,os.homedir()fixtures), no new failures.