schema: lattice.agents/procedure/v1
id: backup
name: "Capture tree snapshots and durable backups"
surfaces:
  in_process:
    api: "ILatticeTreeAdmin.SnapshotTreeAsync and ILatticeBackupOperations.StartBackupAsync"
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#SnapshotTreeAsync"
  grpc:
    api: "/orleans.lattice.api.treeadmin/SnapshotTree and /orleans.lattice.api.backup/StartBackup"
    source: "src/lattice.api.backup.grpc/LatticeBackupGrpcMethods.cs#StartBackupMethodName"
  mcp_tool:
    api: "lattice_treeadmin_tree_snapshot and lattice_backup_start"
    source: "src/lattice.api.mcp/Tools/Backup/BackupToolGroup.cs"
preconditions:
  - id: surface-available
    check: "Every surface this procedure uses is registered on the host: read the availability of api/backup.json, api/treeadmin.json and, for each MCP tool used (lattice_backup_start, lattice_backup_start_health_check, lattice_backup_start_incremental, lattice_backup_start_set, lattice_treeadmin_tree_snapshot), its availability in api/mcp.json. An MCP tool must also be listed in the session's tools/list."
    source: "docs/agents/api/backup.json"
  - id: backup-engine
    check: "AddLatticeBackupApi is called after AddLatticeBackup."
    source: "docs/lattice.api.backup/README.md"
  - id: fresh-destination
    check: "Snapshot destination tree does not already exist."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#SnapshotTreeAsync"
  - id: source-authority
    check: "Caller has whole-tree Admin for snapshot source and destination, or Backup permission over the whole backup scope: whole tree, every key under a prefix, or the exact key."
    source: "src/lattice.backup/BackupAccessAuthorizer.cs#BackupAccessAuthorizer.AuthorizeAsync"
steps:
  - n: 1
    action: "For a live tree copy, start snapshot into a fresh destination tree."
    api: "ILatticeTreeAdmin.SnapshotTreeAsync / SnapshotTree"
    expects: "Returns an accepted snapshot intent."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#SnapshotTreeAsync"
  - n: 2
    action: "Poll snapshot progress until no capture is in flight."
    api: "ILatticeTreeAdmin.GetSnapshotStatusAsync / GetSnapshotStatus"
    expects: "Reports whether a point-in-time capture is in flight."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetSnapshotStatusAsync"
  - n: 3
    action: "For durable backup, start a tracked full backup for the selected scope."
    api: "ILatticeBackupOperations.StartBackupAsync / StartBackup / lattice_backup_start"
    expects: "Returns an operation handle immediately; poll status for the backup id."
    source: "src/lattice.api.abstractions/Backup/ILatticeBackupOperations.cs#StartBackupAsync"
  - n: 4
    action: "For an incremental chain, start a tracked capture against the selected base backup id."
    api: "ILatticeBackupOperations.StartIncrementalBackupAsync / StartIncrementalBackup / lattice_backup_start_incremental"
    expects: "Returns an operation handle immediately; poll status for the incremental backup id."
    source: "src/lattice.api.abstractions/Backup/ILatticeBackupOperations.cs#StartIncrementalBackupAsync"
  - n: 5
    action: "For a backup set, start the tracked set capture."
    api: "ILatticeBackupOperations.StartBackupSetAsync / StartBackupSet / lattice_backup_start_set"
    expects: "Returns an operation handle immediately; poll status for the set id and member backup ids."
    source: "src/lattice.api.abstractions/Backup/ILatticeBackupOperations.cs#StartBackupSetAsync"
  - n: 6
    action: "When validating an existing artifact, start the tracked health check and poll the backup operation status."
    api: "ILatticeBackupOperations.StartBackupHealthCheckAsync / StartBackupHealthCheck / lattice_backup_start_health_check"
    expects: "Returns an operation handle; the result map carries healthStatus and artifact mismatch counts."
    source: "src/lattice.api.abstractions/Backup/ILatticeBackupOperations.cs#StartBackupHealthCheckAsync"
  - n: 7
    action: "For catalog maintenance, start the tracked rebuild or scrub and poll the backup operation status."
    api: "ILatticeBackupOperations.StartCatalogRebuildAsync or StartCatalogScrubAsync"
    expects: "Returns an operation handle; the result map carries rebuild or scrub counts."
    source: "src/lattice.api.abstractions/Backup/ILatticeBackupOperations.cs#StartCatalogRebuildAsync"
  - n: 8
    action: "Describe the backup before using it as a restore point."
    api: "ILatticeBackupControl.DescribeBackupAsync / DescribeBackup"
    expects: "Returns the manifest and base-first restore chain, or null when absent."
    source: "src/lattice.api.abstractions/Backup/ILatticeBackupControl.cs#DescribeBackupAsync"
postconditions:
  - check: "Snapshot destination is a live tree, not a catalogued backup artifact."
    source: "docs/lattice.api.treeadmin/README.md"
  - check: "Backup capture is recorded in the backup catalog and can be described by id."
    source: "src/lattice.api.abstractions/Backup/ILatticeBackupControl.cs#DescribeBackupAsync"
failure_modes:
  - id: broken-chain
    signal: "A retained incremental backup references a deleted base backup."
    effect: "The increment restore chain is broken."
    response: "Delete incremental chains tip-first."
  - id: destination-exists
    signal: "Snapshot returns InvalidOperationException for an existing destination."
    effect: "Snapshot is not started."
    response: "Choose a fresh destination tree id."
  - id: undefined-snapshot-mode
    signal: "Snapshot rejects a mode outside TreeSnapshotMode before tree resolution or authorization."
    effect: "Snapshot is not started."
    response: "Pass a defined snapshot mode."
  - id: unauthorized
    signal: "Facade raises LatticeAuthorizationDeniedException."
    effect: "No data is captured."
    response: "Grant the required tree or backup operation and retry."
  - id: operation-runner-lost
    signal: "Tracked operation status becomes Failed after the accepting silo is lost or stops heartbeating."
    effect: "The background backup is not resumed."
    response: "Start a new tracked operation with a fresh id after confirming the previous status."
  - 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: "No snapshot abort verb is surfaced; unwanted backup artifacts are removed only through DeleteBackupAsync."
  steps:
    - action: "Delete an unwanted backup by id."
      api: "ILatticeBackupControl.DeleteBackupAsync"
      source: "src/lattice.api.abstractions/Backup/ILatticeBackupControl.cs#DeleteBackupAsync"
  source: "src/lattice.api.abstractions/Backup/ILatticeBackupControl.cs#DeleteBackupAsync"
concurrency:
  - rule: "Online snapshot keeps the source serving reads and writes while live mutations shadow-forward."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#SnapshotTreeAsync"
  - rule: "A completed online snapshot releases shadow-forward before reporting complete; the internal snapshot used by ResizeAsync leaves release to the resize coordinator."
    source: "docs/lattice/snapshots.md"
  - rule: "Backup set capture can use one causal fence so a cross-tree atomic write is not torn across the set boundary."
    source: "src/lattice.api.abstractions/Backup/ILatticeBackupOperations.cs#StartBackupSetAsync"
  - rule: "Tracked backup operations run on the accepting silo and are marked Failed on silo loss or lease lapse; resumption is not supported."
    source: "docs/lattice.api.abstractions/operations.md"
sources:
  - "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs"
  - "src/lattice.api.abstractions/Backup/ILatticeBackupControl.cs"
  - "src/lattice.api.abstractions/Backup/ILatticeBackupOperations.cs"
  - "src/lattice.api.backup.grpc/LatticeBackupGrpcMethods.cs"
  - "src/lattice.api.mcp/Tools/Backup/BackupToolGroup.cs"
  - "docs/lattice.api.treeadmin/README.md"
  - "docs/lattice.api.backup/README.md"
