Skip to content

feat: [SDK-5813] split or move the session on a user switch (3/3) - #2856

Merged
fadi-george merged 5 commits into
fadi-sdk-5813-splitfrom
fadi-sdk-5813-wiring
Nov 23, 2026
Merged

fadi-george merged 5 commits into
fadi-sdk-5813-splitfrom
fadi-sdk-5813-wiring

Conversation

@fadi-george

@fadi-george fadi-george commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

One Line Summary

On login or logout, move a held session to the new user, or split a logged-in user's session.

Details

Motivation

Part of SDK-5813. Wires #2853 (hold) and #2854 (split) to user switches, so time after a switch goes to the new user and requests never mix one user's onesignal_id with another user's push sub.

Scope

A UserSwitchHandler in SessionService listens for identity model replaces (login, logout), ignoring HYDRATE (a user refresh). For a valid sessions API session:

Case Result
Create still held (anonymous) Session moves to the new user and keeps its start time. Its create goes out right away if that user is identified, including under JWT.
Anonymous, create already sent No split. The session stays on the anonymous user and records the new external ID, so a later switch splits.
Logged-in user switches or logs out Split (#2854). In the background, S1 ends now and S2 starts on the next focus.
  • A switch before the first focus is ignored: the session then is the last run's, and the cold start ends it.
  • A switch whose user the session already follows is skipped. login() and logout() queue their handling, so back-to-back switches (logout then login, or login B then login C) split once.
  • A background split's end is flushed, since backgroundRun won't send it. A focus without consent leaves the split pending until consent is given.
  • New persisted SessionModel.externalId: the external ID of the user the session follows.
  • Removes the push sub appliesTo workaround on the pinned subscription ID translator, which moving and splitting replace.
  • Removes splitSession(); the split is only reachable from a user switch.

Notes:

  • A session persisted before this change has no externalId, so its first switch doesn't split. Fine since new-session-events hasn't shipped.
  • An anonymous session that wasn't moved now gets its push sub ID translated even after a login (the rare 409 case the plan accepts).

Stack

  1. feat: [SDK-5813] hold an anonymous session's create (1/3) #2853: hold the anonymous create
  2. feat: [SDK-5813] split a session at a user switch (2/3) #2854: split
  3. This PR: wiring

Testing

Unit testing

SessionServiceTests: the #2854 split tests now go through a real identity replace. New: login while held moves and sends the create (JWT off and on); anonymous login after the create was sent keeps the session, then login B splits; JWT logout splits and a later login B moves S2; login after the first unfocus moves a local anonymous session while the JWT requirement is unknown; user refresh doesn't split. Back-to-back switches queued before either runs split once; consent after a background split starts S2 without a new app session; the background split's end is flushed. Removed the workaround test.

Manual testing

Ran the full stack (#2856 head) in the demo app on an Android 16 emulator, with the sessions API flag on. All session requests got 202.

  • Anonymous launch, then background: the held create and the checkpoint went out.
  • Login after the anonymous create was sent: no split. A later login as another user split the session.
  • Logout while focused: split, and the next session started anonymous with its create held.
  • Login while the create was held: the session moved to the new user, with the original start time.
  • Background, process killed, cold start: the end went out, and the create wasn't sent again.

Not covered on the emulator: logout in the background and login before the first focus (both need app code to trigger), and a kill mid-write. Unit tests cover these.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

📊 Diff Coverage Report

Diff Coverage Report (Changed Lines Only)

Gate: aggregate coverage on changed executable lines must be ≥ 80% (JaCoCo line data for lines touched in the diff).

Changed Files Coverage

  • SessionModel.kt: 3/3 touched executable lines (100.0%) (13 touched lines in diff)
  • SessionService.kt: 35/35 touched executable lines (100.0%) (57 touched lines in diff)

Overall (aggregate gate)

38/38 touched executable lines covered (100.0% — requires ≥ 80%)

📥 View workflow run

@fadi-george
fadi-george force-pushed the fadi-sdk-5813-wiring branch 2 times, most recently from 6cfe7b3 to 6700cf6 Compare November 23, 2026 21:29

@abdulraqeeb33 abdulraqeeb33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Back-to-back switches still split twice on 884c5d1.

login() and logout() replace the identity on the caller thread, UserSwitchHandler only queues (SessionService.kt:156), and startNextSession reads the live identity store (:409-416). So logout(); login("b") while focused can land both replaces before the first block runs, and you get end S1 | create S2 (b) | end S2 (b, 0 ms) | create S3 (b). login("b"); login("c") does the same, and so does a login that lands after the first focus is queued but before it runs. The hasFocused check doesn't catch that order.

Skip when the session already follows the captured user, right after the takeIf at :159:

if (onesignalId != null && session.onesignalId == onesignalId) return@runOnSerialIO

Every switch mints a new local ID, so a match means an earlier block already started that user's session. Please add a test that queues both runOnSerialIO blocks before running them.

Also on this head:

  1. Background switch, then a focus without consent, then consent. The pending branch (:383) sets startedWithoutConsent (:418), so the consent focus takes the new-session branch (:375), resets startTime, and fires onSessionStarted again. Gate the pending branch on config?.isAwaitingConsent != true.
  2. The background split enqueues S1's end without flush (:446), and backgroundRun returns before its own flush because the split is pending (:276, :314). flush = !counting.
  3. Nothing defends the move's writes (:164-165), and the IV move test switches to non-local IDs, so waitsForLogin is false either way.

Nits:

  1. SessionModel.kt:122 still says updates keep the onesignalId after a login. A held move rewrites it.
  2. The !releaseHeldCreate(session) -> null branch at :438 is dead.
  3. The PR body doesn't mention that switches before the first focus are ignored.

fadi-george and others added 4 commits November 23, 2026 15:11
On login or logout, a held create moves to the new user and goes out
right away if that user is identified. A logged-in user's switch or
logout splits the session. An anonymous user's login after the create
was sent keeps the session. Removes the push subscription workaround
this replaces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Before the first focus the session is the last run's, which the cold start
ends, so a login then must not move or split it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… is unknown

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ent after a background split

- Skip a switch whose user the session already follows. login() and logout()
  queue their handling, and a focus or split reads the live user, so an earlier
  block can already have started the later user's session.
- A focus without consent leaves a background split pending, so consent starts
  the next session instead of a new app session.
- Flush a background split's end, since backgroundRun won't send it.
- Drop the split's held-create check, which can't be reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fadi-george

Copy link
Copy Markdown
Contributor Author

Thanks! Addressed in 26a9bc6.

  • Back-to-back switches: the handler now skips a switch whose onesignalId the session already follows. Every switch mints a new local ID, so a match means an earlier block already started that user's session. Added a test that queues both runOnSerialIO blocks (logout then login) before running them, and checks one end and one create. login("b"); login("c") and a login after a queued focus take the same path.
  1. The pending branch now waits for consent, so the consent focus starts the next session instead of a new app session. Test added.
  2. A background split's end is enqueued with flush = !counting.
  3. 86055bc covers the move's writes: a local anonymous session logs in while held and the create carries the new user's onesignalId and the original start time.

Nits: the onesignalId KDoc now mentions the held move, the dead !releaseHeldCreate branch is gone, and the body notes that switches before the first focus are ignored.

Also in f494428 (1/3), matching iOS: an unknown JWT requirement now holds a local anonymous create mid-session, but not at the session end.

@fadi-george
fadi-george merged commit 30ff344 into new-session-events Nov 23, 2026
6 checks passed
@fadi-george
fadi-george deleted the fadi-sdk-5813-wiring branch November 23, 2026 23:47
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.

3 participants