-- Add source identity to immutable narrative revisions. Measurements remain run references. ALTER TABLE analysis_artifact_revisions ADD COLUMN IF NOT EXISTS source JSONB; DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname='workspace_source_object' AND conrelid='analysis_artifact_revisions'::regclass) THEN ALTER TABLE analysis_artifact_revisions ADD CONSTRAINT workspace_source_object CHECK (source IS NULL OR jsonb_typeof(source)='object'); END IF; END $$; CREATE OR REPLACE FUNCTION require_workspace_source_evidence() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE source JSONB; revision analysis_artifact_revisions; experiment research_experiments; header research_walk_forwards; document JSONB; allowed TEXT[]; ref RECORD; expected JSONB; count_refs INTEGER:=0; BEGIN IF TG_TABLE_NAME='analysis_evidence' THEN SELECT * INTO revision FROM analysis_artifact_revisions WHERE id=NEW.revision_id; ELSE revision:=NEW; END IF; source:=revision.source; IF source IS NULL THEN RETURN NULL; END IF; IF (jsonb_typeof(source)='object' AND source->>'previewHash' ~ '^[a-f0-9]{64}$') IS NOT TRUE THEN RAISE EXCEPTION 'Finding source requires a strict immutable identity' USING ERRCODE='23514'; END IF; IF source->>'kind'='experiment/v1' THEN IF (source=jsonb_build_object('kind','experiment/v1','experimentId',source->'experimentId','previewHash',source->'previewHash') AND source->>'experimentId' ~ '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$') IS NOT TRUE THEN RAISE EXCEPTION 'Finding experiment source shape is invalid' USING ERRCODE='23514'; END IF; SELECT * INTO experiment FROM research_experiments WHERE id::text=source->>'experimentId'; IF (experiment.id IS NOT NULL AND experiment.validation_phase IS NULL AND experiment.preview_hash=source->>'previewHash' AND experiment.preview->>'previewHash'=experiment.preview_hash) IS NOT TRUE THEN RAISE EXCEPTION 'Finding source requires the exact standalone experiment preview' USING ERRCODE='23514'; END IF; SELECT array_agg(id::text) INTO allowed FROM runs WHERE experiment_id=experiment.id; ELSIF source->>'kind'='chronological-report/v1' THEN IF (source=jsonb_build_object('kind','chronological-report/v1','validationId',source->'validationId','previewHash',source->'previewHash','reportId',source->'reportId','reportHash',source->'reportHash') AND source->>'validationId' ~ '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$' AND source->>'reportId' ~ '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$' AND source->>'reportHash' ~ '^[a-f0-9]{64}$') IS NOT TRUE THEN RAISE EXCEPTION 'Finding chronological source shape is invalid' USING ERRCODE='23514'; END IF; SELECT * INTO experiment FROM research_experiments WHERE id::text=source->>'validationId'; document:=experiment.validation_report; IF (experiment.validation_phase='train' AND document->>'version'='chronological-validation-report/v1' AND document->>'validationId'=experiment.id::text AND document->>'id'=source->>'reportId' AND document->>'reportHash'=source->>'reportHash' AND document->>'previewHash'=source->>'previewHash' AND experiment.validation_manifest->>'previewHash'=source->>'previewHash' AND document->>'selectionHash'=experiment.validation_selection->>'selectionHash') IS NOT TRUE THEN RAISE EXCEPTION 'Finding source requires the exact sealed chronological report' USING ERRCODE='23514'; END IF; SELECT array_agg(item->>'runId') INTO allowed FROM jsonb_array_elements((document->'training') || jsonb_build_array(document->'validation',document->'test')) item; ELSIF source->>'kind'='walk-forward-report/v1' THEN IF (source=jsonb_build_object('kind','walk-forward-report/v1','walkForwardId',source->'walkForwardId','previewHash',source->'previewHash','reportId',source->'reportId','reportHash',source->'reportHash') AND source->>'walkForwardId' ~ '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$' AND source->>'reportId' ~ '^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$' AND source->>'reportHash' ~ '^[a-f0-9]{64}$') IS NOT TRUE THEN RAISE EXCEPTION 'Finding walk-forward source shape is invalid' USING ERRCODE='23514'; END IF; SELECT * INTO header FROM research_walk_forwards WHERE id::text=source->>'walkForwardId'; document:=header.report; IF (document->>'version'='walk-forward-report/v1' AND document->>'walkForwardId'=header.id::text AND document->>'id'=source->>'reportId' AND document->>'reportHash'=source->>'reportHash' AND document->>'previewHash'=source->>'previewHash' AND header.preview_hash=source->>'previewHash') IS NOT TRUE THEN RAISE EXCEPTION 'Finding source requires the exact sealed walk-forward report' USING ERRCODE='23514'; END IF; SELECT array_agg(item->>'runId') INTO allowed FROM jsonb_array_elements(document->'segments') segment, LATERAL jsonb_array_elements(jsonb_build_array(segment->'validation',segment->'test')) item; ELSE RAISE EXCEPTION 'Unsupported finding source kind' USING ERRCODE='23514'; END IF; FOR ref IN SELECT ae.*,r.status,r.experiment_id,r.backtest_id,r.trial_index,r.trial_key,r.request_snapshot,r.dataset_hash, e.preview,br.canonical_result FROM analysis_evidence ae JOIN runs r ON r.id=ae.run_id JOIN backtest_results br ON br.run_id=r.id LEFT JOIN research_experiments e ON e.id=r.experiment_id WHERE ae.revision_id=revision.id LOOP count_refs:=count_refs+1; IF (ref.metrics_version='canonical-analytics/v1' AND ref.run_id::text=ANY(allowed) AND ref.status='completed' AND ref.experiment_id IS NOT NULL AND ref.preview#>>ARRAY['candidates',ref.trial_index::text,'trialKey']=ref.trial_key) IS NOT TRUE THEN RAISE EXCEPTION 'Finding evidence requires completed canonical members of its source' USING ERRCODE='23514'; END IF; expected:=experiment_trial_request(ref.preview,ref.trial_index,ref.run_id); IF (ref.request_snapshot=expected AND ref.dataset_hash=expected->>'datasetHash' AND ref.backtest_id::text=expected->>'backtestId' AND ref.canonical_result->'request'=expected AND ref.canonical_result->>'runId'=ref.run_id::text AND ref.canonical_result->>'kind'='canonical-result/v1') IS NOT TRUE THEN RAISE EXCEPTION 'Finding source evidence requires its exact frozen trial request' USING ERRCODE='23514'; END IF; END LOOP; IF count_refs=0 THEN RAISE EXCEPTION 'A source requires at least one canonical metric reference' USING ERRCODE='23514'; END IF; RETURN NULL; END $$; DROP TRIGGER IF EXISTS workspace_source_evidence ON analysis_artifact_revisions; CREATE CONSTRAINT TRIGGER workspace_source_evidence AFTER INSERT ON analysis_artifact_revisions DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION require_workspace_source_evidence(); DROP TRIGGER IF EXISTS workspace_source_member ON analysis_evidence; CREATE CONSTRAINT TRIGGER workspace_source_member AFTER INSERT ON analysis_evidence DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION require_workspace_source_evidence();