Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
plugin: a reply's chunk frame is read by its fixed shape, not through encoding/json
yetone5774c790bdd6
diff --git a/internal/plugin/chunk_frame_test.go b/internal/plugin/chunk_frame_test.gonew file mode 100644index 000000000..e73e50e0b--- /dev/null+++ b/internal/plugin/chunk_frame_test.go@@ -0,0 +1,66 @@+package plugin++import (+ "encoding/base64"+ "encoding/json"+ "strings"+ "testing"+)++// TestChunkFrameReadsWithoutJSON: a chunk frame as host.js writes it+// (JSON.stringify({id, event: "chunk", data}), which chunkLine joins by+// hand) is read without encoding/json, and reads the same as through it.+// Any line of another shape goes through encoding/json as before.+func TestChunkFrameReadsWithoutJSON(t *testing.T) {+ raw := make([]byte, 70<<10)+ for i := range raw {+ raw[i] = byte(i * 31)+ }+ for _, tc := range []struct {+ id int64+ data string+ }{+ {1, base64.StdEncoding.EncodeToString(raw)},+ {987654321, base64.StdEncoding.EncodeToString([]byte("data: {}\n\n"))},+ {42, "AA=="},+ } {+ b, err := json.Marshal(struct {+ ID int64 `json:"id"`+ Event string `json:"event"`+ Data string `json:"data"`+ }{tc.id, "chunk", tc.data})+ if err != nil {+ t.Fatal(err)+ }+ for _, line := range []string{string(b) + "\n", string(b), string(b) + "\r\n"} {+ got, ok := chunkFrame([]byte(line))+ if !ok {+ t.Fatalf("a chunk frame as host.js writes it (%.60q…) went through encoding/json", line)+ }+ var want message+ if err := json.Unmarshal([]byte(line), &want); err != nil {+ t.Fatal(err)+ }+ if got.ID != want.ID || got.Event != want.Event || got.Data != want.Data {+ t.Fatalf("read as id %d %q %d bytes, encoding/json reads id %d %q %d bytes", got.ID, got.Event, len(got.Data), want.ID, want.Event, len(want.Data))+ }+ }+ }+ for _, line := range []string{+ `{"id":1,"event":"head","status":200,"headers":{}}`,+ `{"id":1,"result":null}`,+ `{"id":1,"event":"chunk","data":"QU` + "\\" + `/A"}`, // an escape: JSON reads it otherwise+ `{"id":1,"event":"chunk","data":"QUFB\"}`,+ `{"id":-1,"event":"chunk","data":"QUFB"}`,+ `{"id":1e3,"event":"chunk","data":"QUFB"}`,+ `{"id":1,"event":"chunk","data":"QUFB","x":"y"}`,+ `{"id":1,"event":"chunk","data":"QUFB"} `,+ `{"id":1,"event":"chunk","data":"` + strings.Repeat("A", 8) + `"}x`,+ `{"id":12345678901234567890,"event":"chunk","data":"QUFB"}`,+ `{"event":"chunk","id":1,"data":"QUFB"}`,+ } {+ if m, ok := chunkFrame([]byte(line)); ok {+ t.Fatalf("%s read as a chunk frame %+v, want it left to encoding/json", line, m)+ }+ }+}diff --git a/internal/plugin/host.go b/internal/plugin/host.goindex b1843548f..e037d29d5 100644--- a/internal/plugin/host.go+++ b/internal/plugin/host.go@@ -2,6 +2,7 @@ package pluginimport ("bufio"+ "bytes""context""crypto/sha256"_ "embed"@@ -1033,8 +1034,9 @@ func (h *host) read(out io.Reader) {for {line, err := readLine(r, h.lineLimit())if len(line) > 0 {- var m message- if json.Unmarshal(line, &m) == nil {+ if m, ok := chunkFrame(line); ok {+ h.dispatch(m)+ } else if json.Unmarshal(line, &m) == nil {h.dispatch(m)}}@@ -1057,6 +1059,45 @@ func (h *host) read(out io.Reader) {h.fail(fmt.Errorf("the plugin host quit: %v", err))}+var (+ chunkHead = []byte(`{"id":`)+ chunkMid = []byte(`,"event":"chunk","data":"`)+)++// chunkFrame is the chunk frame in line when it has exactly the shape+// host.js's chunkLine writes, {"id":N,"event":"chunk","data":"…"}, with+// nothing in the data JSON would escape: the reply's bytes come in these,+// and encoding/json scanned every base64 byte of each, several times what+// decoding them costs. Any other line is not one, and is unmarshalled; push+// checks the data is base64 of a frame's size either way.+func chunkFrame(line []byte) (message, bool) {+ line = bytes.TrimSuffix(bytes.TrimSuffix(line, []byte("\n")), []byte("\r"))+ rest, ok := bytes.CutPrefix(line, chunkHead)+ if !ok {+ return message{}, false+ }+ i := bytes.IndexByte(rest, ',')+ if i <= 0 || i > 18 {+ return message{}, false+ }+ var id int64+ for _, c := range rest[:i] {+ if c < '0' || c > '9' {+ return message{}, false+ }+ id = id*10 + int64(c-'0')+ }+ data, ok := bytes.CutPrefix(rest[i:], chunkMid)+ if !ok {+ return message{}, false+ }+ data, ok = bytes.CutSuffix(data, []byte(`"}`))+ if !ok || bytes.IndexByte(data, '"') >= 0 || bytes.IndexByte(data, '\\') >= 0 {+ return message{}, false+ }+ return message{ID: id, Event: "chunk", Data: string(data)}, true+}+// readLine reads one '\n'-terminated line, at most max bytes, so a child that// never ends a line cannot make the host allocate without bound. It is// errLineTooLong once max is passed.