Commit Graph
2 Commits
Author SHA1 Message Date
yetone b047cfe73c gateway: TestCRTidyTwentyFourMBLine bounds the bytes searched and allocated, not the wall clock (#5282)
The test is the relay hot path's bill for one 24 MB SSE line in 32 KB
reads. It had an 8s wall-clock bar. On main CI's Race step, beside heavy
shards, one_call_mended took 16.9s on ubuntu and 10.7s on macOS. Alone
under -race on mote it takes 4.4s. The bar measured the machine, and it
also missed one of the regressions it was there for: a copy of the held
bytes on every read took 0.37s.

crTidy now counts the bytes it searches for a line's end or the escape
(looked, one add per write and per region). The test bounds two things
the code controls, each against the line's size:
- bytes searched, at most 3x. Measured: 2x with no escape, 3x mended.
- bytes allocated (runtime.MemStats), at most 8x with no escape and
  40x mended. Measured: 5.4x (append grows the held line a quarter at
  a time), and mended 27x, 32x under -race (decoded into maps and said
  again).

Broken by hand, on mote:
- searching the whole held buffer for a line's end on each read (the
  quadratic read from yetone's review of #4256): 9.7 GB searched, red
  in both subtests.
- copying what is held on each read: 9.7 GB and 10.2 GB allocated, red
  in both subtests. The old 8s bar passed it at 0.37s and 0.77s.
Restored, green.

Run on mote at this head:
- go test -race -count=3 -run TestCRTidyTwentyFourMBLine ./internal/gateway:
  ok. mended takes 4.4s, no escape 0.19s.
- go test -race -run 'CR|Glm|GLM|Tidy' ./internal/gateway: ok.
- go vet ./internal/gateway on darwin, GOOS=linux and GOOS=windows: ok.
  gofmt -l: clean.

Not tried: a regression that searches without going through the
counted sites. The count bounds only what passes them. The allocation
bound still catches one that copies.
2026-11-05 03:41:57 +08:00
ggbdpq dd279e9314 gateway: the CR a glm-5.3-flash* replica stitches into tool-call arguments
Yetone's review of the two earlier rounds (#4256): the cure changed
healthy replies and cost the relay hot path. This one keeps to the routes
the defect was seen on, and holds a cut escape the way the encoders do:

- The cure runs only on glm-5.3-flash* routes (glmFlash, the way
  grokModel scopes its rule): a relay installs crTidy for those models
  alone, translate sets Request.CR the way it sets Request.Grok, and the
  encoder wrapper crEncoder and the whole-reply pass crResult hang off
  that flag. Another model's reply goes as it came, healthy or not.
- The relay's mends wait per call (crTidy.calls, by Chat's tool_calls
  index, Messages' block index, Responses' item id), so a \r\n cut
  across two fragments arrives as the pair it is; a \r the next
  fragment does not answer with \n is the defect, and goes. A line's
  end is looked for in the new bytes of a read alone, and a region
  without the escape is said from where it lies: a 24MB line in 32KB
  reads costs a scan (23ms here; the round-2 way took 2.45s).
- The events whose delta is an argument are read from the event's own
  type or shape: a text delta that only mentions the arguments' event
  names stands, \r and all.
- A custom tool's raw input (Codex's apply_patch) is read as bytes: the
  CRLF pair is the line ending it says, a lone CR goes.
- The encoders' dropper is said at a call's end: a lone \r goes, a bare
  \ is said as it came (crEncoder.say).

Refs #4256 (kept open for the reporter)

Run: go test -tags nogui ./internal/gateway/... (ok). The healthy
split-CRLF relays of all three protocols, the custom input's, the text
delta's and the scope's tests fail on the round-2 branch and pass here;
unwiring the flag reds the cure tests again.

Landed by yetone on origin/main e2d7d1f54 with the next commit, run on
mote (macOS) at that tree: go test -tags nogui -count=1 -timeout 45m
./internal/gateway/... ok (506s); GOOS=linux and GOOS=windows go vet
./internal/gateway ok. Rebased onto f45c3c96b (usage and site only)
before the push; there go vet and the 25 CR tests pass again. Broken by hand: glmFlash returning false reds the 13 cure tests
and TestGLMFlashScope; a per-fragment dropCR in place of the per-call
seam reds exactly the four healthy-split tests. Timed on a valid 24MB
response.completed line in 32KB reads: healthy 25ms, text with \r\n
161ms, a call mended 281ms. A model outside glm-5.3-flash* gets no
crTidy and no Request.CR (TestForeignModelStitchedStreamGoesAsItCame).
The 24MB test's mended case was vacuous; the next commit fixes it.

Generated-by: GLM-5.3-Flash (ZCode)
2026-10-31 01:53:21 +08:00