Share stored JSON payloads across code mode cells with Arc (#47036)

## Why

Creating a cell deep-copies every stored JSON value, including values the cell never loads. Storing a value also duplicates its payload between the cell's local state and pending writes.

## What changed

Use `Arc<JsonValue>` throughout the session store, cell runtime, and completion writes to share immutable payloads. Keep a separate map snapshot for each cell so later commits cannot change its view of stored values.

GitOrigin-RevId: 4535f0f2ed0c8f8cbfb8d24cf1cb44cbc74472b5
This commit is contained in:
Charlie Marsh
2026-09-21 13:57:22 +00:00
committed by copyberry
parent 3ead673946
commit dd6eff3954
8 changed files with 23 additions and 16 deletions
@@ -1,4 +1,5 @@
use codex_code_mode_protocol::FunctionCallOutputContentItem;
use std::sync::Arc;
use super::EXIT_SENTINEL;
use super::RuntimeEvent;
@@ -230,7 +231,10 @@ pub(super) fn store_callback(
}
};
if let Some(state) = scope.get_slot_mut::<RuntimeState>() {
state.stored_values.insert(key.clone(), serialized.clone());
let serialized = Arc::new(serialized);
state
.stored_values
.insert(key.clone(), Arc::clone(&serialized));
state.stored_value_writes.insert(key, serialized);
}
}
@@ -8,6 +8,7 @@ mod value;
use std::collections::HashMap;
use std::panic::AssertUnwindSafe;
use std::panic::catch_unwind;
use std::sync::Arc;
use std::sync::mpsc as std_mpsc;
use std::thread;
@@ -65,14 +66,14 @@ pub(crate) enum RuntimeEvent {
text: String,
},
Result {
stored_value_writes: HashMap<String, JsonValue>,
stored_value_writes: HashMap<String, Arc<JsonValue>>,
error_text: Option<String>,
},
ThreadPanicked,
}
pub(crate) fn spawn_runtime(
stored_values: HashMap<String, JsonValue>,
stored_values: HashMap<String, Arc<JsonValue>>,
request: ExecuteRequest,
event_tx: mpsc::UnboundedSender<RuntimeEvent>,
pending_mode: PendingRuntimeMode,
@@ -143,15 +144,15 @@ struct RuntimeConfig {
tool_call_id: String,
enabled_tools: Vec<EnabledToolMetadata>,
source: String,
stored_values: HashMap<String, JsonValue>,
stored_values: HashMap<String, Arc<JsonValue>>,
}
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, JsonValue>,
stored_value_writes: HashMap<String, JsonValue>,
stored_values: HashMap<String, Arc<JsonValue>>,
stored_value_writes: HashMap<String, Arc<JsonValue>>,
enabled_tools: Vec<EnabledToolMetadata>,
next_tool_call_id: u64,
next_timeout_id: u64,
@@ -163,7 +164,7 @@ pub(super) struct RuntimeState {
pub(super) enum CompletionState {
Pending,
Completed {
stored_value_writes: HashMap<String, JsonValue>,
stored_value_writes: HashMap<String, Arc<JsonValue>>,
error_text: Option<String>,
},
}
@@ -321,7 +322,7 @@ fn capture_scope_send_error(
fn send_result(
event_tx: &mpsc::UnboundedSender<RuntimeEvent>,
stored_value_writes: HashMap<String, JsonValue>,
stored_value_writes: HashMap<String, Arc<JsonValue>>,
error_text: Option<String>,
) {
let _ = event_tx.send(RuntimeEvent::Result {