Repository navigation
Conversation
…d of dropping it Both parseProviderSources (Codex/OTel/network providers) and scanProjectDirs (Claude Code, the highest-traffic provider) range-filtered a whole turn by its first call's timestamp. A turn spanning local midnight — common for long-running autonomous sessions — had every call after the boundary silently dropped from the new day: `codeburn today` reported no usage until the turn ended, and daily history attributed the whole turn's cost to the turn-start day. Slice each turn to only its in-range calls instead of dropping it wholesale, and re-anchor `timestamp` to the first surviving call so day-aggregator's turn-anchored day bucketing (which keys off `timestamp`, not per-call timestamps) attributes the retained calls to the day they actually occurred, not the pre-slice turn's original day. - parseProviderSources: turnSlicedToRange slices a CachedTurn's raw calls, applied in both the servedSources and durableSources loops (both had the identical bug). - scanProjectDirs: classifiedTurnSlicedToRange slices a ClassifiedTurn's assistantCalls post-classification. category/subCategory/retries/hasEdits stay exactly as classified from the complete turn (turn-level judgments, not per-call sums) — only assistantCalls and timestamp are trimmed. The PR-linked subagent fold-anchor mechanism (a parent whose own turns are fully out of range still folds an in-range child with zero parent spend) is untouched: it depends on carriedBranch/spawnPrSets built from the FULL pre-slice turn list, and a slice only ever produces a non-empty assistantCalls when the turn genuinely has in-range calls, so it can't turn a real anchor into a spend-bearing turn or vice versa. - Both slicers share a small `callsInRange` predicate; the two composite objects (CachedTurn vs ClassifiedTurn) are built separately since their shapes genuinely differ. Fixes #852. Repro, root-cause diagnosis, and the initial fix shape (filter calls inside the turn rather than dropping it, applied to both provider loops) are from the issue reporter (KENSHI601), verified against the current parser and extended to the Claude Code path and the day-aggregator timestamp reconciliation.
dd110d9 to
1643aa9
Compare
iamtoruk
left a comment
There was a problem hiding this comment.
The today fix is genuine, but as it stands this trades a visible bug for a silent one: periods longer than a day lose usage. Requesting changes on a proven conservation violation.
The blocker, reproduced end-to-end through buildDurablePeriod (the shared headline builder behind CLI/TUI/menubar): turn starting 07-27 23:57 with calls at 23:58 ($0.006) and 00:10 ($0.012). Truth for week/month = 2 calls / $0.018.
| period | main | this PR |
|---|---|---|
| today | 0 / $0 (the bug you're fixing) | 1 / $0.012 ✓ |
| week | 2 / $0.018 ✓ | 1 / $0.006 |
| month | 2 / $0.018 ✓ | 1 / $0.006 |
Mechanism: the backfill parse now slices the turn at the backfillStart..yesterdayEnd boundary, so the cached yesterday keeps only pre-midnight calls — but aggregateProjectsIntoDays stays turn-anchored, so in the period parse the whole turn buckets under yesterday and the today bucket gets zero. unionDaysForPeriod = cache (≤ yesterday) + live (today): the post-midnight calls are in neither. Per-call slicing and per-turn day bucketing are incompatible — the "out of scope" note on aggregateProjectsIntoDays is exactly where the gap opens. On a real month of data: 12 straddling turns, $8.98 silently gone.
Second blocker — half-applied across surfaces: filterProjectsByDateRange (dashboard Today/7-Days) and filterProjectsByDays (menubar/history) still whole-turn-drop, so the same data yields different Today numbers per surface — the exact divergence class the durable-totals parity suite exists to prevent (which has no midnight-straddling case; one should land with whichever PR carries this fix).
Also worth stating: turn-level aggregates (editTurns, categories, workflow) now report on BOTH sides of a split — a turn whose only Edit happened entirely on day N reports editTurns=1 on day N+1 too. If that's the intended semantics it needs documenting; summed-across-days turn counts inflate.
Given your own note that #853 should take precedence: agreed — suggest closing this in favor of landing the conservation-correct version once, there. Two things to carry over: (a) #853 currently does NOT re-anchor timestamp after slicing, so the two implementations diverge on the split rule — whichever survives should pin down one rule; (b) #853 doesn't touch scanProjectDirs, so its exposure at the cache-backfill boundary is structurally the same question and needs the same regression test (a straddling-turn buildDurablePeriod case asserting day-N + day-N+1 == whole-range for calls, cost, and tokens — I have a failing test ready to contribute).
Side-finding while auditing: parseSessionFile (~:1832-1846) is dead code carrying a fourth copy of the old filter and the historically-important "orphan assistant calls" comment — worth a separate cleanup so the next person doesn't have to reconcile four filter sites.
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
left a comment
There was a problem hiding this comment.
Re-reviewed 1643aa9. The today fix is real and the re-anchor is a reasonable idea, but I have to keep this at changes-requested: reproduced end to end, the current commit does not fix the week/month conservation I flagged before, and it now regresses it below main. Requesting changes on a proven data-loss.
Blocker 1 (still open, now a regression): week/month silently lose the post-midnight call
Driven through the real buildDurablePeriod path (the shared headline behind codeburn today/week/month, the TUI, and the menubar): daily cache on disk for the historical range, unioned with a live parseAllSessions for today. Faked clock at local 2026-07-29 00:30 (UTC+8). Fixture is the same straddling turn as last time: one turn, call1 2026-07-28 23:58 ($0.006), call2 2026-07-29 00:10 ($0.012). Truth: today = 1 call / $0.012; week/month = 2 calls / $0.018.
| surface | this PR | main | truth |
|---|---|---|---|
| today | 1 call / $0.012 (fixed) | 0 / $0 (the #852 bug) | 1 / $0.012 |
| week (7d) | 1 call / $0.006 | 2 / $0.018 | 2 / $0.018 |
| month | 1 call / $0.006 | 2 / $0.018 | 2 / $0.018 |
main counted week/month correctly; this PR drops call2 ($0.012) from both. It trades a visible bug (today under-reports) for a silent one (week/month under-report), on the surfaces most people read.
Why the re-anchor does not reach this: the re-anchor only moves the day when a turn is actually sliced, i.e. when part of it falls outside the range. A week/month live parse spans the whole turn, so nothing is sliced, and the intact turn buckets under turn.timestamp's day (day-aggregator.ts buckets per turn, not per call). So todayAllDays is empty for this turn, and unionDaysForPeriod takes historical-from-cache (2026-07-28 = call1 only) + today-from-live (empty) → call2 lands in neither. The day-rows the harness printed confirm it: this PR's week is 2026-07-28: 1c/$0.006, with 2026-07-29 absent.
Scope: a turn straddling an interior week boundary (both days inside one cache parse) conserves on both this PR and main. The loss is specific to the yesterday/today cache seam, so it recurs for whichever turn straddled the last local midnight, in every period that includes today.
Blocker 2 (still open): the parse path slices but the four post-parse filters do not
turnIsInDateRange (parser.ts:3198) still filters a whole turn by its first call, and it feeds both filterProjectsByDateRange (:3533, dashboard Today/7-Days and codeburn export) and filterProjectsByDays (:3322, menubar day view). Parsing the same straddling turn over a broad range and then filtering to today:
| path (Today) | this PR | main |
|---|---|---|
sliced headline (buildDurablePeriod) |
1 / $0.012 | 0 / $0 |
filterProjectsByDateRange (dashboard breakdown, export) |
0 / $0 | 0 / $0 |
filterProjectsByDays (menubar) |
0 / $0 | 0 / $0 |
On this PR the header reads $0.012 while the dashboard project breakdown, codeburn export, and the menubar all read $0 for the same Today. On the dashboard specifically the week header ($0.006) also contradicts its own period breakdown ($0.018). main was internally consistent (all agreeing, even if wrong per #852); this PR makes surfaces disagree.
Also (not blocking on their own, but part of the same root cause)
- Turn-count double-count:
classifiedTurnSlicedToRangekeepshasEdits/categoryfrom the full turn, so a turn whose only Edit was in call1 reportseditTurns=1on day N and day N+1. Summed across days that is 2 for one turn. The Codex/OTel path instead re-classifies from the sliced calls (cachedTurnToClassified(slicedTurn)), so the two providers now attribute edits/categories to slices by different rules. - Dead code:
parseSessionFile(parser.ts:1790) still carries a fourth copy of the old whole-turn filter (:1838). It has no callers and is unexported, so no runtime impact, but it re-introduces #852 the moment anyone rewires it. Worth deleting in whichever PR settles this.
Root cause and suggested direction
Slicing the three parse loops is only one of the places the day rule lives. As long as day-aggregator buckets per turn, unionDaysForPeriod splits today-from-live vs historical-from-cache, and the four post-parse filters drop per turn, a sliced parse and the aggregate/filter/cache layers disagree at the yesterday/today seam. The fix needs one consistent per-call day rule across all of them, not a slice bolted onto turn-anchored bucketing.
Concretely, whichever version lands should carry a single regression test that asserts, through buildDurablePeriod, that for a straddling turn day-N + day-(N+1) == whole-range for calls, cost, and tokens (and, if turn counts are meant to be per-day, that they do not sum above the real turn count). This is the assertion that would have caught the table above; the current tests exercise aggregateProjectsIntoDays/getDateRange directly and never hit the cache+period path where the loss happens. I have that failing test ready to contribute.
As before, given the overlap with #853, my suggestion is to settle one conservation-correct rule once rather than iterate slices on top of turn bucketing. Happy to re-review quickly once week/month conserves and the surfaces agree.
|
Superseded by #853, which merged in b94d0ac. #853 fixes the same #852 defect but is conservation-correct end to end (week/month keep the post-midnight call — verified through |
Relates to #852.
The defect
Both
parseProviderSources(Codex, OTel, network providers) andscanProjectDirs(Claude Code) range-filtered a whole turn by its first call's timestamp. A turn spanning local midnight — common for long autonomous sessions — had every call after the boundary silently dropped from the new day:codeburn todayreported no usage until the turn ended, and daily history attributed the whole turn's cost to the turn-start day.Delta over #853
#853 fixes the two
parseProviderSourcesloops. This branch additionally carries:1. The Claude Code path.
scanProjectDirshas the structurally identical whole-turn drop, filtering onturn.assistantCalls[0].timestamp. Claude Code is the highest-traffic provider in this tool, so the reported symptom persists for most users until this path is fixed too. Handled byclassifiedTurnSlicedToRange, which slicesassistantCallsand leavescategory/subCategory/retries/hasEditsas classified from the complete turn — they are computed once ahead of the date filter, forcarriedBranch/PR-ref tracking reasons already in the code, and are turn-level judgments rather than per-call sums.2. A
timestampre-anchor on a sliced turn.day-aggregator.tsbuckets byturn.timestamprather than per-call timestamps — deliberately, perae18b1f. Without re-anchoring the sliced turn to its first surviving call, the retained post-midnight calls are still filed under the pre-slice day, so they reappear inparseAllSessionsoutput but remain invisible incodeburn today, which is the reported symptom. Verified againstgetDateRange('today')andaggregateProjectsIntoDaysunder a simulated UTC+8 clock.Fold-anchor safety
The PR-linked subagent fold anchor — a parent whose own turns fall entirely out of range is kept as a zero-cost anchor so an in-range child can still attribute — is unchanged.
carriedBranch,carriedPrRefsandspawnPrSetsare built fromcachedFile.turns, the full pre-slice list, before the date filter runs. A slice yields a non-empty call list only when the turn genuinely has an in-range call, so a fully out-of-range turn still filters to nothing. The anchor test passes identically before and after the change, which is the correct obligation: the mechanism was not broken and must not break.Testing
Two regression files built from @KENSHI601's synthetic repro, made deterministic across machine timezones rather than dependent on the runner's local zone:
tests/parser-turn-midnight-range.test.ts(provider path) andtests/parser-claude-turn-midnight-range.test.ts(Claude Code path).Each covers: a turn spanning the boundary attributes post-boundary calls to the new day and pre-boundary calls to the prior day; a same-day control turn is unchanged; a turn entirely outside the range is still excluded; the unfiltered monthly/total aggregate does not double-count; and
aggregateProjectsIntoDayswithgetDateRange('today')reconciles under a simulated UTC+8 clock. The Claude Code file adds the fold-anchor case.All confirmed failing before the change. Full suite 2475 passed,
tsc --noEmitclean.Out of scope
aggregateProjectsIntoDays(dateKey(call.timestamp)instead of the turn day) is deliberately not included. Once turns are sliced per range, a day-scoped query already sees only that day's calls. Reintroducing raw per-call bucketing on a wide unsliced parse — the common path for history and menubar rollups — would resurrect the reconciliation bugae18b1ffixed, where a midnight-straddling turn split across two days andhistory.dailystopped reconciling to the live headline.filterProjectsByDateRange/turnIsInDateRange, the dashboard's in-memory re-narrowing over an already-wide parse, has the same shape but operates on turns already merged intoProjectSummary. Not covered here; a candidate follow-up.Credit
Root-cause diagnosis, the synthetic repro, and the fix shape are @KENSHI601's, in issue #852 and PR #853.