Skip to content

feat(web): rename PostHog chat events, add tool_used tracking, and client-side ask events#1111

Merged
msukkari merged 14 commits intomainfrom
michael/posthog-event-renames-SOU-894
Apr 14, 2026
Merged

feat(web): rename PostHog chat events, add tool_used tracking, and client-side ask events#1111
msukkari merged 14 commits intomainfrom
michael/posthog-event-renames-SOU-894

Conversation

@msukkari
Copy link
Copy Markdown
Contributor

@msukkari msukkari commented Apr 13, 2026

Summary

Renames server-side chat events, adds unified tool usage tracking, sets the source header on the web client chat transport, and cleans up redundant events.

Code changes

Events renamed (dropped wa_ prefix since they fire from multiple sources):

  • wa_chat_thread_created -> ask_thread_created (now includes source property)
  • wa_chat_message_sent -> ask_message_sent (now includes source property)
  • wa_chat_tool_used -> tool_used (moved from agent.ts onStepFinish to the shared tool adapter layer)

Events removed:

  • api_code_search_request -- redundant with api_request which fires for every API call and carries path + source

New behavior:

  • tool_used fires from the shared adapter layer (adapters.ts) for every tool call, covering both ask agent (source: 'sourcebot-ask-agent') and MCP server (source: 'sourcebot-mcp-server') paths
  • tool_used also fires for manually registered MCP tools (list_language_models, ask_codebase) in server.ts
  • Web client DefaultChatTransport now sets X-Sourcebot-Client-Source: sourcebot-web-client header, so ask_message_sent fires with source: 'sourcebot-web-client' for web UI calls
  • Added PostHog event naming convention to CLAUDE.md

Final event inventory (for search/ask/tool workflows)

Event Type Fires from Purpose
search_finished client-side searchResultsPage.tsx Web-only search performance stats
api_request server-side apiHandler.ts (every API route) Generic API traffic log with path, method, source
ask_thread_created server-side actions.ts, askCodebase.ts Chat thread creation with chatId, isAnonymous, source
ask_message_sent server-side chat/route.ts, askCodebase.ts Chat message with chatId, messageCount, selectedReposCount, source
tool_used server-side adapters.ts, server.ts Individual tool call with toolName, source, success

PostHog dashboard configuration

Actions (shared across all 13 company dashboards):

Any Feature Usage (ID: 258475) -- used for the "All" DAU/WAU line:

  1. search_finished (web search)
  2. wa_chat_message_sent (legacy ask, backwards compat)
  3. ask_message_sent where source = 'sourcebot-web-client' (new ask)
  4. api_request where source = 'mcp' (external MCP npm package HTTP calls)
  5. tool_used where source = 'sourcebot-mcp-server' (in-process MCP tool calls)

Ask Usage (ID: 263321) -- used for the "Ask" DAU/WAU line:

  1. wa_chat_message_sent (legacy, backwards compat)
  2. ask_message_sent where source = 'sourcebot-web-client' (new)

MCP Usage (ID: 263322) -- used for the "MCP" DAU/WAU line:

  1. api_request where source = 'mcp' (external npm package HTTP calls)
  2. tool_used where source = 'sourcebot-mcp-server' (in-process MCP tool calls)

Insights:

Feature DAU (ID: 7539703) -- 4 series: All (Any Feature Usage action), Search (search_finished), Ask (Ask Usage action), MCP (MCP Usage action). Daily interval, -90d range. On all 13 dashboards.

Feature WAU (ID: 7539704) -- Same as DAU but weekly interval.

Tool Usage (ID: 7919953) -- tool_used total count, breakdown by toolName. Daily interval, -90d range. On all 13 dashboards. New.

Test plan

  • Verify ask_message_sent fires with source: 'sourcebot-web-client' when sending a message from the web chat UI
  • Verify ask_message_sent fires with source: 'mcp' when using the in-process MCP ask_codebase tool
  • Verify ask_thread_created fires for both web UI and MCP/API thread creation
  • Verify tool_used events fire with correct toolName and source for both ask agent and MCP tool calls
  • Verify Feature DAU/WAU dashboards show historical data correctly (no gaps at the transition)
  • Grep codebase for wa_chat_thread_created, wa_chat_message_sent, wa_chat_tool_used, api_code_search_request -- should only appear in CHANGELOG.md

…d client-side ask events

