|
1 | 1 | import test from 'node:test'; |
2 | 2 | import assert from 'node:assert/strict'; |
| 3 | +import { appendFile, readFile } from 'node:fs/promises'; |
| 4 | +import path from 'node:path'; |
3 | 5 | import { capabilitySchema, type ToolBinding, type RunEvent } from '../src/core/contracts.js'; |
4 | 6 | import { makeBundle } from '../src/lang/bundle.js'; |
5 | 7 | import { invocationSchema } from '../src/gateway/protocol.js'; |
@@ -558,6 +560,81 @@ test('partial approvals do not create duplicate approvals, repeat writes, or spi |
558 | 560 | } |
559 | 561 | }); |
560 | 562 |
|
| 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 | + |
561 | 638 | test('approval decisions arriving before the parking checkpoint are not lost', async () => { |
562 | 639 | let writes = 0; |
563 | 640 | const s = await setup(basicModel(), { maxActiveRuns: 1 }); |
|
0 commit comments