Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/main/acp/acp-structured-launch-resolution-omp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ describe('OMP ACP launch resolution', () => {
ANTHROPIC_API_KEY: 'sk-test'
}
const launch = await resolver(ompRecord(), { launchEnv: userEnv })({ identity })
// Only the shared pane and hook keys, and an inherited scratch folder: this chat has none.
// Only an inherited scratch folder (this chat has none); the seal strips pane and hook keys.
expect(launch.envToDelete).toEqual([
...STRUCTURED_CHILD_ENV_TO_DELETE,
NATIVE_CHAT_SCRATCH_DIR_ENV,
LEGACY_NATIVE_CHAT_VISUALS_DIR_ENV
])
Expand Down
2 changes: 0 additions & 2 deletions src/main/acp/acp-structured-launch-resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
spelledProviderTimelineItemKey
} from '../native-chat/agent-session-timeline/provider-timeline-identity'
import type { StructuredAgentLaunchPartResolver } from '../runtime/structured-agent-launch-composition'
import { STRUCTURED_CHILD_ENV_TO_DELETE } from '../runtime/structured-session-child-env'
import {
LEGACY_NATIVE_CHAT_VISUALS_DIR_ENV,
NATIVE_CHAT_SCRATCH_DIR_ENV,
Expand Down Expand Up @@ -251,7 +250,6 @@ export function acpStructuredLaunchPart(
},
env: withNativeChatVisualsEnv({ ...env, ...skill?.env }, skill ? visuals : null),
inheritedEnvToDelete: [
...STRUCTURED_CHILD_ENV_TO_DELETE,
...envToDelete,
// Without visuals, a folder Orca itself inherited (started from a chat) names another chat's.
...(skill ? [] : [NATIVE_CHAT_SCRATCH_DIR_ENV]),
Expand Down
18 changes: 18 additions & 0 deletions src/main/ipc/pty/host-env/spawn-env-keys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { ORCA_AGENT_SESSION_ID_ENV } from '../../../../shared/agent-session-caller-env'
import {
ORCA_SCRUB_SAFE_LAUNCH_ENV,
ORCA_SCRUB_SAFE_PANE_ENV
} from '../../../../shared/agent-hook-scrub-safe-env'
import { ORCA_STRUCTURED_SESSION_ENV } from '../../../../shared/structured-session-marker'

// Why: what an Orca pane names its shell by; any other process holding them was handed another pane's identity.
export const ORCA_PANE_IDENTITY_ENV_KEYS = [
'ORCA_PANE_KEY',
'ORCA_TAB_ID',
'ORCA_WORKTREE_ID',
'ORCA_WORKSPACE_ID',
'ORCA_PROJECT_GROUP_ID',
'ORCA_WORKSPACE_ROOT',
'ORCA_AGENT_LAUNCH_TOKEN',
ORCA_SCRUB_SAFE_PANE_ENV,
ORCA_SCRUB_SAFE_LAUNCH_ENV,
'ORCA_TERMINAL_HANDLE'
] as const

export const AGENT_HOOK_RUNTIME_ENV_KEYS = [
'ORCA_AGENT_HOOK_PORT',
'ORCA_AGENT_HOOK_TOKEN',
Expand Down
6 changes: 3 additions & 3 deletions src/main/orcad/orcad-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
changedAiVaultSearchSettings,
type AiVaultSearchSettings
} from '../../shared/ai-vault-search-settings'
import { stampRuntimeSourceEnv } from '../../shared/runtime-source-env'
import { dropInheritedLaunchIdentityEnv } from '../startup/inherited-launch-identity-env'
import { adoptWslHostLoginEnvironment } from './orcad-wsl-host-login-environment'

export { parseArgs }
Expand Down Expand Up @@ -70,8 +70,8 @@ export type OrcadHandle = {
*/
export async function startOrcad(options: OrcadOptions = {}): Promise<OrcadHandle> {
installOrcadHostAdapters()
// Why: an orcad started from an Orca terminal must not fence its own CLI children to that Orca.
stampRuntimeSourceEnv(process.env, null)
// Why: an orcad started from an Orca terminal or chat is neither, and must not fence its CLI children to that Orca.
dropInheritedLaunchIdentityEnv(process.env)
const { readiness, instance, stop } = await startOrcadWithHost(
resolveUserDataPath(),
(registerCleanup) => startOrcadRuntime(options, registerCleanup),
Expand Down
6 changes: 2 additions & 4 deletions src/main/pi/rpc-launch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ const CALLER_ENV_TO_DELETE = [
ORCA_AGENT_SESSION_SPAWN_TOKEN_ENV
]

/** What a structured Pi session strips from its inheritance; the seal keeps a worker's handle.
* Pi gets no scratch folder, so an inherited one can only name another chat's. */
/** What a structured Pi session strips beyond the seal's pane identity: Pi gets no scratch
* folder, so an inherited one can only name another chat's. */
export const PI_RPC_SESSION_ENV_TO_DELETE: readonly string[] = [
...STRUCTURED_CHILD_ENV_TO_DELETE,
'ORCA_TERMINAL_HANDLE',
NATIVE_CHAT_SCRATCH_DIR_ENV,
LEGACY_NATIVE_CHAT_VISUALS_DIR_ENV
]
Expand Down

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions src/main/runtime/structured-agent-launch-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ import {
LEGACY_NATIVE_CHAT_VISUALS_DIR_ENV,
NATIVE_CHAT_SCRATCH_DIR_ENV
} from '../native-chat/native-chat-visuals-delivery'
import { dropInheritedLaunchIdentityEnv } from '../startup/inherited-launch-identity-env'
import type { StructuredAgentSessionAcquireInput } from '../native-chat/agent-session-wire/structured-agent-session-adapter'
import type { StructuredAgentSessionEventSink } from '../native-chat/agent-session-wire/structured-agent-session-event-sink'
import type { JournalHostDatabase } from '../native-chat/agent-session-journal/journal-host-database'
Expand Down Expand Up @@ -296,6 +297,14 @@ function settingsFor(launch: LaunchCase): Partial<GlobalSettings> {
}
}

/** This Orca's environment when it was started from a parent Orca pane, after startup dropped
* that pane's identity as every host entry does. */
function hostEnvironment(): Record<string, string> {
const env = inheritedEnvironment()
dropInheritedLaunchIdentityEnv(env)
return env
}

/** Every pane, hook, caller, handle, visuals and auth key a parent Orca pane could hand down. */
function inheritedEnvironment(): Record<string, string> {
const env: Record<string, string> = {
Expand Down Expand Up @@ -556,7 +565,7 @@ beforeAll(async () => {
for (const directory of ['worktree', 'floating', 'floating-pinned', 'home', 'state']) {
await mkdir(join(root, directory), { recursive: true })
}
process.env = inheritedEnvironment()
process.env = hostEnvironment()
installAppEnvironment()
settings = defaultSettings()
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the host wiring reads only the store's getSettings method.
Expand Down Expand Up @@ -597,7 +606,7 @@ beforeAll(async () => {
// and each case inherits the same environment whatever ran before it.
beforeEach(() => {
installAppEnvironment()
process.env = inheritedEnvironment()
process.env = hostEnvironment()
})

afterAll(async () => {
Expand Down
58 changes: 58 additions & 0 deletions src/main/runtime/structured-session-child-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SEALED_TEST_SPAWN_TOKEN,
sealedChildEnv
} from './structured-session-child-env.test-support'
import { sealStructuredSessionChild } from './structured-session-child-env'
import {
mintStructuredWorkerHandle,
mintStructuredWorkerPaneKey,
Expand Down Expand Up @@ -224,6 +225,63 @@ describe('sealStructuredSessionChild identity', () => {
expect(Object.keys(env).filter((key) => key.includes('PANE'))).toEqual([])
})

it.each(['chat', 'worker'] as const)(
'strips a pane identity a settings or shell overlay supplies, for a %s of any agent',
(kind) => {
installFakeAppEnvironment({ isPackaged: () => false, getPath: () => USER_DATA })
const handle = kind === 'worker' ? registerWorker() : null
const sealed = sealStructuredSessionChild({
sessionId: SESSION_ID,
spawnToken: SEALED_TEST_SPAWN_TOKEN,
env: {
PATH: '/usr/bin',
ORCA_PANE_KEY: 'tab-parent:leaf-parent',
ORCA_WORKSPACE_ID: 'global-floating-terminal',
ORCA_TERMINAL_HANDLE: 'term_parent',
ORCA_AGENT_HOOK_PORT: '4100'
},
// An agent that asks for no deletions of its own still loses the pane identity.
inheritedEnvToDelete: []
})
expect(sealed.env).not.toHaveProperty('ORCA_PANE_KEY')
expect(sealed.env).not.toHaveProperty('ORCA_WORKSPACE_ID')
expect(sealed.env).not.toHaveProperty('ORCA_AGENT_HOOK_PORT')
expect(sealed.env.ORCA_TERMINAL_HANDLE).toBe(handle ?? undefined)
expect(sealed.envToDelete).toEqual(
expect.arrayContaining(['ORCA_PANE_KEY', 'ORCA_WORKSPACE_ID', 'ORCA_AGENT_HOOK_PORT'])
)
expect(sealed.envToDelete.includes('ORCA_TERMINAL_HANDLE')).toBe(handle === null)
}
)

it('on Windows, strips a pane identity an overlay spells in any case, and nothing else', () => {
// Windows reads a child's environment case-insensitively, so `orca_pane_key` still names a pane.
pinPlatform('win32')
installFakeAppEnvironment({ isPackaged: () => false, getPath: () => USER_DATA })
const sealed = sealStructuredSessionChild({
sessionId: SESSION_ID,
spawnToken: SEALED_TEST_SPAWN_TOKEN,
env: {
Path: 'C:\\Windows',
orca_pane_key: 'tab-parent:leaf-parent',
Orca_Agent_Hook_Endpoint: 'C:\\hook-endpoint',
orca_terminal_handle: 'term_parent',
// A caller's deliberate lowercase override is not identity and stays.
opencode_config_dir: 'C:\\opencode'
},
inheritedEnvToDelete: []
})
// The host env reached here after startup dropped the identity in every casing.
const child = resolveProviderChildEnv(sealed, { SystemRoot: 'C:\\Windows' })

const named = Object.keys(child).filter((key) =>
/pane_key|hook_endpoint|terminal_handle/i.test(key)
)
expect(named).toEqual([])
expect(child.opencode_config_dir).toBe('C:\\opencode')
expect(child.ORCA_AGENT_SESSION_ID).toBe(SESSION_ID)
})

it('never names the WSL-scoped launcher, because a structured worker cannot run in WSL', () => {
// `orca-ide` is the literal the PTY lane exports for WSL only. A structured session that
// resolves to a WSL distro is refused a host scope, so it never becomes a worker at all —
Expand Down
49 changes: 31 additions & 18 deletions src/main/runtime/structured-session-child-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,22 @@ import {
ORCA_AGENT_SESSION_ID_ENV,
ORCA_AGENT_SESSION_SPAWN_TOKEN_ENV
} from '../../shared/agent-session-caller-env'
import {
ORCA_SCRUB_SAFE_LAUNCH_ENV,
ORCA_SCRUB_SAFE_PANE_ENV
} from '../../shared/agent-hook-scrub-safe-env'
import { ORCA_STRUCTURED_SESSION_ENV } from '../../shared/structured-session-marker'
import { withCliRuntimeOnPath } from '../../shared/node-cli-command-resolution'
import { prependOrcaCliDirToChildPath } from '../cli/orca-cli-child-path'
import { AGENT_HOOK_RUNTIME_ENV_KEYS } from '../ipc/pty/host-env/spawn-env-keys'
import {
AGENT_HOOK_RUNTIME_ENV_KEYS,
ORCA_PANE_IDENTITY_ENV_KEYS
} from '../ipc/pty/host-env/spawn-env-keys'
import { resolveStructuredWorkerIdentityForSession } from './structured-worker-authority'

/**
* A pane's identity in the inherited environment would let the agent's own Orca status hooks
* report for this session too; the structured session is its one status producer.
* A pane's identity would let the agent's own Orca status hooks report for this session too; the
* structured session is its one status producer. The seal strips it for every agent, from whatever
* environment source supplied it, so no agent can opt out.
*/
export const STRUCTURED_CHILD_ENV_TO_DELETE: readonly string[] = [
'ORCA_PANE_KEY',
'ORCA_TAB_ID',
'ORCA_WORKTREE_ID',
'ORCA_AGENT_LAUNCH_TOKEN',
ORCA_SCRUB_SAFE_PANE_ENV,
ORCA_SCRUB_SAFE_LAUNCH_ENV,
...ORCA_PANE_IDENTITY_ENV_KEYS,
...AGENT_HOOK_RUNTIME_ENV_KEYS
]

Expand All @@ -87,9 +82,9 @@ export type SealedStructuredSessionChild = {
}

/**
* `env` named as this session's child. `inheritedEnvToDelete` is required so every agent states
* what it strips, none included; a key the seal itself names the child by is never stripped (a
* registered worker keeps its handle). `cliRuntimeCommand`: a Node CLI whose own directory goes
* `env` named as this session's child. `inheritedEnvToDelete` is what the agent strips beyond
* STRUCTURED_CHILD_ENV_TO_DELETE, none included; a key the seal itself names the child by is never
* stripped (a registered worker keeps its handle). `cliRuntimeCommand`: a Node CLI whose own directory goes
* first on PATH, after the identity's, so it runs on the runtime beside it.
*/
export function sealStructuredSessionChild(input: {
Expand All @@ -99,7 +94,7 @@ export function sealStructuredSessionChild(input: {
inheritedEnvToDelete: readonly string[]
cliRuntimeCommand?: string
}): SealedStructuredSessionChild {
const identity = childIdentity(input.sessionId, input.env)
const identity = childIdentity(input.sessionId, withoutPaneIdentity(input.env))
const named = new Set([
ORCA_AGENT_SESSION_ID_ENV,
ORCA_STRUCTURED_SESSION_ENV,
Expand All @@ -111,10 +106,28 @@ export function sealStructuredSessionChild(input: {
: identity.env
return {
env: { ...env, [ORCA_AGENT_SESSION_SPAWN_TOKEN_ENV]: input.spawnToken },
envToDelete: input.inheritedEnvToDelete.filter((key) => !named.has(key))
envToDelete: [
...new Set([...STRUCTURED_CHILD_ENV_TO_DELETE, ...input.inheritedEnvToDelete])
].filter((key) => !named.has(key))
}
}

/** On Windows in any casing: the child reads its environment case-insensitively, so an
* `orca_pane_key` from a settings or shell overlay still names a pane there. The inherited host
* env needs no such pass; startup already dropped these keys from Windows' case-insensitive env. */
function withoutPaneIdentity(env: Record<string, string>): Record<string, string> {
const identityKeys = new Set(
STRUCTURED_CHILD_ENV_TO_DELETE.map((key) =>
process.platform === 'win32' ? key.toUpperCase() : key
)
)
return Object.fromEntries(
Object.entries(env).filter(
([key]) => !identityKeys.has(process.platform === 'win32' ? key.toUpperCase() : key)
)
)
}

function childIdentity(
sessionId: string,
childEnv: Record<string, string>
Expand Down
11 changes: 8 additions & 3 deletions src/main/startup/configure-process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,23 +394,28 @@ describe('configureOrcaUserDataPathEnv', () => {
expect(configuredUserDataPath).toBe('/tmp/current-orca-user-data')
})

it('drops a runtime source inherited from the Orca terminal that started this Orca', async () => {
it('drops a runtime source and pane inherited from the Orca terminal that started this Orca', async () => {
const { app } = await import('electron')
const { configureOrcaUserDataPathEnv } = await import('./configure-process')
const saved = { ...process.env }
process.env.ORCA_RUNTIME_SOURCE_ID = 'parent-source'
process.env.ORCA_RUNTIME_SOURCE_INCARNATION = 'parent-runtime'
process.env.ORCA_PANE_KEY = 'tab-parent:leaf-parent'
app.setPath('userData', '/tmp/current-orca-user-data')
let inherited: (string | undefined)[] = []

try {
configureOrcaUserDataPathEnv()
inherited = [process.env.ORCA_RUNTIME_SOURCE_ID, process.env.ORCA_RUNTIME_SOURCE_INCARNATION]
inherited = [
process.env.ORCA_RUNTIME_SOURCE_ID,
process.env.ORCA_RUNTIME_SOURCE_INCARNATION,
process.env.ORCA_PANE_KEY
]
} finally {
process.env = saved
}

expect(inherited).toEqual([undefined, undefined])
expect(inherited).toEqual([undefined, undefined, undefined])
})
})

Expand Down
6 changes: 3 additions & 3 deletions src/main/startup/configure-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getMainE2EConfig } from '../e2e-config'
import { DISABLED_CHROMIUM_FEATURES } from './disabled-chromium-features'
import { readHttp1CompatibilityMarker } from './http1-compatibility-marker'
import { checkServeUserDataPath } from './serve-user-data-path-guard'
import { stampRuntimeSourceEnv } from '../../shared/runtime-source-env'
import { dropInheritedLaunchIdentityEnv } from './inherited-launch-identity-env'
import { isLinuxWaylandSession } from '../../shared/linux-wayland-session'
import {
hasMissingProfileStateDatabaseWithRetainedAuthority,
Expand Down Expand Up @@ -245,8 +245,8 @@ export function configureOrcaUserDataPathEnv(): void {
}
// Why: relaunches can inherit a stale ORCA_USER_DATA_PATH; canonicalize before CLI-shared modules build runtime-home paths.
process.env.ORCA_USER_DATA_PATH = userDataPath
// Why: an Orca started from another Orca's terminal must not fence its own CLI children to the parent.
stampRuntimeSourceEnv(process.env, null)
// Why: an Orca started from an Orca terminal or chat is neither, and must not fence its CLI children to the parent.
dropInheritedLaunchIdentityEnv(process.env)
}

export function shouldInstallManagedHooks(isDev: boolean): boolean {
Expand Down
38 changes: 38 additions & 0 deletions src/main/startup/inherited-launch-identity-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest'
import { dropInheritedLaunchIdentityEnv } from './inherited-launch-identity-env'

describe('dropInheritedLaunchIdentityEnv', () => {
it('drops the pane and chat identity of the Orca terminal that started this host', () => {
const parentIdentity = {
ORCA_PANE_KEY: 'tab-parent:leaf-parent',
ORCA_TAB_ID: 'tab-parent',
ORCA_WORKTREE_ID: 'global-floating-terminal',
ORCA_WORKSPACE_ID: 'global-floating-terminal',
ORCA_PROJECT_GROUP_ID: 'parent-group',
ORCA_WORKSPACE_ROOT: '/parent/folder',
ORCA_AGENT_LAUNCH_TOKEN: 'parent-launch',
ORCA_AGENT_PANE: 'tab-parent:leaf-parent',
ORCA_AGENT_LAUNCH: 'parent-launch',
ORCA_TERMINAL_HANDLE: 'term_parent',
ORCA_AGENT_HOOK_PORT: '4100',
ORCA_AGENT_HOOK_TOKEN: 'parent-hook-token',
ORCA_AGENT_HOOK_ENDPOINT: '/parent/endpoint.env',
ORCA_AGENT_SESSION_ID: 'parent-chat',
ORCA_STRUCTURED_SESSION: '1',
ORCA_AGENT_SESSION_SPAWN_TOKEN: 'parent-spawn',
ORCA_CHAT_SCRATCH_DIR: '/parent/scratch',
ORCA_CHAT_VISUALS_DIR: '/parent/visuals',
ORCA_RUNTIME_SOURCE_ID: 'parent-source',
ORCA_RUNTIME_SOURCE_INCARNATION: 'parent-runtime'
}
const env: Record<string, string | undefined> = {
...parentIdentity,
PATH: '/usr/bin',
ORCA_USER_DATA_PATH: '/this/profile'
}

dropInheritedLaunchIdentityEnv(env)

expect(env).toEqual({ PATH: '/usr/bin', ORCA_USER_DATA_PATH: '/this/profile' })
})
})
Loading
Loading