GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — slots: a wait store that cannot be read is not an empty one (#415) (#… · scuttletoby/t3code@0c3dc60

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src
    • slots
      • SlotGate.ts
      • SlotObserver.test.ts
      • SlotObserver.ts
    • waits
      • WaitService.slots.test.ts
      • WaitService.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/slots/SlotGate.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/slots/SlotGate.ts
+74-21Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* is memory only; slots held by delivered slot waits come from the wait store (`attachReservations`).
55
*/
66
import type { ProviderInstanceId, ThreadId } from "@t3tools/contracts";
7-
import type { SlotRuleStatus } from "@t3tools/contracts/waitFor";
7+
import type { SlotRuleStatus, WaitToolFailure } from "@t3tools/contracts/waitFor";
88
import {
99
accountOnlyThreads,
1010
agentSlotsMode,
@@ -50,12 +50,24 @@ export type SlotAcquire =
5050

5151
/** Slots held by delivered slot waits, as the wait store keeps them (`reservedUntil`). */
5252
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>;
5558
/** The gate let the holder start an agent: clear the wait's `reservedUntil` (a write). */
5659
readonly used: (key: string) => Effect.Effect<void>;
5760
}
5861

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+
5971
/** One slot wait asking for a slot; `key` is its wait id. */
6072
export interface SlotRequest {
6173
readonly key: string;
@@ -74,7 +86,7 @@ export type SlotGrant =
7486
/** An earlier request of the same thread was granted in this call: asked again next time. */
7587
| { readonly type: "sameThread" }
7688
| { 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". */
7890
| { readonly type: "unread"; readonly message: string };
7991

8092
/** What `wait_list` shows while slots are on (agents running, starting or held). */
@@ -126,7 +138,10 @@ export class SlotGate extends Context.Service<
126138
* the thread held before counts again.
127139
*/
128140
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+
*/
130145
readonly usage: Effect.Effect<SlotUsage | null>;
131146
/** For `wait_list`: the capacity rule beside the gate; null unless the mode is `observe`. */
132147
readonly observed: Effect.Effect<SlotRuleStatus | null>;
@@ -156,6 +171,12 @@ const make = Effect.gen(function* () {
156171
const hiddenKeys = () =>
157172
new Set([...replacing.values()].flatMap((olds) => olds.map((old) => old.key)));
158173
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>();
159180

160181
/**
161182
* 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* () {
201222
);
202223

203224
/**
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).
208228
*/
209229
const heldAt = Effect.fn("SlotGate.heldAt")(function* (waitsOn: boolean, nowMs: number) {
210230
for (const [key, untilMs] of usedKeys) if (untilMs <= nowMs) usedKeys.delete(key);
@@ -213,14 +233,30 @@ const make = Effect.gen(function* () {
213233
if (olds.every((old) => old.untilMs <= nowMs)) replacing.delete(key);
214234
}
215235
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+
}
217241
const skipped = new Set([...usedKeys.keys(), ...hiddenKeys()]);
218242
const source = stored;
243+
const live = (reservations: ReadonlyArray<SlotReservation>) =>
244+
liveReservations(reservations, nowMs).filter((reservation) => !skipped.has(reservation.key));
219245
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 })),
224260
),
225261
);
226262
});
@@ -282,7 +318,11 @@ const make = Effect.gen(function* () {
282318
},
283319
) {
284320
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+
}
286326
const { startGraceMs, gateRecords, waitsOn } = observe.value;
287327
const atMs = yield* Clock.currentTimeMillis;
288328
const starting = yield* Ref.get(entries);
@@ -298,7 +338,7 @@ const make = Effect.gen(function* () {
298338
today: answers(result),
299339
atMs,
300340
starting,
301-
held: heldBefore,
341+
held: Effect.map(heldBefore, (read) => (read.whole ? read.held : undefined)),
302342
callerThreadId: caller?.threadId,
303343
startGraceMs,
304344
gateRecords,
@@ -379,7 +419,10 @@ const make = Effect.gen(function* () {
379419
const read = counted ? yield* readRunning : undefined;
380420
const running = read?.running.filter((agent) => agent.threadId !== input.callerThreadId);
381421
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;
383426
const decision = slotDecision({
384427
limits: running === undefined ? { ...limits, machine: null, account: () => null } : limits,
385428
running: running ?? [],
@@ -401,6 +444,7 @@ const make = Effect.gen(function* () {
401444
]);
402445
// The agent it held a slot for starts now: its held slot is used up, with every older copy.
403446
const used = yield* useUp(reservations, input.callerThreadId);
447+
if (!heldRead.whole) unreadStarts.add(input.callerThreadId);
404448
return asDecided({ type: "allow", token }, used);
405449
},
406450
(decided, input) =>
@@ -447,7 +491,8 @@ const make = Effect.gen(function* () {
447491
const kept = yield* keepStarting(read?.visible, nowMs, limits);
448492
const memoryBytes = yield* freeMemoryBytes(limits);
449493
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;
451496
const grants: Array<SlotGrant> = [];
452497
const grantedThreads = new Set<string>();
453498
for (const request of requests) {
@@ -457,8 +502,14 @@ const make = Effect.gen(function* () {
457502
continue;
458503
}
459504
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+
});
462513
continue;
463514
}
464515
const decision = slotDecision({
@@ -553,7 +604,9 @@ const make = Effect.gen(function* () {
553604
limits === null
554605
? []
555606
: 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);
557610
// Holds hidden by a re-ask not kept yet: on their own account only, as `decide` counts them.
558611
const hidden = hiddenHeld(settings.value.waitFor.enabled, nowMs, "");
559612
const accountMax = (id: string) => {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.