From 7096e125b43a964472004bf0a45ea1e78cc07fd1 Mon Sep 17 00:00:00 2001 From: Devendra Mishra Date: Wed, 18 Nov 2026 05:12:54 +0530 Subject: [PATCH 1/5] feat(config): persist zero data retention in settings.json ZDR was environment-only and read once at registration, so opting in needed `CMD_ZDR=1` in the host's environment and opting back out needed a restart. Nothing in the host surfaced which state was active. - `commandcode.enableZDR: true` in the agent `settings.json` now opts in. The documented `CMD_ZDR`/`COMMANDCODE_ZDR` variables keep precedence so a shell export cannot be weakened by a file, and only a literal `true` counts. - `/commandcode-zdr [on|off]` reports the effective state and writes the key, preserving every other settings key. A malformed or non-object file is never overwritten, and `off` is refused while the environment forces ZDR on. - Host settings are unreadable while extensions load, so the opt-in starts from the environment and is completed on `session_start`. A changed state re-registers the provider, which is the only way to change a registration header, so a toggle applies to the next request instead of the next restart. - `createProviderConfig` now always registers a `headers` object (`headers ?? {}`): pi merges a re-registration over the previous one and preserves undefined values, so an absent object would keep sending a ZDR header after it is turned off. - `/commandcode-status` and `/commandcode-diagnostics` report the state. --- README.md | 3 +- index.ts | 109 ++++++++++++++++++++++++++++++--- package.json | 5 +- src/zdr.ts | 117 +++++++++++++++++++++++++++++++++++ tests/test-pi-local.mjs | 58 +++++++++++++++++- tests/test-zdr.ts | 131 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 409 insertions(+), 14 deletions(-) create mode 100644 src/zdr.ts create mode 100644 tests/test-zdr.ts diff --git a/README.md b/README.md index 10dd46f..e5d412b 100644 --- a/README.md +++ b/README.md @@ -163,13 +163,14 @@ While pi is running, use these provider commands without restarting: - `/commandcode-refresh` fetches and re-registers the current model catalog, and resets the latest transport status. Every inference attempt uses the Provider API; entitlement denials are not cached. Overlapping refreshes are coalesced, and a failed refresh keeps the last valid catalog active. - `/commandcode-status` shows redacted discovery diagnostics, including the source, model count, timestamps, cache path, endpoint, and warning. Transport status is `unknown` until the latest inference attempt completes successfully; a denial remains `unknown`, not proof of API entitlement. If a Responses reasoning event has an unsupported shape, a separate stream warning is cleared when the next Responses HTTP request starts, before its outcome is known. Requests on Chat Completions or Messages do not clear it. The slot is shared by concurrent Responses requests, so a late warning from an older in-flight response can still replace it. The event is preserved unchanged; no reasoning text is included in the warning. - `/commandcode-diagnostics` shows a shareable support report: plugin/host version, CLI snapshot, selected transport and wire, capability provenance, price status, and the latest request's HTTP status, attempts, outcome, first body-chunk latency and longest gap. It keeps only one request summary in memory. It never collects prompts, tool content, credentials, raw errors, account data or configured local paths. Model identifiers come from the host; recognized path-/credential-shaped values are redacted, but custom names can still identify your setup. Request IDs are included only when they match a bounded hexadecimal/UUID format. Timing is measured between body chunks, not tokens; first-chunk latency may include an error response. A request started most recently owns the report even if another request finishes later. Retry counts are scoped to that stream call, not host-level retries. Review the report before sharing it. +- `/commandcode-zdr` shows the effective zero-data-retention state; `/commandcode-zdr on` or `/commandcode-zdr off` writes it to the agent settings file. The provider re-registers so the change applies to the next request and survives restart. Turning it off is refused while `CMD_ZDR` or `COMMANDCODE_ZDR` is set, because the environment wins. - `/commandcode-quota` shows your Command Code account usage and quota in a dashboard-style layout: credits remaining and used with a percentage, monthly/purchased/free sources, the current plan, available usage totals, the API key name, and the 5-hour and weekly usage windows. As an explicit exception to the inference policy, the `commandcode-quota` command reads from the internal Command Code alpha usage endpoints (the same ones the `cmd` CLI `/usage` command uses): `whoami`, `billing/credits`, `billing/subscriptions`, and `usage/summary`. It authenticates with the same API key the provider already uses. These requests are GET-only account/usage reads, not an inference fallback. Their success says nothing about Provider API entitlement; the undocumented schemas and availability may change independently. If the command cannot reach those endpoints or an endpoint schema changes, unavailable sections are reported explicitly instead of being displayed as zero usage. Output is plain text (via `ui.notify`) so it works across pi and compatible hosts such as OMP. On Oh My Pi the provider also registers an OMP usage provider through the `usage` field of `pi.registerProvider`, reading the same endpoints with the same credentials. It supplies the account's 5-hour and weekly usage windows, credits remaining and used against the billing-period pool, and the current plan to the host's usage surfaces, and it follows the same rule for sections the API does not report. pi has no such field and ignores it. -Set `CMD_ZDR=1` to send Command Code's documented `x-cmd-zdr: 1` zero-data-retention header. The legacy `COMMANDCODE_ZDR=1` alias remains supported. +Set `CMD_ZDR=1` to send Command Code's documented `x-cmd-zdr: 1` zero-data-retention header. The legacy `COMMANDCODE_ZDR=1` alias remains supported. A settings file is an alternative where exporting an environment variable is inconvenient: add `{ "commandcode": { "enableZDR": true } }` to `settings.json` in the agent directory (`~/.pi/agent/settings.json` for pi), or run `/commandcode-zdr on`. Only a literal `true` enables it, the environment wins over the file, and every other settings key is preserved when the command writes it. The opt-in is sent as a registration header, so an edit made outside the command applies after `/reload` or a restart. Some models have no ZDR-capable upstream yet; a request for those models fails instead of routing through a non-ZDR provider. The following environment variables are intended for tests, local mocks, and compatible API endpoints: diff --git a/index.ts b/index.ts index fc63378..0669d36 100644 --- a/index.ts +++ b/index.ts @@ -49,6 +49,7 @@ import { registerCommandCodeQuota } from "./src/quota-command.ts" import { normalizeCommandCodeResponsesResponse } from "./src/responses-stream.ts" import { modelWireTraits, withResponsesWireRepairs } from "./src/responses-compat.ts" import { createCommandCodeRuntime } from "./src/runtime.ts" +import { describeZdr, resolveZdr, saveZdrSetting, zdrHeaders, type ZdrState } from "./src/zdr.ts" import { compatForEndpoint, endpointChoiceForModel, @@ -112,11 +113,18 @@ function providerApiKey(): string | undefined { return compatApiProviderRegistrar() ? "$COMMAND_CODE_API_KEY" : undefined } -function commandCodeHeaders(): Record | undefined { - if (process.env.CMD_ZDR === "1" || process.env.COMMANDCODE_ZDR === "1") { - return { "x-cmd-zdr": "1" } +/** + * Settings live on the host. Oh My Pi has no settings accessor, so a missing + * reader means "no settings section" rather than an error. + */ +function hostSettings(pi: ExtensionAPI): unknown { + const readSettings = (pi as { getSettings?: unknown }).getSettings + if (typeof readSettings !== "function") return undefined + try { + return readSettings.call(pi) + } catch { + return undefined } - return undefined } /** @@ -181,19 +189,23 @@ function createProviderConfig( models: readonly CommandCodeModel[], apiBase: string, streamCommandCode: ProviderConfig["streamSimple"], + headers: Record | undefined, ): CommandCodeProviderConfig { - const headers = commandCodeHeaders() + // The host merges a re-registration over the previous one and preserves + // undefined values, so an empty object is what clears a previously sent + // header after ZDR is turned off. + const registeredHeaders = headers ?? {} return { name: "Command Code", baseUrl: apiBase, apiKey: providerApiKey(), api: COMMAND_CODE_API, streamSimple: streamCommandCode, - headers, + headers: registeredHeaders, // Same alpha endpoints and credentials as the /commandcode-quota command. usage: createCommandCodeUsageProvider({ apiBase: quotaApiBase(apiBase), - headers, + headers: registeredHeaders, }), oauth: { name: "Command Code", @@ -216,7 +228,7 @@ function createProviderConfig( }, contextWindow: model.contextWindow, maxTokens: model.maxTokens, - headers, + headers: registeredHeaders, compat: { ...compatForModel(model), commandCodeWire: model.api }, })), } @@ -260,6 +272,9 @@ export default async function (pi: ExtensionAPI) { // Hosts may select their built-in Command Code model before loading extensions. // Rebind that stale selection to our registered transport before the first turn. pi.on("session_start", async (_event, ctx) => { + // Completes the ZDR opt-in now that host settings are readable, and picks up + // a settings file changed since the last load. + applyHostZdr() if (ctx.model?.provider !== "commandcode") return const registered = ctx.modelRegistry.find("commandcode", ctx.model.id) if (registered?.api === COMMAND_CODE_API && ctx.model.api !== COMMAND_CODE_API) { @@ -277,6 +292,12 @@ export default async function (pi: ExtensionAPI) { // the wire each model answers on is resolved here from the catalog that // `createProviderConfig` registered. Unknown ids fall back to the id rule. const catalogModels = new Map() + // The environment is readable while extensions load, host settings are not, so + // the opt-in starts from the environment and is completed at session start. + // The environment is fixed for the process; settings only reload with the + // extension. /commandcode-zdr updates this state after writing the file. + const settingsPath = join(getAgentDir(), "settings.json") + let zdr: ZdrState = resolveZdr(undefined) const routingPath = join(getAgentDir(), "commandcode-routing.json") let routing: EndpointOverrides = {} let routingWarning: string | undefined @@ -413,15 +434,82 @@ export default async function (pi: ExtensionAPI) { const compatStream: CompatStreamFunction = measuredStream registerCompatApiProvider(compatStream) + const registerProvider = () => + pi.registerProvider( + "commandcode", + createProviderConfig([...catalogModels.values()], apiBase, measuredStream, zdrHeaders(zdr)), + ) + const applyHostZdr = () => { + const resolved = resolveZdr(hostSettings(pi)) + const changed = resolved.enabled !== zdr.enabled + zdr = resolved + // Re-registration is the only way to change a registration header, and it + // needs a catalog to register. + if (changed && catalogModels.size > 0) registerProvider() + } + pi.on("message_end", async (event, ctx) => { if (event.message.role !== "assistant") return const normalized = normalizeCommandCodeMessage(event.message, ctx.model?.provider) return normalized ? { message: normalized.message } : undefined }) + // The opt-in is only sent as a registration header, so a change needs the + // provider re-registered before it reaches the next request. Re-registering + // is the same path /commandcode-refresh already uses. + pi.registerCommand("commandcode-zdr", { + description: "Show or set zero data retention: /commandcode-zdr [on|off]", + handler: async (args, ctx) => { + const [choice, extra] = args.trim().split(/\s+/) + if (!choice) { + ctx.ui.notify( + `Command Code zero data retention: ${describeZdr(zdr)}. Usage: /commandcode-zdr on|off`, + "info", + ) + return + } + if ((choice !== "on" && choice !== "off") || extra) { + ctx.ui.notify("Usage: /commandcode-zdr on|off", "warning") + return + } + // The environment wins at resolution time, so refuse instead of writing a + // setting that would not take effect. + if (choice === "off" && zdr.source === "env") { + ctx.ui.notify( + `Zero data retention is forced on by ${zdr.envVar ?? "the environment"}. Unset that variable to disable it.`, + "warning", + ) + return + } + await ctx.waitForIdle?.() + let written: unknown + try { + written = await saveZdrSetting(settingsPath, choice === "on") + } catch (error) { + ctx.ui.notify( + error instanceof Error ? error.message : "Could not save settings.json", + "error", + ) + return + } + zdr = resolveZdr(written) + // Without a catalog there is nothing to register; the next successful + // load or /commandcode-refresh picks the saved setting up. + const applied = catalogModels.size > 0 + if (applied) registerProvider() + ctx.ui.notify( + `Command Code zero data retention: ${describeZdr(zdr)}. Saved to settings.json; ` + + (applied + ? "applies to the next request and survives restart." + : "applies after the next catalog load or /commandcode-refresh."), + "info", + ) + }, + }) + registerCommandCodeQuota(pi, { apiBase: quotaApiBase(apiBase), - headers: commandCodeHeaders(), + headers: zdrHeaders(zdr), }) const runtime = createCommandCodeRuntime(pi, { @@ -438,7 +526,7 @@ export default async function (pi: ExtensionAPI) { createProviderConfig: (models) => { catalogModels.clear() for (const model of models) catalogModels.set(model.id, model) - return createProviderConfig(models, apiBase, measuredStream) + return createProviderConfig(models, apiBase, measuredStream, zdrHeaders(zdr)) }, getDiagnostics: (model) => { let route = "not selected" @@ -473,6 +561,7 @@ export default async function (pi: ExtensionAPI) { request: diagnostics.snapshot(), }), endpointStatus, + `zdr: ${describeZdr(zdr)}`, routingWarning, ] .filter(Boolean) diff --git a/package.json b/package.json index c1e0f93..f6e3887 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "LICENSE" ], "scripts": { - "test": "npm run typecheck && npm run test:diagnostics && npm run test:routing && node --test tests/test-release.mjs tests/test-maintenance-alert.mjs tests/test-pi-child-cleanup.mjs tests/test-entrypoint-typecheck.mjs tests/test-host-protocol-helpers.mjs && tsx tests/test-package-manifest.ts && tsx tests/test-api-key.ts && tsx tests/test-pure-functions.ts && tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts && tsx tests/test-runtime.ts && tsx tests/test-pricing.ts && tsx tests/test-pricing-check.ts && tsx tests/test-cost.ts && tsx tests/test-oauth.ts && tsx tests/test-overflow.ts && tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts && tsx tests/test-transport.ts && tsx tests/test-responses-compat.ts && node tests/test-pi-isolated.mjs && node tests/test-pi-authenticated.mjs && node tests/test-pi-local.mjs && node tests/test-omp-compat.mjs && node tests/test-host-protocols.mjs && node tests/test-endpoint-routing.mjs", + "test": "npm run typecheck && npm run test:diagnostics && npm run test:routing && node --test tests/test-release.mjs tests/test-maintenance-alert.mjs tests/test-pi-child-cleanup.mjs tests/test-entrypoint-typecheck.mjs tests/test-host-protocol-helpers.mjs && tsx tests/test-package-manifest.ts && tsx tests/test-api-key.ts && tsx tests/test-pure-functions.ts && tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts && tsx tests/test-runtime.ts && tsx tests/test-pricing.ts && tsx tests/test-pricing-check.ts && tsx tests/test-cost.ts && tsx tests/test-oauth.ts && tsx tests/test-overflow.ts && tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts && tsx tests/test-transport.ts && tsx tests/test-responses-compat.ts && tsx tests/test-zdr.ts && node tests/test-pi-isolated.mjs && node tests/test-pi-authenticated.mjs && node tests/test-pi-local.mjs && node tests/test-omp-compat.mjs && node tests/test-host-protocols.mjs && node tests/test-endpoint-routing.mjs", "typecheck": "tsc --noEmit", "typecheck:entrypoint": "node scripts/typecheck-entrypoint.mjs", "format:check": "prettier --check '**/*.{ts,mjs,json,md}'", @@ -41,7 +41,7 @@ "sync:commandcode-catalog": "tsx .github/scripts/check-commandcode-model-metadata.ts command-code@latest --write", "test:quota": "tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts", "test:usage": "tsx tests/test-usage.ts", - "test:unit": "npm run test:diagnostics && npm run test:routing && tsx tests/test-api-key.ts && tsx tests/test-pure-functions.ts && tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts && tsx tests/test-runtime.ts && tsx tests/test-pricing.ts && tsx tests/test-pricing-check.ts && tsx tests/test-cost.ts && tsx tests/test-oauth.ts && tsx tests/test-overflow.ts && tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts && tsx tests/test-transport.ts && tsx tests/test-responses-compat.ts", + "test:unit": "npm run test:diagnostics && npm run test:routing && tsx tests/test-api-key.ts && tsx tests/test-pure-functions.ts && tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts && tsx tests/test-runtime.ts && tsx tests/test-pricing.ts && tsx tests/test-pricing-check.ts && tsx tests/test-cost.ts && tsx tests/test-oauth.ts && tsx tests/test-overflow.ts && tsx tests/test-quota.ts && tsx tests/test-quota-command.ts && tsx tests/test-usage.ts && tsx tests/test-transport.ts && tsx tests/test-responses-compat.ts && tsx tests/test-zdr.ts", "test:api-key": "tsx tests/test-api-key.ts", "test:models": "tsx tests/test-models.ts && tsx tests/test-model-metadata-check.ts", "test:diagnostics": "tsx tests/test-diagnostics.ts", @@ -65,6 +65,7 @@ "test:e2e:live:provider": "node scripts/live-e2e-profile.mjs provider", "test:e2e:live:all": "node scripts/live-e2e-profile.mjs goat provider", "test:cost": "tsx tests/test-cost.ts", + "test:zdr": "tsx tests/test-zdr.ts", "test:responses-compat": "tsx tests/test-responses-compat.ts" }, "pi": { diff --git a/src/zdr.ts b/src/zdr.ts new file mode 100644 index 0000000..dff066b --- /dev/null +++ b/src/zdr.ts @@ -0,0 +1,117 @@ +// Zero-data-retention opt-in, carried in settings.json for hosts where setting +// a process environment variable is inconvenient. Command Code documents the +// `x-cmd-zdr: 1` request header and the `CMD_ZDR=1` CLI environment variable. +import { randomUUID } from "node:crypto" +import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises" +import { dirname } from "node:path" + +/** Top-level settings.json section owned by this provider. */ +export const ZDR_SETTINGS_SECTION = "commandcode" +export const ZDR_SETTING_KEY = "enableZDR" + +export type ZdrSource = "off" | "settings" | "env" +export type ZdrEnvVar = "CMD_ZDR" | "COMMANDCODE_ZDR" + +export interface ZdrState { + enabled: boolean + source: ZdrSource + /** Environment variable that enabled ZDR, when `source` is `env`. */ + envVar?: ZdrEnvVar +} + +type Environment = Record + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value) +} + +/** Only a literal `true` enables ZDR, so a typo cannot silently weaken privacy. */ +export function readZdrSetting(settings: unknown): boolean { + if (!isRecord(settings)) return false + const section = settings[ZDR_SETTINGS_SECTION] + return isRecord(section) && section[ZDR_SETTING_KEY] === true +} + +/** `CMD_ZDR` is the documented name; the legacy alias stays supported. */ +export function zdrEnvVar(env: Environment = process.env): ZdrEnvVar | undefined { + if (env.CMD_ZDR === "1") return "CMD_ZDR" + if (env.COMMANDCODE_ZDR === "1") return "COMMANDCODE_ZDR" + return undefined +} + +/** The environment wins so a shell export cannot be silently weakened by a settings file. */ +export function resolveZdr(settings: unknown, env: Environment = process.env): ZdrState { + const envVar = zdrEnvVar(env) + if (envVar) return { enabled: true, source: "env", envVar } + if (readZdrSetting(settings)) return { enabled: true, source: "settings" } + return { enabled: false, source: "off" } +} + +export function zdrHeaders(state: ZdrState): Record | undefined { + return state.enabled ? { "x-cmd-zdr": "1" } : undefined +} + +export function describeZdr(state: ZdrState): string { + if (state.source === "env") return `on (${state.envVar ?? "environment"})` + if (state.source === "settings") return "on (settings.json commandcode.enableZDR)" + return "off" +} + +/** + * Persist the opt-in into the agent settings file, preserving every other key. + * + * Returns the written settings object so the caller resolves the effective + * state without waiting for the host to reload the file. The host re-reads the + * file and merges only the fields it modified on each of its own saves, so this + * key survives later host writes; writes from separate processes are not + * coordinated. A malformed file is never overwritten, and an unreadable file is + * reported instead of replaced. + */ +export async function saveZdrSetting(path: string, enabled: boolean): Promise { + let raw: string | undefined + try { + raw = await readFile(path, "utf8") + } catch (error) { + if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) { + throw new Error("Could not read settings.json in the agent directory; check file permissions") + } + } + + let settings: Record = {} + if (raw !== undefined && raw.trim() !== "") { + let parsed: unknown + try { + parsed = JSON.parse(raw) + } catch { + throw new Error( + "Invalid settings.json in the agent directory; refusing to overwrite it. Fix the JSON or set commandcode.enableZDR by hand.", + ) + } + if (!isRecord(parsed)) { + throw new Error("settings.json must contain a JSON object; refusing to overwrite it") + } + settings = { ...parsed } + } + const section = settings[ZDR_SETTINGS_SECTION] + settings[ZDR_SETTINGS_SECTION] = { + ...(isRecord(section) ? section : {}), + [ZDR_SETTING_KEY]: enabled, + } + + const temporary = `${path}.${randomUUID()}.tmp` + try { + await mkdir(dirname(path), { recursive: true }) + await writeFile(temporary, `${JSON.stringify(settings, null, 2)}\n`, { + encoding: "utf8", + mode: 0o600, + flag: "wx", + }) + await rename(temporary, path) + } catch { + throw new Error("Could not save settings.json in the agent directory; check file permissions") + } finally { + // Cleanup must not replace the sanitized error with an OS message containing local paths. + await rm(temporary, { force: true }).catch(() => {}) + } + return settings +} diff --git a/tests/test-pi-local.mjs b/tests/test-pi-local.mjs index ef6ad8d..9c4cf05 100644 --- a/tests/test-pi-local.mjs +++ b/tests/test-pi-local.mjs @@ -6,7 +6,15 @@ import assert from "node:assert/strict" import { spawn, spawnSync } from "node:child_process" -import { accessSync, constants, mkdtempSync, rmSync, mkdirSync, writeFileSync } from "node:fs" +import { + accessSync, + constants, + mkdtempSync, + rmSync, + mkdirSync, + readFileSync, + writeFileSync, +} from "node:fs" import { createServer } from "node:http" import { tmpdir } from "node:os" import { delimiter, dirname, join, resolve } from "node:path" @@ -1244,6 +1252,53 @@ export default async function (pi) { "string", ) + const settingsFilePath = join(agentDir, "settings.json") + const baseSettings = JSON.parse(readFileSync(settingsFilePath, "utf-8")) + const zdrArgs = [ + "--no-extensions", + "-e", + EXT_PATH, + "-p", + "say mock token", + "--provider", + "commandcode", + "--model", + TEST_MODEL, + ] + try { + console.log("[pi-local] zero data retention opted in from settings.json") + writeFileSync( + settingsFilePath, + JSON.stringify({ ...baseSettings, commandcode: { enableZDR: true } }), + ) + requestCount = 0 + lastRequestHeaders = {} + const settingsZdr = await runPi(zdrArgs, { + timeoutMs: 30_000, + env: { CMD_ZDR: undefined, COMMANDCODE_ZDR: undefined }, + }) + assert.equal(settingsZdr.code, 0, settingsZdr.stderr) + assert.match(settingsZdr.stdout, /mock-pi-ok/) + assert.equal(lastRequestHeaders["x-cmd-zdr"], "1") + + console.log("[pi-local] zero data retention stays off without an opt-in") + writeFileSync( + settingsFilePath, + JSON.stringify({ ...baseSettings, commandcode: { enableZDR: false } }), + ) + requestCount = 0 + lastRequestHeaders = {} + const noZdr = await runPi(zdrArgs, { + timeoutMs: 30_000, + env: { CMD_ZDR: undefined, COMMANDCODE_ZDR: undefined }, + }) + assert.equal(noZdr.code, 0, noZdr.stderr) + assert.match(noZdr.stdout, /mock-pi-ok/) + assert.equal(lastRequestHeaders["x-cmd-zdr"], undefined) + } finally { + writeFileSync(settingsFilePath, JSON.stringify(baseSettings)) + } + console.log("[pi-local] denied Provider API access never retries generate") requestCount = 0 providerUpgradeRequired = true @@ -1470,6 +1525,7 @@ export default async function (pi) { const runtimeCommands = await runRpcExtensionCommands() assert.ok(runtimeCommands.commandNames.includes("commandcode-refresh")) assert.ok(runtimeCommands.commandNames.includes("commandcode-status")) + assert.ok(runtimeCommands.commandNames.includes("commandcode-zdr")) assert.match(runtimeCommands.statusBefore, /source: live/) assert.match(runtimeCommands.refreshNotification, /4 models from live/) assert.match(runtimeCommands.statusAfter, /model count: 4/) diff --git a/tests/test-zdr.ts b/tests/test-zdr.ts new file mode 100644 index 0000000..dcac1e8 --- /dev/null +++ b/tests/test-zdr.ts @@ -0,0 +1,131 @@ +import assert from "node:assert/strict" +import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { describe, it } from "node:test" + +import { + describeZdr, + readZdrSetting, + resolveZdr, + saveZdrSetting, + zdrEnvVar, + zdrHeaders, +} from "../src/zdr.ts" + +async function isolated(run: (path: string, dir: string) => Promise): Promise { + const dir = await mkdtemp(join(tmpdir(), "pi-commandcode-zdr-")) + try { + await run(join(dir, "settings.json"), dir) + } finally { + await rm(dir, { recursive: true, force: true }) + } +} + +describe("zero data retention resolution", () => { + it("accepts only the documented env values and keeps the legacy alias", () => { + assert.equal(zdrEnvVar({ CMD_ZDR: "1" }), "CMD_ZDR") + assert.equal(zdrEnvVar({ COMMANDCODE_ZDR: "1" }), "COMMANDCODE_ZDR") + assert.equal(zdrEnvVar({ CMD_ZDR: "1", COMMANDCODE_ZDR: "1" }), "CMD_ZDR") + for (const value of ["0", "true", "yes", " 1", "1 ", ""]) { + assert.equal(zdrEnvVar({ CMD_ZDR: value }), undefined, `CMD_ZDR=${JSON.stringify(value)}`) + } + }) + + it("reads only a literal boolean true from settings.json", () => { + assert.equal(readZdrSetting({ commandcode: { enableZDR: true } }), true) + assert.equal(readZdrSetting({ commandcode: { enableZDR: false } }), false) + for (const value of ["true", 1, null, {}, []]) { + assert.equal( + readZdrSetting({ commandcode: { enableZDR: value } }), + false, + `enableZDR=${JSON.stringify(value)}`, + ) + } + assert.equal(readZdrSetting({}), false) + assert.equal(readZdrSetting(undefined), false) + assert.equal(readZdrSetting({ commandcode: true }), false) + }) + + it("lets the environment override a settings file that turns ZDR off", () => { + const off = { commandcode: { enableZDR: false } } + assert.deepEqual(resolveZdr(off, { CMD_ZDR: "1" }), { + enabled: true, + source: "env", + envVar: "CMD_ZDR", + }) + assert.deepEqual(resolveZdr({ commandcode: { enableZDR: true } }, { CMD_ZDR: "1" }), { + enabled: true, + source: "env", + envVar: "CMD_ZDR", + }) + }) + + it("resolves settings, off, headers and their descriptions", () => { + const settings = resolveZdr({ commandcode: { enableZDR: true } }, {}) + assert.deepEqual(settings, { enabled: true, source: "settings" }) + assert.deepEqual(zdrHeaders(settings), { "x-cmd-zdr": "1" }) + assert.match(describeZdr(settings), /^on \(settings\.json/) + + const env = resolveZdr({}, { COMMANDCODE_ZDR: "1" }) + assert.equal(describeZdr(env), "on (COMMANDCODE_ZDR)") + + const off = resolveZdr({}, {}) + assert.deepEqual(off, { enabled: false, source: "off" }) + assert.equal(zdrHeaders(off), undefined) + assert.equal(describeZdr(off), "off") + }) +}) + +describe("saving the settings.json opt-in", () => { + it("creates settings.json and keeps every unrelated key", async () => { + await isolated(async (path, dir) => { + const written = await saveZdrSetting(path, true) + assert.equal(resolveZdr(written, {}).enabled, true) + assert.deepEqual(JSON.parse(await readFile(path, "utf8")), { + commandcode: { enableZDR: true }, + }) + assert.equal((await stat(path)).mode & 0o777, 0o600) + + const existing = { + theme: "matte-black", + terminal: { hideImages: false }, + commandcode: { endpoint: "responses" }, + } + await writeFile(path, JSON.stringify(existing), "utf8") + const updated = await saveZdrSetting(path, true) + assert.deepEqual(JSON.parse(await readFile(path, "utf8")), { + ...existing, + commandcode: { endpoint: "responses", enableZDR: true }, + }) + assert.equal(resolveZdr(updated, {}).enabled, true) + + const disabled = await saveZdrSetting(path, false) + assert.equal(resolveZdr(disabled, {}).enabled, false) + assert.deepEqual(JSON.parse(await readFile(path, "utf8")), { + ...existing, + commandcode: { endpoint: "responses", enableZDR: false }, + }) + }) + }) + + it("creates the agent directory when it does not exist", async () => { + await isolated(async (_path, dir) => { + const nested = join(dir, "agent", "settings.json") + await saveZdrSetting(nested, true) + assert.deepEqual(JSON.parse(await readFile(nested, "utf8")), { + commandcode: { enableZDR: true }, + }) + }) + }) + + it("refuses to overwrite a malformed or non-object settings file", async () => { + await isolated(async (path) => { + for (const content of ["{ not json", "[]", "null", '"text"']) { + await writeFile(path, content, "utf8") + await assert.rejects(() => saveZdrSetting(path, true), /settings\.json/) + assert.equal(await readFile(path, "utf8"), content) + } + }) + }) +}) From 116c7cb741a82a48998893eead9bd5a3ab00098a Mon Sep 17 00:00:00 2001 From: Devendra Mishra Date: Wed, 18 Nov 2026 20:40:09 +0530 Subject: [PATCH 2/5] fix(config): address ZDR review feedback - limit /commandcode-zdr on|off to pi; other hosts keep the CMD_ZDR env opt-in - resolve the ZDR header per /commandcode-quota run - update a symlinked settings.json through its target, refuse dangling links - test the next outgoing request after /commandcode-zdr on and off --- README.md | 4 +- index.ts | 11 ++++- src/quota-command.ts | 5 ++- src/zdr.ts | 24 +++++++++- tests/test-pi-local.mjs | 88 +++++++++++++++++++++++++++++++++++++ tests/test-quota-command.ts | 21 +++++++++ tests/test-zdr.ts | 42 +++++++++++++++++- 7 files changed, 187 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e5d412b..87b3e2f 100644 --- a/README.md +++ b/README.md @@ -163,14 +163,14 @@ While pi is running, use these provider commands without restarting: - `/commandcode-refresh` fetches and re-registers the current model catalog, and resets the latest transport status. Every inference attempt uses the Provider API; entitlement denials are not cached. Overlapping refreshes are coalesced, and a failed refresh keeps the last valid catalog active. - `/commandcode-status` shows redacted discovery diagnostics, including the source, model count, timestamps, cache path, endpoint, and warning. Transport status is `unknown` until the latest inference attempt completes successfully; a denial remains `unknown`, not proof of API entitlement. If a Responses reasoning event has an unsupported shape, a separate stream warning is cleared when the next Responses HTTP request starts, before its outcome is known. Requests on Chat Completions or Messages do not clear it. The slot is shared by concurrent Responses requests, so a late warning from an older in-flight response can still replace it. The event is preserved unchanged; no reasoning text is included in the warning. - `/commandcode-diagnostics` shows a shareable support report: plugin/host version, CLI snapshot, selected transport and wire, capability provenance, price status, and the latest request's HTTP status, attempts, outcome, first body-chunk latency and longest gap. It keeps only one request summary in memory. It never collects prompts, tool content, credentials, raw errors, account data or configured local paths. Model identifiers come from the host; recognized path-/credential-shaped values are redacted, but custom names can still identify your setup. Request IDs are included only when they match a bounded hexadecimal/UUID format. Timing is measured between body chunks, not tokens; first-chunk latency may include an error response. A request started most recently owns the report even if another request finishes later. Retry counts are scoped to that stream call, not host-level retries. Review the report before sharing it. -- `/commandcode-zdr` shows the effective zero-data-retention state; `/commandcode-zdr on` or `/commandcode-zdr off` writes it to the agent settings file. The provider re-registers so the change applies to the next request and survives restart. Turning it off is refused while `CMD_ZDR` or `COMMANDCODE_ZDR` is set, because the environment wins. +- `/commandcode-zdr` shows the effective zero-data-retention state; `/commandcode-zdr on` or `/commandcode-zdr off` writes it to the agent settings file. The provider re-registers so the change applies to the next request and survives restart. Saving is supported on pi only; Oh My Pi keeps the `CMD_ZDR` environment opt-in. A symlinked `settings.json` is updated through its target. Turning it off is refused while `CMD_ZDR` or `COMMANDCODE_ZDR` is set, because the environment wins. - `/commandcode-quota` shows your Command Code account usage and quota in a dashboard-style layout: credits remaining and used with a percentage, monthly/purchased/free sources, the current plan, available usage totals, the API key name, and the 5-hour and weekly usage windows. As an explicit exception to the inference policy, the `commandcode-quota` command reads from the internal Command Code alpha usage endpoints (the same ones the `cmd` CLI `/usage` command uses): `whoami`, `billing/credits`, `billing/subscriptions`, and `usage/summary`. It authenticates with the same API key the provider already uses. These requests are GET-only account/usage reads, not an inference fallback. Their success says nothing about Provider API entitlement; the undocumented schemas and availability may change independently. If the command cannot reach those endpoints or an endpoint schema changes, unavailable sections are reported explicitly instead of being displayed as zero usage. Output is plain text (via `ui.notify`) so it works across pi and compatible hosts such as OMP. On Oh My Pi the provider also registers an OMP usage provider through the `usage` field of `pi.registerProvider`, reading the same endpoints with the same credentials. It supplies the account's 5-hour and weekly usage windows, credits remaining and used against the billing-period pool, and the current plan to the host's usage surfaces, and it follows the same rule for sections the API does not report. pi has no such field and ignores it. -Set `CMD_ZDR=1` to send Command Code's documented `x-cmd-zdr: 1` zero-data-retention header. The legacy `COMMANDCODE_ZDR=1` alias remains supported. A settings file is an alternative where exporting an environment variable is inconvenient: add `{ "commandcode": { "enableZDR": true } }` to `settings.json` in the agent directory (`~/.pi/agent/settings.json` for pi), or run `/commandcode-zdr on`. Only a literal `true` enables it, the environment wins over the file, and every other settings key is preserved when the command writes it. The opt-in is sent as a registration header, so an edit made outside the command applies after `/reload` or a restart. Some models have no ZDR-capable upstream yet; a request for those models fails instead of routing through a non-ZDR provider. +Set `CMD_ZDR=1` to send Command Code's documented `x-cmd-zdr: 1` zero-data-retention header. The legacy `COMMANDCODE_ZDR=1` alias remains supported. On pi, a settings file is an alternative where exporting an environment variable is inconvenient: add `{ "commandcode": { "enableZDR": true } }` to `settings.json` in the agent directory (`~/.pi/agent/settings.json` for pi), or run `/commandcode-zdr on`. Only a literal `true` enables it, the environment wins over the file, and every other settings key is preserved when the command writes it. The opt-in is sent as a registration header, so an edit made outside the command applies after `/reload` or a restart. Some models have no ZDR-capable upstream yet; a request for those models fails instead of routing through a non-ZDR provider. The following environment variables are intended for tests, local mocks, and compatible API endpoints: diff --git a/index.ts b/index.ts index 0669d36..52f230c 100644 --- a/index.ts +++ b/index.ts @@ -472,6 +472,15 @@ export default async function (pi: ExtensionAPI) { ctx.ui.notify("Usage: /commandcode-zdr on|off", "warning") return } + // Only pi reads this settings file back and applies a re-registered header + // to the next request; other hosts keep the environment opt-in. + if (!compatApiProviderRegistrar()) { + ctx.ui.notify( + "Saving zero data retention is only supported on pi. Set CMD_ZDR=1 in the environment instead.", + "warning", + ) + return + } // The environment wins at resolution time, so refuse instead of writing a // setting that would not take effect. if (choice === "off" && zdr.source === "env") { @@ -509,7 +518,7 @@ export default async function (pi: ExtensionAPI) { registerCommandCodeQuota(pi, { apiBase: quotaApiBase(apiBase), - headers: zdrHeaders(zdr), + headers: () => zdrHeaders(zdr), }) const runtime = createCommandCodeRuntime(pi, { diff --git a/src/quota-command.ts b/src/quota-command.ts index 598c19f..c231456 100644 --- a/src/quota-command.ts +++ b/src/quota-command.ts @@ -25,7 +25,8 @@ interface QuotaCommandApi { interface RegisterQuotaCommandOptions { apiBase: string - headers?: Record + /** A function is resolved per run so a header changed after startup (ZDR) is sent. */ + headers?: Record | (() => Record | undefined) getConfiguredKey?: () => string | undefined fetchQuota?: typeof fetchCommandCodeQuota } @@ -54,7 +55,7 @@ export function registerCommandCodeQuota( const result = await fetchQuota({ apiKey, baseUrl: options.apiBase, - extraHeaders: options.headers, + extraHeaders: typeof options.headers === "function" ? options.headers() : options.headers, }) if (!result.ok) { ctx.ui.notify(redactValue(result.error.message), "error") diff --git a/src/zdr.ts b/src/zdr.ts index dff066b..b87b623 100644 --- a/src/zdr.ts +++ b/src/zdr.ts @@ -2,7 +2,7 @@ // a process environment variable is inconvenient. Command Code documents the // `x-cmd-zdr: 1` request header and the `CMD_ZDR=1` CLI environment variable. import { randomUUID } from "node:crypto" -import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises" +import { lstat, mkdir, readFile, realpath, rename, rm, writeFile } from "node:fs/promises" import { dirname } from "node:path" /** Top-level settings.json section owned by this provider. */ @@ -57,6 +57,25 @@ export function describeZdr(state: ZdrState): string { return "off" } +/** + * A symlinked settings.json (e.g. a managed file) is updated through its target + * so the link survives; a dangling link is refused rather than replaced. + */ +async function resolveSettingsTarget(path: string): Promise { + try { + return await realpath(path) + } catch (error) { + if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) { + throw new Error("Could not read settings.json in the agent directory; check file permissions") + } + } + const entry = await lstat(path).catch(() => undefined) + if (entry?.isSymbolicLink()) { + throw new Error("settings.json is a symlink to a missing file; refusing to replace the link") + } + return path +} + /** * Persist the opt-in into the agent settings file, preserving every other key. * @@ -67,7 +86,8 @@ export function describeZdr(state: ZdrState): string { * coordinated. A malformed file is never overwritten, and an unreadable file is * reported instead of replaced. */ -export async function saveZdrSetting(path: string, enabled: boolean): Promise { +export async function saveZdrSetting(settingsPath: string, enabled: boolean): Promise { + const path = await resolveSettingsTarget(settingsPath) let raw: string | undefined try { raw = await readFile(path, "utf8") diff --git a/tests/test-pi-local.mjs b/tests/test-pi-local.mjs index 9c4cf05..664b2a2 100644 --- a/tests/test-pi-local.mjs +++ b/tests/test-pi-local.mjs @@ -816,6 +816,91 @@ async function runRpcExtensionCommands(timeoutMs = 30_000) { } } +async function runRpcZdrCommand(timeoutMs = 30_000) { + const child = spawn( + PI_BIN, + [ + "--no-extensions", + "--mode", + "rpc", + "-e", + EXT_PATH, + "--provider", + "commandcode", + "--model", + TEST_MODEL, + ], + { + cwd: PROJECT_DIR, + env: { ...env, CMD_ZDR: undefined, COMMANDCODE_ZDR: undefined }, + stdio: ["pipe", "pipe", "pipe"], + }, + ) + const waitForClose = rpcChildCloseWaiter(child) + let buffer = "" + let stderr = "" + const events = [] + child.stdout.on("data", (chunk) => { + buffer += chunk.toString("utf-8") + const lines = buffer.split("\n") + buffer = lines.pop() ?? "" + for (const line of lines) { + try { + events.push(JSON.parse(line)) + } catch { + // Ignore non-JSON output. + } + } + }) + child.stderr.on("data", (chunk) => { + stderr += chunk.toString("utf-8") + }) + const waitFor = async (predicate, fromIndex = 0) => { + const deadline = Date.now() + timeoutMs + while (Date.now() < deadline) { + const found = events.slice(fromIndex).find(predicate) + if (found) return found + await new Promise((resolve) => setTimeout(resolve, 25)) + } + throw new Error(`RPC event timeout. stderr: ${stderr.slice(-500)}`) + } + const send = (value) => child.stdin.write(`${JSON.stringify(value)}\n`) + const prompt = async (message, until) => { + const start = events.length + send({ id: `p-${start}`, type: "prompt", message }) + await waitFor((event) => event.type === "response" && event.id === `p-${start}`, start) + return waitFor(until, start) + } + // Asserts on the next outgoing HTTP request, not only on the notification. + const nextRequestHeader = async () => { + lastRequestHeaders = {} + await prompt("say mock token", (event) => event.type === "agent_end") + return lastRequestHeaders["x-cmd-zdr"] + } + const command = async (name) => + ( + await prompt( + name, + (event) => + event.type === "extension_ui_request" && + event.method === "notify" && + /zero data retention/.test(event.message ?? ""), + ) + ).message + try { + // Wait for the cached catalog so the command can re-register. + await prompt("/commandcode-status", (event) => /model count: 3/.test(event.message ?? "")) + assert.equal(await nextRequestHeader(), undefined) + assert.match(await command("/commandcode-zdr on"), /on \(settings\.json/) + assert.equal(await nextRequestHeader(), "1") + assert.match(await command("/commandcode-zdr off"), /off/) + assert.equal(await nextRequestHeader(), undefined) + } finally { + child.kill() + await waitForClose() + } +} + async function runRpcCompatCall(timeoutMs = 30_000) { const child = spawn( PI_BIN, @@ -1295,6 +1380,9 @@ export default async function (pi) { assert.equal(noZdr.code, 0, noZdr.stderr) assert.match(noZdr.stdout, /mock-pi-ok/) assert.equal(lastRequestHeaders["x-cmd-zdr"], undefined) + + console.log("[pi-local] /commandcode-zdr changes the next outgoing request") + await runRpcZdrCommand() } finally { writeFileSync(settingsFilePath, JSON.stringify(baseSettings)) } diff --git a/tests/test-quota-command.ts b/tests/test-quota-command.ts index 3ae753c..c672699 100644 --- a/tests/test-quota-command.ts +++ b/tests/test-quota-command.ts @@ -77,6 +77,27 @@ describe("commandcode-quota command", () => { assert.match(ctx.notifications.at(-1)?.message ?? "", /Requests: 2/) }) + it("resolves headers at run time so a later change is sent", async () => { + const pi = new CommandApiDouble() + let headers: Record | undefined + const seen: Array | undefined> = [] + registerCommandCodeQuota(pi, { + apiBase: "https://api.commandcode.ai", + headers: () => headers, + getConfiguredKey: () => "key", + fetchQuota: async (options) => { + seen.push(options.extraHeaders) + return quotaResult + }, + }) + + assert.ok(pi.handler) + await pi.handler("", context(undefined).value) + headers = { "x-cmd-zdr": "1" } + await pi.handler("", context(undefined).value) + assert.deepEqual(seen, [undefined, { "x-cmd-zdr": "1" }]) + }) + it("warns without calling the endpoint when no API key is available", async () => { const pi = new CommandApiDouble() let called = false diff --git a/tests/test-zdr.ts b/tests/test-zdr.ts index dcac1e8..52ad308 100644 --- a/tests/test-zdr.ts +++ b/tests/test-zdr.ts @@ -1,5 +1,15 @@ import assert from "node:assert/strict" -import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises" +import { + lstat, + mkdir, + mkdtemp, + readFile, + readlink, + rm, + stat, + symlink, + writeFile, +} from "node:fs/promises" import { tmpdir } from "node:os" import { join } from "node:path" import { describe, it } from "node:test" @@ -128,4 +138,34 @@ describe("saving the settings.json opt-in", () => { } }) }) + + it("updates the target of a symlinked settings file and keeps the link", async () => { + await isolated(async (path, dir) => { + await mkdir(join(dir, "managed")) + const target = join(dir, "managed", "settings.json") + await writeFile(target, JSON.stringify({ theme: "dark" }), "utf8") + await symlink(target, path) + + await saveZdrSetting(path, true) + + assert.equal((await lstat(path)).isSymbolicLink(), true) + assert.equal(await readlink(path), target) + assert.deepEqual(JSON.parse(await readFile(target, "utf8")), { + theme: "dark", + commandcode: { enableZDR: true }, + }) + }) + }) + + it("refuses to replace a dangling settings symlink", async () => { + await isolated(async (path, dir) => { + const target = join(dir, "missing.json") + await symlink(target, path) + + await assert.rejects(() => saveZdrSetting(path, true), /symlink/) + + assert.equal((await lstat(path)).isSymbolicLink(), true) + await assert.rejects(() => stat(target)) + }) + }) }) From 32e44e10f255b951ca8c865528d1a6542ad1badf Mon Sep 17 00:00:00 2001 From: Devendra Mishra Date: Thu, 19 Nov 2026 09:08:17 +0530 Subject: [PATCH 3/5] fix(config): read the ZDR opt-in from the agent settings file - read the same agent-directory settings.json the command writes instead of the host's merged global+project view, so a project override cannot turn the persisted opt-in off after restart - treat a missing or malformed settings file as off during startup; the write path still refuses to overwrite it - add a restart regression with a conflicting trusted project setting - document that project .pi/settings.json is not consulted for this opt-in --- README.md | 2 +- index.ts | 45 +++++++++++++++++++---------------------- src/zdr.ts | 17 ++++++++++++++++ tests/test-pi-local.mjs | 37 ++++++++++++++++++++++++++++++++- tests/test-zdr.ts | 27 +++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 87b3e2f..3f5f20d 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ As an explicit exception to the inference policy, the `commandcode-quota` comman On Oh My Pi the provider also registers an OMP usage provider through the `usage` field of `pi.registerProvider`, reading the same endpoints with the same credentials. It supplies the account's 5-hour and weekly usage windows, credits remaining and used against the billing-period pool, and the current plan to the host's usage surfaces, and it follows the same rule for sections the API does not report. pi has no such field and ignores it. -Set `CMD_ZDR=1` to send Command Code's documented `x-cmd-zdr: 1` zero-data-retention header. The legacy `COMMANDCODE_ZDR=1` alias remains supported. On pi, a settings file is an alternative where exporting an environment variable is inconvenient: add `{ "commandcode": { "enableZDR": true } }` to `settings.json` in the agent directory (`~/.pi/agent/settings.json` for pi), or run `/commandcode-zdr on`. Only a literal `true` enables it, the environment wins over the file, and every other settings key is preserved when the command writes it. The opt-in is sent as a registration header, so an edit made outside the command applies after `/reload` or a restart. Some models have no ZDR-capable upstream yet; a request for those models fails instead of routing through a non-ZDR provider. +Set `CMD_ZDR=1` to send Command Code's documented `x-cmd-zdr: 1` zero-data-retention header. The legacy `COMMANDCODE_ZDR=1` alias remains supported. On pi, a settings file is an alternative where exporting an environment variable is inconvenient: add `{ "commandcode": { "enableZDR": true } }` to `settings.json` in the agent directory (`~/.pi/agent/settings.json` for pi), or run `/commandcode-zdr on`. The provider reads that agent-directory file directly, so a project `.pi/settings.json` cannot turn the opt-in on or off; the environment takes precedence over the file. Only a literal `true` enables it, and every other settings key is preserved when the command writes it. The opt-in is sent as a registration header, so an edit made outside the command applies after `/reload` or a restart. Some models have no ZDR-capable upstream yet; a request for those models fails instead of routing through a non-ZDR provider. The following environment variables are intended for tests, local mocks, and compatible API endpoints: diff --git a/index.ts b/index.ts index 52f230c..437a80e 100644 --- a/index.ts +++ b/index.ts @@ -49,7 +49,14 @@ import { registerCommandCodeQuota } from "./src/quota-command.ts" import { normalizeCommandCodeResponsesResponse } from "./src/responses-stream.ts" import { modelWireTraits, withResponsesWireRepairs } from "./src/responses-compat.ts" import { createCommandCodeRuntime } from "./src/runtime.ts" -import { describeZdr, resolveZdr, saveZdrSetting, zdrHeaders, type ZdrState } from "./src/zdr.ts" +import { + describeZdr, + loadZdrSettings, + resolveZdr, + saveZdrSetting, + zdrHeaders, + type ZdrState, +} from "./src/zdr.ts" import { compatForEndpoint, endpointChoiceForModel, @@ -113,20 +120,6 @@ function providerApiKey(): string | undefined { return compatApiProviderRegistrar() ? "$COMMAND_CODE_API_KEY" : undefined } -/** - * Settings live on the host. Oh My Pi has no settings accessor, so a missing - * reader means "no settings section" rather than an error. - */ -function hostSettings(pi: ExtensionAPI): unknown { - const readSettings = (pi as { getSettings?: unknown }).getSettings - if (typeof readSettings !== "function") return undefined - try { - return readSettings.call(pi) - } catch { - return undefined - } -} - /** * The registered config, plus Oh My Pi's extension-only usage hook. * @@ -272,9 +265,9 @@ export default async function (pi: ExtensionAPI) { // Hosts may select their built-in Command Code model before loading extensions. // Rebind that stale selection to our registered transport before the first turn. pi.on("session_start", async (_event, ctx) => { - // Completes the ZDR opt-in now that host settings are readable, and picks up - // a settings file changed since the last load. - applyHostZdr() + // Applies the opt-in from the agent settings file and picks up a file + // changed since the last load. + await applyHostZdr() if (ctx.model?.provider !== "commandcode") return const registered = ctx.modelRegistry.find("commandcode", ctx.model.id) if (registered?.api === COMMAND_CODE_API && ctx.model.api !== COMMAND_CODE_API) { @@ -292,10 +285,11 @@ export default async function (pi: ExtensionAPI) { // the wire each model answers on is resolved here from the catalog that // `createProviderConfig` registered. Unknown ids fall back to the id rule. const catalogModels = new Map() - // The environment is readable while extensions load, host settings are not, so - // the opt-in starts from the environment and is completed at session start. - // The environment is fixed for the process; settings only reload with the - // extension. /commandcode-zdr updates this state after writing the file. + // The environment is readable while extensions load; the agent settings file + // is read at session start so a change made while the host was stopped applies + // before the first request. The environment is fixed for the process, and the + // file only reloads with the extension. /commandcode-zdr updates this state + // after writing the file. const settingsPath = join(getAgentDir(), "settings.json") let zdr: ZdrState = resolveZdr(undefined) const routingPath = join(getAgentDir(), "commandcode-routing.json") @@ -439,8 +433,11 @@ export default async function (pi: ExtensionAPI) { "commandcode", createProviderConfig([...catalogModels.values()], apiBase, measuredStream, zdrHeaders(zdr)), ) - const applyHostZdr = () => { - const resolved = resolveZdr(hostSettings(pi)) + const applyHostZdr = async () => { + // Read the agent settings file, not the host's merged global+project view: + // the command writes this file, so a project override must not be able to + // invalidate the persisted opt-in at restart. + const resolved = resolveZdr(await loadZdrSettings(settingsPath)) const changed = resolved.enabled !== zdr.enabled zdr = resolved // Re-registration is the only way to change a registration header, and it diff --git a/src/zdr.ts b/src/zdr.ts index b87b623..e882216 100644 --- a/src/zdr.ts +++ b/src/zdr.ts @@ -47,6 +47,23 @@ export function resolveZdr(settings: unknown, env: Environment = process.env): Z return { enabled: false, source: "off" } } +/** + * Read the opt-in from the agent settings file itself. Startup uses this instead + * of the host's merged global+project view: the opt-in is owned by the agent + * file that saveZdrSetting writes, so a project settings override cannot + * silently turn it off (or on) after a restart. + * + * A missing or unreadable file is "off"; a malformed file is left for the write + * path to report rather than failing the provider load. + */ +export async function loadZdrSettings(path: string): Promise { + try { + return JSON.parse(await readFile(path, "utf8")) + } catch { + return undefined + } +} + export function zdrHeaders(state: ZdrState): Record | undefined { return state.enabled ? { "x-cmd-zdr": "1" } : undefined } diff --git a/tests/test-pi-local.mjs b/tests/test-pi-local.mjs index 664b2a2..d55341b 100644 --- a/tests/test-pi-local.mjs +++ b/tests/test-pi-local.mjs @@ -431,7 +431,7 @@ function runPi(args, timeoutOrOptions = 30_000) { } return new Promise((resolve) => { const child = spawn(PI_BIN, args, { - cwd: PROJECT_DIR, + cwd: options.cwd ?? PROJECT_DIR, env: childEnv, stdio: ["ignore", "pipe", "pipe"], }) @@ -1350,6 +1350,7 @@ export default async function (pi) { "--model", TEST_MODEL, ] + let zdrProject try { console.log("[pi-local] zero data retention opted in from settings.json") writeFileSync( @@ -1381,9 +1382,43 @@ export default async function (pi) { assert.match(noZdr.stdout, /mock-pi-ok/) assert.equal(lastRequestHeaders["x-cmd-zdr"], undefined) + console.log( + "[pi-local] zero data retention follows the agent settings file over a project override", + ) + zdrProject = mkdtempSync(join(tempHome, "zdr-project-")) + mkdirSync(join(zdrProject, ".pi"), { recursive: true }) + writeFileSync( + join(zdrProject, ".pi", "settings.json"), + JSON.stringify({ commandcode: { enableZDR: false } }), + ) + writeFileSync( + settingsFilePath, + JSON.stringify({ + ...baseSettings, + defaultProjectTrust: "always", + commandcode: { enableZDR: true }, + }), + ) + requestCount = 0 + lastRequestHeaders = {} + const projectOverride = await runPi(zdrArgs, { + timeoutMs: 30_000, + cwd: zdrProject, + env: { CMD_ZDR: undefined, COMMANDCODE_ZDR: undefined }, + }) + assert.equal(projectOverride.code, 0, projectOverride.stderr) + assert.match(projectOverride.stdout, /mock-pi-ok/) + assert.equal( + lastRequestHeaders["x-cmd-zdr"], + "1", + "a trusted project settings override must not turn off the persisted opt-in", + ) + console.log("[pi-local] /commandcode-zdr changes the next outgoing request") + writeFileSync(settingsFilePath, JSON.stringify(baseSettings)) await runRpcZdrCommand() } finally { + if (zdrProject) rmSync(zdrProject, { recursive: true, force: true }) writeFileSync(settingsFilePath, JSON.stringify(baseSettings)) } diff --git a/tests/test-zdr.ts b/tests/test-zdr.ts index 52ad308..2de5b70 100644 --- a/tests/test-zdr.ts +++ b/tests/test-zdr.ts @@ -16,6 +16,7 @@ import { describe, it } from "node:test" import { describeZdr, + loadZdrSettings, readZdrSetting, resolveZdr, saveZdrSetting, @@ -87,6 +88,32 @@ describe("zero data retention resolution", () => { }) }) +describe("loading the settings.json opt-in", () => { + it("reads the agent settings file directly and ignores a missing one", async () => { + await isolated(async (path) => { + assert.equal(await loadZdrSettings(path), undefined) + + await writeFile(path, JSON.stringify({ commandcode: { enableZDR: true } }), "utf8") + assert.equal(resolveZdr(await loadZdrSettings(path), {}).enabled, true) + + await writeFile(path, JSON.stringify({ commandcode: { enableZDR: false } }), "utf8") + assert.equal(resolveZdr(await loadZdrSettings(path), {}).enabled, false) + }) + }) + + it("follows a settings symlink and treats malformed content as off", async () => { + await isolated(async (path, dir) => { + const target = join(dir, "managed.json") + await writeFile(target, JSON.stringify({ commandcode: { enableZDR: true } }), "utf8") + await symlink(target, path) + assert.equal(resolveZdr(await loadZdrSettings(path), {}).enabled, true) + + await writeFile(target, "{ not json", "utf8") + assert.equal(await loadZdrSettings(path), undefined) + }) + }) +}) + describe("saving the settings.json opt-in", () => { it("creates settings.json and keeps every unrelated key", async () => { await isolated(async (path, dir) => { From 2a84bbee663b5f8ab5f4bc2369c0d401f59077ae Mon Sep 17 00:00:00 2001 From: Devendra Mishra Date: Thu, 19 Nov 2026 20:37:41 +0530 Subject: [PATCH 4/5] fix(config): keep the ZDR settings file pi-only and BOM-tolerant - gate the startup settings.json read behind the same pi-only probe as the /commandcode-zdr write, so OMP keeps the environment-only contract and a leftover settings.json cannot turn ZDR on there; CMD_ZDR=1 still applies - strip one leading UTF-8 BOM in both the reader and the writer so the host's BOM-tolerant settings parser cannot silently drop the saved opt-in, while a malformed file is still refused on write - add unit regressions for BOM handling and an OMP regression that a leftover settings.json is ignored and the environment opt-in still works --- README.md | 2 +- index.ts | 18 +++++++++++++++--- src/zdr.ts | 15 ++++++++++++--- tests/test-omp-compat.mjs | 32 ++++++++++++++++++++++++++++++- tests/test-zdr.ts | 40 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3f5f20d..dcc7653 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ As an explicit exception to the inference policy, the `commandcode-quota` comman On Oh My Pi the provider also registers an OMP usage provider through the `usage` field of `pi.registerProvider`, reading the same endpoints with the same credentials. It supplies the account's 5-hour and weekly usage windows, credits remaining and used against the billing-period pool, and the current plan to the host's usage surfaces, and it follows the same rule for sections the API does not report. pi has no such field and ignores it. -Set `CMD_ZDR=1` to send Command Code's documented `x-cmd-zdr: 1` zero-data-retention header. The legacy `COMMANDCODE_ZDR=1` alias remains supported. On pi, a settings file is an alternative where exporting an environment variable is inconvenient: add `{ "commandcode": { "enableZDR": true } }` to `settings.json` in the agent directory (`~/.pi/agent/settings.json` for pi), or run `/commandcode-zdr on`. The provider reads that agent-directory file directly, so a project `.pi/settings.json` cannot turn the opt-in on or off; the environment takes precedence over the file. Only a literal `true` enables it, and every other settings key is preserved when the command writes it. The opt-in is sent as a registration header, so an edit made outside the command applies after `/reload` or a restart. Some models have no ZDR-capable upstream yet; a request for those models fails instead of routing through a non-ZDR provider. +Set `CMD_ZDR=1` to send Command Code's documented `x-cmd-zdr: 1` zero-data-retention header. The legacy `COMMANDCODE_ZDR=1` alias remains supported. On pi, a settings file is an alternative where exporting an environment variable is inconvenient: add `{ "commandcode": { "enableZDR": true } }` to `settings.json` in the agent directory (`~/.pi/agent/settings.json` for pi), or run `/commandcode-zdr on`. The provider reads that agent-directory file directly, so a project `.pi/settings.json` cannot turn the opt-in on or off; the environment takes precedence over the file. Oh My Pi ignores that file and keeps the environment opt-in only, so a settings file shared with a pi install cannot switch protection on there. A single leading UTF-8 BOM in the file is tolerated. Only a literal `true` enables it, and every other settings key is preserved when the command writes it. The opt-in is sent as a registration header, so an edit made outside the command applies after `/reload` or a restart. Some models have no ZDR-capable upstream yet; a request for those models fails instead of routing through a non-ZDR provider. The following environment variables are intended for tests, local mocks, and compatible API endpoints: diff --git a/index.ts b/index.ts index 437a80e..b0e166d 100644 --- a/index.ts +++ b/index.ts @@ -89,6 +89,15 @@ function compatApiProviderRegistrar(): ((...args: unknown[]) => unknown) | undef return typeof register === "function" ? (register as (...args: unknown[]) => unknown) : undefined } +/** + * The settings.json opt-in is pi-only. pi registers the compat API provider that + * applies a re-registered header; Oh My Pi does not, so its contract stays the + * environment only. Both paths use the same probe as the write guard. + */ +function settingsZdrSupported(): boolean { + return compatApiProviderRegistrar() !== undefined +} + function registerCompatApiProvider(stream: CompatStreamFunction): void { compatApiProviderRegistrar()?.( { api: COMMAND_CODE_API, stream, streamSimple: stream }, @@ -436,8 +445,11 @@ export default async function (pi: ExtensionAPI) { const applyHostZdr = async () => { // Read the agent settings file, not the host's merged global+project view: // the command writes this file, so a project override must not be able to - // invalidate the persisted opt-in at restart. - const resolved = resolveZdr(await loadZdrSettings(settingsPath)) + // invalidate the persisted opt-in at restart. Only pi owns this file; OMP + // must not pick up a leftover one, so it keeps the environment opt-in. + const resolved = resolveZdr( + settingsZdrSupported() ? await loadZdrSettings(settingsPath) : undefined, + ) const changed = resolved.enabled !== zdr.enabled zdr = resolved // Re-registration is the only way to change a registration header, and it @@ -471,7 +483,7 @@ export default async function (pi: ExtensionAPI) { } // Only pi reads this settings file back and applies a re-registered header // to the next request; other hosts keep the environment opt-in. - if (!compatApiProviderRegistrar()) { + if (!settingsZdrSupported()) { ctx.ui.notify( "Saving zero data retention is only supported on pi. Set CMD_ZDR=1 in the environment instead.", "warning", diff --git a/src/zdr.ts b/src/zdr.ts index e882216..6d1f089 100644 --- a/src/zdr.ts +++ b/src/zdr.ts @@ -25,6 +25,14 @@ function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value) } +/** + * The host's own settings parser tolerates a leading UTF-8 BOM, so both the + * reader and the writer do too: a BOM must not silently drop a saved opt-in. + */ +function stripBom(text: string): string { + return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text +} + /** Only a literal `true` enables ZDR, so a typo cannot silently weaken privacy. */ export function readZdrSetting(settings: unknown): boolean { if (!isRecord(settings)) return false @@ -58,7 +66,7 @@ export function resolveZdr(settings: unknown, env: Environment = process.env): Z */ export async function loadZdrSettings(path: string): Promise { try { - return JSON.parse(await readFile(path, "utf8")) + return JSON.parse(stripBom(await readFile(path, "utf8"))) } catch { return undefined } @@ -115,10 +123,11 @@ export async function saveZdrSetting(settingsPath: string, enabled: boolean): Pr } let settings: Record = {} - if (raw !== undefined && raw.trim() !== "") { + const text = raw === undefined ? undefined : stripBom(raw) + if (text !== undefined && text.trim() !== "") { let parsed: unknown try { - parsed = JSON.parse(raw) + parsed = JSON.parse(text) } catch { throw new Error( "Invalid settings.json in the agent directory; refusing to overwrite it. Fix the JSON or set commandcode.enableZDR by hand.", diff --git a/tests/test-omp-compat.mjs b/tests/test-omp-compat.mjs index fff2e25..2082221 100644 --- a/tests/test-omp-compat.mjs +++ b/tests/test-omp-compat.mjs @@ -10,7 +10,7 @@ import assert from "node:assert/strict" import { spawn, spawnSync } from "node:child_process" -import { accessSync, constants, mkdtempSync, rmSync } from "node:fs" +import { accessSync, constants, mkdtempSync, rmSync, writeFileSync } from "node:fs" import { createServer } from "node:http" import { tmpdir } from "node:os" import { delimiter, dirname, join, resolve } from "node:path" @@ -346,6 +346,36 @@ try { assert.equal(lastRequestBody?.model, TEST_MODEL) assert.ok(Array.isArray(lastRequestBody?.messages)) + // The settings.json opt-in is pi-only. OMP keeps the environment contract, so + // a settings.json left behind by a pi install must not turn ZDR on there; + // CMD_ZDR=1 stays the control that still works. + console.log("[omp-compat] a leftover settings.json does not enable ZDR") + const zdrOffEnv = { CMD_ZDR: undefined, COMMANDCODE_ZDR: undefined } + writeFileSync( + join(agentDir, "settings.json"), + JSON.stringify({ commandcode: { enableZDR: true } }), + "utf8", + ) + requestCount = 0 + skillDescriptionRequestCount = 0 + lastRequestHeaders = {} + const leftoverSettings = await runOmp(chatArgs, { env: zdrOffEnv }) + assert.equal(leftoverSettings.code, 0, leftoverSettings.stderr) + assert.match(leftoverSettings.stdout, /mock-omp-ok/) + assert.equal(requestCount, 1) + assert.equal(lastRequestHeaders["x-cmd-zdr"], undefined) + + console.log("[omp-compat] CMD_ZDR=1 still enables ZDR on OMP") + requestCount = 0 + skillDescriptionRequestCount = 0 + lastRequestHeaders = {} + const envZdr = await runOmp(chatArgs, { env: { ...zdrOffEnv, CMD_ZDR: "1" } }) + assert.equal(envZdr.code, 0, envZdr.stderr) + assert.match(envZdr.stdout, /mock-omp-ok/) + assert.equal(requestCount, 1) + assert.equal(lastRequestHeaders["x-cmd-zdr"], "1") + rmSync(join(agentDir, "settings.json"), { force: true }) + // OMP stores `/login` credentials in agent.db and consults them only when // the extension does not install a config API key. main registered the // unresolved `$COMMAND_CODE_API_KEY` placeholder, which OMP kept as a diff --git a/tests/test-zdr.ts b/tests/test-zdr.ts index 2de5b70..e65aeb0 100644 --- a/tests/test-zdr.ts +++ b/tests/test-zdr.ts @@ -112,6 +112,17 @@ describe("loading the settings.json opt-in", () => { assert.equal(await loadZdrSettings(path), undefined) }) }) + + it("tolerates a single leading UTF-8 BOM", async () => { + await isolated(async (path) => { + await writeFile(path, `\uFEFF${JSON.stringify({ commandcode: { enableZDR: true } })}`, "utf8") + assert.equal(resolveZdr(await loadZdrSettings(path), {}).enabled, true) + + // Only one BOM is stripped; anything else remains malformed. + await writeFile(path, "\uFEFF\uFEFF{}", "utf8") + assert.equal(await loadZdrSettings(path), undefined) + }) + }) }) describe("saving the settings.json opt-in", () => { @@ -146,6 +157,35 @@ describe("saving the settings.json opt-in", () => { }) }) + it("keeps a BOM-prefixed settings file readable when it writes it", async () => { + await isolated(async (path) => { + const existing = { theme: "matte-black", commandcode: { endpoint: "responses" } } + await writeFile(path, `\uFEFF${JSON.stringify(existing)}`, "utf8") + const written = await saveZdrSetting(path, true) + assert.equal(resolveZdr(written, {}).enabled, true) + const text = await readFile(path, "utf8") + assert.equal(text.charCodeAt(0), "{".charCodeAt(0), "the rewrite must not keep a BOM") + assert.deepEqual(JSON.parse(text), { + ...existing, + commandcode: { endpoint: "responses", enableZDR: true }, + }) + + // A BOM-prefixed file that is still malformed must be refused, not replaced. + const malformed = "\uFEFF{ not json" + await writeFile(path, malformed, "utf8") + await assert.rejects(() => saveZdrSetting(path, true), /settings\.json/) + assert.equal(await readFile(path, "utf8"), malformed) + + // A file holding only a BOM is empty, not malformed. + await writeFile(path, "\uFEFF", "utf8") + const created = await saveZdrSetting(path, true) + assert.equal(resolveZdr(created, {}).enabled, true) + assert.deepEqual(JSON.parse(await readFile(path, "utf8")), { + commandcode: { enableZDR: true }, + }) + }) + }) + it("creates the agent directory when it does not exist", async () => { await isolated(async (_path, dir) => { const nested = join(dir, "agent", "settings.json") From 9f22b73ecfb8779015953d7062eeacb4dcd89332 Mon Sep 17 00:00:00 2001 From: Devendra Mishra Date: Fri, 20 Nov 2026 09:11:27 +0530 Subject: [PATCH 5/5] test(omp): initialize chat args before the ZDR cases - move the shared chatArgs above the print-mode test and reuse it there and in both ZDR calls, fixing the ReferenceError when OMP is installed - create a native config.yml before the leftover settings.json fixture so OMP cannot migrate the legacy file away, and assert the file survived so the test fails on the buggy head for the right reason --- tests/test-omp-compat.mjs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/test-omp-compat.mjs b/tests/test-omp-compat.mjs index 2082221..a350b93 100644 --- a/tests/test-omp-compat.mjs +++ b/tests/test-omp-compat.mjs @@ -10,7 +10,7 @@ import assert from "node:assert/strict" import { spawn, spawnSync } from "node:child_process" -import { accessSync, constants, mkdtempSync, rmSync, writeFileSync } from "node:fs" +import { accessSync, constants, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" import { createServer } from "node:http" import { tmpdir } from "node:os" import { delimiter, dirname, join, resolve } from "node:path" @@ -324,13 +324,11 @@ try { assert.doesNotMatch(result.stdout + result.stderr, /Failed to load extension/) console.log("[omp-compat] print mode through real extension and mock API") + const chatArgs = ["-e", EXT_PATH, "-p", "say mock token", "--model", `commandcode/${TEST_MODEL}`] requestCount = 0 skillDescriptionRequestCount = 0 requestBodies = [] - const print = await runOmp( - ["-e", EXT_PATH, "-p", "say mock token", "--model", `commandcode/${TEST_MODEL}`], - 30_000, - ) + const print = await runOmp(chatArgs, 30_000) assert.equal(print.code, 0, print.stderr) assert.match(print.stdout, /mock-omp-ok/) assert.equal( @@ -351,6 +349,9 @@ try { // CMD_ZDR=1 stays the control that still works. console.log("[omp-compat] a leftover settings.json does not enable ZDR") const zdrOffEnv = { CMD_ZDR: undefined, COMMANDCODE_ZDR: undefined } + // A native OMP config keeps OMP from migrating the legacy settings.json out + // of the way, so the leftover file really is there when OMP starts. + writeFileSync(join(agentDir, "config.yml"), "{}\n", { flag: "wx" }) writeFileSync( join(agentDir, "settings.json"), JSON.stringify({ commandcode: { enableZDR: true } }), @@ -363,6 +364,11 @@ try { assert.equal(leftoverSettings.code, 0, leftoverSettings.stderr) assert.match(leftoverSettings.stdout, /mock-omp-ok/) assert.equal(requestCount, 1) + assert.equal( + JSON.parse(readFileSync(join(agentDir, "settings.json"), "utf8")).commandcode.enableZDR, + true, + "the leftover settings.json must still be present to prove OMP ignored it", + ) assert.equal(lastRequestHeaders["x-cmd-zdr"], undefined) console.log("[omp-compat] CMD_ZDR=1 still enables ZDR on OMP") @@ -381,7 +387,6 @@ try { // unresolved `$COMMAND_CODE_API_KEY` placeholder, which OMP kept as a // literal config override and sent as the Bearer token, so stored // credentials never reached the request (401). - const chatArgs = ["-e", EXT_PATH, "-p", "say mock token", "--model", `commandcode/${TEST_MODEL}`] const noEnvKey = { COMMAND_CODE_API_KEY: undefined, COMMANDCODE_API_KEY: undefined } console.log("[omp-compat] stored /login OAuth credential is used when no env key exists")