@@ -30,6 +30,8 @@ import {
3030 ServerSettingsError ,
3131 ServerProvider ,
3232 type ServerProviderSlashCommand ,
33+ type ServerProviderUpdateState ,
34+ type ServerProviderVersionAdvisoryStatus ,
3335 type ServerSettings as ContractServerSettings ,
3436} from "@t3tools/contracts" ;
3537import { HttpClient , HttpClientResponse } from "effect/http" ;
@@ -305,6 +307,85 @@ const awaitPersistedProvider = (
305307 Effect . forkScoped ,
306308 ) ;
307309
310+ const codexUpdateSnapshot = ( advisory : ServerProviderVersionAdvisoryStatus ) =>
311+ ( {
312+ instanceId : ProviderInstanceId . make ( "codex" ) ,
313+ driver : ProviderDriverKind . make ( "codex" ) ,
314+ status : "ready" ,
315+ enabled : true ,
316+ installed : true ,
317+ auth : { status : "authenticated" } ,
318+ checkedAt : "2026-06-10T00:00:00.000Z" ,
319+ version : "1.0.0" ,
320+ models : [ ] ,
321+ slashCommands : [ ] ,
322+ skills : [ ] ,
323+ versionAdvisory : {
324+ status : advisory ,
325+ currentVersion : "1.0.0" ,
326+ latestVersion : advisory === "unknown" ? null : "2.0.0" ,
327+ updateCommand : null ,
328+ canUpdate : true ,
329+ checkedAt : "2026-06-10T00:00:00.000Z" ,
330+ message : null ,
331+ } ,
332+ } ) as const satisfies ServerProvider ;
333+
334+ const makeUpdateState = ( status : ServerProviderUpdateState [ "status" ] ) =>
335+ ( {
336+ status,
337+ startedAt : "2026-06-10T00:00:00.000Z" ,
338+ finishedAt : status === "queued" || status === "running" ? null : "2026-06-10T00:01:00.000Z" ,
339+ message : `update ${ status } ` ,
340+ output : null ,
341+ } ) satisfies ServerProviderUpdateState ;
342+
343+ // One Codex instance whose probe returns whatever `snapshot` currently holds.
344+ const buildRegistryOverSnapshot = Effect . fn ( "buildRegistryOverSnapshot" ) ( function * (
345+ snapshot : Ref . Ref < ServerProvider > ,
346+ ) {
347+ const driver = ProviderDriverKind . make ( "codex" ) ;
348+ const instance = {
349+ instanceId : ProviderInstanceId . make ( "codex" ) ,
350+ driverKind : driver ,
351+ continuationIdentity : { driverKind : driver , continuationKey : "codex:instance:codex" } ,
352+ displayName : undefined ,
353+ enabled : true ,
354+ snapshot : {
355+ resolveMaintenance : ( ) =>
356+ Effect . succeed (
357+ makeManualOnlyProviderMaintenanceCapabilities ( { provider : driver , packageName : null } ) ,
358+ ) ,
359+ getSnapshot : Ref . get ( snapshot ) ,
360+ refresh : Ref . get ( snapshot ) ,
361+ streamChanges : Stream . empty ,
362+ applyUsageLimits : ( ) => Effect . void ,
363+ } ,
364+ orchestrationAdapter : { } as ProviderInstance [ "orchestrationAdapter" ] ,
365+ textGeneration : { } as ProviderInstance [ "textGeneration" ] ,
366+ } satisfies ProviderInstance ;
367+ const layerInstanceRegistry = Layer . succeed ( ProviderInstanceRegistry . ProviderInstanceRegistry , {
368+ getInstance : ( instanceId ) =>
369+ Effect . succeed ( instanceId === instance . instanceId ? instance : undefined ) ,
370+ listInstances : Effect . succeed ( [ instance ] ) ,
371+ listUnavailable : Effect . succeed ( [ ] ) ,
372+ streamChanges : Stream . empty ,
373+ subscribeChanges : Effect . flatMap ( PubSub . unbounded < void > ( ) , PubSub . subscribe ) ,
374+ } ) ;
375+ const scope = yield * Scope . make ( ) ;
376+ yield * Effect . addFinalizer ( ( ) => Scope . close ( scope , Exit . void ) ) ;
377+ return yield * Layer . build (
378+ ProviderRegistry . layer . pipe (
379+ Layer . provideMerge ( layerInstanceRegistry ) ,
380+ Layer . provideMerge (
381+ ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-provider-registry-update-state-" } ) ,
382+ ) ,
383+ Layer . provideMerge ( layerBackgroundPolicyAlwaysRun ) ,
384+ Layer . provideMerge ( NodeServices . layer ) ,
385+ ) ,
386+ ) . pipe ( Scope . provide ( scope ) ) ;
387+ } ) ;
388+
308389const layerTestNodeServices = Layer . mergeAll (
309390 ProviderLatestVersions . layer ,
310391 NodeServices . layer ,
@@ -1808,6 +1889,74 @@ it.layer(
18081889 } ) ,
18091890 ) ;
18101891
1892+ describe ( "update results across refreshes" , ( ) => {
1893+ const instanceId = ProviderInstanceId . make ( "codex" ) ;
1894+
1895+ it . effect . each ( [
1896+ { status : "unchanged" , advisory : "current" , retired : true } ,
1897+ { status : "failed" , advisory : "current" , retired : true } ,
1898+ { status : "unchanged" , advisory : "behind_latest" , retired : false } ,
1899+ { status : "failed" , advisory : "behind_latest" , retired : false } ,
1900+ // The probe publishes `unknown` until the latest-version check lands.
1901+ { status : "unchanged" , advisory : "unknown" , retired : false } ,
1902+ { status : "queued" , advisory : "current" , retired : false } ,
1903+ { status : "running" , advisory : "current" , retired : false } ,
1904+ ] as const ) (
1905+ "a $status update result is retired: $retired when a refresh reports the provider $advisory" ,
1906+ ( { status, advisory, retired } ) =>
1907+ Effect . gen ( function * ( ) {
1908+ const snapshot = yield * Ref . make < ServerProvider > ( codexUpdateSnapshot ( "behind_latest" ) ) ;
1909+ const runtimeServices = yield * buildRegistryOverSnapshot ( snapshot ) ;
1910+
1911+ yield * Effect . gen ( function * ( ) {
1912+ const registry = yield * ProviderRegistry . ProviderRegistry ;
1913+ const state = makeUpdateState ( status ) ;
1914+ yield * registry . setProviderMaintenanceActionState ( {
1915+ instanceId,
1916+ action : "update" ,
1917+ state,
1918+ } ) ;
1919+ yield * Ref . set ( snapshot , codexUpdateSnapshot ( advisory ) ) ;
1920+
1921+ const [ refreshed ] = yield * registry . refresh ( ProviderDriverKind . make ( "codex" ) ) ;
1922+
1923+ expect ( refreshed ?. updateState ) . toEqual ( retired ? undefined : state ) ;
1924+ } ) . pipe ( Effect . provide ( runtimeServices ) ) ;
1925+ } ) ,
1926+ ) ;
1927+
1928+ it . effect ( "does not bring a retired result back, and a later update still reports" , ( ) =>
1929+ Effect . gen ( function * ( ) {
1930+ const snapshot = yield * Ref . make < ServerProvider > ( codexUpdateSnapshot ( "behind_latest" ) ) ;
1931+ const runtimeServices = yield * buildRegistryOverSnapshot ( snapshot ) ;
1932+
1933+ yield * Effect . gen ( function * ( ) {
1934+ const registry = yield * ProviderRegistry . ProviderRegistry ;
1935+ const codex = ProviderDriverKind . make ( "codex" ) ;
1936+ yield * registry . setProviderMaintenanceActionState ( {
1937+ instanceId,
1938+ action : "update" ,
1939+ state : makeUpdateState ( "unchanged" ) ,
1940+ } ) ;
1941+ yield * Ref . set ( snapshot , codexUpdateSnapshot ( "current" ) ) ;
1942+ yield * registry . refresh ( codex ) ;
1943+
1944+ yield * Ref . set ( snapshot , codexUpdateSnapshot ( "unknown" ) ) ;
1945+ const [ reprobed ] = yield * registry . refresh ( codex ) ;
1946+ expect ( reprobed ?. updateState ) . toBeUndefined ( ) ;
1947+
1948+ const running = makeUpdateState ( "running" ) ;
1949+ const [ updating ] = yield * registry . setProviderMaintenanceActionState ( {
1950+ instanceId,
1951+ action : "update" ,
1952+ state : running ,
1953+ } ) ;
1954+ expect ( updating ?. updateState ) . toEqual ( running ) ;
1955+ } ) . pipe ( Effect . provide ( runtimeServices ) ) ;
1956+ } ) ,
1957+ ) ;
1958+ } ) ;
1959+
18111960 it . effect ( "re-probes when settings change the codex binaryPath" , ( ) =>
18121961 Effect . gen ( function * ( ) {
18131962 const firstMissing = `t3code_codex_first_` ;
0 commit comments