Skip to content

Update session metrics on createTurn and terminal writes - #479

Merged
sr07asthana merged 4 commits into
mainfrom
AGE-2010
Aug 31, 2026
Merged

Update session metrics on createTurn and terminal writes#479
sr07asthana merged 4 commits into
mainfrom
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
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 metrics are 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.

createTurn increments session.metrics.total_turns on the existing session tip update (Postgres/SQLite via jsonb_set, InMemory in process). Cost and duration are not touched at create time.

Winning terminal transitions (updateTurnState when running → done/cancelled/error, and freezeAndGetTurn only when the cancel UPDATE actually matches) fold total_cost_in_usd from terminal turn metrics and total_duration_ms as completed_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 ISessionStore contract 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.

@changeset-bot

changeset-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: de3ba3e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@truefoundry/trueforge-core Patch
@truefoundry/trueforge Patch

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

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread packages/trueforge/src/db/postgres/session-store/queries/turns.ts Outdated
Base automatically changed from AGE-2008 to main August 31, 2026 06:11
Comment thread packages/trueforge-core/src/agent-session/store/ISessionStore.ts Outdated
Comment thread packages/trueforge/src/db/sqlite/session-store/queries/turns.ts Outdated
@sr07asthana
sr07asthana merged commit 6848d71 into main Aug 31, 2026
9 checks passed
@sr07asthana
sr07asthana deleted the AGE-2010 branch August 31, 2026 11:00
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.

3 participants