Skip to content

Commit b6be1ad

Browse files
committed
fix(codex): 按原生安装渠道检查可更新版本
为独立安装器使用官方稳定渠道,避免 npm 先发布时反复提示无法安装的更新。 渠道不可用或存在显式覆盖时保留未知状态,并复用现有缓存与超时保护。 新增版本错位及失败场景回归测试;70 个相关测试和变更范围检查通过。
1 parent 807319c commit b6be1ad

3 files changed

Lines changed: 117 additions & 2 deletions

File tree

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,86 @@ it.layer(layerTest)("CodexDriver", (it) => {
383383
),
384384
);
385385

386+
for (const fixture of [
387+
{ name: "a native release behind npm", tag: "rust-v0.163.0", status: 200, expected: "0.163.0" },
388+
{ name: "an unavailable native channel", tag: "rust-v0.163.0", status: 503, expected: null },
389+
{ name: "malformed native metadata", tag: "not-a-version", status: 200, expected: null },
390+
{ name: "a newer native release", tag: "rust-v0.164.0", status: 200, expected: "0.164.0" },
391+
{
392+
name: "an explicit release",
393+
tag: "rust-v0.164.0",
394+
status: 200,
395+
expected: null,
396+
env: { CODEX_RELEASE: "0.162.0" },
397+
},
398+
{
399+
name: "an alternative installer channel",
400+
tag: "rust-v0.164.0",
401+
status: 200,
402+
expected: null,
403+
env: { CODEX_INSTALLER_USE_RELEASES_OPENAI_COM: "false" },
404+
},
405+
]) {
406+
it.effect.skipIf(windowsHost)(`checks the standalone channel for ${fixture.name}`, () =>
407+
Effect.gen(function* () {
408+
const fs = yield* FileSystem.FileSystem;
409+
const home = yield* fs
410+
.makeTempDirectoryScoped({ prefix: "t3-codex-native-channel-" })
411+
.pipe(Effect.flatMap((directory) => fs.realPath(directory)));
412+
const binaryPath = NodePath.join(home, "packages", "standalone", "bin", "codex");
413+
yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });
414+
yield* fs.writeFileString(binaryPath, "#!/bin/sh\n");
415+
yield* fs.chmod(binaryPath, 0o755);
416+
const requests: string[] = [];
417+
const http = HttpClient.make((request) =>
418+
Effect.sync(() => {
419+
requests.push(request.url);
420+
return HttpClientResponse.fromWeb(
421+
request,
422+
request.url.includes("registry.npmjs.org")
423+
? Response.json({ version: "0.164.0" })
424+
: Response.json({ tag_name: fixture.tag }, { status: fixture.status }),
425+
);
426+
}),
427+
);
428+
const instance = yield* CodexDriver.create({
429+
instanceId: ProviderInstanceId.make("codex-native-channel"),
430+
displayName: "Codex channel test",
431+
enabled: false,
432+
environment: [],
433+
config: { ...CodexDriver.defaultConfig(), binaryPath, homePath: home },
434+
}).pipe(Effect.provideService(HttpClient.HttpClient, http));
435+
const capabilities = yield* instance.snapshot.resolveMaintenance();
436+
expect(requests).toEqual([]);
437+
const latest = yield* resolveLatestProviderVersion(capabilities).pipe(
438+
Effect.provideService(HttpClient.HttpClient, http),
439+
);
440+
expect(latest).toBe(fixture.expected);
441+
expect(requests).toEqual(
442+
fixture.env ? [] : ["https://releases.openai.com/codex/channels/latest"],
443+
);
444+
expect(
445+
createProviderVersionAdvisory({
446+
driver: CodexDriver.driverKind,
447+
currentVersion: "0.163.0",
448+
latestVersion: latest,
449+
maintenanceCapabilities: capabilities,
450+
})?.status,
451+
).toBe(
452+
fixture.expected === null
453+
? "unknown"
454+
: fixture.expected === "0.163.0"
455+
? "current"
456+
: "behind_latest",
457+
);
458+
}).pipe(
459+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
460+
Effect.provideService(HostProcess.Environment, fixture.env ?? {}),
461+
Effect.scoped,
462+
),
463+
);
464+
}
465+
386466
for (const fixture of [
387467
{
388468
name: "leaves mise npm-backend installations manual-only",

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Crypto from "effect/Crypto";
33
import * as Effect from "effect/Effect";
44
import * as FileSystem from "effect/FileSystem";
55
import * as Path from "effect/Path";
6+
import * as Option from "effect/Option";
67
import * as Schema from "effect/Schema";
78
import { HttpClient } from "effect/http";
89
import { ChildProcessSpawner } from "effect/process";
@@ -64,13 +65,39 @@ function isCodexStandaloneCommandPath(commandPath: string): boolean {
6465
return normalizeCommandPath(commandPath).includes("/packages/standalone/");
6566
}
6667

68+
const CodexNativeReleaseMetadata = Schema.Struct({ tag_name: Schema.String });
69+
70+
/** The standalone installer consumes this channel, whose rollout can lag npm. */
71+
const lookupCodexNativeVersion = Effect.gen(function* () {
72+
const env = yield* HostProcess.Environment;
73+
// Explicit installer overrides are not the default stable channel. An unknown
74+
// advisory is safer than promising an npm release this updater will not install.
75+
if (
76+
(env.CODEX_RELEASE && env.CODEX_RELEASE !== "latest") ||
77+
(env.CODEX_INSTALLER_USE_RELEASES_OPENAI_COM &&
78+
!/^(1|true|yes)$/i.test(env.CODEX_INSTALLER_USE_RELEASES_OPENAI_COM))
79+
)
80+
return null;
81+
const http = yield* HttpClient.HttpClient;
82+
const response = yield* http.get("https://releases.openai.com/codex/channels/latest");
83+
if (response.status < 200 || response.status >= 300) return null;
84+
const metadata = yield* response.json.pipe(
85+
Effect.flatMap(Schema.decodeUnknownEffect(CodexNativeReleaseMetadata)),
86+
);
87+
return /^(?:rust-v|v)?(\d+\.\d+\.\d+)$/.exec(metadata.tag_name)?.[1] ?? null;
88+
}).pipe(
89+
Effect.timeoutOption("4 seconds"),
90+
Effect.map(Option.getOrNull),
91+
Effect.orElseSucceed(() => null),
92+
);
93+
6794
/**
6895
* `codex update` replaces the standalone tree under `CODEX_HOME`. That tree
6996
* lives in the shared home even when an auth-overlay shadow home is in use
7097
* (the overlay only carries auth and a few local entries), so the updater
7198
* runs against `sharedHomePath` rather than the instance's effective home.
7299
*/
73-
function makeCodexMaintenanceResolver(sharedHomePath: string) {
100+
function makeCodexMaintenanceResolver(sharedHomePath: string, http: HttpClient.HttpClient) {
74101
return makePackageManagedProviderMaintenanceResolver({
75102
provider: DRIVER_KIND,
76103
npmPackageName: "@openai/codex",
@@ -79,6 +106,9 @@ function makeCodexMaintenanceResolver(sharedHomePath: string) {
79106
isCommandPath: isCodexStandaloneCommandPath,
80107
requiresCommandPath: true,
81108
env: { CODEX_HOME: sharedHomePath },
109+
lookupLatestVersion: lookupCodexNativeVersion.pipe(
110+
Effect.provideService(HttpClient.HttpClient, http),
111+
),
82112
},
83113
});
84114
}
@@ -168,7 +198,7 @@ export const CodexDriver: ProviderDriver<
168198
} satisfies CodexSettings;
169199
const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
170200
resolveProviderMaintenanceCapabilitiesEffect(
171-
makeCodexMaintenanceResolver(homeLayout.sharedHomePath),
201+
makeCodexMaintenanceResolver(homeLayout.sharedHomePath, httpClient),
172202
{
173203
binaryPath: effectiveConfig.binaryPath,
174204
env: processEnv,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ export interface PackageManagedProviderMaintenanceDefinition {
8888
/** The updater cannot detect installs outside `isCommandPath`, so leave those manual-only. */
8989
readonly requiresCommandPath?: boolean;
9090
readonly env?: NodeJS.ProcessEnv;
91+
/** An installer-specific channel must not fall back to npm when unavailable. */
92+
readonly lookupLatestVersion?: Effect.Effect<string | null>;
9193
} | null;
9294
}
9395

@@ -463,6 +465,9 @@ export const resolvePackageManagedProviderMaintenance = Effect.fn(
463465
updateLockKey: `${definition.provider}-native`,
464466
platform: context.platform,
465467
...(nativeUpdate.env ? { env: nativeUpdate.env } : {}),
468+
...(nativeUpdate.lookupLatestVersion
469+
? { latestVersion: null, lookupLatestVersion: nativeUpdate.lookupLatestVersion }
470+
: {}),
466471
})
467472
: manual;
468473
// A native-looking launcher can still delegate to a mise-owned install.

0 commit comments

Comments
 (0)