Implement robust orchestration primitives and connected-server workers (#9925)

This commit is contained in:
OrcaWin
2026-07-27 12:31:37 -07:00
committed by GitHub
parent 8b154d686c
commit cd05f2ff93
127 changed files with 23196 additions and 887 deletions
+55 -15
View File
@@ -26,6 +26,7 @@ import {
import { decrypt, deriveSharedKey, encrypt, generateKeyPair } from './rpc/e2ee-crypto'
import { DeviceRegistry } from './device-registry'
import { DEVICE_REGISTRY_FILENAME, E2EE_KEYPAIR_FILENAME } from './mobile-pairing-files'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../shared/protocol-version'
vi.mock('../git/worktree', () => ({
listWorktrees: vi.fn().mockResolvedValue([
@@ -60,7 +61,7 @@ async function sendRequest(
resolve(JSON.parse(message) as Record<string, unknown>)
})
socket.on('connect', () => {
socket.write(`${JSON.stringify(request)}\n`)
socket.write(`${JSON.stringify(withCurrentOrchestrationContract(request))}\n`)
})
})
}
@@ -111,12 +112,20 @@ function openFramedSession(endpoint: string, request: Record<string, unknown>):
}
})
socket.on('connect', () => {
socket.write(`${JSON.stringify(request)}\n`)
socket.write(`${JSON.stringify(withCurrentOrchestrationContract(request))}\n`)
})
})
return { socket, frames, done }
}
function withCurrentOrchestrationContract(
request: Record<string, unknown>
): Record<string, unknown> {
return typeof request.method === 'string' && request.method.startsWith('orchestration.')
? { ...request, orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION }
: request
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
@@ -131,6 +140,18 @@ async function waitFor(predicate: () => boolean, timeoutMs = 2_000): Promise<voi
}
}
function seedSupervisedAskWorkers(db: OrchestrationDb, workerHandles: string[]): void {
const run = db.createRun({
objective: 'Exercise ask admission',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
})
for (const workerHandle of workerHandles) {
const task = db.createTask({ spec: 'Wait for coordinator input', runId: run.id })
db.createDispatchContext(task.id, workerHandle)
}
}
function connectWs(endpoint: string): Promise<WebSocket> {
return new Promise((resolve, reject) => {
const ws = new WebSocket(endpoint)
@@ -1285,12 +1306,14 @@ describe('OrcaRuntimeRpcServer', () => {
try {
const first = server['handleWebSocketMessage'](
JSON.stringify({
id: 'req_wait',
method: 'orchestration.check',
deviceToken: entry.token,
params: { terminal: 'term_wait', wait: true, timeoutMs: 10_000 }
}),
JSON.stringify(
withCurrentOrchestrationContract({
id: 'req_wait',
method: 'orchestration.check',
deviceToken: entry.token,
params: { terminal: 'term_wait', wait: true, timeoutMs: 10_000 }
})
),
(response) => replies.push(JSON.parse(response) as Record<string, unknown>),
() => {},
undefined,
@@ -1300,12 +1323,14 @@ describe('OrcaRuntimeRpcServer', () => {
await waitFor(() => server['activeLongPolls'] === 1)
await server['handleWebSocketMessage'](
JSON.stringify({
id: 'req_busy',
method: 'orchestration.check',
deviceToken: entry.token,
params: { terminal: 'term_busy', wait: true, timeoutMs: 10_000 }
}),
JSON.stringify(
withCurrentOrchestrationContract({
id: 'req_busy',
method: 'orchestration.check',
deviceToken: entry.token,
params: { terminal: 'term_busy', wait: true, timeoutMs: 10_000 }
})
),
(response) => replies.push(JSON.parse(response) as Record<string, unknown>),
() => {},
undefined,
@@ -1338,6 +1363,7 @@ describe('OrcaRuntimeRpcServer', () => {
const runtime = new OrcaRuntimeService()
const db = new OrchestrationDb(':memory:')
runtime.setOrchestrationDb(db)
seedSupervisedAskWorkers(db, ['term_w0', 'term_w1', 'term_w2'])
// Why: cap 4 → ask sub-cap 2, so the third ask must be shed while waits keep the other half.
const server = new OrcaRuntimeRpcServer({
runtime,
@@ -1358,7 +1384,9 @@ describe('OrcaRuntimeRpcServer', () => {
}
const dispatch = (id: string, method: string, params: unknown): Promise<void> =>
server['handleWebSocketMessage'](
JSON.stringify({ id, method, deviceToken: entry.token, params }),
JSON.stringify(
withCurrentOrchestrationContract({ id, method, deviceToken: entry.token, params })
),
push,
() => {},
undefined,
@@ -4032,6 +4060,17 @@ describe('OrcaRuntimeRpcServer', () => {
const runtime = new OrcaRuntimeService()
const db = new OrchestrationDb(':memory:')
runtime.setOrchestrationDb(db)
const askerPaneKey = 'tab_asker:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
vi.spyOn(runtime, 'getTerminalPaneKey').mockImplementation((handle) =>
handle === 'term_asker' ? askerPaneKey : null
)
const run = db.createRun({
objective: 'Keepalive test',
coordinatorHandle: 'term_nobody',
coordinatorPaneKey: 'tab_coord:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
})
const task = db.createTask({ spec: 'Wait for an answer', runId: run.id })
db.createDispatchContext(task.id, 'term_asker', askerPaneKey)
const server = new OrcaRuntimeRpcServer({
runtime,
userDataPath,
@@ -4380,6 +4419,7 @@ describe('OrcaRuntimeRpcServer', () => {
const runtime = new OrcaRuntimeService()
const db = new OrchestrationDb(':memory:')
runtime.setOrchestrationDb(db)
seedSupervisedAskWorkers(db, ['term_w0', 'term_w1', 'term_w2', 'term_w3'])
// Why: cap 4 → ask sub-cap 2, so 4 concurrent asks can only take half the budget.
const server = new OrcaRuntimeRpcServer({
runtime,