e4e9eb0928becfa4e48484dde85ef5032671d582
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
63c91cae5f |
gateway: the body's key checks look for the needle's rarest byte
Each request ran ~45 bytes.Contains over its whole body to ask whether
a key is there at all ("developer", "compaction, "display", the
passthrough key...). Most needles begin with a quote, JSON's commonest
byte, so bytes.Index stopped at every quote, gave up on that and
compared at every byte: on a 2 MB body ~9 ms a request, 36% of the
gateway's CPU in BenchmarkLargeRequest's profile.
bodyHas/bodyIndex find the needle's rarest byte (a static rank of the
bytes JSON, English and code use most) with IndexByte and compare the
needle only there; a needle of common bytes only hands the rest to
strings.Index after 64 misses, so it is never slower than before, and
it allocates nothing (a []byte(sep) of a needle over 32 bytes did).
148 literal and constant needles in internal/gateway use it, and
mayHoldHEIC's "eXB" walk uses bodyIndex.
mayHoldOddBase64 walked every byte through a switch (~2 ms a 2 MB
body); a run it looks for ends a string, so it now looks back from each
quote with a class table and gives the same answer.
Run on mica (Apple M6) at this tree:
- BenchmarkBodyHas, 7 needles over a 2 MB body: 4.43 ms -> 0.38 ms,
0 allocs (was 1).
- BenchmarkLargeRequest -benchtime 200x, before -> after:
anthropic 31.2 -> 28.9 ms, anthropic-group 31.4 -> 27.8,
chat 35.7 -> 32.8, responses 39.8 -> 29.8.
- go vet ./internal/gateway; gofmt -l clean.
- go test ./internal/gateway -count 1 -timeout 45m: ok (858s).
- Broken by hand: bodyIndex's start off by one -> TestBodyIndexIsIndex
red (wrong index on 5 needles and the random bodies); the URL-safe
class never counted -> TestOddBase64LooksAsItWalked red. Both green
again restored.
TestBodyIndexIsIndex holds bodyIndex to bytes.Index on a large
conversation and 3000 random bodies of common and rare bytes;
TestOddBase64LooksAsItWalked holds the new check to the old byte walk
(kept in the test) on 20000 random bodies and the three protocols'
large conversations.
Real account: the built app (mote, sandbox HOME) sent a 6KB request to
DeepSeek on /v1/responses and /v1/messages, both 200 "pong". Not
tried: Codex (the sandbox's sign-in is stale, and renewing it there
would rotate the live refresh token).
|
||
|
|
72d6103e28 |
gateway: a Chat tool in MCP's or another API's shape goes upstream as a Chat function tool (#2622)
jinyue806 (#2622): ZCode sends its tools on /v1/chat/completions in MCP's shape, {"name","description","inputSchema"}, with no type and no function object. magpie relayed them as they came, and SenseNova turned every request with tools away ("field Tools[0].Type invalid, should be set"; a flat {"type":"function","name":…} got "function cannot be empty"). A missing type is invalid Chat Completions for every upstream, not SenseNova only, and the translated routes were broken too: parseChat read no name from such a tool, so Messages and Responses upstreams were offered a tool named "". shapeChatTools puts a tool with its name at the top (no type, or type "function" without a function object) into {"type":"function","function": {name, description, parameters, strict}}, parameters taken from parameters, inputSchema or input_schema; other fields stay beside it. It runs right after liftMessageTools at the same two choke points: handle, for every Chat body before routing (relay, schema fixers, redaction, plugins), and parseChat, for a body that never came through handle. A tool that has its function, a hosted tool (any other type) and one with no name are left alone, and a request with nothing to shape is relayed byte for byte. Verified: - TestChatToolInAnotherShapeIsShaped sends the reporter's curl body with each of the four tool shapes from the report through the Chat relay, Messages and Responses. With shapeChatTools made a no-op by hand all 12 subtests FAIL: relayed as {"name":"f","inputSchema":{…}} as it came, and translated as {"name":"",…}. TestShapeChatToolsLeavesChatShape (byte for byte on Chat-shaped/hosted/nameless tools; strict and cache_control kept) FAILs on the same break. Both pass restored. - gofmt -l: clean. go vet -tags nogui ./... for darwin, linux, windows: ok. - Full suite at |