Skip to content

Commit 9312c8d

Browse files
committed
provider-codex: update a Homebrew cask install through brew
bb planned `codex update` for every externally managed Codex. On a Homebrew cask install (the binary is a symlink into Caskroom/codex/...) that command cannot detect the install method and fails, so the update badge never clears. When the resolved executable's real path is under Caskroom/codex/, plan `brew upgrade --cask codex` instead. Other installs keep `codex update`, and the npm install path is unchanged. Refs get-bb#3166
1 parent b0bb686 commit 9312c8d

2 files changed

Lines changed: 87 additions & 7 deletions

File tree

‎plugins/provider-codex/src/bridge/provider-maintenance.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,67 @@ describe("Codex provider maintenance", () => {
129129
message: "Codex install is no longer available on this host.",
130130
});
131131
});
132+
133+
it("upgrades a Homebrew cask install through brew instead of codex update", () => {
134+
expect(
135+
__testing.buildProviderInstallationRun(
136+
installationStatus(),
137+
"update",
138+
true,
139+
),
140+
).toEqual({
141+
available: true,
142+
command: {
143+
command: "brew",
144+
args: ["upgrade", "--cask", "codex"],
145+
displayCommand: "brew upgrade --cask codex",
146+
},
147+
verification: { kind: "version_at_least", version: "1.1.0" },
148+
});
149+
});
150+
});
151+
152+
describe("Codex Homebrew cask detection", () => {
153+
let tempDir: string;
154+
155+
beforeEach(async () => {
156+
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "bb-codex-brew-"));
157+
});
158+
159+
afterEach(async () => {
160+
await fs.rm(tempDir, { force: true, recursive: true });
161+
});
162+
163+
it.skipIf(process.platform === "win32")(
164+
"recognises a Linuxbrew cask codex reached through its bin symlink",
165+
async () => {
166+
const caskBin = path.join(tempDir, "Caskroom", "codex", "0.164.0", "bin");
167+
const linkDir = path.join(tempDir, "bin");
168+
await fs.mkdir(caskBin, { recursive: true });
169+
await fs.mkdir(linkDir);
170+
const target = path.join(caskBin, "codex");
171+
await fs.writeFile(target, "");
172+
await fs.symlink(target, path.join(linkDir, "codex"));
173+
174+
await expect(
175+
__testing.isHomebrewCaskInstall(path.join(linkDir, "codex")),
176+
).resolves.toBe(true);
177+
},
178+
);
179+
180+
it.skipIf(process.platform === "win32")(
181+
"does not treat a plain npm-style binary as a Homebrew cask",
182+
async () => {
183+
const binDir = path.join(tempDir, "bin");
184+
await fs.mkdir(binDir);
185+
await fs.writeFile(path.join(binDir, "codex"), "");
186+
187+
await expect(
188+
__testing.isHomebrewCaskInstall(path.join(binDir, "codex")),
189+
).resolves.toBe(false);
190+
await expect(__testing.isHomebrewCaskInstall(null)).resolves.toBe(false);
191+
},
192+
);
132193
});
133194

134195
describe("Codex credential health and usage", () => {

‎plugins/provider-codex/src/bridge/provider-maintenance.ts‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { realpath } from "node:fs/promises";
2+
import path from "node:path";
13
import {
24
type ProviderHealthResult,
35
type ProviderInstallationRunResult,
@@ -69,16 +71,29 @@ function minimumSupportedVersionForRequirement(
6971
: CODEX_MINIMUM_SUPPORTED_VERSION;
7072
}
7173

72-
function codexUpdateCommand(): {
74+
async function isHomebrewCaskInstall(
75+
executablePath: string | null,
76+
): Promise<boolean> {
77+
if (executablePath === null) return false;
78+
try {
79+
const target = await realpath(executablePath);
80+
return target.includes(`${path.sep}Caskroom${path.sep}codex${path.sep}`);
81+
} catch {
82+
return false;
83+
}
84+
}
85+
86+
function codexUpdateCommand(homebrewCask: boolean): {
7387
command: string;
7488
args: string[];
7589
displayCommand: string;
7690
} {
77-
const args = ["update"];
91+
const command = homebrewCask ? "brew" : "codex";
92+
const args = homebrewCask ? ["upgrade", "--cask", "codex"] : ["update"];
7893
return {
79-
command: "codex",
94+
command,
8095
args,
81-
displayCommand: formatCommand("codex", args),
96+
displayCommand: formatCommand(command, args),
8297
};
8398
}
8499

@@ -97,6 +112,7 @@ export async function getCodexProviderInstallationStatus(
97112
? probeNpmGlobalPackage(CODEX_NPM_PACKAGE)
98113
: { npmBin: null, npmGlobalPackageVersion: null },
99114
]);
115+
const homebrewCask = await isHomebrewCaskInstall(resolvedExecutable);
100116
const installed = resolvedExecutable !== null || versionOutput !== null;
101117
const currentVersion = versionFrom(versionOutput);
102118
const needsUpdate =
@@ -138,7 +154,7 @@ export async function getCodexProviderInstallationStatus(
138154
command:
139155
actionKind === "install"
140156
? npmGlobalInstallCommand(CODEX_NPM_PACKAGE).displayCommand
141-
: codexUpdateCommand().displayCommand,
157+
: codexUpdateCommand(homebrewCask).displayCommand,
142158
},
143159
needsUpdate,
144160
versionUnsupported,
@@ -149,12 +165,14 @@ export async function getCodexProviderInstallationRun(
149165
action: "install" | "update",
150166
): Promise<ProviderInstallationRunResult> {
151167
const status = await getCodexProviderInstallationStatus();
152-
return buildCodexProviderInstallationRun(status, action);
168+
const homebrewCask = await isHomebrewCaskInstall(status.executablePath);
169+
return buildCodexProviderInstallationRun(status, action, homebrewCask);
153170
}
154171

155172
function buildCodexProviderInstallationRun(
156173
status: ProviderInstallationStatus,
157174
action: "install" | "update",
175+
homebrewCask = false,
158176
): ProviderInstallationRunResult {
159177
if (status.installAction?.kind !== action) {
160178
return {
@@ -167,7 +185,7 @@ function buildCodexProviderInstallationRun(
167185
command:
168186
action === "install"
169187
? npmGlobalInstallCommand(CODEX_NPM_PACKAGE)
170-
: codexUpdateCommand(),
188+
: codexUpdateCommand(homebrewCask),
171189
verification: installationVerification(status, action),
172190
};
173191
}
@@ -403,6 +421,7 @@ export async function getCodexProviderUsage(): Promise<ProviderUsageResult> {
403421

404422
export const __testing = {
405423
buildProviderInstallationRun: buildCodexProviderInstallationRun,
424+
isHomebrewCaskInstall,
406425
minimumSupportedVersionForRequirement,
407426
normalizeUsage,
408427
};

0 commit comments

Comments
 (0)