Skip to content

perf: shape Zen bodies on the decoded map, marshal once per tier - #79

Closed
SK-DEV-AI wants to merge 1 commit into
jasonxu114514:mainfrom
SK-DEV-AI:pr/single-marshal-shaping
Closed

SK-DEV-AI wants to merge 1 commit into
jasonxu114514:mainfrom
SK-DEV-AI:pr/single-marshal-shaping

Conversation

@SK-DEV-AI

Copy link
Copy Markdown
Contributor

Every request pays 2-3 full JSON serde passes: prepareRouteBodies marshals per tier, then the anonymous lane unmarshals the Zen body back into a map, forces stream+usage+tools, and remarshals. The key lane repeats the unmarshal for its shaped comparison.

This shapes the Zen payload on the decoded map before the single per-tier marshal, so the anonymous lane sends bodies[zen] verbatim. prepareAnonymousBody stays for probes and the Go-tier key path as a thin wrapper over the same map function, so one source of truth. shapeKeyBody on Zen reports shaped structurally (Zen bodies always arrive pre-shaped; collapse only serves non-streaming clients whose payloads always needed forcing, so the old compare was always true everywhere it is read). Go and System One paths unchanged.

Validation: go vet clean, go build -trimpath clean, gofmt clean. Verified byte-identical output on stream/non-stream x tools/no-tools payloads plus System One passthrough against the old path.

@jasonxu114514

Copy link
Copy Markdown
Owner

Thanks. Cutting a serde pass is welcome, but this regresses paid Zen-key requests:

  • The new shaping block in prepareRouteBodies checks only tier == config.TierZen, not whether the model is free. Every paid Zen request would get stream: true and stream_options forced, plus the agent tool set (shell/edit/glob/grep/read) injected. That changes what the model can do. On main, paid bodies are deliberately left alone (see the comment on shapeKeyBody).
  • shapeKeyBody still reports shaped=false for paid models, so meta.Shaped stays false and the stream collapse is skipped. A non-streaming paid client would then receive raw SSE where it expects JSON.

To move forward:

  1. Gate the block on g.catalog.IsFreeModel(route.ID), the same check shapeKeyBody uses.
  2. Rebase on main. The comment block around doAnonymousUpstream was rewritten in 0204ca1, and perf: typed CloneMap deep copy without marshal round-trip #78 (typed CloneMap) and fix: bound each upstream attempt by the full attempt deadline #64 are about to land on main.
  3. Keep prepareAnonymousBody for probe.go.

With the gate in place I'd be glad to look again. Please include the byte-identical comparison for a paid model, both streaming and non-streaming.

PrepareRouteBodies applies the anonymous agent-shape (stream + usage +
core tools) to the decoded Zen payload before the single per-tier
marshal; the anonymous lane sends bodies[zen] verbatim, deleting its
unmarshal+remarshal pass. prepareAnonymousBody stays for probes and the
Go-tier key path as a thin wrapper over the same map function.

Paid Zen bodies are never shaped: the block is gated on
IsFreeModel(route.ID), the same check shapeKeyBody uses, so paid
requests keep their exact client shape and the Shaped collapse flag
stays consistent. Go and System One paths unchanged.
@SK-DEV-AI
SK-DEV-AI force-pushed the pr/single-marshal-shaping branch from cb69098 to 6fcbe8f Compare October 15, 2026 13:23
@SK-DEV-AI

Copy link
Copy Markdown
Contributor Author

Revised per feedback:

  1. The shaping block is now gated on g.catalog.IsFreeModel(route.ID) — paid Zen bodies keep their exact client shape, and the Shaped collapse flag stays consistent (shapeKeyBody already returns false for paid).
  2. Rebased on current main (includes perf: typed CloneMap deep copy without marshal round-trip #78, fix: bound each upstream attempt by the full attempt deadline #64, and the 0204ca1 comment rewrite).
  3. prepareAnonymousBody stays for probe.go and the Go-tier key path, now a thin wrapper over the same map-level function.

Paid-model check: with the gate, a paid Zen payload (streaming and non-streaming) passes through byte-identical with no tools injected — verified locally. Free-model output is byte-identical to the old path on stream/non-stream x tools/no-tools payloads plus System One passthrough.

SK-DEV-AI added a commit to SK-DEV-AI/opencode2api that referenced this pull request Oct 15, 2026
…114514#79 feedback)

Paid Zen bodies must be left alone; shaping is a free-tier requirement.
Without the IsFreeModel gate a paid request would get stream/tools
forced while shapeKeyBody reports unshaped, so a non-streaming paid
client would receive raw SSE instead of JSON.
SK-DEV-AI added a commit to SK-DEV-AI/opencode2api that referenced this pull request Oct 15, 2026
@SK-DEV-AI

Copy link
Copy Markdown
Contributor Author

Thanks — the paid-model regression was a real miss on my part, and the revision (gating on IsFreeModel) is what shipped in my fork.

One update worth having: v1.4.1 (076451f, 2319941, a9470df) makes this PR moot on the anonymous path. Upstream now shapes per lane inside doAnonymousUpstream via prepareAnonymousBody(body, protocol, legacy) — the legacy /zen/v1 lane gets the single-read stub, /inference gets the full tool set — and shapeAnonymousPayload / shapeKeyBody-on-the-declared-map no longer run in prepareRouteBodies. Key tiers still marshal per tier as before, so the double-encode on the key path is gone upstream too.

I don't think there's a perf argument left that upstream's per-lane shaping doesn't already cover, so feel free to close this rather than reviewing it further. If you'd rather keep the map-level helper for the key tiers, say so and I'll rebase it onto v1.4.1 and re-run the paid byte-equivalence proof — otherwise I'll close it from my side.

For the record on what I'd keep from this thread either way: the IsFreeModel gate is now load-bearing in my fork too (docs/FORK.md records it under the v1.4.0 merge).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants