Skip to content

Ledger removal: dispatch-timer deadline, and a test that signals instead of timing - #268

Merged
Mpdreamz merged 1 commit into
mainfrom
fix/ledger-removal-timing
Nov 16, 2026
Merged

Mpdreamz merged 1 commit into
mainfrom
fix/ledger-removal-timing

Conversation

@Mpdreamz

Copy link
Copy Markdown
Contributor

removalWaitsForTheSummaryButNotForever failed on CI at Date().timeIntervalSince(started) < 2 (2.569 s).

Root cause (product and test). Ledger.summarizeThenRemove slept its deadline with Task.sleep on the cooperative pool, so on a loaded pool the deadline resumes late, which makes the bound soft. The test then measured wall clock that included that scheduling. Under a saturated pool the lag is large enough that the 5 s stand-in summary finishes before the 100 ms timer runs, so removal waited on the summary.

Fix.

  • Product: the default deadline is armed on a dispatch queue and yields the give-up straight into the wait. armTimer is an optional parameter that replaces the timer, handing the test the give-up callback.
  • Test: split into three. The first checks removal waits for a quick summary. The second never touches a clock: it arms the timer through armTimer, checks remove has not run, fires the give-up itself, and asserts remove ran without the summary (a stand-in that never comes). The third checks the real timer ends the wait, with no bound on how soon.

Numbers (24 yes CPU hogs plus 4 pool-blocking tasks per case, 250 parametrized cases, 8 cores):

  • Before (old test body): 232/250 cases failed; 215 of them saw the summary win over the 100 ms timer.
  • After: 0 failures in 500 cases (250 real-timer, 250 signal).
  • Mutation: default timer put back on Task.sleep fails the real-timer stress (233 issues); giving up made a no-op fails both new tests.

No stress code is committed. mise run fmt and mise run lint are clean. Full mise run test here ran on a machine at load average ~200 and failed only in unrelated wall-clock tests (theHookStaysFast, once theHostsHeedWritesItsCallAndPrintsTheAnswer); the latter passes alone. All LedgerTests pass.

🤖 Generated with Claude Code

…e-up instead of measuring the clock

summarizeThenRemove slept its deadline on the cooperative pool, so on a loaded pool the deadline lagged (under a saturated pool the 5 s summary finished before the 100 ms timer ran). The default timer is now armed on a dispatch queue, and armTimer lets a test fire the give-up itself.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working label Nov 16, 2026
@Mpdreamz
Mpdreamz merged commit a413a72 into main Nov 16, 2026
5 checks passed
@Mpdreamz
Mpdreamz deleted the fix/ledger-removal-timing branch November 16, 2026 11:55
Mpdreamz added a commit that referenced this pull request Nov 16, 2026
…f timing it (#274)

Tests bounded wall-clock durations and failed on loaded runners (`took <
.milliseconds(800)` at 1.06 s, the forty-asks `< .seconds(3)`, and the
like). This gives the code that owns those timeouts a clock and rewrites
the tests to move time instead of measuring it.

## The clock seam

- `PermissionHook.wait` / `exchange` and `Ledger.summarizeThenRemove`
take `clock: any Clock<Duration> = ContinuousClock()`. That is Swift's
protocol, spelled `_Concurrency.Clock` in HeedCore because heed's own
`Clock` enum (display time) shadows it. The enum is unchanged.
- The hook blocks on sockets on a plain thread, so its deadlines are a
new `Deadline`: an instant on any clock, which `ControlClient` reads
before every poll (connect, write, each read, the backlog retry). On the
continuous clock a poll waits for all the time left, as before. On any
other clock a poll cannot see time pass, so it waits in 5 ms slices and
reads the clock again. Moving a manual clock past the ceiling therefore
ends the wait, with real sockets.
- `exchange(received:)` hears each receipt as the hook takes it. Tests
need it to move time only after the receipt; without it they race.
- `summarizeThenRemove` keeps the dispatch timer from #268 on the
continuous clock and sleeps on any other clock. This replaces
`armTimer`.
- The pauses between reconnect attempts (`Thread.sleep`) stay real time.
`PermissionBroker` already takes `now:`, and `ControlServer` has no
timeout of its own besides its 1 s slow-reader write poll, which is
untouched.

Behaviour with the default clock is the same: the same deadlines, and
polls bounded by the time left.

## Tests

- `ManualClock` (tests only): time moves only on `advance`, `sleep`
waits for it, and `reads` counts how often `now` was read. Hang guards
`waitUntil` and `finished` give up after ten real seconds and record a
failure.
- PermissionRoundTripTests and ApprovalTests' `aSlowAnswerTimesOut…` run
the hook on the manual clock. Each window closes because the test
advanced past it, and the test asserts the outcome: `.ceiling`,
`.noReceipt`, and that heed had not answered yet. Heed's own answer
arrives only after ten real seconds, as a hang guard.
- Forty asks are answered while time stands still, and the slow ask
still waits. Then the test advances past its ceiling.
- `anAnswerIsNotHeldBackByABusyCooperativePool` now asserts an order
instead of `took < 500 ms`: heed's answer arrives before half of the
queued medium-priority work has run.
- The ledger removal test advances the manual clock past the timeout.
- `DomainTests`' 200k-character branch name keeps its outcome check. Its
`< 5 s` moves to `BranchNameBench` (`mise run bench`).
- A grep for `timeIntervalSince(`, `ContinuousClock.now -` and `took <`
in Tests finds no duration assertion left outside Bench.
`LLMRunnerTests.noMoreThanTheConcurrencyRunAtOnce` asserts a lower bound
(`>= 2 s`), which load cannot break, so I left it to the LLMRunner fake
work.

## Verification

- `mise run fmt` and `mise run lint` are clean. A full `mise run test`
passed: 2516 tests, on a machine at load average about 100–240.
- I ran the affected suites (PermissionRoundTrip, Approval, Ledger,
ManualClock) 15 times in a row: 0 failures.
- Each mutation made the tests fail. None of them hung:
- the hook ignores its ceiling: the trickle, slow-answer and forty-asks
tests fail, the last two at the hang guard
  - the hook ignores its receipt window: `noReceipt…` fails
- a receipt does not extend the wait to the ceiling: `aReceiptKeeps…`
fails, 3 out of 3 runs
- the reply task runs at `.medium`: the busy-pool test fails, with 1199
of 1200 tasks run before the answer
  - the removal ignores its timeout: the ledger test fails

## Not tested

- The 5 ms slice runs only under a non-continuous clock, so the real
hook in production never runs it.
- No test checks that the default-clock path keeps its exact poll
timeouts.
- The app-side `wait_seconds` fallback in `AppModel+Approvals` still
uses `Task.sleep`. It is app layer and not under test.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant