Skip to content

fix(push): restore grants after APNs endpoint rotation - #8707

Open
jmecom wants to merge 17 commits into
jm/push-client-revocationfrom
jm/push-endpoint-renewal
Open

jmecom wants to merge 17 commits into
jm/push-client-revocationfrom
jm/push-endpoint-renewal

Conversation

@jmecom

@jmecom jmecom commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Rotate APNs endpoints through an authenticated request, then restore relay grants across enabled communities. Keep successful handoffs until their own renewal deadline or inputs change, so a failing relay does not make healthy communities republish every five seconds. Republish shared grants after either owner renews and avoid concurrent renewals.

Validation: 73 focused Flutter tests and clean mobile analysis, covering failed relays, input changes, renewal deadlines, and accepted handoffs whose local save fails. Physical-device App Attest/APNs verification remains pending.

Stack (merge in order): #8673 → #8703 → #8704 → #8705 → #8706 → #8707 → #8708.

Signed-off-by: jm <jm@squareup.com>
Co-authored-by: Codex <noreply@openai.com>
Signed-off-by: jm <jm@squareup.com>
jmecom and others added 10 commits October 29, 2026 21:18
* blox/push-client-revocation-review-fix:
  test(push): satisfy regression test lint checks
  fix(push): journal gateway and relay cleanup independently

Signed-off-by: jm <jm@squareup.com>

# Conflicts:
#	mobile/test/shared/push/push_bootstrap_test.dart
* blox/push-client-revocation-review-fix:
  fix(push): keep gateway cleanup independent of relay credentials

Signed-off-by: jm <jm@squareup.com>
Signed-off-by: jm <jm@squareup.com>
Co-authored-by: Codex <noreply@openai.com>
* blox/push-ci-lint-pr5-20261029:
  test(push): brace gateway failure callback in PR5

Signed-off-by: jm <jm@squareup.com>
Signed-off-by: jm <jm@squareup.com>
Signed-off-by: jm <jm@squareup.com>
* commit '4cd6bcc05a3e28d10fdb83397500a6e5d37b0201':
  test(desktop): bind duplicate voice players to message IDs

Signed-off-by: jm <jm@squareup.com>
@jmecom
jmecom removed this pull request from stack #8721 October 30, 2026 00:25
@jmecom
jmecom added this pull request to stack #8724 October 30, 2026 00:25
@jmecom
jmecom marked this pull request as ready for review November 3, 2026 20:54
@jmecom
jmecom requested a review from a team as a code owner November 3, 2026 20:54

@joahg joahg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 Read the renewal batch and the lifecycle gating (completed-handoff cache keyed on inputs and deadline, publicationBusy guard, retry after shared-authority change); failures don't suppress the next recovery attempt and healthy origins aren't republished every tick. Physical-device App Attest/APNs verification is still pending per the description.

jmecom and others added 4 commits November 5, 2026 21:11
Signed-off-by: jm <jm@squareup.com>
Co-authored-by: Codex <noreply@openai.com>
@jmecom
jmecom removed this pull request from stack #8724 November 5, 2026 21:43
@jmecom
jmecom added this pull request to stack #9143 November 5, 2026 21:43

@joahg joahg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 Re-approving. The renewal logic is unchanged. The new diff passes hosted-relay assertion headers with background: true on the lease publish, and the fixture now rejects upgrades without the expected identity. That matches the revocation outbox's pattern, and the new test covers all four renewal publishes.

@wesbillman wesbillman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Star Lord: changes needed at b346729; five persistence, rotation-recovery and renewal findings are inline. Source review only, with incomplete product-flow coverage and physical-device App Attest/APNs validation still pending.

/// Lowercase SHA-256 digest of the binary APNs device token.
public let endpointHash: String
/// Endpoint epoch being enrolled, rotated or restored.
public let endpointEpoch: Int64

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Star Lord: [P1] Preserve decoding of existing pending journals. Builds using the same pending-buzz-push-v1 Keychain account already persist records without endpointEpoch. Synthesized Codable requires this new key; the initializer's default does not apply during decoding. One such journal makes the whole array unreadable, blocking enrollment, revocation and the endpoint-grants bridge indefinitely; save/remove also decode first. This affects upgrades of builds where push was enabled. Decode a missing epoch as 1 and cover a pre-change journal fixture. The documented separate-record cutover does not help here because this account name is unchanged.

