@@ -198,6 +198,24 @@ const TWO_RUNNING = [{ id: "a" }, { id: "b" }]; |
198 | 198 | /** The calm wait after a (re)start: the level is "elevated" until this much calm has passed. */ |
199 | 199 | const CALM_WAIT = Duration.seconds(61); |
200 | 200 |
|
| 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 | + |
201 | 219 | describe("SlotGate observe mode", () => { |
202 | 220 | it.effect("fixed mode, old files and unknown modes read nothing for the rule", () => |
203 | 221 | Effect.gen(function* () { |
@@ -771,6 +789,99 @@ describe("SlotGate observe mode", () => { |
771 | 789 | }), |
772 | 790 | ); |
773 | 791 |
|
| 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 | + |
774 | 885 | it.effect("a wait store read that hangs never delays the decision; the rule reads slow", () => |
775 | 886 | Effect.gen(function* () { |
776 | 887 | const world = newWorld(); |
@@ -848,16 +959,7 @@ describe("SlotGate observe mode", () => { |
848 | 959 | Effect.gen(function* () { |
849 | 960 | const world = newWorld(); |
850 | 961 | 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); |
861 | 963 | let decided = 0; |
862 | 964 | /** One decision, observed to the end. */ |
863 | 965 | const decide = (input: Partial<SlotObserveInput> & { atMs: number }) => |
@@ -882,10 +984,10 @@ describe("SlotGate observe mode", () => { |
882 | 984 | callerThreadId: "waiter", |
883 | 985 | ownStart: { token: 1, instanceId: MAIN, atMs: 0 }, |
884 | 986 | }); |
885 | | - assert.deepStrictEqual(yield* observer.kept, { starts: 1, usedHolds: 1 }, "kept"); |
| 987 | + assert.deepStrictEqual(yield* observer.kept, { starts: 1, usedHolds: 1, notes: 0 }, "kept"); |
886 | 988 | // Past its hold's end and the dispatch's safety limit: the next decision lets both go. |
887 | 989 | 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"); |
889 | 991 | // Slots switched on (the gate keeps the records): the rule's own go at once. |
890 | 992 | yield* decide({ |
891 | 993 | atMs: DISPATCH_SAFETY_MS + 1, |
@@ -894,7 +996,11 @@ describe("SlotGate observe mode", () => { |
894 | 996 | held: Effect.succeed([{ ...HOLD, untilMs: 2 * DISPATCH_SAFETY_MS }]), |
895 | 997 | }); |
896 | 998 | 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 | + ); |
898 | 1004 | // Switched on while a slots-off observation still waits: the slots-on one counts the hold |
899 | 1005 | // as the gate does (the gate never used it up). |
900 | 1006 | const ON = DISPATCH_SAFETY_MS + 3; |
@@ -915,6 +1021,40 @@ describe("SlotGate observe mode", () => { |
915 | 1021 | held: Effect.succeed([{ ...HOLD, untilMs: 2 * DISPATCH_SAFETY_MS }]), |
916 | 1022 | }); |
917 | 1023 | 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"); |
918 | 1058 | }).pipe(Effect.scoped), |
919 | 1059 | ); |
920 | 1060 |
|
|