Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(byoa): fall back to package metadata for engine versions (#345)

Strelizia0e84e95e66b2
diff --git a/server/src/__tests__/agents-computer-engine-version.test.ts b/server/src/__tests__/agents-computer-engine-version.test.ts
index 2a9f34c..e9cd860 100644
--- a/server/src/__tests__/agents-computer-engine-version.test.ts
+++ b/server/src/__tests__/agents-computer-engine-version.test.ts
@@ -10,7 +10,7 @@
*/
import assert from 'node:assert/strict'
-import { chmod, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
+import { chmod, mkdir, mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
@@ -21,7 +21,7 @@ process.env.OPENAI_API_KEY ??= 'test-key'
const {
parseCliVersion, isCliOutdated, isCliVersionAtLeast, inferUpdateCommand, updateArgv,
parseCursorAbout, parseGrokCheck, ENGINE_VERSION_SPECS, versionCommandInvocation,
- probeLocalEngineVersionWithRetry, runEngineUpdate,
+ probeLocalEngineVersionWithRetry, runEngineUpdate, npmPackageVersion, probeEngineVersion,
} = await import('../agents/computer/cli-version.js')
const { sanitizeDetectedEngines } = await import('../agents/computer/registry.js')
@@ -76,6 +76,189 @@ test('an inconclusive local version probe retries and stops on the first concret
}
})
+test('npmPackageVersion reads the package that owns a binary inside node_modules', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'cumora-npm-version-'))
+ try {
+ const pkgDir = join(root, 'lib', 'node_modules', '@openai', 'codex')
+ await mkdir(join(pkgDir, 'bin'), { recursive: true })
+ await writeFile(join(pkgDir, 'package.json'), JSON.stringify({ name: '@openai/codex', version: '0.164.0' }))
+ const bin = join(pkgDir, 'bin', 'codex.js')
+ await writeFile(bin, '#!/usr/bin/env node\n')
+ assert.equal(npmPackageVersion(bin), '0.164.0')
+ // npm links a global install through a symlink; the real path decides.
+ // Windows refuses symlinks without developer mode, which is fine — the
+ // path shape above already covers the walk-up.
+ const link = join(root, 'codex')
+ try {
+ await symlink(bin, link)
+ assert.equal(npmPackageVersion(link), '0.164.0')
+ } catch { /* symlinks unavailable on this host */ }
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+test('npmPackageVersion reads the package an npm Windows shim points at', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'cumora-npm-shim-'))
+ try {
+ const pkgDir = join(root, 'node_modules', 'opencode-ai')
+ await mkdir(pkgDir, { recursive: true })
+ await writeFile(join(pkgDir, 'package.json'), JSON.stringify({ name: 'opencode-ai', version: '1.19.4' }))
+ // The shape npm writes: a shim *beside* node_modules, naming its own path.
+ const shim = join(root, 'opencode')
+ await writeFile(shim, [
+ '@ECHO off',
+ 'SETLOCAL',
+ 'SET "dp0=%~dp0"',
+ 'IF EXIST "%dp0%\\node_modules\\opencode-ai\\bin\\opencode.exe" (',
+ ' "%dp0%\\node_modules\\opencode-ai\\bin\\opencode.exe" %*',
+ ')',
+ ].join('\r\n'))
+ assert.equal(npmPackageVersion(shim), '1.19.4')
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+test('npmPackageVersion follows the symlink a POSIX npm global install uses', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'cumora-npm-posix-'))
+ try {
+ // What `npm i -g opencode-ai` leaves on macOS and Linux: <prefix>/bin/<bin>
+ // is a symlink into <prefix>/lib/node_modules, never a file of its own.
+ const prefix = join(root, 'usr', 'local')
+ const pkgDir = join(prefix, 'lib', 'node_modules', 'opencode-ai')
+ await mkdir(join(pkgDir, 'bin'), { recursive: true })
+ await writeFile(join(pkgDir, 'package.json'), JSON.stringify({ name: 'opencode-ai', version: '1.19.4' }))
+ const target = join(pkgDir, 'bin', 'opencode.exe')
+ await writeFile(target, '#!/bin/sh\necho "postinstall was not run" >&2\nexit 1\n')
+ await mkdir(join(prefix, 'bin'), { recursive: true })
+ const link = join(prefix, 'bin', 'opencode')
+ try {
+ await symlink(join('..', 'lib', 'node_modules', 'opencode-ai', 'bin', 'opencode.exe'), link)
+ } catch {
+ return // symlinks unavailable on this host; the shape is covered above
+ }
+ assert.equal(npmPackageVersion(link), '1.19.4')
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+test('a CLI that cannot report a version is answered by its package instead', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'cumora-broken-cli-'))
+ try {
+ const pkgDir = join(root, 'node_modules', 'opencode-ai')
+ await mkdir(join(pkgDir, 'bin'), { recursive: true })
+ await writeFile(join(pkgDir, 'package.json'), JSON.stringify({ name: 'opencode-ai', version: '1.19.4' }))
+ // Exactly what opencode-ai ships before its postinstall: exits 1 and says
+ // nothing about a version. This is the bug, reproduced.
+ const bin = join(pkgDir, 'bin', 'opencode')
+ await writeFile(bin, '#!/bin/sh\necho "Error: postinstall was not run." >&2\nexit 1\n')
+ await chmod(bin, 0o755)
+ if (process.platform === 'win32') {
+ await writeFile(`${bin}.cmd`, '@echo off\r\necho Error: postinstall was not run. 1>&2\r\nexit /b 1\r\n')
+ }
+
+ // No registry in a unit test: probeLatest() must fail quietly, not hang.
+ const realFetch = globalThis.fetch
+ globalThis.fetch = (async () => new Response('{}', { status: 404 })) as typeof fetch
+ try {
+ const info = await probeEngineVersion('opencode', bin)
+ assert.equal(info.version, '1.19.4', 'the package says what the CLI cannot')
+ assert.equal(info.latest, null)
+ assert.equal(info.outdated, false, 'no upstream means no nagging')
+ } finally {
+ globalThis.fetch = realFetch
+ }
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+test('npmPackageVersion reads the package a pnpm global shim points at', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'cumora-pnpm-shim-'))
+ try {
+ // What `pnpm add -g` leaves (9.15.0, verified against a real install): the
+ // shim sits in the global bin dir, but the package lives in the store one
+ // level over — <global-dir>/<major>/node_modules/<pkg>, not beside the shim.
+ const binDir = join(root, 'pnpm')
+ const pkgDir = join(root, 'pnpm-global', '5', 'node_modules', 'opencode-ai')
+ await mkdir(join(pkgDir, 'bin'), { recursive: true })
+ await writeFile(join(pkgDir, 'package.json'), JSON.stringify({ name: 'opencode-ai', version: '1.19.4' }))
+ await mkdir(binDir, { recursive: true })
+ // Verbatim shape, minus the absolute NODE_PATH lines: %~dp0\.. is the store.
+ const cmd = join(binDir, 'opencode.CMD')
+ await writeFile(cmd, [
+ '@SETLOCAL',
+ `@IF EXIST "%~dp0\\..\\pnpm-global\\5\\node_modules\\opencode-ai\\bin\\opencode.exe" (`,
+ ' "%~dp0\\..\\pnpm-global\\5\\node_modules\\opencode-ai\\bin\\opencode.exe" %*',
+ ')',
+ ].join('\r\n'))
+ assert.equal(npmPackageVersion(cmd), '1.19.4')
+ // The POSIX twin names the same store through $basedir.
+ const sh = join(binDir, 'opencode')
+ await writeFile(sh, [
+ '#!/bin/sh',
+ 'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
+ 'exec "$basedir/../pnpm-global/5/node_modules/opencode-ai/bin/opencode.exe" "$@"',
+ ].join('\n'))
+ assert.equal(npmPackageVersion(sh), '1.19.4')
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+test('npmPackageVersion reads a pnpm shim whose global directory holds a space', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'cumora-pnpm-space-'))
+ try {
+ // `pnpm add -g --global-dir "…/global packages"` (9.15.0, real install).
+ // Every path stays quoted; splitting on whitespace truncates it.
+ const binDir = join(root, 'pnpm')
+ const pkgDir = join(root, 'global packages', '5', 'node_modules', 'opencode-ai')
+ await mkdir(join(pkgDir, 'bin'), { recursive: true })
+ await writeFile(join(pkgDir, 'package.json'), JSON.stringify({ name: 'opencode-ai', version: '1.19.4' }))
+ await mkdir(binDir, { recursive: true })
+ const cmd = join(binDir, 'opencode.CMD')
+ await writeFile(cmd, [
+ '@SETLOCAL',
+ ' "%~dp0\\/bin/sh.exe" "%~dp0\\..\\global packages\\5\\node_modules\\opencode-ai\\bin\\opencode.exe" %*',
+ ].join('\r\n'))
+ assert.equal(npmPackageVersion(cmd), '1.19.4')
+ const sh = join(binDir, 'opencode')
+ await writeFile(sh, [
+ '#!/bin/sh',
+ 'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
+ ' exec "$basedir//bin/sh" "$basedir/../global packages/5/node_modules/opencode-ai/bin/opencode.exe" "$@"',
+ ].join('\n'))
+ assert.equal(npmPackageVersion(sh), '1.19.4')
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+test('npmPackageVersion stays null for anything it cannot attribute', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'cumora-npm-none-'))
+ try {
+ // A real binary outside any package: never guess.
+ const standalone = join(root, 'opencode.exe')
+ await writeFile(standalone, Buffer.from([0x4d, 0x5a, 0x90, 0x00]))
+ assert.equal(npmPackageVersion(standalone), null)
+ // A shim whose package is not installed beside it.
+ const shim = join(root, 'codex')
+ await writeFile(shim, 'IF EXIST "%dp0%\\node_modules\\@openai\\codex\\bin\\codex.js" (\r\n)\r\n')
+ assert.equal(npmPackageVersion(shim), null)
+ // A package.json without a version is not a version.
+ const pkgDir = join(root, 'node_modules', 'broken')
+ await mkdir(pkgDir, { recursive: true })
+ await writeFile(join(pkgDir, 'package.json'), '{"name":"broken"}')
+ assert.equal(npmPackageVersion(join(pkgDir, 'index.js')), null)
+ assert.equal(npmPackageVersion(null), null)
+ assert.equal(npmPackageVersion(join(root, 'missing')), null)
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
test('isCliOutdated only fires when upstream is strictly newer', () => {
assert.equal(isCliOutdated('1.2.3', '1.2.4'), true)
assert.equal(isCliOutdated('1.2.3', '1.3.0'), true)
diff --git a/server/src/agents/computer/cli-version.ts b/server/src/agents/computer/cli-version.ts
index 42c92af..47d5384 100644
--- a/server/src/agents/computer/cli-version.ts
+++ b/server/src/agents/computer/cli-version.ts
@@ -13,6 +13,7 @@
import { spawn } from 'node:child_process'
import fs from 'node:fs'
+import path from 'node:path'
interface CommandInvocation {
command: string
@@ -256,6 +257,140 @@ function realpathOf(binPath: string): string {
}
}
+/** npm's Windows shims are a few hundred bytes; anything larger is not one, and
+ * reading a real binary looking for a package name would be pointless work. */
+const SHIM_READ_LIMIT = 64 * 1024
+
+/** The package directory `file` sits inside, when that directory is in a
+ * node_modules tree — `…/node_modules/@openai/codex/bin/codex.js` is in
+ * @openai/codex. */
+function owningPackageDir(file: string): string | null {
+ for (let dir = path.dirname(file); ; dir = path.dirname(dir)) {
+ const parent = path.dirname(dir)
+ if (parent === dir) return null
+ const above = path.basename(parent)
+ // `<pkg>` directly under node_modules, or `<@scope>/<pkg>` under it.
+ if (above === 'node_modules') return dir
+ if (above.startsWith('@') && path.basename(path.dirname(parent)) === 'node_modules') return dir
+ }
+}
+
+/** The version the package containing `file` records, or null when `file` is in
+ * none — the read half of `npmPackageVersion`, shared by both shapes. */
+function versionOfOwner(file: string): string | null {
+ const owner = owningPackageDir(realpathOf(file))
+ return owner ? versionFromPackageJson(owner) : null
+}
+
+/** The path a shim names at `at`, the position of its `/node_modules/`: the text
+ * before that is the directory the line starts from — the shim's own (`%dp0%`,
+ * `%~dp0`, `$basedir`) or a real path — and the text after it runs to the end of
+ * the quoted path. pnpm keeps a store directory in between
+ * (`…/global/5/node_modules`), so the prefix is resolved, not assumed to sit
+ * beside the shim. Null when a variable is left unexpanded: a name nothing can
+ * be read from. */
+function shimPathAt(flat: string, at: number, shimDir: string): string | null {
+ // pnpm quotes every path it writes, and a global directory may hold a space
+ // (`…/global packages/5/…`), so a quoted span — not whitespace — is the
+ // boundary when there is one. Quotes never span a line in a shim.
+ const from = flat.lastIndexOf('\n', at) + 1
+ let to = flat.indexOf('\n', at)
+ if (to < 0) to = flat.length
+ let span: [number, number] | null = null
+ let open = -1
+ let quote = ''
+ for (let i = from; i < to && !span; i++) {
+ const ch = flat.charAt(i)
+ if (!quote) {
+ if (ch === '"' || ch === "'") {
+ quote = ch
+ open = i
+ }
+ } else if (ch === quote) {
+ if (at > open && at < i) span = [open + 1, i]
+ quote = ''
+ }
+ }
+ if (!span && quote && at > open) span = [open + 1, to]
+ if (!span) {
+ let start = at
+ while (start > from && !/[\s(=;,`]/.test(flat.charAt(start - 1))) start--
+ let stop = at
+ while (stop < to && !/[\s);,`]/.test(flat.charAt(stop))) stop++
+ span = [start, stop]
+ }
+ let token = flat.slice(span[0], span[1])
+ const self = /^(?:%~dp0|%dp0%|\$basedir|\$\{basedir\})\/?/i.exec(token)
+ if (self) token = token.slice(self[0].length).replace(/^\/+/, '')
+ if (/[%$]/.test(token)) return null
+ return path.resolve(shimDir, token)
+}
+
+function versionFromPackageJson(pkgDir: string): string | null {
+ try {
+ const raw = fs.readFileSync(path.join(pkgDir, 'package.json'), 'utf8')
+ const version = (JSON.parse(raw) as { version?: unknown }).version
+ return typeof version === 'string' ? parseCliVersion(version) : null
+ } catch {
+ return null
+ }
+}
+
+/** The version recorded by the npm package that owns `binPath`, or null when it
+ * belongs to none — read, never run.
+ *
+ * Two shapes, because npm puts a shim *beside* node_modules rather than a link
+ * into it: a path already inside the package tree is answered by walking up to
+ * its package.json, while `C:\…\opencode` (the shim) only names the package in
+ * its text (`%dp0%\node_modules\opencode-ai\bin\opencode.exe`). Magpie splits
+ * these the same way, as npmVersion and shimVersion. pnpm's shim is the second
+ * shape with the store in the middle — `%~dp0\..\global\5\node_modules\…` — so
+ * the path is resolved from the shim's directory instead of assumed beside it.
+ *
+ * This exists because an installed CLI can fail to answer: opencode-ai ships a
+ * placeholder `bin/opencode.exe` until its postinstall runs, and that
+ * placeholder exits with an error instead of a version — so the Computers tab
+ * read "版本未知" for a CLI whose package plainly said 1.19.4. */
+export function npmPackageVersion(binPath: string | null): string | null {
+ if (!binPath) return null
+ const real = realpathOf(binPath)
+ const owned = versionOfOwner(real)
+ if (owned) return owned
+
+ let text: string
+ try {
+ const stat = fs.statSync(real)
+ if (!stat.isFile() || stat.size > SHIM_READ_LIMIT) return null
+ text = fs.readFileSync(real, 'utf8')
+ } catch {
+ return null
+ }
+ // Shims spell the path with the separator of the platform they came from, and
+ // a pnpm shim mentions several; take the first that names an installed package.
+ const flat = text.replace(/\\/g, '/')
+ for (let at = flat.indexOf('/node_modules/'); at >= 0; at = flat.indexOf('/node_modules/', at + 1)) {
+ const target = shimPathAt(flat, at, path.dirname(real))
+ const version = target ? versionOfOwner(target) : null
+ if (version) return version
+ }
+ return null
+}
+
+/** What this machine has installed for one engine: what the CLI prints, else
+ * what its npm package records.
+ *
+ * Asked in that order deliberately, unlike magpie (which reads the package
+ * first and so never runs an npm CLI): `claude update` and `codex update`
+ * replace the binary in place, so the printed version describes what will
+ * actually run while package.json can lag behind it. The package is only the
+ * fallback for a CLI that cannot answer at all — which is the whole bug. */
+async function installedVersion(id: string, binPath: string): Promise<string | null> {
+ const spec = ENGINE_VERSION_SPECS[id]
+ if (!spec) return null
+ const printed = versionFromOutput(await spawnCommand(binPath, spec.versionArgs, 6000))
+ return printed ?? npmPackageVersion(binPath)
+}
+
export function isHomebrewInstall(real: string, formula: string): boolean {
// npm under Homebrew's Node belongs to npm, not the Node formula's keg.
return real.match(/(?:^|[/\\])(?:Cellar|Caskroom)[/\\]([^/\\]+)[/\\]/i)?.[1] === formula
@@ -403,7 +538,7 @@ export async function probeEngineVersion(id: string, binPath: string | null): Pr
const empty: EngineVersionInfo = { version: null, latest: null, outdated: false, updateCommand: null }
if (!spec || !binPath) return empty
- const version = versionFromOutput(await spawnCommand(binPath, spec.versionArgs, 6000))
+ const version = await installedVersion(id, binPath)
const latest = await probeLatest(id, spec, binPath)
const outdated = isCliOutdated(version, latest)
return {
@@ -415,7 +550,11 @@ export async function probeEngineVersion(id: string, binPath: string | null): Pr
}
/** Local-only version probe for security capability gates. Unlike
- * probeEngineVersion(), this never contacts npm or an engine updater. */
+ * probeEngineVersion(), this never contacts npm or an engine updater — and,
+ * unlike it, never falls back to package.json either: this answer decides
+ * whether a binary may run under the sandbox, so a CLI that cannot say what it
+ * is stays unverified rather than borrowing the version of the package it was
+ * installed from. */
export async function probeLocalEngineVersion(id: string, binPath: string | null): Promise<string | null> {
const spec = ENGINE_VERSION_SPECS[id]
if (!spec || !binPath) return null