Skip to content

Track token usage per session and objective#7

Merged
nathanwhit merged 1 commit into
nathanwhit:mainfrom
nathanwhitbot:orcha/impl-50aaa6c9
Jun 12, 2026
Merged

Track token usage per session and objective#7
nathanwhit merged 1 commit into
nathanwhit:mainfrom
nathanwhitbot:orcha/impl-50aaa6c9

Conversation

@nathanwhitbot

Copy link
Copy Markdown
Contributor

Summary

  • Persist per-session token totals and migrate existing SQLite databases with a used_tokens column.
  • Increment session totals from recordUsage while preserving provider bucket scheduling behavior.
  • Add objective usage aggregation and expose it through objective/session API payloads plus GET /api/objectives/{id}/usage.
  • Surface session and objective token usage in the dashboard with readable formatting.

Verification

  • go test ./...
  • npm --prefix ui run build

nathanwhit added a commit that referenced this pull request Jun 12, 2026
A rebase follow-up that died without landing the fix (e.g. it couldn't push)
marked the conflict feedback handled, so the head SHA being unchanged meant a
re-sync deduped and never tried again — the PR stayed conflicting forever.

RefreshPR now only dispatches a conflict follow-up when none is active for the
PR (no duplicates while one is in flight), and clears stale handled conflict
feedback first so a finished-but-unresolved conflict re-fires on the next
observation. This is what lets PR #7 retry on its own once a working force-push
exists.

Test: a conflicting PR spawns one follow-up; no duplicate while it is active; a
new one once it finishes still unresolved.
@nathanwhitbot

Copy link
Copy Markdown
Contributor Author

Resolved the merge conflict with main by rebasing this branch onto the latest base (now abc5dcd) and force-pushing.

Conflict: only internal/orch/orch_test.go conflicted. Both sides added new test functions at the same location with no overlapping logic — main added TestManagerNotify_WaitsForPendingDependents, TestMarkObjectiveDone_GatedOnOpenPRs, TestConflictingPR_SpawnsRebaseFollowup, and TestUpdatePR_ForcePushViaMCP; this PR added TestRecordUsage_IncrementsSessionAndProviderBucket. I kept all of them. The other 12 files merged automatically.

Verification after rebase:

  • go build ./... — clean
  • go test ./... — all packages pass
  • UI npm run build (tsc + vite) — passes

The branch is now conflict-free and up to date with main.

@nathanwhit nathanwhit enabled auto-merge (squash) June 12, 2026 21:09
@nathanwhit

Copy link
Copy Markdown
Owner

needs rebase to pick up fix of hanging test (cause of failing CI)

Attribute agent token consumption to the session that spent it, in addition
to the existing provider usage buckets used for scheduling. This enables a
per-session and per-objective view of token spend in the dashboard.

Backend:
- schema: add sessions.used_tokens (INTEGER NOT NULL DEFAULT 0), plus an
  idempotent ALTER-based migration so pre-existing DBs backfill to 0.
- store: AddSessionTokens / SessionUsedTokens, and ObjectiveUsage which
  aggregates an objective's sessions into a total with per-session and
  per-provider breakdowns (usage_provider overrides agent).
- orch: recordUsage now also increments the session total alongside the
  provider bucket and transcript usage message.
- api: GET /api/objectives/{id}/usage; usage summary embedded in objective
  detail; used_tokens flows through session + dashboard JSON.

Frontend:
- api.ts: used_tokens on Session/DashboardSession, ObjectiveUsageSummary
  types, and a compact formatTokens helper (e.g. 12.3k).
- Objective page shows objective total + per-provider chips and a per-session
  Tokens column; SessionDetail shows the session's token usage.

Tests: store aggregation/scoping/provider-override, recordUsage session vs
provider-bucket behavior, and the usage endpoint + JSON fields.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
auto-merge was automatically disabled June 12, 2026 21:27

Head branch was pushed to by a user without write access

@nathanwhitbot

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main to pick up the hanging-test fix (#8, fix test) that was causing CI to fail. The token-usage commit now sits cleanly on top of it — no conflicts. Verified locally: go build ./... succeeds and go test ./... passes across all packages. Force-pushed the rebased branch (head 99e5d59).

@nathanwhit nathanwhit enabled auto-merge (squash) June 12, 2026 21:28
@nathanwhit nathanwhit merged commit 868db73 into nathanwhit:main Jun 12, 2026
1 check passed
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.

2 participants