Update session metrics on createTurn and terminal writes - #479
Open
bhaveshpatel640 wants to merge 1 commit into
Open
Update session metrics on createTurn and terminal writes#479bhaveshpatel640 wants to merge 1 commit into
bhaveshpatel640 wants to merge 1 commit into
Conversation
bhaveshpatel640
requested review from
chiragjn,
debajyoti-truefoundry,
heerambavi1998,
sr07asthana and
thesujai
as code owners
August 27, 2026 17:57
|
| Name | Type |
|---|---|
| @truefoundry/trueforge-core | Patch |
| @truefoundry/trueforge | Patch |
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a 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
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
Touches core session-store transactions and billing-adjacent aggregates; incorrect double-folding or missed increments would skew session metrics under concurrency, though first-terminal-wins semantics are tested.
Overview
Session-level aggregates (
total_turns,total_cost_in_usd,total_duration_ms) are now updated inside the same store transactions as turn lifecycle writes, with contract docs onISessionStoredescribing the behavior.createTurnincrementstotal_turnson the session tip update (Postgres/SQLite viajsonb_set; in-memory via+= 1), rolled back if the rest of the turn insert fails. When a turn first becomes terminal—updateTurnStateorfreezeAndGetTurnwhen the running→cancelled update wins—the store adds the turn’smetrics.total_cost_in_usd(default 0) and duration fromcompleted_at − created_at(truncated, floored at 0). Already-terminal freezes and losing second terminal writes do not fold again.Shared helpers implement this for Postgres, SQLite, and
InMemorySessionStore; the contract suite adds coverage for per-create increment, single fold, conflict idempotency, and multi-turn sums.Reviewed by Cursor Bugbot for commit 5f4ff3a. Bugbot is set up for automated code reviews on this repo. Configure here.