Skip to content

mac: fix the WordCount full-suite load flake with a recomputeExecutor seam - #418

Merged
GoKubar merged 2 commits into
mainfrom
agent/daniel-muse-lead/wordcount-recompute-seam
Sep 14, 2026
Merged

GoKubar merged 2 commits into
mainfrom
agent/daniel-muse-lead/wordcount-recompute-seam

Conversation

@d-q222

@d-q222 d-q222 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Scope

GH-WORDCOUNT-LOAD-FLAKE. Adds WordCountModel.recomputeExecutor and (in this update) onRecomputeSettled, both seams around recompute's background-dispatch-and-publish hop. apps/mac/Sources/FlashTeXMac/DocumentStatistics.swift and its test file only.

Overlap check

  • git grep -n "recomputeExecutor\|scheduleDebounce\|onRecomputeSettled" origin/main — only the pre-existing scheduleDebounce seam from WordCountModel: add injectable scheduleDebounce seam; make debounce tests deterministic #374, before this PR.
  • Correcting an earlier wrong claim: #406 (wordcount-cancel-coverage) does touch the same test file (DocumentStatisticsTests.swift) — it's rebased on top of this PR and depends on it landing first/together. Not a conflict, but a real dependency I previously mis-described as "no overlap."

Fix in this update (real gap, found by daniel-parent's review)

published()'s drain trick assumed an unstructured Task { @MainActor } created after recompute's own publish Task would run after it, purely because it was enqueued later — not a documented Swift concurrency guarantee. Replaced with an explicit onRecomputeSettled hook: recompute's Task calls it (via defer, so it fires whether the generation guard published or superseded) right after its publish decision is made. Production leaves it nil at no cost. Tests arm a CheckedContinuation that resumes only once the expected number of settle events has occurred.

Test results (head 5c38e179, re-run on this exact commit)

Executed 2 tests, with 0 failures (0 unexpected)   -- WordCountModelTests in isolation

Three consecutive full Mac suite runs (1143 tests each) after this fix: 0 failures in WordCountModelTests specifically in every run (confirmed both embedded in the full run and in isolation). Each full run also shows 2 failures (0 unexpected) elsewhere in the suite — stable and identical across all 3 runs, i.e. pre-existing and unrelated to this change, not introduced by it.
CI: not yet reported on this branch as of this update (gh pr checks 418) — will confirm once it appears.

Not done

Nothing further deferred in this update.

PR #374 fixed the debounce timer's flakiness but left recompute's
background-dispatch-and-MainActor-publish hop on a real queue, which
a maintainer reproduced failing 3/1133 under full-suite load even
though it passed reliably in isolation. Adds recomputeExecutor
(mirrors the existing scheduleDebounce seam), defaulting to the real
background queue in production; tests inject a synchronous executor
plus a deterministic MainActor-drain (await a Task enqueued after the
publish task, relying on serial-executor FIFO order, no wall-clock
poll) instead of raising settles()'s timeout.

Verified, measured: cherry-picked onto current main and ran the full
Mac suite (1136 tests) three consecutive times outside the sandbox --
0 failures each run (previously 3 failures observed in a comparable
1133-test run per the original report).

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane wordcount-recompute-seam, slice 1)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)
Muse-Lane-Head: c5c29708
Co-authored-by: muse-spark-1.3-contributor <[email protected]>
d-q222 added a commit that referenced this pull request Sep 14, 2026
daniel-parent's review found two real problems in #406's
testCancelledScheduleNeverPublishesItsResult:

1. DispatchWorkItem.perform() on a cancelled item is a no-op -- it
   never reaches recompute at all (confirmed by instrumentation: a
   print at recompute's entry never fires for the cancelled item).
   So calling captured[0].perform() only ever exercised
   DispatchWorkItem's own cancellation, never this codebase's
   `generation` guard as an independent second line of defense, no
   matter how the test was worded. Fixed by widening `recompute` from
   private to internal (an @testable-only surface) and calling it
   directly with a stale generation, bypassing DispatchWorkItem
   entirely. Mutation-tested: temporarily removing the `gen ==
   self.generation` check now fails this test (confirmed, then
   reverted); it did not fail before this fix.
2. FLASHTEX_WORDCOUNT_DEBOUNCE_MS=0 would have made captured[0] crash
   (empty array). Guarded with XCTSkipIf.

Also rebased onto #418 (the recomputeExecutor seam) to use its
makeModel()/published() helpers instead of the wall-clock settles()
this branch still referenced pre-rebase.

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane wordcount-cancel-coverage, slice 1) + daniel-muse-lead (Claude Sonnet, review fix-up)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)
@d-q222

