Skip to content

test(relay): check invariant 4 (one demotion winner) in the step-5 simulation - #28696

Merged
Jinwoo-H merged 5 commits into
mainfrom
relay-reserve-sim-invariant-4
Oct 14, 2026
Merged

Jinwoo-H merged 5 commits into
mainfrom
relay-reserve-sim-invariant-4

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​45 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​44
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​121 $\color{#cf222e}{\Huge{\mathbf{−}}}$​20 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​101

ELI5

The relay test that replays many desktops connecting through five directors at once is supposed to check the design's safety rules. Rule 4 says: when a desktop ends up connected to two cells, every director must agree on which connection survives, so the desktop never loses both. Nothing in the test checked that rule. Now it does, and a deliberately broken version of the rule makes the test fail.

Building the test for rule 4 also showed that rule 3 (an old duplicate connection closes quickly) is promised too strongly in the design. Safety still holds; the promise needs a longer time limit in one case. This PR models that case and the design doc needs the same correction.

What Changed

  • Before: the main test was titled "invariants 1-3 and 5-8". Rule 4 had no check, so a director that picked a different survivor than the others would have gone unnoticed.
  • After: every time a director judges a duplicate (when it asks a cell to close one, or when its map has not yet seen another director's close of the same seat), it records which connection it keeps. Rule 4 is reported broken if two directors keep different connections for the same pair, or if both connections of a desktop end up closed, each in favour of the other.
  • The hard case is a desktop on two cells at the same number (epoch). There, the database row decides the survivor, and rule 4 can only break if two or more directors judge the same pair. The main test's settings do produce same-number closes, but never one that two directors judge. A new test uses settings that do (short sessions, half-open connections, all five directors restarting each minute so their start-up checks overlap). Counting only same-number pairs (a pair where one side has a higher number cannot split, since every director keeps the higher one), it reaches 10, 15 and 7 contested pairs over seeds 11, 23 and 47; the main test reaches 0 in every seed. The new test requires more than zero and asserts every rule, not only rule 4.
  • A new fault, localTieBreak, makes each director break a same-number tie by its own cell order instead of the row. The fault table shows rule 4 then fires.
  • Rule 3 is narrowed to match production (see below), and the model's silence-close time now matches production: 75 s silence (host-session-registry.ts heartbeat) plus one 15 s ping (RELAY_PROTOCOL_LIMITS.controlPingIntervalMs), 90 s, not the 100 s the model used before.
  • Violations are now capped per rule (50 each) instead of 50 in total, so a flood of one rule cannot hide another. This was hiding the rule-4 report in the fault run.
  • The main test is retitled "invariants 1-8", with a comment that rule 9 is covered by its own tests (the main run has no dead-man trips).
  • A reconcile whose map has not yet seen another director's close still raises that director's epoch floor, as production does on every row-judged close (relay-server.ts calls raiseEpochFloor). Rule 8 still holds in every run.

Rule 3 finding: a dead control, not a second owner

With the new settings and no fault, the old rule-3 check failed. The sequence also happens in production:

  1. A desktop joins cell A, and its connection then goes half-open (the desktop moved on; the cell still holds the seat).
  2. A director whose map has not yet seen that join books the desktop on cell B at a higher number.
  3. Supersede (reserve-assignment.ts, around lines 395-417) only closes the seats that director's map holds, so A is never told to close.
  4. A reserve seat has no database lease to re-check, the ledger reconcile runs every 10 minutes and skips seats younger than 30 s, so A closes only by the cell's control silence timeout: 75 s without a pong, checked once per 15 s ping, so up to 90 s.

This is a dead connection, not two owners: a desktop holds one control connection at a time (it left A when it connected to B), and the sticky answer only names the newest number, so nothing routes to A. Safety holds; only the time bound is wrong.

The model's rule 3 now says: a seat the booking director's map held at booking time closes within that director's poll lag plus a round trip (as before); a seat that map had not seen gets the silence-close bound (90 s plus round trips). With that bound every rule holds in every run, and turning the narrowing off makes rule 3 fire again in all three seeds.

The design doc needs two different corrections. Section 9, invariant 3 needs a wording fix only: it calls the old seat a live duplicate, but a half-open seat arguably is not live. Section 3.5 describes the right mechanism (supersede at placement, silence close otherwise); what it lacks is the time bound for a seat the booking director had not seen, which is the silence close (up to 90 s plus round trips), not the poll lag plus a round trip.

Optional production improvement, not in this PR: when a director's feed shows a reserve seat outranked by a join it already knows on another cell, it could close that seat then, instead of waiting for the silence timeout.

Why

The rule-4 check sits where directors judge duplicates: at placement (the newer booking wins) and in the ledger reconcile (the row wins). Those are the only places that name a winner, so recording it there covers every judgement without a second model of the rules. A reconcile whose map is behind still records its judgement even though the cell would drop the duplicate close; that is what a real director does, since it reads its map, not the cell. I considered flagging any desktop whose seats are all closed, but that is normal when a third, newer seat outranks both, so the check works on pairs instead.

Linked Issue

Follows #27040.

Visual Proof

N/A: test-only change to a simulation, no UI or runtime behaviour.

Testing

  • vitest run src/reserve-placement-simulation.test.ts --maxWorkers=4 in cloud/apps/relay: 28 of 28 pass, including the contested-tie runs (seeds 11, 23, 47, all rules) and the rule-4 fault.
  • With the rule-3 narrowing disabled, the contested-tie runs report rule-3 violations in all three seeds (checked locally, not committed).
  • tsc --noEmit reports no errors in the changed files.
  • Automated tests added/updated

AI Disclosure

Review

Two Opus reviews (design, bugs). Fixed: rule-3 bound narrowed for unseen seats; contested count limited to same-number pairs and required only on the new settings; map-judged closes raise the epoch floor; tie-break rule factored into one helper; main test retitled "invariants 1-8".

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Test-only; no production code, wire, SSH or platform impact.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…mulation

Every demotion now names its winner (the newer booking, or the row). The model
flags a director that names a different winner for a divergence another already
named, and a host whose two seats are each demoted for the other. A fault where
directors break level ties by their own cell order shows the check fires.
Violations are capped per invariant so one flood cannot hide another class.

Claude-Session: f2fe8cb5-4467-4a25-8aab-98598b70791f
@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: deb5cdc2-cdc7-498a-89cb-93488f7a4d55
📥 Commits

Reviewing files that changed from the base of the PR and between 5c596c8 and 41c320d.

📒 Files selected for processing: 2
  • cloud/apps/relay/src/reserve-placement-simulation.test.ts
  • cloud/apps/relay/src/reserve-placement-simulation.ts
🚧 Files skipped from review as they are similar to previous changes: 1
  • cloud/apps/relay/src/reserve-placement-simulation.test.ts

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

📝 Walkthrough

The simulation now derives its silence-close timeout from the configured control-ping interval. It routes demotions through shared winner judgment and reports equal-epoch demotions and contested divergences. Duplicate-seat checks use the booking director’s recorded held seats to select a bound. Tests add a seeded level-seat scenario and include localTieBreak in the invariant 4 fault case.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 41c32

This change only extends a relay placement simulation and its tests, with no production behavior change. No actionable merge-blocking risk remains after the earlier review feedback was addressed.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies the main change: adding invariant 4 coverage to the relay step-5 simulation.
Description check Passed The description is complete and directly explains the change, motivation, linked issue, testing, scope, and checklist status. It also documents the rule-3 finding and the updated simulation bounds.
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 2
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • 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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c8af369e-15b4-4747-97da-a65d3f5ec707
📥 Commits

Reviewing files that changed from the base of the PR and between 9c0420f and 21b98af.

📒 Files selected for processing: 2
  • cloud/apps/relay/src/reserve-placement-simulation.test.ts
  • cloud/apps/relay/src/reserve-placement-simulation.ts

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

…all invariants on contested ties

A reserve seat the booking director's map had not seen (joined, then half-open within a poll)
is never superseded and has no lease to re-check, so it lasts until the cell's control silence
close (75 s + one 15 s ping). Invariant 3 now uses that bound for such seats, and the
level-divergence config asserts every invariant.

Stale maps now judge a divergence another director already demoted, so the config's
"contested" counter (divergences judged by two or more directors) is the case invariant 4
guards. The silence-close constant now matches production (90 s, not 100 s).

Claude-Session: f2fe8cb5-4467-4a25-8aab-98598b70791f

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7e619916-3692-4d6f-aaea-895f2a95a76f
📥 Commits

Reviewing files that changed from the base of the PR and between 21b98af and 5c596c8.

📒 Files selected for processing: 2
  • cloud/apps/relay/src/reserve-placement-simulation.test.ts
  • cloud/apps/relay/src/reserve-placement-simulation.ts

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

…el contests

A reconcile whose map has not seen another director's demotion still demotes in
production, which raises its epoch floor; the model now does too. The contested
counter counts only same-epoch pairs, the only ones invariant 4 can split. The
keep-this-seat rule is one helper.

Claude-Session: f2fe8cb5-4467-4a25-8aab-98598b70791f
…fault

Claude-Session: f2fe8cb5-4467-4a25-8aab-98598b70791f
…gment, and bound duplicates by placement records alone

Claude-Session: f2fe8cb5-4467-4a25-8aab-98598b70791f
@Jinwoo-H
Jinwoo-H merged commit fa5533e into main Oct 14, 2026
25 of 27 checks passed
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.

1 participant