Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/open-code-review.md
Original file line number Diff line number Diff line change
@@ -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 <provider>` (`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

Expand All @@ -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/
3 changes: 0 additions & 3 deletions home/.local/bin/ocr-go-key

This file was deleted.

4 changes: 0 additions & 4 deletions home/.local/bin/ocr-go-key.cmd

This file was deleted.

13 changes: 13 additions & 0 deletions home/.local/bin/ocr-key
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# `ocr-key <provider>` — 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" "$@"
4 changes: 4 additions & 0 deletions home/.local/bin/ocr-key.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
setlocal
bun "%USERPROFILE%\.dotfiles\script\ocr-key.ts" %*
exit /b %errorlevel%
11 changes: 8 additions & 3 deletions home/.opencodereview/config.json
Original file line number Diff line number Diff line change
@@ -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}"
}
Expand Down
49 changes: 0 additions & 49 deletions script/ocr-go-key.ts

This file was deleted.

93 changes: 93 additions & 0 deletions script/ocr-key.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> = {}) {
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<string, { secret?: string }>;
};
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<string, { api_key_cmd?: string }>;
custom_providers?: Record<string, { api_key_cmd?: string }>;
};
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 <provider>'`).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);
}
});
});
97 changes: 97 additions & 0 deletions script/ocr-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/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";

const repoRoot = dirname(dirname(fileURLToPath(import.meta.url)));

// 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.
export const PROVIDER_SECRETS = {
minimax: "MINIMAX_M_PLAN_KEY",
"opencode-go": "OPENCODE_API_KEY",
} as const;

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);
}

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];
if (!key || !key.trim() || /[\r\n]/.test(key)) {
fail(`${secretName} is missing or malformed in the Infisical environment`);
}
process.stdout.write(`${key}\n`);
process.exit(0);
}

function main(): void {
const provider = process.argv[2];
if (!provider || !isOcrProvider(provider)) {
fail(`usage: ocr-key <${Object.keys(PROVIDER_SECRETS).join("|")}>`);
}

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`);
}
}

if (import.meta.main) {
main();
}
2 changes: 1 addition & 1 deletion script/shell-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading