-- V25: durable background Tasks (Story 4.2, FR-22, FR-23). -- -- Одна строка `tasks` — принятая durable работа. Идентификатор Task (UUIDv7), owner scope, -- authorization scope, запрошенный Tool и точная published-версия, canonical fingerprint входа, -- support IDs (Request/Trace/parent TraceContext независимо от sampling) и bounded envelopes -- закрепляются в той же PostgreSQL transaction, что и dispatch intent -- (`execution_admission_entries.task_id`) и его queue slot. До commit наружу не выдаётся ни ID, ни ack. -- -- `scoped_key` — digest caller-ключа по полному (workspace, owner kind/id, authorization scope, -- operation) scope; UNIQUE по этому набору и есть защита идемпотентности. Повтор того же ключа с тем -- же canonical fingerprint возвращает сохранённую Task, другой fingerprint — typed conflict. -- -- `retain_until` — срок хранения записи, а не execution deadline: пока запись в retention, replay -- находит тот же Task. Удаление terminal-записей возможно только после retain_until; queued/working/ -- input_required/outcome_unknown не удаляются никогда ради лимита. -- -- Строка не хранит credentials, bearer/prepared headers и confirmation tokens: секретные поля входа -- редактируются до записи, а approval хранится безопасной ссылкой. create table tasks ( task_id uuid not null, workspace_id text not null, owner_kind text not null, owner_id text not null, authorization_scope text not null, operation_id text not null, operation_version integer not null, scoped_key text not null, input_fingerprint text not null, request_id text not null, trace_id text not null, traceparent text not null, trace_sampled boolean not null, policy_mode text not null, approval_reference text, state text not null, version integer not null default 1, attempts integer not null default 0, input_envelope jsonb not null, progress_envelope jsonb, result_envelope jsonb, error_envelope jsonb, execution_deadline timestamptz not null, retain_until timestamptz not null, created_at timestamptz not null default now(), updated_at timestamptz not null default now(), constraint tasks_pkey primary key (task_id), constraint tasks_workspace_check check (octet_length(workspace_id) between 1 and 160), constraint tasks_owner_kind_check check (owner_kind in ('agent', 'admin')), constraint tasks_owner_check check (octet_length(owner_id) between 1 and 160), constraint tasks_authorization_scope_check check ( octet_length(authorization_scope) between 1 and 160 ), constraint tasks_operation_check check (octet_length(operation_id) between 1 and 160), constraint tasks_operation_version_check check (operation_version >= 1), constraint tasks_scoped_key_check check (octet_length(scoped_key) between 16 and 128), constraint tasks_fingerprint_check check (octet_length(input_fingerprint) = 64), constraint tasks_request_check check (octet_length(request_id) between 1 and 128), constraint tasks_trace_check check (octet_length(trace_id) = 32), constraint tasks_traceparent_check check (octet_length(traceparent) = 55), constraint tasks_policy_mode_check check ( policy_mode in ('synchronous', 'background', 'adaptive') ), constraint tasks_state_check check ( state in ('queued', 'working', 'input_required', 'outcome_unknown', 'completed', 'failed', 'cancelled') ), constraint tasks_lifecycle_check check (version >= 1 and attempts >= 0), constraint tasks_retention_check check (retain_until >= execution_deadline), constraint tasks_approval_check check ( approval_reference is null or octet_length(approval_reference) between 1 and 160 ) ); create unique index tasks_scoped_key_uidx on tasks (workspace_id, owner_kind, owner_id, authorization_scope, operation_id, scoped_key); create index tasks_retention_idx on tasks (state, retain_until, task_id); create index tasks_deadline_idx on tasks (state, execution_deadline, task_id); -- Dispatch intent: прежняя reserved-строка admission становится точкой связи Task и capacity. -- FK без cascade: удаление принятой Task не маскируется housekeeping-удалением intent. alter table execution_admission_entries add column task_id uuid, add constraint execution_admission_entries_task_fkey foreign key (task_id) references tasks (task_id); create unique index execution_admission_entries_task_uidx on execution_admission_entries (task_id) where task_id is not null;