schema: lattice.agents/procedure/v1
id: reshard
name: "Grow or shrink a tree with online reshard"
surfaces:
  in_process:
    api: "ILatticeTreeAdmin.ReshardTreeAsync"
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ReshardTreeAsync"
  grpc:
    api: "/orleans.lattice.api.treeadmin/ReshardTree"
    source: "src/lattice.api.treeadmin.grpc/LatticeTreeAdminGrpcMethods.cs#ReshardTreeMethodName"
  mcp_tool:
    api: "lattice_treeadmin_tree_reshard"
    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_tree_reshard), 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: target-in-range
    check: "Target shard count is at least 2 and at most the smaller of 4096 and the tree's virtual slot count."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ReshardTreeAsync"
  - id: no-resize
    check: "No resize is already in flight unless the target can be applied by the empty-tree re-pin fast path before migration starts."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ReshardTreeAsync"
  - id: tree-lifecycle
    check: "Caller has whole-tree TreeLifecycle authority."
    source: "docs/lattice.api.treeadmin/README.md"
steps:
  - n: 1
    action: "Read current reshard status."
    api: "ILatticeTreeAdmin.GetReshardStatusAsync / GetReshardStatus"
    expects: "Returns in-flight flag, physical shard fan-out, virtual-slot space, and map version."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetReshardStatusAsync"
  - n: 2
    action: "Trigger the reshard to the target count."
    api: "ILatticeTreeAdmin.ReshardTreeAsync / ReshardTree"
    expects: "Returns accepted intent and observed fan-out."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ReshardTreeAsync"
  - n: 3
    action: "Poll status until in-flight is false and target fan-out is observed."
    api: "ILatticeTreeAdmin.GetReshardStatusAsync / GetReshardStatus"
    expects: "Reports whether a reshard remains in flight."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetReshardStatusAsync"
postconditions:
  - check: "The tree keeps serving reads and writes throughout the migration."
    source: "docs/lattice.api.treeadmin/README.md"
  - check: "Virtual-slot routing is atomically swapped per split or fold and map version advances."
    source: "docs/lattice.api.treeadmin/README.md"
  - check: "A shrink materialises a default shard map before the first fold when the tree has no persisted map, so overlapping readers observe a map-version change instead of the version-0 fast path."
    source: "src/lattice/BPlusTree/Grains/TreeShardConsolidationGrain.cs#TreeShardConsolidationGrain.InitiateConsolidationStateAsync"
  - check: "After a shrink, a retired donor releases storage only when the live shard map no longer routes to it and the shard has no migration or resize state; otherwise the fold completes as routing-only retirement and leaves donor storage in place."
    source: "src/lattice/BPlusTree/Grains/TreeShardConsolidationGrain.cs#TreeShardConsolidationGrain.RetireDonorAsync"
failure_modes:
  - id: different-inflight
    signal: "InvalidOperationException for a different in-flight reshard target."
    effect: "Existing reshard remains authoritative."
    response: "Poll status and retry after completion."
  - id: resize-conflict
    signal: "InvalidOperationException when a resize is in flight, or a completed resize can still be undone while its previous copy mirrors into the resized one (until the purge, SoftDeleteDuration after the resize), and the target requires a migration."
    effect: "No reshard is started."
    response: "Wait for resize completion or undo."
  - id: out-of-range-target
    signal: "ArgumentOutOfRangeException for a target below 2 or above the smaller of 4096 and the tree's virtual slot count."
    effect: "No reshard is started."
    response: "Pick a target in range."
  - 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: false
  mechanism: "A started reshard runs to its target; there is no revert operation. Reshard again to the previous count to undo its effect."
  steps: []
  source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ReshardTreeAsync"
concurrency:
  - rule: "A matching in-flight target is idempotent; a different target is rejected."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ReshardTreeAsync"
  - rule: "Reshard and resize are mutually exclusive in-flight operations."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ReshardTreeAsync"
  - rule: "A shrink fold refuses a pair while either shard is the target of any in-flight migration and retries once that migration has finished."
    source: "src/lattice/BPlusTree/Grains/TreeShardConsolidationGrain.cs#TreeShardConsolidationGrain.ThrowIfMigrationTargetAsync"
  - rule: "A shrink fold defers releasing the donor's storage while a snapshot of or merge into the same tree is in flight."
    source: "src/lattice/BPlusTree/Grains/TreeShardConsolidationGrain.cs#TreeShardConsolidationGrain.FinaliseAsync"
  - rule: "A split or fold already in flight across an alias cutover abandons before committing its slot diff, so it does not apply the replaced tree's shard-index changes to the new copy's map."
    source: "docs/lattice/shard-splitting.md"
sources:
  - "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs"
  - "src/lattice.api.treeadmin.grpc/LatticeTreeAdminGrpcMethods.cs"
  - "src/lattice.api.mcp/Tools/TreeAdmin/TreeAdminToolGroup.cs"
  - "docs/lattice.api.treeadmin/README.md"
  - "docs/lattice/online-reshard.md"
