Repository navigation
feat(mcp): group sessions by ChatGPT, Codex and VS Code conversation ids - #386
lucasheriques wants to merge 9 commits into
Conversation
Implements the sdk-specs MCP analytics "Session identity" requirement in PostHog::MCP, ported from posthog-js #5577/#5740 and posthog-python #1193. - Reads a client conversation id from tools/call `_meta`, only from the client whose row recognizes the request (User-Agent or client name): ChatGPT `openai/session`, Codex `sessionId`, VS Code `vscode.conversationId`. It outranks a conversation handle. - ChatGPT is handle-free: its listing omits `conversation_id` (still owned and stripped for other clients) and it is minted no handle. - Every event carries `$mcp_session_source` (client_metadata, conversation, transport, generated). - Rule 4: a stateless HTTP request with no session gets a fresh per-request session instead of the server's shared one. - The handshake names the client only off HTTP, so a stateless request is never recognized as another client. - Custom events captured during a call that mints a handle are held and sent with the session and identity the call records. Tested: bundle exec rspec (2612 examples, 0 failures; new client_session_spec failed 13 of 14 before the change), rubocop clean, rake public_api:check passes (adds Property::SESSION_SOURCE).
- Session.resolve keeps its internal sources; EventBuilder maps token and mcp to the wire's transport in one place. - ConversationId.resolve takes mint:, so a handle-free or client-id call never mints a handle it then discards. - Client recognition runs once per request; the client table uses validator lambdas, literal regexes and SchemaMutation.fetch. - Release and hold skip the server mutex when nothing is held. - env_for and parse move to the MCP spec helpers. Tested: bundle exec rspec (2612 examples, 0 failures), rubocop clean, rake public_api:check passes.
… say Tested: bundle exec rspec spec/posthog/mcp (240 examples, 0 failures).
- The client conversation id no longer reaches $mcp_parameters or $identify: the captured request drops the `_meta` key it was read from. - A 2026-07-28 stateless request's per-request gem session is not a transport session; it falls to rule 4 and is labelled generated. - A call with a carried session (token or transport id) is minted no handle, as in posthog-python. - A client id or echoed handle on stdio counts as connection activity, so the 30-minute rollover does not split a live connection. - $identify carries $mcp_session_source; dropping held custom events past the cap warns once instead of logging at debug. Tested: bundle exec rspec (2618 examples, 0 failures; 7 new examples failed first), rubocop clean, rake public_api:check passes.
…e stdio sessions From the second pre-PR gate round: - The gem gives each 2026-07-28 request a throwaway session on stateful transports too, so the era, not the transport's stateless flag, decides that its id is not a transport session. A sessionless modern call is minted a handle again; the stateless flag on RequestScope is gone. - A recognized client's `_meta` key is dropped from the captured request whatever its value, so an invalid id is not sent either. - A client-id or handle call on stdio after 30 idle minutes rolls the connection session instead of extending it. - The changeset names that callbacks lose the stripped `_meta` key. Tested: bundle exec rspec (2621 examples, 0 failures; the new stateful 2026-07-28 and stale-stdio examples failed first), rubocop clean, rake public_api:check passes.
…python does posthog-python #1193 (5308a74) stopped dropping held custom events past 1,000: the rest go out at once with the session the call has so far. Ruby now does the same and warns once. Tested: bundle exec rspec (2621 examples, 0 failures; the bound example failed first), rubocop clean.
Tested: bundle exec rspec (2621 examples, 0 failures).
posthog-ruby-sync Compliance ReportDate: 2026-11-25T17:39:49.383039+00:00 ✅ All Tests Passed!47/47 tests passed Capture Tests✅ 30/30 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
posthog-ruby-async Compliance ReportDate: 2026-11-25T17:39:50.934283+00:00 ✅ All Tests Passed!47/47 tests passed Capture Tests✅ 30/30 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
SDK performance: posthog-rubyBenchmarks completed. No changes flagged for review. Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate. View all results
Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals. Each shard uses its own runner; compare Before PR against With PR within a row, not across shards. Compared 102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in |
|
@PostHog review |
|
@lucasheriques PostHog Review is reviewing this pull request. |
PR overviewNo security issues were found in this pull request. Reviews
|
There was a problem hiding this comment.
PostHog Review · Standard · reviewed bb31d73: 2 Should fix.
New pushes to this pull request won't start an automatic review. Comment @posthog review to review new changes.
See this review and its findings in PostHog Review. Comment @posthog review --deep for a Deep review. A Deep review takes longer and looks deeper than a Standard review.
PostHog Review on bb31d73: a held event copied only its top-level properties, so later changes to nested values reached it, and it kept the full object graph until the call ended. hold now runs the sink's stringify, sanitize and truncate steps at capture, so a held event is a deep snapshot of at most one event's size, and a call holds at most 4 MiB of them besides the 1,000-event cap. Past either bound the rest go out at once with the session the call has so far. Tested: bundle exec rspec (2623 examples, 0 failures; both new examples failed first), rubocop clean, rake public_api:check passes.
|
@PostHog review |
|
@lucasheriques PostHog Review is reviewing this pull request. |
There was a problem hiding this comment.
Not approved — this change needs a human reviewer.
Adding the stamphog label gives the same result unless the changed files change.
stamphog won't auto-review this pull request. The deny-list gate refused it because public_api_snapshot.txt changed (it adds PostHog::MCP::Property::SESSION_SOURCE), and that file is covered by the public_api rule. The tier gate then failed as a result, since a deny-list match on public API means this change is never eligible for automated review. The size gate passed, so splitting the PR won't clear this refusal.
Please ask a human reviewer to look at it. They can judge the public API addition along with the rest of the change.
- posthog[bot] reviewed the current head.
Gate mechanics and policy version
| Gate | Result | |
|---|---|---|
| prerequisites | ✓ | all clear |
| deny-list | ✗ | matches: public_api in public_api_snapshot.txt |
| every_file_attested | ✓ | not a bot PR |
| size | ✓ | 279L, 9F substantive, 649L/14F incl. docs/generated/snapshots — within ceiling |
| tier | ✗ | T2-never because the deny-list matched public_api |
| stamphog 2.9.1 | .stamphog/policy.yml @ unknown · reviewed head 16df26b |
💡 Motivation and Context
MCP Analytics should group one agent conversation into one
$session_id. ChatGPT, Codex and VS Code each name the conversation in a tool call's_meta, and OpenAI saysopenai/sessionis how to group ChatGPT calls.PostHog::MCPignored those ids. Worse, on stateless HTTP it filed every sessionless request under the server's own session, so unrelated clients shared one. This implements the sdk-specs "Session identity" requirement, as posthog-js (PostHog/posthog-js#5577, PostHog/posthog-js#5740) and posthog-python (PostHog/posthog-python#1193) already do.What changed
_meta, and only from the client its row recognizes byUser-Agentor client name. The rows are ChatGPTopenai/session, CodexsessionIdand VS Codevscode.conversationId.$session_idis derived from<key>:<id>.$mcp_parameters, and from the request theidentifyandevent_propertiescallbacks receive.conversation_id, and it is minted no handle.$mcp_session_sourceis on every event:client_metadata,conversation,transportorgenerated.ses_<uuidv7>of its own. That covers every 2026-07-28 request, since the per-request session themcpgem assigns is not a transport session. A call whose transport already carries a session is minted no handle.initializehandshake no longer names the client of a stateless HTTP request, because it may be another client's.analytics.captureinside a tool keeps that call's session and identity. When the call mints a handle, its custom events wait until the call records and then go out with the session it recorded. Past 1,000 held events, the rest go out at once with the session the call has so far.Scope
PostHog::MCP::ClientAPI reads no client ids. It would need_metaand headers parameters, which is a public API change, so that is a separate PR.$identifyevent, which the spec's "Person identity" requirement forbids. That predates this change.Divergence from the spec
None known for the behavior this PR changes. The session resolver ("host") is optional and Ruby has none, so
hostis never sent.💚 How did you test it?
spec/posthog/mcp/client_session_spec.rbruns the spec's scenarios through the real Streamable HTTP transport and stdio, using the spec's$session_idvectors. It also covers the exposure, era, minting, stdio rollover and concurrent custom-event cases. Each example failed before its change.bundle exec rspec: 2621 examples, 0 failures.rubocopandrake public_api:checkpass. The snapshot addsProperty::SESSION_SOURCE.📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Human-driven (agent-assisted)
statelessflag.