Skip to content

Commit 8691343

Browse files
fix(server): saving T3 Connect preferences is all or nothing
The activity setting is saved before the relay hears anything; if the hold change then fails at the relay or locally, the activity setting is put back, so a failed request changes neither preference. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 76b8037 commit 8691343

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ const encode = (value: string) => new TextEncoder().encode(value);
2020

2121
/** A linked environment whose secret store can refuse writes, and the relay calls it made. */
2222
const withService = <A, E>(
23-
options: { readonly failHoldWrite?: boolean; readonly failActivityWrite?: boolean },
23+
options: {
24+
readonly failHoldWrite?: boolean;
25+
readonly failActivityWrite?: boolean;
26+
readonly relayFails?: boolean;
27+
},
2428
body: (input: {
2529
readonly preferences: CloudPreferences.CloudPreferences["Service"];
2630
readonly stored: Map<string, Uint8Array>;
@@ -32,13 +36,16 @@ const withService = <A, E>(
3236
[RELAY_URL_SECRET, encode("https://relay.test")],
3337
[RELAY_ENVIRONMENT_CREDENTIAL_SECRET, encode("credential")],
3438
[HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET, encode("false")],
39+
[PUBLISH_AGENT_ACTIVITY_SECRET, encode("false")],
3540
]);
3641
const relayCalls: Array<boolean> = [];
3742
const fetch: typeof globalThis.fetch = Object.assign(
3843
(_input: Parameters<typeof globalThis.fetch>[0], init?: RequestInit) => {
3944
const payload = JSON.parse(String(init?.body)) as { holdWebhooksWhileOffline: boolean };
4045
relayCalls.push(payload.holdWebhooksWhileOffline);
41-
return Promise.resolve(Response.json(payload));
46+
return options.relayFails
47+
? Promise.resolve(new Response("unavailable", { status: 503 }))
48+
: Promise.resolve(Response.json(payload));
4249
},
4350
{ preconnect: () => {} },
4451
);
@@ -114,3 +121,18 @@ it.effect("leaves the relay untouched when the activity setting can't be saved",
114121
}),
115122
),
116123
);
124+
125+
it.effect("keeps both settings unchanged when the relay refuses the hold change", () =>
126+
withService({ relayFails: true }, ({ preferences, stored }) =>
127+
Effect.gen(function* () {
128+
yield* preferences
129+
.update({ publishAgentActivity: true, holdWebhooksWhileOffline: true })
130+
.pipe(Effect.flip);
131+
assert.equal(new TextDecoder().decode(stored.get(PUBLISH_AGENT_ACTIVITY_SECRET)), "false");
132+
assert.equal(
133+
new TextDecoder().decode(stored.get(HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET)),
134+
"false",
135+
);
136+
}),
137+
),
138+
);

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import * as Context from "effect/Context";
77
import * as Effect from "effect/Effect";
88
import * as Layer from "effect/Layer";
9+
import * as Option from "effect/Option";
910

1011
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
1112
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
@@ -73,18 +74,30 @@ const make = Effect.gen(function* () {
7374

7475
const update: CloudPreferences["Service"]["update"] = Effect.fn("CloudPreferences.update")(
7576
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.
77+
// All or nothing: the activity setting is saved first, before the relay
78+
// is told anything, and put back if the hold change then fails.
79+
const previousActivity = yield* secrets
80+
.get(PUBLISH_AGENT_ACTIVITY_SECRET)
81+
.pipe(Effect.orElseSucceed(() => Option.none<Uint8Array>()));
7882
yield* save(PUBLISH_AGENT_ACTIVITY_SECRET, input.publishAgentActivity);
7983
if (input.holdWebhooksWhileOffline !== undefined) {
8084
const next = input.holdWebhooksWhileOffline;
8185
const previous = yield* readHoldWebhooksWhileOffline(secrets);
82-
yield* pushHoldWebhooksWhileOffline(next);
83-
yield* save(HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET, next).pipe(
86+
yield* pushHoldWebhooksWhileOffline(next).pipe(
87+
Effect.andThen(
88+
save(HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET, next).pipe(
89+
Effect.tapError(() =>
90+
previous === next
91+
? Effect.void
92+
: pushHoldWebhooksWhileOffline(previous).pipe(Effect.ignore),
93+
),
94+
),
95+
),
8496
Effect.tapError(() =>
85-
previous === next
86-
? Effect.void
87-
: pushHoldWebhooksWhileOffline(previous).pipe(Effect.ignore),
97+
Option.match(previousActivity, {
98+
onNone: () => secrets.remove(PUBLISH_AGENT_ACTIVITY_SECRET),
99+
onSome: (bytes) => secrets.set(PUBLISH_AGENT_ACTIVITY_SECRET, bytes),
100+
}).pipe(Effect.ignore),
88101
),
89102
);
90103
}

0 commit comments

Comments
 (0)