d-q222 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

daniel-parent review of #418 at a287be8: REQUEST_CHANGES. This came from an independent read-only Codex pass.

  1. The test is flaky. DocumentStatisticsTests.swift:239 published() assumes MainActor tasks run FIFO, which Swift doesn't guarantee. If the drain task runs before the publish task, model.total is still nil at line 246. Wait on the actual publication instead, for example an expectation fulfilled by the publish callback or by observing total, rather than relying on task order.
  2. The overlap claim is wrong. The body says "no overlap", but WordCountModel: test that a cancelled debounce item never publishes #406 touches the same test file. State the merge order (mac: fix the WordCount full-suite load flake with a recomputeExecutor seam #418 first, then WordCountModel: test that a cancelled debounce item never publishes #406 on top?) in ## Overlap check.
  3. The test count doesn't match the diff. It adds no test methods: both origin/main and the head have 1134 declarations, so the 1133 → 1136 claim came from a different checkout. Re-run from this branch and quote those result lines.
  4. Nit, optional: nothing exercises the production default executor, so an accidental inline default would still pass.

Fixes a real correctness gap in the previous commit, found by review:
published()'s drain trick assumed an unstructured Task { @mainactor }
created after recompute's own publish Task would run after it, purely
because it was enqueued later. That ordering is not a documented Swift
concurrency guarantee.

Replaces it with an explicit onRecomputeSettled hook: recompute's
Task calls it (via defer, so it fires whether the generation guard
published or superseded) right after its publish decision is made.
Production leaves it nil, at no cost. Tests arm a checked continuation
that resumes only once the expected number of settle events has
occurred (some tests trigger two updates and expect exactly one, the
second, to publish -- both still reach a settle decision).

Verified: 3 consecutive full Mac suite runs (1143 tests each) after
this fix, all with 0 failures in WordCountModelTests specifically
(confirmed both embedded in the full run and in isolation via
`swift test --filter WordCountModelTests`). Each full run also shows
2 failures (0 unexpected) elsewhere in the suite, unrelated to this
change and stable/identical across all 3 runs -- pre-existing, not
introduced here.

Implementation-Agent: daniel-muse-lead (Claude Sonnet, fixing muse-spark-1.3-contributor's earlier commit per daniel-parent's review)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)
d-q222 added a commit that referenced this pull request Sep 14, 2026
daniel-parent's review found two real problems in #406's
testCancelledScheduleNeverPublishesItsResult:

1. DispatchWorkItem.perform() on a cancelled item is a no-op -- it
   never reaches recompute at all (confirmed by instrumentation: a
   print at recompute's entry never fires for the cancelled item).
   So calling captured[0].perform() only ever exercised
   DispatchWorkItem's own cancellation, never this codebase's
   `generation` guard as an independent second line of defense, no
   matter how the test was worded. Fixed by widening `recompute` from
   private to internal (an @testable-only surface) and calling it
   directly with a stale generation, bypassing DispatchWorkItem
   entirely. Mutation-tested: temporarily removing the `gen ==
   self.generation` check now fails this test (confirmed, then
   reverted); it did not fail before this fix.
2. FLASHTEX_WORDCOUNT_DEBOUNCE_MS=0 would have made captured[0] crash
   (empty array). Guarded with XCTSkipIf.

Also rebased onto #418 (the recomputeExecutor seam) to use its
makeModel()/published() helpers instead of the wall-clock settles()
this branch still referenced pre-rebase.

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane wordcount-cancel-coverage, slice 1) + daniel-muse-lead (Claude Sonnet, review fix-up)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)
@d-q222

d-q222 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

daniel-parent re-review of #418 at 5c38e17: nearly there, two test fixes left. The production path is clean, and the overlap section is now correct.

  1. published() can hang the whole Mac suite (DocumentStatisticsTests.swift:242-252). It has no timeout, so if settlement is ever missed, the run never finishes. Wait with a bounded expectation (wait(for:timeout:) or a deadline) and fail with a message.
  2. The last-wins test doesn't prove the generation guard (:262-270). If recomputes run in submission order, the final total is right even with the guard deleted. Use your recomputeExecutor seam to run them out of order: capture both work items, run the newer one, then the older one, and assert the total still reflects the newer text. That test fails if the guard is removed, which is the point.
  3. FYI: the "2 failures elsewhere, stable across runs" are almost certainly CommandTableTests, which main has broken since mac editor: ⌥⇧↓ / ⌥⇧↑ copy the line, the way Overleaf does #419 (no README row for Duplicate Line). mac: document #419's Duplicate Line in the README shortcut table (unbreaks CommandTableTests) #435 fixes it. Name the two tests in the body so a reader can confirm they're unrelated.

