Skip to content

Commit 8021acf

Browse files
authored
Merge branch 'main' into fix/claude-shutdown-runtime-error
2 parents cfd73fb + c30a6d9 commit 8021acf

104 files changed

Lines changed: 2676 additions & 674 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/app/DesktopApp.ts‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Crypto from "effect/Crypto";
99
import * as ElectronApp from "../electron/ElectronApp.ts";
1010
import * as ElectronDialog from "../electron/ElectronDialog.ts";
1111
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
12+
import * as ElectronSafeStorage from "../electron/ElectronSafeStorage.ts";
1213
import { installDesktopIpcHandlers } from "../ipc/DesktopIpcHandlers.ts";
1314
import * as DesktopAppIdentity from "./DesktopAppIdentity.ts";
1415
import * as DesktopClerk from "./DesktopClerk.ts";
@@ -17,7 +18,9 @@ import * as DesktopWindow from "../window/DesktopWindow.ts";
1718
import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
1819
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
1920
import * as DesktopLifecycle from "./DesktopLifecycle.ts";
21+
import * as DesktopLinuxUrlHandler from "./DesktopLinuxUrlHandler.ts";
2022
import * as DesktopObservability from "./DesktopObservability.ts";
23+
import * as DesktopPreReadyPlatform from "./DesktopPreReadyPlatform.ts";
2124
import * as DesktopShutdown from "./DesktopShutdown.ts";
2225
import * as DesktopServerExposure from "../backend/DesktopServerExposure.ts";
2326
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
@@ -220,20 +223,49 @@ const startup = Effect.gen(function* () {
220223
const applicationMenu = yield* DesktopApplicationMenu.DesktopApplicationMenu;
221224
const electronApp = yield* ElectronApp.ElectronApp;
222225
const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
226+
const linuxUrlHandler = yield* DesktopLinuxUrlHandler.DesktopLinuxUrlHandler;
223227
const clerk = yield* DesktopClerk.DesktopClerk;
224228
const shellEnvironment = yield* DesktopShellEnvironment.DesktopShellEnvironment;
225229
const desktopSettings = yield* DesktopAppSettings.DesktopAppSettings;
230+
const preReadyElectronOptions = yield* DesktopPreReadyPlatform.DesktopPreReadyElectronOptions;
231+
const safeStorage = yield* ElectronSafeStorage.ElectronSafeStorage;
226232
const updates = yield* DesktopUpdates.DesktopUpdates;
227233
const environment = yield* DesktopEnvironment.DesktopEnvironment;
228234

229235
yield* shellEnvironment.installIntoProcess;
236+
const hasCommandLinePasswordStore =
237+
preReadyElectronOptions.linuxPasswordStoreCommandLine !== null;
238+
const linuxElectronOptions =
239+
environment.platform === "linux" && !hasCommandLinePasswordStore
240+
? DesktopPreReadyPlatform.resolveEarlyLinuxElectronOptionsFromProcess()
241+
: preReadyElectronOptions.linux;
242+
if (linuxElectronOptions !== null && !hasCommandLinePasswordStore) {
243+
if (
244+
linuxElectronOptions.passwordStore !== null ||
245+
preReadyElectronOptions.linux?.passwordStore !== null
246+
) {
247+
yield* electronApp.removeCommandLineSwitch("password-store");
248+
}
249+
if (linuxElectronOptions.passwordStore !== null) {
250+
yield* electronApp.appendCommandLineSwitch(
251+
"password-store",
252+
linuxElectronOptions.passwordStore,
253+
);
254+
}
255+
}
230256
const userDataPath = yield* appIdentity.resolveUserDataPath;
231257
yield* electronApp.setPath("userData", userDataPath);
232258
yield* logStartupInfo("runtime logging configured", { logDir: environment.logDir });
233259
yield* desktopSettings.load;
234260

235-
if (environment.platform === "linux") {
236-
yield* electronApp.appendCommandLineSwitch("class", environment.linuxWmClass);
261+
if (linuxElectronOptions !== null) {
262+
yield* logStartupInfo("linux password store configured", {
263+
passwordStore: hasCommandLinePasswordStore
264+
? "command-line"
265+
: (linuxElectronOptions.passwordStore ?? "electron-default"),
266+
xdgCurrentDesktop: process.env.XDG_CURRENT_DESKTOP ?? null,
267+
xdgSessionDesktop: process.env.XDG_SESSION_DESKTOP ?? null,
268+
});
237269
}
238270

239271
yield* appIdentity.configure;
@@ -245,9 +277,16 @@ const startup = Effect.gen(function* () {
245277
Effect.catchCause((cause) => fatalStartupCause("whenReady", cause)),
246278
);
247279
yield* logStartupInfo("app ready");
280+
if (environment.platform === "linux") {
281+
const selectedBackend = yield* safeStorage.selectedStorageBackend;
282+
yield* logStartupInfo("safe storage ready", {
283+
backend: Option.getOrElse(selectedBackend, () => "unknown"),
284+
});
285+
}
248286
yield* appIdentity.configure;
249287
yield* applicationMenu.configure;
250288
yield* updates.configure;
289+
yield* linuxUrlHandler.register;
251290
yield* bootstrap.pipe(Effect.catchCause((cause) => fatalStartupCause("bootstrap", cause)));
252291
}).pipe(Effect.withSpan("desktop.startup"));
253292

‎apps/desktop/src/app/DesktopAppIdentity.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const makeElectronAppLayer = (calls: ElectronAppCalls) =>
6464
}),
6565
appendCommandLineSwitch: () => Effect.void,
6666
onBeforeQuitForUpdate: () => Effect.void,
67+
removeCommandLineSwitch: () => Effect.void,
6768
on: () => Effect.void,
6869
} satisfies ElectronApp.ElectronApp["Service"]);
6970

‎apps/desktop/src/app/DesktopConfig.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const compactEnv = (env: Readonly<Record<string, string | undefined>>): Record<s
3535
export const DesktopConfig = Config.all({
3636
appDataDirectory: trimmedString("APPDATA"),
3737
xdgConfigHome: trimmedString("XDG_CONFIG_HOME"),
38+
xdgDataHome: trimmedString("XDG_DATA_HOME"),
3839
t3Home: trimmedString("T3CODE_HOME"),
3940
devServerUrl: Config.url("VITE_DEV_SERVER_URL").pipe(Config.option),
4041
appUserModelIdOverride: trimmedString("T3CODE_DESKTOP_APP_USER_MODEL_ID"),

‎apps/desktop/src/app/DesktopConnectionCatalogStore.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function makeSafeStorageLayer(available: boolean, failDecrypt: Ref.Ref<boolean>
3939
return decoded.slice("encrypted:".length);
4040
});
4141
},
42+
selectedStorageBackend: Effect.succeed(Option.none()),
4243
} satisfies ElectronSafeStorage.ElectronSafeStorage["Service"]);
4344
}
4445

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - tests use POSIX path joining to match the Linux startup boundary.
2+
import * as NodePath from "node:path";
3+
import { assert, describe, it } from "@effect/vitest";
4+
5+
import {
6+
resolveEarlyLinuxElectronOptions,
7+
resolveEarlyLinuxPasswordStorePreference,
8+
} from "./DesktopEarlyElectronStartup.ts";
9+
10+
describe("DesktopEarlyElectronStartup", () => {
11+
const joinPath = NodePath.posix.join;
12+
13+
it("reads the persisted linux password-store preference before Electron is ready", () => {
14+
const preference = resolveEarlyLinuxPasswordStorePreference({
15+
env: { T3CODE_HOME: "/home/user/.t3-test" },
16+
homeDirectory: "/home/user",
17+
joinPath,
18+
readFileString: (path) => {
19+
assert.equal(path, "/home/user/.t3-test/userdata/desktop-settings.json");
20+
return JSON.stringify({ linuxPasswordStore: "kwallet6" });
21+
},
22+
});
23+
24+
assert.equal(preference, "kwallet6");
25+
});
26+
27+
it("accepts JSONC in the early desktop settings file", () => {
28+
const preference = resolveEarlyLinuxPasswordStorePreference({
29+
env: { T3CODE_HOME: "/home/user/.t3-test" },
30+
homeDirectory: "/home/user",
31+
joinPath,
32+
readFileString: () => `{
33+
// manually edited setting
34+
"linuxPasswordStore": "gnome-libsecret",
35+
}`,
36+
});
37+
38+
assert.equal(preference, "gnome-libsecret");
39+
});
40+
41+
it("falls back to auto when the early settings document is missing or invalid", () => {
42+
const preference = resolveEarlyLinuxPasswordStorePreference({
43+
env: {},
44+
homeDirectory: "/home/user",
45+
joinPath,
46+
readFileString: () => {
47+
throw new Error("missing");
48+
},
49+
});
50+
51+
assert.equal(preference, "auto");
52+
});
53+
54+
it("preserves absolute root paths when resolving early settings", () => {
55+
const preference = resolveEarlyLinuxPasswordStorePreference({
56+
env: { T3CODE_HOME: "/" },
57+
homeDirectory: "/home/user",
58+
joinPath,
59+
readFileString: (path) => {
60+
assert.equal(path, "/userdata/desktop-settings.json");
61+
return JSON.stringify({ linuxPasswordStore: "kwallet6" });
62+
},
63+
});
64+
65+
assert.equal(preference, "kwallet6");
66+
});
67+
68+
it("resolves the early linux Electron switches", () => {
69+
const options = resolveEarlyLinuxElectronOptions({
70+
env: {
71+
T3CODE_HOME: "/home/user/.t3-test",
72+
XDG_CURRENT_DESKTOP: "niri",
73+
VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
74+
},
75+
homeDirectory: "/home/user",
76+
joinPath,
77+
readFileString: (path) => {
78+
assert.equal(path, "/home/user/.t3-test/userdata/desktop-settings.json");
79+
return JSON.stringify({ linuxPasswordStore: "auto" });
80+
},
81+
});
82+
83+
assert.deepEqual(options, {
84+
linuxWmClass: "t3code-dev",
85+
passwordStore: "gnome-libsecret",
86+
});
87+
});
88+
89+
it("keeps implicit development state under ~/.t3/dev when T3CODE_HOME is unset", () => {
90+
const preference = resolveEarlyLinuxPasswordStorePreference({
91+
env: {
92+
VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
93+
},
94+
homeDirectory: "/home/user",
95+
joinPath,
96+
readFileString: (path) => {
97+
assert.equal(path, "/home/user/.t3/dev/desktop-settings.json");
98+
return JSON.stringify({ linuxPasswordStore: "kwallet" });
99+
},
100+
});
101+
102+
assert.equal(preference, "kwallet");
103+
});
104+
105+
it("treats whitespace-only T3CODE_HOME as unconfigured in development", () => {
106+
const preference = resolveEarlyLinuxPasswordStorePreference({
107+
env: {
108+
T3CODE_HOME: " ",
109+
VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
110+
},
111+
homeDirectory: "/home/user",
112+
joinPath,
113+
readFileString: (path) => {
114+
assert.equal(path, "/home/user/.t3/dev/desktop-settings.json");
115+
return JSON.stringify({ linuxPasswordStore: "gnome-libsecret" });
116+
},
117+
});
118+
119+
assert.equal(preference, "gnome-libsecret");
120+
});
121+
});
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { fromLenientJson } from "@t3tools/shared/schemaJson";
2+
import * as Option from "effect/Option";
3+
import * as Schema from "effect/Schema";
4+
5+
import {
6+
DEFAULT_LINUX_PASSWORD_STORE,
7+
normalizeLinuxPasswordStorePreference,
8+
resolveLinuxPasswordStoreSwitch,
9+
type LinuxPasswordStoreSwitch,
10+
type LinuxPasswordStorePreference,
11+
} from "../linuxSecretStorage.ts";
12+
import {
13+
resolveDesktopBaseDir,
14+
resolveDesktopStateDir,
15+
type JoinPath,
16+
} from "./DesktopStatePaths.ts";
17+
18+
interface EarlyDesktopSettingsInput {
19+
readonly env: NodeJS.ProcessEnv;
20+
readonly homeDirectory: string;
21+
readonly joinPath: JoinPath;
22+
readonly readFileString: (path: string) => string;
23+
}
24+
25+
type EarlyLinuxElectronOptionsInput = EarlyDesktopSettingsInput;
26+
27+
export interface EarlyLinuxElectronOptions {
28+
readonly linuxWmClass: string;
29+
readonly passwordStore: LinuxPasswordStoreSwitch | null;
30+
}
31+
32+
const trimNonEmpty = (value: string | undefined): string | null => {
33+
const trimmed = value?.trim();
34+
return trimmed && trimmed.length > 0 ? trimmed : null;
35+
};
36+
37+
const EarlyDesktopSettingsJson = fromLenientJson(
38+
Schema.Struct({
39+
linuxPasswordStore: Schema.optionalKey(Schema.Unknown),
40+
}),
41+
);
42+
const decodeEarlyDesktopSettingsJson = Schema.decodeSync(EarlyDesktopSettingsJson);
43+
44+
const isDevelopmentEnvironment = (env: NodeJS.ProcessEnv): boolean =>
45+
trimNonEmpty(env.VITE_DEV_SERVER_URL) !== null;
46+
47+
function resolveEarlyDesktopSettingsPath(input: {
48+
readonly env: NodeJS.ProcessEnv;
49+
readonly homeDirectory: string;
50+
readonly joinPath: JoinPath;
51+
}): string {
52+
const t3Home = Option.fromUndefinedOr(input.env.T3CODE_HOME);
53+
const baseDir = resolveDesktopBaseDir({
54+
homeDirectory: input.homeDirectory,
55+
joinPath: input.joinPath,
56+
t3Home,
57+
});
58+
const stateDir = resolveDesktopStateDir({
59+
baseDir,
60+
isDevelopment: isDevelopmentEnvironment(input.env),
61+
joinPath: input.joinPath,
62+
t3Home,
63+
});
64+
return input.joinPath(stateDir, "desktop-settings.json");
65+
}
66+
67+
export function resolveEarlyLinuxPasswordStorePreference(
68+
input: EarlyDesktopSettingsInput,
69+
): LinuxPasswordStorePreference {
70+
const settingsPath = resolveEarlyDesktopSettingsPath(input);
71+
try {
72+
const parsed = decodeEarlyDesktopSettingsJson(input.readFileString(settingsPath));
73+
return normalizeLinuxPasswordStorePreference(parsed.linuxPasswordStore);
74+
} catch {
75+
return DEFAULT_LINUX_PASSWORD_STORE;
76+
}
77+
}
78+
79+
export function resolveEarlyLinuxElectronOptions(
80+
input: EarlyLinuxElectronOptionsInput,
81+
): EarlyLinuxElectronOptions {
82+
const preference = resolveEarlyLinuxPasswordStorePreference(input);
83+
return {
84+
linuxWmClass: isDevelopmentEnvironment(input.env) ? "t3code-dev" : "t3code",
85+
passwordStore: resolveLinuxPasswordStoreSwitch({
86+
preference,
87+
env: input.env,
88+
}),
89+
};
90+
}

