Open History to see the last commit
1import { DesktopWslStateSchema, type DesktopWslState } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Schema from "effect/Schema";
5
6import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
7import * as DesktopAppSettings from "../../settings/DesktopAppSettings.ts";
8import * as DesktopWslBackend from "../../wsl/DesktopWslBackend.ts";
9import * as DesktopWslEnvironment from "../../wsl/DesktopWslEnvironment.ts";
10import * as IpcChannels from "../channels.ts";
11import { makeIpcMethod } from "../DesktopIpc.ts";
12
13const readWslState: Effect.Effect<
14 DesktopWslState,
15 never,
16 | DesktopAppSettings.DesktopAppSettings
17 | DesktopWslEnvironment.DesktopWslEnvironment
18 | DesktopWslBackend.DesktopWslBackend
19> = Effect.gen(function* () {
20 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
21 const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment;
22 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
23 const settings = yield* appSettings.get;
24 const available = yield* wslEnvironment.isAvailable;
25 // Only enumerate distros when WSL is actually available — listDistros on a
26 // non-WSL host would spawn wsl.exe and hit the timeout for nothing.
27 const distros = available ? yield* wslEnvironment.listDistros : [];
28 const preflightError = yield* wslBackend.lastPreflightError;
29 const diagnostic = yield* wslBackend.lastDiagnostic;
30 return {
31 enabled: settings.wslBackendEnabled,
32 distro: settings.wslDistro,
33 available,
34 wslOnly: settings.wslOnly,
35 distros,
36 // Only the dual-mode secondary records this; a wsl-only failure surfaces via
37 // a dialog + Windows fallback, so it stays null there.
38 preflightError: settings.wslOnly ? null : Option.getOrNull(preflightError),
39 diagnostic: Option.getOrNull(diagnostic),
40 };
41});
42
43export const getWslState = makeIpcMethod({
44 channel: IpcChannels.GET_WSL_STATE_CHANNEL,
45 payload: Schema.Void,
46 result: DesktopWslStateSchema,
47 handler: Effect.fn("desktop.ipc.wsl.getState")(function* () {
48 return yield* readWslState;
49 }),
50});
51
52export const setWslBackendEnabled = makeIpcMethod({
53 channel: IpcChannels.SET_WSL_BACKEND_ENABLED_CHANNEL,
54 payload: Schema.Boolean,
55 result: DesktopWslStateSchema,
56 handler: Effect.fn("desktop.ipc.wsl.setEnabled")(function* (enabled) {
57 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
58 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
59 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
60 const previousSettings = yield* appSettings.get;
61 const updateSettings = enabled
62 ? appSettings.setWslBackendEnabled(true)
63 : appSettings.applyWslWindowsFallback;
64 const change = yield* updateSettings;
65 const settings = yield* appSettings.get;
66 const changedWslOnlyPrimary = enabled
67 ? settings.wslOnly
68 : previousSettings.wslBackendEnabled && previousSettings.wslOnly;
69 if (changedWslOnlyPrimary && change.changed) {
70 const state = yield* readWslState;
71 yield* lifecycle.relaunch(`wslBackendEnabled=${enabled}`);
72 return state;
73 }
74 // Reconcile is idempotent and never fails; no need for a swap-style
75 // rollback when the WSL side has trouble coming up. With both
76 // backends running side by side, "WSL didn't start" is a transient
77 // state on one instance — the primary stays up either way.
78 yield* wslBackend.reconcile;
79 return yield* readWslState;
80 }),
81});
82
83export const setWslDistro = makeIpcMethod({
84 channel: IpcChannels.SET_WSL_DISTRO_CHANNEL,
85 payload: Schema.NullOr(Schema.String),
86 result: DesktopWslStateSchema,
87 handler: Effect.fn("desktop.ipc.wsl.setDistro")(function* (distro) {
88 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
89 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
90 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
91 const change = yield* appSettings.setWslDistro(distro);
92 const settings = yield* appSettings.get;
93 // In active wsl-only mode the pool's primary IS the WSL backend, and its
94 // distro is captured when that backend starts, so relaunch to replace it.
95 // When WSL is disabled, this only stages a preference for the next enable.
96 if (settings.wslBackendEnabled && settings.wslOnly && change.changed) {
97 const state = yield* readWslState;
98 yield* lifecycle.relaunch(`wslDistro=${distro ?? "default"}`);
99 return state;
100 }
101 yield* wslBackend.reconcile;
102 return yield* readWslState;
103 }),
104});
105
106export const setWslOnly = makeIpcMethod({
107 channel: IpcChannels.SET_WSL_ONLY_CHANNEL,
108 payload: Schema.Boolean,
109 result: DesktopWslStateSchema,
110 handler: Effect.fn("desktop.ipc.wsl.setOnly")(function* (enabled) {
111 // wsl-only decides which backend the pool spins up as "primary", and that
112 // decision is captured once at layer init. A disabled WSL backend always
113 // leaves Windows primary active, so mode changes can be staged without a
114 // relaunch and applied by the subsequent enable call.
115 const appSettings = yield* DesktopAppSettings.DesktopAppSettings;
116 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
117 const change = yield* appSettings.setWslOnly(enabled);
118 const state = yield* readWslState;
119 if (state.enabled && change.changed) {
120 yield* lifecycle.relaunch(`wslOnly=${enabled}`);
121 }
122 return state;
123 }),
124});
125
126
127export const retryWslBackend = makeIpcMethod({
128 channel: IpcChannels.RETRY_WSL_BACKEND_CHANNEL,
129 payload: Schema.Void,
130 result: DesktopWslStateSchema,
131 handler: Effect.fn("desktop.ipc.wsl.retry")(function* () {
132 const wslBackend = yield* DesktopWslBackend.DesktopWslBackend;
133 yield* wslBackend.retry;
134 return yield* readWslState;
135 }),
136});
137
138export const getWslDiagnosticReport = makeIpcMethod({
139 channel: IpcChannels.GET_WSL_DIAGNOSTIC_REPORT_CHANNEL,
140 payload: Schema.Void,
141 result: Schema.String,
142 handler: Effect.fn("desktop.ipc.wsl.getDiagnosticReport")(function* () {
143 const state = yield* readWslState;
144 const selectedDistro =
145 state.distro ?? state.distros.find((candidate) => candidate.isDefault)?.name ?? null;
146 const report = {
147 generatedAt: new Date().toISOString(),
148 platform: process.platform,
149 arch: process.arch,
150 wsl: {
151 enabled: state.enabled,
152 wslOnly: state.wslOnly,
153 available: state.available,
154 selectedDistro,
155 configuredDistro: state.distro,
156 distros: state.distros,
157 latestFailure: state.diagnostic,
158 },
159 };
160 return `T3 Code WSL diagnostic report\n${JSON.stringify(report, null, 2)}`;
161 }),
162});