Skip to content

feat(id): coordinator state machine + recovery for region-seam handoff (#3903) - #9000

Merged
dezren39 merged 3 commits into
mainfrom
queue/5866-3903-coordinator
Nov 13, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/5866-3903-coordinator

Conversation

@dezren39

@dezren39 dezren39 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Phase 4 of #3870 (issue #3903, task 5866), per design #2818 sections 5/6 and phased plan packet 4. Gate re-checked: phase 3 issue #3902 is CLOSED.

Adds pkgs/id/crates/id-worlds/src/world/coordinator.rs, the coordinator state machine on the phase-1 CoordinatorJournalEntry records:

  • start_handoff appends HandoffStarted before any side effect; handoffs serialized per entity; a handoff ID is never reused.
  • observe_export / observe_import correlate receipts by handoff_id + entity + source/target + prior epoch + module + schema + digest; duplicate receipts are idempotent, conflicting digests are integrity errors; import can never be observed before the export, and completion is never inferred from one side alone.
  • complete appends HandoffCompleted before switching the owner map; owner epoch and region-map epoch each increment exactly once.
  • cancel is legal only pre-export with proof no export exists; no post-export rollback.
  • recover replays the journal deterministically and rejects impossible journals; reconcile re-queries both receipts after a restart, appends missing observations, and completes only when both verify.
  • in_transit_status exposes the section 4 status-only response with the 60s stuck classification (Exports/ImportedAwaitingCoordinator/Stuck); a started handoff with no export stays at_source.

Tests: 13 new coordinator tests including restart-at-each-transition recovery, reconcile-from-both-receipts, one-side-alone refusal, cancel rules, and stuck classification. Full id-worlds lib suite: 729 passed, 0 failed. cargo clippy on the new module: no findings; rustfmt clean (the crate carries pre-existing fmt diffs in other files, untouched).

Client routing (packet 5) remains a separate follow-on; nothing here enables the handoff path end-to-end (per the phased plan, no packet before 5 does).

Closes #3903 on merge.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@dezren39
dezren39 requested a review from a team as a code owner November 12, 2026 19:31
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 23 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4e1a7ed7-243e-4ec2-80a3-b640e55ebae1
Commits

Reviewing files that changed from the base of the PR and between a5318f8 and 39bae6a.

Files selected for processing: 2

  • pkgs/id/crates/id-worlds/src/world/coordinator.rs
  • pkgs/id/crates/id-worlds/src/world/mod.rs
  • 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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T21:48:14.744475Z 39bae6a New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@dezren39 dezren39 added work/assigned Queue worker lifecycle state queue/claimed Queue worker lifecycle state status/in-progress Queue worker lifecycle state labels Nov 12, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d38afd67ca

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +403 to +406
if self.active_by_id(handoff_id).is_some() {
return Err(CoordinatorError::InvalidTransition(
"handoff id is already in flight for another entity",
));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject handoff IDs used by completed or cancelled transfers

After completion or cancellation removes a handoff from active, this check allows its ID to be reused. For example, completing A→B and starting B→A with the same ID succeeds here, but the shard transfer ledgers retain historical IDs for deduplication (WorldCore::validate_entity_export and validate_entity_import), so the new crossing conflicts with the previous transfer and cannot progress. Track previously used IDs across terminal transitions and recovery, and reject reuse before appending another start.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in dc3f9af: the coordinator now tracks every handoff ID it has ever started (used_handoff_ids, populated in apply_started so live starts and journal replay share it). start_handoff rejects a previously used ID with InvalidTransition, and replay rejects a journal that reuses an ID with Integrity. Covered by handoff_ids_are_single_use_across_terminal_transitions and the model property test ID-uniqueness invariant.

Comment on lines +631 to +635
if let Some(receipt) = source_receipt {
self.observe_export(receipt, observed_at)?;
}
if let Some(receipt) = destination_receipt {
self.observe_import(receipt, observed_at)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Correlate reconciliation receipts with the requested handoff

When two handoffs are active, reconcile(A, receipt_for_B, ...) passes the receipt to observation methods that select their own active handoff using the receipt's ID. Consequently, reconciliation for A can journal progress for B and return an outcome for A without reporting any mismatch. Validate both supplied receipts against the requested handoff_id before applying either observation, so receipts from concurrent recovery queries cannot mutate another transfer.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in dc3f9af: reconcile now validates both supplied receipts against the requested handoff_id up front and returns ReceiptMismatch(handoff_id) before applying either observation, so a receipt for a concurrent handoff can no longer mutate it. Covered by reconcile_rejects_receipts_for_another_handoff.

Comment on lines +741 to +743
}
self.apply_started(entry);
Ok(())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Validate replayed starts against recovered ownership

If a journal contains a stale or corrupted start, recovery checks only whether the entity already has an active handoff; it never verifies the source region, expected owner epoch or region-map epoch against the recovered map. A start claiming owner epoch 1 against a seeded owner at epoch 10, followed by otherwise matching observations and completion, is accepted and overwrites ownership with epoch 2. Validate these correlations and the live start preconditions before applying the entry, returning Integrity rather than allowing recovery to roll ownership backward.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in dc3f9af: replay of HandoffStarted now validates the entry against recovered ownership before applying it - the entity must be a known owner and its region, owner epoch, and the entry region-map epoch must match the recovered map exactly (and source != target); any disagreement is Integrity. The completion replay check also verifies the source region. Covered by replay_rejects_a_start_that_disagrees_with_recovered_ownership.

Comment on lines +1102 to +1106
// after every protocol transition and rebuild from the journal.
for prefix in 1..=4 {
let live = match prefix {
1 => started(),
2 => exported(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add generated invariant tests for the coordinator

The new state machine is covered only by hand-written examples, including this fixed four-prefix restart test. Root AGENTS.md explicitly requires property tests for state-machine invariants and model-generated tests when a behavioral specification exists; examples alone do not satisfy that requirement. Add generated transition sequences and crash prefixes, checked against a reference model, covering replay equivalence, monotonic ownership epochs and handoff-ID uniqueness. These combinations are particularly important because the current examples miss terminal-ID reuse and inconsistent replayed starts.

AGENTS.md reference: AGENTS.md:L38-L42

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in dc3f9af: added world::coordinator::model_tests, a proptest model-based suite generating transition sequences (starts, export/import observations with digest faults, completions, cancels, crash-recoveries, schema-mismatch probes) over a two-entity/three-ID universe, checked step-by-step against a reference model. Invariants asserted throughout: replay equivalence (recover of the live journal equals the live coordinator, including after every generated crash), monotonic owner and region-map epochs, and handoff-ID uniqueness; every crash prefix of the final journal is replayed as well. 17/17 coordinator tests pass locally.

@dezren39 dezren39 added queue/parked Queue task is parked (waiting on CI, time, or an event) status/parked Deliberately paused with a recorded resume condition and removed status/in-progress Queue worker lifecycle state queue/claimed Queue worker lifecycle state labels Nov 12, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dc3f9af41a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +825 to +827
new_owner_epoch,
new_region_map_epoch,
..

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Validate completion module and schema during replay

When a recovered HandoffCompleted entry has a different program_module_hash or payload_schema from its journaled start, replay ignores both fields and switches ownership anyway. This accepts an inconsistent journal that the live complete() path could never produce, rather than stopping recovery with CoordinatorError::Integrity. Compare both completion fields with the active handoff before applying the ownership change.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 39bae6a: replay of HandoffCompleted now binds the entry's program_module_hash and payload_schema and compares both against the active (journaled-start) handoff before applying the ownership switch; any disagreement returns CoordinatorError::Integrity, matching what the live complete() path could produce. Covered by the new test replay_rejects_completion_with_mismatched_module_or_schema (both fields, independently). Coordinator suite: 18/18 pass locally (cargo test -p id-worlds --lib world::coordinator, pkgs/id workspace).

@dezren39 dezren39 removed status/parked Deliberately paused with a recorded resume condition queue/parked Queue task is parked (waiting on CI, time, or an event) labels Nov 13, 2026
@dezren39
dezren39 merged commit 7904547 into main Nov 13, 2026
20 of 26 checks passed
@dezren39
dezren39 deleted the queue/5866-3903-coordinator branch November 13, 2026 00:32
@dezren39 dezren39 removed the work/assigned Queue worker lifecycle state label Nov 13, 2026
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.

id: implement #3870 phase 4 — coordinator state machine + recovery for region-seam handoff

1 participant