Skip to content

fix(render): stop publishing a tokenless ack on a configure failure - #2990

Merged
Xuepoo merged 1 commit into
mainfrom
ctx-1735/fix-present-ack-order
Nov 7, 2026
Merged

Xuepoo merged 1 commit into
mainfrom
ctx-1735/fix-present-ack-order

Conversation

@Xuepoo

@Xuepoo Xuepoo commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

What the issue asks

On the macOS ARM64 leg, present::tests::a_configure_failure_never_presents_the_old_extent_frame collected one ack whose token was u64::MAX instead of the submitted frame's token 0. This PR settles the ownership question and removes the ack that caused it.

Ack ownership decision (recorded in code, not just here)

The presenter worker owns every acknowledgement — it is the only component that runs configure/acquire/present, so the event-loop thread never synthesises one. An ack describes one submitted frame and carries that frame's PresentAck::token; consumers attribute acks by token, never by arrival position.

A tokenless ack may not exist. The pre-fix code emitted one (PRESENT_NON_FRAME_TOKEN = u64::MAX) from the configure-failure branch whenever no frame was in the mailbox. That is the ordinary shape of a configure failure, because configure/resize are queued on their own tick (set_window_opacity, resize reflow) ahead of the next frame:

  • a synthetic ack could be the first ack a caller observed, ahead of the ack for a frame submitted microseconds later (exactly the CI failure), and
  • a frame submitted after the worker decided to exit was absorbed by a dead worker's mailbox and never acked at all.

Both are observable behaviour, not test detail. The decision:

situation ack
configure fails with a frame taken that frame's token, Error("configure failed: …") — a frame built for the old extent must never be presented (unchanged)
configure fails without a frame no ack. Not a frame outcome; nothing to attribute. The worker exit is the signal.
a present attempt runs that frame's token, one of presented/outdated/lost/timeout/other/error (unchanged)

The no-frame case is already covered by the runtime: poll_presenter's inactive branch retains every pixel as damage and recovers ("A dead or failed worker never acked the frame it held"), and mark_exited wakes the loop, so nothing waits for the stuck bound. No consumer ever needed the sentinel: the runtime's PRESENT_NON_FRAME_TOKEN branch only set pending_full_redraw, which that same inactive branch sets unconditionally, and it discarded the detail.

The invariant now lives in code: the # Acknowledgements module section (crates/bitty-render/src/present.rs), the configure-failure branch in worker_loop, PresentAck::token, Presenter::drain_acks, and Runtime::apply_present_ack.

Change

  • crates/bitty-render/src/present.rs: the configure-failure branch acks only a frame it actually took; the no-frame case publishes nothing. PRESENT_NON_FRAME_TOKEN is deleted (a reserved sentinel nothing may produce is a dead contract, and the reserved value would otherwise stay part of the ack format).
  • crates/bitty-runtime/src/runtime/present.rs: drop the sentinel branch in apply_present_ack (now unreachable by construction; an ack for any token other than the newest pending one is already ignored), document token-based attribution, drop the now-meaningless assert_ne!(…, PRESENT_NON_FRAME_TOKEN) guard in a_stale_ack_cannot_commit_a_newer_pending_frame_or_arm_retry.
  • crates/bitty-render/src/present/tests.rs:
    • a_configure_failure_never_presents_the_old_extent_frame is no longer racy and no longer positional: frame 0 is blocked inside the acquire so the failing configure and the frame it must fail are taken together (deterministic), both acks are drained in one pass and each is found by token, and the test asserts exactly two acks (one per taken frame) and that the fresh worker afterwards presents.
    • new a_configure_failure_without_a_frame_publishes_no_ack: the frame-less configure failure leaves the ack ring empty, and the following submit is refused (None) instead of being absorbed by a dead worker.

Revert proof (red against the old production code)

Throwaway copy of the tree with only the production hunk reverted (old map_or(PRESENT_NON_FRAME_TOKEN, …) emission restored), built with a separate CARGO_TARGET_DIR (.targets/ctx-1735-revert), per the false-green rule:

$ cargo test -p bitty-render --offline --lib present::tests::a_configure     # EXIT 101
running 2 tests
test present::tests::a_configure_failure_without_a_frame_publishes_no_ack ... FAILED
test present::tests::a_configure_failure_never_presents_the_old_extent_frame ... ok
---- present::tests::a_configure_failure_without_a_frame_publishes_no_ack stdout ----
assertion `left == right` failed: a configure that carried no frame publishes no ack
  left: 1
 right: 0
test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 455 filtered out

