fix(upstream): alias the client read tool to "read" on the legacy /zen/v1 lane

The legacy /zen/v1 free-tier gate needs only a single tool named "read":
probing showed [read] alone passes while bash/shell/edit/glob/grep alone
(or any combination without read) return 403 FreeTierError, and neither
the description nor the parameter schema is validated. The v1.3.7-style
pre-injection of the other core tools was therefore unnecessary traffic
and repeated client schemas on the wire.

Legacy lane:
- Drop the bash/edit/glob/grep pre-injection entirely.
- If the client already declares an exact "read" tool, the body is left
  untouched.
- Otherwise, a read-related client tool (case-insensitive match such as
  "reAd", "read_file", "ReadFile", "file_read", "view_file") is renamed
  in place to "read" with its client description and schema preserved,
  and the matching tool_choice plus any historical tool call in the
  request are rewritten so the outbound body stays self-consistent.
- If no read-related tool exists, a single "read" stub is injected whose
  description marks it deprecated, disabled, and not to be called.

Round trip:
- Add protocol.RenameResponseTool for chat tool_calls, Responses
  function_call items, and Anthropic tool_use blocks.
- Add protocol.RenameStreamReader, a pull-based SSE rewriter (no goroutine
  or io.Pipe, so a disconnected client cannot strand a writer) that
  restores the client's original name in streaming replies. Chat deltas
  may split or repeat a name fragment, so "read" prefixes are held until
  the name resolves, and a continuation past "read" is passed through
  verbatim.
- Carry the alias through the new telemetry.RequestMeta.ToolAlias field
  and shapeKeyBody's new return value; the /inference lane and non-legacy
  upstreams are untouched.
This commit is contained in:
Jason Xu committed 2026-10-17 07:13:19 +08:00
1 parent 2319941a6a
commit a9470df4fc
7 files changed
+578 -94

No files matched your search

+7 -2
View File
@@ -36,10 +36,15 @@ type RequestMeta struct {
// upstream account that served the request.
ServerKeyID string
// Shaped marks a key-tier free-model request whose wire body was
// normalized to agent shape (stream + core tools) like the anonymous
// normalized to agent shape (stream + required tools) like the anonymous
// lane. Non-streaming responses for shaped requests arrive as SSE
// and must be collapsed like anonymous ones.
Shaped bool
Shaped bool
// ToolAlias holds the client's original read-related tool name (e.g.
// "reAd" or "read_file") when the legacy /zen/v1 shaper renamed it to
// "read" on the outbound wire. When non-empty, "read" tool calls in the
// upstream response are rewritten back to ToolAlias before returning.
ToolAlias string
Usage protocol.Usage
UsageReported bool
AttemptOutcome string