Commit Graph
2 Commits
Author SHA1 Message Date
yetone dcfda5f166 gateway: a relayed Responses stream's tidies pass its events in place (#3383)
idTidy and phaseTidy each copied every event of a relayed Responses
stream twice, once out of their buffer and again onto an output grown
event by event, and dropped their buffer's bytes at each write so the
next write took new ones. eventData copied each event's data even when
it is one line, as nearly every upstream's is, and phaseTidy decoded a
reply's whole last event into a map to find no message without a phase.

- tidyEvents goes through a write's whole events into one output sized
  for them; an event that goes as it came is copied once, and the part
  of an event still coming moves to the buffer's front.
- eventData gives a one-line event's data as ev's own bytes; its callers
  only read it.
- phaseTidy.ended reads the output in place (anyUnphased, which agrees
  with unphased item by item) and decodes only when a message needs a
  phase.

Measured with BenchmarkZZCodexStream (a 680 KB Codex /v1/responses
stream through serve, kept out of the tree): 14.16 -> 12.53 MB/op,
~53,450 -> ~49,700 allocs/op.

Run at this head:
- go test ./internal/gateway -timeout 45m: ok (439s, at 848b3bf7e rebased on 4b9694c34)
- New tests: TestAnyUnphasedAgreesWithUnphased (null, number and
  missing phase and role, a non-list output), TestEventDataJoinsAsSSE,
  TestTidiesPassAStreamSplitAnywhere (1, 7, 64 bytes and whole, the
  caller's buffer cleared after each write). Broken by hand: moving
  nothing to the buffer's front turned the split test red, and reading
  a null role as an assistant's turned the agreement test red; restored,
  green.
- gofmt -l clean; go vet ./internal/gateway on darwin, and GOOS=linux and
  GOOS=windows with -tags nogui: ok
- Real APIKEY.FAN account (deepseek-flash over its /v1/responses, so
  both tidies run; sandbox HOME with only that provider copied): five
  Codex-shaped streams, 5 of 5 answered "ok" at 1859da57b and at this
  head.
- Those five real replies, the same with every phase taken out, and a
  real OpenAI billing_not_active reply (error then response.failed),
  replayed to both builds in 7-byte, 4 KB and whole chunks: the client's
  bytes are identical between the builds in every case but the error
  reply at 7 bytes, where both builds cut the stream at a varying point
  after the error event (old: 3633 and 4757 bytes;
  new: 3500, 3857 and 4757),
  as before this change.

Not tried: a real Copilot account (the case idTidy is for); the OpenAI
key's account is not active.
2026-10-24 22:20:26 +08:00
yetone 55d6ea4ffc gateway: a translated model's messages say their phase, so Codex folds them (#2437)
Codex Desktop folds a finished turn's commentary and calls under "Worked
for …" by each assistant message's `phase`, which OpenAI's own models
write ("commentary" / "final_answer"). A message with none is read as an
answer, so every progress line of a model magpie translates (the
reporter's Qoder plugin, a chat upstream) stayed unfolded.

The Responses encoder now holds a message's output_item.done until the
next item that isn't reasoning opens (commentary) or the reply completes
(final_answer); the completed output carries the same phases.
renderResponses does the same for a whole reply. A Responses upstream
relayed as it is (passthrough) gets the same rule through a new phase
tidy, streamed or whole. A phase the upstream wrote is kept; a reply cut
short (incomplete, failed, error, a stream with no end) guesses no final
answer, since Codex reads a missing phase as the answer. Keepalives go
out at once; sequence numbers are given in write order. At most 512
events wait behind a message, then it goes without a phase. Codex's
base_instructions are not touched.

Run (worktree at origin/main cf21b71ff):
- new tests in internal/gateway/phase_test.go: encoder (call, reasoning
  then call, last, reasoning after, length cut, two messages around a
  call, keepalive while held), renderResponses, phase tidy (SSE fed a
  byte at a time, CRLF, upstream phase kept, incomplete, no end, whole
  reply with a 20-digit number kept) and a passthrough through the gateway
- broken by hand, each went red: encoder release ignoring the phase (3
  tests), renderResponses without phaseOutput (1), passthrough without
  the tidy (1), unphased ignoring an upstream phase (1 subtest)
- gofmt -l clean; go vet -tags nogui for darwin, linux, windows ok
- full suite: set -o pipefail; HOME=<scratch>/home-2437 GOMODCACHE=~/go/pkg/mod go test -tags nogui -count=1 -timeout 45m ./... at cf21b71ff plus this change: exit 0, 66 packages ok, none FAIL (gateway 844s, provider 355s)

- rebased onto origin/main a61a6f954 and run again: gofmt -l clean; go vet -tags nogui darwin, linux, windows ok; set -o pipefail; go test -timeout 45m ./... in a scratch HOME: exit 0, 66 packages ok

Full suite: green at a61a6f954 plus this change, 66 packages ok, exit 0.
2026-10-16 02:43:02 +08:00