From 07897c8c5895890aaa659e0083aa1e175a2da816 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 10 Oct 2026 17:55:54 +0800 Subject: [PATCH] factory 0.1.23: a streamed Claude Code request with context management is no longer refused (#71, Vigilans) Claude Code with the context-management beta sends context_management (here clear_thinking_20251015, keep all). Factory's streaming route answers it with 400 "context_management: Extra inputs are not permitted"; the same body with stream false, or without the field, gets 200 (the reporter's three cases). A streamed /messages request now goes without the field; an unstreamed one keeps it. The server then clears nothing, and Claude Code's own compaction still runs. The version bump also releases the previous commit's #72 fix. Run: HOME= bun test in packages/factory, 119 pass (1 new from the reporter's body; the existing all-fields test now expects the field gone on its streamed request). With the deletion disabled by hand both go red. Not sent to Factory: no Factory account here. --- packages/factory/README.md | 6 +++++- packages/factory/index.mjs | 8 ++++++++ packages/factory/package.json | 2 +- packages/factory/request.test.mjs | 23 ++++++++++++++++++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/packages/factory/README.md b/packages/factory/README.md index 6923e71..5331f92 100644 --- a/packages/factory/README.md +++ b/packages/factory/README.md @@ -181,7 +181,11 @@ Use magpie's Claude Code integration to select the Factory provider. magpie manages the provider-specific client settings, including capability and permission configuration. Fields such as `safeguards` and `context_management` are forwarded for Factory to validate; the request -adapter preserves them along with the other request options. +adapter preserves them along with the other request options. One exception: +Factory's streaming route refuses `context_management` ("Extra inputs are +not permitted") while its non-streaming route takes it, so a streamed +request is sent without it. The server then clears nothing from the +context; Claude Code's own compaction still runs. The same metadata adaptation applies to `/messages` and `/messages/count_tokens`, so token counting sees the prompt used for diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index 3d12c30..7b10597 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -1069,6 +1069,14 @@ function anthropicBody(body) { } } } + // Factory's streaming route refuses context_management ("Extra inputs + // are not permitted", #71) while its non-streaming route takes it. A + // streamed request goes without it: the server then edits nothing out of + // the context, and the client's own compaction still runs. + if (request.stream === true && "context_management" in request) { + delete request.context_management + changed = true + } return changed ? JSON.stringify(request) : body } diff --git a/packages/factory/package.json b/packages/factory/package.json index 2a721ba..2fb0fb7 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-factory-auth", - "version": "0.1.22", + "version": "0.1.23", "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/factory/request.test.mjs b/packages/factory/request.test.mjs index 500d1e1..6dae91a 100644 --- a/packages/factory/request.test.mjs +++ b/packages/factory/request.test.mjs @@ -55,7 +55,9 @@ test("adapts Claude Code metadata, preserving instructions, tool turns, images a expect(sent.messages[0].content[1].text).toContain("Model knowledge cutoff: January 2025.") expect(sent.messages[0].content.slice(2)).toEqual(request.messages[0].content.slice(2)) expect(sent.messages.slice(1)).toEqual(request.messages.slice(1)) - for (const field of ["model", "tools", "metadata", "max_tokens", "thinking", "output_config", "stream", "context_management", "safeguards"]) expect(sent[field]).toEqual(request[field]) + for (const field of ["model", "tools", "metadata", "max_tokens", "thinking", "output_config", "stream", "safeguards"]) expect(sent[field]).toEqual(request[field]) + // streamed: Factory's streaming route refuses context_management (#71) + expect("context_management" in sent).toBe(false) expect(seen[0].headers.get("content-length")).toBeNull() expect(seen[0].headers.get("Authorization")).toBe("Bearer factory-token") expect(seen[0].headers.get("x-api-key")).toBe("placeholder") @@ -1296,3 +1298,22 @@ test("only a system message that is a skill list is adapted (#72)", async () => expect(JSON.parse(seen.at(-1).body).messages[1].content).toBe(text) } }) + +// #71 (Vigilans): Factory's streaming route answers context_management with +// 400 "Extra inputs are not permitted"; the same body unstreamed, or +// without it, gets 200. The reporter's body, byte for byte. +test("a streamed request goes without context_management, an unstreamed one keeps it (#71)", async () => { + const { l, seen } = await loaded() + const request = { + model: "factory/claude-opus-5-5", + max_tokens: 32, + stream: true, + messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }], + context_management: { edits: [{ keep: "all", type: "clear_thinking_20251015" }] }, + } + await l.fetch(url, { method: "POST", headers: { "anthropic-beta": "context-management-2025-06-27" }, body: JSON.stringify(request) }) + const { context_management, ...rest } = request + expect(JSON.parse(seen.at(-1).body)).toEqual({ ...rest, system: [{ type: "text", text: droid }] }) + await l.fetch(url, { method: "POST", body: JSON.stringify({ ...request, stream: false }) }) + expect(JSON.parse(seen.at(-1).body).context_management).toEqual(request.context_management) +})