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

RuntimeRequestService.test.ts

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