Distinguish initial provider snapshots from live probes
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
7c257da844
commit
c9f459697d
5 files changed
+86
-65
No files matched your search
@@ -854,7 +854,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("keeps hydrated cache state while the boot snapshot is still pending", () =>
|
||||
it.effect("only keeps hydrated cache state while the boot snapshot is still initial", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const codexDriver = ProviderDriverKind.make("codex");
|
||||
@@ -882,67 +882,74 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
|
||||
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 loadProviders = (snapshotState: "initial" | "live") =>
|
||||
Effect.gen(function* () {
|
||||
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),
|
||||
isInitialSnapshot: () => snapshotState === "initial",
|
||||
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-${snapshotState}-`,
|
||||
});
|
||||
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 scope = yield* Scope.make();
|
||||
yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));
|
||||
const runtimeServices = yield* Layer.build(
|
||||
Layer.fresh(
|
||||
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),
|
||||
);
|
||||
return yield* ProviderRegistry.ProviderRegistry.pipe(
|
||||
Effect.flatMap((registry) => registry.getProviders),
|
||||
Effect.provide(runtimeServices),
|
||||
);
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(providers, [cachedProvider]);
|
||||
assert.deepStrictEqual(yield* loadProviders("initial"), [cachedProvider]);
|
||||
assert.deepStrictEqual(yield* loadProviders("live"), [fallbackProvider]);
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -257,6 +257,9 @@ const buildSnapshotSource = (instance: ProviderInstance): ProviderSnapshotSource
|
||||
instanceId: instance.instanceId,
|
||||
driverKind: instance.driverKind,
|
||||
getSnapshot: instance.snapshot.getSnapshot,
|
||||
...(instance.snapshot.isInitialSnapshot === undefined
|
||||
? {}
|
||||
: { isInitialSnapshot: instance.snapshot.isInitialSnapshot }),
|
||||
refresh: instance.snapshot.refresh,
|
||||
streamChanges: instance.snapshot.streamChanges,
|
||||
});
|
||||
@@ -636,15 +639,15 @@ export const make = Effect.gen(function* () {
|
||||
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.
|
||||
// Keep hydrated cache state only while the original boot instance
|
||||
// still exposes its explicitly-marked initial snapshot. Comparing
|
||||
// snapshot values is insufficient: the first probe may have
|
||||
// completed before registry construction, making the boot-time
|
||||
// fallback a real live result that must replace stale disk state.
|
||||
if (
|
||||
cachedInstanceIds.has(instanceId) &&
|
||||
bootInstancesById.get(instanceId) === instance &&
|
||||
bootFallback !== undefined &&
|
||||
Equal.equals(provider, bootFallback)
|
||||
source.isInitialSnapshot?.(provider) === true
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,15 @@ import type { ProviderMaintenanceCapabilities } from "../providerMaintenance.ts"
|
||||
export interface ServerProviderShape {
|
||||
readonly maintenanceCapabilities: ProviderMaintenanceCapabilities;
|
||||
readonly getSnapshot: Effect.Effect<ServerProvider>;
|
||||
/**
|
||||
* Identifies the driver's unprobed startup snapshot. Registries use this to
|
||||
* keep a hydrated disk snapshot only until the first live probe completes.
|
||||
*
|
||||
* Optional for compatibility with static/test snapshot sources. A source
|
||||
* that does not expose lifecycle state is treated as live rather than
|
||||
* allowing a stale cache entry to mask it.
|
||||
*/
|
||||
readonly isInitialSnapshot?: (snapshot: ServerProvider) => boolean;
|
||||
readonly refresh: Effect.Effect<ServerProvider>;
|
||||
readonly streamChanges: Stream.Stream<ServerProvider>;
|
||||
}
|
||||
@@ -12,6 +12,7 @@ export type ProviderSnapshotSource = {
|
||||
/** Driver implementation kind. */
|
||||
readonly driverKind: ProviderDriverKind;
|
||||
readonly getSnapshot: ServerProviderShape["getSnapshot"];
|
||||
readonly isInitialSnapshot?: (snapshot: ServerProvider) => boolean;
|
||||
readonly refresh: ServerProviderShape["refresh"];
|
||||
readonly streamChanges: Stream.Stream<ServerProvider>;
|
||||
};
|
||||
@@ -153,6 +153,7 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
|
||||
return {
|
||||
maintenanceCapabilities: input.maintenanceCapabilities,
|
||||
getSnapshot: Ref.get(snapshotStateRef).pipe(Effect.map((state) => state.snapshot)),
|
||||
isInitialSnapshot: (snapshot) => snapshot === initialSnapshot,
|
||||
refresh: refreshSnapshot().pipe(Effect.tapError(Effect.logError), Effect.orDie),
|
||||
get streamChanges() {
|
||||
return Stream.fromPubSub(changesPubSub);
|
||||
|
||||
Reference in new issue
Block a user