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 observer: count each hold once with slots off in every order (#… · scuttletoby/t3code@209dc0d

File tree

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

‎apps/server/src/slots/SlotObserver.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/slots/SlotObserver.test.ts
+153-13Lines changed: 153 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,24 @@ const TWO_RUNNING = [{ id: "a" }, { id: "b" }];
198198
/** The calm wait after a (re)start: the level is "elevated" until this much calm has passed. */
199199
const CALM_WAIT = Duration.seconds(61);
200200

201+
/** The observer alone, its projection read as `readRunning` (the order rows drive it by hand). */
202+
const observerFor = (
203+
world: World,
204+
readRunning: Effect.Effect<
205+
{ readonly visible: ReadonlySet<string> } | undefined
206+
> = Effect.succeed({ visible: new Set<string>() }),
207+
) =>
208+
makeSlotObserver(readRunning).pipe(
209+
Effect.provide(
210+
Layer.mergeAll(
211+
Layer.succeed(SlotCapacityHost, hostOf(world)),
212+
Layer.succeed(AgentScope, scopesOf(world)),
213+
Layer.succeed(HostProcess.UserId, UID),
214+
Layer.succeed(HostProcess.Platform, "linux"),
215+
),
216+
),
217+
);
218+
201219
describe("SlotGate observe mode", () => {
202220
it.effect("fixed mode, old files and unknown modes read nothing for the rule", () =>
203221
Effect.gen(function* () {
@@ -771,6 +789,99 @@ describe("SlotGate observe mode", () => {
771789
}),
772790
);
773791

792+
it.effect("slots off: a start uses its thread's hold up in every order", () =>
793+
Effect.gen(function* () {
794+
const HOLD = { key: "w_held", threadId: "waiter", untilMs: 100_000 };
795+
// What another thread's decision 5 s after the holder's start counts. Slots on reads the
796+
// same, as the gate uses the hold up at the holder's decision (the gate test above: 0 once
797+
// the dispatch ended with no child, 1 while the child runs).
798+
const cases = [
799+
// The holder's own store read is slow, or its projection read fails; then its dispatch
800+
// ends with no child, so the start stops counting before any read sees it.
801+
{ own: "slow", after: "no child", pieces: 0 },
802+
{ own: "fails", after: "no child", pieces: 0 },
803+
// The holder's observation is skipped (the queue is full); its child shows its run
804+
// before the next decision, while the queued observations still read.
805+
{ own: "skipped", after: "child runs", pieces: 1 },
806+
// The child's run shows before the next store read: the start no longer counts, but it
807+
// still used its hold up (counted as the child's run, once).
808+
{ own: "slow", after: "child runs", pieces: 1 },
809+
] as const;
810+
const got: Array<string> = [];
811+
for (const entry of cases) {
812+
const name = `own ${entry.own}, ${entry.after}`;
813+
const world = newWorld();
814+
let visible = new Set<string>();
815+
let runningFails = false;
816+
const observer = yield* observerFor(
817+
world,
818+
Effect.sync(() => (runningFails ? undefined : { visible })),
819+
);
820+
const decide = (atMs: number, patch: Partial<SlotObserveInput>) =>
821+
observer.observe({
822+
today: ["allow"],
823+
atMs,
824+
starting: [],
825+
held: Effect.succeed([HOLD]),
826+
startGraceMs: 30_000,
827+
gateRecords: false,
828+
...patch,
829+
});
830+
const done = (count: number) =>
831+
Effect.gen(function* () {
832+
for (let turn = 0; turn < 200; turn += 1) {
833+
const status = yield* observer.status;
834+
if (status.observed + status.skipped >= count) return status;
835+
yield* settle;
836+
}
837+
return yield* Effect.die(new Error(`${name}: fewer than ${count} observations`));
838+
});
839+
let decisions = 0;
840+
if (entry.own === "skipped") {
841+
// Other threads' observations fill the queue; the first one's store read takes 1 s.
842+
for (let index = 0; index < OBSERVE_MAX_WAITING; index += 1) {
843+
yield* decide(0, {
844+
callerThreadId: `other-${index}`,
845+
held: Effect.as(Effect.sleep(Duration.seconds(1)), [HOLD]),
846+
});
847+
decisions += 1;
848+
}
849+
}
850+
runningFails = entry.own === "fails";
851+
yield* decide(0, {
852+
callerThreadId: "waiter",
853+
ownStart: { token: 1, instanceId: MAIN, atMs: 0 },
854+
held: entry.own === "slow" ? Effect.never : Effect.succeed([HOLD]),
855+
});
856+
decisions += 1;
857+
yield* settle;
858+
if (entry.own === "slow") yield* TestClock.adjust(OBSERVE_READ_TIMEOUT);
859+
if (entry.own !== "skipped") yield* done(decisions);
860+
runningFails = false;
861+
if (entry.after === "no child") {
862+
yield* observer.released(1, 1_000);
863+
} else {
864+
yield* observer.started(1, "child");
865+
visible = new Set(["child"]);
866+
}
867+
for (let index = 0; entry.own === "skipped" && index < OBSERVE_MAX_WAITING; index += 1) {
868+
yield* TestClock.adjust(Duration.seconds(1));
869+
yield* settle;
870+
}
871+
yield* done(decisions);
872+
yield* decide(5_000, { callerThreadId: "other" });
873+
const status = yield* done(decisions + 1);
874+
assert.strictEqual(status.skipped, entry.own === "skipped" ? 1 : 0, name);
875+
assert.strictEqual(status.last?.readings, "ok", name);
876+
got.push(`${name}: ${status.last?.pieces}`);
877+
}
878+
assert.deepStrictEqual(
879+
got,
880+
cases.map((entry) => `own ${entry.own}, ${entry.after}: ${entry.pieces}`),
881+
);
882+
}).pipe(Effect.scoped),
883+
);
884+
774885
it.effect("a wait store read that hangs never delays the decision; the rule reads slow", () =>
775886
Effect.gen(function* () {
776887
const world = newWorld();
@@ -848,16 +959,7 @@ describe("SlotGate observe mode", () => {
848959
Effect.gen(function* () {
849960
const world = newWorld();
850961
const HOLD = { key: "w_held", threadId: "waiter", untilMs: 60_000 };
851-
const observer = yield* makeSlotObserver(Effect.succeed({ visible: new Set<string>() })).pipe(
852-
Effect.provide(
853-
Layer.mergeAll(
854-
Layer.succeed(SlotCapacityHost, hostOf(world)),
855-
Layer.succeed(AgentScope, scopesOf(world)),
856-
Layer.succeed(HostProcess.UserId, UID),
857-
Layer.succeed(HostProcess.Platform, "linux"),
858-
),
859-
),
860-
);
962+
const observer = yield* observerFor(world);
861963
let decided = 0;
862964
/** One decision, observed to the end. */
863965
const decide = (input: Partial<SlotObserveInput> & { atMs: number }) =>
@@ -882,10 +984,10 @@ describe("SlotGate observe mode", () => {
882984
callerThreadId: "waiter",
883985
ownStart: { token: 1, instanceId: MAIN, atMs: 0 },
884986
});
885-
assert.deepStrictEqual(yield* observer.kept, { starts: 1, usedHolds: 1 }, "kept");
987+
assert.deepStrictEqual(yield* observer.kept, { starts: 1, usedHolds: 1, notes: 0 }, "kept");
886988
// Past its hold's end and the dispatch's safety limit: the next decision lets both go.
887989
yield* decide({ atMs: DISPATCH_SAFETY_MS, held: Effect.succeed([]) });
888-
assert.deepStrictEqual(yield* observer.kept, { starts: 0, usedHolds: 0 }, "ended");
990+
assert.deepStrictEqual(yield* observer.kept, { starts: 0, usedHolds: 0, notes: 0 }, "ended");
889991
// Slots switched on (the gate keeps the records): the rule's own go at once.
890992
yield* decide({
891993
atMs: DISPATCH_SAFETY_MS + 1,
@@ -894,7 +996,11 @@ describe("SlotGate observe mode", () => {
894996
held: Effect.succeed([{ ...HOLD, untilMs: 2 * DISPATCH_SAFETY_MS }]),
895997
});
896998
yield* decide({ atMs: DISPATCH_SAFETY_MS + 2, gateRecords: true });
897-
assert.deepStrictEqual(yield* observer.kept, { starts: 0, usedHolds: 0 }, "slots on");
999+
assert.deepStrictEqual(
1000+
yield* observer.kept,
1001+
{ starts: 0, usedHolds: 0, notes: 0 },
1002+
"slots on",
1003+
);
8981004
// Switched on while a slots-off observation still waits: the slots-on one counts the hold
8991005
// as the gate does (the gate never used it up).
9001006
const ON = DISPATCH_SAFETY_MS + 3;
@@ -915,6 +1021,40 @@ describe("SlotGate observe mode", () => {
9151021
held: Effect.succeed([{ ...HOLD, untilMs: 2 * DISPATCH_SAFETY_MS }]),
9161022
});
9171023
assert.strictEqual((yield* observer.status).last?.pieces, 1, "switched on");
1024+
// A start's note that no store read could take (every read hangs): it goes after the
1025+
// dispatch's safety limit, or at once when slots switch on.
1026+
const slow = yield* observerFor(world);
1027+
let slowDecided = 0;
1028+
const decideSlow = (input: Partial<SlotObserveInput> & { atMs: number }) =>
1029+
Effect.gen(function* () {
1030+
yield* slow.observe({
1031+
today: ["allow"],
1032+
starting: [],
1033+
held: Effect.never,
1034+
startGraceMs: 30_000,
1035+
gateRecords: false,
1036+
...input,
1037+
});
1038+
slowDecided += 1;
1039+
yield* settle;
1040+
yield* TestClock.adjust(OBSERVE_READ_TIMEOUT);
1041+
for (let turn = 0; (yield* slow.status).observed < slowDecided; turn += 1) {
1042+
if (turn === 200) return yield* Effect.die(new Error("not observed"));
1043+
yield* settle;
1044+
}
1045+
});
1046+
const startAt = (atMs: number, token: number) => ({
1047+
atMs,
1048+
callerThreadId: "waiter",
1049+
ownStart: { token, instanceId: MAIN, atMs },
1050+
});
1051+
yield* decideSlow(startAt(0, 1));
1052+
assert.deepStrictEqual(yield* slow.kept, { starts: 1, usedHolds: 0, notes: 1 }, "unread");
1053+
yield* decideSlow({ atMs: DISPATCH_SAFETY_MS });
1054+
assert.deepStrictEqual(yield* slow.kept, { starts: 0, usedHolds: 0, notes: 0 }, "limit");
1055+
yield* decideSlow(startAt(DISPATCH_SAFETY_MS + 1, 2));
1056+
yield* decideSlow({ atMs: DISPATCH_SAFETY_MS + 2, gateRecords: true });
1057+
assert.deepStrictEqual(yield* slow.kept, { starts: 0, usedHolds: 0, notes: 0 }, "slots on");
9181058
}).pipe(Effect.scoped),
9191059
);
9201060

Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/slots/SlotObserver.ts
+22-10Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import type { SlotRuleStatus } from "@t3tools/contracts/waitFor";
77
import { AgentScope } from "@t3tools/shared/AgentScope";
88
import {
9+
DISPATCH_SAFETY_MS,
910
liveReservations,
1011
liveStarting,
1112
onePerThread,
@@ -87,9 +88,6 @@ export function slicePath(slice: string): string {
8788
return parts.map((_, index) => `/${parts.slice(0, index + 1).join("-")}.slice`).join("");
8889
}
8990

90-
/** One of the rule's own starts, and the thread that asked for it (its holds are used up). */
91-
type OwnStart = StartingEntry & { readonly threadId?: string | undefined };
92-
9391
interface OwnPiece {
9492
readonly scope: { readonly unit: string; readonly slice: string } | undefined;
9593
}
@@ -126,14 +124,20 @@ export const makeSlotObserver = Effect.fn("makeSlotObserver")(function* (
126124
* its child shows a run (then it counts as that run, once). A dispatch that ends with no child
127125
* stops counting at once.
128126
*/
129-
let ownStarts: ReadonlyArray<OwnStart> = [];
127+
let ownStarts: ReadonlyArray<StartingEntry> = [];
130128
/**
131129
* While the gate keeps no records: held slots of the threads those starts were for, by key, until
132130
* they end. A start uses its thread's hold up, as the gate's `useUp` does with slots on (with slots
133131
* off the gate leaves it in the store), so the start and the hold never both count, not even once
134132
* the start stops counting (its child shows its run, or its dispatch ends).
135133
*/
136134
const usedHolds = new Map<string, number>();
135+
/**
136+
* Slots off: threads allowed a start, noted at the decision (before any skip). The next good store
137+
* read uses their holds up, whatever became of the start meanwhile, and drops the notes. Slots on
138+
* drops them all; a note no read took goes after `DISPATCH_SAFETY_MS`.
139+
*/
140+
const startedThreads = new Map<string, number>();
137141

138142
const readFacts = async (pieces: ReadonlyArray<OwnPiece>): Promise<CapacityFacts> => {
139143
const machine = await readMachine(host);
@@ -212,15 +216,12 @@ export const makeSlotObserver = Effect.fn("makeSlotObserver")(function* (
212216
);
213217
const live = liveReservations(heldRead.value, input.atMs);
214218
if (!input.gateRecords) {
215-
// Also the starts this decision counts (a skipped or slow observation of a start's own
216-
// decision leaves its hold to the next one).
217-
const startedThreads = new Set(own.map((entry) => entry.threadId));
218-
if (input.ownStart !== undefined) startedThreads.add(input.callerThreadId);
219219
for (const reservation of live) {
220220
if (startedThreads.has(reservation.threadId)) {
221221
usedHolds.set(reservation.key, reservation.untilMs);
222222
}
223223
}
224+
startedThreads.clear();
224225
}
225226
const held = onePerThread(
226227
live.filter(
@@ -285,9 +286,16 @@ export const makeSlotObserver = Effect.fn("makeSlotObserver")(function* (
285286
for (const [key, untilMs] of usedHolds) {
286287
if (input.gateRecords || untilMs <= input.atMs) usedHolds.delete(key);
287288
}
289+
for (const [threadId, atMs] of startedThreads) {
290+
if (input.gateRecords || input.atMs - atMs >= DISPATCH_SAFETY_MS) {
291+
startedThreads.delete(threadId);
292+
}
293+
}
288294
const ownBefore = ownStarts;
289295
if (!input.gateRecords && input.ownStart !== undefined) {
290-
ownStarts = [...ownStarts, { ...input.ownStart, threadId: input.callerThreadId }];
296+
ownStarts = [...ownStarts, input.ownStart];
297+
if (input.callerThreadId !== undefined)
298+
startedThreads.set(input.callerThreadId, input.atMs);
291299
}
292300
if (waiting >= OBSERVE_MAX_WAITING) {
293301
return Ref.update(status, (current) => ({ ...current, skipped: current.skipped + 1 }));
@@ -332,6 +340,10 @@ export const makeSlotObserver = Effect.fn("makeSlotObserver")(function* (
332340
}),
333341
status: Ref.get(status),
334342
/** What the rule keeps in memory (for tests: nothing past the point it stops counting). */
335-
kept: Effect.sync(() => ({ starts: ownStarts.length, usedHolds: usedHolds.size })),
343+
kept: Effect.sync(() => ({
344+
starts: ownStarts.length,
345+
usedHolds: usedHolds.size,
346+
notes: startedThreads.size,
347+
})),
336348
};
337349
});

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.