22// only record of that, so at `off` AFTER a ramp every transition still has to ask or a resident
33// run's head freezes while Postgres moves on.
44//
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".
5+ // A transition may therefore be skipped only when nothing of this org's could be resident. A run is
6+ // resident only if its org's dial was non-off at its birth, i.e. the global dial had ever gone
7+ // non-off OR the org itself was ever enabled. So the skip is sound only when the global dial has
8+ // NEVER been non-off (globalModeEverEnabled() === false) AND this org is DEFINITELY never-enabled
9+ // (orgDefinitelyNeverEnabled(org) === true). Any uncertainty falls to "probe".
1210import { describe , expect , it } from "vitest" ;
1311import { TaskRunExecutionSnapshotStore } from "./taskRunExecutionSnapshotStore.js" ;
1412import type { SnapshotStoreModeResolver } from "./taskRunExecutionSnapshotStore.js" ;
@@ -24,8 +22,8 @@ const SCOPE = {
2422
2523function harness ( opts : {
2624 mode : "off" | "dual-write" ;
27- everEnabled ?: boolean ;
28- everEnabledForOrg ?: ( organizationId : string ) => boolean ;
25+ globalModeEverEnabled ?: boolean ;
26+ orgDefinitelyNeverEnabled ?: ( organizationId : string ) => boolean ;
2927} ) {
3028 const touched : string [ ] = [ ] ;
3129 const redis = new Proxy ( { } as RedisSnapshotStore , {
@@ -42,8 +40,12 @@ function harness(opts: {
4240
4341 const modeResolver : SnapshotStoreModeResolver = {
4442 resolve : ( ) => opts . mode ,
45- ...( opts . everEnabled !== undefined && { everEnabled : ( ) => opts . everEnabled ! } ) ,
46- ...( opts . everEnabledForOrg && { everEnabledForOrg : opts . everEnabledForOrg } ) ,
43+ ...( opts . globalModeEverEnabled !== undefined && {
44+ globalModeEverEnabled : ( ) => opts . globalModeEverEnabled ! ,
45+ } ) ,
46+ ...( opts . orgDefinitelyNeverEnabled && {
47+ orgDefinitelyNeverEnabled : opts . orgDefinitelyNeverEnabled ,
48+ } ) ,
4749 } ;
4850
4951 const decorated = new TaskRunExecutionSnapshotStore ( delegate , {
@@ -73,68 +75,44 @@ const completion = {
7375 } ,
7476} ;
7577
76- describe ( "the residency latch" , ( ) => {
77- it ( "a transition touches the store NOT AT ALL while the latch is unset" , async ( ) => {
78- const h = harness ( { mode : "off" , everEnabled : false } ) ;
78+ describe ( "the sound transition skip" , ( ) => {
79+ it ( "touches the store NOT AT ALL for a definitely-never org while the global dial has never moved" , async ( ) => {
80+ // The assertion the whole change exists for: no keyspace can exist for this org, so no probe.
81+ const h = harness ( {
82+ mode : "dual-write" ,
83+ globalModeEverEnabled : false ,
84+ orgDefinitelyNeverEnabled : ( ) => true ,
85+ } ) ;
7986
80- await h . decorated . completeAttemptSuccess ( "run_1" , completion , { select : { id : true } } ) ;
87+ await h . decorated . completeAttemptSuccess ( "run_1" , completionForOrg ( "org_a" ) , {
88+ select : { id : true } ,
89+ } ) ;
8190
82- // The assertion the whole change exists for: no probe, so Redis is off the run path entirely.
8391 expect ( h . touched ) . toEqual ( [ ] ) ;
8492 } ) ;
8593
86- it ( "a transition still asks once the latch is set, even at off" , async ( ) => {
87- // Non-negotiable. A run resident from an earlier ramp must keep mirroring, or its head freezes
88- // and the remedy becomes worse than the fault.
89- const h = harness ( { mode : "off" , everEnabled : true } ) ;
90-
91- await h . decorated . completeAttemptSuccess ( "run_1" , completion , { select : { id : true } } ) ;
92-
93- expect ( h . touched ) . toContain ( "append" ) ;
94- } ) ;
95-
96- it ( "asks when the resolver offers no latch, so an unlatched deployment is unchanged" , async ( ) => {
97- const h = harness ( { mode : "off" } ) ;
98-
99- await h . decorated . completeAttemptSuccess ( "run_1" , completion , { select : { id : true } } ) ;
100-
101- expect ( h . touched ) . toContain ( "append" ) ;
102- } ) ;
103-
104- it ( "never suppresses a transition once the dial itself is past off" , async ( ) => {
105- // Belt and braces: the guard makes this state unreachable, but if it ever occurred, suppressing
106- // transitions while births are mirroring is the one combination that strands a head.
107- const h = harness ( { mode : "dual-write" , everEnabled : false } ) ;
108-
109- await h . decorated . completeAttemptSuccess ( "run_1" , completion , { select : { id : true } } ) ;
110-
111- expect ( h . touched ) . toContain ( "append" ) ;
112- } ) ;
113- } ) ;
114-
115- describe ( "the per-organisation latch" , ( ) => {
116- it ( "keeps redis off a never-enabled org's transition path" , async ( ) => {
117- // Deployment-wide latch unset AND this org's latch unset: nothing can be resident for it, so the
118- // probe is pure cost even though the dial has moved past off for the ramping orgs.
94+ it ( "still probes an org that has ever been enabled" , async ( ) => {
95+ // orgDefinitelyNeverEnabled is false for an org that may hold resident runs, so it keeps asking.
11996 const h = harness ( {
12097 mode : "dual-write" ,
121- everEnabled : false ,
122- everEnabledForOrg : ( ) => false ,
98+ globalModeEverEnabled : false ,
99+ orgDefinitelyNeverEnabled : ( org ) => org !== "org_a" ,
123100 } ) ;
124101
125102 await h . decorated . completeAttemptSuccess ( "run_1" , completionForOrg ( "org_a" ) , {
126103 select : { id : true } ,
127104 } ) ;
128105
129- expect ( h . touched ) . toEqual ( [ ] ) ;
106+ expect ( h . touched ) . toContain ( "append" ) ;
130107 } ) ;
131108
132- it ( "still probes an org whose latch is set" , async ( ) => {
133- // An org that has ever been enabled may hold resident runs, so its transitions must keep asking.
109+ it ( "never skips once the global dial has ever been non-off, even for a definitely-never org" , async ( ) => {
110+ // Non-negotiable. Once the global dial has moved, resident runs exist, and suppressing their
111+ // transitions freezes a head while Postgres advances. The global latch beats the per-org one.
134112 const h = harness ( {
135- mode : "dual-write " ,
136- everEnabled : false ,
137- everEnabledForOrg : ( org ) => org === "org_a" ,
113+ mode : "off " ,
114+ globalModeEverEnabled : true ,
115+ orgDefinitelyNeverEnabled : ( ) => true ,
138116 } ) ;
139117
140118 await h . decorated . completeAttemptSuccess ( "run_1" , completionForOrg ( "org_a" ) , {
@@ -144,18 +122,42 @@ describe("the per-organisation latch", () => {
144122 expect ( h . touched ) . toContain ( "append" ) ;
145123 } ) ;
146124
147- it ( "falls back to the global-only latch when the org is undefined" , ( ) => {
148- // No org id means the per-org latch cannot be consulted, so an unknown org errs toward asking:
149- // the deployment-wide check alone decides, exactly as before this latch existed .
125+ it ( "never skips when the organisation is undefined" , ( ) => {
126+ // No org id means orgDefinitelyNeverEnabled cannot be consulted, so an unknown org errs toward
127+ // asking. The per-org term is only reached once an org id is supplied .
150128 const h = harness ( {
151129 mode : "dual-write" ,
152- everEnabled : false ,
153- everEnabledForOrg : ( ) => false ,
130+ globalModeEverEnabled : false ,
131+ orgDefinitelyNeverEnabled : ( ) => true ,
154132 } ) ;
155133
156- // Past off with the global latch unset, so the global-only check probes; the per-org term is
157- // only reached once an org id is supplied.
158134 expect ( h . decorated . writesRedisForTransitionTest ( ) ) . toBe ( true ) ;
159135 expect ( h . decorated . writesRedisForTransitionTest ( "org_a" ) ) . toBe ( false ) ;
160136 } ) ;
137+
138+ it ( "asks when the resolver offers no latch signals, so an unwired deployment is unchanged" , async ( ) => {
139+ const h = harness ( { mode : "off" } ) ;
140+
141+ await h . decorated . completeAttemptSuccess ( "run_1" , completion , { select : { id : true } } ) ;
142+
143+ expect ( h . touched ) . toContain ( "append" ) ;
144+ } ) ;
145+
146+ it ( "asks when only one of the two signals permits a skip" , ( ) => {
147+ // Both halves are required. A cold census (orgDefinitelyNeverEnabled false) with an unmoved
148+ // global dial must still probe, and vice versa.
149+ const globalOnly = harness ( {
150+ mode : "off" ,
151+ globalModeEverEnabled : false ,
152+ orgDefinitelyNeverEnabled : ( ) => false ,
153+ } ) ;
154+ expect ( globalOnly . decorated . writesRedisForTransitionTest ( "org_a" ) ) . toBe ( true ) ;
155+
156+ const orgOnly = harness ( {
157+ mode : "off" ,
158+ globalModeEverEnabled : true ,
159+ orgDefinitelyNeverEnabled : ( ) => true ,
160+ } ) ;
161+ expect ( orgOnly . decorated . writesRedisForTransitionTest ( "org_a" ) ) . toBe ( true ) ;
162+ } ) ;
161163} ) ;
0 commit comments