Update session metrics on createTurn and terminal writes - #479
Merged
Conversation
bhaveshpatel640
requested review from
chiragjn,
debajyoti-truefoundry,
heerambavi1998,
sr07asthana and
thesujai
as code owners
August 27, 2026 17:57
🦋 Changeset detectedLatest commit: de3ba3e The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
bhaveshpatel640
force-pushed
the
AGE-2010
branch
from
August 27, 2026 18:00
176ea12 to
23eb4ec
Compare
bhaveshpatel640
force-pushed
the
AGE-2010
branch
from
August 28, 2026 08:15
23eb4ec to
5f4ff3a
Compare
5 tasks
heerambavi1998
force-pushed
the
AGE-2010
branch
from
August 31, 2026 05:38
5f4ff3a to
c7f052c
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit c7f052c. Configure here.
heerambavi1998
force-pushed
the
AGE-2010
branch
from
August 31, 2026 06:11
c7f052c to
eeacd55
Compare
heerambavi1998
approved these changes
Aug 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Update
session.metricsin the same transactions as turn writes: turn count on create, cost and duration on a winning terminal transition.Linear: AGE-2010
Changes
createTurnincrementstotal_turnson the session tip update (same tx)running → terminal(updateTurnState, or freeze when cancel applies) addstotal_cost_in_usdandcompleted_at − created_atHow was this tested?
pnpm test:store:sqliteandscripts/test-store-local.shChecklist
pnpm build,pnpm test,pnpm typecheck,pnpm lint:ci, andpnpm format:checkpass locallypackages/trueforge-sdk,.github/fern/openapi/openapi.json,docs/openapi.json) — fork PRs omit SDK regen; maintainers regenerate after merge.env.exampleupdated if configuration or behavior changedNote
Medium Risk
Changes core session-store write paths and how usage/cost totals are derived; incorrect folding or double-counting would affect session-level metrics, though behavior is transactional and covered by contract tests.
Overview
Session
metricsare now updated in the same atomic operations as turn lifecycle writes, so turn count, cost, and duration stay consistent with persisted turns without separate aggregation.createTurnincrementssession.metrics.total_turnson the existing session tip update (Postgres/SQLite viajsonb_set, InMemory in process). Cost and duration are not touched at create time.Winning terminal transitions (
updateTurnStatewhenrunning →done/cancelled/error, andfreezeAndGetTurnonly when the cancel UPDATE actually matches) foldtotal_cost_in_usdfrom terminal turn metrics andtotal_duration_msascompleted_at − turn.created_at(truncated, floored at 0). Already-terminal freezes and losing second terminal writes do not double-count. Postgres sums duration with bigint to avoid int overflow on long sessions.The
ISessionStorecontract documents this behavior; shared store contract tests cover increment-only on create, single fold on terminal/freeze, conflict paths, and multi-turn accumulation.Reviewed by Cursor Bugbot for commit de3ba3e. Bugbot is set up for automated code reviews on this repo. Configure here.