diff --git a/src/main/claude/claude-structured-launch-resolution.ts b/src/main/claude/claude-structured-launch-resolution.ts index 667ebfb8ddd..44018725a87 100644 --- a/src/main/claude/claude-structured-launch-resolution.ts +++ b/src/main/claude/claude-structured-launch-resolution.ts @@ -240,17 +240,21 @@ export function createClaudeStructuredLaunchResolver( command, // Only a dispatched structured worker gets the orchestration identity and the Orca CLI on // PATH; an ordinary chat session's env passes through untouched. - structuredWorkerChildIdentityEnv(record.sessionId, { - ...applyClaudeEnvPatch( - cloneDefinedEnv(process.env), - {}, - { - stripAuthEnv: auth.stripAuthEnv, - platform: process.platform - } - ), - ...(overlay ? cloneDefinedEnv(overlay) : {}) - }), + structuredWorkerChildIdentityEnv( + record.sessionId, + { + ...applyClaudeEnvPatch( + cloneDefinedEnv(process.env), + {}, + { + stripAuthEnv: auth.stripAuthEnv, + platform: process.platform + } + ), + ...(overlay ? cloneDefinedEnv(overlay) : {}) + }, + record.lease.runtimeFence + ), { platform: process.platform } ) return { diff --git a/src/main/runtime/rpc/methods/orchestration/worker/worker-start-schema.ts b/src/main/runtime/rpc/methods/orchestration/worker/worker-start-schema.ts index 2f9d9456609..8ef88f9c935 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/worker-start-schema.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/worker-start-schema.ts @@ -1,5 +1,5 @@ import { z } from 'zod' -import { OptionalFiniteNumber, OptionalString, requiredString } from '../../../schemas' +import { OptionalFiniteNumber, OptionalString } from '../../../schemas' export const OptionalWorkerLaunchPreference = z .string() @@ -17,7 +17,9 @@ export const WorkerStartParams = z parent: OptionalString, on: OptionalString, run: OptionalString, - from: requiredString('Missing --from'), + from: z.string().min(1).optional(), + agentSessionId: OptionalString, + runtimeFence: OptionalFiniteNumber, worktree: OptionalString, name: OptionalString, repo: OptionalString, diff --git a/src/main/runtime/rpc/methods/orchestration/worker/workers.ts b/src/main/runtime/rpc/methods/orchestration/worker/workers.ts index 8b14ec044cf..8c341230182 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/workers.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/workers.ts @@ -6,7 +6,7 @@ import { decideWorkerStartMode, readWorkerStartModeSettings } from '../../orchestration-worker-start-mode' -import { resolveOrchestrationCaller } from '../runs/run-scope' +import { resolveOrchestrationCaller, resolveRunScope } from '../runs/run-scope' import { WorkerStartParams } from './worker-start-schema' import { isWorkerStartTimeoutWithinTimerLimit, @@ -30,11 +30,23 @@ export const ORCHESTRATION_WORKER_START_METHODS: RpcMethod[] = [ } const readinessTimeoutMs = resolveWorkerStartReadinessTimeoutMs(params.timeoutMs) const db = runtime.getOrchestrationDb() - const coordinatorPane = resolveOrchestrationCaller(runtime, { - callerTerminalHandle: params.from, - callerEvidence: orchestrationCompatibilityEvidence - }) - const run = coordinatorPane ? db.getCurrentRunForPane(coordinatorPane) : undefined + const native = Boolean(params.agentSessionId) + const coordinatorPane = native + ? null + : resolveOrchestrationCaller(runtime, { + callerTerminalHandle: params.from!, + callerEvidence: orchestrationCompatibilityEvidence + }) + const run = native + ? resolveRunScope(runtime, { + runId: params.run, + callerAgentSessionId: params.agentSessionId, + callerRuntimeFence: params.runtimeFence, + requireCurrentConsumer: true + }) + : coordinatorPane + ? db.getCurrentRunForPane(coordinatorPane) + : undefined if (!run || (params.run && params.run !== run.id)) { throw new OrchestrationError( 'consumer_fenced', diff --git a/src/main/runtime/structured-worker-child-identity-env.ts b/src/main/runtime/structured-worker-child-identity-env.ts index 6cf9f46e910..45745d81ae2 100644 --- a/src/main/runtime/structured-worker-child-identity-env.ts +++ b/src/main/runtime/structured-worker-child-identity-env.ts @@ -42,7 +42,8 @@ import { structuredWorkerIdentities } from './structured-worker-identity' export function structuredWorkerChildIdentityEnv( sessionId: string, - childEnv: Record + childEnv: Record, + runtimeFence?: number ): Record { const identity = structuredWorkerIdentities.getBySessionId(sessionId) if (!identity) { @@ -51,6 +52,10 @@ export function structuredWorkerChildIdentityEnv( const env: Record = { ...childEnv, ORCA_TERMINAL_HANDLE: identity.handle, + ORCA_AGENT_SESSION_ID: sessionId, + ...(runtimeFence !== undefined + ? { ORCA_AGENT_SESSION_RUNTIME_FENCE: String(runtimeFence) } + : {}), ORCA_CLI_COMMAND: 'orca' } applyOrcaCliPath(env)