Skip to content

feat: [SDK-5813] hold an anonymous session's create (1/3) - #2853

Merged
fadi-george merged 6 commits into
new-session-eventsfrom
fadi-sdk-5813
Nov 23, 2026
Merged

fadi-george merged 6 commits into
new-session-eventsfrom
fadi-sdk-5813

Conversation

@fadi-george

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

Copy link
Copy Markdown
Contributor

Description

One Line Summary

Hold an anonymous session's sessions API create until the first background, a heartbeat, or the session end.

Details

Motivation

Part of SDK-5813. The backend sets a session's owner from its create and drops updates with a different onesignal_id. Holding an anonymous create keeps the owner open, so a login before the first background can move the session to that user (wired in 3/3). Under JWT, the queue drops an anonymous create whose user or subscription ID is still local, so that session is lost even if the user logs in. A held create never reaches the queue, so it isn't dropped.

Scope

  • A sessions API session that starts with no external ID saves isCreateHeld (and its notification-open attribution) on SessionModel instead of enqueuing its create.
  • releaseHeldCreate enqueues it right before the first update: the unfocus checkpoint, a heartbeat, or the end. The end covers backgroundRun and a cold start after a kill while held.
  • Under JWT (REQUIRED), an anonymous create waits for a login only while its user or subscription ID is local, since the queue would drop it. With backend IDs it is sent at the first background like any other anonymous create.
  • While the JWT requirement is unknown (before remote params arrive), a local anonymous create stays held the same way, matching iOS, so a create released early isn't purged when the requirement comes back as required. At the session end it still goes out, since the queue waits for the requirement before sending.
  • Stays held while privacy consent is missing, since the queue drops a create sent without consent. A session that ends still held is not reported.
  • SessionModel pins the app ID when the session starts, and every sessions API request uses it. An app ID change keeps the model, so a held create or a later update no longer goes out under the new app.
  • Identified users and the legacy path are unchanged. Login/logout handling is not wired yet (3/3).

Known tradeoffs:

  • Anonymous sessions show up on the server later, after the first background.
  • An anonymous session that ends while consent is revoked is no longer reported (its create used to go out at start).

Stack

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

Test fix

Also fixes a race in SessionOperationQueueTests: enqueueAll verified enqueueNew had started, not finished, so a test could read the queue early. It failed every time in the full suite after main was merged into new-session-events.

Testing

Unit testing

New SessionServiceTests: holds on focus; first unfocus sends the create (with attribution) before the checkpoint and no second create later; heartbeat and background run send it first; cold start after a kill while held sends create then end; JWT anonymous with local IDs stays held and reports nothing; JWT anonymous with backend IDs sends on the first unfocus; requests keep the starting app ID after an app ID change; unknown JWT requirement holds a local anonymous create until the end, and sends one with backend IDs on the first unfocus; JWT identified sends on focus; held create waits for consent. Test mocks now default to an identified user so existing tests keep the send-on-focus behavior.

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: no create on launch. At the first background, the create and the checkpoint went out.
  • Login while the create was held: the session moved to the new user, and its create went out with their external ID and 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

@fadi-george
fadi-george requested a review from a team as a code owner November 20, 2026 01:52
@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] hold an anonymous session's create (1/4) feat: [SDK-5813] hold an anonymous session's create (1/3) Nov 20, 2026
fadi-george and others added 2 commits November 19, 2026 18:25
A session that starts on an anonymous user holds its sessions API create
until the first unfocus, a heartbeat, or the session end. A kill sends it
on the next launch. Under identity verification it stays held while
anonymous, so the queue no longer drops it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A verify on enqueueNew passes once the call starts, so a test could read
the queue before the last enqueue landed. It failed in the full suite
under the extra load from main's new tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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

  • OperationRepo.kt: 5/5 touched executable lines (100.0%) (7 touched lines in diff)
  • SessionQueuePolicy.kt: 1/1 touched executable lines (100.0%) (2 touched lines in diff)
  • SessionModel.kt: 12/12 touched executable lines (100.0%) (39 touched lines in diff)
  • SessionService.kt: 55/56 touched executable lines (98.2%) (106 touched lines in diff)

