Skip to content

Tools can now ask_user() for approval mid-execution, plus save_query tool - #20

Merged
simonw merged 7 commits into
mainfrom
ask-user
Jun 10, 2026
Merged

simonw merged 7 commits into
mainfrom
ask-user

Conversation

simonw and others added 7 commits June 9, 2026 13:25
Tools that declare a context parameter receive a per-invocation
ToolContext. await context.ask_user(...) asks the user a boolean,
multiple-choice or free-text question. With no stored answer the turn
suspends: the question persists to the new agent_questions table, an
SSE question event renders a form in the chat UI, and the chain stops
before the next model call. Answering (POST
/-/agent/{cid}/question/{qid}) re-executes the pending tool calls -
answered questions replay from the database - then continues the
chain. State is fully database-backed, so suspended conversations
survive server restarts; the form re-renders on page load.

Replay is keyed on (conversation_id, call_key, ask_index) where
call_key uses the provider tool_call_id (via the llm library's new
llm_tool_call parameter, now an editable dependency) with a
tool-name+arguments-hash fallback. Background agents and CLI chat
raise QuestionsNotSupported instead of suspending.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
New built-in save_query tool stores SQL the agent has written as a
Datasette stored query (read-only or write - detected by Datasette's
analyzer). Validation and persistence go through Datasette's own
/-/queries/analyze and /-/queries/store JSON endpoints, called via
datasette.client as the requesting actor, so name rules, duplicate
detection, is_write analysis and write-permission enforcement stay in
sync with the query creation web UI. Invalid SQL, permission problems
and name collisions go straight back to the model; everything else
requires explicit human approval before anything is stored.

That approval uses the new ask_user(html=) parameter: trusted HTML
(here the escaped SQL plus name/database/type/visibility details)
rendered above the question in the chat UI. The html rides along in
the agent_questions row (with a migration for existing tables), the
SSE question event and the page-load JSON.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
QuestionPending now subclasses llm.PauseChain, so suspension is a
single except clause around the chain loop: llm guarantees sibling
tool calls complete (their results arrive via after_call as usual),
no provider call is made with a placeholder result, and the paused
call gets no tool_result row - which is exactly what makes the llm
chain re-execute it on resume.

resume_agent() collapses to 'run the chain again': llm detects the
persisted history ending in unresolved tool calls and re-executes
them through the normal before_call/after_call machinery, so resumed
calls now emit the same SSE events as live ones. Deletes the manual
resume runner and find_pending_tool_calls() (~110 lines), the
suspension flag, and the after_call exception sniffing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
All four llm features ask_user() relies on (llm_tool_call parameter,
guaranteed tool_call_ids, PauseChain, chain resume from unresolved
tool calls) shipped in 0.32a3.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@simonw simonw changed the title Tools can now ask_user() for approval mid-execution Tools can now ask_user() for approval mid-execution, plus save_query tool Jun 10, 2026
@simonw

simonw commented Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

Here's what the save query user question looks like:

CleanShot 2026-06-10 at 16 44 21@2x

@simonw
simonw merged commit 95c6a80 into main Jun 10, 2026
10 checks passed
simonw added a commit that referenced this pull request Jun 10, 2026
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