Renames server-side chat events to drop the wa_ prefix (since they fire from
multiple sources), adds a new tool_used event for unified tool call tracking
across the ask agent and MCP server, and introduces client-side wa_ask_* events
for accurate web-only usage dashboards.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions

This comment has been minimized.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 13, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

PostHog analytics events were renamed from wa_chat_* to ask_* or consolidated into tool_used; event payloads now include an optional source where applicable; MCP and tool adapters emit unified tool_used events; search endpoints no longer emit api_code_search_request; web client sets X-Sourcebot-Client-Source on chat requests.

Changes

Cohort / File(s) Summary
Documentation
CHANGELOG.md, CLAUDE.md
Document new event names (ask_thread_created, ask_message_sent, tool_used), add PostHog naming guideline forbidding wa_ for multi-source events and requiring source for multi-source events.
Event Type Definitions
packages/web/src/lib/posthogEvents.ts
Rename wa_chat_* events to ask_* and tool_used; add optional source?: string to ask events; change tool_used payload to { toolName, source, success }; remove api_code_search_request.
Chat Emission & Client Headers
packages/web/src/features/chat/actions.ts, packages/web/src/app/api/(server)/chat/route.ts, packages/web/src/features/chat/components/chatThread/chatThread.tsx, packages/web/src/features/mcp/askCodebase.ts
Emit ask_thread_created / ask_message_sent with optional source; server route extracts X-Sourcebot-Client-Source for source; DefaultChatTransport adds X-Sourcebot-Client-Source: sourcebot-web-client.
Tool Usage Telemetry
packages/web/src/features/tools/adapters.ts, packages/web/src/features/mcp/server.ts, packages/web/src/features/chat/agent.ts
Remove legacy wa_chat_tool_used from agent; add unified tool_used emissions in MCP endpoints and tool adapters with success/failure capture (try/catch), and remove agent-side tool telemetry emission.
Search Routes Cleanup
packages/web/src/app/api/(server)/search/route.ts, packages/web/src/app/api/(server)/stream_search/route.ts
Remove api_code_search_request capture and X-Sourcebot-Client-Source header extraction from blocking and streaming search endpoints.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant WebClient
participant WebServer
participant MCP_Tool as MCP/Tool
participant PostHog
WebClient->>WebServer: POST /api/chat (X-Sourcebot-Client-Source)
WebServer->>MCP_Tool: invoke ask / tool
MCP_Tool-->>WebServer: result or error
alt success or failure
MCP_Tool->>PostHog: captureEvent("tool_used", {toolName, source, success})
end
WebServer->>PostHog: captureEvent("ask_thread_created"/"ask_message_sent", {chatId,...,source})
WebServer-->>WebClient: response

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • brendan-kellam
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The PR title comprehensively and accurately describes the three main changes: renaming PostHog chat events, adding unified tool_used event tracking, and supporting client-side ask events.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch michael/posthog-event-renames-SOU-894

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Add isAnonymous to wa_ask_thread_created and messageCount to
wa_ask_message_sent so they carry the same data as the server-side
events.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Nitpick comments (1)
packages/web/src/features/mcp/server.ts (1)

60-67: Capture failure telemetry for list_language_models too.

Right now only success is tracked; thrown failures won’t be counted in tool_used.

