WeKnora/migrations/versioned/000116_agent_allowed_tools_...

23 lines
1.2 KiB
MySQL
Raw Normal View History

fix(agent): make agent tool selection match the tools the runtime registers (#4211) The tools a smart-reasoning agent may use were defined in three places that never checked each other: the agent editor's checkboxes, the stored allowed_tools, and what registerTools registers. The editor drew from a hard-coded list that had drifted (search_conversations, a default tool, had no checkbox), hid stored entries outside that list, and could not untick a tool whose knowledge base capability was missing. The save API stored any name. And an empty list could not mean "no tools": three places treated it as "use the default tools", so clearing every checkbox gave an agent without a knowledge base search_conversations, while the editor's preview said it had none. One source for the tool list: GET /api/v1/agents/tools serves the tools of AvailableToolDefinitions with their group, danger flag and knowledge base requirement, the default tools, and the tools other agent settings register whatever the list says. The editor renders from it, falling back to the catalog's label for a tool it has no translation for. Stored values stay visible: a missing capability only blocks ticking a tool, and stored names the catalog does not offer are shown ticked in their own section and can be removed. An empty list now means no built-in tools; only a list that was never set selects the defaults. tools.EffectiveAllowedTools makes that decision for tool registration, the session config and the rerank requirement. Migration 000116 (SQLite 000035) writes the default tools into the empty list of every existing smart-reasoning agent, so they keep the tools they ran with. The knowledge base filter follows the stored list, so such an agent with an "all" scope now gets the same scope as one that ticked the defaults: bases search_knowledge cannot use leave it. The editor writes the defaults into an unset list it loads, so saving does not silently turn the defaults into no tools. Saving validates the list: retired names are rewritten, switch-managed and retired skill tools are dropped, names the request adds that no tool answers to are rejected with a 400 that lists them, and unknown names already stored are kept so an existing agent can still be saved. The response reports what was kept or dropped.
2026-10-13 03:29:24 +00:00
DO $$ BEGIN RAISE NOTICE '[Migration 000116] Writing default tools into empty agent tool lists...'; END $$;
-- An empty allowed_tools list used to select the default tools at runtime.
-- It now means no built-in tools, and only an unset list selects the
-- defaults. Smart-reasoning agents that stored an empty list relied on the
-- old meaning, so write the defaults into them explicitly and they keep the
-- tools they ran with. The list is DefaultAllowedTools() as of this
-- migration. Other modes register no tools and are left as they are.
--
-- The knowledge base filter of an agent whose scope is "all" follows the
-- stored list, so a rewritten agent now gets the same scope as one that
-- ticked the default tools explicitly: knowledge bases search_knowledge
-- cannot use (wiki-only ones) leave the scope.
UPDATE custom_agents
SET config = jsonb_set(
config,
'{allowed_tools}',
'["search_knowledge", "read_document", "list_documents", "search_conversations"]'::jsonb)
WHERE config->>'agent_mode' = 'smart-reasoning'
AND config->'allowed_tools' = '[]'::jsonb;
DO $$ BEGIN RAISE NOTICE '[Migration 000116] Empty agent tool lists rewritten'; END $$;