Skip to content
TypeScript

feat(server): read usage limit source keys from a secret provider (#27760)

Yordis Prietoa10490937dbd
diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts
index 8d50f0b597b..72675665574 100644
--- a/apps/server/src/serverSettings.test.ts
+++ b/apps/server/src/serverSettings.test.ts
@@ -27,6 +27,7 @@ import * as Path from "effect/Path";
import * as PlatformError from "effect/PlatformError";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
+import * as SchemaAST from "effect/SchemaAST";
import * as SqlClient from "effect/sql/SqlClient";
import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
import { sourceControlHostSecretName } from "./sourceControl/sourceControlHostSecrets.ts";
@@ -34,6 +35,7 @@ import * as ServerConfig from "./config.ts";
import * as SqlitePersistence from "./persistence/Sqlite.ts";
import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite";
import * as ServerSettingsModule from "./serverSettings.ts";
+import { SECRET_REDACTED } from "./settingsSecrets.ts";
import * as ManagedPolicy from "./policy/ManagedPolicy.ts";
import { resolveProviderInstanceTerminalEnvironment } from "./terminal/Manager.ts";
@@ -102,6 +104,36 @@ const recordProviderUsage = (provider: string, instanceId: string | null = provi
`;
});
+/** Paths of fields typed `SettingsSecretValue` that clients decode with the settings schema itself. */
+function strictSecretReferencePaths(
+ ast: SchemaAST.AST,
+ path = "",
+ seen = new Set<SchemaAST.AST>(),
+): ReadonlyArray<string> {
+ if (seen.has(ast)) return [];
+ seen.add(ast);
+ const walk = (type: SchemaAST.AST, at: string): ReadonlyArray<string> =>
+ type.context?.annotations?.settingsSecretReference === true
+ ? [at]
+ : strictSecretReferencePaths(type, `${at}.`, seen);
+ if (SchemaAST.isObjects(ast)) {
+ return [
+ ...ast.propertySignatures.flatMap((property) =>
+ walk(property.type, `${path}${String(property.name)}`),
+ ),
+ ...ast.indexSignatures.flatMap((signature) => walk(signature.type, `${path}*`)),
+ ];
+ }
+ if (SchemaAST.isArrays(ast)) {
+ return [...ast.elements, ...ast.rest].flatMap((element) => walk(element, `${path}[]`));
+ }
+ if (SchemaAST.isUnion(ast)) {
+ return ast.types.flatMap((member) => strictSecretReferencePaths(member, path, seen));
+ }
+ if (SchemaAST.isSuspend(ast)) return strictSecretReferencePaths(ast.thunk(), path, seen);
+ return [];
+}
+
it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings", (it) => {
it.effect("serializes concurrent model preference edits without cancelling duplicate adds", () =>
Effect.gen(function* () {
@@ -278,7 +310,8 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
},
});
assert.deepStrictEqual<unknown>(
- ServerSettingsModule.redactServerSettingsForClient(settings).secretProviders,
+ ServerSettingsModule.redactServerSettingsForClient(settings, { secretReferences: true })
+ .secretProviders,
{
personal: { kind: "one_password", label: "Personal", config: { account: "my" } },
vault: { kind: "openbao", label: "Vault" },
@@ -1331,7 +1364,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
"quota-secret",
"expo-secret",
];
- const client = ServerSettingsModule.redactServerSettingsForClient(saved);
+ const client = ServerSettingsModule.redactServerSettingsForClient(saved, {
+ secretReferences: true,
+ });
const disk = yield* fs.readFileString(config.settingsPath);
for (const value of values) {
assert.include(JSON.stringify(saved), value);
@@ -1365,7 +1400,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
instance: { ...client.providerInstances[instanceId]!, config: { apiKey } },
});
assert.propertyVal(changed.providerInstances[instanceId]?.config, "apiKey", apiKey);
- const redacted = ServerSettingsModule.redactServerSettingsForClient(changed);
+ const redacted = ServerSettingsModule.redactServerSettingsForClient(changed, {
+ secretReferences: true,
+ });
assert.propertyVal(
redacted.providerInstances[instanceId]?.config,
"apiKey",
@@ -1414,9 +1451,8 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
});
assert.isTrue(Option.isNone(yield* store.get(secretNames[1]!)));
assert.deepEqual(
- ServerSettingsModule.redactServerSettingsForClient(referenced).providerInstances[
- instanceId
- ]?.environment,
+ ServerSettingsModule.redactServerSettingsForClient(referenced, { secretReferences: true })
+ .providerInstances[instanceId]?.environment,
[{ name: "API_TOKEN", value: secret, sensitive: true }],
);
yield* service.updateSettings({
@@ -1463,7 +1499,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
"serverPassword",
"legacy-password",
);
- const client = ServerSettingsModule.redactServerSettingsForClient(saved);
+ const client = ServerSettingsModule.redactServerSettingsForClient(saved, {
+ secretReferences: true,
+ });
for (const value of ["legacy-api-key", "legacy-password"]) {
assert.notInclude(yield* fs.readFileString(config.settingsPath), value);
assert.notInclude(JSON.stringify(client), value);
@@ -1507,8 +1545,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
for (const secret of ["bb-access", "ghp_dotcom", "ghp_ghe"]) assert.notInclude(raw, secret);
assert.include(raw, "[email protected]");
- const forClient =
- ServerSettingsModule.redactServerSettingsForClient(saved).sourceControlHosts;
+ const forClient = ServerSettingsModule.redactServerSettingsForClient(saved, {
+ secretReferences: true,
+ }).sourceControlHosts;
assert.equal(forClient.bitbucket?.email, "[email protected]");
assert.notInclude(String(forClient.bitbucket?.accessToken), "bb-access");
assert.isAbove(String(forClient.bitbucket?.accessToken).length, 0);
@@ -1570,15 +1609,59 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
assert.isTrue(Option.isNone(yield* stored("bitbucket", "accessToken", null)));
assert.isTrue(Option.isNone(yield* stored("github", "tokens", "github.com")));
assert.isTrue(Option.isSome(yield* stored("github", "tokens", "ghe.acme.test")));
- const redacted =
- ServerSettingsModule.redactServerSettingsForClient(referenced).sourceControlHosts;
+ const redacted = ServerSettingsModule.redactServerSettingsForClient(referenced, {
+ secretReferences: true,
+ }).sourceControlHosts;
assert.deepEqual(redacted.bitbucket?.accessToken, reference);
const tokens = redacted.github?.tokens as Record<string, unknown>;
assert.deepEqual(tokens["github.com"], gitHubReference);
assert.notEqual(tokens["ghe.acme.test"], "ghp_ghe");
+
+ // A client that cannot decode references sees saved secrets and can send them back.
+ const usageReference = {
+ provider: SecretProviderId.make("personal"),
+ reference: "op://Private/Hub/key",
+ };
+ const hub = UsageLimitSourceId.make("hub");
+ const withUsage = yield* serverSettings.updateSettings({
+ usageLimitSources: {
+ [hub]: {
+ kind: "cliproxy",
+ url: "https://hub.test",
+ managementKey: usageReference,
+ enabled: true,
+ },
+ },
+ });
+ const legacy = ServerSettingsModule.redactServerSettingsForClient(withUsage, {
+ secretReferences: false,
+ });
+ assert.notInclude(JSON.stringify(legacy), "op://");
+ assert.equal(legacy.sourceControlHosts.bitbucket?.accessToken, SECRET_REDACTED);
+ assert.equal(legacy.usageLimitSources[hub]?.managementKey, SECRET_REDACTED);
+ const echoed = yield* serverSettings.updateSettings({
+ sourceControlHosts: legacy.sourceControlHosts,
+ usageLimitSources: legacy.usageLimitSources,
+ });
+ assert.deepEqual(echoed.sourceControlHosts.bitbucket?.accessToken, reference);
+ assert.deepEqual(echoed.sourceControlHosts.github?.tokens, {
+ "github.com": gitHubReference,
+ "ghe.acme.test": "ghp_ghe",
+ });
+ assert.deepEqual(echoed.usageLimitSources[hub]?.managementKey, usageReference);
+ assert.isTrue(Option.isSome(yield* stored("github", "tokens", "ghe.acme.test")));
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
);
+ it.effect("knows every strictly decoded field that can hold a secret reference", () =>
+ Effect.sync(() => {
+ // Shipped clients decode these as strings. Each must be covered by the older-client case above.
+ assert.deepEqual(strictSecretReferencePaths(ServerSettings.ast), [
+ "usageLimitSources.*.managementKey",
+ ]);
+ }),
+ );
+
it.effect("keeps the retired keys for another load when a token cannot be copied", () => {
const cause = new ServerSecretStore.SecretStoreReadError({
resource: "bitbucket-access-token",
diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts
index 1ead9e27bfc..831a5fad499 100644
--- a/apps/server/src/serverSettings.ts
+++ b/apps/server/src/serverSettings.ts
@@ -18,6 +18,8 @@ import {
type ServerSettingsPatch,
type ManagedPolicySummary,
globalIdleCompaction,
+ isSecretReference,
+ type SecretReference,
withIdleCompaction,
} from "@t3tools/contracts";
import * as Cache from "effect/Cache";
@@ -60,7 +62,12 @@ import {
sourceControlHostSecretName,
} from "./sourceControl/sourceControlHostSecrets.ts";
-import { mapSettingsSecrets, SECRET_REDACTED, type SettingsSecret } from "./settingsSecrets.ts";
+import {
+ mapSettingsSecrets,
+ mapSettingsSecretValues,
+ SECRET_REDACTED,
+ type SettingsSecret,
+} from "./settingsSecrets.ts";
export {
resolveReviewGuideModelSelection,
@@ -131,13 +138,35 @@ const normalizeServerSettings = (
),
);
-export function redactServerSettingsForClient(settings: ServerSettings): ServerSettings {
+export function redactServerSettingsForClient(
+ settings: ServerSettings,
+ client: { readonly secretReferences: boolean },
+): ServerSettings {
const redacted = mapSettingsSecrets(settings, (secret) =>
secret.sensitive
? { value: "", redacted: secret.redacted || secret.value.length > 0 }
: { value: secret.value, redacted: false },
);
- return { ...redacted, secretProviders: withoutUnknownSecretProviderConfigs(redacted) };
+ // Older clients decode secret fields as strings and fail on a reference, so they see a saved secret.
+ const shaped = client.secretReferences
+ ? redacted
+ : mapSettingsSecretValues(redacted, (value) =>
+ isSecretReference(value) ? SECRET_REDACTED : value,
+ );
+ return { ...shaped, secretProviders: withoutUnknownSecretProviderConfigs(shaped) };
+}
+
+/** A client shown a reference as a saved secret sends that marker back; it keeps the reference. */
+function withEchoedSecretReferences(current: ServerSettings, next: ServerSettings): ServerSettings {
+ const references = new Map<string, SecretReference>();
+ mapSettingsSecretValues(current, (value, secretName) => {
+ if (isSecretReference(value)) references.set(secretName, value);
+ return value;
+ });
+ if (references.size === 0) return next;
+ return mapSettingsSecretValues(next, (value, secretName) =>
+ value === SECRET_REDACTED ? (references.get(secretName) ?? value) : value,
+ );
}
// A newer kind's config may hold credentials this build cannot recognize, so it never leaves the server.
@@ -1004,7 +1033,7 @@ const make = Effect.gen(function* () {
});
const changes: SecretChange[] = [];
const nextSecretNames = new Set<string>();
- const settings = mapSettingsSecrets(next, (secret) => {
+ const settings = mapSettingsSecrets(withEchoedSecretReferences(current, next), (secret) => {
const metadata = {
secretName: secret.secretName,
...(secret.providerInstanceId !== undefined
diff --git a/apps/server/src/settingsSecrets.ts b/apps/server/src/settingsSecrets.ts
index 1bebce51b38..17640237ff5 100644
--- a/apps/server/src/settingsSecrets.ts
+++ b/apps/server/src/settingsSecrets.ts
@@ -3,7 +3,9 @@ import {
ClaudeSettings,
CodexSettings,
EasDeviceHostConfig,
+ type ProviderInstanceConfig,
type ServerSettings,
+ type SettingsSecretValue,
SimlockDeviceHostConfig,
UsageLimitSourceConfig,
isSecretReference,
@@ -58,16 +60,19 @@ export interface SettingsSecret {
}
type SecretValue = Pick<SettingsSecret, "value" | "redacted">;
+type SecretSlotName = (field: string, serverHost: string | null) => string;
+type SecretFieldsMapper = (
+ fields: Readonly<Record<string, unknown>>,
+ secretFields: ReadonlyArray<string>,
+ name: SecretSlotName,
+) => Record<string, unknown>;
+type ProviderEnvironment = NonNullable<ProviderInstanceConfig["environment"]>;
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 mapFields: SecretFieldsMapper = (fields, secretFields, name) => {
const next = { ...fields };
for (const field of secretFields) {
if (!(field in next) || isSecretReference(next[field])) continue;
@@ -88,6 +93,70 @@ export function mapSettingsSecrets(
return next;
};
+ return mapSecretFieldGroups(settings, mapFields, (instanceId, environment) =>
+ environment.map((variable) => {
+ const { valueRedacted: _omit, ...rest } = variable;
+ // A secret reference is not itself secret; storage sees the value cleared.
+ const reference = typeof variable.value === "string" ? undefined : variable.value;
+ const result = map({
+ secretName: `provider-env-${encode(instanceId)}-${encode(variable.name)}`,
+ kind: "environment",
+ value: typeof variable.value === "string" ? variable.value : "",
+ redacted: reference === undefined && variable.valueRedacted === true,
+ sensitive: variable.sensitive,
+ providerInstanceId: instanceId,
+ environmentVariable: variable.name,
+ });
+ if (reference !== undefined) return { ...rest, value: reference };
+ return {
+ ...rest,
+ value: result.value,
+ ...(result.redacted ? { valueRedacted: true } : {}),
+ };
+ }),
+ );
+}
+
+/**
+ * Maps each value a secret field holds, literal or reference, with the secret name of its slot.
+ * Provider environment variables are left as they are.
+ */
+export function mapSettingsSecretValues(
+ settings: ServerSettings,
+ map: (value: SettingsSecretValue, secretName: string) => SettingsSecretValue,
+): ServerSettings {
+ const mapValue = (value: unknown, secretName: string) =>
+ typeof value === "string" || isSecretReference(value) ? map(value, secretName) : value;
+ return mapSecretFieldGroups(settings, (fields, secretFields, name) => {
+ const next = { ...fields };
+ for (const field of secretFields) {
+ if (!(field in next)) continue;
+ const value = next[field];
+ next[field] =
+ value !== null &&
+ typeof value === "object" &&
+ !Array.isArray(value) &&
+ !isSecretReference(value)
+ ? Object.fromEntries(
+ Object.entries(value).map(([serverHost, entry]) => [
+ serverHost,
+ mapValue(entry, name(field, serverHost.trim().toLowerCase())),
+ ]),
+ )
+ : mapValue(value, name(field, null));
+ }
+ return next;
+ });
+}
+
+function mapSecretFieldGroups(
+ settings: ServerSettings,
+ mapFields: SecretFieldsMapper,
+ mapEnvironment: (instanceId: string, environment: ProviderEnvironment) => ProviderEnvironment = (
+ _instanceId,
+ environment,
+ ) => environment,
+): ServerSettings {
const providerInstances = Object.fromEntries(
Object.entries(settings.providerInstances).map(([instanceId, instance]) => {
const config = instance.config;
@@ -107,28 +176,7 @@ export function mapSettingsSecrets(
}
: {}),
...(instance.environment
- ? {
- environment: instance.environment.map((variable) => {
- const { valueRedacted: _omit, ...rest } = variable;
- // A secret reference is not itself secret; storage sees the value cleared.
- const reference = typeof variable.value === "string" ? undefined : variable.value;
- const result = map({
- secretName: `provider-env-${encode(instanceId)}-${encode(variable.name)}`,
- kind: "environment",
- value: typeof variable.value === "string" ? variable.value : "",
- redacted: reference === undefined && variable.valueRedacted === true,
- sensitive: variable.sensitive,
- providerInstanceId: instanceId,
- environmentVariable: variable.name,
- });
- if (reference !== undefined) return { ...rest, value: reference };
- return {
- ...rest,
- value: result.value,
- ...(result.redacted ? { valueRedacted: true } : {}),
- };
- }),
- }
+ ? { environment: mapEnvironment(instanceId, instance.environment) }
: {}),
},
];
diff --git a/apps/server/src/sourceControl/sourceControlHostSecrets.test.ts b/apps/server/src/sourceControl/sourceControlHostSecrets.test.ts
index be1da6f3d61..697e166c324 100644
--- a/apps/server/src/sourceControl/sourceControlHostSecrets.test.ts
+++ b/apps/server/src/sourceControl/sourceControlHostSecrets.test.ts
@@ -1,5 +1,5 @@
import { assert, it } from "@effect/vitest";
-import { SecretProviderId } from "@t3tools/contracts";
+import { SecretProviderId, UsageLimitSourceId } from "@t3tools/contracts";
import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
import * as Effect from "effect/Effect";
@@ -125,12 +125,31 @@ it.effect("reads a collected reference again once it is invalidated", () =>
}),
});
const read = () => resolveSourceControlHostSecrets(resolver, hosts, "bitbucket");
+ const usageLimitSources = {
+ [UsageLimitSourceId.make("referenced")]: {
+ kind: "cliproxy",
+ url: "https://hub.test",
+ managementKey: source("cliproxy#key"),
+ enabled: true,
+ },
+ [UsageLimitSourceId.make("literal")]: {
+ kind: "cliproxy",
+ url: "https://hub.test",
+ managementKey: "plain-key",
+ enabled: true,
+ },
+ } as const;
+ cache.set("cliproxy#key", "stale");
assert.deepEqual((yield* read()).bitbucket, {
accessToken: "stale",
});
current = "rotated";
- yield* invalidateSettingsSecrets(resolver, Effect.succeed({ sourceControlHosts: hosts }));
+ yield* invalidateSettingsSecrets(
+ resolver,
+ Effect.succeed({ sourceControlHosts: hosts, usageLimitSources }),
+ );
+ assert.isFalse(cache.has("cliproxy#key"));
assert.deepEqual((yield* read()).bitbucket, {
accessToken: "rotated",
diff --git a/apps/server/src/sourceControl/sourceControlHostSecrets.ts b/apps/server/src/sourceControl/sourceControlHostSecrets.ts
index 82de25be4de..32d2f41f9c5 100644
--- a/apps/server/src/sourceControl/sourceControlHostSecrets.ts
+++ b/apps/server/src/sourceControl/sourceControlHostSecrets.ts
@@ -97,13 +97,23 @@ export const resolveSourceControlHostSecrets = Effect.fn("SourceControlHost.reso
},
);
+const collectUsageLimitSecretReferences = (
+ sources: ServerSettings["usageLimitSources"],
+): ReadonlyArray<SecretReference> =>
+ Object.values(sources).flatMap(({ managementKey }) =>
+ isSecretReference(managementKey) ? [managementKey] : [],
+ );
+
/** Settings that cannot be read clear every cached secret, so a refresh never keeps a stale one. */
export const invalidateSettingsSecrets = Effect.fn("SettingsSecrets.invalidate")(function* <E>(
resolver: SecretResolver.SecretResolver["Service"],
- settings: Effect.Effect<Pick<ServerSettings, "sourceControlHosts">, E>,
+ settings: Effect.Effect<Pick<ServerSettings, "sourceControlHosts" | "usageLimitSources">, E>,
) {
const references = yield* settings.pipe(
- Effect.map((current) => collectSourceControlHostSecretReferences(current.sourceControlHosts)),
+ Effect.map((current) => [
+ ...collectSourceControlHostSecretReferences(current.sourceControlHosts),
+ ...collectUsageLimitSecretReferences(current.usageLimitSources),
+ ]),
Effect.orElseSucceed(() => undefined),
);
yield* resolver.invalidate(references);
diff --git a/apps/server/src/usage/UsageLimitSources.test.ts b/apps/server/src/usage/UsageLimitSources.test.ts
index b8976fca81e..1d955d497ef 100644
--- a/apps/server/src/usage/UsageLimitSources.test.ts
+++ b/apps/server/src/usage/UsageLimitSources.test.ts
@@ -1,10 +1,11 @@
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import { expect, it } from "@effect/vitest";
-import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts";
+import { SecretProviderId, ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts";
+import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
-import { HttpClient } from "effect/http";
+import { HttpClient, HttpClientResponse } from "effect/http";
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
import * as ServerSettings from "../serverSettings.ts";
@@ -36,6 +37,10 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () =>
);
}).pipe(
Effect.provide([
+ Layer.succeed(SecretResolver.SecretResolver, {
+ read: (references) => Effect.succeed(references.map(() => undefined)),
+ invalidate: () => Effect.void,
+ }),
ServerSettings.layerTest({
usageLimitSources: {
[UsageLimitSourceId.make("local-hub")]: {
@@ -83,3 +88,66 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () =>
Effect.scoped,
),
);
+
+it.effect("reads a management key from its secret provider", () => {
+ const authorizations: Array<string | undefined> = [];
+ const keySource = (reference: string) => ({
+ provider: SecretProviderId.make("homelab"),
+ reference,
+ });
+ return Effect.gen(function* () {
+ const sources = yield* UsageLimitSources.make;
+ yield* sources.refresh;
+ const snapshots = yield* sources.current;
+ expect(snapshots.map(({ id, error }) => ({ id, error }))).toEqual([
+ { id: "resolved-hub", error: undefined },
+ {
+ id: "unresolved-hub",
+ error: "Could not read the management key from its secret provider.",
+ },
+ ]);
+ expect(authorizations).toEqual(["Bearer resolved-key"]);
+ }).pipe(
+ Effect.provide([
+ ServerSettings.layerTest({
+ usageLimitSources: {
+ [UsageLimitSourceId.make("resolved-hub")]: {
+ kind: "cliproxy",
+ url: "https://resolved.test",
+ managementKey: keySource("cliproxy#key"),
+ enabled: true,
+ },
+ [UsageLimitSourceId.make("unresolved-hub")]: {
+ kind: "cliproxy",
+ url: "https://unresolved.test",
+ managementKey: keySource("missing#key"),
+ enabled: true,
+ },
+ },
+ }),
+ Layer.mock(BackgroundPolicy.BackgroundPolicy)({
+ shouldRunScopeWork: () => Effect.succeed(false),
+ }),
+ Layer.succeed(SecretResolver.SecretResolver, {
+ read: (references) =>
+ Effect.succeed(
+ references.map((reference) =>
+ reference.reference === "cliproxy#key" ? "resolved-key" : undefined,
+ ),
+ ),
+ invalidate: () => Effect.void,
+ }),
+ Layer.succeed(
+ HttpClient.HttpClient,
+ HttpClient.make((request) =>
+ Effect.sync(() => {
+ authorizations.push(request.headers.authorization);
+ return HttpClientResponse.fromWeb(request, Response.json({ files: [] }));
+ }),
+ ),
+ ),
+ NodeCrypto.layer,
+ ]),
+ Effect.scoped,
+ );
+});
diff --git a/apps/server/src/usage/UsageLimitSources.ts b/apps/server/src/usage/UsageLimitSources.ts
index 7a068cde85f..7d40b0fb9ec 100644
--- a/apps/server/src/usage/UsageLimitSources.ts
+++ b/apps/server/src/usage/UsageLimitSources.ts
@@ -4,7 +4,8 @@ import {
type UsageLimitSourceConsumeResetCreditInput,
type ProviderConsumeResetCreditResult,
type ServerSettings,
- type UsageLimitSourceConfig,
+ UsageLimitSourceConfig,
+ settingsSecretLiteral,
type UsageLimitSourceId,
type UsageLimitSourceSnapshot,
} from "@t3tools/contracts";
@@ -21,6 +22,9 @@ import * as Semaphore from "effect/Semaphore";
import * as Stream from "effect/Stream";
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
+import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
+import * as SecretResolverLayer from "../secrets/SecretResolver.ts";
+import { resolveSettingsSecrets } from "../secrets/settingsSecretValue.ts";
import * as Settings from "../serverSettings.ts";
import { makeCliproxyApi } from "./cliproxyApi.ts";
@@ -46,11 +50,24 @@ function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
}
}
+function missingManagementKey(config: UsageLimitSourceConfig): string {
+ return typeof config.managementKey === "string"
+ ? "No management key configured."
+ : "Could not read the management key from its secret provider.";
+}
+
/** @public */
export const make = Effect.gen(function* () {
const api = yield* makeCliproxyApi;
const settingsService = yield* Settings.ServerSettingsService;
+ const secrets = yield* SecretResolver.SecretResolver;
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
+ const readManagementKey = Effect.fn("UsageLimitSources.readManagementKey")(function* (
+ config: UsageLimitSourceConfig,
+ ) {
+ const resolved = yield* resolveSettingsSecrets(secrets, UsageLimitSourceConfig, config);
+ return settingsSecretLiteral(resolved.managementKey);
+ });
const stateRef = yield* Ref.make<ReadonlyArray<UsageLimitSourceSnapshot>>([]);
const changes = yield* Effect.acquireRelease(
PubSub.unbounded<ReadonlyArray<UsageLimitSourceSnapshot>>(),
@@ -63,10 +80,13 @@ export const make = Effect.gen(function* () {
) {
const checkedAt = DateTime.formatIso(yield* DateTime.now);
const base = { id, kind: config.kind, label: sourceLabel(id, config), checkedAt } as const;
- if (config.managementKey.length === 0) {
- return { ...base, accounts: [], error: "No management key configured." };
+ const managementKey = yield* readManagementKey(config);
+ if (managementKey.length === 0) {
+ return { ...base, accounts: [], error: missingManagementKey(config) };
}
- const accounts = yield* api.readAccounts(config).pipe(Effect.result);
+ const accounts = yield* api
+ .readAccounts({ url: config.url, managementKey })
+ .pipe(Effect.result);
if (accounts._tag === "Failure") {
yield* Effect.logDebug("usage limit source read failed", { id, cause: accounts.failure });
return { ...base, accounts: [], error: accounts.failure.detail };
@@ -108,12 +128,20 @@ export const make = Effect.gen(function* () {
Effect.mapError(() => new UsageLimitSourceError({ detail: "Could not read hub settings." })),
);
const config = settings.usageLimitSources[input.sourceId];
- if (!config?.enabled || !config.managementKey) {
+ if (!config?.enabled) {
return yield* new UsageLimitSourceError({
detail: "The usage limit source is missing or disabled.",
});
}
- const result = yield* api.consume(config, input.accountId, input.creditId);
+ const managementKey = yield* readManagementKey(config);
+ if (managementKey.length === 0) {
+ return yield* new UsageLimitSourceError({ detail: missingManagementKey(config) });
+ }
+ const result = yield* api.consume(
+ { url: config.url, managementKey },
+ input.accountId,
+ input.creditId,
+ );
const snapshot = yield* readSource(input.sourceId, config);
const previous = yield* Ref.get(stateRef);
yield* publish(previous.map((source) => (source.id === input.sourceId ? snapshot : source)));
@@ -167,4 +195,6 @@ export const make = Effect.gen(function* () {
} satisfies UsageLimitSources["Service"];
});
-export const layer = Layer.effect(UsageLimitSources, make);
+export const layer = Layer.effect(UsageLimitSources, make).pipe(
+ Layer.provide(SecretResolverLayer.layer),
+);
diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts
index daa1ea909f0..4fb59d041c3 100644
--- a/apps/server/src/usage/cliproxyApi.ts
+++ b/apps/server/src/usage/cliproxyApi.ts
@@ -18,6 +18,12 @@ import { codexRateLimitsToLimits } from "../provider/codexUsageLimits.ts";
import { claudeUsageResponseToLimits } from "../provider/claudeUsageLimits.ts";
import { makeUnavailableUsageLimits } from "@t3tools/provider-core/server/usageLimits";
+/** A hub with its management key already read from wherever the setting keeps it. */
+export interface CliproxyHub {
+ readonly url: UsageLimitSourceConfig["url"];
+ readonly managementKey: string;
+}
+
const AuthFile = Schema.Struct({
id: Schema.String,
auth_index: Schema.String,
@@ -173,7 +179,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
const digest = yield* Digest.Digest;
const management = Effect.fn("CliproxyApi.management")(function* (
- config: UsageLimitSourceConfig,
+ hub: CliproxyHub,
path: string,
body?: unknown,
) {
@@ -181,13 +187,13 @@ export const makeCliproxyApi = Effect.gen(function* () {
try: () =>
new URL(
`v0/management/${path}`,
- config.url.endsWith("/") ? config.url : `${config.url}/`,
+ hub.url.endsWith("/") ? hub.url : `${hub.url}/`,
).toString(),
catch: () => new UsageLimitSourceError({ detail: "The hub URL is not valid." }),
});
const request = (
body === undefined ? HttpClientRequest.get(url) : HttpClientRequest.post(url)
- ).pipe(HttpClientRequest.setHeader("Authorization", `Bearer ${config.managementKey}`));
+ ).pipe(HttpClientRequest.setHeader("Authorization", `Bearer ${hub.managementKey}`));
const response = yield* client
.execute(body === undefined ? request : request.pipe(HttpClientRequest.bodyJsonUnsafe(body)))
.pipe(
@@ -201,13 +207,13 @@ export const makeCliproxyApi = Effect.gen(function* () {
return response;
});
- const authFiles = Effect.fn("CliproxyApi.authFiles")(function* (config: UsageLimitSourceConfig) {
- const response = yield* management(config, "auth-files");
+ const authFiles = Effect.fn("CliproxyApi.authFiles")(function* (hub: CliproxyHub) {
+ const response = yield* management(hub, "auth-files");
return (yield* decodeAuthFiles(response)).files;
});
const apiCall = Effect.fn("CliproxyApi.apiCall")(function* (
- config: UsageLimitSourceConfig,
+ hub: CliproxyHub,
account: typeof AuthFile.Type,
url: string,
data?: unknown,
@@ -229,7 +235,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
// The OAuth usage endpoint can return 429 without a Claude Code user agent.
"User-Agent": "claude-code/2.1.273",
};
- const raw = yield* management(config, "api-call", {
+ const raw = yield* management(hub, "api-call", {
auth_index: account.auth_index,
method: data === undefined ? "GET" : "POST",
url,
@@ -246,10 +252,10 @@ export const makeCliproxyApi = Effect.gen(function* () {
});
const credits = Effect.fn("CliproxyApi.credits")(function* (
- config: UsageLimitSourceConfig,
+ hub: CliproxyHub,
account: typeof AuthFile.Type,
) {
- const body = yield* apiCall(config, account, CREDIT_URL);
+ const body = yield* apiCall(hub, account, CREDIT_URL);
const response = yield* decodeCreditList(body);
const now = DateTime.toEpochMillis(yield* DateTime.now);
return response.credits
@@ -263,7 +269,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
});
const readAccount = Effect.fn("CliproxyApi.readAccount")(function* (
- config: UsageLimitSourceConfig,
+ hub: CliproxyHub,
account: typeof AuthFile.Type,
) {
const checkedAt = DateTime.formatIso(yield* DateTime.now);
@@ -277,7 +283,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
};
const read = Effect.gen(function* () {
if (account.provider === "claude") {
- const body = yield* apiCall(config, account, "https://api.anthropic.com/api/oauth/usage");
+ const body = yield* apiCall(hub, account, "https://api.anthropic.com/api/oauth/usage");
const usage = yield* decodeClaudeUsage(body);
const model_scoped = (usage.limits ?? []).flatMap((limit) =>
limit.kind === "weekly_scoped" && limit.scope?.model && typeof limit.percent === "number"
@@ -309,7 +315,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
}).limits,
};
}
- const body = yield* apiCall(config, account, `${CODEX_BASE}/usage`);
+ const body = yield* apiCall(hub, account, `${CODEX_BASE}/usage`);
const usage = yield* decodeCodexUsage(body);
const toWindow = (window: typeof CodexWindow.Type | null | undefined) =>
window
@@ -321,7 +327,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
: { windowDurationMins: window.limit_window_seconds / 60 }),
}
: null;
- const available = yield* credits(config, account).pipe(Effect.orElseSucceed(() => undefined));
+ const available = yield* credits(hub, account).pipe(Effect.orElseSucceed(() => undefined));
const next = available?.[0];
const planType =
usage.plan_type ?? account.id_token?.plan_type ?? account.id_token?.chatgpt_plan_type;
@@ -366,9 +372,9 @@ export const makeCliproxyApi = Effect.gen(function* () {
});
const readAccounts = Effect.fn("CliproxyApi.readAccounts")(function* (
- config: UsageLimitSourceConfig,
+ hub: CliproxyHub,
): Effect.fn.Return<ReadonlyArray<UsageLimitSourceAccount>, UsageLimitSourceError> {
- const accounts = yield* authFiles(config).pipe(
+ const accounts = yield* authFiles(hub).pipe(
Effect.mapError((error) =>
isUsageLimitSourceError(error)
? error
@@ -380,24 +386,24 @@ export const makeCliproxyApi = Effect.gen(function* () {
(account) =>
!account.disabled && (account.provider === "codex" || account.provider === "claude"),
),
- (account) => readAccount(config, account),
+ (account) => readAccount(hub, account),
{ concurrency: 4 },
);
});
const consume = Effect.fn("CliproxyApi.consume")(function* (
- config: UsageLimitSourceConfig,
+ hub: CliproxyHub,
accountId: string,
creditId: string,
): Effect.fn.Return<ProviderConsumeResetCreditResult, UsageLimitSourceError> {
const operation = Effect.gen(function* () {
- const account = (yield* authFiles(config)).find((account) => account.id === accountId);
+ const account = (yield* authFiles(hub)).find((account) => account.id === accountId);
if (!account || account.disabled || account.provider !== "codex") {
return yield* new UsageLimitSourceError({
detail: "The Codex hub account is missing or disabled.",
});
}
- const body = yield* apiCall(config, account, `${CREDIT_URL}/consume`, {
+ const body = yield* apiCall(hub, account, `${CREDIT_URL}/consume`, {
redeem_request_id: yield* creditRedeemRequestId(
account.id_token?.chatgpt_account_id ?? account.id,
creditId,
@@ -414,7 +420,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
} as const
)[response.code];
if (outcome !== "reset" && outcome !== "alreadyRedeemed") return { outcome };
- const cleared = yield* management(config, "reset-quota", {
+ const cleared = yield* management(hub, "reset-quota", {
auth_index: account.auth_index,
}).pipe(Effect.result);
return {
diff --git a/apps/server/src/ws.test.ts b/apps/server/src/ws.test.ts
index 4583d3f1530..d1d0d36414a 100644
--- a/apps/server/src/ws.test.ts
+++ b/apps/server/src/ws.test.ts
@@ -886,6 +886,7 @@ it.effect.each([
{},
yield* ServerBrowser.ServerBrowser,
() => Effect.void,
+ { secretReferences: true },
).pipe(Layer.provide(configRpcServices)),
),
);
@@ -930,8 +931,8 @@ it.effect.each([
key: "mod+alt+z",
command: "terminal.split",
});
- const expectedSettings = [duringDiscovery, latestSettings].map(
- ServerSettings.redactServerSettingsForClient,
+ const expectedSettings = [duringDiscovery, latestSettings].map((value) =>
+ ServerSettings.redactServerSettingsForClient(value, { secretReferences: true }),
);
const expectedKeybindings = [duringAck.keybindings, latestKeybindings.keybindings];
// The two sources can interleave, but each must retain its own publication order.
@@ -959,7 +960,7 @@ it.effect.each([
assert.deepEqual(resolved.config.remoteOpenTargets, targets);
assert.deepEqual(
resolved.config.settings,
- ServerSettings.redactServerSettingsForClient(latestSettings),
+ ServerSettings.redactServerSettingsForClient(latestSettings, { secretReferences: true }),
);
assert.deepEqual(resolved.config.keybindings, latestKeybindings.keybindings);
yield* acknowledge;
@@ -974,7 +975,9 @@ it.effect.each([
assert.deepEqual(resolved.config.directEndpoints, endpoints);
assert.deepEqual(
resolved.config.settings,
- ServerSettings.redactServerSettingsForClient(latestSettings),
+ ServerSettings.redactServerSettingsForClient(latestSettings, {
+ secretReferences: true,
+ }),
);
assert.deepEqual(resolved.config.keybindings, latestKeybindings.keybindings);
yield* acknowledge;
@@ -1104,6 +1107,7 @@ it.effect("on-demand clients get provider config without workspace snapshots", (
{},
yield* ServerBrowser.ServerBrowser,
() => Effect.void,
+ { secretReferences: true },
).pipe(Layer.provide(configRpcServices)),
),
);
diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts
index 06682ff4ae4..2bd0acc22b8 100644
--- a/apps/server/src/ws.ts
+++ b/apps/server/src/ws.ts
@@ -55,6 +55,8 @@ import {
OrchestrationGetTurnDiffError,
ORCHESTRATION_V2_WS_METHODS,
ORCHESTRATION_PROTOCOL_QUERY_PARAM,
+ CLIENT_CAPABILITIES_QUERY_PARAM,
+ SETTINGS_SECRET_REFERENCES_CAPABILITY,
ORCHESTRATION_PROTOCOL_VERSION,
OrchestrationV2DispatchCommandError,
OrchestrationV2GetShellSnapshotError,
@@ -695,6 +697,11 @@ function hasCompatibleOrchestrationProtocol(url: URL): boolean {
);
}
+function readSettingsClient(url: URL) {
+ const capabilities = url.searchParams.get(CLIENT_CAPABILITIES_QUERY_PARAM)?.split(",") ?? [];
+ return { secretReferences: capabilities.includes(SETTINGS_SECRET_REFERENCES_CAPABILITY) };
+}
+
function shouldUseBoundedThreadSnapshot(input: {
readonly acceptBoundedSnapshot?: boolean;
readonly acceptDeferredWork?: boolean;
@@ -1341,6 +1348,7 @@ export const layerWsRpc = (
clientAnalyticsProps: Readonly<Record<string, unknown>>,
serverBrowser: ServerBrowser.ServerBrowser["Service"],
reportProfiles: ServerBrowser.ProfileReporter,
+ settingsClient: { readonly secretReferences: boolean },
) =>
Layer.effectContext(
Effect.gen(function* () {
@@ -1854,6 +1862,7 @@ export const layerWsRpc = (
: commandProviders;
const settings = ServerSettings.redactServerSettingsForClient(
yield* serverSettings.getSettings,
+ settingsClient,
);
const environment = yield* serverEnvironment.getDescriptor;
const auth = yield* serverAuth.getDescriptor();
@@ -2415,13 +2424,13 @@ export const layerWsRpc = (
fresh: input.fresh,
});
}
+ if (input.reloadSecrets === true && input.instanceId === undefined) {
+ yield* invalidateSettingsSecrets(secretResolver, serverSettings.getSettings);
+ }
// Await refresh; RPC scope closure would interrupt a fork before the hub answered.
if (input.instanceId === undefined) {
yield* usageLimitSources.refresh;
}
- if (input.reloadSecrets === true && input.instanceId === undefined) {
- yield* invalidateSettingsSecrets(secretResolver, serverSettings.getSettings);
- }
let providers = yield* input.cwd !== undefined && input.instanceId !== undefined
? providerRegistry.refreshWorkspaceSnapshot({
instanceId: input.instanceId,
@@ -2629,7 +2638,7 @@ export const layerWsRpc = (
const settings = yield* providerInstanceMutation === undefined
? serverSettings.updateSettings(nextPatch)
: serverSettings.updateProviderInstance(providerInstanceMutation, nextPatch);
- return ServerSettings.redactServerSettingsForClient(settings);
+ return ServerSettings.redactServerSettingsForClient(settings, settingsClient);
}),
[WS_METHODS.serverDiscoverSourceControl]: (_input) => sourceControlDiscovery.discover,
[WS_METHODS.serverGetTraceDiagnostics]: (_input) =>
@@ -3288,7 +3297,9 @@ export const layerWsRpc = (
)
: Stream.empty;
const settingsUpdates = settingsChanges.pipe(
- Stream.map((settings) => ServerSettings.redactServerSettingsForClient(settings)),
+ Stream.map((settings) =>
+ ServerSettings.redactServerSettingsForClient(settings, settingsClient),
+ ),
Stream.map((settings) => ({
version: 1 as const,
type: "settingsUpdated" as const,
@@ -3475,7 +3486,13 @@ export const layer = Layer.unwrap(
return httpEffect;
}).pipe(
Effect.provide(
- layerWsRpc(session, clientAnalyticsProps, serverBrowser, reportProfiles).pipe(
+ layerWsRpc(
+ session,
+ clientAnalyticsProps,
+ serverBrowser,
+ reportProfiles,
+ readSettingsClient(url.value),
+ ).pipe(
Layer.provideMerge(RpcSerialization.layerJson),
// Handler-context reporters see only request defects, not server-wide defects.
Layer.provide(DefectReporter.layer),
diff --git a/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx b/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx
index e3abd8b9510..12b9bbe8208 100644
--- a/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx
+++ b/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx
@@ -1,4 +1,4 @@
-import type { EnvironmentId, UnifiedSettings } from "@t3tools/contracts";
+import type { EnvironmentId, SecretReference, UnifiedSettings } from "@t3tools/contracts";
import { useState } from "react";
import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
@@ -14,7 +14,7 @@ import {
} from "../ui/dialog";
import { Input } from "../ui/input";
import { sourceIdFromUrl } from "./AddUsageLimitSourceDialog.logic";
-import { SecretInput } from "./SecretInput";
+import { SecretSourceInput, useSecretSources } from "./SecretSourceInput";
import { Label } from "../ui/label";
/** Send the management key once to server secret storage; settings retain only a redaction marker. */
@@ -34,14 +34,16 @@ export function AddUsageLimitSourceDialog({
const updateSettings = useUpdateEnvironmentSettings(environmentId);
const [label, setLabel] = useState("");
const [url, setUrl] = useState("");
- const [managementKey, setManagementKey] = useState("");
+ const secretSources = useSecretSources(environmentId);
+ const [keyDraft, setKeyDraft] = useState<string | SecretReference>("");
+ const managementKey = typeof keyDraft === "string" ? keyDraft.trim() : keyDraft;
const trimmedUrl = url.trim();
- const canSave = trimmedUrl.length > 0 && managementKey.trim().length > 0;
+ const canSave = trimmedUrl.length > 0 && managementKey !== "";
const reset = () => {
setLabel("");
setUrl("");
- setManagementKey("");
+ setKeyDraft("");
};
const save = () => {
@@ -53,7 +55,7 @@ export function AddUsageLimitSourceDialog({
kind: "cliproxy",
...(label.trim() ? { label: label.trim() } : {}),
url: trimmedUrl,
- managementKey: managementKey.trim(),
+ managementKey,
enabled: true,
},
},
@@ -98,10 +100,15 @@ export function AddUsageLimitSourceDialog({
</div>
<div className="grid gap-1.5">
<Label htmlFor="usage-source-key">Management key</Label>
- <SecretInput
+ <SecretSourceInput
id="usage-source-key"
- value={managementKey}
- onChange={(event) => setManagementKey(event.target.value)}
+ aria-label="Management key"
+ value={keyDraft}
+ sources={secretSources}
+ onChange={(event) => setKeyDraft(event.target.value)}
+ onSecretChange={(secret) => setKeyDraft(secret ?? "")}
+ onProviderPick={() => setKeyDraft("")}
+ onReferenceReject={() => setKeyDraft("")}
/>
</div>
<div className="grid gap-1.5">
diff --git a/apps/web/src/components/settings/SecretSourceInput.browser.test.tsx b/apps/web/src/components/settings/SecretSourceInput.browser.test.tsx
index e1b67465c21..18ff271e979 100644
--- a/apps/web/src/components/settings/SecretSourceInput.browser.test.tsx
+++ b/apps/web/src/components/settings/SecretSourceInput.browser.test.tsx
@@ -23,6 +23,7 @@ async function commitReference(screen: Screen, value: string) {
describe("SecretSourceInput", () => {
it("moves a reference to another provider only once one is committed for it", async () => {
const onSecretChange = vi.fn();
+ const onProviderPick = vi.fn();
await using screen = await render(
<SecretSourceInput
aria-label="API key"
@@ -30,12 +31,14 @@ describe("SecretSourceInput", () => {
sources={sources}
onCommit={() => {}}
onSecretChange={onSecretChange}
+ onProviderPick={onProviderPick}
/>,
);
await userEvent.click(screen.getByRole("combobox", { name: "API key source" }));
await userEvent.click(screen.getByRole("option", { name: "Work" }));
expect(onSecretChange).not.toHaveBeenCalled();
+ expect(onProviderPick).toHaveBeenCalledOnce();
await expect.element(reference(screen)).toHaveValue("");
await commitReference(screen, "op://Shared/OpenAI/credential");
@@ -57,6 +60,8 @@ describe("SecretSourceInput", () => {
/>,
);
+ await commitReference(screen, "invalid-reference");
+ await expect.element(reference(screen)).toHaveAttribute("aria-invalid", "true");
await userEvent.clear(reference(screen));
await userEvent.keyboard("{Enter}");
expect(onSecretChange).toHaveBeenCalledExactlyOnceWith(undefined);
@@ -64,6 +69,7 @@ describe("SecretSourceInput", () => {
it("rejects a reference 1Password cannot read", async () => {
const onSecretChange = vi.fn();
+ const onReferenceReject = vi.fn();
await using screen = await render(
<SecretSourceInput
aria-label="API key"
@@ -71,12 +77,29 @@ describe("SecretSourceInput", () => {
sources={sources}
onCommit={() => {}}
onSecretChange={onSecretChange}
+ onReferenceReject={onReferenceReject}
/>,
);
await commitReference(screen, "Private/OpenAI/credential");
expect(onSecretChange).not.toHaveBeenCalled();
+ expect(onReferenceReject).toHaveBeenCalledOnce();
+ await screen.rerender(
+ <SecretSourceInput
+ aria-label="API key"
+ value=""
+ sources={sources}
+ onCommit={() => {}}
+ onSecretChange={onSecretChange}
+ onReferenceReject={onReferenceReject}
+ />,
+ );
await expect.element(reference(screen)).toHaveAttribute("aria-invalid", "true");
await expect.element(reference(screen)).toHaveValue("Private/OpenAI/credential");
+ await commitReference(screen, "op://Private/OpenAI/corrected");
+ expect(onSecretChange).toHaveBeenCalledExactlyOnceWith({
+ provider: personal,
+ reference: "op://Private/OpenAI/corrected",
+ });
});
});
diff --git a/apps/web/src/components/settings/SecretSourceInput.tsx b/apps/web/src/components/settings/SecretSourceInput.tsx
index 2fed001b09e..83ca9ad2ced 100644
--- a/apps/web/src/components/settings/SecretSourceInput.tsx
+++ b/apps/web/src/components/settings/SecretSourceInput.tsx
@@ -64,6 +64,10 @@ type SecretSourceInputProps = Omit<ComponentProps<typeof SecretInput>, "value" |
readonly value: string | SecretReference;
readonly sources: ReadonlyArray<SecretSource>;
readonly onSecretChange: (secret: SecretReference | undefined) => void;
+ /** Called when a provider is picked, before a reference for it exists. */
+ readonly onProviderPick?: () => void;
+ /** Called when an edited reference is rejected, so the committed one no longer matches the field. */
+ readonly onReferenceReject?: () => void;
/** Places the parts in the caller's layout; `source` is null when there is nothing to pick. */
readonly children?: (parts: {
readonly source: ReactNode;
@@ -76,6 +80,8 @@ export function SecretSourceInput({
value,
sources,
onSecretChange,
+ onProviderPick,
+ onReferenceReject,
children,
...rest
}: SecretSourceInputProps) {
@@ -106,6 +112,7 @@ export function SecretSourceInput({
return;
}
const next = value as SecretProviderId;
+ if (next !== secret?.provider) onProviderPick?.();
setPendingProvider(next === secret?.provider ? null : next);
}}
>
@@ -159,6 +166,8 @@ export function SecretSourceInput({
definition?.isReference(decoded.value.reference) === false
) {
setRejectedReference(next);
+ if (onReferenceReject !== undefined) setPendingProvider(provider);
+ onReferenceReject?.();
return;
}
setRejectedReference(null);
diff --git a/packages/client-runtime/src/connection/compatibility.test.ts b/packages/client-runtime/src/connection/compatibility.test.ts
index 3f9dc8f9924..239aed398d7 100644
--- a/packages/client-runtime/src/connection/compatibility.test.ts
+++ b/packages/client-runtime/src/connection/compatibility.test.ts
@@ -31,6 +31,7 @@ describe("orchestration protocol compatibility", () => {
expect(socketUrl.searchParams.get("orchestrationProtocol")).toBe(
String(ORCHESTRATION_PROTOCOL_VERSION),
);
+ expect(socketUrl.searchParams.get("clientCapabilities")).toBe("settingsSecretReferences");
expect(socketUrl.searchParams.get("wsTicket")).toBe("secret");
expect(socketUrl.searchParams.get("connectionMethod")).toBe("relay");
});
diff --git a/packages/client-runtime/src/connection/compatibility.ts b/packages/client-runtime/src/connection/compatibility.ts
index 3ca2e226db9..fccc68bff75 100644
--- a/packages/client-runtime/src/connection/compatibility.ts
+++ b/packages/client-runtime/src/connection/compatibility.ts
@@ -1,6 +1,8 @@
import {
+ CLIENT_CAPABILITIES_QUERY_PARAM,
ORCHESTRATION_PROTOCOL_QUERY_PARAM,
ORCHESTRATION_PROTOCOL_VERSION,
+ SETTINGS_SECRET_REFERENCES_CAPABILITY,
type ExecutionEnvironmentDescriptor,
} from "@t3tools/contracts";
@@ -37,5 +39,6 @@ function canSelfUpdate(descriptor: ExecutionEnvironmentDescriptor): boolean {
export function appendOrchestrationProtocol(socketUrl: string): string {
const url = new URL(socketUrl);
url.searchParams.set(ORCHESTRATION_PROTOCOL_QUERY_PARAM, String(ORCHESTRATION_PROTOCOL_VERSION));
+ url.searchParams.set(CLIENT_CAPABILITIES_QUERY_PARAM, SETTINGS_SECRET_REFERENCES_CAPABILITY);
return url.toString();
}
diff --git a/packages/client-runtime/src/connection/resolver.test.ts b/packages/client-runtime/src/connection/resolver.test.ts
index b70b2ef1761..82a2aafa001 100644
--- a/packages/client-runtime/src/connection/resolver.test.ts
+++ b/packages/client-runtime/src/connection/resolver.test.ts
@@ -202,7 +202,7 @@ describe("ConnectionResolver", () => {
label: "Primary",
httpBaseUrl: "http://127.0.0.1:3777",
socketUrl:
- "ws://127.0.0.1:3777/ws?clientSurface=web&clientDeviceType=desktop&connectionMethod=direct&orchestrationProtocol=2",
+ "ws://127.0.0.1:3777/ws?clientSurface=web&clientDeviceType=desktop&connectionMethod=direct&orchestrationProtocol=2&clientCapabilities=settingsSecretReferences",
httpAuthorization: null,
target,
});
diff --git a/packages/contracts/src/environment.ts b/packages/contracts/src/environment.ts
index f9cd98311d1..9ac89ef499b 100644
--- a/packages/contracts/src/environment.ts
+++ b/packages/contracts/src/environment.ts
@@ -15,6 +15,11 @@ export const ORCHESTRATION_PROTOCOL_VERSION_TEXT = "2";
export const ORCHESTRATION_PROTOCOL_QUERY_PARAM = "orchestrationProtocol";
export const ORCHESTRATION_PROTOCOL_HEADER = "x-t3-orchestration-protocol";
+/** Wire shapes a client understands, comma-separated; a client that omits one gets the older shape. */
+export const CLIENT_CAPABILITIES_QUERY_PARAM = "clientCapabilities";
+/** Secret fields may hold a secret provider reference instead of a string. */
+export const SETTINGS_SECRET_REFERENCES_CAPABILITY = "settingsSecretReferences";
+
export const ExecutionEnvironmentPlatformOs = Schema.Literals([
"darwin",
"linux",
diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts
index cf8e6effe05..7f6e8a4aea8 100644
--- a/packages/contracts/src/settings.ts
+++ b/packages/contracts/src/settings.ts
@@ -22,6 +22,7 @@ import {
import { UsageLimitSourceId } from "./usageLimitSourceId.ts";
import { UsageModelFilter } from "./usage.ts";
import { SecretProviderId, SecretProviderSlug } from "./secretReference.ts";
+import { SettingsSecretValue } from "./settingsSecretValue.ts";
import { EnvironmentMachineKind, ThreadEnvMode, WorktreeSubmodules } from "./environment.ts";
import { KeybindingShortcut } from "./keybindings.ts";
import {
@@ -858,7 +859,10 @@ export const UsageLimitSourceConfig = Schema.Struct({
kind: Schema.Literal("cliproxy"),
label: Schema.optional(TrimmedNonEmptyString),
url: TrimmedNonEmptyString,
- managementKey: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed("")), secretSetting),
+ managementKey: SettingsSecretValue.pipe(
+ Schema.withDecodingDefault(Effect.succeed("")),
+ secretSetting,
+ ),
enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
});
export type UsageLimitSourceConfig = typeof UsageLimitSourceConfig.Type;