Skip to content

feat: [SDK-5813] split a session at a user switch (2/3) - #2854

Merged
fadi-george merged 6 commits into
fadi-sdk-5813from
fadi-sdk-5813-split
Nov 23, 2026
Merged

fadi-george merged 6 commits into
fadi-sdk-5813from
fadi-sdk-5813-split

Conversation

@fadi-george

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

Copy link
Copy Markdown
Contributor

Description

One Line Summary

Add SessionService.splitSession(): end the current sessions API session at a user switch and start the next one for the new user.

Details

Motivation

Part of SDK-5813. When a logged-in user logs in as someone else or logs out, the session should split so time after the switch goes to the new user. This PR adds the split; 3/3 wires it to user switches.

Scope

  • Focused: sends S1's end with the duration so far and end_time = switch time, then starts S2 now: new local session ID, IDs pinned to the current user, S2's start time = switch time. S2's create goes out now, or is held if the new user is anonymous (1/3). The heartbeat restarts from S2's start.
  • Backgrounded: sends S1's end now with its unfocus time and duration, and saves isSplitPending. The next focus starts S2, attributed to a notification open if that focus came from one. A timeout or cold start in between sends no second end.
  • No legacy onSessionStarted / onSessionEnded callbacks fire, since the app session goes on (IAM, outcomes and influence would treat them as a new app session). The focus that starts S2 after a background split fires onSessionActive.
  • The app session's startTime and sessionId stay the same, since IAM reads startTime for session time. S2's start time goes in a new sessionsApiStartTime field, which the create uses.
  • Legacy sessions: no-op.
  • Refactor: the new-session setup in handleOnFocus moved to startNextSession, shared with the split. Added a serial-lane isFocused.
  • Not wired to anything yet.

Stack

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

Testing

Unit testing

New SessionServiceTests: focused split (end, create, IDs, no legacy callbacks, next checkpoint on S2); split to an anonymous user holds S2's create; heartbeat restarts; background split ends now and starts S2 on the next focus; the app session's start time and ID stay the same; a notification open after a background split attributes S2; timeout and cold start after a background split send no second end; legacy no-op.

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.

  • Login as a second user while focused: S1's end went out with end_time at the switch, then S2's create started at the switch.
  • Logout while focused: S2's end went out, and S3 started anonymous with its create held.

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

@fadi-george
fadi-george requested a review from a team as a code owner November 20, 2026 02:06
@fadi-george
fadi-george added this pull request to stack #2855 November 20, 2026 02:07
@fadi-george fadi-george changed the title feat: [SDK-5813] split a session at a user switch (2/4) feat: [SDK-5813] split a session at a user switch (2/3) Nov 20, 2026
@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: 6/6 touched executable lines (100.0%) (21 touched lines in diff)
  • SessionService.kt: 68/68 touched executable lines (100.0%) (106 touched lines in diff)

Overall (aggregate gate)

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

📥 View workflow run

@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.

The split keeps the app session going, and it skips onSessionStarted for that reason. startNextSession still writes session.startTime (SessionService.kt:364) on the focused split (:413) and on the focus that starts the next session after a background split (:343). IAM SESSION_TIME and the IAM list duration both read that field (DynamicTriggerController.kt:49, InAppMessagesManager.kt:359), so a logout or account switch zeros the clock in the same visit. Write startTime only when the app session is new. Pass the switch time into the create. Keep the focusTime and focusElapsedRealtime updates so the next session's active time starts at the switch.

The focus after a background split passes null (SessionService.kt:343) after onFocus already took pendingDirectAttributionId (:309). A warm return from a notification open drops directAttributionId. A cold start still forwards it. Pass directAttributionId on this branch. The focused split at :413 should stay null.

The body says 3/4 and 1/4. The title says 2/3.

@fadi-george

Copy link
Copy Markdown
Contributor Author

Thanks! Fixed in c6a02e3.

  1. A split no longer touches the app session's startTime or sessionId. The next session's start time goes in a new sessionsApiStartTime field, which the create uses. Focus times still reset at the switch.
  2. The focus after a background split now passes the notification-open attribution through. The split while focused still passes null.
  3. Body was already updated to 1/3 and 3/3.

The earlier test and build failures were a Maven Central 429. The new push reran CI.

@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.

Approve with nits. The three from 95825f0 are fixed in c6a02e3.

startNextSession writes sessionsApiStartTime and leaves startTime and sessionId alone, so the IAM clock stays on the app session. The focus after a background split passes directAttributionId. The focused split still passes null. The body matches the title.

The focused split starts the heartbeat even when startSessionsApi just turned this session legacy (SessionService.kt:416). A kill switch after S1 started ends S1 on the sessions API and then ticks updates for a session that was never created. Start the heartbeat only when usesSessionsApi is still true.

The splitSession KDoc (SessionService.kt:384) says no legacy callbacks fire. The focus after a background split fires onSessionActive. Say no onSessionStarted or onSessionEnded.

isSplitPending (SessionModel.kt:170) says the background switch sent the end. It is also set when no end was queued.

@fadi-george

Copy link
Copy Markdown
Contributor Author

Thanks! All three addressed in 733716f.

  • The focused split only starts the heartbeat if the next session still uses the sessions API, with a test for the legacy fallback.
  • splitSession KDoc now says onSessionStarted and onSessionEnded don't fire.
  • isSplitPending now says a background switch ended the session, without claiming the end was sent.

@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.

Heartbeat and the splitSession KDoc are fixed in faeea93.

The focused split starts the heartbeat only when usesSessionsApi is still true (SessionService.kt:422). splitSession now says onSessionStarted and onSessionEnded don't fire (:390).

isSplitPending still says a background switch ended the session (SessionModel.kt:170). It is also set while consent is paused in the foreground, and when the held create can't go out so nothing is queued (SessionService.kt:408, :424). Say the next session starts on the next focus, whether or not an end was queued.

The lock between a split and a session end looks right. CI test hit a Maven Central 429 and needs a rerun.

@fadi-george

Copy link
Copy Markdown
Contributor Author

Thanks! Updated the isSplitPending doc in 9c3d31a: it's set after a switch while the session wasn't counting time (background or waiting for consent), and the next session starts on the next focus whether or not an end was queued. The push reran CI.

fadi-george and others added 6 commits November 23, 2026 15:11
SessionService.splitSession ends the current sessions API session at the
switch and starts the next one for the new user, without legacy session
callbacks. In the background, it sends the end now and starts the next
session on the next focus. Not wired to user switches yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he next session

A split no longer resets the session start time and ID, which in-app messages
read, so the sessions API start time is kept in its own field. A notification
open on the focus after a background split now attributes the next session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also corrects the splitSession and isSplitPending docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A background split and the session timeout could both end the session, and the
split could read durations the end had already reset.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fadi-george
fadi-george merged commit 1f04a18 into new-session-events Nov 23, 2026
5 checks passed
@fadi-george
fadi-george deleted the fadi-sdk-5813-split 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