schema: lattice.agents/procedure/v1
id: replication-reseed
name: "Re-seed a peer from a replication snapshot"
surfaces:
  in_process:
    api: "ILatticeReplicationAdmin.RequestSnapshotAsync / ForceRequestSnapshotAsync and ILatticeBootstrapCoordinator.BootstrapAsync"
    source: "src/lattice.replication/ILatticeReplicationAdmin.cs#ILatticeReplicationAdmin.RequestSnapshotAsync"
preconditions:
  - id: surface-available
    check: "Read api/replication.json availability. Re-seed and bootstrap status are host-code in-process operations, not MCP or operator gRPC tools; the remote snapshot gRPC service is the internal data transport."
    source: "docs/agents/api/replication.json"
  - id: source-cluster-selected
    check: "The operator names a non-empty source cluster id to produce the snapshot."
    source: "src/lattice.replication/ILatticeReplicationAdmin.cs#RequestSnapshotAsync"
  - id: source-tree-enrolled
    check: "The sender-side snapshot service re-resolves the requested tree against local replication enrollment before any tree data is read."
    source: "docs/lattice.replication/snapshot-bootstrap.md"
  - id: fall-off-detected-or-reseed-needed
    check: "Fall-off exists when the receiver high-water mark is strictly less than the sender oldest retained WAL HLC, or an operator has chosen re-seed for a new peer or recovery."
    source: "docs/lattice.replication/auto-bootstrap.md"
  - id: hidden-entries-remediation
    check: "A tree whose WAL is on Azure Table and that replicated under Orleans.Lattice.Storage.AzureTable 10.0.0-10.2.0 may have peers that never received entries a stale cached trim watermark hid from the shipper (issue #6036); after upgrading, re-seed every peer it replicates to unless anti-entropy is enabled to localise and repair divergence. A nonzero outcome=recovered on orleans.lattice.replication.ship.holes_skipped shows the shipper repairing such a range as it ships."
    source: "docs/lattice.storage.azuretable/architecture.md"
  - id: transport-authenticated
    check: "The remote snapshot gRPC calls carry an accepted shared secret and the origin header required by transport security."
    source: "docs/lattice.replication/transport-security.md"
steps:
  - n: 1
    action: "For a suspected retention gap, compare the receiver high-water mark with the sender oldest retained HLC."
    api: "ILatticeFallOffLogDetector.CheckAndTriggerAsync"
    expects: "FellOffLog is true only when incremental replication cannot bridge the gap."
    source: "src/lattice.replication/LatticeFallOffLogDetector.cs#LatticeFallOffLogDetector.CheckAndTriggerAsync"
  - n: 2
    action: "Request a routine operator re-seed, or use the force bypass when disaster recovery or scheduling requires skipping the rate limit."
    api: "ILatticeReplicationAdmin.RequestSnapshotAsync / ForceRequestSnapshotAsync"
    expects: "Routine re-seed can return Triggered false with RetryAfter; force returns Triggered true when the coordinator call succeeds."
    source: "src/lattice.replication/LatticeReplicationAdmin.cs#LatticeReplicationAdmin.RequestSnapshotAsync"
  - n: 3
    action: "Poll bootstrap status until LiveIncremental and unfenced. If Failed, inspect ReadFenced and whether a shadow was already published before choosing recovery; legacy fenced and published-shadow completion attempts automatically re-drive, so Failed alone is not a terminal safe serving state."
    api: "ILatticeBootstrapCoordinator.GetStatusAsync / GetStateAsync"
    expects: "LiveIncremental with ReadFenced false means the complete snapshot is published and incremental replication is live. Inspect EntriesApplied, RedriveAttempts and CompletedSourceClusterId; Triggered true alone is only kickoff."
    source: "src/lattice.replication/ILatticeBootstrapCoordinator.cs"
postconditions:
  - check: "Fresh attempts build a shadow copy and atomically publish it after a complete drain, preserving the prior serving view; legacy partially imported read-fenced attempts resume in place and retain the fence until completion."
    source: "src/lattice.replication/Grains/LatticeBootstrapCoordinatorGrain.cs"
  - check: "Handoff merges the exported frontier by pointwise maximum and clears the legacy pinned floor. Exact identity dedup and per-key merge absorb duplicates; a genuinely new point write is not dropped solely because its HLC is older than a snapshot coordinate."
    source: "src/lattice.replication/Grains/LatticeBootstrapCoordinatorGrain.cs#PinAndCompleteAsync"
  - check: "An applier-deferred snapshot row fails the attempt before handoff; no frontier is sealed past an unapplied row."
    source: "src/lattice.replication/Grains/LatticeBootstrapCoordinatorGrain.cs#DrainSnapshotAsync"
  - check: "Reaped source deletes reconcile only with stable known source generation, aligned lineage and unscoped LwwRegister export; uncertainty keeps rows and records an owed retry. Receiver-origin rows are never inferred deleted from a foreign snapshot."
    source: "src/lattice.replication/Grains/LatticeBootstrapCoordinatorGrain.cs"
  - check: "The source export lineage is recorded durably at drain open; stale-lineage pushes are refused. Do not treat skipped delete reconciliation as proof that old rows were removed."
    source: "src/lattice.replication/Grains/LatticeBootstrapCoordinatorGrain.cs#RecordDrainedLineage"
  - check: "A fall-off probe for the same source while bootstrap is active is suppressed and counted separately."
    source: "docs/lattice.replication/auto-bootstrap.md"
failure_modes:
  - id: fall-off-log-detected
    signal: "peer.fell_off_log increments or FallOffLogDecision.FellOffLog is true."
    effect: "Incremental replication cannot catch the peer up from retained WAL alone."
    response: "Allow auto-bootstrap to run or invoke operator re-seed from a suitable source cluster."
  - id: auto-bootstrap-disabled
    signal: "FallOffLogDecision.FellOffLog is true and BootstrapTriggered is false."
    effect: "The receiver remains unable to bridge the retained-WAL gap automatically."
    response: "Run RequestSnapshotAsync or ForceRequestSnapshotAsync manually."
  - id: rate-limited
    signal: "RequestSnapshotAsync returns Triggered false with RetryAfter."
    effect: "No bootstrap coordinator call was started by that request."
    response: "Wait RetryAfter, or use ForceRequestSnapshotAsync when bypass is operationally justified."
  - id: conflicting-bootstrap
    signal: "BootstrapAsync or the admin seam throws InvalidOperationException for another in-flight source cluster."
    effect: "Only the existing source owns the in-flight bootstrap for the tree."
    response: "Wait for it to finish or fail before retrying from a different source."
  - id: origin-binding-or-authorization-denied
    signal: "Remote snapshot gRPC returns PermissionDenied for bad shared secret, missing or mismatched origin binding, or sender-side enrollment refusal."
    effect: "No snapshot metadata or entries are exported."
    response: "Fix transport secrets, origin binding, endpoint mapping, or source-tree enrollment before re-requesting the snapshot."
  - id: unreachable-peer
    signal: "Remote snapshot transport returns Unavailable, DeadlineExceeded, Aborted or an equivalent transient network fault."
    effect: "The drain retries within the transient budget, then the bootstrap transitions to Failed if the budget is exhausted."
    response: "Restore peer reachability. An unpublished failed shadow is discarded without exposing a partial copy; a published shadow handoff or legacy fenced partial import re-drives automatically. Inspect status before requesting another source."
  - id: surface-not-available
    signal: "The host-code admin or bootstrap interface cannot be resolved, or the source's internal remote-snapshot transport is not mapped."
    effect: "The operation cannot be carried out through that surface on this host."
    response: "Stop and report which registration or opt-in the availability block names as missing; do not emulate the operation through a different surface."
rollback:
  supported: false
  mechanism: "No undo exists after publication. Before publication a failed shadow is discarded and the prior serving view remains; after publication re-seed from the correct source or restore from backup. ForceLiftBootstrapReadFenceAsync is an alarmed host-code-only override, not rollback: it stops legacy re-drive and can expose a partial import."
  steps: []
  source: "docs/lattice.replication/snapshot-bootstrap.md"
concurrency:
  - rule: "Only one bootstrap for a tree runs cluster-wide; a same-source concurrent kickoff is a no-op."
    source: "docs/lattice.replication/snapshot-bootstrap.md"
  - rule: "A concurrent bootstrap for the same tree from a different source cluster is rejected."
    source: "docs/lattice.replication/snapshot-bootstrap.md"
  - rule: "Concurrent bootstraps of different trees run on different coordinator activations and can proceed in parallel."
    source: "docs/lattice.replication/snapshot-bootstrap.md"
  - rule: "Remote snapshot transports are safe to invoke concurrently across distinct tree and source-cluster pairs; receivers serialize a single pair through the bootstrap coordinator."
    source: "docs/lattice.replication/snapshot-bootstrap.md"
sources:
  - "src/lattice.replication/ILatticeBootstrapCoordinator.cs"
  - "src/lattice.replication/Grains/LatticeBootstrapCoordinatorGrain.cs"
  - "docs/lattice.replication/snapshot-bootstrap.md"
  - "docs/lattice.replication/auto-bootstrap.md"
  - "docs/lattice.storage.azuretable/architecture.md"
  - "docs/lattice.replication/transport-security.md"
  - "src/lattice.replication/ILatticeReplicationAdmin.cs"
  - "src/lattice.replication/LatticeReplicationAdmin.cs"
  - "src/lattice.replication/LatticeFallOffLogDetector.cs"
  - "src/lattice.replication.grpc/LatticeRemoteSnapshotGrpcMethods.cs"
  - "src/lattice.replication.grpc/LatticeRemoteSnapshotGrpcService.cs"
