schema: lattice.agents/procedure/v1
id: wal-move
name: "Move WAL partition placement"
surfaces:
  in_process:
    api: "ILatticeTreeAdmin.PlanWalMoveAsync, ILatticeTreeAdminOperations.StartWalMoveAsync, ReclaimMovedWalSourceAsync"
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#PlanWalMoveAsync"
  grpc:
    api: "/orleans.lattice.api.treeadmin/PlanWalMove, StartWalMove, ReclaimMovedWalSource"
    source: "src/lattice.api.treeadmin.grpc/LatticeTreeAdminGrpcMethods.cs#PlanWalMoveMethodName"
  mcp_tool:
    api: "lattice_treeadmin_wal_move_plan, lattice_treeadmin_wal_move_start, lattice_treeadmin_wal_move_reclaim"
    source: "src/lattice.api.mcp/Tools/TreeAdmin/TreeAdminToolGroup.cs"
preconditions:
  - id: surface-available
    check: "Every surface this procedure uses is registered on the host: read the availability of api/treeadmin.json and, for each MCP tool used (lattice_treeadmin_wal_move_plan, lattice_treeadmin_wal_move_reclaim, lattice_treeadmin_wal_move_start), its availability in api/mcp.json. An MCP tool must also be listed in the session's tools/list."
    source: "docs/agents/api/treeadmin.json"
  - id: lifecycle-for-move
    check: "Caller has TreeLifecycle authority to start the move and to reclaim."
    source: "docs/lattice.api.treeadmin/README.md"
  - id: read-for-plan
    check: "Caller has Read authority for inspect, audit, and plan."
    source: "docs/lattice.api.treeadmin/README.md"
  - id: target-resolves
    check: "Target provider key resolves on every silo before the move starts."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#PlanWalMoveAsync"
steps:
  - n: 1
    action: "Inspect current placement."
    api: "ILatticeTreeAdmin.GetWalPlacementAsync / GetWalPlacement"
    expects: "Returns default provider, partition placements, placement version, and resolvability."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetWalPlacementAsync"
  - n: 2
    action: "Audit placement against the storage-provider catalog."
    api: "ILatticeTreeAdmin.AuditWalPlacementAsync / AuditWalPlacement"
    expects: "Reports unresolved provider keys and known keys."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#AuditWalPlacementAsync"
  - n: 3
    action: "Plan the partition move."
    api: "ILatticeTreeAdmin.PlanWalMoveAsync / PlanWalMove / lattice_treeadmin_wal_move_plan"
    expects: "Returns copy range, entry estimate, and target resolvability without side effects."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#PlanWalMoveAsync"
  - n: 4
    action: "Start the tracked move."
    api: "ILatticeTreeAdminOperations.StartWalMoveAsync / StartWalMove / lattice_treeadmin_wal_move_start"
    expects: "Returns an operation handle; poll tree-admin operation status through copy, verify, and flip phases."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdminOperations.cs#StartWalMoveAsync"
  - n: 5
    action: "Reclaim only after rollback is no longer needed and the tracked move has succeeded."
    api: "ILatticeTreeAdmin.ReclaimMovedWalSourceAsync / ReclaimMovedWalSource / lattice_treeadmin_wal_move_reclaim"
    expects: "Discards the orphaned source tail and returns a reclaim receipt."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ReclaimMovedWalSourceAsync"
postconditions:
  - check: "The moved source tail is retained until explicit reclaim."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdminOperations.cs#StartWalMoveAsync"
  - check: "After reclaim, moving back cannot recover the discarded source data."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ReclaimMovedWalSourceAsync"
  - check: "A tracked WAL move cancelled before the placement flip leaves the partition on the source provider; cancellation after the flip does not undo the move."
    source: "docs/lattice.api.treeadmin/operations.md"
failure_modes:
  - id: continuity-failure
    signal: "InvalidOperationException when target cannot continue offsets."
    effect: "Placement pin is not flipped."
    response: "Use a target retaining the required offset range."
  - id: live-source-reclaim
    signal: "Reclaim fails if the source key is still the live placement."
    effect: "No source tail is trimmed."
    response: "Inspect placement and move away first."
  - id: target-missing
    signal: "Unresolvable target key fails before placement flips."
    effect: "Partition remains on the source provider."
    response: "Fix provider catalog configuration and retry."
  - id: surface-not-available
    signal: "The in-process interface cannot be resolved, the gRPC service is not mapped, or the MCP tool is not listed in the session's tools/list."
    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: true
  mechanism: "Move the partition back to its original provider before ReclaimMovedWalSourceAsync; after reclaim rollback is unsupported."
  steps:
    - action: "Start another WAL move to the original provider key."
      api: "ILatticeTreeAdminOperations.StartWalMoveAsync"
      source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdminOperations.cs#StartWalMoveAsync"
  source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdminOperations.cs#StartWalMoveAsync"
concurrency:
  - rule: "Placement mutation must not interleave with another move of the same partition."
    source: "src/lattice/ILatticeAdmin.cs#ExecuteWalMoveAsync"
  - rule: "Batch core moves flip placement once under a single compare-and-swap."
    source: "src/lattice/ILatticeAdmin.cs#ExecuteWalMoveAsync"
sources:
  - "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs"
  - "src/lattice/ILatticeAdmin.cs"
  - "src/lattice.api.treeadmin.grpc/LatticeTreeAdminGrpcMethods.cs"
  - "src/lattice.api.mcp/Tools/TreeAdmin/TreeAdminToolGroup.cs"
  - "docs/lattice.api.treeadmin/README.md"
  - "docs/lattice/wal-storage-providers.md"
