Preserve hydrated provider cache during boot
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
71c6069aa9
commit
7c257da844
2 files changed
+122
-9
No files matched your search
@@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { describe, it, assert } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Fiber from "effect/Fiber";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as PubSub from "effect/PubSub";
|
||||
@@ -42,7 +43,11 @@ import { ProviderInstanceRegistryHydrationLive } from "./Layers/ProviderInstance
|
||||
import * as ProviderRegistry from "./ProviderRegistry.ts";
|
||||
import * as ServerConfig from "../config.ts";
|
||||
import * as ServerSettingsModule from "../serverSettings.ts";
|
||||
import { readProviderStatusCache, resolveProviderStatusCachePath } from "./providerStatusCache.ts";
|
||||
import {
|
||||
readProviderStatusCache,
|
||||
resolveProviderStatusCachePath,
|
||||
writeProviderStatusCache,
|
||||
} from "./providerStatusCache.ts";
|
||||
import type { ProviderInstance } from "./ProviderDriver.ts";
|
||||
import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts";
|
||||
import { makeManualOnlyProviderMaintenanceCapabilities } from "./providerMaintenance.ts";
|
||||
@@ -849,6 +854,98 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("keeps hydrated cache state while the boot snapshot is still pending", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const codexDriver = ProviderDriverKind.make("codex");
|
||||
const codexInstanceId = ProviderInstanceId.make("codex");
|
||||
const fallbackProvider = {
|
||||
instanceId: codexInstanceId,
|
||||
driver: codexDriver,
|
||||
status: "warning",
|
||||
enabled: true,
|
||||
installed: false,
|
||||
auth: { status: "unknown" },
|
||||
checkedAt: "2026-04-29T10:01:00.000Z",
|
||||
version: null,
|
||||
models: [],
|
||||
slashCommands: [],
|
||||
skills: [],
|
||||
message: "Codex provider status has not been checked in this session yet.",
|
||||
} as const satisfies ServerProvider;
|
||||
const cachedProvider = {
|
||||
...fallbackProvider,
|
||||
status: "ready",
|
||||
installed: true,
|
||||
auth: { status: "authenticated" },
|
||||
checkedAt: "2026-04-29T10:00:00.000Z",
|
||||
version: "1.0.0",
|
||||
message: "Loaded from the provider status cache.",
|
||||
} as const satisfies ServerProvider;
|
||||
const instance = {
|
||||
instanceId: codexInstanceId,
|
||||
driverKind: codexDriver,
|
||||
continuationIdentity: {
|
||||
driverKind: codexDriver,
|
||||
continuationKey: "codex:instance:codex",
|
||||
},
|
||||
displayName: undefined,
|
||||
enabled: true,
|
||||
snapshot: {
|
||||
maintenanceCapabilities: makeManualOnlyProviderMaintenanceCapabilities({
|
||||
provider: codexDriver,
|
||||
packageName: null,
|
||||
}),
|
||||
getSnapshot: Effect.succeed(fallbackProvider),
|
||||
refresh: Effect.succeed(fallbackProvider),
|
||||
streamChanges: Stream.empty,
|
||||
},
|
||||
adapter: {} as ProviderInstance["adapter"],
|
||||
textGeneration: {} as ProviderInstance["textGeneration"],
|
||||
} satisfies ProviderInstance;
|
||||
const instanceRegistryLayer = Layer.succeed(
|
||||
ProviderInstanceRegistry.ProviderInstanceRegistry,
|
||||
{
|
||||
getInstance: (instanceId) =>
|
||||
Effect.succeed(instanceId === codexInstanceId ? instance : undefined),
|
||||
listInstances: Effect.succeed([instance]),
|
||||
listUnavailable: Effect.succeed([]),
|
||||
streamChanges: Stream.empty,
|
||||
subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), (pubsub) =>
|
||||
PubSub.subscribe(pubsub),
|
||||
),
|
||||
},
|
||||
);
|
||||
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
|
||||
prefix: "t3-provider-registry-cache-hydration-",
|
||||
});
|
||||
const configLayer = ServerConfig.layerTest(process.cwd(), baseDir);
|
||||
const config = yield* ServerConfig.ServerConfig.pipe(Effect.provide(configLayer));
|
||||
const filePath = yield* resolveProviderStatusCachePath({
|
||||
cacheDir: config.providerStatusCacheDir,
|
||||
instanceId: codexInstanceId,
|
||||
});
|
||||
yield* writeProviderStatusCache({ filePath, provider: cachedProvider });
|
||||
|
||||
const scope = yield* Scope.make();
|
||||
yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));
|
||||
const runtimeServices = yield* Layer.build(
|
||||
ProviderRegistry.layer.pipe(
|
||||
Layer.provideMerge(instanceRegistryLayer),
|
||||
Layer.provideMerge(configLayer),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
),
|
||||
).pipe(Scope.provide(scope));
|
||||
|
||||
const providers = yield* ProviderRegistry.ProviderRegistry.pipe(
|
||||
Effect.flatMap((registry) => registry.getProviders),
|
||||
Effect.provide(runtimeServices),
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(providers, [cachedProvider]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("returns the cached provider list when a manual refresh fails", () =>
|
||||
Effect.gen(function* () {
|
||||
const codexDriver = ProviderDriverKind.make("codex");
|
||||
|
||||
@@ -341,6 +341,10 @@ export const make = Effect.gen(function* () {
|
||||
),
|
||||
),
|
||||
);
|
||||
const cachedInstanceIds = new Set(cachedProviders.map(snapshotInstanceKey));
|
||||
const bootInstancesById = new Map(
|
||||
bootInstances.map((instance) => [instance.instanceId, instance] as const),
|
||||
);
|
||||
const providersRef = yield* Ref.make<ReadonlyArray<ServerProvider>>(cachedProviders);
|
||||
const maintenanceActionStatesRef = yield* Ref.make<
|
||||
ReadonlyMap<ProviderInstanceId, { readonly update?: ServerProviderUpdateState | undefined }>
|
||||
@@ -628,10 +632,22 @@ export const make = Effect.gen(function* () {
|
||||
// or HTTP server construction path.
|
||||
yield* Effect.forEach(
|
||||
newlyAdded,
|
||||
([, instance]) =>
|
||||
([instanceId, instance]) =>
|
||||
Effect.gen(function* () {
|
||||
const source = buildSnapshotSource(instance);
|
||||
const provider = yield* source.getSnapshot;
|
||||
const bootFallback = fallbackByInstance.get(instanceId);
|
||||
// Keep hydrated cache state when this is still the exact pending
|
||||
// snapshot read during boot. A probe that completed before the
|
||||
// subscription was attached differs here and is applied instead.
|
||||
if (
|
||||
cachedInstanceIds.has(instanceId) &&
|
||||
bootInstancesById.get(instanceId) === instance &&
|
||||
bootFallback !== undefined &&
|
||||
Equal.equals(provider, bootFallback)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
yield* correlateSnapshotWithSource(source, provider).pipe(Effect.flatMap(syncProvider));
|
||||
}).pipe(Effect.ignoreCause({ log: true })),
|
||||
{ concurrency: "unbounded", discard: true },
|
||||
@@ -685,13 +701,13 @@ export const make = Effect.gen(function* () {
|
||||
}),
|
||||
);
|
||||
|
||||
// Seed `providersRef` with the boot-time fallback snapshots so
|
||||
// consumers calling `getProviders` immediately after layer build see
|
||||
// a populated list — even before the first `syncLiveSources` refresh
|
||||
// resolves. Cached snapshots (already in `providersRef`) merge with
|
||||
// these via `upsertProviders` so on-disk state wins where present
|
||||
// and pending fallbacks fill the gaps.
|
||||
yield* upsertProviders(fallbackProviders, { publish: false });
|
||||
// Cached snapshots are already in `providersRef`; only seed instances
|
||||
// without cache entries so pending fallbacks cannot overwrite hydrated
|
||||
// status while the managed provider's first probe is still running.
|
||||
yield* upsertProviders(
|
||||
fallbackProviders.filter((provider) => !cachedInstanceIds.has(snapshotInstanceKey(provider))),
|
||||
{ publish: false },
|
||||
);
|
||||
// Subscribe to registry mutations BEFORE running the initial sync.
|
||||
// `subscribeChanges` acquires the `PubSub.Subscription` synchronously
|
||||
// in this fibre; the subscription is registered with the PubSub the
|
||||
|
||||
Reference in new issue
Block a user