{
  "scalars": "SELECT v.field_id,count(*) rows,count(distinct v.document_id) documents_with_rows,count(*) FILTER(WHERE nullif(btrim(v.value),'') IS NOT NULL) populated_rows,count(*) FILTER(WHERE nullif(btrim(v.value),'') IS NULL) blank_rows,count(*) FILTER(WHERE nullif(btrim(v.value),'') IS NOT NULL AND f.field_type::text='number' AND btrim(v.value) !~ '^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?$') numeric_invalid_rows,count(*) FILTER(WHERE nullif(btrim(v.value),'') IS NOT NULL AND f.field_type::text='date' AND parse_stored_date_value(v.value) IS NULL) date_invalid_rows,count(*) FILTER(WHERE nullif(btrim(v.value),'') IS NOT NULL AND f.field_type::text='boolean' AND lower(btrim(v.value)) NOT IN ('true','false','1','0')) boolean_invalid_rows\nFROM app_fields_values v JOIN app_fields f ON f.id=v.field_id AND f.collection_id=v.collection_id JOIN app_documents d ON d.id=v.document_id AND d.collection_id=v.collection_id WHERE v.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' AND v.deleted_at IS NULL AND v.owner_id IS NULL AND v.user_id IS NULL AND f.owner_id IS NULL AND f.deleted_at IS NULL AND d.owner_id IS NULL AND d.deleted_at IS NULL GROUP BY v.field_id",
  "relations": "WITH mapped AS (\n SELECT f.id field_id,f.collection_id,f.reference_id,coalesce(f.meta->>'relation_is_reverse','false')='true' rev,\n CASE WHEN f.meta->>'relation_is_reverse'='true' THEN (f.meta->>'relation_reverse_field_id')::uuid ELSE f.id END stored_field\n FROM app_fields f WHERE f.owner_id IS NULL AND f.deleted_at IS NULL AND f.field_type::text='relation' AND f.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539'\n), facts AS (\n SELECT m.field_id,d.id document_id,count(*) edges,\n count(*) FILTER(WHERE t.id IS NOT NULL AND t.deleted_at IS NULL AND t.owner_id IS NULL AND t.collection_id=m.reference_id AND coalesce(r.is_mask,false)=false) valid_edges,\n count(*) FILTER(WHERE t.id IS NULL OR t.deleted_at IS NOT NULL OR t.owner_id IS NOT NULL OR t.collection_id IS DISTINCT FROM m.reference_id) bad_endpoints,\n count(*) FILTER(WHERE coalesce(r.is_mask,false)) masks\n FROM mapped m JOIN app_fields_relations r ON r.field_id=m.stored_field AND r.owner_id IS NULL AND r.deleted_at IS NULL\n JOIN app_documents d ON d.id=CASE WHEN m.rev THEN r.target_document_id ELSE r.source_document_id END AND d.collection_id=m.collection_id AND d.owner_id IS NULL AND d.deleted_at IS NULL\n LEFT JOIN app_documents t ON t.id=CASE WHEN m.rev THEN r.source_document_id ELSE r.target_document_id END\n GROUP BY m.field_id,d.id\n)\nSELECT field_id,count(*) documents_with_edges,count(*) FILTER(WHERE valid_edges>0) populated,count(*) FILTER(WHERE bad_endpoints>0) documents_with_bad_endpoints,sum(edges) edges,sum(valid_edges) valid_edges,sum(bad_endpoints) bad_endpoints,sum(masks) masks,count(*) FILTER(WHERE valid_edges>1) multiple_targets FROM facts GROUP BY field_id",
  "tags": "with refs as (\nselect v.document_id,v.field_id,f.reference_id,f.field_type,v.value,\ncase when btrim(v.value) ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$' then array[btrim(v.value)::uuid] when v.value ~ '^[[:space:]]*\\[[[:space:]]*(\"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\"([[:space:]]*,[[:space:]]*\"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\")*)?[[:space:]]*\\][[:space:]]*$' then array(select jsonb_array_elements_text(v.value::jsonb)::uuid) else null::uuid[] end ids\nfrom app_fields_values v join app_fields f on f.id=v.field_id and f.collection_id=v.collection_id and f.owner_id is null and f.deleted_at is null and f.field_type::text in ('tag','tags') and f.reference_id is not null join app_documents d on d.id=v.document_id and d.collection_id=v.collection_id and d.owner_id is null and d.deleted_at is null where v.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' and v.owner_id is null and v.user_id is null and v.deleted_at is null\n), facts as (select *, exists(select 1 from unnest(ids) x left join app_documents t on t.id=x where t.id is null or t.owner_id is not null or t.deleted_at is not null or t.collection_id is distinct from refs.reference_id) bad_target from refs)\nselect field_id,count(*) value_rows,count(*) filter(where nullif(btrim(value),'') is null or cardinality(ids)=0) blank_or_empty,count(*) filter(where nullif(btrim(value),'') is not null and ids is null) malformed,count(*) filter(where bad_target) bad_target,count(*) filter(where field_type::text='tag' and cardinality(ids)>1) cardinality_conflict,count(*) filter(where cardinality(ids)>0 and not bad_target and not(field_type::text='tag' and cardinality(ids)>1)) structurally_valid from facts group by field_id",
  "cohorts": "WITH p AS MATERIALIZED (select id,sources,created_at,location,geometry,project_id from app_documents where collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' and deleted_at is null and owner_id is null),\nv AS MATERIALIZED (select v.document_id,\n max(v.value) filter(where field_id='3c5428c9-5790-454d-bbbf-b4af42995fcc') source_key,\n max(v.value) filter(where field_id='2036daa8-bf54-4c32-a5b0-7e97158572d1') agency_key,\n max(v.value) filter(where field_id='de73e8b6-10f1-4d96-85ac-fd76a7562f3d') status_key,\n max(v.value) filter(where field_id='8c6de9d4-24c9-4854-b6b9-35ab9fc603a5') bid_date\n from app_fields_values v join p on p.id=v.document_id where v.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' and v.field_id in ('3c5428c9-5790-454d-bbbf-b4af42995fcc','2036daa8-bf54-4c32-a5b0-7e97158572d1','de73e8b6-10f1-4d96-85ac-fd76a7562f3d','8c6de9d4-24c9-4854-b6b9-35ab9fc603a5') and v.deleted_at is null and v.owner_id is null and v.user_id is null group by v.document_id),\nfacts AS (\n select p.id,p.created_at,to_char(p.created_at at time zone 'UTC','YYYY-MM') ingestion_month,coalesce(s.name,'Unknown or invalid') source,coalesce(a.name,'Unknown or invalid') agency,coalesce(st.name,'Unknown') lifecycle, s.id source_id,a.id agency_id,st.id lifecycle_id,\n coalesce(to_char(parse_stored_date_value(v.bid_date) at time zone 'UTC','YYYY-MM'),'Unknown') bid_month,\n nullif(btrim(v.bid_date),'') is not null has_bid_date,parse_stored_date_value(v.bid_date) is not null parseable_bid_date,\n coalesce(cardinality(p.sources)>0,false) has_sources, p.location is not null or p.geometry is not null has_location,\n exists(select 1 from app_fields_relations r join app_documents t on t.id=r.target_document_id and t.collection_id='4e2db31b-a020-4d52-b8c3-76f5b9c169b1' and t.owner_id is null and t.deleted_at is null where r.source_document_id=p.id and r.field_id='c5acb3f3-4986-4c21-86eb-46f4e1959dcd' and r.owner_id is null and r.deleted_at is null and coalesce(r.is_mask,false)=false) has_files,\n exists(select 1 from app_fields_relations r join app_documents t on t.id=r.source_document_id and t.collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6' and t.owner_id is null and t.deleted_at is null where r.target_document_id=p.id and r.field_id='52f81f92-ad96-45de-a40a-acbcb3a8ecd6' and r.owner_id is null and r.deleted_at is null and coalesce(r.is_mask,false)=false) has_items,\n exists(select 1 from app_fields_relations r join app_documents t on t.id=r.target_document_id and t.collection_id='d0982192-8626-42dd-8ed0-48519c246521' and t.owner_id is null and t.deleted_at is null where r.source_document_id=p.id and r.field_id='a65de11d-2674-4f79-af79-a337f4f36a76' and r.owner_id is null and r.deleted_at is null and coalesce(r.is_mask,false)=false) has_subs\n from p left join v on v.document_id=p.id\n left join app_documents s on s.id=case when v.source_key ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' then v.source_key::uuid end and s.collection_id='fe9ef17d-469c-4e1b-9bb0-01d53b19073d' and s.owner_id is null and s.deleted_at is null\n left join app_documents a on a.id=case when v.agency_key ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' then v.agency_key::uuid end and a.collection_id='4a8b7ae1-92d9-4ec7-b83a-38ef1b5f4333' and a.owner_id is null and a.deleted_at is null\n left join app_documents st on st.id=case when v.status_key ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' then v.status_key::uuid end and st.collection_id='e39f0224-6080-4244-9fd9-95546954f96f' and st.owner_id is null and st.deleted_at is null\n)\nselect source_id,source,agency_id,agency,lifecycle_id,lifecycle,bid_month,ingestion_month,count(*) projects,count(*) filter(where has_sources) with_sources,count(*) filter(where has_files) with_files,count(*) filter(where has_items) with_items,count(*) filter(where has_subs) with_subs,count(*) filter(where has_bid_date) with_bid_date,count(*) filter(where parseable_bid_date) parseable_bid_date,count(*) filter(where has_location) with_location,min(created_at) first_ingested,max(created_at) last_ingested from facts group by source_id,source,agency_id,agency,lifecycle_id,lifecycle,bid_month,ingestion_month",
  "assignment": "-- Candidate detection only; known subs do not prove award or item-to-sub mapping.\n-- First 100 IDs; NOT random or representative. This is not final eligibility.\nWITH candidates AS MATERIALIZED (\n SELECT DISTINCT p.id FROM public.app_documents p\n JOIN public.app_fields_relations s ON s.source_document_id=p.id\n AND s.field_id='a65de11d-2674-4f79-af79-a337f4f36a76'\n JOIN public.app_documents c ON c.id=s.target_document_id\n AND c.collection_id='d0982192-8626-42dd-8ed0-48519c246521'\n AND c.deleted_at IS NULL AND c.owner_id IS NULL\n WHERE p.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539'\n AND p.owner_id IS NULL AND p.deleted_at IS NULL\n AND s.owner_id IS NULL AND s.deleted_at IS NULL ORDER BY p.id\n), facts AS (\n SELECT p.id,\n EXISTS(SELECT 1 FROM public.app_fields_relations ip\n JOIN public.app_documents i ON i.id=ip.source_document_id\n WHERE ip.target_document_id=p.id AND ip.field_id='52f81f92-ad96-45de-a40a-acbcb3a8ecd6'\n AND ip.deleted_at IS NULL AND ip.owner_id IS NULL\n AND i.collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6'\n AND i.owner_id IS NULL AND i.deleted_at IS NULL) AS has_items,\n EXISTS(SELECT 1 FROM public.app_fields_relations ip\n JOIN public.app_documents i ON i.id=ip.source_document_id\n JOIN public.app_fields_relations a ON a.source_document_id=i.id\n JOIN public.app_documents c ON c.id=a.target_document_id\n WHERE ip.target_document_id=p.id AND ip.field_id='52f81f92-ad96-45de-a40a-acbcb3a8ecd6'\n AND ip.deleted_at IS NULL AND ip.owner_id IS NULL\n AND i.collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6'\n AND i.owner_id IS NULL AND i.deleted_at IS NULL\n AND a.field_id='9b30ec2c-7d12-422d-aa27-9cf67d170769'\n AND a.owner_id IS NULL AND a.deleted_at IS NULL\n AND c.collection_id='d0982192-8626-42dd-8ed0-48519c246521'\n AND c.owner_id IS NULL AND c.deleted_at IS NULL) AS has_item_contractor\n FROM candidates p\n)\nSELECT array_agg(id) FILTER (WHERE has_items AND NOT has_item_contractor) AS candidate_project_ids, count(*) AS projects_with_subs,\n count(*) FILTER (WHERE has_items) AS with_items,\n count(*) FILTER (WHERE has_items AND NOT has_item_contractor) AS with_items_but_no_item_contractor\nFROM facts;\n",
  "pipeline": "select task_name,status,count(*) runs,count(*) filter(where rows_processed=0) zero_rows,count(*) filter(where rows_processed is null) unknown_rows,sum(rows_processed) rows_processed,max(started_at) latest_run,max(ended_at) latest_end from scraper_runs where started_at>=now()-interval '7 days' group by task_name,status",
  "schedules": "select scraper_key,coalesce(enabled_override,enabled) effective_enabled,schedule_expr,timezone,aliases,manual_aliases,last_run_at,last_run_status,last_success_at from scraper_schedules",
  "provenance": "select count(*) changes,count(*) filter(where nullif(btrim(source_handle),'') is not null) with_source_handle,count(*) filter(where nullif(btrim(scraper_handle),'') is not null) with_scraper_handle,count(*) filter(where nullif(btrim(source_url),'') is not null) with_source_url,count(*) filter(where source_document_id is not null) with_source_document_id,count(*) filter(where nullif(btrim(run_id),'') is not null) with_run_id,count(*) filter(where item_id is not null) with_item_id,count(*) filter(where meta is not null and meta<>'{}'::jsonb) with_meta,min(created_at) first_change,max(created_at) last_change from app_fields_values_changelog where owner_id is null and user_id is null and created_at>=now()-interval '7 days'",
  "tableInventory": "select table_name from information_schema.tables where table_schema='public' and (table_name ilike '%manifest%' or table_name ilike '%lineage%' or table_name ilike '%extract%' or table_name ilike '%parser%' or table_name ilike '%file%' or table_name ilike '%ingest%') order by table_name",
  "quantity": "select count(*) items,count(*) filter(where v.id is null) missing_quantity,count(*) filter(where v.id is not null and nullif(btrim(v.value),'') is null) blank_quantity,count(*) filter(where btrim(v.value) ~ '^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?$') numeric_quantity,count(*) filter(where btrim(v.value) ~ '^[+-]?0+([.]0*)?([eE][+-]?[0-9]+)?$') zero_quantity,count(*) filter(where btrim(v.value) ~ '^-' and btrim(v.value) ~ '^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?$' and btrim(v.value) !~ '^[+-]?0+([.]0*)?([eE][+-]?[0-9]+)?$') negative_quantity from app_documents d left join app_fields_values v on v.document_id=d.id and v.collection_id=d.collection_id and v.field_id='393fa95b-ca28-5e2b-a2d4-ff45917ef981' and v.owner_id is null and v.user_id is null and v.deleted_at is null where d.collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6' and d.owner_id is null and d.deleted_at is null",
  "quantityIssues": "select v.document_id item_id,v.id value_id,left(v.value,80) value,r.target_document_id project_id,sv.value source_id,av.value agency_id from app_fields_values v join app_documents d on d.id=v.document_id and d.collection_id=v.collection_id and d.deleted_at is null and d.owner_id is null left join app_fields_relations r on r.source_document_id=d.id and r.field_id='52f81f92-ad96-45de-a40a-acbcb3a8ecd6' and r.owner_id is null and r.deleted_at is null left join app_fields_values sv on sv.document_id=r.target_document_id and sv.field_id='3c5428c9-5790-454d-bbbf-b4af42995fcc' and sv.owner_id is null and sv.user_id is null and sv.deleted_at is null left join app_fields_values av on av.document_id=r.target_document_id and av.field_id='2036daa8-bf54-4c32-a5b0-7e97158572d1' and av.owner_id is null and av.user_id is null and av.deleted_at is null where v.collection_id='24d9f103-5734-4156-a7ba-5ba08b5f21e6' and v.field_id='393fa95b-ca28-5e2b-a2d4-ff45917ef981' and v.deleted_at is null and v.owner_id is null and v.user_id is null and nullif(btrim(v.value),'') is not null and btrim(v.value) !~ '^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?$'",
  "provenanceBySource": "select coalesce(nullif(source_handle,''),'Unknown') source_handle,count(*) changes,count(*) filter(where nullif(run_id,'') is not null) with_run_id,count(*) filter(where source_document_id is not null) with_source_document_id,count(*) filter(where nullif(source_url,'') is not null) with_source_url from app_fields_values_changelog where owner_id is null and user_id is null and created_at>=now()-interval '7 days' group by source_handle order by count(*) desc",
  "runMapping": "with ids as (select distinct run_id from app_fields_values_changelog where owner_id is null and user_id is null and created_at>=now()-interval '7 days' and nullif(run_id,'') is not null) select count(*) distinct_changelog_run_ids,count(*) filter(where exists(select 1 from scraper_runs r where r.run_id=ids.run_id)) matching_text_run_id,count(*) filter(where exists(select 1 from scraper_runs r where r.id::text=ids.run_id)) matching_uuid_pk from ids",
  "scalarMismatchPatterns": "select f.default_name,v.value,count(*) rows from app_fields_values v join app_fields f on f.id=v.field_id join app_documents d on d.id=v.document_id and d.collection_id=v.collection_id where v.field_id in ('999002a8-bd19-4ed0-8ab1-d1b43c3ce7ab','c350dc6e-3735-49cb-80e9-eb5b9a761103','86c3fc8f-d37a-4193-9e83-66c6f09a897d') and v.owner_id is null and v.user_id is null and v.deleted_at is null and d.owner_id is null and d.deleted_at is null group by f.default_name,v.value",
  "graphIntegrity": "select f.collection_id,f.id field_id,f.default_name,count(*) relations,count(*) filter(where s.id is null) missing_source,count(*) filter(where s.deleted_at is not null) deleted_source,count(*) filter(where t.id is null) missing_target,count(*) filter(where t.deleted_at is not null) deleted_target,count(*) filter(where s.owner_id is not null or t.owner_id is not null) nonshared_endpoint,count(*) filter(where s.collection_id is distinct from f.collection_id or r.collection_id is distinct from f.collection_id or t.collection_id is distinct from f.reference_id) wrong_collection,count(*) filter(where coalesce(r.is_mask,false)) masks from app_fields_relations r join app_fields f on f.id=r.field_id and f.owner_id is null and f.deleted_at is null and f.field_type::text='relation' left join app_documents s on s.id=r.source_document_id left join app_documents t on t.id=r.target_document_id where r.owner_id is null and r.deleted_at is null group by f.collection_id,f.id,f.default_name",
  "collectionPopulation": "select d.collection_id,count(*) population,count(*) filter(where nullif(btrim(d.name),'') is not null) with_document_name,count(*) filter(where nullif(btrim(d.name),'') is null) blank_document_name,count(*) filter(where cardinality(d.sources)>0) with_sources,count(*) filter(where d.location is not null) with_location_json,count(*) filter(where d.geometry is not null) with_geometry_json,count(*) filter(where nullif(btrim(d.file_url),'') is not null) with_file_url,count(*) filter(where nullif(btrim(d.project_id),'') is not null) with_project_id,min(d.created_at) first_ingestion,max(d.created_at) last_ingestion from app_documents d where d.deleted_at is null and d.owner_id is null group by d.collection_id",
  "fileEvidenceSchema": "select table_name,column_name,data_type from information_schema.columns where table_schema='public' and table_name in ('data_file_meta','data_file_pages','data_file_chunks','data_ingestion_keys') order by table_name,ordinal_position",
  "unclassified": "select count(*) documents,count(*) filter(where c.id is null) missing_collection,count(*) filter(where c.owner_id is not null) nonshared_collection from app_documents d left join app_collections c on c.id=d.collection_id where d.owner_id is null and d.deleted_at is null",
  "duplicates": "with vals as (select document_id,max(value) filter(where field_id='3c5428c9-5790-454d-bbbf-b4af42995fcc') source_id,max(value) filter(where field_id='c0173c63-4571-4124-9b17-7f561455ddf8') posting_id from app_fields_values where collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' and field_id in ('3c5428c9-5790-454d-bbbf-b4af42995fcc','c0173c63-4571-4124-9b17-7f561455ddf8') and owner_id is null and user_id is null and deleted_at is null group by document_id) select source_id,posting_id,count(*) projects,array_agg(v.document_id) project_ids from vals v join app_documents d on d.id=v.document_id and d.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' and d.owner_id is null and d.deleted_at is null where nullif(btrim(posting_id),'') is not null and nullif(btrim(source_id),'') is not null group by source_id,posting_id having count(*)>1",
  "invalidMoneyDates": "select v.document_id,v.id value_id,v.field_id,f.default_name,left(v.value,120) observed_value,sv.value source_id,av.value agency_id from app_fields_values v join app_documents d on d.id=v.document_id and d.collection_id=v.collection_id and d.owner_id is null and d.deleted_at is null join app_fields f on f.id=v.field_id and f.collection_id=v.collection_id left join app_fields_values sv on sv.document_id=d.id and sv.field_id='3c5428c9-5790-454d-bbbf-b4af42995fcc' and sv.owner_id is null and sv.user_id is null and sv.deleted_at is null left join app_fields_values av on av.document_id=d.id and av.field_id='2036daa8-bf54-4c32-a5b0-7e97158572d1' and av.owner_id is null and av.user_id is null and av.deleted_at is null where v.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' and v.owner_id is null and v.user_id is null and v.deleted_at is null and v.field_id in ('10605549-74f6-44b7-a66a-9b05c0434e71','ac721535-c2db-4704-ae2a-66f29ef10355','6be4aac3-de84-4ede-924c-6df21a2070bf') and nullif(btrim(v.value),'') is not null and ((f.field_type::text='number' and btrim(v.value) !~ '^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?$') or (f.field_type::text='date' and parse_stored_date_value(v.value) is null))",
  "fileGraphConvention": "select r.collection_id,r.field_id,s.collection_id source_collection,t.collection_id target_collection,count(*) relations,count(*) filter(where s.owner_id is null and s.deleted_at is null and t.owner_id is null and t.deleted_at is null) active_shared_endpoints from app_fields_relations r left join app_documents s on s.id=r.source_document_id left join app_documents t on t.id=r.target_document_id where r.field_id='c5acb3f3-4986-4c21-86eb-46f4e1959dcd' and r.owner_id is null and r.deleted_at is null group by r.collection_id,r.field_id,s.collection_id,t.collection_id",
  "sharedGraphIssues": "select r.id relation_id,r.collection_id stored_collection_id,f.id field_id,f.collection_id expected_source_collection_id,f.reference_id expected_target_collection_id,s.id source_document_id,s.collection_id source_collection_id,t.id target_document_id,t.collection_id target_collection_id,s.deleted_at source_deleted_at,t.deleted_at target_deleted_at,(s.owner_id is not null) source_is_tenant,(t.owner_id is not null) target_is_tenant from app_fields_relations r join app_fields f on f.id=r.field_id and f.owner_id is null and f.deleted_at is null and f.field_type::text='relation' join app_documents s on s.id=r.source_document_id and s.owner_id is null and s.deleted_at is null left join app_documents t on t.id=r.target_document_id where r.owner_id is null and r.deleted_at is null and coalesce(r.is_mask,false)=false and r.field_id in ('52f81f92-ad96-45de-a40a-acbcb3a8ecd6','51c98a56-5a21-45d5-b8c8-f4d9ff77eb59','22e68628-f379-4093-bf94-dcc8b0ff15f8','5710670e-76d6-497b-8538-59a891fbe2ef','c5acb3f3-4986-4c21-86eb-46f4e1959dcd') and (t.id is null or t.owner_id is not null or t.deleted_at is not null or t.collection_id is distinct from f.reference_id or s.collection_id is distinct from f.collection_id) order by r.field_id,r.id",
  "fileEvidencePopulation": "select count(*) meta_rows,count(distinct m.document_id) files_with_meta,count(*) filter(where nullif(btrim(m.sha256),'') is not null) with_checksum,count(*) filter(where m.size_bytes>0) positive_size,count(*) filter(where m.meta is not null and m.meta<>'{}'::jsonb) with_meta,count(*) filter(where m.payload is not null and m.payload<>'{}'::jsonb) with_payload from data_file_meta m join app_documents d on d.id=m.document_id and d.collection_id='4e2db31b-a020-4d52-b8c3-76f5b9c169b1' and d.deleted_at is null and d.owner_id is null",
  "filePagesPopulation": "select count(*) page_rows,count(distinct p.document_id) files_with_pages,count(*) filter(where p.page_number is null or p.page_number<1) invalid_page_number from data_file_pages p join app_documents d on d.id=p.document_id and d.collection_id='4e2db31b-a020-4d52-b8c3-76f5b9c169b1' and d.deleted_at is null and d.owner_id is null",
  "fileReverseReconciliation": "with reversed as materialized (\nselect r.source_document_id file_id,r.target_document_id project_id\nfrom app_fields_relations r join app_documents f on f.id=r.source_document_id and f.collection_id='4e2db31b-a020-4d52-b8c3-76f5b9c169b1' and f.owner_id is null and f.deleted_at is null join app_documents p on p.id=r.target_document_id and p.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' and p.owner_id is null and p.deleted_at is null\nwhere r.field_id='c5acb3f3-4986-4c21-86eb-46f4e1959dcd' and r.owner_id is null and r.deleted_at is null and coalesce(r.is_mask,false)=false\n), facts as (\n select *, exists(select 1 from app_fields_relations r where r.source_document_id=reversed.project_id and r.target_document_id=reversed.file_id and r.field_id='c5acb3f3-4986-4c21-86eb-46f4e1959dcd' and r.owner_id is null and r.deleted_at is null and coalesce(r.is_mask,false)=false) normal_counterpart,\n exists(select 1 from app_fields_relations r join app_documents f on f.id=r.target_document_id and f.collection_id='4e2db31b-a020-4d52-b8c3-76f5b9c169b1' and f.owner_id is null and f.deleted_at is null where r.source_document_id=reversed.project_id and r.field_id='c5acb3f3-4986-4c21-86eb-46f4e1959dcd' and r.owner_id is null and r.deleted_at is null and coalesce(r.is_mask,false)=false) has_any_normal_file\n from reversed\n)\nselect count(*) valid_reversed_links,count(*) filter(where normal_counterpart) with_forward_counterpart,count(*) filter(where not normal_counterpart) without_forward_counterpart,count(distinct project_id) projects_with_reversed_links,count(distinct project_id) filter(where not has_any_normal_file) projects_only_reversed from facts",
  "assignmentEligibility": "select d.id project_id,s.name lifecycle,v.value lifecycle_id from app_documents d left join app_fields_values v on v.document_id=d.id and v.field_id='de73e8b6-10f1-4d96-85ac-fd76a7562f3d' and v.owner_id is null and v.user_id is null and v.deleted_at is null left join app_documents s on s.id=case when v.value ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' then v.value::uuid end and s.owner_id is null and s.deleted_at is null and s.collection_id='e39f0224-6080-4244-9fd9-95546954f96f' where d.id in ('003f96eb-f93e-5d7f-ac27-311818d1dadf','0909872a-b60a-5371-80e8-05fff2fe51e0','0b2573c3-1c1b-5f0b-ac89-db51e0e7206e','1b5b1a3d-2ea4-5199-890d-1a3803f986b5','1fffe517-6fba-50e6-8419-0831dc087ac7','20563b96-bad1-55d8-951b-ef4b81cc9392','2a8c209b-d815-5092-b3e4-e8d8300fec46','324cae12-f5df-515a-ac37-7cb5492ab1b7','36a3fb2e-28a9-544f-8b10-18ac41f23a3c','422a792d-b924-5176-8f72-e02e45493802','4844bb3a-484f-58a6-bd50-b083b3dd9e3f','535a641a-e6da-515d-af03-c22e39b33adc','611f65a2-937a-55fd-9b2f-6585b421739d','6135ab67-1fd6-5b35-aa8a-242c4b903fbb','628f6f2d-529c-5eb9-9e66-3701aad774ea','6682f918-0701-576a-9345-7fdf3dde45c9','672d76e0-9063-5885-8442-23ce23b7d8fb','697093bc-8234-503c-a912-37f08c987ba0','6c52fc5b-ff38-5dce-aa97-2685b1debc86','71671ffd-8c7f-5f50-ab3f-491231a7c8ed','837b414d-46e2-59f9-a304-557176c48d60','883ef8b6-ce7e-5529-92ab-3e081b09ce77','8ac62968-fada-5f39-8219-92303159db1b','8e17da03-4aa1-58a3-9dc6-4aa6a2540d47','91e5b560-89f8-5efc-a98d-99e063c50c8b','92e95dc2-0f49-5993-a50b-73ccaffcf2ea','96d17b83-1fd6-5833-a3db-da753d4ba69a','98c7e74c-1a6a-5c48-850f-6d9625b0f85b','9a656705-abe3-5c3c-ba94-4ff53929deda','9adae32c-7fa7-5a12-997f-8f7a54d47b60','9cd472e0-d96f-593b-9494-80dd1df4f854','9f218b45-6e71-5caa-b450-d6b8d96cc255','9fa38c12-2457-52a4-a2ff-8a5b28fc8504','9fefe798-911b-582c-bd7d-1df0270902d6','a03bee0b-c488-584f-8819-63e9cba0226d','a2149382-ccc9-58ee-96b3-25718f791db5','a38612bf-1d51-5aaa-8859-1fd2f1bab4db','a543e385-a193-5e8a-a256-c4a614cec1a5','af842ebd-436b-5816-953d-a2f89fd0a5f3','b2c45e52-b70a-58a0-9365-c23296fd1a75','b4382b22-378c-5c45-9f2b-fc9368ca15b4','b9346537-803e-5edb-9792-b2fb324df37c','b97463a2-0075-58c6-af68-3cd852368ac8','ba11678f-667f-53b5-9917-9a7a478a9a9f','bc386bb5-110c-5e67-bc3e-9756e99cc125','bdab7333-8473-59a3-9391-6cc764c9a9d1','bde90c00-abb2-5675-82cf-3511995a14c6','bec66ee8-563f-5cb1-8be4-d5c7c14290ca','bfcbc5a4-4eb1-5d7c-a905-7ca7bf9ede88','bff497fa-9939-596a-b4db-ef8811475f2a','c3c2fb4c-274a-5d20-8d90-d403fce01eb1','c3ce8bc9-7f78-587c-8662-057235f16362','c6cc7031-6fa1-55e8-8cb7-a06d13d6491d','ca1cab69-a096-5cac-9df2-533942b6c2a1','ce8a023e-12e8-5497-b6f3-02858551d98f','d19b20f7-9dda-56fc-9077-c3b53f62c970','d44686b1-b541-56af-bcb5-8340c336c6ce','d4fa2664-4e09-5b79-8012-66d01f601251','d7fbcef4-c7ea-5858-bfd8-6de7e65cd822','d8403ff5-6ee1-51bd-a801-58e180a88487','db73bc87-7dbc-568f-83cf-29e851e84e21','deb3a07d-c53b-5c89-9253-fe78cb17e9ef','dfe1f18d-7968-5ca9-847f-f131a53b0268','e1d37006-dd72-5e56-9274-e67eda608110','e6fe9d6d-9bd5-5aa1-9696-6a10caa01c54','ec1fb8b6-99ad-553e-ba80-f7fbef798ce0','f11aea2c-3e4b-5df8-bbe0-3980dda35d54','f2b88804-8f2f-5f7d-afb3-85aeb48b267f','f4f376c8-077f-5b48-8726-ff068e33fd6a','f5c0362b-dc4b-588c-83f2-b2832d341f4d','f733e9f8-7244-5025-ac75-35ce1a071514','f7a3d477-7352-50e4-9307-a3ab3500d833','f9eb329c-800e-5863-aec3-f76a24720100','fa2865cb-ca0b-5df6-8422-fd8bcdddae51','fb572b0f-6b44-5334-b160-ee83b5b3a51a','fbfb8035-512e-52fe-8a77-273aaca2f5c9','fe3ec4e8-bbf9-51bc-afe8-0ca7ce476ab3') and d.owner_id is null and d.deleted_at is null",
  "location": "select f.id field_id,f.collection_id,count(*) populated_location_values,count(*) filter(where safe_geojson_point(v.value) is not null) parseable_point from app_fields f join app_fields_values v on v.field_id=f.id and v.collection_id=f.collection_id and v.owner_id is null and v.user_id is null and v.deleted_at is null join app_documents d on d.id=v.document_id and d.collection_id=f.collection_id and d.owner_id is null and d.deleted_at is null where f.field_type::text='location' and f.owner_id is null and f.deleted_at is null and nullif(btrim(v.value),'') is not null group by f.id,f.collection_id",
  "tagIssues_bc573d3e-512e-4617-8859-4b05869b0698": "with refs as (\nselect v.document_id,v.field_id,f.reference_id,f.field_type,v.value,\ncase when btrim(v.value) ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$' then array[btrim(v.value)::uuid] when v.value ~ '^[[:space:]]*\\[[[:space:]]*(\"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\"([[:space:]]*,[[:space:]]*\"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\")*)?[[:space:]]*\\][[:space:]]*$' then array(select jsonb_array_elements_text(v.value::jsonb)::uuid) else null::uuid[] end ids\nfrom app_fields_values v join app_fields f on f.id=v.field_id and f.collection_id=v.collection_id and f.owner_id is null and f.deleted_at is null and f.field_type::text in ('tag','tags') and f.reference_id is not null join app_documents d on d.id=v.document_id and d.collection_id=v.collection_id and d.owner_id is null and d.deleted_at is null where v.collection_id='bc573d3e-512e-4617-8859-4b05869b0698' and v.owner_id is null and v.user_id is null and v.deleted_at is null\n), facts as (select *, exists(select 1 from unnest(ids) x left join app_documents t on t.id=x where t.id is null or t.owner_id is not null or t.deleted_at is not null or t.collection_id is distinct from refs.reference_id) bad_target from refs)\nselect document_id,field_id,reference_id,ids,bad_target,(field_type::text='tag' and cardinality(ids)>1) cardinality_conflict from facts where bad_target or (field_type::text='tag' and cardinality(ids)>1)",
  "tagIssues_0950f3b8-5ff7-4867-9162-a9a30d034539": "with refs as (\nselect v.document_id,v.field_id,f.reference_id,f.field_type,v.value,\ncase when btrim(v.value) ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$' then array[btrim(v.value)::uuid] when v.value ~ '^[[:space:]]*\\[[[:space:]]*(\"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\"([[:space:]]*,[[:space:]]*\"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\")*)?[[:space:]]*\\][[:space:]]*$' then array(select jsonb_array_elements_text(v.value::jsonb)::uuid) else null::uuid[] end ids\nfrom app_fields_values v join app_fields f on f.id=v.field_id and f.collection_id=v.collection_id and f.owner_id is null and f.deleted_at is null and f.field_type::text in ('tag','tags') and f.reference_id is not null join app_documents d on d.id=v.document_id and d.collection_id=v.collection_id and d.owner_id is null and d.deleted_at is null where v.collection_id='0950f3b8-5ff7-4867-9162-a9a30d034539' and v.owner_id is null and v.user_id is null and v.deleted_at is null\n), facts as (select *, exists(select 1 from unnest(ids) x left join app_documents t on t.id=x where t.id is null or t.owner_id is not null or t.deleted_at is not null or t.collection_id is distinct from refs.reference_id) bad_target from refs)\nselect document_id,field_id,reference_id,ids,bad_target,(field_type::text='tag' and cardinality(ids)>1) cardinality_conflict from facts where bad_target or (field_type::text='tag' and cardinality(ids)>1)",
  "tagIssues_f5219a3f-83ce-5f77-bfad-e13a2d591184": "with refs as (\nselect v.document_id,v.field_id,f.reference_id,f.field_type,v.value,\ncase when btrim(v.value) ~ '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$' then array[btrim(v.value)::uuid] when v.value ~ '^[[:space:]]*\\[[[:space:]]*(\"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\"([[:space:]]*,[[:space:]]*\"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\")*)?[[:space:]]*\\][[:space:]]*$' then array(select jsonb_array_elements_text(v.value::jsonb)::uuid) else null::uuid[] end ids\nfrom app_fields_values v join app_fields f on f.id=v.field_id and f.collection_id=v.collection_id and f.owner_id is null and f.deleted_at is null and f.field_type::text in ('tag','tags') and f.reference_id is not null join app_documents d on d.id=v.document_id and d.collection_id=v.collection_id and d.owner_id is null and d.deleted_at is null where v.collection_id='f5219a3f-83ce-5f77-bfad-e13a2d591184' and v.owner_id is null and v.user_id is null and v.deleted_at is null\n), facts as (select *, exists(select 1 from unnest(ids) x left join app_documents t on t.id=x where t.id is null or t.owner_id is not null or t.deleted_at is not null or t.collection_id is distinct from refs.reference_id) bad_target from refs)\nselect document_id,field_id,reference_id,ids,bad_target,(field_type::text='tag' and cardinality(ids)>1) cardinality_conflict from facts where bad_target or (field_type::text='tag' and cardinality(ids)>1)",
  "countryRepresentations": "select f.id field_id,f.default_name,case when v.value is null or btrim(v.value)='' then 'Blank' when btrim(v.value)~'^[A-Za-z ]+$' then 'Plain label' when btrim(v.value) like '[%' then 'Array with unresolved representation' else 'Other unresolved representation' end representation,count(*) rows from app_fields_values v join app_fields f on f.id=v.field_id join app_documents d on d.id=v.document_id and d.collection_id=v.collection_id and d.owner_id is null and d.deleted_at is null where v.owner_id is null and v.user_id is null and v.deleted_at is null and v.field_id='f1a2b3c4-d5e6-7890-abcd-ef1234567894' group by f.id,f.default_name,representation",
  "failurePatterns": "select task_name,case when error_log ilike '%no module named%' or error_log ilike '%ModuleNotFoundError%' then 'Missing Python dependency' when error_log ilike '%permission denied%' then 'Permission denied' when error_log ilike '%invalid input syntax%' then 'Invalid input syntax' when error_log ilike '%does not exist%' then 'Missing database or runtime object' when error_log ilike '%timed out%' or error_log ilike '%timeout%' then 'Timeout' when error_log ilike '%401%' or error_log ilike '%unauthorized%' then 'Authorization related text' when error_log ilike '%403%' then 'Forbidden response text' when error_log ilike '%502%' or error_log ilike '%503%' then 'Upstream service response text' when error_log ilike '%KeyError%' then 'KeyError' when error_log ilike '%TypeError%' then 'TypeError' when error_log ilike '%ValueError%' then 'ValueError' else 'Unclassified error' end error_pattern,count(*) failed_runs,min(started_at) first_failure,max(started_at) latest_failure from scraper_runs where started_at>=now()-interval '7 days' and status='failed' group by task_name,error_pattern order by count(*) desc"
}
