Skip to content

The agent that grows with you

fix(desktop): pooled profile backends resolve their own cwd, not the app-global one

Brooklyn Nicholson20ae6087ffa5
diff --git a/apps/desktop/electron/backend-env.test.ts b/apps/desktop/electron/backend-env.test.ts
index 95ac34b2aac..805a1724796 100644
--- a/apps/desktop/electron/backend-env.test.ts
+++ b/apps/desktop/electron/backend-env.test.ts
@@ -10,6 +10,7 @@ import {
buildDesktopBackendEnv,
normalizeHermesHomeRoot,
pathEnvKey,
+ pooledProfileBackendEnv,
POSIX_SANE_PATH_ENTRIES,
profileBackendParentEnv
} from './backend-env'
@@ -274,3 +275,58 @@ test('Windows matches profile homes and dotenv names case-insensitively', () =>
assert.deepEqual(scoped('default'), currentEnv)
assert.deepEqual(scoped('urbot'), { HERMES_HOME: currentEnv.HERMES_HOME, Path: 'C:\\Windows' })
})
+
+test('a pooled profile backend drops the launch profile TERMINAL_CWD (#87584)', () => {
+ withHermesRoot(ROOT_SCOPE_FILES, root => {
+ // The Desktop env carries the app-global cwd (resolveHermesCwd) and the
+ // runtime may add its own; both name the LAUNCH profile's workspace.
+ const env = pooledProfileBackendEnv({
+ hermesHome: root,
+ profile: 'urbot',
+ currentEnv: { ...ROOT_LAUNCHED_ENV, TERMINAL_CWD: '/source/launch-workspace' },
+ backendEnv: { TERMINAL_CWD: '/stale/runtime-workspace', KEEP_BACKEND: '1' },
+ platform: 'linux'
+ })
+
+ assert.equal(env.TERMINAL_CWD, undefined)
+ assert.equal(env.HERMES_HOME, root, 'the child resolves --profile under the Desktop-resolved root')
+ assert.equal(env.KEEP_BACKEND, '1')
+ // The named-profile dotenv scrub (#68367) still applies on top.
+ assert.equal(env.TLON_SHIP_CODE, undefined)
+ assert.equal(env.OP_SERVICE_ACCOUNT_TOKEN, undefined)
+ assert.equal(env.OPENROUTER_API_KEY, 'shell-key')
+ })
+})
+
+test('a pooled backend removes case-insensitive TERMINAL_CWD on Windows', () => {
+ withHermesRoot(ROOT_SCOPE_FILES, root => {
+ const env = pooledProfileBackendEnv({
+ hermesHome: root,
+ profile: 'urbot',
+ currentEnv: { Path: 'C:\\Windows', Terminal_Cwd: 'C:\\source\\workspace' },
+ backendEnv: { terminal_cwd: 'C:\\backend' },
+ platform: 'win32'
+ })
+
+ assert.equal(env.Terminal_Cwd, undefined)
+ assert.equal(env.terminal_cwd, undefined)
+ assert.equal(env.Path, 'C:\\Windows')
+ })
+})
+
+test('the launch profile pooled backend keeps its TERMINAL_CWD', () => {
+ withHermesRoot(ROOT_SCOPE_FILES, root => {
+ // profile 'default' targets the root home: no dotenv scrub, and the pin
+ // survives only when the caller (main.ts) provides it — the pooled helper
+ // itself never stamps one, it only strips inherited/global values.
+ const env = pooledProfileBackendEnv({
+ hermesHome: root,
+ profile: 'default',
+ currentEnv: { TERMINAL_CWD: '/launch-workspace', PATH: '/usr/bin' },
+ platform: 'linux'
+ })
+
+ assert.equal(env.TERMINAL_CWD, undefined, 'the pooled helper never carries the global pin; main.ts owns that decision')
+ assert.equal(env.PATH, '/usr/bin')
+ })
+})
diff --git a/apps/desktop/electron/backend-env.ts b/apps/desktop/electron/backend-env.ts
index 792290a9953..9498da88c51 100644
--- a/apps/desktop/electron/backend-env.ts
+++ b/apps/desktop/electron/backend-env.ts
@@ -244,12 +244,61 @@ function buildDesktopBackendEnv({
}
}
+/**
+ * Spawn env for a POOLED per-profile backend (`spawnPoolBackend`).
+ *
+ * TERMINAL_CWD is the LAUNCH profile's resolved workspace. A pooled child
+ * serves ANOTHER profile (`--profile X`): stamping the app-global cwd makes
+ * that profile's placeholder/unset `terminal.cwd` sessions inherit the launch
+ * (or another) profile's workspace (#87584). Drop TERMINAL_CWD from the
+ * inherited env AND from any runtime-provided mapping (case-insensitively on
+ * Windows); the `--profile` child re-resolves its own cwd from its profile
+ * config, the same way a standalone `hermes -p X serve` would. The launch
+ * profile's own (primary) backend keeps the pin in main.ts.
+ */
+function pooledProfileBackendEnv({
+ hermesHome,
+ profile,
+ currentEnv = process.env,
+ backendEnv = {},
+ platform = process.platform,
+ fsModule = fs,
+ pathModule = pathModuleForPlatform(platform)
+}: any = {}) {
+ const parent = profileBackendParentEnv({ hermesHome, profile, currentEnv, platform, fsModule, pathModule })
+ const fold = platform === 'win32' ? (value: string) => value.toUpperCase() : (value: string) => value
+ const isTerminalCwd = (key: string) => fold(key) === 'TERMINAL_CWD'
+
+ const env = { ...parent }
+
+ for (const key of Object.keys(env)) {
+ if (isTerminalCwd(key)) {
+ delete env[key]
+ }
+ }
+
+ // The resolved root, not process.env's: on Windows it can come from the user
+ // registry, and the --profile child resolves its home under it.
+ env.HERMES_HOME = hermesHome
+
+ for (const [key, value] of Object.entries(backendEnv || {})) {
+ if (isTerminalCwd(key)) {
+ continue
+ }
+
+ env[key] = value
+ }
+
+ return env
+}
+
export {
appendUniquePathEntries,
buildDesktopBackendEnv,
delimiterForPlatform,
normalizeHermesHomeRoot,
pathEnvKey,
+ pooledProfileBackendEnv,
POSIX_SANE_PATH_ENTRIES,
profileBackendParentEnv,
storeFirstPath
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index a2ca2fa6835..f98793129c2 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -73,7 +73,7 @@ import { dashboardFallbackArgs, serveBackendArgs } from './backend-command'
import { createBackendConnectionState } from './backend-connection-state'
import { BackendDialClaims } from './backend-dial-claim'
import type { HostBackendRecord } from './backend-discovery'
-import { buildDesktopBackendEnv, profileBackendParentEnv } from './backend-env'
+import { buildDesktopBackendEnv, pooledProfileBackendEnv, profileBackendParentEnv } from './backend-env'
import { createBackendExitRecoveryLatch } from './backend-exit-recovery'
import { isReauthRequiredError, waitForHermesReady } from './backend-health'
import {
@@ -12340,14 +12340,12 @@ async function runPoolBackendStart(
cwd: hermesCwd,
env: desktopBackendSpawnEnv(
{
- // Never another profile's dotenv credentials from the Desktop env (#68367).
- ...profileBackendParentEnv({ hermesHome: HERMES_HOME, profile }),
- HERMES_HOME,
- ...backend.env,
- // Pin the gateway's tool/terminal cwd to the same directory we chose for
- // the child process. Inherited TERMINAL_CWD (or a stale config bridge)
- // can still point at the install dir even when spawn cwd is home.
- TERMINAL_CWD: hermesCwd,
+ // Never another profile's dotenv credentials from the Desktop env (#68367), and never
+ // the launch profile's TERMINAL_CWD: a pooled backend serves THIS --profile, whose
+ // placeholder/unset terminal.cwd sessions must resolve their own workspace, not the
+ // app-global one (#87584). The child re-resolves from its profile config like a
+ // standalone `hermes -p X serve`.
+ ...pooledProfileBackendEnv({ hermesHome: HERMES_HOME, profile, backendEnv: backend.env }),
HERMES_DASHBOARD_SESSION_TOKEN: token,
// Marks this dashboard backend as desktop-spawned so it runs the cron
// scheduler tick loop (the gateway isn't running under the app).