fix(desktop): Cache Runtime locally on WSL Filesystem, dramatically improving launch times #5769
main fromrefs/pull/5769/headmerged by shivamhwp(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/desktop/src/wsl/DesktopWslEnvironment.ts lines 271 to 274OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -217,2 +254,21 @@217254 const shellQuote = (value: string): string => `'${value.replaceAll("'", "'\\''")}'`;218255256+const WSL_RUNTIME_READY_MARKER = ".t3code-wsl-runtime-ready";257+258+export const sanitizeWslRuntimeId = (value: string): string =>259+ value.replace(/[^A-Za-z0-9._-]/g, "_");260+261+export const buildWslRuntimeInstallScript = (262+ linuxArchivePath: string,263+ runtimeId: string,264+): string => {265+ const safeRuntimeId = sanitizeWslRuntimeId(runtimeId);266+ return [267+ "set -eu",268+ 'runtime_parent="$HOME/.t3/runtime"',269+ `runtime_root="$runtime_parent/${safeRuntimeId}"`,270+ `ready_marker="$runtime_root/${WSL_RUNTIME_READY_MARKER}"`,271+ 'if [ -f "$ready_marker" ]; then',272+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,273+ " exit 0",274+ "fi",
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 370Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -218,0 +267,21 @@267+ "set -eu",268+ 'runtime_parent="$HOME/.t3/runtime"',269+ `runtime_root="$runtime_parent/${safeRuntimeId}"`,270+ `ready_marker="$runtime_root/${WSL_RUNTIME_READY_MARKER}"`,271+ "runtime_is_ready() {",272+ ' [ -f "$ready_marker" ] &&',273+ ' [ -f "$runtime_root/apps/server/dist/bin.mjs" ] &&',274+ ' [ -f "$runtime_root/node_modules/effect/package.json" ]',275+ "}",276+ "if runtime_is_ready; then",277+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,278+ " exit 0",279+ "fi",280+ 'mkdir -p "$runtime_parent"',281+ 'if [ -e "$runtime_root" ]; then',282+ ` runtime_stale=$(mktemp -d "$runtime_parent/.${safeRuntimeId}.stale.XXXXXX")`,283+ ' rmdir "$runtime_stale"',284+ ' if mv -T "$runtime_root" "$runtime_stale" 2>/dev/null; then',285+ ' rm -rf "$runtime_stale"',286+ " fi",287+ "fi",scripts/build-desktop-artifact.ts line 2397Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1718,0 +1735,21 @@1735+ "--exclude=node_modules/@anthropic-ai/claude-agent-sdk-*",1736+ "--exclude=node_modules/.pnpm/@anthropic-ai+claude-agent-sdk-*",1737+ "apps/server/dist",1738+ "node_modules",1739+];1740+1741+const stageWslRuntimeArchive = Effect.fn("stageWslRuntimeArchive")(function* (stageAppDir: string) {1742+ const fs = yield* FileSystem.FileSystem;1743+ const path = yield* Path.Path;1744+ const archivePath = path.join(1745+ stageAppDir,1746+ "apps/desktop/prod-resources",1747+ WSL_RUNTIME_ARCHIVE_NAME,1748+ );1749+ yield* fs.makeDirectory(path.dirname(archivePath), { recursive: true });1750+ yield* runCommand(1751+ ChildProcess.make("tar", buildWslRuntimeArchiveArgs(archivePath), {1752+ cwd: stageAppDir,1753+ }),1754+ { label: "tar WSL runtime", verbose: false },1755+ );
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 282OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -218,0 +262,21 @@262+ linuxArchivePath: string,263+ runtimeId: string,264+): string => {265+ const safeRuntimeId = sanitizeWslRuntimeId(runtimeId);266+ return [267+ "set -eu",268+ 'runtime_parent="$HOME/.t3/runtime"',269+ `runtime_root="$runtime_parent/${safeRuntimeId}"`,270+ `ready_marker="$runtime_root/${WSL_RUNTIME_READY_MARKER}"`,271+ "runtime_is_ready() {",272+ ' [ -f "$ready_marker" ] &&',273+ ' [ -f "$runtime_root/apps/server/dist/bin.mjs" ] &&',274+ ' [ -f "$runtime_root/node_modules/effect/package.json" ]',275+ "}",276+ "if runtime_is_ready; then",277+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,278+ " exit 0",279+ "fi",280+ 'mkdir -p "$runtime_parent"',281+ `runtime_lock="$runtime_parent/.${safeRuntimeId}.install.lock"`,282+ 'while ! mkdir "$runtime_lock" 2>/dev/null; do',
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 304OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -218,0 +284,21 @@284+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,285+ " exit 0",286+ " fi",287+ " sleep 0.1",288+ "done",289+ 'cleanup_runtime_lock() { rm -rf "$runtime_lock"; }',290+ "trap cleanup_runtime_lock EXIT HUP INT TERM",291+ "if runtime_is_ready; then",292+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,293+ " exit 0",294+ "fi",295+ 'if [ -e "$runtime_root" ]; then',296+ ` runtime_stale=$(mktemp -d "$runtime_parent/.${safeRuntimeId}.stale.XXXXXX")`,297+ ' rmdir "$runtime_stale"',298+ ' if mv -T "$runtime_root" "$runtime_stale" 2>/dev/null; then',299+ ' rm -rf "$runtime_stale"',300+ " fi",301+ "fi",302+ `runtime_tmp=$(mktemp -d "$runtime_parent/.${safeRuntimeId}.tmp.XXXXXX")`,303+ 'cleanup_runtime_install() { rm -rf "$runtime_tmp" "$runtime_lock"; }',304+ "trap cleanup_runtime_install EXIT HUP INT TERM",apps/desktop/src/wsl/DesktopWslEnvironment.ts line 288OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -218,0 +268,21 @@268+ 'runtime_parent="$HOME/.t3/runtime"',269+ `runtime_root="$runtime_parent/${safeRuntimeId}"`,270+ `ready_marker="$runtime_root/${WSL_RUNTIME_READY_MARKER}"`,271+ "runtime_is_ready() {",272+ ' [ -f "$ready_marker" ] &&',273+ ' [ -f "$runtime_root/apps/server/dist/bin.mjs" ] &&',274+ ' [ -f "$runtime_root/node_modules/effect/package.json" ]',275+ "}",276+ "if runtime_is_ready; then",277+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,278+ " exit 0",279+ "fi",280+ 'mkdir -p "$runtime_parent"',281+ `runtime_lock="$runtime_parent/.${safeRuntimeId}.install.lock"`,282+ 'while ! mkdir "$runtime_lock" 2>/dev/null; do',283+ " if runtime_is_ready; then",284+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,285+ " exit 0",286+ " fi",287+ " sleep 0.1",288+ "done",
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 285OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -218,0 +265,21 @@265+ const safeRuntimeId = sanitizeWslRuntimeId(runtimeId);266+ return [267+ "set -eu",268+ 'runtime_parent="$HOME/.t3/runtime"',269+ `runtime_root="$runtime_parent/${safeRuntimeId}"`,270+ `ready_marker="$runtime_root/${WSL_RUNTIME_READY_MARKER}"`,271+ "runtime_is_ready() {",272+ ' [ -f "$ready_marker" ] &&',273+ ' [ -f "$runtime_root/apps/server/dist/bin.mjs" ] &&',274+ ' [ -f "$runtime_root/node_modules/effect/package.json" ]',275+ "}",276+ "if runtime_is_ready; then",277+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,278+ " exit 0",279+ "fi",280+ 'mkdir -p "$runtime_parent"',281+ `runtime_lock="$runtime_parent/.${safeRuntimeId}.install.lock"`,282+ `runtime_lock_candidate=$(mktemp -d "$runtime_parent/.${safeRuntimeId}.install-lock.XXXXXX")`,283+ 'printf \'%s\\n\' "$$" > "$runtime_lock_candidate/pid"',284+ 'cleanup_runtime_lock_candidate() { rm -rf "$runtime_lock_candidate"; }',285+ "trap cleanup_runtime_lock_candidate EXIT HUP INT TERM",
- apps/desktop/src/backend/DesktopBackendConfiguration.ts line 304OutdatedResolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -289,15 +290,15 @@289290 }290291291- const entryExists = yield* fileSystem292- .exists(input.windowsEntryPath)292+ const sourceExists = yield* fileSystem293+ .exists(input.windowsSourcePath)293294 .pipe(Effect.orElseSucceed(() => false));294- if (!entryExists) {295+ if (!sourceExists) {295296 return {296297 _tag: "Failed",297- reason: `missing server entry at ${input.windowsEntryPath}`,298+ reason: `missing WSL runtime source at ${input.windowsSourcePath}`,298299 fatal: true,299300 } as const;300301 }301302302- const linuxEntry = yield* wslEnv.windowsToWslPath(runningDistro, input.windowsEntryPath);303- if (Option.isNone(linuxEntry)) {303+ const runtime = yield* wslEnv.prepareRuntime(runningDistro, input.runtimeSource);304+ if (!runtime.ok) {apps/desktop/src/wsl/DesktopWslEnvironment.ts line 295OutdatedResolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -218,0 +275,21 @@275+ "}",276+ "if runtime_is_ready; then",277+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,278+ " exit 0",279+ "fi",280+ 'mkdir -p "$runtime_parent"',281+ `runtime_lock="$runtime_parent/.${safeRuntimeId}.install.lock"`,282+ `runtime_lock_candidate=$(mktemp -d "$runtime_parent/.${safeRuntimeId}.install-lock.XXXXXX")`,283+ 'printf \'%s\\n\' "$$" > "$runtime_lock_candidate/pid"',284+ 'cleanup_runtime_lock_candidate() { rm -rf "$runtime_lock_candidate"; }',285+ "trap cleanup_runtime_lock_candidate EXIT",286+ "trap 'exit 1' HUP INT TERM",287+ 'while ! mv -T "$runtime_lock_candidate" "$runtime_lock" 2>/dev/null; do',288+ " if runtime_is_ready; then",289+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,290+ " exit 0",291+ " fi",292+ ' runtime_lock_pid=$(cat "$runtime_lock/pid" 2>/dev/null || true)',293+ ' case "$runtime_lock_pid" in',294+ " ''|*[!0-9]*) runtime_lock_owner_alive=false ;;",295+ ' *) if kill -0 "$runtime_lock_pid" 2>/dev/null; then runtime_lock_owner_alive=true; else runtime_lock_owner_alive=false; fi ;;',apps/desktop/src/wsl/DesktopWslEnvironment.ts line 306OutdatedResolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -218,0 +286,21 @@286+ "trap 'exit 1' HUP INT TERM",287+ 'while ! mv -T "$runtime_lock_candidate" "$runtime_lock" 2>/dev/null; do',288+ " if runtime_is_ready; then",289+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,290+ " exit 0",291+ " fi",292+ ' runtime_lock_pid=$(cat "$runtime_lock/pid" 2>/dev/null || true)',293+ ' case "$runtime_lock_pid" in',294+ " ''|*[!0-9]*) runtime_lock_owner_alive=false ;;",295+ ' *) if kill -0 "$runtime_lock_pid" 2>/dev/null; then runtime_lock_owner_alive=true; else runtime_lock_owner_alive=false; fi ;;',296+ " esac",297+ ' if [ "$runtime_lock_owner_alive" = false ]; then',298+ ` runtime_lock_stale=$(mktemp -d "$runtime_parent/.${safeRuntimeId}.stale-lock.XXXXXX")`,299+ ' rmdir "$runtime_lock_stale"',300+ ' if mv -T "$runtime_lock" "$runtime_lock_stale" 2>/dev/null; then',301+ ' rm -rf "$runtime_lock_stale"',302+ " fi",303+ " fi",304+ " sleep 0.1",305+ "done",306+ 'cleanup_runtime_lock() { rm -rf "$runtime_lock_candidate" "$runtime_lock"; }',@@ -41,3 +41,11 @@4141 yay -S t3code-bin4242 ```434344+### Windows Subsystem for Linux45+46+When the desktop app runs a WSL backend, it installs the matching server runtime into47+`~/.t3/runtime` inside the selected distro. The first launch after installing or updating T3 Code48+may take a little longer while that runtime is extracted. Later launches reuse the Linux-local49+copy so startup does not depend on reading application files through `/mnt/c`.50+51+Each T3 Code version uses its own runtime directory. Removing an old version's directory is safeapps/desktop/src/wsl/DesktopWslEnvironment.test.ts lines 13 to 14OutdatedResolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -10,4 +10,5 @@1010 import { ChildProcessSpawner } from "effect/unstable/process";11111212 import {13+ buildWslRuntimeInstallScript,1314 buildWslNodeEnvPreamble,apps/desktop/src/wsl/DesktopWslEnvironment.test.ts line 189Resolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -125,3 +128,9 @@125128 });126129 });127130131+describe("WSL runtime cache", () => {132+ it("sanitizes cache ids before interpolating them into Linux paths", () => {133+ expect(sanitizeWslRuntimeId("1.2.3/x64; touch /tmp/nope")).toBe("1.2.3_x64__touch__tmp_nope");134+ });135+136+ it("installs through a temporary directory and only reuses valid completed caches", () => {
- apps/desktop/src/backend/DesktopBackendConfiguration.ts line 514OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -474,9 +496,19 @@496+ // Older/local packages without the archive retain the mounted-directory path497+ // as a compatibility fallback; dev appRoot is already a real directory.474498 const wslAppRoot = environment.isPackaged475499 ? environment.path.join(environment.resourcesPath, "app.asar.unpacked")476500 : environment.appRoot;477501 const wslEntryPath = environment.path.join(wslAppRoot, "apps/server/dist/bin.mjs");478-502+ const wslRuntimeArchivePath = environment.path.join(503+ environment.resourcesPath,504+ WSL_RUNTIME_ARCHIVE_NAME,505+ );506+ const hasWslRuntimeArchive = environment.isPackaged507+ ? yield* fileSystem.exists(wslRuntimeArchivePath).pipe(Effect.orElseSucceed(() => false))508+ : false;479509 const preflight = yield* runWslPreflight({480510 distro: input.distro,481511 windowsEntryPath: wslEntryPath,482- windowsRepoRoot: wslAppRoot,512+ windowsAppRoot: wslAppRoot,513+ windowsRuntimeArchivePath: hasWslRuntimeArchive ? wslRuntimeArchivePath : null,514+ runtimeId: `${environment.appVersion}-${environment.processArch}`,
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 292OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -218,0 +272,21 @@272+ `runtime_lock="$runtime_parent/.${safeRuntimeId}.install.lock"`,273+ "trap 'exit 1' HUP INT TERM",274+ 'exec 9> "$runtime_lock"',275+ "flock -x 9",276+ "if runtime_is_ready; then",277+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,278+ " exit 0",279+ "fi",280+ 'if [ -e "$runtime_root" ]; then',281+ ` runtime_stale=$(mktemp -d "$runtime_parent/.${safeRuntimeId}.stale.XXXXXX")`,282+ ' rmdir "$runtime_stale"',283+ ' if mv -T "$runtime_root" "$runtime_stale" 2>/dev/null; then',284+ ' rm -rf "$runtime_stale"',285+ " fi",286+ "fi",287+ `runtime_tmp=$(mktemp -d "$runtime_parent/.${safeRuntimeId}.tmp.XXXXXX")`,288+ 'cleanup_runtime_install() { rm -rf "$runtime_tmp"; }',289+ "trap cleanup_runtime_install EXIT",290+ `tar -xzf ${shellQuote(linuxArchivePath)} -C "$runtime_tmp"`,291+ 'test -f "$runtime_tmp/apps/server/dist/bin.mjs"',292+ 'test -f "$runtime_tmp/node_modules/effect/package.json"',
t3-code-bot[bot](GitHub profile, opens in a new tab) requested changes
scripts/build-desktop-artifact.ts line 2217OutdatedResolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -2178,0 +2197,21 @@2197+export const buildWslRuntimeArchiveArgs = (2198+ archivePath: string = WSL_RUNTIME_ARCHIVE_EXTRA_RESOURCE.from,2199+): ReadonlyArray<string> => [2200+ "-czf",2201+ archivePath,2202+ "--exclude=node_modules/@anthropic-ai/claude-agent-sdk-*",2203+ "--exclude=node_modules/.pnpm/@anthropic-ai+claude-agent-sdk-*",2204+ "apps/server/dist",2205+ "node_modules",2206+];2207+2208+const stageWslRuntimeArchive = Effect.fn("stageWslRuntimeArchive")(function* (input: {2209+ readonly sourceDir: string;2210+ readonly archivePath: string;2211+ readonly hashPath: string;2212+}) {2213+ const fs = yield* FileSystem.FileSystem;2214+ const path = yield* Path.Path;2215+ yield* fs.makeDirectory(path.dirname(input.archivePath), { recursive: true });2216+ yield* runCommand(2217+ ChildProcess.make("tar", buildWslRuntimeArchiveArgs(input.archivePath), {@@ -43,3 +43,4 @@4343 yay -S t3code-bin4444 ```454546+### Windows Subsystem for Linux- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 265OutdatedResolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -217,2 +245,21 @@217245 const shellQuote = (value: string): string => `'${value.replaceAll("'", "'\\''")}'`;218246247+const WSL_RUNTIME_READY_MARKER = ".t3code-wsl-runtime-ready";248+249+export const sanitizeWslRuntimeId = (value: string): string =>250+ value.replace(/[^A-Za-z0-9._-]/g, "_");251+252+export const buildWslRuntimeInstallScript = (253+ linuxArchivePath: string,254+ runtimeId: string,255+): string => {256+ const safeRuntimeId = sanitizeWslRuntimeId(runtimeId);257+ return [258+ "set -eu",259+ 'runtime_parent="$HOME/.t3/runtime"',260+ `runtime_root="$runtime_parent/${safeRuntimeId}"`,261+ `ready_marker="$runtime_root/${WSL_RUNTIME_READY_MARKER}"`,262+ "runtime_is_ready() {",263+ ' [ -f "$ready_marker" ] &&',264+ ' [ -f "$runtime_root/apps/server/dist/bin.mjs" ] &&',265+ ' [ -f "$runtime_root/node_modules/node-pty/package.json" ]',apps/desktop/src/wsl/DesktopWslEnvironment.ts line 314OutdatedResolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -218,0 +294,21 @@294+ 'if mv -T "$runtime_tmp" "$runtime_root" 2>/dev/null; then',295+ " :",296+ "elif runtime_is_ready; then",297+ ' rm -rf "$runtime_tmp"',298+ "else",299+ ` printf 'Could not promote WSL runtime cache at %s\\n' "$runtime_root" >&2`,300+ " exit 1",301+ "fi",302+ `printf 'runtimeRoot:%s\\n' "$runtime_root"`,303+ ].join("\n");304+};305+306+export const buildWslRuntimePruneScript = (runtimeId: string): string => {307+ const safeRuntimeId = sanitizeWslRuntimeId(runtimeId);308+ return [309+ "set -eu",310+ 'runtime_parent="$HOME/.t3/runtime"',311+ `current_runtime="$runtime_parent/${safeRuntimeId}"`,312+ '[ -d "$runtime_parent" ] || exit 0',313+ 'previous_runtime=""',314+ 'for candidate in "$runtime_parent"/*; do',apps/desktop/src/backend/DesktopBackendConfiguration.ts line 587Resolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -487,8 +518,13 @@487- // WSL backend never needs a compiler, node-gyp, or network on first launch.488- // Compiling from source is a dev-only convenience: a checkout has no shipped489- // prebuilt, and developers have the toolchain. In packaged builds we instead490- // surface a clear diagnostic if the prebuilt can't load (unsupported491- // arch/distro), rather than silently dropping into a fragile runtime build.492- allowBuild: !environment.isPackaged,493- })494- : ({ _tag: "Failed", reason: serverTree.reason, fatal: serverTree.fatal } as const);518+ // The archive is the primary packaged WSL path: it installs directly into519+ // the distro's ext4 filesystem. The server.asar extraction service is only520+ // consulted lazily if the archive is unavailable or cannot be staged.521+ const archivePath = environment.path.join(environment.resourcesPath, WSL_RUNTIME_ARCHIVE_NAME);522+ const archiveHashPath = environment.path.join(523+ environment.resourcesPath,524+ WSL_RUNTIME_ARCHIVE_HASH_NAME,525+ );526+ const hasArchive = environment.isPackaged527+ ? yield* fileSystem.exists(archivePath).pipe(Effect.orElseSucceed(() => false))528+ : false;529+ const archiveHash = hasArchive530+ ? yield* fileSystem.readFileString(archiveHashPath).pipe(
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 459Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -218,0 +308,21 @@308+ return [309+ "set -eu",310+ 'runtime_parent="$HOME/.t3/runtime"',311+ `current_runtime="$runtime_parent/${safeRuntimeId}"`,312+ '[ -d "$runtime_parent" ] || exit 0',313+ 'previous_runtime=""',314+ 'for candidate in "$runtime_parent"/*; do',315+ ' [ -d "$candidate" ] || continue',316+ ' [ "$candidate" != "$current_runtime" ] || continue',317+ ` [ -f "$candidate/${WSL_RUNTIME_READY_MARKER}" ] || continue`,318+ ' if [ -z "$previous_runtime" ] || [ "$candidate" -nt "$previous_runtime" ]; then',319+ ' previous_runtime="$candidate"',320+ " fi",321+ "done",322+ 'for candidate in "$runtime_parent"/*; do',323+ ' [ -d "$candidate" ] || continue',324+ ' [ "$candidate" != "$current_runtime" ] || continue',325+ ' [ "$candidate" != "$previous_runtime" ] || continue',326+ ` [ -f "$candidate/${WSL_RUNTIME_READY_MARKER}" ] || continue`,327+ ' rm -rf -- "$candidate"',328+ "done",
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/desktop/src/backend/DesktopBackendConfiguration.ts line 354OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -310,3 +335,20 @@335+ .pipe(Effect.orElseSucceed(() => false));336+ if (!entryExists) {337+ return {338+ _tag: "Failed",339+ reason: `missing server entry at ${windowsEntryPath}`,340+ fatal: true,341+ } as const;342+ }343+ const mountedAppRoot = yield* wslEnv.windowsToWslPath(runningDistro, serverTree.root);344+ if (Option.isNone(mountedAppRoot)) {345+ return {346+ _tag: "Failed",347+ reason: `wslpath conversion failed for ${serverTree.root}`,348+ fatal: false,349+ } as const;350+ }351+ linuxAppRoot = mountedAppRoot.value;310352 }311353312- const nodePtyResult = yield* wslEnv.ensureNodePty(runningDistro, input.windowsRepoRoot, {354+ const nodePtyResult = yield* wslEnv.ensureNodePty(runningDistro, linuxAppRoot, {
- apps/desktop/src/backend/DesktopBackendConfiguration.ts line 409OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -310,13 +399,11 @@399+ ? failedNodePty(stagedFailure.nodePty)400+ : ({ _tag: "Failed", reason: mounted.reason, fatal: mounted.fatal } as const);310401 }311402312- const nodePtyResult = yield* wslEnv.ensureNodePty(runningDistro, input.windowsRepoRoot, {313- allowBuild: input.allowBuild,314- nodeEngineRange: serverPackageJson.engines.node,315- });403+ const nodePtyResult = yield* wslEnv.ensureNodePty(404+ runningDistro,405+ mounted.linuxAppRoot,406+ nodePtyOptions,407+ );316408 if (!nodePtyResult.ok) {317- return {318- _tag: "Failed",319- reason: `WSL node-pty unavailable: ${nodePtyResult.reason}`,320- fatal: nodePtyResult.fatal,321- ...(nodePtyResult.retryLimit === undefined ? {} : { retryLimit: nodePtyResult.retryLimit }),322- } as const;409+ return failedNodePty(stagedFailure ? stagedFailure.nodePty : nodePtyResult);
- scripts/build-desktop-artifact.ts line 2424OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2351,3 +2417,8 @@23512417 arch: input.arch,23522418 prebuildPath: input.wslPrebuildPath,23532419 });2420+ yield* stageWslRuntimeArchive({2421+ sourceDir: serverStageDir,2422+ archivePath: input.wslRuntimeArchivePath,2423+ hashPath: input.wslRuntimeArchiveHashPath,2424+ });
t3-code-bot[bot](GitHub profile, opens in a new tab) approved these changes
- scripts/build-desktop-artifact.ts line 808Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -795,3 +796,5 @@795796 // staging inputs out of app.asar; they are emitted once at resources/.796797 "!apps/desktop/prod-resources/windows-server",797798 "!apps/desktop/prod-resources/windows-server/**/*",799+ "!apps/desktop/prod-resources/wsl-runtime.tar.gz",800+ "!apps/desktop/prod-resources/wsl-runtime.tar.gz.sha256",
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 369Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -218,0 +316,21 @@316+ 'exec 9> "$runtime_lock"',317+ "flock -x 9",318+ "if runtime_is_ready; then",319+ ` printf 'runtimeRoot:%s\\n' "$runtime_root"`,320+ " exit 0",321+ "fi",322+ // Hash only on a cache miss: a warm launch already exited above, and a cold323+ // install is about to read the whole archive through tar anyway. `set -eu`324+ // turns a distro without sha256sum into an install failure, which falls back325+ // to the mounted server tree rather than trusting unverified bytes.326+ `archive_sha=$(sha256sum ${shellQuote(linuxArchivePath)} | cut -d ' ' -f 1)`,327+ `if [ "$archive_sha" != ${shellQuote(archiveSha256)} ]; then`,328+ ` printf 'WSL runtime archive does not match its recorded SHA-256 (expected %s, got %s)\\n' ${shellQuote(archiveSha256)} "$archive_sha" >&2`,329+ " exit 1",330+ "fi",331+ 'if [ -e "$runtime_root" ]; then',332+ ` runtime_stale=$(mktemp -d "$runtime_parent/.${safeRuntimeId}.stale.XXXXXX")`,333+ ' rmdir "$runtime_stale"',334+ ' if mv -T "$runtime_root" "$runtime_stale" 2>/dev/null; then',335+ ' rm -rf "$runtime_stale"',336+ " fi",
t3-code-bot[bot](GitHub profile, opens in a new tab) requested changes
apps/desktop/src/wsl/DesktopWslEnvironment.ts line 309Resolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -218,0 +285,21 @@285+ 'runtime_parent="$HOME/.t3/runtime"',286+ `runtime_root="$runtime_parent/${safeRuntimeId}"`,287+ `ready_marker="$runtime_root/${WSL_RUNTIME_READY_MARKER}"`,288+ // The native payload is the part of the tree the WSL backend actually289+ // dlopens, and the only part a user can plausibly break by hand. Checking290+ // node-pty's package.json alone let a runtime whose pty.node had gone291+ // missing stay cache-ready forever: every launch reused it and then failed292+ // the native probe, with no reinstall and no fallback. Match on the glob293+ // rather than a mapped `uname -m` so this stays a presence check; the probe294+ // is what decides whether the binary is the right arch and loadable.295+ "node_pty_payload_present() {",296+ ' for candidate in "$1"/node_modules/node-pty/prebuilds/linux-*/pty.node; do',297+ ' [ -f "$candidate" ] || continue',298+ ' [ -f "${candidate%/*}/t3code-wsl-node-pty.json" ] || continue',299+ " return 0",300+ " done",301+ " return 1",302+ "}",303+ "runtime_is_ready() {",304+ ' [ -f "$ready_marker" ] &&',305+ ' [ -f "$runtime_root/apps/server/dist/bin.mjs" ] &&',scripts/build-desktop-artifact.ts line 2362OutdatedResolvedby ikifar2012(GitHub profile, opens in a new tab)@@ -2269,0 +2342,21 @@2342+// makes Git for Windows' GNU tar fail with "Cannot connect to C: resolve2343+// failed". The staged source tree and the archive both live under the build's2344+// stage root, so the target is always expressible relative to tar's cwd.2345+export const wslRuntimeArchiveTarTarget = (relativeArchivePath: string): string =>2346+ relativeArchivePath.replaceAll("\\", "/");2347+2348+// `archivePath` is relative to the cwd tar runs in; see wslRuntimeArchiveTarTarget.2349+export const buildWslRuntimeArchiveArgs = (2350+ archivePath: string = WSL_RUNTIME_ARCHIVE_EXTRA_RESOURCE.from,2351+): ReadonlyArray<string> => [2352+ "-czf",2353+ archivePath,2354+ ...WSL_RUNTIME_ARCHIVE_EXCLUDED_PREFIXES.map((prefix) => `--exclude=${prefix}*`),2355+ WSL_RUNTIME_CONTENT_ID_MANIFEST,2356+ ...WSL_RUNTIME_ARCHIVE_CONTENT_ROOTS,2357+];2358+2359+// The runtime's canonical identity: sha256 over every shipped file's posix2360+// path and bytes, walked in sorted order. It is computed from the staged tree2361+// rather than the archive, and deliberately ignores mtimes, ownership, and2362+// mode, so two builds of the same server payload agree even though their- scripts/build-desktop-artifact.ts lines 2401 to 2403OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2296,0 +2383,21 @@2383+ ...WSL_RUNTIME_ARCHIVE_CONTENT_ROOTS,2384+];2385+2386+// Everything about an entry that survives the trip into the distro and changes2387+// how the runtime behaves: whether it is a file, a directory, or a link, the2388+// bytes of a file, the target of a link, and whether it is executable. Owner2389+// and group bits carry no meaning once tar has restored the tree as whoever ran2390+// the install, so the mode collapses to one bit.2391+const wslRuntimeSemanticMode = (mode: number): string => ((mode & 0o111) === 0 ? "644" : "755");2392+2393+type WslRuntimeWalkEntry =2394+ | { readonly childPath: string; readonly kind: "link"; readonly target: string }2395+ | { readonly childPath: string; readonly kind: "node"; readonly info: FileSystem.File.Info };2396+2397+// The runtime's canonical identity: sha256 over every shipped entry's posix2398+// path, type, semantic mode, and content, walked in sorted order. It is2399+// computed from the staged tree rather than the archive, and deliberately2400+// ignores mtimes and ownership, so two builds of the same server payload agree2401+// even though their tar/gzip bytes do not — gzip stores a timestamp, and tar2402+// records whatever the staging filesystem happened to report. That stability is2403+// what lets a new desktop release reuse a distro-local runtime an older release
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 481OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -218,0 +461,21 @@461+ ' previous_runtime="$candidate"',462+ " fi",463+ "done",464+ 'for candidate in "$runtime_parent"/*; do',465+ ' [ -d "$candidate" ] || continue',466+ ' [ "$candidate" != "$current_runtime" ] || continue',467+ ' [ "$candidate" != "$previous_runtime" ] || continue',468+ ` [ -f "$candidate/${WSL_RUNTIME_READY_MARKER}" ] || continue`,469+ // The newest-previous rule alone picks the wrong survivor whenever two470+ // versions run at once: the other live backend loses its slot to any newer471+ // idle cache, and each side then deletes the other.472+ ' ! runtime_in_use "$candidate" || continue',473+ ' rm -rf -- "$candidate"',474+ "done",475+ // Both loops skip dot entries and require the ready marker, so scratch476+ // directories left by installs that died before promotion (SIGKILL,477+ // `wsl --shutdown`, power loss) are invisible to them, and nothing else ever478+ // removes them. Sweep the ones too old to belong to a live install. Prune479+ // failures are only logged, so a distro without findutils keeps the previous480+ // behavior instead of failing a launch.481+ `find "$runtime_parent" -maxdepth 1 -type d \\( -name '.*.tmp.*' -o -name '.*.stale.*' \\) -mmin +${String(ORPHANED_RUNTIME_SCRATCH_MAX_AGE_MINUTES)} -exec rm -rf -- {} +`,apps/desktop/src/backend/DesktopBackendConfiguration.ts line 380Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -300,0 +360,21 @@360+ | { readonly runtimeId: string; readonly nodePty: FailedNodePtyResult }361+ | undefined;362+363+ if (input.runtimeArchive !== null) {364+ const runtime = yield* wslEnv.prepareRuntime(runningDistro, input.runtimeArchive);365+ if (runtime.ok) {366+ const stagedNodePty = yield* wslEnv.ensureNodePty(367+ runningDistro,368+ runtime.linuxAppRoot,369+ nodePtyOptions,370+ );371+ if (stagedNodePty.ok) {372+ return {373+ _tag: "Ready",374+ runningDistro,375+ windowsEntryPath: environment.backendEntryPath,376+ linuxEntryPath: `${runtime.linuxAppRoot}/apps/server/dist/bin.mjs`,377+ nodePath: stagedNodePty.nodePath,378+ resolvedPath: stagedNodePty.resolvedPath,379+ runtimeId: input.runtimeArchive.runtimeId,380+ } as const;
t3-code-bot[bot](GitHub profile, opens in a new tab) approved these changes
UtkarshUsername(GitHub profile, opens in a new tab) approved these changes
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 458OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -218,0 +438,21 @@438+ ' previous_runtime="$candidate"',439+ " fi",440+ "done",441+ // Only this desktop-owned prefix is eligible. Markerless roots are broken442+ // caches left by invalidation and must not become permanent disk leaks.443+ 'for candidate in "$runtime_parent"/sha256-*; do',444+ ' [ -d "$candidate" ] || continue',445+ ' [ "$candidate" != "$current_runtime" ] || continue',446+ ' [ "$candidate" != "$previous_runtime" ] || continue',447+ ' ! runtime_in_use "$candidate" || continue',448+ " candidate_name=${candidate##*/}",449+ ' candidate_lock="$runtime_parent/.${candidate_name}.install.lock"',450+ ' exec 9> "$candidate_lock"',451+ // A held lock means another launch is installing or repairing this cache.452+ // Skip instead of waiting or deleting underneath it.453+ " flock -n 9 || continue",454+ ' rm -rf -- "$candidate"',455+ " flock -u 9",456+ "done",457+ // Interrupted installs use dot-prefixed names under this dedicated parent.458+ `find "$runtime_parent" -maxdepth 1 -type d \\( -name '.*.tmp.*' -o -name '.*.stale.*' \\) -mmin +${String(ORPHANED_RUNTIME_SCRATCH_MAX_AGE_MINUTES)} -exec rm -rf -- {} +`,
- apps/desktop/src/wsl/DesktopWslEnvironment.ts line 457Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -218,0 +436,21 @@436+ ' [ -d "$candidate" ] || continue',437+ ' [ "$candidate" != "$current_runtime" ] || continue',438+ ` [ -f "$candidate/${WSL_RUNTIME_READY_MARKER}" ] || continue`,439+ ' if [ -z "$previous_runtime" ] || [ "$candidate" -nt "$previous_runtime" ]; then',440+ ' previous_runtime="$candidate"',441+ " fi",442+ "done",443+ // Only this desktop-owned prefix is eligible. Markerless roots are broken444+ // caches left by invalidation and must not become permanent disk leaks.445+ 'for candidate in "$runtime_parent"/sha256-*; do',446+ ' [ -d "$candidate" ] || continue',447+ ' [ "$candidate" != "$current_runtime" ] || continue',448+ ' [ "$candidate" != "$previous_runtime" ] || continue',449+ ' ! runtime_in_use "$candidate" || continue',450+ " candidate_name=${candidate##*/}",451+ ' candidate_lock="$runtime_parent/.${candidate_name}.install.lock"',452+ ' exec 9> "$candidate_lock"',453+ // A held lock means another launch is installing or repairing this cache.454+ // Skip instead of waiting or deleting underneath it.455+ " flock -n 9 || continue",456+ ' rm -rf -- "$candidate"',
- apps/desktop/src/wsl/DesktopWslServerTree.ts line 199Resolvedby t3-code-bot[bot](GitHub profile, opens in a new tab)@@ -173,3 +176,17 @@173176 // first caller extracts, later callers see the marker and reuse the tree.174177 const gate = yield* Semaphore.make(1);175178179+ const cleanupLegacy = gate180+ .withPermits(1)(181+ needsExtraction182+ ? fs.remove(treeRoot, { recursive: true, force: true }).pipe(183+ Effect.catch((cause) =>184+ Effect.logWarning("[wsl-server-tree] Could not remove the legacy extraction cache.", {185+ treeRoot,186+ cause,187+ }),188+ ),189+ )190+ : Effect.void,191+ )192+ .pipe(Effect.withSpan("desktop.wslServerTree.cleanupLegacy"));
shivamhwp(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
901a1437-6992-474d-859a-c6238ec08835You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Comment `@coderabbitai help` to get the list of available commands.