Skip to content

fix(parser): range-filter calls inside turns instead of dropping day-spanning turns - #853

Merged
iamtoruk merged 4 commits into
getagentseal:mainfrom
KENSHI601:fix/turn-range-per-call-filter
Aug 1, 2026
Merged

iamtoruk merged 4 commits into
getagentseal:mainfrom
KENSHI601:fix/turn-range-per-call-filter

Conversation

@KENSHI601

Copy link
Copy Markdown
Contributor

Fixes #852.

Bug: parseProviderSources range-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:

  • New regression test in tests/provider-turn-grouping.test.ts: a synthetic Codex rollout whose single turn has token_count events 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 --noEmit and tsup build are green; parser-related suites (parser, session-cache, codex, date-range, export, durable-totals) pass. Full suite diffed against main: identical failure sets (pre-existing Windows-environment issues — missing jsdom, 5s-timeout flakes, os.homedir() fixtures), no new failures.

…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.
@KENSHI601

Copy link
Copy Markdown
Contributor Author

@iamtoruk Thanks for the thorough review on #857 — this update (7591e68) lands the conservation-correct version here, point by point:

  1. Conservation (blocker) — aggregateProjectsIntoDays now buckets cost/savings/calls/tokens (and the model, project, and provider-slice rollups built from them) under each call's own day. Turn-level stats (categories, editTurns, oneShotTurns) stay turn-anchored, with the both-sides-of-a-split semantics documented in code. Your repro now reads today 1/$0.012, week/month 2/$0.018, with day-N + day-N+1 == whole range.
  2. Re-anchor — sliced turns get timestamp = first surviving call in both parseProviderSources loops, adopting parser: slice midnight-straddling turns instead of dropping them #857's split rule (callsInRange / turnSlicedToRange / classifiedTurnSlicedToRange, same null-on-empty contract).
  3. scanProjectDirs — slices per call instead of whole-turn drop; category/subCategory/retries/hasEdits stay classified from the full turn, per avs-io's approach.
  4. Regression test — added the straddling-turn buildDurablePeriod case to the durable-totals parity suite: day-N + day-N+1 == whole-range calls/cost/tokens, plus a today-only view case on the Claude path and surface-filter cases. Verified all three fail without the fix (including on this PR's previous revision) and pass with it.
  5. Surfaces — filterProjectsByDateRange (dashboard) and filterProjectsByDays (menubar/history) now slice per call too, so every surface reports the same Today number; buildJsonReport's dailyMap fallback follows the same per-call rule so it can't diverge from durable.days. turnIsInDateRange, orphaned by this change, is removed; parseSessionFile left untouched as agreed.

Verification: tsc --noEmit clean, tsup build ok, and the parser / day-aggregator / durable-totals / session-cache / codex / export suites are green — the durable-totals file is now also hermetic on machines with real provider data (USERPROFILE / KIMI_CODE_HOME / desktop-sessions redirects in beforeEach), which is what its 5s-timeout flakiness on this box actually was. Full-suite diff against the previous revision shows no new failures; remaining red is pre-existing environment noise (missing jsdom, homedir-based fixtures, load-sensitive timeouts).

…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 iamtoruk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@iamtoruk
iamtoruk merged commit b94d0ac into getagentseal:main Aug 1, 2026
@KENSHI601
KENSHI601 deleted the fix/turn-range-per-call-filter branch August 3, 2026 02:14
iamtoruk added a commit that referenced this pull request Aug 3, 2026
…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).
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.

Usage after local midnight vanishes when a turn spans days (turn-level range filter drops post-midnight calls)

2 participants