fix(orchestration): preserve active workers across updates (#11271)
* fix(orchestration): preserve active workers across updates * test(ssh): model absent legacy adoption * test(orchestration): align compatibility contracts * fix(windows): escape updater PowerShell booleans * fix(windows): restore stock uninstall process check * fix(orchestration): keep recovery off renderer startup barrier * fix(orchestration): harden legacy recovery migration * fix(orchestration): close recovery review gaps * fix(orchestration): complete legacy worker cutover recovery * fix(orchestration): preserve legacy workers across updates --------- Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
This commit is contained in:
@@ -60,6 +60,9 @@ export type SleepingAgentSessionRecord = {
|
||||
* so only the pane's own cold-restore path may consume them — activation
|
||||
* launching a tab too would duplicate a warm-reattached session (#5232). */
|
||||
origin?: 'worktree-sleep' | 'quit' | 'live'
|
||||
/** Prevents provider-session relaunch while main reconciles a durable
|
||||
* orchestration assignment against authoritative PTY inventory. */
|
||||
automaticResumeBlockedBy?: 'legacy-orchestration-worker'
|
||||
}
|
||||
|
||||
const RESUMABLE_TUI_AGENT_SET: ReadonlySet<string> = new Set(RESUMABLE_TUI_AGENTS)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { prepareOrchestrationCheckOutput } from './orchestration-check-output'
|
||||
|
||||
describe('prepareOrchestrationCheckOutput', () => {
|
||||
it('keeps mixed read-only mail safe and current Run replies executable', () => {
|
||||
const prepared = prepareOrchestrationCheckOutput(
|
||||
{
|
||||
count: 2,
|
||||
messages: [
|
||||
{
|
||||
id: 'msg_current',
|
||||
run_id: 'run_adopted',
|
||||
delivery_contract: 'current_delivery',
|
||||
from_handle: 'term_worker',
|
||||
to_handle: 'run:run_adopted',
|
||||
subject: 'Question'
|
||||
},
|
||||
{
|
||||
id: 'msg_legacy',
|
||||
run_id: 'run_legacy_local',
|
||||
delivery_contract: 'audit_only',
|
||||
from_handle: 'term_legacy',
|
||||
to_handle: 'term_coord',
|
||||
subject: 'Old reply'
|
||||
}
|
||||
],
|
||||
formatted: '[Reply: unsafe stale formatter output]'
|
||||
},
|
||||
'term_current_coord',
|
||||
true
|
||||
)
|
||||
|
||||
expect(prepared.formatted).toContain(
|
||||
'[Reply: orca orchestration reply --id msg_current --body "..."]'
|
||||
)
|
||||
expect(prepared.formatted).not.toContain('--from run:run_adopted')
|
||||
expect(prepared.formatted).toContain(
|
||||
'[Inspection only: reply and acknowledgment are unavailable.]'
|
||||
)
|
||||
expect(prepared.formatted).not.toContain('unsafe stale formatter output')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,199 @@
|
||||
import { ORCHESTRATION_LEGACY_RUN_ID } from './orchestration-rpc-contract'
|
||||
|
||||
export type OrchestrationMessageSummary = {
|
||||
id: string
|
||||
run_id?: string
|
||||
delivery_contract?: 'legacy_direct' | 'current_delivery' | 'audit_only'
|
||||
from_handle: string
|
||||
to_handle?: string
|
||||
subject?: string
|
||||
type?: string
|
||||
body?: string
|
||||
payload?: string | null
|
||||
priority?: string
|
||||
read?: number
|
||||
}
|
||||
|
||||
export type LegacyCompatibilityResult = {
|
||||
recovery?: boolean
|
||||
readOnly?: boolean
|
||||
ackMessageIds?: string[]
|
||||
answerAcknowledgement?: {
|
||||
questionId: string
|
||||
answerMessageId: string
|
||||
}
|
||||
currentDelivery?: {
|
||||
runId: string
|
||||
checkCommand: string
|
||||
ackCommand: string
|
||||
}
|
||||
resumeRequired?: boolean
|
||||
resumeCommand?: string
|
||||
}
|
||||
|
||||
export type OrchestrationCheckOutput = {
|
||||
messages: OrchestrationMessageSummary[]
|
||||
count: number
|
||||
formatted?: string
|
||||
deliveryId?: string | null
|
||||
timedOut?: boolean
|
||||
cancelled?: boolean
|
||||
connectionLost?: boolean
|
||||
legacyCompatibility?: LegacyCompatibilityResult
|
||||
}
|
||||
|
||||
export function formatMessageReadOnlyTag(
|
||||
message: OrchestrationMessageSummary,
|
||||
legacyCompatibilityActive = false
|
||||
): string {
|
||||
return isLegacyReadOnlyMessage(message, legacyCompatibilityActive) ? ' [legacy, read-only]' : ''
|
||||
}
|
||||
|
||||
export function isLegacyReadOnlyMessage(
|
||||
message: OrchestrationMessageSummary,
|
||||
legacyCompatibilityActive = false
|
||||
): boolean {
|
||||
return (
|
||||
message.run_id === ORCHESTRATION_LEGACY_RUN_ID ||
|
||||
(message.delivery_contract === 'legacy_direct' && !legacyCompatibilityActive) ||
|
||||
message.delivery_contract === 'audit_only'
|
||||
)
|
||||
}
|
||||
|
||||
export function formatOrchestrationCheckText(
|
||||
result: OrchestrationCheckOutput,
|
||||
checkedTerminal: string
|
||||
): string {
|
||||
const prepared = prepareOrchestrationCheckOutput(
|
||||
result,
|
||||
checkedTerminal,
|
||||
result.formatted !== undefined
|
||||
)
|
||||
const compatibilityActive = Boolean(
|
||||
prepared.legacyCompatibility && !prepared.legacyCompatibility.readOnly
|
||||
)
|
||||
const legacyHeader = prepared.legacyCompatibility
|
||||
? prepared.legacyCompatibility.readOnly
|
||||
? '[LEGACY READ-ONLY]\n'
|
||||
: prepared.legacyCompatibility.recovery
|
||||
? '[LEGACY RECOVERY REPLAY — MAY HAVE BEEN SEEN]\n'
|
||||
: '[LEGACY COMPATIBILITY]\n'
|
||||
: ''
|
||||
const deliveryNotice = formatCurrentDeliveryNotice(prepared.legacyCompatibility?.currentDelivery)
|
||||
if (prepared.formatted) {
|
||||
return `${legacyHeader}${prepared.formatted}${deliveryNotice}`
|
||||
}
|
||||
if (prepared.count === 0) {
|
||||
if (prepared.timedOut) {
|
||||
return `${legacyHeader}Wait timed out; no messages were consumed.${deliveryNotice}`
|
||||
}
|
||||
if (prepared.cancelled) {
|
||||
const cancelled = prepared.connectionLost
|
||||
? 'Wait cancelled because the connection closed; no messages were consumed.'
|
||||
: 'Wait cancelled; no messages were consumed.'
|
||||
return `${legacyHeader}${cancelled}${deliveryNotice}`
|
||||
}
|
||||
return `${legacyHeader}No messages.${deliveryNotice}`
|
||||
}
|
||||
const rendered = prepared.messages
|
||||
.map(
|
||||
(message) =>
|
||||
`${message.id}${formatMessageReadOnlyTag(
|
||||
message,
|
||||
compatibilityActive
|
||||
)} [${message.type ?? 'status'}] from=${message.from_handle} "${message.subject}"`
|
||||
)
|
||||
.join('\n')
|
||||
const output = prepared.deliveryId ? `Delivery ${prepared.deliveryId}\n${rendered}` : rendered
|
||||
return `${legacyHeader}${output}${deliveryNotice}`
|
||||
}
|
||||
|
||||
export function prepareOrchestrationCheckOutput<T extends OrchestrationCheckOutput>(
|
||||
result: T,
|
||||
checkedTerminal: string,
|
||||
formattedRequested: boolean
|
||||
): T {
|
||||
const compatibilityActive = Boolean(
|
||||
result.legacyCompatibility && !result.legacyCompatibility.readOnly
|
||||
)
|
||||
if (
|
||||
!formattedRequested ||
|
||||
!result.messages.some((message) => isLegacyReadOnlyMessage(message, compatibilityActive))
|
||||
) {
|
||||
return result
|
||||
}
|
||||
return {
|
||||
...result,
|
||||
formatted: formatLegacyAwareCheckMessages(result.messages, checkedTerminal, compatibilityActive)
|
||||
}
|
||||
}
|
||||
|
||||
function formatMessagePriorityTag(message: OrchestrationMessageSummary): string {
|
||||
return message.priority === 'urgent' ? ' [URGENT]' : message.priority === 'high' ? ' [HIGH]' : ''
|
||||
}
|
||||
|
||||
function escapeTerminalControlCharacters(value: string): string {
|
||||
return [...value]
|
||||
.map((character) => {
|
||||
const code = character.charCodeAt(0)
|
||||
if (character === '\n' || (code >= 0x20 && code < 0x7f) || code > 0x9f) {
|
||||
return character
|
||||
}
|
||||
return `\\x${code.toString(16).padStart(2, '0')}`
|
||||
})
|
||||
.join('')
|
||||
}
|
||||
|
||||
function formatQuotedMessageField(label: string, value?: string): string {
|
||||
return `[${label}]\n${escapeTerminalControlCharacters(value ?? '')
|
||||
.split('\n')
|
||||
.map((line) => ` ${line}`)
|
||||
.join('\n')}`
|
||||
}
|
||||
|
||||
function formatLegacyAwareCheckMessages(
|
||||
messages: OrchestrationMessageSummary[],
|
||||
checkedTerminal: string,
|
||||
legacyCompatibilityActive = false
|
||||
): string {
|
||||
return messages
|
||||
.map((message) => {
|
||||
const legacyReadOnly = isLegacyReadOnlyMessage(message, legacyCompatibilityActive)
|
||||
const lines = [
|
||||
`${message.id}${formatMessageReadOnlyTag(message, legacyCompatibilityActive)}${formatMessagePriorityTag(message)} [${message.type ?? 'status'}] from=${message.from_handle}`,
|
||||
formatQuotedMessageField('subject', message.subject)
|
||||
]
|
||||
if (legacyReadOnly) {
|
||||
lines.push('[Inspection only: reply and acknowledgment are unavailable.]')
|
||||
}
|
||||
if (message.body) {
|
||||
lines.push(formatQuotedMessageField('body', message.body))
|
||||
}
|
||||
if (message.payload) {
|
||||
lines.push(formatQuotedMessageField('payload', message.payload))
|
||||
}
|
||||
if (!legacyReadOnly) {
|
||||
const replyTarget = message.to_handle ?? checkedTerminal
|
||||
const replyFrom =
|
||||
replyTarget.startsWith('run:') || replyTarget.startsWith('dispatch:')
|
||||
? ''
|
||||
: ` --from ${replyTarget}`
|
||||
lines.push(`[Reply: orca orchestration reply --id ${message.id}${replyFrom} --body "..."]`)
|
||||
}
|
||||
return lines.join('\n')
|
||||
})
|
||||
.join('\n\n')
|
||||
}
|
||||
|
||||
function formatCurrentDeliveryNotice(
|
||||
delivery: LegacyCompatibilityResult['currentDelivery']
|
||||
): string {
|
||||
if (!delivery) {
|
||||
return ''
|
||||
}
|
||||
return (
|
||||
`\n[CURRENT RUN MAIL WAITING]\n` +
|
||||
`Read: ${delivery.checkCommand}\n` +
|
||||
`Then acknowledge the delivery ID it prints: ${delivery.ackCommand}`
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
ORCHESTRATION_COMPATIBILITY_ATTACHMENT_ENV,
|
||||
ORCHESTRATION_COMPATIBILITY_HOST_ID_ENV,
|
||||
ORCHESTRATION_COMPATIBILITY_HOST_INCARNATION_ENV,
|
||||
ORCHESTRATION_COMPATIBILITY_HOST_KIND_ENV,
|
||||
readOrchestrationCompatibilityEvidence,
|
||||
redactOrchestrationCompatibilitySecrets
|
||||
} from './orchestration-compatibility-evidence'
|
||||
|
||||
describe('orchestration compatibility evidence', () => {
|
||||
it('reads inherited pane evidence and a complete runtime-stamped WSL scope', () => {
|
||||
expect(
|
||||
readOrchestrationCompatibilityEvidence({
|
||||
ORCA_TERMINAL_HANDLE: 'term_wsl',
|
||||
ORCA_PANE_KEY: 'tab:leaf',
|
||||
ORCA_AGENT_LAUNCH_TOKEN: 'launch-secret',
|
||||
[ORCHESTRATION_COMPATIBILITY_HOST_KIND_ENV]: 'wsl',
|
||||
[ORCHESTRATION_COMPATIBILITY_HOST_ID_ENV]: 'local',
|
||||
[ORCHESTRATION_COMPATIBILITY_HOST_INCARNATION_ENV]: 'Ubuntu'
|
||||
})
|
||||
).toEqual({
|
||||
terminalHandle: 'term_wsl',
|
||||
paneKey: 'tab:leaf',
|
||||
launchToken: 'launch-secret',
|
||||
host: { kind: 'wsl', hostId: 'local', distro: 'Ubuntu' }
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects partial host stamps instead of accepting caller-chosen scope', () => {
|
||||
expect(
|
||||
readOrchestrationCompatibilityEvidence({
|
||||
ORCA_PANE_KEY: 'tab:leaf',
|
||||
[ORCHESTRATION_COMPATIBILITY_HOST_KIND_ENV]: 'ssh',
|
||||
[ORCHESTRATION_COMPATIBILITY_HOST_ID_ENV]: 'saved-target',
|
||||
[ORCHESTRATION_COMPATIBILITY_HOST_INCARNATION_ENV]: 'connection-only'
|
||||
})
|
||||
).toEqual({ paneKey: 'tab:leaf' })
|
||||
})
|
||||
|
||||
it('redacts evidence from nested CLI error and structured-log values', () => {
|
||||
const redacted = redactOrchestrationCompatibilitySecrets({
|
||||
compatibilityInvocationId: 'safe-operation-id',
|
||||
orchestrationCompatibilityEvidence: {
|
||||
launchToken: 'secret',
|
||||
host: {
|
||||
kind: 'ssh',
|
||||
[ORCHESTRATION_COMPATIBILITY_ATTACHMENT_ENV]: 'attachment-secret'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
expect(redacted).toEqual({
|
||||
compatibilityInvocationId: 'safe-operation-id',
|
||||
orchestrationCompatibilityEvidence: '[redacted]'
|
||||
})
|
||||
expect(JSON.stringify(redacted)).not.toContain('secret')
|
||||
})
|
||||
|
||||
it('redacts detached SSH attachment proof fields', () => {
|
||||
expect(
|
||||
redactOrchestrationCompatibilitySecrets({
|
||||
host: {
|
||||
targetId: 'saved-target',
|
||||
connectionIncarnation: 'connection-secret',
|
||||
attachmentId: 'attachment-secret'
|
||||
}
|
||||
})
|
||||
).toEqual({
|
||||
host: {
|
||||
targetId: 'saved-target',
|
||||
connectionIncarnation: '[redacted]',
|
||||
attachmentId: '[redacted]'
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('redacts cyclic arrays without recursing forever', () => {
|
||||
const cyclic: unknown[] = []
|
||||
cyclic.push(cyclic)
|
||||
|
||||
expect(redactOrchestrationCompatibilitySecrets(cyclic)).toEqual(['[circular]'])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,107 @@
|
||||
const MAX_EVIDENCE_FIELD_LENGTH = 4_096
|
||||
|
||||
export const ORCHESTRATION_COMPATIBILITY_HOST_KIND_ENV =
|
||||
'ORCA_ORCHESTRATION_COMPATIBILITY_HOST_KIND'
|
||||
export const ORCHESTRATION_COMPATIBILITY_HOST_ID_ENV = 'ORCA_ORCHESTRATION_COMPATIBILITY_HOST_ID'
|
||||
export const ORCHESTRATION_COMPATIBILITY_HOST_INCARNATION_ENV =
|
||||
'ORCA_ORCHESTRATION_COMPATIBILITY_HOST_INCARNATION'
|
||||
export const ORCHESTRATION_COMPATIBILITY_ATTACHMENT_ENV =
|
||||
'ORCA_ORCHESTRATION_COMPATIBILITY_ATTACHMENT'
|
||||
|
||||
export type OrchestrationCompatibilityHostStamp =
|
||||
| {
|
||||
kind: 'wsl'
|
||||
hostId: string
|
||||
distro: string
|
||||
}
|
||||
| {
|
||||
kind: 'ssh'
|
||||
targetId: string
|
||||
connectionIncarnation: string
|
||||
attachmentId: string
|
||||
}
|
||||
|
||||
export type OrchestrationCompatibilityEvidence = {
|
||||
terminalHandle?: string
|
||||
paneKey?: string
|
||||
launchToken?: string
|
||||
host?: OrchestrationCompatibilityHostStamp
|
||||
}
|
||||
|
||||
const SECRET_KEYS = new Set([
|
||||
'launchToken',
|
||||
'connectionIncarnation',
|
||||
'attachmentId',
|
||||
'compatibilityEvidence',
|
||||
'orchestrationCompatibilityEvidence',
|
||||
'ORCA_AGENT_LAUNCH_TOKEN',
|
||||
ORCHESTRATION_COMPATIBILITY_HOST_INCARNATION_ENV,
|
||||
ORCHESTRATION_COMPATIBILITY_ATTACHMENT_ENV
|
||||
])
|
||||
|
||||
function boundedValue(value: string | undefined): string | undefined {
|
||||
const trimmed = value?.trim()
|
||||
return trimmed && trimmed.length <= MAX_EVIDENCE_FIELD_LENGTH ? trimmed : undefined
|
||||
}
|
||||
|
||||
export function readOrchestrationCompatibilityEvidence(
|
||||
env: Readonly<Record<string, string | undefined>>
|
||||
): OrchestrationCompatibilityEvidence | undefined {
|
||||
const terminalHandle = boundedValue(env.ORCA_TERMINAL_HANDLE)
|
||||
const paneKey = boundedValue(env.ORCA_PANE_KEY)
|
||||
const launchToken = boundedValue(env.ORCA_AGENT_LAUNCH_TOKEN)
|
||||
const host = readHostStamp(env)
|
||||
if (!terminalHandle && !paneKey && !launchToken && !host) {
|
||||
return undefined
|
||||
}
|
||||
return {
|
||||
...(terminalHandle ? { terminalHandle } : {}),
|
||||
...(paneKey ? { paneKey } : {}),
|
||||
...(launchToken ? { launchToken } : {}),
|
||||
...(host ? { host } : {})
|
||||
}
|
||||
}
|
||||
|
||||
function readHostStamp(
|
||||
env: Readonly<Record<string, string | undefined>>
|
||||
): OrchestrationCompatibilityHostStamp | undefined {
|
||||
const kind = boundedValue(env[ORCHESTRATION_COMPATIBILITY_HOST_KIND_ENV])
|
||||
const hostId = boundedValue(env[ORCHESTRATION_COMPATIBILITY_HOST_ID_ENV])
|
||||
const incarnation = boundedValue(env[ORCHESTRATION_COMPATIBILITY_HOST_INCARNATION_ENV])
|
||||
const attachment = boundedValue(env[ORCHESTRATION_COMPATIBILITY_ATTACHMENT_ENV])
|
||||
if (kind === 'wsl' && hostId && incarnation) {
|
||||
return { kind, hostId, distro: incarnation }
|
||||
}
|
||||
if (kind === 'ssh' && hostId && incarnation && attachment) {
|
||||
return {
|
||||
kind,
|
||||
targetId: hostId,
|
||||
connectionIncarnation: incarnation,
|
||||
attachmentId: attachment
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export function redactOrchestrationCompatibilitySecrets(value: unknown): unknown {
|
||||
return redactValue(value, new WeakSet<object>())
|
||||
}
|
||||
|
||||
function redactValue(value: unknown, seen: WeakSet<object>): unknown {
|
||||
if (typeof value !== 'object' || value === null) {
|
||||
return value
|
||||
}
|
||||
if (seen.has(value)) {
|
||||
return '[circular]'
|
||||
}
|
||||
seen.add(value)
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((entry) => redactValue(entry, seen))
|
||||
}
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).map(([key, entry]) => [
|
||||
key,
|
||||
SECRET_KEYS.has(key) ? '[redacted]' : redactValue(entry, seen)
|
||||
])
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export const ORCHESTRATION_MESSAGE_WAIT_DEFAULT_TIMEOUT_MS = 2 * 60 * 1000
|
||||
@@ -107,7 +107,9 @@ export async function sendRemoteRuntimeRequest<TResult>(
|
||||
params,
|
||||
orchestrationCapability: envelope?.orchestrationCapability,
|
||||
orchestrationContractVersion: envelope?.orchestrationContractVersion,
|
||||
orchestrationRequestId: envelope?.orchestrationRequestId
|
||||
orchestrationRequestId: envelope?.orchestrationRequestId,
|
||||
compatibilityInvocationId: envelope?.compatibilityInvocationId,
|
||||
orchestrationCompatibilityEvidence: envelope?.orchestrationCompatibilityEvidence
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Keeping the envelope contract here avoids making those clients import each
|
||||
// other just to validate the shared RPC frame shape.
|
||||
import { z } from 'zod'
|
||||
import type { OrchestrationCompatibilityEvidence } from './orchestration-compatibility-evidence'
|
||||
|
||||
// Why: clients and runtimes update independently; strip additive envelope
|
||||
// fields while continuing to validate every known discriminator and field.
|
||||
@@ -78,6 +79,8 @@ export type RuntimeOrchestrationEnvelope = {
|
||||
orchestrationCapability?: string
|
||||
orchestrationContractVersion?: number
|
||||
orchestrationRequestId?: string
|
||||
compatibilityInvocationId?: string
|
||||
orchestrationCompatibilityEvidence?: OrchestrationCompatibilityEvidence
|
||||
}
|
||||
|
||||
export type RuntimeRpcKeepaliveFrame = z.infer<typeof Keepalive>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
export type TerminalRevealIdentity = {
|
||||
worktreeId: string
|
||||
tabId: string
|
||||
leafId: string
|
||||
ptyId: string
|
||||
}
|
||||
|
||||
export type TerminalTabCreateReply = {
|
||||
requestId: string
|
||||
tabId?: string
|
||||
title?: string
|
||||
identity?: TerminalRevealIdentity
|
||||
error?: string
|
||||
}
|
||||
@@ -97,7 +97,8 @@ const sleepingAgentSessionRecordSchema = z
|
||||
interrupted: z.boolean().optional(),
|
||||
connectionId: z.string().nullable().optional(),
|
||||
launchConfig: sleepingAgentLaunchConfigSchema.optional(),
|
||||
origin: z.enum(['worktree-sleep', 'quit', 'live']).optional()
|
||||
origin: z.enum(['worktree-sleep', 'quit', 'live']).optional(),
|
||||
automaticResumeBlockedBy: z.enum(['legacy-orchestration-worker']).optional()
|
||||
})
|
||||
.refine(
|
||||
(record) => getAgentResumeArgv(record.agent, record.providerSession) !== null,
|
||||
|
||||
Reference in New Issue
Block a user