Skip to content

Persist session metrics JSON on Postgres and SQLite - #478

Open
bhaveshpatel640 wants to merge 1 commit into
mainfrom
AGE-2008
Open

Persist session metrics JSON on Postgres and SQLite#478
bhaveshpatel640 wants to merge 1 commit into
mainfrom
AGE-2008

Conversation

@bhaveshpatel640

@bhaveshpatel640bhaveshpatel640 commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Persist a zero-initialized metrics JSON document on agent sessions (total_cost_in_usd, total_duration_ms, total_turns).

Linear: AGE-2008

Public Session HTTP is unchanged (metrics stay store-only).

Changes

  • Add SessionMetrics / SessionRecord.metrics; createSession writes zeros (InMemory, Postgres, SQLite).
  • Postgres: session.metrics jsonb NOT NULL default + partial index (tenant_id, agent_id, created_at) for named-agent time windows.
  • SQLite: rebuild session (ADD COLUMN cannot take DEFAULT (jsonb(...))); same keys; parse metrics via JSON_RESULT_COLUMNS.
  • Store contract asserts create-time zeros. Patch changeset for trueforge-core + trueforge.

How was this tested?

  • Store contract: expect(session.metrics).toEqual({ total_cost_in_usd: 0, total_duration_ms: 0, total_turns: 0 }) (InMemory / Postgres / SQLite via existing store suites).
  • tsc --noEmit on @truefoundry/trueforge; eslint on Postgres + SQLite session query files.

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
Schema migrations alter the session table (SQLite table rebuild) and add a new required column; behavior is additive with defaults, but deploy order and migration failure modes need the usual care.

Overview
Adds store-layerSessionMetrics (total_cost_in_usd, total_duration_ms, total_turns) on SessionRecord, initialized to zeros when a session is created across InMemory, Postgres, and SQLite. The public Session HTTP schema is unchanged—metrics are persistence-only for now.

Postgres adds a NOT NULL session.metrics jsonb column (DB default zeros) and a partial index on (tenant_id, agent_id, created_at) for named-agent time-window queries (e.g. future GET /sessions/metrics). SQLite rebuilds session to add metrics with a jsonb default (ADD COLUMN limitation), registers metrics for JSON result parsing, and adds the same partial index.

Store contract tests assert create-time zero metrics; patch changeset for trueforge-core and trueforge.

Reviewed by Cursor Bugbot for commit d245de5. Bugbot is set up for automated code reviews on this repo. Configure here.

@changeset-bot

changeset-botBot commented Aug 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d245de5

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

This PR includes changesets to release 2 packages
NameType
@truefoundry/trueforge-corePatch
@truefoundry/trueforgePatch

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[bot]

This comment was marked as outdated.

@heerambavi1998

Copy link
Copy Markdown
Contributor

@bhaveshpatel640 where are we actually populating the values from? or is that a separate PR?

title,
last_turn_id,
custom,
jsonb('{"total_cost_in_usd":0,"total_duration_ms":0,"total_turns":0}'),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we are ok not backfilling metrics?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes.

Sign up for freeto 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

@bhaveshpatel640@heerambavi1998@debajyoti-truefoundry