Overall (aggregate gate)

73/74 touched executable lines covered (98.6% — 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.

sendableHeldCreate holds every anonymous create while identity verification is required (SessionService.kt:408), and endSession then drops it (:274). The queue only drops an anonymous create when the onesignal id or the subscription id is missing or local (OperationRepo.kt:415). An anonymous user created before identity verification was turned on still has backend ids. On the base that session is sent. On this head it is held and discarded. The test at SessionServiceTests.kt:820 uses onesignal-id and subscription-id, so it locks in that drop. Hold under identity verification only when the queue would drop the create, and point that test at local ids.

SessionService.kt:395 sets isCreateHeld false before enqueue has stored the create. Those are different saves, and this release runs on unfocus, which is when Android kills a backgrounded process. The next launch has no hold and no create, and the update is dropped. Clear the flag only after the queue has stored the create. The cold-start test covers a kill while the flag is still true.

SessionService.kt:412 builds the held create with the live app id. An app id change replaces the user and leaves SessionModel in place, so app A's session is created under app B. Pin the app id when the session starts, or drop the held session when the live app id differs.

The body says the wiring is 3/4. The title says 1/3.

…on app ID

Under identity verification, an anonymous held create now waits for a login
only while its user or subscription is local, since the queue would drop it.
Sessions API operations use the app ID the session started with.

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

Copy link
Copy Markdown
Contributor Author

Thanks! Fixed 1, 3 and the body in 58a29c9.

  1. Under identity verification, an anonymous create now waits for a login only while its user or subscription ID is local. With backend IDs it goes out at the first background. The test now uses local IDs, plus a new test for backend IDs.
  2. Leaving this as is. Both writes go through the buffered prefs flush, so the gap is a kill within ~200ms of backgrounding. Saving the create before clearing the flag would trade that for a possible duplicate create.
  3. SessionModel now pins the app ID at session start, and the create, updates and end all use it.

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

@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 local-id hold is in d03c9d2. Identity verification holds an anonymous create only while the user or subscription id is local (SessionService.kt:411). Backend ids send on the first unfocus. New sessions pin the app id they started with (SessionService.kt:344).

The saved-session app id fallback is not on this head. 03b1e21 filled a saved session that had no app id at bootstrap, and this tip replaced that commit with the rename. A saved session with no app id still drops its end and checkpoints.

SessionService.kt:396 still sets isCreateHeld false before enqueue has stored the create. enqueue only posts to the operation repo thread. A kill on unfocus leaves no hold and no create, so the update is dropped. A duplicate create is not the cost. The idempotency key is the local session id and it survives a restart. Clear the flag only after the store add returns.

The class KDoc (SessionService.kt:59) still says identity verification holds every anonymous create.

A held create's flag was cleared before the queue stored the create, so a kill
in between lost it. The session now stays unconfirmed until the queue is known
to have the create, and a cold start resends it otherwise. The create's
idempotency key makes a repeat harmless.

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

Copy link
Copy Markdown
Contributor Author

Thanks! Updates in f53a479.

  • Clearing the hold before the store add: you're right, and the idempotency key settles it. Releasing now sets a saved isCreateUnconfirmed before clearing isCreateHeld, in the same model, so no saved state loses both. The session end checks operationRepo.hasSessionCreate(localSessionId), which is true once the create is in the operation store or the queue has its server session ID. If the queue doesn't have it, the create is sent again. Before saved operations load it reports false, so an early cold start resends, which the key makes harmless. Tests cover a resend, no resend when the queue has it, and the repo query.
  • Class KDoc: now says identity verification holds the create only while the user or subscription ID is local.
  • App ID fallback: dropped on purpose per nan-li's thread. No released build saves a sessions API session without an app ID, since new-session-events hasn't shipped.

… unknown

Mid-session, an unknown requirement holds a local anonymous create like a
required one, matching iOS. Otherwise a create released before remote params
arrive is purged if they come back as required, and a later login can't move
the session. At the end the create still goes out, since the queue waits for
the requirement before sending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

4 participants