schema: lattice.agents/procedure/v1
id: resize
name: "Change B+ node capacity with online resize"
surfaces:
  in_process:
    api: "ILatticeTreeAdmin.ResizeTreeAsync and UndoTreeResizeAsync"
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ResizeTreeAsync"
  grpc:
    api: "/orleans.lattice.api.treeadmin/ResizeTree and UndoTreeResize"
    source: "src/lattice.api.treeadmin.grpc/LatticeTreeAdminGrpcMethods.cs#ResizeTreeMethodName"
  mcp_tool:
    api: "lattice_treeadmin_tree_resize and lattice_treeadmin_tree_resize_undo"
    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_resize, lattice_treeadmin_tree_resize_undo), 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: no-reshard
    check: "No reshard is already in flight."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ResizeTreeAsync"
  - id: tree-lifecycle
    check: "Caller has whole-tree TreeLifecycle authority."
    source: "docs/lattice.api.treeadmin/README.md"
  - id: valid-capacity
    check: "newMaxLeafKeys is at least 2 and newMaxInternalChildren is at least 3."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ResizeTreeAsync"
steps:
  - n: 1
    action: "Read current resize status."
    api: "ILatticeTreeAdmin.GetResizeStatusAsync / GetResizeStatus"
    expects: "Returns in-flight flag and effective B+ node capacity."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetResizeStatusAsync"
  - n: 2
    action: "Trigger the online resize with target capacities."
    api: "ILatticeTreeAdmin.ResizeTreeAsync / ResizeTree"
    expects: "Returns accepted intent and observed capacity."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ResizeTreeAsync"
  - n: 3
    action: "Poll resize status until in-flight is false."
    api: "ILatticeTreeAdmin.GetResizeStatusAsync / GetResizeStatus"
    expects: "Reports whether a resize remains in flight."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetResizeStatusAsync"
  - n: 4
    action: "Undo while in flight or while the prior tree is recoverable."
    api: "ILatticeTreeAdmin.UndoTreeResizeAsync / UndoTreeResize"
    expects: "Returns resize status after the undo request."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#UndoTreeResizeAsync"
postconditions:
  - check: "The tree keeps serving ordinary reads and writes while a shadow tree is built and swapped; bulk loads and appends are not retried across the rejecting window."
    source: "docs/lattice/tree-sizing.md"
  - check: "Bulk loads and appends force-refresh routing at call start, so a call made after the swap reaches the destination, but one already in flight when the alias swaps is applied to the old physical tree and removed by its purge."
    source: "src/lattice/BPlusTree/Grains/LatticeGrain.BulkLoad.cs#LatticeGrain.BulkLoadAsync"
  - check: "The logical registry entry keeps the live alias, sizing, configuration overrides, and tombstone compaction schedule after the old physical copy is retired."
    source: "docs/lattice/tree-sizing.md"
  - check: "Undo is available only while a resize is in progress or while the pre-resize tree is inside its soft-delete recovery window."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#UndoTreeResizeAsync"
failure_modes:
  - id: invalid-capacity
    signal: "ArgumentOutOfRangeException for too-small node capacity."
    effect: "No resize is started."
    response: "Use at least 2 leaf keys and 3 internal children."
  - id: no-undo-target
    signal: "InvalidOperationException when no in-flight or recoverable completed resize exists."
    effect: "Undo is not started."
    response: "Treat the current capacity as authoritative."
  - id: purged-before-undo
    signal: "InvalidOperationException when the pre-resize tree has already been purged."
    effect: "Undo is unavailable."
    response: "Restore from backup if a prior point-in-time state is required."
  - id: replicated-after-swap
    signal: "InvalidOperationException naming the tree as replicated, once its alias has swapped onto the resized copy."
    effect: "Undo is refused before any compensation; the tree stays on the resized copy, because writes it took may already be on a peer and replication never retracts them."
    response: "Resize the tree again, back to the previous MaxLeafKeys and MaxInternalChildren the refusal names."
  - 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: "UndoTreeResizeAsync coordinates a return to the pre-resize physical tree while recoverable."
  steps:
    - action: "Call UndoTreeResizeAsync for the logical tree."
      api: "ILatticeTreeAdmin.UndoTreeResizeAsync"
      source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#UndoTreeResizeAsync"
    - action: "Poll GetResizeStatusAsync until the undo completes."
      api: "ILatticeTreeAdmin.GetResizeStatusAsync"
      source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetResizeStatusAsync"
  source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#UndoTreeResizeAsync"
concurrency:
  - rule: "A matching in-flight target is idempotent; different resize parameters are rejected."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ResizeTreeAsync"
  - rule: "The destination physical tree is registered with the source ShardCount and inherited shard map; the snapshot copies every routed shard index, including adaptive-split shards above the pinned count."
    source: "docs/lattice/tree-sizing.md"
  - rule: "Resize and reshard are mutually exclusive in-flight operations."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#ResizeTreeAsync"
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/tree-sizing.md"
