Migration 0033: adds teams.lifecycle ('permanent' | 'ephemeral') and a
topology_runs.team_id back-ref with a partial index for the sibling-in-
flight check.
cm-db repo:
- teams::insert_team_with_lifecycle (insert_team keeps the permanent default)
- topology_runs::enqueue_run_for_team (populates team_id)
- topology_runs::check_ephemeral_teardown — atomic SELECT that only
returns Some when the team is ephemeral AND no siblings are still
queued/running; carries the workspace + bound claw ids for cleanup.
cm-api:
- topology_worker post-terminal hook maybe_teardown_ephemeral_team
runs deprovision_claw on each bound claw (best-effort; failures log
but don't block Postgres deletion), then hard_purge each agent row,
then delete_team.
- routes::teams::build_team_with_lifecycle (build_team keeps default);
run_team enqueues with team_id.
- planner ScaffoldRequest gains mode; lifecycle_for(mode) sets the team
to ephemeral for scheduled + triggered, permanent otherwise.
Frontend MasterPlannerModal passes mode in the scaffold payload so the
backend can derive lifecycle without duplicating the mode taxonomy.
Tests: 3 new (returns claws when no siblings, holds when siblings queued,
ignores permanent teams). 10/10 topology_jobs green; workspace clippy
--tests clean.
29 lines
1.5 KiB
SQL
29 lines
1.5 KiB
SQL
-- Ephemeral team lifecycle: a `lifecycle` column on teams + a back-ref from
|
|
-- topology_runs so the post-terminal hook in cm-api::topology_worker knows
|
|
-- which team an ephemeral run belongs to.
|
|
--
|
|
-- permanent — the default; team + claws persist across runs (Specialists / Team modes).
|
|
-- ephemeral — team is torn down (claws deprovisioned, rows deleted) after the last
|
|
-- in-flight run terminates. Scheduled + Triggered modes use this.
|
|
--
|
|
-- Teardown policy: on a run's terminal transition (completed/failed/cancelled),
|
|
-- if the run's team is ephemeral AND no siblings are still queued/running for
|
|
-- that team, deprovision every bound claw and delete the team row. The FK on
|
|
-- team_members (ON DELETE CASCADE from 0010) cleans the bindings; agents rows
|
|
-- are removed by the worker after deprovision_claw() succeeds.
|
|
|
|
ALTER TABLE teams
|
|
ADD COLUMN lifecycle TEXT NOT NULL DEFAULT 'permanent'
|
|
CHECK (lifecycle IN ('permanent', 'ephemeral'));
|
|
|
|
-- Back-ref from a durable run to its team, when the run was fired by
|
|
-- `POST /api/teams/:id/run` or a scheduled/webhook fire of a team's graph.
|
|
-- Nullable — ad-hoc topology runs + swarm/research runs leave this NULL.
|
|
ALTER TABLE topology_runs
|
|
ADD COLUMN team_id UUID REFERENCES teams (id) ON DELETE SET NULL;
|
|
|
|
-- Fast lookup for "any siblings of this team's run still in flight?".
|
|
CREATE INDEX topology_runs_team_active_idx
|
|
ON topology_runs (team_id, status)
|
|
WHERE team_id IS NOT NULL AND status IN ('queued', 'running');
|