Skip to content

Deliver a courier_later message within about a second of its time - #660

Merged
ivopogace merged 6 commits into
mainfrom
issue-657-later-timer
Nov 18, 2026
Merged

ivopogace merged 6 commits into
mainfrom
issue-657-later-timer

Conversation

@ivopogace

@ivopogace ivopogace commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Issue

Closes #657

Type of change

  • Bug fix
  • New feature or behaviour
  • Refactor
  • Documentation
  • OpenCode pin bump or release

What does this PR do?

A courier_later message was delivered at the scheduler's next 15-second tick after its time. It now gets a timer set for its time, and the tick stays as the safety net.

  • src/scheduling/timer.ts keeps one setTimeout per message due within two ticks (NEAR_MS, 30 s). schedule sets it for a message that is near; each tick's scan of later/, which the tick does anyway, sets it for the ones that have come near, another server's and the ones from before a restart included. cancel (so cancelScheduled too) and the delivery clear it.
  • A timer delivers nothing itself. It asks the hub for a tick at once (tickNow in src/platform/hub.ts: wait for a tick under way, then tick), so everything that guards a tick guards it too: the owner key, the claim set, the copy loaded last running through the instance loaded first.
  • A message due before the plugin loaded, or scheduled less than a tick ahead on the server that does not hold the owner key, is still delivered by the tick.

Decisions:

  • Only near messages get a timer. No timer runs longer than 30 s, so setTimeout's 2^31-1 ms limit needs no cap, nothing has to be cleared on unload (the timers are unref'd, and a leftover one only asks for a tick, which is harmless), and a non-owner never arms for a far message because only the owner's tick scans. Two ticks, not one, so a single tick that does not come leaves no gap.
  • One timer per message, not one for the earliest. Scheduling, cancelling or delivering one message then never has to work out which is next.
  • The timer goes through the hub's tick, not round it. A timer-driven tick also runs the missed-wake job, which decides by elapsed time and not by tick count. Nothing runs while nothing is due.
  • A timer that fires before the clock reaches the message's time (a runtime timer and Date.now can disagree by a millisecond) is set again by the tick it asked for.
  • No change to the hub's shape, so HUB_VERSION stays 2: tickNow is a module function over the existing scheduler.running and scheduler.tick. With an older copy of hub version 2 in the process it degrades to the tick. deliverDue uses no new port of a member: LaterPorts.timer is optional, absent on every real member, and stands in for the copy's own timer in unit tests.
  • No new storage key, option or tool parameter. The timers are in memory, per loaded copy.
  • No model-facing string changed. courier_later's description still says "may arrive up to ~15 seconds late", which stays true as the worst case (another server, a restart). So no real-model run is owed.
  • README.md says nothing about lateness and is unchanged. docs/reference.md grows by 25 words, within its budget.

How #656 would replace this: all of it sits in src/scheduling/ behind schedule, cancel, cancelScheduled, isScheduled and dueJob. Delegating to the host's automations removes timer.ts, deliverDue and dueJob with their tests, and tickNow if nothing else uses it; schedule and cancel drop their arm and disarm calls as they move to the host. Nothing outside the module knows the timer exists, and there is no stored state to migrate beyond the later/ keys #656 already lists.

How did you verify it?

  • Unit: test/later.test.ts (the timer is set for a near message and not for a far one, a tick sets it once the message is near or was scheduled elsewhere, cancel, stop and delivery clear it, an early firing is set again, a failed request for a tick is logged) and test/hub.test.ts (tickNow between two ticks, behind one under way, with no instance, on a server that does not hold the owner key, and one message through the real timer and the hub).
  • Live: the later group of e2e/run.sh gains two scenarios that measure a message's arrival against its time, one due in 3 s and one due in 33 s that a tick has to find near; each must arrive no earlier than its time and at most 2 s after. CI runs them.
  • e2e/two-servers.sh (not in CI), run locally against OpenCode 2.3.2: each of the 20 courier_later messages was delivered once, none twice and none never, with the schedulers in step and out of step, and every other result is as seen at the pin.

Screenshots / recordings

None: no change to the terminal part.

Checklist

  • The unit job's checks pass locally (bun run typecheck, the touched test files; CI runs the rest)
  • A behaviour change comes with unit tests and an e2e/run.sh scenario (or e2e/tui.sh for the terminal part)
  • README.md and docs/reference.md are updated where behaviour changed
  • Nothing added to docs/reference.md is rendering detail, internals or restated model-facing text
  • A change to a model-facing string (src/**/text.ts, src/kernel/protocol.ts, src/skill.ts, a tool description) updates test/__snapshots__/ (none changed)
  • No unrelated changes

🤖 Generated with Claude Code

https://claude.ai/code/session_0138xCuDYXnVSZNjmzjuLbFw

A message due within two ticks gets a timer, set as it is scheduled and
by each tick's scan of the pending ones, cleared when it is cancelled or
delivered. The timer delivers nothing itself: it asks the hub for a tick
at once, behind one under way, so the owner key and the claim set decide
as at any tick. The 15-second tick stays as the safety net.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_0138xCuDYXnVSZNjmzjuLbFw
One message near as it is scheduled and one more than two ticks away,
which a tick finds near, each no earlier than its time and at most two
seconds after.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_0138xCuDYXnVSZNjmzjuLbFw
@sonarqubecloud

Copy link
Copy Markdown

@ivopogace
ivopogace merged commit 07c5208 into main Nov 18, 2026
14 checks passed
@ivopogace
ivopogace deleted the issue-657-later-timer branch November 18, 2026 20:23
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.

A courier_later message arrives up to 15 seconds late

2 participants