WeKnora/internal/database/migration_sqlite_agent_allo...

101 lines
4.1 KiB
Go
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
package database
import (
"database/sql"
"encoding/json"
"path/filepath"
"testing"
"github.com/Tencent/WeKnora/internal/agent/tools"
"github.com/stretchr/testify/require"
)
// agentToolListRows is the custom_agents data both upgrade tests start from:
// every way an allowed_tools value can be stored, in both agent modes.
var agentToolListRows = map[string]string{
"reasoning-empty": `{"agent_mode":"smart-reasoning","allowed_tools":[]}`,
"reasoning-null": `{"agent_mode":"smart-reasoning","allowed_tools":null}`,
"reasoning-missing": `{"agent_mode":"smart-reasoning"}`,
"reasoning-picked": `{"agent_mode":"smart-reasoning","allowed_tools":["thinking"]}`,
"quick-empty": `{"agent_mode":"quick-answer","allowed_tools":[]}`,
// Malformed values must neither break the migration nor be rewritten.
"reasoning-string": `{"agent_mode":"smart-reasoning","allowed_tools":"thinking"}`,
"reasoning-object": `{"agent_mode":"smart-reasoning","allowed_tools":{}}`,
}
// requireAgentToolListsUpgraded checks that an upgrade keeps every agent's
// effective tools: the empty smart-reasoning list, which used to select the
// default tools, now names them, and nothing else changes.
func requireAgentToolListsUpgraded(t *testing.T, db *sql.DB, query string) {
t.Helper()
for _, id := range []string{"reasoning-string", "reasoning-object"} {
var raw string
require.NoError(t, db.QueryRow(query, id).Scan(&raw), id)
var got, want struct {
AllowedTools json.RawMessage `json:"allowed_tools"`
}
require.NoError(t, json.Unmarshal([]byte(raw), &got), id)
require.NoError(t, json.Unmarshal([]byte(agentToolListRows[id]), &want), id)
require.JSONEq(t, string(want.AllowedTools), string(got.AllowedTools), "%s must be left as it is", id)
}
feat(agent): persist human-in-the-loop prompts with a shared broker (#4250) * feat(agent): persist human-in-the-loop prompts with a shared broker Tool approvals and MCP OAuth prompts are now rows in agent_prompts, awaited through hitl.Broker (lease, DB polling, Redis wake-up, per-kind answer validation). Resolve is bound to session and kind. This is P1 of the suspend/resume design; runs still block as before. * feat(agent): suspend runs whose prompt outlives a short in-memory wait Behind agent.prompt_suspend_enabled (off by default). A run raised from the HTTP stream saves its state when a tool approval is raised; after the hot wait the prompt moves to suspended and the assistant message is saved as awaiting_input instead of blocking. A reaper takes over prompts whose waiting replica stopped renewing its lease. Resume comes in a later change. * feat(agent): resume suspended runs once their prompt is answered The replica that records the answer resumes the run in process under the prompt's lease. Superseded, expired and abandoned runs are closed with a result for every tool call, and approved tools are never replayed. * feat(agent): let users answer suspended tool approvals and follow the resumed run * feat(agent): ask_user tool and generic prompt answer endpoint - ask_user built-in tool: options, short forms or free text; validated answers, decline, suspend/resume like tool approval; non-interactive callers (IM, API keys) are told to ask in the reply instead - POST /agent/prompts/{prompt_id} (and embed variant) answers ask_user and tool_approval prompts, returns resumed; SDK ResolveAgentPrompt - agent_prompt_required/resolved events; AskUserCard sharing the countdown with ToolApprovalCard; answered questions rebuilt as cards from history - awaiting-input messages keep their steps expanded so the pending card stays visible; prompt cards keyed by their own id - i18n for all locales; design doc updated * fix(agent): keep prompt answers that race a suspending or vanished owner - Resolve suspends a pending prompt whose owner's lease lapsed, as the reaper would, so the answer resumes the run instead of waking nobody. - Resuming waits for the suspending run to persist its message and clear its live-run marker; a marker left by a vanished owner is cleared. - A resumed run that suspends again on a new prompt no longer has its message closed from the old state. - tool_approval answers are decoded strictly and require "approved"; fix the SDK doc that showed {"decision": "approve"}. - API-key runs block instead of suspending, since they cannot answer. - A run state that cannot be saved falls back to an in-memory wait, and NUL escapes are stripped from snapshots for jsonb. * style(chat): align the ask_user card with the other interaction cards Question and answer use the timeline's step text styles instead of primary-colored and monospace text; options and form controls use the compact step size, the number input spans the row, and the free-text character counter is hidden. * feat(chat): dock pending agent prompts above the composer Tool approvals, ask_user questions and MCP OAuth prompts were answered inline in the agent timeline, where a long answer or scrolling back pushes them out of view. Open prompts of a streaming or suspended run now gather in a dock above the composer (main chat and embed), one at a time with a pager, lettered options and Enter to submit. The timeline keeps a one-line record with a link back to the dock and the outcome once answered. Views without a dock still answer inline. * fix(agent): fence prompt ownership and preserve resumed turn context * fix(agent): keep runs through consume errors and file prompts under the caller Consume reported a store error as a lost claim, so an answer consumed during a database hiccup detached its run: the message was never persisted, the live-run marker stayed, and the reaper skipped the row because the waiter had already dropped the snapshot. Consume now retries, then decides from the row, and gives a run up only when the prompt is known to have moved on. Tool approvals and OAuth prompts of a shared agent were filed under the agent's workspace, so the user's answer failed the tenant check and the prompt timed out. Like ask_user, they now use the caller's workspace; policy and token lookups stay in the agent's. Enter on a picked option submits it instead of toggling it off, and resume errors no longer report a nil cause. * fix(agent): validate prompt choices and require durable approval claims Reject malformed serialized option lists so the model can retry with valid arguments. Distinguish prompt consumption failures from lost ownership without authorizing side effects, and preserve native Enter selection for unselected choices. * fix(agent): resolve prompt workflow lint errors
2026-10-14 12:37:28 +00:00
// Migration 35 freezes the defaults that existed when it was written.
// Later defaults must not silently grant tools to explicit lists.
legacyDefaults := []string{"search_knowledge", "read_document", "list_documents", "search_conversations"}
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
want := map[string][]string{
feat(agent): persist human-in-the-loop prompts with a shared broker (#4250) * feat(agent): persist human-in-the-loop prompts with a shared broker Tool approvals and MCP OAuth prompts are now rows in agent_prompts, awaited through hitl.Broker (lease, DB polling, Redis wake-up, per-kind answer validation). Resolve is bound to session and kind. This is P1 of the suspend/resume design; runs still block as before. * feat(agent): suspend runs whose prompt outlives a short in-memory wait Behind agent.prompt_suspend_enabled (off by default). A run raised from the HTTP stream saves its state when a tool approval is raised; after the hot wait the prompt moves to suspended and the assistant message is saved as awaiting_input instead of blocking. A reaper takes over prompts whose waiting replica stopped renewing its lease. Resume comes in a later change. * feat(agent): resume suspended runs once their prompt is answered The replica that records the answer resumes the run in process under the prompt's lease. Superseded, expired and abandoned runs are closed with a result for every tool call, and approved tools are never replayed. * feat(agent): let users answer suspended tool approvals and follow the resumed run * feat(agent): ask_user tool and generic prompt answer endpoint - ask_user built-in tool: options, short forms or free text; validated answers, decline, suspend/resume like tool approval; non-interactive callers (IM, API keys) are told to ask in the reply instead - POST /agent/prompts/{prompt_id} (and embed variant) answers ask_user and tool_approval prompts, returns resumed; SDK ResolveAgentPrompt - agent_prompt_required/resolved events; AskUserCard sharing the countdown with ToolApprovalCard; answered questions rebuilt as cards from history - awaiting-input messages keep their steps expanded so the pending card stays visible; prompt cards keyed by their own id - i18n for all locales; design doc updated * fix(agent): keep prompt answers that race a suspending or vanished owner - Resolve suspends a pending prompt whose owner's lease lapsed, as the reaper would, so the answer resumes the run instead of waking nobody. - Resuming waits for the suspending run to persist its message and clear its live-run marker; a marker left by a vanished owner is cleared. - A resumed run that suspends again on a new prompt no longer has its message closed from the old state. - tool_approval answers are decoded strictly and require "approved"; fix the SDK doc that showed {"decision": "approve"}. - API-key runs block instead of suspending, since they cannot answer. - A run state that cannot be saved falls back to an in-memory wait, and NUL escapes are stripped from snapshots for jsonb. * style(chat): align the ask_user card with the other interaction cards Question and answer use the timeline's step text styles instead of primary-colored and monospace text; options and form controls use the compact step size, the number input spans the row, and the free-text character counter is hidden. * feat(chat): dock pending agent prompts above the composer Tool approvals, ask_user questions and MCP OAuth prompts were answered inline in the agent timeline, where a long answer or scrolling back pushes them out of view. Open prompts of a streaming or suspended run now gather in a dock above the composer (main chat and embed), one at a time with a pager, lettered options and Enter to submit. The timeline keeps a one-line record with a link back to the dock and the outcome once answered. Views without a dock still answer inline. * fix(agent): fence prompt ownership and preserve resumed turn context * fix(agent): keep runs through consume errors and file prompts under the caller Consume reported a store error as a lost claim, so an answer consumed during a database hiccup detached its run: the message was never persisted, the live-run marker stayed, and the reaper skipped the row because the waiter had already dropped the snapshot. Consume now retries, then decides from the row, and gives a run up only when the prompt is known to have moved on. Tool approvals and OAuth prompts of a shared agent were filed under the agent's workspace, so the user's answer failed the tenant check and the prompt timed out. Like ask_user, they now use the caller's workspace; policy and token lookups stay in the agent's. Enter on a picked option submits it instead of toggling it off, and resume errors no longer report a nil cause. * fix(agent): validate prompt choices and require durable approval claims Reject malformed serialized option lists so the model can retry with valid arguments. Distinguish prompt consumption failures from lost ownership without authorizing side effects, and preserve native Enter selection for unselected choices. * fix(agent): resolve prompt workflow lint errors
2026-10-14 12:37:28 +00:00
"reasoning-empty": legacyDefaults,
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
"reasoning-null": nil,
"reasoning-missing": nil,
"reasoning-picked": {"thinking"},
"quick-empty": {},
}
for id, expected := range want {
var raw string
require.NoError(t, db.QueryRow(query, id).Scan(&raw), id)
var config struct {
AllowedTools []string `json:"allowed_tools"`
}
require.NoError(t, json.Unmarshal([]byte(raw), &config), id)
require.Equal(t, expected, config.AllowedTools, id)
// The default tools still apply to the agents that never set a list.
before := tools.EffectiveAllowedTools(decodeAllowedTools(t, agentToolListRows[id]))
feat(agent): persist human-in-the-loop prompts with a shared broker (#4250) * feat(agent): persist human-in-the-loop prompts with a shared broker Tool approvals and MCP OAuth prompts are now rows in agent_prompts, awaited through hitl.Broker (lease, DB polling, Redis wake-up, per-kind answer validation). Resolve is bound to session and kind. This is P1 of the suspend/resume design; runs still block as before. * feat(agent): suspend runs whose prompt outlives a short in-memory wait Behind agent.prompt_suspend_enabled (off by default). A run raised from the HTTP stream saves its state when a tool approval is raised; after the hot wait the prompt moves to suspended and the assistant message is saved as awaiting_input instead of blocking. A reaper takes over prompts whose waiting replica stopped renewing its lease. Resume comes in a later change. * feat(agent): resume suspended runs once their prompt is answered The replica that records the answer resumes the run in process under the prompt's lease. Superseded, expired and abandoned runs are closed with a result for every tool call, and approved tools are never replayed. * feat(agent): let users answer suspended tool approvals and follow the resumed run * feat(agent): ask_user tool and generic prompt answer endpoint - ask_user built-in tool: options, short forms or free text; validated answers, decline, suspend/resume like tool approval; non-interactive callers (IM, API keys) are told to ask in the reply instead - POST /agent/prompts/{prompt_id} (and embed variant) answers ask_user and tool_approval prompts, returns resumed; SDK ResolveAgentPrompt - agent_prompt_required/resolved events; AskUserCard sharing the countdown with ToolApprovalCard; answered questions rebuilt as cards from history - awaiting-input messages keep their steps expanded so the pending card stays visible; prompt cards keyed by their own id - i18n for all locales; design doc updated * fix(agent): keep prompt answers that race a suspending or vanished owner - Resolve suspends a pending prompt whose owner's lease lapsed, as the reaper would, so the answer resumes the run instead of waking nobody. - Resuming waits for the suspending run to persist its message and clear its live-run marker; a marker left by a vanished owner is cleared. - A resumed run that suspends again on a new prompt no longer has its message closed from the old state. - tool_approval answers are decoded strictly and require "approved"; fix the SDK doc that showed {"decision": "approve"}. - API-key runs block instead of suspending, since they cannot answer. - A run state that cannot be saved falls back to an in-memory wait, and NUL escapes are stripped from snapshots for jsonb. * style(chat): align the ask_user card with the other interaction cards Question and answer use the timeline's step text styles instead of primary-colored and monospace text; options and form controls use the compact step size, the number input spans the row, and the free-text character counter is hidden. * feat(chat): dock pending agent prompts above the composer Tool approvals, ask_user questions and MCP OAuth prompts were answered inline in the agent timeline, where a long answer or scrolling back pushes them out of view. Open prompts of a streaming or suspended run now gather in a dock above the composer (main chat and embed), one at a time with a pager, lettered options and Enter to submit. The timeline keeps a one-line record with a link back to the dock and the outcome once answered. Views without a dock still answer inline. * fix(agent): fence prompt ownership and preserve resumed turn context * fix(agent): keep runs through consume errors and file prompts under the caller Consume reported a store error as a lost claim, so an answer consumed during a database hiccup detached its run: the message was never persisted, the live-run marker stayed, and the reaper skipped the row because the waiter had already dropped the snapshot. Consume now retries, then decides from the row, and gives a run up only when the prompt is known to have moved on. Tool approvals and OAuth prompts of a shared agent were filed under the agent's workspace, so the user's answer failed the tenant check and the prompt timed out. Like ask_user, they now use the caller's workspace; policy and token lookups stay in the agent's. Enter on a picked option submits it instead of toggling it off, and resume errors no longer report a nil cause. * fix(agent): validate prompt choices and require durable approval claims Reject malformed serialized option lists so the model can retry with valid arguments. Distinguish prompt consumption failures from lost ownership without authorizing side effects, and preserve native Enter selection for unselected choices. * fix(agent): resolve prompt workflow lint errors
2026-10-14 12:37:28 +00:00
if id == "reasoning-empty" {
before = legacyDefaults
}
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
require.ElementsMatch(t, before, tools.EffectiveAllowedTools(config.AllowedTools),
"%s must keep its effective tools", id)
}
}
// decodeAllowedTools reads a pre-upgrade row under the old meaning, in which
// an empty list also selected the default tools.
func decodeAllowedTools(t *testing.T, raw string) []string {
t.Helper()
var config struct {
AgentMode string `json:"agent_mode"`
AllowedTools []string `json:"allowed_tools"`
}
require.NoError(t, json.Unmarshal([]byte(raw), &config))
if config.AgentMode == "smart-reasoning" && config.AllowedTools != nil && len(config.AllowedTools) == 0 {
return nil
}
return config.AllowedTools
}
func TestSQLiteMigrationsKeepAgentToolsWhenAnEmptyListMeansNone(t *testing.T) {
repoRoot := sqliteRepoRoot(t)
chdirAndRestore(t, copySQLiteMigrationsThrough(t, repoRoot, 34))
dbPath := filepath.Join(t.TempDir(), "upgrade-agent-tools.db")
opts := MigrationOptions{SQLiteDBPath: dbPath}
require.NoError(t, RunMigrationsWithOptions("sqlite3://unused", opts))
db := openSQLiteDB(t, dbPath)
for id, config := range agentToolListRows {
_, err := db.Exec(`INSERT INTO custom_agents (id, name, tenant_id, config) VALUES (?, ?, 1, ?)`,
id, id, config)
require.NoError(t, err)
}
chdirAndRestore(t, repoRoot)
require.NoError(t, RunMigrationsWithOptions("sqlite3://unused", opts))
requireAgentToolListsUpgraded(t, db, `SELECT config FROM custom_agents WHERE id = ?`)
}