Comment on lines +577 to +580
if let pending = pendingEnrollment, let handle = pending.gatewayInstallationHandle,
storedRecords.contains(where: {
$0.gatewayOrigin == gatewayOrigin && $0.gatewayInstallationHandle == handle
&& $0.endpointEpoch > (pending.endpointEpoch)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Star Lord: [P2] Retire a conflicting rotation journal at the same epoch. If A journals 1→2/token B but its rotation never commits, a later token C can let a sibling commit 1→2/C and rewrite the shared records to epoch 2/hash C. This strict > check leaves A's epoch-2/B journal intact. A then replays B, which the gateway rejects with 404 because neither the old-epoch CAS nor exact-target retry matches. resumeEnrollment only replaces v1/installations errors without a handle, so this community cannot recover until journal expiry, potentially 30 days. Also supersede an equal-epoch rotation journal with a different endpoint hash, in both this check and settlePendingEnrollments, and cover that sequence.

Comment on lines 584 to +586
final allOriginsKey =
'$nativeCleanupKey|${[for (final value in communities) '${value.id}|${value.pushNotificationsEnabled}|'
'${buzzPushSubscriptionsFingerprint(value.pushSubscriptionState.desired)}'].join(';')}';
'$token|$nativeCleanupKey|${activeSelection.hasValue}|${activeSelection.value?.id}|${[for (final value in communities) '${value.id}|${value.relayUrl}|${value.nsec}|${value.pubkey}|${value.pushNotificationsEnabled}|'
'${buzzPushSubscriptionStateFingerprint(value.pushSubscriptionState)}'].join(';')}';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Star Lord: [P2] Keep failure backoff stable across generation reservations. This full-state fingerprint includes generationCursor, which _publish increments and persists before relay submission. If native enrollment succeeds but WebSocket/auth/event submission repeatedly fails, each retry changes allOriginsKey; BuzzPushAttemptGate.failed therefore resets its failure count every time. The worker repeats storage and network work every 2.5–5 seconds indefinitely instead of backing off toward 15 minutes. Separate failure identity from its own generation bookkeeping and extend the existing journey with repeated post-reservation failures.

firstStack ??= stack;
}
}
if (firstError != null) Error.throwWithStackTrace(firstError, firstStack!);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Star Lord: [P2] Preserve healthy renewal deadlines when another origin fails. Throwing here discards the earliest healthy deadline, so the caller schedules only failure backoff. With a sibling repeatedly failing metadata discovery, the key remains stable and backoff reaches 7.5–15 minutes, longer than the five-minute renewal cushion. A healthy origin checked just before its renewal window can then expire before the next batch, losing notifications despite being available. Return successful deadlines alongside failures and bound the next wake by the earliest healthy renewal deadline.

if (value.relayOrigin != grant.relayOrigin) value,
grant,
]),
expiresAt: grant.expiresAt,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Star Lord: [P3] Cache the accepted lease's expiration. publishBuzzDevPushLease expires the relay lease at min(grant.expiresAt, now + descriptor.maxLeaseTtlSeconds), but this cache and the batch timer use only the native grant deadline. A supported relay advertising a shorter TTL can therefore lose its lease while this cache suppresses republication for the rest of the grant lifetime. The current first-party 30-day TTL does not expose this. Return the publication's actual expiration and use it for cached handoffs and renewal scheduling.

@brow brow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Pre-review recommends rework before this lands. Four things regress what works today:

  1. BuzzPushPendingEnrollmentRecord gains a required endpointEpoch, while released builds and main already save pending journals without it under the same pending-buzz-push-v1 account. After an update the whole pending array fails to decode, so enrollment, renewal and cleanup fail for every community, and no path clears it. Decoding a missing epoch as 1 resolves it.
  2. completedOtherOrigins lives in memory and drops the active community, so every launch and every community switch republishes each inactive enabled community (two NIP-11 fetches, a native enroll and a new authenticated socket each) with no token change. Skipping a community whose accepted lease already matches the current grant authority resolves it; that needs the accepted authority persisted.
  3. allOriginsKey includes generationCursor, which _publish bumps before it submits, so a failure after the reservation changes the key and BuzzPushAttemptGate restarts at 2.5 to 5 s on every retry instead of backing off. Keeping the failure streak's identity independent of the generation bookkeeping resolves it.
  4. A pending rotation journal is superseded only by a greater epoch (BuzzDevPushEnrollmentDriver.swift:580 and :439). When a sibling commits the same epoch with a newer token, the stale journal keeps replaying a rotation the gateway rejects until it expires, and that community gets no push; before this change it re-enrolled. Treating a same-epoch record with a different endpoint hash as superseding, in both places, resolves it.

This branch has not been deployed

No deployments
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