fix(rectification): harden v9 runtime after adversarial review

This commit is contained in:
Jesse
2026-08-11 17:57:47 +08:00
parent 724fb64c1a
commit eddd52d1f3
6 changed files with 446 additions and 66 deletions
@@ -464,6 +464,41 @@ begin
raise exception 'agentic_rectification_candidate_time_not_allowed' using errcode = 'P0001';
end if;
-- Idempotent replay: this candidate/time was already accepted for the
-- case. The profile legitimately carries the accepted time now (the
-- baseline snapshot is intentionally stale after acceptance), so the replay
-- validates the profile against the accepted selection instead of the
-- baseline. This mirrors the pre-v9 accept_agentic_rectification_candidate
-- semantics and keeps retries/double-clicks idempotent.
if v_result.selected_time is not null then
if v_result.selected_time is distinct from p_time
or v_case.accepted_time is distinct from p_time then
raise exception 'agentic_rectification_candidate_already_selected' using errcode = 'P0001';
end if;
select * into v_profile
from public.profiles
where id = p_user_id
for update;
if not found
or v_profile.active_birth_time is distinct from v_result.selected_time
or v_profile.birth_time is distinct from v_result.selected_time
or v_profile.birth_time_status is distinct from (
case when v_result.selection_kind = 'engine_confirmed' then 'confirmed' else 'accepted' end
) then
raise exception 'agentic_rectification_candidate_profile_changed' using errcode = 'P0001';
end if;
return jsonb_build_object(
'success', true,
'saved_time', pg_catalog.to_char(p_time, 'HH24:MI'),
'status', case when v_result.selection_kind = 'engine_confirmed' then 'confirmed' else 'accepted' end,
'result_id', v_result.id,
'case_status', v_case.status,
'idempotent', true
);
end if;
-- Fresh acceptance: the profile must still match the case baseline before
-- any write (an engine-confirmed replay already returned above).
v_snapshot := v_case.baseline_birth_snapshot;
select * into v_profile
from public.profiles
@@ -484,21 +519,6 @@ begin
raise exception 'agentic_rectification_candidate_profile_changed' using errcode = 'P0001';
end if;
if v_result.selected_time is not null then
if v_result.selected_time is distinct from p_time
or v_case.accepted_time is distinct from p_time then
raise exception 'agentic_rectification_candidate_already_selected' using errcode = 'P0001';
end if;
return jsonb_build_object(
'success', true,
'saved_time', pg_catalog.to_char(p_time, 'HH24:MI'),
'status', case when v_result.selection_kind = 'engine_confirmed' then 'confirmed' else 'accepted' end,
'result_id', v_result.id,
'case_status', v_case.status,
'idempotent', true
);
end if;
if exists (
select 1
from public.agentic_rectification_results newer
@@ -618,6 +638,9 @@ begin
if v_case.confirmed_time is distinct from p_time then
raise exception 'agentic_rectification_case_already_confirmed' using errcode = 'P0001';
end if;
select id into v_result.id
from public.agentic_rectification_results
where id = p_result_id and user_id = p_user_id and case_id = p_case_id;
return jsonb_build_object(
'success', true,
'saved_time', pg_catalog.to_char(v_case.confirmed_time, 'HH24:MI'),