Skip to content

Commit ed65472

Browse files
author
hzq
committed
Verify identical writes require separate step approvals after resume
1 parent 096ad54 commit ed65472

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎docs/architecture.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ A kernel tool approval is a durable suspension point, not a resident loop that h
150150

151151
Independent graph branches can bypass a blocked approval and keep running. A failed parallel step takes priority over parking, and a side effect with an unknown outcome takes priority over an ordinary failure. A partial approval unlocks only the corresponding ready work; denial, cancellation, expiry, and shutdown invalidate pending approvals and do not approve them implicitly. An approval decision racing with the parking process is checked once more after the execution body has exited.
152152

153+
This is progress by already-planned, independent steps of the same run. It does not mean the model starts another turn while waiting for a tool result. A client disconnecting or polling a background run is a separate case. With per-call approval, two calls with different step IDs need independent decisions even when the tool, arguments and target are identical. Resuming a confirmed first write reuses its result; it does not authorize the second write.
154+
153155
The original run deadline stays unchanged across parking and re-entry; an explicit operator resume after an interruption starts a new execution deadline. A parked request still counts toward the limit of 32 unfinished requests per owner. Under this limit, an existing idempotent replay can still be read, but a new idempotency key cannot get around it. `maxActiveRuns` limits executing execution bodies; a parked kernel run keeps only durable state and one small timer entry. The "requests in progress" total in the console overview includes queued and approval-waiting requests, not only those holding an execution slot.
154156

155157
An external agent's approval still holds its host process and a global run slot, because these opaque backends have no verified pause protocol. Their approval notifications are event-driven rather than polled, but they are not described as suspendable or migratable.

‎docs/zh-CN/architecture.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ Open Agent API 对外提供的是**智能体的能力**,而不仅仅是模型
150150

151151
独立的图分支可以绕过被阻塞的审批继续运行。失败的并行步骤优先于挂起,结果未知的副作用优先于普通失败。部分批准只解锁对应的就绪工作;拒绝、取消、过期与关机会使待决审批失效,而不会隐含地批准它们。与停车过程竞争的审批决定会在执行体退出后再检查一次。
152152

153+
这里能继续的是同一运行中已经规划好、没有依赖阻塞的步骤,不是模型在等待工具结果时又开始一轮推理。客户端断线或轮询后台运行是另一回事。采用逐次审批时,即使工具、参数和目标完全一样,不同步骤的两次调用仍需分别决定。恢复时复用的是第一次写入已确认的结果,不是把它的批准转给第二次写入。
154+
153155
原来的运行截止时间在停车与重新入场之后保持不变;中断后由操作员显式恢复会开始一个新的执行截止时间。停车的请求仍计入每个 owner 32 个未完成请求的上限。在这个上限下,已有的幂等重放仍可读取,但新的幂等键不能绕过它。`maxActiveRuns` 限制的是正在执行的执行体,停车的内核运行只保留持久状态和一个很小的定时器条目。控制台概览的「进行中请求」总数包括排队中与等待审批的请求,而不仅仅是占着执行槽的请求。
154156

155157
外部智能体的审批仍然占着它的宿主进程和全局运行槽,因为这些不透明后端没有经过验证的暂停协议。它们的审批通知是事件驱动的而不是轮询的,但不把它们描述为可挂起或可迁移。

‎test/approval-scheduling.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import test from 'node:test';
22
import assert from 'node:assert/strict';
3+
import { appendFile, readFile } from 'node:fs/promises';
4+
import path from 'node:path';
35
import { capabilitySchema, type ToolBinding, type RunEvent } from '../src/core/contracts.js';
46
import { makeBundle } from '../src/lang/bundle.js';
57
import { invocationSchema } from '../src/gateway/protocol.js';
@@ -558,6 +560,81 @@ test('partial approvals do not create duplicate approvals, repeat writes, or spi
558560
}
559561
});
560562

