-- Phase-1: Router Experience Bus event store (append-only) CREATE TABLE IF NOT EXISTS agent_experience_events ( id BIGSERIAL PRIMARY KEY, event_id UUID NOT NULL UNIQUE, ts TIMESTAMPTZ NOT NULL, node_id TEXT NOT NULL, source TEXT NOT NULL, agent_id TEXT NOT NULL, task_type TEXT NOT NULL, request_id TEXT NULL, channel TEXT NOT NULL DEFAULT 'unknown', inputs_hash TEXT NOT NULL, provider TEXT NOT NULL, model TEXT NOT NULL, profile TEXT NULL, latency_ms INT NOT NULL, tokens_in INT NULL, tokens_out INT NULL, ok BOOLEAN NOT NULL, error_class TEXT NULL, error_msg_redacted TEXT NULL, http_status INT NOT NULL, raw JSONB NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS idx_agent_experience_events_agent_ts ON agent_experience_events (agent_id, ts DESC); CREATE INDEX IF NOT EXISTS idx_agent_experience_events_task_ts ON agent_experience_events (task_type, ts DESC); CREATE INDEX IF NOT EXISTS idx_agent_experience_events_hash_ts ON agent_experience_events (inputs_hash, ts DESC); CREATE INDEX IF NOT EXISTS idx_agent_experience_events_ok_ts ON agent_experience_events (ok, ts DESC);