Reduce tool metadata cloning in the code-mode runtime (#51761)

## What changed

Consume enabled tool definitions and borrow their metadata when installing `tools` and `ALL_TOOLS`, avoiding copies of the tool catalog. Retain only tool names and kinds in runtime callback state, preserving each cell's original callback indices for its lifetime.

GitOrigin-RevId: 62eaf62abce6de1a0cd5cdb3332fbed754a23993
This commit is contained in:
Alex Kira
2026-10-07 16:46:43 +00:00
committed by copyberry
parent 4d53e6ba0e
commit 3e544e8437
3 changed files with 39 additions and 22 deletions
@@ -1,4 +1,3 @@
use super::RuntimeState;
use super::callbacks::audio_callback;
use super::callbacks::clear_timeout_callback;
use super::callbacks::exit_callback;
@@ -11,16 +10,20 @@ use super::callbacks::store_callback;
use super::callbacks::text_callback;
use super::callbacks::tool_callback;
use super::callbacks::yield_control_callback;
use codex_code_mode_protocol::EnabledToolMetadata;
pub(super) fn install_globals(scope: &mut v8::PinScope<'_, '_>) -> Result<(), String> {
pub(super) fn install_globals(
scope: &mut v8::PinScope<'_, '_>,
enabled_tools: &[EnabledToolMetadata],
) -> Result<(), String> {
let global = scope.get_current_context().global(scope);
delete_global(scope, global, "console")?;
delete_global(scope, global, "Atomics")?;
delete_global(scope, global, "SharedArrayBuffer")?;
delete_global(scope, global, "WebAssembly")?;
let tools = build_tools_object(scope)?;
let all_tools = build_all_tools_value(scope)?;
let tools = build_tools_object(scope, enabled_tools)?;
let all_tools = build_all_tools_value(scope, enabled_tools)?;
let clear_timeout = helper_function(scope, "clearTimeout", clear_timeout_callback)?;
let set_timeout = helper_function(scope, "setTimeout", set_timeout_callback)?;
let text = helper_function(scope, "text", text_callback)?;
@@ -57,12 +60,9 @@ pub(super) fn install_globals(scope: &mut v8::PinScope<'_, '_>) -> Result<(), St
fn build_tools_object<'s>(
scope: &mut v8::PinScope<'s, '_>,
enabled_tools: &[EnabledToolMetadata],
) -> Result<v8::Local<'s, v8::Object>, String> {
let tools = v8::Object::new(scope);
let enabled_tools = scope
.get_slot::<RuntimeState>()
.map(|state| state.enabled_tools.clone())
.unwrap_or_default();
for (tool_index, tool) in enabled_tools.iter().enumerate() {
let name = v8::String::new(scope, &tool.global_name)
@@ -75,11 +75,8 @@ fn build_tools_object<'s>(
fn build_all_tools_value<'s>(
scope: &mut v8::PinScope<'s, '_>,
enabled_tools: &[EnabledToolMetadata],
) -> Result<v8::Local<'s, v8::Value>, String> {
let enabled_tools = scope
.get_slot::<RuntimeState>()
.map(|state| state.enabled_tools.clone())
.unwrap_or_default();
let array = v8::Array::new(scope, enabled_tools.len() as i32);
let name_key = v8::String::new(scope, "name")
.ok_or_else(|| "failed to allocate ALL_TOOLS name key".to_string())?;
+28 -10
View File
@@ -16,7 +16,7 @@ use codex_code_mode_protocol::CodeModeToolKind;
use codex_code_mode_protocol::EnabledToolMetadata;
use codex_code_mode_protocol::ExecuteRequest;
use codex_code_mode_protocol::FunctionCallOutputContentItem;
use codex_code_mode_protocol::enabled_tool_metadata;
use codex_code_mode_protocol::normalize_code_mode_identifier;
use codex_protocol::ToolName;
use serde_json::Value as JsonValue;
use tokio::sync::mpsc;
@@ -94,8 +94,13 @@ pub(crate) fn spawn_runtime(
let (isolate_handle_tx, isolate_handle_rx) = std_mpsc::sync_channel(1);
let enabled_tools = request
.enabled_tools
.iter()
.map(enabled_tool_metadata)
.into_iter()
.map(|definition| EnabledToolMetadata {
global_name: normalize_code_mode_identifier(&definition.name),
tool_name: definition.tool_name,
description: definition.description,
kind: definition.kind,
})
.collect::<Vec<_>>();
let config = RuntimeConfig {
tool_call_id: request.tool_call_id,
@@ -147,13 +152,19 @@ struct RuntimeConfig {
stored_values: HashMap<String, Arc<JsonValue>>,
}
// Callback indices refer to this cell's original catalog for its entire lifetime.
struct ToolCallbackMetadata {
tool_name: ToolName,
kind: CodeModeToolKind,
}
pub(super) struct RuntimeState {
event_tx: mpsc::UnboundedSender<RuntimeEvent>,
pending_tool_calls: HashMap<String, v8::Global<v8::PromiseResolver>>,
pending_timeouts: HashMap<u64, timers::ScheduledTimeout>,
stored_values: HashMap<String, Arc<JsonValue>>,
stored_value_writes: HashMap<String, Arc<JsonValue>>,
enabled_tools: Vec<EnabledToolMetadata>,
enabled_tools: Vec<ToolCallbackMetadata>,
next_tool_call_id: u64,
next_timeout_id: u64,
tool_call_id: String,
@@ -189,13 +200,25 @@ fn run_runtime(
let context = v8::Context::new(scope, Default::default());
let scope = &mut v8::ContextScope::new(scope, context);
if let Err(error_text) = globals::install_globals(scope, &config.enabled_tools) {
send_result(&event_tx, HashMap::new(), Some(error_text));
return;
}
scope.set_slot(RuntimeState {
event_tx: event_tx.clone(),
pending_tool_calls: HashMap::new(),
pending_timeouts: HashMap::new(),
stored_values: config.stored_values,
stored_value_writes: HashMap::new(),
enabled_tools: config.enabled_tools,
enabled_tools: config
.enabled_tools
.into_iter()
.map(|tool| ToolCallbackMetadata {
tool_name: tool.tool_name,
kind: tool.kind,
})
.collect(),
next_tool_call_id: 1,
next_timeout_id: 1,
tool_call_id: config.tool_call_id,
@@ -203,11 +226,6 @@ fn run_runtime(
exit_requested: false,
});
if let Err(error_text) = globals::install_globals(scope) {
send_result(&event_tx, HashMap::new(), Some(error_text));
return;
}
let _ = event_tx.send(RuntimeEvent::Started);
let pending_promise = match module_loader::evaluate_main_module(scope, &config.source) {