diff --git a/packages/devin/gzip-request.test.mjs b/packages/devin/gzip-request.test.mjs new file mode 100644 index 0000000..0c7cfee --- /dev/null +++ b/packages/devin/gzip-request.test.mjs @@ -0,0 +1,60 @@ +// Devin's nginx turns away a request body past 10 MiB on the wire (413 +// Request Entity Too Large), so a long turn goes gzipped, a short one as it +// was, at the size the devin CLI gzips from (8,083 bytes plain, 8,333 +// gzipped, captured from 3000.11.92). +import { expect, test } from "bun:test" +import { gunzipSync } from "node:zlib" +import { _internal } from "./index.mjs" + +const unframe = (b) => ({ flags: b[0], data: b.subarray(5, 5 + new DataView(b.buffer, b.byteOffset).getUint32(1)) }) + +test("a short message goes as it is", () => { + const msg = new TextEncoder().encode("hi") + const { flags, data } = unframe(_internal.frame(msg)) + expect(flags).toBe(0) + expect([...data]).toEqual([...msg]) +}) + +test("the CLI's sizes: 8,083 bytes go as they are, 8,333 gzipped", () => { + expect(unframe(_internal.frame(new Uint8Array(8083))).flags).toBe(0) + expect(unframe(_internal.frame(new Uint8Array(8333))).flags).toBe(1) +}) + +test("a long message goes gzipped, and inflates to itself", () => { + const msg = new TextEncoder().encode("x".repeat(12 << 20)) + const out = _internal.frame(msg) + const { flags, data } = unframe(out) + expect(flags).toBe(1) + expect(out.length).toBeLessThan(1 << 20) + expect(Buffer.from(gunzipSync(data)).equals(Buffer.from(msg))).toBe(true) +}) + +const sent = async (content) => { + let seen + const was = globalThis.fetch + globalThis.fetch = async (url, init) => { + seen = init + return new Response("{}", { status: 400 }) + } + try { + await _internal.complete({ key: "k", server: "https://devin.test", families: [] }, { model: "swe-2", messages: [{ role: "user", content }] }) + } finally { + globalThis.fetch = was + } + return seen +} + +test("a long chat request is gzipped and says so", async () => { + const seen = await sent("y".repeat(200 << 10)) + expect(seen.headers["Connect-Content-Encoding"]).toBe("gzip") + expect(unframe(seen.body).flags).toBe(1) + // the CLI asks for no encoding of the reply; Devin gzips it anyway + expect(seen.headers["Connect-Accept-Encoding"]).toBeUndefined() +}) + +test("a short chat request names no encoding, as the CLI's doesn't", async () => { + const seen = await sent("hi") + expect(unframe(seen.body).flags).toBe(0) + expect(seen.headers["Connect-Content-Encoding"]).toBeUndefined() + expect(seen.headers["Connect-Accept-Encoding"]).toBeUndefined() +}) diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index a5a494d..187a292 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -8,7 +8,7 @@ import { Buffer } from "node:buffer" import { createHash, randomBytes, randomUUID } from "node:crypto" import { createServer, STATUS_CODES } from "node:http" -import { gunzipSync } from "node:zlib" +import { gunzipSync, gzipSync } from "node:zlib" import { spawn } from "node:child_process" import { existsSync, readFileSync, realpathSync, statSync } from "node:fs" import { mkdir, readFile, writeFile } from "node:fs/promises" @@ -1065,11 +1065,20 @@ function fields(b) { return out } -// frame wraps a message as the one frame of a Connect stream. +// frame wraps a message as the one frame of a Connect stream. A message +// past GZIP_AT goes gzipped (flag 1, with Connect-Content-Encoding: gzip), +// as the devin CLI sends it: Devin's nginx turns away a body past 10 MiB on +// the wire (413). The CLI (3000.11.92) gzips a GetChatMessage of 8,333 +// bytes and sends one of 8,083 as it is, and names the encoding only on a +// gzipped request; it sends no Connect-Accept-Encoding. +const GZIP_AT = 8 << 10 function frame(msg) { - const out = new Uint8Array(5 + msg.length) - new DataView(out.buffer).setUint32(1, msg.length) - out.set(msg, 5) + const zip = msg.length > GZIP_AT + const body = zip ? gzipSync(msg) : msg + const out = new Uint8Array(5 + body.length) + out[0] = zip ? 1 : 0 + new DataView(out.buffer).setUint32(1, body.length) + out.set(body, 5) return out } @@ -1628,12 +1637,13 @@ const errorResponse = ({ status, message }) => // complete answers a chat completion through GetChatMessage. async function complete({ key, server, families }, chat, signal) { const uid = variantFor(families, chat.model, effortOf(chat.reasoning_effort)) + const body = frame(build(chat, uid, key)) let res try { res = await fetch(server + CHAT_RPC, { method: "POST", - headers: { "Content-Type": "application/connect+proto", "Connect-Protocol-Version": "1", Authorization: "Basic " + key }, - body: frame(build(chat, uid, key)), + headers: { "Content-Type": "application/connect+proto", "Connect-Protocol-Version": "1", ...(body[0] & 1 ? { "Connect-Content-Encoding": "gzip" } : {}), Authorization: "Basic " + key }, + body, signal, }) } catch (e) {