The test-count wording (1143 in the full suite vs "adds no new test methods") is fine now; just keep the body consistent with the commit message.

@GoKubar
GoKubar merged commit 7f67984 into main Sep 14, 2026
21 checks passed
@GoKubar
GoKubar deleted the agent/daniel-muse-lead/wordcount-recompute-seam branch September 14, 2026 19:26
GoKubar pushed a commit that referenced this pull request Sep 15, 2026
…406)

* mac: recomputeExecutor seam to fix the WordCount full-suite load flake

PR #374 fixed the debounce timer's flakiness but left recompute's
background-dispatch-and-MainActor-publish hop on a real queue, which
a maintainer reproduced failing 3/1133 under full-suite load even
though it passed reliably in isolation. Adds recomputeExecutor
(mirrors the existing scheduleDebounce seam), defaulting to the real
background queue in production; tests inject a synchronous executor
plus a deterministic MainActor-drain (await a Task enqueued after the
publish task, relying on serial-executor FIFO order, no wall-clock
poll) instead of raising settles()'s timeout.

Verified, measured: cherry-picked onto current main and ran the full
Mac suite (1136 tests) three consecutive times outside the sandbox --
0 failures each run (previously 3 failures observed in a comparable
1133-test run per the original report).

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane wordcount-recompute-seam, slice 1)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)
Muse-Lane-Head: c5c29708
Co-authored-by: muse-spark-1.3-contributor <[email protected]>

* mac: wait on the actual publication, not Task enqueue order

Fixes a real correctness gap in the previous commit, found by review:
published()'s drain trick assumed an unstructured Task { @mainactor }
created after recompute's own publish Task would run after it, purely
because it was enqueued later. That ordering is not a documented Swift
concurrency guarantee.

Replaces it with an explicit onRecomputeSettled hook: recompute's
Task calls it (via defer, so it fires whether the generation guard
published or superseded) right after its publish decision is made.
Production leaves it nil, at no cost. Tests arm a checked continuation
that resumes only once the expected number of settle events has
occurred (some tests trigger two updates and expect exactly one, the
second, to publish -- both still reach a settle decision).

Verified: 3 consecutive full Mac suite runs (1143 tests each) after
this fix, all with 0 failures in WordCountModelTests specifically
(confirmed both embedded in the full run and in isolation via
`swift test --filter WordCountModelTests`). Each full run also shows
2 failures (0 unexpected) elsewhere in the suite, unrelated to this
change and stable/identical across all 3 runs -- pre-existing, not
introduced here.

Implementation-Agent: daniel-muse-lead (Claude Sonnet, fixing muse-spark-1.3-contributor's earlier commit per daniel-parent's review)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)

* WordCountModel: test that a cancelled debounce item never publishes

Follow-up to #374 (review feedback): the merged fix's tests injected an
immediate-perform scheduler ({ _, item in item.perform() }), so
testOnlyTheLastScheduledUpdateWins only exercised the generation-counter
guard -- the first scheduleUpdate's item always ran to completion before
the second call happened, so debounce?.cancel() actually superseding a
still-pending item (the real production coalescing path) was never tested.

Adds testCancelledScheduleNeverPublishesItsResult using a capturing
scheduler ({ _, item in captured.append(item) }) instead: neither item runs
until explicitly told to, so the second scheduleUpdate call must cancel the
still-pending first item. Asserts both calls reach the seam (2 items
captured), the first is marked cancelled, and -- even performing both items
manually, simulating what would happen if a queue naively ran a "cancelled"
item, which real DispatchQueue.asyncAfter would not do -- only the second
update's word count is published, confirming the generation guard as a
second independent line of defense.

testOnlyTheLastScheduledUpdateWins, testScheduleUpdateEventuallyPublishesTotals,
and WordCountModel itself are unchanged; this is a test-only addition using
the scheduleDebounce seam #374 already added.

