Skip to content

Commit d4cc105

Browse files
author
hzq
committed
Discover POSIX agents without opening their executables
1 parent a1fff4b commit d4cc105

6 files changed

Lines changed: 70 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ English · [简体中文](CHANGELOG.zh-CN.md)
66

77
## 0.0.72 (2026-11-24)
88

9+
- On macOS and Linux, discovering agents checks executable paths without opening the binaries. An agent installed on a removable volume no longer holds up startup while the OS requests access to its contents.
910
- The ACP worker waits for accepted text and progress callbacks before reporting success. A slow or failed delivery is no longer hidden by an earlier `end_turn`; cancellation does not resend the prompt.
1011
- Embed EverythingACP 0.4.0: generated wrappers now carry a package record, managed launches verify recorded assets as well as the recipe, and `oaa acp` exposes the bundled upstream CLI. Existing unrecorded recipe-pinned wrappers keep working. The desktop and console interface are unchanged.
1112
- Opening an approval and parking or yielding a run now verify their own committed records. A lost SQLite acknowledgement no longer fails an already approved run, publishes a phantom request, or repeats a tool. A native caller's pending step and request are registered together.

‎CHANGELOG.zh-CN.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
## 0.0.72(2026-11-24)
88

9+
- macOS 和 Linux 发现本机智能体时只检查可执行文件的路径,不再读取程序内容。程序装在可移除磁盘上时,不会再因这次读取触发授权弹窗而卡住启动。
910
- ACP 工作节点在已收到的文本和进度回调处理完后才报告成功。对端先发来的 `end_turn` 不会掩盖慢回调或写入失败;取消不会重发提示。
1011
- 内置 EverythingACP 0.4.0。新生成的托管包装器会在导入和启动时核对配方与包内记录的文件;旧版仅固定配方的包装器仍可使用。`oaa acp` 可直接运行随包提供的上游 CLI,桌面和控制台界面不变。
1112
- 创建审批待办、暂停等待和公平让出执行槽时,内核会核对本次提交的原记录。SQLite 回执丢失不会再把已批准的任务判为失败,也不会多发待办或重做工具;原生调用方工具的待处理步骤与待办一起登记。

‎src/config/agent-presets.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ async function executableFile(candidate: string): Promise<boolean> {
520520
}
521521
}
522522