Same copy, the pre-fix test body restored from 4f32fd8a with the configure-before-frame race widened (sleep between configure and submit), reproducing the CI assertion verbatim:

$ cargo test -p bitty-render --offline --lib present::tests::zz_revert_check     # EXIT 101
assertion `left == right` failed: the frame's own token is acknowledged
  left: 18446744073709551615
 right: 0

For completeness: 200 consecutive runs of the pre-fix test against the pre-fix code on this Linux host passed 200/0 — the flake needs the worker to win the configure-before-frame race, which macOS hit. The widened-race run above shows the identical assertion, and the new frame-less test makes the same defect deterministic on any host.

Gates (all run in this worktree, CARGO_TARGET_DIR=.targets/ctx-1735)

command exit
cargo fmt --all -- --check 0
cargo clippy -p bitty-render --all-targets --offline -- -D warnings 0
cargo clippy -p bitty-runtime --all-targets --offline -- -D warnings 0
cargo test -p bitty-render --offline (457 lib + all integration suites) 0
cargo test -p bitty-runtime --offline --lib -- runtime::present (151 tests) 0
cargo +1.85 check -p bitty-render --offline --tests (MSRV) 0
cargo +1.85 check -p bitty-runtime --offline --tests (MSRV) 0
cargo check --workspace --all-targets --offline 0
RUSTDOCFLAGS="-D warnings" cargo doc -p bitty-render -p bitty-runtime --no-deps --offline 0
cargo nextest run --workspace --offline 10745 run: 10743 passed, 2 failed — see below
just ci-local not green on this host, environment steps only — see below

Also ran the two configure-failure tests in bitty-render 15 consecutive times: 15/15 green (they are order-independent now).

cargo nextest run --workspace — the two failures are wall-clock soak budgets under full-suite parallelism, not assertions about present behaviour:

FAIL [ 70.499s] bitty-runtime::opencode_soak opencode_shaped_soak_stays_bounded
  panicked at crates/bitty-runtime/tests/opencode_soak.rs:435:5:
  opencode-shaped soak must finish inside its budget, took 70.457413542s (budget 60s)
FAIL [ 74.246s] bitty-runtime::soak soak_headless_1000_ticks_bounded_and_deterministic
Summary [ 116.312s] 10745 tests run: 10743 passed, 2 failed, 24 skipped

Both pass when run alone (same tree, same target dir):

PASS [  47.529s] bitty-runtime::opencode_soak opencode_shaped_soak_stays_bounded
PASS [  54.005s] bitty-runtime::soak soak_headless_1000_ticks_bounded_and_deterministic
Summary [  54.014s] 2 tests run: 2 passed

Neither test exercises the presenter (they are runtime soak/CPU-budget tests), and the present suites are fully green; CI runs them with lower parallelism.

just ci-local was attempted twice and could not reach the code gates on this host:

  1. First attempt aborted cloning an action: Get "https://github.com/cachix/install-nix-action/info/refs?service=git-upload-pack": unexpected EOF (no proxy forwarded; NETWORK_PROXY is unset in non-interactive sessions on this box, so the wrapper's HTTPS_PROXY was empty).

  2. Second attempt (host proxy forwarded) reached the job, then stopped on two environment steps before fmt/clippy/nextest ran:

    • Verify flake.lock resolves: error: opening Git repository ".../ctx-1735-present-ack": failed to resolve path ".../bitty/.git/worktrees/ctx-1735-present-ack': No such file or directory — nix inside the container cannot resolve a git worktree gitdir whose common dir lives outside the bind mount;
    • Show sccache stats (shard 1/2): --show-stats: command not found (exit 127).

    Neither step touches bitty-render/bitty-runtime sources. That is why every code gate above was run directly instead.

Acceptance

  • Ownership stated and recorded in code: yes (above).
  • Frame's ack collected by token / extra ack stopped: both — the extra ack no longer exists, tests attribute by token.
  • "10 consecutive macOS ARM64 runs of present::tests green": not produced — no macOS host available in this lane. Pending CI; the reviewer/guard decides whether the run history satisfies it. Hence Part of #2980 rather than Closes #2980.

Summary by CodeRabbit

  • Bug Fixes
    • Presentation acknowledgements now consistently correspond to a submitted frame, even when a configuration change fails. This makes it easier to identify which frame succeeded or failed.
    • When a configuration change fails and no frame is waiting, the renderer no longer reports a frame acknowledgement. It stops accepting submissions until recovery, after which frames can be presented normally.
    • Acknowledgements are matched by frame token rather than arrival order, improving handling when multiple results are pending.

…e failure

The presenter worker acknowledged a failing `configure` that had no frame
waiting with the reserved `PRESENT_NON_FRAME_TOKEN` (`u64::MAX`) sentinel.
`configure`/`resize` are sent on their own tick (opacity adoption, resize
reflow), so that is the ordinary shape of a configure failure: the synthetic
ack could therefore be the first ack a caller observed, ahead of the ack for
a frame submitted moments later, and a frame submitted after the worker
decided to exit was dropped without any ack at all.

Ack ownership decision (recorded in the module docs, the configure branch and
`PresentAck::token`): the worker owns every ack, and an ack always names the
submitted frame it describes. A configure that carried a frame still
acknowledges *that frame's* token as `error` (a frame built for the old extent
must never be presented). A configure that carried no frame is not a frame
outcome and publishes no ack; the worker exit is its signal, which the runtime
already handles by retaining the damage and recovering (`is_active() == false`
branch). `PRESENT_NON_FRAME_TOKEN` and the runtime's sentinel branch are
removed rather than kept as a contract nothing may produce.

