Skip to content
TypeScript

feat(settings): Simlock, Antigravity and OpenCode credentials can come from a secret provider (#31652)

Julius Marminge4a94658ab477
diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts
index c860f652c4f..ffe652b6a77 100644
--- a/apps/server/src/device/DeviceService.ts
+++ b/apps/server/src/device/DeviceService.ts
@@ -70,6 +70,7 @@ import { readDeviceDetail, runDeviceAction } from "./DeviceActions.ts";
import * as ProcessRunner from "../processRunner.ts";
import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
import * as SecretResolverLayer from "../secrets/SecretResolver.ts";
+import { requireSettingsSecret } from "../secrets/settingsSecretValue.ts";
import * as DeviceHost from "./DeviceHost.ts";
import * as SshDeviceHost from "./SshDeviceHost.ts";
import * as EasDeviceHost from "./EasDeviceHost.ts";
@@ -1594,8 +1595,11 @@ export const make = Effect.gen(function* () {
(host) => host.id === input.hostId,
);
const token = input.agentToken
- ? { agentToken: input.agentToken, source: "entered" as const }
- : SimlockDeviceHost.resolveSimlockToken(gatewayUrl, savedHost, environment);
+ ? {
+ agentToken: yield* requireSettingsSecret(secrets, input.agentToken),
+ source: "entered" as const,
+ }
+ : yield* SimlockDeviceHost.resolveSimlockToken(secrets, gatewayUrl, savedHost, environment);
if (!token)
return yield* new DeviceSimlockIdentityError({
reason: savedHost?.agentToken
@@ -1623,6 +1627,8 @@ export const make = Effect.gen(function* () {
Effect.catchTags({
ServerSettingsError: () =>
Effect.fail(new DeviceSimlockIdentityError({ reason: "Could not read the saved token." })),
+ SettingsSecretUnresolvedError: (error) =>
+ Effect.fail(new DeviceSimlockIdentityError({ reason: error.message })),
}),
Effect.provide(simlockContext),
);
diff --git a/apps/server/src/device/SimlockDeviceHost.test.ts b/apps/server/src/device/SimlockDeviceHost.test.ts
index 1b670a06cd0..6fc5df3e562 100644
--- a/apps/server/src/device/SimlockDeviceHost.test.ts
+++ b/apps/server/src/device/SimlockDeviceHost.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from "@effect/vitest";
import * as NodeServices from "@effect/platform-node/NodeServices";
-import type { SimlockDeviceHostConfig } from "@t3tools/contracts";
+import { SecretProviderId, type SimlockDeviceHostConfig } from "@t3tools/contracts";
+import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
@@ -143,9 +144,22 @@ const fakePool = (
};
});
-const layer = Layer.mergeAll(
- ServerConfig.layerTest(process.cwd(), { prefix: "t3-simlock-host-" }),
-).pipe(Layer.provideMerge(NodeServices.layer));
+const makeSecretResolver = (values: Record<string, string> = {}) =>
+ Layer.succeed(SecretResolver.SecretResolver, {
+ read: (references) =>
+ Effect.succeed(references.map((reference) => values[reference.reference])),
+ invalidate: () => Effect.void,
+ });
+
+const layerWith = (secrets: Layer.Layer<SecretResolver.SecretResolver>) =>
+ Layer.mergeAll(
+ ServerConfig.layerTest(process.cwd(), { prefix: "t3-simlock-host-" }),
+ secrets,
+ ).pipe(Layer.provideMerge(NodeServices.layer));
+
+const layer = layerWith(makeSecretResolver());
+
+const tokenSource = { provider: SecretProviderId.make("homelab"), reference: "simlock#token" };
const makeHost = (
pool: Effect.Success<ReturnType<typeof fakePool>>,
@@ -296,7 +310,8 @@ describe("Simlock device host", () => {
});
expect(
- SimlockDeviceHost.resolveSimlockToken(
+ yield* SimlockDeviceHost.resolveSimlockToken(
+ yield* SecretResolver.SecretResolver,
"https://elsewhere.example:4700",
config({ agentToken: "saved-token" }),
{},
@@ -317,6 +332,41 @@ describe("Simlock device host", () => {
}).pipe(Effect.scoped, Effect.provide(layer)),
);
+ it.effect("reads its agent token from the secret provider the setting names", () =>
+ Effect.gen(function* () {
+ const pool = yield* fakePool();
+ const scope = yield* Scope.make();
+ const host = yield* makeHost(pool, config({ agentToken: tokenSource }), scope);
+ const ready = yield* host.ensureReady(() => Effect.void);
+ const [iphone] = (yield* ready.devices.list).devices;
+ const leaseId = yield* ready.devices.boot(iphone!);
+ expect(yield* ready.devices.agentEndpoint!(leaseId)).toMatchObject({ token: "agent-token" });
+ yield* Scope.close(scope, Exit.void);
+ expect(pool.ran("close")).toHaveLength(1);
+ }).pipe(
+ Effect.scoped,
+ Effect.provide(layerWith(makeSecretResolver({ "simlock#token": "agent-token" }))),
+ ),
+ );
+
+ it.effect("fails instead of using this machine's token when its secret cannot be read", () =>
+ Effect.gen(function* () {
+ const pool = yield* fakePool();
+ const scope = yield* Scope.make();
+ const host = yield* makeHost(pool, config({ agentToken: tokenSource }), scope).pipe(
+ Effect.provideService(HostProcess.Environment, {
+ AGENT_DEVICE_DAEMON_BASE_URL: `${GATEWAY}/v1/agent-device`,
+ AGENT_DEVICE_DAEMON_AUTH_TOKEN: "agent-token",
+ }),
+ );
+ const availability = yield* host.platformAvailability("ios");
+ expect(availability.available).toBe(false);
+ expect(availability.reason).toContain('secret provider "homelab"');
+ expect(pool.requests).toEqual([]);
+ yield* Scope.close(scope, Exit.void);
+ }).pipe(Effect.scoped, Effect.provide(layer)),
+ );
+
it.effect("lists a platform again as soon as its workers return", () =>
Effect.gen(function* () {
const pool = yield* fakePool();
diff --git a/apps/server/src/device/SimlockDeviceHost.ts b/apps/server/src/device/SimlockDeviceHost.ts
index 75476f0333c..85fd399d1e9 100644
--- a/apps/server/src/device/SimlockDeviceHost.ts
+++ b/apps/server/src/device/SimlockDeviceHost.ts
@@ -11,6 +11,7 @@ import {
DeviceOperationError,
type DevicePlatform,
type DevicePlatformAvailability,
+ type SettingsSecretValue,
type SimlockDeviceHostConfig,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
@@ -28,8 +29,10 @@ import { HttpClient, HttpClientError, HttpClientRequest, HttpClientResponse } fr
import * as HostProcess from "@t3tools/shared/HostProcess";
import type { NodeRuntimeUnavailableError } from "@t3tools/shared/nodeRuntime";
+import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
import * as ServerConfig from "../config.ts";
import * as ProcessRunner from "../processRunner.ts";
+import { requireSettingsSecret } from "../secrets/settingsSecretValue.ts";
import { writeAgentDeviceConfig } from "./AgentDeviceTarget.ts";
import * as DeviceHost from "./DeviceHost.ts";
@@ -75,6 +78,8 @@ interface Lease {
readonly dir: string;
readonly configPath: string;
readonly installDirectory: string;
+ /** The token the lease was opened with; ending it must not depend on reading the secret again. */
+ readonly token: string;
}
const catalogId = (platform: DevicePlatform, model: string) => `simlock:${platform}:${model}`;
@@ -108,20 +113,25 @@ export const machineSimlockToken = (environment: NodeJS.ProcessEnv) => {
/**
* The saved token, else this machine's agent-device token; each only for the gateway it belongs to,
- * so a URL being edited never receives either.
+ * so a URL being edited never receives either. A saved secret reference that cannot be read fails
+ * instead of falling back to this machine's token.
*/
-export const resolveSimlockToken = (
+export const resolveSimlockToken = Effect.fn("SimlockDeviceHost.resolveToken")(function* (
+ secrets: SecretResolver.SecretResolver["Service"],
gatewayUrl: string,
- saved: { readonly gatewayUrl: string; readonly agentToken: string } | undefined,
+ saved: { readonly gatewayUrl: string; readonly agentToken: SettingsSecretValue } | undefined,
environment: NodeJS.ProcessEnv,
-) => {
+) {
if (saved?.agentToken && sameOrigin(saved.gatewayUrl, gatewayUrl))
- return { agentToken: saved.agentToken, source: "settings" as const };
+ return {
+ agentToken: yield* requireSettingsSecret(secrets, saved.agentToken),
+ source: "settings" as const,
+ };
const machine = machineSimlockToken(environment);
return machine && sameOrigin(machine.gatewayUrl, gatewayUrl)
? { agentToken: machine.agentToken, source: "env" as const }
: null;
-};
+});
/** What went wrong talking to a gateway, without the gateway's own text. */
export const describeGatewayFailure = (cause: unknown, gatewayUrl: string, tokenOwner: string) => {
@@ -181,9 +191,9 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
const server = yield* ServerConfig.ServerConfig;
const services = yield* Effect.context<FileSystem.FileSystem | Path.Path>();
const environment = yield* HostProcess.Environment;
+ const secrets = yield* SecretResolver.SecretResolver;
const hostId = config.id;
const gateway = config.gatewayUrl.replace(/\/$/, "");
- const agentToken = resolveSimlockToken(config.gatewayUrl, config, environment)?.agentToken ?? "";
const agentDeviceUrl = `${gateway}/v1/agent-device`;
const hostDir = path.join(server.stateDir, "device", "simlock", hostId);
const leases = new Map<string, Lease>();
@@ -209,7 +219,18 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
...(cause === undefined ? {} : { cause }),
});
+ // Read on each use, so a rotated or unlocked secret applies without rebuilding the host.
+ const currentToken = resolveSimlockToken(secrets, config.gatewayUrl, config, environment).pipe(
+ Effect.mapError((error) => unavailable(error.message, error)),
+ Effect.flatMap((token) =>
+ token
+ ? Effect.succeed(token.agentToken)
+ : Effect.fail(unavailable(`Add an agent token for ${config.label} in Settings.`)),
+ ),
+ );
+
const request = (
+ agentToken: string,
method: "GET" | "POST" | "DELETE",
route: string,
body?: Readonly<Record<string, unknown>>,
@@ -284,10 +305,8 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
const fetchCatalog = Effect.gen(function* () {
const now = yield* Clock.currentTimeMillis;
if (catalog && now - catalog.at < CATALOG_TTL_MS) return catalog.devices;
- if (!agentToken) {
- return yield* unavailable(`Add an agent token for ${config.label} in Settings.`);
- }
- const decoded = yield* request("GET", "/v1/catalog").pipe(
+ const agentToken = yield* currentToken;
+ const decoded = yield* request(agentToken, "GET", "/v1/catalog").pipe(
Effect.flatMap(decodeCatalog),
Effect.mapError((cause) =>
unavailable(describeGatewayFailure(cause, gateway, config.label), cause),
@@ -357,7 +376,9 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
/** Ends the lease through its session, or over HTTP when the session is gone. */
const release = Effect.fn("SimlockDeviceHost.release")(function* (lease: Lease) {
yield* agentDevice(lease, ["close"]).pipe(
- Effect.catch(() => request("DELETE", `/v1/leases/${encodeURIComponent(lease.leaseId)}`)),
+ Effect.catch(() =>
+ request(lease.token, "DELETE", `/v1/leases/${encodeURIComponent(lease.leaseId)}`),
+ ),
Effect.tapError((cause) =>
Effect.logWarning("Could not release Simlock lease", { leaseId: lease.leaseId, cause }),
),
@@ -376,6 +397,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
yield* Effect.sleep(Duration.millis(Math.min(left, Duration.toMillis(RENEW_INTERVAL))));
if ((yield* Clock.currentTimeMillis) >= deadline) break;
const renewed = yield* request(
+ lease.token,
"POST",
`/v1/leases/${encodeURIComponent(lease.leaseId)}/renew`,
{ ttlMs: LEASE_TTL_MS },
@@ -429,6 +451,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
const session = `t3-${suffix}`;
const dir = path.join(hostDir, suffix);
const configPath = path.join(dir, "agent-device.json");
+ const agentToken = yield* currentToken;
yield* writeAgentDeviceConfig(configPath, {
baseUrl: agentDeviceUrl,
token: agentToken,
@@ -465,6 +488,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
dir,
configPath,
installDirectory,
+ token: agentToken,
};
if (stops !== stopsAtOpen)
return yield* unavailable("Device support stopped while the device was opening.");
@@ -557,7 +581,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
onSome: (lease) =>
Effect.succeed({
baseUrl: agentDeviceUrl,
- token: agentToken,
+ token: lease.token,
agentDeviceId: null,
session: lease.session,
stateDir: lease.dir,
@@ -588,7 +612,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
if (!lease) return null;
return {
url: `${gateway}/v1/leases/${encodeURIComponent(lease.leaseId)}/${stream ? "stream" : "input"}`,
- headers: { authorization: `Bearer ${agentToken}` },
+ headers: { authorization: `Bearer ${lease.token}` },
protocols: [],
};
});
diff --git a/apps/server/src/provider/Drivers/AntigravityDriver.ts b/apps/server/src/provider/Drivers/AntigravityDriver.ts
index a8f97b0f147..cad62882e68 100644
--- a/apps/server/src/provider/Drivers/AntigravityDriver.ts
+++ b/apps/server/src/provider/Drivers/AntigravityDriver.ts
@@ -1,5 +1,10 @@
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
-import { AntigravitySettings, ProviderDriverKind, ProviderSetupError } from "@t3tools/contracts";
+import {
+ AntigravitySettings,
+ ProviderDriverKind,
+ ProviderSetupError,
+ settingsSecretLiteral,
+} from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime";
import {
@@ -121,7 +126,8 @@ export const AntigravityDriver: ProviderDriver<
const settings = { ...config, enabled } satisfies AntigravitySettings;
const auth: AntigravityAuthConfig = {
authMethod: settings.authMethod,
- apiKey: settings.apiKey,
+ // The instance registry has already read a referenced key from its secret provider.
+ apiKey: settingsSecretLiteral(settings.apiKey),
gcpProject: settings.gcpProject,
gcpLocation: settings.gcpLocation,
};
diff --git a/apps/server/src/provider/ProviderInstanceRegistry.test.ts b/apps/server/src/provider/ProviderInstanceRegistry.test.ts
index c7ea68d585d..14f58c9ffe1 100644
--- a/apps/server/src/provider/ProviderInstanceRegistry.test.ts
+++ b/apps/server/src/provider/ProviderInstanceRegistry.test.ts
@@ -8,6 +8,7 @@ import { vi } from "vite-plus/test";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import {
+ AntigravitySettings,
EnvironmentId,
type ClaudeSettings,
type CodexSettings,
@@ -20,6 +21,7 @@ import {
type SecretReference,
} from "@t3tools/contracts";
import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions";
+import { OpenCodeSettings } from "@t3tools/provider-opencode/settings";
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as DateTime from "effect/DateTime";
@@ -896,8 +898,12 @@ describe("ProviderInstanceRegistry secret environment", () => {
}).pipe(Effect.scoped);
});
- it.effect("marks an instance unavailable and names the variable it could not read", () => {
- const fixture = setup({});
+ // An empty secret must not let a driver fall back to a credential the server inherited.
+ it.effect.each([
+ ["missing", {}],
+ ["empty", { [secret.reference]: " " }],
+ ] as const)("marks an instance unavailable when its secret is %s", ([, initial]) => {
+ const fixture = setup(initial);
return Effect.gen(function* () {
const { registry, mutator } = yield* fixture.make;
const readsAtStartup = fixture.reads.length;
@@ -970,6 +976,22 @@ describe("ProviderInstanceRegistry secret environment", () => {
}).pipe(Effect.scoped);
});
+ it.effect.each([
+ { name: "Antigravity apiKey", schema: AntigravitySettings, field: "apiKey" },
+ { name: "OpenCode serverPassword", schema: OpenCodeSettings, field: "serverPassword" },
+ ])("hands the driver the secret its $name names", ({ schema, field }) => {
+ const reference: SecretReference = { ...secret, reference: "op://Private/Provider/key" };
+ const fixture = setup(
+ { [secret.reference]: "sk-live", [reference.reference]: "provider-live" },
+ schema,
+ { enabled: true, [field]: reference },
+ );
+ return Effect.gen(function* () {
+ yield* fixture.make;
+ expect(fixture.configs).toEqual([expect.objectContaining({ [field]: "provider-live" })]);
+ }).pipe(Effect.scoped);
+ });
+
it.effect("keeps a working instance live when a reload cannot read its secret", () => {
const fixture = setup({ [secret.reference]: "sk-live" });
return Effect.gen(function* () {
diff --git a/apps/server/src/provider/ProviderInstanceRegistry.ts b/apps/server/src/provider/ProviderInstanceRegistry.ts
index da1fd687f48..3f4df0eb35d 100644
--- a/apps/server/src/provider/ProviderInstanceRegistry.ts
+++ b/apps/server/src/provider/ProviderInstanceRegistry.ts
@@ -106,9 +106,14 @@ const readInstanceSecrets = Effect.fn("ProviderInstanceRegistry.readInstanceSecr
references.length === 0 ? [] : yield* (yield* SecretResolver.SecretResolver).read(references);
const unreadable: Array<string> = [];
let index = 0;
+ // An empty secret reads as unreadable, so a driver cannot fall back to an inherited credential.
+ const next = () => {
+ const secret = values[index++];
+ return secret === undefined || secret.trim() === "" ? undefined : secret;
+ };
const environment: ResolvedProviderInstanceEnvironment = variables.flatMap((variable) => {
if (typeof variable.value === "string") return [{ ...variable, value: variable.value }];
- const secret = values[index++];
+ const secret = next();
if (secret === undefined) {
unreadable.push(variable.name);
return [];
@@ -116,7 +121,7 @@ const readInstanceSecrets = Effect.fn("ProviderInstanceRegistry.readInstanceSecr
return [{ ...variable, value: secret, sensitive: true }];
});
const config = configFields.flatMap(({ field, key }) => {
- const secret = values[index++];
+ const secret = next();
if (secret !== undefined) return [{ field, key, value: secret }];
unreadable.push(field);
return [];
diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts
index 0e7ce34df0e..622c7f55f0c 100644
--- a/apps/server/src/serverSettings.test.ts
+++ b/apps/server/src/serverSettings.test.ts
@@ -1626,6 +1626,20 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
provider: SecretProviderId.make("personal"),
reference: "op://Private/Expo/token",
};
+ const simlockReference = {
+ provider: SecretProviderId.make("personal"),
+ reference: "op://Private/Simlock/token",
+ };
+ const antigravityReference = {
+ provider: SecretProviderId.make("personal"),
+ reference: "op://Private/Gemini/key",
+ };
+ const openCodeReference = {
+ provider: SecretProviderId.make("personal"),
+ reference: "op://Private/OpenCode/password",
+ };
+ const antigravity = ProviderInstanceId.make("antigravity");
+ const openCode = ProviderInstanceId.make("opencode");
const hub = UsageLimitSourceId.make("hub");
const withUsage = yield* serverSettings.updateSettings({
usageLimitSources: {
@@ -1647,6 +1661,27 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
maxConcurrentSessions: 2,
},
],
+ simlockDeviceHosts: [
+ {
+ kind: "simlock",
+ id: "pool",
+ label: "Device pool",
+ gatewayUrl: "https://simlock.test:4700",
+ agentToken: simlockReference,
+ maxLeases: 2,
+ maxLeaseMinutes: 60,
+ },
+ ],
+ providerInstances: {
+ [antigravity]: {
+ driver: ProviderDriverKind.make("antigravity"),
+ config: { authMethod: "gemini-api-key", apiKey: antigravityReference },
+ },
+ [openCode]: {
+ driver: ProviderDriverKind.make("opencode"),
+ config: { serverUrl: "http://127.0.0.1:4096", serverPassword: openCodeReference },
+ },
+ },
});
const legacy = ServerSettingsModule.redactServerSettingsForClient(withUsage, {
secretReferences: false,
@@ -1655,10 +1690,17 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
assert.equal(legacy.sourceControlHosts.bitbucket?.accessToken, SECRET_REDACTED);
assert.equal(legacy.usageLimitSources[hub]?.managementKey, SECRET_REDACTED);
assert.equal(legacy.easDeviceHosts[0]?.accessToken, SECRET_REDACTED);
+ assert.equal(legacy.simlockDeviceHosts[0]?.agentToken, SECRET_REDACTED);
+ const configOf = (settings: ServerSettings, id: ProviderInstanceId) =>
+ settings.providerInstances[id]?.config as Record<string, unknown> | undefined;
+ assert.equal(configOf(legacy, antigravity)?.apiKey, SECRET_REDACTED);
+ assert.equal(configOf(legacy, openCode)?.serverPassword, SECRET_REDACTED);
const echoed = yield* serverSettings.updateSettings({
sourceControlHosts: legacy.sourceControlHosts,
usageLimitSources: legacy.usageLimitSources,
easDeviceHosts: legacy.easDeviceHosts,
+ simlockDeviceHosts: legacy.simlockDeviceHosts,
+ providerInstances: legacy.providerInstances,
});
assert.deepEqual(echoed.sourceControlHosts.bitbucket?.accessToken, reference);
assert.deepEqual(echoed.sourceControlHosts.github?.tokens, {
@@ -1667,6 +1709,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
});
assert.deepEqual(echoed.usageLimitSources[hub]?.managementKey, usageReference);
assert.deepEqual(echoed.easDeviceHosts[0]?.accessToken, easReference);
+ assert.deepEqual(echoed.simlockDeviceHosts[0]?.agentToken, simlockReference);
+ assert.deepEqual(configOf(echoed, antigravity)?.apiKey, antigravityReference);
+ assert.deepEqual(configOf(echoed, openCode)?.serverPassword, openCodeReference);
assert.isTrue(Option.isSome(yield* stored("github", "tokens", "ghe.acme.test")));
}).pipe(Effect.provide(layerServerSettingsWithSecrets())),
);
@@ -1676,6 +1721,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings"
// Shipped clients decode these as strings. Each must be covered by the older-client case above.
assert.deepEqual(strictSecretReferencePaths(ServerSettings.ast), [
"easDeviceHosts.[].accessToken",
+ "simlockDeviceHosts.[].agentToken",
"usageLimitSources.*.managementKey",
]);
}),
diff --git a/apps/server/src/sourceControl/sourceControlHostSecrets.test.ts b/apps/server/src/sourceControl/sourceControlHostSecrets.test.ts
index 974fc7cc91c..45c156895f4 100644
--- a/apps/server/src/sourceControl/sourceControlHostSecrets.test.ts
+++ b/apps/server/src/sourceControl/sourceControlHostSecrets.test.ts
@@ -150,8 +150,20 @@ it.effect("reads a collected reference again once it is invalidated", () =>
accessToken: source("expo#token"),
},
];
+ const simlockDeviceHosts = [
+ {
+ kind: "simlock" as const,
+ id: DeviceHostId.make("pool"),
+ label: "Pool",
+ gatewayUrl: "https://simlock.test:4700",
+ agentToken: source("simlock#token"),
+ maxLeases: 2,
+ maxLeaseMinutes: 60,
+ },
+ ];
cache.set("cliproxy#key", "stale");
cache.set("expo#token", "stale");
+ cache.set("simlock#token", "stale");
assert.deepEqual((yield* read()).bitbucket, {
accessToken: "stale",
@@ -159,10 +171,16 @@ it.effect("reads a collected reference again once it is invalidated", () =>
current = "rotated";
yield* invalidateSettingsSecrets(
resolver,
- Effect.succeed({ sourceControlHosts: hosts, usageLimitSources, easDeviceHosts }),
+ Effect.succeed({
+ sourceControlHosts: hosts,
+ usageLimitSources,
+ easDeviceHosts,
+ simlockDeviceHosts,
+ }),
);
assert.isFalse(cache.has("cliproxy#key"));
assert.isFalse(cache.has("expo#token"));
+ assert.isFalse(cache.has("simlock#token"));
assert.deepEqual((yield* read()).bitbucket, {
accessToken: "rotated",
diff --git a/apps/server/src/sourceControl/sourceControlHostSecrets.ts b/apps/server/src/sourceControl/sourceControlHostSecrets.ts
index 21213d27bdb..ba5a868e079 100644
--- a/apps/server/src/sourceControl/sourceControlHostSecrets.ts
+++ b/apps/server/src/sourceControl/sourceControlHostSecrets.ts
@@ -104,16 +104,22 @@ const collectUsageLimitSecretReferences = (
isSecretReference(managementKey) ? [managementKey] : [],
);
-const collectEasSecretReferences = (
- hosts: ServerSettings["easDeviceHosts"],
+const collectDeviceHostSecretReferences = (
+ settings: Pick<ServerSettings, "easDeviceHosts" | "simlockDeviceHosts">,
): ReadonlyArray<SecretReference> =>
- hosts.flatMap(({ accessToken }) => (isSecretReference(accessToken) ? [accessToken] : []));
+ [
+ ...settings.easDeviceHosts.map(({ accessToken }) => accessToken),
+ ...settings.simlockDeviceHosts.map(({ agentToken }) => agentToken),
+ ].filter(isSecretReference);
/** 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" | "usageLimitSources" | "easDeviceHosts">,
+ Pick<
+ ServerSettings,
+ "sourceControlHosts" | "usageLimitSources" | "easDeviceHosts" | "simlockDeviceHosts"
+ >,
E
>,
) {
@@ -121,7 +127,7 @@ export const invalidateSettingsSecrets = Effect.fn("SettingsSecrets.invalidate")
Effect.map((current) => [
...collectSourceControlHostSecretReferences(current.sourceControlHosts),
...collectUsageLimitSecretReferences(current.usageLimitSources),
- ...collectEasSecretReferences(current.easDeviceHosts),
+ ...collectDeviceHostSecretReferences(current),
]),
Effect.orElseSucceed(() => undefined),
);
diff --git a/apps/web/src/components/settings/SimlockDeviceHostEditor.tsx b/apps/web/src/components/settings/SimlockDeviceHostEditor.tsx
index 3498492d59c..9037508b279 100644
--- a/apps/web/src/components/settings/SimlockDeviceHostEditor.tsx
+++ b/apps/web/src/components/settings/SimlockDeviceHostEditor.tsx
@@ -1,6 +1,11 @@
import { useAtomValue } from "@effect/atom-react";
import { useId, useState, type ReactNode } from "react";
-import type { EnvironmentId, SimlockDeviceHostConfig } from "@t3tools/contracts";
+import {
+ isSecretReference,
+ type EnvironmentId,
+ type SettingsSecretValue,
+ type SimlockDeviceHostConfig,
+} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Option from "effect/Option";
import { AsyncResult, Atom } from "effect/reactivity";
@@ -21,7 +26,7 @@ import {
} from "../ui/dialog";
import { Input } from "../ui/input";
import { Spinner } from "../ui/spinner";
-import { SecretInput } from "./SecretInput";
+import { SecretSourceInput, useSecretSources } from "./SecretSourceInput";
import {
planSimlockDeviceHostSave,
simlockDeviceHostProblem,
@@ -67,8 +72,9 @@ export function SimlockDeviceHostEditor(props: {
.map((result) => Option.getOrUndefined(AsyncResult.value(result))?.gatewayUrl)
.find(Boolean);
const gatewayUrl = urlEdited || draft.gatewayUrl ? draft.gatewayUrl : (detectedUrl ?? "");
- const savedToken = (environment: SimlockHostEnvironment) =>
- environment.hosts.some((host) => host.id === props.host.id && host.agentToken !== "");
+ const savedValue = (environment: SimlockHostEnvironment) =>
+ environment.hosts.find((host) => host.id === props.host.id)?.agentToken ?? "";
+ const savedToken = (environment: SimlockHostEnvironment) => savedValue(environment) !== "";
const [tokens, setTokens] = useState<ReadonlyMap<EnvironmentId, SimlockTokenEntry>>(
() =>
new Map(
@@ -84,8 +90,15 @@ export function SimlockDeviceHostEditor(props: {
gatewayUrl: gatewayUrl.trim(),
};
const checkUrl = simlockDeviceHostProblem(next) === null ? next.gatewayUrl : null;
+ // Environments whose token field holds a secret reference that cannot be saved yet.
+ const [invalidTokens, setInvalidTokens] = useState<ReadonlySet<EnvironmentId>>(new Set());
const plan = planSimlockDeviceHostSave(props.environments, next, tokens);
- const problem = "problem" in plan ? plan.problem : null;
+ const problem =
+ "problem" in plan
+ ? plan.problem
+ : invalidTokens.size > 0
+ ? "Enter a valid secret reference."
+ : null;
const several = props.environments.length > 1;
const limit = (
@@ -171,11 +184,19 @@ export function SimlockDeviceHostEditor(props: {
gatewayUrl={checkUrl}
label={several ? `Agent token · ${environment.label}` : "Agent token"}
token={tokens.get(environment.environmentId) ?? { kind: "cleared" }}
- hasSaved={savedToken(environment)}
+ saved={savedValue(environment)}
busy={props.busy}
onChange={(token) =>
setTokens((current) => new Map(current).set(environment.environmentId, token))
}
+ onValidityChange={(valid) =>
+ setInvalidTokens((current) => {
+ const nextInvalid = new Set(current);
+ if (valid) nextInvalid.delete(environment.environmentId);
+ else nextInvalid.add(environment.environmentId);
+ return nextInvalid;
+ })
+ }
/>
))}
<p className="text-xs text-muted-foreground">
@@ -220,12 +241,23 @@ function SimlockTokenField(props: {
gatewayUrl: string | null;
label: ReactNode;
token: SimlockTokenEntry;
- hasSaved: boolean;
+ /** This environment's saved token: redacted when a literal, shown when a secret reference. */
+ saved: SettingsSecretValue;
busy: boolean;
onChange: (token: SimlockTokenEntry) => void;
+ onValidityChange: (valid: boolean) => void;
}) {
const id = useId();
- const entered = props.token.kind === "new" ? props.token.value.trim() : "";
+ const sources = useSecretSources(props.environmentId);
+ const [inputKey, setInputKey] = useState(0);
+ // Emptying the field or leaving a reference returns to a saved literal, never to a saved reference.
+ const savedLiteral = typeof props.saved === "string" && props.saved !== "";
+ const entered =
+ props.token.kind !== "new"
+ ? ""
+ : typeof props.token.value === "string"
+ ? props.token.value.trim()
+ : props.token.value;
// A cleared field asks about the machine's own token, which a blank one falls back to.
const check = useAtomValue(
props.gatewayUrl === null
@@ -265,20 +297,38 @@ function SimlockTokenField(props: {
{props.label}
</label>
<div className="flex items-center gap-2">
- <SecretInput
+ <SecretSourceInput
+ key={inputKey}
id={id}
+ aria-label="Agent token"
className="min-w-0 flex-1"
- configured={props.token.kind === "saved"}
- value={props.token.kind === "new" ? props.token.value : ""}
+ configured={props.token.kind === "saved" && typeof props.saved === "string"}
+ value={
+ props.token.kind === "new"
+ ? props.token.value
+ : props.token.kind === "saved" && isSecretReference(props.saved)
+ ? props.saved
+ : ""
+ }
+ sources={sources}
+ onValidityChange={props.onValidityChange}
+ onSecretChange={(secret) =>
+ props.onChange(
+ secret
+ ? { kind: "new", value: secret }
+ : savedLiteral
+ ? { kind: "saved" }
+ : { kind: "cleared" },
+ )
+ }
disabled={props.busy}
onChange={(event) => {
const value = event.target.value;
- // Emptying the field is not clearing the saved token; the Clear
- // button is, so a saved token comes back here.
+ // Emptying the field is not clearing the saved token; the Clear button is.
props.onChange(
value
? { kind: "new", value }
- : props.hasSaved
+ : savedLiteral
? { kind: "saved" }
: { kind: "cleared" },
);
@@ -291,7 +341,11 @@ function SimlockTokenField(props: {
variant="ghost-destructive"
aria-label="Clear agent token"
disabled={props.busy}
- onClick={() => props.onChange({ kind: "cleared" })}
+ onClick={() => {
+ props.onChange({ kind: "cleared" });
+ props.onValidityChange(true);
+ setInputKey((key) => key + 1);
+ }}
>
<XIcon className="size-3.5" />
</Button>
diff --git a/apps/web/src/components/settings/simlockDeviceHostsSettings.logic.test.ts b/apps/web/src/components/settings/simlockDeviceHostsSettings.logic.test.ts
index 2efe3c78c24..5a28f0a863b 100644
--- a/apps/web/src/components/settings/simlockDeviceHostsSettings.logic.test.ts
+++ b/apps/web/src/components/settings/simlockDeviceHostsSettings.logic.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vite-plus/test";
-import type { EnvironmentId } from "@t3tools/contracts";
+import { SecretProviderId, type EnvironmentId } from "@t3tools/contracts";
import {
newSimlockDeviceHost,
@@ -68,5 +68,15 @@ describe("Simlock device host settings", () => {
{ environmentId: cups, hosts: [{ ...draft, agentToken: "cups-token" }] },
],
});
+ const reference = { provider: SecretProviderId.make("homelab"), reference: "simlock#token" };
+ expect(
+ planSimlockDeviceHostSave(
+ environments,
+ draft,
+ new Map([[cups, { kind: "new", value: reference }]]),
+ ),
+ ).toMatchObject({
+ patches: [{}, { environmentId: cups, hosts: [{ ...draft, agentToken: reference }] }],
+ });
});
});
diff --git a/apps/web/src/components/settings/simlockDeviceHostsSettings.logic.ts b/apps/web/src/components/settings/simlockDeviceHostsSettings.logic.ts
index a4ef1e1bb90..ee274bf46ec 100644
--- a/apps/web/src/components/settings/simlockDeviceHostsSettings.logic.ts
+++ b/apps/web/src/components/settings/simlockDeviceHostsSettings.logic.ts
@@ -1,6 +1,7 @@
import {
SimlockDeviceHostConfig,
type EnvironmentId,
+ type SettingsSecretValue,
type SimlockDeviceHostConfig as SimlockDeviceHostConfigType,
} from "@t3tools/contracts";
import * as Option from "effect/Option";
@@ -54,10 +55,10 @@ export interface SimlockHostEnvironment {
readonly hosts: ReadonlyArray<SimlockDeviceHostConfigType>;
}
-/** A token field: the environment's saved token, a replacement, or none. */
+/** A token field: the environment's saved token, a replacement or secret reference, or none. */
export type SimlockTokenEntry =
| { kind: "saved" }
- | { kind: "new"; value: string }
+ | { kind: "new"; value: SettingsSecretValue }
| { kind: "cleared" };
/**
@@ -83,7 +84,13 @@ export function planSimlockDeviceHostSave(
const saved = environment.hosts.find((host) => host.id === draft.id)?.agentToken ?? "";
const entry = tokens.get(environment.environmentId) ?? { kind: "cleared" };
const agentToken =
- entry.kind === "saved" ? saved : entry.kind === "new" ? entry.value.trim() : "";
+ entry.kind === "saved"
+ ? saved
+ : entry.kind !== "new"
+ ? ""
+ : typeof entry.value === "string"
+ ? entry.value.trim()
+ : entry.value;
patches.push({
environmentId: environment.environmentId,
hosts: updateSimlockDeviceHosts(environment.hosts, { ...draft, agentToken }, false),
diff --git a/packages/contracts/src/device.ts b/packages/contracts/src/device.ts
index 417d1c81b41..a36f796abfe 100644
--- a/packages/contracts/src/device.ts
+++ b/packages/contracts/src/device.ts
@@ -79,8 +79,11 @@ export const SimlockDeviceHostConfig = Schema.Struct({
label: TrimmedNonEmptyString,
/** The gateway's origin, such as https://gateway.example.ts.net:4700. */
gatewayUrl: TrimmedNonEmptyString.check(Schema.isPattern(/^https?:\/\/[^\s/]+\/?$/)),
- /** Leases, drives, watches, and takes input on its own leases. Kept in the secret store. */
- agentToken: TrimmedString.pipe(
+ /**
+ * Leases, drives, watches, and takes input on its own leases. Kept in the secret store, or the
+ * secret provider to read it from.
+ */
+ agentToken: SettingsSecretValue.pipe(
Schema.withDecodingDefaultKey(Effect.succeed("")),
Schema.annotateKey({ settingsSecret: true }),
),
@@ -133,7 +136,7 @@ export const DeviceSimlockIdentityInput = Schema.Struct({
gatewayUrl: Schema.optional(TrimmedString),
/** A saved Simlock host whose stored token is checked when no new token is given. */
hostId: Schema.optional(DeviceHostId),
- agentToken: Schema.optional(TrimmedString),
+ agentToken: Schema.optional(SettingsSecretValue),
});
export type DeviceSimlockIdentityInput = typeof DeviceSimlockIdentityInput.Type;
diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts
index 798e076495a..a726135bcca 100644
--- a/packages/contracts/src/settings.ts
+++ b/packages/contracts/src/settings.ts
@@ -808,7 +808,7 @@ export const AntigravitySettings = makeProviderSettingsSchema(
},
}),
),
- apiKey: TrimmedString.pipe(
+ apiKey: SettingsSecretValue.pipe(
secretSetting,
Schema.withDecodingDefault(Effect.succeed("")),
Schema.annotateKey({
diff --git a/packages/provider-opencode/src/server/adapter.ts b/packages/provider-opencode/src/server/adapter.ts
index c4386e84854..1daf5ec1f4f 100644
--- a/packages/provider-opencode/src/server/adapter.ts
+++ b/packages/provider-opencode/src/server/adapter.ts
@@ -35,6 +35,7 @@ import {
type ProviderSessionId,
type RuntimeRequestId,
type ThreadId,
+ settingsSecretLiteral,
} from "@t3tools/contracts";
import { OpenCodeSettings } from "../settings.ts";
import * as Cause from "effect/Cause";
@@ -947,8 +948,8 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
binaryPath: options.settings.binaryPath,
directory: cwd,
serverUrl: options.settings.serverUrl,
- ...(options.settings.serverPassword
- ? { serverPassword: options.settings.serverPassword }
+ ...(settingsSecretLiteral(options.settings.serverPassword)
+ ? { serverPassword: settingsSecretLiteral(options.settings.serverPassword) }
: {}),
environment: options.environment,
});
diff --git a/packages/provider-opencode/src/server/driver.ts b/packages/provider-opencode/src/server/driver.ts
index eb62d44b9aa..56473cd617c 100644
--- a/packages/provider-opencode/src/server/driver.ts
+++ b/packages/provider-opencode/src/server/driver.ts
@@ -1,4 +1,4 @@
-import { ProviderDriverKind } from "@t3tools/contracts";
+import { ProviderDriverKind, settingsSecretLiteral } from "@t3tools/contracts";
import { OpenCodeSettings } from "../settings.ts";
import * as Digest from "effect/crypto/Digest";
import * as SecureRandom from "effect/crypto/SecureRandom";
@@ -215,6 +215,8 @@ export const OpenCodeDriver: ProviderDriver<
...config,
enabled,
binaryPath: expandHomePath(config.binaryPath, yield* HostProcess.HomeDirectory),
+ // The instance registry has already read a referenced password from its secret provider.
+ serverPassword: settingsSecretLiteral(config.serverPassword),
} satisfies OpenCodeSettings;
const runtimeProbe = yield* makeOpenCodeRuntimeProbe(
probeOpenCodeRuntime(effectiveConfig, processEnv).pipe(
diff --git a/packages/provider-opencode/src/server/status.test.ts b/packages/provider-opencode/src/server/status.test.ts
index 802f544ae6f..b6560a2f4a1 100644
--- a/packages/provider-opencode/src/server/status.test.ts
+++ b/packages/provider-opencode/src/server/status.test.ts
@@ -15,6 +15,7 @@ import * as Tracer from "effect/Tracer";
import * as HttpClient from "effect/http/HttpClient";
import * as HttpClientResponse from "effect/http/HttpClientResponse";
import { beforeEach } from "vite-plus/test";
+import { settingsSecretLiteral } from "@t3tools/contracts";
import { OpenCodeSettings } from "../settings.ts";
import * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
@@ -481,8 +482,9 @@ const layerTest = Layer.succeed(OpenCodeRuntime.OpenCodeRuntime, OpenCodeRuntime
Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
);
-const makeOpenCodeSettings = (overrides?: Partial<OpenCodeSettings>): OpenCodeSettings =>
- decodeOpenCodeSettings({
+// As the instance registry hands them to the driver: a referenced password is already read.
+const makeOpenCodeSettings = (overrides?: Partial<OpenCodeSettings>) => {
+ const settings = decodeOpenCodeSettings({
enabled: true,
binaryPath: "opencode",
serverUrl: "",
@@ -490,9 +492,11 @@ const makeOpenCodeSettings = (overrides?: Partial<OpenCodeSettings>): OpenCodeSe
customModels: [],
...overrides,
});
+ return { ...settings, serverPassword: settingsSecretLiteral(settings.serverPassword) };
+};
const checkProvider = Effect.fn("checkProvider")(function* (
- settings: OpenCodeSettings,
+ settings: ReturnType<typeof makeOpenCodeSettings>,
cwd = process.cwd(),
environment?: NodeJS.ProcessEnv,
server = replayOpenCodeServer(OPENCODE_1_RESPONSES, settings.serverPassword),
diff --git a/packages/provider-opencode/src/server/status.ts b/packages/provider-opencode/src/server/status.ts
index b14333445ce..0afd0dde163 100644
--- a/packages/provider-opencode/src/server/status.ts
+++ b/packages/provider-opencode/src/server/status.ts
@@ -3,6 +3,7 @@ import {
type ServerProviderModel,
type ServerProviderSkill,
type ServerProviderSlashCommand,
+ settingsSecretLiteral,
} from "@t3tools/contracts";
import type { OpenCodeSettings } from "../settings.ts";
import * as Cause from "effect/Cause";
@@ -801,8 +802,8 @@ export const checkOpenCodeProviderStatus = Effect.fn("checkOpenCodeProviderStatu
binaryPath: openCodeSettings.binaryPath,
directory: cwd,
serverUrl: openCodeSettings.serverUrl,
- ...(openCodeSettings.serverPassword
- ? { serverPassword: openCodeSettings.serverPassword }
+ ...(settingsSecretLiteral(openCodeSettings.serverPassword)
+ ? { serverPassword: settingsSecretLiteral(openCodeSettings.serverPassword) }
: {}),
})
.pipe(Effect.flatMap(loadInventory), Effect.scoped)
diff --git a/packages/provider-opencode/src/server/textGeneration.test.ts b/packages/provider-opencode/src/server/textGeneration.test.ts
index a5a74402578..c877bfdd4ea 100644
--- a/packages/provider-opencode/src/server/textGeneration.test.ts
+++ b/packages/provider-opencode/src/server/textGeneration.test.ts
@@ -1,4 +1,4 @@
-import { ProviderInstanceId, TextGenerationError } from "@t3tools/contracts";
+import { ProviderInstanceId, settingsSecretLiteral, TextGenerationError } from "@t3tools/contracts";
import { OpenCodeSettings } from "../settings.ts";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it } from "@effect/vitest";
@@ -227,7 +227,9 @@ function withOpenCodeTextGeneration<A, E, R>(
const serverOwner = yield* OpenCodeServerOwner.make({
binaryPath: settings.binaryPath,
directory: process.cwd(),
- ...(settings.serverPassword ? { serverPassword: settings.serverPassword } : {}),
+ ...(settingsSecretLiteral(settings.serverPassword)
+ ? { serverPassword: settingsSecretLiteral(settings.serverPassword) }
+ : {}),
...(environment ? { environment } : {}),
});
const textGeneration = yield* OpenCodeTextGeneration.makeOpenCodeTextGeneration(settings).pipe(
diff --git a/packages/provider-opencode/src/server/textGeneration.ts b/packages/provider-opencode/src/server/textGeneration.ts
index 570d2ebf6e2..d0fdfdf8a7c 100644
--- a/packages/provider-opencode/src/server/textGeneration.ts
+++ b/packages/provider-opencode/src/server/textGeneration.ts
@@ -1,7 +1,7 @@
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
-import { NonNegativeInt, TextGenerationError } from "@t3tools/contracts";
+import { NonNegativeInt, settingsSecretLiteral, TextGenerationError } from "@t3tools/contracts";
import type { OpenCodeSettings } from "../settings.ts";
import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
@@ -358,8 +358,8 @@ export const makeOpenCodeTextGeneration = Effect.fn("makeOpenCodeTextGeneration"
binaryPath: openCodeSettings.binaryPath,
directory: input.cwd,
serverUrl: openCodeSettings.serverUrl,
- ...(openCodeSettings.serverPassword
- ? { serverPassword: openCodeSettings.serverPassword }
+ ...(settingsSecretLiteral(openCodeSettings.serverPassword)
+ ? { serverPassword: settingsSecretLiteral(openCodeSettings.serverPassword) }
: {}),
})
.pipe(Effect.flatMap(runAgainstServer), Effect.scoped)
diff --git a/packages/provider-opencode/src/settings.ts b/packages/provider-opencode/src/settings.ts
index e6a21e99691..4391821bb5e 100644
--- a/packages/provider-opencode/src/settings.ts
+++ b/packages/provider-opencode/src/settings.ts
@@ -4,6 +4,7 @@ import {
makeBinaryPathSetting,
makeProviderSettingsSchema,
secretSetting,
+ SettingsSecretValue,
TrimmedString,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
@@ -36,7 +37,7 @@ export const OpenCodeSettings = makeProviderSettingsSchema(
},
}),
),
- serverPassword: TrimmedString.pipe(
+ serverPassword: SettingsSecretValue.pipe(
secretSetting,
Schema.withDecodingDefault(Effect.succeed("")),
Schema.annotateKey({