Skip to content

fix(attribution): resolve user_id at run/session creation in local mode#11

Merged
iaj6 merged 1 commit into
mainfrom
fix/null-user-attribution
Jun 8, 2026
Merged

fix(attribution): resolve user_id at run/session creation in local mode#11
iaj6 merged 1 commit into
mainfrom
fix/null-user-attribution

Conversation

@iaj6

@iaj6 iaj6 commented Jun 8, 2026

Copy link
Copy Markdown
Owner

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 = NULL and relied on the cleanup --reassign-null-user backfill. (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, and run start.

Resolver precedence:

  1. credentials.json present → local user matched by emailnot 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.
  2. No creds + exactly one local user → that user (sole-user fallback, per owner's call 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 agent 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-over-fallback, case-insensitivity, fail-open) + a DirectOps end-to-end test proving run/session attribution and convergence with countRunsWithoutUser. 1232 pass, build green.

Review

Adversarial 2-lens (completeness / correctness-safety): 0 real problems. Confirmed no other local create path leaves userId NULL (orchestrator/dispatcher never insert runs; SdkOps untouched; sessions round-trip userId), 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

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]>
@iaj6 iaj6 merged commit 42bd552 into main Jun 8, 2026
3 checks passed
@iaj6 iaj6 deleted the fix/null-user-attribution branch June 8, 2026 20:36
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