|
| 1 | +// `off` was not inert. A transition must ask whether its run is resident, and the keyspace is the |
| 2 | +// only record of that, so at `off` AFTER a ramp every transition still has to ask or a resident |
| 3 | +// run's head freezes while Postgres moves on. |
| 4 | +// |
| 5 | +// Before any ramp, nothing CAN be resident: only a birth creates a keyspace and every birth was |
| 6 | +// refused. So the question has one possible answer and asking it is pure cost. Measured: 2 per cent |
| 7 | +// with a healthy endpoint, four times the run duration with a slow one, for every run, and it did |
| 8 | +// not decay. |
| 9 | +// |
| 10 | +// The latch is one way. Unset means this deployment has never enabled the store, so transitions skip |
| 11 | +// it entirely. Once set it is never cleared, and `off` returns to meaning "drain". |
| 12 | +import { describe, expect, it } from "vitest"; |
| 13 | +import { TaskRunExecutionSnapshotStore } from "./taskRunExecutionSnapshotStore.js"; |
| 14 | +import type { SnapshotStoreModeResolver } from "./taskRunExecutionSnapshotStore.js"; |
| 15 | +import type { RedisSnapshotStore } from "./redisSnapshotStore.js"; |
| 16 | +import type { RunStore } from "./types.js"; |
| 17 | + |
| 18 | +const SCOPE = { |
| 19 | + environmentId: "env_1", |
| 20 | + environmentType: "PRODUCTION", |
| 21 | + projectId: "proj_1", |
| 22 | + organizationId: "org_1", |
| 23 | +} as const; |
| 24 | + |
| 25 | +function harness(opts: { mode: "off" | "dual-write"; everEnabled?: boolean }) { |
| 26 | + const touched: string[] = []; |
| 27 | + const redis = new Proxy({} as RedisSnapshotStore, { |
| 28 | + get: (_t, prop) => { |
| 29 | + return (...__: unknown[]) => { |
| 30 | + touched.push(String(prop)); |
| 31 | + return Promise.resolve({ outcome: "written", seq: 1 }); |
| 32 | + }; |
| 33 | + }, |
| 34 | + }); |
| 35 | + const delegate = new Proxy({} as Record<string, unknown>, { |
| 36 | + get: () => () => Promise.resolve({}), |
| 37 | + }) as unknown as RunStore; |
| 38 | + |
| 39 | + const modeResolver: SnapshotStoreModeResolver = { |
| 40 | + resolve: () => opts.mode, |
| 41 | + ...(opts.everEnabled !== undefined && { everEnabled: () => opts.everEnabled! }), |
| 42 | + }; |
| 43 | + |
| 44 | + const decorated = new TaskRunExecutionSnapshotStore(delegate, { |
| 45 | + store: redis, |
| 46 | + mode: opts.mode, |
| 47 | + modeResolver, |
| 48 | + }); |
| 49 | + return { decorated, touched }; |
| 50 | +} |
| 51 | + |
| 52 | +const completion = { |
| 53 | + completedAt: new Date(), |
| 54 | + outputType: "application/json", |
| 55 | + usageDurationMs: 1, |
| 56 | + costInCents: 0, |
| 57 | + snapshot: { |
| 58 | + executionStatus: "FINISHED" as const, |
| 59 | + description: "done", |
| 60 | + runStatus: "COMPLETED_SUCCESSFULLY" as const, |
| 61 | + attemptNumber: 1, |
| 62 | + ...SCOPE, |
| 63 | + }, |
| 64 | +}; |
| 65 | + |
| 66 | +describe("the residency latch", () => { |
| 67 | + it("a transition touches the store NOT AT ALL while the latch is unset", async () => { |
| 68 | + const h = harness({ mode: "off", everEnabled: false }); |
| 69 | + |
| 70 | + await h.decorated.completeAttemptSuccess("run_1", completion, { select: { id: true } }); |
| 71 | + |
| 72 | + // The assertion the whole change exists for: no probe, so Redis is off the run path entirely. |
| 73 | + expect(h.touched).toEqual([]); |
| 74 | + }); |
| 75 | + |
| 76 | + it("a transition still asks once the latch is set, even at off", async () => { |
| 77 | + // Non-negotiable. A run resident from an earlier ramp must keep mirroring, or its head freezes |
| 78 | + // and the remedy becomes worse than the fault. |
| 79 | + const h = harness({ mode: "off", everEnabled: true }); |
| 80 | + |
| 81 | + await h.decorated.completeAttemptSuccess("run_1", completion, { select: { id: true } }); |
| 82 | + |
| 83 | + expect(h.touched).toContain("append"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("asks when the resolver offers no latch, so an unlatched deployment is unchanged", async () => { |
| 87 | + const h = harness({ mode: "off" }); |
| 88 | + |
| 89 | + await h.decorated.completeAttemptSuccess("run_1", completion, { select: { id: true } }); |
| 90 | + |
| 91 | + expect(h.touched).toContain("append"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("never suppresses a transition once the dial itself is past off", async () => { |
| 95 | + // Belt and braces: the guard makes this state unreachable, but if it ever occurred, suppressing |
| 96 | + // transitions while births are mirroring is the one combination that strands a head. |
| 97 | + const h = harness({ mode: "dual-write", everEnabled: false }); |
| 98 | + |
| 99 | + await h.decorated.completeAttemptSuccess("run_1", completion, { select: { id: true } }); |
| 100 | + |
| 101 | + expect(h.touched).toContain("append"); |
| 102 | + }); |
| 103 | +}); |
0 commit comments