Reduce unified exec output buffer allocations (#39712)

## What changed

- Accept borrowed byte slices when appending output chunks, avoiding clones before
  forwarding those chunks to streaming consumers.
- Merge drained head-tail buffers by reusing their owned storage while preserving
  the retained prefix, latest suffix, and omitted-byte count.
- Make unified exec output handles capacity-generic so bounded-output tests can
  exercise repeated drains with small buffers.

## Testing

- Update head-tail buffer and process manager tests to cover chunk retention,
  omission accounting, and repeated output drains with the new APIs.

GitOrigin-RevId: caa9813d3c9d9921cff28888263a212def381640
This commit is contained in:
Tamir Duberstein authored and copyberry committed 2026-08-20 13:17:20 +00:00
1 parent 59f7da58d6
commit 4a942885c8
7 files changed
+118 -134

No files matched your search

+6 -6
View File
@@ -29,6 +29,7 @@ use codex_utils_pty::ExecCommandSession;
use codex_utils_pty::ProcessSignal as PtyProcessSignal;
use codex_utils_pty::SpawnedPty;
use super::UNIFIED_EXEC_OUTPUT_MAX_BYTES;
use super::UNIFIED_EXEC_OUTPUT_MAX_TOKENS;
use super::UnifiedExecError;
use super::head_tail_buffer::HeadTailBuffer;
@@ -56,11 +57,10 @@ pub(crate) struct NoopSpawnLifecycle;
impl SpawnLifecycle for NoopSpawnLifecycle {}
pub(crate) type OutputBuffer = Arc<Mutex<HeadTailBuffer>>;
/// Shared output state exposed to polling and streaming consumers.
#[derive(Clone)]
pub(crate) struct OutputHandles {
pub(crate) output_buffer: OutputBuffer,
pub(crate) struct OutputHandles<const MAX_BYTES: usize = UNIFIED_EXEC_OUTPUT_MAX_BYTES> {
pub(crate) output_buffer: Arc<Mutex<HeadTailBuffer<MAX_BYTES>>>,
pub(crate) output_notify: Arc<Notify>,
pub(crate) output_closed: Arc<AtomicBool>,
pub(crate) output_closed_notify: Arc<Notify>,
@@ -501,7 +501,7 @@ impl UnifiedExecProcess {
for chunk in chunks.into_iter().filter(|chunk| chunk.seq > last_seq) {
let bytes = chunk.chunk.into_inner();
let mut guard = output_buffer.lock().await;
guard.push_chunk(bytes.clone());
guard.push_chunk(&bytes);
drop(guard);
let _ = output_tx.send(bytes);
output_notify.notify_waiters();
@@ -544,7 +544,7 @@ impl UnifiedExecProcess {
last_seq = chunk.seq;
let bytes = chunk.chunk.into_inner();
let mut guard = output_buffer.lock().await;
guard.push_chunk(bytes.clone());
guard.push_chunk(&bytes);
drop(guard);
let _ = output_tx.send(bytes);
output_notify.notify_waiters();
@@ -605,7 +605,7 @@ impl UnifiedExecProcess {
match receiver.recv().await {
Ok(chunk) => {
let mut guard = output_buffer.lock().await;
guard.push_chunk(chunk.clone());
guard.push_chunk(&chunk);
drop(guard);
let _ = output_tx.send(chunk);
output_notify.notify_waiters();