Resolve `previous_response_id` to a prompt prefix instead of replaying every Responses item

perfloop/ds4 · Redundant Serialization

https://perfloop.ai/t/oss/case_avefhzk2jy

Verdict

VERIFIED · settled 2026-11-16

What happened: The paired measurements met the required improvement.

Hypothesis

`client_main` currently passes the entire body to `parse_responses_request`, whose array-input path makes `parse_responses_input` walk every replayed item, decode its strings through `json_string` and growable buffers, construct transient `chat_msgs`, then render and tokenize the whole history. Only after that conversion does `generate_job` get a chance to recognize a `responses-visible` live prefix and prefill just a suffix. The source explicitly rejects non-null `previous_response_id` and `conversation`, emits `resp_` ids only while serializing a response, and retains only a per-slot live frontier, while the Responses note documents clients that resend full visible replay; consequently the same semantic history is repeatedly JSON-encoded, decoded, rendered, and tokenized before the existing prefix machinery can reuse it. A durable response-id prefix index would remove that historical conversion class on stateful continuations while preserving hidden reasoning and tool-call bindings through the saved exact checkpoint; its hit-path floor is state lookup, validation, delta parsing, tail rendering/tokenization, and any checkpoint load or suffix prefill, while first turns, misses, and edited branches keep the current full replay behavior. A later controlled growing-conversation `/v1/responses` trace should separately record request bytes, client-thread parse/render/tokenize allocation and CPU, state-hit rate, and cached versus prefetched tokens; it falsifies this ceiling if stateful continuations are not materially exercised or those history-sized ingress terms do not become bounded by the new tail.

Change to test: Generate one stable `resp_` id before either streaming or non-streaming emission and maintain a bounded, tenant-scoped append-only response-state index keyed by it. Each record should retain the exact token/KV checkpoint reference, prompt syntax and configuration fingerprint, continuation rendering boundary, visible/model prefix metadata, and generated call ids. For a request carrying `previous_response_id`, resolve and validate that record, parse only the supplied delta input, render and tokenize only a generic continuation tail, and synchronize that tail onto the saved prefix; retain the existing full-input replay path for stateless requests, expired or mismatched records, and explicit edits.

Where it lives

perfloop/ds4 · ds4_server.c

Evidence

growing code-audit Responses conversation · 26 sample pairs

metric baseline candidate paired median change confidence range required result
host_ingress_ns/op 4862121 496566 −89.5% (−4349829) −4645714 to −4117477 gain > 0; gain > 0.05 × |baseline median|; arm medians separated PASSED
host_cpu_ns/op 4921875 703125 −86.5% (−4257813) −4687500 to −3828125 loss ≤ 0.05 × |baseline median| PASSED
peak_rss_bytes 32663552 31932416 −167936 −2838528 to +1355776 loss ≤ 0.05 × |baseline median| PASSED

disk-cached code-audit Responses conversation · 11 sample pairs

metric baseline candidate paired median change confidence range required result
host_ingress_ns/op 442993259 691877 −99.8% (−442236692) −452292318 to −415739206 loss ≤ 0.05 × |baseline median| PASSED

stateless code-audit Responses request · 58 sample pairs

metric baseline candidate paired median change confidence range required result
host_ingress_ns/op 4243007 4247004 −13612 −194475 to +188482 loss ≤ 0.05 × |baseline median| PASSED
peak_rss_bytes 32454656 31967232 −440320 −1654784 to +843776 loss ≤ 0.05 × |baseline median| PASSED

smallest stateless Responses request · 82 sample pairs

metric baseline candidate paired median change confidence range required result
host_ingress_ns/op 329253 334621 +2891 −10180 to +15604 loss ≤ 0.05 × |baseline median| PASSED

disk-cached stateless code-audit replay · 48 sample pairs

metric baseline candidate paired median change confidence range required result
host_ingress_ns/op 136445872 136322319 +312678 −7278719 to +5790115 loss ≤ 0.05 × |baseline median| PASSED

disk-cached first Responses request · 165 sample pairs

metric baseline candidate paired median change confidence range required result
host_ingress_ns/op 314084143 314450226 +691908 −17143325 to +14824525 loss ≤ 0.05 × |baseline median| PASSED

disk-cached first response-id continuation · 14 sample pairs

metric baseline candidate paired median change confidence range required result
host_ingress_ns/op 427966955 310575387 −27.7% (−118660350) −179926990 to −52394254 loss ≤ 0.05 × |baseline median| PASSED

The check fails before this change and passes with it: `With a deterministic model double and the Flash tokenizer, text-only HTTP Responses chains expose stable streaming and buffered IDs, preserve exact saved token prefixes and tool-call bindings, reject invalid or oversized deltas without consuming the saved state, and recover retained IDs after restart.`

Checks: 4 of 4 passed. Verification: no defect found.

Timeline