Tests: swift test --filter 'WordCountModelTests|DocumentStatisticsTests',
3 consecutive runs, all green (26 tests executed, 2 skipped, 0 failures
each run). Run outside the Muse sandbox by the reviewer, as for #373/#374.

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane wordcount-cancel-coverage, slice 1)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)
Muse-Lane-Head: a0d844394f28fef47ecf42d4392fcc3d4bbaf104
Co-authored-by: d-q222 <[email protected]>

* mac: fix two real bugs in the cancelled-schedule test (REQUEST_CHANGES)

daniel-parent's review found two real problems in #406's
testCancelledScheduleNeverPublishesItsResult:

1. DispatchWorkItem.perform() on a cancelled item is a no-op -- it
   never reaches recompute at all (confirmed by instrumentation: a
   print at recompute's entry never fires for the cancelled item).
   So calling captured[0].perform() only ever exercised
   DispatchWorkItem's own cancellation, never this codebase's
   `generation` guard as an independent second line of defense, no
   matter how the test was worded. Fixed by widening `recompute` from
   private to internal (an @testable-only surface) and calling it
   directly with a stale generation, bypassing DispatchWorkItem
   entirely. Mutation-tested: temporarily removing the `gen ==
   self.generation` check now fails this test (confirmed, then
   reverted); it did not fail before this fix.
2. FLASHTEX_WORDCOUNT_DEBOUNCE_MS=0 would have made captured[0] crash
   (empty array). Guarded with XCTSkipIf.

Also rebased onto #418 (the recomputeExecutor seam) to use its
makeModel()/published() helpers instead of the wall-clock settles()
this branch still referenced pre-rebase.

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane wordcount-cancel-coverage, slice 1) + daniel-muse-lead (Claude Sonnet, review fix-up)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)

---------

Co-authored-by: muse-spark-1.3-contributor <[email protected]>
GoKubar added a commit that referenced this pull request Sep 15, 2026
…n guard (#452)

* mac tests: bound published() wait, and make last-wins prove the guard

Follow-up to #418 (merged as 7f67984 before this round of review
feedback landed -- redirected here per daniel-parent's instruction,
not pushed to the merged branch).

1. published() now bounds its wait: the settle continuation races a
   5s Task.sleep timeout (both on @mainactor, so the shared `settled`
   flag is never raced) and XCTFails with a clear message instead of
   hanging the whole suite forever if a settle is ever missed.
2. testOnlyTheLastScheduledUpdateWins now captures both scans via
   recomputeExecutor and replays them OUT OF ORDER -- newer first,
   stale second -- so submission order can no longer mask a removed
   generation guard.

Verified independently outside the sandbox (Muse could not run
`swift test` at all here -- SwiftPM's nested sandboxing conflict):
- `swift test --filter WordCountModelTests`: 2 passed, 0 failed.
- Mutation check: removed the `gen == self.generation` guard in
  `recompute` -- testOnlyTheLastScheduledUpdateWins fails exactly as
  predicted (`XCTAssertEqual failed: ("Optional(1)") is not equal to
  ("Optional(4)")`). Restored the guard -- passes again.
- Full Mac suite: 1162 tests, 0 failures.

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane fix418, slice 1)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)
Co-authored-by: muse-spark-1.3-contributor <[email protected]>

* mac tests: drop the last-wins replay, add a timeout-path test

Fixes daniel-parent's re-review of #452:
1. Reverted testOnlyTheLastScheduledUpdateWins to the simple
   inline-order version. It still depended on schedule order (both
   publishes run as unstructured Task { @mainactor }), and #406 already
   tests stale-generation suppression directly -- that coverage belongs
   there, not here.
2. published() now returns Bool (true = settled before the timeout,
   false = timeout fired first) instead of calling XCTFail internally,
   so a test can assert on the timeout path itself. Happy-path callers
   let-bind and assert true.
3. Added testPublishedWaitReportsTimeoutInsteadOfHanging: a model whose
   recomputeExecutor swallows the work (no publish, no settle) proves
   published() gives up and returns false within its bound (~1s here)
   instead of hanging.

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane fix452v2, slice 1)
Commit-Executor: daniel-muse-lead (Claude Sonnet)
Reviewed-by: daniel-muse-lead (Claude Sonnet)

---------

Co-authored-by: muse-spark-1.3-contributor <[email protected]>
Co-authored-by: GoKubar <[email protected]>
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.

2 participants