Skip to content
TypeScript

fix(server): a settings save no longer reads an unreadable secret again (#31226)

Yordis Prietoedeea8cd67e6
diff --git a/apps/server/src/provider/ProviderInstanceRegistry.test.ts b/apps/server/src/provider/ProviderInstanceRegistry.test.ts
index 4bae35385d6..c7ea68d585d 100644
--- a/apps/server/src/provider/ProviderInstanceRegistry.test.ts
+++ b/apps/server/src/provider/ProviderInstanceRegistry.test.ts
@@ -878,7 +878,7 @@ describe("ProviderInstanceRegistry secret environment", () => {
yield* PubSub.take(changes);
return built;
});
- return { vault, reads, invalidated, created, configs, make, makeGated, gate };
+ return { vault, reads, invalidated, created, configs, configMap, make, makeGated, gate };
}
it.effect("starts an instance with the secret its environment points at", () => {
@@ -899,13 +899,19 @@ describe("ProviderInstanceRegistry secret environment", () => {
it.effect("marks an instance unavailable and names the variable it could not read", () => {
const fixture = setup({});
return Effect.gen(function* () {
- const { registry } = yield* fixture.make;
+ const { registry, mutator } = yield* fixture.make;
+ const readsAtStartup = fixture.reads.length;
expect(yield* registry.getInstance(instanceId)).toBeUndefined();
expect(fixture.created).toEqual([]);
const [unavailable] = yield* registry.listUnavailable;
expect(unavailable?.instanceId).toBe(instanceId);
expect(unavailable?.unavailableReason).toContain("OPENAI_API_KEY");
+
+ // A settings save that leaves it alone does not read the vault again.
+ yield* mutator.reconcile({ ...fixture.configMap });
+ expect(fixture.reads).toHaveLength(readsAtStartup);
+ expect(yield* registry.listUnavailable).toHaveLength(1);
}).pipe(Effect.scoped);
});
diff --git a/apps/server/src/provider/ProviderInstanceRegistry.ts b/apps/server/src/provider/ProviderInstanceRegistry.ts
index cc3ca60fcb8..da1fd687f48 100644
--- a/apps/server/src/provider/ProviderInstanceRegistry.ts
+++ b/apps/server/src/provider/ProviderInstanceRegistry.ts
@@ -60,6 +60,9 @@ interface LiveEntry {
interface RegistryState {
readonly entries: Ref.Ref<ReadonlyMap<ProviderInstanceId, LiveEntry>>;
readonly unavailable: Ref.Ref<ReadonlyMap<ProviderInstanceId, ServerProvider>>;
+ // Configs parked on an unreadable secret. A settings save that leaves one alone does not read
+ // its vault again, so a locked vault is not prompted for on every unrelated save.
+ readonly secretsUnreadable: Ref.Ref<ReadonlyMap<ProviderInstanceId, ProviderInstanceConfig>>;
readonly changes: PubSub.PubSub<void>;
}
@@ -155,7 +158,11 @@ const buildEntry = Effect.fn("ProviderInstanceRegistry.buildEntry")(function* <R
readonly deferSecrets: boolean;
}): Effect.fn.Return<
| { readonly kind: "live"; readonly live: LiveEntry }
- | { readonly kind: "unavailable"; readonly snapshot: ServerProvider },
+ | {
+ readonly kind: "unavailable";
+ readonly snapshot: ServerProvider;
+ readonly secretsUnreadable?: true;
+ },
never,
R | SecretResolver.SecretResolver
> {
@@ -234,6 +241,7 @@ const buildEntry = Effect.fn("ProviderInstanceRegistry.buildEntry")(function* <R
if (unreadable.length > 0) {
return {
kind: "unavailable" as const,
+ secretsUnreadable: true as const,
snapshot: yield* buildUnavailableProviderSnapshot({
driverKind: entry.driver,
instanceId,
@@ -304,6 +312,7 @@ const makeReconcile = <R>(input: {
Effect.gen(function* () {
const previousEntries = yield* Ref.get(state.entries);
const previousUnavailable = yield* Ref.get(state.unavailable);
+ const previousSecretsUnreadable = yield* Ref.get(state.secretsUnreadable);
const nextRaw = Object.entries(configMap);
const nextKeys = new Set<ProviderInstanceId>(
nextRaw.map(([raw]) => ProviderInstanceId.make(raw)),
@@ -338,12 +347,21 @@ const makeReconcile = <R>(input: {
}
}
+ const parkedSnapshot = (instanceId: ProviderInstanceId, entry: ProviderInstanceConfig) => {
+ const parked = previousSecretsUnreadable.get(instanceId);
+ return !rebuild.has(instanceId) && parked !== undefined && entryEqual(parked, entry)
+ ? previousUnavailable.get(instanceId)
+ : undefined;
+ };
+
// Read every secret the builds below need in one go, so a vault unlocks once.
const references = yield* Effect.forEach(nextRaw, ([rawInstanceId, entry]) =>
Effect.gen(function* () {
const instanceId = ProviderInstanceId.make(rawInstanceId);
const kept = previousEntries.has(instanceId) && !replacedIds.has(instanceId);
- return kept || !(yield* isEntryEnabled(driversById, entry))
+ return kept ||
+ parkedSnapshot(instanceId, entry) !== undefined ||
+ !(yield* isEntryEnabled(driversById, entry))
? []
: secretReferencesOf(driversById, entry);
}),
@@ -354,6 +372,7 @@ const makeReconcile = <R>(input: {
const builtEntries = new Map<ProviderInstanceId, LiveEntry>();
const builtUnavailable = new Map<ProviderInstanceId, ServerProvider>();
+ const builtSecretsUnreadable = new Map<ProviderInstanceId, ProviderInstanceConfig>();
let orderChanged = false;
const previousOrder = [...previousEntries.keys()];
const nextOrder: Array<ProviderInstanceId> = [];
@@ -367,6 +386,12 @@ const makeReconcile = <R>(input: {
builtEntries.set(instanceId, existing);
continue;
}
+ const parked = parkedSnapshot(instanceId, entry);
+ if (parked !== undefined) {
+ builtUnavailable.set(instanceId, parked);
+ builtSecretsUnreadable.set(instanceId, entry);
+ continue;
+ }
const result = yield* buildEntry({
driversById,
@@ -381,6 +406,7 @@ const makeReconcile = <R>(input: {
builtEntries.set(instanceId, result.live);
} else {
builtUnavailable.set(instanceId, result.snapshot);
+ if (result.secretsUnreadable) builtSecretsUnreadable.set(instanceId, entry);
}
}
@@ -410,6 +436,7 @@ const makeReconcile = <R>(input: {
yield* Ref.set(state.entries, builtEntries);
yield* Ref.set(state.unavailable, builtUnavailable);
+ yield* Ref.set(state.secretsUnreadable, builtSecretsUnreadable);
if (entriesChanged || unavailableChanged) {
yield* PubSub.publish(state.changes, undefined);
@@ -446,10 +473,13 @@ export const makeProviderInstanceRegistry = Effect.fn(
const entries = yield* Ref.make<ReadonlyMap<ProviderInstanceId, LiveEntry>>(new Map());
const unavailable = yield* Ref.make<ReadonlyMap<ProviderInstanceId, ServerProvider>>(new Map());
+ const secretsUnreadable = yield* Ref.make<
+ ReadonlyMap<ProviderInstanceId, ProviderInstanceConfig>
+ >(new Map());
const changes = yield* PubSub.unbounded<void>();
yield* Effect.addFinalizer(() => PubSub.shutdown(changes));
- const state: RegistryState = { entries, unavailable, changes };
+ const state: RegistryState = { entries, unavailable, secretsUnreadable, changes };
const reconcileWithR = makeReconcile({
state,
driversById,
@@ -488,7 +518,10 @@ export const makeProviderInstanceRegistry = Effect.fn(
const rebuild = new Set<ProviderInstanceId>();
for (const [rawInstanceId, entry] of targets) {
const current = live.get(ProviderInstanceId.make(rawInstanceId));
- if (current === undefined) continue;
+ if (current === undefined) {
+ rebuild.add(ProviderInstanceId.make(rawInstanceId));
+ continue;
+ }
const { secrets, unreadable } = yield* readInstanceSecrets(
driversById.get(entry.driver),
entry,
diff --git a/apps/server/src/provider/ProviderInstanceRegistryHydration.test.ts b/apps/server/src/provider/ProviderInstanceRegistryHydration.test.ts
index c497e00ac17..dc44f0324a6 100644
--- a/apps/server/src/provider/ProviderInstanceRegistryHydration.test.ts
+++ b/apps/server/src/provider/ProviderInstanceRegistryHydration.test.ts
@@ -3,12 +3,16 @@ import {
ProjectId,
ProviderDriverKind,
ProviderInstanceId,
+ SecretProviderId,
+ SecretProviderKind,
type ServerSettings,
} from "@t3tools/contracts";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it as effectIt } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
+import * as Queue from "effect/Queue";
+import * as Stream from "effect/Stream";
import { describe, expect, it } from "vite-plus/test";
import * as ServerConfig from "../config.ts";
@@ -16,8 +20,10 @@ import { ServerSettingsService } from "../serverSettings.ts";
import {
DEV_REPLAY_INSTANCE_ID,
deriveProviderInstanceConfigMap,
+ layerSettingsWatcher,
seedDevReplayInstance,
} from "./ProviderInstanceRegistryHydration.ts";
+import { ProviderInstanceRegistryMutator } from "./ProviderInstanceRegistryMutator.ts";
const projectId = ProjectId.make("project-a");
const instanceId = ProviderInstanceId.make("codex_work");
@@ -141,3 +147,67 @@ describe("seedDevReplayInstance", () => {
),
);
});
+
+describe("layerSettingsWatcher", () => {
+ const provider = (label: string) => ({
+ [SecretProviderId.make("personal")]: {
+ kind: SecretProviderKind.make("one_password"),
+ label,
+ config: { account: "my" },
+ },
+ });
+
+ const watch = (initial: ServerSettings | undefined) =>
+ Effect.gen(function* () {
+ const changes = yield* Queue.unbounded<ServerSettings>();
+ const reconciled = yield* Queue.unbounded<void>();
+ let reloads = 0;
+ const mutator = Layer.succeed(ProviderInstanceRegistryMutator, {
+ reconcile: () => Queue.offer(reconciled, undefined),
+ reloadSecrets: () => Effect.sync(() => void reloads++),
+ });
+ yield* Layer.build(
+ layerSettingsWatcher(Stream.fromQueue(changes), initial).pipe(Layer.provide(mutator)),
+ );
+ return {
+ reloads: () => reloads,
+ // Changes run one at a time, so the repeat's reconcile means the first one's reload decision is done.
+ change: (settings: ServerSettings) =>
+ Queue.offerAll(changes, [settings, settings]).pipe(
+ Effect.andThen(Queue.take(reconciled)),
+ Effect.andThen(Queue.take(reconciled)),
+ ),
+ };
+ });
+
+ effectIt.effect(
+ "retries unreadable secrets when a secret provider changes, not when it is renamed",
+ () =>
+ Effect.gen(function* () {
+ const settings = yield* ServerSettingsService;
+ const { reloads, change } = yield* watch(yield* settings.getSettings);
+
+ yield* change(yield* settings.updateSettings({ secretProviders: provider("Personal") }));
+ expect(reloads()).toBe(1);
+
+ // A label is not read by any vault, so renaming a provider retries nothing.
+ yield* change(yield* settings.updateSettings({ secretProviders: provider("Renamed") }));
+ expect(reloads()).toBe(1);
+ }).pipe(Effect.scoped, Effect.provide(ServerSettingsService.layerTest())),
+ );
+
+ effectIt.effect(
+ "compares a change queued during startup with the settings the registry started from",
+ () =>
+ Effect.gen(function* () {
+ const settings = yield* ServerSettingsService;
+ const started = yield* settings.getSettings;
+ const saved = yield* settings.updateSettings({ secretProviders: provider("Personal") });
+ const { reloads, change } = yield* watch(started);
+
+ yield* change(saved);
+
+ expect(reloads()).toBe(1);
+ }).pipe(Effect.scoped, Effect.provide(ServerSettingsService.layerTest())),
+ );
+});
diff --git a/apps/server/src/provider/ProviderInstanceRegistryHydration.ts b/apps/server/src/provider/ProviderInstanceRegistryHydration.ts
index fbf7a5320c4..256bf2e7979 100644
--- a/apps/server/src/provider/ProviderInstanceRegistryHydration.ts
+++ b/apps/server/src/provider/ProviderInstanceRegistryHydration.ts
@@ -8,6 +8,8 @@ import {
type ProviderInstanceConfigMap,
ServerSettings,
} from "@t3tools/contracts";
+import * as NodeUtil from "node:util";
+
import * as HostProcess from "@t3tools/shared/HostProcess";
import type * as SecureRandom from "effect/crypto/SecureRandom";
import * as Effect from "effect/Effect";
@@ -122,18 +124,36 @@ export const seedDevReplayInstance = Effect.fn(
});
});
-const layerSettingsWatcher = (settingsChanges: Stream.Stream<ServerSettings>) =>
+// A label is display-only, so only kind and config can change what a vault returns.
+const vaultConfigs = (settings: ServerSettings) =>
+ Object.entries(settings.secretProviders).map(([id, { kind, config }]) => [id, kind, config]);
+
+export const layerSettingsWatcher = (
+ settingsChanges: Stream.Stream<ServerSettings>,
+ // The settings the registry was built from: a change queued during startup differs from these.
+ initialSettings: ServerSettings | undefined,
+) =>
Layer.effectDiscard(
Effect.gen(function* () {
const mutator = yield* ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutator;
const serverSettings = yield* Settings.ServerSettingsService;
+ let secretProviders = initialSettings && vaultConfigs(initialSettings);
yield* settingsChanges.pipe(
Stream.runForEach(() =>
// The subscription predates the seed read, so a queued change can be
// older than the seed. A failed current read must leave the registry intact.
serverSettings.getSettings.pipe(
Effect.flatMap((current) =>
- mutator.reconcile(deriveProviderInstanceConfigMap(current)),
+ Effect.gen(function* () {
+ yield* mutator.reconcile(deriveProviderInstanceConfigMap(current));
+ // Reconcile leaves instances parked on an unreadable secret alone,
+ // and a corrected secret provider is what can make them readable.
+ const next = vaultConfigs(current);
+ if (!NodeUtil.isDeepStrictEqual(secretProviders, next)) {
+ secretProviders = next;
+ yield* mutator.reloadSecrets();
+ }
+ }),
),
Effect.catchCause((cause) =>
Effect.logError("ProviderInstanceRegistry reconcile failed", cause),
@@ -186,7 +206,9 @@ export const layer: Layer.Layer<
Layer.provide(SecretResolver.layer),
);
- return layerSettingsWatcher(settingsChanges).pipe(Layer.provideMerge(layerMutable));
+ return layerSettingsWatcher(settingsChanges, initialSettings).pipe(
+ Layer.provideMerge(layerMutable),
+ );
}),
) as Layer.Layer<
| ProviderInstanceRegistry.ProviderInstanceRegistry