Tests: acks are now attributed by token. The old-extent test blocks a frame in
the acquire so the failing configure and its frame are taken together (the
other ordering is a separate case), drains both acks in one pass and finds
each by token; a new test asserts a frame-less configure failure publishes no
ack, leaves the ring empty, and makes the following `submit` refuse the frame.
@Xuepoo Xuepoo added this to the v0.1.0 milestone Nov 7, 2026
@Xuepoo Xuepoo added fix Bug fix area:render Area: rendering P2 Priority: medium labels Nov 7, 2026
@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Configure failures now produce an error acknowledgement only when the presenter worker has taken a frame. The acknowledgement uses that frame’s token. Runtime handling and tests associate acknowledgements by token.

Changes: Presenter acknowledgement flow

Layer / File(s) Summary
Frame acknowledgement contract
crates/bitty-render/src/present.rs
The worker publishes an error acknowledgement for a taken frame when configuration fails. If it took no frame, it publishes no acknowledgement and exits. Documentation states that acknowledgement consumers must match by token.
Runtime acknowledgement handling
crates/bitty-runtime/src/runtime/present.rs
Runtime handling removes the special non-frame-token branch. Its documentation describes token attribution and worker exit when a configure failure occurs without a frame.
Configure-failure validation
crates/bitty-render/src/present/tests.rs
Tests match acknowledgements by token and check configure failures with and without a waiting frame, including worker exit and recovery.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related issues

Possibly related PRs

  • bitty-terminal/bitty#2904: That PR introduced the presenter worker and non-frame-token acknowledgement; this change updates that acknowledgement contract and removes the sentinel handling.

Suggested labels: area:runtime, bug

Merge Risk: Minimal · up to 33787

No actionable merge-blocking issue is established. Complete the pending macOS ARM64 acceptance runs through CI.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly identifies the main change: the presenter no longer publishes a tokenless acknowledgement after a configure failure.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 2 files. (1 skipped: 1 too large.)

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 60369076-bfca-48e0-8518-90536c303914
Commits

Reviewing files that changed from the base of the PR and between 4f32fd8 and 3378741.

Files selected for processing: 3

  • crates/bitty-render/src/present.rs
  • crates/bitty-render/src/present/tests.rs
  • crates/bitty-runtime/src/runtime/present.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@Xuepoo

Xuepoo commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

CI status on head 33787414 at hand-off (not a merge request, just the observed state):

Quality gates (1)          pass   15m19s
Quality gates (2)          pass   12m10s
macOS ARM64 check and test pass   20m28s   <- the leg that flaked in #2980, green here (run 1 of the 10 the acceptance asks for)
Linux X11 (xvfb)           pass   15m33s
Linux Wayland             pass   13m19s
MSRV 1.85 check           pass    1m12s
Supply chain (deny/audit)  pass     26s
Analyze (rust)/(actions)   pass   13m51s / 44s
Linux X11|Wayland compat matrix, VM tier (PR), Extension pin drift  pass
Windows check and test     pending

The macOS acceptance item ("10 consecutive macOS ARM64 runs of present::tests green") is therefore 1 of 10 — the reviewer/guard decides whether the run history satisfies it. Hence Part of #2980, not Closes #2980.

@Xuepoo Xuepoo left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent guard-lane review — PR #2990 (head 33787414)

Reviewed independently of the authoring lane: I read the diff, the PR body, and the touched code in a separate worktree (.worktrees/ctx-1735-present-ack), and re-ran the focused gates myself against CARGO_TARGET_DIR=.targets/ctx-1735. I did not modify, commit, or push the branch.

