commandcode 0.1.14: a Zero Data Retention switch sends x-cmd-zdr: 1 on the Provider API and Go's /alpha/generate (#246)
moonshardx (#246): Command Code keeps request content up to 30 days unless Zero Data Retention is asked for (commandcode.ai/docs/resources/ zdr): CMD_ZDR=1 for the CLI, x-cmd-zdr: 1 on the Provider API. The plugin had no way to send it. - The plugin declares a "Zero Data Retention" switch (auth.options, magpie #2038, off by default, with a cost note to confirm). magpie shows it in the provider's editor; the plugin reads it through input.magpie.option at each request, so no restart is needed. CMD_ZDR=1 in the plugin process's environment also turns it on, as for the CLI; only "1" does, as the CLI's isCmdZdrEnabled reads it. - While on, the Provider API request and Go's /alpha/generate request each carry x-cmd-zdr: 1. For /alpha/generate this mirrors the CLI: its 1.84.0 bundle (npm pack, read, not run) adds [CMD_ZDR]:"1" in buildCommandAuthHeaders, and the installed CLI 1.74.1, run with CMD_ZDR=1 in a sandbox HOME through a logging pass-through proxy (COMMANDCODE_SANDBOX=true, COMMANDCODE_API_URL), sent x-cmd-zdr=1 on /alpha/generate and on every other request. - A model with no ZDR upstream: the Provider API's 422 cmd_zdr_no_providers goes on as it came. On /alpha/generate Command Code answers 200 with an error line ({"type":"cmd_zdr_no_providers","code": "CMD_ZDR_NO_PROVIDERS","statusCode":400}); the agent gets that 400 and message as before, and now the error's type cmd_zdr_no_providers instead of commandcode_error. Run: zdr.test.mjs is new (6 tests, fixtures are Command Code's literal bodies from today). Broke each by hand and watched red: no option, no Provider header, no generate header, CMD_ZDR read as truthy, the switch read once, the 422 rewritten, the type dropped: 1 to 3 fail each. bun test packages/commandcode: 55 pass, 1 skip, 0 fail. bun scripts/check.mjs commandcode: ok. HOME=$(mktemp -d under $TMPDIR) bun test: 1822 pass, 0 fail. Live, with the owner's GOAT key, the plugin's fetch with the switch on: Provider API deepseek-v4-flash 200 with x-cmd-zdr: 1; Provider API poolside/laguna-s-2.1-free 422 cmd_zdr_no_providers, body as it came; /alpha/generate (plan forced to Go) deepseek-v4-flash 200 with the header, laguna 400 cmd_zdr_no_providers; with the switch off neither had the header. Not tried: a real Go account (none here), the switch in magpie's editor itself (driven by a stand-in for input.magpie), OpenCode.
This commit is contained in:
@@ -48,6 +48,26 @@ When the plan isn't known yet and the Provider API refuses the key with
|
||||
then on (and saved so with it), and a chat completion is sent again to
|
||||
`/alpha/generate`.
|
||||
|
||||
## Zero Data Retention
|
||||
|
||||
[Zero Data Retention](https://commandcode.ai/docs/resources/zdr) sends
|
||||
requests only to upstreams that keep no prompts or replies. It is off
|
||||
until you turn it on, either way:
|
||||
|
||||
- **In magpie**: turn on **Zero Data Retention** in the Command Code
|
||||
provider's editor (magpie v0.1.1231 or later). The switch is read at
|
||||
each request, so it needs no restart.
|
||||
- **Anywhere** (OpenCode too): set `CMD_ZDR=1` in the environment of the
|
||||
process the plugin runs in, as for Command Code's CLI. Only `1` turns it
|
||||
on.
|
||||
|
||||
While it is on, every request for a reply carries `x-cmd-zdr: 1`, on the
|
||||
Provider API and on Go's `/alpha/generate` alike, as the CLI sends it with `CMD_ZDR=1`.
|
||||
ZDR requests usually cost more. A model with no ZDR upstream is refused,
|
||||
and the refusal reaches the agent as Command Code said it: on the Provider
|
||||
API a 422 with code `cmd_zdr_no_providers`, on Go a 400 whose type is
|
||||
`cmd_zdr_no_providers`. Turn the switch off, or pick another model.
|
||||
|
||||
## Models
|
||||
|
||||
The `config` hook declares the default list:
|
||||
|
||||
@@ -12,6 +12,13 @@
|
||||
// is only taken where the CLI itself asks, POST /alpha/generate, in the
|
||||
// CLI's own format (command-code 1.72.2), which the fetch here writes from
|
||||
// the chat completion OpenCode sends and turns back into one.
|
||||
//
|
||||
// Zero Data Retention (commandcode.ai/docs/resources/zdr) is asked for as
|
||||
// the CLI asks it: x-cmd-zdr: 1 on every request for a reply, the Provider
|
||||
// API's and /alpha/generate's alike (command-code's buildCommandAuthHeaders
|
||||
// on CMD_ZDR=1: 1.84.0's bundle, and 1.74.1 as captured). It is on when the user turns on the provider's "Zero Data
|
||||
// Retention" switch in magpie, or when CMD_ZDR is "1" in the environment,
|
||||
// as for the CLI.
|
||||
import { createServer, STATUS_CODES } from "node:http"
|
||||
import { randomBytes, randomUUID } from "node:crypto"
|
||||
import { readFile } from "node:fs/promises"
|
||||
@@ -935,8 +942,25 @@ function generateBody(chat, place = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
// generateHeaders are the CLI's own (buildCommandAuthHeaders).
|
||||
const generateHeaders = (key) => ({
|
||||
// ZDR is the header that asks for Zero Data Retention, and ZDR_OPTION the
|
||||
// provider's switch for it in magpie (auth.options, read through
|
||||
// input.magpie.option).
|
||||
const ZDR = "x-cmd-zdr"
|
||||
const ZDR_OPTION = "zdr"
|
||||
|
||||
// zdrOn says whether requests ask for Zero Data Retention: magpie's switch
|
||||
// for it, read at each request so a change needs no restart, or CMD_ZDR
|
||||
// as the CLI reads it ("1" alone; isCmdZdrEnabled).
|
||||
function zdrOn(magpie) {
|
||||
try {
|
||||
if (magpie?.option?.(ID, ZDR_OPTION) === true) return true
|
||||
} catch {}
|
||||
return process.env.CMD_ZDR === "1"
|
||||
}
|
||||
|
||||
// generateHeaders are the CLI's own (buildCommandAuthHeaders), with its
|
||||
// x-cmd-zdr: 1 when ZDR is on.
|
||||
const generateHeaders = (key, zdr = false) => ({
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "cli",
|
||||
"x-command-code-version": CLI_VERSION,
|
||||
@@ -945,6 +969,7 @@ const generateHeaders = (key) => ({
|
||||
"x-taste-learning": "false",
|
||||
"x-session-id": session,
|
||||
Authorization: `Bearer ${key}`,
|
||||
...(zdr ? { [ZDR]: "1" } : {}),
|
||||
})
|
||||
|
||||
// QUOTA_WORDS are magpie's quotaWords (internal/gateway/fallback.go): a
|
||||
@@ -996,14 +1021,20 @@ function streamError(e) {
|
||||
if (j?.error?.message) msg = j.error.message
|
||||
} catch {}
|
||||
}
|
||||
return failure(status || 500, msg)
|
||||
const f = failure(status || 500, msg)
|
||||
// a ZDR request for a model no upstream that keeps nothing serves: its
|
||||
// type goes on with it, as the Provider API's 422 says it
|
||||
// ({"type":"cmd_zdr_no_providers","code":"CMD_ZDR_NO_PROVIDERS",
|
||||
// "statusCode":400} on /alpha/generate)
|
||||
if (typeof e === "object" && (e?.type === "cmd_zdr_no_providers" || e?.code === "CMD_ZDR_NO_PROVIDERS")) f.type = "cmd_zdr_no_providers"
|
||||
return f
|
||||
}
|
||||
|
||||
// errorResponse is a failure as the built-in answered it, the account
|
||||
// kept: the built-in never marked a Command Code account lapsed, a 401 of
|
||||
// Command Code's among them.
|
||||
const errorResponse = ({ status, message }) =>
|
||||
new Response(JSON.stringify({ error: { message, type: "commandcode_error", code: status } }), {
|
||||
const errorResponse = ({ status, message, type }) =>
|
||||
new Response(JSON.stringify({ error: { message, type: type || "commandcode_error", code: status } }), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept" },
|
||||
})
|
||||
@@ -1112,10 +1143,10 @@ async function* events(body) {
|
||||
}
|
||||
|
||||
// generate answers a chat completion through /alpha/generate.
|
||||
async function generate(key, chat, signal, place) {
|
||||
async function generate(key, chat, signal, place, zdr = false) {
|
||||
const res = await fetch(API + "/alpha/generate", {
|
||||
method: "POST",
|
||||
headers: generateHeaders(key),
|
||||
headers: generateHeaders(key, zdr),
|
||||
body: JSON.stringify(generateBody(chat, place)),
|
||||
signal,
|
||||
})
|
||||
@@ -1189,9 +1220,9 @@ async function generate(key, chat, signal, place) {
|
||||
|
||||
// ---- the plugin ---------------------------------------------------------------
|
||||
|
||||
export const _internal = { subsSeen, extraSeen, waits, liveKey, failure, goModels, refuses, GO_MODELS, GO_REFUSED }
|
||||
export const _internal = { subsSeen, extraSeen, waits, liveKey, failure, goModels, refuses, GO_MODELS, GO_REFUSED, zdrOn }
|
||||
|
||||
export async function CommandCodePlugin({ client } = {}) {
|
||||
export async function CommandCodePlugin({ client, magpie } = {}) {
|
||||
// isGo keeps a key as Go's, for its models and endpoint: in the
|
||||
// subscriptions seen, and saved with the key as usage saves the plan read
|
||||
async function isGo(key, auth) {
|
||||
@@ -1224,6 +1255,11 @@ export async function CommandCodePlugin({ client } = {}) {
|
||||
headers.set("Authorization", `Bearer ${key}`)
|
||||
headers.set("x-api-key", key)
|
||||
const place = workPlace(headers)
|
||||
// Zero Data Retention, as the CLI asks it; a 422
|
||||
// cmd_zdr_no_providers (no upstream that keeps nothing serves
|
||||
// the model) goes on to the agent as it came
|
||||
const zdr = zdrOn(magpie)
|
||||
if (zdr) headers.set(ZDR, "1")
|
||||
const chatting = /\/chat\/completions$/.test(new URL(url).pathname)
|
||||
let chat
|
||||
if (chatting) {
|
||||
@@ -1233,7 +1269,7 @@ export async function CommandCodePlugin({ client } = {}) {
|
||||
} catch {
|
||||
return errorResponse({ status: 400, message: "a request that isn't JSON" })
|
||||
}
|
||||
if ((await planNow(key, saved)) === "Go") return kept(await generate(key, chat, init.signal, place))
|
||||
if ((await planNow(key, saved)) === "Go") return kept(await generate(key, chat, init.signal, place, zdr))
|
||||
}
|
||||
// the Provider API's answer goes on as it came, the account
|
||||
// kept: the built-in neither marked one lapsed nor cleared it
|
||||
@@ -1246,12 +1282,23 @@ export async function CommandCodePlugin({ client } = {}) {
|
||||
const text = await res.text()
|
||||
if (GO_NO_API.test(text)) {
|
||||
await isGo(key, (await getAuth()) ?? auth)
|
||||
if (chatting) return kept(await generate(key, chat, init.signal, place))
|
||||
if (chatting) return kept(await generate(key, chat, init.signal, place, zdr))
|
||||
}
|
||||
return kept(new Response(text, { status: res.status, statusText: res.statusText, headers: res.headers }))
|
||||
},
|
||||
}
|
||||
},
|
||||
// magpie's switches for the provider (#2038): shown in its editor,
|
||||
// read with input.magpie.option at each request
|
||||
options: [
|
||||
{
|
||||
key: ZDR_OPTION,
|
||||
label: "Zero Data Retention",
|
||||
description:
|
||||
"Sends x-cmd-zdr: 1, as the CLI's CMD_ZDR=1 does: requests go only to upstreams that keep no prompts or replies. They usually cost more, and a model with no such upstream fails (422 cmd_zdr_no_providers).",
|
||||
confirm: "ZDR requests usually cost more, and a model with no zero-data-retention upstream fails instead of falling back.",
|
||||
},
|
||||
],
|
||||
methods: [
|
||||
{ type: "oauth", label: "Command Code (browser)", authorize: browserSignIn },
|
||||
{ type: "oauth", label: "Command Code CLI's sign-in", authorize: cliSignIn },
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-commandcode-auth",
|
||||
"version": "0.1.13",
|
||||
"version": "0.1.14",
|
||||
"description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
// Zero Data Retention (magpie-community/plugins#246): with the provider's
|
||||
// "Zero Data Retention" switch on in magpie, or CMD_ZDR=1 as for the CLI,
|
||||
// every request carries x-cmd-zdr: 1, the Provider API's and Go's
|
||||
// /alpha/generate alike (command-code 1.74.1 sends it on both, captured
|
||||
// through a proxy). A model no ZDR upstream serves is refused, and that
|
||||
// refusal reaches the agent as Command Code said it.
|
||||
import { afterEach, beforeEach, expect, test } from "bun:test"
|
||||
import { CommandCodePlugin, _internal } from "./index.mjs"
|
||||
|
||||
const real = globalThis.fetch
|
||||
let env
|
||||
beforeEach(() => {
|
||||
env = process.env.CMD_ZDR
|
||||
delete process.env.CMD_ZDR
|
||||
})
|
||||
afterEach(() => {
|
||||
globalThis.fetch = real
|
||||
_internal.subsSeen.clear()
|
||||
if (env === undefined) delete process.env.CMD_ZDR
|
||||
else process.env.CMD_ZDR = env
|
||||
})
|
||||
|
||||
// Command Code's own answers, as they came on 2026-10-19 for
|
||||
// poolside/laguna-s-2.1-free with x-cmd-zdr: 1: the Provider API's 422,
|
||||
// and /alpha/generate's error line (a 200, the error in the stream)
|
||||
const PROVIDER_422 =
|
||||
'{"error":{"message":"This model has no zero-data-retention upstream. Remove the x-cmd-zdr header or choose a different model.","type":"invalid_request_error","code":"cmd_zdr_no_providers"}}'
|
||||
const GENERATE_LINE =
|
||||
'{"type":"error","error":{"type":"cmd_zdr_no_providers","code":"CMD_ZDR_NO_PROVIDERS","message":"This model has no zero-data-retention upstream. Disable CMD_ZDR or pick a different model.","statusCode":400,"isRetryable":false}}'
|
||||
|
||||
const OK_LINES = [{ type: "text-delta", text: "ok" }, { type: "finish", finishReason: "stop", totalUsage: { inputTokens: 1, outputTokens: 1 } }]
|
||||
.map((l) => JSON.stringify(l))
|
||||
.join("\n")
|
||||
|
||||
// setup gives the plugin a magpie whose switch is as on.zdr says, and
|
||||
// records each request's path and its x-cmd-zdr
|
||||
function setup(on, reply = {}) {
|
||||
const sent = []
|
||||
globalThis.fetch = async (input, init = {}) => {
|
||||
const path = new URL(typeof input === "string" ? input : input.url ?? String(input)).pathname
|
||||
sent.push({ path, zdr: new Headers(init.headers).get("x-cmd-zdr") })
|
||||
if (path === "/alpha/generate") return new Response(reply.generate ?? OK_LINES + "\n")
|
||||
if (path.endsWith("/chat/completions")) return reply.provider ? reply.provider() : Response.json({ choices: [] })
|
||||
return new Response("", { status: 404 })
|
||||
}
|
||||
const asked = []
|
||||
const magpie = {
|
||||
option(provider, key) {
|
||||
asked.push([provider, key])
|
||||
return on.zdr === true && provider === "commandcode-plan" && key === "zdr"
|
||||
},
|
||||
}
|
||||
return { sent, asked, magpie }
|
||||
}
|
||||
|
||||
async function ask(magpie, plan, model = "deepseek/deepseek-v4-flash") {
|
||||
const hooks = await CommandCodePlugin({ magpie })
|
||||
const opts = await hooks.auth.loader(async () => ({ type: "api", key: "k-" + plan, metadata: { plan } }))
|
||||
return opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ model, stream: false, messages: [{ role: "user", content: "hi" }] }),
|
||||
})
|
||||
}
|
||||
|
||||
test("the plugin declares a Zero Data Retention switch, off unless turned on", async () => {
|
||||
const hooks = await CommandCodePlugin()
|
||||
const o = hooks.auth.options
|
||||
expect(o.map((x) => x.key)).toEqual(["zdr"])
|
||||
expect(o[0].label).toBe("Zero Data Retention")
|
||||
expect(o[0].default).not.toBe(true)
|
||||
expect(o[0].description).toContain("x-cmd-zdr: 1")
|
||||
expect(o[0].description.length).toBeLessThanOrEqual(300)
|
||||
})
|
||||
|
||||
test("the Provider API is sent x-cmd-zdr: 1 while the switch is on, read at each request", async () => {
|
||||
const on = { zdr: true }
|
||||
const { sent, asked, magpie } = setup(on)
|
||||
const hooks = await CommandCodePlugin({ magpie })
|
||||
const opts = await hooks.auth.loader(async () => ({ type: "api", key: "k", metadata: { plan: "GOAT" } }))
|
||||
const go = () =>
|
||||
opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", { method: "POST", body: JSON.stringify({ model: "m", messages: [] }) })
|
||||
await go()
|
||||
on.zdr = false
|
||||
await go()
|
||||
const chats = sent.filter((s) => s.path.endsWith("/chat/completions"))
|
||||
expect(chats.map((s) => s.zdr)).toEqual(["1", null])
|
||||
expect(asked).toContainEqual(["commandcode-plan", "zdr"])
|
||||
})
|
||||
|
||||
test("a Go request to /alpha/generate carries x-cmd-zdr: 1 as the CLI's does, and none while off", async () => {
|
||||
const on = { zdr: true }
|
||||
const { sent, magpie } = setup(on)
|
||||
expect((await ask(magpie, "Go")).status).toBe(200)
|
||||
on.zdr = false
|
||||
expect((await ask(magpie, "Go")).status).toBe(200)
|
||||
expect(sent.filter((s) => s.path === "/alpha/generate").map((s) => s.zdr)).toEqual(["1", null])
|
||||
})
|
||||
|
||||
test("CMD_ZDR=1 turns it on as for the CLI, and only \"1\" does", async () => {
|
||||
const { sent } = setup({})
|
||||
process.env.CMD_ZDR = "1"
|
||||
await ask(undefined, "GOAT")
|
||||
await ask(undefined, "Go")
|
||||
process.env.CMD_ZDR = "true"
|
||||
await ask(undefined, "GOAT")
|
||||
await ask(undefined, "Go")
|
||||
delete process.env.CMD_ZDR
|
||||
await ask(undefined, "GOAT")
|
||||
expect(sent.filter((s) => s.path === "/alpha/generate" || s.path.endsWith("/chat/completions")).map((s) => s.zdr)).toEqual(["1", "1", null, null, null])
|
||||
})
|
||||
|
||||
test("the Provider API's 422 cmd_zdr_no_providers reaches the agent as it came", async () => {
|
||||
const { magpie } = setup({ zdr: true }, { provider: () => new Response(PROVIDER_422, { status: 422, headers: { "Content-Type": "application/json" } }) })
|
||||
const res = await ask(magpie, "GOAT", "poolside/laguna-s-2.1-free")
|
||||
expect(res.status).toBe(422)
|
||||
expect(await res.text()).toBe(PROVIDER_422)
|
||||
expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept")
|
||||
})
|
||||
|
||||
test("Go's cmd_zdr_no_providers error line reaches the agent with its status, message and type", async () => {
|
||||
const { magpie } = setup({ zdr: true }, { generate: GENERATE_LINE + "\n" })
|
||||
const res = await ask(magpie, "Go", "poolside/laguna-s-2.1-free")
|
||||
expect(res.status).toBe(400)
|
||||
expect((await res.json()).error).toEqual({
|
||||
message: "This model has no zero-data-retention upstream. Disable CMD_ZDR or pick a different model.",
|
||||
type: "cmd_zdr_no_providers",
|
||||
code: 400,
|
||||
})
|
||||
expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept")
|
||||
})
|
||||
Reference in New Issue
Block a user