Files
magpie/docs
yetone ba3a7384fb gateway: a Responses request may name the response it follows (previous_response_id, kevinzhow on X)
kevinzhow on X: magpie didn't support previous_response_id on
/v1/responses, so every turn carried the whole conversation, images
included, and each turn was slow.

Before: magpie kept no responses. previous_response_id went on as
sent, so a provider that never made that id (or a route that translates
to Chat or Messages) had nothing but the new input to give the model.

After (prev_response.go, run in serveAgent before anything reads the
body):
- Each Responses reply passed with 200 (not failed/cancelled, not cut at
  64 MB) is kept by its id with input + output, in memory, LRU to 256 MB
  of own bytes, 6h TTL. store:false keeps nothing.
- Kept per caller (prevScope): gateway key, remote magpie, keyless remote
  address. Another caller naming the id is told it isn't there.
- A kept id expands the request to the whole conversation, so routing,
  translation and masking see what they would have seen had the client
  sent it all. Masking runs on each request as it goes (deterministic
  placeholders), so a stored secret is masked the same on every turn;
  TestPreviousResponseMasked.
- An unknown id gets OpenAI's 400 previous_response_not_found on a route
  that translates (and on Claude/Codex accounts), before anything is sent.
  A Responses relay is sent the id as it came.
- Where the provider keeps responses itself (prevKeeps: API key, no
  plugin; api.openai.com, Azure, or Ark /api/v3 pay-as-you-go) and it
  answered the previous turn on the same key, with store not off, only
  the new input goes with the provider's own id (prevDelta). A provider
  that says it doesn't know the id (prevRefused) is sent the whole
  conversation at once.

Codex: its HTTP ResponsesApiRequest has no previous_response_id; only the
websocket's ResponseCreateWsRequest has it (openai/codex 9def09a,
codex-rs/codex-api/src/common.rs:341, client.rs:2284). Over HTTP it sends
store:false with the whole input, so nothing it sends or keeps changes.
No prompt is touched.

Ark checked with a real pay-as-you-go key (10-29): store:true then
previous_response_id returned the stored word; an unknown id answers 400
InvalidParameter.PreviousResponseNotFound (param previous_response_id),
which prevRefused matches.

Tests (prev_response_test.go): GivenWholeToChat (3 turns), NotFound
(unknown, other key, no key, store:false; upstream not called),
UnknownPreviousResponseRelayedAsSent, KeptUpstreamSentNewInputOnly
(200 KB image; delta < 2000 bytes; refusal retried whole), Masked,
PrevStoreBounded.

Broken by hand, each red, then restored:
- previousResponse a no-op: GivenWholeToChat, NotFound, KeptUpstream,
  Masked red.
- prevDelta off: KeptUpstream red (200355 bytes sent, no prev id).
- prevRefused always false: KeptUpstream red at the retry turn (400
  relayed).

Timings on mote (BenchmarkImageRequest / BenchmarkPreviousResponseTurn,
8 images, 20.18 MB conversation):
- whole Responses -> Responses: 277 ms, 145.6 MB/op before; 263 ms,
  145.6 MB/op after (a first version that json.Unmarshal'd the input
  cost 364 ms and +20 MB; it now reads items over the body's bytes).
- whole Responses -> Chat: 273 ms before, 224 ms after.
- previous_response_id to a provider that keeps responses: client sends
  82 B, upstream gets 122 B (was 20.18 MB each way), 309 ms locally.
- previous_response_id to Chat: client sends 83 B, upstream gets the
  whole 20.18 MB, 295 ms.

Run at this head on mote: gofmt -l clean; go vet ./internal/gateway for
darwin, linux, windows ok; go test -count=1 -timeout 45m
./internal/gateway ./internal/redact: gateway ok (464.4s), redact ok (1.8s).

Not tried: a real OpenAI or Azure key (Ark's real key only); Ark's
Coding Plan Responses path (not in prevKeeps). The store is in memory,
so ids are lost on restart (a translated route then answers 400, a relay
passes the id on). A kept-id request is still read and masked whole on
this computer before the delta is cut (~300 ms local for 20 MB).
Doc: docs/subsystems/gateway-routing.md, "A Responses request that names
the response it follows".
2026-10-29 21:25:33 +08:00
..