563+
for (const reopen of [false, true]) {
564+
test(`one approval cannot dispatch a second identical write (reopen=${reopen})`, async () => {
565+
const args = { target: 'approved.txt', text: 'one write\n' };
566+
const model = new FixtureModel(() => ({
567+
calls: ['first', 'second'].map((id) => ({ id, name: 'guarded', arguments: { ...args } })),
568+
}));
569+
const s = await setup(model, { maxActiveRuns: 1, approvalTimeoutMs: 60_000 });
570+
let replacement: Awaited<ReturnType<typeof createGateway>> | undefined;
571+
try {
572+
const target = path.join(s.dir, 'approved.txt');
573+
const binding = installTool(s, 'guarded', true, async (input) => {
574+
assert.deepEqual(input, args);
575+
await appendFile(target, args.text, { mode: 0o600 });
576+
return { value: 'recorded' };
577+
});
578+
s.store.publish('owner', makeBundle(agent({ id: 'agent/review', tools: [binding] })));
579+
const run = submit(s);
580+
await parked(s, run.id);
581+
const actions = s.store.approvals(run.id);
582+
assert.equal(actions.length, 2);
583+
const [first, second] = [actions[0]!, actions[1]!];
584+
assert.deepEqual(first.detail, second.detail);
585+
assert.notEqual(first.stepId, second.stepId);
586+
assert.notEqual(first.id, second.id);
587+
await assert.rejects(readFile(target), { code: 'ENOENT' });
588+
s.kernel.decide(principal, run.id, first.id, 'approve');
589+
await waitFor(
590+
() =>
591+
s.store.run(run.id).checkpoint.suspension?.approvalIds.length === 1 &&
592+
s.store
593+
.steps(run.id)
594+
.some((step) => step.id === first.stepId && step.status === 'completed'),
595+
);
596+
assert.equal(await readFile(target, 'utf8'), args.text);
597+
598+
if (reopen) {
599+
await s.app.close();
600+
replacement = await createGateway(
601+
s.config,
602+
new WorkerRuntime(s.runtime.config, s.runtime.handlers, new Map([['fixture', model]])),
603+
);
604+
assert.equal(replacement.store.run(run.id).status, 'interrupted');
605+
assert.equal(replacement.store.getApproval(second.id, 'owner').status, 'expired');
606+
replacement.kernel.resume(principal, run.id);
607+
await waitFor(
608+
() => replacement!.store.run(run.id).checkpoint.suspension?.kind === 'approval',
609+
);
610+
}
611+
const current = replacement ?? s;
612+
const pending = current.store.approvals(run.id).filter((a) => a.status === 'pending');
613+
assert.equal(pending.length, 1);
614+
assert.equal(pending[0]!.stepId, second.stepId);
615+
if (reopen) assert.notEqual(pending[0]!.id, second.id);
616+
assert.throws(() => current.kernel.decide(principal, run.id, first.id, 'approve'), {
617+
code: 'stale_approval',
618+
});
619+
current.kernel.decide(principal, run.id, pending[0]!.id, 'deny');
620+
const final = await current.kernel.wait(run.id);
621+
assert.equal(final.status, 'failed');
622+
assert.equal(final.error?.code, 'permission_denied');
623+
assert.equal(await readFile(target, 'utf8'), args.text);
624+
assert.equal(
625+
current.store
626+
.steps(run.id)
627+
.filter((step) => step.status === 'completed' && step.name === 'guarded').length,
628+
1,
629+
);
630+
assert.equal(model.calls, 1);
631+
} finally {
632+
await replacement?.app.close();
633+
await s.cleanup();
634+
}
635+
});
636+
}
637+
561638
test('approval decisions arriving before the parking checkpoint are not lost', async () => {
562639
let writes = 0;
563640
const s = await setup(basicModel(), { maxActiveRuns: 1 });

0 commit comments

Comments
 (0)