Share byte buffers across exec-server output chunks (#49972)

## Why

Retaining process output, publishing events, and serving reads cloned the underlying byte buffers. Sharing those buffers avoids copying output bytes when chunks are cloned.

## What changed

Store `ByteChunk` bytes in `Arc<Vec<u8>>` and retain `ProcessOutputChunk` values directly. Add borrowed byte access for the MCP process transport while preserving base64 serialization and owned byte extraction through `into_inner()`.

## Testing

Add a unit test covering base64 serialization, deserialization, and independent owned bytes extracted from shared chunks.

GitOrigin-RevId: 6bba114af48424aa844487aa75163cfc604e0575
This commit is contained in:
jif
2026-10-01 12:41:39 +00:00
committed by copyberry
parent d6c3b448a4
commit a933dd77db
7 changed files with 60 additions and 44 deletions
+15 -31
View File
@@ -47,6 +47,7 @@ use crate::network_policy_decisions::network_policy_decider;
use crate::process::ExecProcessEventLog;
use crate::process::sandbox_type_from_protocol;
use crate::process_sandbox::prepare_exec_request_with_telemetry;
use crate::protocol::ByteChunk;
use crate::protocol::EXEC_CLOSED_METHOD;
use crate::protocol::ExecClosedNotification;
use crate::protocol::ExecEnvPolicy;
@@ -95,19 +96,12 @@ const EXITED_PROCESS_RETENTION: Duration = Duration::from_millis(25);
#[cfg(not(test))]
const EXITED_PROCESS_RETENTION: Duration = Duration::from_secs(30);
#[derive(Clone)]
struct RetainedOutputChunk {
seq: u64,
stream: ExecOutputStream,
chunk: Vec<u8>,
}
struct RunningProcess {
session: ExecCommandSession,
tty: bool,
pipe_stdin: bool,
accepted_stdin_write_ids: Arc<Mutex<AcceptedStdinWriteIds>>,
output: VecDeque<RetainedOutputChunk>,
output: VecDeque<ProcessOutputChunk>,
retained_bytes: usize,
next_seq: u64,
exit_code: Option<i32>,
@@ -578,16 +572,12 @@ impl LocalProcess {
let mut total_bytes = 0;
let mut next_seq = process.next_seq;
for retained in process.output.iter().filter(|chunk| chunk.seq > after_seq) {
let chunk_len = retained.chunk.len();
let chunk_len = retained.chunk.0.len();
if !chunks.is_empty() && total_bytes + chunk_len > max_bytes {
break;
}
total_bytes += chunk_len;
chunks.push(ProcessOutputChunk {
seq: retained.seq,
stream: retained.stream,
chunk: retained.chunk.clone().into(),
});
chunks.push(retained.clone());
next_seq = retained.seq + 1;
if total_bytes >= max_bytes {
break;
@@ -984,6 +974,7 @@ async fn stream_output(
) {
while let Some(chunk) = receiver.recv().await {
let _chunk_len = chunk.len();
let chunk = ByteChunk::from(chunk);
let notification = {
let mut processes = inner.processes.lock().await;
let Some(entry) = processes.get_mut(&process_id) else {
@@ -994,26 +985,19 @@ async fn stream_output(
};
let seq = process.next_seq;
process.next_seq += 1;
process.retained_bytes += chunk.len();
process.output.push_back(RetainedOutputChunk {
seq,
stream,
chunk: chunk.clone(),
});
process.retained_bytes += chunk.0.len();
let output = ProcessOutputChunk { seq, stream, chunk };
process.output.push_back(output.clone());
while process.retained_bytes > RETAINED_OUTPUT_BYTES_PER_PROCESS
|| process.output.len() > RETAINED_OUTPUT_CHUNKS_PER_PROCESS
{
let Some(evicted) = process.output.pop_front() else {
break;
};
process.retained_bytes = process.retained_bytes.saturating_sub(evicted.chunk.len());
process.retained_bytes =
process.retained_bytes.saturating_sub(evicted.chunk.0.len());
}
let _ = process.wake_tx.send(seq);
let output = ProcessOutputChunk {
seq,
stream,
chunk: chunk.into(),
};
process
.events
.publish(ExecProcessEvent::Output(output.clone()));
@@ -1087,11 +1071,11 @@ fn watch_exit(
for chunk in &process.output {
match chunk.stream {
ExecOutputStream::Stdout | ExecOutputStream::Pty => {
stdout.extend_from_slice(&chunk.chunk);
stdout.extend_from_slice(&chunk.chunk.0);
}
ExecOutputStream::Stderr => stderr.extend_from_slice(&chunk.chunk),
ExecOutputStream::Stderr => stderr.extend_from_slice(&chunk.chunk.0),
}
aggregated.extend_from_slice(&chunk.chunk);
aggregated.extend_from_slice(&chunk.chunk.0);
}
let exec_output = ExecToolCallOutput {
exit_code,
@@ -1734,10 +1718,10 @@ mod tests {
panic!("process should be running");
};
running.output = (1..=retained_chunk_count)
.map(|seq| RetainedOutputChunk {
.map(|seq| ProcessOutputChunk {
seq,
stream: ExecOutputStream::Stdout,
chunk: vec![b'x'],
chunk: vec![b'x'].into(),
})
.collect();
running.retained_bytes = RETAINED_OUTPUT_CHUNKS_PER_PROCESS;