Skip to content

feat: approve listed tools before they run, with per-conversation permission modes in ChatUI - #11283

Open
liquidtoy001 wants to merge 4 commits into
AstrBotDevs:masterfrom
liquidtoy001:feat/tool-approval-permission-modes
Open

liquidtoy001 wants to merge 4 commits into
AstrBotDevs:masterfrom
liquidtoy001:feat/tool-approval-permission-modes

Conversation

@liquidtoy001

@liquidtoy001 liquidtoy001 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Dependencies / 依赖

This PR is now on current master (7be85d2), which includes the merged WebChat turn history fixes (#11282). It has no other open dependency. The provider request fixes (#11303) and the failed speech and knowledge base model call cards (#11304) are independent of it.

The follow-up ChatUI branches (file change preview and undo, thinking level, polish, session panel) are in my fork (liquidtoy001/AstrBot) and build on this PR; I will open them after this one.

Size: +3761/−79, of which tests +1520/−9 (backend +795/−8, dashboard +981/−57, i18n +156/−4, generated and spec +140/−1, docs +169).

Branch: feat/tool-approval-permission-modes, on master 7be85d2. Related: #5398, #9942, #9377.

Relation to recent master changes:

Today a tool runs as soon as the model calls it, including shell commands, file writes and MCP tools called by scheduled tasks or sub-agents while nobody watches. Plugins cannot stop it: the result of on_using_llm_tool is ignored, and sub-agents do not pass through the main agent's hooks. This PR adds an opt-in approval list that works on every platform, and per-conversation permission modes in ChatUI.

Plugin-facing changes / 面向插件的改动

  • Every FunctionToolExecutor.execute call now passes the approval gate first. With the default empty list nothing is asked and behaviour is unchanged.
  • New public ContextVar CURRENT_TOOL_CALL_ID (astrbot/core/agent/tool_executor.py): the ID of the tool call being executed, set and reset by the runner.
  • ActionRow gains optional single_use, title, detail and tool_call_id fields (only WebChat renders them).
  • WebChatMessageEvent.send returns whether the message was delivered.

Modifications / 改动点

  • Approval gate (astrbot/core/astr_agent_runtime/tool_approval.py, new). FunctionToolExecutor.execute calls check_tool_approval() first, so main agents and sub-agents are both covered.

    • Config: agent.config.misc.tool_approval.tools (empty by default, so nothing changes) and timeout (default 120 s), built-in runner only. Config metadata in four languages.
    • Only listed tools are asked about. The prompt shows the tool name and arguments. Text platforms cut arguments at 1500 characters and say how much is hidden. Arguments above 100,000 characters are refused.
    • Only the requester can answer. A custom SessionFilter matches origin and sender, not the whole group ([Bug] 群聊中空提及等待器会截获其他成员的消息(session_waiter 未绑定发送者) #9377), and wakeup events never match. Concurrent requests for one approver take turns.
    • Denied, timed out or undeliverable approvals return an error result to the model ("not executed, do not retry unless the user asks"). The approval wait happens before tool_call_timeout starts.
    • Prompt texts are added to astrbot/core/i18n.py (CORE_MESSAGES, zh-CN and en-US) and follow the configured system language (Context.locale, feat: add system language for core and built-in replies #11254).
    • A background sub-agent that asks after its turn's stream has closed falls back to a proactive message (WebChatMessageEvent.send now returns whether it was delivered). The decision itself is only sent to an open stream, so it is never saved as a text message.
  • Scheduled tasks.

    • With future_task in the list, creating or editing a task needs approval. The approval is stored in the job payload (tool_approval_granted) and also covers the listed tools the task uses when it runs.
    • An edit keeps the grant only if the edit was approved.
    • A task without the grant asks its creator (payload.sender_id). A task with no creator does not run listed tools.
    • A grant never covers creating further tasks.
  • Permission modes (ChatUI) (permission_mode.py, new): workspace, ask (default), full, plus workspace_no_ask (see below).

    • Don't ask again in this conversation on a card approves the call and stops the conversation from asking, without widening access: from ask it switches to full; from workspace it switches to workspace_no_ask (still confined and sandboxed; the pill shows "Workspace · not asking", choosing Workspace again asks again). Unattended runs only ever get workspace, and future_task create/edit still asks.
    • They are the first option on the per-conversation options path below.
    • Modes only narrow access. workspace turns a role's host file scope into workspace in get_local_permission_policy(), which also requires process isolation for execution. Shell sessions opened earlier pick up the change (require_sandboxed). In the local runtime it skips approval for the file, grep, shell and Python tools only.
    • full skips approval except for future_task create/edit. Unattended runs never take full. Tasks created or edited in workspace mode store it and run narrowed.
    • The live mode is kept per WebChat conversation, so a change applies at once to queued tool calls and running sub-agents. Only requests that state a mode change it: thread button clicks, plugin view actions and other clients leave it as it is. PUT /api/v1/chat/sessions/{session_id}/permission-mode (chat scope) returns 403 unless the session is the caller's own WebChat session. The mode is cleared when the conversation or thread is deleted.
  • Per-conversation options path (new, so later options do not have to be threaded through every handler):

    • astrbot/core/platform/sources/webchat/request_flags.py: WEBCHAT_CHAT_OPTIONS and resolve_webchat_chat_options(). Missing or invalid values become None, so the configured behaviour applies.
    • astrbot/dashboard/schemas.py: a shared ChatOptions request model, used by regenerate and thread messages.
    • openspec/openapi-v1.yaml: one ChatPermissionMode schema referenced by ChatRequest, ChatMessageRegenerateRequest and ChatThreadMessageRequest. The generated client and docs/public/openapi.json are regenerated.
    • chat_service.py (send, regenerate, thread) and live_chat_service.py (WebSocket) forward the resolved options; the WebChat adapter sets each one as an event extra of the same name.
    • Dashboard: utils/chatSessionPrefs.mjs keeps the options in the conversation's record of upstream's per-user ChatUI preferences (chatPreferences().sessions[id].options, feat(chatui): add tabbed workbench and resident plugin views #11218), keyed by request field. Send, regenerate, WebSocket and thread requests spread them. A new conversation starts from the defaults, and the options are removed when the conversation is deleted (single or batch).
    • Adding an option means adding a validator to WEBCHAT_CHAT_OPTIONS, a field to ChatOptions and the OpenAPI spec, and a writeChatSessionPref(sessionId, "<field>", value) call in the dashboard.
  • ActionRow gains optional single_use, title, detail and tool_call_id. Only WebChat uses them; toDict omits unset fields. WebChat also receives expires_at/expires_in. The tool executor exposes CURRENT_TOOL_CALL_ID, so the card can point at its tool call. After a decision, an approval_result chunk reports the outcome the backend applied. BotMessageAccumulator stores it on the card, so a click after the deadline cannot show "Allowed".

  • Dashboard

    • ChatInput.vue:
      • permission pill next to +, shown only when the parent binds it;
      • an extension slot for the docked card.
    • Chat.vue:
      • owns the mode per conversation;
      • syncs it to the backend on change;
      • docks the newest open approval card on the input box.
    • ButtonActionRow.vue:
      • card layout with title and collapsible detail;
      • single-use locking and an expiry state;
      • a one-line record after the card is answered.
    • useActionRowChoices.ts: shared choices plus one clock for all card deadlines.
    • ToolCallCard.vue: a "Waiting for approval" state with a paused timer.
    • ThreadPanel.vue: a thread starts with the conversation's mode, and "Don't ask again" there affects only the thread.
    • i18n in four languages. The MDI icon subset is generated at build time since chore: stop tracking generated MDI font assets #11250, so no font files are part of this PR.
  • OpenAPI: ChatPermissionMode, ChatPermissionModeRequest, the PUT path. The generated client, docs/public/openapi.json and the scopes table are updated.

  • This is NOT a breaking change. / 这不是一个破坏性变更。An empty approval list (the default) and the default ask mode behave as before.

Screenshots or Test Results / 运行截图或测试结果

The approval card docked above the input, with the arguments folded and the three choices:

Docked approval card

The same card with its arguments shown:

Approval card details

The tool call waiting for the decision in the run details:

Tool call waiting for approval

After the choice, the one-line record folded into the run details with the tool call:

Answered approval in the run details

The permission menu next to +:

Permission mode menu

After Don't ask again in this conversation from Workspace, the conversation stays confined but stops asking; choosing Workspace again asks again:

Workspace, not asking

Permission menu in the not-asking state

Verification (Windows 11, Python 3.12, Node 22):

pytest tests
5880 passed, 97 skipped, 0 failed

node --test dashboard/tests/*.test.mjs
141 passed
  • Backend tests (one per behavior):
    • Gate (test_tool_approval.py, end to end through the agent runner with a scripted provider):
      • approve runs the tool once; any other reply and the timeout do not run it;
      • only the requester can answer, by reply code or button; the answer never reaches the model;
      • arguments too long to review are refused without asking; unlisted tools are not gated;
      • /stop ends the wait; sub-agent calls are gated;
      • a scheduled run is answered by the task's creator, a background wakeup fails closed;
      • approving a future_task grants its scheduled run, but that run must still ask to schedule another task;
      • full skips, workspace skips only confined tools, and an unattended run is never full;
      • "Don't ask again in this conversation" from ask goes full, from workspace stays confined;
      • a closed WebChat stream falls back to a proactive approval, and the decision is not saved as a text message; the prompt follows the system language.
    • Policy narrowing (test_computer_tool_permissions.py, test_local_shell_component.py).
    • Cron payloads keep only a narrowing ChatUI mode (test_cron_tools.py, test_cron_manager.py).
    • Options on the event, unknown options rejected, only a request that states the mode changes the live mode, regenerate forwarding (test_webchat_chat_options.py).
    • The PUT route: unauthenticated, an invalid mode, setting a mode, and a missing, another user's or non-WebChat session (test_fastapi_v1_dashboard.py).
    • Reloaded approval cards get the remaining time, not a stale value (test_chat_service_stream.py).
  • Dashboard tests (toolApproval, permissionWatcher): per-conversation mode storage, card locking (chosen, answered, expired, unlocked after a failed delivery), the docked card, approval waits on the matching tool call only, and the backend sync watcher. Each was checked to fail when the rule it covers is broken.
  • vue-tsc --noEmit, pnpm run i18n:check and pnpm build pass. ruff check and ruff format pass on the changed files.
  • cd docs && pnpm run docs:build builds. Pages to check:
    • new: "Tool approval and permission modes" (zh/en, sidebar under AstrBot Built-in AI);
    • Proactive capabilities → What a scheduled task can use;
    • WebUI → ChatUI;
    • Plugin guide → Sending messages → Interactive buttons.

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc. / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above. / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
  • 📚 I checked the affected WebUI instructions and screenshots in docs/zh and docs/en against the changed navigation, page structure, and labels, and updated them in this PR. / 新增 use/tool-approval.md(中英),并更新 webui.md、proactive-agent.md、send-message.md;无入口移动。
  • 🤓 I have ensured that no new dependencies are introduced. / 我确保没有引入新依赖库。
  • 😮 My changes do not introduce malicious code. / 我的更改没有引入恶意代码。

Models used: Claude Fable 5.2, Claude Opus 5.5 (Claude Code), GPT-6 Astra (Codex).

@sourcery-ai

sourcery-ai Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Sorry @liquidtoy001, your pull request is larger than the review limit of 150,000 diff characters

…mission modes

Tools listed in agent.config.misc.tool_approval.tools wait for the
requester's approval before FunctionToolExecutor runs them, so sub-agents
are covered too. Only the requester (or a scheduled task's creator) can
answer; wakeups never do. Approving a future_task create/edit also grants
its listed tools when it runs on schedule.

ChatUI requests gain one path for per-conversation options:
WEBCHAT_CHAT_OPTIONS and resolve_webchat_chat_options() validate them, the
ChatOptions request model carries them for send, regenerate, thread and
WebSocket requests, and the WebChat adapter puts them on the event. The
first option is the permission mode (workspace, workspace_no_ask, ask,
full): modes only narrow the role's local policy, follow changes live
through PUT /api/v1/chat/sessions/{id}/permission-mode, and never pass
full access or a waiver to scheduled tasks.

ActionRow gains optional single_use, title, detail and tool_call_id fields
for WebChat, and an approval_result chunk records the applied outcome.
Per-conversation options are stored in one browser entry per conversation
(chatSessionPrefs), spread into every chat request and cleared when the
conversation is deleted or the user logs out. The newest open approval
card is docked above the chat input; answered cards fold into the run
details. Tool cards show a waiting state while their approval is pending.
A pill next to + selects the conversation's permission mode; "Don't ask
again" from workspace shows as "Workspace · not asking".
@liquidtoy001
liquidtoy001 force-pushed the feat/tool-approval-permission-modes branch from 5851c04 to 12a81a1 Compare November 26, 2026 07:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant