feat(supabase): retry delay of 2, 5, 10, 20 s for technical verification failures

M6 amended in place (applied only to the throwaway sandbox): a hidden
not_before on the round, set by fail_sheet_verification and honoured by the
claim; RPC signatures unchanged. pgTAP 021 (68), sandbox cases i (delay with
the real worker), j (inputs at the caps, 2 MiB base), k (just beyond).
This commit is contained in:
bchanot committed 2026-11-01 21:09:00 +01:00
1 parent e967ce6736
commit 5adad3709f
8 files changed
+398 -29

No files matched your search

@@ -4,7 +4,10 @@
--
-- The round row (M5 `anatomical_sheet_reviews`) is the job, with the avatar
-- lease pattern of 20261013100000_files_foundation.sql (lease token,
-- `locked_at`, a fixed lease, attempts below a limit), without backoff:
-- `locked_at`, a fixed lease, attempts below a limit), plus a progressive
-- delay for technical retries only (owner decision 2026-11-01): a failure
-- reported by the worker sets `not_before`, the claim skips the round until
-- then, so no worker sleeps and any worker honours it:
-- * claim_sheet_verifications: rounds `verifying` with no live lease;
-- * get_sheet_verification_input: the frozen document, the base, the rows of
-- the drawings the document references;
@@ -17,11 +20,28 @@
-- ===========================================================================
-- 1. Constants
-- ===========================================================================
-- The earliest time a technical retry may be claimed (null = now). Hidden:
-- the M5 grant to `authenticated` is column-level and does not list it.
alter table public.anatomical_sheet_reviews add column not_before timestamptz;
comment on column public.anatomical_sheet_reviews.not_before is
'Earliest time a technical retry may be claimed; null = now. Set by fail_sheet_verification, cleared by the claim. Service role only.';
-- Attempts before a round closes `verification_failed` / `unavailable`
-- (§6.5 « five, proposal, tuned in V1-2.3 »).
create or replace function private.sheet_verification_attempt_limit()
returns integer language sql immutable set search_path = '' as $$ select 5 $$;
-- Wait before the next claim after a technical failure at attempt n: 2 s, 5 s,
-- 10 s, then 20 s (attempt 1 itself is claimed at once). Validation and
-- integrity refusals are verdicts, never retried.
create or replace function private.sheet_verification_retry_delay(attempts integer)
returns interval language sql immutable set search_path = '' as $$
select case when attempts <= 1 then interval '2 seconds'
when attempts = 2 then interval '5 seconds'
when attempts = 3 then interval '10 seconds'
else interval '20 seconds' end
$$;
-- A lease older than this is dead: the worker crashed or lost the database.
-- The child's wall-clock deadline is far below it (crates/worker limits).
create or replace function private.sheet_verification_lease()
@@ -191,12 +211,14 @@ begin
where r.status = 'verifying'
and (r.locked_at is null or r.locked_at < now() - private.sheet_verification_lease())
and r.attempts < private.sheet_verification_attempt_limit()
and (r.not_before is null or r.not_before <= now())
order by r.submitted_at
limit v_limit
for update skip locked
), leased as (
update public.anatomical_sheet_reviews r
set lease_token = gen_random_uuid(), locked_at = now(), attempts = r.attempts + 1,
not_before = null,
-- A dead lease left no code: record the interruption it was.
failure_code = case when r.locked_at is not null then 'verification.worker.lease_expired' else r.failure_code end
from picked
@@ -344,8 +366,8 @@ end;
$$;
-- A technical failure of a leased round (§6.5): the round stays `verifying`
-- and claimable, its code recorded, until the attempt limit closes it
-- `verification_failed` / `unavailable`. Answers `retry`, `unavailable` or
-- and claimable after its retry delay, its code recorded, until the attempt
-- limit closes it `verification_failed` / `unavailable`. 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
@@ -370,6 +392,8 @@ begin
v_final := 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
else now() + private.sheet_verification_retry_delay(v_review.attempts) end,
status = case when v_final then 'verification_failed'::public.sheet_round_status else r.status end,
failure_outcome = case when v_final then 'unavailable'::public.sheet_verification_outcome else null end
where r.id = v_review.id;
@@ -377,7 +401,9 @@ begin
case when v_final then 'anatomical_sheet_review.verification_failed' else 'anatomical_sheet_review.verification_attempt_failed' end,
'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));
'outcome', case when v_final then 'unavailable' 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;
end;
$$;
@@ -386,6 +412,7 @@ $$;
-- 4. Privileges: the service role only
-- ===========================================================================
revoke all on function private.sheet_verification_attempt_limit() from public;
revoke all on function private.sheet_verification_retry_delay(integer) from public;
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;