feat(supabase): verification retry by effective cause

resource_limit (intrinsic to the frozen input: frame, input or output cap,
SIGXCPU, SIGABRT, SIGSYS) and verdict_refused close the round at the first
occurrence; timeout, unavailable (now also an external SIGKILL) and
lease_expired keep the 2/5/10/20 s retries up to five attempts. pgTAP 021: 89;
sandbox case k closes at attempt 1.
This commit is contained in:
bchanot committed 2026-11-01 23:01:30 +01:00
1 parent c98a9c8f82
commit a5844b9d56
8 files changed
+156 -46

No files matched your search

@@ -13,7 +13,9 @@
-- the drawings the document references;
-- * complete_sheet_verification: the verdict, checked for its binding and
-- its structure; `stale` or `malformed` write nothing;
-- * fail_sheet_verification: a technical failure, from a closed list.
-- * fail_sheet_verification: a technical failure, from a closed list;
-- transient codes retry after a delay up to the attempt limit, the
-- definitive codes (`sheet_worker_final_codes`) close the round at once.
-- The verdict itself is the core's (`compute_verdict`, crates/core); SQL
-- checks only that it answers this round and covers this document.
@@ -67,7 +69,8 @@ returns text[] language sql immutable set search_path = '' as $$
$$;
-- The worker's technical failure codes (§6.5 transient, timeout, resource,
-- interruption rows; crates/worker `verify_job`).
-- interruption rows; crates/worker `verify_job`). The worker maps the cause
-- to a code; the retry policy of each code is `sheet_worker_final_codes`.
create or replace function private.sheet_worker_codes()
returns text[] language sql immutable set search_path = '' as $$
select array[
@@ -77,6 +80,15 @@ returns text[] language sql immutable set search_path = '' as $$
]
$$;
-- The codes whose cause is fixed by the frozen input or the contract caps
-- (resource_limit) or by a verdict the SQL refuses (verdict_refused): a retry
-- would give the same answer, so the round closes at the first occurrence.
-- The others (timeout, unavailable, lease_expired) are transient.
create or replace function private.sheet_worker_final_codes()
returns text[] language sql immutable set search_path = '' as $$
select array['verification.worker.resource_limit', 'verification.worker.verdict_refused']
$$;
-- The service role only (the worker's key); anything else is refused.
create or replace function private.require_service_role()
returns void language plpgsql stable set search_path = '' as $$
@@ -367,9 +379,10 @@ begin
end;
$$;
-- A technical failure of a leased round (§6.5): the round stays `verifying`
-- and claimable after its retry delay, its code recorded, until the attempt
-- limit closes it `verification_failed` / `unavailable`. Answers `retry`, `unavailable` or
-- A technical failure of a leased round (§6.5): a transient code leaves the
-- round `verifying` and claimable after its retry delay, its code recorded,
-- until the attempt limit closes it `verification_failed` / `unavailable`; a
-- definitive code closes it at once. Answers `retry`, `unavailable` or
-- `stale` (nothing written).
create or replace function public.fail_sheet_verification(review_id uuid, lease_token uuid, code text)
returns text
@@ -382,6 +395,7 @@ as $$
declare
v_review public.anatomical_sheet_reviews%rowtype;
v_final boolean;
v_definitive boolean;
begin
perform private.require_service_role();
if code is null or not (code = any (private.sheet_worker_codes())) then
@@ -392,7 +406,9 @@ begin
or lease_token is null or v_review.lease_token is distinct from lease_token then
return 'stale';
end if;
v_final := v_review.attempts >= private.sheet_verification_attempt_limit();
v_definitive := code = any (private.sheet_worker_final_codes());
v_final := v_definitive
or v_review.attempts >= private.sheet_verification_attempt_limit();
update public.anatomical_sheet_reviews r
set failure_code = code, lease_token = null, locked_at = null,
not_before = case when v_final then null
@@ -405,6 +421,9 @@ begin
'anatomical_sheet_review', v_review.id, null, null,
jsonb_build_object('code', code, 'attempts', v_review.attempts,
'outcome', case when v_final then 'unavailable' end,
'closed_by', case when not v_final then null
when v_definitive then 'definitive'
else 'attempt_limit' end,
'retry_delay_seconds', case when not v_final then
extract(epoch from private.sheet_verification_retry_delay(v_review.attempts))::integer end));
return case when v_final then 'unavailable' else 'retry' end;
@@ -420,6 +439,7 @@ revoke all on function private.sheet_verification_lease() from public;
revoke all on function private.sheet_verification_max_verdict_bytes() from public;
revoke all on function private.sheet_verdict_codes() from public;
revoke all on function private.sheet_worker_codes() from public;
revoke all on function private.sheet_worker_final_codes() from public;
revoke all on function private.require_service_role() from public;
revoke all on function private.sheet_document_group_keys(jsonb) from public;
revoke all on function private.sheet_review_base(uuid) from public;