fix(attribution): resolve user_id at run/session creation in local mode#11
Merged
Conversation
Second of the two attribution root-cause fixes — moves user attribution from
cleanup-after to correct-at-write. Local (DirectOps/CLI) run + session creation
had no auth context, so rows landed with user_id = NULL and needed the
`cleanup --reassign-null-user` backfill. (The SDK/remote path already attributes
via bearer auth.)
Adds resolveLocalUserId(db) (cli/attribution.ts) and stamps userId on the run
AND session at every local write path: DirectOps.startSessionAndRun (hook-ops),
wrap, and run start. Resolver precedence:
1. credentials.json present → local user matched by EMAIL (not creds.user.id,
which is the remote dashboard's UUID and may not exist locally) — converges
on the same row the reassign-null-user backfill targets.
2. No creds + exactly one local user → that user (sole-user fallback, chosen
for the single-operator trial).
3. Otherwise null (anonymous local dev / ambiguous multi-user DB) — preserves
prior behavior.
Fails open: any error returns null, so a hook never throws or blocks a real run.
The --reassign-null-user backfill is kept for historical rows.
Tests: resolveLocalUserId precedence unit suite (creds-by-email,
sole-user fallback, ambiguity, creds-precedence, case-insensitivity,
fail-open) + a DirectOps end-to-end test proving run/session attribution and
convergence with countRunsWithoutUser. Full suite 1232 pass, build green.
Review: adversarial 2-lens (completeness/safety) — 0 real problems; confirmed
no other local create path leaves userId NULL (orchestrator/dispatch never
insert runs) and the resolver can't propagate an exception into a hook.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
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.
What
Second of the two attribution root-cause fixes — moves user attribution from cleanup-after to correct-at-write.
Local (DirectOps / CLI) run + session creation had no auth context, so rows landed with
user_id = NULLand relied on thecleanup --reassign-null-userbackfill. (The SDK/remote path already attributes via bearer auth.)Change
New
resolveLocalUserId(db)(cli/attribution.ts), stamped on the run and session at every local write path:DirectOps.startSessionAndRun(hook-ops),wrap, andrun start.Resolver precedence:
creds.user.id, which is the remote dashboard's UUID and may not exist in local SQLite. Converges on the same row the backfill targets.Fails open: any error returns
null, so a hook never throws or blocks a real agent run.The
--reassign-null-userbackfill is kept for historical rows.Tests
resolveLocalUserIdprecedence unit suite (creds-by-email, sole-user fallback, ambiguity, creds-precedence-over-fallback, case-insensitivity, fail-open) + a DirectOps end-to-end test proving run/session attribution and convergence withcountRunsWithoutUser. 1232 pass, build green.Review
Adversarial 2-lens (completeness / correctness-safety): 0 real problems. Confirmed no other local create path leaves
userIdNULL (orchestrator/dispatcher never insert runs; SdkOps untouched; sessions round-tripuserId), and the resolver provably can't propagate an exception into a hook.Decision
Sole-user fallback chosen (vs strict creds-only). Tradeoff noted: on a shared single-user local DB with no creds, the lone user is auto-attributed — acceptable for the single-tenant trial.
Part 2 of 2 (attribution root causes). Independent of #10 (PR B, repo normalization) — they touch different regions; merge in any order.
🤖 Generated with Claude Code