Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor([issue-3194]): extract composeProviderEnv and close the two delta sites

[._.]/ Adam Eivy80730f93b79c
diff --git a/server/cos-runner/index.js b/server/cos-runner/index.js
index ca1a3d7bd2..894740a22a 100644
--- a/server/cos-runner/index.js
+++ b/server/cos-runner/index.js
@@ -188,17 +188,13 @@ app.post('/spawn', async (req, res) => {
// Ensure workspacePath is valid
const cwd = workspacePath && typeof workspacePath === 'string' ? workspacePath : ROOT_DIR;
- // Shared composition (provider.envVars + OpenCode models map + PWD pin +
- // CLAUDECODE strip) — see buildCliChildEnv. The runner receives the provider's
- // envVars over HTTP rather than a provider record, so it wraps them into the
- // `provider` slot; with no `command` in the payload the OpenCode layer is a
- // no-op and the result is exactly `{ ...process.env, ...envVars }` as before.
- // The PWD pin is the path #3193 was reported through: the log line above named
- // the app's workspace correctly while every OpenCode agent still ran in the
- // PortOS folder. No `guard`, preserving today's behavior: this runner is a
- // separate process that does not carry the pm2 shim (only the in-process
- // agent-spawning paths opt in).
- const childEnv = buildCliChildEnv({ provider: { envVars }, cwd });
+ // The env arrives already composed — agentLifecycle built it with
+ // composeProviderEnv (provider.envVars + the OpenCode models map) and POSTed it
+ // — so this side only needs the base, the PWD pin, and the CLAUDECODE strip.
+ // The pin is the path #3193 was reported through: the log line above named the
+ // app's workspace correctly while every OpenCode agent still ran in the PortOS
+ // folder. No `guard` — this separate process has never carried the pm2 shim.
+ const childEnv = buildCliChildEnv({ before: envVars, cwd });
// Resolve a bare npm-installed CLI (opencode/codex/claude/… — a .cmd/.bat
// shim on Windows) to its real path and wrap a shim as `cmd.exe /c <path>` so
diff --git a/server/lib/README.md b/server/lib/README.md
index 0344f9a447..0ccacfa3c9 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -105,7 +105,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `usageRange.js` | `resolveUsageRange({ period, from, to })` — pure period→inclusive-YYYY-MM-DD range resolution for the usage cost report (explicit dates win; `all` unbounded; default 7d). |
| `providerTranscriptUsage.js` | Parsers for the real per-message token counts the coding CLIs write to disk (0 tokens to read) — `parseClaudeTranscript` (`~/.claude/projects/<cwd-slug>/*.jsonl`), `parseCodexRollout` (`~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`), `claudeProjectSlug`, `totalTranscriptTokens`. Both de-duplicate a format hazard that otherwise inflates counts badly: Claude repeats one response across several lines sharing a `message.id`, and Codex's `total_token_usage` is cumulative and repeated. Each parser returns per-model buckets (`byModel`) plus the message keys it counted (`countedKeys`), and accepts an `exclude` set — that is what stops two overlapping PortOS runs from both billing the same messages. Tolerant of truncated (mid-write) files; consumed by `services/usageReconciler.js`. |
| `opencodeConfig.js` | OpenCode config builder — `buildOpencodeEnvVars(provider, model)` builds dynamic `OPENCODE_CONFIG_CONTENT` declaring the models map under `provider.ollama.models` (bare ids) for Ollama-backed OpenCode providers. Fixes --model rejection. |
-| `cliChildEnv.js` | `buildCliChildEnv({ baseEnv, before, provider, model, cwd, extra, guard })` — the one place the AI-CLI child environment is composed. Layers `baseEnv → before → provider.envVars → buildOpencodeEnvVars → extra`, pins `PWD` to the spawn cwd, strips `CLAUDECODE`, and (with `guard: true`) prepends the pm2 guard shim onto the final `PATH`. Replaces the hand-rolled copy that lived at all five spawn sites (`services/runner.js`, `cliProviderRun.js`, `services/agentCliSpawning.js`, `tuiPromptRunner.js`, `cos-runner/index.js`) and made every env-level fix an N-file sweep (#3194). The two slots are not interchangeable: `before` sits UNDER `provider.envVars` (forgeTokenEnv/claudeSettingsEnv, so a provider override still wins), `extra` sits OVER it (TERM/COLORTERM for a PTY) — `cliChildEnv.test.js` asserts the composed order per call site. |
+| `cliChildEnv.js` | The one place the AI-CLI child environment is composed, replacing the hand-rolled copy every spawn site carried — which made each env-level fix an N-file sweep (#3194). `buildCliChildEnv({ baseEnv, before, provider, model, cwd, extra, guard })` returns a COMPLETE env for `spawn`: layers `baseEnv → before → provider.envVars → buildOpencodeEnvVars → extra`, pins `PWD` to `cwd`, strips `CLAUDECODE`, and (with `guard: true`) prepends the pm2 guard shim onto the final `PATH`. `composeProviderEnv({ before, provider, model, extra })` returns just the ordered provider layers, for sites that build a DELTA someone else bases and spawns (the CoS runner payload, a shell-session overlay). The two slots are not interchangeable: `before` sits UNDER `provider.envVars` (forgeTokenEnv/claudeSettingsEnv, so a provider override still wins), `extra` sits OVER it (TERM/COLORTERM for a PTY). `cliChildEnv.test.js` asserts the composed order per call site and **discovers** any new site that hand-rolls the tuple instead of calling these — so the call-site list stays in the test, not in prose here. |
| `cliProviderArgs.js` | Per-CLI argv conventions (`buildCliArgs`) for stdin prompt delivery — dependency-light extraction from runner.js so out-of-process callers (autofixer) can import it. |
| `cliProviderRun.js` | One-shot CLI provider invocation (`pickCliProvider` + `runCliProviderPrompt`) — lightweight path for the autofixer + calendar MCP sync to honor the configured provider/model. |
| `grok.js` | xAI Grok Build (`grok`) provider helpers — id/endpoint constants (`GROK_API_ID`/`GROK_CLI_ID`/`GROK_TUI_ID`/`GROK_API_ENDPOINT`), `isGrokCommand`/`isGrokCliProvider`/`isGrokTuiProvider` predicates, `ensureGrokHeadlessArgs`/`ensureGrokTuiArgs` argv builders (grok reads its prompt from `--prompt-file /dev/stdin`, not raw stdin; model selection uses the `GROK_CONFIGURED_DEFAULT` sentinel in `providerModels.js` so PortOS omits `--model` like Antigravity), and `prepareGrokPromptFile` (Windows temp-file delivery fallback). |
diff --git a/server/lib/cliChildEnv.js b/server/lib/cliChildEnv.js
index 6c648651d6..216b671b9e 100644
--- a/server/lib/cliChildEnv.js
+++ b/server/lib/cliChildEnv.js
@@ -1,42 +1,43 @@
/**
* Child-environment composition for every AI-CLI spawn.
*
- * Five spawn sites — `services/runner.js` (Run Prompt), `lib/cliProviderRun.js`
- * (fire-and-collect), `services/agentCliSpawning.js` (CoS agents),
- * `lib/tuiPromptRunner.js` (PTY TUI runs), and `cos-runner/index.js`
- * (`POST /spawn`) — each independently rebuilt the same env tuple:
+ * Ten sites independently rebuilt the same env layering, and three separate
+ * fixes each paid an N-file sweep to keep them in step: the OpenCode
+ * declared-models map (#2190/#2243), the `CLAUDECODE` strip, and the `PWD` pin
+ * (#3193). Every time, the guard against missing a site was "a reviewer noticed"
+ * or a source-grep test — and one of those sweeps did miss a site (see the
+ * comment at `services/agentLifecycle.js#spawnViaRunner`). This module owns the
+ * composition so the next env-level concern is a one-file change (#3194).
*
- * { ...baseEnv, ...providerEnv, ...opencodeEnv }
- * → pin PWD to the spawn cwd
- * → strip CLAUDECODE
- * → prepend the pm2 guard shim (agent-spawning sites only)
+ * Two entry points, because the sites split into two shapes:
*
- * Three separate fixes each paid an N-file sweep to keep those five in step:
- * the OpenCode declared-models map (#2190/#2243), the `CLAUDECODE` strip, and
- * the `PWD` pin (#3193). Every time, the guard against missing a site was
- * "a reviewer noticed" or a source-grep test. This module owns the composition
- * so the next env-level concern is a one-file change (#3194).
+ * - `buildCliChildEnv` — a COMPLETE child env, handed straight to `spawn`.
+ * - `composeProviderEnv` — just the ordered provider layers, for the sites
+ * that produce a DELTA someone else bases and spawns (the CoS runner
+ * payload, a shell session's env overlay).
*
- * **Precedence is the load-bearing part.** The sites do NOT all layer their
- * extras the same way, and flattening them into one order would silently change
- * which value wins:
+ * The layer ORDER is the load-bearing part, and the sites do not all layer the
+ * same way — flattening them into one order would silently change which value
+ * wins:
*
- * - `agentCliSpawning` puts `forgeTokenEnv`/`claudeSettingsEnv` BEFORE
+ * - the agent spawners put `forgeTokenEnv`/`claudeSettingsEnv` BEFORE
* `provider.envVars`, so an explicit provider `GH_TOKEN` still overrides the
- * repo-owner-pinned one.
- * - `tuiPromptRunner` puts `TERM`/`COLORTERM` AFTER everything, so the PTY
- * always gets a truecolor terminal regardless of provider config.
+ * repo-owner-pinned one;
+ * - the PTY runners put `TERM`/`COLORTERM` AFTER everything, so the terminal
+ * is always truecolor regardless of provider config.
*
* Hence two distinct slots — `before` (lower precedence than the provider) and
- * `extra` (higher) — rather than one `extra`. `cliChildEnv.test.js` asserts the
- * exact composed order for each of the five call sites.
+ * `extra` (higher) — rather than one. `cliChildEnv.test.js` asserts the composed
+ * order for each call site, and fails when a new site hand-rolls it instead.
*
* Deliberately NOT in scope: a `spawnAiCli({ command, args, cwd, env })` wrapper
- * that would also own cwd resolution and the Windows `.cmd` shim. The eight
- * spawn shapes differ too much (child_process vs node-pty, stdio variants,
- * kill-tree wiring) to fold in the same change. `lib/aiToolkit/runner.js` is
- * also out of scope: it is vendored and must not import out to other PortOS
- * modules (see `lib/aiToolkit/CLAUDE.md`), so it keeps its inline pin.
+ * that would also own cwd resolution and the Windows `.cmd` shim. The spawn
+ * shapes differ too much (child_process vs node-pty vs an HTTP hand-off to the
+ * CoS runner, stdio variants, kill-tree wiring) to fold in the same change.
+ * `lib/aiToolkit/runner.js` is also out of scope: it is vendored and must not
+ * import out to other PortOS modules (see `lib/aiToolkit/CLAUDE.md`), and its
+ * spawn is dormant under PortOS's `setCliRunner` override, so it keeps its
+ * inline copy.
*/
import { withSpawnCwdEnv } from './spawnCwd.js';
@@ -44,33 +45,60 @@ import { buildOpencodeEnvVars } from './opencodeConfig.js';
import { agentGuardEnv } from './agentGuard/index.js';
/**
- * Compose the environment an AI-CLI child process is spawned with.
+ * The ordered provider env layers, without a base env, PWD pin, or strip.
*
- * Layering, lowest precedence first:
- * `baseEnv` → `before` → `provider.envVars` → OpenCode env → `extra`
- * then `PWD` is pinned to `cwd`, `CLAUDECODE` is stripped, and — only when
- * `guard` is true — the pm2 guard shim is prepended to the final `PATH`.
+ * Use this for a site that builds a DELTA — an overlay another layer bases and
+ * spawns (`spawnAgentViaRunner`'s `envVars` payload, `createShellSession`'s
+ * `env`). Sites that spawn directly want `buildCliChildEnv` instead.
*
* @param {object} options
- * @param {NodeJS.ProcessEnv|object} [options.baseEnv=process.env] - env to start from.
- * Callers that must not leak host credentials to an autonomous agent (e.g. the
- * autofixer) pass a sanitized allowlist here; every later layer still overlays it.
- * @param {object|null} [options.before] - env spread AFTER `baseEnv` but BEFORE
- * `provider.envVars`, so a provider-configured value still wins over it
- * (forgeTokenEnv, claudeSettingsEnv).
+ * @param {object|null} [options.before] - layered first, so `provider.envVars`
+ * overrides it (forgeTokenEnv, claudeSettingsEnv).
* @param {{command?:string, envVars?:object, models?:string[], defaultModel?:string|null, ollamaBacked?:boolean}|null} [options.provider]
- * - the provider record. `provider.envVars` is layered in, and the provider's
- * command/ollamaBacked flags decide whether an OpenCode config is built.
- * @param {string|null} [options.model] - the model being run this invocation, used
- * to build the OpenCode declared-models map. Pass `provider.defaultModel` when
- * the site has no per-call model.
+ * @param {string|null} [options.model] - the model being run this invocation,
+ * unioned into the OpenCode declared-models map. Omit when the site has no
+ * per-call model — `provider.defaultModel` is always declared regardless.
+ * @param {object|null} [options.extra] - layered last, so it overrides every
+ * other layer including `provider.envVars` (TERM/COLORTERM for a PTY).
+ * @returns {object} a fresh object holding only these layers
+ */
+export function composeProviderEnv({ before = null, provider = null, model = null, extra = null } = {}) {
+ return {
+ ...(before || {}),
+ ...(provider?.envVars || {}),
+ // Rebuilds OPENCODE_CONFIG_CONTENT with a declared models map for OpenCode
+ // Ollama providers (an empty object for everyone else) so the injected
+ // `--model ollama/<id>` isn't rejected as "not valid" — see #2190. It lands
+ // after provider.envVars to override the provider's STATIC
+ // OPENCODE_CONFIG_CONTENT, which it was built from.
+ ...buildOpencodeEnvVars(provider, model),
+ ...(extra || {}),
+ };
+}
+
+/**
+ * Compose the complete environment an AI-CLI child process is spawned with.
+ *
+ * Layering, lowest precedence first: `baseEnv` → the `composeProviderEnv` layers
+ * → then `PWD` pinned to `cwd`, `CLAUDECODE` stripped, and — only when `guard`
+ * is true — the pm2 guard shim prepended to the final `PATH`.
+ *
+ * @param {object} options
+ * @param {NodeJS.ProcessEnv|object} [options.baseEnv=process.env] - env to start
+ * from. Callers that must not leak host credentials to an autonomous agent
+ * (e.g. the autofixer) pass a sanitized allowlist; every later layer still
+ * overlays it.
+ * @param {object|null} [options.before] - see `composeProviderEnv`.
+ * @param {object|null} [options.provider] - see `composeProviderEnv`.
+ * @param {string|null} [options.model] - see `composeProviderEnv`.
* @param {string|undefined|null} options.cwd - the directory being handed to
* `spawn`/`pty.spawn`; `PWD` is pinned to it (see `withSpawnCwdEnv`, #3193).
- * @param {object|null} [options.extra] - env spread LAST, so it overrides every
- * other layer including `provider.envVars` (TERM/COLORTERM for a PTY).
+ * Pass it whenever the spawn names a cwd — omitting it silently reopens #3193,
+ * which is why `spawnCwd.test.js` only counts a call carrying `cwd` as a pin.
+ * @param {object|null} [options.extra] - see `composeProviderEnv`.
* @param {boolean} [options.guard=false] - prepend the pm2 guard shim onto the
* final `PATH` so an unrestricted agent can't `pm2 kill` the shared daemon.
- * Only the two agent-spawning sites opt in; the Run Prompt / fire-and-collect
+ * Only the agent-spawning sites opt in; the Run Prompt / fire-and-collect
* paths keep today's unguarded behavior.
* @returns {object} a fresh env object safe to hand straight to `spawn`
*/
@@ -83,25 +111,13 @@ export function buildCliChildEnv({
extra = null,
guard = false,
} = {}) {
- // buildOpencodeEnvVars rebuilds OPENCODE_CONFIG_CONTENT with a declared models
- // map for OpenCode Ollama providers (an empty object for everyone else) so the
- // injected `--model ollama/<id>` isn't rejected as "not valid" — see #2190.
- // It comes after provider.envVars to override the provider's STATIC
- // OPENCODE_CONFIG_CONTENT, which it was built from.
const env = withSpawnCwdEnv(
- {
- ...baseEnv,
- ...(before || {}),
- ...(provider?.envVars || {}),
- ...buildOpencodeEnvVars(provider, model),
- ...(extra || {}),
- },
+ { ...baseEnv, ...composeProviderEnv({ before, provider, model, extra }) },
cwd,
);
// CLAUDECODE is set when PortOS itself runs inside Claude Code; passing it
- // through would make a spawned Claude CLI think it's nested inside the parent
- // session.
+ // through would make a spawned Claude CLI think it's nested in that session.
delete env.CLAUDECODE;
// Must be applied to the FINAL PATH (after every override above), or a
diff --git a/server/lib/cliChildEnv.test.js b/server/lib/cliChildEnv.test.js
index d3277dd4fd..c9e224d18b 100644
--- a/server/lib/cliChildEnv.test.js
+++ b/server/lib/cliChildEnv.test.js
@@ -1,9 +1,7 @@
import { describe, it, expect } from 'vitest';
-import { readFileSync, readdirSync } from 'fs';
-import { join, relative } from 'path';
-import { fileURLToPath } from 'url';
-import { buildCliChildEnv } from './cliChildEnv.js';
+import { buildCliChildEnv, composeProviderEnv } from './cliChildEnv.js';
import { AGENT_GUARD_BIN } from './agentGuard/index.js';
+import { collectServerSources, readServerSource } from './testHelper.js';
// An OpenCode provider that IS ollama-backed — the only shape for which
// buildOpencodeEnvVars returns anything. Everyone else gets `{}`, which is why
@@ -64,12 +62,11 @@ describe('buildCliChildEnv — layering', () => {
});
it('tolerates an absent provider / before / extra', () => {
- expect(buildCliChildEnv({ baseEnv: { A: '1' } })).toEqual({ A: '1' });
expect(buildCliChildEnv({ baseEnv: { A: '1' }, provider: null, before: null, extra: null })).toEqual({ A: '1' });
});
it('defaults baseEnv to process.env', () => {
- expect(buildCliChildEnv({ cwd: undefined }).PATH).toBe(process.env.PATH);
+ expect(buildCliChildEnv().PATH).toBe(process.env.PATH);
});
it('copies rather than mutating the caller env', () => {
@@ -132,6 +129,44 @@ describe('buildCliChildEnv — pm2 guard', () => {
});
});
+// composeProviderEnv is the same layering WITHOUT a base env, PWD pin, or strip
+// — for the two sites that build a DELTA someone else bases and spawns. Those
+// are exactly the sites an earlier draft of the guard could not see, and where
+// the OpenCode sweep was missed once before.
+describe('composeProviderEnv — delta for sites that do not spawn directly', () => {
+ it('emits only the provider layers, with no base env, PWD, or strip', () => {
+ const delta = composeProviderEnv({
+ before: { GH_TOKEN: 'forge' },
+ provider: { envVars: { GH_TOKEN: 'provider', CLAUDECODE: '1' } },
+ });
+ // No process.env keys leak in — the consumer supplies the base.
+ expect(delta).toEqual({ GH_TOKEN: 'provider', CLAUDECODE: '1' });
+ // And no PWD is invented for a caller that has no cwd to pin.
+ expect(delta.PWD).toBeUndefined();
+ });
+
+ it('keeps the same layer order buildCliChildEnv uses', () => {
+ expect(composeProviderEnv({
+ before: { K: 'before' },
+ provider: { envVars: { K: 'provider' } },
+ extra: { K: 'extra' },
+ }).K).toBe('extra');
+ });
+
+ it('declares the OpenCode models map for the runner payload (#2243/#2190)', () => {
+ // agentLifecycle hands this to the cos-runner over HTTP, which has no
+ // provider record of its own — so the map has to be baked in HERE or the
+ // runner-spawned agent rejects its own --model.
+ const delta = composeProviderEnv({ provider: OLLAMA_OPENCODE, model: 'llama3.1:8b' });
+ expect(declaredModels(delta).sort()).toEqual(['llama3.1:8b', 'qwen2.5:7b']);
+ });
+
+ it('is what buildCliChildEnv layers over its base env', () => {
+ const layers = { before: { A: '1' }, provider: { envVars: { B: '2' } }, extra: { C: '3' } };
+ expect(buildCliChildEnv({ baseEnv: {}, ...layers })).toEqual(composeProviderEnv(layers));
+ });
+});
+
// One case per real call site, asserting the precedence THAT site depends on.
// The sites do not all layer the same way, so a single "extra wins" rule would
// have silently changed two of them — these pin the actual contracts.
@@ -145,10 +180,10 @@ describe('buildCliChildEnv — per-call-site composition', () => {
});
it('cliProviderRun.js: honors a sanitized baseEnv instead of process.env', () => {
- // The autofixer passes an allowlist so host credentials never reach the CLI.
+ // The autofixer passes an allowlist so host credentials never reach the CLI —
+ // so the builder must not smuggle process.env back in under it.
const env = buildCliChildEnv({ baseEnv: { PATH: '/usr/bin' }, provider: { envVars: {} }, cwd: '/w' });
- expect(env.HOME).toBeUndefined();
- expect(env.PATH).toBe('/usr/bin');
+ expect(Object.keys(env).sort()).toEqual(['PATH', 'PWD']);
});
it('agentCliSpawning.js: forgeToken/claudeSettings sit UNDER provider.envVars', () => {
@@ -215,22 +250,15 @@ describe('buildCliChildEnv — per-call-site composition', () => {
// an allowlist of "these files must call the builder" passes the day someone
// adds a ninth spawn site, which is exactly when the guard needs to fire.
describe('no spawn site rebuilds the CLI child env by hand', () => {
- const SERVER_DIR = fileURLToPath(new URL('..', import.meta.url));
-
// Files allowed to compose the tuple themselves, each with the reason.
const EXEMPT = new Map([
['lib/cliChildEnv.js', 'this module IS the shared composer'],
- ['lib/aiToolkit/runner.js', 'vendored toolkit — must not import out to other PortOS modules'],
+ // Worth stating both halves: the import constraint is why it cannot call the
+ // composer, and the dormancy is why its missing CLAUDECODE strip / OpenCode
+ // map is not a live PortOS gap someone needs to chase.
+ ['lib/aiToolkit/runner.js', 'vendored toolkit — must not import out to other PortOS modules, and its spawn is dormant under PortOS\'s setCliRunner override'],
]);
- const collectSources = (dir) => readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
- if (entry.name === 'node_modules' || entry.name.startsWith('.')) return [];
- const abs = join(dir, entry.name);
- if (entry.isDirectory()) return collectSources(abs);
- if (!entry.name.endsWith('.js') || entry.name.endsWith('.test.js')) return [];
- return [relative(SERVER_DIR, abs)];
- });
-
// Two independent markers, because either one alone has a blind spot: a new
// site could strip CLAUDECODE without spreading provider.envVars, or spread
// provider.envVars while forgetting the strip (which is itself the bug).
@@ -239,45 +267,51 @@ describe('no spawn site rebuilds the CLI child env by hand', () => {
// strips it, and nothing else in the tree does.
const STRIPS_CLAUDECODE = /delete\s+[A-Za-z_$][\w$]*\.CLAUDECODE\b/;
- // Marker B — a provider-env-over-base composition that is handed to a child
- // running in a workspace. The `cwd` requirement is what separates a real spawn
- // env from the two shapes that merge the same objects for a different purpose
- // and correctly need none of this: a model-id LOOKUP env
- // (`resolveBedrockCliModel({ env })`, `resolveWindowsExecutable(…, searchEnv)`)
- // and a capability PROBE (`agy models`, `--version`) — neither runs a model,
- // writes files, or has a workspace to be misrouted into.
+ // Marker B — a `provider.envVars` spread that reaches a child process.
//
- // A window rather than a parser: the composition is always one object literal.
- // KNOWN BLIND SPOT, stated rather than papered over: a real spawn env built
- // with NO cwd (askService's Ask path is the one such shape today) is invisible
- // to this marker. Marker A still catches it as long as the new site strips
- // CLAUDECODE — and a site that does not strip it has the very bug marker B
- // was never going to be able to name.
+ // Keyed on the spread + the handoff, NOT on a `...process.env` base. An
+ // earlier draft anchored on the base env and had to be widened once already
+ // (for cliProviderRun's `baseEnv` parameter name) — and it still could not see
+ // the two sites that compose a DELTA someone else bases: agentLifecycle's
+ // runner payload and agentTuiSpawning's shell overlay, which is exactly where
+ // the OpenCode sweep was missed once before. Requiring only the spread and a
+ // nearby handoff catches both shapes.
+ //
+ // The handoff requirement is what separates a real child env from the two
+ // shapes that merge the same objects for a different purpose and correctly
+ // need none of this: a model-id LOOKUP env (`resolveBedrockCliModel({ env })`,
+ // `resolveWindowsExecutable(…, searchEnv)`) and a capability PROBE
+ // (`agy models`, `--version`) — neither runs a model, writes files, nor has a
+ // workspace to be misrouted into.
+ //
+ // A window rather than a parser, sized from the real tree: the widest real gap
+ // is ~1400 chars (agentTuiSpawning's `env:` overlay sits that far below its
+ // `createShellSession(`, behind a long comment block), so the back-window is
+ // 1500. Verified empirically — at 1500 the only files flagged across all of
+ // `server/` are the two EXEMPT ones, and every pre-refactor hand-rolled site
+ // is caught. Over-matching costs one EXEMPT line; under-matching silently
+ // reopens the N-file sweep, so the bias is deliberate.
+ //
+ // The optional `(` in the spread pattern matters: `...(provider.envVars || {})`
+ // is the shape two sites use, and a pattern without it reads them as clean.
+ const HANDS_OFF_TO_CHILD = /\b(?:spawn|ptySpawn|spawnImpl|pty\.spawn|createShellSession|spawnAgentViaRunner)\s*\(|\benvVars:/;
const SPREADS_PROVIDER_ENV_INTO_SPAWN = (src) => {
- for (const m of src.matchAll(/\.\.\.\s*(?:process\.env|baseEnv)\b/g)) {
- // Wide enough to span the comment blocks that sit between the composition
- // and the spawn call it feeds (the vendored toolkit runner has ~500 chars
- // of them). Over-matching costs one EXEMPT line; under-matching silently
- // reopens the N-file sweep, so the bias is deliberate.
- const window = src.slice(m.index, m.index + 900);
- if (/\.\.\.\s*[A-Za-z_$][\w$]*\??\.envVars\b/.test(window) && /\bcwd\s*[:,]/.test(window)) return true;
+ for (const m of src.matchAll(/\.\.\.\s*\(?\s*[A-Za-z_$][\w$]*\??\.envVars\b/g)) {
+ if (HANDS_OFF_TO_CHILD.test(src.slice(Math.max(0, m.index - 1500), m.index + 900))) return true;
}
return false;
};
- const offenders = () => collectSources(SERVER_DIR).filter((rel) => {
- if (EXEMPT.has(rel)) return false;
- const src = readFileSync(join(SERVER_DIR, rel), 'utf8');
- return STRIPS_CLAUDECODE.test(src) || SPREADS_PROVIDER_ENV_INTO_SPAWN(src);
- });
+ const isOffender = (src) => STRIPS_CLAUDECODE.test(src) || SPREADS_PROVIDER_ENV_INTO_SPAWN(src);
+ const offenders = () => collectServerSources()
+ .filter((rel) => !EXEMPT.has(rel) && isOffender(readServerSource(rel)));
it('finds the exempt files (guard is not vacuous)', () => {
// If the markers stop matching even the composer itself, the scan broke and
// the assertion below would pass for the wrong reason.
for (const rel of EXEMPT.keys()) {
- const src = readFileSync(join(SERVER_DIR, rel), 'utf8');
expect(
- STRIPS_CLAUDECODE.test(src) || SPREADS_PROVIDER_ENV_INTO_SPAWN(src),
+ isOffender(readServerSource(rel)),
`${rel} no longer matches either marker — the scan broke`,
).toBe(true);
}
diff --git a/server/lib/cliProviderRun.js b/server/lib/cliProviderRun.js
index dae9d49a48..2bbdc44b1b 100644
--- a/server/lib/cliProviderRun.js
+++ b/server/lib/cliProviderRun.js
@@ -111,7 +111,6 @@ export function runCliProviderPrompt(args = {}) {
const childEnv = buildCliChildEnv({
baseEnv,
provider: effectiveProvider,
- model: effectiveProvider.defaultModel,
cwd: effectiveCwd,
});
diff --git a/server/lib/spawnCwd.test.js b/server/lib/spawnCwd.test.js
index 2b3443245a..d96d1442a4 100644
--- a/server/lib/spawnCwd.test.js
+++ b/server/lib/spawnCwd.test.js
@@ -1,9 +1,9 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
-import { mkdtempSync, rmSync, writeFileSync, readFileSync, readdirSync } from 'fs';
+import { mkdtempSync, rmSync, writeFileSync } from 'fs';
import { tmpdir, homedir } from 'os';
-import { join, relative } from 'path';
-import { fileURLToPath } from 'url';
+import { join } from 'path';
import { resolveSpawnCwd, withSpawnCwdEnv } from './spawnCwd.js';
+import { collectServerSources, readServerSource } from './testHelper.js';
describe('resolveSpawnCwd', () => {
let dir;
@@ -198,8 +198,7 @@ describe('withSpawnCwdEnv', () => {
// cwd-passing spawn fails the suite until it is either pinned or explicitly
// exempted below with a reason.
describe('every cwd-passing spawn pins PWD', () => {
- const SERVER_DIR = fileURLToPath(new URL('..', import.meta.url));
-
+
// Files whose cwd-passing spawn does NOT need the pin, each with the reason it
// is safe. Anything not listed here must pin. The common reason is "this spawns
// a tool that resolves paths from its real cwd, never from PWD" — git, gh,
@@ -240,14 +239,19 @@ describe('every cwd-passing spawn pins PWD', () => {
const SPAWN_CALL_RE = /\b(?:spawn|ptySpawn|spawnImpl|pty\.spawn|bufferedSpawn|execFile|execAsync)\s*\(/g;
- /** Recursively collect non-test .js files under `dir`, as server-relative paths. */
- const collectSources = (dir) => readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
- if (entry.name === 'node_modules' || entry.name.startsWith('.')) return [];
- const abs = join(dir, entry.name);
- if (entry.isDirectory()) return collectSources(abs);
- if (!entry.name.endsWith('.js') || entry.name.endsWith('.test.js')) return [];
- return [relative(SERVER_DIR, abs)];
- });
+ /**
+ * Count `buildCliChildEnv({ … cwd … })` calls — the shared AI-CLI env composer
+ * (#3194), which pins PWD internally. Only a call that actually PASSES a cwd
+ * counts: `cwd` is an optional key there, so a bare `buildCliChildEnv({ provider })`
+ * next to a `spawn(cmd, args, { cwd })` would otherwise satisfy this guard while
+ * reintroducing #3193 verbatim. `withSpawnCwdEnv(x, cwd)` can't be called without
+ * one, so requiring the key keeps the two markers equally strong.
+ *
+ * A window rather than a brace match, because an argument object nests
+ * (`before: { … }`) and `[^}]*` would stop at the inner brace.
+ */
+ const countCwdBearingComposerCalls = (src) => [...src.matchAll(/\bbuildCliChildEnv\s*\(/g)]
+ .filter((m) => /\bcwd\s*[:,}]/.test(src.slice(m.index, m.index + 400))).length;
// A spawn is in scope when its options carry a `cwd` — either written inline
// (`spawn(cmd, args, { cwd, ... })`) or FORWARDED from a caller-supplied
@@ -291,7 +295,7 @@ describe('every cwd-passing spawn pins PWD', () => {
};
it('discovers the cwd-passing spawn sites (guard is not vacuous)', () => {
- const found = collectSources(SERVER_DIR).filter((rel) => spawnSitesInScope(readFileSync(join(SERVER_DIR, rel), 'utf8')) > 0);
+ const found = collectServerSources().filter((rel) => spawnSitesInScope(readServerSource(rel)) > 0);
// If this ever collapses toward zero the scan broke (a rename, a new spawn
// wrapper) and every assertion below would pass for the wrong reason.
expect(found.length, 'expected the scan to still find the known spawn sites').toBeGreaterThan(10);
@@ -307,15 +311,15 @@ describe('every cwd-passing spawn pins PWD', () => {
// A stale entry is worse than no entry: it silently pre-approves whatever
// cwd-passing spawn is added to that file later.
it('has no dead EXEMPT / DELEGATES / INLINE_PIN entries', () => {
- const inScope = new Set(collectSources(SERVER_DIR).filter((rel) => spawnSitesInScope(readFileSync(join(SERVER_DIR, rel), 'utf8')) > 0));
+ const inScope = new Set(collectServerSources().filter((rel) => spawnSitesInScope(readServerSource(rel)) > 0));
const dead = [...EXEMPT.keys(), ...DELEGATES.keys(), ...INLINE_PIN.keys()].filter((rel) => !inScope.has(rel));
expect(dead, 'these entries name files the scan no longer finds — delete them').toEqual([]);
});
it('every cwd-passing spawn either pins PWD or is explicitly exempt', () => {
const unpinned = [];
- for (const rel of collectSources(SERVER_DIR)) {
- const src = readFileSync(join(SERVER_DIR, rel), 'utf8');
+ for (const rel of collectServerSources()) {
+ const src = readServerSource(rel);
const sites = spawnSitesInScope(src);
if (sites === 0) continue;
if (EXEMPT.has(rel)) continue;
@@ -325,11 +329,19 @@ describe('every cwd-passing spawn pins PWD', () => {
// unpinned cwd-spawn is added to it.
// Two markers count as a pin: the low-level helper, and buildCliChildEnv —
// the shared AI-CLI env composer, which calls withSpawnCwdEnv internally
- // (#3194). Counting the composer here is what lets the five AI-CLI spawn
- // sites collapse to one call each without going dark to this guard.
+ // (#3194). Counting the composer here is what lets the AI-CLI spawn sites
+ // collapse to one call each without going dark to this guard.
+ //
+ // The composer's marker REQUIRES a `cwd` in the call. `withSpawnCwdEnv(x, cwd)`
+ // cannot be called without one, but `buildCliChildEnv({ … })` takes `cwd` as an
+ // optional key — so a bare `buildCliChildEnv({ provider })` followed by a
+ // `spawn(cmd, args, { cwd })` would otherwise satisfy this guard while
+ // reintroducing #3193 verbatim. Requiring the key keeps the two markers
+ // equally strong. (askService.js legitimately calls it with no cwd — it also
+ // spawns with no cwd, so it is not in scope here at all.)
const pins = INLINE_PIN.has(rel)
? (src.match(/childEnv\.PWD\s*=/g) || []).length
- : (src.match(/(?:withSpawnCwdEnv|buildCliChildEnv)\(/g) || []).length;
+ : (src.match(/withSpawnCwdEnv\(/g) || []).length + countCwdBearingComposerCalls(src);
if (pins < sites) unpinned.push(`${rel} (${sites} cwd-passing spawn(s), ${pins} pin(s))`);
}
expect(
diff --git a/server/lib/testHelper.js b/server/lib/testHelper.js
index d2b5d881e1..16396cc263 100644
--- a/server/lib/testHelper.js
+++ b/server/lib/testHelper.js
@@ -5,6 +5,12 @@
*/
import { createServer } from 'http';
+import { readdirSync, readFileSync } from 'fs';
+import { join, relative } from 'path';
+import { fileURLToPath } from 'url';
+
+/** The `server/` root — the scan root for the source-guard helpers below. */
+export const SERVER_DIR = fileURLToPath(new URL('..', import.meta.url));
function startServer(app) {
return new Promise((resolve, reject) => {
@@ -132,3 +138,31 @@ export function mockJsonResponse(body, { ok = true, status = 200 } = {}) {
export function mockTextResponse(body = '', { ok = true, status = 200 } = {}) {
return { ok, status, text: async () => body };
}
+
+/**
+ * Every non-test `.js` file under `server/`, as server-relative paths.
+ *
+ * Shared by the source-scanning guard suites — `spawnCwd.test.js` (every
+ * cwd-passing spawn pins PWD, #3193) and `cliChildEnv.test.js` (every AI-CLI
+ * spawn composes its env through the shared builder, #3194). Those two guards
+ * deliberately overlap, so they must agree on what "a source file" is: a change
+ * to the ignore rules here (a new extension, a skipped directory) has to apply
+ * to both, or one guard silently stops covering files the other still checks.
+ *
+ * @param {string} [dir] - directory to walk (defaults to the `server/` root)
+ * @returns {string[]} paths relative to `server/`, e.g. `services/runner.js`
+ */
+export function collectServerSources(dir = SERVER_DIR) {
+ return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
+ if (entry.name === 'node_modules' || entry.name.startsWith('.')) return [];
+ const abs = join(dir, entry.name);
+ if (entry.isDirectory()) return collectServerSources(abs);
+ if (!entry.name.endsWith('.js') || entry.name.endsWith('.test.js')) return [];
+ return [relative(SERVER_DIR, abs)];
+ });
+}
+
+/** Read a source file named by a `collectServerSources()` path. */
+export function readServerSource(rel) {
+ return readFileSync(join(SERVER_DIR, rel), 'utf8');
+}
diff --git a/server/lib/tuiPromptRunner.js b/server/lib/tuiPromptRunner.js
index ad42ecab0b..5940c199d3 100644
--- a/server/lib/tuiPromptRunner.js
+++ b/server/lib/tuiPromptRunner.js
@@ -178,7 +178,6 @@ ${prompt}`;
// `guard`: this is a Run Prompt TUI, not an autonomous agent.
const childEnv = buildCliChildEnv({
provider,
- model: provider.defaultModel,
cwd: workingDir,
extra: { TERM: 'xterm-256color', COLORTERM: 'truecolor' },
});
diff --git a/server/lib/tuiUsageScrape.js b/server/lib/tuiUsageScrape.js
index 1c3114bac7..70a631d7d4 100644
--- a/server/lib/tuiUsageScrape.js
+++ b/server/lib/tuiUsageScrape.js
@@ -96,15 +96,13 @@ export async function scrapeTuiUsage({
mkdirSync(sandboxDir, { recursive: true });
- // Shared composition (provider envVars + PWD pin + CLAUDECODE strip) — see
- // buildCliChildEnv. `extraEnv` is the provider's auth/config env, handed in
- // loose rather than as a provider record; with no command to classify, the
- // OpenCode layer is a no-op here. TERM/COLORTERM go in `extra` so the PTY is
- // always a truecolor xterm regardless of provider config (mirrors
- // tuiPromptRunner.js). The PWD pin keeps the scrape in the throwaway sandbox
- // instead of opening the PortOS checkout as a project.
+ // TERM/COLORTERM go in `extra` so the PTY is always a truecolor xterm
+ // regardless of the provider's own env (mirrors tuiPromptRunner.js). The PWD
+ // pin keeps the scrape in the throwaway sandbox instead of opening the PortOS
+ // checkout as a project. No provider record here — this drives a slash command,
+ // not a model, so there is nothing for the OpenCode layer to declare.
const env = buildCliChildEnv({
- provider: { envVars: extraEnv },
+ before: extraEnv,
cwd: sandboxDir,
extra: { TERM: 'xterm-256color', COLORTERM: 'truecolor' },
});
diff --git a/server/services/agentLifecycle.js b/server/services/agentLifecycle.js
index 73bc053d13..bf50d3fe45 100644
--- a/server/services/agentLifecycle.js
+++ b/server/services/agentLifecycle.js
@@ -38,7 +38,7 @@ import { analyzeAgentFailure } from './agentErrorAnalysis.js';
import { createAgentRun, checkForTaskCommit } from './agentRunTracking.js';
import { buildAgentPrompt, getAppWorkspace } from './agentPromptBuilder.js';
import { isOllamaClaudeProvider, isClaudeCommand, providerSuppliesGithubToken } from '../lib/providerModels.js';
-import { buildOpencodeEnvVars } from '../lib/opencodeConfig.js';
+import { composeProviderEnv } from '../lib/cliChildEnv.js';
import { PROVIDER_TYPES } from '../lib/aiToolkit/constants.js';
import { buildCliSpawnConfig, isClaudeCliProvider, isTuiProvider, getClaudeSettingsEnv, spawnDirectly } from './agentCliSpawning.js';
import { buildTuiSpawnConfig, spawnTuiAgent } from './agentTuiSpawning.js';
@@ -687,22 +687,23 @@ export async function spawnViaRunner(agentId, task, opts) {
providerSuppliesGithubToken(provider) ? Promise.resolve({}) : resolveForgeTokenEnv(workspacePath),
]);
- // For OpenCode Ollama providers, build dynamic OPENCODE_CONFIG_CONTENT with the
- // models map so the injected `--model ollama/<id>` is accepted (empty/no-op
- // otherwise). The direct-spawn path (agentCliSpawning.spawnDirectly) and the
- // "Run Prompt" path (server/services/runner.js) already do this; the runner
- // path omitted it, so an OpenCode Ollama CoS task on the runner would reject
- // the model even after the spawn itself succeeds. See issue #2243 / #2190.
- const opencodeEnv = buildOpencodeEnvVars(provider, model);
-
const result = await spawnAgentViaRunner({
agentId,
taskId: task.id,
prompt,
workspacePath,
model,
- // forgeTokenEnv before provider.envVars so an explicit provider override wins.
- envVars: { ...forgeTokenEnv, ...claudeSettingsEnv, ...provider.envVars, ...opencodeEnv },
+ // A DELTA, not a full env — the cos-runner bases it on its own process.env
+ // and does the PWD pin / CLAUDECODE strip. composeProviderEnv owns the layer
+ // order: forgeTokenEnv before provider.envVars so an explicit provider
+ // override wins, and the OpenCode declared-models map after it so the
+ // injected `--model ollama/<id>` is accepted (#2243/#2190 — this path was
+ // the site that sweep originally missed).
+ envVars: composeProviderEnv({
+ before: { ...forgeTokenEnv, ...claudeSettingsEnv },
+ provider,
+ model,
+ }),
cliCommand: cliConfig.command,
cliArgs: cliConfig.args
});
diff --git a/server/services/agentLifecycle.test.js b/server/services/agentLifecycle.test.js
index d8b34a6e32..1bd9e06700 100644
--- a/server/services/agentLifecycle.test.js
+++ b/server/services/agentLifecycle.test.js
@@ -441,31 +441,44 @@ describe('runAgentSpawn source — instance provenance + claim ordering (#1563)'
});
});
+// These used to be three source-regex assertions pinning a hand-spread env
+// literal (`...forgeTokenEnv, ...provider.envVars, ...opencodeEnv`) inside
+// spawnViaRunner. The layering now lives in `lib/cliChildEnv.js#composeProviderEnv`,
+// where `cliChildEnv.test.js` asserts the ORDER against the real function instead
+// of against source text — which is the point of #3194: a grep-shaped guard was
+// what let this exact site miss the #2243/#2190 sweep in the first place.
+//
+// What is still worth pinning HERE is the wiring: that spawnViaRunner routes
+// through the shared composer and feeds it the right inputs.
describe('agentLifecycle — runner OpenCode Ollama env (#2243 / #2190)', () => {
- it('source: imports buildOpencodeEnvVars from opencodeConfig', () => {
+ const runnerBody = () => {
+ const fnStart = AGENT_LIFECYCLE_SRC.indexOf('export async function spawnViaRunner');
+ expect(fnStart, 'spawnViaRunner must exist').toBeGreaterThan(-1);
+ return AGENT_LIFECYCLE_SRC.slice(fnStart, fnStart + 4000);
+ };
+
+ it('source: composes the runner envVars through the shared composeProviderEnv', () => {
expect(AGENT_LIFECYCLE_SRC).toMatch(
- /import\s*\{\s*buildOpencodeEnvVars\s*\}\s*from\s*'\.\.\/lib\/opencodeConfig\.js';/
+ /import\s*\{\s*composeProviderEnv\s*\}\s*from\s*'\.\.\/lib\/cliChildEnv\.js';/
);
+ // The payload must BE the composed env — not a literal that re-spreads it,
+ // which is how the layer order drifted from the other spawn sites before.
+ expect(runnerBody()).toMatch(/envVars:\s*composeProviderEnv\(\{/);
});
- it('source: spawnViaRunner merges buildOpencodeEnvVars into the runner envVars so --model ollama/<id> is accepted', () => {
- const fnStart = AGENT_LIFECYCLE_SRC.indexOf('export async function spawnViaRunner');
- expect(fnStart, 'spawnViaRunner must exist').toBeGreaterThan(-1);
- const fnBody = AGENT_LIFECYCLE_SRC.slice(fnStart, fnStart + 4000);
- const buildIdx = fnBody.indexOf('buildOpencodeEnvVars(provider, model)');
- expect(buildIdx, 'must build the opencode env from provider+model').toBeGreaterThan(-1);
- expect(fnBody).toMatch(/envVars:\s*\{[^}]*\.\.\.opencodeEnv[^}]*\}/);
- expect(fnBody.indexOf('...opencodeEnv'), 'opencodeEnv must be spread AFTER provider.envVars so it overrides the static config')
- .toBeGreaterThan(fnBody.indexOf('...provider.envVars'));
+ it('source: feeds the composer the provider + per-call model so --model ollama/<id> is accepted', () => {
+ const fnBody = runnerBody();
+ const call = fnBody.slice(fnBody.indexOf('composeProviderEnv({'), fnBody.indexOf('composeProviderEnv({') + 300);
+ expect(call, 'the OpenCode declared-models map is built from provider+model').toContain('provider,');
+ expect(call).toContain('model,');
});
- it("source: spawnViaRunner pins GH_TOKEN via resolveForgeTokenEnv so the runner-spawned agent's `gh` uses the repo-owner account", () => {
- const fnStart = AGENT_LIFECYCLE_SRC.indexOf('export async function spawnViaRunner');
- const fnBody = AGENT_LIFECYCLE_SRC.slice(fnStart, fnStart + 4000);
+ it("source: pins GH_TOKEN via resolveForgeTokenEnv so the runner-spawned agent's `gh` uses the repo-owner account", () => {
+ const fnBody = runnerBody();
expect(fnBody).toContain('resolveForgeTokenEnv(workspacePath)');
- expect(fnBody).toMatch(/envVars:\s*\{[^}]*\.\.\.forgeTokenEnv[^}]*\}/);
- expect(fnBody.indexOf('...forgeTokenEnv'), 'forgeTokenEnv must be spread BEFORE provider.envVars')
- .toBeLessThan(fnBody.indexOf('...provider.envVars'));
+ // `before` is the slot that sits UNDER provider.envVars, so an explicit
+ // provider GH_TOKEN still wins — passing it as `extra` would invert that.
+ expect(fnBody).toMatch(/before:\s*\{[^}]*\.\.\.forgeTokenEnv[^}]*\}/);
});
});
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index 45894a11fe..1a2ab881d7 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -66,7 +66,7 @@ import {
isPasteConfirmed,
} from '../lib/tuiHandshake.js';
import { agentGuardEnv } from '../lib/agentGuard/index.js';
-import { buildOpencodeEnvVars } from '../lib/opencodeConfig.js';
+import { composeProviderEnv } from '../lib/cliChildEnv.js';
import { execFile } from 'child_process';
// Agent-specific timing/lifecycle constants (not shared with the one-shot
@@ -103,9 +103,6 @@ const DONE_POLL_INTERVAL_MS = 2000;
* to bail out via its `finish` path.
*/
export function createAgentTuiSession({ agentId, provider, model, tuiConfig, cwd, forgeTokenEnv = {}, onData, onExit, onInitialCommandSent }) {
- // For OpenCode Ollama providers, build dynamic OPENCODE_CONFIG_CONTENT with
- // the models map so --model is accepted (the static env var lacked this).
- const opencodeEnv = buildOpencodeEnvVars(provider, model);
const sessionId = shellService.createShellSession(null, {
cwd,
kind: 'agent-tui',
@@ -122,15 +119,19 @@ export function createAgentTuiSession({ agentId, provider, model, tuiConfig, cwd
// claude's input-readiness only after this so the readiness probe's own
// shell activity can't prematurely open the paste gate.
onInitialCommandSent,
- // agentGuardEnv() prepends the pm2 shim to the agent session's PATH (and
- // points it at the real pm2). Spread LAST so it wins over any provider PATH.
- // Only AI agent sessions get this — the user's own Shell page does not.
- // opencodeEnv comes after provider.envVars to override the static config.
- // forgeTokenEnv is threaded in explicitly because buildSafeEnv strips
- // GH_TOKEN from the inherited env (see resolveForgeTokenEnv); it goes before
- // provider.envVars so an explicit provider GH_TOKEN override still wins,
- // matching the direct-CLI and runner spawn paths.
- env: { ...forgeTokenEnv, ...(provider.envVars || {}), ...opencodeEnv, ...agentGuardEnv() },
+ // A DELTA, not a full env — buildSafeEnv inside createShellSession supplies
+ // the base and shell.js does the PWD pin. composeProviderEnv owns the layer
+ // order (forgeTokenEnv before provider.envVars so an explicit provider
+ // GH_TOKEN still wins; the OpenCode declared-models map after it, overriding
+ // the static config). forgeTokenEnv has to be threaded in explicitly because
+ // buildSafeEnv strips GH_TOKEN from the inherited env (resolveForgeTokenEnv).
+ //
+ // agentGuardEnv() is spread last so the pm2 shim wins over any provider PATH.
+ // It reads PATH from process.env rather than the composed env — correct here
+ // and NOT what buildCliChildEnv's `guard` does, because this is an overlay
+ // whose real base env is assembled downstream. Only AI agent sessions get
+ // the shim; the user's own Shell page does not.
+ env: { ...composeProviderEnv({ before: forgeTokenEnv, provider, model }), ...agentGuardEnv() },
onData,
onExit,
});
diff --git a/server/services/runner.js b/server/services/runner.js
index 821b51b602..ae3f85426c 100644
--- a/server/services/runner.js
+++ b/server/services/runner.js
@@ -266,7 +266,6 @@ export async function executeCliRun({ runId, provider, prompt, workspacePath, on
// shared daemon.
const childEnv = buildCliChildEnv({
provider,
- model: provider.defaultModel,
cwd: effectiveCwd,
guard: true,
});