Skip to content

feat(usage): attribute requests to agent sessions and projects - #14833

Open
fouadSalkini wants to merge 6 commits into
diegosouzapw:release/v3.8.52from
fouadSalkini:feat/agent-sessions-project-attribution
Open

fouadSalkini wants to merge 6 commits into
diegosouzapw:release/v3.8.52from
fouadSalkini:feat/agent-sessions-project-attribution

Conversation

@fouadSalkini

@fouadSalkini fouadSalkini commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Attributes each proxied request to a coding-agent session and project for team usage reporting:

  • open-sse/handlers/chatCore/agentContext.ts: pure extraction of client, clientSessionId, projectName, projectRepo, projectPath, and gitBranch.
    • Project resolution: explicit x-omniroute-project / x-omniroute-project-repo headers win; falls back to announced working directory in role: "system" message (Claude Code 2.1.282 mid-conversation-system beta), top-level system (older builds), or <environment_context><cwd> (Codex).
    • Worktree segments (/.claude/worktrees/, /.worktrees/) are stripped to resolve the true repository name.
    • Session resolution: x-omniroute-session-id -> x-claude-code-session-id -> metadata.user_id.session_id -> x-codex-session-id -> x-opencode-session -> metadata.session_id.
    • Keys with noLog drop prompt-derived fields (working directory, branch) while preserving explicit headers.
  • src/lib/db/agentSessions.ts: upserts one aggregate row per (apiKeyId, clientSessionId) with input/output/cache/reasoning tokens, request/error counts, priced cost_usd, and unpriced count. Without a client session id, requests from the same key and project within 30 minutes share a session.
  • src/lib/usage/usageHistory.ts: prices the request via calculateCostDetailed before the transaction, then records session usage and links usage_history.agent_session_id within the existing transaction.
  • Migrations 190_agent_sessions.sql and 191_usage_history_agent_session_id.sql.
  • docs/guides/CLAUDE-CODE-CONFIGURATION.md: documents usage attribution and the dynamic claude() shell wrapper using ANTHROPIC_CUSTOM_HEADERS.

Verification

node --import tsx/esm --test tests/unit/agent-context.test.ts
node --import tsx/esm --test tests/unit/agent-sessions-usage.test.ts
node --import tsx/esm --test tests/unit/db/migration-190-agent-sessions.test.ts
npm run typecheck:core
npm run check:docs-all
npm run check:file-size
npm run check:cycles

All passed clean.

⚠️ base-red inherited: #15306

@diegosouzapw diegosouzapw added the deferred-v3.8.52 Grande demais / suspeito para o lote atual; precisa de sessão dedicada no ciclo v3.8.52 label Sep 25, 2026
@diegosouzapw diegosouzapw changed the title feat(usage): attribute requests to agent sessions and projects [defer] feat(usage): attribute requests to agent sessions and projects Sep 25, 2026
…-sessions-project-attribution

# Conflicts:
#	config/quality/file-size-baseline.json
The release tip now owns 190_call_logs_content_provenance.sql, so this
PR's 190/191 collided. Move to the tentative slots 197/198 (age-order
scheme; diegosouzapw#14771 got 194, diegosouzapw#14801 got 196) pending maintainer confirmation,
rename the matching test, and update the migration-count claims in
README.md, AGENTS.md, llm.txt and its 66 i18n mirrors (regenerated with
scripts/i18n/sync-llm-mirrors.mjs).
@fouadSalkini

Copy link
Copy Markdown
Contributor Author

Updated to latest release/v3.8.51 base. Migration numbers 190/191 have been tentatively renumbered to 197/198 to avoid collision with base 190/191 — please confirm or assign official slots.

…-sessions-project-attribution

# Conflicts:
#	config/quality/file-size-baseline.json
@fouadSalkini

Copy link
Copy Markdown
Contributor Author

Merged the latest release/v3.8.51 to clear the conflict in config/quality/file-size-baseline.json. Every base entry is kept; the only change is the open-sse/handlers/chatCore.ts ceiling (6441 → 6442, re-measured on the merged tree: the +3 lines are this PR's own import, extraction call and failure-usage property) with a note that explains it. Migrations 197/198 stay above the base's highest (193). Focused tests pass (agent-context, agent-sessions-usage, migration-197: 20/20) and git merge-tree against the base reports no conflicts.

The migration-count docs failure is the inherited base-red #14866.

fouadSalkini added a commit to fouadSalkini/OmniRoute that referenced this pull request Sep 26, 2026
…egosouzapw#14864 agent sessions

Agent session and project attribution (diegosouzapw#14833), the /v1/me/sessions
self-service endpoints (diegosouzapw#14840), and /v1/me/sessions/{id}/messages with
opt-in turn capture (diegosouzapw#14864), including the chatCore wiring, as deployed.

Migrations keep the deployed numbers 9191-9193, which production databases
have already applied. chatCore.ts also carries the diegosouzapw#14314 and diegosouzapw#14862 wiring.
fouadSalkini added a commit to fouadSalkini/OmniRoute that referenced this pull request Sep 26, 2026
…zapw#14863 and diegosouzapw#14864 files

Adds the deployed useApiKeyDetails.ts (diegosouzapw#14863), the agent-session-messages
retention in cleanup.ts (diegosouzapw#14864), the agent session attribution in
usageHistory.ts (diegosouzapw#14833/diegosouzapw#14864), and the Opus 5.5 case in the combo catalog
test. The content is as deployed, reformatted by prettier only.
@diegosouzapw
diegosouzapw changed the base branch from release/v3.8.51 to release/v3.8.52 September 29, 2026 11:19
@diegosouzapw

Copy link
Copy Markdown
Owner

Re-homed to release/v3.8.52: v3.8.51 entered its release freeze, so the branch now belongs to the release captain and development continues on the next cycle. Nothing is wrong with this PR — it just needed a live base. No action needed from you; CI will re-run against the new base.

@diegosouzapw diegosouzapw changed the title [defer] feat(usage): attribute requests to agent sessions and projects feat(usage): attribute requests to agent sessions and projects Oct 1, 2026
@diegosouzapw diegosouzapw removed the deferred-v3.8.52 Grande demais / suspeito para o lote atual; precisa de sessão dedicada no ciclo v3.8.52 label Oct 1, 2026

This branch has not been deployed

No deployments
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