From e39f4f4dc076daaa77f3e73657c362a474fea0ba Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 03:43:41 +0800 Subject: [PATCH] =?UTF-8?q?devin=200.1.4:=20errors=20no=20longer=20start?= =?UTF-8?q?=20with=20"Devin:=20",=20which=20magpie=20adds=20(it=20read=20"?= =?UTF-8?q?Devin:=20Devin:=20usage=20limit=20reached:=20=E2=80=A6"),=20a?= =?UTF-8?q?=20stream=20error=20before=20anything=20included;=20an=20error?= =?UTF-8?q?=20Devin=20gives=20no=20words=20for=20is=20worded=20as=20Go's?= =?UTF-8?q?=20status=20text=20("Payment=20Required")=20as=20the=20built-in?= =?UTF-8?q?'s=20devinFailure=20does,=20where=20it=20read=20"HTTP=20402";?= =?UTF-8?q?=20an=20empty=20reply=20is=20the=20built-in's=20502=20"the=20re?= =?UTF-8?q?ply=20broke=20off:=20EOF".=20Statuses=20were=20already=20the=20?= =?UTF-8?q?built-in's=20(unauthenticated=20401,=20resource=5Fexhausted/quo?= =?UTF-8?q?ta/rate=20limit=20429,=20too=20long=20400,=20invalid=5Fargument?= =?UTF-8?q?=20400,=20unavailable=20503);=20errors.test.mjs=20checks=20them?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/devin/errors.test.mjs | 73 ++++++++++++++++++++++++++++++++++ packages/devin/index.mjs | 27 ++++++++----- packages/devin/package.json | 2 +- 3 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 packages/devin/errors.test.mjs diff --git a/packages/devin/errors.test.mjs b/packages/devin/errors.test.mjs new file mode 100644 index 0000000..2fc947d --- /dev/null +++ b/packages/devin/errors.test.mjs @@ -0,0 +1,73 @@ +// A request Devin fails is answered with the status and words magpie's +// built-in Devin (internal/gateway/devin.go, devinFailure) gave it, so the +// gateway moves on to the next account or member as it did. magpie puts +// "Devin: " before the message itself. +import { afterEach, expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const real = globalThis.fetch +afterEach(() => (globalThis.fetch = real)) + +const enc = new TextEncoder() +const end = (obj) => { + const b = enc.encode(JSON.stringify(obj)) + const out = new Uint8Array(5 + b.length) + out[0] = 2 + new DataView(out.buffer).setUint32(1, b.length) + out.set(b, 5) + return out +} +const join = (...parts) => { + const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0)) + let i = 0 + for (const p of parts) out.set(p, (i += p.length) - p.length) + return out +} + +const ask = async (reply, stream = false) => { + globalThis.fetch = async () => reply() + const res = await _internal.complete( + { key: "k", server: "https://devin.test", families: _internal.familiesOf(_internal.SNAPSHOT) }, + { model: "swe-2", stream, messages: [{ role: "user", content: "hi" }] }, + ) + return { status: res.status, body: await res.text() } +} +const errOf = (r) => JSON.parse(r.body).error.message + +test("an HTTP error that says nothing is worded as Go's status text", async () => { + const r = await ask(() => new Response("", { status: 402 })) + expect(r.status).toBe(402) + expect(errOf(r)).toBe("Payment Required") +}) + +test("a stream that ends on resource_exhausted before anything is a 429 without a second Devin:", async () => { + const r = await ask(() => new Response(end({ error: { code: "resource_exhausted", message: "daily quota used up" } })), true) + expect(r.status).toBe(429) + expect(errOf(r)).toBe("usage limit reached: daily quota used up") +}) + +test("a too-long conversation is the built-in's 400", async () => { + const r = await ask(() => new Response(end({ error: { code: "invalid_argument", message: "prompt is too long" } }))) + expect(r.status).toBe(400) + expect(errOf(r)).toBe("input is too long for the model's context: prompt is too long") +}) + +test("a refused key is the 401 magpie marks the account lapsed on", async () => { + const r = await ask(() => new Response(JSON.stringify({ code: "unauthenticated", message: "bad key" }), { status: 401 })) + expect(r.status).toBe(401) + expect(errOf(r)).toBe("bad key — sign in to Devin again") +}) + +test("an empty reply is the built-in's 502 of a broken-off one", async () => { + const r = await ask(() => new Response("")) + expect(r.status).toBe(502) + expect(errOf(r)).toBe("the reply broke off: EOF") +}) + +test("an error after a frame with nothing in it is streamed in the built-in's words", async () => { + const empty = new Uint8Array(5) // a message frame with no fields + const r = await ask(() => new Response(join(empty, end({ error: { code: "resource_exhausted", message: "rate limit" } }))), true) + expect(r.status).toBe(200) + const ev = r.body.split("\n\n").map((l) => l.replace(/^data: /, "")).find((l) => l.includes('"error"')) + expect(JSON.parse(ev).error.message).toBe("usage limit reached: rate limit") +}) diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index ab74044..a8d817d 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -6,7 +6,7 @@ // (internal/provider/devin*.go, internal/gateway/devin.go). import { createHash, randomBytes, randomUUID } from "node:crypto" -import { createServer } from "node:http" +import { createServer, STATUS_CODES } from "node:http" import { gunzipSync } from "node:zlib" import { spawn } from "node:child_process" import { existsSync, readFileSync, statSync } from "node:fs" @@ -902,6 +902,11 @@ function encodeMsg(m) { // ---- the reply ----------------------------------------------------------------------- +// statusText is a status as magpie's built-in words an error that says +// nothing (Go's http.StatusText). +const statusText = (s) => + ({ 413: "Request Entity Too Large", 414: "Request URI Too Long", 416: "Requested Range Not Satisfiable", 418: "I'm a teapot", 509: "" })[s] ?? STATUS_CODES[s] ?? "" + // failure is the status and message for a Connect error: {"code", // "message"}, or at a stream's end {"error": {"code", "message"}}. function failure(status, text) { @@ -913,7 +918,7 @@ function failure(status, text) { else if (e?.code) [code, msg] = [e.code, e.message ?? ""] msg ||= code } catch {} - msg ||= `HTTP ${status}` + msg ||= statusText(status) || `HTTP ${status}` const low = msg.toLowerCase() if (code === "unauthenticated") return { status: 401, message: msg + " — sign in to Devin again" } if (code === "resource_exhausted" || low.includes("quota") || low.includes("rate limit")) return { status: 429, message: "usage limit reached: " + msg } @@ -941,7 +946,7 @@ async function* events(it) { try { r = await it.next() } catch (e) { - yield { error: { status: 502, message: "Devin: the reply broke off: " + e.message } } + yield { error: { status: 502, message: "the reply broke off: " + e.message } } return } if (r.done) break @@ -949,7 +954,7 @@ async function* events(it) { if (f.end) { const e = failure(200, dec.decode(f.data)) if (e.status >= 300) { - yield { error: { status: e.status, message: "Devin: " + e.message } } + yield { error: e } return } break @@ -1018,11 +1023,11 @@ async function complete({ key, server, families }, chat, signal) { signal, }) } catch (e) { - return errorResponse({ status: 502, message: "Devin: " + e.message }) + return errorResponse({ status: 502, message: e.message }) } if (!res.ok) { const e = failure(res.status, (await res.text()).slice(0, 1 << 20)) - return errorResponse({ status: e.status, message: "Devin: " + e.message }) + return errorResponse(e) } // an error comes as the stream's end, before anything else: read that // far so it is answered with its own status @@ -1031,12 +1036,12 @@ async function complete({ key, server, families }, chat, signal) { try { first = await fr.next() } catch (e) { - return errorResponse({ status: 502, message: "Devin: the reply broke off: " + e.message }) + return errorResponse({ status: 502, message: "the reply broke off: " + e.message }) } - if (first.done) return errorResponse({ status: 502, message: "Devin: an empty reply" }) + if (first.done) return errorResponse({ status: 502, message: "the reply broke off: EOF" }) if (first.value.end) { const e = failure(200, dec.decode(first.value.data)) - return errorResponse(e.status < 300 ? { status: 502, message: "Devin: an empty reply" } : { status: e.status, message: "Devin: " + e.message }) + return errorResponse(e.status < 300 ? { status: 502, message: "an empty reply" } : e) } let replay = first const it = events({ @@ -1187,13 +1192,13 @@ export async function DevinAuthPlugin() { const now = await getAuth() if (now?.type !== "api" || !now.key) return errorResponse({ status: 401, message: "Devin isn't signed in" }) const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url - if (!/\/chat\/completions$/.test(new URL(url).pathname)) return errorResponse({ status: 404, message: "Devin: only chat completions are served" }) + if (!/\/chat\/completions$/.test(new URL(url).pathname)) return errorResponse({ status: 404, message: "only chat completions are served" }) let chat try { const b = init.body ?? (input instanceof Request ? await input.clone().text() : undefined) chat = JSON.parse(typeof b === "string" ? b : dec.decode(b)) } catch { - return errorResponse({ status: 400, message: "Devin: a request that isn't JSON" }) + return errorResponse({ status: 400, message: "a request that isn't JSON" }) } const { key, server } = await live(now) const families = await familiesFor(key, server) diff --git a/packages/devin/package.json b/packages/devin/package.json index 45eb95f..943f2f8 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.3", + "version": "0.1.4", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs",