From bbfed6c19eb40bc271473849b50b10e21cf878ca Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 29 Jul 2026 04:13:46 +0200 Subject: [PATCH] fix(orchestrator): execute resolved runtime responses Co-authored-by: codex --- .../RuntimeRequestService.test.ts | 72 ++++++++++++++++++- .../orchestration-v2/RuntimeRequestService.ts | 6 +- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/apps/server/src/orchestration-v2/RuntimeRequestService.test.ts b/apps/server/src/orchestration-v2/RuntimeRequestService.test.ts index df8096bbcb..b6420f6e3e 100644 --- a/apps/server/src/orchestration-v2/RuntimeRequestService.test.ts +++ b/apps/server/src/orchestration-v2/RuntimeRequestService.test.ts @@ -11,6 +11,7 @@ import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import type { ProviderAdapterV2RuntimeRequestResponseInput } from "./ProviderAdapter.ts"; import { ProjectionStoreV2 } from "./ProjectionStore.ts"; import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts"; import { @@ -18,11 +19,13 @@ import { RuntimeRequestServiceV2, } from "./RuntimeRequestService.ts"; -it.effect("rejects resolved runtime requests before invoking the live adapter", () => { +it.effect("forwards orchestrator-resolved runtime requests to the live adapter", () => { const threadId = ThreadId.make("thread-runtime-request-resolved"); const providerSessionId = ProviderSessionId.make("provider-session-runtime-request-resolved"); const requestId = RuntimeRequestId.make("request-resolved"); - const respondToRuntimeRequest = vi.fn(() => Effect.void); + const respondToRuntimeRequest = vi.fn( + (_input: ProviderAdapterV2RuntimeRequestResponseInput) => Effect.void, + ); const getSession = vi.fn(() => Effect.succeed( Option.some({ @@ -61,6 +64,69 @@ it.effect("rejects resolved runtime requests before invoking the live adapter", ), ); + return Effect.gen(function* () { + const service = yield* RuntimeRequestServiceV2; + yield* service.respond({ + threadId, + providerSessionId, + requestId, + decision: "accept", + }); + + assert.equal(getSession.mock.calls.length, 1); + assert.equal(respondToRuntimeRequest.mock.calls.length, 1); + assert.deepEqual(respondToRuntimeRequest.mock.calls[0]?.[0], { + requestId, + decision: "accept", + }); + }).pipe(Effect.provide(testLayer)); +}); + +it.effect("rejects expired runtime requests before invoking the live adapter", () => { + const threadId = ThreadId.make("thread-runtime-request-expired"); + const providerSessionId = ProviderSessionId.make("provider-session-runtime-request-expired"); + const requestId = RuntimeRequestId.make("request-expired"); + const respondToRuntimeRequest = vi.fn( + (_input: ProviderAdapterV2RuntimeRequestResponseInput) => Effect.void, + ); + const getSession = vi.fn(() => + Effect.succeed( + Option.some({ + respondToRuntimeRequest, + } as never), + ), + ); + const projection = { + runtimeRequests: [ + { + id: requestId, + nodeId: NodeId.make("node-runtime-request-expired"), + providerTurnId: null, + nativeRequestRef: null, + kind: "command", + status: "expired", + responseCapability: { + type: "live", + providerSessionId, + }, + createdAt: DateTime.makeUnsafe("2026-07-29T00:00:00.000Z"), + resolvedAt: DateTime.makeUnsafe("2026-07-29T00:00:01.000Z"), + }, + ], + } as unknown as OrchestrationV2ThreadProjection; + const testLayer = runtimeRequestServiceLayer.pipe( + Layer.provide( + Layer.mergeAll( + Layer.mock(ProjectionStoreV2)({ + getThreadProjection: () => Effect.succeed(projection), + }), + Layer.mock(ProviderSessionManagerV2)({ + get: getSession, + }), + ), + ), + ); + return Effect.gen(function* () { const service = yield* RuntimeRequestServiceV2; const error = yield* service @@ -72,7 +138,7 @@ it.effect("rejects resolved runtime requests before invoking the live adapter", }) .pipe(Effect.flip); - assert.equal(error.cause, "The runtime request is no longer pending."); + assert.equal(error.cause, "The runtime request is not ready for response execution."); assert.equal(getSession.mock.calls.length, 0); assert.equal(respondToRuntimeRequest.mock.calls.length, 0); }).pipe(Effect.provide(testLayer)); diff --git a/apps/server/src/orchestration-v2/RuntimeRequestService.ts b/apps/server/src/orchestration-v2/RuntimeRequestService.ts index e92fb7a332..81647140d3 100644 --- a/apps/server/src/orchestration-v2/RuntimeRequestService.ts +++ b/apps/server/src/orchestration-v2/RuntimeRequestService.ts @@ -62,11 +62,13 @@ export const layer: Layer.Layer< cause: "The runtime request no longer exists.", }); } - if (request.status !== "pending") { + // Dispatch validates the request while it is pending, then persists the + // resolved projection before this effect is executed. + if (request.status !== "resolved") { return yield* new RuntimeRequestResponseExecutionError({ threadId: input.threadId, requestId: input.requestId, - cause: "The runtime request is no longer pending.", + cause: "The runtime request is not ready for response execution.", }); } if (