feat(routes): live group-route expansion + full-live invalidation (§11 tail R3)

The runtime half. Group route TEMPLATES now expand into every descendant
app's in-memory match slice, live, with no per-app materialization.

- `compile_effective_routes` consumes the `list_effective` expansion and
  applies NEAREST-OWNER-WINS shadowing: for one app, identical binding
  tuples (method+host+path) owned at multiple chain levels collapse to the
  nearest (an app's own route shadows an ancestor-group template); a nearer
  group beats a farther one. Non-identical bindings coexist and the
  existing matcher precedence resolves each request. `compile_route` now
  takes the effective app_id, so the matcher + dispatch are unchanged.
- `rebuild_route_table` is the single refresh chokepoint (list_effective →
  compile_effective_routes → replace_all). Route CRUD, the apply reconcile,
  and startup all route through it; the apply guards drop the "a group node
  touches no routes" assumption (a group apply may now create templates).
- FULL-LIVE INVALIDATION: app create/delete (apps_api) and group reparent
  (groups_api) rebuild the snapshot, so inherited routes take effect the
  instant the tree changes — matching the live model of triggers/vars.
  Best-effort, mirroring apply: a failed rebuild self-heals on the next
  write or restart, never failing the committed mutation.

The isolation boundary is the chain expansion: a template lands only in the
slices of apps whose ancestor chain contains the owning group. Pinned by a
deterministic live-DB integration test (descendant inherits, sibling
subtree does not, app shadows by identical binding, non-identical coexists).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-06-30 21:49:58 +02:00
parent 469e8526d7
commit 96277e1e7c
7 changed files with 354 additions and 39 deletions

View File

@@ -16,7 +16,7 @@ use axum::http::StatusCode;
use axum::response::{IntoResponse, Json, Response};
use axum::routing::{delete, get, post};
use axum::{Extension, Router};
use picloud_orchestrator_core::routing::{pattern, AppDomainTable, CompiledAppDomain};
use picloud_orchestrator_core::routing::{pattern, AppDomainTable, CompiledAppDomain, RouteTable};
use picloud_shared::{App, AppDomain, AppId, AppRole, InstanceRole, Principal};
use serde::{Deserialize, Serialize};
use serde_json::json;
@@ -43,6 +43,10 @@ pub struct AppsState {
/// Cached host → app_id lookup; replaced after every domain CRUD
/// operation so the orchestrator sees changes immediately.
pub domain_table: Arc<AppDomainTable>,
/// §11 tail: the route match snapshot. Creating/deleting an app changes
/// which group route TEMPLATES are inherited, so it is rebuilt here
/// (full-live invalidation) — mirroring the domain_table refresh.
pub route_table: Arc<RouteTable>,
/// Capability gate — Phase 3.5.
pub authz: Arc<dyn AuthzRepo>,
/// Group tree — resolves an app's parent group at create time
@@ -226,6 +230,9 @@ async fn create_app(
)
.await?
};
// The new app may sit under a group that owns route TEMPLATES — make them
// servable immediately (full-live invalidation).
refresh_route_cache(&s).await;
Ok((StatusCode::CREATED, Json(created)))
}
@@ -368,6 +375,8 @@ async fn delete_app(
s.apps.delete(app.id).await?;
}
refresh_domain_cache(&s).await?;
// Drop the deleted app's slice (and any routes it owned) from the snapshot.
refresh_route_cache(&s).await;
Ok(StatusCode::NO_CONTENT)
}
@@ -545,6 +554,19 @@ fn validate_slug(slug: &str) -> Result<(), AppsApiError> {
/// Rebuild the in-memory host → app_id cache used by the orchestrator.
/// Called after every domain CRUD operation.
/// §11 tail: rebuild the route match snapshot after a tree mutation that
/// changes route inheritance (an app gained/lost its place in the group tree,
/// so the set of ancestor-group route TEMPLATES it serves changed). Best-effort
/// to mirror the apply path — a failure leaves a stale-but-self-healing table
/// rather than failing the committed mutation.
async fn refresh_route_cache(state: &AppsState) {
if let Err(e) =
crate::route_admin::rebuild_route_table(state.routes.as_ref(), &state.route_table).await
{
tracing::warn!(error = %e, "apps: route table refresh failed; it will self-heal");
}
}
pub async fn refresh_domain_cache(state: &AppsState) -> Result<(), AppsApiError> {
let all = state.domains.list_all().await?;
let compiled = all