‎apps/desktop/src/app/DesktopEnvironment.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as Path from "effect/Path";
1313

1414
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
1515
import * as DesktopConfig from "./DesktopConfig.ts";
16+
import { resolveDesktopBaseDir, resolveDesktopStateDir } from "./DesktopStatePaths.ts";
1617
import { isNightlyDesktopVersion } from "../updates/updateChannels.ts";
1718

1819
export interface MakeDesktopEnvironmentInput {
@@ -66,6 +67,8 @@ export class DesktopEnvironment extends Context.Service<
6667
readonly appUserModelId: string;
6768
readonly linuxDesktopEntryName: string;
6869
readonly linuxWmClass: string;
70+
readonly linuxApplicationsDir: string;
71+
readonly appImagePath: Option.Option<string>;
6972
readonly userDataDirName: string;
7073
readonly legacyUserDataDirName: string;
7174
readonly defaultDesktopSettings: DesktopAppSettings.DesktopSettings;
@@ -147,21 +150,30 @@ const make = Effect.fn("desktop.environment.make")(function* (
147150
: input.platform === "darwin"
148151
? path.join(homeDirectory, "Library", "Application Support")
149152
: Option.getOrElse(config.xdgConfigHome, () => path.join(homeDirectory, ".config"));
150-
const configuredBaseDir = config.t3Home;
151-
const baseDir = Option.getOrElse(configuredBaseDir, () => path.join(homeDirectory, ".t3"));
153+
const baseDir = resolveDesktopBaseDir({
154+
homeDirectory,
155+
joinPath: path.join,
156+
t3Home: config.t3Home,
157+
});
152158
const rootDir = path.resolve(input.dirname, "../../..");
153159
const appRoot = input.isPackaged ? input.appPath : rootDir;
154160
const branding = resolveDesktopAppBranding({
155161
isDevelopment,
156162
appVersion: input.appVersion,
157163
});
158164
const displayName = branding.displayName;
159-
const stateDir = path.join(
165+
const stateDir = resolveDesktopStateDir({
160166
baseDir,
161-
isDevelopment && Option.isNone(configuredBaseDir) ? "dev" : "userdata",
162-
);
167+
isDevelopment,
168+
joinPath: path.join,
169+
t3Home: config.t3Home,
170+
});
163171
const userDataDirName = isDevelopment ? "t3code-dev" : "t3code";
164172
const legacyUserDataDirName = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)";
173+
const linuxApplicationsDir = path.join(
174+
Option.getOrElse(config.xdgDataHome, () => path.join(homeDirectory, ".local", "share")),
175+
"applications",
176+
);
165177
const resourcesPath = input.resourcesPath;
166178

167179
return DesktopEnvironment.of({
@@ -205,6 +217,8 @@ const make = Effect.fn("desktop.environment.make")(function* (
205217
),
206218
linuxDesktopEntryName: isDevelopment ? "t3code-dev.desktop" : "t3code.desktop",
207219
linuxWmClass: isDevelopment ? "t3code-dev" : "t3code",
220+
linuxApplicationsDir,
221+
appImagePath: config.appImagePath,
208222
userDataDirName,
209223
legacyUserDataDirName,
210224
defaultDesktopSettings: DesktopAppSettings.resolveDefaultDesktopSettings(input.appVersion),

‎apps/desktop/src/app/DesktopLifecycle.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe("DesktopLifecycle", () => {
3535
setDesktopName: () => Effect.void,
3636
setDockIcon: () => Effect.void,
3737
appendCommandLineSwitch: () => Effect.void,
38+
removeCommandLineSwitch: () => Effect.void,
3839
onBeforeQuitForUpdate: (listener) =>
3940
Effect.acquireRelease(
4041
Effect.sync(() => {

0 commit comments

Comments
 (0)