Skip to content

add_add_stored_queries and app_remove_stored_queries tools - #43

Merged
simonw merged 2 commits into
mainfrom
claude/datasette-canned-query-approval-4lclqz
Nov 4, 2026
Merged

simonw merged 2 commits into
mainfrom
claude/datasette-canned-query-approval-4lclqz

Conversation

@simonw

@simonw simonw commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Adds agent tools for managing which stored queries an existing app may call. Write queries need the user's approval first.

Claude Opus 5.5

New tools

  • app_add_stored_queries(app_id, stored_queries)
    • Requires edit-app. Each database/query must exist and the actor needs view-query on it, the same check the web form makes (_selected_stored_queries).
    • Read-only queries are granted immediately.
    • Write queries not yet granted trigger one context.ask_user() approval for the whole call. It shows the app name, a warning that the app runs these queries with the viewer's permissions, and the SQL for each write query.
    • Nothing is saved, not even the read-only queries in the same call, until the user answers.
    • If the user declines, the tool returns {"cancelled": true, ...} with "do not retry unless they ask you to".
    • Where ask_user isn't available (QuestionsNotSupported, matched by class name so datasette-agent stays optional), write grants return an error pointing the user to the app's edit page.
    • Changes go through Registry.set_stored_queries, so each one is recorded as an app revision.
  • app_remove_stored_queries(app_id, stored_queries) removes access and never prompts.

Changes to app_create

  • stored_queries is now validated as above. Before, the agent path saved whatever strings it was given.
  • If the new app would get write queries, the user is asked to approve before the app is created. No app is created while approval is pending or if the user declines.

Tests

New tests/test_stored_query_agent_tools.py covers:

  • read-only grants with no prompt
  • pending, approved and declined write grants
  • write queries the app already has not prompting again
  • the error when questions aren't supported
  • unknown and invalid query keys, and queries the actor can't view
  • permission checks
  • removal
  • the app_create paths
  • an integration test against datasette-agent's real ToolContext: the first call raises QuestionPending, and once the question is answered the re-run saves the grant

tests/test_debug_jobs.py::test_frame_serves_job_revision_with_debug_bridge fails on main too. It expects a no-store cache header but gets private, no-store. This PR doesn't touch it.

Not in this PR

  • app_create still doesn't validate sql_databases.
  • Granting sql_databases or csp_origins doesn't ask for approval.

Generated by Claude Code

New app_add_stored_queries and app_remove_stored_queries agent tools
manage which stored queries an existing app may call. Read-only queries
are granted immediately; write queries require user approval through
context.ask_user(), showing the SQL for each one. Removing access never
prompts.

app_create now validates stored_queries the same way the web form
does (query must exist and the actor needs view-query) and asks for
approval before creating an app that is granted write queries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016BvfqAShzDp3cKzTBJtGfa
Resolves the agent_tools.py conflict by keeping both the stored query
grant helpers from this branch and the AppEditToolset/_render_app
changes from main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonw simonw changed the title Agent tools for granting stored queries to apps, with approval for write queries add_add_stored_queries and app_remove_stored_queries tools Nov 4, 2026
@simonw
simonw merged commit a8ca417 into main Nov 4, 2026
16 checks passed
simonw added a commit that referenced this pull request Nov 4, 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.

2 participants