Skip to content

Commit 5abf67a

Browse files
merge: resolve main conflicts
2 parents c476dab + 09aac71 commit 5abf67a

140 files changed

Lines changed: 16011 additions & 1629 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/settings/DesktopClientSettings.diagnostics.test.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const readWithLogs = (fileSystemLayer: Layer.Layer<FileSystem.FileSystem>) => {
5454
const environment = yield* DesktopEnvironment.DesktopEnvironment;
5555
const settings = yield* DesktopClientSettings.DesktopClientSettings;
5656
return {
57-
result: yield* settings.get,
57+
result: yield* Effect.result(settings.get),
5858
settingsPath: environment.clientSettingsPath,
5959
records,
6060
};
@@ -73,12 +73,13 @@ describe("DesktopClientSettings diagnostics", () => {
7373
Effect.gen(function* () {
7474
const result = yield* readWithLogs(FileSystem.layerNoop({}));
7575

76-
assert.isTrue(Option.isNone(result.result));
76+
if (result.result._tag !== "Success") return assert.fail("expected a successful read");
77+
assert.isTrue(Option.isNone(result.result.success));
7778
assert.deepEqual(result.records, []);
7879
}),
7980
);
8081

81-
it.effect("logs non-missing filesystem failures with the settings path", () => {
82+
it.effect("reports non-missing filesystem failures and logs the settings path", () => {
8283
const permissionError = PlatformError.systemError({
8384
_tag: "PermissionDenied",
8485
module: "FileSystem",
@@ -93,7 +94,12 @@ describe("DesktopClientSettings diagnostics", () => {
9394
}),
9495
);
9596

96-
assert.isTrue(Option.isNone(result.result));
97+
if (result.result._tag !== "Failure") return assert.fail("expected a read failure");
98+
assert.instanceOf(
99+
result.result.failure,
100+
DesktopClientSettings.DesktopClientSettingsReadError,
101+
);
102+
assert.strictEqual(result.result.failure.cause, permissionError);
97103
assert.equal(result.records.length, 1);
98104
assert.deepEqual(result.records[0]?.message, [
99105
"Could not read desktop client settings.",
@@ -103,15 +109,20 @@ describe("DesktopClientSettings diagnostics", () => {
103109
});
104110
});
105111

106-
it.effect("logs malformed settings documents with the settings path", () =>
112+
it.effect("reports malformed settings documents and logs the settings path", () =>
107113
Effect.gen(function* () {
108114
const result = yield* readWithLogs(
109115
FileSystem.layerNoop({
110116
readFileString: () => Effect.succeed("{not-json"),
111117
}),
112118
);
113119

114-
assert.isTrue(Option.isNone(result.result));
120+
if (result.result._tag !== "Failure") return assert.fail("expected a decode failure");
121+
assert.instanceOf(
122+
result.result.failure,
123+
DesktopClientSettings.DesktopClientSettingsReadError,
124+
);
125+
assert.equal(result.result.failure.operation, "decode-document");
115126
assert.equal(result.records.length, 1);
116127
const message = result.records[0]?.message;
117128
if (!Array.isArray(message)) {

‎apps/desktop/src/settings/DesktopClientSettings.test.ts‎

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const clientSettings: ClientSettings = {
4444
fontSizeTerminal: 12,
4545
fontSmoothing: true,
4646
glassOpacity: 80,
47+
onboardingCompletedAt: null,
4748
panelAnimationDurationMs: 0,
4849
planModeEnabled: false,
4950
proactivePanelsEnabled: true,
@@ -136,6 +137,59 @@ describe("DesktopClientSettings", () => {
136137
),
137138
);
138139

140+
for (const failure of [
141+
{ label: "permission", reason: "PermissionDenied" },
142+
{ label: "I/O", reason: "Unknown" },
143+
] as const) {
144+
it.effect(`preserves saved preferences across ${failure.label} read failures and retries`, () =>
145+
withClientSettings(
146+
Effect.gen(function* () {
147+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
148+
const fileSystem = yield* FileSystem.FileSystem;
149+
const settings = yield* DesktopClientSettings.DesktopClientSettings;
150+
const savedSettings = {
151+
...clientSettings,
152+
onboardingCompletedAt: "2026-09-05T12:00:00.000Z",
153+
};
154+
yield* settings.set(savedSettings);
155+
const savedContents = yield* fileSystem.readFileString(environment.clientSettingsPath);
156+
const cause = PlatformError.systemError({
157+
_tag: failure.reason,
158+
module: "FileSystem",
159+
method: "readFileString",
160+
pathOrDescriptor: environment.clientSettingsPath,
161+
});
162+
let failRead = true;
163+
const retryableSettings = yield* DesktopClientSettings.make.pipe(
164+
Effect.provideService(
165+
FileSystem.FileSystem,
166+
FileSystem.FileSystem.of({
167+
...fileSystem,
168+
readFileString: (path) =>
169+
Effect.suspend(() =>
170+
failRead ? Effect.fail(cause) : fileSystem.readFileString(path),
171+
),
172+
}),
173+
),
174+
);
175+
176+
const error = yield* retryableSettings.get.pipe(Effect.flip);
177+
assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError);
178+
assert.equal(error.operation, "read-file");
179+
assert.equal(error.path, environment.clientSettingsPath);
180+
assert.strictEqual(error.cause, cause);
181+
assert.equal(
182+
yield* fileSystem.readFileString(environment.clientSettingsPath),
183+
savedContents,
184+
);
185+
186+
failRead = false;
187+
assert.deepEqual(yield* retryableSettings.get, Option.some(savedSettings));
188+
}),
189+
),
190+
);
191+
}
192+
139193
it.effect("reports the failed client settings write operation and path", () =>
140194
withClientSettings(
141195
Effect.gen(function* () {
@@ -222,17 +276,31 @@ describe("DesktopClientSettings", () => {
222276
),
223277
);
224278

225-
it.effect("treats malformed client settings documents as absent", () =>
226-
withClientSettings(
227-
Effect.gen(function* () {
228-
const environment = yield* DesktopEnvironment.DesktopEnvironment;
229-
const fileSystem = yield* FileSystem.FileSystem;
230-
const settings = yield* DesktopClientSettings.DesktopClientSettings;
231-
yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
232-
yield* fileSystem.writeFileString(environment.clientSettingsPath, "{not-json");
279+
for (const document of [
280+
{ label: "malformed JSON", contents: "{not-json" },
281+
{ label: "invalid direct settings", contents: '{"fontSizeCode":"large"}' },
282+
{ label: "invalid legacy settings", contents: '{"settings":{"fontSizeCode":"large"}}' },
283+
]) {
284+
it.effect(`reports ${document.label} without treating the settings file as absent`, () =>
285+
withClientSettings(
286+
Effect.gen(function* () {
287+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
288+
const fileSystem = yield* FileSystem.FileSystem;
289+
const settings = yield* DesktopClientSettings.DesktopClientSettings;
290+
yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
291+
yield* fileSystem.writeFileString(environment.clientSettingsPath, document.contents);
233292

234-
assert.isTrue(Option.isNone(yield* settings.get));
235-
}),
236-
),
237-
);
293+
const error = yield* settings.get.pipe(Effect.flip);
294+
assert.instanceOf(error, DesktopClientSettings.DesktopClientSettingsReadError);
295+
assert.equal(error.operation, "decode-document");
296+
assert.equal(error.path, environment.clientSettingsPath);
297+
assert.instanceOf(error.cause, Schema.SchemaError);
298+
assert.equal(
299+
yield* fileSystem.readFileString(environment.clientSettingsPath),
300+
document.contents,
301+
);
302+
}),
303+
),
304+
);
305+
}
238306
});

‎apps/desktop/src/settings/DesktopClientSettings.ts‎

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,33 @@ import * as Ref from "effect/Ref";
1212

1313
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
1414

15-
const ClientSettingsDocumentSchema = Schema.Struct({
16-
settings: ClientSettingsSchema,
17-
});
18-
1915
const ClientSettingsJson = fromLenientJson(ClientSettingsSchema);
20-
const LegacyClientSettingsDocumentJson = fromLenientJson(ClientSettingsDocumentSchema);
21-
const decodeLegacyClientSettingsDocumentJson = Schema.decodeEffect(
22-
LegacyClientSettingsDocumentJson,
16+
const decodeClientSettingsDocument = Schema.decodeEffect(
17+
fromLenientJson(Schema.Record(Schema.String, Schema.Unknown)),
2318
);
24-
const decodeClientSettingsJsonValue = Schema.decodeEffect(ClientSettingsJson);
25-
const decodeClientSettingsJson = (raw: string): Effect.Effect<ClientSettings, Schema.SchemaError> =>
26-
decodeLegacyClientSettingsDocumentJson(raw).pipe(
27-
Effect.map((document) => document.settings),
28-
Effect.catchTags({
29-
SchemaError: () => decodeClientSettingsJsonValue(raw),
30-
}),
19+
const decodeClientSettingsValue = Schema.decodeUnknownEffect(ClientSettingsSchema);
20+
const decodeClientSettingsJson = Effect.fnUntraced(function* (raw: string) {
21+
const document = yield* decodeClientSettingsDocument(raw);
22+
// Select the shape before validation so invalid legacy settings cannot become defaults.
23+
return yield* decodeClientSettingsValue(
24+
Object.hasOwn(document, "settings") ? document.settings : document,
3125
);
26+
});
3227
const encodeClientSettingsJson = Schema.encodeEffect(ClientSettingsJson);
3328

29+
export class DesktopClientSettingsReadError extends Schema.TaggedErrorClass<DesktopClientSettingsReadError>()(
30+
"DesktopClientSettingsReadError",
31+
{
32+
operation: Schema.Literals(["read-file", "decode-document"]),
33+
path: Schema.String,
34+
cause: Schema.Defect(),
35+
},
36+
) {
37+
override get message(): string {
38+
return `Desktop client settings read failed during ${this.operation} at ${this.path}.`;
39+
}
40+
}
41+
3442
const DesktopClientSettingsWriteOperation = Schema.Literals([
3543
"create-temporary-file-name",
3644
"encode-document",
@@ -55,7 +63,7 @@ export class DesktopClientSettingsWriteError extends Schema.TaggedErrorClass<Des
5563
export class DesktopClientSettings extends Context.Service<
5664
DesktopClientSettings,
5765
{
58-
readonly get: Effect.Effect<Option.Option<ClientSettings>>;
66+
readonly get: Effect.Effect<Option.Option<ClientSettings>, DesktopClientSettingsReadError>;
5967
readonly set: (
6068
settings: ClientSettings,
6169
) => Effect.Effect<void, DesktopClientSettingsWriteError>;
@@ -65,7 +73,7 @@ export class DesktopClientSettings extends Context.Service<
6573
const readClientSettings = (
6674
fileSystem: FileSystem.FileSystem,
6775
settingsPath: string,
68-
): Effect.Effect<Option.Option<ClientSettings>> =>
76+
): Effect.Effect<Option.Option<ClientSettings>, DesktopClientSettingsReadError> =>
6977
fileSystem.readFileString(settingsPath).pipe(
7078
Effect.map(Option.some),
7179
Effect.catchTags({
@@ -74,7 +82,15 @@ const readClientSettings = (
7482
? Effect.succeed(Option.none<string>())
7583
: Effect.logWarning("Could not read desktop client settings.", cause).pipe(
7684
Effect.annotateLogs({ settingsPath }),
77-
Effect.as(Option.none<string>()),
85+
Effect.andThen(
86+
Effect.fail(
87+
new DesktopClientSettingsReadError({
88+
operation: "read-file",
89+
path: settingsPath,
90+
cause,
91+
}),
92+
),
93+
),
7894
),
7995
}),
8096
Effect.flatMap(
@@ -87,7 +103,15 @@ const readClientSettings = (
87103
SchemaError: (cause) =>
88104
Effect.logWarning("Could not decode desktop client settings.", cause).pipe(
89105
Effect.annotateLogs({ settingsPath }),
90-
Effect.as(Option.none<ClientSettings>()),
106+
Effect.andThen(
107+
Effect.fail(
108+
new DesktopClientSettingsReadError({
109+
operation: "decode-document",
110+
path: settingsPath,
111+
cause,
112+
}),
113+
),
114+
),
91115
),
92116
}),
93117
),

‎apps/mobile/src/features/cloud/linkEnvironment.test.ts‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ import { MobilePreferencesStore } from "../../persistence/mobile-preferences";
1111
import { MobileStorage } from "../../persistence/mobile-storage";
1212

1313
import {
14-
cloudEnvironmentsPendingStatus,
1514
linkEnvironmentToCloud,
1615
linkEnvironmentToCloudWithPreference,
1716
connectCloudEnvironment,
1817
listCloudEnvironments,
1918
listCloudEnvironmentsWithStatus,
20-
normalizeRelayBaseUrl,
2119
refreshCloudEnvironmentConnection,
2220
} from "./linkEnvironment";
2321

@@ -195,23 +193,6 @@ describe("mobile cloud link environment client", () => {
195193
loadPreferences.mockClear();
196194
});
197195

198-
it("normalizes configured relay base URLs before building DPoP-bound requests", () => {
199-
expect(normalizeRelayBaseUrl(" https://relay.example.test/// ")).toBe(
200-
"https://relay.example.test",
201-
);
202-
expect(normalizeRelayBaseUrl(" ")).toBeNull();
203-
});
204-
205-
it("makes linked environments visible while their status is still loading", () => {
206-
expect(cloudEnvironmentsPendingStatus([listedEnvironment("env-1")])).toMatchObject([
207-
{
208-
environment: { environmentId: "env-1", label: "Desktop" },
209-
status: null,
210-
statusError: "Checking status...",
211-
},
212-
]);
213-
});
214-
215196
it.effect("decodes relay environment list responses before returning records", () =>
216197
Effect.gen(function* () {
217198
vi.stubGlobal(

‎apps/mobile/src/features/cloud/linkEnvironment.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ const RELAY_STATUS_AND_CONNECT_SCOPES = [
4343
RelayEnvironmentConnectScope,
4444
] satisfies ReadonlyArray<RelayDpopAccessTokenScope>;
4545

46-
export function normalizeRelayBaseUrl(value: string | null | undefined): string | null {
47-
const trimmed = value?.trim();
48-
if (!trimmed) {
49-
return null;
50-
}
51-
return trimmed.replace(/\/+$/g, "");
52-
}
53-
5446
function readRelayUrl(): string | null {
5547
return resolveCloudPublicConfig().relay.url;
5648
}
@@ -405,16 +397,6 @@ export function getCloudEnvironmentStatus(input: {
405397
});
406398
}
407399

408-
export function cloudEnvironmentsPendingStatus(
409-
environments: ReadonlyArray<RelayClientEnvironmentRecord>,
410-
): ReadonlyArray<CloudEnvironmentRecordWithStatus> {
411-
return environments.map((environment) => ({
412-
environment,
413-
status: null,
414-
statusError: "Checking status...",
415-
}));
416-
}
417-
418400
export function loadCloudEnvironmentStatuses(input: {
419401
readonly clerkToken: string;
420402
readonly environments: ReadonlyArray<RelayClientEnvironmentRecord>;

0 commit comments

Comments
 (0)