schema: lattice.agents/procedure/v1
id: tree-delete-recover
name: "Soft-delete, recover, and purge a tree"
surfaces:
  in_process:
    api: "ILatticeTreeAdmin.DeleteTreeAsync, RecoverTreeAsync, PurgeTreeAsync"
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#DeleteTreeAsync"
  grpc:
    api: "/orleans.lattice.api.treeadmin/DeleteTree, RecoverTree, PurgeTree"
    source: "src/lattice.api.treeadmin.grpc/LatticeTreeAdminGrpcMethods.cs#DeleteTreeMethodName"
  mcp_tool:
    api: "lattice_treeadmin_tree_delete, lattice_treeadmin_tree_recover, lattice_treeadmin_tree_purge"
    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_delete, lattice_treeadmin_tree_purge, lattice_treeadmin_tree_recover), 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: not-reserved
    check: "Tree id is not reserved."
    source: "docs/lattice.api.treeadmin/README.md"
  - id: purge-confirmation
    check: "Hard purge call sets confirm to true."
    source: "docs/lattice.api.treeadmin/README.md"
  - id: tree-lifecycle
    check: "Caller has whole-tree TreeLifecycle authority for mutating verbs."
    source: "docs/lattice.api.treeadmin/README.md"
steps:
  - n: 1
    action: "Read deletion status before acting."
    api: "ILatticeTreeAdmin.GetTreeDeletionStatusAsync / GetTreeDeletionStatus"
    expects: "Reports live/deleted state, recovery deadline, purge progress, and recoverability."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetTreeDeletionStatusAsync"
  - n: 2
    action: "Soft-delete the tree when reversible removal is intended."
    api: "ILatticeTreeAdmin.DeleteTreeAsync / DeleteTree"
    expects: "Marks shards deleted and schedules deferred hard purge."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#DeleteTreeAsync"
  - n: 3
    action: "Recover within the recovery window when rollback is required."
    api: "ILatticeTreeAdmin.RecoverTreeAsync / RecoverTree"
    expects: "Restores normal operation and cancels deferred purge."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#RecoverTreeAsync"
  - n: 4
    action: "Hard-purge only with explicit confirmation."
    api: "ILatticeTreeAdmin.PurgeTreeAsync / PurgeTree"
    expects: "Irreversibly removes tree state and unregisters the tree."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#PurgeTreeAsync"
postconditions:
  - check: "Soft-delete is reversible until the recovery window elapses or purge runs."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#DeleteTreeAsync"
  - check: "Soft-delete, recover, and purge walk shard indexes from 0 through the greatest registry-recorded pinned count, shard-map index, or split-allocation high-water mark; an empty-tree reshard that re-pins downward raises the high-water mark to the highest prior shard index, so prior shards above the new count stay inside later delete, recovery, and purge walks."
    source: "docs/lattice/tree-deletion.md"
  - check: "Purge is irreversible."
    source: "docs/lattice.api.treeadmin/README.md"
  - check: "A purge counts as complete only once the tree's registry entry has been removed; until then the deletion status reports PurgeInProgress with every shard done, and TreeExistsAsync still finds the tree."
    source: "docs/lattice/tree-deletion.md"
  - check: "On an aliased tree, delete, recover and purge act on the live backing copy the delete pinned, and purge unregisters both that copy and the logical tree."
    source: "docs/lattice/tree-registry.md"
  - check: "On an aliased tree, a shard that a split adds to the live copy after the alias is set is recorded against the logical tree, and delete, recover and purge read that record as well as the copy's, so they reach the shard with the rest of the copy."
    source: "docs/lattice/tree-deletion.md"
failure_modes:
  - id: not-deleted
    signal: "Recover rejects a tree that is not deleted, including a live resized tree."
    effect: "No state change."
    response: "Read deletion status before recover."
  - id: purge-unconfirmed
    signal: "Purge with confirm false or omitted is rejected."
    effect: "No hard purge occurs."
    response: "Set confirm only after irreversible deletion is intended."
  - id: view-source
    signal: "Delete is rejected while the tree is the source of a materialised view."
    effect: "Tree remains live."
    response: "Drop dependent views first."
  - id: alias-change-in-progress
    signal: "Delete is rejected while a resize, restore or remediation holds the tree's alias-change reservation."
    effect: "Tree remains live."
    response: "Wait for the alias-changing operation to complete, then retry the delete."
  - id: alias-not-owned
    signal: "Delete is rejected because the tree's alias targets a tree it does not own or another tree aliases it."
    effect: "Tree remains live and no tree is deleted through the wrong name."
    response: "Correct or remove the alias, or delete the aliasing tree first, then retry."
  - id: purge-interrupted-before-unregister
    signal: "A purge interrupted after recording its completion but before removing the registry entry leaves that removal owed."
    effect: "The deletion status keeps reporting PurgeInProgress with every shard done, and the id is not reported purged until registry removal lands."
    response: "Let the keepalive reminder or a PurgeTreeAsync retry re-drive registry removal before reusing the id."
  - id: write-during-purge-settling
    signal: "A write to the tree id throws LatticeTreePurgeSettlingException while the purge is walking or still owes the removal of the purged tree's registry entry."
    effect: "The write was not committed and the purged tree is not resurrected."
    response: "Retry the write after about one minute, once the purge's keepalive has removed the registry entry; the write is then a deliberate reuse of the purged id."
  - 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: "RecoverTreeAsync reverses soft delete within the recovery window; purge has no rollback."
  steps:
    - action: "Call RecoverTreeAsync before the recovery deadline."
      api: "ILatticeTreeAdmin.RecoverTreeAsync"
      source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#RecoverTreeAsync"
    - action: "After purge, restore only from backup if available."
      api: "ILatticeTreeAdmin.RestoreTreeAsync"
      source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#RestoreTreeAsync"
  source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#RecoverTreeAsync"
concurrency:
  - rule: "Delete is idempotent for an already-deleted tree."
    source: "docs/lattice.api.treeadmin/README.md"
  - rule: "Purge may be in progress or complete according to deletion status."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetTreeDeletionStatusAsync"
  - rule: "Shard enumeration uses the greatest registry-recorded pinned ShardCount, shard map, or split allocation high-water mark, so it is not limited to the current pinned count."
    source: "docs/lattice/tree-deletion.md"
  - rule: "Alias-changing operations and logical deletion never overlap: a delete refuses while an alias-change reservation is held, and an alias change refuses while the tree is deleted or a delete is pending."
    source: "docs/lattice/tree-registry.md"
  - rule: "The resolved alias target is pinned in durable state before any physical effect, so a retry after an ambiguous failure continues on the same target."
    source: "docs/lattice/tree-registry.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/tree-deletion.md"
  - "docs/lattice/tree-registry.md"
