From 80f509a27fd1a8d1f00b35c57f1816201f5a2360 Mon Sep 17 00:00:00 2001 From: Kristof Katzenberger Date: Thu, 29 Oct 2026 20:32:52 +0100 Subject: [PATCH 1/2] fix(devices): explain missing npm during tool updates --- apps/server/src/device/DeviceService.test.ts | 138 ++++++++++-------- apps/server/src/device/DeviceService.ts | 4 +- .../server/src/device/DeviceToolchain.test.ts | 39 +++++ apps/server/src/device/DeviceToolchain.ts | 15 +- packages/contracts/src/device.ts | 3 + 5 files changed, 138 insertions(+), 61 deletions(-) diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index a9ff957b9199..0e3d4788cdb6 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -21,6 +21,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as PubSub from "effect/PubSub"; +import * as PlatformError from "effect/PlatformError"; import * as Queue from "effect/Queue"; import * as Ref from "effect/Ref"; import * as Schema from "effect/Schema"; @@ -41,6 +42,8 @@ import { agentDeviceSession } from "./AgentDeviceTarget.ts"; const decodeJson = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown)); const decodeBootError = Schema.decodeUnknownSync(Schema.fromJsonString(DeviceBootError)); const encodeBootError = Schema.encodeSync(Schema.fromJsonString(DeviceBootError)); +const decodeOperationError = Schema.decodeUnknownSync(Schema.fromJsonString(DeviceOperationError)); +const encodeOperationError = Schema.encodeSync(Schema.fromJsonString(DeviceOperationError)); const fixture = Effect.fn("fixture")(function* ( onBoot: Effect.Effect = Effect.void, @@ -1300,67 +1303,86 @@ it.effect("reconciles SSH host settings without publishing no-op changes", () => ), ); -it.effect("a failed tool update reports npm's exit code but not its output", () => - Effect.gen(function* () { - const host: DeviceHost.DeviceHost["Service"] = { - id: LOCAL_DEVICE_HOST_ID, - label: "This machine", - summary: Effect.succeed({ +it.effect.each(["exit", "missing"] as const)( + "a failed tool update reports a safe %s failure", + (failure) => + Effect.gen(function* () { + const host: DeviceHost.DeviceHost["Service"] = { id: LOCAL_DEVICE_HOST_ID, - kind: "local", - label: "Test server", - platforms: [], - hubInstalled: false, - agentDeviceInstalled: false, - }), - platformAvailability: (platform) => Effect.succeed({ platform, available: false }), - ensureReady: () => Effect.die("Updating a tool does not start the hub"), - ensureAgentReady: () => Effect.die("Updating a tool does not start the agent"), - current: Effect.succeed(null), - stopAgent: Effect.void, - stop: Effect.void, - }; - const npmFailure = { - code: ChildProcessSpawner.ExitCode(243), - stdout: "", - stderr: "npm error EACCES https://private:credential@registry.example.test/expo-device-hub", - timedOut: false, - stdoutTruncated: false, - stderrTruncated: false, - stdoutInvalidUtf8: false, - stderrInvalidUtf8: false, - }; - const service = yield* DeviceService.make.pipe( - Effect.provideService(DeviceHost.DeviceHost, host), - Effect.provideService(ProcessRunner.ProcessRunner, { - run: () => Effect.succeed(npmFailure), - }), - Effect.provideService( - HttpClient.HttpClient, - HttpClient.make(() => Effect.die("Updating a tool makes no hub requests")), - ), - ); + label: "This machine", + summary: Effect.succeed({ + id: LOCAL_DEVICE_HOST_ID, + kind: "local", + label: "Test server", + platforms: [], + hubInstalled: false, + agentDeviceInstalled: false, + }), + platformAvailability: (platform) => Effect.succeed({ platform, available: false }), + ensureReady: () => Effect.die("Updating a tool does not start the hub"), + ensureAgentReady: () => Effect.die("Updating a tool does not start the agent"), + current: Effect.succeed(null), + stopAgent: Effect.void, + stop: Effect.void, + }; + const npmFailure = { + code: ChildProcessSpawner.ExitCode(243), + stdout: "", + stderr: "npm error EACCES https://private:credential@registry.example.test/expo-device-hub", + timedOut: false, + stdoutTruncated: false, + stderrTruncated: false, + stdoutInvalidUtf8: false, + stderrInvalidUtf8: false, + }; + const service = yield* DeviceService.make.pipe( + Effect.provideService(DeviceHost.DeviceHost, host), + Effect.provideService(ProcessRunner.ProcessRunner, { + run: ({ command, args }) => + failure === "exit" + ? Effect.succeed(npmFailure) + : Effect.fail( + new ProcessRunner.ProcessSpawnError({ + command, + argumentCount: args.length, + cause: PlatformError.systemError({ + _tag: "NotFound", + module: "ChildProcess", + method: "spawn", + description: "private diagnostic text", + }), + }), + ), + }), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make(() => Effect.die("Updating a tool makes no hub requests")), + ), + ); - const error = yield* service.updateTool("hub").pipe(Effect.flip); - expect(error).toMatchObject({ - _tag: "DeviceOperationError", - reason: "command_failed", - exitCode: 243, - }); - expect(error.message).toBe( - "Device update device tool failed: The device command failed (exit code 243).", - ); - }).pipe( - Effect.provide( - Layer.mergeAll( - ServerSettings.layerTest({ enableDeviceSupport: true }), - ServerConfig.layerTest(process.cwd(), { prefix: "t3-device-update-" }), - NodeCrypto.layer, - NetService.layer, - ).pipe(Layer.provideMerge(NodeServices.layer)), + const error = yield* service.updateTool("hub").pipe(Effect.flip); + expect(error).toMatchObject({ + _tag: "DeviceOperationError", + reason: failure === "exit" ? "command_failed" : "package_manager_missing", + }); + if (error._tag !== "DeviceOperationError") throw error; + const publicError = decodeOperationError(encodeOperationError(error)); + expect(publicError.message).toBe( + failure === "exit" + ? "Device update device tool failed: The device command failed (exit code 243)." + : "Device update device tool failed: Neither npm nor pnpm was found on this host. Install Node.js with npm on this host, make npm available to the T3 server, then retry.", + ); + }).pipe( + Effect.provide( + Layer.mergeAll( + ServerSettings.layerTest({ enableDeviceSupport: true }), + ServerConfig.layerTest(process.cwd(), { prefix: "t3-device-update-" }), + NodeCrypto.layer, + NetService.layer, + ).pipe(Layer.provideMerge(NodeServices.layer)), + ), + Effect.scoped, ), - Effect.scoped, - ), ); describe("locating a device id across hosts", () => { diff --git a/apps/server/src/device/DeviceService.ts b/apps/server/src/device/DeviceService.ts index b2ef41f148a1..28e6df05bb2f 100644 --- a/apps/server/src/device/DeviceService.ts +++ b/apps/server/src/device/DeviceService.ts @@ -1312,8 +1312,8 @@ export const make = Effect.gen(function* () { (cause) => new DeviceOperationError({ operation: "update device tool", - reason: "command_failed", - // Only the exit code goes into the shown message; npm's stderr can carry registry credentials. + reason: cause.reason ?? "command_failed", + // npm's stderr can carry registry credentials; expose only the reason and exit code. ...(cause.exitCode !== undefined ? { exitCode: cause.exitCode } : {}), cause, }), diff --git a/apps/server/src/device/DeviceToolchain.test.ts b/apps/server/src/device/DeviceToolchain.test.ts index 78dc719b141e..b70ec0037aaa 100644 --- a/apps/server/src/device/DeviceToolchain.test.ts +++ b/apps/server/src/device/DeviceToolchain.test.ts @@ -44,6 +44,45 @@ it.effect("failed installation cleans staging and exposes only a safe failure me }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); +it.effect.each([ + { npmReason: "NotFound", pnpmReason: "NotFound", reason: "package_manager_missing" }, + { npmReason: "NotFound", pnpmReason: "PermissionDenied", reason: undefined }, + { npmReason: "PermissionDenied", pnpmReason: "NotFound", reason: undefined }, +] as const)( + "classifies npm $npmReason and pnpm $pnpmReason spawn failures", + ({ npmReason, pnpmReason, reason }) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-install-" }); + const commands: string[] = []; + const error = yield* ensureDeviceHub(baseDir).pipe( + Effect.provideService(ProcessRunner.ProcessRunner, { + run: ({ command, args }) => { + commands.push(command); + return Effect.fail( + new ProcessRunner.ProcessSpawnError({ + command, + argumentCount: args.length, + cause: PlatformError.systemError({ + _tag: command === "npm" ? npmReason : pnpmReason, + module: "ChildProcess", + method: "spawn", + description: "private diagnostic text", + }), + }), + ); + }, + }), + Effect.flip, + ); + expect(error.reason).toBe(reason); + expect(error.message).not.toContain("private diagnostic text"); + expect(commands).toEqual(npmReason === "NotFound" ? ["npm", "pnpm"] : ["npm"]); + expect(yield* fs.readDirectory(path.join(baseDir, "tools", "expo-device-hub"))).toEqual([]); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + it.effect("inventory reports only completed versions without installing the required version", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/device/DeviceToolchain.ts b/apps/server/src/device/DeviceToolchain.ts index 250e6cf14f3b..c04b5cfde7a9 100644 --- a/apps/server/src/device/DeviceToolchain.ts +++ b/apps/server/src/device/DeviceToolchain.ts @@ -36,6 +36,7 @@ export class DeviceToolchainInstallError extends Schema.TaggedError + new DeviceToolchainInstallError({ + tool: spec.name, + step: "running npm install", + ...(cause._tag === "ProcessSpawnError" && + cause.cause instanceof PlatformError.PlatformError && + cause.cause.reason._tag === "NotFound" + ? { reason: "package_manager_missing" as const } + : {}), + cause, + }), + ), ); if (result.code !== 0) { return yield* new DeviceToolchainInstallError({ diff --git a/packages/contracts/src/device.ts b/packages/contracts/src/device.ts index 24b72dbe26d7..276860b2790c 100644 --- a/packages/contracts/src/device.ts +++ b/packages/contracts/src/device.ts @@ -583,6 +583,7 @@ export class DeviceOperationError extends Schema.TaggedError Date: Tue, 3 Nov 2026 23:08:42 +0000 Subject: [PATCH 2/2] fix(devices): explain restart after installing npm --- apps/server/src/device/DeviceService.test.ts | 2 +- packages/contracts/src/device.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index 353ca1691f91..5a3736c09187 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -1523,7 +1523,7 @@ it.effect.each(["exit", "missing"] as const)( expect(publicError.message).toBe( failure === "exit" ? "Device update device tool failed: The device command failed (exit code 243)." - : "Device update device tool failed: Neither npm nor pnpm was found on this host. Install Node.js with npm on this host, make npm available to the T3 server, then retry.", + : "Device update device tool failed: Neither npm nor pnpm was found on this host. Install Node.js with npm on this host, make npm available to the T3 server, then fully restart T3 Code (including the tray app) or the T3 server before retrying.", ); }).pipe( Effect.provide( diff --git a/packages/contracts/src/device.ts b/packages/contracts/src/device.ts index 158eed2857d3..0f935a74eb4b 100644 --- a/packages/contracts/src/device.ts +++ b/packages/contracts/src/device.ts @@ -598,7 +598,7 @@ export class DeviceOperationError extends Schema.TaggedError