-- Companies and Orgs: the upper rungs of the deploy ladder (single → team → -- company → org). The model is recursive — *every tier is a topology whose -- nodes are the tier below*. A team binds nodes to claws (migration 0010); a -- company binds nodes to teams; an org binds nodes to companies. Children are -- resolved from these tables at execution time (cm-topology stays pure — the -- parent graph never embeds a child graph, only a string id in node.attrs). -- A company = a baseline TOPOLOGY staffed with real teams. `graph` is the -- TopologyGraph whose nodes carry attrs["team_id"] = the bound team's uuid -- (and attrs["agent"] = the same id, so the recursive executor receives it via -- TurnRequest the same way a team node forwards its claw alias). CREATE TABLE companies ( id UUID PRIMARY KEY, workspace_id UUID NOT NULL REFERENCES workspaces (id) ON DELETE CASCADE, name TEXT NOT NULL, kind TEXT NOT NULL, -- TopologyKind (snake_case) graph JSONB NOT NULL, -- TopologyGraph (nodes/edges) status TEXT NOT NULL DEFAULT 'active', created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE INDEX companies_workspace_idx ON companies (workspace_id, created_at DESC); CREATE TABLE company_teams ( company_id UUID NOT NULL REFERENCES companies (id) ON DELETE CASCADE, node_id TEXT NOT NULL, -- topology node id in companies.graph team_id UUID NOT NULL REFERENCES teams (id) ON DELETE CASCADE, role TEXT NOT NULL, PRIMARY KEY (company_id, node_id) ); -- An org = a TOPOLOGY whose nodes are companies. Nodes carry -- attrs["company_id"] = the bound company's uuid. CREATE TABLE orgs ( id UUID PRIMARY KEY, workspace_id UUID NOT NULL REFERENCES workspaces (id) ON DELETE CASCADE, name TEXT NOT NULL, kind TEXT NOT NULL, graph JSONB NOT NULL, status TEXT NOT NULL DEFAULT 'active', created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE INDEX orgs_workspace_idx ON orgs (workspace_id, created_at DESC); CREATE TABLE org_companies ( org_id UUID NOT NULL REFERENCES orgs (id) ON DELETE CASCADE, node_id TEXT NOT NULL, company_id UUID NOT NULL REFERENCES companies (id) ON DELETE CASCADE, role TEXT NOT NULL, PRIMARY KEY (org_id, node_id) ); -- Which deploy tier a durable run belongs to. 'team' drives claws directly -- (today's path); 'company'/'org' drive the recursive sub-topology executor. ALTER TABLE topology_runs ADD COLUMN tier TEXT NOT NULL DEFAULT 'team';