Skip to content

Update session metrics on createTurn and terminal writes - #479

Open
bhaveshpatel640 wants to merge 1 commit into
AGE-2008from
AGE-2010
Open

Update session metrics on createTurn and terminal writes#479
bhaveshpatel640 wants to merge 1 commit into
AGE-2008from
AGE-2010

Conversation

@bhaveshpatel640

@bhaveshpatel640 bhaveshpatel640 commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Update session.metrics in the same transactions as turn writes: turn count on create, cost and duration on a winning terminal transition.

Linear: AGE-2010

Changes

  • createTurn increments total_turns on the session tip update (same tx)
  • running → terminal (updateTurnState, or freeze when cancel applies) adds total_cost_in_usd and completed_at − created_at
  • Postgres, SQLite, and InMemory; store contract tests for increment, cost/duration, two-turn sums, freeze once.

How was this tested?

  • Store contract (InMemory) plus pnpm test:store:sqlite and scripts/test-store-local.sh

Checklist

  • I have read the contributing guidelines
  • pnpm build, pnpm test, pnpm typecheck, pnpm lint:ci, and pnpm format:check pass locally
  • Tests added/updated where it makes sense
  • No hand-edits to generated code (packages/trueforge-sdk, .github/fern/openapi/openapi.json, docs/openapi.json) — fork PRs omit SDK regen; maintainers regenerate after merge
  • Docs / .env.example updated if configuration or behavior changed

Note

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 on ISessionStore describing the behavior.

createTurn increments total_turns on the session tip update (Postgres/SQLite via jsonb_set; in-memory via += 1), rolled back if the rest of the turn insert fails. When a turn first becomes terminal—updateTurnState or freezeAndGetTurn when the running→cancelled update wins—the store adds the turn’s metrics.total_cost_in_usd (default 0) and duration from completed_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.

@changeset-bot

changeset-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5f4ff3a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes changesets to release 2 packages
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

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.

1 participant