-- Chain contract definitions - stores contract specs before actual contracts are created -- This enables on-demand contract creation when dependencies are met CREATE TABLE IF NOT EXISTS chain_contract_definitions ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), chain_id UUID NOT NULL REFERENCES chains(id) ON DELETE CASCADE, name VARCHAR(255) NOT NULL, description TEXT, contract_type VARCHAR(32) NOT NULL DEFAULT 'simple', -- simple/specification/execute initial_phase VARCHAR(32) DEFAULT 'plan', -- Dependencies by name (resolved to IDs when contract is created) depends_on_names TEXT[] DEFAULT '{}', -- Task definitions as JSON array: [{name, plan}, ...] tasks JSONB DEFAULT '[]', -- Deliverable definitions as JSON array: [{id, name, priority}, ...] deliverables JSONB DEFAULT '[]', -- Position for GUI editor editor_x FLOAT DEFAULT 0, editor_y FLOAT DEFAULT 0, -- Order for display/processing order_index INTEGER NOT NULL DEFAULT 0, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX idx_chain_contract_definitions_chain_id ON chain_contract_definitions(chain_id); -- Add supervisor and retry control to chains ALTER TABLE chains ADD COLUMN IF NOT EXISTS supervisor_task_id UUID REFERENCES tasks(id) ON DELETE SET NULL; ALTER TABLE chains ADD COLUMN IF NOT EXISTS max_retries INTEGER NOT NULL DEFAULT 3; -- Add definition link and retry tracking to chain_contracts ALTER TABLE chain_contracts ADD COLUMN IF NOT EXISTS definition_id UUID REFERENCES chain_contract_definitions(id) ON DELETE SET NULL; ALTER TABLE chain_contracts ADD COLUMN IF NOT EXISTS retry_count INTEGER NOT NULL DEFAULT 0;