Repository navigation
Conversation
Avoid redundant Blob copies for shared model requests and normalize Anthropic system blocks once per prepared prompt revision. Preserve exact request bytes, editable hooks, stream metadata, and native Fetch replay. Keep Anthropic frozen-message Blob composition because the string alternative regresses CPU and allocation.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
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 reuses encoded request buffers and normalizes Anthropic system blocks once per prepared prompt revision and cache policy. Example: Prepare repeated requests with a 614,400-byte encoded body.
Review scores
ProductKind: Performance · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR provides a useful, focused performance improvement absent from current main; no actionable correctness or security findings were identified. Priority: P2 Before mergeNone. FindingsNone. Tests
Agent review detailsHow this fits togetherThe AI package converts prepared agent context into provider requests; encoded bodies pass through SDK dispatch and guarded fetch, while Anthropic system blocks pass through cache policy and editable payload hooks. flowchart TD
A[Prepared agent context] --> B[Anthropic system policy]
B --> C[Editable payload hooks]
A --> D[Shared request encoder]
C --> E[Provider request]
D --> E
E --> F[Guarded fetch]
F --> G[Provider endpoint]
Technical reviewBest possible solution: Reuse the existing encoder buffer and revision-owned system projections while preserving exact bytes, editable hooks, and cache-policy separation. Do we have a high-confidence way to reproduce the issue? The base source visibly constructs the redundant Blob and repeats system normalization; the PR supplies paired measurements and reports discriminating base failures, but no target code was executed during this review. Is this the best way to solve the issue? The patch is a small complete optimization using existing owners, with benchmark-only flags and no runtime configuration or persisted-format changes. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 194b10349b12. Provenance checked
TestingProof path: in-process harness. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +26, Tests +103, Other +159. Total +288 across 10 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 31, 2026, 11:12 AM ET / 15:12 UTC. |
|
Merged via squash.
|
…89476) Avoid redundant Blob copies for shared model requests and normalize Anthropic system blocks once per prepared prompt revision. Preserve exact request bytes, editable hooks, stream metadata, and native Fetch replay. Keep Anthropic frozen-message Blob composition because the string alternative regresses CPU and allocation.
What Problem This Solves
Repeated large model requests allocate redundant body copies and normalize the same Anthropic system prompt again on every call.
User Impact
Lower request-preparation CPU and allocation, with unchanged request bytes, prompt-cache boundaries, diagnostics, and retry behavior. No configuration or migration is needed.
Why This Change Was Made
The shared OpenAI request owner now passes its already-encoded Uint8Array directly to Fetch. This removes the intermediate Blob and the fetch guard's materialization of that Blob; native Fetch still takes its required dispatch snapshot. The unused encoded-body observer is removed. Generic Blob replay handling remains intact.
Anthropic system normalization now follows the existing immutable prepared-prompt revision, with separate cache-policy variants. Payload hooks receive editable copies, OAuth identity headers remain current, and obsolete revisions are weakly owned. This uses the session's existing revision lifetime rather than adding a string pool or eviction policy.
Anthropic's frozen-message Blob fragments from #186872 remain: an exploratory cached-string/byte composition comparison increased CPU and JS allocation. Codex inference already retains one upload buffer and has no matching Blob conversion.
Evidence
Five serial A/A/B pairs per mode; timing and allocation sampling ran separately. Synthetic 600 KiB UTF-8 request bodies, roughly a 150k-token byte budget; no tokenizer or real user data. Shared handoff exercised the real fetch guard and native Request body consumption. Encoding and network/model latency are excluded.
Two additional native 614,400-byte copies per request are eliminated; they are excluded from sampled JS allocation. Every arm produced the same wire SHA-256.
Wire hashes match across tool turns, moved cache boundaries, payload edits/replacements, and both revisions. The new regressions fail on the original owner: encoded-buffer identity differs, and repeated system normalization occurs 12 times instead of 6. Native Request replay preserves bytes after mutation of the first consumed snapshot. The system regression covers interleaved revisions/policies, editable blocks, stale revisions, and changing OAuth versions.
Reproduce shared handoff with
node --expose-gc --import ./scripts/tsx.mjs scripts/perf/model-request-body-measure.ts --base ed51affd7873d3e3125d9e3b72d65535e874fbf4 --iterations 500 --pairs 5, then repeat with--allocation. System owner usesscripts/perf/anthropic-request-construction.ts --messages 150 --mode system --prepared-system --iterations 20000, with the two prior Anthropic owner modules supplied through its existing--baseline-diroverlay option. The acceptance rule is identical bytes, lower owner CPU/allocation, and no material GC regression.Focused validation: 34 policy/transport tests, 114 body/diagnostic/fetch tests, 14 prompt-prefix tests, and the immutable Blob redirect/retry regression passed. Actual regression test time is milliseconds; first-run compiled prefix proof took 79 seconds including compilation. Package artifact E2E is left to PR CI. No whole-Gateway, live-provider cache-token, or production-runtime improvement is claimed.
Production delta: +26 lines. Shared body handling shrinks by 2; revision-owned system reuse adds 28 to preserve policy separation and editable hooks. The +103 test-line allocation is scoped to the two regression contracts. Open #170438 touches the same Anthropic modules but changes message checkpoint placement, not the system helper.
Independent Codex review is clean through P2.
pnpm check:changedpassed every gate except three new non-null assertions in the system regression; those were replaced by an explicit fixture check, and targeted lint plus the 10-test policy suite passed afterward. The test-line ratchet also passed. No inherited failure is being excused.