Files
Jyotisha/docs/operations/rect-p0-aggregate-20261117-v3.sql

388 lines
22 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Rectification P0 aggregate v3, 2026-11-17. Internal authorized audit only.
-- Schema: staging 1c921c956. Check the actual columns and status constraints
-- before any audit. Record the script SHA-256, migration version, execution
-- time, and the audit role's effective row visibility.
-- Use an authorized read-only audit identity with verified cross-account
-- visibility. A caller restricted by RLS can see zero while other rows exist.
-- Do not use a superuser. The execution tool sets timeouts, for example
-- SET LOCAL statement_timeout = '30s';
-- SET LOCAL lock_timeout = '3s';
-- SET LOCAL idle_in_transaction_session_timeout = '30s';
-- On timeout or error, roll back, disconnect, and leave retries to the owner.
-- Zero is visible; 1–4 is masked. Potentially revealing unknown and conflict
-- totals are suppressed. Groups and features overlap and must not be added.
-- Even has_any exposes whether a small group exists. Keep results internal.
-- Neither these counts nor has_any = no authorize bulk withdrawal or establish
-- that legacy artifacts are safe. An old receipt does not date a report's input.
--
-- Added metric sections: provenance_without_history, artifacts_without_subject,
-- legacy_chart_side, self_choice_20261116, reports_outside_whitelist,
-- standalone_synastry_unbound, coverage_notes.
-- 20260806010000_personal_reports.sql and 20261019010000_synastry_full_report.sql
-- use plain subject UUID/text columns. The current delete_chart_subject RPC
-- (20261101010000_rectification_chart_subject.sql) removes referenced personal,
-- annual and full synastry reports. Direct person deletion can leave those
-- plain references dangling. The 20261018010000_synastry_reports_marriage_match.sql
-- trigger removes referenced standalone rows on person deletion. A prior
-- missing reference is still measurable. No subject-column FK sets IDs to null.
-- Bare nulls cannot establish prior binding; artifacts_subject_nulled has
-- no defensible count and appears in coverage_notes, never as an asserted zero.
-- The standalone unbound metric is only a total; attribution is unavailable.
-- V1 explicit side picks are user_override/override with before/after. In the
-- old engine, event evidence is evidence/event; majority, strong_override,
-- tie_keeps_reported and reported_kept come from answers or the reported side.
-- Exclude those and chart_answers. A matched old side pick remains auditable
-- independently of the withdrawal categories.
begin transaction isolation level repeatable read read only;
with subjects as (
select id as owner_id, null::uuid as subject_id, birth_time_status, birth_date,
active_birth_date, active_birth_time, active_birth_provenance as provenance
from public.profiles
union all
select user_id, id, birth_time_status, birth_date,
active_birth_date, active_birth_time, active_birth_provenance
from public.chart_profiles
where role = 'other'
), record_evidence as (
select r.id, r.user_id, r.chart_profile_id, r.saved_date, r.saved_time, r.chosen_side, r.rule,
p.provenance, p.decision, p.contract,
coalesce(p.provenance ->> 'basis', p.decision ->> 'basis')
is not distinct from 'self_choice_20261116' as is_self_choice,
coalesce(p.decision ->> 'rule', p.provenance ->> 'rule', r.rule) as receipt_rule,
coalesce(p.decision ->> 'decided_by', p.provenance ->> 'decided_by') as decided_by
from public.chart_choice_records r
join subjects s on s.owner_id = r.user_id and s.subject_id is not distinct from r.chart_profile_id
cross join lateral (
select r.detail -> 'provenance' as provenance, r.detail -> 'decision' as decision,
coalesce(r.detail #>> '{provenance,contract}',
case when jsonb_typeof(r.detail -> 'decision') = 'object'
and r.detail #> '{decision,segment}' is null
then 'chart-choice-v1' end) as contract
) p
), active as (
select s.*, r.id as receipt_id,
coalesce(jsonb_typeof(r.provenance) = 'object' or jsonb_typeof(r.decision) = 'object', false) as has_receipt,
s.provenance ->> 'basis' is not distinct from 'self_choice_20261116' as self_choice_basis,
(s.provenance ->> 'basis' is not distinct from 'self_choice_20261116'
or coalesce(r.is_self_choice, false)) as is_self_choice,
exists (
select 1 from record_evidence h
where h.user_id = s.owner_id and h.chart_profile_id is not distinct from s.subject_id
) as has_history
from subjects s
left join record_evidence r
on r.user_id = s.owner_id and r.chart_profile_id is not distinct from s.subject_id
and r.saved_date = coalesce(s.active_birth_date, s.birth_date)
and r.saved_time = s.active_birth_time
and r.id = case when s.provenance ->> 'record_id' ~* '^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$'
then (s.provenance ->> 'record_id')::uuid end
where s.active_birth_time is not null and s.birth_time_status in ('accepted', 'confirmed')
), legacy_side_receipts as (
select id, user_id as owner_id, chart_profile_id as subject_id
from record_evidence r
where r.contract = 'chart-choice-v1' and not r.is_self_choice
and r.chosen_side in ('before', 'after')
and (r.receipt_rule = 'user_override' or r.decided_by = 'override')
and coalesce(r.decided_by, 'override') = 'override'
and r.receipt_rule not in ('evidence', 'chart_answers', 'majority', 'strong_override', 'tie_keeps_reported', 'reported_kept')
and r.provenance ->> 'source' is distinct from 'user_choice_from_answers'
), legacy_side_active as (
select a.owner_id, a.subject_id
from active a
where not a.is_self_choice
and (
exists (select 1 from legacy_side_receipts r where r.id = a.receipt_id)
or (a.provenance ->> 'contract' = 'chart-choice-v1'
and a.provenance ->> 'chosen_side' in ('before', 'after')
and (a.provenance ->> 'rule' = 'user_override' or a.provenance ->> 'decided_by' = 'override')
and coalesce(a.provenance ->> 'decided_by', 'override') = 'override'
and coalesce(a.provenance ->> 'rule', 'user_override')
not in ('evidence', 'chart_answers', 'majority', 'strong_override', 'tie_keeps_reported', 'reported_kept')
and a.provenance ->> 'source' is distinct from 'user_choice_from_answers')
)
), evidence as (
-- Keep recorded, decision and active rules independent so one cannot mask another.
select 'history' as scope, r.user_id as owner_id, r.chart_profile_id as subject_id,
r.contract, r.provenance ->> 'source' as source, facts.rule, facts.decided_by, facts.rounds
from record_evidence r
cross join lateral (
values (r.provenance ->> 'rule', r.provenance ->> 'decided_by', r.provenance -> 'rounds'),
(r.decision ->> 'rule', r.decision ->> 'decided_by', r.decision -> 'rounds'),
(r.rule, coalesce(r.decision ->> 'decided_by', r.provenance ->> 'decided_by'),
coalesce(r.decision -> 'rounds', r.provenance -> 'rounds'))
) facts(rule, decided_by, rounds)
where not r.is_self_choice
union all
select 'active', a.owner_id, a.subject_id, facts.contract, facts.source, facts.rule, facts.decided_by, facts.rounds
from active a
left join record_evidence r on r.id = a.receipt_id
cross join lateral (
values (a.provenance ->> 'contract', a.provenance ->> 'source', a.provenance ->> 'rule',
a.provenance ->> 'decided_by', a.provenance -> 'rounds'),
(r.contract, r.provenance ->> 'source', r.provenance ->> 'rule',
r.provenance ->> 'decided_by', r.provenance -> 'rounds'),
(coalesce(r.contract, a.provenance ->> 'contract'), null, r.decision ->> 'rule',
r.decision ->> 'decided_by', r.decision -> 'rounds'),
(coalesce(r.contract, a.provenance ->> 'contract'), null, r.rule,
coalesce(r.decision ->> 'decided_by', r.provenance ->> 'decided_by'),
coalesce(r.decision -> 'rounds', r.provenance -> 'rounds'))
) facts(contract, source, rule, decided_by, rounds)
where not a.is_self_choice
), signals as (
select scope, owner_id, subject_id,
coalesce(bool_or(source = 'user_choice_from_answers' or rule = 'chart_answers'), false) as old_questionnaire,
coalesce(bool_or(contract = 'chart-choice-v1' and rule = 'evidence' and decided_by = 'event'), false) as old_event,
coalesce(bool_or(contract = 'segment-v1' or source = 'server_segment_midpoint'), false) as old_segment,
coalesce(bool_or(rule = 'user_override'), false) as manual_choice,
coalesce(bool_or(contract = 'chart-choice-v2' and jsonb_typeof(rounds) = 'array' and rounds <> '[]'::jsonb),
false) as chart_side,
coalesce(bool_or(source in ('reported_time_default', 'window_midpoint_default')), false) as declared_default
from evidence
group by scope, owner_id, subject_id
), active_features as (
select a.owner_id, a.subject_id, a.provenance, a.has_history, a.has_receipt,
s.old_questionnaire or (s.manual_choice and coalesce(h.old_questionnaire, false)) as old_questionnaire,
s.old_event or (s.manual_choice and coalesce(h.old_event, false)) as old_event,
s.old_segment or (s.manual_choice and coalesce(h.old_segment, false)) as old_segment,
s.chart_side or (s.manual_choice and coalesce(h.chart_side, false)) as chart_side,
s.manual_choice, s.declared_default
from active a
join signals s on s.scope = 'active' and s.owner_id = a.owner_id and s.subject_id is not distinct from a.subject_id
-- Old traces on the same subject leave a manual choice open to review even
-- without an active receipt. An audit conflict does not establish causality.
left join signals h on h.scope = 'history' and h.owner_id = a.owner_id and h.subject_id is not distinct from a.subject_id
), active_signals as (
select f.*,
(old_questionnaire::int + old_event::int + old_segment::int
+ manual_choice::int + chart_side::int + declared_default::int) as matches
from active_features f
), features as (
select f.category, count(*) filter (where f.matched) as n
from (values (true)) as zero_fill(dummy)
left join active_signals a on true
cross join lateral (
values ('old_questionnaire', a.old_questionnaire),
('old_event', a.old_event),
('segment-v1', a.old_segment),
('explicit_manual_choice', a.manual_choice and a.matches = 1),
('issue_229_chart_side', a.chart_side),
('declared_default', a.declared_default),
('conflict', a.matches >= 2),
('conflict_manual_and_old', a.matches >= 2 and a.manual_choice
and (a.old_questionnaire or a.old_event or a.old_segment or a.chart_side)),
('conflict_other', a.matches >= 2 and not (a.manual_choice
and (a.old_questionnaire or a.old_event or a.old_segment or a.chart_side))),
('unknown', a.matches = 0)
) f(category, matched)
group by f.category
), provenance_groups as (
select case when provenance ->> 'contract' in ('segment-v1', 'chart-choice-v1', 'chart-choice-v2', 'agentic-rectification-v1')
then provenance ->> 'contract' when provenance ->> 'contract' is null then 'missing' else 'other' end as contract,
case when provenance ->> 'source' in ('user_choice', 'user_choice_from_answers', 'server_segment_midpoint',
'reported_time_default', 'window_midpoint_default', 'certain')
then provenance ->> 'source' when provenance ->> 'source' is null then 'missing' else 'other' end as source,
case when provenance ->> 'rule' in ('chart_answers', 'evidence', 'user_override', 'reused_record', 'reported_time_default',
'window_midpoint_default', 'majority', 'tie_keeps_reported', 'strong_override',
'reported_kept', 'stopped', 'round_limit', 'no_line_in_range')
then provenance ->> 'rule' when provenance ->> 'rule' is null then 'missing' else 'other' end as rule
from active_signals
), active_retired as (
select owner_id, subject_id
from active_signals
where provenance ->> 'retired_reason' = 'BUG-1890' or old_questionnaire or old_event or old_segment
), historical_subjects as (
select q.owner_id, q.subject_id
from signals q
where q.scope = 'history' and (q.old_questionnaire or q.old_event or q.old_segment or q.chart_side)
and exists (
select 1 from subjects s where s.owner_id = q.owner_id and s.subject_id is not distinct from q.subject_id
)
), report_statuses(status) as (
values ('generating'), ('ready'), ('failed')
), report_types(report_type) as (
values ('personal_full'), ('personal_thematic'), ('annual_full'), ('synastry_full')
), artifacts as (
select r.id, r.user_id as owner_id,
case when r.report_type in (select report_type from report_types) then r.report_type else 'other' end as report_type,
case when r.report_type = 'synastry_full'
then array[r.chart_profile_id::text, r.partner_chart_profile_id::text, r.subject_low, r.subject_high]
else array[coalesce(r.chart_profile_id::text, 'self')] end as refs
from public.personal_reports r
union all
select r.id, r.user_id, 'legacy_synastry', array[r.subject_low, r.subject_high]
from public.synastry_reports r
where r.rules_version is null
), artifact_refs as (
select a.id, a.owner_id, a.report_type, lower(p.ref) as ref, binding.subject_id
from artifacts a
cross join lateral unnest(a.refs) p(ref)
cross join lateral (
select case when p.ref ~* '^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$'
then p.ref::uuid end as subject_id
) binding
where p.ref = 'self' or binding.subject_id is not null
), legacy_pairs as (
select id
from artifact_refs
where report_type = 'legacy_synastry'
group by id
having count(distinct ref) = 2
), current_without_history as (
select exists (
select 1 from artifact_refs ar
where ar.owner_id = s.owner_id and ar.ref = coalesce(s.subject_id::text, 'self')
and (ar.report_type <> 'legacy_synastry' or exists (select 1 from legacy_pairs p where p.id = ar.id))
) as has_artifact
from subjects s
where (s.provenance is not null and s.provenance not in ('null'::jsonb, '{}'::jsonb, '[]'::jsonb)
or (s.birth_time_status in ('accepted', 'confirmed') and s.active_birth_time is not null))
and not exists (
select 1 from record_evidence r
where r.user_id = s.owner_id and r.chart_profile_id is not distinct from s.subject_id
)
), missing_subject_artifacts as (
select distinct ar.report_type, ar.id
from artifact_refs ar
where ar.subject_id is not null
and not exists (select 1 from public.chart_profiles c where c.id = ar.subject_id)
and (ar.report_type <> 'legacy_synastry' or exists (select 1 from legacy_pairs p where p.id = ar.id))
), outside_report_counts as (
select case when r.report_type in (select report_type from report_types) then r.report_type else 'other' end as report_type,
case when r.status in (select status from report_statuses) then 'listed' else 'outside' end as status_group,
count(*) as n
from public.personal_reports r
where (r.report_type in (select report_type from report_types)) is not true
or (r.status in (select status from report_statuses)) is not true
group by 1, 2
), artifact_types(report_type) as (
select report_type from report_types union all values ('legacy_synastry'), ('other')
), outside_report_groups(report_type, status_group) as (
select report_type, 'outside' from report_types
union all values ('other', 'listed'), ('other', 'outside')
), coverage_notes(note) as (
values ('browser_downloads_outside_server'),
('delivered_chat_text_not_covered'),
('exports_outside_server'),
('artifacts_subject_nulled_cannot_be_distinguished'),
('unbound_standalone_synastry_has_no_subject_attribution')
), statuses_by_type as (
select t.report_type, s.status from report_types t cross join report_statuses s
union all select 'legacy_synastry', 'not_recorded'
), ready_reports as (
select r.report_type
from public.personal_reports r
where r.status = 'ready' and r.report_type in (select report_type from report_types)
and exists (
select 1 from active_retired s where s.owner_id = r.user_id
and (s.subject_id is not distinct from r.chart_profile_id
or (r.report_type = 'synastry_full' and r.partner_chart_profile_id = s.subject_id))
)
), historical_reports as (
select r.report_type, r.status
from public.personal_reports r
where r.report_type in (select report_type from report_types)
and exists (
select 1 from historical_subjects s where s.owner_id = r.user_id
and (s.subject_id is not distinct from r.chart_profile_id
or (r.report_type = 'synastry_full' and r.partner_chart_profile_id = s.subject_id))
)
union all
select 'legacy_synastry', 'not_recorded'
from public.synastry_reports r
where (r.rules_version is not null or exists (select 1 from legacy_pairs p where p.id = r.id))
and exists (
select 1 from historical_subjects s where s.owner_id = r.user_id
and coalesce(s.subject_id::text, 'self') in (r.subject_low, r.subject_high)
)
), presence(flag) as (
values (true), (false)
), unknown_counts as (
select h.flag as has_history, r.flag as has_receipt, m.flag as missing_contract, count(a.owner_id) as n
from presence h cross join presence r cross join presence m
left join active_signals a on a.matches = 0 and a.has_history = h.flag and a.has_receipt = r.flag
and (a.provenance ->> 'contract' is null) = m.flag
group by h.flag, r.flag, m.flag
), counts as (
select 'provenance' as metric, null::text as category, contract, source, rule,
null::text as report_type, null::text as report_status, null::boolean as has_history,
null::boolean as has_receipt, null::boolean as missing_contract, count(*) as n
from provenance_groups
group by contract, source, rule
union all
select 'provenance', null, 'missing', 'missing', 'missing', null, null, null, null, null, 0
where not exists (select 1 from provenance_groups)
union all
select 'provenance_without_history', g.category, null, null, null, null, null, null, null, null, count(s.has_artifact)
from (values ('has_artifact', true), ('no_artifact', false)) g(category, has_artifact)
left join current_without_history s using (has_artifact)
group by g.category
union all
select 'artifacts_without_subject', null, null, null, null, t.report_type, null, null, null, null, count(r.id)
from artifact_types t left join missing_subject_artifacts r using (report_type)
group by t.report_type
union all
select 'legacy_chart_side', 'active', null, null, null, null, null, null, null, null, count(*)
from legacy_side_active
union all
select 'legacy_chart_side', 'history_only', null, null, null, null, null, null, null, null, count(*)
from subjects s
where exists (select 1 from legacy_side_receipts r where r.owner_id = s.owner_id
and r.subject_id is not distinct from s.subject_id)
and not exists (select 1 from legacy_side_active a where a.owner_id = s.owner_id
and a.subject_id is not distinct from s.subject_id)
union all
select 'self_choice_20261116', 'active', null, null, null, null, null, null, null, null, count(*)
from active where self_choice_basis
union all
select 'self_choice_20261116', 'history', null, null, null, null, null, null, null, null, count(*)
from subjects s
where exists (select 1 from record_evidence r where r.user_id = s.owner_id
and r.chart_profile_id is not distinct from s.subject_id and r.is_self_choice)
union all
select 'reports_outside_whitelist', case when g.report_type = 'other' then 'outside' else 'listed' end,
null, null, null, g.report_type, g.status_group, null, null, null, coalesce(r.n, 0)
from outside_report_groups g left join outside_report_counts r using (report_type, status_group)
union all
select 'standalone_synastry_unbound', null, null, null, null, null, null, null, null, null, count(*)
from artifacts a
where a.report_type = 'legacy_synastry'
and not exists (select 1 from legacy_pairs p where p.id = a.id)
union all
select 'coverage_notes', note, null, null, null, null, null, null, null, null, null
from coverage_notes
union all
select 'feature', category, null, null, null, null, null, null, null, null, f.n
from features f
where (f.category <> 'unknown' or not exists (select 1 from unknown_counts u where u.n between 1 and 4))
and (f.category <> 'conflict' or not exists (
select 1 from features child where child.category in ('conflict_manual_and_old', 'conflict_other')
and child.n between 1 and 4
))
union all
select 'unknown_breakdown', null, null, null, null, null, null, has_history, has_receipt, missing_contract, n
from unknown_counts
union all
select 'active_retired', null, null, null, null, null, null, null, null, null, count(*) from active_retired
union all
select 'ready_reports_on_retired', null, null, null, null, t.report_type, 'ready', null, null, null, count(r.report_type)
from report_types t left join ready_reports r using (report_type)
group by t.report_type
union all
select 'reports_on_historical_sources', null, null, null, null, t.report_type, t.status, null, null, null, count(r.report_type)
from statuses_by_type t left join historical_reports r on r.report_type = t.report_type and r.status = t.status
group by t.report_type, t.status
)
select metric, category, contract, source, rule, report_type, report_status,
case has_history when true then 'yes' when false then 'no' end as has_history,
case has_receipt when true then 'yes' when false then 'no' end as has_receipt,
case missing_contract when true then 'yes' when false then 'no' end as missing_contract,
case when n is null then null when n = 0 then 'no' else 'yes' end as has_any,
case when n between 1 and 4 then '<5' else n::text end as count_display
from counts
order by metric, category, contract, source, rule, report_type, report_status,
has_history, has_receipt, missing_contract;
commit;