523-
/** Resolve the first executable `names` entry on PATH. Only file metadata is read; nothing runs. */
523+
/** Resolve by file metadata, or by a small npm batch shim on Windows. Nothing runs. */
524524
export async function findOnPath(
525525
names: readonly string[],
526526
pathEnv = process.env.PATH ?? '',
@@ -537,7 +537,8 @@ export async function findOnPath(
537537
if (!candidate || !(await executableFile(candidate))) continue;
538538
if (realpathIncludes) {
539539
try {
540-
const real = npmShimTarget(candidate) ?? (await realpath(candidate));
540+
const shim = process.platform === 'win32' ? npmShimTarget(candidate) : undefined;
541+
const real = shim ?? (await realpath(candidate));
541542
if (!real.toLowerCase().includes(realpathIncludes)) continue;
542543
} catch {
543544
continue;

‎src/core/which.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* and launching. On Windows a bare name or an extension-less path is resolved through PATHEXT,
44
* the way `where` does, so `codex` finds `codex.cmd` / `codex.exe`. File metadata only; nothing runs.
55
*/
6-
import { accessSync, constants, readFileSync, statSync } from 'node:fs';
6+
import { accessSync, closeSync, constants, fstatSync, openSync, readSync, statSync } from 'node:fs';
77
import path from 'node:path';
88

99
/** Extensions tried for a bare command name, from PATHEXT (lower-cased, with a sane default). */
@@ -50,15 +50,32 @@ export function resolveWindowsCommand(
5050
* or undefined when the batch file is anything else.
5151
*/
5252
export function npmShimTarget(shimPath: string, text?: string): string | undefined {
53+
if (!/\.(cmd|bat)$/i.test(shimPath)) return undefined;
54+
const maxBytes = 16_384;
5355
let body = text;
5456
if (body === undefined) {
57+
let fd: number | undefined;
5558
try {
56-
body = readFileSync(shimPath, 'utf8');
59+
fd = openSync(shimPath, 'r');
60+
const info = fstatSync(fd);
61+
if (!info.isFile() || info.size > maxBytes) return undefined;
62+
// Bound the read even if the launcher is replaced or grows during discovery.
63+
const bytes = Buffer.alloc(info.size + 1);
64+
let length = 0;
65+
while (length < bytes.length) {
66+
const count = readSync(fd, bytes, length, bytes.length - length, null);
67+
if (!count) break;
68+
length += count;
69+
}
70+
if (length !== info.size) return undefined;
71+
body = bytes.toString('utf8', 0, length);
5772
} catch {
5873
return undefined;
74+
} finally {
75+
if (fd !== undefined) closeSync(fd);
5976
}
6077
}
61-
if (body.length > 16_384 || !/%\*/.test(body)) return undefined;
78+
if (body.length > maxBytes || !/%\*/.test(body)) return undefined;
6279
const match = /"%~?dp0%?\\([^"%]+\.(?:js|mjs|cjs))"/i.exec(body);
6380
if (!match) return undefined;
6481
const target = path.join(path.dirname(shimPath), match[1]!);

‎test/acp.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import { promisify } from 'node:util';
1010
import { runAcp, type AcpTimings } from '../src/engines/acp.js';
1111
import { gatewaySchema, initialize, workerSchema } from '../src/config/config.js';
1212
import { WorkerRuntime } from '../src/fabric/worker.js';
13-
import { acpPresets, discoverAcpAgents, nativeBackends } from '../src/config/agent-presets.js';
13+
import {
14+
acpPresets,
15+
discoverAcpAgents,
16+
findOnPath,
17+
nativeBackends,
18+
} from '../src/config/agent-presets.js';
1419
import { conversionProfile } from '../src/engines/conformance.js';
1520
import { createGateway } from '../src/gateway/server.js';
1621
import { invocationSchema } from '../src/gateway/protocol.js';
@@ -640,6 +645,29 @@ async function fakeBinaries(names: string[]) {
640645
return { root, bin };
641646
}
642647

648+
test(
649+
'POSIX discovery identifies an executable by its real path, not its contents',
650+
{
651+
skip: process.platform === 'win32',
652+
},
653+
async (t) => {
654+
const root = await tempDir('oaa-path-marker-');
655+
t.after(() => rm(root, { recursive: true, force: true }));
656+
await writeFile(path.join(root, 'cursor.js'), '');
657+
for (const name of ['agent', 'agent.cmd']) {
658+
await writeFile(path.join(root, name), '"%dp0%\\cursor.js" %*', { mode: 0o755 });
659+
assert.equal(await findOnPath([name], root, 'cursor'), undefined);
660+
}
661+
const real = path.join(root, 'cursor-native');
662+
await writeFile(real, '', { mode: 0o111 });
663+
await symlink(real, path.join(root, 'vendor-agent'));
664+
assert.equal(
665+
await findOnPath(['vendor-agent'], root, 'cursor'),
666+
path.join(root, 'vendor-agent'),
667+
);
668+
},
669+
);
670+
643671
test('discovery finds installed ACP executables by name only and init records them disabled', async () => {
644672
const { root, bin } = await fakeBinaries(['gemini', 'qwen', 'dsh', 'codex-acp', 'npx', 'agent']);
645673
const original = process.env.PATH;

‎test/win-spawn.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ test('npmShimTarget reads the entry point of an npm cmd-shim', () => {
8686
assert.equal(npmShimTarget('x.cmd', '@echo off\r\nnode other.js'), undefined);
8787
});
8888

89+
test('only small batch launchers are read as npm shims', async (t) => {
90+
const dir = await mkdtemp(path.join(os.tmpdir(), 'oaa-shim-'));
91+
t.after(() => rm(dir, { recursive: true, force: true }));
92+
const entry = path.join(dir, 'cli.js');
93+
const shim = '"%dp0%\\cli.js" %*';
94+
await writeFile(entry, '');
95+
for (const name of ['tool.cmd', 'tool.BAT', 'tool.exe']) {
96+
const file = path.join(dir, name);
97+
await writeFile(file, shim);
98+
assert.equal(npmShimTarget(file), name === 'tool.exe' ? undefined : entry);
99+
}
100+
const big = path.join(dir, 'large.cmd');
101+
await writeFile(big, shim.padEnd(16_385));
102+
assert.equal(npmShimTarget(big), undefined);
103+
});
104+
89105
test('launchPlan is a pass-through outside Windows', () => {
90106
assert.deepEqual(launchPlan('codex', ['exec', 'a b'], process.env, 'linux'), {
91107
command: 'codex',

0 commit comments

Comments
 (0)