@devries-cameron20 / t3code-1 / commits / 8bcc9911f1
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(cli): keep published runtimes in place during installation (#24092)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Victor Vogel committed Oct 29, 2026 at 13:05 UTC 8bcc9911f11e5dd9666e1a1b625766e0ccd2cb49
2 files changed +139 -2
scripts/build-npm-launcher.test.ts
+112
@@ -195,6 +195,118 @@ it.layer(NodeServices.layer)("build-npm-launcher", (it) => {
195 VERSION,
196 ]);
197
198 + // A peer's executable and sidecar stay available after every later rename or removal,
199 + // even when it publishes after our missing-marker read.
200 + const staleHome = path.join(root, "stale-home");
201 + const staleDir = path.join(staleHome, "runtime/versions", VERSION);
202 + const peerDir = path.join(root, "peer-runtime");
203 + yield* fs.makeDirectory(staleDir, { recursive: true });
204 + yield* fs.writeFileString(path.join(staleDir, "t3"), "");
205 + yield* fs.makeDirectory(peerDir);
206 + yield* fs.writeFileString(path.join(peerDir, "t3"), '#!/bin/sh\necho "peer $*"\nexit 7\n');
207 + yield* fs.chmod(path.join(peerDir, "t3"), 0o755);
208 + yield* fs.writeFileString(path.join(peerDir, ".install-complete"), `${VERSION}\n`);
209 + yield* fs.writeFileString(path.join(peerDir, "sidecar"), "peer sidecar");
210 + const peerChecks = path.join(root, "peer-checks.txt");
211 + // The peer publishes after the launcher's rename fails and it reads the marker.
212 + const peerHook = path.join(root, "peer-publishes.mjs");
213 + yield* fs.writeFileString(
214 + peerHook,
215 + `import fs from "node:fs";
216 +import { spawnSync } from "node:child_process";
217 +const dir = ${JSON.stringify(staleDir)};
218 +const { readFileSync, renameSync, rmSync } = fs;
219 +let checking = false;
220 +let published = false;
221 +const checkPeer = () => {
222 + const child = spawnSync(dir + "/t3", ["--version"], { encoding: "utf8" });
223 + let sidecar;
224 + try {
225 + sidecar = readFileSync(dir + "/sidecar", "utf8");
226 + } catch (error) {
227 + sidecar = error.code;
228 + }
229 + fs.appendFileSync(${JSON.stringify(peerChecks)},
230 + [child.status, child.error?.code ?? child.stdout.trim(), sidecar].join("|") + "\\n");
231 +};
232 +fs.renameSync = (from, to) => {
233 + try {
234 + return renameSync(from, to);
235 + } catch (error) {
236 + checking ||= to === dir;
237 + throw error;
238 + } finally {
239 + if (published) checkPeer();
240 + }
241 +};
242 +fs.rmSync = (...args) => {
243 + try {
244 + return rmSync(...args);
245 + } finally {
246 + if (published) checkPeer();
247 + }
248 +};
249 +fs.readFileSync = (file, ...rest) => {
250 + try {
251 + return readFileSync(file, ...rest);
252 + } finally {
253 + if (checking && file === dir + "/.install-complete") {
254 + checking = false;
255 + rmSync(dir, { recursive: true });
256 + renameSync(${JSON.stringify(peerDir)}, dir);
257 + published = true;
258 + checkPeer();
259 + }
260 + }
261 +};
262 +`,
263 + );
264 + const recovered = yield* run(
265 + process.execPath,
266 + ["--import", peerHook, launcher, "--version"],
267 + { cwd: root, env: { ...env, T3CODE_HOME: staleHome } },
268 + );
269 + const checks = (yield* fs.readFileString(peerChecks)).trim().split("\n");
270 + assert.isAtLeast(checks.length, 2);
271 + assert.deepStrictEqual([...new Set(checks)], ["7|peer --version|peer sidecar"]);
272 + assert.equal(recovered.stdout.trim(), "peer --version", recovered.stderr);
273 + assert.equal(recovered.exitCode, 7);
274 + assert.deepStrictEqual(yield* fs.readDirectory(path.join(staleHome, "runtime/versions")), [
275 + VERSION,
276 + ]);
277 +
278 + // While another launch holds the lock, a stale folder is left alone and the error says
279 + // how to clear a lock left by a crash.
280 + const staleOnlyHome = path.join(root, "stale-only-home");
281 + const staleOnlyDir = path.join(staleOnlyHome, "runtime/versions", VERSION);
282 + yield* fs.makeDirectory(staleOnlyDir, { recursive: true });
283 + yield* fs.writeFileString(path.join(staleOnlyDir, "t3"), "incomplete");
284 + yield* fs.makeDirectory(`${staleOnlyDir}.lock`);
285 + const locked = yield* run(process.execPath, [launcher, "--version"], {
286 + cwd: root,
287 + env: { ...env, T3CODE_HOME: staleOnlyHome },
288 + });
289 + assert.equal(locked.exitCode, 1);
290 + assert.include(locked.stderr, `delete ${staleOnlyDir}.lock`);
291 + assert.equal(yield* fs.readFileString(path.join(staleOnlyDir, "t3")), "incomplete");
292 +
293 + // Without a peer, the stale folder is replaced by our own download.
294 + yield* fs.remove(`${staleOnlyDir}.lock`, { recursive: true });
295 + const replaced = yield* run(process.execPath, [launcher, "--version"], {
296 + cwd: root,
297 + env: { ...env, T3CODE_HOME: staleOnlyHome },
298 + });
299 + assert.equal(
300 + replaced.stdout.trim(),
301 + `stub --version launcher=${launcher}`,
302 + replaced.stderr,
303 + );
304 + assert.equal(replaced.exitCode, 7);
305 + assert.deepStrictEqual(
306 + yield* fs.readDirectory(path.join(staleOnlyHome, "runtime/versions")),
307 + [VERSION],
308 + );
309 +
310 release.corruptChecksum();
311 const corrupt = yield* run(process.execPath, [launcher, "--version"], {
312 cwd: root,
scripts/npm-launcher/t3.mjs
+27 -2
@@ -109,11 +109,36 @@ const isComplete = (dir) => {
109 function publish(staging, dir) {
110 try {
111 fs.renameSync(staging, dir);
112 + return;
113 } catch (error) {
114 if (isComplete(dir)) return;
115 if (!fs.existsSync(dir)) throw error;
115 - fs.rmSync(dir, { recursive: true, force: true });
116 - fs.renameSync(staging, dir);
116 + }
117 + const lock = `${dir}.lock`;
118 + try {
119 + fs.mkdirSync(lock);
120 + } catch (error) {
121 + if (error.code !== "EEXIST") throw error;
122 + throw new Error(
123 + `another t3 is installing ${version}. If none is running, delete ${lock} and try again.`,
124 + { cause: error },
125 + );
126 + }
127 + const stale = `${staging}.stale`;
128 + try {
129 + // A peer may have published between our marker check and taking the lock.
130 + if (isComplete(dir)) return;
131 + // Move it aside rather than deleting in place: once the folder is empty, a
132 + // peer's first rename can land in it.
133 + fs.renameSync(dir, stale);
134 + try {
135 + fs.renameSync(staging, dir);
136 + } catch (error) {
137 + if (!isComplete(dir)) throw error;
138 + }
139 + } finally {
140 + fs.rmSync(lock, { recursive: true, force: true });
141 + fs.rmSync(stale, { recursive: true, force: true });
142 }
143 }
144