diff --git a/src/main/runtime/rpc/methods/orchestration/federation/federated-worker-agent-launch.ts b/src/main/runtime/rpc/methods/orchestration/federation/federated-worker-agent-launch.ts index 3382e1ba28b..b717e9053bf 100644 --- a/src/main/runtime/rpc/methods/orchestration/federation/federated-worker-agent-launch.ts +++ b/src/main/runtime/rpc/methods/orchestration/federation/federated-worker-agent-launch.ts @@ -15,6 +15,7 @@ import { type FederationEffect } from './federation-effects' import type { FederationAttachStartInput } from './federation-start-schema' +import type { Worktree } from '../../../../../../shared/worktree/types' const TERMINAL_ONLY_MESSAGE = 'A federated worker starts a terminal agent only.' @@ -26,6 +27,8 @@ export async function launchFederatedWorkerAgent(args: { launchPreferences: AgentLaunchPreferences | undefined /** The workspace to start in; absent creates the new top-level worktree the params describe. */ worktree?: { id: string } + /** The paired client that asked, so its own sidebar counts the new worktree as its own. */ + creatorProvenance?: Worktree['creatorProvenance'] setupSource: string effects: FederationEffect[] /** The created worktree's setup receipt, as soon as it exists, so a later failure reports it. */ @@ -97,6 +100,7 @@ export async function launchFederatedWorkerAgent(args: { startupSuppressUpdatePrompt: true, ...(args.launchPreferences ? { startupLaunchPreferences: args.launchPreferences } : {}), activate: false, + ...(args.creatorProvenance ? { creatorProvenance: args.creatorProvenance } : {}), lineage: { noParent: true } }) created = result.worktree diff --git a/src/main/runtime/rpc/methods/orchestration/federation/federation-creator-provenance.test.ts b/src/main/runtime/rpc/methods/orchestration/federation/federation-creator-provenance.test.ts new file mode 100644 index 00000000000..2331a53c058 --- /dev/null +++ b/src/main/runtime/rpc/methods/orchestration/federation/federation-creator-provenance.test.ts @@ -0,0 +1,98 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { OrcaRuntimeService } from '../../../../orca-runtime' +import { OrchestrationDb } from '../../../../orchestration/db' +import { ORCHESTRATION_METHODS } from '../../orchestration' +import { isWorkspaceOwnedBySomeoneElse } from '../../../../../../shared/workspace-creator-visibility' +import { configureFederationWorkerRuntime } from './federation-runtime.test-support' + +const HANDLE = 'term_windows_worker' + +// A worktree a paired client starts here through orchestration is that client's, as with worktree.create. +describe('federated worker-start creator provenance', () => { + const databases: OrchestrationDb[] = [] + + afterEach(() => { + for (const db of databases.splice(0)) { + db.close() + } + vi.restoreAllMocks() + }) + + function createWorkerHost(): OrcaRuntimeService { + const db = new OrchestrationDb(':memory:') + databases.push(db) + const runtime = new OrcaRuntimeService() + runtime.setOrchestrationDb(db) + configureFederationWorkerRuntime(runtime) + vi.spyOn(runtime, 'waitForFreshWorkerComposer').mockResolvedValue({ + handle: HANDLE, + condition: 'tui-idle', + satisfied: true, + status: 'running', + exitCode: null + }) + return runtime + } + + async function attach( + runtime: OrcaRuntimeService, + caller: { pairedDeviceId?: string; clientKind?: 'mobile' | 'runtime' } + ): Promise { + const method = ORCHESTRATION_METHODS.find( + (candidate) => candidate.name === 'orchestration.federationAttachStart' + ) + if (!method) { + throw new Error('federationAttachStart method is not registered') + } + return await method.handler( + method.params!.parse({ + runId: 'run-home', + dispatchId: 'ctx_remote', + taskId: 'task_remote', + taskSpec: 'remote worker', + protocolVersion: 3, + worktree: 'new-top-level', + repo: 'windows-repo', + name: 'remote-worker', + agent: 'claude' + }), + { + runtime, + ...caller, + orchestrationMutation: { + callerFingerprint: 'home_peer', + requestId: 'request_remote', + method: 'orchestration.federationAttachStart', + payloadHash: 'remote_payload' + } + } + ) + } + + it('stamps the paired client that asked, so its sidebar keeps the worktree as its own', async () => { + const runtime = createWorkerHost() + + await expect( + attach(runtime, { pairedDeviceId: 'device-home', clientKind: 'runtime' }) + ).resolves.toMatchObject({ state: 'ready' }) + + const request = vi.mocked(runtime.createManagedWorktree).mock.calls[0]?.[0] + expect(request?.creatorProvenance).toEqual({ kind: 'paired-device', deviceId: 'device-home' }) + expect( + isWorkspaceOwnedBySomeoneElse( + { creatorProvenance: request?.creatorProvenance, runtimeOwnerEnvironmentId: 'env-server' }, + new Map([['env-server', 'device-home']]) + ) + ).toBe(false) + }) + + it('stamps the host when no paired device asked', async () => { + const runtime = createWorkerHost() + + await expect(attach(runtime, {})).resolves.toMatchObject({ state: 'ready' }) + + expect(vi.mocked(runtime.createManagedWorktree).mock.calls[0]?.[0]?.creatorProvenance).toEqual({ + kind: 'host' + }) + }) +}) diff --git a/src/main/runtime/rpc/methods/orchestration/federation/federation.ts b/src/main/runtime/rpc/methods/orchestration/federation/federation.ts index 5cc684c4e34..7227fbee489 100644 --- a/src/main/runtime/rpc/methods/orchestration/federation/federation.ts +++ b/src/main/runtime/rpc/methods/orchestration/federation/federation.ts @@ -25,6 +25,7 @@ import { } from '../../../../../../shared/orchestration-timing-budgets' import { assertWorkerStartTaskSpecWithinPromptBudget } from '../worker/worker-start-prompt-budget' import { prepareFederatedAttachmentAuthority } from './federation-attachment-authority' +import { resolveRpcWorkspaceCreatorProvenance } from '../../../workspace-creator-context' export const ORCHESTRATION_FEDERATION_ATTACH_METHODS = [ defineMethod({ @@ -32,7 +33,8 @@ export const ORCHESTRATION_FEDERATION_ATTACH_METHODS = [ requires: ['accounts'], permission: 'workspace', params: FederationAttachStartParams, - handler: async (params, { runtime, orchestrationMutation }) => { + handler: async (params, context) => { + const { runtime, orchestrationMutation } = context if (!orchestrationMutation) { throw new OrchestrationError( 'invalid_argument', @@ -102,7 +104,9 @@ export const ORCHESTRATION_FEDERATION_ATTACH_METHODS = [ params, agent, launchPreferences: launch.preferences, - ...(existing ? { worktree: existing } : {}), + ...(existing + ? { worktree: existing } + : { creatorProvenance: resolveRpcWorkspaceCreatorProvenance(context) }), setupSource, effects, onSetup: (created) => {