Files
Jyotisha/docs/operations/rect-p0-aggregate-20261116-v2.sql

226 lines
13 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 v2, 2026-11-16. Internal authorized audit only.
-- Business schema through staging 052e8ab6, including the chart-choice records,
-- adoption sources, annual report and marriage-match migrations.
-- Counts describe stored state at execution time and existing reports. They
-- cannot establish adoption frequency or which rule a report used at generation.
-- Output sections are named by metric. unknown_breakdown is the separate
-- unknown-profile table, grouped only by history, receipt and contract presence.
-- Unknown means neither safe nor subject to automatic withdrawal.
-- Zero is visible; 1–4 is masked. Suppress the unknown and conflict totals if
-- any of their groups is masked. No other group totals are emitted. This does not
-- guarantee anonymity. Review combined disclosure before sharing results.
-- No profile-bound browser download ledger. Legacy standalone match rows have
-- no status field; rows without a subject binding cannot be attributed.
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'
), active as (
select s.*, r.id as receipt_id,
coalesce(jsonb_typeof(r.detail -> 'provenance') = 'object'
or jsonb_typeof(r.detail -> 'decision') = 'object', false) as has_receipt,
exists (
select 1 from public.chart_choice_records 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 public.chart_choice_records 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')
), record_evidence as (
select r.id, r.user_id, r.chart_profile_id,
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,
r.rule
from public.chart_choice_records r
), 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)
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)
), 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')
), 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 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 '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 = 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;