-- V18: typed exact Resource bindings Agent (Story 3.7 AC04, FR-15/FR-16). -- -- Отдельная typed-таблица вместо JSON-поля: строка адресует точную -- Published Resource Version (`resource_publications`) для точной версии Agent -- (`agent_versions`). `latest`/floating версия невозможна, потому что FK указывает -- на конкретную строку публикации. -- -- Workspace-равенство обеспечивается на ОБОИХ концах composite FK: -- * (workspace_id, agent_id) -> agents(workspace_id, id); -- * (workspace_id, resource_id, resource_version) -> resource_publications(...). -- Поэтому ни Agent другого workspace, ни чужая/неопубликованная Resource Version -- не могут стать частью binding. -- -- Published Agent Version immutable: V18-local trigger function проверяет и OLD, -- и NEW ключ строки. Canonical V9 function при UPDATE смотрит только на NEW, -- поэтому перенос binding из опубликованного snapshot в Draft-версию другого -- Agent проходил бы через неё без отказа. V9/V17 SQL не изменяются. -- Immutable guard V18: отказ, если затронута опубликованная версия Agent — -- либо покидаемая (OLD), либо новая (NEW). create function crank_reject_published_agent_resource_binding_mutation() returns trigger language plpgsql as $$ declare bound_status text; begin if tg_op in ('UPDATE', 'DELETE') then select status into bound_status from agent_versions where agent_id = old.agent_id and version = old.agent_version; if bound_status = 'published' then raise exception 'published Agent catalog bindings are immutable' using errcode = '23514'; end if; end if; if tg_op in ('INSERT', 'UPDATE') then select status into bound_status from agent_versions where agent_id = new.agent_id and version = new.agent_version; if bound_status = 'published' then raise exception 'published Agent catalog bindings are immutable' using errcode = '23514'; end if; end if; if tg_op = 'DELETE' then return old; end if; return new; end; $$; create unique index agents_workspace_id_idx on agents(workspace_id, id); create table agent_resource_bindings ( workspace_id text not null, agent_id text not null, agent_version integer not null, resource_id text not null, resource_version integer not null, constraint agent_resource_bindings_agent_version_check check (agent_version >= 1), constraint agent_resource_bindings_resource_version_check check (resource_version >= 1), primary key (agent_id, agent_version, resource_id, resource_version), constraint agent_resource_bindings_agent_workspace_fkey foreign key (workspace_id, agent_id) references agents(workspace_id, id) on delete cascade, constraint agent_resource_bindings_agent_version_fkey foreign key (agent_id, agent_version) references agent_versions(agent_id, version) on delete cascade, constraint agent_resource_bindings_publication_fkey foreign key (workspace_id, resource_id, resource_version) references resource_publications(workspace_id, resource_id, version) on delete cascade ); create trigger agent_resource_bindings_immutable_guard before insert or update or delete on agent_resource_bindings for each row execute function crank_reject_published_agent_resource_binding_mutation(); create index agent_resource_bindings_resource_idx on agent_resource_bindings (workspace_id, resource_id, resource_version);