diff --git a/packages/zed/context-edits.test.mjs b/packages/zed/context-edits.test.mjs new file mode 100644 index 0000000..c3943d4 --- /dev/null +++ b/packages/zed/context-edits.test.mjs @@ -0,0 +1,81 @@ +// Zed's cloud parses an Anthropic request's context_management into its own +// enum (zed crates/anthropic), which has only compact_20260112, and needs +// edits. Claude Code sends clear_thinking_20251015, which Zed answered 400 +// "unknown variant `clear_thinking_20251015`, expected `compact_20260112`" +// (nllay, #636). The plugin passes compaction on and leaves the rest out. +import { afterEach, expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const real = globalThis.fetch +afterEach(() => { + globalThis.fetch = real + _internal.tokens.clear() +}) + +const s = { userId: "4242", access: "plain-access", systemId: "", org: "org-me", plan: "zed_pro", who: "octo" } + +// sent is the provider_request the plugin hands Zed for an Anthropic body. +async function sent(body) { + let got + globalThis.fetch = async (url, init) => { + const path = new URL(String(url)).pathname + if (path === "/client/llm_tokens") return Response.json({ token: "llm-token" }) + if (path === "/completions") { + got = JSON.parse(init.body) + const ev = (e) => JSON.stringify({ event: e }) + return new Response( + [ + ev({ type: "message_start", message: { id: "m1", type: "message", role: "assistant", model: "claude-sonnet-5", content: [], usage: { input_tokens: 1, output_tokens: 0 } } }), + ev({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } }), + ev({ type: "message_stop" }), + JSON.stringify({ status: "stream_ended" }), + ].join("\n") + "\n", + { headers: { "x-zed-server-supports-status-messages": "true" } }, + ) + } + return new Response("", { status: 404 }) + } + const res = await _internal.complete(s, "https://cloud.zed.dev/v1/messages", { method: "POST", body: JSON.stringify(body) }) + expect(res.status).toBe(200) + await res.text() + return got.provider_request +} + +const body = (context_management) => ({ + model: "claude-sonnet-5", + stream: true, + max_tokens: 100, + messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }], + context_management, +}) + +const CLEAR_THINKING = { type: "clear_thinking_20251015", keep: "all" } +const CLEAR_TOOLS = { type: "clear_tool_uses_20250919", trigger: { type: "input_tokens", value: 30000 } } +const COMPACT = { type: "compact_20260112", trigger: { type: "input_tokens", value: 100000 } } + +test("Claude Code's clear-thinking edit alone leaves context_management out", async () => { + expect("context_management" in (await sent(body({ edits: [CLEAR_THINKING] })))).toBe(false) +}) + +test("an edit Zed has no variant for is left out too", async () => { + expect("context_management" in (await sent(body({ edits: [CLEAR_TOOLS] })))).toBe(false) + expect("context_management" in (await sent(body({ edits: [CLEAR_THINKING, CLEAR_TOOLS] })))).toBe(false) +}) + +test("compaction goes on, alone, as it was written", async () => { + expect((await sent(body({ edits: [CLEAR_THINKING, COMPACT, CLEAR_TOOLS] }))).context_management).toEqual({ edits: [COMPACT] }) + expect((await sent(body({ edits: [COMPACT] }))).context_management).toEqual({ edits: [COMPACT] }) +}) + +test("edits that aren't a list of edits are left out, not thrown on", async () => { + expect((await sent(body({ edits: [null, CLEAR_THINKING, COMPACT] }))).context_management).toEqual({ edits: [COMPACT] }) + for (const cm of [{ edits: "x" }, { edits: [] }, {}, null, "x"]) { + expect("context_management" in (await sent(body(cm)))).toBe(false) + } +}) + +test("a request without context_management is sent without one", async () => { + const b = body() + delete b.context_management + expect("context_management" in (await sent(b))).toBe(false) +}) diff --git a/packages/zed/index.mjs b/packages/zed/index.mjs index bf7af00..ddaef50 100644 --- a/packages/zed/index.mjs +++ b/packages/zed/index.mjs @@ -653,6 +653,18 @@ function providerRequest(wire, req, model) { switch (wire) { case "anthropic": { const stream = !!req.stream + // Zed's cloud parses context_management into its own enum (zed + // crates/anthropic), which has only compact_20260112, and edits is + // required: any other edit (Claude Code's clear_thinking_20251015, + // clear_tool_uses_20250919) is answered 400 "unknown variant …, + // expected `compact_20260112`" (nllay, #636). Only compaction goes on, + // and without one the field is left out. + if ("context_management" in req) { + const cm = req.context_management + const edits = Array.isArray(cm?.edits) ? cm.edits.filter((e) => e?.type === "compact_20260112") : [] + if (edits.length) req.context_management = { ...cm, edits } + else delete req.context_management + } // Zed's Anthropic request has no stream field: the cloud always streams delete req.stream // Zed's cloud refuses a tool result without is_error ("missing field diff --git a/packages/zed/package.json b/packages/zed/package.json index 289b03c..7c261bd 100644 --- a/packages/zed/package.json +++ b/packages/zed/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zed-auth", - "version": "0.1.14", + "version": "0.1.15", "description": "Sign in to a Zed subscription (Zed Pro, trial, student, business) from OpenCode or magpie", "type": "module", "main": "./index.mjs",