Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/server/src/serverRuntimeStartup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ export const make = Effect.gen(function* () {
Effect.catch((error) =>
Effect.logWarning("failed to start server settings runtime", {
path: error.settingsPath,
detail: error.detail,
operation: error.operation,
providerInstanceId: error.providerInstanceId,
environmentVariable: error.environmentVariable,
cause: error.cause,
}),
),
Expand Down
57 changes: 57 additions & 0 deletions apps/server/src/serverSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as Effect from "effect/Effect";
import * as Duration from "effect/Duration";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as PlatformError from "effect/PlatformError";
import * as Schema from "effect/Schema";
import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
import * as ServerConfig from "./config.ts";
Expand All @@ -32,7 +33,63 @@ const makeServerSettingsLayer = () =>
),
);

const makeFailingSecretStoreLayer = (cause: ServerSecretStore.SecretStoreError) =>
Layer.succeed(
ServerSecretStore.ServerSecretStore,
ServerSecretStore.ServerSecretStore.of({
get: () => Effect.fail(cause),
set: () => Effect.void,
create: () => Effect.void,
getOrCreateRandom: () => Effect.succeed(new Uint8Array()),
remove: () => Effect.void,
}),
);

it.layer(NodeServices.layer)("server settings", (it) => {
it.effect("preserves context when reading a provider environment secret fails", () => {
const platformCause = PlatformError.systemError({
_tag: "PermissionDenied",
module: "FileSystem",
method: "readFile",
pathOrDescriptor: "provider environment secret",
description: "Secret backend unavailable.",
});
const cause = new ServerSecretStore.SecretStoreReadError({
resource: "provider environment secret",
cause: platformCause,
});
const configLayer = Layer.fresh(
ServerConfig.layerTest(process.cwd(), {
prefix: "t3code-server-settings-secret-failure-test-",
}),
);
const settingsLayer = ServerSettingsModule.layer.pipe(
Layer.provide(makeFailingSecretStoreLayer(cause)),
Layer.provideMerge(configLayer),
);

return Effect.gen(function* () {
const serverConfig = yield* ServerConfig.ServerConfig;
const fileSystem = yield* FileSystem.FileSystem;
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
yield* fileSystem.writeFileString(
serverConfig.settingsPath,
'{"providerInstances":{"codex_personal":{"driver":"codex","environment":[{"name":"OPENROUTER_API_KEY","value":"","sensitive":true,"valueRedacted":true}],"config":{}}}}',
);

const error = yield* Effect.flip(serverSettings.getSettings);

assert.deepInclude(error, {
_tag: "ServerSettingsError",
operation: "read-secret",
providerInstanceId: "codex_personal",
environmentVariable: "OPENROUTER_API_KEY",
});
assert.strictEqual(error.cause, cause);
assert.notInclude(error.message, cause.message);
}).pipe(Effect.provide(settingsLayer));
});

it.effect("decodes nested settings patches", () =>
Effect.gen(function* () {
assert.deepEqual(
Expand Down
115 changes: 66 additions & 49 deletions apps/server/src/serverSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import * as Path from "effect/Path";
import * as PubSub from "effect/PubSub";
import * as Ref from "effect/Ref";
import * as Schema from "effect/Schema";
import * as SchemaIssue from "effect/SchemaIssue";
import * as Semaphore from "effect/Semaphore";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
Expand All @@ -67,7 +66,7 @@ const normalizeServerSettings = (
(cause) =>
new ServerSettingsError({
settingsPath: "<memory>",
detail: `failed to normalize server settings: ${SchemaIssue.makeFormatterDefault()(cause.issue)}`,
operation: "normalize",
cause,
}),
),
Expand Down Expand Up @@ -277,7 +276,7 @@ const make = Effect.gen(function* () {
(cause) =>
new ServerSettingsError({
settingsPath,
detail: "failed to check settings file existence",
operation: "check-exists",
cause,
}),
),
Expand All @@ -288,7 +287,7 @@ const make = Effect.gen(function* () {
(cause) =>
new ServerSettingsError({
settingsPath,
detail: "failed to read settings file",
operation: "read-file",
cause,
}),
),
Expand All @@ -305,6 +304,7 @@ const make = Effect.gen(function* () {
yield* Effect.logWarning("failed to parse settings.json, using defaults", {
path: settingsPath,
issues: Cause.pretty(decoded.cause),
cause: decoded.cause,
});
return DEFAULT_SERVER_SETTINGS;
}
Expand All @@ -318,13 +318,6 @@ const make = Effect.gen(function* () {

const getSettingsFromCache = Cache.get(settingsCache, cacheKey);

const toSettingsError = (detail: string, cause: unknown) =>
new ServerSettingsError({
settingsPath,
detail,
cause,
});

const materializeProviderEnvironmentSecrets = (
settings: ServerSettings,
): Effect.Effect<ServerSettings, ServerSettingsError> =>
Expand All @@ -343,11 +336,15 @@ const make = Effect.gen(function* () {
const secret = yield* secretStore
.get(providerEnvironmentSecretName({ instanceId, name: variable.name }))
.pipe(
Effect.mapError((cause) =>
toSettingsError(
`failed to read sensitive environment variable ${variable.name}`,
cause,
),
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath,
operation: "read-secret",
providerInstanceId: instanceId,
environmentVariable: variable.name,
cause,
}),
),
);
environment.push({
Expand Down Expand Up @@ -382,36 +379,51 @@ const make = Effect.gen(function* () {
for (const variable of instance.environment) {
const secretName = providerEnvironmentSecretName({ instanceId, name: variable.name });
if (!variable.sensitive) {
yield* secretStore
.remove(secretName)
.pipe(
Effect.mapError((cause) =>
toSettingsError(`failed to remove environment secret ${variable.name}`, cause),
),
);
yield* secretStore.remove(secretName).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath,
operation: "remove-secret",
providerInstanceId: instanceId,
environmentVariable: variable.name,
cause,
}),
),
);
environment.push(redactProviderEnvironmentVariable(variable));
continue;
}

nextSecretKeys.add(secretName);
if (!variable.valueRedacted) {
if (variable.value.length > 0) {
yield* secretStore
.set(secretName, textEncoder.encode(variable.value))
.pipe(
Effect.mapError((cause) =>
toSettingsError(`failed to persist environment secret ${variable.name}`, cause),
),
);
yield* secretStore.set(secretName, textEncoder.encode(variable.value)).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath,
operation: "write-secret",
providerInstanceId: instanceId,
environmentVariable: variable.name,
cause,
}),
),
);
environment.push({ ...variable, value: "", valueRedacted: true });
} else {
yield* secretStore
.remove(secretName)
.pipe(
Effect.mapError((cause) =>
toSettingsError(`failed to remove environment secret ${variable.name}`, cause),
),
);
yield* secretStore.remove(secretName).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath,
operation: "remove-secret",
providerInstanceId: instanceId,
environmentVariable: variable.name,
cause,
}),
),
);
const { valueRedacted: _omit, ...rest } = variable;
environment.push(rest);
}
Expand All @@ -431,16 +443,18 @@ const make = Effect.gen(function* () {
if (!variable.sensitive) continue;
const secretName = providerEnvironmentSecretName({ instanceId, name: variable.name });
if (nextSecretKeys.has(secretName)) continue;
yield* secretStore
.remove(secretName)
.pipe(
Effect.mapError((cause) =>
toSettingsError(
`failed to remove stale environment secret ${variable.name}`,
yield* secretStore.remove(secretName).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath,
operation: "remove-stale-secret",
providerInstanceId: instanceId,
environmentVariable: variable.name,
cause,
),
),
);
}),
),
);
}
}

Expand Down Expand Up @@ -468,7 +482,7 @@ const make = Effect.gen(function* () {
(cause) =>
new ServerSettingsError({
settingsPath,
detail: "failed to write settings file",
operation: "write-file",
cause,
}),
),
Expand All @@ -492,7 +506,7 @@ const make = Effect.gen(function* () {
(cause) =>
new ServerSettingsError({
settingsPath,
detail: "failed to prepare settings directory",
operation: "prepare-directory",
cause,
}),
),
Expand Down Expand Up @@ -571,7 +585,10 @@ const make = Effect.gen(function* () {
materializeProviderEnvironmentSecrets(settings).pipe(
Effect.catch((error: ServerSettingsError) =>
Effect.logWarning("failed to materialize provider environment secrets", {
detail: error.detail,
operation: error.operation,
providerInstanceId: error.providerInstanceId,
environmentVariable: error.environmentVariable,
cause: error.cause,
}).pipe(Effect.as(settings)),
),
),
Expand Down
27 changes: 24 additions & 3 deletions packages/contracts/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,16 +414,37 @@ export type ServerSettings = typeof ServerSettings.Type;

export const DEFAULT_SERVER_SETTINGS: ServerSettings = Schema.decodeSync(ServerSettings)({});

export const ServerSettingsOperation = Schema.Literals([
"normalize",
"check-exists",
"read-file",
"read-secret",
"remove-secret",
"remove-stale-secret",
"write-secret",
"write-file",
"prepare-directory",
]);
export type ServerSettingsOperation = typeof ServerSettingsOperation.Type;

export class ServerSettingsError extends Schema.TaggedErrorClass<ServerSettingsError>()(
"ServerSettingsError",
{
settingsPath: Schema.String,
detail: Schema.String,
cause: Schema.optional(Schema.Defect()),
operation: ServerSettingsOperation,
providerInstanceId: Schema.optional(Schema.String),
environmentVariable: Schema.optional(Schema.String),
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Server settings error at ${this.settingsPath}: ${this.detail}`;
const provider =
this.providerInstanceId === undefined ? "" : ` for provider ${this.providerInstanceId}`;
const variable =
this.environmentVariable === undefined
? ""
: ` and environment variable ${this.environmentVariable}`;
return `Server settings ${this.operation} failed${provider}${variable} at ${this.settingsPath}.`;
}
}

Expand Down
Loading