-- ADR-105 MCP audit and snapshot foundation -- 2026-05-01 -- Notes: -- AWOOOI incident ids are string values such as INC-20260429-xxxx, not UUIDs. -- Keep incident_id as VARCHAR(64) so MCP audit can join existing incident records. CREATE TABLE IF NOT EXISTS mcp_audit_log ( id BIGSERIAL PRIMARY KEY, session_id VARCHAR(36) NOT NULL, flywheel_node VARCHAR(20), mcp_server VARCHAR(80) NOT NULL, tool_name VARCHAR(120) NOT NULL, input_params JSONB, output_result JSONB, duration_ms INTEGER, success BOOLEAN, error_message TEXT, incident_id VARCHAR(64), agent_role VARCHAR(40), created_at TIMESTAMPTZ DEFAULT NOW() ); ALTER TABLE mcp_audit_log ADD COLUMN IF NOT EXISTS agent_role VARCHAR(40); CREATE INDEX IF NOT EXISTS idx_mcp_audit_session ON mcp_audit_log(session_id); CREATE INDEX IF NOT EXISTS idx_mcp_audit_incident ON mcp_audit_log(incident_id); CREATE INDEX IF NOT EXISTS idx_mcp_audit_node ON mcp_audit_log(flywheel_node, created_at DESC); CREATE INDEX IF NOT EXISTS idx_mcp_audit_server_tool ON mcp_audit_log(mcp_server, tool_name, created_at DESC); CREATE INDEX IF NOT EXISTS idx_mcp_audit_agent_role ON mcp_audit_log(agent_role, created_at DESC); CREATE TABLE IF NOT EXISTS mcp_daily_stats ( date DATE NOT NULL, mcp_server VARCHAR(80) NOT NULL, tool_name VARCHAR(120) NOT NULL, call_count INTEGER DEFAULT 0 NOT NULL, success_count INTEGER DEFAULT 0 NOT NULL, avg_duration_ms FLOAT, PRIMARY KEY (date, mcp_server, tool_name) ); CREATE TABLE IF NOT EXISTS k8s_state_snapshots ( id BIGSERIAL PRIMARY KEY, incident_id VARCHAR(64), snapshot_type VARCHAR(40) NOT NULL, namespace VARCHAR(63), resource_type VARCHAR(80), resource_name VARCHAR(253), state_json JSONB, captured_at TIMESTAMPTZ DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS idx_k8s_snapshot_incident ON k8s_state_snapshots(incident_id); CREATE INDEX IF NOT EXISTS idx_k8s_snapshot_resource ON k8s_state_snapshots(namespace, resource_type, resource_name); CREATE INDEX IF NOT EXISTS idx_k8s_snapshot_captured ON k8s_state_snapshots(captured_at DESC); CREATE TABLE IF NOT EXISTS prometheus_snapshots ( id BIGSERIAL PRIMARY KEY, incident_id VARCHAR(64), query TEXT NOT NULL, result_json JSONB, snapshot_type VARCHAR(40), captured_at TIMESTAMPTZ DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS idx_prom_snapshot_incident ON prometheus_snapshots(incident_id); CREATE INDEX IF NOT EXISTS idx_prom_snapshot_type ON prometheus_snapshots(snapshot_type, captured_at DESC);