Skip to content

perf(cli): coalesce streaming-chunk posts per chunkId (latest wins, one in flight) - #999

Merged
edgar-durand merged 1 commit into
mainfrom
perf/cli-coalesce-streaming-chunks
Oct 29, 2026
Merged

edgar-durand merged 1 commit into
mainfrom
perf/cli-coalesce-streaming-chunks

Conversation

@edgar-durand

Copy link
Copy Markdown
Collaborator

Why

2026-10-29 p99 alert: one CLI posted 10-30 POST /api/sessions/:id/streaming-chunk per second, each carrying the CUMULATIVE content of the same chunkId. The backend now caches the share-recipient read; this stops the flood at the source. Bead codeagent-p6nu.

What

New apps/cli/src/services/streaming/chunk-coalescer.ts (StreamingChunkCoalescer), used by both publishers of the feed:

  • AcpPublisher.publishStreamingChunk (ACP agents) — replaces the >=8 KB-only coalescing; big snapshots keep their 300 ms window, smaller ones get 100 ms.
  • StreamingEmitterService (PTY agents) — its POSTs (with their retry) now go through it; stop() flushes.

The VS Code extension and the JetBrains plugin do not post this endpoint.

Per chunkId:

  • first snapshot goes out immediately (no added latency);
  • at most one request in flight, >=100 ms between posts, latest content wins;
  • isFinal supersedes anything pending, skips the spacing wait, is sent right after the in-flight request and is always the last post; later non-final snapshots of that id are dropped (monotonic);
  • chunkIds are independent; a new message never waits on an older one.

Shutdown: the ACP SIGINT/SIGTERM path awaits publisher.flushStreamingChunks() (bounded to 2 s); turn close already sends isFinal for every open chunk, which supersedes any pending snapshot. Errors keep their current handling (send never throws; PTY keeps its retry inside one send).

Measured

Simulated 20 updates/s stream for 10 s (chunk-coalescer.test.ts):

request latency before after
60 ms 201 posts 102 posts
300 ms (slow backend) 201 posts 35 posts

Tests

  • New __tests__/services/chunk-coalescer.test.ts (fake timers): burst of 30 → 2 posts with the last content; done always last and delivered after in-flight; in-flight + newer → only latest; monotonic ordering; two interleaved chunkIds; flush; send failure; the measurement.
  • acp.publisher.streaming-chunk-cap.test.ts: the "every small delta is posted" case now asserts the new pacing; added a flush case.
  • Ran locally: the coalescer + publisher + streaming-emitter + all ACP runner tests (67 files, 680 passed), tsc --noEmit clean.

🤖 Generated with Claude Code

…ne in flight)

2026-10-29 p99 alert: one CLI posted 10-30 cumulative streaming-chunk
snapshots per second for the same chunkId. A new StreamingChunkCoalescer
paces both publishers (ACP AcpPublisher and the PTY StreamingEmitterService):
the first snapshot goes out at once, then at most one request in flight and
>=100 ms between posts per chunkId, latest content wins. The isFinal frame
supersedes anything pending, skips the spacing wait and is always sent after
the in-flight request, so it is the last post of its chunkId; late non-final
snapshots after it are dropped. chunkIds never wait on each other. Big
snapshots (>=8 KB) keep their 300 ms window. Shutdown flushes pending content.

Measured on a simulated 20 updates/s stream (10 s): 201 -> 102 posts at
60 ms request latency, 201 -> 35 at 300 ms.

Bead: codeagent-p6nu

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T17:28:02.211041Z d15e55f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@edgar-durand
edgar-durand merged commit be7a518 into main Oct 29, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants