CREATE TABLE assessor_regulatory_mapping_runs ( id text PRIMARY KEY, project_id text NOT NULL REFERENCES assessor_projects(id) ON DELETE CASCADE, source_result_id text NOT NULL REFERENCES assessor_finding_occurrences(id) ON DELETE CASCADE, result_projection_id text NOT NULL, idempotency_key text NOT NULL, run_status text NOT NULL CHECK (run_status IN ('queued', 'running', 'succeeded', 'failed')), query_hash text NOT NULL CHECK (query_hash ~ '^[a-f0-9]{64}$'), corpus_id text NOT NULL, corpus_version text NOT NULL, embedding_model text NOT NULL, retrieval_version text NOT NULL, prompt_version text NOT NULL, output_schema_version integer NOT NULL, model text NOT NULL, created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, data jsonb NOT NULL, CONSTRAINT assessor_regulatory_mapping_runs_idempotency_uq UNIQUE (project_id, source_result_id, idempotency_key) ); CREATE UNIQUE INDEX assessor_regulatory_mapping_runs_active_finding_uq ON assessor_regulatory_mapping_runs (project_id, source_result_id) WHERE run_status IN ('queued', 'running'); CREATE INDEX assessor_regulatory_mapping_runs_finding_created_idx ON assessor_regulatory_mapping_runs (project_id, source_result_id, created_at DESC, id DESC); CREATE TABLE assessor_regulatory_mapping_decisions ( id text PRIMARY KEY, project_id text NOT NULL REFERENCES assessor_projects(id) ON DELETE CASCADE, source_result_id text NOT NULL REFERENCES assessor_finding_occurrences(id) ON DELETE CASCADE, run_id text NOT NULL REFERENCES assessor_regulatory_mapping_runs(id) ON DELETE CASCADE, control_id text NOT NULL, decision text NOT NULL CHECK (decision IN ('accepted', 'rejected')), previous_decision_id text REFERENCES assessor_regulatory_mapping_decisions(id), created_at timestamptz NOT NULL, data jsonb NOT NULL ); CREATE INDEX assessor_regulatory_mapping_decisions_finding_created_idx ON assessor_regulatory_mapping_decisions (project_id, source_result_id, control_id, created_at DESC, id DESC);