-- 014_agent.sql — "hey superbrain" agent automations (internal/agent). -- -- Additive and idempotent; schema.sql carries identical structures, so this -- is a structural no-op on a fresh database (db-converge proves both paths -- land the same). No existing table gains a constraint here, so the -- converge pass-2 CASCADE-drop simulation has nothing external to repair. -- -- Invariants encoded in the DDL: -- * AgentTask.spec is validated by code on every write and every engine -- load (internal/agent.ValidateSpec); the engine executes nothing else. -- * AgentRun is a bounded audit trail (the engine prunes to a per-task -- tail and an age horizon). CREATE TABLE IF NOT EXISTS "AgentTask" ( id text PRIMARY KEY, "ownerId" text NOT NULL REFERENCES "User"(id) ON DELETE CASCADE, name text NOT NULL, spec jsonb NOT NULL, enabled boolean NOT NULL DEFAULT true, status text NOT NULL DEFAULT 'active' CHECK (status IN ('active','done','paused')), "lastCheckedAt" timestamptz, "lastFiredAt" timestamptz, "fireCount" integer NOT NULL DEFAULT 0, version integer NOT NULL DEFAULT 1, "createdAt" timestamptz NOT NULL DEFAULT now(), "updatedAt" timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS "AgentTask_ownerId_idx" ON "AgentTask"("ownerId"); CREATE TABLE IF NOT EXISTS "AgentRun" ( id text PRIMARY KEY, "taskId" text NOT NULL REFERENCES "AgentTask"(id) ON DELETE CASCADE, "ownerId" text NOT NULL REFERENCES "User"(id) ON DELETE CASCADE, outcome text NOT NULL CHECK (outcome IN ('checked','fired','failed','skipped')), detail text, "createdAt" timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS "AgentRun_taskId_createdAt_idx" ON "AgentRun"("taskId", "createdAt" DESC);