Skip to content

Commit 35c17f1

Browse files
committed
fix(server): npm provider updates only offer versions npm will install
1 parent d7a8d6f commit 35c17f1

3 files changed

Lines changed: 158 additions & 21 deletions

File tree

‎apps/server/src/provider/Drivers/CodexDriver.test.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ it.layer(layerTest)("CodexDriver", (it) => {
516516
const shimDir = NodePath.join(tempDir, fixture.dataRoot, "shims");
517517
const npmPrefix = NodePath.join(tempDir, "mise", "installs", "node", "24.13.0");
518518
const npmBin = NodePath.join(npmPrefix, "bin");
519+
const npmPath = NodePath.join(npmBin, "npm");
519520
const npmEntry = NodePath.join(
520521
npmPrefix,
521522
"lib",
@@ -525,7 +526,7 @@ it.layer(layerTest)("CodexDriver", (it) => {
525526
"bin",
526527
"codex.js",
527528
);
528-
for (const file of [brewPath, misePath, npmEntry]) {
529+
for (const file of [brewPath, misePath, npmPath, npmEntry]) {
529530
yield* fs.makeDirectory(NodePath.dirname(file), { recursive: true });
530531
yield* fs.writeFileString(file, "#!/bin/sh\n");
531532
yield* fs.chmod(file, 0o755);
@@ -540,12 +541,20 @@ it.layer(layerTest)("CodexDriver", (it) => {
540541
].join(NodePath.delimiter);
541542
const probes: Array<ReadonlyArray<string>> = [];
542543
const metadataSpawner = ChildProcessSpawner.make((command) => {
543-
if (!ChildProcess.isStandardCommand(command) || command.command !== brewPath) {
544+
const isNpmOutdated =
545+
ChildProcess.isStandardCommand(command) &&
546+
command.command === npmPath &&
547+
command.args[0] === "outdated";
548+
if (
549+
!ChildProcess.isStandardCommand(command) ||
550+
(command.command !== brewPath && !isNpmOutdated)
551+
) {
544552
return Effect.die("Provider resolution must not execute a provider or updater");
545553
}
546-
probes.push(command.args);
547-
const stdout =
548-
command.args[0] === "--prefix"
554+
if (!isNpmOutdated) probes.push(command.args);
555+
const stdout = isNpmOutdated
556+
? JSON.stringify({ "@openai/codex": { latest: "0.153.4" } })
557+
: command.args[0] === "--prefix"
549558
? brewPrefix
550559
: JSON.stringify({ formulae: [{ versions: { stable: "2026.9.1" } }] });
551560
return Effect.succeed(
@@ -575,7 +584,9 @@ it.layer(layerTest)("CodexDriver", (it) => {
575584
homePath: NodePath.join(tempDir, "codex-home"),
576585
},
577586
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, metadataSpawner));
578-
const capabilities = yield* instance.snapshot.resolveMaintenance();
587+
const capabilities = yield* instance.snapshot
588+
.resolveMaintenance()
589+
.pipe(Effect.provideService(HostProcess.Environment, { PATH: lookupPath }));
579590
const latestVersion = yield* resolveLatestProviderVersion(capabilities).pipe(
580591
Effect.provideServiceEffect(
581592
ProviderLatestVersions.ProviderLatestVersions,

‎packages/provider-core/src/server/maintenanceResolver.test.ts‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ it.layer(NodeServices.layer)("providerMaintenance", (it) => {
197197
expect(advisoryFor(pnpmTool, "1.1.0-beta.1")).toBe(
198198
"pnpm add -g --allow-build=@example/package-tool @example/package-tool@latest",
199199
);
200-
// Only pnpm's minimumReleaseAge skips fresh releases; npm keeps @latest.
200+
// npm keeps @latest; its outdated probe already reports what that resolves to.
201201
expect(
202202
advisoryFor(
203203
makeProviderMaintenanceCapabilities({
@@ -289,6 +289,8 @@ it.layer(NodeServices.layer)("providerMaintenance", (it) => {
289289
expect(capabilities).toEqual({
290290
provider: driver("packageTool"),
291291
packageName: "@example/package-tool",
292+
latestVersion: null,
293+
lookupLatestVersion: expect.anything(),
292294
update: {
293295
command: `npm install -g --prefix ${tempDir} --allow-scripts=@example/package-tool @example/package-tool@latest`,
294296
executable: "npm",
@@ -712,6 +714,94 @@ it.layer(NodeServices.layer)("providerMaintenance", (it) => {
712714
{ skip: !symlinksSupported || windowsHost },
713715
);
714716

717+
it.effect.each([
718+
{
719+
case: "the eligible release",
720+
outdated: { "@example/package-tool": { current: "1.0.0", wanted: "1.1.0", latest: "1.1.0" } },
721+
exitCode: 1,
722+
status: "behind_latest",
723+
latestVersion: "1.1.0",
724+
},
725+
{
726+
case: "no update during cooldown",
727+
outdated: {},
728+
exitCode: 0,
729+
status: "current",
730+
latestVersion: "1.0.0",
731+
},
732+
{
733+
case: "no update during cooldown beside a broken global",
734+
outdated: {},
735+
exitCode: 0,
736+
listExitCode: 1,
737+
status: "current",
738+
latestVersion: "1.0.0",
739+
},
740+
{
741+
case: "no registry version when npm cannot reach the registry",
742+
outdated: { error: { code: "ECONNREFUSED" } },
743+
exitCode: 1,
744+
status: "unknown",
745+
latestVersion: null,
746+
},
747+
])(
748+
"advertises $case for npm-global installs",
749+
(fixture) =>
750+
Effect.gen(function* () {
751+
const tempDir = yield* makeTempDir("t3-npm-latest");
752+
const npmBinDir = NodePath.join(tempDir, "npm-bin");
753+
writeExecutable(NodePath.join(npmBinDir, "npm"));
754+
const link = linkIntoPackage(tempDir, "package-tool", [
755+
"lib",
756+
"node_modules",
757+
"@example",
758+
"package-tool",
759+
]);
760+
761+
const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
762+
packageToolUpdate,
763+
{ binaryPath: link, env: { PATH: "" } },
764+
).pipe(
765+
Effect.provideService(HostProcess.Environment, { PATH: npmBinDir }),
766+
Effect.provideService(
767+
ChildProcessSpawner.ChildProcessSpawner,
768+
stdoutSpawner(
769+
(_command, args) =>
770+
args[args.indexOf("--prefix") + 1] !== tempDir
771+
? "{}"
772+
: args[0] === "outdated"
773+
? JSON.stringify(fixture.outdated)
774+
: JSON.stringify({
775+
dependencies: { "@example/package-tool": { version: "1.0.0" } },
776+
}),
777+
(args) => (args[0] === "outdated" ? fixture.exitCode : (fixture.listExitCode ?? 0)),
778+
),
779+
),
780+
);
781+
782+
const provider = yield* enrichProviderSnapshotWithVersionAdvisory(
783+
installedPackageToolProvider,
784+
capabilities,
785+
).pipe(
786+
Effect.provideServiceEffect(
787+
ProviderLatestVersions.ProviderLatestVersions,
788+
ProviderLatestVersions.make([["@example/package-tool", "9.9.9"]]),
789+
),
790+
Effect.provideService(
791+
HttpClient.HttpClient,
792+
HttpClient.make(() => Effect.die("the npm version is already cached")),
793+
),
794+
);
795+
expect(provider.versionAdvisory).toMatchObject({
796+
status: fixture.status,
797+
currentVersion: "1.0.0",
798+
latestVersion: fixture.latestVersion,
799+
updateCommand: expect.stringMatching(/@example\/package-tool@latest$/),
800+
});
801+
}),
802+
{ skip: !symlinksSupported || windowsHost },
803+
);
804+
715805
it.effect.skipIf(windowsHost)(
716806
"switches to bun updates when the resolved binary lives in bun's global bin",
717807
() =>

‎packages/provider-core/src/server/maintenanceResolver.ts‎

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -376,24 +376,22 @@ const runProbe = Effect.fn("runProbe")(function* (
376376
);
377377
});
378378

379-
const decodePnpmOutdated = Schema.decodeUnknownOption(
379+
const decodeOutdated = Schema.decodeUnknownOption(
380380
Schema.fromJsonString(
381381
Schema.Record(Schema.String, Schema.Struct({ latest: Schema.optional(Schema.String) })),
382382
),
383383
);
384-
const decodePnpmList = Schema.decodeUnknownOption(
385-
Schema.fromJsonString(
386-
Schema.Array(
387-
Schema.Struct({
388-
dependencies: Schema.optional(
389-
Schema.Record(Schema.String, Schema.Struct({ version: Schema.optional(Schema.String) })),
390-
),
391-
}),
392-
),
384+
const GlobalPackageList = Schema.Struct({
385+
dependencies: Schema.optional(
386+
Schema.Record(Schema.String, Schema.Struct({ version: Schema.optional(Schema.String) })),
393387
),
388+
});
389+
const decodePnpmList = Schema.decodeUnknownOption(
390+
Schema.fromJsonString(Schema.Array(GlobalPackageList)),
394391
);
392+
const decodeNpmList = Schema.decodeUnknownOption(Schema.fromJsonString(GlobalPackageList));
395393

396-
// Unlike npm's latest tag, pnpm outdated respects minimumReleaseAge and its excludes.
394+
// Unlike the registry's latest tag, pnpm outdated respects minimumReleaseAge and its excludes.
397395
const resolvePnpmGlobalLatestVersion = Effect.fn("resolvePnpmGlobalLatestVersion")(function* (
398396
packageName: string,
399397
) {
@@ -408,9 +406,7 @@ const resolvePnpmGlobalLatestVersion = Effect.fn("resolvePnpmGlobalLatestVersion
408406
// pnpm outdated exits 1 when updates are available.
409407
[0, 1],
410408
);
411-
const outdatedEntries = outdated
412-
? Option.getOrUndefined(decodePnpmOutdated(outdated))
413-
: undefined;
409+
const outdatedEntries = outdated ? Option.getOrUndefined(decodeOutdated(outdated)) : undefined;
414410
if (!outdatedEntries) return null;
415411
const latest = nonEmptyString(outdatedEntries[packageName]?.latest);
416412
if (latest) return latest;
@@ -424,6 +420,39 @@ const resolvePnpmGlobalLatestVersion = Effect.fn("resolvePnpmGlobalLatestVersion
424420
return nonEmptyString(listedEntries?.[0]?.dependencies?.[packageName]?.version);
425421
});
426422

423+
// `npm view` ignores min-release-age and before, but `outdated` applies them and
424+
// their excludes the way `install <pkg>@latest` does.
425+
const resolveNpmGlobalLatestVersion = Effect.fn("resolveNpmGlobalLatestVersion")(function* (
426+
packageName: string,
427+
npmPrefix: string,
428+
) {
429+
// Match the updater's npm and server environment, rather than the instance's.
430+
const npmPath = yield* resolveCommandPath("npm");
431+
if (!npmPath) return null;
432+
const outdated = yield* runProbe(
433+
npmPath,
434+
["outdated", "-g", "--prefix", npmPrefix, "--json", packageName],
435+
{},
436+
// npm outdated exits 1 when updates are available.
437+
[0, 1],
438+
);
439+
const outdatedEntries = outdated ? Option.getOrUndefined(decodeOutdated(outdated)) : undefined;
440+
// npm prints failures such as an unreachable registry as `{"error": ...}`, also exiting 1.
441+
if (!outdatedEntries || "error" in outdatedEntries) return null;
442+
const latest = nonEmptyString(outdatedEntries[packageName]?.latest);
443+
if (latest) return latest;
444+
// An empty result means the installed version is still the eligible release.
445+
const listed = yield* runProbe(
446+
npmPath,
447+
["ls", "-g", "--prefix", npmPrefix, "--depth", "0", "--json", packageName],
448+
{},
449+
// npm ls still prints the tree but exits 1 when other globals have problems.
450+
[0, 1],
451+
);
452+
const listedEntries = listed ? Option.getOrUndefined(decodeNpmList(listed)) : undefined;
453+
return nonEmptyString(listedEntries?.dependencies?.[packageName]?.version);
454+
});
455+
427456
export const resolvePackageManagedProviderMaintenance = Effect.fn(
428457
"resolvePackageManagedProviderMaintenance",
429458
)(function* (
@@ -546,6 +575,7 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
546575
// `Cellar/node/<ver>/lib/node_modules/`, and that is npm's install, not brew's.
547576
const npmPrefix = yield* resolveNpmGlobalPrefix(context, packageName);
548577
if (npmPrefix) {
578+
const services = yield* Effect.context<ProviderMaintenanceResolverServices>();
549579
// npm 12 blocks install scripts by default (empty allow-scripts allowlist)
550580
// and still exits 0, so a package whose postinstall finishes the install
551581
// (claude copies its native binary over a placeholder stub) is left broken
@@ -564,6 +594,12 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
564594
`${packageName}@latest`,
565595
],
566596
updateLockKey: `npm-global:${normalizeCommandPath(npmPrefix)}`,
597+
// Inside min-release-age `@latest` installs an older release than the
598+
// registry's tag, so only npm's own answer is worth advertising.
599+
latestVersion: null,
600+
lookupLatestVersion: resolveNpmGlobalLatestVersion(packageName, npmPrefix).pipe(
601+
Effect.provideContext(services),
602+
),
567603
});
568604
}
569605

0 commit comments

Comments
 (0)