zed 0.1.15: only compact_20260112 reaches Zed in context_management (nllay, #636)

Zed's cloud parses an Anthropic request's context_management into its own
enum (zed crates/anthropic), which has one variant, compact_20260112, and
needs edits. Claude Code's clear_thinking_20251015 was answered 400
"unknown variant ..., expected `compact_20260112`". nllay's patch dropped
that edit. The follow-up keeps only compaction edits, since any other
type (clear_tool_uses_20250919) fails the same way. context_management is
left out when no edit remains, and a non-list edits or a null entry no
longer throws. zed 0.1.15.

Run at the reviewed head 79aab88 on mote (Bun 1.4.2), packages/zed:
bun test, 66 pass 0 fail. The new context-edits.test.mjs is red without
the change: 2 pass 3 fail with 74caff5's index.mjs, 1 pass 4 fail with
main's.
Not tried: a real Zed account (nllay reports a 200 through magpie for the
clear-thinking case).
This commit is contained in:
nllay
2026-11-02 20:10:55 +08:00
committed by GitHub
parent 32d58dd511
commit 48d0cecaf8
3 changed files with 94 additions and 1 deletions
+81
View File
@@ -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)
})
+12
View File
@@ -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
+1 -1
View File
@@ -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",