schema: lattice.agents/procedure/v1
id: view-rebuild
name: "Rebuild or reconcile a materialised view"
surfaces:
  in_process:
    api: "ILatticeTreeAdminOperations.StartViewRebuildAsync and StartViewReconcileAsync"
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdminOperations.cs#StartViewRebuildAsync"
  grpc:
    api: "/orleans.lattice.api.treeadmin/StartViewRebuild and StartViewReconcile"
    source: "src/lattice.api.treeadmin.grpc/LatticeTreeAdminGrpcMethods.cs#StartViewRebuildMethodName"
  mcp_tool:
    api: "lattice_treeadmin_view_rebuild_start and lattice_treeadmin_view_reconcile_start"
    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_view_rebuild_start, lattice_treeadmin_view_reconcile_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: runtime-view
    check: "Target is a runtime-registered view for runtime control."
    source: "docs/lattice.api.treeadmin/README.md"
  - id: source-admin
    check: "Caller has Admin authority over the view source tree."
    source: "docs/lattice.api.treeadmin/README.md"
  - id: view-subsystem
    check: "The view subsystem is registered."
    source: "docs/lattice.api.treeadmin/README.md"
steps:
  - n: 1
    action: "Read view status."
    api: "ILatticeTreeAdmin.GetViewStatusAsync / GetViewStatus"
    expects: "Returns source tree, apply lag, active generation tree id, provider key, and projection version."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdmin.cs#GetViewStatusAsync"
  - n: 2
    action: "Start tracked reconcile when drift repair is conditional."
    api: "ILatticeTreeAdminOperations.StartViewReconcileAsync / StartViewReconcile / lattice_treeadmin_view_reconcile_start"
    expects: "Returns an operation handle; poll tree-admin operation status for driftRepaired."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdminOperations.cs#StartViewReconcileAsync"
  - n: 3
    action: "Start tracked rebuild when a full generation rebuild is intended."
    api: "ILatticeTreeAdminOperations.StartViewRebuildAsync / StartViewRebuild / lattice_treeadmin_view_rebuild_start"
    expects: "Returns an operation handle; poll tree-admin operation status until the shadow generation swaps or the operation fails."
    source: "src/lattice.api.abstractions/TreeAdmin/ILatticeTreeAdminOperations.cs#StartViewRebuildAsync"
postconditions:
  - check: "Readers do not observe a half-built view during rebuild."
    source: "src/lattice.api.mcp/Tools/TreeAdmin/TreeAdminToolGroup.cs"
  - check: "Provider payloads are never returned by list or status."
    source: "src/lattice.api.mcp/Tools/TreeAdmin/TreeAdminToolGroup.cs"
failure_modes:
  - id: source-unresolved
    signal: "A view whose source cannot be resolved is rejected fail-closed."
    effect: "No maintenance is performed."
    response: "Repair or recreate the view registration."
  - id: operation-runner-lost
    signal: "Tracked view operation status becomes Failed after the accepting silo is lost or stops heartbeating."
    effect: "The background rebuild or reconcile is not resumed."
    response: "Start a new tracked operation after confirming the previous status."
  - id: startup-declared-drop
    signal: "Drop rejects startup-declared views."
    effect: "The view remains declared by code."
    response: "Remove the code declaration instead of using runtime drop."
  - id: hidden-wal-entries-after-upgrade
    signal: "The source tree's WAL is on Azure Table and the cluster ran Orleans.Lattice.Storage.AzureTable 10.0.0-10.2.0, or orleans.lattice.wal.subscriber.holes_skipped records outcome recovered, or a view stays stalled while orleans.lattice.wal.subscriber.empty_backlog_reads climbs on one partition."
    effect: "Before 10.3.0 a stale cached trim watermark could hide committed WAL entries, so the view stalled on the partition or read past them and missed those writes (issue #6036)."
    response: "After upgrading, reconcile every view of the tree, then rebuild any view that still lags or differs from its source; see the remediation in docs/lattice.storage.azuretable/architecture.md."
  - id: subsystem-absent
    signal: "InvalidOperationException when no view subsystem is registered."
    effect: "No view verb is performed."
    response: "Register AddLatticeViews for runtime view support."
  - 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 revert for a completed view rebuild is surfaced; rebuild again from current source state if needed."
  steps: []
  source: "docs/lattice.api.treeadmin/README.md"
concurrency:
  - rule: "View authorization is derived from the source tree resolved by the facade, not caller input."
    source: "docs/lattice.api.treeadmin/README.md"
  - rule: "Reconcile is online and idempotent when the view already matches its source."
    source: "src/lattice.api.mcp/Tools/TreeAdmin/TreeAdminToolGroup.cs"
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/materialised-views.md"
  - "docs/lattice.storage.azuretable/architecture.md"
