-- Optional server-plane extension tables CREATE TABLE IF NOT EXISTS command_dispatches ( id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), tenant_id uuid NOT NULL REFERENCES tenants(id) ON DELETE CASCADE, job_id uuid NOT NULL REFERENCES jobs(id) ON DELETE CASCADE, server_id uuid NOT NULL REFERENCES servers(id) ON DELETE CASCADE, command_type varchar(64) NOT NULL, idempotency_key varchar(120) NOT NULL, status varchar(32) NOT NULL DEFAULT 'queued', queued_at timestamptz NOT NULL DEFAULT now(), dispatched_at timestamptz NULL, acked_at timestamptz NULL, finished_at timestamptz NULL, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), UNIQUE (tenant_id, idempotency_key) ); CREATE TABLE IF NOT EXISTS agent_heartbeats ( id bigserial PRIMARY KEY, tenant_id uuid NOT NULL REFERENCES tenants(id) ON DELETE CASCADE, server_id uuid NOT NULL REFERENCES servers(id) ON DELETE CASCADE, agent_uid varchar(128) NOT NULL, payload jsonb NOT NULL DEFAULT '{}'::jsonb, created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS idx_agent_heartbeats_server_created ON agent_heartbeats (server_id, created_at DESC);