Skip to content

fix(mcp-server): attribute mcp_metrics session via client-identity fallback#546

Merged
NagyVikt merged 1 commit into
mainfrom
agent/claude/metrics-wrapper-session-attribution-via-2026-05-14-23-51
May 14, 2026
Merged

fix(mcp-server): attribute mcp_metrics session via client-identity fallback#546
NagyVikt merged 1 commit into
mainfrom
agent/claude/metrics-wrapper-session-attribution-via-2026-05-14-23-51

Conversation

@NagyVikt
Copy link
Copy Markdown
Collaborator

Automated by gx branch finish (PR flow).

…llback

`colony gain` was bucketing ~9,000 calls/day into a single `<unknown>`
session row because high-volume read-only tools (task_plan_list,
get_observations, search, task_timeline, list_sessions, examples_list,
…) have no `session_id` in their schema — `metricContextOf` had nothing
to attribute the call to and recorded `session_id: null`, which the
sessions aggregate SQL collapses into `<unknown>`.

The metrics wrapper now reuses the same `detectMcpClientIdentity`
heuristic the heartbeat wrapper already runs per call: env-derived
identity (CODEX_SESSION_ID, CLAUDECODE_SESSION_ID,
COLONY_CLIENT_SESSION_ID) or the stable `mcp-<ppid>` fallback when no
signal is available. Explicit `args.session_id` /
`args.current_session_id` still win — the fallback only fires when both
are absent.

Effect: per-client session rows replace the giant `<unknown>` bucket in
`colony gain` so the next regression investigation can see WHICH agent
is driving the bulk of the load — the gap PR #531's compact-mode fix
left open.

Tests (apps/mcp-server/test/metrics-wrapper.test.ts):
* explicit args.session_id still wins (regression guard)
* sessionless tool + CODEX_SESSION_ID env → codex session bucket
* sessionless tool + no env signal → stable `mcp-<ppid>` bucket
  (two calls collapse into one row, not 2 `<unknown>` rows)

mcp-server suite: 276/276 PASS. Lint clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@NagyVikt NagyVikt merged commit f1659a8 into main May 14, 2026
@NagyVikt NagyVikt deleted the agent/claude/metrics-wrapper-session-attribution-via-2026-05-14-23-51 branch May 14, 2026 21:55
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.

1 participant