Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
// ensure-agent-binaries 纯辅助函数的单测。
//
// 这些 helper 是整个 de-LFS 改动的正确性核心:必须拒绝 LFS pointer / 过小占位,
// 才能保证按需下载逻辑不会把半成品当成"已就位"而跳过。用 node 内置 test runner,
// 不依赖 vitest / 任何 deps,可直接 `node --test scripts/__tests__/`。
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import {
binaryRelativePathFor,
binFileFor,
ensureCodexPackageLauncher,
isValidBinary,
isValidDirDist,
listSiblingWorktreeRoots,
readInstalledVersion,
SUPPORTED_BINARY_KINDS,
supportsCdnFallback,
tryReuseFromSiblingWorktree,
tryReuseDirDistFromSiblingWorktree,
updateScriptForKind,
} from '../ensure-agent-binaries.mjs';
import { verifyDirDistManifest, writeDirDistManifest } from '../../tools/shared/dir-dist-manifest.mjs';
// 测试价值:生产 ensureCodexPackageLauncher 为已验证包补入口并保持清单有效;
// 回归 Codex shell 被旧系统 CLI 抢占。原覆盖只验证下载/复用,未覆盖包内入口。
// 最小输入是有效但缺入口的包;node unit,POSIX 另执行一次脚本检查 argv 不变。
test('Codex launcher preserves the manifest, updates managed files and leaves upstream files alone', (t) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'codex launcher space-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
fs.mkdirSync(path.join(root, 'bin'));
fs.mkdirSync(path.join(root, 'codex-path'));
fs.writeFileSync(path.join(root, 'bin', 'codex'), '#!/bin/sh\nprintf \'%s\\0\' "$@"\n', { mode: 0o755 });
fs.writeFileSync(path.join(root, 'codex-package.json'), JSON.stringify({ pathDir: 'codex-path' }));
writeDirDistManifest(root);
const destination = path.join(root, 'codex-path', 'codex');
const manifestPath = path.join(root, '.manifest');
assert.equal(verifyDirDistManifest(root), true);
ensureCodexPackageLauncher(root, 'darwin-arm64');
assert.equal(verifyDirDistManifest(root), true);
const content = fs.readFileSync(destination, 'utf8');
const previous = new Date('2000-01-01T00:00:00Z');
fs.utimesSync(destination, previous, previous);
fs.utimesSync(manifestPath, previous, previous);
ensureCodexPackageLauncher(root, 'darwin-arm64');
assert.equal(fs.statSync(destination).mtimeMs, previous.getTime());
assert.equal(fs.statSync(manifestPath).mtimeMs, previous.getTime());
if (process.platform !== 'win32') {
assert.equal(fs.statSync(destination).mode & 0o777, 0o755);
const args = ['a b', '"quoted"', "it's", '', '-n', '$HOME', '%^!'];
assert.deepEqual(execFileSync(destination, args), Buffer.from(args.join('\0') + '\0'));
}
fs.writeFileSync(destination, content.replace('exec ', '# old\nexec '));
writeDirDistManifest(root);
ensureCodexPackageLauncher(root, 'linux-x64');
assert.equal(fs.readFileSync(destination, 'utf8'), content);
assert.equal(verifyDirDistManifest(root), true);
fs.writeFileSync(destination, 'upstream executable');
writeDirDistManifest(root);
ensureCodexPackageLauncher(root, 'linux-x64');
assert.equal(fs.readFileSync(destination, 'utf8'), 'upstream executable');
assert.equal(verifyDirDistManifest(root), true);
});
test('Windows Codex targets never get a POSIX launcher, including cross-platform preparation', (t) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'codex-launcher-win-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
fs.mkdirSync(path.join(root, 'codex-path'));
fs.writeFileSync(path.join(root, 'codex-package.json'), '{}');
writeDirDistManifest(root);
ensureCodexPackageLauncher(root, 'win32-x64');
assert.deepEqual(fs.readdirSync(path.join(root, 'codex-path')), []);
assert.equal(verifyDirDistManifest(root), true);
});
test('directory distributions never use the single-binary CDN fallback', () => {
assert.equal(supportsCdnFallback('pi'), false);
assert.equal(supportsCdnFallback('codex'), false);
});
test('dev startup prepares every supported runtime, including Pi', () => {
assert.deepEqual(SUPPORTED_BINARY_KINDS, ['claude', 'codex', 'ripgrep', 'pi']);
const devGuard = fs.readFileSync(
new URL('../ensure-dev-runtime-assets.mjs', import.meta.url),
'utf8',
);
assert.match(devGuard, /const AGENT_KINDS = SUPPORTED_BINARY_KINDS;/);
});
const LFS_POINTER = [
'version https://git-lfs.github.com/spec/v1',
'oid sha256:0000000000000000000000000000000000000000000000000000000000000000',
'size 239438648',
'',
].join('\n');
test('directory reuse validates all assets, skips mismatches and replaces stale files', (t) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'reuse-dir-dist-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const makeSource = (name, version) => {
const dir = path.join(root, name);
fs.mkdirSync(path.join(dir, 'bin'), { recursive: true });
fs.writeFileSync(path.join(dir, 'bin', 'codex'), Buffer.alloc(2048, 1), { mode: 0o755 });
fs.writeFileSync(path.join(dir, 'asset.json'), 'good');
fs.writeFileSync(path.join(dir, '.version'), version);
writeDirDistManifest(dir);
return dir;
};
const old = makeSource('old', '1');
const damaged = makeSource('damaged', '2');
fs.writeFileSync(path.join(damaged, 'asset.json'), 'evil');
const good = makeSource('good', '2');
const destDir = makeSource('dest', '1');
fs.writeFileSync(path.join(destDir, 'stale'), 'old asset');
const options = {
binaryRelativePath: path.join('bin', 'codex'),
requiredFiles: ['asset.json'], version: '2', destDir,
};
assert.equal(tryReuseDirDistFromSiblingWorktree({
...options, candidates: [old, damaged, path.join(root, 'missing')],
}), null);
assert.equal(fs.readFileSync(path.join(destDir, '.version'), 'utf8'), '1');
assert.equal(tryReuseDirDistFromSiblingWorktree({
...options, candidates: [damaged, good],
}), good);
assert.equal(verifyDirDistManifest(destDir), true);
assert.equal(fs.existsSync(path.join(destDir, 'stale')), false);
assert.equal(fs.readFileSync(path.join(destDir, 'asset.json'), 'utf8'), 'good');
fs.writeFileSync(path.join(destDir, 'asset.json'), 'edit');
assert.equal(fs.readFileSync(path.join(good, 'asset.json'), 'utf8'), 'good');
assert.equal(fs.readdirSync(root).some((name) => name.startsWith('dest.reuse-')), false);
});
function tmpFile(name, contents) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ensure-bin-test-'));
const p = path.join(dir, name);
fs.writeFileSync(p, contents);
return p;
}
test('binFileFor: win32 gets .exe, other platforms get bare name', () => {
assert.equal(binFileFor('claude', 'win32-x64'), 'claude.exe');
assert.equal(binFileFor('rg', 'win32-x64'), 'rg.exe');
assert.equal(binFileFor('claude', 'darwin-arm64'), 'claude');
assert.equal(binFileFor('codex', 'linux-x64'), 'codex');
});
test('Codex dev runtime resolves the complete package entrypoint and updater', () => {
assert.equal(binaryRelativePathFor('codex', 'win32-x64'), path.join('bin', 'codex.exe'));
assert.equal(binaryRelativePathFor('codex', 'darwin-arm64'), path.join('bin', 'codex'));
assert.equal(updateScriptForKind('codex'), 'codex-package');
});
test('readInstalledVersion: trims content, null on missing/empty', () => {
assert.equal(readInstalledVersion(tmpFile('.version', '15.1.0\n')), '15.1.0');
assert.equal(readInstalledVersion(tmpFile('.version', ' 2.1.186 ')), '2.1.186');
assert.equal(readInstalledVersion(tmpFile('.version', '')), null);
assert.equal(readInstalledVersion(path.join(os.tmpdir(), 'does-not-exist-xyz', '.version')), null);
});
test('isValidBinary: rejects missing, LFS pointer, and tiny placeholder', () => {
// missing
assert.equal(isValidBinary(path.join(os.tmpdir(), 'nope-xyz', 'claude')), false);
// LFS pointer (starts with the spec header) — must be rejected even though it is a real file
assert.equal(isValidBinary(tmpFile('claude', LFS_POINTER)), false);
// tiny placeholder (< 1024 bytes)
assert.equal(isValidBinary(tmpFile('claude', 'x'.repeat(100))), false);
});
test('isValidBinary: accepts a non-pointer file >= 1024 bytes', () => {
assert.equal(isValidBinary(tmpFile('claude', Buffer.alloc(2048, 1))), true);
});
// ── dirDist(目录分发)安装清单 ────────────────────────────────────────────────
// codex review 回归:pi 缺 theme/ 等旁侧资产时 RPC 启动即崩,skip 判定只验主执行
// 文件会把残缺目录当"已就位"打进安装包 → 就位判定必须整目录对清单校验。
test('isValidDirDist: requires the manifest and every sidecar asset, not just the main binary', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ensure-dirdist-'));
const binPath = path.join(dir, 'pi');
fs.writeFileSync(binPath, Buffer.alloc(4096, 1));
fs.mkdirSync(path.join(dir, 'theme'));
fs.writeFileSync(path.join(dir, 'theme', 'default.json'), '{"accent":"pi"}');
fs.writeFileSync(path.join(dir, '.version'), '0.82.1\n');
// 主执行文件合法但无清单(旧安装/半成品)→ 不算就位
assert.equal(isValidDirDist(dir, binPath), false);
assert.equal(writeDirDistManifest(dir) >= 2, true);
assert.equal(isValidDirDist(dir, binPath), true);
// 旁侧资产被删 → 清单校验失败,重新进入下载/promote
fs.rmSync(path.join(dir, 'theme'), { recursive: true, force: true });
assert.equal(isValidDirDist(dir, binPath), false);
});
test('isValidDirDist: required runtime assets must also be present', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ensure-dirdist-required-'));
const binPath = path.join(dir, 'pi');
fs.writeFileSync(binPath, Buffer.alloc(4096, 1));
fs.mkdirSync(path.join(dir, 'theme'));
for (const name of ['dark.json', 'light.json', 'theme-schema.json']) {
fs.writeFileSync(path.join(dir, 'theme', name), '{}');
}
writeDirDistManifest(dir);
const required = ['theme/dark.json', 'theme/light.json', 'theme/theme-schema.json'];
assert.equal(isValidDirDist(dir, binPath, required), true);
fs.rmSync(path.join(dir, 'theme', 'light.json'));
assert.equal(isValidDirDist(dir, binPath, required), false);
});
test('verifyDirDistManifest: rejects size drift, empty manifests, and malformed entries', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ensure-dirdist-verify-'));
fs.writeFileSync(path.join(dir, 'pi'), Buffer.alloc(4096, 1));
fs.writeFileSync(path.join(dir, 'photon.wasm'), Buffer.alloc(512, 2));
writeDirDistManifest(dir);
assert.equal(verifyDirDistManifest(dir), true);
// 同长度内容被替换/损坏(字节数不变)→ 现按 sha256 拒绝(供应链加固 P1)
writeDirDistManifest(dir);
assert.equal(verifyDirDistManifest(dir), true);
fs.writeFileSync(path.join(dir, 'photon.wasm'), Buffer.alloc(512, 9)); // 同长度、不同内容
assert.equal(verifyDirDistManifest(dir), false);
// 旧版本写的 size-only 清单(无 sha256)→ 一律不可信,拒绝(自愈重下)
fs.writeFileSync(path.join(dir, 'photon.wasm'), Buffer.alloc(512, 2)); // 复原内容
fs.writeFileSync(
path.join(dir, '.manifest'),
JSON.stringify({ files: [{ path: 'photon.wasm', size: 512 }] }),
);
assert.equal(verifyDirDistManifest(dir), false);
// 字节数漂移(截断/损坏)→ 拒绝
writeDirDistManifest(dir);
fs.writeFileSync(path.join(dir, 'photon.wasm'), Buffer.alloc(8, 2));
assert.equal(verifyDirDistManifest(dir), false);
// 清单之外的多余文件(旧构建残留 / 本地污染)→ 拒绝(不能作为未验证资产打进安装包)
fs.writeFileSync(path.join(dir, 'photon.wasm'), Buffer.alloc(512, 2)); // 复原
writeDirDistManifest(dir);
assert.equal(verifyDirDistManifest(dir), true);
fs.writeFileSync(path.join(dir, 'stray-residue.bin'), Buffer.alloc(16, 3));
assert.equal(verifyDirDistManifest(dir), false);
fs.rmSync(path.join(dir, 'stray-residue.bin'));
assert.equal(verifyDirDistManifest(dir), true); // 移除多余文件后恢复
// 嵌套子目录里的多余文件同样拒绝
fs.mkdirSync(path.join(dir, 'nested'), { recursive: true });
fs.writeFileSync(path.join(dir, 'nested', 'extra.json'), '{}');
assert.equal(verifyDirDistManifest(dir), false);
fs.rmSync(path.join(dir, 'nested'), { recursive: true, force: true });
// 空清单 / 非法结构 → 拒绝
fs.writeFileSync(path.join(dir, '.manifest'), JSON.stringify({ files: [] }));
assert.equal(verifyDirDistManifest(dir), false);
fs.writeFileSync(path.join(dir, '.manifest'), '{"files":"nope"}');
assert.equal(verifyDirDistManifest(dir), false);
fs.writeFileSync(path.join(dir, '.manifest'), 'not json');
assert.equal(verifyDirDistManifest(dir), false);
});
// ── 兄弟 worktree 本地复用 ────────────────────────────────────────────────────
function tmpDir(prefix) {
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
}
function makeBinDir(version, binFile, contents) {
const dir = tmpDir('reuse-src-');
if (version !== null) fs.writeFileSync(path.join(dir, '.version'), `${version}\n`);
if (contents !== null) fs.writeFileSync(path.join(dir, binFile), contents);
return dir;
}
const REAL_BINARY = Buffer.alloc(4096, 7);
test('tryReuseFromSiblingWorktree: copies matching valid binary and writes marker', () => {
const src = makeBinDir('1.2.3', 'claude', REAL_BINARY);
const dest = tmpDir('reuse-dest-');
const reusedFrom = tryReuseFromSiblingWorktree({
candidates: [src],
binFile: 'claude',
version: '1.2.3',
destDir: dest,
});
assert.equal(reusedFrom, src);
assert.deepEqual(fs.readFileSync(path.join(dest, 'claude')), REAL_BINARY);
assert.equal(fs.readFileSync(path.join(dest, '.version'), 'utf8').trim(), '1.2.3');
if (process.platform !== 'win32') {
assert.notEqual(fs.statSync(path.join(dest, 'claude')).mode & 0o111, 0);
}
});
test('tryReuseFromSiblingWorktree: skips version mismatch, LFS pointer, and missing candidates', () => {
const wrongVersion = makeBinDir('9.9.9', 'claude', REAL_BINARY);
const lfsPointer = makeBinDir('1.2.3', 'claude', LFS_POINTER);
const noBinary = makeBinDir('1.2.3', 'claude', null);
const noMarker = makeBinDir(null, 'claude', REAL_BINARY);
const missingDir = path.join(os.tmpdir(), 'reuse-does-not-exist-xyz');
const dest = tmpDir('reuse-dest-');
const reusedFrom = tryReuseFromSiblingWorktree({
candidates: [wrongVersion, lfsPointer, noBinary, noMarker, missingDir],
binFile: 'claude',
version: '1.2.3',
destDir: dest,
});
assert.equal(reusedFrom, null);
assert.equal(fs.existsSync(path.join(dest, 'claude')), false);
});
test('tryReuseFromSiblingWorktree: falls through bad candidates to the first good one', () => {
const bad = makeBinDir('9.9.9', 'rg', REAL_BINARY);
const good = makeBinDir('15.1.0', 'rg', REAL_BINARY);
const dest = tmpDir('reuse-dest-');
const reusedFrom = tryReuseFromSiblingWorktree({
candidates: [bad, good],
binFile: 'rg',
version: '15.1.0',
destDir: dest,
});
assert.equal(reusedFrom, good);
assert.deepEqual(fs.readFileSync(path.join(dest, 'rg')), REAL_BINARY);
});
test('listSiblingWorktreeRoots: returns [] outside a git repository', () => {
assert.deepEqual(listSiblingWorktreeRoots(tmpDir('not-a-repo-')), []);
});
test('listSiblingWorktreeRoots: lists other worktrees of the same repo, excluding self', () => {
const repo = tmpDir('reuse-repo-');
const git = (...args) =>
execFileSync('git', ['-C', repo, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
git('init', '--initial-branch=main');
git('config', 'user.email', '[email protected]');
git('config', 'user.name', 'Test');
fs.writeFileSync(path.join(repo, 'README.md'), 'hi');
git('add', '.');
git('commit', '-m', 'init', '--no-gpg-sign');
const wt = path.join(tmpDir('reuse-wt-parent-'), 'wt-a');
git('worktree', 'add', wt, '-b', 'wt-a');
const fromMain = listSiblingWorktreeRoots(repo);
assert.deepEqual(fromMain, [fs.realpathSync.native(wt)]);
const fromWt = listSiblingWorktreeRoots(wt);
assert.deepEqual(fromWt, [fs.realpathSync.native(repo)]);
});