Skip to content

Commit c470a9d

Browse files
refactor(server): errors are built where they happen
SecretRequests constructs each SecretRequestError at its failure point, with the underlying cause where there is one, instead of through a forwarding helper. CloudPreferences maps a store failure at each write rather than catching a whole union with a predicate. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 4a22a8b commit c470a9d

2 files changed

Lines changed: 43 additions & 31 deletions

File tree

‎apps/server/src/cloud/CloudPreferences.ts‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,28 @@ const make = Effect.gen(function* () {
6666
},
6767
);
6868

69+
const save = (name: string, value: boolean) =>
70+
secrets
71+
.set(name, encode(value))
72+
.pipe(Effect.catch(internalError("Could not persist environment cloud preferences.")));
73+
6974
const update: CloudPreferences["Service"]["update"] = Effect.fn("CloudPreferences.update")(
7075
function* (input) {
7176
if (input.holdWebhooksWhileOffline !== undefined) {
7277
const next = input.holdWebhooksWhileOffline;
7378
const previous = yield* readHoldWebhooksWhileOffline(secrets);
7479
yield* pushHoldWebhooksWhileOffline(next);
75-
yield* secrets
76-
.set(HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET, encode(next))
77-
.pipe(
78-
Effect.tapError(() =>
79-
previous === next
80-
? Effect.void
81-
: pushHoldWebhooksWhileOffline(previous).pipe(Effect.ignore),
82-
),
83-
);
80+
yield* save(HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET, next).pipe(
81+
Effect.tapError(() =>
82+
previous === next
83+
? Effect.void
84+
: pushHoldWebhooksWhileOffline(previous).pipe(Effect.ignore),
85+
),
86+
);
8487
}
85-
yield* secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, encode(input.publishAgentActivity));
88+
yield* save(PUBLISH_AGENT_ACTIVITY_SECRET, input.publishAgentActivity);
8689
yield* awarenessRelay.requestCatchUp();
8790
},
88-
Effect.catchIf(
89-
ServerSecretStore.isSecretStoreError,
90-
internalError("Could not persist environment cloud preferences."),
91-
),
9291
);
9392

9493
return CloudPreferences.of({ update });

