Files
clawmates/crates/cm-api/tests/topology_jobs.rs
T
Omar SobhandClaude Opus 4.8 3eca4ed70c Recursive deploy ladder: Company + Org tiers, mesh mark, two-tier rail
Completes the scale ladder (single → team → company → org). Every tier is a
topology whose nodes are the tier below; running a parent recursively runs each
child's sub-topology down to the leaf claws.

Backend:
- migration 0011: companies/company_teams, orgs/org_companies, topology_runs.tier
- cm-db repos for companies + orgs (mirror teams)
- TurnRequest.attrs (forwarded from node.attrs) for child-id binding
- SubTopologyExecutor (recursive_exec.rs): a parent "turn" runs the child's
  sub-topology; durability via parent updated_at keepalive + cancel propagation
  + depth cap; boxed future breaks the org→company recursion
- topology_worker selects executor by job.tier
- routes: /api/companies, /api/orgs (create/list/get/run) + unified
  /api/structure/{level}/{id} for the zoom canvas

Frontend:
- MeshMark: node-mesh brand glyph (replaces the claw PNG), tier variants
- TopologyGraphView: optional onNodeClick/nodeMeta + dark-token theming
- StructureCanvas + Breadcrumb: one recursive zoom view for every tier
  (drill down on node click, breadcrumb up); TeamRunPanel extracted + shared
- two-tier Discord-style rail: StructureRail (mesh mark + org/company/team
  glyphs + tools popover + deploy + user) | RosterColumn (selected group's
  children, or your claws); SecondaryNav for cross-cutting tools
- ComposeWizard (company/org) wired into DeployWizard; /companies + /orgs pages

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-18 14:25:06 -07:00

134 lines
4.6 KiB
Rust

//! The durable topology-job lifecycle at the persistence layer: enqueue → claim
//! (CAS) → checkpoint → complete, plus the stale-run resume sweep. This is the
//! foundation that lets long-horizon topology runs survive worker restarts.
use cm_db::repo::{topology_runs, workspaces};
use cm_domain::{Workspace, WorkspaceId};
use serde_json::json;
use uuid::Uuid;
async fn seed_workspace(pool: &sqlx::PgPool) -> WorkspaceId {
let ws = Workspace {
id: WorkspaceId::new(),
name: "Acme".into(),
plan: "team".into(),
};
workspaces::insert(pool, &ws).await.unwrap();
ws.id
}
#[tokio::test]
async fn durable_run_lifecycle_enqueue_claim_checkpoint_complete() {
let pool = cm_testkit::test_pool().await;
let ws = seed_workspace(&pool).await;
let id = Uuid::now_v7();
let graph = json!({"kind": "pipeline", "nodes": [{"id":"n1","role":"drafter"}], "edges": []});
topology_runs::enqueue_run(&pool, id, ws, "write a haiku", &graph)
.await
.unwrap();
// Status starts queued, no result yet.
let st = topology_runs::status(&pool, id, ws).await.unwrap();
assert_eq!(st.status, "queued");
assert_eq!(st.kind, "run");
assert!(st.result.is_none());
// Claim flips it to running and returns the job + its graph.
let claimed = topology_runs::claim_next_queued(&pool)
.await
.unwrap()
.expect("a queued job to claim");
assert_eq!(claimed.id, id);
assert!(claimed.graph.is_some());
assert!(claimed.checkpoint.is_none());
// A second claim finds nothing (the job is no longer queued).
assert!(topology_runs::claim_next_queued(&pool)
.await
.unwrap()
.is_none());
// Checkpoint mid-run progress.
let progress = json!({"completed": 1, "outputs": ["draft"], "records": [], "totals": {}});
topology_runs::checkpoint(&pool, id, &progress, 1)
.await
.unwrap();
let st = topology_runs::status(&pool, id, ws).await.unwrap();
assert_eq!(st.status, "running");
assert_eq!(st.last_event_id, 1);
assert!(st.checkpoint.is_some());
// Complete with a result blob.
let result = json!({"final_output": "a haiku", "totals": {"turns": 1}});
topology_runs::complete(&pool, id, &result).await.unwrap();
let st = topology_runs::status(&pool, id, ws).await.unwrap();
assert_eq!(st.status, "completed");
assert_eq!(st.result.unwrap()["final_output"], "a haiku");
}
#[tokio::test]
async fn stale_running_jobs_are_requeued_for_resume() {
let pool = cm_testkit::test_pool().await;
let ws = seed_workspace(&pool).await;
let id = Uuid::now_v7();
let graph = json!({"kind": "pipeline", "nodes": [{"id":"n1","role":"drafter"}], "edges": []});
topology_runs::enqueue_run(&pool, id, ws, "task", &graph)
.await
.unwrap();
// Claim it → running.
topology_runs::claim_next_queued(&pool)
.await
.unwrap()
.unwrap();
// Not stale yet (just claimed) → sweep is a no-op.
assert_eq!(topology_runs::requeue_stale(&pool, 60.0).await.unwrap(), 0);
// With a zero threshold the running job counts as stale and is requeued,
// so the next claim picks it up again (resume from checkpoint).
assert_eq!(topology_runs::requeue_stale(&pool, 0.0).await.unwrap(), 1);
let st = topology_runs::status(&pool, id, ws).await.unwrap();
assert_eq!(st.status, "queued");
assert!(topology_runs::claim_next_queued(&pool)
.await
.unwrap()
.is_some());
}
#[tokio::test]
async fn cancel_transitions_only_active_runs() {
let pool = cm_testkit::test_pool().await;
let ws = seed_workspace(&pool).await;
let id = Uuid::now_v7();
let graph = json!({"kind": "pipeline", "nodes": [{"id":"n1","role":"drafter"}], "edges": []});
topology_runs::enqueue_run(&pool, id, ws, "task", &graph)
.await
.unwrap();
// A queued run cancels; the worker sees the new status (no workspace scope).
assert!(topology_runs::cancel(&pool, id, ws).await.unwrap());
assert_eq!(
topology_runs::current_status(&pool, id)
.await
.unwrap()
.as_deref(),
Some("cancelled")
);
assert_eq!(
topology_runs::status(&pool, id, ws).await.unwrap().status,
"cancelled"
);
// Already terminal → cannot cancel again; wrong workspace → no-op.
assert!(!topology_runs::cancel(&pool, id, ws).await.unwrap());
let other = seed_workspace(&pool).await;
let id2 = Uuid::now_v7();
topology_runs::enqueue_run(&pool, id2, ws, "t", &graph)
.await
.unwrap();
assert!(!topology_runs::cancel(&pool, id2, other).await.unwrap());
}