Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

RuntimeRequestService.test.ts

301 lines9.8 KB

1import { assert, it, vi } from "@effect/vitest";
2import {
3 NodeId,
4 ProviderSessionId,
5 RuntimeRequestId,
6 ThreadId,
7 type OrchestrationV2RuntimeRequest,
8 type OrchestrationV2ThreadProjection,
9} from "@t3tools/contracts";
10import * as DateTime from "effect/DateTime";
11import * as Effect from "effect/Effect";
12import * as Layer from "effect/Layer";
13import * as Option from "effect/Option";
14​
15import type { ProviderAdapterV2RuntimeRequestResponseInput } from "./ProviderAdapter.ts";
16import { ProjectionStoreV2 } from "./ProjectionStore.ts";
17import {
18 ProviderSessionLookupError,
19 ProviderSessionManagerV2,
20 type ProviderSessionManagerV2Shape,
21} from "./ProviderSessionManager.ts";
22import {
23 layer as runtimeRequestServiceLayer,
24 RuntimeRequestResponseExecutionError,
25 RuntimeRequestServiceV2,
26} from "./RuntimeRequestService.ts";
27​
28function projectionWithRuntimeRequest(
29 runtimeRequest?: OrchestrationV2RuntimeRequest,
30): OrchestrationV2ThreadProjection {
31 return {
32 runtimeRequests: runtimeRequest === undefined ? [] : [runtimeRequest],
33 } as unknown as OrchestrationV2ThreadProjection;
35​
36function resolvedRuntimeRequest(
37 requestId: RuntimeRequestId,
38 providerSessionId: ProviderSessionId,
39): OrchestrationV2RuntimeRequest {
40 return {
41 id: requestId,
42 nodeId: NodeId.make(`node-${requestId}`),
43 providerTurnId: null,
44 nativeRequestRef: null,
45 kind: "command",
46 status: "resolved",
47 responseCapability: {
48 type: "live",
49 providerSessionId,
50 },
51 createdAt: DateTime.makeUnsafe("2026-07-29T00:00:00.000Z"),
52 resolvedAt: DateTime.makeUnsafe("2026-07-29T00:00:01.000Z"),
53 };
55​
56function runtimeRequestTestLayer(
57 projection: OrchestrationV2ThreadProjection,
58 getSession: ProviderSessionManagerV2Shape["get"],
59) {
60 return runtimeRequestServiceLayer.pipe(
61 Layer.provide(
62 Layer.mergeAll(
63 Layer.mock(ProjectionStoreV2)({
64 getRuntimeRequest: (_threadId, requestId) =>
65 Effect.succeed(projection.runtimeRequests.find((request) => request.id === requestId)),
66 }),
67 Layer.mock(ProviderSessionManagerV2)({
68 get: getSession,
69 }),
70 ),
71 ),
72 );
74​
75it.effect("forwards orchestrator-resolved runtime requests to the live adapter", () => {
76 const threadId = ThreadId.make("thread-runtime-request-resolved");
77 const providerSessionId = ProviderSessionId.make("provider-session-runtime-request-resolved");
78 const requestId = RuntimeRequestId.make("request-resolved");
79 const respondToRuntimeRequest = vi.fn(
80 (_input: ProviderAdapterV2RuntimeRequestResponseInput) => Effect.void,
81 );
82 const getSession = vi.fn(() =>
83 Effect.succeed(
84 Option.some({
85 respondToRuntimeRequest,
86 } as never),
87 ),
88 );
89 const projection = {
90 runtimeRequests: [
91 {
92 id: requestId,
93 nodeId: NodeId.make("node-runtime-request-resolved"),
94 providerTurnId: null,
95 nativeRequestRef: null,
96 kind: "command",
97 status: "resolved",
98 responseCapability: {
99 type: "live",
100 providerSessionId,
101 },
102 createdAt: DateTime.makeUnsafe("2026-07-29T00:00:00.000Z"),
103 resolvedAt: DateTime.makeUnsafe("2026-07-29T00:00:01.000Z"),
104 },
105 ],
106 } as unknown as OrchestrationV2ThreadProjection;
107 const testLayer = runtimeRequestServiceLayer.pipe(
108 Layer.provide(
109 Layer.mergeAll(
110 Layer.mock(ProjectionStoreV2)({
111 getRuntimeRequest: (_threadId, requestId) =>
112 Effect.succeed(projection.runtimeRequests.find((request) => request.id === requestId)),
113 }),
114 Layer.mock(ProviderSessionManagerV2)({
115 get: getSession,
116 }),
117 ),
118 ),
119 );
120​
121 return Effect.gen(function* () {
122 const service = yield* RuntimeRequestServiceV2;
123 yield* service.respond({
124 threadId,
125 providerSessionId,
126 requestId,
127 decision: "accept",
128 });
129​
130 assert.equal(getSession.mock.calls.length, 1);
131 assert.equal(respondToRuntimeRequest.mock.calls.length, 1);
132 assert.deepEqual(respondToRuntimeRequest.mock.calls[0]?.[0], {
133 requestId,
134 decision: "accept",
135 });
136 }).pipe(Effect.provide(testLayer));
137});
138​
139it.effect("rejects expired runtime requests before invoking the live adapter", () => {
140 const threadId = ThreadId.make("thread-runtime-request-expired");
141 const providerSessionId = ProviderSessionId.make("provider-session-runtime-request-expired");
142 const requestId = RuntimeRequestId.make("request-expired");
143 const respondToRuntimeRequest = vi.fn(
144 (_input: ProviderAdapterV2RuntimeRequestResponseInput) => Effect.void,
145 );
146 const getSession = vi.fn(() =>
147 Effect.succeed(
148 Option.some({
149 respondToRuntimeRequest,
150 } as never),
151 ),
152 );
153 const projection = {
154 runtimeRequests: [
155 {
156 id: requestId,
157 nodeId: NodeId.make("node-runtime-request-expired"),
158 providerTurnId: null,
159 nativeRequestRef: null,
160 kind: "command",
161 status: "expired",
162 responseCapability: {
163 type: "live",
164 providerSessionId,
165 },
166 createdAt: DateTime.makeUnsafe("2026-07-29T00:00:00.000Z"),
167 resolvedAt: DateTime.makeUnsafe("2026-07-29T00:00:01.000Z"),
168 },
169 ],
170 } as unknown as OrchestrationV2ThreadProjection;
171 const testLayer = runtimeRequestServiceLayer.pipe(
172 Layer.provide(
173 Layer.mergeAll(
174 Layer.mock(ProjectionStoreV2)({
175 getRuntimeRequest: (_threadId, requestId) =>
176 Effect.succeed(projection.runtimeRequests.find((request) => request.id === requestId)),
177 }),
178 Layer.mock(ProviderSessionManagerV2)({
179 get: getSession,
180 }),
181 ),
182 ),
183 );
184​
185 return Effect.gen(function* () {
186 const service = yield* RuntimeRequestServiceV2;
187 const error = yield* service
188 .respond({
189 threadId,
190 providerSessionId,
191 requestId,
192 decision: "accept",
193 })
194 .pipe(Effect.flip);
195​
196 assert.equal(error.reason, "request-not-ready");
197 assert.equal(
198 error.message,
199 `Runtime request ${requestId} on thread ${threadId} is not ready for response execution.`,
200 );
201 assert.isUndefined(error.cause);
202 assert.equal(getSession.mock.calls.length, 0);
203 assert.equal(respondToRuntimeRequest.mock.calls.length, 0);
204 }).pipe(Effect.provide(testLayer));
205});
206​
207it.effect("classifies runtime request response validation failures", () => {
208 const threadId = ThreadId.make("thread-runtime-request-validation");
209 const providerSessionId = ProviderSessionId.make("provider-session-runtime-request-validation");
210 const otherProviderSessionId = ProviderSessionId.make(
211 "provider-session-runtime-request-validation-other",
212 );
213 const requestId = RuntimeRequestId.make("request-validation");
214 const unexpectedGetSession = vi.fn(() => Effect.succeed(Option.none()));
215 const missingLayer = runtimeRequestTestLayer(
216 projectionWithRuntimeRequest(),
217 unexpectedGetSession,
218 );
219 const notResumableLayer = runtimeRequestTestLayer(
220 projectionWithRuntimeRequest(resolvedRuntimeRequest(requestId, otherProviderSessionId)),
221 unexpectedGetSession,
222 );
223 const inactiveLayer = runtimeRequestTestLayer(
224 projectionWithRuntimeRequest(resolvedRuntimeRequest(requestId, providerSessionId)),
225 () => Effect.succeed(Option.none()),
226 );
227 const respond = Effect.gen(function* () {
228 const service = yield* RuntimeRequestServiceV2;
229 return yield* service
230 .respond({
231 threadId,
232 providerSessionId,
233 requestId,
234 decision: "accept",
235 })
236 .pipe(Effect.flip);
237 });
238​
239 return Effect.gen(function* () {
240 const missing = yield* respond.pipe(Effect.provide(missingLayer));
241 assert.equal(missing.reason, "request-missing");
242 assert.equal(
243 missing.message,
244 `Runtime request ${requestId} no longer exists on thread ${threadId}.`,
245 );
246 assert.isUndefined(missing.cause);
247​
248 const notResumable = yield* respond.pipe(Effect.provide(notResumableLayer));
249 assert.equal(notResumable.reason, "request-not-resumable");
250 assert.equal(
251 notResumable.message,
252 `Runtime request ${requestId} on thread ${threadId} is not resumable on provider session ${providerSessionId}.`,
253 );
254 assert.isUndefined(notResumable.cause);
255​
256 const inactive = yield* respond.pipe(Effect.provide(inactiveLayer));
257 assert.equal(inactive.reason, "provider-session-not-active");
258 assert.equal(
259 inactive.message,
260 `Provider session ${providerSessionId} is not active for runtime request ${requestId} on thread ${threadId}.`,
261 );
262 assert.isUndefined(inactive.cause);
263 assert.equal(unexpectedGetSession.mock.calls.length, 0);
264 });
265});
266​
267it.effect("preserves genuine provider session lookup failures as the cause", () => {
268 const threadId = ThreadId.make("thread-runtime-request-lookup-failure");
269 const providerSessionId = ProviderSessionId.make(
270 "provider-session-runtime-request-lookup-failure",
271 );
272 const requestId = RuntimeRequestId.make("request-lookup-failure");
273 const lookupFailure = new ProviderSessionLookupError({
274 providerSessionId,
275 cause: "lookup failed",
276 });
277 const testLayer = runtimeRequestTestLayer(
278 projectionWithRuntimeRequest(resolvedRuntimeRequest(requestId, providerSessionId)),
279 () => Effect.fail(lookupFailure),
280 );
281​
282 return Effect.gen(function* () {
283 const service = yield* RuntimeRequestServiceV2;
284 const error = yield* service
285 .respond({
286 threadId,
287 providerSessionId,
288 requestId,
289 decision: "accept",
290 })
291 .pipe(Effect.flip);
292​
293 assert.instanceOf(error, RuntimeRequestResponseExecutionError);
294 assert.equal(error.reason, "unexpected-failure");
295 assert.equal(
296 error.message,
297 `Failed to respond to runtime request ${requestId} on thread ${threadId} via provider session ${providerSessionId}.`,
298 );
299 assert.strictEqual(error.cause, lookupFailure);
300 }).pipe(Effect.provide(testLayer));
301});