-- V24: authoritative execution admission state (Story 4.1, FR-21, FR-24). -- -- Один deployment-wide admission: PostgreSQL хранит cursor/fairness state, ожидающие тикеты, -- выданные permits и durable enqueue decisions, поэтому несколько процессов делят общий бюджет. -- -- `execution_admission_entries` — bounded набор: waiting (ожидает capacity), executing (scoped -- permit выдан, держится до явного release) и reserved (durable enqueue decision с зарезервированным -- queue slot; не исполняется и не рекламируется как Task до Story 4.2). -- -- Строка не хранит ни credentials, ни connection/session id, ни prepared headers, ни тело запроса: -- quota key — (workspace, agent) и (workspace, operation) без version. `operation_version` пишется -- только как pin исполняемых semantics и не участвует в квотах. -- -- `expires_at` — абсолютный deadline. Queue decisions без dispatch можно expire по deadline; активный -- permit НЕ освобождается по TTL (иначе paused/crashed owner обойдёт global limit): его снимает только -- явный release/cancel владельца. create table execution_admission_controllers ( workspace_id text not null, cursor_agent text, cursor_agent_credit integer not null default 0, cursor_tool text, cursor_tool_credit integer not null default 0, updated_at timestamptz not null default now(), constraint execution_admission_controllers_pkey primary key (workspace_id), constraint execution_admission_controllers_workspace_check check ( octet_length(workspace_id) between 1 and 160 ), constraint execution_admission_controllers_agent_credit_check check ( cursor_agent_credit between 0 and 64 ), constraint execution_admission_controllers_tool_credit_check check ( cursor_tool_credit between 0 and 64 ) ); create table execution_admission_entries ( ticket_id bigint generated always as identity, workspace_id text not null, agent_key text not null, operation_id text not null, operation_version integer not null, mode text not null, policy_mode text not null, state text not null, owner_token text not null, fingerprint text not null, created_at timestamptz not null default now(), expires_at timestamptz not null, granted_at timestamptz, constraint execution_admission_entries_pkey primary key (ticket_id), constraint execution_admission_entries_workspace_check check ( octet_length(workspace_id) between 1 and 160 ), constraint execution_admission_entries_agent_check check ( octet_length(agent_key) between 1 and 160 ), constraint execution_admission_entries_operation_check check ( octet_length(operation_id) between 1 and 160 ), constraint execution_admission_entries_version_check check (operation_version >= 1), constraint execution_admission_entries_mode_check check ( mode in ('synchronous', 'background') ), constraint execution_admission_entries_policy_mode_check check ( policy_mode in ('synchronous', 'background', 'adaptive') ), constraint execution_admission_entries_state_check check ( state in ('waiting', 'executing', 'reserved') ), constraint execution_admission_entries_owner_check check ( octet_length(owner_token) between 1 and 64 ), constraint execution_admission_entries_fingerprint_check check ( octet_length(fingerprint) between 1 and 128 ), constraint execution_admission_entries_reserved_mode_check check ( state <> 'reserved' or mode = 'background' ) ); create unique index execution_admission_entries_owner_uidx on execution_admission_entries (owner_token); create index execution_admission_entries_state_idx on execution_admission_entries (state, workspace_id, ticket_id); create index execution_admission_entries_agent_idx on execution_admission_entries (state, workspace_id, agent_key); create index execution_admission_entries_operation_idx on execution_admission_entries (state, workspace_id, operation_id);