Skip to content

Commit 76b8037

Browse files
fix(server): a failed preferences save never leaves the relay changed
The agent-activity setting was saved after the relay had been told the new hold setting, so a failure there returned an error with the relay already changed. It is now saved first, before the relay hears anything. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent c470a9d commit 76b8037

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as AgentAwarenessRelay from "../relay/AgentAwarenessRelay.ts";
1111
import * as CloudPreferences from "./CloudPreferences.ts";
1212
import {
1313
HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET,
14+
PUBLISH_AGENT_ACTIVITY_SECRET,
1415
RELAY_ENVIRONMENT_CREDENTIAL_SECRET,
1516
RELAY_URL_SECRET,
1617
} from "./config.ts";
@@ -19,7 +20,7 @@ const encode = (value: string) => new TextEncoder().encode(value);
1920

2021
/** A linked environment whose secret store can refuse writes, and the relay calls it made. */
2122
const withService = <A, E>(
22-
options: { readonly failHoldWrite?: boolean },
23+
options: { readonly failHoldWrite?: boolean; readonly failActivityWrite?: boolean },
2324
body: (input: {
2425
readonly preferences: CloudPreferences.CloudPreferences["Service"];
2526
readonly stored: Map<string, Uint8Array>;
@@ -45,7 +46,8 @@ const withService = <A, E>(
4546
Layer.mock(ServerSecretStore.ServerSecretStore)({
4647
get: (name) => Effect.succeed(Option.fromNullishOr(stored.get(name))),
4748
set: (name, value) =>
48-
options.failHoldWrite && name === HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET
49+
(options.failHoldWrite && name === HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET) ||
50+
(options.failActivityWrite && name === PUBLISH_AGENT_ACTIVITY_SECRET)
4951
? Effect.fail(
5052
new ServerSecretStore.SecretStorePersistError({
5153
resource: name,
@@ -96,3 +98,19 @@ it.effect("puts the relay back when the local save fails", () =>
9698
}),
9799
),
98100
);
101+
102+
it.effect("leaves the relay untouched when the activity setting can't be saved", () =>
103+
withService({ failActivityWrite: true }, ({ preferences, stored, relayCalls }) =>
104+
Effect.gen(function* () {
105+
const error = yield* preferences
106+
.update({ publishAgentActivity: true, holdWebhooksWhileOffline: true })
107+
.pipe(Effect.flip);
108+
assert.equal(error._tag, "EnvironmentHttpInternalServerError");
109+
assert.deepEqual(relayCalls, []);
110+
assert.equal(
111+
new TextDecoder().decode(stored.get(HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET)),
112+
"false",
113+
);
114+
}),
115+
),
116+
);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ const make = Effect.gen(function* () {
7373

7474
const update: CloudPreferences["Service"]["update"] = Effect.fn("CloudPreferences.update")(
7575
function* (input) {
76+
// Saved before the relay is told anything, so a failure here leaves the
77+
// relay and the hold setting as they were.
78+
yield* save(PUBLISH_AGENT_ACTIVITY_SECRET, input.publishAgentActivity);
7679
if (input.holdWebhooksWhileOffline !== undefined) {
7780
const next = input.holdWebhooksWhileOffline;
7881
const previous = yield* readHoldWebhooksWhileOffline(secrets);
@@ -85,7 +88,6 @@ const make = Effect.gen(function* () {
8588
),
8689
);
8790
}
88-
yield* save(PUBLISH_AGENT_ACTIVITY_SECRET, input.publishAgentActivity);
8991
yield* awarenessRelay.requestCatchUp();
9092
},
9193
);

0 commit comments

Comments
 (0)