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).
82 lines
3.6 KiB
JavaScript
82 lines
3.6 KiB
JavaScript
// 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)
|
|
})
|