The change is small and the interesting question is not "does it compile" but "does deleting the sentinel silently drop a production state transition". It does not: the transition the sentinel branch performed (pending_full_redraw = true) is performed unconditionally by the inactive branch of the same poll_presenter call, and the dead-worker wake still happens through mark_exited.

Checks

1. Ownership claim — PASS.
Only two ack producers exist, both inside worker_loop and both carrying a real frame token:

  • crates/bitty-render/src/present.rs:745-750 — configure failure, token: frame.token, only under if let Some(frame).
  • crates/bitty-render/src/present.rs:785-788 — present attempt, token: frame.token.

WorkerShared::push_ack is private (present.rs:567) and is the only writer to the ring; PresentAck.token is a plain u64 with no reserved value (present.rs:387-393); drain_acks (present.rs:1141) is the only reader and the runtime attributes by token (crates/bitty-runtime/src/runtime/present.rs:7403-7409). grep -rn PRESENT_NON_FRAME_TOKEN over the tree (excluding target/, .git/) returns nothing. The doc comment's claim is true of the code, not just of the commit message.

2. Deleted PRESENT_NON_FRAME_TOKEN — PASS.

  • No consumer is unaccounted for: zero remaining references anywhere (rs and md).
  • Configure-failure recovery still retains damage and requests a full redraw: runtime/present.rs:7298-7302 (inactive branch) sets pending_present = None; pending_full_redraw = true, and it runs in the same poll_presenter invocation that drained the acks (7229-7237), before recover() (7313-7332).
  • A dead worker still wakes the loop: mark_exited (present.rs:591-612) stores stop = true and fires the wake callback, so is_active() (941-943) goes false, submit refuses (1105-1107), and Runtime::set_present_waker (runtime.rs:2892-2897) has the callback installed.
  • No now-dead state: the deleted branch's entire effect was pending_full_redraw = true (it discarded the detail), which the inactive branch sets unconditionally — the old branch is a strict subset, so nothing became unreachable. clippy -p bitty-render -p bitty-runtime --all-targets -- -D warnings is clean, so no dead_code.
  • Note (non-blocking): the const was pub in a crate with publish = true; its removal is an API-surface change worth a release note.

3. Behavioural contract — PASS.

  • Frame taken with the failing configure: acked Error("configure failed: …") with that frame's token, and the return at present.rs:751 precedes the present path (756-788), so the old-extent frame is never presented. Test asserts control.presented() == 1 (frame 0 only).
  • No frame: nothing is pushed; the exit is the signal. The runtime then refuses the next submit and retains damage at runtime/present.rs:7827-7832, and the inactive branch clears pending_present and forces the redraw.
  • No production caller can block on an ack that will never arrive: the only production consumer is poll_presenter (runtime/present.rs:7232), which drains at most PRESENT_ACKS_PER_TICK and never waits. drain_until/wait_until/drain_token exist only in present/tests.rs (307-318, 320-330, 344-358) and all are deadline-bounded. There is no recv/wait/blocking-read on the ack ring anywhere. The stuck branch is also unreachable as a substitute wait: inactive is evaluated after the ack drain and before stuck (7298 vs 7347).

4. Test discrimination — PASS (with nits).

  • a_configure_failure_never_presents_the_old_extent_frame is deterministic, not just "less racy": Gate::enter is called inside FakeBackend::present (tests.rs:225, 51-58), wait_entered(1) therefore proves frame 0 is already inside the acquire, the failing configure and submit(frame 1) both happen before release() (tests.rs:799-819), and the worker takes frame+configure atomically (present.rs:697-716). Both acks are then matched by token (tests.rs:823-839), with acks.len() == 2 and queued_acks() == 0 after exit (851-855).
    Red if: frame 1 were presented (presented() == 2), acked under a token other than second, an extra ack appeared (len != 2 or queued_acks() != 0), or the recovered worker did not present. It would not go red if the sentinel emission were restored — the taken frame is Some in that scenario, which is exactly why the second test exists.
  • a_configure_failure_without_a_frame_publishes_no_ack: red iff any ack is synthesised on the no-frame path — restoring map_or(PRESENT_NON_FRAME_TOKEN, …) makes queued_acks() (tests.rs:884-890) 1 instead of 0. The scenario is genuinely frame-less (nothing is ever submitted; the worker takes the configure alone).
  • Nits: assert_eq!(second_ack.token(), second) (tests.rs:835-839) is tautological given the find predicate; "never presented" is proven by present count, not by extent.
  • Empirically deterministic here: 8/8 consecutive runs of both tests green, and the full present::tests set 23/23.

5. Race soundness — PASS.
The ordering window is removed, not hidden: after this change there is no ordering between a configure's failure and an ack at all — the failing configure either acks the frame taken in the same lock acquisition (present.rs:697-716) or acks nothing. One residual ordering remains, and it is pre-existing and handled: between worker_loop's return (751) and mark_exited (691) — deliberately after the backend drop (present.rs:678-691) — is_active() is still true, so a submit in that window returns Some and its frame is absorbed without an ack. That is bounded by surface teardown (previously it extended indefinitely past the exit decision), and poll_presenter's inactive branch clears pending_present and forces the full redraw (7298-7302), so nothing is committed and no pixels are lost. The contract is therefore well defined: an ack always names a frame the worker took; a submission either gets its own ack or is covered by the exit signal, which the runtime treats as "retain everything".

6. MSRV 1.85 — PASS.
cargo +1.85 check -p bitty-render -p bitty-runtime --tests --offline --locked exits 0. The only new conditional is if let Some(frame) = frame.as_ref() (present.rs:745) — no let-chain. No const-eval-only constructs, no new cfg gates.

7. Hygiene — PASS.
English only, no host-specific values (test extents are synthetic), no new allow(...), no secrets. rustdoc: I forced fresh doc builds (cargo rustdoc -p bitty-render -- -D warnings, same for bitty-runtime), both exit 0, so the new module-doc link [PresentAck::token] (present.rs:56) resolves and no private item is linked.

  • Nit: present.rs:66 calls it "the one-ack-per-submitted-frame mapping", but coalescing (present.rs:1113-1119) means a replaced submission is never taken and never acked. The invariant that actually holds — and the one tests.rs:335 states correctly — is one ack per frame the worker took. Worth aligning the normative paragraph, since "every submission must be acked" is exactly the misreading that produced #2980.
  • Nit: present.rs:57-58 and 1137-1140 say a superseded frame's ack can arrive after a newer one. With one serial worker per generation and a synchronous push_ack before the next take, acks in one ring are monotonic in token; the real defect was a tokenless ack, not reordering. Harmless defensive wording, but the justification is theoretical.

8. Scope — PASS.
Three files, +177/−38 against 4f32fd8a (crates/bitty-render/src/present.rs, present/tests.rs, crates/bitty-runtime/src/runtime/present.rs), all presenter ack plumbing. No Cargo.toml/Cargo.lock change, no dependency edge, no unrelated behaviour.

9. Honesty of the PR body — PASS.
The body admits just ci-local never reached the code gates (nix cannot resolve a git worktree gitdir inside the act container; sccache --show-stats exit 127), reports the two nextest soak budget failures and that they pass in isolation, and states plainly that the "10 consecutive macOS ARM64 runs" were not produced, keeping Part of #2980 rather than Closes. Nothing in the body cites evidence the diff does not support. Two caveats, both conservative rather than overclaims: (a) "the next submit is refused instead of being absorbed by a dead worker" holds only after mark_exited (see check 5); (b) CI now shows the macOS ARM64 leg green on this head — one run, not the ten the issue asks for; Windows is still pending.

Gates re-run in this lane

CARGO_TARGET_DIR=/mnt/data/Workspace/Projects/bitty-terminal/.targets/ctx-1735, in the PR worktree:

command result
cargo fmt --all -- --check exit 0
cargo test -p bitty-render --lib present::tests 23 passed, 0 failed
8× cargo test -p bitty-render --lib present::tests::a_configure 8/8 green (2 passed each)
cargo test -p bitty-runtime --lib -- runtime::present 151 passed, 0 failed
cargo clippy -p bitty-render -p bitty-runtime --all-targets -- -D warnings exit 0
cargo +1.85 check -p bitty-render -p bitty-runtime --tests exit 0
cargo rustdoc -p bitty-render / -p bitty-runtime -- -D warnings (forced) exit 0

Findings

  • No functional defect found. The sentinel's only production effect is preserved by the inactive branch, the dead-worker wake/is_active transition is intact, and no consumer waits on an ack.
  • Three non-blocking nits: the "one ack per submitted frame" phrasing in the normative module doc (coalescing makes that false as written), the out-of-order-arrival justification, and a tautological assertion in the reworked test.
  • One residual, pre-existing window documented rather than fixed: a submit between the worker's loop return and mark_exited (after the backend drop) is accepted and never acked; the runtime covers it via inactive → retain + full redraw, so it is safe, but the PR body's "next submit is refused" is only true after the exit marker.

Approving: the fix is correct, the deletion is not silent, and the new frame-less test is the one that would have caught #2980 deterministically.

VERDICT: APPROVE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:render Area: rendering fix Bug fix P2 Priority: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant