Skip to content

Add session metrics API and per-session totals - #484

Merged
sr07asthana merged 26 commits into
mainfrom
AGE-2011
Sep 1, 2026
Merged

Add session metrics API and per-session totals#484
sr07asthana merged 26 commits into
mainfrom
AGE-2011

Conversation

@bhaveshpatel640

@bhaveshpatel640 bhaveshpatel640 commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Caller-scoped session metrics for named agents: persist rolled-up per-session totals, and expose meters / chart catalog / chart-data APIs aligned with the ServiceFoundry-style dashboard split.

Closes AGE-2011

Changes

  • Persist Session.metrics (total_cost_in_usd, total_duration_ms, total_turns): zero on create; createTurn increments turns; terminal writes fold cost + duration
  • Store APIs: getSessionMetricsMeters / getSessionMetricsChartData on InMemory, Postgres, and SQLite (shared builders + inclusion rules)
  • HTTP (caller-scoped, fern-ignored):
    • GET /api/v1/sessions/metrics/meters
    • GET /api/v1/sessions/metrics/charts
    • GET /api/v1/sessions/metrics/charts-data
  • Duration distributions use total_duration_ms > 0 (not total_turns > 0) so in-flight first turns don’t collapse min/median/p95 to 0ms

How was this tested?

  • Unit: packages/trueforge-core session metrics builders + fold (sessionMetrics.test.ts)
  • Store contract: InMemory + SQLite (storeContractSuite session metrics cases, including all-12-meters fixture)
  • HTTP: packages/trueforge/tests/unit/apis/sessionHttp.test.ts
  • Local: pnpm --filter @truefoundry/trueforge-core test (targeted), pnpm --filter @truefoundry/trueforge test:store:sqlite (metrics cases)

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) — metrics routes are fern-ignored; fork PRs omit SDK regen; maintainers regenerate after merge
  • Docs / .env.example updated if configuration or behavior changed

Note

Medium Risk
Breaking API change requiring metrics on every Session, plus new aggregation paths over cost/duration data that must stay caller-scoped via auth.

Overview
Adds caller-scoped session metrics for named agents: rolled-up Session.metrics (cost, duration, turns) is now required on session API responses, and new authenticated /internal/metrics endpoints expose dashboard-style meters, a chart catalog, and zero-filled chart time series filtered by agent_id, created_by, and session created_at (hourly buckets ≤24h, daily otherwise; 30-day max window).

Server wiring introduces ISessionMetricsStore with shared aggregation/chart builders and SQLite/Postgres query implementations; the SDK/OpenAPI gain client.internal.metrics (listCharts, getChartData, getMeters) plus the new metrics types.

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

@changeset-bot

changeset-bot Bot commented Aug 28, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ef62077

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

Comment thread packages/trueforge/src/routes/sessionRoutes.ts Outdated
Comment thread packages/trueforge-core/src/agent-session/schemas/session.ts Outdated
Comment thread packages/trueforge/src/schemas/session.ts Outdated
Comment thread packages/trueforge-core/src/agent-session/schemas/session.ts Outdated
@bhaveshpatel640
bhaveshpatel640 force-pushed the AGE-2011 branch 2 times, most recently from a647804 to 4f7e36f Compare August 28, 2026 09:30
Comment thread packages/trueforge/src/db/sessionMetricsStore.ts
Comment thread packages/trueforge/src/db/sessionMetricsStore.ts
Comment thread packages/trueforge/src/schemas/session.ts Outdated
Comment thread packages/trueforge-core/src/agent-session/store/InMemorySessionStore.ts Outdated

@heerambavi1998 heerambavi1998 left a comment

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 need tests specifically for the DB queries for both impl.

Comment thread .github/fern/openapi/openapi.json Outdated
Comment thread .github/fern/openapi/openapi.json Outdated
Comment thread .github/fern/openapi/openapi.json Outdated
Comment thread .github/fern/openapi/openapi.json Outdated
Comment thread packages/trueforge/src/db/postgres/migrations/20260827_000001_session_metrics.ts Outdated
Comment thread packages/trueforge/src/db/postgres/session-store/queries/sessions.ts Outdated
Comment thread packages/trueforge/src/apis/sessions.ts Outdated
Comment thread packages/trueforge/src/db/sqlite/session-store/queries/sessions.ts Outdated
Base automatically changed from AGE-2010 to main August 31, 2026 11:00

@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 ef62077. Configure here.

Comment thread packages/trueforge/src/apis/sessionMetrics.ts
sr07asthana and others added 2 commits September 1, 2026 11:16
@sr07asthana
sr07asthana merged commit 172bf14 into main Sep 1, 2026
11 checks passed
@sr07asthana
sr07asthana deleted the AGE-2011 branch September 1, 2026 05:52
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