{{ message }}
fix(langfuse): resolve trace user from runtime context#3794
Open
d33kayyy wants to merge 1 commit into
Open
Conversation
The worker built langfuse_user_id from get_effective_user_id(), which reads the request-scoped _current_user ContextVar. For runs invoked over an internal token on behalf of an end user, that ContextVar is never the end user, so traces recorded langfuse_user_id="default". Switch to resolve_runtime_user_id(runtime), matching the sandbox middleware/tools sites: it reads runtime.context["user_id"] (the owner carried in the run request's context, which survives background-task boundaries) and falls back to get_effective_user_id() for no-auth / browser paths. Caller-supplied metadata still wins via inject_langfuse_metadata's setdefault.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Why
Background / gateway agent runs were recording the wrong user on their Langfuse trace. The trace's
langfuse_user_idis built fromget_effective_user_id(), which reads the request-scoped_current_userContextVar. When a run is invoked over an internal token on behalf of an end user, that ContextVar is never the end user — so every such run's trace was attributed tolangfuse_user_id="default".That makes per-user filtering / cost attribution in Langfuse useless for the entire background-run population: distinct end users all collapse onto one
defaultbucket.What changed
context.user_id— instead of collapsing todefault.langfuse_user_idmetadata still wins (unchanged).This is the trace-attribute wiring in the run worker only — no agent, graph, prompt, or output behavior changes. It brings
worker.pyin line with the sandbox middleware/tools sites, which already resolve the effective user viaresolve_runtime_user_id(runtime)(runtime.context["user_id"]→get_effective_user_id()→default).This change inspired by PR #3729
Surface area
frontend/backend/applanggraph.json, or prompt changedocker/or sandboxed executionskills/backend/pyproject.tomlorfrontend/package.json(say what it buys us)Screenshots / Recording
N/A — backend observability-metadata change only.
Bug fix verification
Bug is encoded as a failing test that goes red before the fix:
backend/tests/test_worker_langfuse_metadata.py::test_run_agent_uses_context_user_id_over_contextvartest_run_agent_falls_back_to_default_user_when_unsetwasrepointed to patch
get_effective_user_idat its definition module(
user_context) — the name the resolver actually calls — and still pins thedefaultfallback.Validation
AI assistance
Tool(s) used: Claude Code
How you used it: Investigated the trace-attribution bug and located the root cause, wrote the failing test first (TDD), then applied the one-line resolver swap and repointed the affected fallback test. I reviewed every line.