Skip to content

feat(posthog): correlate task events with copilot logs via request_id#4453

Merged
waleedlatif1 merged 3 commits intostagingfrom
waleedlatif1/posthog-task-request-id
May 5, 2026
Merged

feat(posthog): correlate task events with copilot logs via request_id#4453
waleedlatif1 merged 3 commits intostagingfrom
waleedlatif1/posthog-task-request-id

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

@waleedlatif1 waleedlatif1 commented May 5, 2026

Summary

  • Auto-injects server request_id from AsyncLocalStorage context into all PostHog server events (captureServerEvent)
  • Adds task_request_started client event fired when SSE traceparent response header arrives, carrying the W3C trace ID used by Go for log correlation (one-to-one with task_message_sent)
  • Includes in-flight request_id in task_generation_aborted when available

Test plan

  • Send a task message → verify task_message_sent and task_request_started both fire with matching workspace_id and a 32-hex request_id
  • Abort an in-flight generation → verify task_generation_aborted carries the same request_id
  • Cross-check the trace ID against Go copilot logs to confirm correlation

Auto-injects server request_id into all PostHog server events from
AsyncLocalStorage context. Adds task_request_started client event fired
when SSE traceparent response header arrives, carrying the trace ID used
by Go for log correlation. task_generation_aborted now includes the
in-flight request_id when available.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel Bot commented May 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped May 5, 2026 8:16pm

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 5, 2026

PR Summary

Medium Risk
Adds cross-cutting analytics correlation by deriving/injecting request_id from both SSE traceparent headers and server request context, which could subtly affect event payloads across the app if parsing or context propagation is wrong.

Overview
Adds request_id correlation across client and server PostHog events to tie task actions to backend logs.

On the client, useChat now extracts a W3C trace ID from the initial chat response traceparent header, exposes it via getCurrentRequestId(), and fires a new task_request_started event; both Home (mothership) and Workflow Copilot views include this ID when emitting task_generation_aborted.

On the server, captureServerEvent auto-injects a request_id from AsyncLocalStorage request context when the caller didn’t already provide one, and the PostHog event map is updated to type the new event and optional request_id fields.

Reviewed by Cursor Bugbot for commit 1ed9f52. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 5, 2026

Greptile Summary

This PR wires PostHog event correlation for Sim's AI task features by extracting the W3C trace ID from the traceparent response header and emitting it as task_request_started, and by injecting the server-side AsyncLocalStorage request_id automatically into all captureServerEvent calls.

  • Adds task_request_started client event (mothership + copilot views) fired when the traceparent response header arrives, capturing the 32-hex W3C trace ID and user_message_id for cross-event join.
  • Exposes getCurrentRequestId() from useChat, deriving the trace ID from streamTraceparentRef so task_generation_aborted can carry the same ID as the paired task_request_started.
  • Extends captureServerEvent to auto-inject request_id from AsyncLocalStorage context with caller-supplied values taking precedence.

Confidence Score: 5/5

Safe to merge — all changes are additive telemetry instrumentation with no impact on existing chat or streaming logic.

The trace ID extraction uses a consistent path in both onRequestStarted and getCurrentRequestId (same streamTraceparentRefsplit('-')[1] → 32-hex regex). The server-side injection correctly guards against overriding caller-supplied request_id. The onRequestStarted callback is wrapped in try/catch. The abort handler reads the ref before invoking stopGeneration(), so clearActiveTurn() cannot race and clear it first. No streaming, auth, or data-path logic is altered.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts Adds onRequestStarted callback option and getCurrentRequestId() return value; both consistently derive the trace ID from streamTraceparentRef via the same split/regex path; callback is guarded with try/catch to prevent stream errors.
apps/sim/lib/posthog/server.ts Injects request_id from AsyncLocalStorage into all server-side capture calls; spread order correctly gives caller-supplied request_id precedence via 'request_id' in props guard.
apps/sim/lib/posthog/events.ts Adds task_request_started event type and makes request_id optional on task_generation_aborted; types are consistent with usage at call sites.
apps/sim/app/workspace/[workspaceId]/home/home.tsx Wires onRequestStarted callback and getCurrentRequestId for the mothership view; abort handler calls getCurrentRequestId() before stopGeneration(), correctly reading the live ref value.
apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/panel.tsx Copilot abort handler correctly added getCopilotCurrentRequestId to the useCallback dependency array; mirrors the mothership pattern cleanly.

Sequence Diagram

sequenceDiagram
    participant Client
    participant useChat
    participant Server
    participant PostHog

    Client->>Server: POST /api/mothership/chat (userMessageId)
    Server-->>Client: HTTP 200, traceparent: 00-{traceId}-{parentId}-01

    useChat->>useChat: store traceparent in streamTraceparentRef
    useChat->>useChat: extract traceId = traceparent.split('-')[1]
    useChat->>Client: onRequestStarted({ requestId: traceId, userMessageId })
    Client->>PostHog: captureEvent('task_request_started', { request_id: traceId, user_message_id })

    Note over Server,PostHog: Server-side events auto-inject request_id from AsyncLocalStorage
    Server->>PostHog: captureServerEvent(any_event) → auto-injects request_id if absent

    alt User aborts generation
        Client->>useChat: stopGeneration()
        useChat->>useChat: getCurrentRequestId() reads streamTraceparentRef
        Client->>PostHog: captureEvent('task_generation_aborted', { request_id: traceId })
        useChat->>Server: abort stream
        useChat->>useChat: clearActiveTurn() → streamTraceparentRef = undefined
    end
Loading

Reviews (2): Last reviewed commit: "fix(posthog): use traceparent header as ..." | Re-trigger Greptile

Comment thread apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts Outdated
Comment thread apps/sim/lib/posthog/server.ts Outdated
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

@waleedlatif1 waleedlatif1 changed the title feat(posthog): correlate task events with Go logs via request_id feat(posthog): correlate task events with copilot logs via request_id May 5, 2026
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1ed9f52. Configure here.

@waleedlatif1 waleedlatif1 merged commit a9e9ecf into staging May 5, 2026
14 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/posthog-task-request-id branch May 5, 2026 21:11
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