Repository navigation
perf(agents): avoid promise assimilation for observed stream deltas - #184369
Conversation
Replace the diagnostic async generator with a serialized iterator while preserving context, queued operations, source-value snapshots, and cleanup ownership. Five paired owner measurements reduce allocation 10.9% and main-thread CPU 5.7%.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review endedClawSweeper stopped reviewing this revision because the pull request closed. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex review: needs maintainer review before merge. What this changesThis PR replaces the diagnostic stream’s async generator with a serialized iterator to reduce per-delta promise work. Example: A model response emits 100 text deltas followed by its terminal event.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep open: this PR provides a measured streaming optimization, and source review found no actionable introduced defect; the required CI gate remains red. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe embedded agent’s diagnostic wrapper consumes provider streams, observes events and final results, and forwards them to agent consumers while retaining transport context and cleanup ownership. flowchart LR
A[Provider stream] --> B[Plugin binding]
B --> C[Diagnostic iterator]
C --> D[Agent consumer]
C --> E[Diagnostic lifecycle]
C --> F[Tracked cleanup]
Technical reviewBest possible solution: Retain the focused iterator replacement and resolve the required CI failure’s attribution before landing. Do we have a high-confidence way to reproduce the issue? The base generator yields every observed chunk through promise assimilation, and the supplied regression reports a base failure on the event’s then-property read; this review executed no target code. Is this the best way to solve the issue? The replacement removes the identified work within the existing owner, preserves single-read event snapshots and tracked settlement, and introduces no competing implementation. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 423ae422ef67. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +49, Tests +101. Total +150 across 2 files. Added test files: 0. View PR surface stats
Review metrics
LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed October 26, 2026, 4:08 AM ET / 08:08 UTC. |
|
CI attribution update: the two Both local runs used the single failing file with |
|
Maintainer admin merge at the pinned head I reverted Equivalence review: the hand-written iterator matches the old async generator's semantics:
Owner benchmark: −10.9% allocation and −5.7% CPU per 1,000 deltas. Root-cause fixes for the main reds are tracked separately. No reruns. |
…penclaw#184369) Replace the diagnostic async generator with a serialized iterator while preserving context, queued operations, source-value snapshots, and cleanup ownership. Five paired owner measurements reduce allocation 10.9% and main-thread CPU 5.7%.
What Problem This Solves
Streaming model diagnostics create unnecessary promises for every delta by yielding concrete assistant events through an additional async generator. This is residual work after #183875, which is already an ancestor of the profiled build
b70762fc.User Impact
Small reduction in streaming allocation and main-thread CPU, with no intended change to event values, order, diagnostics, cleanup, or transport/plugin context.
Why This Change Was Made
Replace the generator with one serialized iterator. Sequential reads add one shared observation continuation; only overlapping operations chain behind an outstanding operation. Keep per-operation transport context entry and snapshot each source value once. Forwarding the original iterator-result object would re-read accessor-backed values and change the event consumers receive.
Production delta: +49 lines (100 added, 51 removed). The explicit FIFO/closed state replaces native generator serialization and preserves queued return/throw, unopened cleanup, rejected return values, and EOF result ownership. There is no parallel fallback, cache, new option, or public API.
Evidence
CI is blocked by a reproduced inherited failure, not green. On exact head
f679a7d7ae5a2431f41eacf86428a73099a1085d, changed-test job 121458275766 fails the twofinalizes opaque native usage only for its current/replaced writercases insrc/agents/embedded-agent-runner/run.shared-integration.test.ts. Both also fail on the unchanged main parentbfc4c09267ee5909ce050f80eed56c0f704523a3withSqliteSessionMutationConflictError,operationLabel: session-entry.patch. This harness mocksrunEmbeddedAttempt, so it does not execute the changed observer.Head and parent reproduction (same pinned local Bun runtime as the failing CI shard):
node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run.shared-integration.test.ts \ --maxWorkers=1 -t 'finalizes opaque native usage'Each run: 2 failed / 154 skipped, same error signature. The failed CI job's other 154 cases passed. No CI rerun was requested. The PR is left ready and unmerged for the owning maintainer; the requested exact-head-green completion condition remains unmet.
The existing committed workload is
scripts/perf/model-stream-observation-measure.ts, with a real diagnostic observer andPluginInstancebinding around a synthetic streaming provider. Linux, Node 26.12.1; base source fromc076148ee7e5600e581a60e7b338d83406ab09c3. Five concurrent synchronized pairs, alternating interleaved CPU sets under the sandbox quiet lock. Untraced timing and sampled-allocation passes are separate. Each timing arm runs 5,000 turns / 10,000 requests / 1,000,000 deltas; each allocation arm runs 2,000 turns / 4,000 requests / 400,000 deltas. Warm-up, interleaved request identities, exact event identity/sequence and terminal metrics are checked by the workload.Allocation and CPU improved in all five pairs. These are owner measurements, not a claim of an equivalent whole-Gateway improvement. This lane uses the requested focused fast-path proof exception; no 10-minute Gateway load campaign or live Gateway load was run. No
sessions.list, server-handler latency, RSS/heap, or whole-Gateway CPU claim is made.Paired measurements and limits
Event-loop p99 is context only: each arm has fewer than 1,000 samples. Each arm has two major collections, so major-GC tails are also context only. Minor-GC totals across the five pairs: 810.17 → 742.83 ms; major: 147.02 → 156.87 ms; incremental: 4.93 → 5.88 ms. No adverse conclusion is drawn from those small major/incremental deltas; total GC/work improves. A/A rejection controls were therefore not triggered. Timing host load was 3.63–4.24; allocation-pass load rose to about 15, shared by both concurrent arms. Dirty pages stayed below 200 MB. An initial synchronization setup failed before producing measurements; those arms are excluded, and all five recorded pairs use a fresh start directory.
The regression
does not assimilate concrete event values as promisesfails on the base (thenread 1, expected 0) and passes with the candidate, preserving event identity. Additional cases protect overlapping reads followed by return, throw, or a rejected return value. Existing frozen-provider/accessor and interleaved transport-context tests remain intact.Validation: 87 focused tests passed across diagnostic events, lifecycle and stream custody; changed-file formatting, lint and the single targeted core typecheck. The larger stream-custody file takes 18 seconds of test execution because it exercises real replay repair, cancellation and tracked cleanup; the two diagnostic files take under one second of test execution together. No Testbox lease was used. Exact-head PR CI provides broad proof.
Review: independent subagent review (autoreview route unavailable), clean at P2; the configured autoreview helper rejected the requested provider route before inference. Configuration was not changed.