diff --git a/src/main/runtime/orchestration/legacy-remote/federation.ts b/src/main/runtime/orchestration/legacy-remote/federation.ts index 522efc19515..4150b8d9618 100644 --- a/src/main/runtime/orchestration/legacy-remote/federation.ts +++ b/src/main/runtime/orchestration/legacy-remote/federation.ts @@ -19,6 +19,7 @@ import { import { FederationAttachStartParams } from '../../../../shared/rpc-contract/orchestration/orchestration-federation-start-params' import { failFederatedAttachmentWithReceipt } from './federation-start-receipt' import { prepareFederationWorkerLaunchOnHost } from '../../rpc/methods/orchestration/worker/start/launch/worker-opencode-model-preflight' +import { reusedWorkerLaunchReceipt } from '../../rpc/methods/orchestration/worker/start/launch/worker-launch-preferences' import { isWorkerStartTimeoutWithinTimerLimit, resolveWorkerStartReadinessTimeoutMs @@ -158,6 +159,11 @@ export const ORCHESTRATION_FEDERATION_ATTACH_METHODS = [ `Terminal ${terminalHandle} is not running a recognized agent.` ) } + launch.receipt = reusedWorkerLaunchReceipt(runtime, launch.receipt, { + address: terminalHandle, + terminalHandle, + orcaSessionId: null + }) effects.push({ kind: 'terminal', role: 'agent', diff --git a/src/main/runtime/orchestration/mailbox/senders/orchestration-sender-name.ts b/src/main/runtime/orchestration/mailbox/senders/orchestration-sender-name.ts index 52bcf492372..647e5a21acc 100644 --- a/src/main/runtime/orchestration/mailbox/senders/orchestration-sender-name.ts +++ b/src/main/runtime/orchestration/mailbox/senders/orchestration-sender-name.ts @@ -252,12 +252,16 @@ function observedTaskName( return task?.display_name || task?.task_title || null } +export function liveSenderAgent(observation: SenderObservation): AgentType | null { + return observation.chat?.agent || observation.terminal?.agent || null +} + /** Current live kind wins; exact durable dispatch knowledge survives loss of live observations. */ export function orchestrationSenderAgent( sources: Pick, observation: SenderObservation ): AgentType | null { - const live = observation.chat?.agent || observation.terminal?.agent + const live = liveSenderAgent(observation) if (live) { return live } diff --git a/src/main/runtime/orchestration/mailbox/senders/runtime-orchestration-sender-names.ts b/src/main/runtime/orchestration/mailbox/senders/runtime-orchestration-sender-names.ts index 3a343377bc9..468e0bde791 100644 --- a/src/main/runtime/orchestration/mailbox/senders/runtime-orchestration-sender-names.ts +++ b/src/main/runtime/orchestration/mailbox/senders/runtime-orchestration-sender-names.ts @@ -12,6 +12,8 @@ import { structuredAgentSessionIdFromTabId } from '../../../../../shared/agent-s import type { TerminalTab } from '../../../../../shared/workspace-layout/terminal-tab-types' import type { OrchestrationDb } from '../../db/orchestration-db' import { + liveSenderAgent, + observeOrchestrationSender, orchestrationSenderName, structuredChatNaming, captureMirroredTabs, @@ -93,6 +95,11 @@ export class RuntimeOrchestrationSenderNames { ) } + /** The agent a chat or terminal party is running now, as its sender card would show it. */ + liveAgentOf(party: AgentMessageSender['party']): AgentType | null { + return liveSenderAgent(observeOrchestrationSender(party, this.namingSources())) + } + /** Name and agent share one current chat or terminal observation. */ snapshotOf(party: AgentMessageSender['party'], reportedDispatchId?: string) { return observeSenderSnapshot(party, this.namingSources(), reportedDispatchId) diff --git a/src/main/runtime/rpc/methods/orchestration/worker/start/failed-worker-start-report.test.ts b/src/main/runtime/rpc/methods/orchestration/worker/start/failed-worker-start-report.test.ts index 3edacf8f406..efa444c6ec9 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/start/failed-worker-start-report.test.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/start/failed-worker-start-report.test.ts @@ -26,4 +26,29 @@ describe('a worker-start that fails', () => { ) expect(mail[0]?.body).not.toContain('still open') }) + + it('names the agent already running in a reused terminal when the start never binds', async () => { + h.setup() + vi.spyOn(h.runtime.orchestrationSenderNames, 'liveAgentOf').mockImplementation((party) => + party.terminalHandle === 'term_reused' ? 'claude' : null + ) + vi.spyOn(h.runtime, 'waitForTerminal').mockResolvedValue({ + handle: 'term_reused', + condition: 'tui-idle', + satisfied: false, + status: 'running', + exitCode: null + }) + const task = h.db.createTask({ spec: 'follow up on the PR', runId: h.activeRunId }) + const receipt = await h.call('orchestration.workerStart', { + task: task.id, + from: 'term_coord', + terminal: 'term_reused' + }) + expect(receipt).toMatchObject({ state: 'failed', failedStage: 'agent_readiness' }) + + const [lost] = h.db.getUnreadRunMailbox(h.activeRunId, 100, ['escalation']) + expect(lost?.from_handle).toMatch(/^dispatch:/) + expect(h.runtime.orchestrationSenderNames.sender(lost!.from_handle).agent).toBe('claude') + }) }) diff --git a/src/main/runtime/rpc/methods/orchestration/worker/start/launch/worker-launch-preferences.ts b/src/main/runtime/rpc/methods/orchestration/worker/start/launch/worker-launch-preferences.ts index 71b4a7a77c2..a64d7b36e91 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/start/launch/worker-launch-preferences.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/start/launch/worker-launch-preferences.ts @@ -2,6 +2,9 @@ import type { AgentLaunchPreferences } from '../../../../../../../../shared/agen import { checkAgentLaunchModelPreferences } from '../../../../../../../../shared/agent-launch/choices/agent-launch-model-preferences' import { ORCHESTRATION_WORKER_LAUNCH_PREFERENCES_RUNTIME_CAPABILITY } from '../../../../../../../../shared/orchestration/orchestration-runtime-capabilities' import type { TuiAgent } from '../../../../../../../../shared/agent-launch/catalog/tui-agent' +import { isTuiAgent } from '../../../../../../../../shared/agent-launch/catalog/tui-agent-config' +import type { OrcaRuntimeService } from '../../../../../../orca-runtime' +import type { AgentMessageSender } from '../../../../../../../../shared/agent-session/agent-session-message-source' import { OrchestrationError } from '../../../../../../orchestration/orchestration-error' export type OrchestrationWorkerLaunchSelection = { @@ -28,6 +31,25 @@ export function createWorkerLaunchReceipt(args: { return { requested: selection, effective: { ...selection } } } +/** A reuse start launches nothing; the agent already running there is its effective agent, so + * mail from a start that never binds still names it. */ +export function reusedWorkerLaunchReceipt( + runtime: Pick, + receipt: OrchestrationWorkerLaunchReceipt, + party: AgentMessageSender['party'] +): OrchestrationWorkerLaunchReceipt { + if (receipt.effective?.agent) { + return receipt + } + const running = runtime.orchestrationSenderNames.liveAgentOf(party) + return isTuiAgent(running) + ? { + ...receipt, + effective: { ...(receipt.effective ?? receipt.requested), agent: running } + } + : receipt +} + export function createPendingWorkerLaunchReceipt(args: { agent: TuiAgent | null model?: string diff --git a/src/main/runtime/rpc/methods/orchestration/worker/start/local-worker-start.ts b/src/main/runtime/rpc/methods/orchestration/worker/start/local-worker-start.ts index 60589e0be52..5556d9c04f3 100644 --- a/src/main/runtime/rpc/methods/orchestration/worker/start/local-worker-start.ts +++ b/src/main/runtime/rpc/methods/orchestration/worker/start/local-worker-start.ts @@ -34,6 +34,7 @@ import { recordCreatedWorkerTerminalCustody } from './placement/created-worker-t import { releaseRetriedWorkerTerminal } from '../release/retried-worker-terminal-release' import { requireWorkerAuthority, type WorkerEffect } from './placement/worker-placement-topology' import { prepareLocalWorkerStart } from './launch/worker-start-validation' +import { reusedWorkerLaunchReceipt } from './launch/worker-launch-preferences' import { deliverAndSettleWorkerStartReadiness } from './readiness/worker-start-readiness-settlement' import type { WorkerStartMutation } from './worker-start-mutation' import { prepareWorkerStartCard } from '../../../../../orchestration/workers/launch/worker-start-card' @@ -135,6 +136,7 @@ export async function startLocalWorker(args: { coordinator, resolvedWorktreeId: resolvedWorktree?.id }) + launch.receipt = reusedWorkerLaunchReceipt(runtime, launch.receipt, terminal) } const hostMode = resolveWorkerStartModeOnHost(runtime, args.mode, resolvedWorktree?.id, agent) let mode = chatWorkerMode(await hostMode, chat)