Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 94 additions & 18 deletions apps/server/src/device/DeviceService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,54 @@ describe("device setup consent", () => {
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);

it.effect(
"names npm's release-age policy in host status when min-release-age blocks install",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-release-age-" });
const installError = yield* ensureDeviceHub(baseDir).pipe(
Effect.provideService(ProcessRunner.ProcessRunner, {
run: () =>
Effect.succeed({
code: ChildProcessSpawner.ExitCode(1),
stdout: "",
stderr: [
"npm error code ETARGET",
"npm error notarget No matching version found for expo-device-hub@0.26.1 with a date before 20/11/2026, 10:56:49 am.",
].join("\n"),
timedOut: false,
stdoutTruncated: false,
stderrTruncated: false,
stdoutInvalidUtf8: false,
stderrInvalidUtf8: false,
}),
}),
Effect.flip,
);
const { service, settings } = yield* fixture(
Effect.void,
undefined,
false,
new DeviceHost.DeviceHostError({
hostId: LOCAL_DEVICE_HOST_ID,
step: "installing device support",
cause: installError,
}),
);
yield* Ref.update(settings, (current) => ({ ...current, enableDeviceSupport: true }));
const detail = (yield* service.list).hostStatuses[LOCAL_DEVICE_HOST_ID]?.detail;
expect(detail).toContain("min-release-age");
expect(detail).toContain("20/11/2026, 10:56:49 am");
const unavailable = yield* service.readiness(LOCAL_DEVICE_HOST_ID).pipe(Effect.flip);
expect(unavailable._tag).toBe("DeviceHostUnavailableError");
if (unavailable._tag === "DeviceHostUnavailableError") {
expect(unavailable.reason).toContain("min-release-age");
expect(unavailable.reason).toContain("20/11/2026, 10:56:49 am");
}
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);

it.effect.each([
{
name: "a Windows host",
Expand Down Expand Up @@ -1868,7 +1916,7 @@ it.effect("reconciles SSH host settings without publishing no-op changes", () =>
),
);

it.effect.each(["exit", "missing"] as const)(
it.effect.each(["exit", "missing", "release_age"] as const)(
"a failed tool update reports a safe %s failure",
(failure) =>
Effect.gen(function* () {
Expand Down Expand Up @@ -1900,24 +1948,40 @@ it.effect.each(["exit", "missing"] as const)(
stdoutInvalidUtf8: false,
stderrInvalidUtf8: false,
};
const releaseAgeFailure = {
code: ChildProcessSpawner.ExitCode(1),
stdout: "",
stderr: [
"npm warn registry https://private:credential@registry.example.test/expo-device-hub",
"npm error code ETARGET",
"npm error notarget No matching version found for expo-device-hub@0.26.1 with a date before 20/11/2026, 10:56:49 am.",
].join("\n"),
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",
: failure === "release_age"
? Effect.succeed(releaseAgeFailure)
: 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,
Expand All @@ -1928,13 +1992,25 @@ it.effect.each(["exit", "missing"] as const)(
const error = yield* service.updateTool("hub").pipe(Effect.flip);
expect(error).toMatchObject({
_tag: "DeviceOperationError",
reason: failure === "exit" ? "command_failed" : "package_manager_missing",
reason:
failure === "exit"
? "command_failed"
: failure === "release_age"
? "release_age_blocked"
: "package_manager_missing",
...(failure === "release_age" ? { releaseAgeCutoff: "20/11/2026, 10:56:49 am" } : {}),
});
expect(error.message).toBe(
failure === "exit"
? "Device update device tool failed: The device command failed (exit code 243)."
: "Device update device tool failed: Device support needs npm, which is not on the T3 server's PATH. Install npm (it comes with Node.js from nodejs.org), restart T3 Code, then retry.",
);
if (failure === "release_age") {
expect(error.message).toContain("min-release-age");
expect(error.message).toContain("20/11/2026, 10:56:49 am");
} else {
expect(error.message).toBe(
failure === "exit"
? "Device update device tool failed: The device command failed (exit code 243)."
: "Device update device tool failed: Device support needs npm, which is not on the T3 server's PATH. Install npm (it comes with Node.js from nodejs.org), restart T3 Code, then retry.",
);
}
expect(error.message).not.toContain("private:credential");
}).pipe(
Effect.provide(
Layer.mergeAll(
Expand Down
35 changes: 18 additions & 17 deletions apps/server/src/device/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,26 @@ const isToolchainInstallError = Schema.is(DeviceToolchainInstallError);

const actionableHostFailure = (error: DeviceHost.DeviceHostError): string | undefined => {
if (error.step === "probe") return sshProbeFailureMessage(error);
if (isToolchainInstallError(error.cause) && error.cause.reason === "npm_not_found")
if (isToolchainInstallError(error.cause) && error.cause.reason !== undefined)
return error.cause.message;
return undefined;
};

const toolchainOperationError = (operation: string, cause: DeviceToolchainInstallError) =>
new DeviceOperationError({
operation,
reason:
cause.reason === "npm_not_found"
? "package_manager_missing"
: cause.reason === "release_age_blocked"
? "release_age_blocked"
: "command_failed",
// npm's stderr can carry registry credentials; expose only the reason, cutoff, and exit code.
...(cause.exitCode !== undefined ? { exitCode: cause.exitCode } : {}),
...(cause.releaseAgeCutoff !== undefined ? { releaseAgeCutoff: cause.releaseAgeCutoff } : {}),
cause,
});

export interface DeviceScreenshot {
readonly device: DeviceSummary;
readonly png: Uint8Array;
Expand Down Expand Up @@ -1319,11 +1334,7 @@ export const make = Effect.gen(function* () {
reason: nodeRuntimeUnavailableMessage("Device automation"),
cause: error,
})
: new DeviceOperationError({
operation: "install agent CLI",
reason: "command_failed",
cause: error,
}),
: toolchainOperationError("install agent CLI", error),
),
);
const launchers: AgentDeviceLaunchers = {
Expand Down Expand Up @@ -1430,17 +1441,7 @@ export const make = Effect.gen(function* () {
Effect.provideService(FileSystem.FileSystem, fs),
Effect.provideService(Path.Path, path),
Effect.provideService(ProcessRunner.ProcessRunner, runner),
Effect.mapError(
(cause) =>
new DeviceOperationError({
operation: "update device tool",
reason:
cause.reason === "npm_not_found" ? "package_manager_missing" : "command_failed",
// npm's stderr can carry registry credentials; expose only the reason and exit code.
...(cause.exitCode !== undefined ? { exitCode: cause.exitCode } : {}),
cause,
}),
),
Effect.mapError((cause) => toolchainOperationError("update device tool", cause)),
),
launchers,
);
Expand Down
36 changes: 36 additions & 0 deletions apps/server/src/device/DeviceToolchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@ it.effect("failed installation cleans staging and exposes only a safe failure me
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);

it.effect(
"names npm's release-age cutoff on ETARGET date-cutoff failures without leaking stderr",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-release-age-" });
const result = {
code: ChildProcessSpawner.ExitCode(1),
stdout: "",
stderr: [
"npm warn registry https://private:credential@example.test/package",
"npm error code ETARGET",
"npm error notarget No matching version found for expo-device-hub@0.26.1 with a date before 20/11/2026, 10:56:49 am.",
"npm error notarget In most cases you or one of your dependencies are requesting a package version that doesn't exist.",
].join("\n"),
timedOut: false,
stdoutTruncated: false,
stderrTruncated: false,
stdoutInvalidUtf8: false,
stderrInvalidUtf8: false,
};
const error = yield* ensureDeviceHub(baseDir).pipe(
Effect.provideService(ProcessRunner.ProcessRunner, {
run: () => Effect.succeed(result),
}),
Effect.flip,
);
expect(error.reason).toBe("release_age_blocked");
expect(error.releaseAgeCutoff).toBe("20/11/2026, 10:56:49 am");
expect(error.message).toContain("min-release-age");
expect(error.message).toContain("20/11/2026, 10:56:49 am");
expect(error.message).not.toContain("private:credential");
expect(error.cause).toBe(result);
}).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;
Expand Down
34 changes: 33 additions & 1 deletion apps/server/src/device/DeviceToolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ export class DeviceToolchainInstallError extends Schema.TaggedError<DeviceToolch
tool: Schema.String,
step: Schema.String,
exitCode: Schema.optional(Schema.Number),
reason: Schema.optional(Schema.Literal("npm_not_found")),
reason: Schema.optional(Schema.Literals(["npm_not_found", "release_age_blocked"])),
releaseAgeCutoff: Schema.optional(Schema.String),
cause: Schema.optional(Schema.Defect()),
},
) {
override get message(): string {
if (this.reason === "npm_not_found")
return "Device support needs npm, which is not on the T3 server's PATH. Install npm (it comes with Node.js from nodejs.org), restart T3 Code, then retry.";
if (this.reason === "release_age_blocked") {
const policy = this.releaseAgeCutoff
? `min-release-age / before cutoff: ${this.releaseAgeCutoff}`
: "min-release-age / before";
return `npm blocked installing ${this.tool} because of your npm release-age policy (${policy}). If using min-release-age, wait until the release ages past that cutoff; if using a fixed before date, adjust your npm config, then retry.`;
}
const suffix = this.exitCode === undefined ? "" : ` (exit code ${this.exitCode})`;
return `Installing ${this.tool} failed while ${this.step}${suffix}.`;
}
Expand Down Expand Up @@ -101,6 +108,24 @@ const isCommandNotFound = (error: ProcessRunner.ProcessRunError) =>
error.cause instanceof PlatformError.PlatformError &&
error.cause.reason._tag === "NotFound";

const NPM_ETARGET_PATTERN = /^npm (?:error|ERR!) code ETARGET\b/m;
const NPM_RELEASE_AGE_CUTOFF_PATTERN =
/^npm (?:error|ERR!) notarget No matching version found for [^\s]+ with a date before ([^\r\n]+?)\.?$/m;

const parseReleaseAgeCutoff = (
stderr: string,
): { readonly blocked: true; readonly cutoff?: string } | null => {
if (!NPM_ETARGET_PATTERN.test(stderr)) return null;
const match = NPM_RELEASE_AGE_CUTOFF_PATTERN.exec(stderr);
if (!match) return null;
const rawCutoff = match[1]?.trim();
const cutoff =
rawCutoff && rawCutoff.length <= 64 && !rawCutoff.includes("://") && !rawCutoff.includes("@")
? rawCutoff
: undefined;
return cutoff ? { blocked: true, cutoff } : { blocked: true };
};

const installTool = Effect.fn("DeviceToolchain.installTool")(function* (
spec: ToolSpec,
paths: DeviceToolPaths,
Expand Down Expand Up @@ -158,10 +183,17 @@ const installTool = Effect.fn("DeviceToolchain.installTool")(function* (
),
);
if (result.code !== 0) {
const releaseAge = parseReleaseAgeCutoff(result.stderr);
return yield* new DeviceToolchainInstallError({
tool: spec.name,
step: "running npm install",
exitCode: Number(result.code),
...(releaseAge
? {
reason: "release_age_blocked" as const,
...(releaseAge.cutoff !== undefined ? { releaseAgeCutoff: releaseAge.cutoff } : {}),
}
: {}),
cause: result,
});
}
Expand Down
3 changes: 3 additions & 0 deletions packages/contracts/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,10 @@ export class DeviceOperationError extends Schema.TaggedError<DeviceOperationErro
"settings_failed",
"hub_rejected",
"package_manager_missing",
"release_age_blocked",
]),
exitCode: Schema.optional(Schema.Number),
releaseAgeCutoff: Schema.optional(Schema.String),
cause: Schema.Defect(),
},
) {
Expand All @@ -603,6 +605,7 @@ export class DeviceOperationError extends Schema.TaggedError<DeviceOperationErro
hub_rejected: "The device hub could not complete the request.",
package_manager_missing:
"Device support needs npm, which is not on the T3 server's PATH. Install npm (it comes with Node.js from nodejs.org), restart T3 Code, then retry.",
release_age_blocked: `npm blocked installing device tools because of your npm release-age policy (${this.releaseAgeCutoff ? `min-release-age / before cutoff: ${this.releaseAgeCutoff}` : "min-release-age / before"}). If using min-release-age, wait until the release ages past that cutoff; if using a fixed before date, adjust your npm config, then retry.`,
}[this.reason];
return `Device ${this.operation} failed: ${explanation}`;
}
Expand Down
Loading