-- V30: continuation through validated input (Story 4.5). alter table tasks add column if not exists effective_input_fingerprint text; update tasks set effective_input_fingerprint = input_fingerprint where effective_input_fingerprint is null; alter table tasks alter column effective_input_fingerprint set not null; alter table tasks add constraint tasks_effective_input_fingerprint_check check ( length(effective_input_fingerprint) = 64 and effective_input_fingerprint ~ '^[0-9a-f]{64}$' ); create table if not exists task_input_requests ( task_id uuid primary key references tasks (task_id) on delete cascade, wait_id text not null, expected_version integer not null, schema_revision integer not null, schema_format_version integer not null, fingerprint_format_version integer not null, schema_snapshot jsonb not null, projected_schema jsonb not null, prompt_code text not null, missing_fields jsonb not null default '[]'::jsonb, expires_at timestamptz not null, created_at timestamptz not null default now(), accepted_at timestamptz, update_key_digest text, canonical_fingerprint text, effective_input_fingerprint text, committed_state text, committed_version integer, constraint task_input_requests_wait_unique unique (wait_id), constraint task_input_requests_prompt_check check (octet_length(prompt_code) between 1 and 64), constraint task_input_requests_receipt_check check ( (accepted_at is null and update_key_digest is null and canonical_fingerprint is null and effective_input_fingerprint is null and committed_state is null and committed_version is null) or (accepted_at is not null and update_key_digest is not null and canonical_fingerprint is not null and effective_input_fingerprint is not null and committed_state is not null and committed_version is not null) ) ); -- Not unique on purpose: several waiting Tasks may share one absolute TTL value (the same pinned -- policy deadline), and an expired/cancelled wait keeps its row. Per-Task uniqueness comes from the -- primary key and task_input_requests_wait_unique. create index if not exists task_input_requests_active_ttl_idx on task_input_requests (expires_at) where accepted_at is null; create table if not exists task_input_audit ( id bigint generated always as identity primary key, task_id uuid not null references tasks (task_id) on delete cascade, wait_id text not null, event text not null, from_state text, to_state text, from_version integer, to_version integer, actor text, reason_code text, created_at timestamptz not null default now(), constraint task_input_audit_event_check check (event in ('input_required','input_accepted','input_expired')), constraint task_input_audit_actor_check check (actor is null or octet_length(actor) between 1 and 160), constraint task_input_audit_reason_check check (reason_code is null or octet_length(reason_code) between 1 and 64) ); create index if not exists task_input_audit_task_idx on task_input_audit (task_id, id);