@@ -9,10 +9,10 @@ import * as Duration from "effect/Duration";
99import * as Effect from "effect/Effect" ;
1010import * as Equal from "effect/Equal" ;
1111import * as Fiber from "effect/Fiber" ;
12+ import * as Option from "effect/Option" ;
1213import * as PubSub from "effect/PubSub" ;
1314import * as Queue from "effect/Queue" ;
1415import * as Ref from "effect/Ref" ;
15- import * as Schema from "effect/Schema" ;
1616import * as Scope from "effect/Scope" ;
1717import * as Stream from "effect/Stream" ;
1818import * as Semaphore from "effect/Semaphore" ;
@@ -22,8 +22,6 @@ import { ServerSettingsService } from "../serverSettings.ts";
2222import { ProviderProbeTimeoutError } from "./providerSnapshot.ts" ;
2323import type { ServerProviderShape } from "./Services/ServerProvider.ts" ;
2424
25- const isProviderProbeTimeoutError = Schema . is ( ProviderProbeTimeoutError ) ;
26-
2725interface ProviderSnapshotState {
2826 readonly snapshot : ServerProvider ;
2927 readonly enrichmentGeneration : number ;
@@ -67,9 +65,9 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
6765 enrichmentGeneration : 0 ,
6866 } ) ;
6967 const settingsRef = yield * Ref . make ( initialSettings ) ;
70- // Gates the "keep what we knew" path: before the first successful check there
71- // is no known status, only the pending placeholder .
72- const hasCompletedCheck = yield * Ref . make ( false ) ;
68+ // Gates the "keep what we knew" path: set once a check produces a status the
69+ // UI can act on, cleared again if a later check reports the provider broken .
70+ const hasUsableStatus = yield * Ref . make ( false ) ;
7371 const enrichmentFiberRef = yield * Ref . make < Fiber . Fiber < void , unknown > | null > ( null ) ;
7472 const scope = yield * Effect . scope ;
7573
@@ -133,17 +131,18 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
133131 * the cache, a manual refresh visibly does something, and the timeout is
134132 * stated rather than leaving the provider silently frozen.
135133 *
136- * Before the first successful check there is nothing worth carrying forward.
137- * The pending snapshot describes a check that has not run, not one that ran
138- * out of time, so the timeout is reported plainly instead.
134+ * Without a usable status there is nothing worth carrying forward. The
135+ * pending snapshot describes a check that has not run, and a failed one
136+ * describes a provider that was already broken, so the timeout is reported
137+ * plainly along with whatever the probe did establish before it gave up.
139138 */
140139 const snapshotAfterProbeTimeout = Effect . fn ( "snapshotAfterProbeTimeout" ) ( function * (
141140 error : ProviderProbeTimeoutError ,
142141 ) {
143142 const checkedAt = DateTime . formatIso ( yield * DateTime . now ) ;
144- const checkedBefore = yield * Ref . get ( hasCompletedCheck ) ;
143+ const carryForward = yield * Ref . get ( hasUsableStatus ) ;
145144 const next = yield * Ref . modify ( snapshotStateRef , ( state ) => {
146- const snapshot : ServerProvider = checkedBefore
145+ const snapshot : ServerProvider = carryForward
147146 ? {
148147 ...state . snapshot ,
149148 checkedAt,
@@ -153,11 +152,21 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
153152 ...state . snapshot ,
154153 checkedAt,
155154 installed : error . installed ,
155+ version : error . version ?? state . snapshot . version ,
156156 status : "error" ,
157157 message : error . message ,
158158 } ;
159- return [ snapshot , { ...state , snapshot } ] as const ;
159+ // Bumping the generation retires any enrichment still running against the
160+ // superseded snapshot, so it cannot publish over this timeout later.
161+ return [
162+ snapshot ,
163+ { snapshot, enrichmentGeneration : state . enrichmentGeneration + 1 } ,
164+ ] as const ;
160165 } ) ;
166+ const staleEnrichment = yield * Ref . getAndSet ( enrichmentFiberRef , null ) ;
167+ if ( staleEnrichment ) {
168+ yield * Fiber . interrupt ( staleEnrichment ) . pipe ( Effect . ignore ) ;
169+ }
161170 yield * PubSub . publish ( changesPubSub , next ) ;
162171 return next ;
163172 } ) ;
@@ -173,26 +182,32 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
173182 return yield * Ref . get ( snapshotStateRef ) . pipe ( Effect . map ( ( state ) => state . snapshot ) ) ;
174183 }
175184
176- const checkOutcome = yield * input . checkProvider . pipe (
177- Effect . tap ( ( ) => Ref . set ( hasCompletedCheck , true ) ) ,
178- Effect . catchTag ( "ProviderProbeTimeoutError" , ( error ) =>
179- Effect . logWarning ( "Provider status probe timed out." ) . pipe (
180- Effect . annotateLogs ( {
181- "provider.name" : error . provider ,
182- "provider.probe" : error . probe ,
183- "provider.probe.timeout_ms" : error . timeoutMs ,
184- } ) ,
185- Effect . as ( error ) ,
186- ) ,
187- ) ,
185+ const checked = yield * input . checkProvider . pipe (
186+ // Only a status the UI can act on is worth protecting from a later
187+ // timeout. A verdict of "broken" is not, or a provider that was missing
188+ // when last checked would stay missing after it is installed.
189+ Effect . tap ( ( snapshot ) => Ref . set ( hasUsableStatus , snapshot . status !== "error" ) ) ,
190+ Effect . map ( Option . some ) ,
191+ Effect . catchTags ( {
192+ ProviderProbeTimeoutError : ( error ) =>
193+ Effect . logWarning ( "Provider status probe timed out." ) . pipe (
194+ Effect . annotateLogs ( {
195+ "provider.name" : error . provider ,
196+ "provider.probe" : error . probe ,
197+ "provider.probe.timeout_ms" : error . timeoutMs ,
198+ } ) ,
199+ Effect . andThen ( snapshotAfterProbeTimeout ( error ) ) ,
200+ Effect . as ( Option . none < ServerProvider > ( ) ) ,
201+ ) ,
202+ } ) ,
188203 ) ;
189- if ( isProviderProbeTimeoutError ( checkOutcome ) ) {
204+ if ( Option . isNone ( checked ) ) {
190205 // `settingsRef` deliberately stays on the previous value: these settings
191206 // were never applied to a snapshot, so the next emission must still count
192207 // as a change and re-probe rather than short-circuiting above.
193- return yield * snapshotAfterProbeTimeout ( checkOutcome ) ;
208+ return yield * Ref . get ( snapshotStateRef ) . pipe ( Effect . map ( ( state ) => state . snapshot ) ) ;
194209 }
195- const nextSnapshot = checkOutcome ;
210+ const nextSnapshot = checked . value ;
196211 const nextGeneration = yield * Ref . modify ( snapshotStateRef , ( state ) => {
197212 const generation = input . enrichSnapshot
198213 ? state . enrichmentGeneration + 1
0 commit comments