@@ -15,6 +15,7 @@ import { |
15 | 15 | } from "@t3tools/contracts"; |
16 | 16 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
17 | 17 | import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; |
| 18 | +import * as Deferred from "effect/Deferred"; |
18 | 19 | import * as Effect from "effect/Effect"; |
19 | 20 | import * as FileSystem from "effect/FileSystem"; |
20 | 21 | import * as Layer from "effect/Layer"; |
@@ -54,8 +55,14 @@ interface World { |
54 | 55 | /** Threads the gate refuses whatever is free. */ |
55 | 56 | full: Set<ThreadId>; |
56 | 57 | asks: Array<SlotGate.SlotAcquireInput>; |
| 58 | + /** The gate's calls after an ask: `started <thread>`, `release <token>`, `leave <key>`. */ |
| 59 | + calls: Array<string>; |
| 60 | + /** Threads whose ask takes this long (a slow count read), then is refused. */ |
| 61 | + slow: Set<ThreadId>; |
57 | 62 | /** Threads gone (`null` shell), and whether a shell read fails. */ |
58 | 63 | gone: Set<ThreadId>; |
| 64 | + /** Threads the gate sees live but upstream's worker then reads archived. */ |
| 65 | + archivedForUpstream: Set<ThreadId>; |
59 | 66 | unreadable: boolean; |
60 | 67 | logs: Array<string>; |
61 | 68 | } |
@@ -64,27 +71,36 @@ const newWorld = (free: number): World => ({ |
64 | 71 | free, |
65 | 72 | full: new Set(), |
66 | 73 | asks: [], |
| 74 | + calls: [], |
| 75 | + slow: new Set(), |
67 | 76 | gone: new Set(), |
| 77 | + archivedForUpstream: new Set(), |
68 | 78 | unreadable: false, |
69 | 79 | logs: [], |
70 | 80 | }); |
71 | 81 |
|
72 | 82 | const gateOf = (world: World) => |
73 | 83 | Layer.mock(SlotGate.SlotGate)({ |
74 | 84 | acquire: (input) => |
75 | | - Effect.sync((): SlotGate.SlotAcquire => { |
| 85 | + Effect.gen(function* () { |
76 | 86 | world.asks.push(input); |
77 | | - if (world.free === 0 || world.full.has(input.callerThreadId!)) |
78 | | - return { |
79 | | - type: "refuse", |
80 | | - message: "full", |
81 | | - refusal: { type: "unread", problem: "timeout" }, |
82 | | - }; |
| 87 | + const refused: SlotGate.SlotAcquire = { |
| 88 | + type: "refuse", |
| 89 | + message: "full", |
| 90 | + refusal: { type: "unread", problem: "timeout" }, |
| 91 | + }; |
| 92 | + if (world.slow.has(input.callerThreadId!)) { |
| 93 | + yield* Effect.sleep("5 seconds"); |
| 94 | + return refused; |
| 95 | + } |
| 96 | + if (world.free === 0 || world.full.has(input.callerThreadId!)) return refused; |
83 | 97 | world.free -= 1; |
84 | | - return { type: "allow", token: world.asks.length }; |
| 98 | + return { type: "allow", token: world.asks.length } satisfies SlotGate.SlotAcquire; |
85 | 99 | }), |
86 | | - started: () => Effect.void, |
87 | | - release: () => Effect.void, |
| 100 | + started: (token, childThreadId) => |
| 101 | + Effect.sync(() => world.calls.push(`started ${childThreadId} ${token}`)), |
| 102 | + release: (token) => Effect.sync(() => world.calls.push(`release ${token}`)), |
| 103 | + leave: (keys) => Effect.sync(() => world.calls.push(...keys.map((key) => `leave ${key}`))), |
88 | 104 | }); |
89 | 105 |
|
90 | 106 | const projection = { |
@@ -115,7 +131,7 @@ const server = ( |
115 | 131 | ), |
116 | 132 | getThreadRecords: (threadId) => |
117 | 133 | Effect.succeed( |
118 | | - world.gone.has(threadId) |
| 134 | + world.gone.has(threadId) || world.archivedForUpstream.has(threadId) |
119 | 135 | ? { ...projection, thread: { archivedAt: "2026-11-21T00:00:00.000Z", deletedAt: null } } |
120 | 136 | : projection, |
121 | 137 | ) as never, |
@@ -152,6 +168,12 @@ const offer = (value: ProviderContinuationRequests.ProviderContinuationRequest) |
152 | 168 | Effect.flatMap((requests) => requests.offer(value)), |
153 | 169 | ); |
154 | 170 |
|
| 171 | +/** Lets forked fibers run (scheduler turns, no clock moved). */ |
| 172 | +const settle = Effect.forEach(Array.from({ length: 50 }), () => Effect.yieldNow, { discard: true }); |
| 173 | + |
| 174 | +/** The gate's slot calls, without the line's. */ |
| 175 | +const slotCalls = (world: World) => world.calls.filter((call) => !call.startsWith("leave ")); |
| 176 | + |
155 | 177 | const waitLogs = (world: World) => |
156 | 178 | world.logs.filter((log) => log.includes("provider continuation waits for an agent slot")); |
157 | 179 |
|
@@ -182,7 +204,10 @@ describe("provider continuations take an agent slot", () => { |
182 | 204 | targetInstanceId: INSTANCE, |
183 | 205 | intoThreadId: A, |
184 | 206 | keepsHeld: true, |
| 207 | + place: { key: "continuation:0" }, |
185 | 208 | }); |
| 209 | + // Every ask in its one place in the gate's line. |
| 210 | + assert.isTrue(world.asks.every((asked) => asked.place?.key === "continuation:0")); |
186 | 211 | world.free = 1; |
187 | 212 | yield* TestClock.adjust("5 seconds"); |
188 | 213 | const sent = yield* Queue.take(dispatched); |
@@ -214,6 +239,69 @@ describe("provider continuations take an agent slot", () => { |
214 | 239 | }), |
215 | 240 | ); |
216 | 241 |
|
| 242 | + it.effect("a slow count read for one thread holds up no other thread's continuation", () => |
| 243 | + Effect.gen(function* () { |
| 244 | + const world = newWorld(5); |
| 245 | + world.slow.add(A); |
| 246 | + const dispatched = yield* Queue.unbounded<OrchestrationV2ServerCommand>(); |
| 247 | + yield* Effect.gen(function* () { |
| 248 | + yield* offer(request(A)); |
| 249 | + yield* offer(request(B)); |
| 250 | + // No time passes: B is not queued behind A's 5-second ask. |
| 251 | + yield* settle; |
| 252 | + assert.strictEqual(yield* Queue.size(dispatched), 1); |
| 253 | + const sent = yield* Queue.take(dispatched); |
| 254 | + assert.strictEqual(sent.type === "message.dispatch" && sent.threadId, B); |
| 255 | + // A's ask ends (it cannot be cut off half-way), so the server can stop. |
| 256 | + yield* TestClock.adjust("5 seconds"); |
| 257 | + }).pipe(Effect.provide(server(world, dispatched)), Effect.scoped); |
| 258 | + }), |
| 259 | + ); |
| 260 | + |
| 261 | + it.effect( |
| 262 | + "a granted slot is held until upstream's dispatch of it ends, however long it waits", |
| 263 | + () => |
| 264 | + Effect.gen(function* () { |
| 265 | + const world = newWorld(1); |
| 266 | + const dispatched = yield* Queue.unbounded<OrchestrationV2ServerCommand>(); |
| 267 | + const lockFree = yield* Deferred.make<void>(); |
| 268 | + yield* Effect.gen(function* () { |
| 269 | + yield* offer( |
| 270 | + request(A, { |
| 271 | + // Behind its own thread's earlier dispatch (its parent's lock) for 2 minutes. |
| 272 | + dispatchIfCurrent: (effect) => |
| 273 | + Deferred.await(lockFree).pipe(Effect.andThen(Effect.asSome(effect))), |
| 274 | + }), |
| 275 | + ); |
| 276 | + yield* TestClock.adjust("2 minutes"); |
| 277 | + assert.lengthOf(world.asks, 1); |
| 278 | + // Asked and granted, not ended: the start counts whatever the start grace. |
| 279 | + assert.deepStrictEqual(slotCalls(world), []); |
| 280 | + yield* Deferred.succeed(lockFree, undefined); |
| 281 | + yield* Queue.take(dispatched); |
| 282 | + yield* settle; |
| 283 | + assert.deepStrictEqual(slotCalls(world), [`started ${A} 1`, "release 1"]); |
| 284 | + }).pipe(Effect.provide(server(world, dispatched)), Effect.scoped); |
| 285 | + }), |
| 286 | + ); |
| 287 | + |
| 288 | + it.effect("a granted request upstream drops (its thread archived meanwhile) frees its slot", () => |
| 289 | + Effect.gen(function* () { |
| 290 | + const world = newWorld(1); |
| 291 | + const dispatched = yield* Queue.unbounded<OrchestrationV2ServerCommand>(); |
| 292 | + const cleared: Array<ThreadId> = []; |
| 293 | + yield* Effect.gen(function* () { |
| 294 | + // Granted on a live shell; upstream then reads the thread archived and clears the latch. |
| 295 | + world.archivedForUpstream.add(A); |
| 296 | + yield* offer(request(A, { clearIfCurrent: () => Effect.sync(() => cleared.push(A)) })); |
| 297 | + yield* settle; |
| 298 | + assert.deepStrictEqual(cleared, [A]); |
| 299 | + assert.strictEqual(yield* Queue.size(dispatched), 0); |
| 300 | + assert.deepStrictEqual(slotCalls(world), [`started ${A} 1`, "release 1"]); |
| 301 | + }).pipe(Effect.provide(server(world, dispatched)), Effect.scoped); |
| 302 | + }), |
| 303 | + ); |
| 304 | + |
217 | 305 | it.effect("a thread that cannot be read waits, then is sent once", () => |
218 | 306 | Effect.gen(function* () { |
219 | 307 | const world = newWorld(5); |
@@ -241,7 +329,7 @@ describe("provider continuations take an agent slot", () => { |
241 | 329 | const cleared: Array<ThreadId> = []; |
242 | 330 | yield* Effect.gen(function* () { |
243 | 331 | yield* offer(request(A, { clearIfCurrent: () => Effect.sync(() => cleared.push(A)) })); |
244 | | - yield* TestClock.adjust("0 seconds"); |
| 332 | + yield* settle; |
245 | 333 | assert.deepStrictEqual(cleared, [A]); |
246 | 334 | assert.deepStrictEqual(world.asks, []); |
247 | 335 | assert.strictEqual(yield* Queue.size(dispatched), 0); |
@@ -254,7 +342,7 @@ describe("provider continuations take an agent slot", () => { |
254 | 342 | const world = newWorld(0); |
255 | 343 | const inner = |
256 | 344 | yield* Queue.unbounded<ProviderContinuationRequests.ProviderContinuationRequest>(); |
257 | | - const gated = ContinuationSlot.gateContinuations( |
| 345 | + const gated = yield* ContinuationSlot.gateContinuations( |
258 | 346 | { |
259 | 347 | offer: (value) => Queue.offer(inner, value).pipe(Effect.asVoid), |
260 | 348 | take: Queue.take(inner), |
@@ -291,6 +379,8 @@ describe("provider continuations take an agent slot", () => { |
291 | 379 | yield* TestClock.adjust("12 seconds"); |
292 | 380 | }).pipe(Effect.provide(server(world, dispatched)), Effect.scoped); |
293 | 381 | const asked = world.asks.length; |
| 382 | + // Its place in the line goes with it. |
| 383 | + assert.include(world.calls, "leave continuation:0"); |
294 | 384 | world.free = 5; |
295 | 385 | yield* TestClock.adjust("1 minute"); |
296 | 386 | assert.strictEqual(yield* Queue.size(dispatched), 0); |
@@ -308,7 +398,7 @@ describe("provider continuations take an agent slot", () => { |
308 | 398 | }).pipe(Effect.provide(server(world, dispatched, null)), Effect.scoped); |
309 | 399 | const inner = { offer: () => Effect.void, take: Effect.never }; |
310 | 400 | assert.strictEqual( |
311 | | - ContinuationSlot.gateContinuations( |
| 401 | + yield* ContinuationSlot.gateContinuations( |
312 | 402 | inner, |
313 | 403 | Option.none(), |
314 | 404 | () => Effect.succeed(null), |
|