Suggested refactor
         async () => {
-            const models = await getConfiguredLanguageModelsInfo();
-            captureEvent('tool_used', {
-                toolName: 'list_language_models',
-                source: 'sourcebot-mcp-server',
-                success: true,
-            });
-            return { content: [{ type: "text", text: JSON.stringify(models) }] };
+            try {
+                const models = await getConfiguredLanguageModelsInfo();
+                captureEvent('tool_used', {
+                    toolName: 'list_language_models',
+                    source: 'sourcebot-mcp-server',
+                    success: true,
+                });
+                return { content: [{ type: "text", text: JSON.stringify(models) }] };
+            } catch (error) {
+                captureEvent('tool_used', {
+                    toolName: 'list_language_models',
+                    source: 'sourcebot-mcp-server',
+                    success: false,
+                });
+                throw error;
+            }
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/web/src/features/mcp/server.ts` around lines 60 - 67, Wrap the call
to getConfiguredLanguageModelsInfo inside a try/catch in the handler that
currently calls captureEvent('tool_used', { toolName: 'list_language_models',
... }) so that failures are also reported; on success keep the existing
captureEvent with success: true, and in the catch call captureEvent('tool_used',
{ toolName: 'list_language_models', source: 'sourcebot-mcp-server', success:
false, error: <error message/stack> }) before rethrowing or returning an error
response—locate this behavior around the getConfiguredLanguageModelsInfo
invocation and the captureEvent calls to implement the telemetry on failure.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/web/src/app/api/`(server)/chat/route.ts:
- Around line 94-102: The code emits ask_message_sent even when
X-Sourcebot-Client-Source header is missing; update the block around
captureEvent to only call captureEvent('ask_message_sent', ...) when source is
defined (or set an explicit allowed default source per guidelines), ensuring the
event payload always includes the source property; reference the
variables/identifiers captureEvent, source, id, messages, expandedRepos, and
env.EXPERIMENT_ASK_GH_ENABLED so you add a guard (if (source) { ... }) or
provide a permitted default before invoking captureEvent.

In `@packages/web/src/features/chat/actions.ts`:
- Around line 52-56: The event capture for 'ask_thread_created' is sending
source optionally and may be emitted without origin metadata; update the call to
captureEvent('ask_thread_created', ...) in actions.ts to always include the
source property (use the existing source variable or a default like 'web' when
undefined) so the payload includes chatId (chat.id), isAnonymous (isGuestUser)
and source consistently; ensure captureEvent and the 'ask_thread_created'
invocation pass the non-optional source value.

In `@packages/web/src/features/tools/adapters.ts`:
- Around line 23-27: The telemetry calls to captureEvent (used in the tools
adapter around the handler where def.name and context are passed, and the other
two occurrences) are unawaited and can produce unhandled promise rejections;
update each call to await captureEvent(...) inside a try/catch (or attach
.catch) so failures are swallowed and logged without affecting tool
execution—specifically modify the captureEvent invocations referenced by
def.name, and the other two captureEvent calls in this file to be awaited with
error handling that logs the error via the existing logger instead of leaving
the promise unhandled.

In `@packages/web/src/lib/posthogEvents.ts`:
- Around line 169-179: The schema for ask_thread_created and ask_message_sent
currently marks source as optional; change both event type definitions (symbols:
ask_thread_created, ask_message_sent in posthogEvents.ts) to require source
(remove the optional ?), then update any callers that emit these events to pass
a source value so TypeScript stays satisfied; ensure related comments/validation
reflect that multi-source events must include source and avoid using the wa_
prefix if fired outside the web app.

---

Nitpick comments:
In `@packages/web/src/features/mcp/server.ts`:
- Around line 60-67: Wrap the call to getConfiguredLanguageModelsInfo inside a
try/catch in the handler that currently calls captureEvent('tool_used', {
toolName: 'list_language_models', ... }) so that failures are also reported; on
success keep the existing captureEvent with success: true, and in the catch call
captureEvent('tool_used', { toolName: 'list_language_models', source:
'sourcebot-mcp-server', success: false, error: <error message/stack> }) before
rethrowing or returning an error response—locate this behavior around the
getConfiguredLanguageModelsInfo invocation and the captureEvent calls to
implement the telemetry on failure.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 11a0b7d9-653b-4152-82c4-951b00e9adbd

📥 Commits

Reviewing files that changed from the base of the PR and between ae4513e and acae640.

📒 Files selected for processing (11)
  • CHANGELOG.md
  • CLAUDE.md
  • packages/web/src/app/api/(server)/chat/route.ts
  • packages/web/src/features/chat/actions.ts
  • packages/web/src/features/chat/agent.ts
  • packages/web/src/features/chat/components/chatThread/chatThread.tsx
  • packages/web/src/features/chat/useCreateNewChatThread.ts
  • packages/web/src/features/mcp/askCodebase.ts
  • packages/web/src/features/mcp/server.ts
  • packages/web/src/features/tools/adapters.ts
  • packages/web/src/lib/posthogEvents.ts

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/web/src/features/chat/components/chatThread/chatThread.tsx (1)

129-172: ⚠️ Potential issue | 🟡 Minor

Missing captureEvent in dependency array.

The captureEvent function is used inside sendMessage but is not included in its dependency array (lines 164-172). This is inconsistent with the onSubmit callback which correctly includes captureEvent in its dependencies (line 331). While captureEvent is likely a stable reference, adding it ensures correctness and silences potential lint warnings.

Proposed fix
     }, [
         selectedLanguageModel,
         _sendMessage,
         selectedSearchScopes,
         messages.length,
         toast,
         chatId,
         router,
+        captureEvent,
     ]);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/web/src/features/chat/components/chatThread/chatThread.tsx` around
lines 129 - 172, The callback that sends messages (the useCallback containing
captureEvent, _sendMessage and generateAndUpdateChatNameFromMessage) uses
captureEvent but doesn't list it in its dependency array; update that dependency
array to include captureEvent so React hooks are correct and lint warnings are
silenced—add captureEvent alongside selectedLanguageModel, _sendMessage,
selectedSearchScopes, messages.length, toast, chatId, and router.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@packages/web/src/features/chat/components/chatThread/chatThread.tsx`:
- Around line 129-172: The callback that sends messages (the useCallback
containing captureEvent, _sendMessage and generateAndUpdateChatNameFromMessage)
uses captureEvent but doesn't list it in its dependency array; update that
dependency array to include captureEvent so React hooks are correct and lint
warnings are silenced—add captureEvent alongside selectedLanguageModel,
_sendMessage, selectedSearchScopes, messages.length, toast, chatId, and router.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 329f0f7c-0975-4892-9d07-7e5dc4481b68

