The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import { |
| 3 | CommandId, |
| 4 | MessageId, |
| 5 | ProjectId, |
| 6 | ProviderDriverKind, |
| 7 | ProviderInstanceId, |
| 8 | ProviderThreadId, |
| 9 | RunId, |
| 10 | ThreadId, |
| 11 | type OrchestrationV2ThreadProjection, |
| 12 | type OrchestrationV2Notification, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import * as Clock from "effect/Clock"; |
| 15 | import * as Deferred from "effect/Deferred"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Exit from "effect/Exit"; |
| 18 | import * as Layer from "effect/Layer"; |
| 19 | import * as Option from "effect/Option"; |
| 20 | import * as Queue from "effect/Queue"; |
| 21 | import * as Ref from "effect/Ref"; |
| 22 | import * as Semaphore from "effect/Semaphore"; |
| 23 | import * as TestClock from "effect/testing/TestClock"; |
| 24 | |
| 25 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
| 26 | import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; |
| 27 | import * as Orchestrator from "./Orchestrator.ts"; |
| 28 | import * as ProviderContinuationService from "./ProviderContinuationService.ts"; |
| 29 | import * as ThreadManagementService from "./ThreadManagementService.ts"; |
| 30 | |
| 31 | const threadId = ThreadId.make("thread-provider-continuation"); |
| 32 | const providerThreadId = ProviderThreadId.make("provider-thread-continuation"); |
| 33 | const driver = ProviderDriverKind.make("continuation-test"); |
| 34 | const parentRunId = RunId.make("run-provider-continuation-parent"); |
| 35 | const delegatedMessageId = MessageId.make("message-provider-continuation-delegated"); |
| 36 | const projection = { |
| 37 | thread: { archivedAt: null, deletedAt: null }, |
| 38 | messages: [], |
| 39 | } as unknown as OrchestrationV2ThreadProjection; |
| 40 | |
| 41 | const request = ( |
| 42 | dispatchIfCurrent?: ProviderContinuationRequests.ProviderContinuationRequest["dispatchIfCurrent"], |
| 43 | detail: string | null = null, |
| 44 | ): ProviderContinuationRequests.ProviderContinuationRequest => ({ |
| 45 | threadId, |
| 46 | providerThreadId, |
| 47 | driver, |
| 48 | detail, |
| 49 | ...(dispatchIfCurrent === undefined ? {} : { dispatchIfCurrent }), |
| 50 | }); |
| 51 | |
| 52 | const delegatedProjection = (disposition: "open" | "stopped" | "disposed" = "open") => |
| 53 | ({ |
| 54 | ...projection, |
| 55 | runs: [ |
| 56 | { |
| 57 | id: parentRunId, |
| 58 | delegatedCompletion: { |
| 59 | disposition, |
| 60 | nextGeneration: 2, |
| 61 | delivery: { |
| 62 | generation: 1, |
| 63 | messageId: delegatedMessageId, |
| 64 | taskIds: ["node-provider-continuation-first", "node-provider-continuation-second"], |
| 65 | }, |
| 66 | }, |
| 67 | }, |
| 68 | ], |
| 69 | }) as unknown as OrchestrationV2ThreadProjection; |
| 70 | |
| 71 | const makeGuard = Effect.fnUntraced(function* (completed?: Deferred.Deferred<void>) { |
| 72 | const generation = yield* Ref.make(0); |
| 73 | const permit = yield* Semaphore.make(1); |
| 74 | const capture = Effect.gen(function* () { |
| 75 | const captured = yield* Ref.updateAndGet(generation, (value) => value + 1); |
| 76 | return <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 77 | permit |
| 78 | .withPermit( |
| 79 | Effect.gen(function* () { |
| 80 | if ((yield* Ref.get(generation)) !== captured) return Option.none(); |
| 81 | return Option.some(yield* effect); |
| 82 | }), |
| 83 | ) |
| 84 | .pipe( |
| 85 | completed === undefined |
| 86 | ? (effect) => effect |
| 87 | : Effect.ensuring(Deferred.succeed(completed, undefined)), |
| 88 | ); |
| 89 | }); |
| 90 | return { |
| 91 | capture, |
| 92 | invalidate: permit.withPermit(Ref.update(generation, (value) => value + 1)), |
| 93 | }; |
| 94 | }); |
| 95 | |
| 96 | function layerTest(input: { |
| 97 | readonly dispatched: Queue.Queue<unknown>; |
| 98 | readonly getThreadRecords: () => Effect.Effect<OrchestrationV2ThreadProjection>; |
| 99 | readonly beforeDispatch?: (threadId: ThreadId) => Effect.Effect<void>; |
| 100 | }) { |
| 101 | const layerThreads = Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 102 | getThreadRecords: input.getThreadRecords, |
| 103 | dispatch: (command) => |
| 104 | ( |
| 105 | ("threadId" in command ? input.beforeDispatch?.(command.threadId) : undefined) ?? |
| 106 | Effect.void |
| 107 | ).pipe(Effect.andThen(Queue.offer(input.dispatched, command)), Effect.as({} as never)), |
| 108 | }); |
| 109 | const layerWorker = ProviderContinuationService.layer.pipe( |
| 110 | Layer.provide( |
| 111 | Layer.mergeAll(IdAllocator.layer, ProviderContinuationRequests.layer, layerThreads), |
| 112 | ), |
| 113 | ); |
| 114 | return Layer.merge(ProviderContinuationRequests.layer, layerWorker); |
| 115 | } |
| 116 | |
| 117 | describe("ProviderContinuationService", () => { |
| 118 | it.effect("recovers an unaccepted persisted steer using the same delivery identity", () => |
| 119 | Effect.gen(function* () { |
| 120 | const dispatched = yield* Queue.unbounded<unknown>(); |
| 121 | const original = delegatedProjection(); |
| 122 | const interruptedRunId = RunId.make("interrupted-mailbox-run"); |
| 123 | const recovered = { |
| 124 | ...original, |
| 125 | runs: [ |
| 126 | ...original.runs, |
| 127 | { |
| 128 | id: interruptedRunId, |
| 129 | status: "interrupted", |
| 130 | userMessageId: MessageId.make("original-user-input"), |
| 131 | }, |
| 132 | ], |
| 133 | messages: [ |
| 134 | { |
| 135 | id: delegatedMessageId, |
| 136 | runId: interruptedRunId, |
| 137 | delegatedCompletion: { parentRunId, generation: 1, taskIds: [] }, |
| 138 | }, |
| 139 | ], |
| 140 | } as unknown as OrchestrationV2ThreadProjection; |
| 141 | yield* Effect.gen(function* () { |
| 142 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 143 | yield* requests.offer({ |
| 144 | threadId, |
| 145 | providerThreadId, |
| 146 | driver, |
| 147 | detail: null, |
| 148 | delivery: "message_text", |
| 149 | delegatedCompletion: { parentRunId, generation: 1, messageId: delegatedMessageId }, |
| 150 | }); |
| 151 | const command = (yield* Queue.take(dispatched)) as { |
| 152 | messageId: MessageId; |
| 153 | delegatedCompletion: { generation: number; taskIds: readonly string[] }; |
| 154 | }; |
| 155 | assert.equal(command.messageId, delegatedMessageId); |
| 156 | assert.equal(command.delegatedCompletion.generation, 1); |
| 157 | assert.equal(command.delegatedCompletion.taskIds.length, 2); |
| 158 | }).pipe( |
| 159 | Effect.provide( |
| 160 | layerTest({ dispatched, getThreadRecords: () => Effect.succeed(recovered) }), |
| 161 | ), |
| 162 | Effect.scoped, |
| 163 | ); |
| 164 | }), |
| 165 | ); |
| 166 | |
| 167 | it.effect("delivers a message_text wake as a real prompt, not a buffered wake", () => { |
| 168 | return Effect.gen(function* () { |
| 169 | const dispatched = yield* Queue.unbounded<unknown>(); |
| 170 | yield* Effect.gen(function* () { |
| 171 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 172 | yield* requests.offer({ |
| 173 | threadId, |
| 174 | providerThreadId, |
| 175 | driver, |
| 176 | detail: "Delegated task completed.", |
| 177 | notification: { |
| 178 | source: { kind: "monitor" }, |
| 179 | outcome: "updated", |
| 180 | summary: "Monitor updated", |
| 181 | }, |
| 182 | delivery: "message_text", |
| 183 | }); |
| 184 | const command = (yield* Queue.take(dispatched)) as { |
| 185 | readonly creationSource: string; |
| 186 | readonly text: string; |
| 187 | readonly notification: OrchestrationV2Notification; |
| 188 | }; |
| 189 | assert.notEqual(command.creationSource, "provider"); |
| 190 | assert.equal(command.creationSource, "server"); |
| 191 | assert.equal(command.text, "Delegated task completed."); |
| 192 | assert.deepEqual(command.notification, { |
| 193 | source: { kind: "monitor" }, |
| 194 | outcome: "updated", |
| 195 | summary: "Monitor updated", |
| 196 | }); |
| 197 | }).pipe( |
| 198 | Effect.provide( |
| 199 | layerTest({ dispatched, getThreadRecords: () => Effect.succeed(projection) }), |
| 200 | ), |
| 201 | Effect.scoped, |
| 202 | ); |
| 203 | }); |
| 204 | }); |
| 205 | |
| 206 | it.effect("dispatches a current delegated completion as one server-owned queued message", () => { |
| 207 | return Effect.gen(function* () { |
| 208 | const dispatched = yield* Queue.unbounded<unknown>(); |
| 209 | yield* Effect.gen(function* () { |
| 210 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 211 | yield* requests.offer({ |
| 212 | threadId, |
| 213 | providerThreadId, |
| 214 | driver, |
| 215 | detail: null, |
| 216 | delivery: "message_text", |
| 217 | delegatedCompletion: { |
| 218 | parentRunId, |
| 219 | generation: 1, |
| 220 | messageId: delegatedMessageId, |
| 221 | }, |
| 222 | }); |
| 223 | const command = (yield* Queue.take(dispatched)) as { |
| 224 | readonly createdBy: string; |
| 225 | readonly creationSource: string; |
| 226 | readonly delegatedCompletion: { |
| 227 | readonly generation: number; |
| 228 | readonly parentRunId: RunId; |
| 229 | readonly taskIds: ReadonlyArray<string>; |
| 230 | }; |
| 231 | readonly dispatchMode: { readonly type: string }; |
| 232 | readonly messageId: MessageId; |
| 233 | readonly text: string; |
| 234 | }; |
| 235 | assert.equal(command.createdBy, "agent"); |
| 236 | assert.equal(command.creationSource, "server"); |
| 237 | assert.deepEqual(command.dispatchMode, { type: "queue_after_active" }); |
| 238 | assert.equal(command.messageId, delegatedMessageId); |
| 239 | assert.equal(command.delegatedCompletion.parentRunId, parentRunId); |
| 240 | assert.equal(command.delegatedCompletion.generation, 1); |
| 241 | assert.deepEqual(command.delegatedCompletion.taskIds, [ |
| 242 | "node-provider-continuation-first", |
| 243 | "node-provider-continuation-second", |
| 244 | ]); |
| 245 | assert.include(command.text, "task_status"); |
| 246 | }).pipe( |
| 247 | Effect.provide( |
| 248 | layerTest({ |
| 249 | dispatched, |
| 250 | getThreadRecords: () => Effect.succeed(delegatedProjection()), |
| 251 | }), |
| 252 | ), |
| 253 | Effect.scoped, |
| 254 | ); |
| 255 | }); |
| 256 | }); |
| 257 | |
| 258 | it.effect( |
| 259 | "a project-disabled instance refuses a delegated-completion wake exactly once, disposes it, and never retries", |
| 260 | () => { |
| 261 | return Effect.gen(function* () { |
| 262 | const attempts = yield* Ref.make(0); |
| 263 | const cleared = yield* Ref.make(false); |
| 264 | const disposed = yield* Queue.unbounded<unknown>(); |
| 265 | const layerThreads = Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 266 | getThreadRecords: () => Effect.succeed(delegatedProjection()), |
| 267 | dispatch: (command) => { |
| 268 | if ((command as { type: string }).type === "message.dispatch") { |
| 269 | return Ref.update(attempts, (count) => count + 1).pipe( |
| 270 | Effect.andThen( |
| 271 | Effect.fail( |
| 272 | new ThreadManagementService.ThreadManagementProviderInstanceDisabledError({ |
| 273 | projectId: ProjectId.make("project-provider-continuation-disabled"), |
| 274 | instanceId: ProviderInstanceId.make( |
| 275 | "instance-provider-continuation-disabled", |
| 276 | ), |
| 277 | }) as never, |
| 278 | ), |
| 279 | ), |
| 280 | ); |
| 281 | } |
| 282 | return Queue.offer(disposed, command).pipe(Effect.as({} as never)); |
| 283 | }, |
| 284 | }); |
| 285 | const layerWorker = ProviderContinuationService.layer.pipe( |
| 286 | Layer.provide( |
| 287 | Layer.mergeAll(IdAllocator.layer, ProviderContinuationRequests.layer, layerThreads), |
| 288 | ), |
| 289 | ); |
| 290 | |
| 291 | yield* Effect.gen(function* () { |
| 292 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 293 | yield* requests.offer({ |
| 294 | threadId, |
| 295 | providerThreadId, |
| 296 | driver, |
| 297 | detail: null, |
| 298 | delivery: "message_text", |
| 299 | clearIfCurrent: () => Ref.set(cleared, true), |
| 300 | delegatedCompletion: { |
| 301 | parentRunId, |
| 302 | generation: 1, |
| 303 | messageId: delegatedMessageId, |
| 304 | }, |
| 305 | }); |
| 306 | yield* Effect.yieldNow; |
| 307 | assert.equal(yield* Ref.get(attempts), 1); |
| 308 | assert.isTrue(yield* Ref.get(cleared)); |
| 309 | |
| 310 | const dispositions = (yield* Queue.takeAll(disposed)) as ReadonlyArray<{ |
| 311 | type: string; |
| 312 | taskId: string; |
| 313 | }>; |
| 314 | assert.deepEqual( |
| 315 | dispositions.map((command) => command.taskId), |
| 316 | ["node-provider-continuation-first", "node-provider-continuation-second"], |
| 317 | ); |
| 318 | for (const command of dispositions) { |
| 319 | assert.equal(command.type, "delegated_task.completion-delivery.dispose"); |
| 320 | } |
| 321 | |
| 322 | yield* TestClock.adjust("1 hour"); |
| 323 | yield* Effect.yieldNow; |
| 324 | assert.equal(yield* Ref.get(attempts), 1); |
| 325 | assert.equal(yield* Queue.size(disposed), 0); |
| 326 | }).pipe( |
| 327 | Effect.provide(Layer.merge(ProviderContinuationRequests.layer, layerWorker)), |
| 328 | Effect.scoped, |
| 329 | ); |
| 330 | }); |
| 331 | }, |
| 332 | ); |
| 333 | |
| 334 | it.effect( |
| 335 | "clears the adapter's sticky continuation flag when a non-delegated wake is refused", |
| 336 | () => { |
| 337 | return Effect.gen(function* () { |
| 338 | const requested = yield* Ref.make(true); |
| 339 | const layerThreads = Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 340 | getThreadRecords: () => Effect.succeed(projection), |
| 341 | dispatch: () => |
| 342 | Effect.fail( |
| 343 | new ThreadManagementService.ThreadManagementProviderInstanceDisabledError({ |
| 344 | projectId: ProjectId.make("project-provider-continuation-disabled"), |
| 345 | instanceId: ProviderInstanceId.make("instance-provider-continuation-disabled"), |
| 346 | }) as never, |
| 347 | ), |
| 348 | }); |
| 349 | const layerWorker = ProviderContinuationService.layer.pipe( |
| 350 | Layer.provide( |
| 351 | Layer.mergeAll(IdAllocator.layer, ProviderContinuationRequests.layer, layerThreads), |
| 352 | ), |
| 353 | ); |
| 354 | |
| 355 | // ACP clears its sticky continuation flag on raw dispatch failure; pre-recovery would prevent that cleanup. |
| 356 | const dispatchIfCurrent = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 357 | Effect.gen(function* () { |
| 358 | const exit = yield* Effect.exit(effect); |
| 359 | if (Exit.isFailure(exit)) { |
| 360 | yield* Ref.set(requested, false); |
| 361 | return yield* Effect.failCause(exit.cause); |
| 362 | } |
| 363 | return Option.some(exit.value); |
| 364 | }); |
| 365 | |
| 366 | yield* Effect.gen(function* () { |
| 367 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 368 | yield* requests.offer(request(dispatchIfCurrent)); |
| 369 | yield* Effect.yieldNow; |
| 370 | yield* Effect.yieldNow; |
| 371 | assert.isFalse( |
| 372 | yield* Ref.get(requested), |
| 373 | "a disabled instance must still clear the adapter's sticky offer flag", |
| 374 | ); |
| 375 | }).pipe( |
| 376 | Effect.provide(Layer.merge(ProviderContinuationRequests.layer, layerWorker)), |
| 377 | Effect.scoped, |
| 378 | ); |
| 379 | }); |
| 380 | }, |
| 381 | ); |
| 382 | |
| 383 | it.effect( |
| 384 | "clears a sticky continuation offer for an adapter whose dispatchIfCurrent does not self-clear on failure", |
| 385 | () => { |
| 386 | return Effect.gen(function* () { |
| 387 | const wakeHeld = yield* Ref.make(true); |
| 388 | const layerThreads = Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 389 | getThreadRecords: () => Effect.succeed(projection), |
| 390 | dispatch: () => |
| 391 | Effect.fail( |
| 392 | new ThreadManagementService.ThreadManagementProviderInstanceDisabledError({ |
| 393 | projectId: ProjectId.make("project-provider-continuation-disabled"), |
| 394 | instanceId: ProviderInstanceId.make("instance-provider-continuation-disabled"), |
| 395 | }) as never, |
| 396 | ), |
| 397 | }); |
| 398 | const layerWorker = ProviderContinuationService.layer.pipe( |
| 399 | Layer.provide( |
| 400 | Layer.mergeAll(IdAllocator.layer, ProviderContinuationRequests.layer, layerThreads), |
| 401 | ), |
| 402 | ); |
| 403 | |
| 404 | // Muse forwards dispatch failures unchanged; clearIfCurrent must release its sticky wake flag. |
| 405 | const dispatchIfCurrent = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 406 | Effect.asSome(effect); |
| 407 | |
| 408 | yield* Effect.gen(function* () { |
| 409 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 410 | yield* requests.offer({ |
| 411 | ...request(dispatchIfCurrent), |
| 412 | clearIfCurrent: () => Ref.set(wakeHeld, false), |
| 413 | }); |
| 414 | yield* Effect.yieldNow; |
| 415 | yield* Effect.yieldNow; |
| 416 | assert.isFalse( |
| 417 | yield* Ref.get(wakeHeld), |
| 418 | "a disabled instance must clear the offer via the explicit callback " + |
| 419 | "when the adapter itself does not clear on dispatch failure", |
| 420 | ); |
| 421 | }).pipe( |
| 422 | Effect.provide(Layer.merge(ProviderContinuationRequests.layer, layerWorker)), |
| 423 | Effect.scoped, |
| 424 | ); |
| 425 | }); |
| 426 | }, |
| 427 | ); |
| 428 | |
| 429 | it.effect( |
| 430 | "clears a sticky continuation offer when a non-delegated wake dispatch fails generically", |
| 431 | () => { |
| 432 | return Effect.gen(function* () { |
| 433 | const cleared = yield* Deferred.make<void>(); |
| 434 | const layerThreads = Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 435 | getThreadRecords: () => Effect.succeed(projection), |
| 436 | dispatch: (command) => |
| 437 | Effect.fail( |
| 438 | new Orchestrator.OrchestratorDispatchError({ |
| 439 | commandId: CommandId.make(String(command.commandId)), |
| 440 | commandType: command.type, |
| 441 | cause: new Error("SQLITE_FULL: database or disk is full"), |
| 442 | }), |
| 443 | ), |
| 444 | }); |
| 445 | const layerWorker = ProviderContinuationService.layer.pipe( |
| 446 | Layer.provide( |
| 447 | Layer.mergeAll(IdAllocator.layer, ProviderContinuationRequests.layer, layerThreads), |
| 448 | ), |
| 449 | ); |
| 450 | |
| 451 | yield* Effect.gen(function* () { |
| 452 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 453 | yield* requests.offer({ |
| 454 | ...request(), |
| 455 | clearIfCurrent: () => Deferred.succeed(cleared, undefined), |
| 456 | }); |
| 457 | yield* Deferred.await(cleared); |
| 458 | }).pipe( |
| 459 | Effect.provide(Layer.merge(ProviderContinuationRequests.layer, layerWorker)), |
| 460 | Effect.scoped, |
| 461 | ); |
| 462 | }); |
| 463 | }, |
| 464 | ); |
| 465 | |
| 466 | it.effect( |
| 467 | "drops stopped and disposed delegated completions instead of reviving them after recovery", |
| 468 | () => { |
| 469 | return Effect.gen(function* () { |
| 470 | for (const disposition of ["stopped", "disposed"] as const) { |
| 471 | const dispatched = yield* Queue.unbounded<unknown>(); |
| 472 | yield* Effect.gen(function* () { |
| 473 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 474 | yield* requests.offer({ |
| 475 | threadId, |
| 476 | providerThreadId, |
| 477 | driver, |
| 478 | detail: null, |
| 479 | delivery: "message_text", |
| 480 | delegatedCompletion: { |
| 481 | parentRunId, |
| 482 | generation: 1, |
| 483 | messageId: delegatedMessageId, |
| 484 | }, |
| 485 | }); |
| 486 | yield* Effect.yieldNow; |
| 487 | yield* Effect.yieldNow; |
| 488 | assert.isTrue(Option.isNone(yield* Queue.poll(dispatched))); |
| 489 | }).pipe( |
| 490 | Effect.provide( |
| 491 | layerTest({ |
| 492 | dispatched, |
| 493 | getThreadRecords: () => Effect.succeed(delegatedProjection(disposition)), |
| 494 | }), |
| 495 | ), |
| 496 | Effect.scoped, |
| 497 | ); |
| 498 | } |
| 499 | }); |
| 500 | }, |
| 501 | ); |
| 502 | |
| 503 | it.effect("does not redispatch a persisted delegated completion message during recovery", () => { |
| 504 | return Effect.gen(function* () { |
| 505 | const dispatched = yield* Queue.unbounded<unknown>(); |
| 506 | yield* Effect.gen(function* () { |
| 507 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 508 | yield* requests.offer({ |
| 509 | threadId, |
| 510 | providerThreadId, |
| 511 | driver, |
| 512 | detail: null, |
| 513 | delivery: "message_text", |
| 514 | delegatedCompletion: { |
| 515 | parentRunId, |
| 516 | generation: 1, |
| 517 | messageId: delegatedMessageId, |
| 518 | }, |
| 519 | }); |
| 520 | yield* Effect.yieldNow; |
| 521 | yield* Effect.yieldNow; |
| 522 | assert.isTrue(Option.isNone(yield* Queue.poll(dispatched))); |
| 523 | }).pipe( |
| 524 | Effect.provide( |
| 525 | layerTest({ |
| 526 | dispatched, |
| 527 | getThreadRecords: () => |
| 528 | Effect.succeed({ |
| 529 | ...delegatedProjection(), |
| 530 | messages: [{ id: delegatedMessageId }], |
| 531 | } as unknown as OrchestrationV2ThreadProjection), |
| 532 | }), |
| 533 | ), |
| 534 | Effect.scoped, |
| 535 | ); |
| 536 | }); |
| 537 | }); |
| 538 | |
| 539 | it.effect("dispatches a current request exactly once", () => { |
| 540 | return Effect.gen(function* () { |
| 541 | const dispatched = yield* Queue.unbounded<unknown>(); |
| 542 | yield* Effect.gen(function* () { |
| 543 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 544 | yield* requests.offer(request()); |
| 545 | yield* Queue.take(dispatched); |
| 546 | yield* Effect.yieldNow; |
| 547 | assert.isTrue(Option.isNone(yield* Queue.poll(dispatched))); |
| 548 | }).pipe( |
| 549 | Effect.provide( |
| 550 | layerTest({ dispatched, getThreadRecords: () => Effect.succeed(projection) }), |
| 551 | ), |
| 552 | Effect.scoped, |
| 553 | ); |
| 554 | }); |
| 555 | }); |
| 556 | |
| 557 | it.effect("drops a request invalidated while projection is blocked", () => { |
| 558 | return Effect.gen(function* () { |
| 559 | const dispatched = yield* Queue.unbounded<unknown>(); |
| 560 | const projectionEntered = yield* Deferred.make<void>(); |
| 561 | const releaseProjection = yield* Deferred.make<void>(); |
| 562 | const guardCompleted = yield* Deferred.make<void>(); |
| 563 | yield* Effect.gen(function* () { |
| 564 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 565 | const guard = yield* makeGuard(guardCompleted); |
| 566 | const dispatchIfCurrent = yield* guard.capture; |
| 567 | yield* requests.offer(request(dispatchIfCurrent)); |
| 568 | yield* Deferred.await(projectionEntered); |
| 569 | yield* guard.invalidate; |
| 570 | yield* Deferred.succeed(releaseProjection, undefined); |
| 571 | yield* Deferred.await(guardCompleted); |
| 572 | assert.isTrue(Option.isNone(yield* Queue.poll(dispatched))); |
| 573 | }).pipe( |
| 574 | Effect.provide( |
| 575 | layerTest({ |
| 576 | dispatched, |
| 577 | getThreadRecords: () => |
| 578 | Deferred.succeed(projectionEntered, undefined).pipe( |
| 579 | Effect.andThen(Deferred.await(releaseProjection)), |
| 580 | Effect.as(projection), |
| 581 | ), |
| 582 | }), |
| 583 | ), |
| 584 | Effect.scoped, |
| 585 | ); |
| 586 | }); |
| 587 | }); |
| 588 | |
| 589 | it.effect("does not revive an old request when a later generation is current", () => { |
| 590 | return Effect.gen(function* () { |
| 591 | const dispatched = yield* Queue.unbounded<unknown>(); |
| 592 | const firstProjectionEntered = yield* Deferred.make<void>(); |
| 593 | const releaseFirstProjection = yield* Deferred.make<void>(); |
| 594 | let projectionCalls = 0; |
| 595 | yield* Effect.gen(function* () { |
| 596 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 597 | const guard = yield* makeGuard(); |
| 598 | const first = yield* guard.capture; |
| 599 | yield* requests.offer(request(first, "A")); |
| 600 | yield* Deferred.await(firstProjectionEntered); |
| 601 | const second = yield* guard.capture; |
| 602 | yield* requests.offer(request(second, "B")); |
| 603 | yield* Deferred.succeed(releaseFirstProjection, undefined); |
| 604 | const command = yield* Queue.take(dispatched); |
| 605 | assert.equal((command as { readonly text?: unknown }).text, "B"); |
| 606 | yield* Effect.yieldNow; |
| 607 | assert.isTrue(Option.isNone(yield* Queue.poll(dispatched))); |
| 608 | }).pipe( |
| 609 | Effect.provide( |
| 610 | layerTest({ |
| 611 | dispatched, |
| 612 | getThreadRecords: () => { |
| 613 | projectionCalls += 1; |
| 614 | return projectionCalls === 1 |
| 615 | ? Deferred.succeed(firstProjectionEntered, undefined).pipe( |
| 616 | Effect.andThen(Deferred.await(releaseFirstProjection)), |
| 617 | Effect.as(projection), |
| 618 | ) |
| 619 | : Effect.succeed(projection); |
| 620 | }, |
| 621 | }), |
| 622 | ), |
| 623 | Effect.scoped, |
| 624 | ); |
| 625 | }); |
| 626 | }); |
| 627 | it.effect( |
| 628 | "a wake held by another thread's parent lock does not delay other threads' wakes", |
| 629 | () => { |
| 630 | return Effect.gen(function* () { |
| 631 | const dispatched = yield* Queue.unbounded<unknown>(); |
| 632 | const blockedChildId = ThreadId.make("thread-provider-continuation-blocked-child"); |
| 633 | const dispatchedAt = new Map<ThreadId, number>(); |
| 634 | yield* Effect.gen(function* () { |
| 635 | const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 636 | yield* requests.offer({ ...request(), threadId: blockedChildId }); |
| 637 | yield* requests.offer(request()); |
| 638 | yield* TestClock.adjust("10 minutes"); |
| 639 | yield* Queue.take(dispatched); |
| 640 | yield* Queue.take(dispatched); |
| 641 | assert.equal(dispatchedAt.get(blockedChildId), 10 * 60 * 1000); |
| 642 | assert.equal(dispatchedAt.get(threadId), 0); |
| 643 | }).pipe( |
| 644 | Effect.provide( |
| 645 | layerTest({ |
| 646 | dispatched, |
| 647 | getThreadRecords: () => Effect.succeed(projection), |
| 648 | // Models the child's message intake waiting on its coordinator's dispatch lock. |
| 649 | beforeDispatch: (dispatchThreadId) => |
| 650 | (dispatchThreadId === blockedChildId |
| 651 | ? Effect.sleep("10 minutes") |
| 652 | : Effect.void |
| 653 | ).pipe( |
| 654 | Effect.andThen(Clock.currentTimeMillis), |
| 655 | Effect.map((now) => void dispatchedAt.set(dispatchThreadId, now)), |
| 656 | ), |
| 657 | }), |
| 658 | ), |
| 659 | Effect.scoped, |
| 660 | ); |
| 661 | }); |
| 662 | }, |
| 663 | ); |
| 664 | }); |