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:
File diff suppressed because it is too large
Load Diff
+1393
-101
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,350 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { AgentHookServer } from '../agent-hooks/server'
|
||||
import { OrcaRuntimeService } from './orca-runtime'
|
||||
|
||||
const PANE_KEY = '11111111-1111-4111-8111-111111111111:22222222-2222-4222-8222-222222222222'
|
||||
const TOKEN = 'launch-secret'
|
||||
const TOKEN_HASH = createHash('sha256').update(TOKEN).digest('hex')
|
||||
|
||||
type TerminalAuthorityResolver = {
|
||||
getOrchestrationDispatchAuthority: (terminalHandle: string) => unknown
|
||||
restoredOrchestrationAuthorityByPtyId: Map<string, Record<string, unknown>>
|
||||
}
|
||||
|
||||
function createRuntime(
|
||||
hostScope:
|
||||
| { kind: 'local'; hostId: 'local' }
|
||||
| { kind: 'wsl'; hostId: 'local'; distro: string }
|
||||
| { kind: 'ssh'; targetId: string },
|
||||
launchTokenHash: string | null = TOKEN_HASH
|
||||
) {
|
||||
const runtime = new OrcaRuntimeService(null, undefined, {
|
||||
attestAgentHookCompatibilityAuthority: ({ paneKey, launchTokenHash, connectionId }) =>
|
||||
paneKey === PANE_KEY &&
|
||||
launchTokenHash === TOKEN_HASH &&
|
||||
connectionId === (hostScope.kind === 'ssh' ? hostScope.targetId : null)
|
||||
? { paneKey, source: 'hydrated_commitment' }
|
||||
: null
|
||||
})
|
||||
const resolveTerminal = vi.fn(() => ({
|
||||
runtimeId: 'runtime-1',
|
||||
terminalHandle: 'term-1',
|
||||
ptyId: 'pty-1',
|
||||
worktreeId: 'repo-1::/worktree',
|
||||
processIncarnation: 'incarnation-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchTokenHash,
|
||||
hostScope
|
||||
}))
|
||||
;(runtime as unknown as TerminalAuthorityResolver).getOrchestrationDispatchAuthority =
|
||||
resolveTerminal
|
||||
if (launchTokenHash === null) {
|
||||
;(runtime as unknown as TerminalAuthorityResolver).restoredOrchestrationAuthorityByPtyId.set(
|
||||
'pty-1',
|
||||
{
|
||||
ptyId: 'pty-1',
|
||||
worktreeId: 'repo-1::/worktree',
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
processIncarnation: 'incarnation-1',
|
||||
hostScope
|
||||
}
|
||||
)
|
||||
}
|
||||
return runtime
|
||||
}
|
||||
|
||||
describe('orchestration compatibility runtime authority', () => {
|
||||
it('returns only attested local identity and its token hash', () => {
|
||||
const runtime = createRuntime({ kind: 'local', hostId: 'local' })
|
||||
|
||||
const authority = runtime.verifyOrchestrationCompatibilityCaller({
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN
|
||||
})
|
||||
|
||||
expect(authority).toEqual({
|
||||
hostScope: { kind: 'local', hostId: 'local' },
|
||||
paneKey: PANE_KEY,
|
||||
terminalHandle: 'term-1',
|
||||
processIncarnation: 'incarnation-1',
|
||||
launchTokenHash: TOKEN_HASH
|
||||
})
|
||||
expect(JSON.stringify(authority)).not.toContain(TOKEN)
|
||||
expect(
|
||||
runtime.verifyOrchestrationCompatibilityCaller({
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: 'wrong'
|
||||
})
|
||||
).toBeNull()
|
||||
expect(
|
||||
runtime.verifyOrchestrationCompatibilityCaller({
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN_HASH
|
||||
})
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('keeps local and WSL authority stable across app runtime generations', () => {
|
||||
const firstLocal = createRuntime({ kind: 'local', hostId: 'local' })
|
||||
const secondLocal = createRuntime({ kind: 'local', hostId: 'local' })
|
||||
const wsl = createRuntime({ kind: 'wsl', hostId: 'local', distro: 'Ubuntu' })
|
||||
const localEvidence = {
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN
|
||||
}
|
||||
const wslEvidence = {
|
||||
...localEvidence,
|
||||
host: { kind: 'wsl', hostId: 'local', distro: 'Ubuntu' }
|
||||
} as const
|
||||
|
||||
expect(firstLocal.verifyOrchestrationCompatibilityCaller(localEvidence)?.hostScope).toEqual(
|
||||
secondLocal.verifyOrchestrationCompatibilityCaller(localEvidence)?.hostScope
|
||||
)
|
||||
expect(wsl.verifyOrchestrationCompatibilityCaller(wslEvidence)?.hostScope).toEqual({
|
||||
kind: 'wsl',
|
||||
hostId: 'local',
|
||||
distro: 'Ubuntu'
|
||||
})
|
||||
expect(wsl.verifyOrchestrationCompatibilityCaller(localEvidence)).toBeNull()
|
||||
expect(
|
||||
wsl.verifyOrchestrationCompatibilityCaller({
|
||||
...wslEvidence,
|
||||
host: { kind: 'wsl', hostId: 'runtime-before-restart', distro: 'Ubuntu' }
|
||||
})
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('requires a live exact terminal even when the hook proof is hydrated', () => {
|
||||
const runtime = createRuntime({ kind: 'local', hostId: 'local' })
|
||||
;(runtime as unknown as TerminalAuthorityResolver).getOrchestrationDispatchAuthority = () =>
|
||||
null
|
||||
|
||||
expect(
|
||||
runtime.verifyOrchestrationCompatibilityCaller({
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN
|
||||
})
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('uses the hydrated hook commitment for a restored exact PTY', () => {
|
||||
const restored = createRuntime({ kind: 'local', hostId: 'local' }, null)
|
||||
const uncommitted = new OrcaRuntimeService()
|
||||
;(uncommitted as unknown as TerminalAuthorityResolver).getOrchestrationDispatchAuthority =
|
||||
() => ({
|
||||
runtimeId: 'runtime-1',
|
||||
terminalHandle: 'term-1',
|
||||
ptyId: 'pty-1',
|
||||
worktreeId: 'repo-1::/worktree',
|
||||
processIncarnation: 'incarnation-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchTokenHash: null,
|
||||
hostScope: { kind: 'local', hostId: 'local' }
|
||||
})
|
||||
const evidence = {
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN
|
||||
}
|
||||
|
||||
expect(restored.verifyOrchestrationCompatibilityCaller(evidence)).toMatchObject({
|
||||
processIncarnation: 'incarnation-1',
|
||||
launchTokenHash: TOKEN_HASH
|
||||
})
|
||||
expect(uncommitted.verifyOrchestrationCompatibilityCaller(evidence)).toBeNull()
|
||||
expect(
|
||||
restored.verifyOrchestrationCompatibilityCaller({
|
||||
...evidence,
|
||||
launchToken: 'wrong'
|
||||
})
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it.each([
|
||||
['PTY', { ptyId: 'pty-other' }],
|
||||
['worktree', { worktreeId: 'repo-1::/other' }],
|
||||
['terminal handle', { terminalHandle: 'term-other' }],
|
||||
[
|
||||
'pane',
|
||||
{ paneKey: '33333333-3333-4333-8333-333333333333:44444444-4444-4444-8444-444444444444' }
|
||||
],
|
||||
['process incarnation', { processIncarnation: 'incarnation-other' }]
|
||||
])('rejects a restored receipt with mismatched %s identity', (_field, mismatch) => {
|
||||
const runtime = createRuntime({ kind: 'local', hostId: 'local' }, null)
|
||||
const internals = runtime as unknown as TerminalAuthorityResolver
|
||||
const receipt = internals.restoredOrchestrationAuthorityByPtyId.get('pty-1')!
|
||||
internals.restoredOrchestrationAuthorityByPtyId.set('pty-1', { ...receipt, ...mismatch })
|
||||
|
||||
expect(
|
||||
runtime.verifyOrchestrationCompatibilityCaller({
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN
|
||||
})
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('rejects restored receipts from a different WSL distro or SSH target', () => {
|
||||
const wsl = createRuntime({ kind: 'wsl', hostId: 'local', distro: 'Ubuntu' }, null)
|
||||
const wslInternals = wsl as unknown as TerminalAuthorityResolver
|
||||
const wslReceipt = wslInternals.restoredOrchestrationAuthorityByPtyId.get('pty-1')!
|
||||
wslInternals.restoredOrchestrationAuthorityByPtyId.set('pty-1', {
|
||||
...wslReceipt,
|
||||
hostScope: { kind: 'wsl', hostId: 'local', distro: 'Debian' }
|
||||
})
|
||||
expect(
|
||||
wsl.verifyOrchestrationCompatibilityCaller({
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN,
|
||||
host: { kind: 'wsl', hostId: 'local', distro: 'Ubuntu' }
|
||||
})
|
||||
).toBeNull()
|
||||
|
||||
const ssh = createRuntime({ kind: 'ssh', targetId: 'saved-target' }, null)
|
||||
const sshInternals = ssh as unknown as TerminalAuthorityResolver
|
||||
const sshReceipt = sshInternals.restoredOrchestrationAuthorityByPtyId.get('pty-1')!
|
||||
sshInternals.restoredOrchestrationAuthorityByPtyId.set('pty-1', {
|
||||
...sshReceipt,
|
||||
hostScope: { kind: 'ssh', targetId: 'other-target' }
|
||||
})
|
||||
const host = ssh.registerOrchestrationCompatibilitySshAttachment('saved-target', 'connection-1')
|
||||
expect(
|
||||
ssh.verifyOrchestrationCompatibilityCaller({
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN,
|
||||
host
|
||||
})
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('does not fall back to a restored receipt when a fresh launch token mismatches', () => {
|
||||
const runtime = createRuntime({ kind: 'local', hostId: 'local' })
|
||||
;(runtime as unknown as TerminalAuthorityResolver).restoredOrchestrationAuthorityByPtyId.set(
|
||||
'pty-1',
|
||||
{
|
||||
ptyId: 'pty-1',
|
||||
worktreeId: 'repo-1::/worktree',
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
processIncarnation: 'incarnation-1',
|
||||
hostScope: { kind: 'local', hostId: 'local' }
|
||||
}
|
||||
)
|
||||
|
||||
expect(
|
||||
runtime.verifyOrchestrationCompatibilityCaller({
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: 'wrong'
|
||||
})
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('attests current coordinators immediately without upgrading restored legacy callers', () => {
|
||||
const server = new AgentHookServer()
|
||||
server.ingestRemote(
|
||||
{
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN,
|
||||
payload: { state: 'working', prompt: 'coordinate', agentType: 'codex' }
|
||||
},
|
||||
'saved-target'
|
||||
)
|
||||
const createIntegratedRuntime = (launchTokenHash: string | null): OrcaRuntimeService => {
|
||||
const runtime = new OrcaRuntimeService(null, undefined, {
|
||||
attestAgentHookCompatibilityAuthority: (candidate) =>
|
||||
server.attestCompatibilityAuthority(candidate)
|
||||
})
|
||||
;(runtime as unknown as TerminalAuthorityResolver).getOrchestrationDispatchAuthority = vi.fn(
|
||||
() => ({
|
||||
runtimeId: 'runtime-1',
|
||||
terminalHandle: 'term-1',
|
||||
ptyId: 'pty-1',
|
||||
worktreeId: 'repo-1::/worktree',
|
||||
processIncarnation: 'incarnation-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchTokenHash,
|
||||
hostScope: { kind: 'ssh', targetId: 'saved-target' }
|
||||
})
|
||||
)
|
||||
if (launchTokenHash === null) {
|
||||
;(
|
||||
runtime as unknown as TerminalAuthorityResolver
|
||||
).restoredOrchestrationAuthorityByPtyId.set('pty-1', {
|
||||
ptyId: 'pty-1',
|
||||
worktreeId: 'repo-1::/worktree',
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
processIncarnation: 'incarnation-1',
|
||||
hostScope: { kind: 'ssh', targetId: 'saved-target' }
|
||||
})
|
||||
}
|
||||
return runtime
|
||||
}
|
||||
const evidenceFor = (runtime: OrcaRuntimeService, launchToken = TOKEN) => ({
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken,
|
||||
host: runtime.registerOrchestrationCompatibilitySshAttachment('saved-target', 'connection-1')
|
||||
})
|
||||
|
||||
const current = createIntegratedRuntime(TOKEN_HASH)
|
||||
expect(current.verifyOrchestrationCompatibilityCaller(evidenceFor(current))).not.toBeNull()
|
||||
|
||||
const restored = createIntegratedRuntime(null)
|
||||
expect(restored.verifyOrchestrationCompatibilityCaller(evidenceFor(restored))).toBeNull()
|
||||
|
||||
server.ingestRemote(
|
||||
{
|
||||
paneKey: '33333333-3333-4333-8333-333333333333:44444444-4444-4444-8444-444444444444',
|
||||
launchToken: TOKEN,
|
||||
payload: { state: 'working', prompt: 'duplicate', agentType: 'codex' }
|
||||
},
|
||||
'saved-target'
|
||||
)
|
||||
expect(current.verifyOrchestrationCompatibilityCaller(evidenceFor(current))).toBeNull()
|
||||
|
||||
const mismatchedToken = 'different-launch-secret'
|
||||
const mismatched = createIntegratedRuntime(
|
||||
createHash('sha256').update(mismatchedToken).digest('hex')
|
||||
)
|
||||
expect(
|
||||
mismatched.verifyOrchestrationCompatibilityCaller(evidenceFor(mismatched, mismatchedToken))
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('accepts only a live runtime-issued SSH attachment', () => {
|
||||
const runtime = createRuntime({ kind: 'ssh', targetId: 'saved-target' })
|
||||
const host = runtime.registerOrchestrationCompatibilitySshAttachment(
|
||||
'saved-target',
|
||||
'connection-1'
|
||||
)
|
||||
const evidence = {
|
||||
terminalHandle: 'term-1',
|
||||
paneKey: PANE_KEY,
|
||||
launchToken: TOKEN,
|
||||
host
|
||||
} as const
|
||||
|
||||
expect(runtime.verifyOrchestrationCompatibilityCaller(evidence)).not.toBeNull()
|
||||
expect(
|
||||
runtime.verifyOrchestrationCompatibilityCaller({
|
||||
...evidence,
|
||||
host: { ...host, attachmentId: 'caller-chosen' }
|
||||
})
|
||||
).toBeNull()
|
||||
|
||||
runtime.releaseOrchestrationCompatibilitySshAttachment(host.attachmentId)
|
||||
|
||||
expect(runtime.verifyOrchestrationCompatibilityCaller(evidence)).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -942,7 +942,7 @@ describe('OrchestrationDb', () => {
|
||||
|
||||
// v1 data preserved
|
||||
expect(d.getMessageById('msg_v1')?.subject).toBe('pre-migration')
|
||||
expect(d.getMessageById('msg_v1')?.run_id).toBe(LEGACY_RUN_ID)
|
||||
expect(d.getMessageById('msg_v1')?.run_id).toBe(d.getLegacyAdoption()?.adopted_run_id)
|
||||
expect(d.getRun(LEGACY_RUN_ID)).toMatchObject({ legacy: 1 })
|
||||
})
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -77,6 +77,14 @@ describe('formatMessageBanner', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('lets the CLI resolve the live sender for Run and Dispatch addresses', () => {
|
||||
for (const to_handle of ['run:run_test', 'dispatch:dispatch_test']) {
|
||||
const banner = formatMessageBanner(makeMessage({ to_handle }))
|
||||
expect(banner).toContain('[Reply: orca orchestration reply --id msg_test1 --body "..."]')
|
||||
expect(banner).not.toContain(`--from ${to_handle}`)
|
||||
}
|
||||
})
|
||||
|
||||
it('marks legacy messages read-only without reply or acknowledgment affordances', () => {
|
||||
const banner = formatMessageBanner(
|
||||
makeMessage({ id: 'msg_legacy', run_id: 'run_legacy_local' })
|
||||
@@ -88,6 +96,63 @@ describe('formatMessageBanner', () => {
|
||||
expect(banner).not.toContain('orchestration reply')
|
||||
})
|
||||
|
||||
it('renders only attested actions for legacy compatibility authority', () => {
|
||||
const banner = formatMessageBanner(makeMessage({ id: 'msg_legacy' }), {
|
||||
authority: 'legacy_compatibility',
|
||||
supportedActionHints: [
|
||||
'orca orchestration reply --id msg_legacy --from term_coord --body "..."'
|
||||
]
|
||||
})
|
||||
|
||||
expect(banner).toContain('[LEGACY COMPATIBILITY]')
|
||||
expect(banner).toContain(
|
||||
'[Supported action: orca orchestration reply --id msg_legacy --from term_coord --body "..."]'
|
||||
)
|
||||
expect(banner).not.toContain('[Reply:')
|
||||
expect(banner).not.toContain('acknowledgment')
|
||||
})
|
||||
|
||||
it('warns that a bounded legacy recovery replay may already have been seen', () => {
|
||||
const banner = formatMessageBanner(makeMessage(), {
|
||||
authority: 'legacy_recovery_replay',
|
||||
supportedActionHints: ['orca orchestration check --ack delivery_legacy']
|
||||
})
|
||||
|
||||
expect(banner).toContain('[LEGACY RECOVERY REPLAY — MAY HAVE BEEN SEEN]')
|
||||
expect(banner).toContain('bounded recovery replay may already have been seen')
|
||||
expect(banner).toContain('[Supported action: orca orchestration check --ack delivery_legacy]')
|
||||
expect(banner).not.toContain('[Reply:')
|
||||
})
|
||||
|
||||
it('does not infer live compatibility from legacy database provenance', () => {
|
||||
const banner = formatMessageBanner(makeMessage({ run_id: 'run_legacy_local' }), {
|
||||
supportedActionHints: ['orca orchestration check --ack delivery_legacy']
|
||||
})
|
||||
|
||||
expect(banner).toContain('[LEGACY READ-ONLY]')
|
||||
expect(banner).not.toContain('Supported action:')
|
||||
expect(banner).not.toContain('check --ack')
|
||||
})
|
||||
|
||||
it('keeps adopted legacy and audit messages read-only without runtime authority', () => {
|
||||
for (const deliveryContract of ['legacy_direct', 'audit_only'] as const) {
|
||||
const banner = formatMessageBanner(
|
||||
makeMessage({ run_id: 'run_adopted', delivery_contract: deliveryContract })
|
||||
)
|
||||
|
||||
expect(banner).toContain('[LEGACY READ-ONLY]')
|
||||
expect(banner).not.toContain('[Reply:')
|
||||
}
|
||||
})
|
||||
|
||||
it('keeps current formatting unchanged when authority is explicit', () => {
|
||||
const message = makeMessage({ id: 'msg_current' })
|
||||
|
||||
expect(formatMessageBanner(message, { authority: 'current' })).toBe(
|
||||
formatMessageBanner(message)
|
||||
)
|
||||
})
|
||||
|
||||
it('ends with a separator line', () => {
|
||||
const banner = formatMessageBanner(makeMessage())
|
||||
const lines = banner.split('\n')
|
||||
|
||||
@@ -4,19 +4,77 @@ import { ORCHESTRATION_LEGACY_RUN_ID } from '../../../shared/orchestration-rpc-c
|
||||
const BANNER_WIDTH = 60
|
||||
const SEPARATOR = '─'.repeat(BANNER_WIDTH)
|
||||
|
||||
export function formatMessageBanner(msg: MessageRow): string {
|
||||
export type MessageFormattingAuthority =
|
||||
| 'current'
|
||||
| 'legacy_compatibility'
|
||||
| 'legacy_recovery_replay'
|
||||
| 'legacy_read_only'
|
||||
|
||||
export type MessageFormattingOptions = {
|
||||
authority?: MessageFormattingAuthority
|
||||
supportedActionHints?: readonly string[]
|
||||
}
|
||||
|
||||
function resolveAuthority(
|
||||
msg: MessageRow,
|
||||
authority: MessageFormattingAuthority | undefined
|
||||
): MessageFormattingAuthority {
|
||||
if (authority) {
|
||||
return authority
|
||||
}
|
||||
return msg.run_id === ORCHESTRATION_LEGACY_RUN_ID ||
|
||||
msg.delivery_contract === 'legacy_direct' ||
|
||||
msg.delivery_contract === 'audit_only'
|
||||
? 'legacy_read_only'
|
||||
: 'current'
|
||||
}
|
||||
|
||||
function appendLegacyGuidance(
|
||||
lines: string[],
|
||||
authority: MessageFormattingAuthority,
|
||||
supportedActionHints: readonly string[]
|
||||
): void {
|
||||
if (authority === 'legacy_read_only') {
|
||||
lines.push('[Inspection only: reply and acknowledgment are unavailable.]')
|
||||
return
|
||||
}
|
||||
if (authority === 'legacy_compatibility') {
|
||||
lines.push('[Use only the supported legacy action shown below.]')
|
||||
} else if (authority === 'legacy_recovery_replay') {
|
||||
lines.push(
|
||||
'[This bounded recovery replay may already have been seen. Use only the action shown below.]'
|
||||
)
|
||||
}
|
||||
for (const hint of supportedActionHints) {
|
||||
lines.push(`[Supported action: ${hint}]`)
|
||||
}
|
||||
}
|
||||
|
||||
export function formatMessageBanner(msg: MessageRow, options: MessageFormattingOptions): string
|
||||
export function formatMessageBanner(msg: MessageRow): string
|
||||
export function formatMessageBanner(
|
||||
msg: MessageRow,
|
||||
options: MessageFormattingOptions = {}
|
||||
): string {
|
||||
const priorityTag =
|
||||
msg.priority === 'urgent' ? ' [URGENT]' : msg.priority === 'high' ? ' [HIGH]' : ''
|
||||
const legacyReadOnly = msg.run_id === ORCHESTRATION_LEGACY_RUN_ID
|
||||
const authorityTag = legacyReadOnly ? ' [LEGACY READ-ONLY]' : ''
|
||||
const authority = resolveAuthority(msg, options.authority)
|
||||
const authorityTag =
|
||||
authority === 'legacy_compatibility'
|
||||
? ' [LEGACY COMPATIBILITY]'
|
||||
: authority === 'legacy_recovery_replay'
|
||||
? ' [LEGACY RECOVERY REPLAY — MAY HAVE BEEN SEEN]'
|
||||
: authority === 'legacy_read_only'
|
||||
? ' [LEGACY READ-ONLY]'
|
||||
: ''
|
||||
const senderName = msg.from_handle.toUpperCase()
|
||||
|
||||
const header = `──── From: ${senderName} (${msg.from_handle})${priorityTag}${authorityTag} (${msg.type}) ────`
|
||||
|
||||
const lines: string[] = [header]
|
||||
lines.push(`Subject: ${msg.subject}`)
|
||||
if (legacyReadOnly) {
|
||||
lines.push('[Inspection only: reply and acknowledgment are unavailable.]')
|
||||
if (authority !== 'current') {
|
||||
appendLegacyGuidance(lines, authority, options.supportedActionHints ?? [])
|
||||
}
|
||||
|
||||
if (msg.body) {
|
||||
@@ -27,11 +85,12 @@ export function formatMessageBanner(msg: MessageRow): string {
|
||||
lines.push(`[Payload: ${msg.payload}]`)
|
||||
}
|
||||
|
||||
if (!legacyReadOnly) {
|
||||
// Why: older shells can lack Orca's terminal identity environment.
|
||||
lines.push(
|
||||
`[Reply: orca orchestration reply --id ${msg.id} --from ${msg.to_handle} --body "..."]`
|
||||
)
|
||||
if (authority === 'current') {
|
||||
const explicitFrom =
|
||||
msg.to_handle.startsWith('run:') || msg.to_handle.startsWith('dispatch:')
|
||||
? ''
|
||||
: ` --from ${msg.to_handle}`
|
||||
lines.push(`[Reply: orca orchestration reply --id ${msg.id}${explicitFrom} --body "..."]`)
|
||||
}
|
||||
lines.push(SEPARATOR)
|
||||
|
||||
|
||||
@@ -0,0 +1,381 @@
|
||||
import { rmSync } from 'node:fs'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import { LEGACY_RUN_ID, OrchestrationDb } from './db'
|
||||
import {
|
||||
createLegacyStorageCutoverFixture,
|
||||
type LegacyStorageCutoverFixture
|
||||
} from './orchestration-legacy-storage-test-fixture'
|
||||
|
||||
describe('OrchestrationDb legacy coordinator authority', () => {
|
||||
let db: OrchestrationDb | undefined
|
||||
let tempDir: string | undefined
|
||||
|
||||
afterEach(() => {
|
||||
db?.close()
|
||||
db = undefined
|
||||
if (tempDir) {
|
||||
rmSync(tempDir, { recursive: true, force: true })
|
||||
tempDir = undefined
|
||||
}
|
||||
})
|
||||
|
||||
function createCutoverFixture(): LegacyStorageCutoverFixture {
|
||||
const created = createLegacyStorageCutoverFixture()
|
||||
tempDir = created.tempDir
|
||||
return created.fixture
|
||||
}
|
||||
|
||||
function openAdoptedFixture(): {
|
||||
fixture: LegacyStorageCutoverFixture
|
||||
adoptedRunId: string
|
||||
workerPrincipalId: string
|
||||
} {
|
||||
const fixture = createCutoverFixture()
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
const worker = db.commitLegacyCompatibilityPrincipal({
|
||||
runId: adoptedRunId,
|
||||
dispatchId: fixture.legacyDispatchId,
|
||||
role: 'worker',
|
||||
hostScope: 'local:runtime_1',
|
||||
terminalHandle: 'term_legacy_worker',
|
||||
paneKey: 'tab_legacy:33333333-3333-4333-8333-333333333333',
|
||||
launchTokenHash: 'legacy_launch_hash',
|
||||
processIncarnation: 'process_1'
|
||||
})
|
||||
return {
|
||||
fixture,
|
||||
adoptedRunId,
|
||||
workerPrincipalId: worker.principal.id
|
||||
}
|
||||
}
|
||||
|
||||
it('records exact scheduler loss without admitting naturally settled coordinators', () => {
|
||||
const fixture = createCutoverFixture()
|
||||
const raw = new Database(fixture.dbPath)
|
||||
raw.exec(`
|
||||
UPDATE tasks
|
||||
SET created_by_terminal_handle = 'term_manual_coord'
|
||||
WHERE run_id = '${LEGACY_RUN_ID}';
|
||||
UPDATE messages
|
||||
SET from_handle = CASE
|
||||
WHEN from_handle = 'term_legacy_coord' THEN 'term_manual_coord'
|
||||
ELSE from_handle
|
||||
END,
|
||||
to_handle = CASE
|
||||
WHEN to_handle = 'term_legacy_coord' THEN 'term_manual_coord'
|
||||
ELSE to_handle
|
||||
END
|
||||
WHERE run_id = '${LEGACY_RUN_ID}';
|
||||
INSERT INTO coordinator_runs (
|
||||
id, spec, status, coordinator_handle, created_at
|
||||
) VALUES (
|
||||
'coordinator_manual', 'manual legacy coordinator', 'running',
|
||||
'term_manual_coord', datetime('now', '-1 minute')
|
||||
);
|
||||
INSERT INTO coordinator_runs (
|
||||
id, spec, status, coordinator_handle, created_at, completed_at
|
||||
) VALUES (
|
||||
'coordinator_natural', 'naturally completed coordinator', 'completed',
|
||||
'term_natural_coord', datetime('now', '-2 minutes'),
|
||||
'2099-01-01T00:00:00.000Z'
|
||||
);
|
||||
`)
|
||||
raw.close()
|
||||
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
expect(db.getActiveCoordinatorRun()).toBeUndefined()
|
||||
const adoption = db.getLegacyAdoption()!
|
||||
const migratedCoordinator = db.getCoordinatorRun('coordinator_manual')!
|
||||
expect(migratedCoordinator).toMatchObject({
|
||||
status: 'failed',
|
||||
completed_at: adoption.adopted_at,
|
||||
scheduler_lost_at: adoption.adopted_at
|
||||
})
|
||||
expect(db.getCoordinatorRun('coordinator_natural')).toMatchObject({
|
||||
status: 'completed',
|
||||
completed_at: '2099-01-01T00:00:00.000Z',
|
||||
scheduler_lost_at: null
|
||||
})
|
||||
expect(
|
||||
db.resolveLegacyCoordinatorCandidate({
|
||||
runId: adoptedRunId,
|
||||
terminalHandle: 'term_manual_coord',
|
||||
paneKey: 'tab_manual:44444444-4444-4444-8444-444444444444'
|
||||
})
|
||||
).toMatchObject({ terminalHandle: 'term_manual_coord' })
|
||||
expect(db.isLegacyCoordinatorHandle(adoptedRunId, 'term_natural_coord')).toBe(false)
|
||||
|
||||
db.close()
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
expect(db.getLegacyAdoption()).toEqual(adoption)
|
||||
expect(db.getCoordinatorRun('coordinator_manual')).toEqual(migratedCoordinator)
|
||||
expect(
|
||||
db.resolveLegacyCoordinatorCandidate({
|
||||
runId: adoptedRunId,
|
||||
terminalHandle: 'term_manual_coord',
|
||||
paneKey: 'tab_manual:44444444-4444-4444-8444-444444444444'
|
||||
})
|
||||
).toMatchObject({ terminalHandle: 'term_manual_coord' })
|
||||
})
|
||||
|
||||
it('fails closed when multiple pre-cutover schedulers lose authority', () => {
|
||||
const fixture = createCutoverFixture()
|
||||
const raw = new Database(fixture.dbPath)
|
||||
raw.exec(`
|
||||
INSERT INTO coordinator_runs (
|
||||
id, spec, status, coordinator_handle, created_at
|
||||
) VALUES
|
||||
('coordinator_first', 'first', 'running', 'term_first_coord', datetime('now', '-2 minutes')),
|
||||
('coordinator_second', 'second', 'running', 'term_second_coord', datetime('now', '-1 minute'));
|
||||
`)
|
||||
raw.close()
|
||||
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
const adoption = db.getLegacyAdoption()!
|
||||
const adoptedRunId = adoption.adopted_run_id
|
||||
expect(db.getActiveCoordinatorRun()).toBeUndefined()
|
||||
expect(db.getCoordinatorRun('coordinator_first')).toMatchObject({
|
||||
status: 'failed',
|
||||
scheduler_lost_at: adoption.adopted_at
|
||||
})
|
||||
expect(db.getCoordinatorRun('coordinator_second')).toMatchObject({
|
||||
status: 'failed',
|
||||
scheduler_lost_at: adoption.adopted_at
|
||||
})
|
||||
expect(
|
||||
db.resolveLegacyCoordinatorCandidate({
|
||||
runId: adoptedRunId,
|
||||
terminalHandle: 'term_legacy_coord',
|
||||
paneKey: 'tab_manual:44444444-4444-4444-8444-444444444444'
|
||||
})
|
||||
).toBeUndefined()
|
||||
})
|
||||
|
||||
it('does not infer a coordinator from worker-to-worker legacy mail', () => {
|
||||
const fixture = createCutoverFixture()
|
||||
const raw = new Database(fixture.dbPath)
|
||||
raw.exec(`
|
||||
INSERT INTO tasks (
|
||||
id, run_id, spec, status, created_by_terminal_handle
|
||||
) VALUES (
|
||||
'task_second_worker', '${LEGACY_RUN_ID}', 'second worker',
|
||||
'dispatched', 'term_legacy_coord'
|
||||
);
|
||||
INSERT INTO dispatch_contexts (
|
||||
id, run_id, task_id, contract_version, assignee_handle,
|
||||
assignee_pane_key, status, dispatched_at
|
||||
) VALUES (
|
||||
'dispatch_second_worker', '${LEGACY_RUN_ID}', 'task_second_worker', 0,
|
||||
'term_second_worker', 'tab_second:55555555-5555-4555-8555-555555555555',
|
||||
'dispatched', datetime('now')
|
||||
);
|
||||
INSERT INTO messages (
|
||||
id, run_id, from_handle, to_handle, subject
|
||||
) VALUES (
|
||||
'message_worker_to_worker', '${LEGACY_RUN_ID}', 'term_legacy_worker',
|
||||
'term_second_worker', 'peer coordination'
|
||||
);
|
||||
`)
|
||||
raw.close()
|
||||
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
expect(db.isLegacyCoordinatorHandle(adoptedRunId, 'term_second_worker')).toBe(false)
|
||||
expect(db.isLegacyCoordinatorHandle(adoptedRunId, 'term_legacy_worker')).toBe(false)
|
||||
expect(db.isLegacyCoordinatorHandle(adoptedRunId, 'term_legacy_coord')).toBe(true)
|
||||
})
|
||||
|
||||
it('promotes only the unacknowledged coordinator recovery cohort during takeover', () => {
|
||||
const fixture = createCutoverFixture()
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
const unacknowledged = db.insertMessage({
|
||||
runId: adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'second recovered coordinator outcome'
|
||||
})
|
||||
db.markAsRead([unacknowledged.id])
|
||||
const coordinator = db.commitLegacyCompatibilityPrincipal({
|
||||
runId: adoptedRunId,
|
||||
role: 'coordinator',
|
||||
hostScope: 'local:runtime_1',
|
||||
terminalHandle: 'term_legacy_coord',
|
||||
paneKey: 'tab_coord:44444444-4444-4444-8444-444444444444',
|
||||
launchTokenHash: 'coord_launch_hash',
|
||||
processIncarnation: 'process_coord'
|
||||
}).principal
|
||||
const firstPage = db.getLegacyMailPage({ principalId: coordinator.id, limit: 1 })
|
||||
expect(firstPage.recovery).toBe(true)
|
||||
db.acknowledgeLegacyMail({
|
||||
principalId: coordinator.id,
|
||||
messageIds: firstPage.messages.map((message) => message.id)
|
||||
})
|
||||
const acknowledgedId = firstPage.messages[0].id
|
||||
|
||||
db.bindRun({
|
||||
runId: adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:11111111-1111-4111-8111-111111111111',
|
||||
takeoverLegacy: true
|
||||
})
|
||||
|
||||
expect(db.getMessageById(acknowledgedId)).toMatchObject({
|
||||
to_handle: 'term_legacy_coord',
|
||||
delivery_contract: 'legacy_direct',
|
||||
read: 1
|
||||
})
|
||||
expect(db.getMessageById(unacknowledged.id)).toMatchObject({
|
||||
to_handle: `run:${adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery',
|
||||
read: 0
|
||||
})
|
||||
})
|
||||
|
||||
it('promotes a pre-cutover read recovery message when takeover precedes attestation', () => {
|
||||
const fixture = createCutoverFixture()
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
const recoveryMessageId = fixture.legacyMessageIds[1]
|
||||
expect(db.getLegacyCoordinatorPrincipal(adoptedRunId)).toBeUndefined()
|
||||
expect(db.getMessageById(recoveryMessageId)).toMatchObject({
|
||||
to_handle: 'term_legacy_coord',
|
||||
delivery_contract: 'legacy_direct',
|
||||
read: 1
|
||||
})
|
||||
|
||||
const run = db.bindRun({
|
||||
runId: adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:11111111-1111-4111-8111-111111111111',
|
||||
takeoverLegacy: true
|
||||
})!
|
||||
expect(db.getMessageById(recoveryMessageId)).toMatchObject({
|
||||
to_handle: `run:${adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery',
|
||||
read: 0
|
||||
})
|
||||
|
||||
const first = db.getOrCreateRunDelivery({
|
||||
runId: adoptedRunId,
|
||||
consumerGeneration: run.consumer_generation
|
||||
})!
|
||||
const replay = db.getOrCreateRunDelivery({
|
||||
runId: adoptedRunId,
|
||||
consumerGeneration: run.consumer_generation
|
||||
})!
|
||||
expect(first.messages.map((message) => message.id)).toContain(recoveryMessageId)
|
||||
expect(replay).toMatchObject({ replayed: true, delivery: { id: first.delivery.id } })
|
||||
|
||||
db.acknowledgeRunDelivery({
|
||||
runId: adoptedRunId,
|
||||
consumerGeneration: run.consumer_generation,
|
||||
deliveryId: first.delivery.id
|
||||
})
|
||||
expect(
|
||||
db
|
||||
.getOrCreateRunDelivery({
|
||||
runId: adoptedRunId,
|
||||
consumerGeneration: run.consumer_generation
|
||||
})
|
||||
?.messages.map((message) => message.id) ?? []
|
||||
).not.toContain(recoveryMessageId)
|
||||
})
|
||||
|
||||
it('promotes retained mail on an ordinary post-settlement coordinator replacement', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const coordinator = db!.commitLegacyCompatibilityPrincipal({
|
||||
runId: state.adoptedRunId,
|
||||
role: 'coordinator',
|
||||
hostScope: 'local:runtime_1',
|
||||
terminalHandle: 'term_legacy_coord',
|
||||
paneKey: 'tab_coord:44444444-4444-4444-8444-444444444444',
|
||||
launchTokenHash: 'coord_launch_hash',
|
||||
processIncarnation: 'process_coord'
|
||||
}).principal
|
||||
const completion = db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Completed before ordinary run-use',
|
||||
type: 'worker_done'
|
||||
})
|
||||
db!.settleWorkerReport({
|
||||
taskId: state.fixture.legacyTaskId,
|
||||
dispatchId: state.fixture.legacyDispatchId,
|
||||
outcome: 'succeeded',
|
||||
result: 'done'
|
||||
})
|
||||
|
||||
const run = db!.bindRun({
|
||||
runId: state.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:11111111-1111-4111-8111-111111111111'
|
||||
})!
|
||||
|
||||
expect(db!.getLegacyCompatibilityPrincipal(coordinator.id)?.status).toBe('revoked')
|
||||
expect(db!.getMessageById(completion.id)).toMatchObject({
|
||||
to_handle: `run:${state.adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery',
|
||||
read: 0
|
||||
})
|
||||
expect(
|
||||
db!
|
||||
.getOrCreateRunDelivery({
|
||||
runId: state.adoptedRunId,
|
||||
consumerGeneration: run.consumer_generation
|
||||
})
|
||||
?.messages.map((message) => message.id)
|
||||
).toContain(completion.id)
|
||||
})
|
||||
|
||||
it('acknowledges the exact current Run answer to a legacy ask after takeover', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const ask = db!.commitLegacyAskOperation({
|
||||
principalId: state.workerPrincipalId,
|
||||
operationKey: 'ask_before_takeover',
|
||||
method: 'orchestration.ask',
|
||||
payloadHash: 'ask_before_takeover_payload',
|
||||
question: 'Continue after takeover?',
|
||||
recipientHandle: 'term_legacy_coord'
|
||||
})
|
||||
const run = db!.bindRun({
|
||||
runId: state.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:11111111-1111-4111-8111-111111111111',
|
||||
takeoverLegacy: true
|
||||
})!
|
||||
const answered = db!.answerQuestion({
|
||||
messageId: ask.question.message_id,
|
||||
runId: state.adoptedRunId,
|
||||
consumerGeneration: run.consumer_generation,
|
||||
body: 'continue'
|
||||
})
|
||||
expect(answered.message).toMatchObject({
|
||||
from_handle: `run:${state.adoptedRunId}`,
|
||||
to_handle: `dispatch:${state.fixture.legacyDispatchId}`,
|
||||
delivery_contract: 'current_delivery'
|
||||
})
|
||||
|
||||
const acknowledged = db!.acknowledgeLegacyQuestionAnswer({
|
||||
principalId: state.workerPrincipalId,
|
||||
questionId: ask.question.message_id,
|
||||
answerMessageId: answered.message.id
|
||||
})
|
||||
|
||||
expect(acknowledged.duplicate).toBe(false)
|
||||
expect(db!.getMessageById(answered.message.id)?.read).toBe(1)
|
||||
expect(
|
||||
db!.acknowledgeLegacyQuestionAnswer({
|
||||
principalId: state.workerPrincipalId,
|
||||
questionId: ask.question.message_id,
|
||||
answerMessageId: answered.message.id
|
||||
}).duplicate
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,255 @@
|
||||
import { rmSync } from 'node:fs'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import { LEGACY_RUN_ID, OrchestrationDb } from './db'
|
||||
import { createLegacyStorageCutoverFixture } from './orchestration-legacy-storage-test-fixture'
|
||||
|
||||
describe('OrchestrationDb legacy question migration', () => {
|
||||
let db: OrchestrationDb | undefined
|
||||
let tempDir: string | undefined
|
||||
|
||||
afterEach(() => {
|
||||
db?.close()
|
||||
if (tempDir) {
|
||||
rmSync(tempDir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
function prepareTakenOverSchema19Question(readAndAcknowledge: boolean): {
|
||||
dbPath: string
|
||||
adoptedRunId: string
|
||||
questionId: string
|
||||
} {
|
||||
const created = createLegacyStorageCutoverFixture()
|
||||
tempDir = created.tempDir
|
||||
db = new OrchestrationDb(created.fixture.dbPath)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
const questionId = readAndAcknowledge ? 'msg_schema19_acknowledged' : 'msg_schema19_unread'
|
||||
db.insertMessage({
|
||||
id: questionId,
|
||||
runId: adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Question',
|
||||
body: 'Continue after takeover?',
|
||||
type: 'decision_gate',
|
||||
threadId: questionId,
|
||||
payload: JSON.stringify({
|
||||
taskId: created.fixture.legacyTaskId,
|
||||
dispatchId: created.fixture.legacyDispatchId
|
||||
})
|
||||
})
|
||||
if (readAndAcknowledge) {
|
||||
db.markAsRead([questionId])
|
||||
}
|
||||
const coordinator = db.commitLegacyCompatibilityPrincipal({
|
||||
runId: adoptedRunId,
|
||||
role: 'coordinator',
|
||||
hostScope: 'local:runtime_1',
|
||||
terminalHandle: 'term_legacy_coord',
|
||||
paneKey: 'tab_coord:44444444-4444-4444-8444-444444444444',
|
||||
launchTokenHash: 'coord_launch_hash',
|
||||
processIncarnation: 'process_coord'
|
||||
}).principal
|
||||
if (readAndAcknowledge) {
|
||||
const recovery = db.getLegacyMailPage({ principalId: coordinator.id })
|
||||
expect(recovery.messages.map((message) => message.id)).toContain(questionId)
|
||||
db.acknowledgeLegacyMail({
|
||||
principalId: coordinator.id,
|
||||
messageIds: recovery.messages.map((message) => message.id)
|
||||
})
|
||||
}
|
||||
db.bindRun({
|
||||
runId: adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:11111111-1111-4111-8111-111111111111',
|
||||
takeoverLegacy: true
|
||||
})
|
||||
expect(db.getMessageById(questionId)).toMatchObject(
|
||||
readAndAcknowledge
|
||||
? { delivery_contract: 'legacy_direct', read: 1 }
|
||||
: { delivery_contract: 'current_delivery', read: 0 }
|
||||
)
|
||||
db.close()
|
||||
db = undefined
|
||||
|
||||
const raw = new Database(created.fixture.dbPath)
|
||||
raw.exec('DROP TABLE question_threads')
|
||||
raw.pragma('user_version = 19')
|
||||
raw.close()
|
||||
return { dbPath: created.fixture.dbPath, adoptedRunId, questionId }
|
||||
}
|
||||
|
||||
it('backfills a pre-question-thread ask and its accepted answer', () => {
|
||||
const created = createLegacyStorageCutoverFixture()
|
||||
tempDir = created.tempDir
|
||||
db = new OrchestrationDb(created.fixture.dbPath)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
db.close()
|
||||
db = undefined
|
||||
|
||||
const raw = new Database(created.fixture.dbPath)
|
||||
raw.exec(`
|
||||
DROP TABLE question_threads;
|
||||
INSERT INTO messages (
|
||||
id, run_id, delivery_contract, from_handle, to_handle,
|
||||
subject, body, type, thread_id, payload
|
||||
) VALUES (
|
||||
'msg_cutover', '${adoptedRunId}', 'legacy_direct',
|
||||
'term_legacy_worker', 'term_legacy_coord', 'Question',
|
||||
'Continue?', 'decision_gate', 'msg_cutover',
|
||||
'{"taskId":"${created.fixture.legacyTaskId}","dispatchId":"${created.fixture.legacyDispatchId}"}'
|
||||
);
|
||||
INSERT INTO messages (
|
||||
id, run_id, delivery_contract, from_handle, to_handle,
|
||||
subject, body, thread_id
|
||||
) VALUES
|
||||
(
|
||||
'msg_cross_run_decoy', 'run_decoy', 'legacy_direct',
|
||||
'term_legacy_coord', 'term_legacy_worker',
|
||||
'Re: Question', 'cross-run decoy', 'msg_cutover'
|
||||
),
|
||||
(
|
||||
'msg_self_sent_decoy', '${adoptedRunId}', 'legacy_direct',
|
||||
'term_legacy_worker', 'term_legacy_worker',
|
||||
'Re: Question', 'self-sent decoy', 'msg_cutover'
|
||||
),
|
||||
(
|
||||
'msg_cutover_answer', '${adoptedRunId}', 'legacy_direct',
|
||||
'term_legacy_coord', 'term_legacy_worker',
|
||||
'Re: Question', 'yes', 'msg_cutover'
|
||||
);
|
||||
`)
|
||||
raw.pragma('user_version = 19')
|
||||
raw.close()
|
||||
|
||||
db = new OrchestrationDb(created.fixture.dbPath)
|
||||
expect(db.getQuestion('msg_cutover')).toMatchObject({
|
||||
run_id: adoptedRunId,
|
||||
dispatch_id: created.fixture.legacyDispatchId,
|
||||
asker_handle: 'term_legacy_worker',
|
||||
status: 'answered',
|
||||
answer_message_id: 'msg_cutover_answer',
|
||||
answer_body: 'yes'
|
||||
})
|
||||
})
|
||||
|
||||
it('leaves a question pending when only cross-run and self-sent decoys exist', () => {
|
||||
const created = createLegacyStorageCutoverFixture()
|
||||
tempDir = created.tempDir
|
||||
db = new OrchestrationDb(created.fixture.dbPath)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
db.close()
|
||||
db = undefined
|
||||
|
||||
const raw = new Database(created.fixture.dbPath)
|
||||
raw.exec(`
|
||||
DROP TABLE question_threads;
|
||||
INSERT INTO messages (
|
||||
id, run_id, delivery_contract, from_handle, to_handle,
|
||||
subject, body, type, thread_id, payload
|
||||
) VALUES (
|
||||
'msg_pending_cutover', '${adoptedRunId}', 'legacy_direct',
|
||||
'term_legacy_worker', 'term_legacy_coord', 'Question',
|
||||
'Continue?', 'decision_gate', 'msg_pending_cutover',
|
||||
'{"taskId":"${created.fixture.legacyTaskId}","dispatchId":"${created.fixture.legacyDispatchId}"}'
|
||||
);
|
||||
INSERT INTO messages (
|
||||
id, run_id, delivery_contract, from_handle, to_handle,
|
||||
subject, body, thread_id
|
||||
) VALUES
|
||||
(
|
||||
'msg_pending_cross_run_decoy', 'run_decoy', 'legacy_direct',
|
||||
'term_legacy_coord', 'term_legacy_worker',
|
||||
'Re: Question', 'cross-run decoy', 'msg_pending_cutover'
|
||||
),
|
||||
(
|
||||
'msg_pending_self_sent_decoy', '${adoptedRunId}', 'legacy_direct',
|
||||
'term_legacy_worker', 'term_legacy_worker',
|
||||
'Re: Question', 'self-sent decoy', 'msg_pending_cutover'
|
||||
);
|
||||
`)
|
||||
raw.pragma('user_version = 19')
|
||||
raw.close()
|
||||
|
||||
db = new OrchestrationDb(created.fixture.dbPath)
|
||||
expect(db.getQuestion('msg_pending_cutover')).toMatchObject({
|
||||
run_id: adoptedRunId,
|
||||
dispatch_id: created.fixture.legacyDispatchId,
|
||||
asker_handle: 'term_legacy_worker',
|
||||
status: 'pending',
|
||||
answer_message_id: null,
|
||||
answer_body: null
|
||||
})
|
||||
})
|
||||
|
||||
it('uses task provenance to disambiguate same-handle same-second asks', () => {
|
||||
const created = createLegacyStorageCutoverFixture()
|
||||
tempDir = created.tempDir
|
||||
const raw = new Database(created.fixture.dbPath)
|
||||
raw.exec(`
|
||||
INSERT INTO tasks (
|
||||
id, run_id, spec, status, created_by_terminal_handle, created_at
|
||||
) VALUES
|
||||
('task_question_target', '${LEGACY_RUN_ID}', 'target', 'dispatched',
|
||||
'term_legacy_coord', '2026-01-01 00:00:00'),
|
||||
('task_question_other', '${LEGACY_RUN_ID}', 'other', 'dispatched',
|
||||
'term_legacy_coord', '2026-01-01 00:00:00');
|
||||
INSERT INTO dispatch_contexts (
|
||||
id, run_id, task_id, contract_version, assignee_handle,
|
||||
assignee_pane_key, status, dispatched_at, created_at
|
||||
) VALUES
|
||||
('dispatch_question_target', '${LEGACY_RUN_ID}', 'task_question_target', 0,
|
||||
'term_legacy_worker', 'tab_target:11111111-1111-4111-8111-111111111111',
|
||||
'dispatched', '2026-01-01 00:00:00', '2026-01-01 00:00:00'),
|
||||
('dispatch_question_other', '${LEGACY_RUN_ID}', 'task_question_other', 0,
|
||||
'term_legacy_worker', 'tab_other:22222222-2222-4222-8222-222222222222',
|
||||
'dispatched', '2026-01-01 00:00:00', '2026-01-01 00:00:00');
|
||||
INSERT INTO messages (
|
||||
id, run_id, delivery_contract, from_handle, to_handle,
|
||||
subject, body, type, payload, created_at
|
||||
) VALUES (
|
||||
'msg_question_task_only', '${LEGACY_RUN_ID}', 'legacy_direct',
|
||||
'term_legacy_worker', 'term_legacy_coord', 'Question', 'Continue?',
|
||||
'decision_gate', '{"taskId":"task_question_target","question":"Continue?","options":[]}',
|
||||
'2026-01-01 00:00:00'
|
||||
);
|
||||
`)
|
||||
raw.close()
|
||||
|
||||
db = new OrchestrationDb(created.fixture.dbPath)
|
||||
expect(db.getQuestion('msg_question_task_only')).toMatchObject({
|
||||
dispatch_id: 'dispatch_question_target',
|
||||
asker_handle: 'term_legacy_worker',
|
||||
status: 'pending'
|
||||
})
|
||||
})
|
||||
|
||||
it.each([
|
||||
['promoted unread', false],
|
||||
['acknowledged recovery', true]
|
||||
] as const)('backfills and routes a %s v19 question after takeover', (_label, acknowledged) => {
|
||||
const state = prepareTakenOverSchema19Question(acknowledged)
|
||||
|
||||
db = new OrchestrationDb(state.dbPath)
|
||||
expect(db.getQuestion(state.questionId)).toMatchObject({
|
||||
run_id: state.adoptedRunId,
|
||||
status: 'pending'
|
||||
})
|
||||
expect(db.getMessageById(state.questionId)).toMatchObject({
|
||||
to_handle: `run:${state.adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery',
|
||||
read: 0
|
||||
})
|
||||
const run = db.getRun(state.adoptedRunId)!
|
||||
expect(
|
||||
db
|
||||
.getOrCreateRunDelivery({
|
||||
runId: state.adoptedRunId,
|
||||
consumerGeneration: run.consumer_generation
|
||||
})
|
||||
?.messages.map((message) => message.id)
|
||||
).toContain(state.questionId)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,751 @@
|
||||
import { rmSync } from 'node:fs'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import {
|
||||
CURRENT_CONTRACT_VERSION,
|
||||
LEGACY_CONTRACT_VERSION,
|
||||
LEGACY_RUN_ID,
|
||||
OrchestrationDb
|
||||
} from './db'
|
||||
import {
|
||||
createLegacyStorageCutoverFixture,
|
||||
type LegacyStorageCutoverFixture
|
||||
} from './orchestration-legacy-storage-test-fixture'
|
||||
|
||||
describe('OrchestrationDb legacy contract storage', () => {
|
||||
let db: OrchestrationDb | undefined
|
||||
let tempDir: string | undefined
|
||||
|
||||
afterEach(() => {
|
||||
db?.close()
|
||||
db = undefined
|
||||
if (tempDir) {
|
||||
rmSync(tempDir, { recursive: true, force: true })
|
||||
tempDir = undefined
|
||||
}
|
||||
})
|
||||
|
||||
function createCutoverFixture(): LegacyStorageCutoverFixture {
|
||||
const created = createLegacyStorageCutoverFixture()
|
||||
tempDir = created.tempDir
|
||||
return created.fixture
|
||||
}
|
||||
|
||||
function openAdoptedFixture(): {
|
||||
fixture: LegacyStorageCutoverFixture
|
||||
adoptedRunId: string
|
||||
workerPrincipalId: string
|
||||
coordinatorPrincipalId: string
|
||||
} {
|
||||
const fixture = createCutoverFixture()
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
const worker = db.commitLegacyCompatibilityPrincipal({
|
||||
runId: adoptedRunId,
|
||||
dispatchId: fixture.legacyDispatchId,
|
||||
role: 'worker',
|
||||
hostScope: 'local:runtime_1',
|
||||
terminalHandle: 'term_legacy_worker',
|
||||
paneKey: 'tab_legacy:33333333-3333-4333-8333-333333333333',
|
||||
launchTokenHash: 'legacy_launch_hash',
|
||||
processIncarnation: 'process_1'
|
||||
})
|
||||
const coordinator = db.commitLegacyCompatibilityPrincipal({
|
||||
runId: adoptedRunId,
|
||||
role: 'coordinator',
|
||||
hostScope: 'local:runtime_1',
|
||||
terminalHandle: 'term_legacy_coord',
|
||||
paneKey: 'tab_coord:44444444-4444-4444-8444-444444444444',
|
||||
launchTokenHash: 'coord_launch_hash',
|
||||
processIncarnation: 'process_coord'
|
||||
})
|
||||
return {
|
||||
fixture,
|
||||
adoptedRunId,
|
||||
workerPrincipalId: worker.principal.id,
|
||||
coordinatorPrincipalId: coordinator.principal.id
|
||||
}
|
||||
}
|
||||
|
||||
it('atomically rehomes the full graph, fences legacy Delivery, and preserves current rows', () => {
|
||||
const fixture = createCutoverFixture()
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
const adoption = db.getLegacyAdoption()
|
||||
const adoptedRunId = adoption?.adopted_run_id as string
|
||||
const sqlite = (db as unknown as { db: Database.Database }).db
|
||||
|
||||
expect(adoption).toMatchObject({
|
||||
source_run_id: LEGACY_RUN_ID,
|
||||
scheduler_state_lost: 1
|
||||
})
|
||||
expect(db.getRun(adoptedRunId)).toMatchObject({ legacy: 0, consumer_generation: 0 })
|
||||
expect(db.listTasks({ runId: LEGACY_RUN_ID })).toEqual([])
|
||||
expect(db.getDispatchContextById(fixture.legacyDispatchId)).toMatchObject({
|
||||
run_id: adoptedRunId,
|
||||
contract_version: LEGACY_CONTRACT_VERSION,
|
||||
launch_token_hash: null
|
||||
})
|
||||
expect(db.getGate(fixture.legacyGateId)).toMatchObject({ run_id: adoptedRunId })
|
||||
expect(db.getQuestion(fixture.legacyQuestionId)).toMatchObject({ run_id: adoptedRunId })
|
||||
expect(db.getMessageById(fixture.legacyMessageIds[0])).toMatchObject({
|
||||
run_id: adoptedRunId,
|
||||
delivery_contract: 'legacy_direct'
|
||||
})
|
||||
expect(db.getMessageById(fixture.rejectionMessageId)).toMatchObject({
|
||||
run_id: adoptedRunId,
|
||||
delivery_contract: 'audit_only'
|
||||
})
|
||||
expect(db.getMessageById(fixture.lookalikeMessageId)).toMatchObject({
|
||||
run_id: adoptedRunId,
|
||||
delivery_contract: 'legacy_direct'
|
||||
})
|
||||
for (const messageId of fixture.malformedRejectionMessageIds) {
|
||||
expect(db.getMessageById(messageId)).toMatchObject({
|
||||
run_id: adoptedRunId,
|
||||
delivery_contract: 'legacy_direct'
|
||||
})
|
||||
}
|
||||
expect(
|
||||
sqlite.prepare('SELECT * FROM deliveries WHERE id = ?').get(fixture.legacyDeliveryId)
|
||||
).toMatchObject({ run_id: adoptedRunId, status: 'fenced' })
|
||||
expect(db.getDispatchContextById(fixture.currentDispatchId)).toMatchObject({
|
||||
run_id: fixture.currentRunId,
|
||||
contract_version: CURRENT_CONTRACT_VERSION,
|
||||
launch_token_hash: 'current_launch_hash'
|
||||
})
|
||||
|
||||
db.close()
|
||||
const partial = new Database(fixture.dbPath)
|
||||
partial
|
||||
.prepare('UPDATE tasks SET run_id = ? WHERE id = ?')
|
||||
.run(LEGACY_RUN_ID, fixture.legacyTaskId)
|
||||
partial.pragma('user_version = 19')
|
||||
partial.close()
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
expect(db.getLegacyAdoption()?.adopted_run_id).toBe(adoptedRunId)
|
||||
expect(db.getTask(fixture.legacyTaskId)?.run_id).toBe(adoptedRunId)
|
||||
expect(db.getDispatchContextById(fixture.currentDispatchId)?.contract_version).toBe(
|
||||
CURRENT_CONTRACT_VERSION
|
||||
)
|
||||
expect(db.listTasks({ runId: LEGACY_RUN_ID })).toEqual([])
|
||||
})
|
||||
|
||||
it('ignores unrelated cross-Run anomalies while validating adopted rows', () => {
|
||||
const fixture = createCutoverFixture()
|
||||
const raw = new Database(fixture.dbPath)
|
||||
raw
|
||||
.prepare('UPDATE dispatch_contexts SET run_id = ? WHERE id = ?')
|
||||
.run(fixture.unrelatedRunId, fixture.currentDispatchId)
|
||||
raw.close()
|
||||
|
||||
db = new OrchestrationDb(fixture.dbPath)
|
||||
|
||||
expect(db.getLegacyAdoption()).toBeDefined()
|
||||
expect(db.getDispatchContextById(fixture.currentDispatchId)?.run_id).toBe(
|
||||
fixture.unrelatedRunId
|
||||
)
|
||||
})
|
||||
|
||||
it('does not synthesize an adopted Run or compatibility authority for a fresh database', () => {
|
||||
db = new OrchestrationDb(':memory:')
|
||||
|
||||
expect(db.getLegacyAdoption()).toBeUndefined()
|
||||
expect(db.listLegacyCompatibilityPrincipals(LEGACY_RUN_ID)).toEqual([])
|
||||
expect(db.listRuns()).toEqual([expect.objectContaining({ id: LEGACY_RUN_ID, legacy: 1 })])
|
||||
})
|
||||
|
||||
it('keeps current Delivery disjoint from adopted direct and audit-only mail', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const run = db!.getRun(state.adoptedRunId) as NonNullable<ReturnType<OrchestrationDb['getRun']>>
|
||||
db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
from: 'current_worker',
|
||||
to: `run:${state.adoptedRunId}`,
|
||||
subject: 'current retry mail'
|
||||
})
|
||||
|
||||
const delivery = db!.getOrCreateRunDelivery({
|
||||
runId: state.adoptedRunId,
|
||||
consumerGeneration: run.consumer_generation
|
||||
})
|
||||
|
||||
expect(delivery?.messages.map((message) => message.subject)).toEqual(['current retry mail'])
|
||||
})
|
||||
|
||||
it('drains a durable recovery cohort in bounded replaying pages before unread mail', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const first = db!.getLegacyMailPage({ principalId: state.workerPrincipalId, limit: 1 })
|
||||
const replay = db!.getLegacyMailPage({ principalId: state.workerPrincipalId, limit: 1 })
|
||||
|
||||
expect(first.recovery).toBe(true)
|
||||
expect(replay.messages.map((message) => message.id)).toEqual(
|
||||
first.messages.map((message) => message.id)
|
||||
)
|
||||
expect(() =>
|
||||
db!.acknowledgeLegacyMail({
|
||||
principalId: state.workerPrincipalId,
|
||||
messageIds: [state.fixture.legacyMessageIds[2]]
|
||||
})
|
||||
).toThrow(/current replay page/)
|
||||
db!.acknowledgeLegacyMail({
|
||||
principalId: state.workerPrincipalId,
|
||||
messageIds: first.messages.map((message) => message.id)
|
||||
})
|
||||
const second = db!.getLegacyMailPage({ principalId: state.workerPrincipalId, limit: 1 })
|
||||
expect(second.recovery).toBe(true)
|
||||
expect(second.messages[0].id).not.toBe(first.messages[0].id)
|
||||
db!.acknowledgeLegacyMail({
|
||||
principalId: state.workerPrincipalId,
|
||||
messageIds: second.messages.map((message) => message.id)
|
||||
})
|
||||
expect(db!.getLegacyMailPage({ principalId: state.workerPrincipalId, limit: 1 }).recovery).toBe(
|
||||
false
|
||||
)
|
||||
})
|
||||
|
||||
it('returns complete addressed legacy history without changing read state', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const unread = db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_coord',
|
||||
to: 'term_legacy_worker',
|
||||
subject: 'unread history'
|
||||
})
|
||||
db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'other',
|
||||
to: 'other',
|
||||
subject: 'not addressed'
|
||||
})
|
||||
|
||||
const history = db!.getLegacyMailHistory({ principalId: state.workerPrincipalId })
|
||||
|
||||
expect(history.recovery).toBe(false)
|
||||
expect(history.messages.map((message) => message.subject)).toEqual([
|
||||
'read worker mail',
|
||||
'second worker page',
|
||||
'unread history'
|
||||
])
|
||||
expect(db!.getMessageById(unread.id)?.read).toBe(0)
|
||||
})
|
||||
|
||||
it('resolves legacy principals and completion evidence only within exact assignments', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const taskId = db!.getDispatchContextById(state.fixture.legacyDispatchId)!.task_id
|
||||
const payload = JSON.stringify({ taskId, dispatchId: state.fixture.legacyDispatchId })
|
||||
const completion = db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Completed',
|
||||
body: 'done',
|
||||
type: 'worker_done',
|
||||
payload
|
||||
})
|
||||
|
||||
expect(
|
||||
db!.resolveLegacyWorkerCandidate({
|
||||
runId: state.adoptedRunId,
|
||||
terminalHandle: 'term_legacy_worker',
|
||||
paneKey: 'tab_reminted:33333333-3333-4333-8333-333333333333',
|
||||
taskId
|
||||
})
|
||||
).toMatchObject({ dispatch: { id: state.fixture.legacyDispatchId } })
|
||||
expect(
|
||||
db!.resolveLegacyWorkerCandidate({
|
||||
runId: state.adoptedRunId,
|
||||
terminalHandle: 'term_legacy_worker',
|
||||
paneKey: 'tab_wrong:99999999-9999-4999-8999-999999999999',
|
||||
taskId
|
||||
})
|
||||
).toBeUndefined()
|
||||
expect(
|
||||
db!.resolveLegacyCoordinatorCandidate({
|
||||
runId: state.adoptedRunId,
|
||||
terminalHandle: 'term_legacy_coord',
|
||||
paneKey: 'tab_coord:44444444-4444-4444-8444-444444444444'
|
||||
})
|
||||
).toMatchObject({ terminalHandle: 'term_legacy_coord' })
|
||||
expect(
|
||||
db!.findLegacyWorkerCompletion({
|
||||
principalId: state.workerPrincipalId,
|
||||
taskId,
|
||||
recipientHandle: 'term_legacy_coord',
|
||||
subject: 'Completed',
|
||||
body: 'done',
|
||||
payload
|
||||
})
|
||||
).toMatchObject({ id: completion.id })
|
||||
})
|
||||
|
||||
it('prevents an unproven coordinator from taking over an active adopted Run', () => {
|
||||
const state = openAdoptedFixture()
|
||||
expect(() =>
|
||||
db!.bindRun({
|
||||
runId: state.adoptedRunId,
|
||||
coordinatorHandle: 'term_other',
|
||||
coordinatorPaneKey: 'tab_other:55555555-5555-4555-8555-555555555555'
|
||||
})
|
||||
).toThrow(/attested coordinator may rebind/)
|
||||
|
||||
const bound = db!.bindRun({
|
||||
runId: state.adoptedRunId,
|
||||
coordinatorHandle: 'term_legacy_coord',
|
||||
coordinatorPaneKey: 'tab_coord:44444444-4444-4444-8444-444444444444',
|
||||
legacyCoordinatorAuthority: {
|
||||
runId: state.adoptedRunId,
|
||||
principalId: state.coordinatorPrincipalId,
|
||||
terminalHandle: 'term_legacy_coord',
|
||||
paneKey: 'tab_coord:44444444-4444-4444-8444-444444444444',
|
||||
consumerGeneration: 0
|
||||
}
|
||||
})
|
||||
expect(bound).toMatchObject({ coordinator_handle: 'term_legacy_coord' })
|
||||
expect(
|
||||
db!.bindRun({
|
||||
runId: state.adoptedRunId,
|
||||
coordinatorHandle: 'term_legacy_coord',
|
||||
coordinatorPaneKey: 'tab_coord:44444444-4444-4444-8444-444444444444'
|
||||
})
|
||||
).toMatchObject({ coordinator_handle: 'term_legacy_coord' })
|
||||
})
|
||||
|
||||
it.each([
|
||||
['generation', { consumerGeneration: 1 }],
|
||||
['principal', { principalId: 'legacy_principal_wrong' }],
|
||||
['handle', { terminalHandle: 'term_wrong' }],
|
||||
['pane', { paneKey: 'tab_wrong:99999999-9999-4999-8999-999999999999' }]
|
||||
] as const)('rejects stale legacy coordinator %s proof inside Run binding', (_label, patch) => {
|
||||
const state = openAdoptedFixture()
|
||||
const before = db!.getRun(state.adoptedRunId)
|
||||
|
||||
expect(() =>
|
||||
db!.bindRun({
|
||||
runId: state.adoptedRunId,
|
||||
coordinatorHandle: 'term_legacy_coord',
|
||||
coordinatorPaneKey: 'tab_coord:44444444-4444-4444-8444-444444444444',
|
||||
legacyCoordinatorAuthority: {
|
||||
runId: state.adoptedRunId,
|
||||
principalId: state.coordinatorPrincipalId,
|
||||
terminalHandle: 'term_legacy_coord',
|
||||
paneKey: 'tab_coord:44444444-4444-4444-8444-444444444444',
|
||||
consumerGeneration: 0,
|
||||
...patch
|
||||
}
|
||||
})
|
||||
).toThrow(/no longer has lifecycle authority/)
|
||||
expect(db!.getRun(state.adoptedRunId)).toEqual(before)
|
||||
})
|
||||
|
||||
it('revokes the legacy coordinator when a current coordinator takes over after settlement', () => {
|
||||
const state = openAdoptedFixture()
|
||||
db!.settleWorkerReport({
|
||||
taskId: state.fixture.legacyTaskId,
|
||||
dispatchId: state.fixture.legacyDispatchId,
|
||||
outcome: 'succeeded',
|
||||
result: 'done'
|
||||
})
|
||||
|
||||
expect(
|
||||
db!.bindRun({
|
||||
runId: state.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:11111111-1111-4111-8111-111111111111'
|
||||
})
|
||||
).toMatchObject({ coordinator_handle: 'term_current_coord' })
|
||||
expect(db!.getLegacyCompatibilityPrincipal(state.coordinatorPrincipalId)?.status).toBe(
|
||||
'revoked'
|
||||
)
|
||||
expect(() =>
|
||||
db!.commitLegacyCompatibilityPrincipal({
|
||||
runId: state.adoptedRunId,
|
||||
role: 'coordinator',
|
||||
hostScope: 'local:runtime_1',
|
||||
terminalHandle: 'term_legacy_coord',
|
||||
paneKey: 'tab_coord:44444444-4444-4444-8444-444444444444',
|
||||
launchTokenHash: 'coord_launch_hash',
|
||||
processIncarnation: 'process_coord'
|
||||
})
|
||||
).toThrow(/revoked/)
|
||||
})
|
||||
|
||||
it('promotes only mail addressed to the replaced legacy coordinator', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const coordinatorMail = db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'coordinator outcome'
|
||||
})
|
||||
const workerMail = db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_other_worker',
|
||||
subject: 'worker-only guidance'
|
||||
})
|
||||
|
||||
db!.bindRun({
|
||||
runId: state.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:11111111-1111-4111-8111-111111111111',
|
||||
takeoverLegacy: true
|
||||
})
|
||||
|
||||
expect(db!.getMessageById(coordinatorMail.id)).toMatchObject({
|
||||
to_handle: `run:${state.adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery'
|
||||
})
|
||||
expect(db!.getMessageById(workerMail.id)).toMatchObject({
|
||||
to_handle: 'term_other_worker',
|
||||
delivery_contract: 'legacy_direct'
|
||||
})
|
||||
})
|
||||
|
||||
it('commits legacy messages, lifecycle effects, and invocation receipts exactly once', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const params = {
|
||||
principalId: state.workerPrincipalId,
|
||||
operationKey: 'invocation_1',
|
||||
method: 'orchestration.send',
|
||||
payloadHash: 'payload_1',
|
||||
message: {
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'alive',
|
||||
type: 'heartbeat' as const
|
||||
},
|
||||
lifecycle: { kind: 'heartbeat' as const, at: '2026-07-28T12:00:00.000Z' }
|
||||
}
|
||||
|
||||
const first = db!.commitLegacyLifecycleOperation(params)
|
||||
const replay = db!.commitLegacyLifecycleOperation(params)
|
||||
|
||||
expect(replay).toMatchObject({
|
||||
duplicate: true,
|
||||
message: { id: first.message.id },
|
||||
receipt: { effect_id: first.message.id }
|
||||
})
|
||||
expect(db!.getDispatchContextById(state.fixture.legacyDispatchId)?.last_heartbeat_at).toBe(
|
||||
'2026-07-28T12:00:00.000Z'
|
||||
)
|
||||
expect(db!.getUnreadMessages('term_legacy_coord')).toEqual([])
|
||||
expect(db!.getUndeliveredUnreadMessages('term_legacy_coord')).toEqual([])
|
||||
expect(() =>
|
||||
db!.commitLegacyLifecycleOperation({ ...params, payloadHash: 'different' })
|
||||
).toThrow(/different input/)
|
||||
})
|
||||
|
||||
it('reconstructs a matching pre-receipt settlement without changing its persisted outcome', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const accepted = db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Completed',
|
||||
type: 'worker_done'
|
||||
})
|
||||
expect(
|
||||
db!.settleWorkerReport({
|
||||
taskId: db!.getDispatchContextById(state.fixture.legacyDispatchId)!.task_id,
|
||||
dispatchId: state.fixture.legacyDispatchId,
|
||||
outcome: 'succeeded',
|
||||
result: 'accepted by A'
|
||||
})
|
||||
).toMatchObject({ action: 'settled', duplicate: false })
|
||||
|
||||
const reconstructed = db!.commitLegacyLifecycleOperation({
|
||||
principalId: state.workerPrincipalId,
|
||||
operationKey: `settlement:${state.fixture.legacyDispatchId}`,
|
||||
method: 'orchestration.send',
|
||||
payloadHash: 'settlement_payload',
|
||||
message: {
|
||||
existingId: accepted.id,
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Completed',
|
||||
type: 'worker_done'
|
||||
},
|
||||
lifecycle: {
|
||||
kind: 'worker_report',
|
||||
taskId: db!.getDispatchContextById(state.fixture.legacyDispatchId)!.task_id,
|
||||
outcome: 'succeeded',
|
||||
result: 'accepted by A'
|
||||
}
|
||||
})
|
||||
|
||||
expect(reconstructed).toMatchObject({
|
||||
duplicate: false,
|
||||
message: { id: accepted.id },
|
||||
settlement: { action: 'settled', outcome: 'succeeded', duplicate: true }
|
||||
})
|
||||
expect(db!.getLegacyCompatibilityPrincipal(state.workerPrincipalId)?.status).toBe('settled')
|
||||
})
|
||||
|
||||
it('reconstructs a read pre-takeover completion through its original legacy route', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const taskId = db!.getDispatchContextById(state.fixture.legacyDispatchId)!.task_id
|
||||
const accepted = db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Completed',
|
||||
body: 'accepted before takeover',
|
||||
type: 'worker_done'
|
||||
})
|
||||
db!.markAsRead([accepted.id])
|
||||
db!.settleWorkerReport({
|
||||
taskId,
|
||||
dispatchId: state.fixture.legacyDispatchId,
|
||||
outcome: 'succeeded',
|
||||
result: 'accepted before takeover'
|
||||
})
|
||||
db!.bindRun({
|
||||
runId: state.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:11111111-1111-4111-8111-111111111111',
|
||||
takeoverLegacy: true
|
||||
})
|
||||
const beforeIds = db!.getInbox(100).map((message) => message.id)
|
||||
|
||||
const reconstructed = db!.commitLegacyLifecycleOperation({
|
||||
principalId: state.workerPrincipalId,
|
||||
operationKey: 'read_completion_after_takeover',
|
||||
method: 'orchestration.send',
|
||||
payloadHash: 'read_completion_after_takeover_payload',
|
||||
message: {
|
||||
existingId: accepted.id,
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Completed',
|
||||
body: 'accepted before takeover',
|
||||
type: 'worker_done'
|
||||
},
|
||||
lifecycle: {
|
||||
kind: 'worker_report',
|
||||
taskId,
|
||||
outcome: 'succeeded',
|
||||
result: 'accepted before takeover'
|
||||
}
|
||||
})
|
||||
|
||||
expect(reconstructed).toMatchObject({
|
||||
duplicate: false,
|
||||
message: {
|
||||
id: accepted.id,
|
||||
to_handle: 'term_legacy_coord',
|
||||
delivery_contract: 'legacy_direct'
|
||||
},
|
||||
settlement: { action: 'settled', outcome: 'succeeded', duplicate: true }
|
||||
})
|
||||
expect(db!.getInbox(100).map((message) => message.id)).toEqual(beforeIds)
|
||||
})
|
||||
|
||||
it('rejects cross-cutover completion reconstruction for another recipient', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const taskId = db!.getDispatchContextById(state.fixture.legacyDispatchId)!.task_id
|
||||
const payload = JSON.stringify({ taskId, dispatchId: state.fixture.legacyDispatchId })
|
||||
const foreign = db!.insertMessage({
|
||||
runId: state.adoptedRunId,
|
||||
deliveryContract: 'legacy_direct',
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_other_coord',
|
||||
subject: 'Completed',
|
||||
body: 'accepted elsewhere',
|
||||
type: 'worker_done',
|
||||
payload
|
||||
})
|
||||
db!.settleWorkerReport({
|
||||
taskId,
|
||||
dispatchId: state.fixture.legacyDispatchId,
|
||||
outcome: 'succeeded',
|
||||
result: 'accepted elsewhere'
|
||||
})
|
||||
const beforeIds = db!.getInbox(100).map((message) => message.id)
|
||||
|
||||
expect(
|
||||
db!.findLegacyWorkerCompletion({
|
||||
principalId: state.workerPrincipalId,
|
||||
taskId,
|
||||
recipientHandle: 'term_legacy_coord',
|
||||
subject: 'Completed',
|
||||
body: 'accepted elsewhere',
|
||||
payload
|
||||
})
|
||||
).toBeUndefined()
|
||||
expect(() =>
|
||||
db!.commitLegacyLifecycleOperation({
|
||||
principalId: state.workerPrincipalId,
|
||||
operationKey: 'wrong_recipient_retry',
|
||||
method: 'orchestration.send',
|
||||
payloadHash: 'wrong_recipient_retry_payload',
|
||||
message: {
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Completed',
|
||||
body: 'accepted elsewhere',
|
||||
type: 'worker_done',
|
||||
payload
|
||||
},
|
||||
lifecycle: {
|
||||
kind: 'worker_report',
|
||||
taskId,
|
||||
outcome: 'succeeded',
|
||||
result: 'accepted elsewhere'
|
||||
}
|
||||
})
|
||||
).toThrow(/settled/)
|
||||
expect(() =>
|
||||
db!.commitLegacyLifecycleOperation({
|
||||
principalId: state.workerPrincipalId,
|
||||
operationKey: 'wrong_recipient_reconstruction',
|
||||
method: 'orchestration.send',
|
||||
payloadHash: 'wrong_recipient_payload',
|
||||
message: {
|
||||
existingId: foreign.id,
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Completed',
|
||||
body: 'accepted elsewhere',
|
||||
type: 'worker_done',
|
||||
payload
|
||||
},
|
||||
lifecycle: {
|
||||
kind: 'worker_report',
|
||||
taskId,
|
||||
outcome: 'succeeded',
|
||||
result: 'accepted elsewhere'
|
||||
}
|
||||
})
|
||||
).toThrow(/does not match this principal/)
|
||||
expect(db!.getInbox(100).map((message) => message.id)).toEqual(beforeIds)
|
||||
expect(
|
||||
db!.getLegacyOperationReceipt(state.workerPrincipalId, 'wrong_recipient_reconstruction')
|
||||
).toBeUndefined()
|
||||
expect(
|
||||
db!.getLegacyOperationReceipt(state.workerPrincipalId, 'wrong_recipient_retry')
|
||||
).toBeUndefined()
|
||||
expect(db!.getTask(taskId)).toMatchObject({
|
||||
status: 'completed',
|
||||
result: 'accepted elsewhere'
|
||||
})
|
||||
})
|
||||
|
||||
it('uses invocation identity for repeated asks and atomically conflicts divergent replies', () => {
|
||||
const state = openAdoptedFixture()
|
||||
const ask = {
|
||||
principalId: state.workerPrincipalId,
|
||||
operationKey: 'ask_invocation_1',
|
||||
method: 'orchestration.ask',
|
||||
payloadHash: 'ask_payload',
|
||||
question: 'Same text?',
|
||||
options: ['yes', 'no'],
|
||||
recipientHandle: 'term_legacy_coord'
|
||||
}
|
||||
const first = db!.commitLegacyAskOperation(ask)
|
||||
const replay = db!.commitLegacyAskOperation(ask)
|
||||
const repeated = db!.commitLegacyAskOperation({
|
||||
...ask,
|
||||
operationKey: 'ask_invocation_2'
|
||||
})
|
||||
|
||||
expect(replay).toMatchObject({ duplicate: true, question: { message_id: first.message.id } })
|
||||
expect(repeated.message.id).not.toBe(first.message.id)
|
||||
expect(db!.findPendingLegacyQuestions(ask)).toHaveLength(2)
|
||||
expect(
|
||||
db!.findPendingLegacyQuestions({
|
||||
...ask,
|
||||
question: ' Same text?\r\n',
|
||||
options: ['yes ', ' no']
|
||||
})
|
||||
).toHaveLength(2)
|
||||
|
||||
const inherited = db!.createQuestion({
|
||||
runId: state.adoptedRunId,
|
||||
dispatchId: state.fixture.legacyDispatchId,
|
||||
askerHandle: 'term_legacy_worker',
|
||||
question: 'Inherited?'
|
||||
})
|
||||
const sqlite = (db as unknown as { db: Database.Database }).db
|
||||
sqlite
|
||||
.prepare(
|
||||
`UPDATE messages
|
||||
SET from_handle = 'term_legacy_worker', to_handle = 'term_legacy_coord',
|
||||
delivery_contract = 'legacy_direct'
|
||||
WHERE id = ?`
|
||||
)
|
||||
.run(inherited.message.id)
|
||||
const inheritedAsk = {
|
||||
...ask,
|
||||
operationKey: 'ask_inherited_1',
|
||||
payloadHash: 'ask_inherited_payload_1',
|
||||
question: 'Inherited?',
|
||||
options: [],
|
||||
existingQuestionId: inherited.message.id
|
||||
}
|
||||
const adopted = db!.commitLegacyAskOperation(inheritedAsk)
|
||||
const distinct = db!.commitLegacyAskOperation({
|
||||
...inheritedAsk,
|
||||
operationKey: 'ask_inherited_2',
|
||||
payloadHash: 'ask_inherited_payload_2'
|
||||
})
|
||||
expect(adopted.message.id).toBe(inherited.message.id)
|
||||
expect(distinct.message.id).not.toBe(inherited.message.id)
|
||||
|
||||
const reply = {
|
||||
principalId: state.coordinatorPrincipalId,
|
||||
operationKey: 'reply_invocation_1',
|
||||
method: 'orchestration.reply',
|
||||
payloadHash: 'reply_payload',
|
||||
questionId: first.message.id,
|
||||
body: 'yes'
|
||||
}
|
||||
const answered = db!.commitLegacyReplyOperation(reply)
|
||||
const answerReplay = db!.commitLegacyReplyOperation(reply)
|
||||
expect(answerReplay).toMatchObject({
|
||||
duplicate: true,
|
||||
message: { id: answered.message.id }
|
||||
})
|
||||
expect(
|
||||
db!
|
||||
.findLegacyQuestionsBySemanticIdentity(ask)
|
||||
.find((row) => row.question.message_id === first.message.id)
|
||||
).toMatchObject({ question: { status: 'answered' }, answerAcknowledged: false })
|
||||
db!.acknowledgeLegacyQuestionAnswer({
|
||||
principalId: state.workerPrincipalId,
|
||||
questionId: first.message.id,
|
||||
answerMessageId: answered.message.id
|
||||
})
|
||||
expect(
|
||||
db!
|
||||
.findLegacyQuestionsBySemanticIdentity(ask)
|
||||
.find((row) => row.question.message_id === first.message.id)
|
||||
).toMatchObject({ answerAcknowledged: true })
|
||||
expect(() =>
|
||||
db!.commitLegacyReplyOperation({
|
||||
...reply,
|
||||
operationKey: 'reply_invocation_2',
|
||||
payloadHash: 'different_reply',
|
||||
body: 'no'
|
||||
})
|
||||
).toThrow(/different answer/)
|
||||
|
||||
const currentTask = db!.createTask({ runId: state.adoptedRunId, spec: 'current retry' })
|
||||
const currentDispatch = db!.createDispatchContext(currentTask.id, 'term_current_retry')
|
||||
const currentQuestion = db!.createQuestion({
|
||||
runId: state.adoptedRunId,
|
||||
dispatchId: currentDispatch.id,
|
||||
askerHandle: 'term_current_retry',
|
||||
question: 'Current question?'
|
||||
})
|
||||
expect(() =>
|
||||
db!.commitLegacyReplyOperation({
|
||||
...reply,
|
||||
operationKey: 'reply_current_question',
|
||||
payloadHash: 'current_question',
|
||||
questionId: currentQuestion.message.id
|
||||
})
|
||||
).toThrow(/not actionable/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,206 @@
|
||||
import { mkdtempSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import { LEGACY_RUN_ID, OrchestrationDb } from './db'
|
||||
|
||||
export type LegacyStorageCutoverFixture = {
|
||||
dbPath: string
|
||||
currentRunId: string
|
||||
unrelatedRunId: string
|
||||
currentDispatchId: string
|
||||
legacyTaskId: string
|
||||
legacyDispatchId: string
|
||||
legacyGateId: string
|
||||
legacyMessageIds: string[]
|
||||
legacyQuestionId: string
|
||||
legacyDeliveryId: string
|
||||
rejectionMessageId: string
|
||||
lookalikeMessageId: string
|
||||
malformedRejectionMessageIds: string[]
|
||||
}
|
||||
|
||||
export function createLegacyStorageCutoverFixture(): {
|
||||
fixture: LegacyStorageCutoverFixture
|
||||
tempDir: string
|
||||
} {
|
||||
const tempDir = mkdtempSync(join(tmpdir(), 'orca-legacy-storage-'))
|
||||
const dbPath = join(tempDir, 'orchestration.db')
|
||||
const first = new OrchestrationDb(dbPath)
|
||||
const currentRun = first.createRun({
|
||||
objective: 'Current work',
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:11111111-1111-4111-8111-111111111111'
|
||||
})
|
||||
const currentTask = first.createTask({ spec: 'current', runId: currentRun.id })
|
||||
const unrelatedRun = first.createRun({
|
||||
objective: 'Unrelated current work',
|
||||
coordinatorHandle: 'term_unrelated_coord',
|
||||
coordinatorPaneKey: 'tab_unrelated:55555555-5555-4555-8555-555555555555'
|
||||
})
|
||||
const currentDispatch = first.createDispatchContext(
|
||||
currentTask.id,
|
||||
'term_current_worker',
|
||||
'tab_current:22222222-2222-4222-9222-222222222222',
|
||||
'current_launch_hash'
|
||||
)
|
||||
first.insertMessage({
|
||||
runId: currentRun.id,
|
||||
from: 'term_current_worker',
|
||||
to: `run:${currentRun.id}`,
|
||||
subject: 'current mail'
|
||||
})
|
||||
|
||||
const legacyTask = first.createTask({
|
||||
spec: 'legacy',
|
||||
createdByTerminalHandle: 'term_legacy_coord'
|
||||
})
|
||||
first.createDispatchContext(
|
||||
legacyTask.id,
|
||||
'term_legacy_worker',
|
||||
'tab_legacy:33333333-3333-4333-8333-333333333333'
|
||||
)
|
||||
const legacyGate = first.createGate({
|
||||
taskId: legacyTask.id,
|
||||
question: 'Retained gate?'
|
||||
})
|
||||
first.resolveGate(legacyGate.id, 'continue')
|
||||
const retryDispatch = first.createDispatchContext(
|
||||
legacyTask.id,
|
||||
'term_legacy_worker',
|
||||
'tab_legacy:33333333-3333-4333-8333-333333333333'
|
||||
)
|
||||
const legacyMessages = [
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_coord',
|
||||
to: 'term_legacy_worker',
|
||||
subject: 'read worker mail'
|
||||
}),
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'read coordinator mail'
|
||||
}),
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_coord',
|
||||
to: 'term_legacy_worker',
|
||||
subject: 'second worker page'
|
||||
})
|
||||
]
|
||||
first.markAsRead(legacyMessages.map((message) => message.id))
|
||||
const question = first.createQuestion({
|
||||
runId: LEGACY_RUN_ID,
|
||||
dispatchId: retryDispatch.id,
|
||||
askerHandle: 'term_legacy_worker',
|
||||
question: 'Retained question?'
|
||||
})
|
||||
const rejection = first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Rejected heartbeat',
|
||||
type: 'heartbeat',
|
||||
payload: JSON.stringify({ _orcaLifecycleRejection: { code: 'migration', reason: 'cutover' } })
|
||||
})
|
||||
const lookalike = first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Ordinary legacy mail',
|
||||
payload: JSON.stringify({
|
||||
userData: { _orcaLifecycleRejection: { code: 'not-a-top-level-audit-marker' } }
|
||||
})
|
||||
})
|
||||
const malformedRejections = [
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Invalid JSON marker',
|
||||
payload: '{"_orcaLifecycleRejection":'
|
||||
}),
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Array marker',
|
||||
payload: JSON.stringify({ _orcaLifecycleRejection: [] })
|
||||
}),
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'String marker',
|
||||
payload: JSON.stringify({ _orcaLifecycleRejection: 'migration' })
|
||||
}),
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Incomplete marker',
|
||||
payload: JSON.stringify({ _orcaLifecycleRejection: { code: 'migration' } })
|
||||
}),
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Non-string marker fields',
|
||||
payload: JSON.stringify({ _orcaLifecycleRejection: { code: 19, reason: false } })
|
||||
}),
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'Array root',
|
||||
payload: JSON.stringify([
|
||||
{ _orcaLifecycleRejection: { code: 'migration', reason: 'nested' } }
|
||||
])
|
||||
}),
|
||||
first.insertMessage({
|
||||
from: 'term_legacy_worker',
|
||||
to: 'term_legacy_coord',
|
||||
subject: 'String root',
|
||||
payload: JSON.stringify('_orcaLifecycleRejection')
|
||||
})
|
||||
]
|
||||
first.close()
|
||||
|
||||
const raw = new Database(dbPath)
|
||||
const legacyDeliveryId = 'delivery_legacy_outstanding'
|
||||
raw
|
||||
.prepare(
|
||||
`INSERT INTO deliveries (
|
||||
id, run_id, consumer_generation, message_ids, status
|
||||
) VALUES (?, ?, 0, ?, 'outstanding')`
|
||||
)
|
||||
.run(legacyDeliveryId, LEGACY_RUN_ID, JSON.stringify([legacyMessages[0].id]))
|
||||
raw
|
||||
.prepare("UPDATE messages SET delivery_contract = 'legacy_direct' WHERE id = ?")
|
||||
.run(rejection.id)
|
||||
const seedAuditOnly = raw.prepare(
|
||||
"UPDATE messages SET delivery_contract = 'audit_only' WHERE id = ?"
|
||||
)
|
||||
for (const message of [lookalike, ...malformedRejections]) {
|
||||
seedAuditOnly.run(message.id)
|
||||
}
|
||||
raw.exec(`
|
||||
DROP INDEX IF EXISTS idx_messages_delivery_contract;
|
||||
DROP TABLE legacy_mail_receipts;
|
||||
DROP TABLE legacy_operation_receipts;
|
||||
DROP TABLE legacy_compatibility_principals;
|
||||
DROP TABLE legacy_adoptions;
|
||||
`)
|
||||
raw.pragma('user_version = 18')
|
||||
raw.close()
|
||||
|
||||
return {
|
||||
tempDir,
|
||||
fixture: {
|
||||
dbPath,
|
||||
currentRunId: currentRun.id,
|
||||
unrelatedRunId: unrelatedRun.id,
|
||||
currentDispatchId: currentDispatch.id,
|
||||
legacyTaskId: legacyTask.id,
|
||||
legacyDispatchId: retryDispatch.id,
|
||||
legacyGateId: legacyGate.id,
|
||||
legacyMessageIds: legacyMessages.map((message) => message.id),
|
||||
legacyQuestionId: question.message.id,
|
||||
legacyDeliveryId,
|
||||
rejectionMessageId: rejection.id,
|
||||
lookalikeMessageId: lookalike.id,
|
||||
malformedRejectionMessageIds: malformedRejections.map((message) => message.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { LegacyWorkerTerminalRecoveryRow } from './types'
|
||||
import { planLegacyWorkerTerminalRecovery } from './orchestration-legacy-worker-terminal-recovery'
|
||||
|
||||
const LEAF_ID = '11111111-1111-4111-8111-111111111111'
|
||||
const INCARNATION_ID = '22222222-2222-4222-8222-222222222222'
|
||||
|
||||
function recoveryRow(
|
||||
overrides: Partial<LegacyWorkerTerminalRecoveryRow> = {}
|
||||
): LegacyWorkerTerminalRecoveryRow {
|
||||
return {
|
||||
dispatch_id: 'dispatch-1',
|
||||
task_id: 'task-1',
|
||||
dispatch_status: 'completed',
|
||||
contract_version: 0,
|
||||
assignee_handle: 'term-worker',
|
||||
assignee_pane_key: `tab-worker:${LEAF_ID}`,
|
||||
process_incarnation: `ssh:ssh-1@@pty-worker:${INCARNATION_ID}`,
|
||||
worker_state: 'ready',
|
||||
worktree_id: 'repo::/workspace',
|
||||
agent_terminal_handle: 'term-worker',
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
describe('legacy worker terminal recovery planning', () => {
|
||||
it('retains completed Dispatches when the worker process row is still live', () => {
|
||||
expect(planLegacyWorkerTerminalRecovery([recoveryRow()])).toEqual({
|
||||
blockedPanes: [
|
||||
{
|
||||
worktreeId: 'repo::/workspace',
|
||||
paneKey: `tab-worker:${LEAF_ID}`,
|
||||
contractVersion: 0
|
||||
}
|
||||
],
|
||||
candidates: [
|
||||
expect.objectContaining({
|
||||
dispatchId: 'dispatch-1',
|
||||
ptyId: 'ssh:ssh-1@@pty-worker',
|
||||
incarnationId: INCARNATION_ID
|
||||
})
|
||||
],
|
||||
ambiguousDispatchIds: []
|
||||
})
|
||||
})
|
||||
|
||||
it('blocks resume but refuses recovery when durable handles disagree', () => {
|
||||
expect(
|
||||
planLegacyWorkerTerminalRecovery([recoveryRow({ agent_terminal_handle: 'term-replacement' })])
|
||||
).toEqual({
|
||||
blockedPanes: [
|
||||
{
|
||||
worktreeId: 'repo::/workspace',
|
||||
paneKey: `tab-worker:${LEAF_ID}`,
|
||||
contractVersion: 0
|
||||
}
|
||||
],
|
||||
candidates: [],
|
||||
ambiguousDispatchIds: []
|
||||
})
|
||||
})
|
||||
|
||||
it('fails closed when two Dispatches claim one terminal identity', () => {
|
||||
const plan = planLegacyWorkerTerminalRecovery([
|
||||
recoveryRow(),
|
||||
recoveryRow({ dispatch_id: 'dispatch-2', task_id: 'task-2' })
|
||||
])
|
||||
|
||||
expect(plan.candidates).toEqual([])
|
||||
expect(plan.ambiguousDispatchIds).toEqual(['dispatch-1', 'dispatch-2'])
|
||||
expect(plan.blockedPanes).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('does not trust malformed pane or process identities', () => {
|
||||
const plan = planLegacyWorkerTerminalRecovery([
|
||||
recoveryRow({
|
||||
assignee_pane_key: 'tab-worker:1',
|
||||
process_incarnation: 'runtime:pty:generation'
|
||||
})
|
||||
])
|
||||
|
||||
expect(plan).toEqual({
|
||||
blockedPanes: [],
|
||||
candidates: [],
|
||||
ambiguousDispatchIds: []
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,119 @@
|
||||
import { isPtyIncarnationId, type PtyIncarnationId } from '../../../shared/pty-incarnation'
|
||||
import { parsePaneKey } from '../../../shared/stable-pane-id'
|
||||
import type { LegacyWorkerTerminalRecoveryRow } from './types'
|
||||
|
||||
export type LegacyWorkerTerminalRecoveryCandidate = {
|
||||
dispatchId: string
|
||||
dispatchStatus: LegacyWorkerTerminalRecoveryRow['dispatch_status']
|
||||
contractVersion: number
|
||||
taskId: string
|
||||
worktreeId: string
|
||||
terminalHandle: string
|
||||
paneKey: string
|
||||
tabId: string
|
||||
leafId: string
|
||||
processIncarnation: string
|
||||
ptyId: string
|
||||
incarnationId: PtyIncarnationId
|
||||
}
|
||||
|
||||
export type LegacyWorkerTerminalRecoveryPlan = {
|
||||
blockedPanes: { worktreeId: string; paneKey: string; contractVersion: number }[]
|
||||
candidates: LegacyWorkerTerminalRecoveryCandidate[]
|
||||
ambiguousDispatchIds: string[]
|
||||
}
|
||||
|
||||
function parseProcessIncarnation(
|
||||
value: string
|
||||
): { ptyId: string; incarnationId: PtyIncarnationId } | null {
|
||||
const separator = value.lastIndexOf(':')
|
||||
if (separator <= 0) {
|
||||
return null
|
||||
}
|
||||
const ptyId = value.slice(0, separator)
|
||||
const incarnationId = value.slice(separator + 1)
|
||||
return ptyId && isPtyIncarnationId(incarnationId) ? { ptyId, incarnationId } : null
|
||||
}
|
||||
|
||||
function countCandidateKeys(
|
||||
candidates: readonly LegacyWorkerTerminalRecoveryCandidate[],
|
||||
select: (candidate: LegacyWorkerTerminalRecoveryCandidate) => string
|
||||
): Map<string, number> {
|
||||
const counts = new Map<string, number>()
|
||||
for (const candidate of candidates) {
|
||||
const key = select(candidate)
|
||||
counts.set(key, (counts.get(key) ?? 0) + 1)
|
||||
}
|
||||
return counts
|
||||
}
|
||||
|
||||
export function planLegacyWorkerTerminalRecovery(
|
||||
rows: readonly LegacyWorkerTerminalRecoveryRow[]
|
||||
): LegacyWorkerTerminalRecoveryPlan {
|
||||
const blockedPanes = new Map<
|
||||
string,
|
||||
{ worktreeId: string; paneKey: string; contractVersion: number }
|
||||
>()
|
||||
const parsedCandidates: LegacyWorkerTerminalRecoveryCandidate[] = []
|
||||
for (const row of rows) {
|
||||
const worktreeId = row.worktree_id?.trim()
|
||||
const paneKey = row.assignee_pane_key?.trim()
|
||||
const pane = paneKey ? parsePaneKey(paneKey) : null
|
||||
if (worktreeId && paneKey && pane) {
|
||||
blockedPanes.set(`${worktreeId}\0${paneKey}`, {
|
||||
worktreeId,
|
||||
paneKey,
|
||||
contractVersion: row.contract_version
|
||||
})
|
||||
}
|
||||
const terminalHandle = row.assignee_handle?.trim()
|
||||
const workerHandle = row.agent_terminal_handle?.trim()
|
||||
const processIncarnation = row.process_incarnation?.trim()
|
||||
const process = processIncarnation ? parseProcessIncarnation(processIncarnation) : null
|
||||
if (
|
||||
!worktreeId ||
|
||||
!paneKey ||
|
||||
!pane ||
|
||||
!terminalHandle ||
|
||||
terminalHandle !== workerHandle ||
|
||||
!processIncarnation ||
|
||||
!process
|
||||
) {
|
||||
continue
|
||||
}
|
||||
parsedCandidates.push({
|
||||
dispatchId: row.dispatch_id,
|
||||
dispatchStatus: row.dispatch_status,
|
||||
contractVersion: row.contract_version,
|
||||
taskId: row.task_id,
|
||||
worktreeId,
|
||||
terminalHandle,
|
||||
paneKey,
|
||||
tabId: pane.tabId,
|
||||
leafId: pane.leafId,
|
||||
processIncarnation,
|
||||
ptyId: process.ptyId,
|
||||
incarnationId: process.incarnationId
|
||||
})
|
||||
}
|
||||
|
||||
const identityCounts = [
|
||||
countCandidateKeys(parsedCandidates, (candidate) => candidate.terminalHandle),
|
||||
countCandidateKeys(parsedCandidates, (candidate) => candidate.paneKey),
|
||||
countCandidateKeys(parsedCandidates, (candidate) => candidate.processIncarnation)
|
||||
]
|
||||
const ambiguousDispatchIds = new Set<string>()
|
||||
const candidates = parsedCandidates.filter((candidate) => {
|
||||
const keys = [candidate.terminalHandle, candidate.paneKey, candidate.processIncarnation]
|
||||
const ambiguous = keys.some((key, index) => (identityCounts[index]?.get(key) ?? 0) !== 1)
|
||||
if (ambiguous) {
|
||||
ambiguousDispatchIds.add(candidate.dispatchId)
|
||||
}
|
||||
return !ambiguous
|
||||
})
|
||||
return {
|
||||
blockedPanes: [...blockedPanes.values()],
|
||||
candidates,
|
||||
ambiguousDispatchIds: [...ambiguousDispatchIds]
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,12 @@ import type Database from '../../sqlite/sync-database'
|
||||
|
||||
const POST_V6_COLUMNS = [
|
||||
['messages', 'run_id'],
|
||||
['messages', 'delivery_contract'],
|
||||
['coordinator_runs', 'scheduler_lost_at'],
|
||||
['tasks', 'run_id'],
|
||||
['dispatch_contexts', 'run_id'],
|
||||
['dispatch_contexts', 'contract_version'],
|
||||
['dispatch_contexts', 'launch_token_hash'],
|
||||
['dispatch_contexts', 'capability_hash'],
|
||||
['dispatch_contexts', 'process_incarnation'],
|
||||
['dispatch_contexts', 'capability_revoked_at'],
|
||||
@@ -14,11 +18,16 @@ const POST_V6_COLUMNS = [
|
||||
['remote_dispatch_attachments', 'to_worker_imported_sequence'],
|
||||
['remote_dispatch_attachments', 'protocol_version'],
|
||||
['federation_relay_items', 'dispatch_id'],
|
||||
['remote_questions', 'message_id']
|
||||
['remote_questions', 'message_id'],
|
||||
['legacy_adoptions', 'source_run_id'],
|
||||
['legacy_compatibility_principals', 'id'],
|
||||
['legacy_operation_receipts', 'principal_id'],
|
||||
['legacy_mail_receipts', 'principal_id']
|
||||
] as const
|
||||
|
||||
const POST_V6_INDEXES = [
|
||||
'idx_messages_run_sequence',
|
||||
'idx_messages_delivery_contract',
|
||||
'idx_tasks_run_status',
|
||||
'idx_dispatch_run_status',
|
||||
'idx_gates_run_status',
|
||||
@@ -46,11 +55,39 @@ function messagesAllowQuestions(db: Database.Database): boolean {
|
||||
return !!row && row.sql.includes("'question'")
|
||||
}
|
||||
|
||||
function hasConsistentLegacyAdoption(db: Database.Database): boolean {
|
||||
const sourceRunId = 'run_legacy_local'
|
||||
const sourceGraph = db
|
||||
.prepare(
|
||||
`SELECT 1
|
||||
WHERE EXISTS(SELECT 1 FROM tasks WHERE run_id = ?)
|
||||
OR EXISTS(SELECT 1 FROM dispatch_contexts WHERE run_id = ?)
|
||||
OR EXISTS(SELECT 1 FROM decision_gates WHERE run_id = ?)
|
||||
OR EXISTS(SELECT 1 FROM messages WHERE run_id = ?)
|
||||
OR EXISTS(SELECT 1 FROM question_threads WHERE run_id = ?)
|
||||
OR EXISTS(SELECT 1 FROM deliveries WHERE run_id = ?)`
|
||||
)
|
||||
.get(sourceRunId, sourceRunId, sourceRunId, sourceRunId, sourceRunId, sourceRunId)
|
||||
const adoption = db
|
||||
.prepare('SELECT adopted_run_id FROM legacy_adoptions WHERE source_run_id = ?')
|
||||
.get(sourceRunId) as { adopted_run_id: string } | undefined
|
||||
if (sourceGraph) {
|
||||
return false
|
||||
}
|
||||
if (adoption) {
|
||||
return Boolean(
|
||||
db.prepare('SELECT 1 FROM runs WHERE id = ? AND legacy = 0').get(adoption.adopted_run_id)
|
||||
)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function hasCompletePostV6Schema(db: Database.Database): boolean {
|
||||
return (
|
||||
POST_V6_COLUMNS.every(([table, column]) => hasOrchestrationColumn(db, table, column)) &&
|
||||
POST_V6_INDEXES.every((index) => hasOrchestrationIndex(db, index)) &&
|
||||
messagesAllowQuestions(db)
|
||||
messagesAllowQuestions(db) &&
|
||||
hasConsistentLegacyAdoption(db)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -59,9 +96,12 @@ export function resolveOrchestrationMigrationStartVersion(
|
||||
storedVersion: number,
|
||||
schemaVersion: number
|
||||
): number {
|
||||
if (storedVersion >= schemaVersion || hasCompletePostV6Schema(db)) {
|
||||
if (storedVersion > schemaVersion) {
|
||||
return storedVersion
|
||||
}
|
||||
if (hasCompletePostV6Schema(db)) {
|
||||
return storedVersion
|
||||
}
|
||||
// Why: version-skewed pre-Run databases can claim the post-v6 range while retaining v6 tables.
|
||||
return Math.min(storedVersion, 6)
|
||||
return Math.min(storedVersion, schemaVersion, 6)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import { LEGACY_RUN_ID, OrchestrationDb } from './db'
|
||||
import { LEGACY_CONTRACT_VERSION, LEGACY_RUN_ID, OrchestrationDb } from './db'
|
||||
import { resolveOrchestrationMigrationStartVersion } from './orchestration-schema-version-skew'
|
||||
|
||||
describe('OrchestrationDb version-skew migration', () => {
|
||||
let db: OrchestrationDb | undefined
|
||||
@@ -11,12 +12,14 @@ describe('OrchestrationDb version-skew migration', () => {
|
||||
|
||||
afterEach(() => {
|
||||
db?.close()
|
||||
db = undefined
|
||||
if (tempDir) {
|
||||
rmSync(tempDir, { recursive: true, force: true })
|
||||
tempDir = undefined
|
||||
}
|
||||
})
|
||||
|
||||
function createLegacySchemaClaimingVersion17(): string {
|
||||
function createLegacySchemaClaimingVersion(claimedVersion = 17): string {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'orca-db-version-skew-'))
|
||||
const dbPath = join(tempDir, 'orchestration.db')
|
||||
const raw = new Database(dbPath)
|
||||
@@ -126,20 +129,28 @@ describe('OrchestrationDb version-skew migration', () => {
|
||||
'gate_legacy', 'task_legacy', 'retained gate'
|
||||
);
|
||||
`)
|
||||
raw.pragma('user_version = 17')
|
||||
raw.pragma(`user_version = ${claimedVersion}`)
|
||||
raw.close()
|
||||
return dbPath
|
||||
}
|
||||
|
||||
it('repairs retained v6 rows when the database already claims v17', () => {
|
||||
const dbPath = createLegacySchemaClaimingVersion17()
|
||||
const dbPath = createLegacySchemaClaimingVersion()
|
||||
db = new OrchestrationDb(dbPath)
|
||||
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id
|
||||
expect(adoptedRunId).toBeTruthy()
|
||||
expect(db.getRun(LEGACY_RUN_ID)).toMatchObject({ legacy: 1 })
|
||||
expect(db.getMessageById('msg_legacy')).toMatchObject({ run_id: LEGACY_RUN_ID })
|
||||
expect(db.getTask('task_legacy')).toMatchObject({ run_id: LEGACY_RUN_ID })
|
||||
expect(db.getDispatchContextById('ctx_legacy')).toMatchObject({ run_id: LEGACY_RUN_ID })
|
||||
expect(db.getGate('gate_legacy')).toMatchObject({ run_id: LEGACY_RUN_ID })
|
||||
expect(db.getMessageById('msg_legacy')).toMatchObject({
|
||||
run_id: adoptedRunId,
|
||||
delivery_contract: 'legacy_direct'
|
||||
})
|
||||
expect(db.getTask('task_legacy')).toMatchObject({ run_id: adoptedRunId })
|
||||
expect(db.getDispatchContextById('ctx_legacy')).toMatchObject({
|
||||
run_id: adoptedRunId,
|
||||
contract_version: LEGACY_CONTRACT_VERSION
|
||||
})
|
||||
expect(db.getGate('gate_legacy')).toMatchObject({ run_id: adoptedRunId })
|
||||
|
||||
const run = db.createRun({
|
||||
objective: 'verify repaired orchestration',
|
||||
@@ -164,8 +175,18 @@ describe('OrchestrationDb version-skew migration', () => {
|
||||
db.close()
|
||||
db = undefined
|
||||
db = new OrchestrationDb(dbPath)
|
||||
expect(db.listTasks({ runId: LEGACY_RUN_ID }).map((row) => row.id)).toEqual(['task_legacy'])
|
||||
expect(db.listTasks({ runId: adoptedRunId }).map((row) => row.id)).toEqual(['task_legacy'])
|
||||
expect(db.getRun(run.id)).toBeDefined()
|
||||
expect(db.getQuestion(question.message.id)).toMatchObject({ status: 'pending' })
|
||||
})
|
||||
|
||||
it('does not repair an incomplete schema written by a future binary', () => {
|
||||
const dbPath = createLegacySchemaClaimingVersion(20)
|
||||
const raw = new Database(dbPath)
|
||||
|
||||
expect(resolveOrchestrationMigrationStartVersion(raw, 20, 19)).toBe(20)
|
||||
expect(raw.pragma('user_version', { simple: true })).toBe(20)
|
||||
|
||||
raw.close()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { OrchestrationDb } from './db'
|
||||
import { CURRENT_CONTRACT_VERSION, OrchestrationDb } from './db'
|
||||
|
||||
describe('OrchestrationDb worker Dispatch state', () => {
|
||||
let db: OrchestrationDb | undefined
|
||||
@@ -44,6 +44,51 @@ describe('OrchestrationDb worker Dispatch state', () => {
|
||||
status: 'dispatched',
|
||||
assignee_handle: 'term_worker'
|
||||
})
|
||||
expect(d.listLegacyWorkerTerminalRecoveryRows()).toEqual([
|
||||
expect.objectContaining({
|
||||
dispatch_id: started.dispatch.id,
|
||||
contract_version: CURRENT_CONTRACT_VERSION,
|
||||
worker_state: 'ready',
|
||||
agent_terminal_handle: 'term_worker'
|
||||
})
|
||||
])
|
||||
})
|
||||
|
||||
it('requeues an active Task before settling a worker whose terminal is missing', () => {
|
||||
const d = createDb()
|
||||
const task = d.createTask({ spec: 'recover missing worker' })
|
||||
const started = d.createStartingWorkerDispatch({
|
||||
taskId: task.id,
|
||||
startOptions: { topology: 'current', agent: 'codex' }
|
||||
})
|
||||
d.prepareStartingWorkerAuthority({
|
||||
dispatchId: started.dispatch.id,
|
||||
handle: 'term_missing',
|
||||
paneKey: 'tab_missing:11111111-1111-4111-8111-111111111111',
|
||||
processIncarnation: 'pty-missing:22222222-2222-4222-8222-222222222222',
|
||||
worktreeId: 'repo::worktree',
|
||||
setupState: 'not_applicable',
|
||||
effects: []
|
||||
})
|
||||
d.markWorkerDispatchReady(started.dispatch.id)
|
||||
|
||||
expect(
|
||||
d.reconcileMissingWorkerTerminal(started.dispatch.id, 'worker terminal is no longer live')
|
||||
).toMatchObject({
|
||||
state: 'abandoned',
|
||||
stage: 'terminal_missing',
|
||||
last_error: 'worker terminal is no longer live'
|
||||
})
|
||||
expect(d.getDispatchContextById(started.dispatch.id)).toMatchObject({
|
||||
status: 'failed',
|
||||
failure_count: 1,
|
||||
last_failure: 'worker terminal is no longer live'
|
||||
})
|
||||
expect(d.getTask(task.id)?.status).toBe('ready')
|
||||
|
||||
d.reconcileMissingWorkerTerminal(started.dispatch.id, 'duplicate recovery')
|
||||
expect(d.getDispatchContextById(started.dispatch.id)?.failure_count).toBe(1)
|
||||
expect(d.getTask(task.id)?.status).toBe('ready')
|
||||
})
|
||||
|
||||
it('commits worker-start mutation acceptance with the starting Dispatch', () => {
|
||||
|
||||
@@ -14,6 +14,8 @@ export type MessageType = (typeof MESSAGE_TYPES)[number]
|
||||
|
||||
export type MessagePriority = 'normal' | 'high' | 'urgent'
|
||||
|
||||
export type MessageDeliveryContract = 'legacy_direct' | 'current_delivery' | 'audit_only'
|
||||
|
||||
export type TaskStatus = 'pending' | 'ready' | 'dispatched' | 'completed' | 'failed' | 'blocked'
|
||||
|
||||
export type DispatchStatus = 'pending' | 'dispatched' | 'completed' | 'failed' | 'circuit_broken'
|
||||
@@ -61,6 +63,46 @@ export type DeliveryRow = {
|
||||
acknowledged_at: string | null
|
||||
}
|
||||
|
||||
export type LegacyAdoptionRow = {
|
||||
source_run_id: string
|
||||
adopted_run_id: string
|
||||
scheduler_state_lost: number
|
||||
adopted_at: string
|
||||
}
|
||||
|
||||
export type LegacyPrincipalRole = 'worker' | 'coordinator'
|
||||
|
||||
export type LegacyPrincipalStatus = 'committed' | 'settled' | 'revoked'
|
||||
|
||||
export type LegacyCompatibilityPrincipalRow = {
|
||||
id: string
|
||||
run_id: string
|
||||
dispatch_id: string | null
|
||||
role: LegacyPrincipalRole
|
||||
host_scope: string
|
||||
terminal_handle: string
|
||||
pane_key: string
|
||||
launch_token_hash: string
|
||||
process_incarnation: string | null
|
||||
status: LegacyPrincipalStatus
|
||||
}
|
||||
|
||||
export type LegacyOperationReceiptRow = {
|
||||
principal_id: string
|
||||
operation_key: string
|
||||
method: string
|
||||
payload_hash: string
|
||||
effect_id: string
|
||||
response_json: string
|
||||
completed_at: string
|
||||
}
|
||||
|
||||
export type LegacyMailReceiptRow = {
|
||||
principal_id: string
|
||||
message_id: string
|
||||
acknowledged_at: string | null
|
||||
}
|
||||
|
||||
export type QuestionStatus = 'pending' | 'answered' | 'closed'
|
||||
|
||||
export type QuestionRow = {
|
||||
@@ -117,6 +159,19 @@ export type WorkerDispatchRow = {
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export type LegacyWorkerTerminalRecoveryRow = {
|
||||
dispatch_id: string
|
||||
task_id: string
|
||||
dispatch_status: DispatchStatus
|
||||
contract_version: number
|
||||
assignee_handle: string | null
|
||||
assignee_pane_key: string | null
|
||||
process_incarnation: string | null
|
||||
worker_state: WorkerDispatchState
|
||||
worktree_id: string | null
|
||||
agent_terminal_handle: string | null
|
||||
}
|
||||
|
||||
export type FederatedDispatchRow = {
|
||||
dispatch_id: string
|
||||
environment_id: string
|
||||
@@ -170,6 +225,7 @@ export type FederationRelayItemRow = {
|
||||
export type MessageRow = {
|
||||
id: string
|
||||
run_id: string
|
||||
delivery_contract?: MessageDeliveryContract
|
||||
from_handle: string
|
||||
to_handle: string
|
||||
subject: string
|
||||
@@ -204,6 +260,8 @@ export type DispatchContextRow = {
|
||||
id: string
|
||||
run_id: string
|
||||
task_id: string
|
||||
contract_version: number
|
||||
launch_token_hash: string | null
|
||||
assignee_handle: string | null
|
||||
assignee_pane_key: string | null
|
||||
capability_hash: string | null
|
||||
@@ -238,4 +296,5 @@ export type CoordinatorRun = {
|
||||
poll_interval_ms: number
|
||||
created_at: string
|
||||
completed_at: string | null
|
||||
scheduler_lost_at: string | null
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Why: single boundary between raw RPC frames and OrcaRuntimeService; keeps schema, handler, and result type on one object.
|
||||
import { ZodError, type ZodType } from 'zod'
|
||||
import type { TerminalStreamFrame } from '../../../shared/terminal-stream-protocol'
|
||||
import type { OrcaRuntimeService } from '../orca-runtime'
|
||||
import type { OrcaRuntimeService, OrchestrationCompatibilityCallerAuthority } from '../orca-runtime'
|
||||
import type {
|
||||
DeviceCredentialInstalled,
|
||||
PairingGetEndpointsParams,
|
||||
@@ -9,6 +9,7 @@ import type {
|
||||
PairingProvisionRelayParams
|
||||
} from '../../../shared/mobile-relay-credential-contract'
|
||||
import type { RuntimeCapability } from '../../../shared/protocol-version'
|
||||
import type { OrchestrationCompatibilityEvidence } from '../../../shared/orchestration-compatibility-evidence'
|
||||
|
||||
export type PairingRpcContext = {
|
||||
getEndpoints(params: PairingGetEndpointsParams): Promise<PairingGetEndpointsResult>
|
||||
@@ -48,8 +49,18 @@ export type RpcRequest = {
|
||||
orchestrationCapability?: string
|
||||
orchestrationContractVersion?: number
|
||||
orchestrationRequestId?: string
|
||||
compatibilityInvocationId?: string
|
||||
orchestrationCompatibilityEvidence?: OrchestrationCompatibilityEvidence
|
||||
}
|
||||
|
||||
export type LegacyCoordinatorAuthorityProof = Readonly<{
|
||||
runId: string
|
||||
principalId: string | null
|
||||
terminalHandle: string
|
||||
paneKey: string
|
||||
consumerGeneration: number
|
||||
}>
|
||||
|
||||
export type RpcContext = {
|
||||
runtime: OrcaRuntimeService
|
||||
// Why: lets long-poll handlers release immediately on client disconnect instead of running down timeoutMs. See design doc §3.1.
|
||||
@@ -77,6 +88,11 @@ export type RpcContext = {
|
||||
method: string
|
||||
payloadHash: string
|
||||
}
|
||||
// Why: only the compatibility authority router can set this trusted scope; user params cannot bypass Run consumer binding.
|
||||
legacyCoordinatorRunId?: string
|
||||
legacyCoordinatorAuthority?: LegacyCoordinatorAuthorityProof
|
||||
revalidateLegacyCoordinator?: () => string
|
||||
orchestrationCompatibilityCallerAuthority?: OrchestrationCompatibilityCallerAuthority
|
||||
// Why: federation pins the authenticated saved-environment caller without exposing its token to handlers or storage.
|
||||
authenticatedCallerFingerprint?: string
|
||||
pairing?: PairingRpcContext
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { InvalidArgumentError, ZodError, formatZodError } from './core'
|
||||
import type { RpcEnvelopeMeta, RpcRequest, RpcResponse } from './core'
|
||||
import {
|
||||
computerErrorData,
|
||||
errorResponse,
|
||||
mapBrowserError,
|
||||
mapEmulatorError,
|
||||
mapRuntimeError
|
||||
} from './errors'
|
||||
|
||||
export function invalidArgumentResponse(
|
||||
request: RpcRequest,
|
||||
meta: RpcEnvelopeMeta,
|
||||
message: string
|
||||
): RpcResponse {
|
||||
return errorResponse(
|
||||
request.id,
|
||||
meta,
|
||||
'invalid_argument',
|
||||
message,
|
||||
request.method.startsWith('computer.') ? computerErrorData('invalid_argument') : undefined
|
||||
)
|
||||
}
|
||||
|
||||
export function mapDispatcherError(
|
||||
request: RpcRequest,
|
||||
meta: RpcEnvelopeMeta,
|
||||
error: unknown
|
||||
): RpcResponse {
|
||||
if (error instanceof ZodError) {
|
||||
return invalidArgumentResponse(request, meta, formatZodError(error))
|
||||
}
|
||||
if (error instanceof InvalidArgumentError) {
|
||||
return invalidArgumentResponse(request, meta, error.message)
|
||||
}
|
||||
if (request.method.startsWith('browser.')) {
|
||||
return mapBrowserError(request.id, meta, error)
|
||||
}
|
||||
if (request.method.startsWith('emulator.')) {
|
||||
return mapEmulatorError(request.id, meta, error)
|
||||
}
|
||||
return mapRuntimeError(request.id, meta, error)
|
||||
}
|
||||
@@ -46,6 +46,10 @@ function makeRuntime(ui: PersistedUIState = getDefaultUIState()): OrcaRuntimeSer
|
||||
updateUIState: vi.fn((updates: Partial<PersistedUIState>) => {
|
||||
currentUI = { ...currentUI, ...updates }
|
||||
return currentUI
|
||||
}),
|
||||
getOrchestrationDb: () => ({
|
||||
getLegacyAdoption: () => undefined,
|
||||
resolveLegacyWorkerCandidate: () => undefined
|
||||
})
|
||||
} as unknown as OrcaRuntimeService
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { RuntimeCapability } from '../../../shared/protocol-version'
|
||||
import type { TerminalStreamFrame } from '../../../shared/terminal-stream-protocol'
|
||||
import type { PairingRpcContext } from './core'
|
||||
|
||||
export type RpcDispatchStreamingOptions = {
|
||||
connectionId?: string
|
||||
signal?: AbortSignal
|
||||
clientId?: string
|
||||
pairedDeviceId?: string
|
||||
clientKind?: 'mobile' | 'runtime'
|
||||
clientCapabilities?: readonly RuntimeCapability[]
|
||||
pairing?: PairingRpcContext
|
||||
sendBinary?: (bytes: Uint8Array<ArrayBufferLike>) => boolean | void
|
||||
registerBinaryStreamHandler?: (
|
||||
streamId: number,
|
||||
handler: (frame: TerminalStreamFrame) => void
|
||||
) => () => void
|
||||
}
|
||||
+100
-118
@@ -1,57 +1,44 @@
|
||||
// Why: the dispatcher is the one place that knows how to turn a validated
|
||||
// RPC request into a response envelope. Splitting it from the transport
|
||||
// makes it unit-testable without spinning up a socket, and keeps
|
||||
// runtime-rpc.ts focused on framing/auth/connection bookkeeping.
|
||||
import {
|
||||
ZodError,
|
||||
InvalidArgumentError,
|
||||
buildRegistry,
|
||||
formatZodError,
|
||||
isStreamingMethod,
|
||||
type RpcAnyMethod,
|
||||
type RpcEnvelopeMeta,
|
||||
type PairingRpcContext,
|
||||
type RpcRegistry,
|
||||
type RpcRequest,
|
||||
type RpcResponse
|
||||
} from './core'
|
||||
|
||||
import type { TerminalStreamFrame } from '../../../shared/terminal-stream-protocol'
|
||||
import type { FeatureInteractionId } from '../../../shared/feature-interactions'
|
||||
import {
|
||||
computerErrorData,
|
||||
errorResponse,
|
||||
mapBrowserError,
|
||||
mapEmulatorError,
|
||||
mapRuntimeError,
|
||||
successResponse
|
||||
} from './errors'
|
||||
import { errorResponse, successResponse } from './errors'
|
||||
import { ALL_RPC_METHODS } from './methods'
|
||||
import { emulatorProbe, emulatorProbeError } from '../../emulator/emulator-probe'
|
||||
import type { OrcaRuntimeService } from '../orca-runtime'
|
||||
import type { RuntimeCapability } from '../../../shared/protocol-version'
|
||||
import {
|
||||
OrchestrationMutationExecutor,
|
||||
authenticatedCallerFingerprint,
|
||||
getOrchestrationMutationExecutor,
|
||||
type OrchestrationMutationExecutor,
|
||||
type DurableMutationInvocation
|
||||
} from './orchestration-mutation-executor'
|
||||
import { orchestrationMigrationFence } from './orchestration-contract-fence'
|
||||
import { getRuntimeFeatureInteractionId } from './runtime-feature-interaction'
|
||||
import { recordRuntimeFeatureInteraction } from './runtime-feature-interaction'
|
||||
import { OrchestrationLegacyCompatibility } from './orchestration-legacy-compatibility'
|
||||
import type { RpcDispatchStreamingOptions } from './dispatcher-stream-options'
|
||||
import { invalidArgumentResponse, mapDispatcherError } from './dispatcher-error-response'
|
||||
|
||||
export type DispatcherOptions = {
|
||||
runtime: OrcaRuntimeService
|
||||
methods?: readonly RpcAnyMethod[]
|
||||
}
|
||||
export type DispatcherOptions = { runtime: OrcaRuntimeService; methods?: readonly RpcAnyMethod[] }
|
||||
|
||||
export class RpcDispatcher {
|
||||
private readonly runtime: OrcaRuntimeService
|
||||
private readonly registry: RpcRegistry
|
||||
private readonly orchestrationMutations: OrchestrationMutationExecutor
|
||||
private readonly legacyOrchestration: OrchestrationLegacyCompatibility
|
||||
|
||||
constructor({ runtime, methods = ALL_RPC_METHODS }: DispatcherOptions) {
|
||||
this.runtime = runtime
|
||||
this.registry = buildRegistry(methods)
|
||||
this.orchestrationMutations = new OrchestrationMutationExecutor(runtime)
|
||||
this.orchestrationMutations = getOrchestrationMutationExecutor(runtime)
|
||||
this.legacyOrchestration = new OrchestrationLegacyCompatibility(runtime)
|
||||
}
|
||||
|
||||
async dispatch(request: RpcRequest, options?: { signal?: AbortSignal }): Promise<RpcResponse> {
|
||||
@@ -76,9 +63,6 @@ export class RpcDispatcher {
|
||||
return parsedParams.error
|
||||
}
|
||||
|
||||
// Why: streaming methods are not supported over one-shot transports like
|
||||
// Unix sockets. They require a reply function that can be called multiple
|
||||
// times, which is only available via dispatchStreaming.
|
||||
if (isStreamingMethod(method)) {
|
||||
return errorResponse(
|
||||
request.id,
|
||||
@@ -88,29 +72,60 @@ export class RpcDispatcher {
|
||||
)
|
||||
}
|
||||
|
||||
const isEmulator = request.method.startsWith('emulator.')
|
||||
if (isEmulator) {
|
||||
if (request.method.startsWith('emulator.')) {
|
||||
emulatorProbe(`rpc ${request.method}`, request.params)
|
||||
}
|
||||
try {
|
||||
const invoke = (mutation?: DurableMutationInvocation) =>
|
||||
method.handler(parsedParams.value, {
|
||||
const compatibility = await this.legacyOrchestration.tryHandle(
|
||||
request,
|
||||
parsedParams.value,
|
||||
options?.signal
|
||||
)
|
||||
if (compatibility.handled) {
|
||||
return successResponse(request.id, meta, compatibility.result)
|
||||
}
|
||||
const effectiveParams = compatibility.params ?? parsedParams.value
|
||||
const legacyCoordinator = this.legacyOrchestration.createCoordinatorInvocation(
|
||||
request,
|
||||
compatibility.legacyCoordinatorAuthority
|
||||
)
|
||||
const invoke = (mutation?: DurableMutationInvocation) => {
|
||||
const legacyCoordinatorRunId = legacyCoordinator?.revalidate()
|
||||
return method.handler(effectiveParams, {
|
||||
runtime: this.runtime,
|
||||
signal: options?.signal,
|
||||
requestId: request.id,
|
||||
orchestrationCapability: request.orchestrationCapability,
|
||||
authenticatedCallerFingerprint: authenticatedCallerFingerprint(request),
|
||||
authenticatedCallerFingerprint:
|
||||
mutation?.identity.callerFingerprint ?? authenticatedCallerFingerprint(request),
|
||||
recordMutationReceipt: mutation?.recordReceipt,
|
||||
orchestrationMutation: mutation?.identity
|
||||
orchestrationMutation: mutation?.identity,
|
||||
legacyCoordinatorRunId,
|
||||
legacyCoordinatorAuthority: legacyCoordinator?.authority,
|
||||
revalidateLegacyCoordinator: legacyCoordinator?.revalidate,
|
||||
orchestrationCompatibilityCallerAuthority:
|
||||
compatibility.orchestrationCompatibilityCallerAuthority
|
||||
})
|
||||
const result = await this.orchestrationMutations.run(request, parsedParams.value, invoke)
|
||||
this.recordRuntimeFeatureInteraction(request.method, result, undefined, request.params)
|
||||
}
|
||||
const result = await this.orchestrationMutations.run(
|
||||
request,
|
||||
effectiveParams,
|
||||
invoke,
|
||||
legacyCoordinator?.mutationCallerFingerprint
|
||||
)
|
||||
recordRuntimeFeatureInteraction(
|
||||
this.runtime,
|
||||
request.method,
|
||||
result,
|
||||
undefined,
|
||||
request.params
|
||||
)
|
||||
return successResponse(request.id, meta, result)
|
||||
} catch (error) {
|
||||
if (isEmulator) {
|
||||
if (request.method.startsWith('emulator.')) {
|
||||
emulatorProbeError(`rpc ${request.method}`, error, { params: request.params })
|
||||
}
|
||||
return this.mapError(request, meta, error)
|
||||
return mapDispatcherError(request, meta, error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,20 +135,7 @@ export class RpcDispatcher {
|
||||
async dispatchStreaming(
|
||||
request: RpcRequest,
|
||||
reply: (response: string) => void,
|
||||
options?: {
|
||||
connectionId?: string
|
||||
signal?: AbortSignal
|
||||
clientId?: string
|
||||
pairedDeviceId?: string
|
||||
clientKind?: 'mobile' | 'runtime'
|
||||
clientCapabilities?: readonly RuntimeCapability[]
|
||||
pairing?: PairingRpcContext
|
||||
sendBinary?: (bytes: Uint8Array<ArrayBufferLike>) => boolean | void
|
||||
registerBinaryStreamHandler?: (
|
||||
streamId: number,
|
||||
handler: (frame: TerminalStreamFrame) => void
|
||||
) => () => void
|
||||
}
|
||||
options?: RpcDispatchStreamingOptions
|
||||
): Promise<void> {
|
||||
const meta = this.meta()
|
||||
const method = this.registry.get(request.method)
|
||||
@@ -160,8 +162,23 @@ export class RpcDispatcher {
|
||||
|
||||
if (!isStreamingMethod(method)) {
|
||||
try {
|
||||
const invoke = (mutation?: DurableMutationInvocation) =>
|
||||
method.handler(parsedParams.value, {
|
||||
const compatibility = await this.legacyOrchestration.tryHandle(
|
||||
request,
|
||||
parsedParams.value,
|
||||
options?.signal
|
||||
)
|
||||
if (compatibility.handled) {
|
||||
reply(JSON.stringify(successResponse(request.id, meta, compatibility.result)))
|
||||
return
|
||||
}
|
||||
const effectiveParams = compatibility.params ?? parsedParams.value
|
||||
const legacyCoordinator = this.legacyOrchestration.createCoordinatorInvocation(
|
||||
request,
|
||||
compatibility.legacyCoordinatorAuthority
|
||||
)
|
||||
const invoke = (mutation?: DurableMutationInvocation) => {
|
||||
const legacyCoordinatorRunId = legacyCoordinator?.revalidate()
|
||||
return method.handler(effectiveParams, {
|
||||
runtime: this.runtime,
|
||||
signal: options?.signal,
|
||||
requestId: request.id,
|
||||
@@ -171,25 +188,44 @@ export class RpcDispatcher {
|
||||
clientKind: options?.clientKind,
|
||||
clientCapabilities: options?.clientCapabilities,
|
||||
orchestrationCapability: request.orchestrationCapability,
|
||||
authenticatedCallerFingerprint: authenticatedCallerFingerprint(request),
|
||||
authenticatedCallerFingerprint:
|
||||
mutation?.identity.callerFingerprint ?? authenticatedCallerFingerprint(request),
|
||||
recordMutationReceipt: mutation?.recordReceipt,
|
||||
orchestrationMutation: mutation?.identity,
|
||||
pairing: options?.pairing,
|
||||
sendBinary: options?.sendBinary,
|
||||
registerBinaryStreamHandler: options?.registerBinaryStreamHandler
|
||||
registerBinaryStreamHandler: options?.registerBinaryStreamHandler,
|
||||
legacyCoordinatorRunId,
|
||||
legacyCoordinatorAuthority: legacyCoordinator?.authority,
|
||||
revalidateLegacyCoordinator: legacyCoordinator?.revalidate,
|
||||
orchestrationCompatibilityCallerAuthority:
|
||||
compatibility.orchestrationCompatibilityCallerAuthority
|
||||
})
|
||||
const result = await this.orchestrationMutations.run(request, parsedParams.value, invoke)
|
||||
this.recordRuntimeFeatureInteraction(request.method, result, undefined, request.params)
|
||||
}
|
||||
const result = await this.orchestrationMutations.run(
|
||||
request,
|
||||
effectiveParams,
|
||||
invoke,
|
||||
legacyCoordinator?.mutationCallerFingerprint
|
||||
)
|
||||
recordRuntimeFeatureInteraction(
|
||||
this.runtime,
|
||||
request.method,
|
||||
result,
|
||||
undefined,
|
||||
request.params
|
||||
)
|
||||
reply(JSON.stringify(successResponse(request.id, meta, result)))
|
||||
} catch (error) {
|
||||
reply(JSON.stringify(this.mapError(request, meta, error)))
|
||||
reply(JSON.stringify(mapDispatcherError(request, meta, error)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const recordedStreamingFeatureInteractions = new Set<FeatureInteractionId>()
|
||||
const emit = (result: unknown): void => {
|
||||
this.recordRuntimeFeatureInteraction(
|
||||
recordRuntimeFeatureInteraction(
|
||||
this.runtime,
|
||||
request.method,
|
||||
result,
|
||||
recordedStreamingFeatureInteractions,
|
||||
@@ -212,20 +248,22 @@ export class RpcDispatcher {
|
||||
pairedDeviceId: options?.pairedDeviceId,
|
||||
clientKind: options?.clientKind,
|
||||
clientCapabilities: options?.clientCapabilities,
|
||||
orchestrationCapability: request.orchestrationCapability,
|
||||
pairing: options?.pairing,
|
||||
sendBinary: options?.sendBinary,
|
||||
registerBinaryStreamHandler: options?.registerBinaryStreamHandler
|
||||
},
|
||||
emit
|
||||
)
|
||||
this.recordRuntimeFeatureInteraction(
|
||||
recordRuntimeFeatureInteraction(
|
||||
this.runtime,
|
||||
request.method,
|
||||
result,
|
||||
recordedStreamingFeatureInteractions,
|
||||
request.params
|
||||
)
|
||||
} catch (error) {
|
||||
reply(JSON.stringify(this.mapError(request, meta, error)))
|
||||
reply(JSON.stringify(mapDispatcherError(request, meta, error)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,69 +279,13 @@ export class RpcDispatcher {
|
||||
const result = method.params.safeParse(rawParams)
|
||||
if (!result.success) {
|
||||
return {
|
||||
error: this.invalidArgumentResponse(request, meta, formatZodError(result.error))
|
||||
error: invalidArgumentResponse(request, meta, formatZodError(result.error))
|
||||
}
|
||||
}
|
||||
return { value: result.data }
|
||||
}
|
||||
|
||||
private mapError(request: RpcRequest, meta: RpcEnvelopeMeta, error: unknown): RpcResponse {
|
||||
if (error instanceof ZodError) {
|
||||
return this.invalidArgumentResponse(request, meta, formatZodError(error))
|
||||
}
|
||||
if (error instanceof InvalidArgumentError) {
|
||||
return this.invalidArgumentResponse(request, meta, error.message)
|
||||
}
|
||||
|
||||
// Why: browser methods throw BrowserError with a structured `code`;
|
||||
// every other runtime error has a plain-message code. Routing by method
|
||||
// prefix keeps the mapping a single decision rather than a per-method
|
||||
// flag callers must remember to set.
|
||||
if (request.method.startsWith('browser.')) {
|
||||
return mapBrowserError(request.id, meta, error)
|
||||
}
|
||||
if (request.method.startsWith('emulator.')) {
|
||||
return mapEmulatorError(request.id, meta, error)
|
||||
}
|
||||
return mapRuntimeError(request.id, meta, error)
|
||||
}
|
||||
|
||||
private invalidArgumentResponse(
|
||||
request: RpcRequest,
|
||||
meta: RpcEnvelopeMeta,
|
||||
message: string
|
||||
): RpcResponse {
|
||||
return errorResponse(
|
||||
request.id,
|
||||
meta,
|
||||
'invalid_argument',
|
||||
message,
|
||||
request.method.startsWith('computer.') ? computerErrorData('invalid_argument') : undefined
|
||||
)
|
||||
}
|
||||
|
||||
private meta(): RpcEnvelopeMeta {
|
||||
return { runtimeId: this.runtime.getRuntimeId() }
|
||||
}
|
||||
|
||||
private recordRuntimeFeatureInteraction(
|
||||
method: string,
|
||||
result: unknown,
|
||||
alreadyRecorded?: Set<FeatureInteractionId>,
|
||||
rawParams?: unknown
|
||||
): void {
|
||||
const id = getRuntimeFeatureInteractionId(method, result, rawParams)
|
||||
if (!id) {
|
||||
return
|
||||
}
|
||||
if (alreadyRecorded?.has(id)) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
this.runtime.recordFeatureInteraction(id)
|
||||
alreadyRecorded?.add(id)
|
||||
} catch {
|
||||
// Best-effort education state must not break runtime tools.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,7 @@ const STRUCTURED_RUNTIME_PASSTHROUGH_CODES: ReadonlySet<string> = new Set([
|
||||
'agent_unconfigured',
|
||||
'terminal_worktree_mismatch',
|
||||
'request_mismatch',
|
||||
'legacy_read_only',
|
||||
'orchestration_migration_required',
|
||||
'operation_unknown',
|
||||
'question_not_found',
|
||||
|
||||
@@ -265,7 +265,7 @@ describe('orchestration federation control mail', () => {
|
||||
})
|
||||
await expect(waiting).resolves.toMatchObject({
|
||||
ok: true,
|
||||
result: { count: 0, timedOut: true }
|
||||
result: { count: 0 }
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -224,6 +224,7 @@ describe('orchestration federation', () => {
|
||||
])
|
||||
)
|
||||
expect(workerDb.listTasks()).toHaveLength(0)
|
||||
expect(vi.mocked(workerRuntime.createManagedWorktree).mock.calls[0]?.[0].activate).toBe(false)
|
||||
expect(workerRuntime.sendTerminalAgentPrompt).toHaveBeenCalledWith(
|
||||
'term_windows_worker',
|
||||
expect.stringContaining(`Your task ID is: ${task.id}`)
|
||||
|
||||
@@ -103,8 +103,12 @@ export const ORCHESTRATION_GATE_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.gateCreate',
|
||||
params: GateCreateParams,
|
||||
handler: (params, { runtime }) => {
|
||||
handler: (params, { runtime, legacyCoordinatorRunId }) => {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const task = db.getTask(params.task)
|
||||
if (legacyCoordinatorRunId && task?.run_id !== legacyCoordinatorRunId) {
|
||||
throw new Error(`Task not found: ${params.task}`)
|
||||
}
|
||||
let options: string[] | undefined
|
||||
if (params.options) {
|
||||
try {
|
||||
@@ -129,8 +133,12 @@ export const ORCHESTRATION_GATE_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.gateResolve',
|
||||
params: GateResolveParams,
|
||||
handler: (params, { runtime }) => {
|
||||
handler: (params, { runtime, legacyCoordinatorRunId }) => {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const existing = db.getGate(params.id)
|
||||
if (legacyCoordinatorRunId && existing?.run_id !== legacyCoordinatorRunId) {
|
||||
throw new Error(`Gate not found: ${params.id}`)
|
||||
}
|
||||
const gate = db.resolveGate(params.id, params.resolution)
|
||||
if (!gate) {
|
||||
throw new Error(`Gate not found: ${params.id}`)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { z } from 'zod'
|
||||
import { defineMethod, type RpcMethod } from '../core'
|
||||
import { OptionalString, requiredString } from '../schemas'
|
||||
import { OptionalBoolean, OptionalString, requiredString } from '../schemas'
|
||||
import type { OrcaRuntimeService } from '../../orca-runtime'
|
||||
import { OrchestrationError } from '../../orchestration/orchestration-error'
|
||||
|
||||
@@ -11,7 +11,8 @@ const RunCreateParams = z.object({
|
||||
|
||||
const RunUseParams = z.object({
|
||||
id: requiredString('Missing --id'),
|
||||
from: requiredString('Missing coordinator terminal')
|
||||
from: requiredString('Missing coordinator terminal'),
|
||||
takeoverLegacy: OptionalBoolean
|
||||
})
|
||||
|
||||
const RunCurrentParams = z.object({ from: requiredString('Missing coordinator terminal') })
|
||||
@@ -51,14 +52,33 @@ export const ORCHESTRATION_RUN_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.runUse',
|
||||
params: RunUseParams,
|
||||
handler: (params, { runtime }) => {
|
||||
handler: (
|
||||
params,
|
||||
{
|
||||
runtime,
|
||||
legacyCoordinatorAuthority,
|
||||
orchestrationCompatibilityCallerAuthority: callerAuthority
|
||||
}
|
||||
) => {
|
||||
const paneKey = requireCallerPane(runtime, params.from)
|
||||
if (
|
||||
params.takeoverLegacy &&
|
||||
(callerAuthority?.terminalHandle !== params.from || callerAuthority.paneKey !== paneKey)
|
||||
) {
|
||||
throw new OrchestrationError(
|
||||
'legacy_read_only',
|
||||
'Legacy takeover must be invoked by the live coordinator agent terminal it will bind. No effects were applied.',
|
||||
{ effectsApplied: false }
|
||||
)
|
||||
}
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const priorRun = db.getCurrentRunForPane(paneKey)
|
||||
const run = db.bindRun({
|
||||
runId: params.id,
|
||||
coordinatorHandle: params.from,
|
||||
coordinatorPaneKey: paneKey
|
||||
coordinatorPaneKey: paneKey,
|
||||
takeoverLegacy: params.takeoverLegacy,
|
||||
legacyCoordinatorAuthority
|
||||
})
|
||||
if (!run) {
|
||||
throw new OrchestrationError(
|
||||
|
||||
@@ -36,6 +36,21 @@ export type WorkerSetupReceipt = {
|
||||
| 'not_applicable'
|
||||
}
|
||||
|
||||
export function requireWorkerAuthority(runtime: OrcaRuntimeService, terminalHandle: string) {
|
||||
const authority = runtime.getOrchestrationDispatchAuthority(terminalHandle)
|
||||
const paneKey = authority?.paneKey ?? runtime.getTerminalPaneKey(terminalHandle)
|
||||
const processIncarnation =
|
||||
authority?.processIncarnation ?? runtime.getTerminalProcessIncarnation(terminalHandle)
|
||||
if (!paneKey || !processIncarnation) {
|
||||
throw new Error('stable_pane_required')
|
||||
}
|
||||
return {
|
||||
paneKey,
|
||||
processIncarnation,
|
||||
...(authority?.launchTokenHash ? { launchTokenHash: authority.launchTokenHash } : {})
|
||||
}
|
||||
}
|
||||
|
||||
export async function createExistingWorktreeWorkerTerminal(args: {
|
||||
runtime: OrcaRuntimeService
|
||||
worktreeId: string
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
createExistingWorktreeWorkerTerminal,
|
||||
createWorkerWorktree,
|
||||
monitorWorkerSetup,
|
||||
requireWorkerAuthority,
|
||||
type WorkerEffect,
|
||||
type WorkerSetupReceipt
|
||||
} from './orchestration-worker-topology'
|
||||
@@ -235,16 +236,11 @@ export const ORCHESTRATION_WORKER_START_METHODS: RpcMethod[] = [
|
||||
: `Agent did not become ready (${wait.status}).`
|
||||
)
|
||||
}
|
||||
const paneKey = runtime.getTerminalPaneKey(terminalHandle)
|
||||
const processIncarnation = runtime.getTerminalProcessIncarnation(terminalHandle)
|
||||
if (!paneKey || !processIncarnation) {
|
||||
throw new Error('stable_pane_required')
|
||||
}
|
||||
const terminalAuthority = requireWorkerAuthority(runtime, terminalHandle)
|
||||
const capability = db.prepareStartingWorkerAuthority({
|
||||
dispatchId: started.dispatch.id,
|
||||
handle: terminalHandle,
|
||||
paneKey,
|
||||
processIncarnation,
|
||||
...terminalAuthority,
|
||||
worktreeId: resolvedWorktree.id,
|
||||
effects,
|
||||
setupState: setupReceipt.state
|
||||
|
||||
@@ -1918,6 +1918,30 @@ describe('orchestration RPC methods', () => {
|
||||
expect(db.getDispatchContextById(result.dispatch.id)?.assignee_pane_key).toBe('tab_w:leaf_w')
|
||||
})
|
||||
|
||||
it('commits the target process launch token on a manual dispatch', async () => {
|
||||
setup()
|
||||
vi.spyOn(runtime, 'getOrchestrationDispatchAuthority').mockReturnValue({
|
||||
runtimeId: runtime.getRuntimeId(),
|
||||
terminalHandle: 'term_a',
|
||||
ptyId: 'pty_a',
|
||||
worktreeId: 'repo::worktree',
|
||||
paneKey: 'tab_w:leaf_w',
|
||||
processIncarnation: 'runtime_test:term_a:1',
|
||||
launchTokenHash: 'launch-token-hash',
|
||||
hostScope: { kind: 'local', hostId: 'local' }
|
||||
})
|
||||
const task = db.createTask({ spec: 'work' })
|
||||
|
||||
const result = (await call('orchestration.dispatch', {
|
||||
task: task.id,
|
||||
to: 'term_a'
|
||||
})) as { dispatch: { id: string } }
|
||||
|
||||
expect(db.getDispatchContextById(result.dispatch.id)?.launch_token_hash).toBe(
|
||||
'launch-token-hash'
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects dispatch for a pending task', async () => {
|
||||
setup()
|
||||
const parent = db.createTask({ spec: 'parent' })
|
||||
@@ -2162,6 +2186,32 @@ describe('orchestration RPC methods', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('commits the launched worker token with its durable authority', async () => {
|
||||
setup()
|
||||
mockCurrentWorkerStart()
|
||||
vi.spyOn(runtime, 'getOrchestrationDispatchAuthority').mockReturnValue({
|
||||
runtimeId: runtime.getRuntimeId(),
|
||||
terminalHandle: 'term_worker',
|
||||
ptyId: 'pty_worker',
|
||||
worktreeId: 'repo::worktree',
|
||||
paneKey: 'tab_worker:leaf_worker',
|
||||
processIncarnation: 'runtime_test:term_worker:1',
|
||||
launchTokenHash: 'worker-launch-token-hash',
|
||||
hostScope: { kind: 'local', hostId: 'local' }
|
||||
})
|
||||
const task = db.createTask({ spec: 'persist worker identity' })
|
||||
|
||||
const result = (await call('orchestration.workerStart', {
|
||||
task: task.id,
|
||||
from: 'term_coord',
|
||||
agent: 'codex'
|
||||
})) as { dispatchId: string }
|
||||
|
||||
expect(db.getDispatchContextById(result.dispatchId)?.launch_token_hash).toBe(
|
||||
'worker-launch-token-hash'
|
||||
)
|
||||
})
|
||||
|
||||
it('surfaces a worker terminal reveal failure without discarding the live worker', async () => {
|
||||
setup()
|
||||
mockCurrentWorkerStart()
|
||||
|
||||
@@ -2,7 +2,12 @@
|
||||
import { z } from 'zod'
|
||||
import { defineMethod, type RpcMethod } from '../core'
|
||||
import { OptionalFiniteNumber, OptionalString, OptionalBoolean, requiredString } from '../schemas'
|
||||
import type { MessageType, MessagePriority, TaskStatus } from '../../orchestration/db'
|
||||
import {
|
||||
LEGACY_CONTRACT_VERSION,
|
||||
type MessageType,
|
||||
type MessagePriority,
|
||||
type TaskStatus
|
||||
} from '../../orchestration/db'
|
||||
import { MESSAGE_TYPES } from '../../orchestration/types'
|
||||
import { buildDispatchPreamble } from '../../orchestration/preamble'
|
||||
import { formatMessageBanner } from '../../orchestration/formatter'
|
||||
@@ -51,6 +56,10 @@ function parseRemoteWorkerPayload(payload: string | undefined): Record<string, u
|
||||
}
|
||||
}
|
||||
|
||||
function isWorkerReportOutcome(value: unknown): value is 'succeeded' | 'failed' {
|
||||
return value === 'succeeded' || value === 'failed'
|
||||
}
|
||||
|
||||
const SendParams = z
|
||||
.object({
|
||||
to: OptionalString,
|
||||
@@ -107,6 +116,9 @@ const CheckParams = z
|
||||
// Why: one-release RPC compatibility only; the public CLI uses --format because no terminal input is injected.
|
||||
inject: OptionalBoolean,
|
||||
ack: OptionalString,
|
||||
compatibilityAck: OptionalString,
|
||||
compatibilityQuestionAck: OptionalString,
|
||||
compatibilityCliCommand: z.enum(['orca', 'orca-ide', 'orca-dev']).optional(),
|
||||
run: OptionalString,
|
||||
wait: OptionalBoolean,
|
||||
timeoutMs: OptionalFiniteNumber
|
||||
@@ -205,7 +217,9 @@ const AskParams = z
|
||||
options: OptionalString,
|
||||
timeoutMs: OptionalFiniteNumber,
|
||||
from: OptionalString,
|
||||
run: OptionalString
|
||||
run: OptionalString,
|
||||
compatibilityCliCommand: z.enum(['orca', 'orca-ide', 'orca-dev']).optional(),
|
||||
compatibilityWindowsCommand: z.enum(['orca', 'orca-ide']).optional()
|
||||
})
|
||||
.superRefine((params, ctx) => {
|
||||
if ((params.question ? 1 : 0) + (params.resume ? 1 : 0) !== 1) {
|
||||
@@ -241,6 +255,7 @@ function resolveRunScope(
|
||||
callerTerminalHandle?: string
|
||||
callerPaneKey?: string
|
||||
requireCurrentConsumer: boolean
|
||||
legacyCoordinatorRunId?: string
|
||||
}
|
||||
): RunRow {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
@@ -252,6 +267,9 @@ function resolveRunScope(
|
||||
if (!params.requireCurrentConsumer && explicit) {
|
||||
return explicit
|
||||
}
|
||||
if (explicit && params.legacyCoordinatorRunId === explicit.id) {
|
||||
return explicit
|
||||
}
|
||||
if (!params.callerTerminalHandle) {
|
||||
throw new OrchestrationError(
|
||||
'run_required',
|
||||
@@ -366,6 +384,49 @@ function resolveMessageRun(
|
||||
return { run, dispatchId: dispatch?.id ?? dispatchId }
|
||||
}
|
||||
|
||||
function legacyWorkerDeliveryContract(
|
||||
runtime: OrcaRuntimeService,
|
||||
runId: string | undefined,
|
||||
recipient: string
|
||||
): 'legacy_direct' | undefined {
|
||||
if (!runId) {
|
||||
return undefined
|
||||
}
|
||||
if (!recipient.startsWith('dispatch:')) {
|
||||
return runtime
|
||||
.getOrchestrationDb()
|
||||
.resolveLegacyWorkerCandidate({ runId, terminalHandle: recipient })
|
||||
? 'legacy_direct'
|
||||
: undefined
|
||||
}
|
||||
const dispatch = runtime
|
||||
.getOrchestrationDb()
|
||||
.getDispatchContextById(recipient.slice('dispatch:'.length))
|
||||
return dispatch?.run_id === runId &&
|
||||
dispatch.contract_version === LEGACY_CONTRACT_VERSION &&
|
||||
(dispatch.status === 'pending' || dispatch.status === 'dispatched')
|
||||
? 'legacy_direct'
|
||||
: undefined
|
||||
}
|
||||
|
||||
function interruptedAcknowledgedCheck(
|
||||
runId: string,
|
||||
acknowledged: string,
|
||||
reason: 'consumer_fenced' | 'outcome_unknown' | 'waiter_exists'
|
||||
): Record<string, unknown> {
|
||||
return {
|
||||
runId,
|
||||
deliveryId: null,
|
||||
messages: [],
|
||||
count: 0,
|
||||
acknowledged,
|
||||
timedOut: false,
|
||||
cancelled: false,
|
||||
connectionLost: false,
|
||||
waitInterrupted: reason
|
||||
}
|
||||
}
|
||||
|
||||
function rejectFederatedExplicitTarget(params: { to?: string; run?: string }): void {
|
||||
if (params.to || params.run) {
|
||||
throw new OrchestrationError(
|
||||
@@ -382,7 +443,10 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.send',
|
||||
params: SendParams,
|
||||
handler: async (params, { runtime, orchestrationCapability }) => {
|
||||
handler: async (
|
||||
params,
|
||||
{ runtime, orchestrationCapability, legacyCoordinatorRunId, revalidateLegacyCoordinator }
|
||||
) => {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const from = params.from ?? 'unknown'
|
||||
// Why: caller-supplied pane fields are only compatibility metadata; lifecycle authority uses the runtime-observed pane plus capability.
|
||||
@@ -463,6 +527,15 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
runId: params.run,
|
||||
payload: params.payload
|
||||
})
|
||||
if (
|
||||
params.type === 'worker_done' &&
|
||||
!isWorkerReportOutcome(parseRemoteWorkerPayload(params.payload).outcome)
|
||||
) {
|
||||
throw new OrchestrationError(
|
||||
'invalid_argument',
|
||||
'worker_done requires outcome=succeeded|failed for a current Dispatch.'
|
||||
)
|
||||
}
|
||||
if (params.to?.startsWith('task:')) {
|
||||
throw new OrchestrationError(
|
||||
'invalid_argument',
|
||||
@@ -514,6 +587,7 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
'Coordinator-to-worker control mail cannot report worker lifecycle.'
|
||||
)
|
||||
}
|
||||
revalidateLegacyCoordinator?.()
|
||||
const relay = db.enqueueFederationRelay({
|
||||
dispatchId,
|
||||
direction: 'to_worker',
|
||||
@@ -540,6 +614,7 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
}
|
||||
}
|
||||
// Point-to-point — existing single-recipient behavior
|
||||
revalidateLegacyCoordinator?.()
|
||||
const msg = db.insertMessage({
|
||||
from,
|
||||
to,
|
||||
@@ -550,7 +625,12 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
threadId: params.threadId,
|
||||
payload: params.payload,
|
||||
senderPaneKey,
|
||||
runId: routing.run?.id
|
||||
runId: routing.run?.id,
|
||||
deliveryContract: legacyWorkerDeliveryContract(
|
||||
runtime,
|
||||
routing.run?.id ?? legacyCoordinatorRunId,
|
||||
to
|
||||
)
|
||||
})
|
||||
const dispatch = routing.dispatchId
|
||||
? db.getDispatchContextById(routing.dispatchId)
|
||||
@@ -607,6 +687,7 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
throw new Error(`No recipients resolved for group address: ${to}`)
|
||||
}
|
||||
|
||||
revalidateLegacyCoordinator?.()
|
||||
const threadId = params.threadId ?? `thread_${Date.now()}`
|
||||
const messages = handles.map((handle) =>
|
||||
db.insertMessage({
|
||||
@@ -619,7 +700,12 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
threadId,
|
||||
payload: params.payload,
|
||||
senderPaneKey,
|
||||
runId: routing.run?.id
|
||||
runId: routing.run?.id,
|
||||
deliveryContract: legacyWorkerDeliveryContract(
|
||||
runtime,
|
||||
routing.run?.id ?? legacyCoordinatorRunId,
|
||||
handle
|
||||
)
|
||||
})
|
||||
)
|
||||
for (const message of messages) {
|
||||
@@ -633,7 +719,16 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.check',
|
||||
params: CheckParams,
|
||||
handler: async (params, { runtime, signal }) => {
|
||||
handler: async (
|
||||
params,
|
||||
{
|
||||
runtime,
|
||||
signal,
|
||||
legacyCoordinatorRunId,
|
||||
revalidateLegacyCoordinator,
|
||||
recordMutationReceipt
|
||||
}
|
||||
) => {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const handle = params.terminal ?? 'unknown'
|
||||
const typeFilter = parseMessageTypes(params.types)
|
||||
@@ -646,7 +741,8 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
runId: params.run,
|
||||
callerTerminalHandle: handle,
|
||||
callerPaneKey: paneKey ?? undefined,
|
||||
requireCurrentConsumer: true
|
||||
requireCurrentConsumer: true,
|
||||
legacyCoordinatorRunId
|
||||
})
|
||||
const generation = run.consumer_generation
|
||||
const address = `run:${run.id}`
|
||||
@@ -659,6 +755,11 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
deliveryId: params.ack
|
||||
})
|
||||
: undefined
|
||||
if (acknowledged) {
|
||||
recordMutationReceipt?.(
|
||||
interruptedAcknowledgedCheck(run.id, acknowledged.delivery.id, 'outcome_unknown')
|
||||
)
|
||||
}
|
||||
if (params.peek || params.all || params.unread === false) {
|
||||
const history = db.getRunMailboxHistory(run.id, 100, typeFilter)
|
||||
const messages =
|
||||
@@ -722,14 +823,28 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
signal,
|
||||
exclusive: true
|
||||
})
|
||||
try {
|
||||
revalidateLegacyCoordinator?.()
|
||||
} catch (error) {
|
||||
if (!acknowledged) {
|
||||
throw error
|
||||
}
|
||||
return interruptedAcknowledgedCheck(run.id, acknowledged.delivery.id, 'consumer_fenced')
|
||||
}
|
||||
const latestRun = db.getRun(run.id)
|
||||
if (!latestRun || latestRun.consumer_generation !== generation) {
|
||||
if (acknowledged) {
|
||||
return interruptedAcknowledgedCheck(run.id, acknowledged.delivery.id, 'consumer_fenced')
|
||||
}
|
||||
throw new OrchestrationError(
|
||||
'consumer_fenced',
|
||||
'This mailbox consumer was replaced while waiting.'
|
||||
)
|
||||
}
|
||||
if (waitResult === 'waiter_exists') {
|
||||
if (acknowledged) {
|
||||
return interruptedAcknowledgedCheck(run.id, acknowledged.delivery.id, 'waiter_exists')
|
||||
}
|
||||
throw new OrchestrationError(
|
||||
'waiter_exists',
|
||||
`Run ${run.id} already has an active actionable waiter.`
|
||||
@@ -911,13 +1026,28 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.reply',
|
||||
params: ReplyParams,
|
||||
handler: async (params, { runtime }) => {
|
||||
handler: async (params, { runtime, legacyCoordinatorRunId }) => {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const original = db.getMessageById(params.id)
|
||||
if (!original) {
|
||||
throw new Error(`Message not found: ${params.id}`)
|
||||
}
|
||||
if (original.run_id === ORCHESTRATION_LEGACY_RUN_ID) {
|
||||
if (
|
||||
legacyCoordinatorRunId &&
|
||||
(original.run_id !== legacyCoordinatorRunId ||
|
||||
(params.run !== undefined && params.run !== legacyCoordinatorRunId))
|
||||
) {
|
||||
throw new OrchestrationError(
|
||||
'request_mismatch',
|
||||
`Message ${params.id} does not belong to this adopted Run.`,
|
||||
{ effectsApplied: false }
|
||||
)
|
||||
}
|
||||
if (
|
||||
original.run_id === ORCHESTRATION_LEGACY_RUN_ID ||
|
||||
original.delivery_contract === 'legacy_direct' ||
|
||||
original.delivery_contract === 'audit_only'
|
||||
) {
|
||||
throw new OrchestrationError(
|
||||
'legacy_read_only',
|
||||
'Legacy orchestration messages are inspect-only; no reply was applied.',
|
||||
@@ -930,7 +1060,8 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
const run = resolveRunScope(runtime, {
|
||||
runId: params.run ?? question.run_id,
|
||||
callerTerminalHandle: params.from,
|
||||
requireCurrentConsumer: true
|
||||
requireCurrentConsumer: true,
|
||||
legacyCoordinatorRunId
|
||||
})
|
||||
const answered = db.answerQuestion({
|
||||
messageId: question.message_id,
|
||||
@@ -993,7 +1124,7 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.taskCreate',
|
||||
params: TaskCreateParams,
|
||||
handler: (params, { runtime }) => {
|
||||
handler: (params, { runtime, legacyCoordinatorRunId }) => {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
let deps: string[] | undefined
|
||||
if (params.deps) {
|
||||
@@ -1017,7 +1148,8 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
runId: resolveRunScope(runtime, {
|
||||
runId: params.run,
|
||||
callerTerminalHandle: params.callerTerminalHandle,
|
||||
requireCurrentConsumer: true
|
||||
requireCurrentConsumer: true,
|
||||
legacyCoordinatorRunId
|
||||
}).id
|
||||
})
|
||||
return { task }
|
||||
@@ -1027,7 +1159,7 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.taskList',
|
||||
params: TaskListParams,
|
||||
handler: (params, { runtime }) => {
|
||||
handler: (params, { runtime, legacyCoordinatorRunId }) => {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const explicitRun = params.run ? db.getRun(params.run) : undefined
|
||||
const run =
|
||||
@@ -1036,7 +1168,8 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
: resolveRunScope(runtime, {
|
||||
runId: params.run,
|
||||
callerTerminalHandle: params.callerTerminalHandle,
|
||||
requireCurrentConsumer: params.run === undefined
|
||||
requireCurrentConsumer: params.run === undefined,
|
||||
legacyCoordinatorRunId
|
||||
})
|
||||
// Why: listTasksWithDispatch adds assignee_handle + dispatch_id (NULL for non-dispatched), so legacy-shape consumers are unaffected.
|
||||
const joined = db.listTasksWithDispatch({
|
||||
@@ -1063,12 +1196,13 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.taskUpdate',
|
||||
params: TaskUpdateParams,
|
||||
handler: (params, { runtime }) => {
|
||||
handler: (params, { runtime, legacyCoordinatorRunId }) => {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const run = resolveRunScope(runtime, {
|
||||
runId: params.run,
|
||||
callerTerminalHandle: params.callerTerminalHandle,
|
||||
requireCurrentConsumer: true
|
||||
requireCurrentConsumer: true,
|
||||
legacyCoordinatorRunId
|
||||
})
|
||||
const existing = db.getTask(params.id)
|
||||
if (!existing || existing.run_id !== run.id) {
|
||||
@@ -1088,7 +1222,7 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
defineMethod({
|
||||
name: 'orchestration.dispatch',
|
||||
params: DispatchParams,
|
||||
handler: async (params, { runtime }) => {
|
||||
handler: async (params, { runtime, legacyCoordinatorRunId, revalidateLegacyCoordinator }) => {
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const task = db.getTask(params.task)
|
||||
if (!task) {
|
||||
@@ -1097,7 +1231,8 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
const run = resolveRunScope(runtime, {
|
||||
runId: params.run,
|
||||
callerTerminalHandle: params.from,
|
||||
requireCurrentConsumer: true
|
||||
requireCurrentConsumer: true,
|
||||
legacyCoordinatorRunId
|
||||
})
|
||||
if (task.run_id !== run.id) {
|
||||
throw new OrchestrationError(
|
||||
@@ -1143,8 +1278,13 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
}
|
||||
}
|
||||
|
||||
const assigneePaneKey = runtime.getTerminalPaneKey(to) ?? undefined
|
||||
const processIncarnation = runtime.getTerminalProcessIncarnation(to) ?? undefined
|
||||
const dispatchAuthority = runtime.getOrchestrationDispatchAuthority(to)
|
||||
const assigneePaneKey =
|
||||
dispatchAuthority?.paneKey ?? runtime.getTerminalPaneKey(to) ?? undefined
|
||||
const processIncarnation =
|
||||
dispatchAuthority?.processIncarnation ??
|
||||
runtime.getTerminalProcessIncarnation(to) ??
|
||||
undefined
|
||||
if (params.inject && (!assigneePaneKey || !processIncarnation)) {
|
||||
throw new OrchestrationError(
|
||||
'stable_pane_required',
|
||||
@@ -1152,7 +1292,13 @@ export const ORCHESTRATION_METHODS: RpcMethod[] = [
|
||||
)
|
||||
}
|
||||
|
||||
const ctx = db.createDispatchContext(params.task, to, assigneePaneKey)
|
||||
revalidateLegacyCoordinator?.()
|
||||
const ctx = db.createDispatchContext(
|
||||
params.task,
|
||||
to,
|
||||
assigneePaneKey,
|
||||
dispatchAuthority?.launchTokenHash ?? undefined
|
||||
)
|
||||
const dispatchCapability = params.inject
|
||||
? db.mintDispatchCapability({
|
||||
dispatchId: ctx.id,
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
import type { OrchestrationCompatibilityEvidence } from '../../../shared/orchestration-compatibility-evidence'
|
||||
import type {
|
||||
DispatchContextRow,
|
||||
LegacyCompatibilityPrincipalRow,
|
||||
LegacyPrincipalRole
|
||||
} from '../orchestration/types'
|
||||
import { OrchestrationError } from '../orchestration/orchestration-error'
|
||||
import type { OrcaRuntimeService, OrchestrationCompatibilityCallerAuthority } from '../orca-runtime'
|
||||
import { LEGACY_CONTRACT_VERSION } from '../orchestration/db'
|
||||
import type { RpcRequest } from './core'
|
||||
import {
|
||||
equivalentLegacyPaneKey,
|
||||
legacyCoordinatorReadOnly,
|
||||
legacyReadOnlyError
|
||||
} from './orchestration-legacy-process-identity'
|
||||
|
||||
export type LegacyPrincipalCandidate = {
|
||||
runId: string
|
||||
role: LegacyPrincipalRole
|
||||
dispatchId?: string
|
||||
terminalHandle: string
|
||||
paneKey: string
|
||||
}
|
||||
|
||||
export function resolveAttestedLegacyPrincipal(args: {
|
||||
runtime: OrcaRuntimeService
|
||||
evidence?: OrchestrationCompatibilityEvidence
|
||||
candidate: LegacyPrincipalCandidate
|
||||
authority?: OrchestrationCompatibilityCallerAuthority
|
||||
}): LegacyCompatibilityPrincipalRow {
|
||||
const authority = args.authority ?? verifyAttestedLegacyCandidate(args)
|
||||
return args.runtime.getOrchestrationDb().commitLegacyCompatibilityPrincipal({
|
||||
runId: args.candidate.runId,
|
||||
dispatchId: args.candidate.dispatchId,
|
||||
role: args.candidate.role,
|
||||
hostScope: JSON.stringify(authority.hostScope),
|
||||
terminalHandle: args.candidate.terminalHandle,
|
||||
paneKey: args.candidate.paneKey,
|
||||
launchTokenHash: authority.launchTokenHash,
|
||||
processIncarnation: authority.processIncarnation
|
||||
}).principal
|
||||
}
|
||||
|
||||
export function verifyAttestedLegacyCandidate(args: {
|
||||
runtime: OrcaRuntimeService
|
||||
evidence?: OrchestrationCompatibilityEvidence
|
||||
candidate: LegacyPrincipalCandidate
|
||||
}) {
|
||||
const authority = args.runtime.verifyOrchestrationCompatibilityCaller(args.evidence)
|
||||
if (
|
||||
!authority ||
|
||||
!equivalentLegacyPaneKey(args.candidate.paneKey, authority.paneKey) ||
|
||||
args.candidate.terminalHandle !== authority.terminalHandle
|
||||
) {
|
||||
throw legacyReadOnlyError()
|
||||
}
|
||||
if (
|
||||
args.candidate.role === 'worker' &&
|
||||
args.candidate.dispatchId &&
|
||||
!args.runtime.getOrchestrationDb().isDispatchProcessCurrent({
|
||||
dispatchId: args.candidate.dispatchId,
|
||||
paneKey: authority.paneKey,
|
||||
processIncarnation: authority.processIncarnation
|
||||
})
|
||||
) {
|
||||
throw legacyReadOnlyError()
|
||||
}
|
||||
return authority
|
||||
}
|
||||
|
||||
export class LegacyCompatibilityAuthority {
|
||||
constructor(private readonly runtime: OrcaRuntimeService) {}
|
||||
|
||||
resolveWorkerDispatch(
|
||||
request: RpcRequest,
|
||||
target: { terminalHandle?: string; dispatchId?: string; taskId?: string }
|
||||
): DispatchContextRow | undefined {
|
||||
const db = this.runtime.getOrchestrationDb()
|
||||
const evidence = request.orchestrationCompatibilityEvidence
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id
|
||||
const existing = adoptedRunId
|
||||
? db.resolveLegacyCompatibilityPrincipalByIdentity({
|
||||
runId: adoptedRunId,
|
||||
role: 'worker',
|
||||
terminalHandle: evidence?.terminalHandle,
|
||||
paneKey: evidence?.paneKey
|
||||
})
|
||||
: undefined
|
||||
if (existing?.dispatch_id) {
|
||||
verifyAttestedLegacyCandidate({
|
||||
runtime: this.runtime,
|
||||
evidence,
|
||||
candidate: candidateFromPrincipal(existing)
|
||||
})
|
||||
const settledDispatch = db.getDispatchContextById(existing.dispatch_id)
|
||||
if (
|
||||
settledDispatch &&
|
||||
(!target.dispatchId || target.dispatchId === settledDispatch.id) &&
|
||||
(!target.taskId || target.taskId === settledDispatch.task_id)
|
||||
) {
|
||||
return settledDispatch
|
||||
}
|
||||
}
|
||||
const dispatch = db.resolveLegacyWorkerCandidate({
|
||||
runId: adoptedRunId,
|
||||
terminalHandle: evidence?.terminalHandle ?? target.terminalHandle,
|
||||
paneKey: evidence?.paneKey,
|
||||
dispatchId: target.dispatchId,
|
||||
taskId: target.taskId
|
||||
})?.dispatch
|
||||
if (!dispatch && evidence) {
|
||||
const retained = db.resolveLegacyWorkerCandidate({
|
||||
runId: adoptedRunId,
|
||||
terminalHandle: target.terminalHandle,
|
||||
dispatchId: target.dispatchId,
|
||||
taskId: target.taskId
|
||||
})
|
||||
if (retained) {
|
||||
throw legacyReadOnlyError()
|
||||
}
|
||||
}
|
||||
return dispatch
|
||||
}
|
||||
|
||||
resolveAskDispatch(
|
||||
request: RpcRequest,
|
||||
params: { from?: string; resume?: string }
|
||||
): DispatchContextRow | undefined {
|
||||
const db = this.runtime.getOrchestrationDb()
|
||||
if (params.resume) {
|
||||
const question = db.getQuestion(params.resume)
|
||||
return question ? db.getDispatchContextById(question.dispatch_id) : undefined
|
||||
}
|
||||
return this.resolveWorkerDispatch(request, { terminalHandle: params.from })
|
||||
}
|
||||
|
||||
resolveCheckPrincipal(
|
||||
request: RpcRequest,
|
||||
terminalHandle?: string
|
||||
): LegacyCompatibilityPrincipalRow | undefined {
|
||||
const db = this.runtime.getOrchestrationDb()
|
||||
const adoption = db.getLegacyAdoption()
|
||||
if (!adoption) {
|
||||
return undefined
|
||||
}
|
||||
const evidence = request.orchestrationCompatibilityEvidence
|
||||
const existingWorker = db.resolveLegacyCompatibilityPrincipalByIdentity({
|
||||
runId: adoption.adopted_run_id,
|
||||
role: 'worker',
|
||||
terminalHandle: evidence?.terminalHandle,
|
||||
paneKey: evidence?.paneKey
|
||||
})
|
||||
if (existingWorker) {
|
||||
verifyAttestedLegacyCandidate({
|
||||
runtime: this.runtime,
|
||||
evidence,
|
||||
candidate: candidateFromPrincipal(existingWorker)
|
||||
})
|
||||
return existingWorker
|
||||
}
|
||||
const worker = db.resolveLegacyWorkerCandidate({
|
||||
runId: adoption.adopted_run_id,
|
||||
terminalHandle: evidence?.terminalHandle ?? terminalHandle,
|
||||
paneKey: evidence?.paneKey
|
||||
})
|
||||
if (worker) {
|
||||
return this.attestWorker(request, worker.dispatch)
|
||||
}
|
||||
if (evidence) {
|
||||
const retained = db.resolveLegacyWorkerCandidate({
|
||||
runId: adoption.adopted_run_id,
|
||||
terminalHandle
|
||||
})
|
||||
const settled = db.resolveLegacyCompatibilityPrincipalByIdentity({
|
||||
runId: adoption.adopted_run_id,
|
||||
role: 'worker',
|
||||
terminalHandle
|
||||
})
|
||||
if (retained || settled) {
|
||||
throw legacyReadOnlyError()
|
||||
}
|
||||
}
|
||||
return this.attestCoordinator(request, adoption.adopted_run_id, false)
|
||||
}
|
||||
|
||||
attestWorker(request: RpcRequest, dispatch: DispatchContextRow): LegacyCompatibilityPrincipalRow {
|
||||
if (dispatch.contract_version !== LEGACY_CONTRACT_VERSION) {
|
||||
throw new OrchestrationError(
|
||||
'request_mismatch',
|
||||
`Dispatch ${dispatch.id} does not use the legacy contract.`
|
||||
)
|
||||
}
|
||||
return resolveAttestedLegacyPrincipal({
|
||||
runtime: this.runtime,
|
||||
evidence: request.orchestrationCompatibilityEvidence,
|
||||
candidate: candidateFromDispatch(dispatch)
|
||||
})
|
||||
}
|
||||
|
||||
attestCoordinator(
|
||||
request: RpcRequest,
|
||||
runId: string,
|
||||
required = true
|
||||
): LegacyCompatibilityPrincipalRow | undefined {
|
||||
const db = this.runtime.getOrchestrationDb()
|
||||
const candidate = db.resolveLegacyCoordinatorCandidate({
|
||||
runId,
|
||||
terminalHandle: request.orchestrationCompatibilityEvidence?.terminalHandle,
|
||||
paneKey: request.orchestrationCompatibilityEvidence?.paneKey
|
||||
})
|
||||
if (!candidate) {
|
||||
if (required) {
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
const principal = resolveAttestedLegacyPrincipal({
|
||||
runtime: this.runtime,
|
||||
evidence: request.orchestrationCompatibilityEvidence,
|
||||
candidate: {
|
||||
runId,
|
||||
role: 'coordinator',
|
||||
terminalHandle: candidate.terminalHandle,
|
||||
paneKey: candidate.paneKey
|
||||
}
|
||||
})
|
||||
return principal
|
||||
}
|
||||
}
|
||||
|
||||
function candidateFromDispatch(dispatch: DispatchContextRow): LegacyPrincipalCandidate {
|
||||
if (!dispatch.assignee_handle || !dispatch.assignee_pane_key) {
|
||||
throw new OrchestrationError(
|
||||
'legacy_read_only',
|
||||
`Dispatch ${dispatch.id} lacks durable process identity. No effects were applied.`
|
||||
)
|
||||
}
|
||||
return {
|
||||
runId: dispatch.run_id,
|
||||
role: 'worker',
|
||||
dispatchId: dispatch.id,
|
||||
terminalHandle: dispatch.assignee_handle,
|
||||
paneKey: dispatch.assignee_pane_key
|
||||
}
|
||||
}
|
||||
|
||||
function candidateFromPrincipal(
|
||||
principal: LegacyCompatibilityPrincipalRow
|
||||
): LegacyPrincipalCandidate {
|
||||
return {
|
||||
runId: principal.run_id,
|
||||
role: principal.role,
|
||||
dispatchId: principal.dispatch_id ?? undefined,
|
||||
terminalHandle: principal.terminal_handle,
|
||||
paneKey: principal.pane_key
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { mkdtempSync, rmSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { expect, vi } from 'vitest'
|
||||
import type { OrchestrationCompatibilityEvidence } from '../../../shared/orchestration-compatibility-evidence'
|
||||
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../shared/protocol-version'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import { OrcaRuntimeService } from '../orca-runtime'
|
||||
import { OrchestrationDb } from '../orchestration/db'
|
||||
import type { RpcRequest, RpcResponse } from './core'
|
||||
import { RpcDispatcher } from './dispatcher'
|
||||
import { ORCHESTRATION_METHODS } from './methods/orchestration'
|
||||
|
||||
export const WORKER_HANDLE = 'term_legacy_worker'
|
||||
export const WORKER_PANE = 'tab_worker:33333333-3333-4333-8333-333333333333'
|
||||
export const COORDINATOR_HANDLE = 'term_legacy_coord'
|
||||
export const COORDINATOR_PANE = 'tab_coord:44444444-4444-4444-8444-444444444444'
|
||||
|
||||
type Transport = 'dispatch' | 'websocket'
|
||||
|
||||
export type LegacyCompatibilityDispatcherHarness = {
|
||||
db: OrchestrationDb
|
||||
dispatcher: RpcDispatcher
|
||||
runtime: OrcaRuntimeService
|
||||
adoptedRunId: string
|
||||
taskId: string
|
||||
dispatchId: string
|
||||
notify: ReturnType<typeof vi.spyOn>
|
||||
verify: ReturnType<typeof vi.spyOn>
|
||||
}
|
||||
|
||||
const tempDirs: string[] = []
|
||||
const databases: OrchestrationDb[] = []
|
||||
|
||||
export function cleanupLegacyCompatibilityDispatcherHarnesses(): void {
|
||||
for (const database of databases.splice(0)) {
|
||||
database.close()
|
||||
}
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
}
|
||||
|
||||
export function createHarness(): LegacyCompatibilityDispatcherHarness {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'orca-legacy-dispatcher-'))
|
||||
tempDirs.push(dir)
|
||||
const dbPath = join(dir, 'orchestration.db')
|
||||
const before = new OrchestrationDb(dbPath)
|
||||
const task = before.createTask({
|
||||
spec: 'legacy assignment',
|
||||
createdByTerminalHandle: COORDINATOR_HANDLE
|
||||
})
|
||||
const dispatch = before.createDispatchContext(task.id, WORKER_HANDLE, WORKER_PANE)
|
||||
before.close()
|
||||
|
||||
const raw = new Database(dbPath)
|
||||
raw.exec(`
|
||||
UPDATE dispatch_contexts SET process_incarnation = 'process-1';
|
||||
DROP INDEX IF EXISTS idx_messages_delivery_contract;
|
||||
DROP TABLE legacy_mail_receipts;
|
||||
DROP TABLE legacy_operation_receipts;
|
||||
DROP TABLE legacy_compatibility_principals;
|
||||
DROP TABLE legacy_adoptions;
|
||||
`)
|
||||
raw.pragma('user_version = 18')
|
||||
raw.close()
|
||||
|
||||
const db = new OrchestrationDb(dbPath)
|
||||
databases.push(db)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
const runtime = new OrcaRuntimeService()
|
||||
runtime.setOrchestrationDb(db)
|
||||
vi.spyOn(runtime, 'getTerminalPaneKey').mockImplementation((handle) =>
|
||||
handle === COORDINATOR_HANDLE ? COORDINATOR_PANE : handle === WORKER_HANDLE ? WORKER_PANE : null
|
||||
)
|
||||
const verify = vi
|
||||
.spyOn(runtime, 'verifyOrchestrationCompatibilityCaller')
|
||||
.mockImplementation((evidence) => {
|
||||
const validWorker =
|
||||
evidence?.terminalHandle === WORKER_HANDLE && evidence.paneKey === WORKER_PANE
|
||||
const validCoordinator =
|
||||
evidence?.terminalHandle === COORDINATOR_HANDLE && evidence.paneKey === COORDINATOR_PANE
|
||||
if ((!validWorker && !validCoordinator) || !evidence?.launchToken) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
hostScope: { kind: 'local', hostId: 'local' },
|
||||
terminalHandle: evidence.terminalHandle as string,
|
||||
paneKey: evidence.paneKey as string,
|
||||
processIncarnation: 'process-1',
|
||||
launchTokenHash: createHash('sha256').update(evidence.launchToken).digest('hex')
|
||||
}
|
||||
})
|
||||
const notify = vi.spyOn(runtime, 'notifyMessageArrived').mockImplementation(() => {})
|
||||
return {
|
||||
db,
|
||||
dispatcher: new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS }),
|
||||
runtime,
|
||||
adoptedRunId,
|
||||
taskId: task.id,
|
||||
dispatchId: dispatch.id,
|
||||
notify,
|
||||
verify
|
||||
}
|
||||
}
|
||||
|
||||
export function evidence(
|
||||
role: 'worker' | 'coordinator',
|
||||
valid = true
|
||||
): OrchestrationCompatibilityEvidence {
|
||||
const worker = role === 'worker'
|
||||
return {
|
||||
terminalHandle: worker ? WORKER_HANDLE : COORDINATOR_HANDLE,
|
||||
paneKey: valid ? (worker ? WORKER_PANE : COORDINATOR_PANE) : 'tab_wrong:wrong-leaf',
|
||||
launchToken: `${role}-token`
|
||||
}
|
||||
}
|
||||
|
||||
export function request(
|
||||
method: string,
|
||||
params: unknown,
|
||||
proof: OrchestrationCompatibilityEvidence,
|
||||
invocationId: string
|
||||
): RpcRequest {
|
||||
return {
|
||||
id: `rpc_${invocationId}`,
|
||||
authToken: 'caller-token',
|
||||
method,
|
||||
params,
|
||||
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
|
||||
orchestrationRequestId: invocationId,
|
||||
compatibilityInvocationId: invocationId,
|
||||
orchestrationCompatibilityEvidence: proof
|
||||
}
|
||||
}
|
||||
|
||||
export async function invoke(
|
||||
dispatcher: RpcDispatcher,
|
||||
rpcRequest: RpcRequest,
|
||||
transport: Transport
|
||||
): Promise<RpcResponse> {
|
||||
if (transport === 'dispatch') {
|
||||
return await dispatcher.dispatch(rpcRequest)
|
||||
}
|
||||
const replies: string[] = []
|
||||
await dispatcher.dispatchStreaming(rpcRequest, (reply) => replies.push(reply))
|
||||
expect(replies).toHaveLength(1)
|
||||
return JSON.parse(replies[0]) as RpcResponse
|
||||
}
|
||||
|
||||
export function counts(db: OrchestrationDb): Record<string, number> {
|
||||
const sqlite = (db as unknown as { db: Database.Database }).db
|
||||
return Object.fromEntries(
|
||||
[
|
||||
'messages',
|
||||
'legacy_compatibility_principals',
|
||||
'legacy_operation_receipts',
|
||||
'legacy_mail_receipts',
|
||||
'mutation_receipts'
|
||||
].map((table) => [
|
||||
table,
|
||||
(sqlite.prepare(`SELECT COUNT(*) AS count FROM ${table}`).get() as { count: number }).count
|
||||
])
|
||||
)
|
||||
}
|
||||
|
||||
export function escalationParams(harness: LegacyCompatibilityDispatcherHarness) {
|
||||
return {
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
subject: 'Blocked',
|
||||
type: 'escalation',
|
||||
payload: JSON.stringify({ taskId: harness.taskId, dispatchId: harness.dispatchId })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,755 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type Database from '../../sqlite/sync-database'
|
||||
import {
|
||||
cleanupLegacyCompatibilityDispatcherHarnesses,
|
||||
COORDINATOR_HANDLE,
|
||||
COORDINATOR_PANE,
|
||||
counts,
|
||||
createHarness,
|
||||
escalationParams,
|
||||
evidence,
|
||||
invoke,
|
||||
request,
|
||||
WORKER_HANDLE,
|
||||
WORKER_PANE
|
||||
} from './orchestration-legacy-compatibility-dispatcher-test-fixture'
|
||||
|
||||
afterEach(() => {
|
||||
cleanupLegacyCompatibilityDispatcherHarnesses()
|
||||
})
|
||||
|
||||
describe('legacy compatibility through RpcDispatcher', () => {
|
||||
it('rejects malformed current-contract input before compatibility attestation', async () => {
|
||||
const harness = createHarness()
|
||||
const before = counts(harness.db)
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{ from: WORKER_HANDLE, type: 'escalation' },
|
||||
evidence('worker'),
|
||||
'malformed'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'invalid_argument' } })
|
||||
expect(harness.verify).not.toHaveBeenCalled()
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
})
|
||||
|
||||
it.each([
|
||||
['dispatch', true],
|
||||
['dispatch', false],
|
||||
['websocket', true],
|
||||
['websocket', false]
|
||||
] as const)(
|
||||
'%s routes task-only escalation with valid proof=%s and zero partial effects',
|
||||
async (transport, valid) => {
|
||||
const harness = createHarness()
|
||||
const before = counts(harness.db)
|
||||
const response = await invoke(
|
||||
harness.dispatcher,
|
||||
request(
|
||||
'orchestration.send',
|
||||
escalationParams(harness),
|
||||
evidence('worker', valid),
|
||||
`${transport}-${valid}`
|
||||
),
|
||||
transport
|
||||
)
|
||||
|
||||
if (!valid) {
|
||||
expect(response).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: 'legacy_read_only' }
|
||||
})
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
expect(harness.notify).not.toHaveBeenCalled()
|
||||
return
|
||||
}
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: { type: 'escalation', delivery_contract: 'legacy_direct' },
|
||||
legacyCompatibility: { replayed: false }
|
||||
}
|
||||
})
|
||||
expect(counts(harness.db)).toEqual({
|
||||
...before,
|
||||
messages: before.messages + 1,
|
||||
legacy_compatibility_principals: before.legacy_compatibility_principals + 1,
|
||||
legacy_operation_receipts: before.legacy_operation_receipts + 1
|
||||
})
|
||||
expect(harness.notify).toHaveBeenCalledOnce()
|
||||
}
|
||||
)
|
||||
|
||||
it('validates, infers, settles, and replays legacy worker completion exactly once', async () => {
|
||||
const harness = createHarness()
|
||||
const baseParams = {
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
type: 'worker_done',
|
||||
body: 'legacy result',
|
||||
payload: JSON.stringify({ taskId: harness.taskId, dispatchId: harness.dispatchId })
|
||||
}
|
||||
const before = counts(harness.db)
|
||||
const invalid = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{ ...baseParams, subject: 'Completed', payload: JSON.stringify({ outcome: 'maybe' }) },
|
||||
evidence('worker'),
|
||||
'invalid-outcome'
|
||||
)
|
||||
)
|
||||
|
||||
expect(invalid).toMatchObject({ ok: false, error: { code: 'invalid_argument' } })
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
|
||||
const firstRequest = request(
|
||||
'orchestration.send',
|
||||
{ ...baseParams, subject: 'Completed' },
|
||||
evidence('worker'),
|
||||
'completion'
|
||||
)
|
||||
const first = await harness.dispatcher.dispatch(firstRequest)
|
||||
const replay = await harness.dispatcher.dispatch({ ...firstRequest, id: 'rpc_replay' })
|
||||
const mismatch = await harness.dispatcher.dispatch({
|
||||
...firstRequest,
|
||||
id: 'rpc_mismatch',
|
||||
params: { ...baseParams, subject: 'Changed completion' }
|
||||
})
|
||||
|
||||
expect(first).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
lifecycle: { action: 'settled', outcome: 'succeeded' },
|
||||
legacyCompatibility: { replayed: false }
|
||||
}
|
||||
})
|
||||
expect(replay).toMatchObject({
|
||||
ok: true,
|
||||
result: { legacyCompatibility: { replayed: true } }
|
||||
})
|
||||
expect(mismatch).toMatchObject({ ok: false, error: { code: 'request_mismatch' } })
|
||||
expect(harness.db.getTask(harness.taskId)?.status).toBe('completed')
|
||||
expect(harness.db.getDispatchContextById(harness.dispatchId)?.status).toBe('completed')
|
||||
expect(counts(harness.db)).toEqual({
|
||||
...before,
|
||||
messages: before.messages + 1,
|
||||
legacy_compatibility_principals: before.legacy_compatibility_principals + 1,
|
||||
legacy_operation_receipts: before.legacy_operation_receipts + 1
|
||||
})
|
||||
expect(harness.notify).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('replays an A-era completion without touching a newer current attempt', async () => {
|
||||
const harness = createHarness()
|
||||
const payload = JSON.stringify({
|
||||
taskId: harness.taskId,
|
||||
dispatchId: harness.dispatchId
|
||||
})
|
||||
const completion = harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
subject: 'Failed: pinned A treated this as complete',
|
||||
body: 'persisted result',
|
||||
type: 'worker_done',
|
||||
payload,
|
||||
senderPaneKey: WORKER_PANE,
|
||||
deliveryContract: 'legacy_direct'
|
||||
})
|
||||
harness.db.settleWorkerReport({
|
||||
taskId: harness.taskId,
|
||||
dispatchId: harness.dispatchId,
|
||||
outcome: 'succeeded',
|
||||
result: 'persisted result'
|
||||
})
|
||||
harness.db.commitLegacyCompatibilityPrincipal({
|
||||
runId: harness.adoptedRunId,
|
||||
dispatchId: harness.dispatchId,
|
||||
role: 'worker',
|
||||
hostScope: JSON.stringify({ kind: 'local', hostId: 'local' }),
|
||||
terminalHandle: WORKER_HANDLE,
|
||||
paneKey: WORKER_PANE,
|
||||
launchTokenHash: createHash('sha256').update('worker-token').digest('hex'),
|
||||
processIncarnation: 'process-1'
|
||||
})
|
||||
harness.db.updateTaskStatus(harness.taskId, 'ready')
|
||||
const currentDispatch = harness.db.createDispatchContext(
|
||||
harness.taskId,
|
||||
'term_current_worker',
|
||||
'tab_current_worker:77777777-7777-4777-8777-777777777777',
|
||||
'current-launch-hash'
|
||||
)
|
||||
const currentTaskBefore = harness.db.getTask(harness.taskId)
|
||||
const currentDispatchBefore = harness.db.getDispatchContextById(currentDispatch.id)
|
||||
const before = counts(harness.db)
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
subject: completion.subject,
|
||||
body: completion.body,
|
||||
type: 'worker_done',
|
||||
payload
|
||||
},
|
||||
evidence('worker'),
|
||||
'reconstruct-pinned-a-completion'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: { id: completion.id },
|
||||
lifecycle: { action: 'settled', outcome: 'succeeded', duplicate: true }
|
||||
}
|
||||
})
|
||||
expect(harness.db.getTask(harness.taskId)).toEqual(currentTaskBefore)
|
||||
expect(harness.db.getDispatchContextById(harness.dispatchId)?.status).toBe('completed')
|
||||
expect(harness.db.getDispatchContextById(currentDispatch.id)).toEqual(currentDispatchBefore)
|
||||
expect(counts(harness.db)).toEqual({
|
||||
...before,
|
||||
legacy_operation_receipts: before.legacy_operation_receipts + 1
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects a legacy lifecycle recipient outside the adopted Run with zero effects', async () => {
|
||||
const harness = createHarness()
|
||||
const before = counts(harness.db)
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{
|
||||
...escalationParams(harness),
|
||||
to: 'term_unrelated'
|
||||
},
|
||||
evidence('worker'),
|
||||
'wrong-recipient'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: 'request_mismatch' }
|
||||
})
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
expect(harness.verify).not.toHaveBeenCalled()
|
||||
expect(harness.notify).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects a reused pane whose live process incarnation is not the legacy worker', async () => {
|
||||
const harness = createHarness()
|
||||
const sqlite = (harness.db as unknown as { db: Database.Database }).db
|
||||
sqlite
|
||||
.prepare('UPDATE dispatch_contexts SET process_incarnation = ? WHERE id = ?')
|
||||
.run('different-process', harness.dispatchId)
|
||||
const before = counts(harness.db)
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request('orchestration.send', escalationParams(harness), evidence('worker'), 'reused-pane')
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'legacy_read_only' } })
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
expect(harness.notify).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects a legacy question recipient outside the adopted Run with zero effects', async () => {
|
||||
const harness = createHarness()
|
||||
const before = counts(harness.db)
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.ask',
|
||||
{
|
||||
from: WORKER_HANDLE,
|
||||
to: 'term_unrelated',
|
||||
question: 'Proceed?',
|
||||
timeoutMs: 1
|
||||
},
|
||||
evidence('worker'),
|
||||
'wrong-question-recipient'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: 'request_mismatch' }
|
||||
})
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
expect(harness.verify).not.toHaveBeenCalled()
|
||||
expect(harness.notify).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps distinct compatibility ask invocations on distinct questions', async () => {
|
||||
const harness = createHarness()
|
||||
const ask = {
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
question: 'Same question?',
|
||||
options: ['yes', 'no'],
|
||||
timeoutMs: 1
|
||||
}
|
||||
const first = await harness.dispatcher.dispatch(
|
||||
request('orchestration.ask', ask, evidence('worker'), 'ask-one')
|
||||
)
|
||||
const replay = await harness.dispatcher.dispatch(
|
||||
request('orchestration.ask', ask, evidence('worker'), 'ask-one')
|
||||
)
|
||||
const second = await harness.dispatcher.dispatch(
|
||||
request('orchestration.ask', ask, evidence('worker'), 'ask-two')
|
||||
)
|
||||
|
||||
expect(first).toMatchObject({ ok: true, result: { legacyCompatibility: { replayed: false } } })
|
||||
expect(replay).toMatchObject({ ok: true, result: { legacyCompatibility: { replayed: true } } })
|
||||
expect(second).toMatchObject({ ok: true, result: { legacyCompatibility: { replayed: false } } })
|
||||
const firstId = (first as { result: { messageId: string } }).result.messageId
|
||||
const replayId = (replay as { result: { messageId: string } }).result.messageId
|
||||
const secondId = (second as { result: { messageId: string } }).result.messageId
|
||||
expect(replayId).toBe(firstId)
|
||||
expect(secondId).not.toBe(firstId)
|
||||
expect(harness.db.getMessageById(firstId)?.type).toBe('decision_gate')
|
||||
|
||||
const coordinatorCheck = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{
|
||||
terminal: COORDINATOR_HANDLE,
|
||||
types: 'worker_done,escalation,decision_gate',
|
||||
format: true
|
||||
},
|
||||
evidence('coordinator'),
|
||||
'coordinator-check-decision-gates'
|
||||
)
|
||||
)
|
||||
expect(coordinatorCheck).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
messages: [
|
||||
{ id: firstId, type: 'decision_gate' },
|
||||
{ id: secondId, type: 'decision_gate' }
|
||||
],
|
||||
count: 2
|
||||
}
|
||||
})
|
||||
expect((coordinatorCheck as { result: { formatted: string } }).result.formatted).toContain(
|
||||
`orca orchestration reply --id ${firstId}`
|
||||
)
|
||||
})
|
||||
|
||||
it('keeps the normal wait budget when legacy check omits timeout', async () => {
|
||||
const harness = createHarness()
|
||||
const waitForMessage = vi
|
||||
.spyOn(harness.runtime, 'waitForMessage')
|
||||
.mockResolvedValue('timed_out')
|
||||
let clockReads = 0
|
||||
const now = vi.spyOn(Date, 'now').mockImplementation(() => {
|
||||
clockReads += 1
|
||||
return clockReads === 1 ? 0 : clockReads === 2 ? 1 : 120_001
|
||||
})
|
||||
|
||||
try {
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: WORKER_HANDLE, wait: true },
|
||||
evidence('worker'),
|
||||
'legacy-check-default-timeout'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: { messages: [], count: 0, timedOut: true }
|
||||
})
|
||||
expect(waitForMessage).toHaveBeenCalledOnce()
|
||||
} finally {
|
||||
now.mockRestore()
|
||||
}
|
||||
})
|
||||
|
||||
it('does not infer an outcome for a current Dispatch when legacy adoption exists', async () => {
|
||||
const harness = createHarness()
|
||||
const run = harness.db.createRun({
|
||||
objective: 'current work',
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current_coord:55555555-5555-4555-8555-555555555555'
|
||||
})
|
||||
const task = harness.db.createTask({ spec: 'current assignment', runId: run.id })
|
||||
const dispatch = harness.db.createDispatchContext(
|
||||
task.id,
|
||||
'term_current_worker',
|
||||
'tab_current_worker:66666666-6666-4666-8666-666666666666',
|
||||
'current-launch-hash'
|
||||
)
|
||||
const before = counts(harness.db)
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{
|
||||
from: 'term_current_worker',
|
||||
to: COORDINATOR_HANDLE,
|
||||
subject: 'Completed',
|
||||
type: 'worker_done',
|
||||
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id })
|
||||
},
|
||||
{
|
||||
terminalHandle: 'term_current_worker',
|
||||
paneKey: 'tab_current_worker:66666666-6666-4666-8666-666666666666',
|
||||
launchToken: 'current-token'
|
||||
},
|
||||
'current-missing-outcome'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'invalid_argument' } })
|
||||
expect(harness.db.getTask(task.id)?.status).toBe('dispatched')
|
||||
expect(harness.db.getDispatchContextById(dispatch.id)?.status).toBe('dispatched')
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
})
|
||||
|
||||
it('rejects invalid typed ACKs and consumes only the filtered legacy page', async () => {
|
||||
const harness = createHarness()
|
||||
await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{
|
||||
...escalationParams(harness),
|
||||
type: 'worker_done',
|
||||
subject: 'Completed',
|
||||
payload: JSON.stringify({ taskId: harness.taskId, dispatchId: harness.dispatchId })
|
||||
},
|
||||
evidence('worker'),
|
||||
'settle-for-mail'
|
||||
)
|
||||
)
|
||||
const status = harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: COORDINATOR_HANDLE,
|
||||
to: WORKER_HANDLE,
|
||||
subject: 'status first',
|
||||
type: 'status',
|
||||
deliveryContract: 'legacy_direct'
|
||||
})
|
||||
const question = harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: COORDINATOR_HANDLE,
|
||||
to: WORKER_HANDLE,
|
||||
subject: 'question second',
|
||||
type: 'question',
|
||||
deliveryContract: 'legacy_direct'
|
||||
})
|
||||
const check = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: WORKER_HANDLE, types: 'question' },
|
||||
evidence('worker'),
|
||||
'question-check'
|
||||
)
|
||||
)
|
||||
expect(check).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
messages: [{ id: question.id }],
|
||||
legacyCompatibility: { ackMessageIds: [question.id] }
|
||||
}
|
||||
})
|
||||
|
||||
const invalid = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{
|
||||
terminal: WORKER_HANDLE,
|
||||
compatibilityAck: JSON.stringify({
|
||||
messageIds: [question.id],
|
||||
types: ['not-a-message-type']
|
||||
})
|
||||
},
|
||||
evidence('worker'),
|
||||
'invalid-ack'
|
||||
)
|
||||
)
|
||||
expect(invalid).toMatchObject({ ok: false, error: { code: 'invalid_argument' } })
|
||||
expect(harness.db.getMessageById(status.id)?.read).toBe(0)
|
||||
expect(harness.db.getMessageById(question.id)?.read).toBe(0)
|
||||
|
||||
const acknowledged = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{
|
||||
terminal: WORKER_HANDLE,
|
||||
compatibilityAck: JSON.stringify({
|
||||
messageIds: [question.id],
|
||||
types: ['question']
|
||||
})
|
||||
},
|
||||
evidence('worker'),
|
||||
'valid-ack'
|
||||
)
|
||||
)
|
||||
expect(acknowledged).toMatchObject({
|
||||
ok: true,
|
||||
result: { acknowledged: [question.id], legacyCompatibility: { acknowledged: true } }
|
||||
})
|
||||
expect(harness.db.getMessageById(status.id)?.read).toBe(0)
|
||||
expect(harness.db.getMessageById(question.id)?.read).toBe(1)
|
||||
})
|
||||
|
||||
it('rejects invalid legacy check types before attestation or mail consumption', async () => {
|
||||
const harness = createHarness()
|
||||
const message = harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: COORDINATOR_HANDLE,
|
||||
to: WORKER_HANDLE,
|
||||
subject: 'retained status',
|
||||
type: 'status',
|
||||
deliveryContract: 'legacy_direct'
|
||||
})
|
||||
const before = counts(harness.db)
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: WORKER_HANDLE, types: 'status,not-a-message-type' },
|
||||
evidence('worker'),
|
||||
'invalid-types'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: 'invalid_argument', message: 'Invalid --types: not-a-message-type' }
|
||||
})
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
expect(harness.db.getMessageById(message.id)?.read).toBe(0)
|
||||
expect(harness.verify).not.toHaveBeenCalled()
|
||||
expect(harness.notify).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('advertises only Run-addressed current delivery to a legacy coordinator', async () => {
|
||||
const harness = createHarness()
|
||||
harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: 'term_current_worker',
|
||||
to: WORKER_HANDLE,
|
||||
subject: 'worker-only current mail',
|
||||
deliveryContract: 'current_delivery'
|
||||
})
|
||||
const workerOnly = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: COORDINATOR_HANDLE },
|
||||
evidence('coordinator'),
|
||||
'worker-current-mail'
|
||||
)
|
||||
)
|
||||
expect(workerOnly).toMatchObject({
|
||||
ok: true,
|
||||
result: { legacyCompatibility: { currentDelivery: undefined } }
|
||||
})
|
||||
|
||||
harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: 'term_current_worker',
|
||||
to: `run:${harness.adoptedRunId}`,
|
||||
subject: 'Run current mail',
|
||||
deliveryContract: 'current_delivery'
|
||||
})
|
||||
const runMail = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: COORDINATOR_HANDLE },
|
||||
evidence('coordinator'),
|
||||
'run-current-mail'
|
||||
)
|
||||
)
|
||||
expect(runMail).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
legacyCompatibility: {
|
||||
currentDelivery: {
|
||||
runId: harness.adoptedRunId,
|
||||
checkCommand: expect.stringContaining(`--run ${harness.adoptedRunId}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects a legacy coordinator reply outside the adopted Run with zero effects', async () => {
|
||||
const harness = createHarness()
|
||||
const unrelatedRun = harness.db.createRun({
|
||||
objective: 'unrelated current work',
|
||||
coordinatorHandle: 'term_unrelated_coord',
|
||||
coordinatorPaneKey: 'tab_unrelated_coord:55555555-5555-4555-8555-555555555555'
|
||||
})
|
||||
const unrelated = harness.db.insertMessage({
|
||||
runId: unrelatedRun.id,
|
||||
from: 'term_unrelated_worker',
|
||||
to: `run:${unrelatedRun.id}`,
|
||||
subject: 'Unrelated current status',
|
||||
deliveryContract: 'current_delivery'
|
||||
})
|
||||
const before = counts(harness.db)
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.reply',
|
||||
{
|
||||
id: unrelated.id,
|
||||
body: 'Forged cross-Run reply',
|
||||
from: COORDINATOR_HANDLE,
|
||||
run: harness.adoptedRunId
|
||||
},
|
||||
evidence('coordinator'),
|
||||
'cross-run-reply'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
code: 'request_mismatch',
|
||||
data: { effectsApplied: false }
|
||||
}
|
||||
})
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
expect(harness.db.getMessageById(unrelated.id)?.read).toBe(0)
|
||||
expect(harness.notify).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('keeps explicit adopted Run task inspection available to unrelated callers', async () => {
|
||||
const harness = createHarness()
|
||||
const before = counts(harness.db)
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.taskList',
|
||||
{ run: harness.adoptedRunId, callerTerminalHandle: 'term_unrelated' },
|
||||
{
|
||||
terminalHandle: 'term_unrelated',
|
||||
paneKey: 'tab_unrelated:55555555-5555-4555-8555-555555555555',
|
||||
launchToken: 'unrelated-token'
|
||||
},
|
||||
'explicit-adopted-run-inspection'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
runId: harness.adoptedRunId,
|
||||
tasks: [expect.objectContaining({ id: harness.taskId })]
|
||||
}
|
||||
})
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
expect(harness.verify).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it.each(['dispatch', 'websocket'] as const)(
|
||||
'%s binds an attested coordinator once and rejects contradictory proof before binding',
|
||||
async (transport) => {
|
||||
const invalidHarness = createHarness()
|
||||
const invalidBefore = counts(invalidHarness.db)
|
||||
const rejected = await invoke(
|
||||
invalidHarness.dispatcher,
|
||||
request(
|
||||
'orchestration.runUse',
|
||||
{ id: invalidHarness.adoptedRunId, from: COORDINATOR_HANDLE },
|
||||
evidence('coordinator', false),
|
||||
`run-use-invalid-${transport}`
|
||||
),
|
||||
transport
|
||||
)
|
||||
expect(rejected).toMatchObject({ ok: false, error: { code: 'legacy_read_only' } })
|
||||
expect(invalidHarness.db.getRun(invalidHarness.adoptedRunId)?.consumer_generation).toBe(0)
|
||||
expect(counts(invalidHarness.db)).toEqual(invalidBefore)
|
||||
|
||||
const harness = createHarness()
|
||||
const runUse = request(
|
||||
'orchestration.runUse',
|
||||
{ id: harness.adoptedRunId, from: COORDINATOR_HANDLE },
|
||||
evidence('coordinator'),
|
||||
`run-use-${transport}`
|
||||
)
|
||||
const first = await invoke(harness.dispatcher, runUse, transport)
|
||||
const replay = await invoke(
|
||||
harness.dispatcher,
|
||||
{ ...runUse, id: 'rpc_run-use-replay' },
|
||||
transport
|
||||
)
|
||||
expect(first).toMatchObject({
|
||||
ok: true,
|
||||
result: { binding: { consumerGeneration: 1 }, mutation: { replayed: false } }
|
||||
})
|
||||
expect(replay).toMatchObject({
|
||||
ok: true,
|
||||
result: { binding: { consumerGeneration: 1 }, mutation: { replayed: true } }
|
||||
})
|
||||
expect(harness.db.getRun(harness.adoptedRunId)?.consumer_generation).toBe(1)
|
||||
}
|
||||
)
|
||||
|
||||
it('passes trusted coordinator scope to a failing handler without binding or receipts', async () => {
|
||||
const harness = createHarness()
|
||||
const before = counts(harness.db)
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.taskUpdate',
|
||||
{
|
||||
id: 'task_missing',
|
||||
status: 'completed',
|
||||
callerTerminalHandle: COORDINATOR_HANDLE
|
||||
},
|
||||
evidence('coordinator'),
|
||||
'missing-task'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'task_not_found' } })
|
||||
expect(harness.db.getRun(harness.adoptedRunId)?.consumer_generation).toBe(0)
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
})
|
||||
|
||||
it('rejects stale legacy coordinator proof after current takeover', async () => {
|
||||
const harness = createHarness()
|
||||
const principal = harness.db.commitLegacyCompatibilityPrincipal({
|
||||
runId: harness.adoptedRunId,
|
||||
role: 'coordinator',
|
||||
hostScope: JSON.stringify({ kind: 'local', hostId: 'local' }),
|
||||
terminalHandle: COORDINATOR_HANDLE,
|
||||
paneKey: COORDINATOR_PANE,
|
||||
launchTokenHash: createHash('sha256').update('coordinator-token').digest('hex'),
|
||||
processIncarnation: 'process-1'
|
||||
}).principal
|
||||
harness.db.settleWorkerReport({
|
||||
taskId: harness.taskId,
|
||||
dispatchId: harness.dispatchId,
|
||||
outcome: 'succeeded',
|
||||
result: 'done'
|
||||
})
|
||||
harness.db.bindRun({
|
||||
runId: harness.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:55555555-5555-4555-8555-555555555555'
|
||||
})
|
||||
expect(harness.db.getLegacyCompatibilityPrincipal(principal.id)?.status).toBe('revoked')
|
||||
const before = counts(harness.db)
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.taskList',
|
||||
{},
|
||||
evidence('coordinator'),
|
||||
'stale-coordinator-after-takeover'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'legacy_read_only' } })
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,170 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import type { LegacyCoordinatorAuthorityProof, RpcRequest } from './core'
|
||||
import type { OrcaRuntimeService, OrchestrationCompatibilityCallerAuthority } from '../orca-runtime'
|
||||
import { LegacyCompatibilityAuthority } from './orchestration-legacy-authority'
|
||||
import { handleLegacyLifecycleSend } from './orchestration-legacy-lifecycle'
|
||||
import { handleLegacyCheck, handleLegacyReply } from './orchestration-legacy-mail'
|
||||
import { handleLegacyAsk } from './orchestration-legacy-question'
|
||||
import type {
|
||||
LegacyAskParams,
|
||||
LegacyCheckParams,
|
||||
LegacyReplyParams,
|
||||
LegacySendParams
|
||||
} from './orchestration-legacy-operation'
|
||||
import { LegacyCoordinatorAuthority } from './orchestration-legacy-coordinator-authority'
|
||||
|
||||
const COORDINATOR_PREFLIGHT_METHODS = new Set([
|
||||
'orchestration.taskCreate',
|
||||
'orchestration.taskList',
|
||||
'orchestration.taskUpdate',
|
||||
'orchestration.dispatch',
|
||||
'orchestration.gateCreate',
|
||||
'orchestration.gateResolve',
|
||||
'orchestration.runUse',
|
||||
'orchestration.send',
|
||||
'orchestration.check',
|
||||
'orchestration.reply'
|
||||
])
|
||||
|
||||
export type LegacyCompatibilityRoute =
|
||||
| { handled: true; result: unknown }
|
||||
| {
|
||||
handled: false
|
||||
params?: unknown
|
||||
legacyCoordinatorAuthority?: LegacyCoordinatorAuthorityProof
|
||||
orchestrationCompatibilityCallerAuthority?: OrchestrationCompatibilityCallerAuthority
|
||||
}
|
||||
|
||||
export type LegacyCoordinatorInvocation = Readonly<{
|
||||
authority: LegacyCoordinatorAuthorityProof
|
||||
mutationCallerFingerprint: string
|
||||
revalidate: () => string
|
||||
}>
|
||||
|
||||
export class OrchestrationLegacyCompatibility {
|
||||
private readonly authority: LegacyCompatibilityAuthority
|
||||
private readonly coordinatorAuthority: LegacyCoordinatorAuthority
|
||||
|
||||
constructor(private readonly runtime: OrcaRuntimeService) {
|
||||
this.authority = new LegacyCompatibilityAuthority(runtime)
|
||||
this.coordinatorAuthority = new LegacyCoordinatorAuthority(runtime)
|
||||
}
|
||||
|
||||
async tryHandle(
|
||||
request: RpcRequest,
|
||||
params: unknown,
|
||||
signal?: AbortSignal
|
||||
): Promise<LegacyCompatibilityRoute> {
|
||||
if (!request.method.startsWith('orchestration.')) {
|
||||
return { handled: false }
|
||||
}
|
||||
const result = await this.route(request, params, signal)
|
||||
if (result !== undefined) {
|
||||
return { handled: true, result }
|
||||
}
|
||||
if (!COORDINATOR_PREFLIGHT_METHODS.has(request.method)) {
|
||||
return { handled: false }
|
||||
}
|
||||
const values = params as Record<string, unknown>
|
||||
if (request.method === 'orchestration.runUse' && values.takeoverLegacy === true) {
|
||||
const callerAuthority = this.runtime.verifyOrchestrationCompatibilityCaller(
|
||||
request.orchestrationCompatibilityEvidence
|
||||
)
|
||||
return {
|
||||
handled: false,
|
||||
...(callerAuthority ? { orchestrationCompatibilityCallerAuthority: callerAuthority } : {})
|
||||
}
|
||||
}
|
||||
const requestedRunId =
|
||||
request.method === 'orchestration.runUse' ? stringValue(values.id) : stringValue(values.run)
|
||||
if (request.method === 'orchestration.taskList' && requestedRunId) {
|
||||
return { handled: false }
|
||||
}
|
||||
const authority = this.coordinatorAuthority.resolve(request, requestedRunId)
|
||||
return authority
|
||||
? {
|
||||
handled: false,
|
||||
params: { ...values, run: authority.runId },
|
||||
legacyCoordinatorAuthority: authority
|
||||
}
|
||||
: { handled: false }
|
||||
}
|
||||
|
||||
revalidateCoordinatorAuthority(
|
||||
request: RpcRequest,
|
||||
proof: LegacyCoordinatorAuthorityProof
|
||||
): string {
|
||||
return this.coordinatorAuthority.revalidate(request, proof)
|
||||
}
|
||||
|
||||
createCoordinatorInvocation(
|
||||
request: RpcRequest,
|
||||
authority?: LegacyCoordinatorAuthorityProof
|
||||
): LegacyCoordinatorInvocation | undefined {
|
||||
return authority
|
||||
? {
|
||||
authority,
|
||||
mutationCallerFingerprint: legacyCoordinatorMutationCallerFingerprint(authority),
|
||||
revalidate: () => this.revalidateCoordinatorAuthority(request, authority)
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
|
||||
private async route(
|
||||
request: RpcRequest,
|
||||
params: unknown,
|
||||
signal?: AbortSignal
|
||||
): Promise<unknown | undefined> {
|
||||
if (request.method === 'orchestration.send') {
|
||||
return await handleLegacyLifecycleSend({
|
||||
runtime: this.runtime,
|
||||
authority: this.authority,
|
||||
request,
|
||||
params: params as LegacySendParams
|
||||
})
|
||||
}
|
||||
if (request.method === 'orchestration.check') {
|
||||
return await handleLegacyCheck({
|
||||
runtime: this.runtime,
|
||||
authority: this.authority,
|
||||
request,
|
||||
params: params as LegacyCheckParams,
|
||||
signal
|
||||
})
|
||||
}
|
||||
if (request.method === 'orchestration.ask') {
|
||||
return await handleLegacyAsk({
|
||||
runtime: this.runtime,
|
||||
authority: this.authority,
|
||||
request,
|
||||
params: params as LegacyAskParams,
|
||||
signal
|
||||
})
|
||||
}
|
||||
if (request.method === 'orchestration.reply') {
|
||||
return await handleLegacyReply({
|
||||
runtime: this.runtime,
|
||||
authority: this.authority,
|
||||
request,
|
||||
params: params as LegacyReplyParams
|
||||
})
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
function legacyCoordinatorMutationCallerFingerprint(
|
||||
authority: LegacyCoordinatorAuthorityProof
|
||||
): string {
|
||||
return createHash('sha256')
|
||||
.update(
|
||||
['legacy-coordinator-v1', authority.runId, authority.terminalHandle, authority.paneKey].join(
|
||||
'\0'
|
||||
)
|
||||
)
|
||||
.digest('hex')
|
||||
}
|
||||
|
||||
function stringValue(value: unknown): string | undefined {
|
||||
return typeof value === 'string' && value.length > 0 ? value : undefined
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import type { OrcaRuntimeService, OrchestrationCompatibilityCallerAuthority } from '../orca-runtime'
|
||||
import type { LegacyCompatibilityPrincipalRow } from '../orchestration/types'
|
||||
import type { LegacyCoordinatorAuthorityProof, RpcRequest } from './core'
|
||||
import {
|
||||
resolveAttestedLegacyPrincipal,
|
||||
verifyAttestedLegacyCandidate,
|
||||
type LegacyPrincipalCandidate
|
||||
} from './orchestration-legacy-authority'
|
||||
import {
|
||||
equivalentLegacyPaneKey,
|
||||
legacyCoordinatorReadOnly
|
||||
} from './orchestration-legacy-process-identity'
|
||||
|
||||
export class LegacyCoordinatorAuthority {
|
||||
constructor(private readonly runtime: OrcaRuntimeService) {}
|
||||
|
||||
resolve(
|
||||
request: RpcRequest,
|
||||
requestedRunId?: string
|
||||
): LegacyCoordinatorAuthorityProof | undefined {
|
||||
const db = this.runtime.getOrchestrationDb()
|
||||
const adoption = db.getLegacyAdoption()
|
||||
const requestedRun = requestedRunId ?? adoption?.adopted_run_id
|
||||
if (!adoption || requestedRun !== adoption.adopted_run_id) {
|
||||
return undefined
|
||||
}
|
||||
const candidate = db.resolveLegacyCoordinatorCandidate({
|
||||
runId: adoption.adopted_run_id,
|
||||
terminalHandle: request.orchestrationCompatibilityEvidence?.terminalHandle,
|
||||
paneKey: request.orchestrationCompatibilityEvidence?.paneKey
|
||||
})
|
||||
if (!candidate) {
|
||||
if (request.orchestrationCompatibilityEvidence) {
|
||||
const caller = this.runtime.verifyOrchestrationCompatibilityCaller(
|
||||
request.orchestrationCompatibilityEvidence
|
||||
)
|
||||
const run = db.getRun(adoption.adopted_run_id)
|
||||
if (
|
||||
caller &&
|
||||
run?.coordinator_handle === caller.terminalHandle &&
|
||||
run.coordinator_pane_key &&
|
||||
equivalentLegacyPaneKey(run.coordinator_pane_key, caller.paneKey)
|
||||
) {
|
||||
return undefined
|
||||
}
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
const existing = db.getLegacyCoordinatorPrincipal(adoption.adopted_run_id)
|
||||
const proofCandidate = existing
|
||||
? this.candidate(adoption.adopted_run_id, existing)
|
||||
: this.candidate(adoption.adopted_run_id, candidate)
|
||||
const attestation = verifyAttestedLegacyCandidate({
|
||||
runtime: this.runtime,
|
||||
evidence: request.orchestrationCompatibilityEvidence,
|
||||
candidate: proofCandidate
|
||||
})
|
||||
const run = db.getRun(adoption.adopted_run_id)
|
||||
if (!run || !this.bindingMatches(run, proofCandidate)) {
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
if (existing && !this.principalMatchesAttestation(existing, attestation)) {
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
const principal = existing
|
||||
? existing
|
||||
: request.method === 'orchestration.runUse'
|
||||
? resolveAttestedLegacyPrincipal({
|
||||
runtime: this.runtime,
|
||||
evidence: request.orchestrationCompatibilityEvidence,
|
||||
candidate: proofCandidate,
|
||||
authority: attestation
|
||||
})
|
||||
: undefined
|
||||
return {
|
||||
runId: run.id,
|
||||
principalId: principal?.id ?? null,
|
||||
terminalHandle: proofCandidate.terminalHandle,
|
||||
paneKey: proofCandidate.paneKey,
|
||||
consumerGeneration: run.consumer_generation
|
||||
}
|
||||
}
|
||||
|
||||
revalidate(request: RpcRequest, proof: LegacyCoordinatorAuthorityProof): string {
|
||||
const db = this.runtime.getOrchestrationDb()
|
||||
const adoption = db.getLegacyAdoption()
|
||||
const run = db.getRun(proof.runId)
|
||||
if (
|
||||
adoption?.adopted_run_id !== proof.runId ||
|
||||
!run ||
|
||||
run.consumer_generation !== proof.consumerGeneration
|
||||
) {
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
const candidate = this.candidate(proof.runId, proof)
|
||||
const principal = db.getLegacyCoordinatorPrincipal(proof.runId)
|
||||
if (proof.principalId) {
|
||||
if (
|
||||
principal?.id !== proof.principalId ||
|
||||
principal.status !== 'committed' ||
|
||||
principal.terminal_handle !== proof.terminalHandle ||
|
||||
!equivalentLegacyPaneKey(principal.pane_key, proof.paneKey)
|
||||
) {
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
} else if (
|
||||
principal &&
|
||||
(principal.status !== 'committed' ||
|
||||
principal.terminal_handle !== proof.terminalHandle ||
|
||||
!equivalentLegacyPaneKey(principal.pane_key, proof.paneKey))
|
||||
) {
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
const attestation = verifyAttestedLegacyCandidate({
|
||||
runtime: this.runtime,
|
||||
evidence: request.orchestrationCompatibilityEvidence,
|
||||
candidate
|
||||
})
|
||||
if (principal && !this.principalMatchesAttestation(principal, attestation)) {
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
if (!this.bindingMatches(run, candidate)) {
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
return run.id
|
||||
}
|
||||
|
||||
private candidate(
|
||||
runId: string,
|
||||
identity: {
|
||||
terminalHandle?: string
|
||||
terminal_handle?: string
|
||||
paneKey?: string
|
||||
pane_key?: string
|
||||
}
|
||||
): LegacyPrincipalCandidate {
|
||||
return {
|
||||
runId,
|
||||
role: 'coordinator',
|
||||
terminalHandle: identity.terminalHandle ?? (identity.terminal_handle as string),
|
||||
paneKey: identity.paneKey ?? (identity.pane_key as string)
|
||||
}
|
||||
}
|
||||
|
||||
private bindingMatches(
|
||||
run: { coordinator_handle: string | null; coordinator_pane_key: string | null },
|
||||
candidate: LegacyPrincipalCandidate
|
||||
): boolean {
|
||||
return (
|
||||
!run.coordinator_pane_key ||
|
||||
(run.coordinator_handle === candidate.terminalHandle &&
|
||||
equivalentLegacyPaneKey(run.coordinator_pane_key, candidate.paneKey))
|
||||
)
|
||||
}
|
||||
|
||||
private principalMatchesAttestation(
|
||||
principal: LegacyCompatibilityPrincipalRow,
|
||||
attestation: OrchestrationCompatibilityCallerAuthority
|
||||
): boolean {
|
||||
return (
|
||||
principal.status === 'committed' &&
|
||||
principal.host_scope === JSON.stringify(attestation.hostScope) &&
|
||||
principal.terminal_handle === attestation.terminalHandle &&
|
||||
equivalentLegacyPaneKey(principal.pane_key, attestation.paneKey) &&
|
||||
principal.launch_token_hash === attestation.launchTokenHash &&
|
||||
principal.process_incarnation === attestation.processIncarnation
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,664 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { mkdtempSync, rmSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../shared/protocol-version'
|
||||
import type { OrchestrationCompatibilityEvidence } from '../../../shared/orchestration-compatibility-evidence'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import { OrcaRuntimeService } from '../orca-runtime'
|
||||
import { OrchestrationDb } from '../orchestration/db'
|
||||
import type { RpcRequest, RpcResponse } from './core'
|
||||
import { RpcDispatcher } from './dispatcher'
|
||||
import { ORCHESTRATION_METHODS } from './methods/orchestration'
|
||||
|
||||
const COORDINATOR_HANDLE = 'term_legacy_coord'
|
||||
const COORDINATOR_PANE = 'tab_coord:44444444-4444-4444-8444-444444444444'
|
||||
const WORKER_HANDLE = 'term_legacy_worker'
|
||||
const WORKER_PANE = 'tab_worker:33333333-3333-4333-8333-333333333333'
|
||||
|
||||
type Transport = 'dispatch' | 'websocket'
|
||||
|
||||
type Harness = {
|
||||
db: OrchestrationDb
|
||||
dispatcher: RpcDispatcher
|
||||
runtime: OrcaRuntimeService
|
||||
adoptedRunId: string
|
||||
taskId: string
|
||||
dispatchId: string
|
||||
}
|
||||
|
||||
const tempDirs: string[] = []
|
||||
const databases: OrchestrationDb[] = []
|
||||
|
||||
afterEach(() => {
|
||||
databases.splice(0).forEach((database) => database.close())
|
||||
tempDirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true }))
|
||||
})
|
||||
|
||||
function createHarness(): Harness {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'orca-legacy-coordinator-race-'))
|
||||
tempDirs.push(dir)
|
||||
const dbPath = join(dir, 'orchestration.db')
|
||||
const before = new OrchestrationDb(dbPath)
|
||||
const task = before.createTask({
|
||||
spec: 'legacy assignment',
|
||||
createdByTerminalHandle: COORDINATOR_HANDLE
|
||||
})
|
||||
const dispatch = before.createDispatchContext(task.id, WORKER_HANDLE, WORKER_PANE)
|
||||
before.close()
|
||||
|
||||
const raw = new Database(dbPath)
|
||||
raw.exec(`
|
||||
UPDATE dispatch_contexts SET process_incarnation = 'process-1';
|
||||
DROP INDEX IF EXISTS idx_messages_delivery_contract;
|
||||
DROP TABLE legacy_mail_receipts;
|
||||
DROP TABLE legacy_operation_receipts;
|
||||
DROP TABLE legacy_compatibility_principals;
|
||||
DROP TABLE legacy_adoptions;
|
||||
`)
|
||||
raw.pragma('user_version = 18')
|
||||
raw.close()
|
||||
|
||||
const db = new OrchestrationDb(dbPath)
|
||||
databases.push(db)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
const runtime = new OrcaRuntimeService()
|
||||
runtime.setOrchestrationDb(db)
|
||||
vi.spyOn(runtime, 'getTerminalPaneKey').mockImplementation((handle) =>
|
||||
handle === COORDINATOR_HANDLE ? COORDINATOR_PANE : handle === WORKER_HANDLE ? WORKER_PANE : null
|
||||
)
|
||||
vi.spyOn(runtime, 'verifyOrchestrationCompatibilityCaller').mockImplementation(
|
||||
(compatibilityEvidence) => {
|
||||
const coordinator =
|
||||
compatibilityEvidence?.terminalHandle === COORDINATOR_HANDLE &&
|
||||
compatibilityEvidence.paneKey === COORDINATOR_PANE &&
|
||||
compatibilityEvidence.launchToken === 'coordinator-token'
|
||||
const worker =
|
||||
compatibilityEvidence?.terminalHandle === WORKER_HANDLE &&
|
||||
compatibilityEvidence.paneKey === WORKER_PANE &&
|
||||
compatibilityEvidence.launchToken === 'worker-token'
|
||||
return coordinator || worker
|
||||
? {
|
||||
hostScope: { kind: 'local', hostId: 'local' },
|
||||
terminalHandle: coordinator ? COORDINATOR_HANDLE : WORKER_HANDLE,
|
||||
paneKey: coordinator ? COORDINATOR_PANE : WORKER_PANE,
|
||||
processIncarnation: 'process-1',
|
||||
launchTokenHash: createHash('sha256')
|
||||
.update(coordinator ? 'coordinator-token' : 'worker-token')
|
||||
.digest('hex')
|
||||
}
|
||||
: null
|
||||
}
|
||||
)
|
||||
return {
|
||||
db,
|
||||
dispatcher: new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS }),
|
||||
runtime,
|
||||
adoptedRunId,
|
||||
taskId: task.id,
|
||||
dispatchId: dispatch.id
|
||||
}
|
||||
}
|
||||
|
||||
function evidence(
|
||||
role: 'coordinator' | 'worker' = 'coordinator'
|
||||
): OrchestrationCompatibilityEvidence {
|
||||
return {
|
||||
terminalHandle: role === 'coordinator' ? COORDINATOR_HANDLE : WORKER_HANDLE,
|
||||
paneKey: role === 'coordinator' ? COORDINATOR_PANE : WORKER_PANE,
|
||||
launchToken: role === 'coordinator' ? 'coordinator-token' : 'worker-token'
|
||||
}
|
||||
}
|
||||
|
||||
function request(
|
||||
method: string,
|
||||
params: unknown,
|
||||
invocationId: string,
|
||||
callerEvidence = evidence()
|
||||
): RpcRequest {
|
||||
return {
|
||||
id: `rpc_${invocationId}`,
|
||||
authToken: 'caller-token',
|
||||
method,
|
||||
params,
|
||||
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
|
||||
orchestrationRequestId: invocationId,
|
||||
compatibilityInvocationId: invocationId,
|
||||
orchestrationCompatibilityEvidence: callerEvidence
|
||||
}
|
||||
}
|
||||
|
||||
async function invoke(
|
||||
dispatcher: RpcDispatcher,
|
||||
rpcRequest: RpcRequest,
|
||||
transport: Transport
|
||||
): Promise<RpcResponse> {
|
||||
if (transport === 'dispatch') {
|
||||
return await dispatcher.dispatch(rpcRequest)
|
||||
}
|
||||
const replies: string[] = []
|
||||
await dispatcher.dispatchStreaming(rpcRequest, (reply) => replies.push(reply))
|
||||
expect(replies).toHaveLength(1)
|
||||
return JSON.parse(replies[0]) as RpcResponse
|
||||
}
|
||||
|
||||
function installTakeoverDuringMutationPreflight(harness: Harness): void {
|
||||
const originalBegin = harness.db.beginMutationReceipt.bind(harness.db)
|
||||
vi.spyOn(harness.db, 'beginMutationReceipt').mockImplementation((identity) => {
|
||||
const begun = originalBegin(identity)
|
||||
settleLegacyWorkerAndTakeOver(harness, 'settled during takeover')
|
||||
return begun
|
||||
})
|
||||
}
|
||||
|
||||
function settleLegacyWorkerAndTakeOver(harness: Harness, result: string): void {
|
||||
harness.db.settleWorkerReport({
|
||||
taskId: harness.taskId,
|
||||
dispatchId: harness.dispatchId,
|
||||
outcome: 'succeeded',
|
||||
result
|
||||
})
|
||||
harness.db.bindRun({
|
||||
runId: harness.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:55555555-5555-4555-8555-555555555555'
|
||||
})
|
||||
}
|
||||
|
||||
function mutationReceiptCount(db: OrchestrationDb): number {
|
||||
const sqlite = (db as unknown as { db: Database.Database }).db
|
||||
return (
|
||||
sqlite.prepare('SELECT COUNT(*) AS count FROM mutation_receipts').get() as { count: number }
|
||||
).count
|
||||
}
|
||||
|
||||
function messageCount(db: OrchestrationDb): number {
|
||||
const sqlite = (db as unknown as { db: Database.Database }).db
|
||||
return (sqlite.prepare('SELECT COUNT(*) AS count FROM messages').get() as { count: number }).count
|
||||
}
|
||||
|
||||
describe('legacy coordinator takeover races', () => {
|
||||
it.each(['dispatch', 'websocket'] as const)(
|
||||
'%s routes a pre-bind coordinator send to the retained worker mailbox',
|
||||
async (transport) => {
|
||||
const harness = createHarness()
|
||||
|
||||
const response = await invoke(
|
||||
harness.dispatcher,
|
||||
request(
|
||||
'orchestration.send',
|
||||
{ from: COORDINATOR_HANDLE, to: WORKER_HANDLE, subject: 'continue safely' },
|
||||
`send-direct-${transport}`
|
||||
),
|
||||
transport
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: {
|
||||
run_id: harness.adoptedRunId,
|
||||
to_handle: WORKER_HANDLE,
|
||||
delivery_contract: 'legacy_direct'
|
||||
}
|
||||
}
|
||||
})
|
||||
const workerCheck = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: WORKER_HANDLE },
|
||||
`worker-check-${transport}`,
|
||||
evidence('worker')
|
||||
)
|
||||
)
|
||||
expect(workerCheck).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
messages: [
|
||||
{
|
||||
subject: 'continue safely',
|
||||
run_id: harness.adoptedRunId,
|
||||
delivery_contract: 'legacy_direct'
|
||||
}
|
||||
],
|
||||
legacyCompatibility: { ackMessageIds: [expect.any(String)] }
|
||||
}
|
||||
})
|
||||
expect(
|
||||
harness.db.getInbox(100).some((message) => message.run_id === 'run_legacy_local')
|
||||
).toBe(false)
|
||||
}
|
||||
)
|
||||
|
||||
it('replays coordinator mutations after transport authentication rotates', async () => {
|
||||
const harness = createHarness()
|
||||
const mutation = request(
|
||||
'orchestration.send',
|
||||
{ from: COORDINATOR_HANDLE, to: WORKER_HANDLE, subject: 'send once' },
|
||||
'coordinator-restart-send'
|
||||
)
|
||||
mutation.authToken = 'before-restart'
|
||||
|
||||
const first = await harness.dispatcher.dispatch(mutation)
|
||||
const restartedDispatcher = new RpcDispatcher({
|
||||
runtime: harness.runtime,
|
||||
methods: ORCHESTRATION_METHODS
|
||||
})
|
||||
const replay = await restartedDispatcher.dispatch({
|
||||
...mutation,
|
||||
id: 'rpc_coordinator-restart-send-retry',
|
||||
authToken: 'after-restart'
|
||||
})
|
||||
|
||||
expect(first).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: { subject: 'send once' },
|
||||
mutation: { requestId: 'coordinator-restart-send', replayed: false }
|
||||
}
|
||||
})
|
||||
expect(replay).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: { id: (first as { result: { message: { id: string } } }).result.message.id },
|
||||
mutation: { requestId: 'coordinator-restart-send', replayed: true }
|
||||
}
|
||||
})
|
||||
expect(messageCount(harness.db)).toBe(1)
|
||||
expect(mutationReceiptCount(harness.db)).toBe(1)
|
||||
})
|
||||
|
||||
it('routes an exact legacy Dispatch recipient to compatibility delivery', async () => {
|
||||
const harness = createHarness()
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{
|
||||
from: COORDINATOR_HANDLE,
|
||||
to: `dispatch:${harness.dispatchId}`,
|
||||
subject: 'dispatch guidance'
|
||||
},
|
||||
'send-dispatch'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: {
|
||||
run_id: harness.adoptedRunId,
|
||||
to_handle: `dispatch:${harness.dispatchId}`,
|
||||
delivery_contract: 'legacy_direct'
|
||||
}
|
||||
}
|
||||
})
|
||||
const workerCheck = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: WORKER_HANDLE },
|
||||
'worker-check-dispatch',
|
||||
evidence('worker')
|
||||
)
|
||||
)
|
||||
expect(workerCheck).toMatchObject({
|
||||
ok: true,
|
||||
result: { messages: [{ subject: 'dispatch guidance' }] }
|
||||
})
|
||||
})
|
||||
|
||||
it('partitions a coordinator group send by legacy recipient contract', async () => {
|
||||
const harness = createHarness()
|
||||
vi.spyOn(harness.runtime, 'listTerminals').mockResolvedValue({
|
||||
terminals: [
|
||||
{ handle: COORDINATOR_HANDLE },
|
||||
{ handle: WORKER_HANDLE },
|
||||
{ handle: 'term_current_worker' }
|
||||
],
|
||||
totalCount: 3,
|
||||
truncated: false
|
||||
} as never)
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{ from: COORDINATOR_HANDLE, to: '@all', subject: 'group guidance' },
|
||||
'send-group'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: true, result: { recipients: 2 } })
|
||||
const messages = harness.db
|
||||
.getInbox(100)
|
||||
.filter((message) => message.subject === 'group guidance')
|
||||
expect(messages).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
run_id: harness.adoptedRunId,
|
||||
to_handle: WORKER_HANDLE,
|
||||
delivery_contract: 'legacy_direct'
|
||||
}),
|
||||
expect.objectContaining({
|
||||
run_id: harness.adoptedRunId,
|
||||
to_handle: 'term_current_worker',
|
||||
delivery_contract: 'current_delivery'
|
||||
})
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it.each(['dispatch', 'websocket'] as const)(
|
||||
'%s rejects a coordinator send after takeover during durable preflight',
|
||||
async (transport) => {
|
||||
const harness = createHarness()
|
||||
installTakeoverDuringMutationPreflight(harness)
|
||||
const before = messageCount(harness.db)
|
||||
|
||||
const response = await invoke(
|
||||
harness.dispatcher,
|
||||
request(
|
||||
'orchestration.send',
|
||||
{ from: COORDINATOR_HANDLE, to: WORKER_HANDLE, subject: 'must not send' },
|
||||
`send-takeover-${transport}`
|
||||
),
|
||||
transport
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'legacy_read_only' } })
|
||||
expect(messageCount(harness.db)).toBe(before)
|
||||
expect(mutationReceiptCount(harness.db)).toBe(0)
|
||||
}
|
||||
)
|
||||
|
||||
it('revalidates a group send after asynchronous terminal discovery', async () => {
|
||||
const harness = createHarness()
|
||||
let resolveTerminals:
|
||||
| ((value: {
|
||||
terminals: { handle: string }[]
|
||||
totalCount: number
|
||||
truncated: boolean
|
||||
}) => void)
|
||||
| undefined
|
||||
let signalListingStarted: (() => void) | undefined
|
||||
const listingStarted = new Promise<void>((resolve) => {
|
||||
signalListingStarted = resolve
|
||||
})
|
||||
vi.spyOn(harness.runtime, 'listTerminals').mockImplementation(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
resolveTerminals = resolve as typeof resolveTerminals
|
||||
signalListingStarted?.()
|
||||
}) as never
|
||||
)
|
||||
const before = messageCount(harness.db)
|
||||
const pending = harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{ from: COORDINATOR_HANDLE, to: '@all', subject: 'must remain unsent' },
|
||||
'send-group-takeover'
|
||||
)
|
||||
)
|
||||
await listingStarted
|
||||
settleLegacyWorkerAndTakeOver(harness, 'settled during terminal discovery')
|
||||
resolveTerminals?.({
|
||||
terminals: [{ handle: COORDINATOR_HANDLE }, { handle: WORKER_HANDLE }],
|
||||
totalCount: 2,
|
||||
truncated: false
|
||||
})
|
||||
|
||||
await expect(pending).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { code: 'legacy_read_only' }
|
||||
})
|
||||
expect(messageCount(harness.db)).toBe(before)
|
||||
expect(mutationReceiptCount(harness.db)).toBe(0)
|
||||
})
|
||||
|
||||
it('reports a committed ACK when coordinator takeover interrupts its wait', async () => {
|
||||
const harness = createHarness()
|
||||
const incoming = harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: WORKER_HANDLE,
|
||||
to: `run:${harness.adoptedRunId}`,
|
||||
subject: 'ready to acknowledge'
|
||||
})
|
||||
const first = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: COORDINATOR_HANDLE, run: harness.adoptedRunId },
|
||||
'check-delivery-before-takeover'
|
||||
)
|
||||
)
|
||||
expect(first).toMatchObject({
|
||||
ok: true,
|
||||
result: { deliveryId: expect.any(String), messages: [{ id: incoming.id }] }
|
||||
})
|
||||
const deliveryId = (first as { result: { deliveryId: string } }).result.deliveryId
|
||||
let resolveWait: (() => void) | undefined
|
||||
let signalWaitStarted: (() => void) | undefined
|
||||
const waitStarted = new Promise<void>((resolve) => {
|
||||
signalWaitStarted = resolve
|
||||
})
|
||||
vi.spyOn(harness.runtime, 'waitForMessage').mockImplementation(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
resolveWait = () => resolve('notified')
|
||||
signalWaitStarted?.()
|
||||
})
|
||||
)
|
||||
const pending = harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{
|
||||
terminal: COORDINATOR_HANDLE,
|
||||
run: harness.adoptedRunId,
|
||||
ack: deliveryId,
|
||||
wait: true,
|
||||
timeoutMs: 10_000
|
||||
},
|
||||
'check-ack-takeover'
|
||||
)
|
||||
)
|
||||
await waitStarted
|
||||
expect(harness.db.getMessageById(incoming.id)?.read).toBe(1)
|
||||
settleLegacyWorkerAndTakeOver(harness, 'settled during acknowledged wait')
|
||||
resolveWait?.()
|
||||
|
||||
await expect(pending).resolves.toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
acknowledged: deliveryId,
|
||||
count: 0,
|
||||
waitInterrupted: 'consumer_fenced',
|
||||
mutation: { requestId: 'check-ack-takeover', replayed: false }
|
||||
}
|
||||
})
|
||||
const sqlite = (harness.db as unknown as { db: Database.Database }).db
|
||||
expect(sqlite.prepare('SELECT state FROM mutation_receipts').get()).toEqual({
|
||||
state: 'completed'
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps concurrent duplicates on the live ACK-and-wait result', async () => {
|
||||
const harness = createHarness()
|
||||
harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: WORKER_HANDLE,
|
||||
to: `run:${harness.adoptedRunId}`,
|
||||
subject: 'first delivery'
|
||||
})
|
||||
const firstCheck = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: COORDINATOR_HANDLE, run: harness.adoptedRunId },
|
||||
'check-first-delivery'
|
||||
)
|
||||
)
|
||||
const deliveryId = (firstCheck as { result: { deliveryId: string } }).result.deliveryId
|
||||
let resolveWait: (() => void) | undefined
|
||||
let signalWaitStarted: (() => void) | undefined
|
||||
const waitStarted = new Promise<void>((resolve) => {
|
||||
signalWaitStarted = resolve
|
||||
})
|
||||
vi.spyOn(harness.runtime, 'waitForMessage').mockImplementation(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
resolveWait = () => resolve('notified')
|
||||
signalWaitStarted?.()
|
||||
})
|
||||
)
|
||||
const ackRequest = request(
|
||||
'orchestration.check',
|
||||
{
|
||||
terminal: COORDINATOR_HANDLE,
|
||||
run: harness.adoptedRunId,
|
||||
ack: deliveryId,
|
||||
wait: true,
|
||||
timeoutMs: 10_000
|
||||
},
|
||||
'check-ack-concurrent'
|
||||
)
|
||||
const original = harness.dispatcher.dispatch(ackRequest)
|
||||
await waitStarted
|
||||
const secondLiveDispatcher = new RpcDispatcher({
|
||||
runtime: harness.runtime,
|
||||
methods: ORCHESTRATION_METHODS
|
||||
})
|
||||
const duplicate = secondLiveDispatcher.dispatch({
|
||||
...ackRequest,
|
||||
id: 'rpc_check-ack-concurrent-duplicate'
|
||||
})
|
||||
let duplicateSettled = false
|
||||
void duplicate.then(() => {
|
||||
duplicateSettled = true
|
||||
})
|
||||
await Promise.resolve()
|
||||
expect(duplicateSettled).toBe(false)
|
||||
|
||||
const arrived = harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: WORKER_HANDLE,
|
||||
to: `run:${harness.adoptedRunId}`,
|
||||
subject: 'arrived while waiting'
|
||||
})
|
||||
resolveWait?.()
|
||||
const [originalResult, duplicateResult] = await Promise.all([original, duplicate])
|
||||
expect(originalResult).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
messages: [{ id: arrived.id }],
|
||||
mutation: { requestId: 'check-ack-concurrent', replayed: false }
|
||||
}
|
||||
})
|
||||
expect(duplicateResult).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
messages: [{ id: arrived.id }],
|
||||
mutation: { requestId: 'check-ack-concurrent', replayed: true }
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it.each(['dispatch', 'websocket'] as const)(
|
||||
'%s rejects run-use after takeover during durable preflight',
|
||||
async (transport) => {
|
||||
const harness = createHarness()
|
||||
installTakeoverDuringMutationPreflight(harness)
|
||||
|
||||
const response = await invoke(
|
||||
harness.dispatcher,
|
||||
request(
|
||||
'orchestration.runUse',
|
||||
{ id: harness.adoptedRunId, from: COORDINATOR_HANDLE },
|
||||
`run-use-takeover-${transport}`
|
||||
),
|
||||
transport
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'legacy_read_only' } })
|
||||
expect(harness.db.getRun(harness.adoptedRunId)).toMatchObject({
|
||||
coordinator_handle: 'term_current_coord',
|
||||
consumer_generation: 1
|
||||
})
|
||||
expect(mutationReceiptCount(harness.db)).toBe(0)
|
||||
expect(harness.db.getLegacyCoordinatorPrincipal(harness.adoptedRunId)?.status).toBe('revoked')
|
||||
}
|
||||
)
|
||||
|
||||
it.each(['dispatch', 'websocket'] as const)(
|
||||
'%s rejects a mutation after takeover during durable preflight',
|
||||
async (transport) => {
|
||||
const harness = createHarness()
|
||||
const target = harness.db.createTask({
|
||||
spec: 'must remain ready',
|
||||
runId: harness.adoptedRunId
|
||||
})
|
||||
installTakeoverDuringMutationPreflight(harness)
|
||||
|
||||
const response = await invoke(
|
||||
harness.dispatcher,
|
||||
request(
|
||||
'orchestration.taskUpdate',
|
||||
{
|
||||
id: target.id,
|
||||
status: 'completed',
|
||||
callerTerminalHandle: COORDINATOR_HANDLE
|
||||
},
|
||||
`task-update-takeover-${transport}`
|
||||
),
|
||||
transport
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'legacy_read_only' } })
|
||||
expect(harness.db.getTask(target.id)?.status).toBe('ready')
|
||||
expect(mutationReceiptCount(harness.db)).toBe(0)
|
||||
}
|
||||
)
|
||||
|
||||
it('revalidates after asynchronous agent detection', async () => {
|
||||
const harness = createHarness()
|
||||
const target = harness.db.createTask({
|
||||
spec: 'must not dispatch after takeover',
|
||||
runId: harness.adoptedRunId
|
||||
})
|
||||
const targetHandle = 'term_current_worker'
|
||||
const targetPane = 'tab_current_worker:77777777-7777-4777-8777-777777777777'
|
||||
vi.spyOn(harness.runtime, 'getTerminalPaneKey').mockImplementation((handle: string) =>
|
||||
handle === COORDINATOR_HANDLE ? COORDINATOR_PANE : handle === targetHandle ? targetPane : null
|
||||
)
|
||||
vi.spyOn(harness.runtime, 'getTerminalProcessIncarnation').mockReturnValue('current-process')
|
||||
let resolveDetection: ((detected: boolean) => void) | undefined
|
||||
let signalDetectionStarted: (() => void) | undefined
|
||||
const detectionStarted = new Promise<void>((resolve) => {
|
||||
signalDetectionStarted = resolve
|
||||
})
|
||||
vi.spyOn(harness.runtime, 'isTerminalRunningAgent').mockImplementation(
|
||||
() =>
|
||||
new Promise<boolean>((resolve) => {
|
||||
resolveDetection = resolve
|
||||
signalDetectionStarted?.()
|
||||
})
|
||||
)
|
||||
const sendPrompt = vi
|
||||
.spyOn(harness.runtime, 'sendTerminalAgentPrompt')
|
||||
.mockResolvedValue({ handle: targetHandle, accepted: true, bytesWritten: 1 })
|
||||
|
||||
const pending = harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.dispatch',
|
||||
{ task: target.id, to: targetHandle, from: COORDINATOR_HANDLE, inject: true },
|
||||
'dispatch-detection-takeover'
|
||||
)
|
||||
)
|
||||
await detectionStarted
|
||||
settleLegacyWorkerAndTakeOver(harness, 'settled during detection')
|
||||
resolveDetection?.(true)
|
||||
const response = await pending
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'legacy_read_only' } })
|
||||
expect(harness.db.getTask(target.id)?.status).toBe('ready')
|
||||
expect(harness.db.getDispatchContext(target.id)).toBeUndefined()
|
||||
expect(sendPrompt).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,114 @@
|
||||
import type { RpcRequest } from './core'
|
||||
import type { OrcaRuntimeService } from '../orca-runtime'
|
||||
import type { MessageType } from '../orchestration/db'
|
||||
import { OrchestrationError } from '../orchestration/orchestration-error'
|
||||
import type { LegacyCompatibilityAuthority } from './orchestration-legacy-authority'
|
||||
import {
|
||||
inferLegacyWorkerOutcome,
|
||||
isWorkerOutcome,
|
||||
operationIdentity,
|
||||
parseLegacyPayload,
|
||||
stringValue,
|
||||
type LegacySendParams
|
||||
} from './orchestration-legacy-operation'
|
||||
|
||||
export async function handleLegacyLifecycleSend(args: {
|
||||
runtime: OrcaRuntimeService
|
||||
authority: LegacyCompatibilityAuthority
|
||||
request: RpcRequest
|
||||
params: LegacySendParams
|
||||
}): Promise<unknown | undefined> {
|
||||
const { runtime, authority, request, params } = args
|
||||
if (!['heartbeat', 'worker_done', 'escalation'].includes(params.type ?? '')) {
|
||||
return undefined
|
||||
}
|
||||
const payload = parseLegacyPayload(params.payload)
|
||||
const dispatch = authority.resolveWorkerDispatch(request, {
|
||||
terminalHandle: params.from,
|
||||
dispatchId: stringValue(payload.dispatchId),
|
||||
taskId: stringValue(payload.taskId)
|
||||
})
|
||||
if (!dispatch) {
|
||||
if (params.type === 'worker_done' && !isWorkerOutcome(payload.outcome)) {
|
||||
throw new OrchestrationError(
|
||||
'invalid_argument',
|
||||
'worker_done requires outcome=succeeded|failed for a current Dispatch.'
|
||||
)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
if (!params.to) {
|
||||
throw new OrchestrationError('invalid_argument', 'Legacy lifecycle mail requires --to.')
|
||||
}
|
||||
const db = runtime.getOrchestrationDb()
|
||||
if (!db.isLegacyCoordinatorHandle(dispatch.run_id, params.to)) {
|
||||
throw new OrchestrationError(
|
||||
'request_mismatch',
|
||||
`Terminal ${params.to} is not a retained coordinator for this legacy Dispatch.`
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
params.type === 'worker_done' &&
|
||||
Object.hasOwn(payload, 'outcome') &&
|
||||
!isWorkerOutcome(payload.outcome)
|
||||
) {
|
||||
throw new OrchestrationError(
|
||||
'invalid_argument',
|
||||
'Legacy worker_done outcome must be succeeded or failed when provided.'
|
||||
)
|
||||
}
|
||||
const principal = authority.attestWorker(request, dispatch)
|
||||
const operation = operationIdentity(request, params.type as string, { ...params, payload })
|
||||
const inferredOutcome =
|
||||
params.type === 'worker_done'
|
||||
? isWorkerOutcome(payload.outcome)
|
||||
? payload.outcome
|
||||
: inferLegacyWorkerOutcome(params.subject)
|
||||
: undefined
|
||||
const existingId =
|
||||
params.type === 'worker_done'
|
||||
? db.findLegacyWorkerCompletion({
|
||||
principalId: principal.id,
|
||||
taskId: dispatch.task_id,
|
||||
recipientHandle: params.to,
|
||||
subject: params.subject,
|
||||
body: params.body ?? '',
|
||||
payload: params.payload ?? null
|
||||
})?.id
|
||||
: undefined
|
||||
const committed = db.commitLegacyLifecycleOperation({
|
||||
principalId: principal.id,
|
||||
operationKey: operation.key,
|
||||
method: request.method,
|
||||
payloadHash: operation.payloadHash,
|
||||
message: {
|
||||
existingId,
|
||||
to: params.to,
|
||||
subject: params.subject,
|
||||
body: params.body,
|
||||
type: params.type as MessageType,
|
||||
priority: params.priority,
|
||||
payload: params.payload
|
||||
},
|
||||
lifecycle:
|
||||
params.type === 'heartbeat'
|
||||
? { kind: 'heartbeat', at: new Date().toISOString() }
|
||||
: params.type === 'worker_done'
|
||||
? {
|
||||
kind: 'worker_report',
|
||||
taskId: dispatch.task_id,
|
||||
outcome: inferredOutcome as 'succeeded' | 'failed',
|
||||
result: params.body || params.subject
|
||||
}
|
||||
: { kind: 'message_only' }
|
||||
})
|
||||
if (!committed.duplicate) {
|
||||
runtime.notifyMessageArrived(committed.message.to_handle, committed.message.type)
|
||||
}
|
||||
return {
|
||||
message: committed.message,
|
||||
...(committed.settlement ? { lifecycle: committed.settlement } : {}),
|
||||
legacyCompatibility: { replayed: committed.duplicate }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
import type { RpcRequest } from './core'
|
||||
import type { OrcaRuntimeService } from '../orca-runtime'
|
||||
import { OrchestrationError } from '../orchestration/orchestration-error'
|
||||
import { formatMessageBanner } from '../orchestration/formatter'
|
||||
import { ORCHESTRATION_MESSAGE_WAIT_DEFAULT_TIMEOUT_MS } from '../../../shared/orchestration-message-wait-timeout'
|
||||
import type { LegacyCompatibilityAuthority } from './orchestration-legacy-authority'
|
||||
import {
|
||||
operationIdentity,
|
||||
parseLegacyMailAck,
|
||||
parseLegacyQuestionAck,
|
||||
parseLegacyTypes,
|
||||
supportedLegacyHints,
|
||||
type LegacyCheckParams,
|
||||
type LegacyReplyParams
|
||||
} from './orchestration-legacy-operation'
|
||||
|
||||
export async function handleLegacyCheck(args: {
|
||||
runtime: OrcaRuntimeService
|
||||
authority: LegacyCompatibilityAuthority
|
||||
request: RpcRequest
|
||||
params: LegacyCheckParams
|
||||
signal?: AbortSignal
|
||||
}): Promise<unknown | undefined> {
|
||||
const { runtime, authority, request, params, signal } = args
|
||||
if (params.run) {
|
||||
return undefined
|
||||
}
|
||||
const typeFilter = parseLegacyTypes(params.types)
|
||||
const principal = authority.resolveCheckPrincipal(request, params.terminal)
|
||||
if (!principal) {
|
||||
return undefined
|
||||
}
|
||||
const db = runtime.getOrchestrationDb()
|
||||
if (params.compatibilityQuestionAck) {
|
||||
const answer = parseLegacyQuestionAck(params.compatibilityQuestionAck)
|
||||
const acknowledged = db.acknowledgeLegacyQuestionAnswer({
|
||||
principalId: principal.id,
|
||||
...answer
|
||||
})
|
||||
return {
|
||||
messages: [],
|
||||
count: 0,
|
||||
acknowledged: [answer.answerMessageId],
|
||||
duplicate: acknowledged.duplicate,
|
||||
legacyCompatibility: { acknowledged: true }
|
||||
}
|
||||
}
|
||||
if (params.compatibilityAck) {
|
||||
const ack = parseLegacyMailAck(params.compatibilityAck)
|
||||
const acknowledged = db.acknowledgeLegacyMail({
|
||||
principalId: principal.id,
|
||||
messageIds: ack.messageIds,
|
||||
types: ack.types
|
||||
})
|
||||
return {
|
||||
messages: [],
|
||||
count: 0,
|
||||
acknowledged: ack.messageIds,
|
||||
duplicate: acknowledged.duplicate,
|
||||
legacyCompatibility: { acknowledged: true }
|
||||
}
|
||||
}
|
||||
|
||||
const history = params.all || (params.unread === false && !params.peek)
|
||||
const readOnly = Boolean(params.peek || history)
|
||||
const read = () =>
|
||||
history
|
||||
? db.getLegacyMailHistory({ principalId: principal.id, types: typeFilter })
|
||||
: db.getLegacyMailPage({ principalId: principal.id, types: typeFilter })
|
||||
let page = read()
|
||||
const deadline =
|
||||
Date.now() + Math.max(params.timeoutMs ?? ORCHESTRATION_MESSAGE_WAIT_DEFAULT_TIMEOUT_MS, 0)
|
||||
while (page.messages.length === 0 && params.wait && !signal?.aborted && Date.now() < deadline) {
|
||||
if (principal.role === 'coordinator' && db.hasPendingCurrentDelivery(principal.run_id)) {
|
||||
break
|
||||
}
|
||||
await runtime.waitForMessage(
|
||||
principal.role === 'worker'
|
||||
? `dispatch:${principal.dispatch_id as string}`
|
||||
: principal.terminal_handle,
|
||||
{
|
||||
typeFilter,
|
||||
timeoutMs: Math.min(1_000, Math.max(deadline - Date.now(), 1)),
|
||||
signal
|
||||
}
|
||||
)
|
||||
page = read()
|
||||
}
|
||||
|
||||
const consuming = !readOnly
|
||||
const messageIds = consuming ? page.messages.map((message) => message.id) : []
|
||||
const formattingAuthority = readOnly
|
||||
? 'legacy_read_only'
|
||||
: page.recovery
|
||||
? 'legacy_recovery_replay'
|
||||
: 'legacy_compatibility'
|
||||
const formatted =
|
||||
params.format || params.inject
|
||||
? page.messages
|
||||
.map((message) =>
|
||||
formatMessageBanner(message, {
|
||||
authority: formattingAuthority,
|
||||
supportedActionHints: readOnly
|
||||
? []
|
||||
: supportedLegacyHints(message, principal, params.compatibilityCliCommand ?? 'orca')
|
||||
})
|
||||
)
|
||||
.join('\n\n')
|
||||
: undefined
|
||||
const currentDelivery =
|
||||
principal.role === 'coordinator' && db.hasPendingCurrentDelivery(principal.run_id)
|
||||
? {
|
||||
runId: principal.run_id,
|
||||
checkCommand: `${params.compatibilityCliCommand ?? 'orca'} orchestration check --run ${principal.run_id}`,
|
||||
ackCommand: `${params.compatibilityCliCommand ?? 'orca'} orchestration check --run ${principal.run_id} --ack <delivery-id>`
|
||||
}
|
||||
: undefined
|
||||
return {
|
||||
runId: principal.run_id,
|
||||
dispatchId: principal.dispatch_id,
|
||||
messages: page.messages,
|
||||
count: page.messages.length,
|
||||
formatted,
|
||||
timedOut: Boolean(params.wait && page.messages.length === 0 && !signal?.aborted),
|
||||
cancelled: signal?.aborted === true,
|
||||
connectionLost: signal?.aborted === true,
|
||||
legacyCompatibility: {
|
||||
recovery: page.recovery,
|
||||
readOnly,
|
||||
ackMessageIds: messageIds,
|
||||
currentDelivery
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleLegacyReply(args: {
|
||||
runtime: OrcaRuntimeService
|
||||
authority: LegacyCompatibilityAuthority
|
||||
request: RpcRequest
|
||||
params: LegacyReplyParams
|
||||
}): Promise<unknown | undefined> {
|
||||
const { runtime, authority, request, params } = args
|
||||
const db = runtime.getOrchestrationDb()
|
||||
const original = db.getMessageById(params.id)
|
||||
if (!original || original.delivery_contract !== 'legacy_direct') {
|
||||
return undefined
|
||||
}
|
||||
const principal = authority.attestCoordinator(request, original.run_id)
|
||||
if (!principal) {
|
||||
throw legacyCoordinatorReadOnly()
|
||||
}
|
||||
const operation = operationIdentity(request, 'reply', {
|
||||
questionId: params.id,
|
||||
body: params.body
|
||||
})
|
||||
const committed = db.commitLegacyReplyOperation({
|
||||
principalId: principal.id,
|
||||
operationKey: operation.key,
|
||||
method: request.method,
|
||||
payloadHash: operation.payloadHash,
|
||||
questionId: params.id,
|
||||
body: params.body
|
||||
})
|
||||
if (!committed.duplicate) {
|
||||
runtime.notifyMessageArrived(committed.question.asker_handle, 'status')
|
||||
}
|
||||
return {
|
||||
message: committed.message,
|
||||
question: committed.question,
|
||||
duplicate: committed.duplicate,
|
||||
legacyCompatibility: { replayed: committed.duplicate }
|
||||
}
|
||||
}
|
||||
|
||||
function legacyCoordinatorReadOnly(): OrchestrationError {
|
||||
return new OrchestrationError(
|
||||
'legacy_read_only',
|
||||
'This retained legacy coordinator could not prove its original process identity. No effects were applied.',
|
||||
{ effectsApplied: false }
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
import { createHash, randomUUID } from 'node:crypto'
|
||||
import type { RpcRequest } from './core'
|
||||
import type { MessageType } from '../orchestration/db'
|
||||
import { MESSAGE_TYPES } from '../orchestration/types'
|
||||
import type { LegacyCompatibilityPrincipalRow, MessageRow } from '../orchestration/types'
|
||||
import { OrchestrationError } from '../orchestration/orchestration-error'
|
||||
|
||||
const MESSAGE_TYPE_SET = new Set<string>(MESSAGE_TYPES)
|
||||
|
||||
export type LegacySendParams = {
|
||||
from?: string
|
||||
to?: string
|
||||
subject: string
|
||||
body?: string
|
||||
type?: string
|
||||
priority?: 'normal' | 'high' | 'urgent'
|
||||
payload?: string
|
||||
}
|
||||
|
||||
export type LegacyCheckParams = {
|
||||
terminal?: string
|
||||
run?: string
|
||||
unread?: boolean
|
||||
peek?: boolean
|
||||
all?: boolean
|
||||
types?: string
|
||||
format?: boolean
|
||||
inject?: boolean
|
||||
wait?: boolean
|
||||
timeoutMs?: number
|
||||
compatibilityAck?: string
|
||||
compatibilityQuestionAck?: string
|
||||
compatibilityCliCommand?: 'orca' | 'orca-ide' | 'orca-dev'
|
||||
}
|
||||
|
||||
export type LegacyAskParams = {
|
||||
from?: string
|
||||
to?: string
|
||||
run?: string
|
||||
question?: string
|
||||
resume?: string
|
||||
options?: string
|
||||
timeoutMs?: number
|
||||
compatibilityCliCommand?: 'orca' | 'orca-ide' | 'orca-dev'
|
||||
compatibilityWindowsCommand?: 'orca' | 'orca-ide'
|
||||
}
|
||||
|
||||
export type LegacyReplyParams = {
|
||||
id: string
|
||||
body: string
|
||||
from?: string
|
||||
run?: string
|
||||
}
|
||||
|
||||
export function operationIdentity(
|
||||
request: RpcRequest,
|
||||
method: string,
|
||||
payload: unknown
|
||||
): { key: string; payloadHash: string } {
|
||||
const canonical = JSON.stringify(canonicalize(payload))
|
||||
const payloadHash = createHash('sha256').update(canonical).digest('hex')
|
||||
const semantic =
|
||||
method === 'worker_done' || method === 'reply' || method === 'ask'
|
||||
? `${method}:${payloadHash}`
|
||||
: `${method}:${randomUUID()}`
|
||||
return {
|
||||
key: request.compatibilityInvocationId
|
||||
? `invocation:${request.compatibilityInvocationId}`
|
||||
: `semantic:${semantic}`,
|
||||
payloadHash
|
||||
}
|
||||
}
|
||||
|
||||
export function parseLegacyPayload(raw: string | undefined): Record<string, unknown> {
|
||||
if (!raw) {
|
||||
return {}
|
||||
}
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(raw)
|
||||
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
||||
return parsed as Record<string, unknown>
|
||||
}
|
||||
} catch {
|
||||
// Fall through to one stable validation error.
|
||||
}
|
||||
throw new OrchestrationError('invalid_argument', 'Message payload must be valid JSON.')
|
||||
}
|
||||
|
||||
export function parseLegacyMailAck(raw: string): { messageIds: string[]; types?: MessageType[] } {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(raw)
|
||||
const messageIds = Array.isArray(parsed)
|
||||
? parsed
|
||||
: parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
||||
? (parsed as { messageIds?: unknown }).messageIds
|
||||
: undefined
|
||||
const types =
|
||||
parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
||||
? (parsed as { types?: unknown }).types
|
||||
: undefined
|
||||
if (
|
||||
Array.isArray(messageIds) &&
|
||||
messageIds.length > 0 &&
|
||||
messageIds.every((value) => typeof value === 'string' && value.length > 0) &&
|
||||
(types === undefined ||
|
||||
(Array.isArray(types) &&
|
||||
types.every((value) => typeof value === 'string' && MESSAGE_TYPE_SET.has(value))))
|
||||
) {
|
||||
return {
|
||||
messageIds,
|
||||
types: types && types.length > 0 ? (types as MessageType[]) : undefined
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Fall through to one stable validation error.
|
||||
}
|
||||
throw new OrchestrationError('invalid_argument', 'Invalid compatibility acknowledgment.')
|
||||
}
|
||||
|
||||
export function parseLegacyQuestionAck(raw: string): {
|
||||
questionId: string
|
||||
answerMessageId: string
|
||||
} {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(raw)
|
||||
if (
|
||||
parsed &&
|
||||
typeof parsed === 'object' &&
|
||||
!Array.isArray(parsed) &&
|
||||
typeof (parsed as { questionId?: unknown }).questionId === 'string' &&
|
||||
typeof (parsed as { answerMessageId?: unknown }).answerMessageId === 'string'
|
||||
) {
|
||||
return parsed as { questionId: string; answerMessageId: string }
|
||||
}
|
||||
} catch {
|
||||
// Fall through to one stable validation error.
|
||||
}
|
||||
throw new OrchestrationError('invalid_argument', 'Invalid legacy question acknowledgment.')
|
||||
}
|
||||
|
||||
export function parseLegacyTypes(raw: string | undefined): MessageType[] | undefined {
|
||||
const values = raw
|
||||
?.split(',')
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean)
|
||||
const invalidValues = values?.filter((value) => !MESSAGE_TYPE_SET.has(value))
|
||||
if (invalidValues && invalidValues.length > 0) {
|
||||
throw new OrchestrationError('invalid_argument', `Invalid --types: ${invalidValues.join(',')}`)
|
||||
}
|
||||
return values && values.length > 0 ? (values as MessageType[]) : undefined
|
||||
}
|
||||
|
||||
export function parseLegacyOptions(raw: string | undefined): string[] {
|
||||
return (
|
||||
raw
|
||||
?.split(',')
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean) ?? []
|
||||
)
|
||||
}
|
||||
|
||||
export function supportedLegacyHints(
|
||||
message: MessageRow,
|
||||
principal: LegacyCompatibilityPrincipalRow,
|
||||
cliCommand: 'orca' | 'orca-ide' | 'orca-dev'
|
||||
): string[] {
|
||||
if (
|
||||
principal.role !== 'coordinator' ||
|
||||
(message.type !== 'question' && message.type !== 'decision_gate')
|
||||
) {
|
||||
return []
|
||||
}
|
||||
return [
|
||||
`${cliCommand} orchestration reply --id ${message.id} --from ${principal.terminal_handle} --body "..."`
|
||||
]
|
||||
}
|
||||
|
||||
export function inferLegacyWorkerOutcome(subject: string): 'succeeded' | 'failed' {
|
||||
const normalized = subject.trim()
|
||||
return normalized === 'Failed' || normalized.startsWith('Failed:') ? 'failed' : 'succeeded'
|
||||
}
|
||||
|
||||
export function isWorkerOutcome(value: unknown): value is 'succeeded' | 'failed' {
|
||||
return value === 'succeeded' || value === 'failed'
|
||||
}
|
||||
|
||||
export function stringValue(value: unknown): string | undefined {
|
||||
return typeof value === 'string' && value.length > 0 ? value : undefined
|
||||
}
|
||||
|
||||
export function normalizeLegacyText(value: string): string {
|
||||
return value.replace(/\r\n/g, '\n').trim()
|
||||
}
|
||||
|
||||
function canonicalize(value: unknown): unknown {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(canonicalize)
|
||||
}
|
||||
if (!value || typeof value !== 'object') {
|
||||
return value
|
||||
}
|
||||
const source = value as Record<string, unknown>
|
||||
return Object.fromEntries(
|
||||
Object.keys(source)
|
||||
.sort()
|
||||
.filter((key) => source[key] !== undefined)
|
||||
.map((key) => [key, canonicalize(source[key])])
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { parsePaneKey } from '../../../shared/stable-pane-id'
|
||||
import { OrchestrationError } from '../orchestration/orchestration-error'
|
||||
|
||||
export function equivalentLegacyPaneKey(
|
||||
a: string | null | undefined,
|
||||
b: string | null | undefined
|
||||
): boolean {
|
||||
if (!a || !b) {
|
||||
return false
|
||||
}
|
||||
if (a === b) {
|
||||
return true
|
||||
}
|
||||
const aLeaf = parsePaneKey(a)?.leafId
|
||||
const bLeaf = parsePaneKey(b)?.leafId
|
||||
return Boolean(aLeaf && bLeaf && aLeaf === bLeaf)
|
||||
}
|
||||
|
||||
export function legacyReadOnlyError(): OrchestrationError {
|
||||
return new OrchestrationError(
|
||||
'legacy_read_only',
|
||||
'This retained legacy assignment could not prove authority from its original live process. No effects were applied.',
|
||||
{ effectsApplied: false }
|
||||
)
|
||||
}
|
||||
|
||||
export function legacyCoordinatorReadOnly(): OrchestrationError {
|
||||
return new OrchestrationError(
|
||||
'legacy_read_only',
|
||||
'This retained legacy coordinator could not prove its original process identity. No effects were applied.',
|
||||
{ effectsApplied: false }
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { mkdtempSync, rmSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { OrchestrationCompatibilityEvidence } from '../../../shared/orchestration-compatibility-evidence'
|
||||
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../shared/protocol-version'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import { OrchestrationDb } from '../orchestration/db'
|
||||
import { OrcaRuntimeService } from '../orca-runtime'
|
||||
import type { RpcRequest } from './core'
|
||||
import { RpcDispatcher } from './dispatcher'
|
||||
import { ORCHESTRATION_METHODS } from './methods/orchestration'
|
||||
|
||||
const COORDINATOR_HANDLE = 'term_legacy_coord'
|
||||
const CURRENT_COORDINATOR_HANDLE = 'term_current_coord'
|
||||
const CURRENT_COORDINATOR_PANE = 'tab_current:55555555-5555-4555-8555-555555555555'
|
||||
const WORKER_HANDLE = 'term_legacy_worker'
|
||||
const WORKER_PANE = 'tab_worker:33333333-3333-4333-8333-333333333333'
|
||||
|
||||
const tempDirs: string[] = []
|
||||
const databases: OrchestrationDb[] = []
|
||||
|
||||
afterEach(() => {
|
||||
databases.splice(0).forEach((database) => database.close())
|
||||
tempDirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true }))
|
||||
})
|
||||
|
||||
function createHarness(options?: { seedCutoverQuestion?: boolean; seedCutoverAnswer?: boolean }): {
|
||||
db: OrchestrationDb
|
||||
dispatcher: RpcDispatcher
|
||||
adoptedRunId: string
|
||||
dispatchId: string
|
||||
cutoverQuestionId?: string
|
||||
cutoverAnswerId?: string
|
||||
} {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'orca-legacy-question-takeover-'))
|
||||
tempDirs.push(dir)
|
||||
const dbPath = join(dir, 'orchestration.db')
|
||||
const before = new OrchestrationDb(dbPath)
|
||||
const task = before.createTask({
|
||||
spec: 'legacy assignment',
|
||||
createdByTerminalHandle: COORDINATOR_HANDLE
|
||||
})
|
||||
const dispatch = before.createDispatchContext(task.id, WORKER_HANDLE, WORKER_PANE)
|
||||
const cutoverQuestion = options?.seedCutoverQuestion
|
||||
? before.insertMessage({
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
subject: 'Question',
|
||||
body: 'Continue after update?',
|
||||
type: 'decision_gate',
|
||||
payload: JSON.stringify({
|
||||
question: 'Continue after update?',
|
||||
options: ['yes', 'no']
|
||||
})
|
||||
})
|
||||
: undefined
|
||||
const cutoverAnswer =
|
||||
cutoverQuestion && options?.seedCutoverAnswer
|
||||
? before.insertMessage({
|
||||
from: COORDINATOR_HANDLE,
|
||||
to: WORKER_HANDLE,
|
||||
subject: 'Re: Question',
|
||||
body: 'Continue.',
|
||||
threadId: cutoverQuestion.id
|
||||
})
|
||||
: undefined
|
||||
before.close()
|
||||
|
||||
const raw = new Database(dbPath)
|
||||
raw.exec(`
|
||||
UPDATE dispatch_contexts SET process_incarnation = 'process-1';
|
||||
DROP INDEX IF EXISTS idx_messages_delivery_contract;
|
||||
DROP TABLE legacy_mail_receipts;
|
||||
DROP TABLE legacy_operation_receipts;
|
||||
DROP TABLE legacy_compatibility_principals;
|
||||
DROP TABLE legacy_adoptions;
|
||||
`)
|
||||
raw.pragma('user_version = 18')
|
||||
raw.close()
|
||||
|
||||
const db = new OrchestrationDb(dbPath)
|
||||
databases.push(db)
|
||||
const runtime = new OrcaRuntimeService()
|
||||
runtime.setOrchestrationDb(db)
|
||||
vi.spyOn(runtime, 'getTerminalPaneKey').mockImplementation((handle) =>
|
||||
handle === WORKER_HANDLE
|
||||
? WORKER_PANE
|
||||
: handle === CURRENT_COORDINATOR_HANDLE
|
||||
? CURRENT_COORDINATOR_PANE
|
||||
: null
|
||||
)
|
||||
vi.spyOn(runtime, 'verifyOrchestrationCompatibilityCaller').mockImplementation((evidence) => {
|
||||
const worker =
|
||||
evidence?.terminalHandle === WORKER_HANDLE &&
|
||||
evidence.paneKey === WORKER_PANE &&
|
||||
evidence.launchToken === 'worker-token'
|
||||
const coordinator =
|
||||
evidence?.terminalHandle === CURRENT_COORDINATOR_HANDLE &&
|
||||
evidence.paneKey === CURRENT_COORDINATOR_PANE &&
|
||||
evidence.launchToken === 'current-coordinator-token'
|
||||
const launchToken = worker
|
||||
? 'worker-token'
|
||||
: coordinator
|
||||
? 'current-coordinator-token'
|
||||
: undefined
|
||||
return launchToken
|
||||
? {
|
||||
hostScope: { kind: 'local', hostId: 'local' },
|
||||
terminalHandle: evidence?.terminalHandle as string,
|
||||
paneKey: evidence?.paneKey as string,
|
||||
processIncarnation: 'process-1',
|
||||
launchTokenHash: createHash('sha256').update(launchToken).digest('hex')
|
||||
}
|
||||
: null
|
||||
})
|
||||
return {
|
||||
db,
|
||||
dispatcher: new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS }),
|
||||
adoptedRunId: db.getLegacyAdoption()?.adopted_run_id as string,
|
||||
dispatchId: dispatch.id,
|
||||
...(cutoverQuestion ? { cutoverQuestionId: cutoverQuestion.id } : {}),
|
||||
...(cutoverAnswer ? { cutoverAnswerId: cutoverAnswer.id } : {})
|
||||
}
|
||||
}
|
||||
|
||||
function request(
|
||||
method: string,
|
||||
params: unknown,
|
||||
invocationId: string,
|
||||
evidence?: OrchestrationCompatibilityEvidence
|
||||
): RpcRequest {
|
||||
return {
|
||||
id: `rpc_${invocationId}`,
|
||||
authToken: 'caller-token',
|
||||
method,
|
||||
params,
|
||||
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
|
||||
orchestrationRequestId: invocationId,
|
||||
compatibilityInvocationId: invocationId,
|
||||
orchestrationCompatibilityEvidence: evidence
|
||||
}
|
||||
}
|
||||
|
||||
const workerEvidence: OrchestrationCompatibilityEvidence = {
|
||||
terminalHandle: WORKER_HANDLE,
|
||||
paneKey: WORKER_PANE,
|
||||
launchToken: 'worker-token'
|
||||
}
|
||||
|
||||
describe('legacy question takeover compatibility', () => {
|
||||
it('reuses a pending ask that crossed the schema cutover', async () => {
|
||||
const harness = createHarness({ seedCutoverQuestion: true })
|
||||
|
||||
const asked = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.ask',
|
||||
{
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
question: 'Continue after update?',
|
||||
options: 'yes,no',
|
||||
timeoutMs: 0
|
||||
},
|
||||
'retry-cutover-ask',
|
||||
workerEvidence
|
||||
)
|
||||
)
|
||||
|
||||
expect(asked).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
messageId: harness.cutoverQuestionId,
|
||||
legacyCompatibility: { replayed: true }
|
||||
}
|
||||
})
|
||||
const sqlite = (harness.db as unknown as { db: Database.Database }).db
|
||||
expect(
|
||||
(
|
||||
sqlite
|
||||
.prepare(
|
||||
`SELECT COUNT(*) AS count
|
||||
FROM messages
|
||||
WHERE type IN ('decision_gate', 'question')`
|
||||
)
|
||||
.get() as { count: number }
|
||||
).count
|
||||
).toBe(1)
|
||||
expect(
|
||||
(sqlite.prepare('SELECT COUNT(*) AS count FROM question_threads').get() as { count: number })
|
||||
.count
|
||||
).toBe(1)
|
||||
})
|
||||
|
||||
it('resumes an answered pinned-A ask without duplicating it', async () => {
|
||||
const harness = createHarness({
|
||||
seedCutoverQuestion: true,
|
||||
seedCutoverAnswer: true
|
||||
})
|
||||
|
||||
const resumed = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.ask',
|
||||
{
|
||||
from: WORKER_HANDLE,
|
||||
resume: harness.cutoverQuestionId,
|
||||
timeoutMs: 0
|
||||
},
|
||||
'resume-answered-cutover-ask',
|
||||
workerEvidence
|
||||
)
|
||||
)
|
||||
|
||||
expect(resumed).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
answer: 'Continue.',
|
||||
answerMessageId: harness.cutoverAnswerId,
|
||||
messageId: harness.cutoverQuestionId,
|
||||
legacyCompatibility: {
|
||||
answerAcknowledgement: {
|
||||
questionId: harness.cutoverQuestionId,
|
||||
answerMessageId: harness.cutoverAnswerId
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
const sqlite = (harness.db as unknown as { db: Database.Database }).db
|
||||
expect(
|
||||
(sqlite.prepare('SELECT COUNT(*) AS count FROM question_threads').get() as { count: number })
|
||||
.count
|
||||
).toBe(1)
|
||||
})
|
||||
|
||||
it('resumes and idempotently acknowledges the exact current Run answer', async () => {
|
||||
const harness = createHarness()
|
||||
const asked = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.ask',
|
||||
{
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
question: 'Continue after takeover?',
|
||||
timeoutMs: 0
|
||||
},
|
||||
'ask-before-takeover',
|
||||
workerEvidence
|
||||
)
|
||||
)
|
||||
const questionId = (asked as { result: { messageId: string } }).result.messageId
|
||||
|
||||
await expect(
|
||||
harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.runUse',
|
||||
{
|
||||
id: harness.adoptedRunId,
|
||||
from: CURRENT_COORDINATOR_HANDLE,
|
||||
takeoverLegacy: true
|
||||
},
|
||||
'take-over',
|
||||
{
|
||||
terminalHandle: CURRENT_COORDINATOR_HANDLE,
|
||||
paneKey: CURRENT_COORDINATOR_PANE,
|
||||
launchToken: 'current-coordinator-token'
|
||||
}
|
||||
)
|
||||
)
|
||||
).resolves.toMatchObject({ ok: true })
|
||||
await expect(
|
||||
harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.reply',
|
||||
{
|
||||
id: questionId,
|
||||
body: 'Continue.',
|
||||
from: CURRENT_COORDINATOR_HANDLE,
|
||||
run: harness.adoptedRunId
|
||||
},
|
||||
'answer-after-takeover'
|
||||
)
|
||||
)
|
||||
).resolves.toMatchObject({ ok: true })
|
||||
|
||||
const resumed = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.ask',
|
||||
{ from: WORKER_HANDLE, resume: questionId, timeoutMs: 100 },
|
||||
'resume-after-takeover',
|
||||
workerEvidence
|
||||
)
|
||||
)
|
||||
expect(resumed).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
answer: 'Continue.',
|
||||
legacyCompatibility: {
|
||||
answerAcknowledgement: {
|
||||
questionId,
|
||||
answerMessageId: expect.any(String)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
const acknowledgement = (
|
||||
resumed as {
|
||||
result: {
|
||||
legacyCompatibility: {
|
||||
answerAcknowledgement: { questionId: string; answerMessageId: string }
|
||||
}
|
||||
}
|
||||
}
|
||||
).result.legacyCompatibility.answerAcknowledgement
|
||||
expect(harness.db.getMessageById(acknowledgement.answerMessageId)).toMatchObject({
|
||||
run_id: harness.adoptedRunId,
|
||||
from_handle: `run:${harness.adoptedRunId}`,
|
||||
to_handle: `dispatch:${harness.dispatchId}`,
|
||||
delivery_contract: 'current_delivery',
|
||||
thread_id: questionId
|
||||
})
|
||||
|
||||
const acknowledge = (invocationId: string) =>
|
||||
harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{
|
||||
terminal: WORKER_HANDLE,
|
||||
compatibilityQuestionAck: JSON.stringify(acknowledgement)
|
||||
},
|
||||
invocationId,
|
||||
workerEvidence
|
||||
)
|
||||
)
|
||||
const sqlite = (harness.db as unknown as { db: Database.Database }).db
|
||||
sqlite
|
||||
.prepare('UPDATE messages SET to_handle = ? WHERE id = ?')
|
||||
.run('dispatch:foreign', acknowledgement.answerMessageId)
|
||||
await expect(acknowledge('ack-wrong-answer-route')).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { code: 'request_mismatch' }
|
||||
})
|
||||
sqlite
|
||||
.prepare('UPDATE messages SET to_handle = ? WHERE id = ?')
|
||||
.run(`dispatch:${harness.dispatchId}`, acknowledgement.answerMessageId)
|
||||
|
||||
await expect(acknowledge('ack-answer')).resolves.toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
acknowledged: [acknowledgement.answerMessageId],
|
||||
duplicate: false,
|
||||
legacyCompatibility: { acknowledged: true }
|
||||
}
|
||||
})
|
||||
await expect(acknowledge('ack-answer-repeat')).resolves.toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
acknowledged: [acknowledgement.answerMessageId],
|
||||
duplicate: true,
|
||||
legacyCompatibility: { acknowledged: true }
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,169 @@
|
||||
import { clampOrchestrationAskTimeoutMs } from '../../../shared/orchestration-ask-timeout'
|
||||
import type { RpcRequest } from './core'
|
||||
import type { OrcaRuntimeService } from '../orca-runtime'
|
||||
import { OrchestrationError } from '../orchestration/orchestration-error'
|
||||
import { LEGACY_CONTRACT_VERSION } from '../orchestration/db'
|
||||
import type { LegacyCompatibilityAuthority } from './orchestration-legacy-authority'
|
||||
import {
|
||||
normalizeLegacyText,
|
||||
operationIdentity,
|
||||
parseLegacyOptions,
|
||||
type LegacyAskParams
|
||||
} from './orchestration-legacy-operation'
|
||||
|
||||
export async function handleLegacyAsk(args: {
|
||||
runtime: OrcaRuntimeService
|
||||
authority: LegacyCompatibilityAuthority
|
||||
request: RpcRequest
|
||||
params: LegacyAskParams
|
||||
signal?: AbortSignal
|
||||
}): Promise<unknown | undefined> {
|
||||
const { runtime, authority, request, params, signal } = args
|
||||
const dispatch = authority.resolveAskDispatch(request, params)
|
||||
if (!dispatch || dispatch.contract_version !== LEGACY_CONTRACT_VERSION) {
|
||||
return undefined
|
||||
}
|
||||
const db = runtime.getOrchestrationDb()
|
||||
if (!params.resume) {
|
||||
if (!params.to) {
|
||||
throw new OrchestrationError('invalid_argument', 'Legacy ask requires --to.')
|
||||
}
|
||||
if (!db.isLegacyCoordinatorHandle(dispatch.run_id, params.to)) {
|
||||
throw new OrchestrationError(
|
||||
'request_mismatch',
|
||||
`Terminal ${params.to} is not a retained coordinator for this legacy Dispatch.`
|
||||
)
|
||||
}
|
||||
}
|
||||
const principal = authority.attestWorker(request, dispatch)
|
||||
const timeoutMs = clampOrchestrationAskTimeoutMs(params.timeoutMs)
|
||||
let questionId = params.resume
|
||||
let duplicate = true
|
||||
if (questionId) {
|
||||
const question = db.getQuestion(questionId)
|
||||
if (!question || question.dispatch_id !== dispatch.id) {
|
||||
throw new OrchestrationError(
|
||||
'question_not_found',
|
||||
`Question ${questionId} does not belong to this legacy Dispatch.`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
const question = params.question as string
|
||||
const options = parseLegacyOptions(params.options)
|
||||
const recipient = params.to as string
|
||||
const operation = operationIdentity(request, 'ask', {
|
||||
question: normalizeLegacyText(question),
|
||||
options: options.map(normalizeLegacyText),
|
||||
recipient
|
||||
})
|
||||
const priorReceipt = db.getLegacyOperationReceipt(principal.id, operation.key)
|
||||
let existingQuestionId: string | undefined
|
||||
if (!priorReceipt) {
|
||||
const matches = db.findLegacyQuestionsBySemanticIdentity({
|
||||
principalId: principal.id,
|
||||
question,
|
||||
options,
|
||||
recipientHandle: recipient
|
||||
})
|
||||
const unclaimed = matches.filter((match) => !match.claimedByOperation)
|
||||
const pending = unclaimed.filter((match) => match.question.status === 'pending')
|
||||
const lostAnswer = unclaimed.find(
|
||||
(match) => match.question.status === 'answered' && !match.answerAcknowledged
|
||||
)
|
||||
if (lostAnswer) {
|
||||
const cliCommand =
|
||||
params.compatibilityCliCommand ?? params.compatibilityWindowsCommand ?? 'orca'
|
||||
throw new OrchestrationError(
|
||||
'operation_unknown',
|
||||
`A matching legacy answer may have been accepted before the update. Run ${cliCommand} orchestration check --terminal ${principal.terminal_handle} before asking again.`
|
||||
)
|
||||
}
|
||||
if (pending.length > 1) {
|
||||
throw new OrchestrationError(
|
||||
'operation_unknown',
|
||||
'Multiple matching pending legacy questions exist; drain legacy check before retrying.'
|
||||
)
|
||||
}
|
||||
existingQuestionId = pending[0]?.question.message_id
|
||||
}
|
||||
const committed = db.commitLegacyAskOperation({
|
||||
question,
|
||||
principalId: principal.id,
|
||||
operationKey: operation.key,
|
||||
method: request.method,
|
||||
payloadHash: operation.payloadHash,
|
||||
options,
|
||||
recipientHandle: recipient,
|
||||
existingQuestionId
|
||||
})
|
||||
questionId = committed.question.message_id
|
||||
duplicate = committed.duplicate || Boolean(existingQuestionId)
|
||||
if (!duplicate) {
|
||||
runtime.notifyMessageArrived(committed.message.to_handle, committed.message.type)
|
||||
}
|
||||
}
|
||||
|
||||
if (params.compatibilityWindowsCommand && !params.resume) {
|
||||
return {
|
||||
answer: null,
|
||||
messageId: questionId,
|
||||
threadId: questionId,
|
||||
timedOut: false,
|
||||
cancelled: false,
|
||||
connectionLost: false,
|
||||
timeoutMs,
|
||||
legacyCompatibility: {
|
||||
resumeRequired: true,
|
||||
resumeCommand: `${params.compatibilityWindowsCommand} orchestration ask --resume ${questionId}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const deadline = Date.now() + timeoutMs
|
||||
while (true) {
|
||||
const current = db.getQuestion(questionId as string)
|
||||
if (!current || current.status === 'closed') {
|
||||
throw new OrchestrationError(
|
||||
'dispatch_inactive',
|
||||
`Question ${questionId as string} closed because its Dispatch is inactive.`
|
||||
)
|
||||
}
|
||||
if (current.status === 'answered') {
|
||||
return {
|
||||
answer: current.answer_body,
|
||||
answerMessageId: current.answer_message_id,
|
||||
messageId: current.message_id,
|
||||
threadId: current.message_id,
|
||||
timedOut: false,
|
||||
cancelled: false,
|
||||
connectionLost: false,
|
||||
timeoutMs,
|
||||
legacyCompatibility: {
|
||||
replayed: duplicate,
|
||||
answerAcknowledgement: current.answer_message_id
|
||||
? {
|
||||
questionId: current.message_id,
|
||||
answerMessageId: current.answer_message_id
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
if (signal?.aborted || Date.now() >= deadline) {
|
||||
return {
|
||||
answer: null,
|
||||
messageId: questionId,
|
||||
threadId: questionId,
|
||||
timedOut: !signal?.aborted,
|
||||
cancelled: signal?.aborted === true,
|
||||
connectionLost: signal?.aborted === true,
|
||||
timeoutMs,
|
||||
legacyCompatibility: { replayed: duplicate, ackMessageIds: [] }
|
||||
}
|
||||
}
|
||||
await runtime.waitForMessage(principal.terminal_handle, {
|
||||
timeoutMs: Math.min(1_000, Math.max(deadline - Date.now(), 1)),
|
||||
signal
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,565 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { mkdtempSync, rmSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { OrchestrationCompatibilityEvidence } from '../../../shared/orchestration-compatibility-evidence'
|
||||
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../shared/protocol-version'
|
||||
import Database from '../../sqlite/sync-database'
|
||||
import { OrcaRuntimeService } from '../orca-runtime'
|
||||
import { OrchestrationDb } from '../orchestration/db'
|
||||
import type { RpcRequest } from './core'
|
||||
import { RpcDispatcher } from './dispatcher'
|
||||
import { ORCHESTRATION_METHODS } from './methods/orchestration'
|
||||
|
||||
const WORKER_HANDLE = 'term_legacy_worker'
|
||||
const WORKER_PANE = 'tab_worker:33333333-3333-4333-8333-333333333333'
|
||||
const COORDINATOR_HANDLE = 'term_legacy_coord'
|
||||
const COORDINATOR_PANE = 'tab_coord:44444444-4444-4444-8444-444444444444'
|
||||
const CURRENT_COORDINATOR_HANDLE = 'term_current_coord'
|
||||
const CURRENT_COORDINATOR_PANE = 'tab_current:55555555-5555-4555-8555-555555555555'
|
||||
|
||||
type Harness = {
|
||||
db: OrchestrationDb
|
||||
dispatcher: RpcDispatcher
|
||||
adoptedRunId: string
|
||||
taskId: string
|
||||
dispatchId: string
|
||||
}
|
||||
|
||||
const tempDirs: string[] = []
|
||||
const databases: OrchestrationDb[] = []
|
||||
|
||||
afterEach(() => {
|
||||
for (const database of databases.splice(0)) {
|
||||
database.close()
|
||||
}
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
function createHarness(): Harness {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'orca-legacy-takeover-'))
|
||||
tempDirs.push(dir)
|
||||
const dbPath = join(dir, 'orchestration.db')
|
||||
const before = new OrchestrationDb(dbPath)
|
||||
const task = before.createTask({
|
||||
spec: 'legacy assignment',
|
||||
createdByTerminalHandle: COORDINATOR_HANDLE
|
||||
})
|
||||
const dispatch = before.createDispatchContext(task.id, WORKER_HANDLE, WORKER_PANE)
|
||||
before.close()
|
||||
|
||||
const raw = new Database(dbPath)
|
||||
raw.exec(`
|
||||
UPDATE dispatch_contexts SET process_incarnation = 'process-1';
|
||||
DROP INDEX IF EXISTS idx_messages_delivery_contract;
|
||||
DROP TABLE legacy_mail_receipts;
|
||||
DROP TABLE legacy_operation_receipts;
|
||||
DROP TABLE legacy_compatibility_principals;
|
||||
DROP TABLE legacy_adoptions;
|
||||
`)
|
||||
raw.pragma('user_version = 18')
|
||||
raw.close()
|
||||
|
||||
const db = new OrchestrationDb(dbPath)
|
||||
databases.push(db)
|
||||
const adoptedRunId = db.getLegacyAdoption()?.adopted_run_id as string
|
||||
const runtime = new OrcaRuntimeService()
|
||||
runtime.setOrchestrationDb(db)
|
||||
vi.spyOn(runtime, 'getTerminalPaneKey').mockImplementation((handle) =>
|
||||
handle === COORDINATOR_HANDLE
|
||||
? COORDINATOR_PANE
|
||||
: handle === WORKER_HANDLE
|
||||
? WORKER_PANE
|
||||
: handle === CURRENT_COORDINATOR_HANDLE
|
||||
? CURRENT_COORDINATOR_PANE
|
||||
: null
|
||||
)
|
||||
vi.spyOn(runtime, 'verifyOrchestrationCompatibilityCaller').mockImplementation((proof) => {
|
||||
const validWorker = proof?.terminalHandle === WORKER_HANDLE && proof.paneKey === WORKER_PANE
|
||||
const validCoordinator =
|
||||
proof?.terminalHandle === COORDINATOR_HANDLE && proof.paneKey === COORDINATOR_PANE
|
||||
const validCurrentCoordinator =
|
||||
proof?.terminalHandle === CURRENT_COORDINATOR_HANDLE &&
|
||||
proof.paneKey === CURRENT_COORDINATOR_PANE
|
||||
if ((!validWorker && !validCoordinator && !validCurrentCoordinator) || !proof?.launchToken) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
hostScope: { kind: 'local', hostId: 'local' },
|
||||
terminalHandle: proof.terminalHandle as string,
|
||||
paneKey: proof.paneKey as string,
|
||||
processIncarnation: 'process-1',
|
||||
launchTokenHash: createHash('sha256').update(proof.launchToken).digest('hex')
|
||||
}
|
||||
})
|
||||
vi.spyOn(runtime, 'notifyMessageArrived').mockImplementation(() => {})
|
||||
return {
|
||||
db,
|
||||
dispatcher: new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS }),
|
||||
adoptedRunId,
|
||||
taskId: task.id,
|
||||
dispatchId: dispatch.id
|
||||
}
|
||||
}
|
||||
|
||||
function evidence(
|
||||
role: 'worker' | 'coordinator' | 'current-coordinator'
|
||||
): OrchestrationCompatibilityEvidence {
|
||||
const worker = role === 'worker'
|
||||
const currentCoordinator = role === 'current-coordinator'
|
||||
return {
|
||||
terminalHandle: worker
|
||||
? WORKER_HANDLE
|
||||
: currentCoordinator
|
||||
? CURRENT_COORDINATOR_HANDLE
|
||||
: COORDINATOR_HANDLE,
|
||||
paneKey: worker
|
||||
? WORKER_PANE
|
||||
: currentCoordinator
|
||||
? CURRENT_COORDINATOR_PANE
|
||||
: COORDINATOR_PANE,
|
||||
launchToken: `${role}-token`
|
||||
}
|
||||
}
|
||||
|
||||
function request(
|
||||
method: string,
|
||||
params: unknown,
|
||||
proof: OrchestrationCompatibilityEvidence,
|
||||
invocationId: string
|
||||
): RpcRequest {
|
||||
return {
|
||||
id: `rpc_${invocationId}`,
|
||||
authToken: 'caller-token',
|
||||
method,
|
||||
params,
|
||||
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
|
||||
orchestrationRequestId: invocationId,
|
||||
compatibilityInvocationId: invocationId,
|
||||
orchestrationCompatibilityEvidence: proof
|
||||
}
|
||||
}
|
||||
|
||||
function counts(db: OrchestrationDb): Record<string, number> {
|
||||
const sqlite = (db as unknown as { db: Database.Database }).db
|
||||
return Object.fromEntries(
|
||||
[
|
||||
'messages',
|
||||
'legacy_compatibility_principals',
|
||||
'legacy_operation_receipts',
|
||||
'legacy_mail_receipts',
|
||||
'mutation_receipts'
|
||||
].map((table) => [
|
||||
table,
|
||||
(sqlite.prepare(`SELECT COUNT(*) AS count FROM ${table}`).get() as { count: number }).count
|
||||
])
|
||||
)
|
||||
}
|
||||
|
||||
function escalationParams(harness: Harness) {
|
||||
return {
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
subject: 'Blocked',
|
||||
type: 'escalation',
|
||||
payload: JSON.stringify({ taskId: harness.taskId, dispatchId: harness.dispatchId })
|
||||
}
|
||||
}
|
||||
|
||||
describe('legacy compatibility after explicit takeover', () => {
|
||||
it('binds takeover to the authenticated invoking coordinator pane', async () => {
|
||||
const harness = createHarness()
|
||||
const before = harness.db.getRun(harness.adoptedRunId)
|
||||
|
||||
const spoofed = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.runUse',
|
||||
{
|
||||
id: harness.adoptedRunId,
|
||||
from: COORDINATOR_HANDLE,
|
||||
takeoverLegacy: true
|
||||
},
|
||||
evidence('current-coordinator'),
|
||||
'spoofed-takeover'
|
||||
)
|
||||
)
|
||||
|
||||
expect(spoofed).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: 'legacy_read_only', data: { effectsApplied: false } }
|
||||
})
|
||||
expect(harness.db.getRun(harness.adoptedRunId)).toEqual(before)
|
||||
|
||||
const bound = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.runUse',
|
||||
{
|
||||
id: harness.adoptedRunId,
|
||||
from: CURRENT_COORDINATOR_HANDLE,
|
||||
takeoverLegacy: true
|
||||
},
|
||||
evidence('current-coordinator'),
|
||||
'authenticated-takeover'
|
||||
)
|
||||
)
|
||||
|
||||
expect(bound).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
run: {
|
||||
coordinator_handle: CURRENT_COORDINATOR_HANDLE,
|
||||
coordinator_pane_key: CURRENT_COORDINATOR_PANE
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('does not let an uncommitted legacy coordinator attest after explicit takeover', async () => {
|
||||
const harness = createHarness()
|
||||
harness.db.bindRun({
|
||||
runId: harness.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:55555555-5555-4555-8555-555555555555',
|
||||
takeoverLegacy: true
|
||||
})
|
||||
expect(harness.db.getLegacyCoordinatorPrincipal(harness.adoptedRunId)).toBeUndefined()
|
||||
const before = counts(harness.db)
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.taskList',
|
||||
{},
|
||||
evidence('coordinator'),
|
||||
'uncommitted-coordinator-after-takeover'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({ ok: false, error: { code: 'legacy_read_only' } })
|
||||
expect(harness.db.getLegacyCoordinatorPrincipal(harness.adoptedRunId)).toBeUndefined()
|
||||
expect(counts(harness.db)).toEqual(before)
|
||||
})
|
||||
|
||||
it.each(['escalation', 'worker_done'] as const)(
|
||||
'keeps the retained coordinator address routable for post-takeover %s',
|
||||
async (type) => {
|
||||
const harness = createHarness()
|
||||
harness.db.bindRun({
|
||||
runId: harness.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:55555555-5555-4555-8555-555555555555',
|
||||
takeoverLegacy: true
|
||||
})
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{
|
||||
...escalationParams(harness),
|
||||
type,
|
||||
subject: type === 'worker_done' ? 'Completed' : 'Blocked'
|
||||
},
|
||||
evidence('worker'),
|
||||
`post-takeover-${type}`
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: {
|
||||
to_handle: `run:${harness.adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery',
|
||||
type
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
it('routes future worker mail to Run Delivery after same-handle takeover', async () => {
|
||||
const harness = createHarness()
|
||||
harness.db.bindRun({
|
||||
runId: harness.adoptedRunId,
|
||||
coordinatorHandle: COORDINATOR_HANDLE,
|
||||
coordinatorPaneKey: COORDINATOR_PANE,
|
||||
takeoverLegacy: true
|
||||
})
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
escalationParams(harness),
|
||||
evidence('worker'),
|
||||
'same-handle-takeover-mail'
|
||||
)
|
||||
)
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: {
|
||||
to_handle: `run:${harness.adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery',
|
||||
type: 'escalation'
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps post-takeover legacy ask routed to the adopted Run', async () => {
|
||||
const harness = createHarness()
|
||||
harness.db.bindRun({
|
||||
runId: harness.adoptedRunId,
|
||||
coordinatorHandle: 'term_current_coord',
|
||||
coordinatorPaneKey: 'tab_current:55555555-5555-4555-8555-555555555555',
|
||||
takeoverLegacy: true
|
||||
})
|
||||
|
||||
const response = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.ask',
|
||||
{
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
question: 'Continue after takeover?',
|
||||
timeoutMs: 1
|
||||
},
|
||||
evidence('worker'),
|
||||
'post-takeover-ask'
|
||||
)
|
||||
)
|
||||
const messageId = (response as { result?: { messageId?: string } }).result?.messageId
|
||||
|
||||
expect(response).toMatchObject({
|
||||
ok: true,
|
||||
result: { timedOut: true, messageId: expect.any(String) }
|
||||
})
|
||||
expect(harness.db.getMessageById(messageId as string)).toMatchObject({
|
||||
to_handle: `run:${harness.adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery',
|
||||
type: 'question'
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps an active legacy worker usable across authenticated takeover', async () => {
|
||||
const harness = createHarness()
|
||||
await expect(
|
||||
harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.runUse',
|
||||
{ id: harness.adoptedRunId, from: COORDINATOR_HANDLE },
|
||||
evidence('coordinator'),
|
||||
'bind-original-coordinator'
|
||||
)
|
||||
)
|
||||
).resolves.toMatchObject({ ok: true })
|
||||
const pendingAsk = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.ask',
|
||||
{
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
question: 'Continue with the migration?',
|
||||
timeoutMs: 0
|
||||
},
|
||||
evidence('worker'),
|
||||
'pending-before-takeover'
|
||||
)
|
||||
)
|
||||
const questionId = (pendingAsk as { result: { messageId: string } }).result.messageId
|
||||
const unrelated = harness.db.insertMessage({
|
||||
runId: harness.adoptedRunId,
|
||||
from: WORKER_HANDLE,
|
||||
to: 'term_peer_worker',
|
||||
subject: 'peer-only',
|
||||
deliveryContract: 'legacy_direct'
|
||||
})
|
||||
const currentRequest = (method: string, params: unknown, invocationId: string) =>
|
||||
request(method, params, evidence('current-coordinator'), invocationId)
|
||||
const takeoverParams = {
|
||||
id: harness.adoptedRunId,
|
||||
from: CURRENT_COORDINATOR_HANDLE,
|
||||
takeoverLegacy: true
|
||||
}
|
||||
|
||||
const takeover = await harness.dispatcher.dispatch(
|
||||
currentRequest('orchestration.runUse', takeoverParams, 'explicit-takeover')
|
||||
)
|
||||
const repeated = await harness.dispatcher.dispatch(
|
||||
currentRequest('orchestration.runUse', takeoverParams, 'explicit-takeover-repeat')
|
||||
)
|
||||
|
||||
expect(takeover).toMatchObject({
|
||||
ok: true,
|
||||
result: { binding: { consumerGeneration: 2 } }
|
||||
})
|
||||
expect(repeated).toMatchObject({
|
||||
ok: true,
|
||||
result: { binding: { consumerGeneration: 2 } }
|
||||
})
|
||||
expect(harness.db.getDispatchContextById(harness.dispatchId)?.status).toBe('dispatched')
|
||||
expect(harness.db.getLegacyCoordinatorPrincipal(harness.adoptedRunId)?.status).toBe('revoked')
|
||||
expect(harness.db.getMessageById(unrelated.id)).toMatchObject({
|
||||
to_handle: 'term_peer_worker',
|
||||
delivery_contract: 'legacy_direct'
|
||||
})
|
||||
|
||||
const promoted = await harness.dispatcher.dispatch(
|
||||
currentRequest(
|
||||
'orchestration.check',
|
||||
{
|
||||
terminal: CURRENT_COORDINATOR_HANDLE,
|
||||
run: harness.adoptedRunId,
|
||||
format: true
|
||||
},
|
||||
'read-promoted-question'
|
||||
)
|
||||
)
|
||||
expect(promoted).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
messages: [
|
||||
{
|
||||
id: questionId,
|
||||
to_handle: `run:${harness.adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery'
|
||||
}
|
||||
],
|
||||
formatted: expect.not.stringContaining(`--from run:${harness.adoptedRunId}`)
|
||||
}
|
||||
})
|
||||
const deliveryId = (promoted as { result: { deliveryId: string } }).result.deliveryId
|
||||
await expect(
|
||||
harness.dispatcher.dispatch(
|
||||
currentRequest(
|
||||
'orchestration.reply',
|
||||
{
|
||||
id: questionId,
|
||||
body: 'Yes, continue.',
|
||||
from: CURRENT_COORDINATOR_HANDLE,
|
||||
run: harness.adoptedRunId
|
||||
},
|
||||
'reply-after-takeover'
|
||||
)
|
||||
)
|
||||
).resolves.toMatchObject({ ok: true, result: { question: { status: 'answered' } } })
|
||||
await expect(
|
||||
harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.ask',
|
||||
{ from: WORKER_HANDLE, resume: questionId, timeoutMs: 100 },
|
||||
evidence('worker'),
|
||||
'resume-after-takeover'
|
||||
)
|
||||
)
|
||||
).resolves.toMatchObject({ ok: true, result: { answer: 'Yes, continue.' } })
|
||||
|
||||
const followUp = await harness.dispatcher.dispatch(
|
||||
currentRequest(
|
||||
'orchestration.send',
|
||||
{
|
||||
from: CURRENT_COORDINATOR_HANDLE,
|
||||
to: `dispatch:${harness.dispatchId}`,
|
||||
subject: 'Continue from the new coordinator'
|
||||
},
|
||||
'current-coordinator-to-legacy-worker'
|
||||
)
|
||||
)
|
||||
expect(followUp).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: {
|
||||
run_id: harness.adoptedRunId,
|
||||
to_handle: `dispatch:${harness.dispatchId}`,
|
||||
delivery_contract: 'legacy_direct'
|
||||
}
|
||||
}
|
||||
})
|
||||
const workerFollowUp = await harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{ terminal: WORKER_HANDLE },
|
||||
evidence('worker'),
|
||||
'legacy-worker-check-after-takeover'
|
||||
)
|
||||
)
|
||||
expect(workerFollowUp).toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
messages: [{ subject: 'Continue from the new coordinator' }],
|
||||
legacyCompatibility: { ackMessageIds: [expect.any(String)] }
|
||||
}
|
||||
})
|
||||
const [followUpMessageId] = (
|
||||
workerFollowUp as { result: { legacyCompatibility: { ackMessageIds: string[] } } }
|
||||
).result.legacyCompatibility.ackMessageIds
|
||||
await expect(
|
||||
harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.check',
|
||||
{
|
||||
terminal: WORKER_HANDLE,
|
||||
compatibilityAck: JSON.stringify({ messageIds: [followUpMessageId] })
|
||||
},
|
||||
evidence('worker'),
|
||||
'legacy-worker-ack-after-takeover'
|
||||
)
|
||||
)
|
||||
).resolves.toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
acknowledged: [followUpMessageId],
|
||||
legacyCompatibility: { acknowledged: true }
|
||||
}
|
||||
})
|
||||
|
||||
await harness.dispatcher.dispatch(
|
||||
currentRequest(
|
||||
'orchestration.check',
|
||||
{
|
||||
terminal: CURRENT_COORDINATOR_HANDLE,
|
||||
run: harness.adoptedRunId,
|
||||
ack: deliveryId
|
||||
},
|
||||
'ack-promoted-question'
|
||||
)
|
||||
)
|
||||
await expect(
|
||||
harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.send',
|
||||
{
|
||||
from: WORKER_HANDLE,
|
||||
to: COORDINATOR_HANDLE,
|
||||
subject: 'Need current coordinator',
|
||||
type: 'escalation',
|
||||
payload: JSON.stringify({ taskId: harness.taskId })
|
||||
},
|
||||
evidence('worker'),
|
||||
'escalation-after-takeover'
|
||||
)
|
||||
)
|
||||
).resolves.toMatchObject({
|
||||
ok: true,
|
||||
result: {
|
||||
message: {
|
||||
to_handle: `run:${harness.adoptedRunId}`,
|
||||
delivery_contract: 'current_delivery'
|
||||
}
|
||||
}
|
||||
})
|
||||
await expect(
|
||||
harness.dispatcher.dispatch(
|
||||
request(
|
||||
'orchestration.taskList',
|
||||
{ callerTerminalHandle: COORDINATOR_HANDLE },
|
||||
evidence('coordinator'),
|
||||
'old-coordinator-fenced'
|
||||
)
|
||||
)
|
||||
).resolves.toMatchObject({ ok: false, error: { code: 'legacy_read_only' } })
|
||||
})
|
||||
})
|
||||
@@ -22,13 +22,14 @@ export class OrchestrationMutationExecutor {
|
||||
async run(
|
||||
request: RpcRequest,
|
||||
params: unknown,
|
||||
invoke: (mutation?: DurableMutationInvocation) => Promise<unknown> | unknown
|
||||
invoke: (mutation?: DurableMutationInvocation) => Promise<unknown> | unknown,
|
||||
callerFingerprintOverride?: string
|
||||
): Promise<unknown> {
|
||||
const requestId = request.orchestrationRequestId
|
||||
if (!requestId || !isOrchestrationMutation(request.method, params)) {
|
||||
return await invoke()
|
||||
}
|
||||
const callerFingerprint = authenticatedCallerFingerprint(request)
|
||||
const callerFingerprint = callerFingerprintOverride ?? authenticatedCallerFingerprint(request)
|
||||
const payloadHash = createHash('sha256')
|
||||
.update(JSON.stringify(canonicalize({ method: request.method, params })))
|
||||
.digest('hex')
|
||||
@@ -55,6 +56,10 @@ export class OrchestrationMutationExecutor {
|
||||
: db.beginMutationReceipt(identity)
|
||||
|
||||
if (begun.disposition === 'completed') {
|
||||
const active = this.inFlight.get(key)
|
||||
if (active) {
|
||||
return attachMutationReceipt(await active, requestId, true)
|
||||
}
|
||||
return attachMutationReceipt(JSON.parse(begun.row.receipt ?? 'null'), requestId, true)
|
||||
}
|
||||
if (begun.disposition === 'pending') {
|
||||
@@ -102,6 +107,20 @@ export class OrchestrationMutationExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
const executorsByRuntime = new WeakMap<OrcaRuntimeService, OrchestrationMutationExecutor>()
|
||||
|
||||
export function getOrchestrationMutationExecutor(
|
||||
runtime: OrcaRuntimeService
|
||||
): OrchestrationMutationExecutor {
|
||||
const existing = executorsByRuntime.get(runtime)
|
||||
if (existing) {
|
||||
return existing
|
||||
}
|
||||
const executor = new OrchestrationMutationExecutor(runtime)
|
||||
executorsByRuntime.set(runtime, executor)
|
||||
return executor
|
||||
}
|
||||
|
||||
export function authenticatedCallerFingerprint(request: RpcRequest): string {
|
||||
const callerToken =
|
||||
request.authToken ||
|
||||
|
||||
@@ -252,7 +252,10 @@ describe('durable orchestration mutation ledger', () => {
|
||||
})
|
||||
|
||||
it('recovers a lost ask acceptance without creating a second question', async () => {
|
||||
const db = new OrchestrationDb(':memory:')
|
||||
const dir = mkdtempSync(join(tmpdir(), 'orca-mutation-ask-recovery-'))
|
||||
paths.push(dir)
|
||||
const dbPath = join(dir, 'orchestration.db')
|
||||
const db = new OrchestrationDb(dbPath)
|
||||
const runtime = new OrcaRuntimeService()
|
||||
runtime.setOrchestrationDb(db)
|
||||
vi.spyOn(runtime, 'getTerminalPaneKey').mockReturnValue('tab_worker:leaf_worker')
|
||||
@@ -290,7 +293,16 @@ describe('durable orchestration mutation ledger', () => {
|
||||
const first = firstDispatcher.dispatch(askRequest, { signal: controller.signal })
|
||||
await vi.waitFor(() => expect(db.getInbox(10)).toHaveLength(1))
|
||||
|
||||
const restartedDispatcher = new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS })
|
||||
const restartedDb = new OrchestrationDb(dbPath)
|
||||
const restartedRuntime = new OrcaRuntimeService()
|
||||
restartedRuntime.setOrchestrationDb(restartedDb)
|
||||
vi.spyOn(restartedRuntime, 'getTerminalPaneKey').mockReturnValue('tab_worker:leaf_worker')
|
||||
vi.spyOn(restartedRuntime, 'getTerminalProcessIncarnation').mockReturnValue('runtime:pty:1')
|
||||
vi.spyOn(restartedRuntime, 'notifyMessageArrived').mockImplementation(() => {})
|
||||
const restartedDispatcher = new RpcDispatcher({
|
||||
runtime: restartedRuntime,
|
||||
methods: ORCHESTRATION_METHODS
|
||||
})
|
||||
const recovered = await restartedDispatcher.dispatch({ ...askRequest, id: 'rpc_ask_2' })
|
||||
expect(recovered).toMatchObject({
|
||||
ok: true,
|
||||
@@ -304,6 +316,7 @@ describe('durable orchestration mutation ledger', () => {
|
||||
|
||||
controller.abort()
|
||||
await first
|
||||
restartedDb.close()
|
||||
db.close()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { FeatureInteractionId } from '../../../shared/feature-interactions'
|
||||
import { isBrowserPaneUiRuntimeRpcParams } from '../../../shared/runtime-rpc-feature-interaction-source'
|
||||
import type { OrcaRuntimeService } from '../orca-runtime'
|
||||
|
||||
export function getRuntimeFeatureInteractionId(
|
||||
method: string,
|
||||
@@ -37,6 +38,25 @@ export function getRuntimeFeatureInteractionId(
|
||||
return method.startsWith('orchestration.') ? 'agent-orchestration' : null
|
||||
}
|
||||
|
||||
export function recordRuntimeFeatureInteraction(
|
||||
runtime: OrcaRuntimeService,
|
||||
method: string,
|
||||
result: unknown,
|
||||
alreadyRecorded?: Set<FeatureInteractionId>,
|
||||
rawParams?: unknown
|
||||
): void {
|
||||
const id = getRuntimeFeatureInteractionId(method, result, rawParams)
|
||||
if (!id || alreadyRecorded?.has(id)) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
runtime.recordFeatureInteraction(id)
|
||||
alreadyRecorded?.add(id)
|
||||
} catch {
|
||||
// Best-effort education state must not break runtime tools.
|
||||
}
|
||||
}
|
||||
|
||||
function hasBooleanResult(value: unknown, key: string): boolean {
|
||||
return (
|
||||
value !== null && typeof value === 'object' && (value as Record<string, unknown>)[key] === true
|
||||
|
||||
Reference in New Issue
Block a user