From adb0efd92cab3212bfba781cd4b8c57316b0fd71 Mon Sep 17 00:00:00 2001 From: rebuildup Date: Thu, 26 Nov 2026 14:36:30 +0900 Subject: [PATCH 1/2] feat(ocr): switch default provider to MiniMax M Plan OCR's built-in minimax provider (api.minimax.io/v1, openai protocol) replaces opencode-go as the default, with MiniMax-M3.1-Flash per config/agents.toml [providers.minimax]. The ocr-go-key bridge becomes the provider-agnostic ocr-key, mapping provider names to Infisical secrets; opencode-go remains selectable with no automatic fallback. Closes #243 Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- docs/open-code-review.md | 12 +++++----- home/.local/bin/ocr-go-key | 3 --- home/.local/bin/ocr-go-key.cmd | 4 ---- home/.local/bin/ocr-key | 3 +++ home/.local/bin/ocr-key.cmd | 4 ++++ home/.opencodereview/config.json | 11 ++++++--- script/{ocr-go-key.ts => ocr-key.ts} | 35 ++++++++++++++++++++++------ script/shell-files.txt | 2 +- 8 files changed, 50 insertions(+), 24 deletions(-) delete mode 100755 home/.local/bin/ocr-go-key delete mode 100644 home/.local/bin/ocr-go-key.cmd create mode 100755 home/.local/bin/ocr-key create mode 100644 home/.local/bin/ocr-key.cmd rename script/{ocr-go-key.ts => ocr-key.ts} (57%) diff --git a/docs/open-code-review.md b/docs/open-code-review.md index e4e266a9..d1b2151d 100644 --- a/docs/open-code-review.md +++ b/docs/open-code-review.md @@ -1,11 +1,11 @@ -# Open Code Review — OpenCode Go / MiMo +# Open Code Review — MiniMax M Plan (default) / OpenCode Go (opt-in) `pc-setup` installs the `ocr` binary (official GitHub Release via mise). `dotfiles` owns only the user configuration and the credential bridge. - Config: `home/.opencodereview/config.json` → `~/.opencodereview/config.json` via `script/link` (POSIX) or `script/link.ps1 -Apply` (Windows). -- Provider: custom `opencode-go`, protocol `openai`, base URL `https://opencode.ai/inference/go/v1`, model `mimo-v2.6-flash` (OpenAI-compatible chat completions). -- Session: `extra_headers` sends `x-opencode-session: {ocr_session_key}`. OpenCode Go rejects any request without that header (HTTP 400 `MissingSessionID`); OCR expands the template to a stable per-conversation key, falling back to a generated key for session-less calls such as `ocr llm test`. -- Credential: `api_key_cmd = "ocr-go-key"`, resolved by OCR's shell from `PATH`. POSIX and Windows launchers in `home/.local/bin/` run `script/ocr-go-key.ts` with Bun. That script strips an ambient `OPENCODE_API_KEY`, then invokes the existing self-hosted Infisical `script/with-secrets` / `script/with-secrets.ps1` boundary and prints only the freshly injected key. No secret value is stored in Git or OCR config. +- Default provider: built-in `minimax`, model `MiniMax-M3.1-Flash`. Auth and model selection follow `config/agents.toml` `[providers.minimax]` (`MINIMAX_M_PLAN_KEY` secret, `MiniMax-M3.1-Flash` default). The built-in preset targets `https://api.minimax.io/v1` with the OpenAI protocol; the M Plan key was verified live against it on 2026-11-26. +- OpenCode Go stays selectable: `custom_providers.opencode-go` keeps the `https://opencode.ai/inference/go/v1` endpoint, `mimo-v2.6-flash`, and the `x-opencode-session: {ocr_session_key}` header (OpenCode Go rejects requests without that header, HTTP 400 `MissingSessionID`). Switch with `ocr config set provider opencode-go`. There is no automatic fallback between providers; a failed default fails closed. +- Credential: `api_key_cmd` runs `ocr-key ` (`minimax` or `opencode-go`), resolved by OCR's shell from `PATH`. POSIX and Windows launchers in `home/.local/bin/` run `script/ocr-key.ts` with Bun. That script maps the provider to its Infisical secret (`MINIMAX_M_PLAN_KEY` / `OPENCODE_API_KEY`), strips an ambient copy of that variable, then invokes the existing self-hosted Infisical `script/with-secrets` / `script/with-secrets.ps1` boundary and prints only the freshly injected key. Unknown provider names fail before any lookup. No secret value is stored in Git or OCR config. ## Apply and verify @@ -31,8 +31,8 @@ ocr llm test The managed config is an ordinary file in Git and a symlink at the live path; do not run `ocr config set ...api_key` or paste a key in the TUI. That would write plaintext into a tracked file. For settings changes, modify the Git source and review its diff. The key command intentionally emits a secret to stdout **when called**; never print or capture its output in logs. Only use `ocr llm test` to verify connectivity. -If an existing `~/.opencodereview/config.json` conflicts, the linking scripts fail/report a conflict instead of overwriting it. Back it up outside the repository and manually migrate desired non-secret settings before linking. OCR sessions and other files within `~/.opencodereview/` remain untouched. +If an existing `~/.opencodereview/config.json` conflicts, the linking scripts fail/report a conflict instead of overwriting it. Back it up outside the repository and manually migrate desired non-secret settings before linking. OCR sessions and other files within `~/.opencodereview/` remain untouched. When upgrading from the `ocr-go-key` bridge, delete the stale `ocr-go-key` / `ocr-go-key.cmd` launchers under `~/.local/bin`; the link scripts never remove files. -OpenCode Go and Zen may share an `OPENCODE_API_KEY`, but only this `/inference/go/v1` endpoint is configured. Keep Extra Usage disabled in the OpenCode console if you need to prevent pay-as-you-go overflow; the endpoint alone cannot enforce billing controls. Missing/invalid credentials fail closed; no fallback model or provider is configured. +OpenCode Go and Zen may share an `OPENCODE_API_KEY`, but only this `/inference/go/v1` endpoint is configured for OCR. Keep Extra Usage disabled in the OpenCode console if you need to prevent pay-as-you-go overflow; the endpoint alone cannot enforce billing controls. Missing/invalid credentials fail closed; no fallback model or provider is configured. References: https://github.com/alibaba/open-code-review/blob/main/pages/src/content/docs/en/configuration.md and https://opencode.ai/docs/go/ diff --git a/home/.local/bin/ocr-go-key b/home/.local/bin/ocr-go-key deleted file mode 100755 index dbcf5f46..00000000 --- a/home/.local/bin/ocr-go-key +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -exec bun "$HOME/.dotfiles/script/ocr-go-key.ts" diff --git a/home/.local/bin/ocr-go-key.cmd b/home/.local/bin/ocr-go-key.cmd deleted file mode 100644 index c403f45d..00000000 --- a/home/.local/bin/ocr-go-key.cmd +++ /dev/null @@ -1,4 +0,0 @@ -@echo off -setlocal -bun "%USERPROFILE%\.dotfiles\script\ocr-go-key.ts" -exit /b %errorlevel% diff --git a/home/.local/bin/ocr-key b/home/.local/bin/ocr-key new file mode 100755 index 00000000..5becf0a6 --- /dev/null +++ b/home/.local/bin/ocr-key @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +set -euo pipefail +exec bun "$HOME/.dotfiles/script/ocr-key.ts" "$@" diff --git a/home/.local/bin/ocr-key.cmd b/home/.local/bin/ocr-key.cmd new file mode 100644 index 00000000..39aa51aa --- /dev/null +++ b/home/.local/bin/ocr-key.cmd @@ -0,0 +1,4 @@ +@echo off +setlocal +bun "%USERPROFILE%\.dotfiles\script\ocr-key.ts" %* +exit /b %errorlevel% diff --git a/home/.opencodereview/config.json b/home/.opencodereview/config.json index 60cd641e..629dfb94 100644 --- a/home/.opencodereview/config.json +++ b/home/.opencodereview/config.json @@ -1,12 +1,17 @@ { - "provider": "opencode-go", - "model": "mimo-v2.6-flash", + "provider": "minimax", + "model": "MiniMax-M3.1-Flash", + "providers": { + "minimax": { + "api_key_cmd": "ocr-key minimax" + } + }, "custom_providers": { "opencode-go": { "url": "https://opencode.ai/inference/go/v1", "protocol": "openai", "model": "mimo-v2.6-flash", - "api_key_cmd": "ocr-go-key", + "api_key_cmd": "ocr-key opencode-go", "extra_headers": { "x-opencode-session": "{ocr_session_key}" } diff --git a/script/ocr-go-key.ts b/script/ocr-key.ts similarity index 57% rename from script/ocr-go-key.ts rename to script/ocr-key.ts index 72448748..f94ca410 100644 --- a/script/ocr-go-key.ts +++ b/script/ocr-key.ts @@ -5,22 +5,42 @@ import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; const repoRoot = dirname(dirname(fileURLToPath(import.meta.url))); -const secretName = "OPENCODE_API_KEY"; + +// OCR provider name -> Infisical secret in environment `dev`. The `minimax` +// entry mirrors config/agents.toml [providers.minimax] (`secret`); the +// `opencode-go` entry mirrors its OPENCODE_API_KEY credential. The map is +// deliberately closed: an unknown provider fails before any secret lookup so +// a config typo cannot fetch an arbitrary Infisical value. +const PROVIDER_SECRETS = { + minimax: "MINIMAX_M_PLAN_KEY", + "opencode-go": "OPENCODE_API_KEY", +} as const; + +type OcrProvider = keyof typeof PROVIDER_SECRETS; function fail(message: string): never { - process.stderr.write(`ocr-go-key: ${message}\n`); + process.stderr.write(`ocr-key: ${message}\n`); process.exit(1); } -if (process.argv[2] === "--emit-injected-key") { +const provider = process.argv[2]; +if (!provider || !Object.hasOwn(PROVIDER_SECRETS, provider)) { + fail(`usage: ocr-key <${Object.keys(PROVIDER_SECRETS).join("|")}>`); +} +const secretName = PROVIDER_SECRETS[provider as OcrProvider]; + +if (process.argv[3] === "--emit-injected-key") { + if (process.argv.length !== 4) { + fail("unexpected arguments"); + } // This stage is launched ONLY by script/with-secrets( .ps1 ) after the - // parent has removed any inherited ambient value of OPENCODE_API_KEY. + // parent has removed any inherited ambient value of the target secret. const key = process.env[secretName]; if (!key || !key.trim() || /[\r\n]/.test(key)) { fail(`${secretName} is missing or malformed in the Infisical environment`); } process.stdout.write(`${key}\n`); -} else if (process.argv.length !== 2) { +} else if (process.argv.length !== 3) { fail("unexpected arguments"); } else { const cleanEnv = { ...process.env }; @@ -28,10 +48,11 @@ if (process.argv[2] === "--emit-injected-key") { // The tracked with-secrets wrappers verify the bound self-hosted project. // No secret appears in the command arguments, checked-in config, or stderr. const script = join(repoRoot, "script", process.platform === "win32" ? "with-secrets.ps1" : "with-secrets"); + const self = fileURLToPath(import.meta.url); const executable = process.platform === "win32" ? "pwsh" : script; const args = process.platform === "win32" - ? ["-NoProfile", "-File", script, "bun", fileURLToPath(import.meta.url), "--emit-injected-key"] - : ["bun", fileURLToPath(import.meta.url), "--emit-injected-key"]; + ? ["-NoProfile", "-File", script, "bun", self, provider, "--emit-injected-key"] + : ["bun", self, provider, "--emit-injected-key"]; const result = spawnSync(executable, args, { encoding: "utf8", env: cleanEnv, diff --git a/script/shell-files.txt b/script/shell-files.txt index 289f2ad7..d78660af 100644 --- a/script/shell-files.txt +++ b/script/shell-files.txt @@ -6,7 +6,7 @@ home/.claude/statusline-command.sh home/.local/bin/agent home/.local/bin/agy home/.local/bin/devices -home/.local/bin/ocr-go-key +home/.local/bin/ocr-key script/agent/infisical-key.sh script/agent/mimo script/agent/minimax From 1add72978cc2bc1980a39b4ce1aba88b3d5ebc4a Mon Sep 17 00:00:00 2001 From: rebuildup Date: Thu, 26 Nov 2026 14:56:27 +0900 Subject: [PATCH 2/2] test(ocr): cover ocr-key dispatch and tighten credential hop Address review findings: pin PROVIDER_SECRETS to agents.toml via a bun test, replace the provider cast with a type guard, require a one-shot hop nonce before the emit stage reads any credential, and document the shim like its siblings. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- home/.local/bin/ocr-key | 10 +++++ script/ocr-key.test.ts | 93 ++++++++++++++++++++++++++++++++++++++ script/ocr-key.ts | 99 ++++++++++++++++++++++++++--------------- 3 files changed, 166 insertions(+), 36 deletions(-) create mode 100644 script/ocr-key.test.ts diff --git a/home/.local/bin/ocr-key b/home/.local/bin/ocr-key index 5becf0a6..4dadf136 100755 --- a/home/.local/bin/ocr-key +++ b/home/.local/bin/ocr-key @@ -1,3 +1,13 @@ #!/usr/bin/env bash +# `ocr-key ` — Open Code Review credential bridge shim. +# Installed into ~/.local/bin by script/link; the implementation lives in +# the dotfiles repository (script/ocr-key.ts). It resolves the provider's +# Infisical secret and prints only that key for OCR's api_key_cmd. set -euo pipefail + +if ! command -v bun >/dev/null 2>&1; then + printf 'ocr-key: bun is required (install it via pc-setup / mise)\n' >&2 + exit 1 +fi + exec bun "$HOME/.dotfiles/script/ocr-key.ts" "$@" diff --git a/script/ocr-key.test.ts b/script/ocr-key.test.ts new file mode 100644 index 00000000..93c01c42 --- /dev/null +++ b/script/ocr-key.test.ts @@ -0,0 +1,93 @@ +import { describe, expect, test } from "bun:test"; +import { readFileSync, realpathSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { PROVIDER_SECRETS, isOcrProvider } from "./ocr-key"; + +const REPO = dirname(dirname(realpathSync(import.meta.path))); +const SCRIPT = join(REPO, "script", "ocr-key.ts"); + +function run(args: string[], env: Record = {}) { + return Bun.spawnSync([process.execPath, SCRIPT, ...args], { + env: { ...process.env, ...env }, + stdout: "pipe", + stderr: "pipe", + }); +} + +describe("ocr-key provider map", () => { + test("minimax secret mirrors config/agents.toml [providers.minimax].secret", () => { + const agents = Bun.TOML.parse(readFileSync(join(REPO, "config", "agents.toml"), "utf8")) as { + providers: Record; + }; + expect(agents.providers.minimax?.secret).toBe(PROVIDER_SECRETS.minimax); + }); + + test("every api_key_cmd provider in the OCR config is one the bridge accepts", () => { + const ocr = JSON.parse(readFileSync(join(REPO, "home", ".opencodereview", "config.json"), "utf8")) as { + provider: string; + providers?: Record; + custom_providers?: Record; + }; + const entries = { ...ocr.providers, ...ocr.custom_providers }; + for (const [name, entry] of Object.entries(entries)) { + const cmd = entry.api_key_cmd; + if (!cmd) continue; + const match = /^ocr-key (\S+)$/.exec(cmd); + expect(match, `${name}.api_key_cmd must be 'ocr-key '`).not.toBeNull(); + expect(isOcrProvider(match![1]), `${name} uses unknown provider '${match![1]}'`).toBe(true); + } + // The selected provider must resolve a credential through the bridge. + const active = entries[ocr.provider]; + expect(active?.api_key_cmd, `provider '${ocr.provider}' has no api_key_cmd`).toBeTruthy(); + }); +}); + +describe("ocr-key dispatch", () => { + test("unknown provider fails closed before any Infisical lookup", () => { + const result = run(["bogus"]); + expect(result.exitCode).not.toBe(0); + expect(result.stderr.toString()).toContain("usage: ocr-key"); + }); + + test("prototype keys are not providers", () => { + expect(isOcrProvider("constructor")).toBe(false); + expect(isOcrProvider("minimax")).toBe(true); + expect(isOcrProvider("opencode-go")).toBe(true); + }); + + test("emit stage refuses to run without the parent hop nonce", () => { + const result = run(["minimax", "--emit-injected-key"], { MINIMAX_M_PLAN_KEY: "ambient-value" }); + expect(result.exitCode).not.toBe(0); + expect(result.stdout.toString()).not.toContain("ambient-value"); + }); + + test("emit stage prints exactly the injected secret for its own provider", () => { + const result = run(["minimax", "--emit-injected-key"], { + OCR_KEY_HOP_NONCE: "test-nonce", + MINIMAX_M_PLAN_KEY: "plan-key", + OPENCODE_API_KEY: "go-key", + }); + expect(result.exitCode).toBe(0); + expect(result.stdout.toString()).toBe("plan-key\n"); + }); + + test("emit stage never prints another provider's secret", () => { + const result = run(["opencode-go", "--emit-injected-key"], { + OCR_KEY_HOP_NONCE: "test-nonce", + MINIMAX_M_PLAN_KEY: "plan-key", + OPENCODE_API_KEY: "go-key", + }); + expect(result.exitCode).toBe(0); + expect(result.stdout.toString()).toBe("go-key\n"); + }); + + test("emit stage rejects empty and multi-line values", () => { + for (const bad of ["", "two\nlines"]) { + const result = run(["minimax", "--emit-injected-key"], { + OCR_KEY_HOP_NONCE: "test-nonce", + MINIMAX_M_PLAN_KEY: bad, + }); + expect(result.exitCode).not.toBe(0); + } + }); +}); diff --git a/script/ocr-key.ts b/script/ocr-key.ts index f94ca410..ccc09759 100644 --- a/script/ocr-key.ts +++ b/script/ocr-key.ts @@ -1,6 +1,7 @@ #!/usr/bin/env bun /** Supply OCR's api_key_cmd from the canonical Infisical project, never ambient credentials. */ import { spawnSync } from "node:child_process"; +import { randomUUID } from "node:crypto"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; @@ -11,28 +12,33 @@ const repoRoot = dirname(dirname(fileURLToPath(import.meta.url))); // `opencode-go` entry mirrors its OPENCODE_API_KEY credential. The map is // deliberately closed: an unknown provider fails before any secret lookup so // a config typo cannot fetch an arbitrary Infisical value. -const PROVIDER_SECRETS = { +export const PROVIDER_SECRETS = { minimax: "MINIMAX_M_PLAN_KEY", "opencode-go": "OPENCODE_API_KEY", } as const; -type OcrProvider = keyof typeof PROVIDER_SECRETS; +export type OcrProvider = keyof typeof PROVIDER_SECRETS; + +export function isOcrProvider(value: string): value is OcrProvider { + return Object.hasOwn(PROVIDER_SECRETS, value); +} + +// One-shot marker the parent stage injects into the with-secrets child env so +// the --emit-injected-key stage cannot be invoked directly against an ambient +// credential. It is not a secret; it only proves the caller went through the +// parent stage, which strips the ambient value first. +const HOP_NONCE_ENV = "OCR_KEY_HOP_NONCE"; function fail(message: string): never { process.stderr.write(`ocr-key: ${message}\n`); process.exit(1); } -const provider = process.argv[2]; -if (!provider || !Object.hasOwn(PROVIDER_SECRETS, provider)) { - fail(`usage: ocr-key <${Object.keys(PROVIDER_SECRETS).join("|")}>`); -} -const secretName = PROVIDER_SECRETS[provider as OcrProvider]; - -if (process.argv[3] === "--emit-injected-key") { - if (process.argv.length !== 4) { - fail("unexpected arguments"); +function emitInjectedKey(provider: OcrProvider): never { + if (!process.env[HOP_NONCE_ENV]) { + fail("--emit-injected-key is only valid for the parent ocr-key stage"); } + const secretName = PROVIDER_SECRETS[provider]; // This stage is launched ONLY by script/with-secrets( .ps1 ) after the // parent has removed any inherited ambient value of the target secret. const key = process.env[secretName]; @@ -40,31 +46,52 @@ if (process.argv[3] === "--emit-injected-key") { fail(`${secretName} is missing or malformed in the Infisical environment`); } process.stdout.write(`${key}\n`); -} else if (process.argv.length !== 3) { - fail("unexpected arguments"); -} else { - const cleanEnv = { ...process.env }; - delete cleanEnv[secretName]; - // The tracked with-secrets wrappers verify the bound self-hosted project. - // No secret appears in the command arguments, checked-in config, or stderr. - const script = join(repoRoot, "script", process.platform === "win32" ? "with-secrets.ps1" : "with-secrets"); - const self = fileURLToPath(import.meta.url); - const executable = process.platform === "win32" ? "pwsh" : script; - const args = process.platform === "win32" - ? ["-NoProfile", "-File", script, "bun", self, provider, "--emit-injected-key"] - : ["bun", self, provider, "--emit-injected-key"]; - const result = spawnSync(executable, args, { - encoding: "utf8", - env: cleanEnv, - windowsHide: true, - maxBuffer: 1024 * 1024, - }); - if (result.error || result.status !== 0) { - fail("Infisical retrieval failed; check login, project binding, and secret availability"); + process.exit(0); +} + +function main(): void { + const provider = process.argv[2]; + if (!provider || !isOcrProvider(provider)) { + fail(`usage: ocr-key <${Object.keys(PROVIDER_SECRETS).join("|")}>`); } - const key = result.stdout.replace(/\r?\n$/, ""); - if (!key || !key.trim() || /[\r\n]/.test(key)) { - fail("secret output was empty or contained unexpected extra lines"); + + if (process.argv[3] === "--emit-injected-key") { + if (process.argv.length !== 4) { + fail("unexpected arguments"); + } + emitInjectedKey(provider); + } else if (process.argv.length !== 3) { + fail("unexpected arguments"); + } else { + const secretName = PROVIDER_SECRETS[provider]; + const cleanEnv = { ...process.env }; + delete cleanEnv[secretName]; + cleanEnv[HOP_NONCE_ENV] = randomUUID(); + // The tracked with-secrets wrappers verify the bound self-hosted project. + // No secret appears in the command arguments, checked-in config, or stderr. + const script = join(repoRoot, "script", process.platform === "win32" ? "with-secrets.ps1" : "with-secrets"); + const self = fileURLToPath(import.meta.url); + const executable = process.platform === "win32" ? "pwsh" : script; + const args = process.platform === "win32" + ? ["-NoProfile", "-File", script, "bun", self, provider, "--emit-injected-key"] + : ["bun", self, provider, "--emit-injected-key"]; + const result = spawnSync(executable, args, { + encoding: "utf8", + env: cleanEnv, + windowsHide: true, + maxBuffer: 1024 * 1024, + }); + if (result.error || result.status !== 0) { + fail("Infisical retrieval failed; check login, project binding, and secret availability"); + } + const key = result.stdout.replace(/\r?\n$/, ""); + if (!key || !key.trim() || /[\r\n]/.test(key)) { + fail("secret output was empty or contained unexpected extra lines"); + } + process.stdout.write(`${key}\n`); } - process.stdout.write(`${key}\n`); +} + +if (import.meta.main) { + main(); }