Skip to content

feat(studio): wire update_notebook tool - #48949

Merged
charislam merged 3 commits into
masterfrom
feature/notebooks-update-tool
Aug 12, 2026
Merged

charislam merged 3 commits into
masterfrom
feature/notebooks-update-tool

Conversation

@charislam

@charislam charislam commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Adds update_notebook, an AI tool (needsApproval: true) that re-fetches a notebook, applies an ordered list of cell operations (insert/replace/delete/move, from notebook-operations.ts) via applyNotebookOperations, and PUTs the resolved content. Concurrent edits are last-write-wins, since re-fetching at execute time is the only mitigation per the RFC.
  • Registers update_notebook in toolSetValidationSchema / TOOL_CATEGORY_MAP, and documents it in the notebooks system prompt.
  • The untrusted→safe SQL promotion (acceptUntrustedSql/acceptUntrustedLogsSql) is inlined directly inside each needsApproval: true tool's own execute (both create_notebook and update_notebook), rather than behind a shared helper — kept auditable right next to the approval gate that authorizes it, and not reusable by an unrelated caller unaware it needs re-promotion.

Resolves FE-4083

Test plan

  • notebook-tools.test.ts: re-fetch → apply operations → PUT with promoted SQL; descriptive error (no PUT) when an operation targets an unknown cell id; needsApproval asserted for both create and update
  • pnpm vitest run on notebook-tools.test.ts, tools/index.test.ts, tool-filter.test.ts — all pass
  • tsc --noEmit, eslint, prettier --check — clean on all touched files

Summary by CodeRabbit

  • New Features

    • AI-assisted notebook editing now supports updating existing notebooks.
    • Notebook changes are refreshed before being applied to help preserve the latest content.
    • Updates report operation errors and return a summary of the modified notebook.
    • Notebook edits require user approval before being saved.
  • Bug Fixes

    • Invalid or unknown cell references are reported without applying a partial update.

Re-fetches the notebook, applies the requested cell operations from
notebook-operations.ts, and PUTs the resolved content — last-write-wins,
since re-fetching at execute time is the only mitigation for concurrent
edits per the RFC.

refactor(studio): inline SQL promotion next to each needsApproval gate

Un-shares the untrusted-to-safe SQL promotion between create_notebook and
update_notebook so it's visible directly beside each tool's own
needsApproval: true, rather than in a helper a reader has to jump to.
toWireCell downcasts a cell's branded unchecked_sql to a plain sql: string,
matching CellWire's wire schema. Document that this is intentional and that
the only place user approval is granted for that SQL is the inline
acceptUntrustedSql/acceptUntrustedLogsSql promotion beside each tool's own
needsApproval: true, so a reader isn't left wondering where the brand went.
…call site

A shared helper that strips the unchecked_sql brand down to a plain string
is reusable by any future caller that has no reason to know the result must
be re-promoted before use. Inline the conversion separately in get_notebook
(display only) and in update_notebook, where it now sits directly inside
the same execute as that tool's own needsApproval: true and its later
acceptUntrustedSql/acceptUntrustedLogsSql re-promotion.
@charislam
charislam requested a review from a team as a code owner August 11, 2026 17:06
@vercel

vercel Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
studio-self-hosted Ready Ready Preview Aug 11, 2026 5:11pm
studio-staging Ready Ready Preview Aug 11, 2026 5:11pm
5 Skipped Deployments
Project Deployment Actions Updated (UTC)
studio Ignored Ignored Aug 11, 2026 5:11pm
design-system Skipped Skipped Aug 11, 2026 5:11pm
docs Skipped Skipped v0 Aug 11, 2026 5:11pm
ui-library Skipped Skipped Aug 11, 2026 5:11pm
zone-www-dot-com Skipped Skipped v0 Aug 11, 2026 5:11pm

Request Review

@supabase

supabase Bot commented Aug 11, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project xguihxuzqibwxjnimxev because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds an approved update_notebook AI tool. The tool refetches the latest notebook, applies validated cell operations, preserves SQL trust, persists the result, and returns its identifier and name.

Changes

Notebook update workflow

Layer / File(s) Summary
Tool registration and prompt guidance
apps/studio/lib/ai/prompts.ts, apps/studio/lib/ai/tool-filter.ts
The tool filter accepts update_notebook as a schema-level tool. The notebook prompt describes direct creation and update tool usage and latest-content refetching.
Notebook operation and persistence flow
apps/studio/lib/ai/tools/notebook-tools.ts
The approved tool refetches notebooks, converts cell data, applies validated operations, reports operation errors, re-promotes SQL as trusted, persists updates, and returns { id, name }.
Update tool integration coverage
apps/studio/lib/ai/tools/notebook-tools.test.ts
Tests cover tool registration, approval requirements, delete and insert operations, resolved content, PUT payloads, returned summaries, and unknown cell IDs that prevent updates.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AI as AI assistant
  participant update_notebook
  participant NotebookAPI as Notebook API
  AI->>update_notebook: Submit notebook operations
  update_notebook->>NotebookAPI: Refetch latest notebook
  NotebookAPI-->>update_notebook: Return notebook content
  update_notebook->>update_notebook: Apply validated operations
  update_notebook->>NotebookAPI: Persist updated notebook
  NotebookAPI-->>update_notebook: Return updated notebook
  update_notebook-->>AI: Return notebook id and name
Loading

Possibly related PRs

Suggested reviewers: awaseem

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the main change: adding and wiring the update_notebook tool.
Description check ✅ Passed The description explains the new behavior, affected areas, issue, and test plan, but it omits several template headings and the CONTRIBUTING confirmation.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/notebooks-update-tool

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