‎apps/server/src/secrets/SecretRequests.ts‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
CommandId,
1313
SecretRef,
1414
SecretRequestError,
15-
type SecretRequestFailureReason,
1615
type ProjectId,
1716
type SecretRequestAnswerInput,
1817
type ThreadId,
@@ -62,9 +61,6 @@ const StoredSecret = Schema.fromJsonString(
6261
const encodeStored = Schema.encodeEffect(StoredSecret);
6362
const decodeStored = Schema.decodeUnknownOption(StoredSecret);
6463

65-
const fail = (reason: SecretRequestFailureReason, cause?: unknown) =>
66-
new SecretRequestError({ reason, ...(cause === undefined ? {} : { cause }) });
67-
6864
export class SecretRequests extends Context.Service<
6965
SecretRequests,
7066
{
@@ -109,19 +105,23 @@ const make = Effect.gen(function* () {
109105
turnItemTypes: ["secret_request"],
110106
messageRoles: [],
111107
})
112-
.pipe(Effect.mapError((cause) => fail("load_failed", cause)));
108+
.pipe(
109+
Effect.mapError(
110+
(cause) => new SecretRequestError({ reason: "load_failed", cause: cause }),
111+
),
112+
);
113113
const item = records.turnItems.find((candidate) => candidate.id === input.turnItemId);
114114
if (item?.type !== "secret_request" || item.runId === null || item.nodeId === null) {
115-
return yield* fail("not_found");
115+
return yield* new SecretRequestError({ reason: "not_found" });
116116
}
117117
if (item.secretStatus !== "pending") {
118-
return yield* fail("already_answered");
118+
return yield* new SecretRequestError({ reason: "already_answered" });
119119
}
120120
// The agent is waiting inside the run that asked; once it has ended,
121121
// nobody will ever receive the ref, so a value saved now would be lost.
122122
const run = records.runs.find((candidate) => candidate.id === item.runId);
123123
if (run === undefined || ThreadManagementService.isTerminalRunStatus(run.status)) {
124-
return yield* fail("agent_stopped");
124+
return yield* new SecretRequestError({ reason: "agent_stopped" });
125125
}
126126
// Store first: the card only says saved once the value is kept. Create,
127127
// not set: a second answer racing this one must not replace the value.
@@ -140,7 +140,9 @@ const make = Effect.gen(function* () {
140140
)
141141
.pipe(
142142
Effect.catchIf(ServerSecretStore.isSecretAlreadyExistsError, () => Effect.void),
143-
Effect.mapError((error) => fail("store_failed", error)),
143+
Effect.mapError(
144+
(error) => new SecretRequestError({ reason: "store_failed", cause: error }),
145+
),
144146
);
145147
}
146148
const secretStatus = input.answer.type === "save" ? "saved" : "declined";
@@ -158,7 +160,9 @@ const make = Effect.gen(function* () {
158160
secretStatus,
159161
})
160162
.pipe(
161-
Effect.mapError((cause) => fail("record_failed", cause)),
163+
Effect.mapError(
164+
(cause) => new SecretRequestError({ reason: "record_failed", cause: cause }),
165+
),
162166
// The card still says pending, so the user can save again; the value
163167
// stored above would make that retry look already answered.
164168
Effect.tapError(() =>
@@ -176,11 +180,15 @@ const make = Effect.gen(function* () {
176180
turnItemTypes: ["secret_request"],
177181
messageRoles: [],
178182
})
179-
.pipe(Effect.mapError((cause) => fail("record_failed", cause)));
183+
.pipe(
184+
Effect.mapError(
185+
(cause) => new SecretRequestError({ reason: "record_failed", cause: cause }),
186+
),
187+
);
180188
const card = recorded.turnItems.find((candidate) => candidate.id === item.id);
181189
if (card?.type === "secret_request" && card.secretStatus !== "saved") {
182190
yield* removeLogged(storeName(refFor(salt, input.threadId, item.id)));
183-
return yield* fail("agent_stopped");
191+
return yield* new SecretRequestError({ reason: "agent_stopped" });
184192
}
185193
}).pipe(Effect.withSpan("SecretRequests.answer"));
186194

@@ -217,25 +225,30 @@ const make = Effect.gen(function* () {
217225

218226
const consumeRef = (input: { readonly ref: SecretRef; readonly projectId: ProjectId }) =>
219227
Effect.gen(function* () {
220-
if (!REF_PATTERN.test(input.ref)) return yield* fail("invalid_ref");
228+
if (!REF_PATTERN.test(input.ref))
229+
return yield* new SecretRequestError({ reason: "invalid_ref" });
221230
const stored = yield* store
222231
.get(storeName(input.ref))
223-
.pipe(Effect.mapError((cause) => fail("read_failed", cause)));
232+
.pipe(
233+
Effect.mapError(
234+
(cause) => new SecretRequestError({ reason: "read_failed", cause: cause }),
235+
),
236+
);
224237
const decoded = Option.flatMap(stored, (bytes) =>
225238
decodeStored(new TextDecoder().decode(bytes)),
226239
);
227240
if (Option.isNone(decoded) || decoded.value.projectId !== input.projectId) {
228-
return yield* fail("ref_unavailable");
241+
return yield* new SecretRequestError({ reason: "ref_unavailable" });
229242
}
230243
if ((yield* Clock.currentTimeMillis) - decoded.value.savedAt > SECRET_REF_TTL_MS) {
231244
// The hourly sweep retries a removal that fails here.
232245
yield* removeLogged(storeName(input.ref));
233-
return yield* fail("ref_expired");
246+
return yield* new SecretRequestError({ reason: "ref_expired" });
234247
}
235248
// One use: the value moves into whatever consumed it. If it cannot be
236249
// deleted, it is not handed out, so a ref is never used twice.
237250
if (!(yield* removeLogged(storeName(input.ref)))) {
238-
return yield* fail("consume_failed");
251+
return yield* new SecretRequestError({ reason: "consume_failed" });
239252
}
240253
return decoded.value.value;
241254
});

0 commit comments

Comments
 (0)