Commit Graph
4 Commits
Author SHA1 Message Date
yetone fafce65455 gateway: the facts' allocation bound is checked when not under -race (#3956)
TestChatAndMessagesFactsLeaveTheHistory (9a888323a, #3472) was red every
time under -race on ubuntu and macOS CI from ec6c2de52 on; earlier runs
hid it, as their Test step was red and the Race step never ran.

regexp keeps its backtracker in a sync.Pool, and under -race sync.Pool
lets a quarter of what is put in it go, so userSaid's matches over a long
conversation made new ones: a memory profile of readFacts alone shows
regexp.(*bitState).reset at 13.61 MB over five reads under -race, 0.07 MB
without. The bound is now skipped under raceOn, as its sibling at
parse_memo_test.go:71 already is for workDirIn's regexps.

Run at origin/main 833f51adc + this change, on mote:
- go test -race -run TestChatAndMessagesFacts -count=3 ./internal/gateway/:
  ok, 3 of 3. Before the change: FAIL 3 of 3 (chat 3851848 bytes against
  a parse's 4524408).
- go test -run TestChatAndMessagesFacts -count=3 ./internal/gateway/: ok
  (facts 980072 bytes against 4503336, the bound unchanged).
- go vet ./internal/gateway/: ok. gofmt -l: none.
Not run: the full gateway race shards; CI runs them.
2026-10-28 08:23:31 +08:00
yetone 9a888323a2 gateway: a Chat or Messages request is read for its folder and turn without its history (#3472)
A plugin's request is read for its work folder on every try
(tellWorkPlace -> readFacts -> parseIn(proto, body, true)). Only the
Responses reader had a facts mode (#3394); Chat and Messages parsed the
whole conversation, copying every assistant turn, tool call and tool
output, to learn a folder and whether the last turn was a tool's answer.
On a long conversation that history was most of what each try allocated.

readChat and readAnthropic now take facts, as readResponses does: they
decode with decodeJSONView, keep an assistant message as its role alone
and a tool result as its kind and call id, and keep the system and user
text that workPlace reads. parseChat and parseAnthropicRequest are
readChat/readAnthropic with facts off, unchanged.

Numbers (1.3 MB body, least of 3 runs, TotalAlloc):
  Chat facts 980 KB, whole parse 4.5 MB
  Messages facts 1.08 MB, whole parse 5.3 MB
Through the gateway on the plugin path (probe, 60 requests of ~938 KB,
counting the test client and fake vendor), per request:
  /v1/chat/completions 24.4 MB -> 21.2 MB
  /v1/messages         39.8 MB -> 33.7 MB
Retained heap is unchanged (~15-19 MB); this is churn, which is what the
reporter's heapSys grows from.

Tests:
- TestChatAndMessagesFactsAsTheWholeParse: facts give the same folder,
  turn and tool-answer facts as the whole parse, over the real fixtures
  (workdir/pi-1.1.0, claude-code-2.1.296, context/claude-code, the
  upstream_bodies chat/anthropic families), a large conversation and
  hand cases (model and tool both spoke; answered then a tool; answered
  then nothing; a tool's result alone).
- TestChatAndMessagesFactsLeaveTheHistory: facts allocate under a third
  of the whole parse.
- TestReadersOfAHeldBodyParseItOnce's last check now counts readFacts
  calls through the factsParsed hook instead of allocation, which the
  cheaper facts no longer show.
Broken by hand, each red then green: dropping the assistant message in
Chat facts; keeping only role-less messages in Messages facts; facts off
in parseIn (the allocation test); a held-facts lookup matching any entry.

Run at this head (base origin/main 1bb8143b3):
- go test ./internal/gateway -skip '^TestZZ' -count=1 -timeout 45m: ok
  (401s)
- go test ./internal/plugin ./internal/provider (provider with
  -skip TestUnreadLoginsWriteErrorsKeepCredentials, #3632): ok, run
  with the next commit in the tree
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui for
  internal/gateway, internal/plugin, internal/provider: clean
- Real accounts in a sandbox HOME (only the deepseek provider and the
  Command Code plugin copied): /v1/messages and /v1/chat/completions
  multi-turn requests with a working-directory line, a tool call and a
  tool result, to deepseek/deepseek-flash and
  commandcode-plan/claude-haiku-5-5: all 200, answers right (the plugin
  quoted the tool's "file a: 你好" back exactly).
Not tried: a real Claude Code or Codex session against this build.

Rebased onto origin/main 6e6aba51c (a providers key change) and run again at the pushed head: go build -tags nogui ./... ok; go test ./internal/provider (same skip) ok 115s, ./internal/plugin ok 110s, ./internal/gateway -run 'Facts|Parse|Plugin|WorkPlace|Fetch' ok 50s. The full gateway run above was at 1bb8143b3.
2026-10-26 21:04:48 +08:00
yetone d088030315 gateway: a long Codex request's facts, tier and metadata skip the walks over its conversation (#3394)
A Codex Responses request carries the whole conversation, and the gateway
walked it several times per request for what it reads at the top level:

- requestSessionMetadata searched the body for "client_metadata", which
  Codex sends last, before asking topMember. A long body now asks the
  memoized split straight away.
- bodyTier asked gjson for speed and service_tier, walking the body each
  time; a long body now asks topMember too (topValue).
- userWords ran its reminder and "## My request" regexes on every text;
  each now runs only when its marker is present.
- readFacts (turn count, work folder) ran the full parse. It now parses
  in facts mode (readResponses): views over the raw bytes, no parts for
  assistant replies, and tool outputs as bare ToolResult parts, which is
  all turnIn and workPlace read.

Bench (a fake ChatGPT backend, ~2MB Codex body, client_metadata last),
per request: 31.7ms, 24.2MB, 71.2k allocs on origin/main; 19.2-21.4ms,
17.2MB, 59.6k allocs with these and the provider commit after it.

Real account: a sandbox HOME with only the deepseek provider, 310KB
Responses bodies to deepseek/deepseek-flash through `magpie serve`:
base 15ms process CPU per request (19/20 ok), new 13ms (20/20, then
8/8 again).

TestResponsesFactsAsTheWholeParse compares readFacts with the full
parse's turnIn and workPlace over constructed bodies and five real Codex
fixtures. Broken by hand twice (user parts skipped; ToolResult turned
into Text): red both times. tier_record_test adds four long-body cases
(tier after the conversation, a nested one ignored, Anthropic speed
fast, a Chat numeric tier).

Run at this head (720e491d2 + these two commits): gofmt clean, go build,
GOOS=linux and GOOS=windows go vet -tags nogui on gateway and provider;
go test -run 'Facts|Tier|Session|Responses|Metadata|UserWords|Prompt|Memo'
./internal/gateway ok. The full gateway and provider packages are
running and are reported on #3394.
2026-10-26 16:13:27 +08:00
yetone 41147956d2 gateway: a conversation's turn count and folder come from one parse of it, not one each (yg6888 on X: ~1 GB)
turnOf (affinity), dirOf (the trace's folder) and tellWorkPlace (a
plugin's work-place headers) each parsed the whole conversation for a
number and a folder. A parse decodes every message and tool output into
strings of its own, so a long conversation was decoded twice over on
every request for them. In the 60-min soak before (dee5c9d75, 4746
requests), parse was 26.4 GB of 106.7 GB allocated: turnOf 11.5 GB and
dirOf 11.4 GB.

factsOf parses once for the turn count, the folder, and whether the
text says it is a git repository (bodyFacts). For a body being served
(holdBody), it keeps those facts, a few words, while the body is held.
The pause-by-folder check reads the same facts.

It keeps the facts, not the parse. A first version kept the *Request
weakly. In the served benchmark the weak pointer was already collected
between turnOf and dirOf on 1 read in 6, and under real load the
collector runs more often. Keeping the parse strongly would keep a copy
of the conversation for the whole request. The facts are kept only
while their body is listed whole; the entry goes when the body is
released, so a body's memory given to another never reads as this one.
A body not held, or read in another protocol, is parsed for its own.

BenchmarkLargeRequest, ~2 MB conversation, B/op before → after:
  anthropic        53.81 → 45.84 MB
  anthropic-group  65.26 → 57.30 MB
  chat             43.27 → 36.61 MB
  responses        53.81 → 46.04 MB
6.7 to 8.0 MB less per request, about 3.5× the body.

Tests (parse_memo_test.go):
- TestReadersOfAHeldBodyParseItOnce: what the three readers say equals
  a parse of their own. Together they allocate at most 1.5× one parse,
  and readers after the first allocate almost nothing. A Chat read of
  the same bytes is its own, and a copy not held is parsed again.
- TestHeldFactsGoWithTheirBody: the entry goes when the request ends,
  a released body is not listed again, and a prefix of a held body is
  not the body.
Broken by hand:
- factsOf always parsing: red, "turnOf, dirOf and tellWorkPlace
  allocate 446064 bytes on a held body, a parse 135096", and "a held
  body's facts were not kept".
- The delete at release removed: red, "a body's facts are still listed
  after its request ended".
TestPauseByFolder, TestRuleByFolder, the WorkDir and WorkPlace tests and
the affinity and turn tests pass, with -race too.
Under -race the readers' byte bound is skipped (raceOn, as
request_zstd_pool_test does): sync.Pool drops a quarter of what is put
in it there, and workDirIn's regexps take their ~37 KB matchers from
one. Five tries read 1.3x to 2.1x a parse, while the parse stayed at
135304 bytes; -race -count=3 went red at 1.57x before the skip.

Run (darwin arm64, go1.26.8, sandbox HOME, load 40 to 90 from other
sessions' suites):
- go test ./... -count=1 -timeout 45m on the series' final tree at
  ba319a758: exit 0, all 68 packages ok (gateway 1498s, agent 1633s).
- After the rebase onto b855cfee9 (two redact commits): go test
  ./internal/redact ./internal/gateway -count=1 -timeout 45m, both ok
  (gateway 917s).
- go test -race -count=3 on the Held, ReadersOfAHeldBody, RecentRing,
  RequestBody, CaptureRequest, folder, WorkPlace, WorkDir and Turn
  tests: ok, after
  TestReadersOfAHeldBodyParseItOnce's byte bound was skipped under
  -race (the third commit says why); that test and
  TestHeldFactsGoWithTheirBody then passed -count=30 with and without
  -race.
- Each commit alone: go vet ./internal/gateway and gofmt -l clean.
- Final tree: gofmt -l clean; go vet ./... for darwin, GOOS=windows and
  GOOS=linux -tags nogui, all exit 0.
Not tried: a second 60-minute soak after the series (the one before is
the "before" here; the "after" numbers are the benchmark's), the GUI
suite (nothing in it changed), Windows and Linux test runs (vet only).
2026-10-21 19:17:09 +08:00