Skip to content

Fix stale identity overwrite with timestamp-based protection (TT-031 follow-up) - #890

Merged
unjica merged 5 commits into
developfrom
cursor/fix-identity-overwrite-a93a
Oct 28, 2026
Merged

unjica merged 5 commits into
developfrom
cursor/fix-identity-overwrite-a93a

Conversation

@unjica

@unjica unjica commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Root cause confirmed: In core, init() calls startSession() and builds the session payload right away with the module-level userId, which still holds the previous user. That stale init request reaches the server (potentially FIRST due to network ordering), and the correct identify() request comes after it.

Fixes applied:

  1. Old SDKs handling: Dropped the started_at fallback. Requests without identity_updated_at keep Fix duplicate session race condition (TT-031) #887's behavior (a non-null value overwrites). Use strict-newer comparison (incoming > stored) only when BOTH the stored and incoming values have a client timestamp. SDK 1.3.1 stays affected until apps upgrade to 1.3.2 / core 1.5.3. This is documented in CHANGELOG.md.

  2. Migration: No backfill. identity_updated_at remains NULL for existing sessions (will use Fix duplicate session race condition (TT-031) #887 behavior until re-identified).

  3. Timestamp clamping: Client timestamp is clamped on the server to LEAST(client_ts, now()), and no earlier than started_at (i.e., GREATEST(started_at, LEAST(client_ts, now()))). This prevents malicious or skewed client clocks from setting infinite future timestamps.

  4. Core init() fix: Preserve identity only if it was set via identify() before ANY session started (supports identify-before-init pattern). Clear stale identity from previous sessions when re-init is called. This prevents stale user carryover while keeping apps that call identify() before init() working correctly.

  5. Regression Tests:

    • Replay QA's exact sequence: stale init request first (without timestamp), then identify (with timestamp)
    • Test old-SDK identify mid-session (without timestamp) using Fix duplicate session race condition (TT-031) #887 behavior
    • Test future timestamp clamping
    • Test identify-before-init preservation (SDK test)
    • Test re-init with same user (SDK test) - ensures stale identity is cleared
  6. Version bumps (DO NOT publish to npm; Sanja publishes):

    • @telemetry-tracker/core → 1.5.3
    • @telemetry-tracker/react-native → 1.3.2

All tests pass. All CI checks green ✅

Refs #886

Open in Web Open in Cursor 

**Server:** Add identity_updated_at column to Session table. Only update user_id/user_email when incoming timestamp is newer than stored value. Protects against delayed SDK requests overwriting fresher identity data.

**SDK:** Track identity timestamps in telemetry-core. When identify() is called, capture current timestamp and send as identity_updated_at. Old SDKs without this field fall back to started_at as the identity timestamp.

**Tests:** Added regression tests for:
- Stale request arriving after identify() (should be rejected)
- Newer identity overwriting older identity (should succeed)
- Backward compat: old SDK without identity_updated_at (uses started_at)

**Root cause:** RN SDK 1.3.1 sends delayed session requests ~1s after identify(), carrying previous user's ID. With new non-null-wins COALESCE logic from #887, stale requests overwrote correct user. Solution: timestamp-based ordering prevents out-of-order updates.

**Versions:**
- @telemetry-tracker/core: 1.5.2 → 1.5.3
- @telemetry-tracker/react-native: 1.3.1 → 1.3.2

Refs #886
1. Drop started_at fallback: Only use strict-newer comparison when BOTH stored and incoming have timestamps. Requests without identity_updated_at use #887 behavior (new non-null wins). Old SDKs (1.3.1) remain affected until apps upgrade.

2. Migration: Remove backfill, leave identity_updated_at NULL for existing sessions.

3. Timestamp clamping: Clamp client timestamp to LEAST(client_ts, now()) and bound by started_at to prevent future timestamps and ensure logical ordering.

4. Core init() fix: Preserve identity only if set via identify-before-init (no active session). Clear stale identity from previous sessions. Supports identify-before-init pattern while preventing stale user carryover from re-init scenarios.

5. Tests updated:
   - QA sequence: stale init() first, then identify()
   - Old-SDK identify mid-session (uses #887 behavior)
   - Future timestamp clamping
   - SDK: identify-before-init preservation
   - SDK: re-init clears stale identity

6. CHANGELOG updated to reflect actual behavior and note that old SDKs stay affected.

Root cause confirmed: init() calls startSession() which builds payload with module-level userId before clearing it. With new strict-newer logic and timestamp clamping, this is now properly handled.

Refs #886
Use timestamps 1 minute in the past for QA sequence test to prevent
clamping to server time when test runs close to 'now'.
@unjica
unjica merged commit 312f2d4 into develop Oct 28, 2026
7 checks passed
@unjica unjica mentioned this pull request Oct 28, 2026
2 of 6 tasks
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