Repository navigation
Ledger removal: dispatch-timer deadline, and a test that signals instead of timing - #268
Merged
Merged
Conversation
…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]>
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
removalWaitsForTheSummaryButNotForeverfailed on CI atDate().timeIntervalSince(started) < 2(2.569 s).Root cause (product and test).
Ledger.summarizeThenRemoveslept its deadline withTask.sleepon 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.
armTimeris an optional parameter that replaces the timer, handing the test the give-up callback.armTimer, checksremovehas not run, fires the give-up itself, and assertsremoveran 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
yesCPU hogs plus 4 pool-blocking tasks per case, 250 parametrized cases, 8 cores):Task.sleepfails the real-timer stress (233 issues); giving up made a no-op fails both new tests.No stress code is committed.
mise run fmtandmise run lintare clean. Fullmise run testhere ran on a machine at load average ~200 and failed only in unrelated wall-clock tests (theHookStaysFast, oncetheHostsHeedWritesItsCallAndPrintsTheAnswer); the latter passes alone. All LedgerTests pass.🤖 Generated with Claude Code