Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

OpenCode 1: discovery no longer leaves the catalog lock held, so a refresh gets past it (#4706)

Milind Soni07b8bc1a6984
diff --git a/server/drivers/acp/opencode-go.ts b/server/drivers/acp/opencode-go.ts
index 4edd6b3ea2..59de0a63c8 100644
--- a/server/drivers/acp/opencode-go.ts
+++ b/server/drivers/acp/opencode-go.ts
@@ -241,12 +241,15 @@ function runOpenCodeModelsVerbose(
});
}
-/** OpenCode (1.19.4) lists models from its models.dev cache, or without one the snapshot built into it, and neither
- * `opencode models` nor `opencode acp` refetches it; `models --refresh` does, and the next process reads it. OpenCode
- * 2.2.5 rejects the flag (exit 1): each of its processes refetches the catalog itself, in the background, at start. */
+/** OpenCode 1.19.4 lists models from its models.dev cache file and `models --refresh` refetches it. Every other
+ * process of it starts a background refetch under a cross-process lock that a process ended mid-fetch leaves held,
+ * and the next refetch waits until the lock is a minute old. OpenCode 2.2.5 rejects the flag (exit 1): each of its
+ * processes refetches its catalog itself, in the background, at start. */
+const MODELS_REFRESH_TIMEOUT_MS = 75_000;
+
function refreshOpenCodeModelCache(cli: string, environment: Record<string, string | undefined>): Promise<boolean> {
return new Promise((resolve) => {
- execCli(cli, ["models", "--refresh"], { timeout: DISCOVERY_TIMEOUT_MS, maxBuffer: 8 * 1024 * 1024, env: environment }, (error) => resolve(!error));
+ execCli(cli, ["models", "--refresh"], { timeout: MODELS_REFRESH_TIMEOUT_MS, maxBuffer: 8 * 1024 * 1024, env: environment }, (error) => resolve(!error));
});
}
@@ -433,8 +436,10 @@ export async function discoverOpenCodeModels(
const settleMs = options.refresh && !await refreshOpenCodeModelCache(cli, environment)
? options.settleMs ?? SELF_REFRESH_SETTLE_MS
: 0;
- const probeSession = () => probeOpenCodeSession(cli, environment, undefined, DISCOVERY_TIMEOUT_MS, settleMs);
- const readMetadata = () => runOpenCodeModelsVerbose(cli, environment).then(parseOpenCodeModels);
+ // A listing ends long before a catalog refetch would: it reads the cache, unless it waits for OpenCode 2's own refetch.
+ const listing = settleMs ? environment : { ...environment, OPENCODE_DISABLE_MODELS_FETCH: "1" };
+ const probeSession = () => probeOpenCodeSession(cli, listing, undefined, DISCOVERY_TIMEOUT_MS, settleMs);
+ const readMetadata = () => runOpenCodeModelsVerbose(cli, listing).then(parseOpenCodeModels);
let [session, verbose] = await Promise.allSettled([probeSession(), readMetadata()]);
// Two OpenCode processes starting together in a fresh home race to create its database and one fails
// (1 in 8 fresh homes). A timed-out half is not retried.
diff --git a/server/drivers/acp/opencode-refresh.test.ts b/server/drivers/acp/opencode-refresh.test.ts
index daf16d6373..157788f544 100644
--- a/server/drivers/acp/opencode-refresh.test.ts
+++ b/server/drivers/acp/opencode-refresh.test.ts
@@ -110,6 +110,13 @@ describe("OpenCode Refresh", () => {
await expect.poll(refreshes).toBe(2);
});
+ it("leaves OpenCode 1's catalog lock free, so a refresh right after a read gets past it", async () => {
+ const { env, scratch } = home(STALE, FRESH);
+ env.FAKE_ACP_MODELS_LOCK = join(scratch, "models.lock");
+ expect(ids(await discoverOpenCodeModels(env, FAKE_CLI))).not.toContain("venice/deepseek-v4-1-flash-uncensored");
+ expect(ids(await discoverOpenCodeModels(env, FAKE_CLI, { refresh: true, settleMs: 0 }))).toContain("venice/deepseek-v4-1-flash-uncensored");
+ });
+
it("picks up the catalog OpenCode 2 refreshes itself, as it has no `models --refresh`", async () => {
const { env } = home(STALE, FRESH);
env.FAKE_ACP_MODELS_LIST = "v2";
diff --git a/server/testing/fake-acp-cli.ts b/server/testing/fake-acp-cli.ts
index 13dbc18543..04e5663403 100755
--- a/server/testing/fake-acp-cli.ts
+++ b/server/testing/fake-acp-cli.ts
@@ -45,6 +45,8 @@
// refetches its models.dev cache only then; OPENCODE_CONFIG_CONTENT's provider models join any model list
// FAKE_ACP_MODELS_LIST=v2 OpenCode 2: `models` answers empty with exit 0, `models --verbose` and `models --refresh` exit 1;
// with FAKE_ACP_MODELS_REFRESH_FROM a running process refreshes its own list after FAKE_ACP_SELF_REFRESH_MS (default 300)
+// FAKE_ACP_MODELS_LOCK path: as OpenCode 1.19.4, a `models` or `acp` process without OPENCODE_DISABLE_MODELS_FETCH=1
+// starts a catalog fetch under this lock and, ended early, leaves it; `models --refresh` fails while it is held
// FAKE_ACP_MODELS_LIST=auto-only Cursor CLI 2026.10.22: `models` lists only `auto`
// FAKE_ACP_VERBOSE_FAIL_ONCE marker path: the first `models --verbose` fails, like a new OpenCode home's first run
// FAKE_ACP_MODEL_STICKS set_config_option succeeds but keeps the old model
@@ -310,7 +312,15 @@ if (argv[0] === "status" || argv[0] === "whoami") {
console.log(JSON.stringify({ isAuthenticated: authenticated }));
process.exit(0);
}
+const catalogLock = process.env.FAKE_ACP_MODELS_LOCK;
+if (catalogLock && (argv[0] === "models" || argv[0] === "acp") && !argv.includes("--refresh") && process.env.OPENCODE_DISABLE_MODELS_FETCH !== "1") {
+ writeFileSync(catalogLock, String(process.pid));
+}
if (argv[0] === "models" && argv.includes("--refresh")) {
+ if (catalogLock && existsSync(catalogLock)) {
+ console.error("models.dev cache is locked by another process");
+ process.exit(1);
+ }
if (process.env.FAKE_ACP_MODELS_LIST === "v2") {
console.error("Unrecognized flag: --refresh in command opencode models");
process.exit(1);