fix: await attested owned host during slow startup registration

This commit is contained in:
Agent Workbench Dev
2026-11-01 14:02:13 +08:00
parent 52d9b86ae1
commit b144d123a0
3 changed files with 63 additions and 1 deletions
+1
View File
@@ -3,6 +3,7 @@
## 1.20.1 · 真实案例任务控制修复
- Windows停止核验处理进程在快照和用户查询之间退出的竞态,仅经新查询确认进程不存在才判定退出;用户权限失败、查询失败或PID复用继续拒绝。
- 所属宿主启动期间的健康探测超时可在原期限内继续只读等待,每次核对原进程创建/用户/程序身份,最终仍须完整连接证明;超时不重启第二个宿主。
- 原目标、检查约定与短句续接独立保存。错误诊断使用只读Run,答复结束保留原任务状态;明确修复要求仍正常执行。
- 审查后继续改为中性系统操作,不再新增通用测试要求。无需测试的文件在审查应用后直接反馈路径,模型文字不替代文件交付或检查通过。
+12 -1
View File
@@ -132,7 +132,18 @@ export class HostController {
while (Date.now() < deadline) {
if (failure || child.exitCode !== null) throw hostError('所属宿主启动失败,请查看本机启动记录。', 'HOST_START_FAILED');
await delay(100);
const health = await this.health();
let health;
try { health = await this.health(); }
catch (cause) {
if (cause.code !== 'HOST_PORT_UNVERIFIED') throw cause;
const waiting = observeWindowsProcess(child.pid);
if (!waiting || !observedChild || waiting.createdAt !== observedChild.createdAt || waiting.userSid !== observedChild.userSid
|| !samePath(waiting.executable, this.node.path)) throw hostError('所属宿主启动进程已结束或身份变化,请核对原日志。', 'HOST_START_FAILED');
// Registration can still occupy this owned process after listen().
// Retry only observation, within the original deadline; never launch
// another host or accept a connection without the complete proof.
continue;
}
if (!health) continue;
if (health.pid !== child.pid) throw hostError('启动期间端口被其他进程占用。');
const connected = await this.connect(); if (connected) {
+50
View File
@@ -0,0 +1,50 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { setTimeout as delay } from 'node:timers/promises';
import { fixture, workspace } from './helpers.mjs';
import { HostController } from '../src/core/desktop/host-controller.mjs';
import { observeWindowsProcess } from '../src/core/desktop/process-identity.mjs';
function ownedStartup(t, timeoutMs = 10000) {
const f = fixture(t), appRoot = path.join(f.root, 'owned-startup-app'), dataRoot = path.join(f.root, 'owned-startup-data');
for (const directory of ['dist-client', 'dist-server', 'dist-desktop']) fs.cpSync(path.join(workspace, directory), path.join(appRoot, directory), { recursive: true });
fs.copyFileSync(path.join(workspace, 'package.json'), path.join(appRoot, 'package.json'));
const source = "import fs from 'node:fs';import path from 'node:path';const stop=path.join(process.env.AGENT_WORKBENCH_DATA_DIR,'exit-owned-startup');setInterval(()=>{if(fs.existsSync(stop))process.exit(0)},25);";
fs.writeFileSync(path.join(appRoot, 'dist-server/server.mjs'), source);
const controller = new HostController({ appRoot, dataRoot, nodePath: process.execPath, port: 49997, timeoutMs });
const intent = () => JSON.parse(fs.readFileSync(path.join(dataRoot, 'desktop-launch-state.json'), 'utf8'));
f.onCleanup(async () => {
fs.writeFileSync(path.join(dataRoot, 'exit-owned-startup'), 'exit through own fixture');
const pid = intent().pid, deadline = Date.now() + 10000;
while (observeWindowsProcess(pid)) { assert.ok(Date.now() < deadline, 'Owned fixture must exit through its own sentinel.'); await delay(50); }
});
return { controller, intent };
}
test('owned startup retries only read-only observation after a slow health response and attests the same original child', { timeout: 30000 }, async t => {
const { controller, intent } = ownedStartup(t); let connects = 0, observations = 0;
controller.connect = async () => ++connects === 1 ? null : { identity: { pid: intent().pid, instanceId: 'owned-attested-result' } };
controller.health = async () => {
if (++observations === 1) throw Object.assign(new Error('controlled registration delay'), { code: 'HOST_PORT_UNVERIFIED' });
return { pid: intent().pid };
};
const result = await controller.start();
assert.equal(result.reused, false); assert.equal(connects, 2); assert.equal(observations, 2);
assert.equal(result.identity.pid, intent().pid); assert.equal(intent().state, 'ready');
assert.equal(observeWindowsProcess(intent().pid).createdAt, intent().createdAt);
});
test('unverified startup expires with its original live child and never treats timeout or an unrelated health PID as proof', { timeout: 30000 }, async t => {
const { controller, intent } = ownedStartup(t, 1000); let connects = 0;
controller.connect = async () => { connects++; return null; };
controller.health = async () => { throw Object.assign(new Error('controlled unknown response'), { code: 'HOST_PORT_UNVERIFIED' }); };
await assert.rejects(controller.start(), error => error.code === 'HOST_START_UNVERIFIED');
const original = intent(); assert.equal(connects, 1); assert.equal(original.state, 'starting');
assert.equal(observeWindowsProcess(original.pid).createdAt, original.createdAt);
controller.health = async () => ({ pid: original.pid + 1 });
// The original unresolved launch prevents a second spawn, even after timeout.
await assert.rejects(controller.start(), error => error.code === 'HOST_START_UNVERIFIED');
assert.equal(intent().pid, original.pid);
});