Skip to content
12 changes: 10 additions & 2 deletions src/main/ipc/pty/host-env/opencode-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { openCode2HookService, openCodeHookService } from '../../../opencode/hoo
import { resolveOpenCodeSourceConfigDir, restoreOrStripOverlayEnv } from './pi-agent'
import { selectOpenCodeHookAgent } from '../../../../shared/opencode-launch-command'
import { isTuiAgentEnabled } from '../../../../shared/tui-agent-selection'
import { resolveOpenCodeConfigDirectory } from '../../../../shared/opencode-config-directory'
import { readSessionShellStartupEnvVar } from '../../../pty/shell-startup-env'
import type { BuildPtyHostEnvOptions } from './types'

type OpenCodeSourceConfig = {
Expand Down Expand Up @@ -41,7 +43,7 @@ export function applyOpenCodeStatusPluginEnv(
config: OpenCodeSourceConfig,
options: Pick<
BuildPtyHostEnvOptions,
'launchAgent' | 'agentStatusHooksEnabled' | 'disabledTuiAgents' | 'isWsl'
'launchAgent' | 'agentStatusHooksEnabled' | 'disabledTuiAgents' | 'isWsl' | 'shellPath'
>,
command: string | undefined
): 'opencode' | 'opencode2' | null {
Expand Down Expand Up @@ -69,7 +71,13 @@ export function applyOpenCodeStatusPluginEnv(
env.ORCA_OPENCODE_AGENT = agent
// WSL owns its config writes; only the guest overlay may enter a WSL pane.
if (!options.isWsl) {
Object.assign(env, service.buildPtyEnv(id, config.directory))
const executionEnv = { ...process.env, ...env }
const shellConfigHome = readSessionShellStartupEnvVar('XDG_CONFIG_HOME', env, options.shellPath)
const defaultConfigDir = resolveOpenCodeConfigDirectory({
...executionEnv,
XDG_CONFIG_HOME: shellConfigHome ?? executionEnv.XDG_CONFIG_HOME
})
Object.assign(env, service.buildPtyEnv(id, config.directory, defaultConfigDir))
}
if (env.OPENCODE_CONFIG_DIR) {
// Shell startup can re-export the default; preserve this pane's overlay and original source.
Expand Down
79 changes: 79 additions & 0 deletions src/main/ipc/pty/host-env/opencode-hook-installation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,85 @@ afterEach(() => {
})

describe('OpenCode installation uses the current enabled agents', () => {
it.each(
process.platform === 'win32' ? (['home', 'xdg'] as const) : (['home', 'xdg', 'shell'] as const)
)(
'consumer config root follows the %s execution environment without redirecting JSON',
(kind) => {
const home = join(root, 'consumer-home')
const xdg = join(root, 'consumer-xdg')
const consumer = kind === 'home' ? join(home, '.config', 'opencode') : join(xdg, 'opencode')
mkdirSync(home, { recursive: true })
mkdirSync(consumer, { recursive: true })
const settings = '{"model":"consumer-model","description":"external-settings"}'
writeFileSync(join(consumer, 'opencode.json'), settings)
if (kind === 'shell') {
writeFileSync(join(home, '.zshrc'), `export XDG_CONFIG_HOME='${xdg}'\n`)
}
const input = {
HOME: home,
USERPROFILE: home,
XDG_CONFIG_HOME: kind === 'xdg' ? xdg : '',
SHELL: kind === 'shell' ? '/bin/zsh' : '/bin/sh'
}
const env = buildPtyHostEnv(
'folder-pane',
{ ...input },
{ ...options, launchAgent: 'opencode' }
)
expect(existsSync(plugin(consumer, 'opencode'))).toBe(true)
expect(existsSync(plugin(config, 'opencode'))).toBe(false)
expect(readFileSync(join(consumer, 'opencode.json'), 'utf8')).toBe(settings)
expect(env.OPENCODE_CONFIG_DIR).toBeUndefined()
for (const key of ['HOME', 'USERPROFILE', 'XDG_CONFIG_HOME', 'SHELL'] as const) {
expect(env[key]).toBe(input[key])
}
}
)

it.skipIf(process.platform === 'win32').each([
{ selected: '/bin/bash', inherited: '/bin/zsh', file: '.bash_profile' },
{ selected: '/bin/zsh', inherited: '/bin/bash', file: '.zshrc' }
])('consumer config follows selected $selected over inherited $inherited', (selection) => {
const home = join(root, 'selected-shell-home')
const selectedXdg = join(root, 'selected-shell-xdg')
const inheritedXdg = join(root, 'inherited-shell-xdg')
mkdirSync(home)
writeFileSync(
join(home, '.bash_profile'),
`export XDG_CONFIG_HOME='${selection.file === '.bash_profile' ? selectedXdg : inheritedXdg}'\n`
)
writeFileSync(
join(home, '.zshrc'),
`export XDG_CONFIG_HOME='${selection.file === '.zshrc' ? selectedXdg : inheritedXdg}'\n`
)
const input = { HOME: home, USERPROFILE: home, XDG_CONFIG_HOME: '', SHELL: selection.inherited }
const launchOptions = {
...options,
launchAgent: 'opencode' as const,
shellPath: selection.selected
}
const env = buildPtyHostEnv('selected-shell-pane', input, launchOptions)
expect(existsSync(plugin(join(selectedXdg, 'opencode'), 'opencode'))).toBe(true)
expect(existsSync(plugin(join(inheritedXdg, 'opencode'), 'opencode'))).toBe(false)
expect(env.SHELL).toBe(selection.inherited)
expect(env.OPENCODE_CONFIG_DIR).toBeUndefined()
})

it.each([false, true])('consumer config root is untouched with hooks disabled %s', (disabled) => {
const consumer = join(root, 'disabled-consumer')
buildPtyHostEnv(
'pane',
{ XDG_CONFIG_HOME: consumer },
{
...options,
agentStatusHooksEnabled: disabled,
disabledTuiAgents: disabled ? ['opencode', 'opencode2'] : []
}
)
expect(existsSync(join(consumer, 'opencode'))).toBe(false)
})

it('refuses spawn if a prepared startup intent loses its owned installer', () => {
mkdirSync(fixture.userData, { recursive: true })
writeFileSync(
Expand Down
2 changes: 2 additions & 0 deletions src/main/ipc/pty/host-env/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type BuildPtyHostEnvOptions = {
launchCommand?: string
/** Trusted agent identity for wrapped commands that cannot be recognized from text. */
launchAgent?: TuiAgent
/** Selected execution shell, which may differ from inherited SHELL. */
shellPath?: string
isWsl?: boolean
/** Distro for WSL spawns (null = Windows default distro); drives the WSL hook relay + endpoint repoint. Only read when isWsl. */
wslDistro?: string | null
Expand Down
1 change: 1 addition & 0 deletions src/main/ipc/pty/ipc/spawn-env-codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export async function assemblePtyIpcSpawnCodexEnv(ctx: PtyIpcSpawnState): Promis
stripInheritedOrcaCodexHome: ctx.stripInheritedOrcaCodexHome,
launchCommand: ctx.launchCommand,
launchAgent: isTuiAgent(args.launchAgent) ? args.launchAgent : undefined,
shellPath: ctx.effectiveShellOverride,
isWsl: shouldSkipCodexHomeEnvForWindowsShell(ctx.effectiveShellOverride, ctx.cwd),
wslDistro: ctx.codexSelectionTarget.runtime === 'wsl' ? ctx.expectedWslDistro : null,
agentStatusHooksEnabled: isAgentStatusHooksEnabled(ptySettings),
Expand Down
1 change: 1 addition & 0 deletions src/main/ipc/pty/provider/local-configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export function configureLocalPtyProvider(args: {
}),
launchCommand: ctx?.command,
launchAgent: ctx?.launchAgent,
shellPath: ctx?.shellPath,
isWsl: ctx?.isWsl,
wslDistro: ctx?.wslDistro ?? null,
agentStatusHooksEnabled: isAgentStatusHooksEnabled(ptySettings),
Expand Down
1 change: 1 addition & 0 deletions src/main/ipc/pty/runtime/spawn-preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export async function prepareRuntimePtySpawn(
stripInheritedOrcaCodexHome: ctx.stripInheritedOrcaCodexHome,
launchCommand: ctx.launchCommand,
launchAgent: isTuiAgent(args.launchAgent) ? args.launchAgent : undefined,
shellPath: ctx.daemonShellOverride,
isWsl: shouldSkipCodexHomeEnvForWindowsShell(ctx.daemonShellOverride, ctx.cwd),
wslDistro: ctx.codexSelectionTarget.runtime === 'wsl' ? ctx.expectedWslDistro : null,
agentStatusHooksEnabled: isAgentStatusHooksEnabled(ptySettings),
Expand Down
8 changes: 6 additions & 2 deletions src/main/opencode/hook-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ export class OpenCodeHookService {
}
}

buildPtyEnv(ptyId: string, existingConfigDir?: string | undefined): Record<string, string> {
buildPtyEnv(
ptyId: string,
existingConfigDir?: string | undefined,
defaultConfigDir?: string
): Record<string, string> {
if (!isUsableId(ptyId)) {
// Why: on a bad id, still preserve a user-set OPENCODE_CONFIG_DIR; only the Orca status plugin is forfeited.
return existingConfigDir ? { OPENCODE_CONFIG_DIR: existingConfigDir } : {}
Expand All @@ -131,7 +135,7 @@ export class OpenCodeHookService {
const managedConfigDir = this.getSharedConfigDir()
if (!existingConfigDir || existingConfigDir === managedConfigDir) {
try {
this.writePluginToConfigDir(resolveOpenCodeConfigDirectory())
this.writePluginToConfigDir(defaultConfigDir ?? resolveOpenCodeConfigDirectory())
return {}
} catch {
return {}
Expand Down
35 changes: 35 additions & 0 deletions src/relay/opencode-hook-selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ vi.mock('node-pty', () => ({ spawn: mocks.mockPtySpawn }))
vi.mock('../main/shell-prompt-readiness-probe', () => ({
createShellPromptReadinessProbe: mocks.mockCreateShellPromptReadinessProbe
}))
vi.mock('../main/opencode/opencode-launch-capabilities', () => ({
probeOpenCodeLaunchCapabilities: async () => null
}))
vi.mock('../main/pty/posix-pty-process-groups', () => ({
forceKillPosixPtyProcessGroups: vi.fn((_pid: number, fallback: () => void) => fallback())
}))
Expand Down Expand Up @@ -94,6 +97,38 @@ async function spawn(params: Record<string, unknown> = {}): Promise<Record<strin
}

describe('relay OpenCode source selection on real fixture files', () => {
it.each(['home', 'xdg', 'shell'] as const)(
'consumer config root follows the remote %s environment',
async (kind) => {
await install('// remote plugin', '')
const home = join(root, 'remote-home')
const xdg = join(root, 'remote-xdg')
const consumer = kind === 'home' ? join(home, '.config', 'opencode') : join(xdg, 'opencode')
mkdirSync(home, { recursive: true })
mkdirSync(consumer, { recursive: true })
writeFileSync(join(consumer, 'opencode.json'), '{"model":"remote-model"}')
if (kind === 'shell') {
writeFileSync(join(home, '.zshrc'), `export XDG_CONFIG_HOME='${xdg}'\n`)
}
const env = await spawn({
cwd: home,
launchAgent: 'opencode',
env: {
HOME: home,
USERPROFILE: home,
XDG_CONFIG_HOME: kind === 'xdg' ? xdg : '',
SHELL: kind === 'shell' ? '/bin/zsh' : '/bin/sh'
}
})
expect(readFileSync(plugin(consumer, 'opencode'), 'utf8')).toBe('// remote plugin')
expect(existsSync(plugin(join(root, 'xdg', 'opencode'), 'opencode'))).toBe(false)
expect(readFileSync(join(consumer, 'opencode.json'), 'utf8')).toBe('{"model":"remote-model"}')
expect(env.OPENCODE_CONFIG_DIR).toBeUndefined()
expect(env.HOME).toBe(home)
expect(env.XDG_CONFIG_HOME).toBe(kind === 'xdg' ? xdg : '')
}
)

it('leaves a standalone relay without supplied sources unconfigured', async () => {
const env = await spawn()
expect(env.ORCA_OPENCODE_AGENT).toBeUndefined()
Expand Down
8 changes: 7 additions & 1 deletion src/relay/relay-agent-hook-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { selectOpenCodeHookAgent } from '../shared/opencode-launch-command'
import { relayLogLine } from './relay-diagnostic-log'
import { restoreOrStripOverlayEnv } from '../shared/agent-overlay-env'
import { registerManagedHookInstaller } from './managed-hook-installer'
import { readSessionShellStartupEnvVar } from '../main/pty/shell-startup-env'

export class RelayAgentHookRuntime {
private readonly hookServer: RelayAgentHookServer
Expand Down Expand Up @@ -122,7 +123,12 @@ export class RelayAgentHookRuntime {
env.ORCA_OPENCODE_SOURCE_CONFIG_DIR = sourceDir
}
} else {
this.pluginOverlay.installOpenCodePlugin(opencodeAgent, context.env)
this.pluginOverlay.installOpenCodePlugin(opencodeAgent, {
...context.env,
XDG_CONFIG_HOME:
readSessionShellStartupEnvVar('XDG_CONFIG_HOME', context.env, context.shell) ??
context.env.XDG_CONFIG_HOME
})
}
}
const explicitKind = isPiCompatibleAgentType(context.launchAgent)
Expand Down
45 changes: 45 additions & 0 deletions src/shared/opencode-config-directory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { afterEach, describe, expect, it } from 'vitest'
import { join } from 'node:path'
import { resolveOpenCodeConfigDirectory } from './opencode-config-directory'

const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform')
afterEach(() => {
if (originalPlatform) {
Object.defineProperty(process, 'platform', originalPlatform)
}
})

describe('OpenCode consumer config directory', () => {
it('uses the execution HOME instead of the installer home on POSIX', () => {
Object.defineProperty(process, 'platform', { value: 'linux' })
expect(resolveOpenCodeConfigDirectory({ HOME: '/consumer' }, '/installer')).toBe(
join('/consumer', '.config', 'opencode')
)
})

it('uses the execution USERPROFILE on Windows', () => {
Object.defineProperty(process, 'platform', { value: 'win32' })
expect(
resolveOpenCodeConfigDirectory({ USERPROFILE: '/consumer', HOME: '/shell' }, '/installer')
).toBe(join('/consumer', '.config', 'opencode'))
})

it('keeps the host home fallback when the execution home is absent', () => {
expect(resolveOpenCodeConfigDirectory({}, '/installer')).toBe(
join('/installer', '.config', 'opencode')
)
})

it('does not treat a Windows shell HOME as the native OpenCode home', () => {
Object.defineProperty(process, 'platform', { value: 'win32' })
expect(resolveOpenCodeConfigDirectory({ HOME: '/shell' }, '/installer')).toBe(
join('/installer', '.config', 'opencode')
)
})

it('keeps XDG_CONFIG_HOME ahead of the execution home', () => {
expect(
resolveOpenCodeConfigDirectory({ HOME: '/consumer', XDG_CONFIG_HOME: '/xdg' }, '/installer')
).toBe(join('/xdg', 'opencode'))
})
})
6 changes: 5 additions & 1 deletion src/shared/opencode-config-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ export function resolveOpenCodeConfigDirectory(
environment: NodeJS.ProcessEnv | Record<string, string> = process.env,
homeDirectory = homedir()
): string {
return join(environment.XDG_CONFIG_HOME?.trim() || join(homeDirectory, '.config'), 'opencode')
const executionHome = process.platform === 'win32' ? environment.USERPROFILE : environment.HOME
return join(
environment.XDG_CONFIG_HOME?.trim() || join(executionHome || homeDirectory, '.config'),
'opencode'
)
}
Loading