Skip to content

Commit 71e0148

Browse files
donjor-agent[bot]donjor
authored andcommitted
fix(server): a client server update repoints the install.sh t3 launcher (#1641)
## Problem A client **Update server** moves the service to the new runtime but leaves the `install.sh` `t3` symlink on the old one. After the `.957` update, `t3 --version` on PATH still printed `.933`, without the `--mcp-*` device-approval flags. Only `t3 update` repoints that link. ## Change | Piece | Detail | | --- | --- | | Logic | new `launcherRepoint.fork.ts` | | Reuse | upstream's `repointLauncher` and ownership rule | | Load | lazy; a static import closes a module cycle | | Candidates | `T3CODE_INSTALL_BIN_DIR`, PATH, `~/.local/bin` | | Owned only | links into this home's `runtime/versions` | | When | after the service launcher accepts the handoff | | Failure | logged; the update still proceeds | | Seam | two `fork-hook` lines in `selfUpdate.ts` | | Scan | `selfUpdate.ts`, `cli/update.ts` in upstream-fixes | The launcher waits for a handoff delay, then gives a 5s terminate grace, so the repoint finishes inside the uninterruptible handoff. Windows has no boot service, so client updates never reach it there. ## Scope and approval Refs #1639: Jordon asked for the client update to move the PATH `t3`. The Task's live condition needs a nightly carrying this change, so it stays open after merge. ## Close conditions ### Behavior - [x] **Owned link:** repointed to the target - [x] **Foreign link:** untouched - [x] **Plain executable copy:** untouched - [x] **Failed preflight:** link unchanged - [x] **No owned link:** update unchanged ### Fork shape - [x] **Seam:** `fork-hook` lines in `selfUpdate.ts` - [x] **Logic:** in the `.fork.ts` file - [x] **Domain:** `upstream-fixes` - [x] **Tier:** `bugfix` - [x] **Upstreamable:** `yes` - [x] **Gate:** `vp run fork:ci` passes ## Verification | Check | Result | | --- | --- | | `vp test run` fork + `selfUpdate` tests | 16 passed | | Hook line commented out | seam test fails | | `vp run typecheck` (server) | exit 0 | | `vp lint` changed files | exit 0 | | `vp run fork:ci` | exit 0 | | `vp run build:bundle` (server) | exit 0 | | `ExternalMcpServer.auth.fork.test.ts` | passes; failed on the static import | Not checked: a live client update; that waits for the next nightly. Claude Opus 5.5 via Claude Code in T3 Code. ## Fork trailers Squashes: - a1faf7d fix(server): a client server update repoints the install.sh t3 launcher (#1641) (#1641) - c86ec17 fixup! fix(server): a client server update repoints the install.sh t3 launcher (#1641) (#1641) Fork-Domain: upstream-fixes Fork-Tier: bugfix Fork-Upstreamable: yes Co-authored-by: donjor <[email protected]>
1 parent d13e32b commit 71e0148

4 files changed

Lines changed: 277 additions & 4 deletions

File tree

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { expect, it } from "@effect/vitest";
3+
import {
4+
HostProcessArchitecture,
5+
HostProcessEnvironment,
6+
HostProcessPlatform,
7+
} from "@t3tools/shared/hostProcess";
8+
import * as Effect from "effect/Effect";
9+
import * as FileSystem from "effect/FileSystem";
10+
import * as Path from "effect/Path";
11+
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
12+
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
13+
14+
import * as ServerConfig from "../config.ts";
15+
import * as DesktopAppUpdate from "../desktopUpdate/DesktopAppUpdate.ts";
16+
import * as ProcessRunner from "../processRunner.ts";
17+
import { repointLaunchersFork } from "./launcherRepoint.fork.ts";
18+
import * as ServerSelfUpdate from "./selfUpdate.ts";
19+
import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
20+
import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
21+
22+
// A home with an installed old runtime, and an install.sh-style `bin/t3`
23+
// symlink pointing at it.
24+
const makeHome = Effect.fn("test.make_launcher_home")(function* () {
25+
const fs = yield* FileSystem.FileSystem;
26+
const path = yield* Path.Path;
27+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-launcher-repoint-test-" });
28+
const baseDir = path.join(root, "home");
29+
const versionsDir = path.join(baseDir, "runtime", "versions");
30+
const binDir = path.join(root, "bin");
31+
yield* fs.makeDirectory(path.join(versionsDir, "1.0.0"), { recursive: true });
32+
yield* fs.makeDirectory(binDir, { recursive: true });
33+
yield* fs.writeFileString(path.join(versionsDir, "1.0.0", "t3"), "#!/bin/sh\n");
34+
const launcher = path.join(binDir, "t3");
35+
const target = (version: string) => path.join(versionsDir, version, "t3");
36+
return { fs, path, root, baseDir, binDir, launcher, target };
37+
});
38+
39+
const repoint = (
40+
home: Effect.Success<ReturnType<typeof makeHome>>,
41+
environment: NodeJS.ProcessEnv,
42+
) =>
43+
repointLaunchersFork(home.fs, home.path, home.baseDir, "1.1.0").pipe(
44+
Effect.provideService(HostProcessEnvironment, environment),
45+
Effect.provideService(HostProcessPlatform, "linux"),
46+
);
47+
48+
const runResult = (stdout: string) => ({
49+
stdout,
50+
stderr: "",
51+
code: ChildProcessSpawner.ExitCode(0),
52+
timedOut: false,
53+
stdoutTruncated: false,
54+
stderrTruncated: false,
55+
stdoutInvalidUtf8: false,
56+
stderrInvalidUtf8: false,
57+
});
58+
59+
// The self-update service with a fake release download, tar, and preflight,
60+
// so the hook is proven at its seam rather than only in isolation.
61+
const makeSelfUpdate = Effect.fn("test.make_launcher_self_update")(function* (
62+
home: Effect.Success<ReturnType<typeof makeHome>>,
63+
preflight: "ready" | "blocked",
64+
) {
65+
const { fs, path } = home;
66+
const archiveBytes = new TextEncoder().encode("not really a tarball");
67+
const digest = yield* Effect.promise(() => crypto.subtle.digest("SHA-256", archiveBytes));
68+
const hex = Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0"));
69+
const httpClient = HttpClient.make((request) =>
70+
Effect.succeed(
71+
HttpClientResponse.fromWeb(
72+
request,
73+
new Response(
74+
request.url.endsWith("/SHA256SUMS")
75+
? `${hex.join("")} t3-1.1.0-linux-x64.tar.gz\n`
76+
: archiveBytes,
77+
),
78+
),
79+
),
80+
);
81+
const preflightResult =
82+
preflight === "ready"
83+
? { status: "ready", version: "1.1.0", launcherProtocol: SERVICE_LAUNCHER_PROTOCOL }
84+
: { status: "blocked", version: "1.1.0", reason: "local update required" };
85+
// @effect-diagnostics-next-line preferSchemaOverJson:off - fake child-process stdout.
86+
const preflightStdout = JSON.stringify(preflightResult);
87+
const runner = ProcessRunner.ProcessRunner.of({
88+
run: (input) => {
89+
if (input.command !== "tar") return Effect.succeed(runResult(preflightStdout));
90+
const stagingDir = input.args[input.args.indexOf("-C") + 1];
91+
if (stagingDir === undefined) return Effect.die("missing tar target");
92+
return fs
93+
.writeFileString(path.join(stagingDir, "t3"), "")
94+
.pipe(Effect.orDie, Effect.as(runResult("")));
95+
},
96+
});
97+
const config = yield* ServerConfig.ServerConfig.pipe(
98+
Effect.provide(ServerConfig.layerTest(process.cwd(), home.baseDir)),
99+
);
100+
return yield* ServerSelfUpdate.make().pipe(
101+
Effect.provideService(ProcessRunner.ProcessRunner, runner),
102+
Effect.provideService(
103+
ServiceLauncherClient.ServiceLauncherClient,
104+
ServiceLauncherClient.ServiceLauncherClient.of({
105+
managed: true,
106+
requestUpdate: () => Effect.succeed("launcher-id"),
107+
prepareTrial: Effect.undefined,
108+
}),
109+
),
110+
Effect.provideService(DesktopAppUpdate.DesktopAppUpdate, {
111+
available: false,
112+
run: () => Effect.die("unexpected desktop app update run"),
113+
commit: () => Effect.die("unexpected desktop app update commit"),
114+
}),
115+
Effect.provideService(HttpClient.HttpClient, httpClient),
116+
Effect.provideService(HostProcessPlatform, "linux"),
117+
Effect.provideService(HostProcessArchitecture, "x64"),
118+
Effect.provide(ServerConfig.layer({ ...config, mode: "web" })),
119+
);
120+
});
121+
122+
it.layer(NodeServices.layer)("launcher repoint on a client update", (it) => {
123+
it.effect("repoints the owned launcher once the service launcher accepts the update", () =>
124+
Effect.gen(function* () {
125+
const home = yield* makeHome();
126+
yield* home.fs.symlink(home.target("1.0.0"), home.launcher);
127+
const selfUpdate = yield* makeSelfUpdate(home, "ready");
128+
yield* selfUpdate
129+
.update({ targetVersion: "1.1.0" })
130+
.pipe(Effect.provideService(HostProcessEnvironment, { PATH: home.binDir }));
131+
expect(yield* home.fs.readLink(home.launcher)).toBe(home.target("1.1.0"));
132+
}),
133+
);
134+
135+
it.effect("leaves the launcher when the staged preflight refuses the update", () =>
136+
Effect.gen(function* () {
137+
const home = yield* makeHome();
138+
yield* home.fs.symlink(home.target("1.0.0"), home.launcher);
139+
const selfUpdate = yield* makeSelfUpdate(home, "blocked");
140+
yield* selfUpdate
141+
.update({ targetVersion: "1.1.0" })
142+
.pipe(Effect.provideService(HostProcessEnvironment, { PATH: home.binDir }), Effect.flip);
143+
expect(yield* home.fs.readLink(home.launcher)).toBe(home.target("1.0.0"));
144+
}),
145+
);
146+
147+
it.effect("finds the install default bin dir when the service PATH omits it", () =>
148+
Effect.gen(function* () {
149+
const home = yield* makeHome();
150+
const defaultBin = home.path.join(home.root, ".local", "bin");
151+
yield* home.fs.makeDirectory(defaultBin, { recursive: true });
152+
yield* home.fs.symlink(home.target("1.0.0"), home.path.join(defaultBin, "t3"));
153+
expect(yield* repoint(home, { HOME: home.root, PATH: "/nonexistent" })).toEqual([
154+
home.path.join(defaultBin, "t3"),
155+
]);
156+
expect(yield* home.fs.readLink(home.path.join(defaultBin, "t3"))).toBe(home.target("1.1.0"));
157+
}),
158+
);
159+
160+
it.effect("leaves a launcher for another home and a plain copy alone", () =>
161+
Effect.gen(function* () {
162+
const home = yield* makeHome();
163+
const foreignTarget = home.path.join(
164+
home.root,
165+
"other",
166+
"runtime",
167+
"versions",
168+
"1.0.0",
169+
"t3",
170+
);
171+
yield* home.fs.symlink(foreignTarget, home.launcher);
172+
const copyDir = home.path.join(home.root, "copy");
173+
yield* home.fs.makeDirectory(copyDir);
174+
yield* home.fs.writeFileString(home.path.join(copyDir, "t3"), "#!/bin/sh\n");
175+
expect(yield* repoint(home, { PATH: `${home.binDir}:${copyDir}` })).toEqual([]);
176+
expect(yield* home.fs.readLink(home.launcher)).toBe(foreignTarget);
177+
expect(yield* home.fs.readFileString(home.path.join(copyDir, "t3"))).toBe("#!/bin/sh\n");
178+
}),
179+
);
180+
181+
it.effect("does nothing when no launcher is installed", () =>
182+
Effect.gen(function* () {
183+
const home = yield* makeHome();
184+
expect(yield* repoint(home, { PATH: home.binDir })).toEqual([]);
185+
}),
186+
);
187+
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
2+
import * as Effect from "effect/Effect";
3+
import * as FileSystem from "effect/FileSystem";
4+
import * as Option from "effect/Option";
5+
import * as Path from "effect/Path";
6+
7+
import { pinnedRuntimePaths, pinnedRuntimeVersionsDir } from "./pinnedRuntime.ts";
8+
9+
/**
10+
* Directories an install script may have put the `t3` launcher in: its
11+
* `T3CODE_INSTALL_BIN_DIR` override, every PATH entry, and its `~/.local/bin`
12+
* default. The service's PATH is not the user's shell PATH, so the default is
13+
* checked even when PATH omits it.
14+
*/
15+
const launcherCandidates = Effect.gen(function* () {
16+
const path = yield* Path.Path;
17+
const environment = yield* HostProcessEnvironment;
18+
const directories = [
19+
environment["T3CODE_INSTALL_BIN_DIR"],
20+
...(environment["PATH"] ?? "").split(":"),
21+
environment["HOME"] ? path.join(environment["HOME"], ".local", "bin") : undefined,
22+
].filter((entry): entry is string => entry !== undefined && entry.trim().length > 0);
23+
return [...new Set(directories.map((directory) => path.join(path.resolve(directory), "t3")))];
24+
});
25+
26+
/**
27+
* Points every `install.sh` launcher this home owns at the runtime a client update just handed
28+
* the service launcher, as `t3 update` does. A launcher for another home, a plain copy, or a
29+
* failed write is left alone: the server update must not fail over the PATH convenience link.
30+
*/
31+
const repointLaunchers = Effect.fn("cloud.server_self_update.repoint_launchers")(function* (
32+
baseDir: string,
33+
targetVersion: string,
34+
) {
35+
const path = yield* Path.Path;
36+
const platform = yield* HostProcessPlatform;
37+
// The boot service, and with it client updates, exists only on Linux and macOS.
38+
if (platform === "win32") return [];
39+
const versionsDir = pinnedRuntimeVersionsDir(path, baseDir);
40+
const { entryPath } = pinnedRuntimePaths(path, baseDir, targetVersion, platform);
41+
// Loaded lazily: `cli/update.ts` reaches back to this module's importer
42+
// through `serverRuntimeState.ts` and `ServerEnvironment.ts`.
43+
const { repointLauncher } = yield* Effect.promise(() => import("../cli/update.ts"));
44+
const repointed: Array<string> = [];
45+
for (const candidate of yield* launcherCandidates) {
46+
const result = yield* repointLauncher({
47+
launchedAs: candidate,
48+
versionsDir,
49+
targetEntryPath: entryPath,
50+
}).pipe(
51+
Effect.catch((error) =>
52+
Effect.logWarning("Could not repoint a t3 launcher.", {
53+
candidate,
54+
reason: error.reason,
55+
}).pipe(Effect.as(Option.none<string>())),
56+
),
57+
);
58+
if (Option.isSome(result)) repointed.push(result.value);
59+
}
60+
if (repointed.length > 0) {
61+
yield* Effect.logInfo("Repointed t3 launchers to the updated runtime.", {
62+
launchers: repointed,
63+
targetVersion,
64+
});
65+
}
66+
return repointed;
67+
});
68+
69+
export const repointLaunchersFork = (
70+
fs: FileSystem.FileSystem,
71+
path: Path.Path,
72+
baseDir: string,
73+
targetVersion: string,
74+
) =>
75+
repointLaunchers(baseDir, targetVersion).pipe(
76+
// The launcher has already accepted the update, so nothing here may fail it.
77+
Effect.catchCause((cause) =>
78+
Effect.logWarning("Could not repoint t3 launchers.", { cause }).pipe(Effect.as([])),
79+
),
80+
Effect.provideService(FileSystem.FileSystem, fs),
81+
Effect.provideService(Path.Path, path),
82+
);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
PinnedRuntimePreflightBlockedError,
3838
prunePinnedRuntimes,
3939
} from "./pinnedRuntime.ts";
40+
import { repointLaunchersFork } from "./launcherRepoint.fork.ts"; // fork-hook: upstream-fixes/launcher-repoint-import
4041
import { decodeServicePreflightResult } from "./servicePreflight.ts";
4142
import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
4243
import { isExactServiceVersion, SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
@@ -354,6 +355,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
354355
),
355356
),
356357
Effect.tap(() => onHandoffAccepted()),
358+
Effect.tap(() => repointLaunchersFork(fs, path, serverConfig.baseDir, targetVersion)), // fork-hook: upstream-fixes/launcher-repoint
357359
),
358360
);
359361

‎docs/fork/internals/fork-delta.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,12 @@ The domain retires when it is empty.
690690

691691
### Rebase scan
692692

693-
| Path | Why it matters |
694-
| ------------------------------ | ------------------------------------------------------------------------------------------------------- |
695-
| `native/libghostty-vt/VERSION` | Moved: rebuild `ghostty-sprite.wasm`; Ghostty `76e568b` or later needs a `global.zig` stub and Zig 0.16 |
696-
| `**` | Other shared paths |
693+
| Path | Why it matters |
694+
| ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
695+
| `native/libghostty-vt/VERSION` | Moved: rebuild `ghostty-sprite.wasm`; Ghostty `76e568b` or later needs a `global.zig` stub and Zig 0.16 |
696+
| `apps/server/src/cloud/selfUpdate.ts` | Launcher repoint runs after the handoff is accepted; retire once a client update repoints `install.sh`'s `t3` upstream |
697+
| `apps/server/src/cli/update.ts` | `repointLauncher` and its ownership rule are reused, not copied |
698+
| `**` | Other shared paths |
697699

698700
## zmux-estate
699701

0 commit comments

Comments
 (0)