📥 Commits

Reviewing files that changed from the base of the PR and between acae640 and df039d0.

📒 Files selected for processing (3)
  • packages/web/src/features/chat/components/chatThread/chatThread.tsx
  • packages/web/src/features/chat/useCreateNewChatThread.ts
  • packages/web/src/lib/posthogEvents.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/web/src/features/chat/useCreateNewChatThread.ts
  • packages/web/src/lib/posthogEvents.ts

msukkari and others added 2 commits April 13, 2026 17:16
This event is fully covered by api_request which fires for every API
call and carries path + source. Updated the Any Feature Usage PostHog
action to remove the api_code_search_request step.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Redundant with ask_thread_created which fires server-side for the same
thread creation (including from the web UI via createChat server action).
Dashboards track ask usage via wa_ask_message_sent, not thread creation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
packages/web/src/lib/posthogEvents.ts (1)

171-180: ⚠️ Potential issue | 🟠 Major

Make source required on the shared ask events.

ask_thread_created and ask_message_sent are the multi-source variants, so keeping source optional here lets emitters silently skip the origin and breaks the source breakdown this PR is introducing.

Suggested schema fix
     ask_thread_created: {
         chatId: string,
         isAnonymous: boolean,
-        source?: string,
+        source: string,
     },
     ask_message_sent: {
         chatId: string,
         messageCount: number,
         selectedReposCount: number,
-        source?: string,
+        source: string,
         /**

Based on learnings, "PostHog events using the wa_ prefix can ONLY be fired from the web app; events fired from multiple sources must NOT use the wa_ prefix" and "Multi-source PostHog events should include a source property to identify the origin (e.g., 'sourcebot-web-client', 'sourcebot-mcp-server', 'sourcebot-ask-agent')."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/web/src/lib/posthogEvents.ts` around lines 171 - 180, The event
schemas ask_thread_created and ask_message_sent currently define source as
optional, which allows callers to omit it and breaks multi-source analytics;
update the type definitions for ask_thread_created and ask_message_sent to make
source a required string (remove the optional marker) so every emitter must
supply a source value (e.g., 'sourcebot-web-client' etc.), and run a quick grep
for other "ask_" multi-source events to ensure they also require source if
shared across origins.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@packages/web/src/lib/posthogEvents.ts`:
- Around line 171-180: The event schemas ask_thread_created and ask_message_sent
currently define source as optional, which allows callers to omit it and breaks
multi-source analytics; update the type definitions for ask_thread_created and
ask_message_sent to make source a required string (remove the optional marker)
so every emitter must supply a source value (e.g., 'sourcebot-web-client' etc.),
and run a quick grep for other "ask_" multi-source events to ensure they also
require source if shared across origins.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bbeb595a-dab3-4f1b-92ee-b77b2feb8bce

📥 Commits

Reviewing files that changed from the base of the PR and between df039d0 and 277ed60.

📒 Files selected for processing (3)
  • packages/web/src/app/api/(server)/search/route.ts
  • packages/web/src/app/api/(server)/stream_search/route.ts
  • packages/web/src/lib/posthogEvents.ts
💤 Files with no reviewable changes (2)
  • packages/web/src/app/api/(server)/search/route.ts
  • packages/web/src/app/api/(server)/stream_search/route.ts

…transport

Instead of a separate client-side event, the web client now sets the
X-Sourcebot-Client-Source header on DefaultChatTransport. This makes
ask_message_sent fire with source='sourcebot-web-client' for web UI
calls, enabling web-only filtering directly on the server-side event.

Updated Ask Usage and Any Feature Usage PostHog actions to filter
ask_message_sent by source=sourcebot-web-client.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
packages/web/src/lib/posthogEvents.ts (1)

162-172: ⚠️ Potential issue | 🟠 Major

Require source for ask_* multi-source events.

At Line 165 and Line 171, source is optional. For multi-source events this should be required to keep telemetry filters and dashboards consistent.

Suggested fix
     ask_thread_created: {
         chatId: string,
         isAnonymous: boolean,
-        source?: string,
+        source: string,
     },
     ask_message_sent: {
         chatId: string,
         messageCount: number,
         selectedReposCount: number,
-        source?: string,
+        source: string,
         /**

As per coding guidelines, "Events fired from multiple sources (web app, MCP server, API) must NOT use the wa_ prefix. Multi-source events should include a source property".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/web/src/lib/posthogEvents.ts` around lines 162 - 172, The types for
the multi-source events ask_thread_created and ask_message_sent currently mark
source as optional; change their definitions so source is required (remove the
`?` on `source`) in the ask_thread_created and ask_message_sent event shapes,
update any call sites that construct these events to supply a `source` value,
and run TS to fix any resulting type errors (look for usages of
ask_thread_created and ask_message_sent to add the appropriate source strings).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@packages/web/src/lib/posthogEvents.ts`:
- Around line 162-172: The types for the multi-source events ask_thread_created
and ask_message_sent currently mark source as optional; change their definitions
so source is required (remove the `?` on `source`) in the ask_thread_created and
ask_message_sent event shapes, update any call sites that construct these events
to supply a `source` value, and run TS to fix any resulting type errors (look
for usages of ask_thread_created and ask_message_sent to add the appropriate
source strings).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 17069cf1-6a5c-47dc-8842-d6cba7873642

📥 Commits

Reviewing files that changed from the base of the PR and between 818f237 and 49a1b76.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • packages/web/src/features/chat/components/chatThread/chatThread.tsx
  • packages/web/src/lib/posthogEvents.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • CHANGELOG.md
  • packages/web/src/features/chat/components/chatThread/chatThread.tsx

msukkari and others added 2 commits April 13, 2026 17:38
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…nt calls

Add .catch(() => {}) to all fire-and-forget captureEvent('tool_used')
calls in adapters.ts and server.ts, matching the existing pattern in
apiHandler.ts. Prevents unhandled promise rejections if telemetry fails.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@msukkari msukkari force-pushed the michael/posthog-event-renames-SOU-894 branch from 4817d7e to 39aaac0 Compare April 14, 2026 00:41
…l telemetry

captureEvent() relies on tryGetPostHogDistinctId() which reads cookies,
session, or API key headers. In the MCP server and ask agent contexts,
none of these are available, so every tool_used event got a random
distinct_id — inflating DAU counts (e.g., 8 tool calls = 8 "users").

Fix: add optional distinctId to ToolContext and captureEvent(). Thread
user.id from the auth context through:
- MCP route → createMcpServer(userId) → ToolContext.distinctId
- chat route / askCodebase → createMessageStream(distinctId) →
  createAgentStream → createTools

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
msukkari and others added 3 commits April 13, 2026 18:02
The field carries a user ID, not a PostHog-specific concept. The
captureEvent options parameter remains distinctId since that's the
PostHog term, but the application-level interfaces use userId.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ed event

Match the same pattern used in toVercelAITool — single captureEvent call
in a finally block with a success boolean, instead of duplicated calls
in try and catch.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
captureEvent now wraps its body in try/catch so callers are guaranteed
it won't throw. Removed all .catch() handlers from call sites in
adapters.ts, server.ts, and apiHandler.ts.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
msukkari and others added 2 commits April 13, 2026 18:09
tryGetPostHogDistinctId() was missing OAuth Bearer token resolution,
causing all MCP tool calls to get random distinct_ids (inflated DAU).

Fix: replace the manual cookie/session/API key checks with a call to
getAuthenticatedUser(), which already handles all auth methods (session,
OAuth, API keys). This removes the need to thread userId through
ToolContext, agent options, and all callers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@msukkari msukkari merged commit 5305019 into main Apr 14, 2026
13 checks 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