diff --git a/README.md b/README.md index 10dd46f..dcc7653 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. 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. +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 fc63378..b0e166d 100644 --- a/index.ts +++ b/index.ts @@ -49,6 +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, + loadZdrSettings, + resolveZdr, + saveZdrSetting, + zdrHeaders, + type ZdrState, +} from "./src/zdr.ts" import { compatForEndpoint, endpointChoiceForModel, @@ -81,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 }, @@ -112,13 +129,6 @@ 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" } - } - return undefined -} - /** * The registered config, plus Oh My Pi's extension-only usage hook. * @@ -181,19 +191,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 +230,7 @@ function createProviderConfig( }, contextWindow: model.contextWindow, maxTokens: model.maxTokens, - headers, + headers: registeredHeaders, compat: { ...compatForModel(model), commandCodeWire: model.api }, })), } @@ -260,6 +274,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) => { + // 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) { @@ -277,6 +294,13 @@ 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; 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") let routing: EndpointOverrides = {} let routingWarning: string | undefined @@ -413,15 +437,97 @@ 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 = 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. 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 + // 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 + } + // 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 (!settingsZdrSupported()) { + 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") { + 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 +544,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 +579,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/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 new file mode 100644 index 0000000..6d1f089 --- /dev/null +++ b/src/zdr.ts @@ -0,0 +1,163 @@ +// 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 { 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. */ +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) +} + +/** + * 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 + 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" } +} + +/** + * 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(stripBom(await readFile(path, "utf8"))) + } catch { + return undefined + } +} + +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" +} + +/** + * 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. + * + * 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(settingsPath: string, enabled: boolean): Promise { + const path = await resolveSettingsTarget(settingsPath) + 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 = {} + const text = raw === undefined ? undefined : stripBom(raw) + if (text !== undefined && text.trim() !== "") { + let parsed: unknown + try { + 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.", + ) + } + 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-omp-compat.mjs b/tests/test-omp-compat.mjs index fff2e25..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 } 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( @@ -346,12 +344,49 @@ 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 } + // 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 } }), + "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( + 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") + 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 // 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") diff --git a/tests/test-pi-local.mjs b/tests/test-pi-local.mjs index ef6ad8d..d55341b 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" @@ -423,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"], }) @@ -808,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, @@ -1244,6 +1337,91 @@ 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, + ] + let zdrProject + 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) + + 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)) + } + console.log("[pi-local] denied Provider API access never retries generate") requestCount = 0 providerUpgradeRequired = true @@ -1470,6 +1648,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-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 new file mode 100644 index 0000000..e65aeb0 --- /dev/null +++ b/tests/test-zdr.ts @@ -0,0 +1,238 @@ +import assert from "node:assert/strict" +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" + +import { + describeZdr, + loadZdrSettings, + 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("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) + }) + }) + + 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", () => { + 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("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") + 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) + } + }) + }) + + 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)) + }) + }) +})