Skip to content

improve(ai): reduce repeated work in Responses continuations - #192702

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-responses-continuation-normalize-alloc-20260919
Nov 4, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-responses-continuation-normalize-alloc-20260919

Conversation

@steipete

@steipete steipete commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Long OpenAI Responses tool conversations repeatedly allocate normalized copies of history that conversion has already prepared and frozen.

User Impact

Unchanged immutable histories need substantially less local CPU and allocation per continuation request. Wire deltas, response IDs, and tool-result ownership stay unchanged. Mutable hooks, reconstructed histories, and images retain the existing comparison path; no configuration or migration is required.

Why This Change Was Made

The continuation owner now compares the producer's existing immutable item identities before rebuilding both historical owner maps and JSON projections. This introduces no new cache or retained state. The latest response and every pending/delta tool result still undergo ownership checks. An identical already-sent prefix can continue even when historical IDs share a replay alias; changed colliding results still reject continuation.

Runtime production delta is +18 LOC: the identity admission branch preserves the complete mutable/replacement fallback. Tests add 51 lines for reuse, replacement/unknown-result rejection, and historical alias safety. The benchmark adds 10 lines to distinguish reused identities from mutable-image history.

Evidence

Synthetic owner fixture: 200 growing decisions after 120 initial turns, 604–1,599 text/tool/reasoning items, approximately 4 KiB new content per turn. Five conversations per sample and five alternating A1/A2/B samples per metric; timing and allocation run in separate processes on Node v26.12.1. Acceptance: unchanged prefix/delta correctness, CPU and allocation improvement beyond A/A spread, no material fallback regression.

Warm metric per decision Before median (p95) After median (p95)
Main-thread CPU 1,329.43 µs (1,368.08) 32.01 µs (32.88)
Wall time 1,330.44 µs (1,368.22) 31.44 µs (31.80)
Process CPU 1,344.62 µs (1,381.28) 45.83 µs (47.00)
Sampled allocation 2,303,176 B (2,317,835) 7,475 B (7,754)

Main-thread CPU falls 97.6%, allocation 99.7%. A/A median drift: CPU 0.69%, allocation 0.10%. Five-sample p95 uses the maximum. With a mutable image every 25 turns, A1/A2/candidate CPU medians were 1,681.80/1,519.82/1,536.19 µs and allocations 2,318,644/2,321,587/2,327,882 B: no mixed-image speedup claimed. Profiles confirm the historical normalization/ownership-map work disappears from the shared-prefix path. These are owner measurements, not whole-Gateway or live-provider latency claims.

Reproduce on parent and candidate with the committed benchmark (copy the parent's owner beside the current file and pass --module for the baseline):

node --expose-gc --import ./scripts/tsx.mjs scripts/perf/responses-continuation.ts --flow resolve --initial-turns 120 --frozen --reuse-input --image-every 0 --conversations 5

Add --allocation for sampled allocations; use --image-every 25 for the fallback control.

  • The reuse regression fails on the exact parent with two historical object enumerations where zero are required, then passes on the candidate.
  • 148 focused tests pass, including real HTTP loopback continuation, noncanonical IDs, async replay, retention, reasoning/compaction replay, and prompt-prefix stability (64.55 s overall).
  • Chromium import smoke and 8 UI streaming E2E tests pass. No new Node imports; the changed owner has no UI runtime importer.
  • Codex autoreview of the complete local change against 5d3c080a173fe4690e117e689248aae4a4f7261b, through P2: scoped clean, no actionable findings. Author simplification pass retained only the identity admission branch and existing fallback.
  • pnpm check:changed completed all selected checks. Its initial package-test typecheck found two fixture index reads typed as possibly undefined; both were repaired, and the failed package typecheck plus owning tests were rerun successfully. All other selected checks passed. Final formatting and test-line budget checks pass.

No production systems or data were accessed. Session database and workspace size do not affect this owner, which only receives prepared request history. Stream decoding already scans new text plus bounded delimiter overlap; this PR does not change it.

@clawsweeper

clawsweeper Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts size: S labels Nov 4, 2026
@steipete
steipete marked this pull request as ready for review November 4, 2026 10:01
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T10:03:20.744954Z 96bb29d Draft marked ready
ℹ️ 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.

@steipete
steipete merged commit 2896c3f into main Nov 4, 2026
169 of 171 checks passed
@steipete
steipete deleted the steipete/perf-micro-responses-continuation-normalize-alloc-20260919 branch November 4, 2026 10:13
@steipete

steipete commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scripts Repository scripts size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant