Skip to content
TypeScript

refactor(settings): centralize schema-declared secrets and inputs (#18505)

Julius Marminged514a8c6176a
diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts
index 43b6eaf1ce6..1cbd6c7bf81 100644
--- a/apps/server/src/serverSettings.test.ts
+++ b/apps/server/src/serverSettings.test.ts
@@ -9,6 +9,7 @@ import {
resolveProviderInstanceEnabled,
ServerSettings,
ServerSettingsPatch,
+ UsageLimitSourceId,
} from "@t3tools/contracts";
import { createModelSelection } from "@t3tools/shared/model";
import { assert, it } from "@effect/vitest";
@@ -1440,6 +1441,156 @@ it.layer(NodeServices.layer)("server settings", (it) => {
}).pipe(Effect.provide(layerServerSettings())),
);
+ it.effect(
+ "uses the same secret lifecycle for provider config, environment, host tokens, and quota keys",
+ () =>
+ Effect.gen(function* () {
+ const service = yield* ServerSettingsModule.ServerSettingsService;
+ const config = yield* ServerConfig.ServerConfig;
+ const fs = yield* FileSystem.FileSystem;
+ const store = yield* ServerSecretStore.ServerSecretStore;
+ const instanceId = ProviderInstanceId.make("antigravity_secrets");
+ const saved = yield* service.updateSettings({
+ providerInstances: {
+ [instanceId]: {
+ driver: ProviderDriverKind.make("antigravity"),
+ config: { apiKey: "config-secret" },
+ environment: [
+ { name: "API_TOKEN", value: "env-secret", sensitive: true },
+ { name: "BASE_URL", value: "https://example.test", sensitive: false },
+ ],
+ },
+ },
+ sourceControlHosts: {
+ github: { tokens: { "github.com": "github-secret" } },
+ bitbucket: { email: "[email protected]", accessToken: "bitbucket-secret" },
+ gitcafe: { token: "gitcafe-secret" },
+ },
+ usageLimitSources: {
+ [UsageLimitSourceId.make("hub")]: {
+ kind: "cliproxy",
+ url: "https://hub.example.test",
+ managementKey: "quota-secret",
+ enabled: true,
+ },
+ },
+ });
+ const values = [
+ "config-secret",
+ "env-secret",
+ "github-secret",
+ "bitbucket-secret",
+ "gitcafe-secret",
+ "quota-secret",
+ ];
+ const client = ServerSettingsModule.redactServerSettingsForClient(saved);
+ const disk = yield* fs.readFileString(config.settingsPath);
+ for (const value of values) {
+ assert.include(JSON.stringify(saved), value);
+ assert.notInclude(JSON.stringify(client), value);
+ assert.notInclude(disk, value);
+ }
+ assert.include(JSON.stringify(client), "https://example.test");
+ assert.include(JSON.stringify(client), "[email protected]");
+ const echoed = yield* service.updateSettings({
+ providerInstances: client.providerInstances,
+ sourceControlHosts: client.sourceControlHosts,
+ usageLimitSources: client.usageLimitSources,
+ });
+ for (const value of values) assert.include(JSON.stringify(echoed), value);
+ const renamed = yield* service.updateProviderInstance({
+ operation: "upsert",
+ instanceId,
+ instance: { ...client.providerInstances[instanceId]!, displayName: "Renamed" },
+ });
+ assert.propertyVal(
+ renamed.providerInstances[instanceId]?.config,
+ "apiKey",
+ "config-secret",
+ );
+
+ for (const apiKey of ["replacement-secret", ""]) {
+ const changed = yield* service.updateProviderInstance({
+ operation: "upsert",
+ instanceId,
+ instance: { ...client.providerInstances[instanceId]!, config: { apiKey } },
+ });
+ assert.propertyVal(changed.providerInstances[instanceId]?.config, "apiKey", apiKey);
+ const redacted = ServerSettingsModule.redactServerSettingsForClient(changed);
+ assert.propertyVal(
+ redacted.providerInstances[instanceId]?.config,
+ "apiKey",
+ apiKey ? "••••••" : "",
+ );
+ }
+ // Restore the value so removing the instance below exercises deletion of both secrets.
+ yield* service.updateProviderInstance({
+ operation: "upsert",
+ instanceId,
+ instance: saved.providerInstances[instanceId]!,
+ });
+
+ const secretNames = [
+ "provider-config-YW50aWdyYXZpdHlfc2VjcmV0cw-YW50aWdyYXZpdHk-YXBpS2V5",
+ "provider-env-YW50aWdyYXZpdHlfc2VjcmV0cw-QVBJX1RPS0VO",
+ sourceControlHostSecretName({
+ kind: "github",
+ field: "tokens",
+ serverHost: "github.com",
+ }),
+ sourceControlHostSecretName({
+ kind: "bitbucket",
+ field: "accessToken",
+ serverHost: null,
+ }),
+ sourceControlHostSecretName({ kind: "gitcafe", field: "token", serverHost: null }),
+ "usage-limit-source-aHVi",
+ ];
+ for (const name of secretNames) {
+ assert.isTrue(Option.isSome(yield* store.get(name)), name);
+ }
+ yield* service.updateSettings({
+ providerInstances: {},
+ sourceControlHosts: {
+ github: { tokens: {} },
+ bitbucket: { accessToken: "" },
+ gitcafe: { token: "" },
+ },
+ usageLimitSources: { [UsageLimitSourceId.make("hub")]: null },
+ });
+ for (const name of secretNames) {
+ assert.isTrue(Option.isNone(yield* store.get(name)));
+ }
+ }).pipe(Effect.provide(layerServerSettingsWithSecrets())),
+ );
+
+ it.effect("moves a saved Antigravity key to the secret store when settings load", () =>
+ Effect.gen(function* () {
+ const service = yield* ServerSettingsModule.ServerSettingsService;
+ const config = yield* ServerConfig.ServerConfig;
+ const fs = yield* FileSystem.FileSystem;
+ yield* fs.writeFileString(
+ config.settingsPath,
+ JSON.stringify({
+ providerInstances: {
+ antigravity: { driver: "antigravity", config: { apiKey: "legacy-api-key" } },
+ },
+ }),
+ );
+ const saved = yield* service.getSettings;
+ assert.propertyVal(
+ saved.providerInstances[ProviderInstanceId.make("antigravity")]?.config,
+ "apiKey",
+ "legacy-api-key",
+ );
+ assert.notInclude(yield* fs.readFileString(config.settingsPath), "legacy-api-key");
+ assert.notInclude(
+ JSON.stringify(ServerSettingsModule.redactServerSettingsForClient(saved)),
+ "legacy-api-key",
+ );
+ }).pipe(Effect.provide(layerServerSettings())),
+ );
+
it.effect("stores sensitive provider instance environment values outside settings.json", () =>
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
@@ -1714,7 +1865,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
? Effect.fail(writeFailure)
: fileSystem.rename(fromPath, toPath),
});
- const instanceId = ProviderInstanceId.make("codex_write_failure");
+ const instanceId = ProviderInstanceId.make("antigravity_write_failure");
const layerSettings = layerServerSettings().pipe(
Layer.provideMerge(Layer.succeed(FileSystem.FileSystem, failingFileSystem)),
);
@@ -1726,9 +1877,9 @@ it.layer(NodeServices.layer)("server settings", (it) => {
operation: "upsert",
instanceId,
instance: {
- driver: ProviderDriverKind.make("codex"),
+ driver: ProviderDriverKind.make("antigravity"),
environment: [{ name: "OPENROUTER_API_KEY", value: "sk-kept", sensitive: true }],
- config: {},
+ config: { apiKey: "config-kept" },
},
});
@@ -1738,9 +1889,9 @@ it.layer(NodeServices.layer)("server settings", (it) => {
operation: "upsert",
instanceId,
instance: {
- driver: ProviderDriverKind.make("codex"),
+ driver: ProviderDriverKind.make("antigravity"),
environment: [{ name: "OPENROUTER_API_KEY", value: "sk-new", sensitive: true }],
- config: {},
+ config: { apiKey: "config-new" },
},
})
.pipe(Effect.result);
@@ -1750,6 +1901,11 @@ it.layer(NodeServices.layer)("server settings", (it) => {
?.value,
"sk-kept",
);
+ assert.propertyVal(
+ (yield* serverSettings.getSettings).providerInstances[instanceId]?.config,
+ "apiKey",
+ "config-kept",
+ );
const failed = yield* serverSettings
.updateProviderInstance({ operation: "remove", instanceId })
@@ -1760,6 +1916,11 @@ it.layer(NodeServices.layer)("server settings", (it) => {
?.value,
"sk-kept",
);
+ assert.propertyVal(
+ (yield* serverSettings.getSettings).providerInstances[instanceId]?.config,
+ "apiKey",
+ "config-kept",
+ );
}).pipe(Effect.provide(layerSettings));
}),
);
@@ -1841,9 +2002,9 @@ it.layer(NodeServices.layer)("server settings", (it) => {
yield* serverSettings.updateSettings({
providerInstances: {
[instanceId]: {
- driver: ProviderDriverKind.make("codex"),
+ driver: ProviderDriverKind.make("antigravity"),
environment: [{ name: "OPENROUTER_API_KEY", value: "sk-kept", sensitive: true }],
- config: {},
+ config: { apiKey: "config-kept" },
},
},
});
@@ -1853,9 +2014,9 @@ it.layer(NodeServices.layer)("server settings", (it) => {
.updateSettings({
providerInstances: {
[instanceId]: {
- driver: ProviderDriverKind.make("codex"),
+ driver: ProviderDriverKind.make("antigravity"),
environment: [{ name: "OPENROUTER_API_KEY", value: "sk-new", sensitive: true }],
- config: {},
+ config: { apiKey: "config-new" },
},
},
})
@@ -1868,6 +2029,11 @@ it.layer(NodeServices.layer)("server settings", (it) => {
?.value,
"sk-kept",
);
+ assert.propertyVal(
+ (yield* serverSettings.getSettings).providerInstances[instanceId]?.config,
+ "apiKey",
+ "config-kept",
+ );
}).pipe(Effect.provide(layerSettings));
},
);
diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts
index 18f3ada048e..44e645858c4 100644
--- a/apps/server/src/serverSettings.ts
+++ b/apps/server/src/serverSettings.ts
@@ -17,8 +17,6 @@ import {
ProjectScript,
ProjectSettingsOverrides,
type ProviderInstanceConfig,
- type ProviderInstanceEnvironmentVariable,
- type UsageLimitSourceConfig,
type ProviderInstanceMutation,
defaultInstanceIdForDriver,
isProviderDriverKind,
@@ -64,12 +62,11 @@ import {
import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
import {
readSecretFieldValues,
- SOURCE_CONTROL_HOST_SECRET_FIELDS,
- type SourceControlHostSecretSlot,
sourceControlHostSecretName,
- writeSecretFieldValues,
} from "./sourceControl/sourceControlHostSecrets.ts";
+import { mapSettingsSecrets, SECRET_REDACTED, type SettingsSecret } from "./settingsSecrets.ts";
+
export {
resolveReviewGuideModelSelection,
resolveSourceControlWriterModelSelection,
@@ -147,106 +144,12 @@ const normalizeServerSettings = (
),
);
-function providerEnvironmentSecretName(input: {
- readonly instanceId: string;
- readonly name: string;
-}): string {
- return `provider-env-${Buffer.from(input.instanceId, "utf8").toString("base64url")}-${Buffer.from(input.name, "utf8").toString("base64url")}`;
-}
-
-/**
- * On disk a hub key or Bitbucket token is replaced by this marker and the
- * real value lives in the secret store, mirroring provider environment
- * secrets. A client that sends the marker back means "keep what you have".
- */
-const SECRET_REDACTED = "\u2022\u2022\u2022\u2022\u2022\u2022";
-
-function usageLimitSourceSecretName(sourceId: string): string {
- return `usage-limit-source-${Buffer.from(sourceId, "utf8").toString("base64url")}`;
-}
-
-const redactSecret = (value: string) => (value.length > 0 ? SECRET_REDACTED : "");
-
-/** Each host's settings with every secret slot's value replaced by `map(slot, value)`. */
-function mapSlotValues(
- hosts: ServerSettings["sourceControlHosts"],
- map: (slot: SourceControlHostSecretSlot, value: string) => string,
-): ServerSettings["sourceControlHosts"] {
- return Object.fromEntries(
- Object.entries(hosts).map(([kind, fields]) => {
- const secretFields = SOURCE_CONTROL_HOST_SECRET_FIELDS.get(kind) ?? [];
- if (secretFields.length === 0) return [kind, fields];
- const next = { ...fields };
- for (const field of secretFields) {
- if (!(field in next)) continue;
- next[field] = writeSecretFieldValues(
- next[field],
- readSecretFieldValues(next[field]).map(
- ([serverHost, value]) => [serverHost, map({ kind, field, serverHost }, value)] as const,
- ),
- );
- }
- return [kind, next];
- }),
- );
-}
-
-/** Every saved secret slot in `hosts`, with its current value. */
-function sourceControlHostSecretSlots(
- hosts: ServerSettings["sourceControlHosts"],
-): ReadonlyArray<SourceControlHostSecretSlot & { readonly value: string }> {
- return Object.entries(hosts).flatMap(([kind, fields]) =>
- (SOURCE_CONTROL_HOST_SECRET_FIELDS.get(kind) ?? []).flatMap((field) =>
- readSecretFieldValues(fields[field]).map(([serverHost, value]) => ({
- kind,
- field,
- serverHost,
- value,
- })),
- ),
- );
-}
-
-function redactProviderEnvironmentVariable(
- variable: ProviderInstanceEnvironmentVariable,
-): ProviderInstanceEnvironmentVariable {
- if (!variable.sensitive) {
- const { valueRedacted: _omit, ...rest } = variable;
- return rest;
- }
- return {
- ...variable,
- value: "",
- ...(variable.value.length > 0 || variable.valueRedacted ? { valueRedacted: true } : {}),
- };
-}
-
export function redactServerSettingsForClient(settings: ServerSettings): ServerSettings {
- const providerInstances = Object.fromEntries(
- Object.entries(settings.providerInstances).map(([instanceId, instance]) => [
- instanceId,
- instance.environment
- ? {
- ...instance,
- environment: instance.environment.map(redactProviderEnvironmentVariable),
- }
- : instance,
- ]),
- );
- // The hub key is a bearer secret; clients only need to know one is set.
- const usageLimitSources = Object.fromEntries(
- Object.entries(settings.usageLimitSources).map(([id, source]) => [
- id,
- {
- ...source,
- managementKey: redactSecret(source.managementKey),
- },
- ]),
- );
- const sourceControlHosts = mapSlotValues(settings.sourceControlHosts, (_slot, value) =>
- redactSecret(value),
+ return mapSettingsSecrets(settings, (secret) =>
+ secret.sensitive
+ ? { value: "", redacted: secret.redacted || secret.value.length > 0 }
+ : { value: secret.value, redacted: false },
);
- return { ...settings, providerInstances, usageLimitSources, sourceControlHosts };
}
export function applyProviderInstanceMutation(
@@ -781,35 +684,36 @@ const make = Effect.gen(function* () {
);
/**
- * Moves source control host secrets hand-edited into settings.json into the secret store as
- * they load, so plaintext does not stay on disk. If the store is unavailable, the secret keeps
- * working from the file and the move is retried on the next load.
+ * Migrate plaintext schema-declared credentials on load. Failed moves stay usable from
+ * the file and are retried next time; client redaction does not depend on migration.
*/
- const moveInlineSourceControlHostSecrets = (settings: ServerSettings) =>
+ const moveInlineSettingsSecrets = (settings: ServerSettings) =>
Effect.gen(function* () {
+ const pending: SettingsSecret[] = [];
+ mapSettingsSecrets(settings, (secret) => {
+ if (secret.kind === "field" && !secret.redacted && secret.value.length > 0) {
+ pending.push(secret);
+ }
+ return secret;
+ });
const moved = new Set<string>();
- for (const slot of sourceControlHostSecretSlots(settings.sourceControlHosts)) {
- if (slot.value.length === 0 || slot.value === SECRET_REDACTED) continue;
+ for (const secret of pending) {
const stored = yield* secretStore
- .set(sourceControlHostSecretName(slot), textEncoder.encode(slot.value))
+ .set(secret.secretName, textEncoder.encode(secret.value))
.pipe(
Effect.as(true),
Effect.catch(() =>
- Effect.logWarning("failed to move a source control secret into the secret store", {
- kind: slot.kind,
- field: slot.field,
- }).pipe(Effect.as(false)),
+ Effect.logWarning("failed to move a settings secret into the secret store").pipe(
+ Effect.as(false),
+ ),
),
);
- if (stored) moved.add(sourceControlHostSecretName(slot));
+ if (stored) moved.add(secret.secretName);
}
if (moved.size === 0) return settings;
- return {
- ...settings,
- sourceControlHosts: mapSlotValues(settings.sourceControlHosts, (slot, value) =>
- moved.has(sourceControlHostSecretName(slot)) ? SECRET_REDACTED : value,
- ),
- };
+ return mapSettingsSecrets(settings, (secret) =>
+ moved.has(secret.secretName) ? { value: "", redacted: true } : secret,
+ );
});
const loadSettingsFromDisk = Effect.gen(function* () {
@@ -934,7 +838,7 @@ const make = Effect.gen(function* () {
: { settings: folded, secretMoves: [] };
// Only rewrite a file that decoded cleanly; an untrusted one stays for the user to repair.
const migrated = settingsFileTrusted
- ? yield* moveInlineSourceControlHostSecrets(sourceControlMigration.settings)
+ ? yield* moveInlineSettingsSecrets(sourceControlMigration.settings)
: folded;
// A file still carrying the retired `providers`, `bitbucket`, or `github` keys is rewritten
// once so the migrated values persist and the old keys disappear.
@@ -962,96 +866,49 @@ const make = Effect.gen(function* () {
const getSettingsFromCache = Cache.get(settingsCache, cacheKey);
- const materializeProviderEnvironmentSecrets = (
+ const materializeSettingsSecrets = (
settings: ServerSettings,
): Effect.Effect<ServerSettings, ServerSettingsError> =>
Effect.gen(function* () {
- const providerInstances: Record<string, ProviderInstanceConfig> = {
- ...settings.providerInstances,
- };
- for (const [instanceId, instance] of Object.entries(settings.providerInstances)) {
- if (!instance.environment) continue;
- const environment: ProviderInstanceEnvironmentVariable[] = [];
- for (const variable of instance.environment) {
- if (!variable.sensitive || !variable.valueRedacted) {
- environment.push(variable);
- continue;
- }
- const secret = yield* secretStore
- .get(providerEnvironmentSecretName({ instanceId, name: variable.name }))
- .pipe(
- Effect.mapError(
- (cause) =>
- new ServerSettingsError({
- settingsPath,
- operation: "read-secret",
- providerInstanceId: instanceId,
- environmentVariable: variable.name,
- cause,
- }),
- ),
- );
- environment.push({
- ...variable,
- value: Option.isSome(secret) ? textDecoder.decode(secret.value) : "",
- });
- }
- providerInstances[instanceId] = {
- ...instance,
- environment,
- } satisfies ProviderInstanceConfig;
- }
- const usageLimitSources: Record<string, UsageLimitSourceConfig> = {};
- for (const [sourceId, source] of Object.entries(settings.usageLimitSources)) {
- if (source.managementKey !== SECRET_REDACTED) {
- usageLimitSources[sourceId] = source;
- continue;
- }
- const secret = yield* secretStore
- .get(usageLimitSourceSecretName(sourceId))
- .pipe(
- Effect.mapError(
- (cause) => new ServerSettingsError({ settingsPath, operation: "read-secret", cause }),
- ),
- );
- usageLimitSources[sourceId] = {
- ...source,
- managementKey: Option.isSome(secret) ? textDecoder.decode(secret.value) : "",
- };
- }
const storedSecrets = new Map<string, string>();
- for (const slot of sourceControlHostSecretSlots(settings.sourceControlHosts)) {
- if (slot.value !== SECRET_REDACTED) continue;
- const secret = yield* secretStore
- .get(sourceControlHostSecretName(slot))
- .pipe(
- Effect.mapError(
- (cause) => new ServerSettingsError({ settingsPath, operation: "read-secret", cause }),
- ),
- );
+ const pending = new Map<string, SettingsSecret>();
+ mapSettingsSecrets(settings, (secret) => {
+ if (secret.sensitive && secret.redacted) pending.set(secret.secretName, secret);
+ return secret;
+ });
+ for (const secret of pending.values()) {
+ const value = yield* secretStore.get(secret.secretName).pipe(
+ Effect.mapError(
+ (cause) =>
+ new ServerSettingsError({
+ settingsPath,
+ operation: "read-secret",
+ providerInstanceId: secret.providerInstanceId,
+ environmentVariable: secret.environmentVariable,
+ cause,
+ }),
+ ),
+ );
storedSecrets.set(
- sourceControlHostSecretName(slot),
- Option.isSome(secret) ? textDecoder.decode(secret.value) : "",
+ secret.secretName,
+ Option.isSome(value) ? textDecoder.decode(value.value) : "",
);
}
- return {
- ...settings,
- providerInstances: providerInstances as ServerSettings["providerInstances"],
- usageLimitSources: usageLimitSources as ServerSettings["usageLimitSources"],
- sourceControlHosts: mapSlotValues(settings.sourceControlHosts, (slot, value) =>
- value === SECRET_REDACTED
- ? (storedSecrets.get(sourceControlHostSecretName(slot)) ?? "")
- : value,
- ),
- };
+ return mapSettingsSecrets(settings, (secret) => ({
+ value:
+ secret.sensitive && secret.redacted
+ ? (storedSecrets.get(secret.secretName) ?? "")
+ : secret.value,
+ redacted: secret.sensitive && secret.kind === "environment" && secret.redacted,
+ }));
});
const materializeChanges = (changes: Stream.Stream<ServerSettings>) =>
changes.pipe(
Stream.mapEffect((settings) =>
- materializeProviderEnvironmentSecrets(settings).pipe(
+ materializeSettingsSecrets(settings).pipe(
Effect.catch((error: ServerSettingsError) =>
- Effect.logWarning("failed to materialize provider environment secrets", {
+ Effect.logWarning("failed to materialize settings secrets", {
operation: error.operation,
providerInstanceId: error.providerInstanceId,
environmentVariable: error.environmentVariable,
@@ -1072,162 +929,65 @@ const make = Effect.gen(function* () {
| { readonly kind: "remove"; readonly operation: "remove-secret" | "remove-stale-secret" }
);
- const persistProviderEnvironmentSecrets = (current: ServerSettings, next: ServerSettings) =>
+ const persistSettingsSecrets = (current: ServerSettings, next: ServerSettings) =>
Effect.sync(() => {
- const providerInstances: Record<string, ProviderInstanceConfig> = {
- ...next.providerInstances,
- };
+ const previous = new Map<string, SettingsSecret>();
+ mapSettingsSecrets(current, (secret) => {
+ // Duplicate environment names follow the provider's last-value-wins behavior.
+ previous.set(secret.secretName, secret);
+ return secret;
+ });
const changes: SecretChange[] = [];
-
- const nextSecretKeys = new Set<string>();
- for (const [instanceId, instance] of Object.entries(next.providerInstances)) {
- if (!instance.environment) continue;
- const environment: ProviderInstanceEnvironmentVariable[] = [];
- for (const variable of instance.environment) {
- const secretName = providerEnvironmentSecretName({ instanceId, name: variable.name });
- if (!variable.sensitive) {
- changes.push({
- kind: "remove",
- secretName,
- operation: "remove-secret",
- providerInstanceId: instanceId,
- environmentVariable: variable.name,
- });
- environment.push(redactProviderEnvironmentVariable(variable));
- continue;
- }
-
- nextSecretKeys.add(secretName);
- // Match the provider environment's last-value-wins behavior for duplicate names.
- const previous = variable.valueRedacted
- ? current.providerInstances[ProviderInstanceId.make(instanceId)]?.environment?.findLast(
- (entry) => entry.name === variable.name,
- )
- : undefined;
- const inlineValue =
- previous?.sensitive && !previous.valueRedacted && previous.value.length > 0
- ? previous.value
- : undefined;
- const value = inlineValue ?? variable.value;
- if (!variable.valueRedacted || inlineValue !== undefined) {
- if (value.length > 0) {
- changes.push({
- kind: "write",
- secretName,
- value: textEncoder.encode(value),
- providerInstanceId: instanceId,
- environmentVariable: variable.name,
- });
- environment.push({ ...variable, value: "", valueRedacted: true });
- } else {
- changes.push({
- kind: "remove",
- secretName,
- operation: "remove-secret",
- providerInstanceId: instanceId,
- environmentVariable: variable.name,
- });
- const { valueRedacted: _omit, ...rest } = variable;
- environment.push(rest);
- }
- continue;
- }
-
- environment.push(redactProviderEnvironmentVariable(variable));
- }
- providerInstances[instanceId] = {
- ...instance,
- environment,
- } satisfies ProviderInstanceConfig;
- }
-
- for (const [instanceId, instance] of Object.entries(current.providerInstances)) {
- for (const variable of instance.environment ?? []) {
- if (!variable.sensitive) continue;
- const secretName = providerEnvironmentSecretName({ instanceId, name: variable.name });
- if (nextSecretKeys.has(secretName)) continue;
- changes.push({
- kind: "remove",
- secretName,
- operation: "remove-stale-secret",
- providerInstanceId: instanceId,
- environmentVariable: variable.name,
- });
+ const nextSecretNames = new Set<string>();
+ const settings = mapSettingsSecrets(next, (secret) => {
+ const metadata = {
+ secretName: secret.secretName,
+ ...(secret.providerInstanceId !== undefined
+ ? { providerInstanceId: secret.providerInstanceId }
+ : {}),
+ ...(secret.environmentVariable !== undefined
+ ? { environmentVariable: secret.environmentVariable }
+ : {}),
+ };
+ if (!secret.sensitive) {
+ changes.push({ ...metadata, kind: "remove", operation: "remove-secret" });
+ return { value: secret.value, redacted: false };
}
- }
-
- const usageLimitSources: Record<string, UsageLimitSourceConfig> = {};
- for (const [sourceId, source] of Object.entries(next.usageLimitSources)) {
- const secretName = usageLimitSourceSecretName(sourceId);
- if (source.managementKey === SECRET_REDACTED) {
- usageLimitSources[sourceId] = source;
- continue;
+ const old = previous.get(secret.secretName);
+ const inlineValue =
+ old?.sensitive && !old.redacted && old.value.length > 0 ? old.value : undefined;
+ const value = secret.redacted ? inlineValue : secret.value;
+ if (value === undefined) {
+ nextSecretNames.add(secret.secretName);
+ return { value: "", redacted: true };
}
- if (source.managementKey.length === 0) {
- changes.push({ kind: "remove", secretName, operation: "remove-secret" });
- usageLimitSources[sourceId] = source;
- continue;
+ if (value.length === 0) {
+ changes.push({ ...metadata, kind: "remove", operation: "remove-secret" });
+ return { value: "", redacted: false };
}
- changes.push({
- kind: "write",
- secretName,
- value: textEncoder.encode(source.managementKey),
- });
- usageLimitSources[sourceId] = { ...source, managementKey: SECRET_REDACTED };
- }
- for (const sourceId of Object.keys(current.usageLimitSources)) {
- if (sourceId in next.usageLimitSources) continue;
+ nextSecretNames.add(secret.secretName);
+ changes.push({ ...metadata, kind: "write", value: textEncoder.encode(value) });
+ return { value: "", redacted: true };
+ });
+ for (const secret of previous.values()) {
+ if (!secret.sensitive || nextSecretNames.has(secret.secretName)) continue;
+ if (changes.some((change) => change.secretName === secret.secretName)) continue;
changes.push({
kind: "remove",
- secretName: usageLimitSourceSecretName(sourceId),
+ secretName: secret.secretName,
operation: "remove-stale-secret",
+ ...(secret.providerInstanceId !== undefined
+ ? { providerInstanceId: secret.providerInstanceId }
+ : {}),
+ ...(secret.environmentVariable !== undefined
+ ? { environmentVariable: secret.environmentVariable }
+ : {}),
});
}
-
- // Hand-edited plaintext not yet in the store, so the redaction marker can move it there.
- const inlineSecrets = new Map(
- sourceControlHostSecretSlots(current.sourceControlHosts).flatMap((slot) =>
- slot.value.length > 0 && slot.value !== SECRET_REDACTED
- ? [[sourceControlHostSecretName(slot), slot.value] as const]
- : [],
- ),
- );
- const nextSecretNames = new Set<string>();
- const sourceControlHosts = mapSlotValues(next.sourceControlHosts, (slot, raw) => {
- const secretName = sourceControlHostSecretName(slot);
- // The marker keeps what is saved; a hand-edited plaintext value moves into the store.
- const value = raw === SECRET_REDACTED ? inlineSecrets.get(secretName) : raw;
- if (value === undefined) {
- nextSecretNames.add(secretName);
- return SECRET_REDACTED;
- }
- if (value.length === 0) {
- changes.push({ kind: "remove", secretName, operation: "remove-secret" });
- return "";
- }
- nextSecretNames.add(secretName);
- changes.push({ kind: "write", secretName, value: textEncoder.encode(value) });
- return SECRET_REDACTED;
- });
- for (const slot of sourceControlHostSecretSlots(current.sourceControlHosts)) {
- const secretName = sourceControlHostSecretName(slot);
- if (nextSecretNames.has(secretName)) continue;
- if (changes.some((change) => change.secretName === secretName)) continue;
- changes.push({ kind: "remove", secretName, operation: "remove-stale-secret" });
- }
-
- return {
- settings: {
- ...next,
- providerInstances: providerInstances as ServerSettings["providerInstances"],
- usageLimitSources: usageLimitSources as ServerSettings["usageLimitSources"],
- sourceControlHosts,
- },
- changes,
- };
+ return { settings, changes };
});
- const rollbackProviderEnvironmentSecretWrites = (
+ const rollbackSettingsSecretWrites = (
writes: ReadonlyArray<{
readonly secretName: string;
readonly previousValue: Option.Option<Uint8Array>;
@@ -1243,7 +1003,7 @@ const make = Effect.gen(function* () {
: secretStore.remove(write.secretName)
).pipe(
Effect.catch((cause) =>
- Effect.logWarning("failed to roll back provider environment secret", {
+ Effect.logWarning("failed to roll back settings secret", {
providerInstanceId: write.providerInstanceId,
environmentVariable: write.environmentVariable,
cause,
@@ -1253,14 +1013,14 @@ const make = Effect.gen(function* () {
{ discard: true },
);
- const applyProviderEnvironmentSecretChanges = (changes: ReadonlyArray<SecretChange>) => {
+ const applySettingsSecretChanges = (changes: ReadonlyArray<SecretChange>) => {
const applied: Array<{
readonly secretName: string;
readonly previousValue: Option.Option<Uint8Array>;
readonly providerInstanceId?: string;
readonly environmentVariable?: string;
}> = [];
- const rollback = Effect.suspend(() => rollbackProviderEnvironmentSecretWrites(applied));
+ const rollback = Effect.suspend(() => rollbackSettingsSecretWrites(applied));
return Effect.forEach(
changes,
(change) =>
@@ -1310,16 +1070,12 @@ const make = Effect.gen(function* () {
Effect.gen(function* () {
const current = yield* getSettingsFromCache;
const updated = yield* update(current);
- const persisted = yield* persistProviderEnvironmentSecrets(current, updated);
+ const persisted = yield* persistSettingsSecrets(current, updated);
const next = yield* normalizeServerSettings(persisted.settings);
const materialized = yield* Effect.uninterruptibleMask(() =>
Effect.gen(function* () {
- const rollbackSecretChanges = yield* applyProviderEnvironmentSecretChanges(
- persisted.changes,
- );
- const materializedExit = yield* Effect.exit(
- materializeProviderEnvironmentSecrets(next),
- );
+ const rollbackSecretChanges = yield* applySettingsSecretChanges(persisted.changes);
+ const materializedExit = yield* Effect.exit(materializeSettingsSecrets(next));
if (Exit.isFailure(materializedExit)) {
yield* rollbackSecretChanges;
return yield* Effect.failCause(materializedExit.cause);
@@ -1341,7 +1097,7 @@ const make = Effect.gen(function* () {
const withSettingsSnapshot: ServerSettingsService["Service"]["withSettingsSnapshot"] = (use) =>
writeSemaphore.withPermits(1)(
getSettingsFromCache.pipe(
- Effect.flatMap(materializeProviderEnvironmentSecrets),
+ Effect.flatMap(materializeSettingsSecrets),
Effect.map(resolveTextGenerationProvider),
Effect.flatMap(use),
),
@@ -1457,7 +1213,7 @@ const make = Effect.gen(function* () {
start,
ready: Deferred.await(startedDeferred),
getSettings: getSettingsFromCache.pipe(
- Effect.flatMap(materializeProviderEnvironmentSecrets),
+ Effect.flatMap(materializeSettingsSecrets),
Effect.map(resolveTextGenerationProvider),
),
updateSettings: (patch) =>
diff --git a/apps/server/src/settingsSecrets.ts b/apps/server/src/settingsSecrets.ts
new file mode 100644
index 00000000000..42adf7977eb
--- /dev/null
+++ b/apps/server/src/settingsSecrets.ts
@@ -0,0 +1,150 @@
+import {
+ AntigravitySettings,
+ ClaudeSettings,
+ CodexSettings,
+ type ServerSettings,
+ UsageLimitSourceConfig,
+ secretSettingsFields,
+} from "@t3tools/contracts";
+import { acpRegistryClient } from "@t3tools/provider-acp-registry/client";
+import { cursorClient } from "@t3tools/provider-cursor/client";
+import { grokClient } from "@t3tools/provider-grok/client";
+import { museClient } from "@t3tools/provider-muse/client";
+import { openCodeClient } from "@t3tools/provider-opencode/client";
+import { piClient } from "@t3tools/provider-pi/client";
+import {
+ readSecretFieldValues,
+ SOURCE_CONTROL_HOST_SECRET_FIELDS,
+ sourceControlHostSecretName,
+ writeSecretFieldValues,
+} from "./sourceControl/sourceControlHostSecrets.ts";
+
+export const SECRET_REDACTED = "\u2022\u2022\u2022\u2022\u2022\u2022";
+
+const providerSecretFields = new Map([
+ ["codex", secretSettingsFields(CodexSettings)],
+ ["claudeAgent", secretSettingsFields(ClaudeSettings)],
+ ["antigravity", secretSettingsFields(AntigravitySettings)],
+ ...[cursorClient, grokClient, museClient, openCodeClient, piClient, acpRegistryClient].map(
+ (definition) =>
+ [definition.driverKind, secretSettingsFields(definition.settingsSchema)] as const,
+ ),
+]);
+const usageLimitSecretFields = secretSettingsFields(UsageLimitSourceConfig);
+const encode = (value: string) => Buffer.from(value, "utf8").toString("base64url");
+
+export interface SettingsSecret {
+ readonly secretName: string;
+ readonly kind: "field" | "environment";
+ readonly value: string;
+ readonly redacted: boolean;
+ readonly sensitive: boolean;
+ readonly providerInstanceId?: string;
+ readonly environmentVariable?: string;
+}
+
+type SecretValue = Pick<SettingsSecret, "value" | "redacted">;
+
+/**
+ * Visits credential fields declared by schemas and dynamically sensitive environment variables.
+ * Store keys and client representations stay here; callers supply only the secret operation.
+ */
+export function mapSettingsSecrets(
+ settings: ServerSettings,
+ map: (secret: SettingsSecret) => SecretValue,
+): ServerSettings {
+ const mapFields = (
+ fields: Readonly<Record<string, unknown>>,
+ secretFields: ReadonlyArray<string>,
+ name: (field: string, serverHost: string | null) => string,
+ ) => {
+ const next = { ...fields };
+ for (const field of secretFields) {
+ if (!(field in next)) continue;
+ next[field] = writeSecretFieldValues(
+ next[field],
+ readSecretFieldValues(next[field]).map(([serverHost, value]) => {
+ const result = map({
+ secretName: name(field, serverHost),
+ kind: "field",
+ value,
+ redacted: value === SECRET_REDACTED,
+ sensitive: true,
+ });
+ return [serverHost, result.redacted ? SECRET_REDACTED : result.value] as const;
+ }),
+ );
+ }
+ return next;
+ };
+
+ const providerInstances = Object.fromEntries(
+ Object.entries(settings.providerInstances).map(([instanceId, instance]) => {
+ const config = instance.config;
+ return [
+ instanceId,
+ {
+ ...instance,
+ ...(config !== null && typeof config === "object" && !Array.isArray(config)
+ ? {
+ config: mapFields(
+ config as Record<string, unknown>,
+ providerSecretFields.get(instance.driver) ?? [],
+ (field, serverHost) =>
+ `provider-config-${encode(instanceId)}-${encode(instance.driver)}-${encode(field)}` +
+ (serverHost === null ? "" : `-${encode(serverHost)}`),
+ ),
+ }
+ : {}),
+ ...(instance.environment
+ ? {
+ environment: instance.environment.map((variable) => {
+ const result = map({
+ secretName: `provider-env-${encode(instanceId)}-${encode(variable.name)}`,
+ kind: "environment",
+ value: variable.value,
+ redacted: variable.valueRedacted === true,
+ sensitive: variable.sensitive,
+ providerInstanceId: instanceId,
+ environmentVariable: variable.name,
+ });
+ const { valueRedacted: _omit, ...rest } = variable;
+ return {
+ ...rest,
+ value: result.value,
+ ...(result.redacted ? { valueRedacted: true } : {}),
+ };
+ }),
+ }
+ : {}),
+ },
+ ];
+ }),
+ );
+
+ const sourceControlHosts = Object.fromEntries(
+ Object.entries(settings.sourceControlHosts).map(([kind, fields]) => [
+ kind,
+ mapFields(fields, SOURCE_CONTROL_HOST_SECRET_FIELDS.get(kind) ?? [], (field, serverHost) =>
+ sourceControlHostSecretName({ kind, field, serverHost }),
+ ),
+ ]),
+ ) as ServerSettings["sourceControlHosts"];
+
+ const usageLimitSources = Object.fromEntries(
+ Object.entries(settings.usageLimitSources).map(([id, source]) => [
+ id,
+ mapFields(
+ source,
+ usageLimitSecretFields,
+ (field, serverHost) =>
+ `usage-limit-source-${encode(id)}` +
+ (field === "managementKey" && serverHost === null
+ ? ""
+ : `-${encode(field)}${serverHost === null ? "" : `-${encode(serverHost)}`}`),
+ ),
+ ]),
+ ) as ServerSettings["usageLimitSources"];
+
+ return { ...settings, providerInstances, sourceControlHosts, usageLimitSources };
+}
diff --git a/apps/server/src/sourceControl/sourceControlHostSecrets.ts b/apps/server/src/sourceControl/sourceControlHostSecrets.ts
index 7790c1dbc03..8c566318f13 100644
--- a/apps/server/src/sourceControl/sourceControlHostSecrets.ts
+++ b/apps/server/src/sourceControl/sourceControlHostSecrets.ts
@@ -7,12 +7,10 @@
*
* @module sourceControl/sourceControlHostSecrets
*/
+import { secretSettingsFields } from "@t3tools/contracts";
import * as AzureDevOps from "@t3tools/source-control-azure-devops/client/definition";
import * as Bitbucket from "@t3tools/source-control-bitbucket/client/definition";
-import {
- secretSourceControlHostSettingsFields,
- type SourceControlClientDefinition,
-} from "@t3tools/source-control-core/client/definition";
+import type { SourceControlClientDefinition } from "@t3tools/source-control-core/client/definition";
import * as Forgejo from "@t3tools/source-control-forgejo/client/definition";
import * as GitCafe from "@t3tools/source-control-gitcafe/client/definition";
import * as GitHub from "@t3tools/source-control-github/client/definition";
@@ -33,9 +31,7 @@ export const SOURCE_CONTROL_HOST_SECRET_FIELDS: ReadonlyMap<
ReadonlyArray<string>
> = new Map(
definitions.flatMap((definition) => {
- const fields = definition.settings
- ? secretSourceControlHostSettingsFields(definition.settings)
- : [];
+ const fields = definition.settings ? secretSettingsFields(definition.settings) : [];
return fields.length > 0 ? [[definition.kind, fields] as const] : [];
}),
);
diff --git a/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx b/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx
index 80727a8e970..aacfcca9bbc 100644
--- a/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx
+++ b/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx
@@ -13,6 +13,7 @@ import {
DialogTitle,
} from "../ui/dialog";
import { Input } from "../ui/input";
+import { SecretInput } from "./SecretInput";
import { Label } from "../ui/label";
/**
@@ -118,10 +119,8 @@ export function AddUsageLimitSourceDialog({
</div>
<div className="grid gap-1.5">
<Label htmlFor="usage-source-key">Management key</Label>
- <Input
+ <SecretInput
id="usage-source-key"
- type="password"
- autoComplete="off"
value={managementKey}
onChange={(event) => setManagementKey(event.target.value)}
/>
diff --git a/apps/web/src/components/settings/GitHubTokenSettings.tsx b/apps/web/src/components/settings/GitHubTokenSettings.tsx
index 9516d448b73..a6f5c07b29e 100644
--- a/apps/web/src/components/settings/GitHubTokenSettings.tsx
+++ b/apps/web/src/components/settings/GitHubTokenSettings.tsx
@@ -8,6 +8,7 @@ import { serverEnvironment } from "../../state/server";
import { useAtomCommand } from "../../state/use-atom-command";
import { Button, InlineButton } from "../ui/button";
import { Input } from "../ui/input";
+import { SecretInput } from "./SecretInput";
import { Label } from "../ui/label";
const DEFAULT_HOST = "github.com";
@@ -103,12 +104,10 @@ export function GitHubTokenSettings({
</div>
<div className="grid gap-1.5">
<Label htmlFor={`github-token-${environmentId}`}>Token</Label>
- <Input
+ <SecretInput
id={`github-token-${environmentId}`}
- type="password"
- autoComplete="off"
size="sm"
- placeholder={isSaved ? "Stored secret, enter a new value to replace" : "Not set"}
+ configured={isSaved}
value={draft}
onChange={(event) => setDraft(event.target.value)}
/>
diff --git a/apps/web/src/components/settings/ProviderInstanceCard.tsx b/apps/web/src/components/settings/ProviderInstanceCard.tsx
index 4a95cdccd55..b9f2a4569b0 100644
--- a/apps/web/src/components/settings/ProviderInstanceCard.tsx
+++ b/apps/web/src/components/settings/ProviderInstanceCard.tsx
@@ -44,6 +44,7 @@ import { Badge } from "../ui/badge";
import { Button } from "../ui/button";
import { MorphIcon } from "~/components/MorphIcon";
import { DraftInput } from "../ui/draft-input";
+import { SecretInput } from "./SecretInput";
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
import { Switch } from "../ui/switch";
import { stackedThreadToast, toastManager } from "../ui/toast";
@@ -263,9 +264,6 @@ function ProviderEnvironmentFieldRow(props: {
}) {
const inputId = `${props.idPrefix}-environment-${props.field.name}`;
const value = props.variable?.valueRedacted ? "" : (props.variable?.value ?? "");
- const placeholder = props.variable?.valueRedacted
- ? "Stored secret - enter a new value to replace"
- : props.field.placeholder;
return (
<SettingsRow
@@ -273,17 +271,29 @@ function ProviderEnvironmentFieldRow(props: {
description={props.field.description}
control={
<div className="flex w-full min-w-0 items-center gap-2 @min-[32rem]/settings-row:w-56">
- <DraftInput
- id={inputId}
- size="sm"
- className="min-w-0 flex-1"
- type={props.field.sensitive === false ? undefined : "password"}
- autoComplete="off"
- value={value}
- onCommit={(next) => props.onCommit(props.field, next)}
- placeholder={placeholder}
- spellCheck={false}
- />
+ {props.field.sensitive === false ? (
+ <DraftInput
+ id={inputId}
+ size="sm"
+ className="min-w-0 flex-1"
+ autoComplete="off"
+ value={value}
+ onCommit={(next) => props.onCommit(props.field, next)}
+ placeholder={props.field.placeholder}
+ spellCheck={false}
+ />
+ ) : (
+ <SecretInput
+ id={inputId}
+ size="sm"
+ className="min-w-0 flex-1"
+ value={value}
+ onCommit={(next) => props.onCommit(props.field, next)}
+ placeholder={props.field.placeholder}
+ configured={props.variable?.valueRedacted === true}
+ spellCheck={false}
+ />
+ )}
{props.variable ? (
<Button
type="button"
@@ -410,20 +420,31 @@ export function ProviderEnvironmentSection(props: {
<span className="hidden text-xs text-muted-foreground sm:inline" aria-hidden>
=
</span>
- <DraftInput
- size="sm"
- font="mono"
- className="min-w-0 flex-1"
- value={variable.valueRedacted ? "" : variable.value}
- onCommit={(value) => updateVariable(variable.id, { value })}
- type={variable.sensitive ? "password" : undefined}
- autoComplete="off"
- placeholder={
- variable.valueRedacted ? "Stored secret, enter a new value to replace" : "value"
- }
- spellCheck={false}
- aria-label={`Environment variable value ${index + 1}`}
- />
+ {variable.sensitive ? (
+ <SecretInput
+ size="sm"
+ font="mono"
+ className="min-w-0 flex-1"
+ value={variable.valueRedacted ? "" : variable.value}
+ onCommit={(value) => updateVariable(variable.id, { value })}
+ placeholder="value"
+ configured={variable.valueRedacted === true}
+ spellCheck={false}
+ aria-label={`Environment variable value ${index + 1}`}
+ />
+ ) : (
+ <DraftInput
+ size="sm"
+ font="mono"
+ className="min-w-0 flex-1"
+ value={variable.valueRedacted ? "" : variable.value}
+ onCommit={(value) => updateVariable(variable.id, { value })}
+ autoComplete="off"
+ placeholder="value"
+ spellCheck={false}
+ aria-label={`Environment variable value ${index + 1}`}
+ />
+ )}
<Tooltip>
<TooltipTrigger
render={
diff --git a/apps/web/src/components/settings/ProviderSettingsForm.tsx b/apps/web/src/components/settings/ProviderSettingsForm.tsx
index 3671f72912c..bdd87e24fca 100644
--- a/apps/web/src/components/settings/ProviderSettingsForm.tsx
+++ b/apps/web/src/components/settings/ProviderSettingsForm.tsx
@@ -3,11 +3,12 @@
import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
-import type {
- ProviderSettingsFormAnnotation,
- ProviderSettingsFormControl,
- ProviderSettingsFormOption,
- ProviderSettingsFormSchemaAnnotation,
+import {
+ isSecretSetting,
+ type ProviderSettingsFormAnnotation,
+ type ProviderSettingsFormControl,
+ type ProviderSettingsFormOption,
+ type ProviderSettingsFormSchemaAnnotation,
} from "@t3tools/contracts";
import { PlusIcon, XIcon } from "lucide-react";
@@ -15,6 +16,7 @@ import { cn } from "../../lib/utils";
import { Button } from "../ui/button";
import { DraftInput } from "../ui/draft-input";
import { Input } from "../ui/input";
+import { SecretInput } from "./SecretInput";
import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
import { Switch } from "../ui/switch";
import { Textarea } from "../ui/textarea";
@@ -131,7 +133,7 @@ export function deriveProviderSettingsFields(
? { placeholder: formAnnotation.placeholder }
: {}),
clearWhenEmpty: formAnnotation.clearWhenEmpty ?? "omit",
- ...(formAnnotation.secret ? { secret: true } : {}),
+ ...(isSecretSetting(fieldSchema) ? { secret: true } : {}),
...(formAnnotation.control === "switch"
? { defaultBooleanValue: readFieldBooleanDefault(fieldSchema) }
: {}),
@@ -255,19 +257,6 @@ function readProviderConfigString(config: unknown, key: string): string {
return typeof value === "string" ? value : "";
}
-/**
- * A secret field never shows its saved value: it starts empty, and its placeholder says whether
- * one is saved. Saving one is typing a new value; clearing it is the host form's job.
- */
-function secretFieldInputProps(field: ProviderSettingsFieldModel, config: unknown) {
- const saved = readProviderConfigString(config, field.key);
- if (!field.secret) return { value: saved, placeholder: field.placeholder };
- return {
- value: "",
- placeholder: saved.length > 0 ? "Stored secret, enter a new value to replace" : "Not set",
- };
-}
-
function readProviderConfigBoolean(config: unknown, key: string, defaultValue = false): boolean {
if (config === null || typeof config !== "object") return defaultValue;
const value = (config as Record<string, unknown>)[key];
@@ -423,6 +412,16 @@ function ProviderSettingsFieldRow({
placeholder={field.placeholder}
spellCheck={false}
/>
+ ) : field.secret ? (
+ <SecretInput
+ id={inputId}
+ aria-describedby={descriptionId}
+ size="sm"
+ className="w-full max-w-full @min-[32rem]/settings-row:w-56"
+ configured={readProviderConfigString(value, field.key).length > 0}
+ value=""
+ onCommit={(next) => onChange(nextProviderConfigWithFieldValue(value, field, next))}
+ />
) : (
<DraftInput
id={inputId}
@@ -431,10 +430,9 @@ function ProviderSettingsFieldRow({
className="w-full max-w-full @min-[32rem]/settings-row:w-56"
type={field.control === "password" ? "password" : undefined}
autoComplete={field.control === "password" ? "off" : undefined}
- {...secretFieldInputProps(field, value)}
+ value={readProviderConfigString(value, field.key)}
+ placeholder={field.placeholder}
onCommit={(next) => {
- // An untouched saved secret commits empty; only a typed value replaces it.
- if (field.secret && next === "") return;
onChange(nextProviderConfigWithFieldValue(value, field, next));
}}
spellCheck={false}
@@ -515,27 +513,29 @@ function ProviderSettingsFieldRow({
}
const type = field.control === "password" ? "password" : undefined;
+ const BufferedInput = field.secret ? SecretInput : DraftInput;
+ const ControlledInput = field.secret ? SecretInput : Input;
return (
<FieldFrame variant={variant}>
<label htmlFor={inputId} className={cn(variant === "card" && "block")}>
{label}
{variant === "card" ? (
- <DraftInput
+ <BufferedInput
id={inputId}
size="sm"
className="mt-1.5"
- type={type}
- autoComplete={field.control === "password" ? "off" : undefined}
+ type={field.secret ? undefined : type}
+ autoComplete={!field.secret && field.control === "password" ? "off" : undefined}
value={readProviderConfigString(value, field.key)}
onCommit={(next) => onChange(nextProviderConfigWithFieldValue(value, field, next))}
placeholder={field.placeholder}
spellCheck={false}
/>
) : (
- <Input
+ <ControlledInput
id={inputId}
- type={type}
- autoComplete={field.control === "password" ? "off" : undefined}
+ type={field.secret ? undefined : type}
+ autoComplete={!field.secret && field.control === "password" ? "off" : undefined}
value={readProviderConfigString(value, field.key)}
onChange={(event) =>
onChange(nextProviderConfigWithFieldValue(value, field, event.target.value))
diff --git a/apps/web/src/components/settings/SecretInput.test.tsx b/apps/web/src/components/settings/SecretInput.test.tsx
new file mode 100644
index 00000000000..8b7f7928a88
--- /dev/null
+++ b/apps/web/src/components/settings/SecretInput.test.tsx
@@ -0,0 +1,63 @@
+import { act } from "react";
+import { create, type ReactTestRenderer } from "react-test-renderer";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
+
+import { SecretInput } from "./SecretInput";
+
+vi.mock("../ui/input", () => ({
+ Input: (props: Record<string, unknown>) => <input {...props} />,
+}));
+
+let renderer: ReactTestRenderer;
+
+beforeEach(() => vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true));
+afterEach(async () => {
+ await act(() => renderer?.unmount());
+ vi.unstubAllGlobals();
+});
+
+async function edit(value: string) {
+ await act(() => renderer.root.findByType("input").props.onFocus());
+ await act(() => renderer.root.findByType("input").props.onChange({ target: { value } }));
+ await act(() => renderer.root.findByType("input").props.onBlur());
+}
+
+describe("SecretInput", () => {
+ it("preserves a configured secret on empty edits and commits a replacement", async () => {
+ const onCommit = vi.fn();
+ await act(() => {
+ renderer = create(<SecretInput configured value="" onCommit={onCommit} />);
+ });
+ await edit("");
+ expect(onCommit).not.toHaveBeenCalled();
+ await edit("replacement");
+ expect(onCommit).toHaveBeenCalledExactlyOnceWith("replacement");
+ expect(renderer.root.findByType("input").props.value).toBe("");
+ });
+
+ it("allows clearing an unsaved draft", async () => {
+ const onCommit = vi.fn();
+ await act(() => {
+ renderer = create(<SecretInput value="draft" onCommit={onCommit} />);
+ });
+ await edit("");
+ expect(onCommit).toHaveBeenCalledExactlyOnceWith("");
+ });
+
+ it("keeps an in-progress replacement when settings refresh", async () => {
+ const onCommit = vi.fn();
+ await act(() => {
+ renderer = create(<SecretInput configured value="" onCommit={onCommit} />);
+ });
+ await act(() => renderer.root.findByType("input").props.onFocus());
+ await act(() =>
+ renderer.root.findByType("input").props.onChange({ target: { value: "replacement" } }),
+ );
+ await act(() => {
+ renderer.update(<SecretInput configured value="" onCommit={onCommit} />);
+ });
+ expect(renderer.root.findByType("input").props.value).toBe("replacement");
+ await act(() => renderer.root.findByType("input").props.onBlur());
+ expect(onCommit).toHaveBeenCalledExactlyOnceWith("replacement");
+ });
+});
diff --git a/apps/web/src/components/settings/SecretInput.tsx b/apps/web/src/components/settings/SecretInput.tsx
new file mode 100644
index 00000000000..0bb06e927d6
--- /dev/null
+++ b/apps/web/src/components/settings/SecretInput.tsx
@@ -0,0 +1,39 @@
+import { DraftInput, type DraftInputProps } from "../ui/draft-input";
+import { Input, type InputProps } from "../ui/input";
+
+type SecretInputProps = Omit<InputProps, "type" | "autoComplete" | "defaultValue" | "value"> & {
+ /** Whether the server already holds a value. Pass only the replacement draft as `value`. */
+ readonly configured?: boolean;
+ readonly value: string;
+ readonly onCommit?: DraftInputProps["onCommit"];
+};
+
+/** Shared presentation for write-only settings. Clearing a saved value belongs to its form. */
+export function SecretInput({
+ configured = false,
+ placeholder,
+ onCommit,
+ ...props
+}: SecretInputProps) {
+ const shared = {
+ ...props,
+ type: "password" as const,
+ autoComplete: "off",
+ spellCheck: false,
+ placeholder: configured
+ ? "Stored secret, enter a new value to replace"
+ : (placeholder ?? "Not set"),
+ };
+ if (onCommit) {
+ return (
+ <DraftInput
+ {...shared}
+ onCommit={(next) => {
+ if (configured && next === "") return;
+ onCommit(next);
+ }}
+ />
+ );
+ }
+ return <Input {...shared} />;
+}
diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts
index 15db09f45e8..c0de88f4ac6 100644
--- a/packages/contracts/src/settings.ts
+++ b/packages/contracts/src/settings.ts
@@ -551,15 +551,19 @@ export interface ProviderSettingsFormAnnotation {
readonly placeholder?: string | undefined;
readonly hidden?: boolean | undefined;
readonly clearWhenEmpty?: "omit" | "persist" | undefined;
- /**
- * The value is a credential: the server keeps it in its secret store and clients only learn
- * whether one is saved. Source control host settings honor it; provider settings do not yet.
- */
- readonly secret?: boolean | undefined;
/** Choices for a `select` control. The first entry is the default. */
readonly options?: ReadonlyArray<ProviderSettingsFormOption> | undefined;
}
+/** Secret fields declared by a settings schema, shared by persistence and client redaction. */
+export function secretSettingsFields(schema: {
+ readonly fields: Readonly<Record<string, Schema.Top>>;
+}): ReadonlyArray<string> {
+ return Object.entries(schema.fields).flatMap(([field, fieldSchema]) => {
+ return isSecretSetting(fieldSchema) ? [field] : [];
+ });
+}
+
export interface ProviderSettingsFormSchemaAnnotation {
readonly order?: readonly string[] | undefined;
}
@@ -567,12 +571,24 @@ export interface ProviderSettingsFormSchemaAnnotation {
declare module "effect/Schema" {
namespace Annotations {
interface Annotations {
+ /** Store this credential separately and expose only whether it is configured to clients. */
+ readonly settingsSecret?: boolean | undefined;
readonly providerSettingsForm?: ProviderSettingsFormAnnotation | undefined;
readonly providerSettingsFormSchema?: ProviderSettingsFormSchemaAnnotation | undefined;
}
}
}
+/** Mark a string or a record of strings as a credential in its settings schema. */
+export function secretSetting<S extends Schema.Top>(schema: S): S["Rebuild"] {
+ return schema.annotateKey({ settingsSecret: true });
+}
+
+export function isSecretSetting(schema: Schema.Top): boolean {
+ const annotations = Schema.resolveAnnotationsKey(schema) ?? Schema.resolveAnnotations(schema);
+ return annotations?.settingsSecret === true;
+}
+
export type ProviderSettingsOrder<Fields extends Schema.Struct.Fields> = readonly Extract<
keyof Fields,
string
@@ -749,10 +765,11 @@ export const AntigravitySettings = makeProviderSettingsSchema(
}),
),
apiKey: TrimmedString.pipe(
+ secretSetting,
Schema.withDecodingDefault(Effect.succeed("")),
Schema.annotateKey({
title: "API key",
- description: "Gemini or Vertex AI express key. Stored in plain text.",
+ description: "Gemini or Vertex AI express key. Stored separately on the host.",
providerSettingsForm: {
control: "password",
placeholder: "Optional",
@@ -804,7 +821,7 @@ export const UsageLimitSourceConfig = Schema.Struct({
kind: Schema.Literal("cliproxy"),
label: Schema.optional(TrimmedNonEmptyString),
url: TrimmedNonEmptyString,
- managementKey: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
+ managementKey: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed("")), secretSetting),
enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
});
export type UsageLimitSourceConfig = typeof UsageLimitSourceConfig.Type;
diff --git a/packages/source-control-bitbucket/src/client/definition.ts b/packages/source-control-bitbucket/src/client/definition.ts
index fc0321ad810..797dc93f64c 100644
--- a/packages/source-control-bitbucket/src/client/definition.ts
+++ b/packages/source-control-bitbucket/src/client/definition.ts
@@ -39,7 +39,8 @@ export const settings = makeProviderSettingsSchema(
title: "Access token",
description:
"Scoped to one repository, project, or workspace. Create it in that item's Bitbucket settings.",
- providerSettingsForm: { control: "password", secret: true },
+ settingsSecret: true,
+ providerSettingsForm: { control: "password" },
}),
email: text({
title: "Atlassian account email",
@@ -50,7 +51,8 @@ export const settings = makeProviderSettingsSchema(
title: "API token",
description:
"Reaches every repository your Atlassian account can. Give it read and write access to repositories and pull requests, and read:user:bitbucket.",
- providerSettingsForm: { control: "password", secret: true },
+ settingsSecret: true,
+ providerSettingsForm: { control: "password" },
}),
},
{ order: ["accessToken", "email", "apiToken"] },
diff --git a/packages/source-control-core/src/client/definition.ts b/packages/source-control-core/src/client/definition.ts
index 2f245844e46..6fc6eb69b79 100644
--- a/packages/source-control-core/src/client/definition.ts
+++ b/packages/source-control-core/src/client/definition.ts
@@ -144,7 +144,7 @@ export interface SourceControlClientDefinition {
/**
* What the environment saves for this host under `settings.sourceControlHosts[kind]`, rendered
* as a form in Source Control settings and read by the host's server package. Fields carry
- * `providerSettingsForm` annotations, as agent provider settings do; one marked `secret` is
+ * `providerSettingsForm` annotations, as agent provider settings do; a `secretSetting` is
* kept in the server's secret store. Omitted for a host with nothing to configure.
*/
readonly settings?: SourceControlHostSettingsSchema;
@@ -322,17 +322,3 @@ export function readSourceControlHostSettings<S extends SourceControlHostSetting
if (fallback._tag === "Some") return fallback.value;
throw new Error("A source control host settings schema must decode an empty object.");
}
-
-/**
- * The settings fields a host keeps in the server's secret store: those annotated
- * `providerSettingsForm.secret`. A secret field holds a string, or a string per server host.
- */
-export function secretSourceControlHostSettingsFields(
- schema: SourceControlHostSettingsSchema,
-): ReadonlyArray<string> {
- return Object.entries(schema.fields).flatMap(([field, fieldSchema]) => {
- const annotations =
- Schema.resolveAnnotationsKey(fieldSchema) ?? Schema.resolveAnnotations(fieldSchema);
- return annotations?.providerSettingsForm?.secret ? [field] : [];
- });
-}
diff --git a/packages/source-control-gitcafe/src/client/definition.ts b/packages/source-control-gitcafe/src/client/definition.ts
index 59eb472648c..978fcee1de2 100644
--- a/packages/source-control-gitcafe/src/client/definition.ts
+++ b/packages/source-control-gitcafe/src/client/definition.ts
@@ -5,6 +5,7 @@
*/
import {
makeProviderSettingsSchema,
+ secretSetting,
SourceControlProviderKind,
TrimmedString,
} from "@t3tools/contracts";
@@ -25,12 +26,13 @@ const KIND = SourceControlProviderKind.make("gitcafe");
/** A git.cafe token, used before `CAFE_TOKEN` and the `cafe` login. */
export const settings = makeProviderSettingsSchema({
token: TrimmedString.pipe(
+ secretSetting,
Schema.withDecodingDefault(Effect.succeed("")),
Schema.annotateKey({
title: "Token",
description:
"A git.cafe token, used before CAFE_TOKEN and the cafe login, so GitCafe works without the CLI signed in.",
- providerSettingsForm: { control: "password", secret: true },
+ providerSettingsForm: { control: "password" },
}),
),
});
diff --git a/packages/source-control-github/src/client/definition.ts b/packages/source-control-github/src/client/definition.ts
index ca664336135..9d20128f8df 100644
--- a/packages/source-control-github/src/client/definition.ts
+++ b/packages/source-control-github/src/client/definition.ts
@@ -6,6 +6,7 @@
import {
GitHubHost,
makeProviderSettingsSchema,
+ secretSetting,
pullRequestHostOf,
SourceControlProviderKind,
TrimmedNonEmptyString,
@@ -45,7 +46,8 @@ export const settings = makeProviderSettingsSchema({
),
tokens: Schema.Record(GitHubHost, TrimmedString).pipe(
Schema.withDecodingDefault(Effect.succeed({})),
- Schema.annotateKey({ providerSettingsForm: { hidden: true, secret: true } }),
+ secretSetting,
+ Schema.annotateKey({ providerSettingsForm: { hidden: true } }),
),
});
export type GitHubSettings = typeof settings.Type;