Repository navigation
Let chat research turns batch catalog calls in codemode scripts (staging only) - #1219
NielsRogge wants to merge 7 commits into
Conversation
Behind chat_codemode_enabled (off by default), sandbox-free MCP research turns get Pi 1.0's codemode tool. The 21 PwC catalog tools are registered but deactivated, so the model sees only their typed declarations inside the codemode description and calls them from QuickJS scripts, which receive structured results. Only a script's output reaches the model. Nested calls still pass the tool policy: each counts against the call and search budgets, while the script itself counts one round. The worker tags their tool_call_start events with parent_id, the backend validates and persists it, and the chat UI groups them under one codemode card that shows each lookup live and folds the script and its output away. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… tools Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Staging results (2026-11-27)Staging runs The same 8 reference research prompts were run twice each on Luna at medium effort (synthetic staging users, concurrency 2):
Codemode as wired works, but with the v1 prompt Luna doesn't use it well. 43 of 59 scripts made exactly one catalog call, and most printed whole results ( Capacity: 8 concurrent codemode turns all completed (median 20.4 s). Memory use of all Pi worker processes (production and staging) rose by about 220 MB at peak, available host memory dipped by about 600 MB to 4.6 GB, and the 1-minute load average peaked at 2.6 on 8 vCPUs. QuickJS is not a capacity concern at this scale. The binding limits stay the OpenAI and MCP circuits. Open before this could ship:
|
Staging A/B showed Luna wrapping single catalog calls in scripts, printing whole results and using Promise.all, which added model round trips. The guidance now names the result fields, requires allSettled, bounds fan-out to the call budget, and includes a worked task-to-papers script checked against the live catalog. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Prompt v2 results (2026-11-27)v2 (
Read: with v2, codemode is modestly better: about 11% faster at the median, about 17% fewer input tokens, about 18% cheaper, and it gathers more evidence per turn. 16 turns per arm is a small sample, so these deltas are within plausible noise. A larger run, or a few days of staging traffic, should confirm them before production. Staging (codemode on) is being updated to |
Larger A/B (2026-11-27): 24 prompts × 2 per armThe prompts cover leaderboards (with size and open-weight constraints), latest papers, method explanations, reading a specific paper, conferences, organizations and frameworks. Luna at medium effort, concurrency 3, prompt v2. The on-arm ran on staging
A rough quality check gave about the same answers: 1,281 vs 1,342 characters, 4.4 vs 4.0 distinct cited papers, 0.8 vs 0.75 benchmarks, 1.0 vs 1.25 links, and 3 vs 2 answers without citations. Conclusions
The earlier 8-prompt run's −11% median latency did not replicate. It was noise. Staging is being switched back to codemode on, on the active release. The PR stays unmerged. |
The 48-turn staging A/B showed scripts fanning out into the shared 12-call budget: 20 of 28 failures were budget refusals of the last paper lookups. Script calls now spend a separate per-turn budget of 24 catalog calls and 1 MiB of returned results, below the hosted MCP server's 60 requests per minute per chat session. Each codemode script still counts as one call and round, and searches keep their own ceiling. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> # Conflicts: # backend/chat_sandbox_worker/pi_worker/src/policy.ts
A spent script budget now refuses further script catalog calls and new codemode scripts with its own message, instead of closing tooling for the whole turn, so web search and diagrams keep their own budgets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> # Conflicts: # backend/chat_sandbox_worker/pi_worker/src/policy.ts
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 1f49f5c. Configure here.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> # Conflicts: # backend/chat_sandbox_worker/pi_worker/src/policy.ts
Script budget A/B (2026-11-27)
Same 24 prompts × 2, Luna at medium effort, concurrency 3, on staging
Against the previous on-arm, with the shared 12-call budget: failed tool calls fell 64% (−96% … −20%), and there were no script-budget refusals at all (20 before). Catalog calls changed by +5.6% (−6% … +19%). Ten turns used more than 12 catalog calls, peaking at 20. The remaining failures are real: catalog misses (for example "Google DeepMind" as an organization name), one MCP 502, three script errors, and two turn-limit refusals of a further codemode call. Read: the budget fixed the refusals, and codemode now fails less often than direct calls while staying about 19% cheaper. Its catalog traffic is about 47% higher, and latency is not better: the mean is up 15%, but the CI still includes no change, so this run cannot say whether codemode is slower. Before production, it would be worth running one more pair of arms to settle latency, or capping script fan-out lower if latency matters more than evidence gathered. Staging keeps codemode on ( |
Latency settled: second A/B pair (2026-11-27)I ran a second pair with the script budget on staging
The second pair alone gives +16.7% (+7.4% … +27.1%) for total time, consistent with the first (+14.8%). The two off-arms differ from each other by 13%, so run-to-run drift is real, and the alternating order is what makes the pooled result trustworthy. Where the time goes: of the 3.2 s added per turn, about 1.4 s is extra model time (writing the script), and about 1.8 s is spent outside the model running about 50% more catalog calls, often as sequential chains through the per-turn concurrency gate of 3. Conclusion: for this chat with Luna, codemode trades about 16% more latency, and a slower first tool call, for about 19% lower cost and fewer failed calls. It does not reduce model round trips, because Luna already makes parallel direct calls. I would not ship it as the default for latency-sensitive chat. Options if the cost saving matters:
Staging is being switched back to codemode on ( |

Summary
Prototype of Pi's codemode for chat research turns, behind a new setting
chat_codemode_enabled(default off). Staging only: do not merge without explicit production authorization (chat change).When enabled, sandbox-free MCP research turns get Pi 1.0's
codemodetool (already in our pinned Pi 1.0.0; no Pi upgrade, no contract or Modal image change):codemodeplus their typed declarations inside its description. Scripts calltools.pwc_*()in a QuickJS sandbox and get structured results (outputSchema+structuredContent). Only the script's output reaches the model.search_web, Excalidraw and file tools stay direct tools. Poster and paper authoring turns never get codemode.pwc-policy. Each counts against the call and search budgets, the script itself counts one round, and only the script's output is size-bounded (nested results never enter model context).tool_call_startevents carryparent_id. The backend validates that the parent is known and persists it on display blocks (the field is omitted when null, so existing events and blocks are unchanged).The flag reaches the worker in the per-turn run frame (
codemode_enabled, sent only when true), so no deploy-tool or worker-env change is needed. Turning the setting off restores today's behaviour exactly.Validation
pi_worker/test/codemode.test.ts: a real Pi session with a fake Responses provider runs a model-written script that calls two catalog tools in parallel. It asserts that the model sees onlycodemodewith all 21 declarations, that scripts get structured results, that only the output reaches the model, policy accounting, andparent_idon nested events. Plus run-frame, prompt and policy unit tests.tests/chat,chat_sandboxandexternal_pi, the remaining failures also fail onmain(19 external-Pi runner tests andtest_loadtest_bridge_probe), plus two timing-flakytest_runtimetests.Staging rollout (pending)
pwc-deploy staging.PWC_CHAT_CODEMODE_ENABLED=trueinstaging.envand--refresh-environment.🤖 Generated with Claude Code
Note
Medium Risk
Changes tool execution, budgets, and streaming for research turns, but behavior is gated by a default-off setting and does not affect poster/paper authoring paths.
Overview
Adds
chat_codemode_enabled(default off). When set, sandbox-free Pi MCP research turns (not poster/paper) expose only thecodemodetool to the model while the 21 PwC catalog tools stay script-reachable with structured results; only script output enters model context.The worker passes
codemode_enabledon the run frame, wires Pi’s codemode extension, and updates research prompts with codemode usage guidance. Policy tracks a separate per-turn script budget (catalog call count and result bytes); nested catalog calls useparent_idin streamed events, validated and persisted on tool blocks.The chat UI groups nested catalog lookups under a single codemode card (script/output fold behind the header). Tests cover end-to-end script execution, policy accounting, protocol, run frames, and UI grouping.
Reviewed by Cursor Bugbot for commit f7cf99e. Bugbot is set up for automated code reviews on this repo. Configure here.