Skip to content

iOS checks the date before a press, a native timer and a page notice, and retells a refused correction - #1

Draft
janicduplessis wants to merge 1 commit into
mainfrom
@janic/clock-wake
Draft

janicduplessis wants to merge 1 commit into
mainfrom
@janic/clock-wake

Conversation

@janicduplessis

@janicduplessis janicduplessis commented Nov 14, 2026 •

Copy link
Copy Markdown

Description

On Apple hosts the date an app reads is time.epochAtZero + performanceNow(), and performanceNow() comes from CACurrentMediaTime(), which stops while the device sleeps. Upstream's expo/exact@1353a1de9 fixes most of this. followOffset now retells the date when it slipped by a second or more, before each advance and frame, and on the system's clock-change, zone-change, wake and did-become-active notices.

This PR covers two gaps that remain on top of that:

  • The first event after a sleep. followOffset also runs before a press, a native task timer and a page notice. So a press, or a timer that fires before any frame or notice, reads the real date instead of the one from before the sleep.
  • A refused correction. Upstream records the told epoch and offset before it applies setTime. If the runner refuses that batch (busy or held), the correction is lost until the date moves again. Here, a refused setTime clears the told epoch and the held offset, so the next check tells it again.

Test plan

  • On macOS, bun host/apple/build.mjs --test --only SessionClockTimerTests,HeadlessSessionTests,NativeTaskTimersTests,PageFactsMacTests,PageFactsSoonMacTests,PressFeedbackMacTests: 6 classes, 0 failed. SessionClockTimerTests includes upstream's sleep test, which moves ExactSession.machineDate ahead.
  • Not reproduced: the slip itself. It needs a real iPhone that sleeps with the app suspended, because a simulator does not stop CACurrentMediaTime.

… and retells a refused correction

Upstream's followOffset (1353a1d) tells the date when it slipped by a
second, before each advance and frame and on the system's clock, zone, wake
and activation notices. It now also runs before a press, a native task timer
and a page notice, so the first event after a sleep reads the real date, and a
setTime the runner refuses (busy or held) leaves the date untold, to be told
again at the next check.
@janicduplessis
janicduplessis changed the base branch from main to @janic/sync-upstream November 15, 2026 02:26
@janicduplessis janicduplessis changed the title iOS tells the date again when the clock slips, as after the device sleeps iOS checks the date before a press, a native timer and a page notice, and retells a refused correction Nov 15, 2026
@janicduplessis

Copy link
Copy Markdown
Author

I rebased this onto @janic/sync-upstream (#10), which merges expo/exact main at a107143. It conflicted in Session.swift and SessionTime.swift, so it would have stopped merging into main once the sync lands. The base is now @janic/sync-upstream, so the diff shows only this PR. Retarget it to main after #10 merges.

Upstream's expo/exact@1353a1de9 already does the core of this PR. followOffset retells the date when it slipped by a second, before each advance and frame, and on the clock, zone, wake and activation notices. SessionTime.swift and toldEpoch are upstream's now. What's left of this PR:

  • followOffset before a press, a native task timer and a page notice. These are the same three call sites as before.
  • A refused setTime (busy or held) clears toldEpoch and offsetHeld, so the date is told again at the next check. Upstream records them before applying the batch.

The three commits are squashed into one, and the description is updated to match. On macOS, --test --only SessionClockTimerTests,HeadlessSessionTests,NativeTaskTimersTests,PageFactsMacTests,PageFactsSoonMacTests,PressFeedbackMacTests passes: 6 classes, 0 failed.

Upstream covers the wake case through didBecomeActiveNotification, so this PR could also be closed if the two gaps above aren't worth carrying.

@janicduplessis
janicduplessis changed the base branch from @janic/sync-upstream to main November 15, 2026 03:40
ccheever added a commit to expo/exact that referenced this pull request Nov 15, 2026
…e is told again

The session's clock stops while the system sleeps and the date does not,
so after a wake the date at the clock's zero is behind. `followOffset`
checked it only before frames, clock advances and system notices: a press,
a native task timer or a page notice that came first read the date from
before the sleep. Each now checks first: `applyInput`, which every person's
input goes through (press, edit, key, pointer), `deliverNativeTimer`,
`tellPage`, and `tellPageSoon`, which queues the date ahead of the page's
facts.

`dateMoved` recorded the date as told before `setTime` reached the runner,
so a batch the runner refused (busy, or held over a restart's reset) left
the session believing it was told, and the date was not sent again until it
moved by another second. A refused tell is now forgotten, and the next
check tells it.

The approach follows Janic Duplessis's fork (appandflow#1), which
checked before a press alone. A new SessionClockTimerTests case presses
right after `ExactSession.machineDate` moves ahead and holds the runtime
over a check; both parts fail without this change.

Fixes #668

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.

1 participant