🎭 Playwright Test Results (next)

passed  238 passed
flaky  5 flaky
skipped  5 skipped

Details

stats  248 tests across 29 suites
duration  17 minutes, 33 seconds
commit  a282652

Flaky tests

Features › realtime-inspector.spec.ts › Realtime Inspector › Basic Inspector UI › channel selection popover opens and works
Features › realtime-inspector.spec.ts › Realtime Inspector › Basic Inspector UI › can join and leave a channel
Features › realtime-inspector.spec.ts › Realtime Inspector › Broadcast Messages › broadcast modal validates JSON payload
Features › realtime-inspector.spec.ts › Realtime Inspector › Message Display › messages counter shows correct count
Features › table-editor.spec.ts › table editor › should show rls accordingly

Skipped tests

Features › auth-users.spec.ts › should show web3 users as enabled when the matching web3 provider is enabled
Features › sql-editor.spec.ts › SQL Editor › snippet favourite works as expected
Features › sql-editor.spec.ts › SQL Editor › share with team works as expected
Features › sql-editor.spec.ts › SQL Editor › folders works as expected
Features › sql-editor.spec.ts › SQL Editor › other SQL snippets actions work as expected

@charislam charislam mentioned this pull request Aug 11, 2026
3 tasks done

@joshenlim joshenlim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes generally LGTM! 🙂🙏

@charislam
charislam merged commit 4da8bfe into master Aug 12, 2026
39 of 40 checks passed
@charislam
charislam deleted the feature/notebooks-update-tool branch August 12, 2026 13:31
@github-actions

github-actions Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Braintrust eval report

Assistant (master-1786541720)

Score Average Improvements Regressions
Completeness 98.9% (+0pp) 1 🟢 1 🔴
Conciseness 43.7% (-1pp) 8 🟢 9 🔴
Docs Faithfulness 68.3% (+2pp) 4 🟢 3 🔴
Goal Completion 80.5% (+1pp) 6 🟢 6 🔴
Tool Usage 72.5% (-3pp) 1 🟢 2 🔴
Correctness 71.1% (+2pp) 3 🟢 2 🔴
Knowledge Usage 94.9% (-3pp) - 1 🔴
SQL Identifier Quoting 100% (+0pp) - -
SQL Validity 100% (+5pp) 1 🟢 -
URL Validity 100% (+0pp) - -
Safety 100% (+10pp) 2 🟢 -
Time_to_first_token 3.31tok (+0.32tok) 11 🟢 18 🔴
Llm_calls 7.15 (-0.05) 11 🟢 13 🔴
Tool_calls 2.84 (-0.03) 9 🟢 9 🔴
Errors 0.05 (+0.05) - 2 🔴
Llm_errors 0.02 (+0.02) - 2 🔴
Tool_errors 0 (+0) - -
Prompt_tokens 26496.89tok (+4601.62tok) 10 🟢 15 🔴
Prompt_cached_tokens 7703.54tok (+5.89tok) 6 🟢 14 🔴
Prompt_cache_creation_tokens 0tok (+0tok) - -
Prompt_cache_creation_5m_tokens 0tok (+0tok) - -
Prompt_cache_creation_1h_tokens 0tok (+0tok) - -
Completion_tokens 602.71tok (+8.66tok) 12 🟢 17 🔴
Completion_reasoning_tokens 98.66tok (-0.41tok) 15 🟢 11 🔴
Completion_accepted_prediction_tokens 0tok (+0tok) - -
Completion_rejected_prediction_tokens 0tok (+0tok) - -
Completion_audio_tokens 0tok (+0tok) - -
Total_tokens 27099.6tok (+4610.28tok) 10 🟢 19 🔴
Estimated_cost 0$ (+0$) 8 🟢 14 🔴
Duration 17.51s (+0.24s) 15 🟢 14 🔴
Llm_duration 12.35s (+0.55s) 12 🟢 17 🔴

charislam added a commit that referenced this pull request Aug 12, 2026
## Summary
- Adds deterministic `list_notebooks`/`get_notebook` fixtures (two
seeded notebooks, each with markdown/database/log cells) and stateful
in-memory `create_notebook`/`update_notebook` mocks to
`apps/studio/lib/ai/tools/mock-tools.ts`, so Braintrust evals can
exercise notebook tool calls without a real project.
- Both write-tool mocks force `needsApproval: false`, matching the
existing `execute_sql`/`deploy_edge_function` mock pattern — the eval
harness filters out tool-parts in `'approval-requested'` state and can
never answer an approval gate.
- All four notebook tools are wrapped from the real `getNotebookTools()`
definitions (only `execute`/`needsApproval` overridden), so evals
validate the model's arguments against the exact production schemas.
- Dedupes `describeOperationError` (previously duplicated between this
new mock and `notebook-tools.ts`) into a single exported
`describeNotebookOperationError` in `notebook-operations.ts`.

**Stacked on #48949** (`feature/notebooks-update-tool`) — this PR's base
branch is that PR, not `master`, because it reuses `update_notebook` and
the shared error helper that only exist there. Merge #48949 first, then
retarget/merge this one.

## Test plan
- [x] \`pnpm --filter studio typecheck\` passes
- [x] \`pnpm --filter studio test\` — all notebook-related suites pass
(\`mock-tools.test.ts\`, \`notebook-tools.test.ts\`,
\`data/content/notebooks/*\`)
- [x] \`eslint\` / \`prettier --check\` clean on all touched files
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