|
4 | 4 | * is memory only; slots held by delivered slot waits come from the wait store (`attachReservations`). |
5 | 5 | */ |
6 | 6 | import type { ProviderInstanceId, ThreadId } from "@t3tools/contracts"; |
7 | | -import type { SlotRuleStatus } from "@t3tools/contracts/waitFor"; |
| 7 | +import type { SlotRuleStatus, WaitToolFailure } from "@t3tools/contracts/waitFor"; |
8 | 8 | import { |
9 | 9 | accountOnlyThreads, |
10 | 10 | agentSlotsMode, |
@@ -50,12 +50,24 @@ export type SlotAcquire = |
50 | 50 |
|
51 | 51 | /** Slots held by delivered slot waits, as the wait store keeps them (`reservedUntil`). */ |
52 | 52 | export interface StoredReservations { |
53 | | - /** The live ones; read only while waits are on. */ |
54 | | - readonly read: Effect.Effect<ReadonlyArray<SlotReservation>>; |
| 53 | + /** |
| 54 | + * The live ones; read only while waits are on. Fails when the store (or the settings) cannot be |
| 55 | + * read: that is never an empty store. |
| 56 | + */ |
| 57 | + readonly read: Effect.Effect<ReadonlyArray<SlotReservation>, WaitToolFailure>; |
55 | 58 | /** The gate let the holder start an agent: clear the wait's `reservedUntil` (a write). */ |
56 | 59 | readonly used: (key: string) => Effect.Effect<void>; |
57 | 60 | } |
58 | 61 |
|
| 62 | +/** |
| 63 | + * The live held slots, one read. `whole: false`: the wait store could not be read, so `held` is the |
| 64 | + * gate's memory only (a lower count, never a good read). |
| 65 | + */ |
| 66 | +interface HeldRead { |
| 67 | + readonly held: ReadonlyArray<SlotReservation>; |
| 68 | + readonly whole: boolean; |
| 69 | +} |
| 70 | + |
59 | 71 | /** One slot wait asking for a slot; `key` is its wait id. */ |
60 | 72 | export interface SlotRequest { |
61 | 73 | readonly key: string; |
@@ -74,7 +86,7 @@ export type SlotGrant = |
74 | 86 | /** An earlier request of the same thread was granted in this call: asked again next time. */ |
75 | 87 | | { readonly type: "sameThread" } |
76 | 88 | | { readonly type: "full"; readonly decision: Extract<SlotDecision, { readonly type: "refuse" }> } |
77 | | - /** The running agents cannot be counted now: never "free". */ |
| 89 | + /** The running agents or the held slots cannot be counted now: never "free". */ |
78 | 90 | | { readonly type: "unread"; readonly message: string }; |
79 | 91 |
|
80 | 92 | /** What `wait_list` shows while slots are on (agents running, starting or held). */ |
@@ -126,7 +138,10 @@ export class SlotGate extends Context.Service< |
126 | 138 | * the thread held before counts again. |
127 | 139 | */ |
128 | 140 | readonly drop: (keys: ReadonlyArray<string>) => Effect.Effect<void>; |
129 | | - /** For `wait_list`: null while slots are off, or when the running agents cannot be read. */ |
| 141 | + /** |
| 142 | + * For `wait_list`: null while slots are off, or when the running agents or the held slots |
| 143 | + * cannot be read. |
| 144 | + */ |
130 | 145 | readonly usage: Effect.Effect<SlotUsage | null>; |
131 | 146 | /** For `wait_list`: the capacity rule beside the gate; null unless the mode is `observe`. */ |
132 | 147 | readonly observed: Effect.Effect<SlotRuleStatus | null>; |
@@ -156,6 +171,12 @@ const make = Effect.gen(function* () { |
156 | 171 | const hiddenKeys = () => |
157 | 172 | new Set([...replacing.values()].flatMap((olds) => olds.map((old) => old.key))); |
158 | 173 | let stored: StoredReservations | null = null; |
| 174 | + /** |
| 175 | + * Threads started while the wait store could not be read: the next good read uses their stored |
| 176 | + * holds up and clears this. A counted grant needs a good read (or waits off, which clears this), |
| 177 | + * so none comes in between; an uncounted one may, and counts against nothing. |
| 178 | + */ |
| 179 | + const unreadStarts = new Set<string>(); |
159 | 180 |
|
160 | 181 | /** |
161 | 182 | * Threads with an active run, settled or not (settling a thread does not stop its run), nobody |
@@ -201,10 +222,9 @@ const make = Effect.gen(function* () { |
201 | 222 | ); |
202 | 223 |
|
203 | 224 | /** |
204 | | - * Every live held slot: granted in memory and kept in the wait store, one per wait (counted one |
205 | | - * per thread, `onePerThread`). None while waits are off (nothing of the store is read then). A |
206 | | - * store that cannot be read holds none. Taken now; the store's part is read when the returned |
207 | | - * effect runs, without the used and hidden keys of now (the capacity rule reads it later). |
| 225 | + * Every live held slot (memory and wait store, one per wait); none while waits are off. A store |
| 226 | + * that cannot be read gives `whole: false` (memory only). Taken now; the store's part is read when |
| 227 | + * the returned effect runs, without the used and hidden keys of now (the rule reads it later). |
208 | 228 | */ |
209 | 229 | const heldAt = Effect.fn("SlotGate.heldAt")(function* (waitsOn: boolean, nowMs: number) { |
210 | 230 | for (const [key, untilMs] of usedKeys) if (untilMs <= nowMs) usedKeys.delete(key); |
@@ -213,14 +233,30 @@ const make = Effect.gen(function* () { |
213 | 233 | if (olds.every((old) => old.untilMs <= nowMs)) replacing.delete(key); |
214 | 234 | } |
215 | 235 | const memory = yield* Ref.updateAndGet(held, (current) => liveReservations(current, nowMs)); |
216 | | - if (!waitsOn) return Effect.succeed<ReadonlyArray<SlotReservation>>([]); |
| 236 | + if (!waitsOn) { |
| 237 | + // Stored holds do not count while waits are off; a start then never uses them up either. |
| 238 | + unreadStarts.clear(); |
| 239 | + return Effect.succeed<HeldRead>({ held: [], whole: true }); |
| 240 | + } |
217 | 241 | const skipped = new Set([...usedKeys.keys(), ...hiddenKeys()]); |
218 | 242 | const source = stored; |
| 243 | + const live = (reservations: ReadonlyArray<SlotReservation>) => |
| 244 | + liveReservations(reservations, nowMs).filter((reservation) => !skipped.has(reservation.key)); |
219 | 245 | return (source === null ? Effect.succeed([]) : source.read).pipe( |
220 | | - Effect.map((fromStore) => |
221 | | - liveReservations([...memory, ...fromStore], nowMs).filter( |
222 | | - (reservation) => !skipped.has(reservation.key), |
223 | | - ), |
| 246 | + Effect.map((fromStore): HeldRead => { |
| 247 | + // A start allowed while the store was unread uses its thread's stored holds up now. |
| 248 | + for (const reservation of fromStore) { |
| 249 | + if (!unreadStarts.has(reservation.threadId)) continue; |
| 250 | + usedKeys.set(reservation.key, reservation.untilMs); |
| 251 | + skipped.add(reservation.key); |
| 252 | + } |
| 253 | + unreadStarts.clear(); |
| 254 | + return { held: live([...memory, ...fromStore]), whole: true }; |
| 255 | + }), |
| 256 | + Effect.catch((cause) => |
| 257 | + Effect.logWarning("agent slots: the slots held in the wait store cannot be read", { |
| 258 | + cause, |
| 259 | + }).pipe(Effect.as<HeldRead>({ held: live(memory), whole: false })), |
224 | 260 | ), |
225 | 261 | ); |
226 | 262 | }); |
@@ -282,7 +318,11 @@ const make = Effect.gen(function* () { |
282 | 318 | }, |
283 | 319 | ) { |
284 | 320 | const observe = yield* observing; |
285 | | - if (Option.isNone(observe)) return yield* decision; |
| 321 | + if (Option.isNone(observe)) { |
| 322 | + // A decision outside observe mode may grant a hold the rule's notes must never take. |
| 323 | + yield* observer.outside; |
| 324 | + return yield* decision; |
| 325 | + } |
286 | 326 | const { startGraceMs, gateRecords, waitsOn } = observe.value; |
287 | 327 | const atMs = yield* Clock.currentTimeMillis; |
288 | 328 | const starting = yield* Ref.get(entries); |
@@ -298,7 +338,7 @@ const make = Effect.gen(function* () { |
298 | 338 | today: answers(result), |
299 | 339 | atMs, |
300 | 340 | starting, |
301 | | - held: heldBefore, |
| 341 | + held: Effect.map(heldBefore, (read) => (read.whole ? read.held : undefined)), |
302 | 342 | callerThreadId: caller?.threadId, |
303 | 343 | startGraceMs, |
304 | 344 | gateRecords, |
@@ -379,7 +419,10 @@ const make = Effect.gen(function* () { |
379 | 419 | const read = counted ? yield* readRunning : undefined; |
380 | 420 | const running = read?.running.filter((agent) => agent.threadId !== input.callerThreadId); |
381 | 421 | const kept = yield* keepStarting(read?.visible, nowMs, limits); |
382 | | - const reservations = yield* liveHeld(waitsOn, nowMs); |
| 422 | + // A store that cannot be read: its holds go uncounted (memory only), as unread counts never |
| 423 | + // stop a delegation; the caller's stored holds are used up at the next good read. |
| 424 | + const heldRead = yield* liveHeld(waitsOn, nowMs); |
| 425 | + const reservations = heldRead.held; |
383 | 426 | const decision = slotDecision({ |
384 | 427 | limits: running === undefined ? { ...limits, machine: null, account: () => null } : limits, |
385 | 428 | running: running ?? [], |
@@ -401,6 +444,7 @@ const make = Effect.gen(function* () { |
401 | 444 | ]); |
402 | 445 | // The agent it held a slot for starts now: its held slot is used up, with every older copy. |
403 | 446 | const used = yield* useUp(reservations, input.callerThreadId); |
| 447 | + if (!heldRead.whole) unreadStarts.add(input.callerThreadId); |
404 | 448 | return asDecided({ type: "allow", token }, used); |
405 | 449 | }, |
406 | 450 | (decided, input) => |
@@ -447,7 +491,8 @@ const make = Effect.gen(function* () { |
447 | 491 | const kept = yield* keepStarting(read?.visible, nowMs, limits); |
448 | 492 | const memoryBytes = yield* freeMemoryBytes(limits); |
449 | 493 | const reserveMs = settings.value.agentSlots.reserveSeconds * 1000; |
450 | | - let reservations = yield* liveHeld(settings.value.waitFor.enabled, nowMs); |
| 494 | + const heldRead = yield* liveHeld(settings.value.waitFor.enabled, nowMs); |
| 495 | + let reservations = heldRead.held; |
451 | 496 | const grants: Array<SlotGrant> = []; |
452 | 497 | const grantedThreads = new Set<string>(); |
453 | 498 | for (const request of requests) { |
@@ -457,8 +502,14 @@ const make = Effect.gen(function* () { |
457 | 502 | continue; |
458 | 503 | } |
459 | 504 | const own = limitsFor(request); |
460 | | - if (read === undefined && limitsCount(own, request.account ?? "")) { |
461 | | - grants.push({ type: "unread", message: "The running agents cannot be counted now." }); |
| 505 | + if ((read === undefined || !heldRead.whole) && limitsCount(own, request.account ?? "")) { |
| 506 | + grants.push({ |
| 507 | + type: "unread", |
| 508 | + message: |
| 509 | + read === undefined |
| 510 | + ? "The running agents cannot be counted now." |
| 511 | + : "The held slots cannot be counted now.", |
| 512 | + }); |
462 | 513 | continue; |
463 | 514 | } |
464 | 515 | const decision = slotDecision({ |
@@ -553,7 +604,9 @@ const make = Effect.gen(function* () { |
553 | 604 | limits === null |
554 | 605 | ? [] |
555 | 606 | : liveStarting(yield* Ref.get(entries), read.visible, nowMs, limits.startGraceMs); |
556 | | - const reservations = onePerThread(yield* liveHeld(settings.value.waitFor.enabled, nowMs)); |
| 607 | + const heldRead = yield* liveHeld(settings.value.waitFor.enabled, nowMs); |
| 608 | + if (!heldRead.whole) return null; |
| 609 | + const reservations = onePerThread(heldRead.held); |
557 | 610 | // Holds hidden by a re-ask not kept yet: on their own account only, as `decide` counts them. |
558 | 611 | const hidden = hiddenHeld(settings.value.waitFor.enabled, nowMs, ""); |
559 | 612 | const accountMax = (id: string) => { |
|