Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: stock mode gives the agent shell stock OpenCode's env and PATH

ogsamrat860d3e981316
diff --git a/apps/desktop/electron/runtime.mjs b/apps/desktop/electron/runtime.mjs
index 5a511d67..02094f97 100644
--- a/apps/desktop/electron/runtime.mjs
+++ b/apps/desktop/electron/runtime.mjs
@@ -674,6 +674,14 @@ export function extraPathEntries() {
return candidates.filter((entry) => entry && isDirectory(entry));
}
+// The PATH stock OpenCode would see: the launch PATH; for a Finder/Dock launch on macOS (launchd's
+// default PATH) the login shell's, as path_helper gives it to every login shell.
+const LAUNCHD_PATH = "/usr/bin:/bin:/usr/sbin:/sbin";
+export function launchShellPath(launchPath, platform = process.platform, loginShellEntries = pathHelperEntries) {
+ if (platform !== "darwin" || launchPath !== LAUNCHD_PATH) return launchPath;
+ return loginShellEntries().join(path.delimiter) || launchPath;
+}
+
export function enrichedPath(sidecarDirs, currentPath) {
const entries = [
...sidecarDirs.filter(isDirectory),
@@ -1633,6 +1641,11 @@ export function createRuntimeManager({
!Object.prototype.hasOwnProperty.call(env, "Path")
? "PATH"
: "Path";
+ // Stock mode (OMNIRUSH_FARM): the agent's shell gets the launch PATH and CA file back (server stock-mode.mjs).
+ env.OMNIRUSH_STOCK_SHELL_ENV = JSON.stringify({
+ PATH: launchShellPath(inheritedProcessEnv[pathKey]),
+ NODE_EXTRA_CA_CERTS: inheritedProcessEnv.NODE_EXTRA_CA_CERTS,
+ });
const pathEnv = enrichedPath(sidecarDirs, env[pathKey]);
if (pathEnv) {
env[pathKey] = pathEnv;
diff --git a/apps/desktop/electron/runtime.test.mjs b/apps/desktop/electron/runtime.test.mjs
index d9d3f8fb..d644eeb2 100644
--- a/apps/desktop/electron/runtime.test.mjs
+++ b/apps/desktop/electron/runtime.test.mjs
@@ -9,6 +9,7 @@ import {
commandMatchesPackagedSidecar,
createRuntimeManager,
embeddedServerImportUrl,
+ launchShellPath,
migrateOmniRushServerTokenStore,
prepareRuntimeWorkspaceRoot,
prioritizeWorkspacePaths,
@@ -292,6 +293,16 @@ describe("commandMatchesPackagedSidecar", () => {
});
});
+describe("launchShellPath (stock mode's agent PATH)", () => {
+ it("keeps the launch PATH; a macOS Finder launch gets the login shell's", () => {
+ const login = () => ["/usr/local/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin"];
+ assert.equal(launchShellPath("/kit/bin:/usr/bin:/bin", "linux", login), "/kit/bin:/usr/bin:/bin");
+ assert.equal(launchShellPath("/opt/homebrew/bin:/usr/bin:/bin", "darwin", login), "/opt/homebrew/bin:/usr/bin:/bin");
+ assert.equal(launchShellPath("/usr/bin:/bin:/usr/sbin:/sbin", "darwin", login), "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin");
+ assert.equal(launchShellPath("/usr/bin:/bin:/usr/sbin:/sbin", "darwin", () => []), "/usr/bin:/bin:/usr/sbin:/sbin");
+ });
+});
+
describe("embeddedServerImportUrl", () => {
it("returns the same file URL for unchanged metadata", async () => {
const dir = await mkdtemp(path.join(os.tmpdir(), "omnirush-runtime-"));
diff --git a/apps/server/src/managed-opencode-v2.ts b/apps/server/src/managed-opencode-v2.ts
index 7f2e171a..44d52c8c 100644
--- a/apps/server/src/managed-opencode-v2.ts
+++ b/apps/server/src/managed-opencode-v2.ts
@@ -14,7 +14,7 @@ import { writeFileAtomic } from "./atomic-write.js";
import { currentProviderLockPolicy, noteEngineStarted, providerAllowed } from "./provider-lock.js";
import { APP_PROVIDER_IDS, providerLockEnv } from "./provider-lock/provider-lock.mjs";
import { gateEnv } from "./engine-gates/engine-gates.mjs";
-import { STOCK_ENV, STOCK_PROVIDER, stockMode, stockProviders } from "./stock-mode/stock-mode.mjs";
+import { STOCK_ENV, STOCK_PROVIDER, STOCK_SHELL_ENV, stockMode, stockProviders } from "./stock-mode/stock-mode.mjs";
import { releaseEngineBinary } from "./release-guard.js";
export interface OpencodeV2ModelSpec {
@@ -194,11 +194,12 @@ export async function createManagedOpencodeV2Server(
"LANG", "LC_ALL", "LC_CTYPE", "TZ", "TERM", "CI",
"XDG_CACHE_HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_RUNTIME_DIR",
"SystemRoot", "SYSTEMROOT", "WINDIR", "COMSPEC", "PATHEXT", "USERPROFILE", "APPDATA", "LOCALAPPDATA",
- "NODE_EXTRA_CA_CERTS", "SSL_CERT_FILE", "SSL_CERT_DIR", STOCK_ENV,
+ "NODE_EXTRA_CA_CERTS", "SSL_CERT_FILE", "SSL_CERT_DIR", STOCK_ENV, STOCK_SHELL_ENV,
]) {
const value = options.env?.[key] ?? process.env[key];
if (value !== undefined) inherited[key] = value;
}
+ if (!stockMode(inherited)) delete inherited[STOCK_SHELL_ENV];
// A caller may deliberately provide a config file; never inherit the server's
// OPENCODE_CONFIG or OPENCODE_PURE settings implicitly.
if (options.env?.OPENCODE_CONFIG) inherited.OPENCODE_CONFIG = options.env.OPENCODE_CONFIG;
diff --git a/apps/server/src/opencode-plugins/omnirush-session-next.ts b/apps/server/src/opencode-plugins/omnirush-session-next.ts
index 08b4ae32..c869455c 100644
--- a/apps/server/src/opencode-plugins/omnirush-session-next.ts
+++ b/apps/server/src/opencode-plugins/omnirush-session-next.ts
@@ -1,4 +1,5 @@
import { SHELL_SECRET_ENV } from "./shell-secret-env.js";
+import { stockMode, stockShellEnv } from "../stock-mode/stock-mode.mjs";
/**
* 2.x engine plugin: what the 1.x plugins' `chat.headers` and `shell.env` did.
@@ -65,12 +66,15 @@ export default {
// Captured before any shell runs: the engine's own XDG folders are not the user's.
const userXdg = Object.fromEntries(["DATA", "STATE", "CONFIG"].map((kind) =>
[`XDG_${kind}_HOME`, process.env[`OMNIRUSH_USER_XDG_${kind}_HOME`]]));
+ const stock = stockMode(process.env);
await ctx.shell.hook("create.before", (event) => {
for (const name of SHELL_SECRET_ENV) event.env[name] = "";
for (const [name, value] of Object.entries(userXdg)) {
if (value === undefined) continue;
event.env[name] = value || undefined;
}
+ // Stock mode: the agent's shell has stock OpenCode's environment, nothing of the app's.
+ if (stock) stockShellEnv(event.env);
});
},
};
diff --git a/apps/server/src/stock-mode/stock-mode.d.mts b/apps/server/src/stock-mode/stock-mode.d.mts
index c54e4712..48b64d52 100644
--- a/apps/server/src/stock-mode/stock-mode.d.mts
+++ b/apps/server/src/stock-mode/stock-mode.d.mts
@@ -5,3 +5,5 @@ export declare const STOCK_SWITCHES: Readonly<Record<string, string>>;
export declare function stockMode(env?: Record<string, string | undefined>): boolean;
export declare function applyStockSwitches(env?: Record<string, string | undefined>): boolean;
export declare function stockProviders<T extends { id: string; models: { id: string; config?: Record<string, unknown> }[] }>(specs: T[]): T[];
+export declare const STOCK_SHELL_ENV: "OMNIRUSH_STOCK_SHELL_ENV";
+export declare function stockShellEnv<T extends Record<string, string | undefined>>(env: T, engineEnv?: Record<string, string | undefined>): T;
diff --git a/apps/server/src/stock-mode/stock-mode.mjs b/apps/server/src/stock-mode/stock-mode.mjs
index cda16a8c..28edf6c3 100644
--- a/apps/server/src/stock-mode/stock-mode.mjs
+++ b/apps/server/src/stock-mode/stock-mode.mjs
@@ -4,6 +4,8 @@
// note, automatic compaction or wrap-up offer, and no install-log wrappers. Sign-in, the gateway broker (auth and
// routing only), capture and uploads, and the sandbox stay. Uploads keep the exact shape of any 2.x chat.
+import { join } from "node:path";
+
export const STOCK_ENV = "OMNIRUSH_FARM";
export const STOCK_PROVIDER = "omnirush";
export const STOCK_MODEL = "gpt-6-astra";
@@ -55,3 +57,26 @@ export function stockProviders(specs) {
return models.length ? [{ ...spec, models }] : [];
});
}
+
+// The PATH and NODE_EXTRA_CA_CERTS the app was launched with (JSON, set by the desktop's runtime.mjs): the app
+// widens both for itself, the agent's shell gets them back in stock mode.
+export const STOCK_SHELL_ENV = "OMNIRUSH_STOCK_SHELL_ENV";
+// What the app puts in its engine's environment that stock OpenCode's has not (managed-opencode-v2.ts).
+const APP_ENGINE_NAME = /^(?:OMNIRUSH_|OPENCODE_(?:PASSWORD|SERVER_PASSWORD|SERVER_USERNAME|DB|CONFIG_DIR|DISABLE_AUTOUPDATE|DISABLE_CLAUDE_CODE)$)/;
+
+/**
+ * Stock mode: turns an agent shell's environment (the engine's, in place) into stock OpenCode's, as the stock run
+ * gets it: no app names, the launch PATH and CA file, and OPENCODE_CONFIG naming the engine's config file.
+ */
+export function stockShellEnv(env, engineEnv = process.env) {
+ for (const name of Object.keys(env)) if (APP_ENGINE_NAME.test(name)) delete env[name];
+ if (engineEnv.OPENCODE_CONFIG_DIR) env.OPENCODE_CONFIG = join(engineEnv.OPENCODE_CONFIG_DIR, "opencode.json");
+ let launch = null;
+ try { launch = JSON.parse(engineEnv[STOCK_SHELL_ENV] ?? "null"); } catch { /* Not set by the desktop: the engine's own. */ }
+ if (!launch || typeof launch !== "object") return env;
+ const pathKey = Object.keys(env).find((name) => name.toUpperCase() === "PATH") ?? "PATH";
+ if (typeof launch.PATH === "string") env[pathKey] = launch.PATH;
+ if (typeof launch.NODE_EXTRA_CA_CERTS === "string") env.NODE_EXTRA_CA_CERTS = launch.NODE_EXTRA_CA_CERTS;
+ else delete env.NODE_EXTRA_CA_CERTS;
+ return env;
+}
diff --git a/apps/server/src/stock-mode/stock-mode.test.ts b/apps/server/src/stock-mode/stock-mode.test.ts
index 6350476a..69eee8ce 100644
--- a/apps/server/src/stock-mode/stock-mode.test.ts
+++ b/apps/server/src/stock-mode/stock-mode.test.ts
@@ -1,7 +1,7 @@
import { expect, test } from "bun:test";
import { engineV2SpawnEnv } from "../managed-opencode-v2.js";
-import { applyStockSwitches, STOCK_SWITCHES, stockMode, stockProviders } from "./stock-mode.mjs";
+import { applyStockSwitches, STOCK_SWITCHES, stockMode, stockProviders, stockShellEnv } from "./stock-mode.mjs";
const base = { PATH: "/usr/bin", OMNIRUSH_NO_SYSTEM_CONFIG: "1" };
@@ -42,3 +42,30 @@ test("the engine gate lets only the gateway and no MCP server through in stock m
expect(stock.OMNIRUSH_GATE_PROVIDERS).toBe("omnirush");
expect(stock.OMNIRUSH_GATE_MCP).toBe("");
});
+
+test("a stock-mode agent shell has stock OpenCode's names, launch PATH and CA file", () => {
+ // The names the 3.8.1 desktop gave a stock-mode agent shell (evidence release-381-prep/stock-env), values made up.
+ const names = ["AGENT", "AI_AGENT", "HOME", "LANG", "NODE_EXTRA_CA_CERTS", "OMNIRUSH_ACCESS_TOKEN", "OMNIRUSH_FARM",
+ "OMNIRUSH_GATE_MCP", "OMNIRUSH_GATE_PROVIDERS", "OMNIRUSH_NO_SYSTEM_CONFIG", "OMNIRUSH_POLICY_TOKEN", "OMNIRUSH_PROVIDER_ALLOW",
+ "OMNIRUSH_PROVIDER_LOCK", "OMNIRUSH_PROVIDER_LOCK_MESSAGE", "OMNIRUSH_SERVER_TOKEN", "OMNIRUSH_SERVER_URL",
+ "OMNIRUSH_USER_XDG_CONFIG_HOME", "OMNIRUSH_USER_XDG_DATA_HOME", "OMNIRUSH_USER_XDG_STATE_HOME", "OPENCODE", "OPENCODE_CONFIG_DIR",
+ "OPENCODE_DB", "OPENCODE_DISABLE_AUTOUPDATE", "OPENCODE_DISABLE_CLAUDE_CODE", "OPENCODE_DISABLE_MODELS_FETCH", "OPENCODE_PASSWORD",
+ "OPENCODE_SERVER_PASSWORD", "OPENCODE_SERVER_USERNAME", "OPENCODE_SESSION_ID", "OPENCODE_TERMINAL", "PATH", "PWD", "SHLVL", "TERM",
+ "XDG_CACHE_HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME"];
+ const shell: Record<string, string | undefined> = Object.fromEntries(names.map((name) => [name, "x"]));
+ shell.PATH = "/app/sidecars:/usr/local/sbin:/usr/bin";
+ const engine = { OPENCODE_CONFIG_DIR: "/state/config", OMNIRUSH_STOCK_SHELL_ENV: JSON.stringify({ PATH: "/usr/bin" }) };
+ stockShellEnv(shell, engine);
+ // Stock's names (evidence envn-oc) with the rig's launch LANG; `_` is the shell's own.
+ expect(Object.keys(shell).sort()).toEqual(["AGENT", "AI_AGENT", "HOME", "LANG", "OPENCODE", "OPENCODE_CONFIG",
+ "OPENCODE_DISABLE_MODELS_FETCH", "OPENCODE_SESSION_ID", "OPENCODE_TERMINAL", "PATH", "PWD", "SHLVL", "TERM",
+ "XDG_CACHE_HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME"]);
+ expect(shell.PATH).toBe("/usr/bin");
+ expect(shell.OPENCODE_CONFIG).toBe("/state/config/opencode.json");
+ // A CA file the user launched with is theirs; without the desktop's launch record the engine's PATH stays.
+ const own: Record<string, string> = { Path: "C:\\app;C:\\Windows", NODE_EXTRA_CA_CERTS: "/app/ca.pem" };
+ stockShellEnv(own, { OMNIRUSH_STOCK_SHELL_ENV: JSON.stringify({ PATH: "C:\\Windows", NODE_EXTRA_CA_CERTS: "/me/ca.pem" }) });
+ expect(own).toEqual({ Path: "C:\\Windows", NODE_EXTRA_CA_CERTS: "/me/ca.pem" });
+ const bare: Record<string, string> = { PATH: "/engine/bin", OMNIRUSH_FARM: "1" };
+ expect(stockShellEnv(bare, {})).toEqual({ PATH: "/engine/bin" });
+});