agent-gear 2.5.0 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +53 -5
- package/dist/acp/__fixtures__/opencode-2.0.31-capabilities.json +25 -0
- package/dist/acp/bridge.d.ts +23 -1
- package/dist/acp/bridge.d.ts.map +1 -1
- package/dist/acp/bridge.js +152 -17
- package/dist/acp/bridge.js.map +1 -1
- package/dist/acp/bridge.test.js +366 -1
- package/dist/acp/bridge.test.js.map +1 -1
- package/dist/acp/capabilities.d.ts +81 -0
- package/dist/acp/capabilities.d.ts.map +1 -0
- package/dist/acp/capabilities.js +211 -0
- package/dist/acp/capabilities.js.map +1 -0
- package/dist/acp/capabilities.test.d.ts +2 -0
- package/dist/acp/capabilities.test.d.ts.map +1 -0
- package/dist/acp/capabilities.test.js +156 -0
- package/dist/acp/capabilities.test.js.map +1 -0
- package/dist/acp/elicitation.integration.test.d.ts +2 -0
- package/dist/acp/elicitation.integration.test.d.ts.map +1 -0
- package/dist/acp/elicitation.integration.test.js +98 -0
- package/dist/acp/elicitation.integration.test.js.map +1 -0
- package/dist/acp/prober.d.ts +4 -0
- package/dist/acp/prober.d.ts.map +1 -1
- package/dist/acp/prober.js +1 -0
- package/dist/acp/prober.js.map +1 -1
- package/dist/acp/spawner.d.ts.map +1 -1
- package/dist/acp/spawner.js +3 -0
- package/dist/acp/spawner.js.map +1 -1
- package/dist/acp/sync-bridge.d.ts.map +1 -1
- package/dist/acp/sync-bridge.js +5 -1
- package/dist/acp/sync-bridge.js.map +1 -1
- package/dist/cloud-host/supervisor.d.ts +13 -0
- package/dist/cloud-host/supervisor.d.ts.map +1 -1
- package/dist/cloud-host/supervisor.js +21 -0
- package/dist/cloud-host/supervisor.js.map +1 -1
- package/dist/cloud-host/supervisor.test.js +71 -0
- package/dist/cloud-host/supervisor.test.js.map +1 -1
- package/dist/github/credential-manager.d.ts +70 -0
- package/dist/github/credential-manager.d.ts.map +1 -0
- package/dist/github/credential-manager.js +205 -0
- package/dist/github/credential-manager.js.map +1 -0
- package/dist/github/credential-manager.test.d.ts +2 -0
- package/dist/github/credential-manager.test.d.ts.map +1 -0
- package/dist/github/credential-manager.test.js +215 -0
- package/dist/github/credential-manager.test.js.map +1 -0
- package/dist/github/gh-config.d.ts +17 -0
- package/dist/github/gh-config.d.ts.map +1 -0
- package/dist/github/gh-config.js +68 -0
- package/dist/github/gh-config.js.map +1 -0
- package/dist/github/gh-config.test.d.ts +2 -0
- package/dist/github/gh-config.test.d.ts.map +1 -0
- package/dist/github/gh-config.test.js +90 -0
- package/dist/github/gh-config.test.js.map +1 -0
- package/dist/harness/env.d.ts +8 -0
- package/dist/harness/env.d.ts.map +1 -1
- package/dist/harness/env.js +30 -0
- package/dist/harness/env.js.map +1 -1
- package/dist/harness/env.test.js +40 -1
- package/dist/harness/env.test.js.map +1 -1
- package/dist/harness/opencode-config.d.ts +6 -3
- package/dist/harness/opencode-config.d.ts.map +1 -1
- package/dist/harness/opencode-config.js +6 -2
- package/dist/harness/opencode-config.js.map +1 -1
- package/dist/harness/opencode-config.test.js +12 -0
- package/dist/harness/opencode-config.test.js.map +1 -1
- package/dist/harness/prepare.test.js +19 -0
- package/dist/harness/prepare.test.js.map +1 -1
- package/dist/socketio-client.d.ts +12 -0
- package/dist/socketio-client.d.ts.map +1 -1
- package/dist/socketio-client.github.test.d.ts +2 -0
- package/dist/socketio-client.github.test.d.ts.map +1 -0
- package/dist/socketio-client.github.test.js +66 -0
- package/dist/socketio-client.github.test.js.map +1 -0
- package/dist/socketio-client.js +115 -65
- package/dist/socketio-client.js.map +1 -1
- package/dist/socketio-client.test.js +251 -1
- package/dist/socketio-client.test.js.map +1 -1
- package/dist/workload-executor.d.ts +14 -1
- package/dist/workload-executor.d.ts.map +1 -1
- package/dist/workload-executor.js +33 -3
- package/dist/workload-executor.js.map +1 -1
- package/dist/workload-executor.test.js +106 -2
- package/dist/workload-executor.test.js.map +1 -1
- package/package.json +1 -1
- package/src/acp/__fixtures__/fake-elicitation-agent.mjs +113 -0
- package/src/acp/__fixtures__/opencode-2.0.31-capabilities.json +25 -0
- package/src/acp/bridge.test.ts +480 -1
- package/src/acp/bridge.ts +163 -18
- package/src/acp/capabilities.test.ts +200 -0
- package/src/acp/capabilities.ts +274 -0
- package/src/acp/elicitation.integration.test.ts +126 -0
- package/src/acp/prober.ts +4 -0
- package/src/acp/spawner.ts +3 -0
- package/src/acp/sync-bridge.ts +5 -1
- package/src/cloud-host/supervisor.test.ts +96 -1
- package/src/cloud-host/supervisor.ts +37 -0
- package/src/github/credential-manager.test.ts +268 -0
- package/src/github/credential-manager.ts +251 -0
- package/src/github/gh-config.test.ts +103 -0
- package/src/github/gh-config.ts +76 -0
- package/src/harness/env.test.ts +44 -1
- package/src/harness/env.ts +32 -0
- package/src/harness/opencode-config.test.ts +13 -0
- package/src/harness/opencode-config.ts +12 -6
- package/src/harness/prepare.test.ts +22 -0
- package/src/socketio-client.github.test.ts +76 -0
- package/src/socketio-client.test.ts +306 -1
- package/src/socketio-client.ts +113 -53
- package/src/workload-executor.test.ts +144 -2
- package/src/workload-executor.ts +48 -2
- package/tests/acp-sync-bridge.test.ts +5 -1
- package/tests/socketio-client.test.ts +2 -2
- package/vitest.config.ts +5 -1
package/src/acp/bridge.test.ts
CHANGED
|
@@ -22,9 +22,14 @@ const { mockNewSession, mockResumeSession, mockInitialize, mockClientSideConnect
|
|
|
22
22
|
vi.mock('@agentclientprotocol/sdk', () => ({
|
|
23
23
|
ndJsonStream: vi.fn(() => ({})),
|
|
24
24
|
ClientSideConnection: mockClientSideConnection,
|
|
25
|
+
CreateElicitationRequest: {
|
|
26
|
+
isForm: vi.fn((params: { mode?: string }) => params?.mode === 'form'),
|
|
27
|
+
isUrl: vi.fn((params: { mode?: string }) => params?.mode === 'url'),
|
|
28
|
+
},
|
|
25
29
|
}));
|
|
26
30
|
|
|
27
31
|
import { AcpSessionBridge } from './bridge.js';
|
|
32
|
+
import { CLIENT_CAPABILITIES, FEATURE_IDS } from './capabilities.js';
|
|
28
33
|
|
|
29
34
|
class MockAcpSpawner {
|
|
30
35
|
start = vi.fn();
|
|
@@ -32,9 +37,13 @@ class MockAcpSpawner {
|
|
|
32
37
|
isRunning = vi.fn().mockReturnValue(true);
|
|
33
38
|
_stdin = new WritableStream();
|
|
34
39
|
_stdout = new ReadableStream();
|
|
40
|
+
/** 可控进程退出:默认不退出,测试显式 triggerExit() */
|
|
41
|
+
private resolveClosed!: () => void;
|
|
42
|
+
private closedPromise = new Promise<void>((resolve) => { this.resolveClosed = resolve; });
|
|
43
|
+
triggerExit(): void { this.resolveClosed(); }
|
|
35
44
|
get stdin() { return this._stdin; }
|
|
36
45
|
get stdout() { return this._stdout; }
|
|
37
|
-
get closed() { return
|
|
46
|
+
get closed() { return this.closedPromise; }
|
|
38
47
|
}
|
|
39
48
|
|
|
40
49
|
function createMockSocket() {
|
|
@@ -504,3 +513,473 @@ describe('AcpSessionBridge - initial mode/model', () => {
|
|
|
504
513
|
);
|
|
505
514
|
});
|
|
506
515
|
});
|
|
516
|
+
|
|
517
|
+
describe('AcpSessionBridge - capability snapshot', () => {
|
|
518
|
+
let mockSocket: ReturnType<typeof createMockSocket>;
|
|
519
|
+
let mockSpawner: MockAcpSpawner;
|
|
520
|
+
|
|
521
|
+
function createBridge() {
|
|
522
|
+
return new AcpSessionBridge(
|
|
523
|
+
mockSocket as never,
|
|
524
|
+
{ sessionId: 'sess-1', cwd: '/home/user' },
|
|
525
|
+
mockSpawner as never,
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
beforeEach(() => {
|
|
530
|
+
vi.clearAllMocks();
|
|
531
|
+
vi.useFakeTimers();
|
|
532
|
+
mockSocket = createMockSocket();
|
|
533
|
+
mockSpawner = new MockAcpSpawner();
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
afterEach(() => {
|
|
537
|
+
vi.useRealTimers();
|
|
538
|
+
vi.restoreAllMocks();
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
it('T-B1: start() 后 acp:initialized 含 capabilities', async () => {
|
|
542
|
+
const bridge = createBridge();
|
|
543
|
+
const initialized = vi.fn();
|
|
544
|
+
bridge.on('acp:initialized', initialized);
|
|
545
|
+
|
|
546
|
+
await bridge.start();
|
|
547
|
+
|
|
548
|
+
const payload = initialized.mock.calls[0][0] as { capabilities?: { features?: unknown[]; raw?: { clientCapabilities?: { elicitation?: unknown } } } };
|
|
549
|
+
expect(payload.capabilities).toBeDefined();
|
|
550
|
+
expect(payload.capabilities?.features).toHaveLength(FEATURE_IDS.length);
|
|
551
|
+
// 不引用被测常量自身(否则常量改成 {} 仍恒绿):显式断言 elicitation 能力实际随 initialize 上送
|
|
552
|
+
expect(payload.capabilities?.raw?.clientCapabilities).toEqual({
|
|
553
|
+
session: { configOptions: { boolean: {} } },
|
|
554
|
+
elicitation: { form: {}, url: {} },
|
|
555
|
+
});
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
it('T-B2: resume() 后同样携带 capabilities', async () => {
|
|
559
|
+
const bridge = createBridge();
|
|
560
|
+
const initialized = vi.fn();
|
|
561
|
+
bridge.on('acp:initialized', initialized);
|
|
562
|
+
|
|
563
|
+
await bridge.resume('ext-123');
|
|
564
|
+
|
|
565
|
+
const payload = initialized.mock.calls[0][0] as { capabilities?: { features?: unknown[] } };
|
|
566
|
+
expect(payload.capabilities?.features).toHaveLength(FEATURE_IDS.length);
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
it('T-B6: raw.authMethods 保留 initialize 原文(穿过 start/buildInitializedPayload 调用点)', async () => {
|
|
570
|
+
// 原文含 name/link 等归一化会丢弃的字段,且 type 未重命名为 kind
|
|
571
|
+
const rawAuthMethods = [
|
|
572
|
+
{ type: 'env_var', id: 'login', name: 'Login', description: 'sign in', link: 'https://example.com/auth' },
|
|
573
|
+
{ id: 'legacy-agent' },
|
|
574
|
+
];
|
|
575
|
+
mockInitialize.mockResolvedValueOnce({ protocolVersion: 1, authMethods: rawAuthMethods });
|
|
576
|
+
|
|
577
|
+
const bridge = createBridge();
|
|
578
|
+
const initialized = vi.fn();
|
|
579
|
+
bridge.on('acp:initialized', initialized);
|
|
580
|
+
|
|
581
|
+
await bridge.start();
|
|
582
|
+
|
|
583
|
+
const payload = initialized.mock.calls[0][0] as {
|
|
584
|
+
authMethods: unknown;
|
|
585
|
+
capabilities: { raw: { authMethods: unknown } };
|
|
586
|
+
};
|
|
587
|
+
// 下发/展示用归一化产物
|
|
588
|
+
expect(payload.authMethods).toEqual([
|
|
589
|
+
{ id: 'login', kind: 'env_var', description: 'sign in', url: null },
|
|
590
|
+
{ id: 'legacy-agent', kind: 'agent', description: null, url: null },
|
|
591
|
+
]);
|
|
592
|
+
// 快照 raw 必须是原文,不能是转换后产物
|
|
593
|
+
expect(payload.capabilities.raw.authMethods).toEqual(rawAuthMethods);
|
|
594
|
+
expect(payload.capabilities.raw.authMethods).not.toEqual(payload.authMethods);
|
|
595
|
+
});
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
describe('AcpSessionBridge - elicitation observability', () => {
|
|
599
|
+
let mockSocket: ReturnType<typeof createMockSocket>;
|
|
600
|
+
let mockSpawner: MockAcpSpawner;
|
|
601
|
+
|
|
602
|
+
function createBridge() {
|
|
603
|
+
return new AcpSessionBridge(
|
|
604
|
+
mockSocket as never,
|
|
605
|
+
{ sessionId: 'sess-1', cwd: '/home/user' },
|
|
606
|
+
mockSpawner as never,
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
beforeEach(() => {
|
|
611
|
+
vi.clearAllMocks();
|
|
612
|
+
vi.useFakeTimers();
|
|
613
|
+
mockSocket = createMockSocket();
|
|
614
|
+
mockSpawner = new MockAcpSpawner();
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
afterEach(() => {
|
|
618
|
+
vi.useRealTimers();
|
|
619
|
+
vi.restoreAllMocks();
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it('T-B3: 未知 elicitation 模式返回 cancel 并发出 resolved(unsupported_mode)', async () => {
|
|
623
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
624
|
+
const bridge = createBridge();
|
|
625
|
+
|
|
626
|
+
const result = await bridge.unstable_createElicitation({ mode: 'custom' } as never);
|
|
627
|
+
|
|
628
|
+
expect(result).toEqual({ action: 'cancel' });
|
|
629
|
+
expect(mockSocket.emit).toHaveBeenCalledWith('acp:elicitation:resolved', expect.objectContaining({
|
|
630
|
+
requestId: expect.stringMatching(/^elic_\d+_/),
|
|
631
|
+
sessionId: 'sess-1',
|
|
632
|
+
reason: 'unsupported_mode',
|
|
633
|
+
detail: 'custom',
|
|
634
|
+
}));
|
|
635
|
+
expect(mockSocket.emit).not.toHaveBeenCalledWith('acp:elicitation', expect.anything());
|
|
636
|
+
expect(warnSpy).toHaveBeenCalledTimes(1);
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
it('T-B3b: unsupported_mode 的 detail 截断 Agent 可控字符串', async () => {
|
|
640
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
641
|
+
const bridge = createBridge();
|
|
642
|
+
|
|
643
|
+
await bridge.unstable_createElicitation({ mode: 'x'.repeat(500) } as never);
|
|
644
|
+
|
|
645
|
+
const resolved = mockSocket.emit.mock.calls.find(
|
|
646
|
+
(c: unknown[]) => c[0] === 'acp:elicitation:resolved',
|
|
647
|
+
);
|
|
648
|
+
expect((resolved![1] as { detail: string }).detail).toHaveLength(100);
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
it('T-B4: 65s 超时返回 cancel 并发出 resolved(timeout),不残留 pending', async () => {
|
|
652
|
+
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
653
|
+
const bridge = createBridge();
|
|
654
|
+
|
|
655
|
+
const promise = bridge.unstable_createElicitation({
|
|
656
|
+
mode: 'form',
|
|
657
|
+
message: 'Need input',
|
|
658
|
+
requestedSchema: { type: 'object', properties: {} },
|
|
659
|
+
} as never);
|
|
660
|
+
|
|
661
|
+
const emitCall = mockSocket.emit.mock.calls.find((c: unknown[]) => c[0] === 'acp:elicitation');
|
|
662
|
+
const requestId = (emitCall![1] as { requestId: string }).requestId;
|
|
663
|
+
|
|
664
|
+
await vi.advanceTimersByTimeAsync(65_000);
|
|
665
|
+
|
|
666
|
+
expect(await promise).toEqual({ action: 'cancel' });
|
|
667
|
+
expect(mockSocket.emit).toHaveBeenCalledWith('acp:elicitation:resolved', expect.objectContaining({
|
|
668
|
+
requestId,
|
|
669
|
+
reason: 'timeout',
|
|
670
|
+
detail: 'bridge timeout (65s)',
|
|
671
|
+
}));
|
|
672
|
+
expect(warnSpy).toHaveBeenCalled();
|
|
673
|
+
|
|
674
|
+
bridge.handleElicitationResponse(requestId, 'accept', { answer: 'x' });
|
|
675
|
+
expect(warnSpy).toHaveBeenCalledWith(`Elicitation request ${requestId} not found`);
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
it('T-B5: 正常 form 模式只发 acp:elicitation,响应后 65s 内不发 resolved(clearTimeout 生效)', async () => {
|
|
679
|
+
const bridge = createBridge();
|
|
680
|
+
|
|
681
|
+
const promise = bridge.unstable_createElicitation({
|
|
682
|
+
mode: 'form',
|
|
683
|
+
message: 'Need input',
|
|
684
|
+
requestedSchema: { type: 'object', properties: {} },
|
|
685
|
+
} as never);
|
|
686
|
+
|
|
687
|
+
const emitCall = mockSocket.emit.mock.calls.find((c: unknown[]) => c[0] === 'acp:elicitation');
|
|
688
|
+
const requestId = (emitCall![1] as { requestId: string }).requestId;
|
|
689
|
+
|
|
690
|
+
bridge.handleElicitationResponse(requestId, 'accept', { answer: 'x' });
|
|
691
|
+
|
|
692
|
+
expect(await promise).toEqual({ action: 'accept', content: { answer: 'x' } });
|
|
693
|
+
|
|
694
|
+
// 推进 65s:已响应的请求不得再发 timeout resolved
|
|
695
|
+
await vi.advanceTimersByTimeAsync(65_000);
|
|
696
|
+
expect(mockSocket.emit).not.toHaveBeenCalledWith('acp:elicitation:resolved', expect.anything());
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
it('T-B5b: url 模式 payload 携带 elicitationId/url 且不带 requestedSchema', async () => {
|
|
700
|
+
const bridge = createBridge();
|
|
701
|
+
|
|
702
|
+
const promise = bridge.unstable_createElicitation({
|
|
703
|
+
mode: 'url',
|
|
704
|
+
message: 'Authorize',
|
|
705
|
+
elicitationId: 'gh-oauth-1',
|
|
706
|
+
url: 'https://github.com/login/oauth/authorize?x=1',
|
|
707
|
+
} as never);
|
|
708
|
+
|
|
709
|
+
expect(mockSocket.emit).toHaveBeenCalledWith('acp:elicitation', {
|
|
710
|
+
requestId: expect.stringMatching(/^elic_\d+_/),
|
|
711
|
+
sessionId: 'sess-1',
|
|
712
|
+
mode: 'url',
|
|
713
|
+
message: 'Authorize',
|
|
714
|
+
elicitationId: 'gh-oauth-1',
|
|
715
|
+
url: 'https://github.com/login/oauth/authorize?x=1',
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
const emitCall = mockSocket.emit.mock.calls.find((c: unknown[]) => c[0] === 'acp:elicitation');
|
|
719
|
+
bridge.handleElicitationResponse((emitCall![1] as { requestId: string }).requestId, 'cancel');
|
|
720
|
+
expect(await promise).toEqual({ action: 'cancel' });
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
it('T-B5c: stop() 就地收敛 pending 问询为 cancelled,65s 后不再发 timeout', async () => {
|
|
724
|
+
const bridge = createBridge();
|
|
725
|
+
|
|
726
|
+
const promise = bridge.unstable_createElicitation({
|
|
727
|
+
mode: 'form',
|
|
728
|
+
message: 'Need input',
|
|
729
|
+
requestedSchema: { type: 'object', properties: {} },
|
|
730
|
+
} as never);
|
|
731
|
+
const requestId = (mockSocket.emit.mock.calls.find(
|
|
732
|
+
(c: unknown[]) => c[0] === 'acp:elicitation',
|
|
733
|
+
)![1] as { requestId: string }).requestId;
|
|
734
|
+
|
|
735
|
+
await bridge.stop();
|
|
736
|
+
|
|
737
|
+
expect(await promise).toEqual({ action: 'cancel' });
|
|
738
|
+
expect(mockSocket.emit).toHaveBeenCalledWith('acp:elicitation:resolved', expect.objectContaining({
|
|
739
|
+
requestId,
|
|
740
|
+
reason: 'cancelled',
|
|
741
|
+
}));
|
|
742
|
+
|
|
743
|
+
mockSocket.emit.mockClear();
|
|
744
|
+
await vi.advanceTimersByTimeAsync(65_000);
|
|
745
|
+
expect(mockSocket.emit).not.toHaveBeenCalledWith('acp:elicitation:resolved', expect.anything());
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it('T-B5d: stop() 同时收敛 pending 授权为 cancelled 并发出 settled', async () => {
|
|
749
|
+
const bridge = createBridge();
|
|
750
|
+
const settled = vi.fn();
|
|
751
|
+
bridge.on('permission:settled', settled);
|
|
752
|
+
|
|
753
|
+
const promise = bridge.requestPermission({
|
|
754
|
+
toolCall: { toolCallId: 'tc-1' },
|
|
755
|
+
options: [{ optionId: 'opt-1', name: 'Allow', kind: 'allow_once' }],
|
|
756
|
+
} as never);
|
|
757
|
+
|
|
758
|
+
await bridge.stop();
|
|
759
|
+
|
|
760
|
+
expect(await promise).toEqual({ outcome: { outcome: 'cancelled' } });
|
|
761
|
+
expect(settled).toHaveBeenCalledTimes(1);
|
|
762
|
+
await vi.advanceTimersByTimeAsync(65_000);
|
|
763
|
+
// timer 已清理:65s 后不会再有新的 permission 事件,且不会二次 settled
|
|
764
|
+
expect(mockSocket.emit.mock.calls.filter((c: unknown[]) => c[0] === 'acp:permission')).toHaveLength(1);
|
|
765
|
+
expect(settled).toHaveBeenCalledTimes(1);
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
it('R1: stop({silent:true}) 不发 acp:stopped(替换/对账旧 bridge 不误删新会话)', async () => {
|
|
769
|
+
const bridge = createBridge();
|
|
770
|
+
const stopped = vi.fn();
|
|
771
|
+
bridge.on('acp:stopped', stopped);
|
|
772
|
+
await bridge.start();
|
|
773
|
+
|
|
774
|
+
await bridge.stop({ silent: true });
|
|
775
|
+
|
|
776
|
+
expect(mockSocket.emit).not.toHaveBeenCalledWith('acp:stopped', expect.anything());
|
|
777
|
+
expect(stopped).not.toHaveBeenCalled();
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
it('R6a: 非 silent stop 在途被 silent stop 覆盖后,迟到 emitStopped 不发 stopped(服务端+本地)', async () => {
|
|
781
|
+
const bridge = createBridge();
|
|
782
|
+
const localStopped = vi.fn();
|
|
783
|
+
bridge.on('acp:stopped', localStopped);
|
|
784
|
+
await bridge.start();
|
|
785
|
+
const connection = (bridge as unknown as {
|
|
786
|
+
connection: { cancel: ReturnType<typeof vi.fn> };
|
|
787
|
+
}).connection;
|
|
788
|
+
connection.cancel.mockReturnValue(new Promise(() => {}));
|
|
789
|
+
mockSocket.emit.mockClear();
|
|
790
|
+
|
|
791
|
+
const first = bridge.stop();
|
|
792
|
+
const second = bridge.stop({ silent: true });
|
|
793
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
794
|
+
await Promise.all([first, second]);
|
|
795
|
+
|
|
796
|
+
expect(mockSocket.emit).not.toHaveBeenCalledWith('acp:stopped', expect.anything());
|
|
797
|
+
expect(localStopped).not.toHaveBeenCalled();
|
|
798
|
+
});
|
|
799
|
+
|
|
800
|
+
it('R6b: stop 5s race 在途(stopped 尚未置位)时 handleError 仍被 silentStop 抑制(服务端+本地)', async () => {
|
|
801
|
+
const bridge = createBridge();
|
|
802
|
+
const localStopped = vi.fn();
|
|
803
|
+
bridge.on('acp:stopped', localStopped);
|
|
804
|
+
await bridge.start();
|
|
805
|
+
const connection = (bridge as unknown as {
|
|
806
|
+
connection: { cancel: ReturnType<typeof vi.fn> };
|
|
807
|
+
}).connection;
|
|
808
|
+
connection.cancel.mockReturnValue(new Promise(() => {}));
|
|
809
|
+
mockSocket.emit.mockClear();
|
|
810
|
+
|
|
811
|
+
const first = bridge.stop();
|
|
812
|
+
const second = bridge.stop({ silent: true });
|
|
813
|
+
// 此时 stopped 仍为 false(emitStopped 尚未执行),只能靠 silentStop 早退
|
|
814
|
+
(bridge as unknown as { handleError: (error: unknown) => void })
|
|
815
|
+
.handleError(new Error('late init failure in stop race window'));
|
|
816
|
+
|
|
817
|
+
expect(mockSocket.emit).not.toHaveBeenCalledWith('acp:stopped', expect.anything());
|
|
818
|
+
expect(localStopped).not.toHaveBeenCalled();
|
|
819
|
+
|
|
820
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
821
|
+
await Promise.all([first, second]);
|
|
822
|
+
expect(localStopped).not.toHaveBeenCalled();
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
it('R5a: silent stop 后迟到的 handleError 不再向服务端/本地发 acp:stopped', async () => {
|
|
826
|
+
const bridge = createBridge();
|
|
827
|
+
const stopped = vi.fn();
|
|
828
|
+
bridge.on('acp:stopped', stopped);
|
|
829
|
+
await bridge.start();
|
|
830
|
+
await bridge.stop({ silent: true });
|
|
831
|
+
mockSocket.emit.mockClear();
|
|
832
|
+
|
|
833
|
+
(bridge as unknown as { handleError: (error: unknown) => void })
|
|
834
|
+
.handleError(new Error('late init failure after replacement'));
|
|
835
|
+
|
|
836
|
+
expect(mockSocket.emit).not.toHaveBeenCalledWith('acp:stopped', expect.anything());
|
|
837
|
+
expect(stopped).not.toHaveBeenCalled();
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
it('R3: stop() 在 cancel/closeSession 的 5s race 之前同步收敛 pending', async () => {
|
|
841
|
+
const bridge = createBridge();
|
|
842
|
+
await bridge.start();
|
|
843
|
+
|
|
844
|
+
const promise = bridge.unstable_createElicitation({
|
|
845
|
+
mode: 'form',
|
|
846
|
+
message: 'Need input',
|
|
847
|
+
requestedSchema: { type: 'object', properties: {} },
|
|
848
|
+
} as never);
|
|
849
|
+
|
|
850
|
+
// 模拟 cancel 永久挂起:若 settle 在 5s race 之后,下面的 await 会超时
|
|
851
|
+
const connection = (bridge as unknown as {
|
|
852
|
+
connection: { cancel: ReturnType<typeof vi.fn> };
|
|
853
|
+
}).connection;
|
|
854
|
+
connection.cancel.mockReturnValue(new Promise(() => {}));
|
|
855
|
+
|
|
856
|
+
const stopPromise = bridge.stop();
|
|
857
|
+
expect(await promise).toEqual({ action: 'cancel' });
|
|
858
|
+
expect(mockSocket.emit).toHaveBeenCalledWith('acp:elicitation:resolved', expect.objectContaining({
|
|
859
|
+
reason: 'cancelled',
|
|
860
|
+
}));
|
|
861
|
+
|
|
862
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
863
|
+
await stopPromise;
|
|
864
|
+
});
|
|
865
|
+
|
|
866
|
+
it('T-B11: 进程退出(spawner.closed)收敛 pending 并发 cancelled,65s 后不再发 timeout', async () => {
|
|
867
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
868
|
+
const bridge = createBridge();
|
|
869
|
+
await bridge.start();
|
|
870
|
+
|
|
871
|
+
const promise = bridge.unstable_createElicitation({
|
|
872
|
+
mode: 'form',
|
|
873
|
+
message: 'Need input',
|
|
874
|
+
requestedSchema: { type: 'object', properties: {} },
|
|
875
|
+
} as never);
|
|
876
|
+
const requestId = (mockSocket.emit.mock.calls.find(
|
|
877
|
+
(c: unknown[]) => c[0] === 'acp:elicitation',
|
|
878
|
+
)![1] as { requestId: string }).requestId;
|
|
879
|
+
|
|
880
|
+
mockSpawner.triggerExit();
|
|
881
|
+
await Promise.resolve();
|
|
882
|
+
await Promise.resolve();
|
|
883
|
+
|
|
884
|
+
expect(await promise).toEqual({ action: 'cancel' });
|
|
885
|
+
expect(mockSocket.emit).toHaveBeenCalledWith('acp:elicitation:resolved', expect.objectContaining({
|
|
886
|
+
requestId,
|
|
887
|
+
reason: 'cancelled',
|
|
888
|
+
detail: 'process exited',
|
|
889
|
+
}));
|
|
890
|
+
|
|
891
|
+
// 真实时钟推进 65s:timer 已清理,不得再发 timeout
|
|
892
|
+
const resolvedReasons = mockSocket.emit.mock.calls
|
|
893
|
+
.filter((c: unknown[]) => c[0] === 'acp:elicitation:resolved')
|
|
894
|
+
.map((c: unknown[]) => (c[1] as { reason: string }).reason);
|
|
895
|
+
await vi.advanceTimersByTimeAsync(65_000);
|
|
896
|
+
const afterReasons = mockSocket.emit.mock.calls
|
|
897
|
+
.filter((c: unknown[]) => c[0] === 'acp:elicitation:resolved')
|
|
898
|
+
.map((c: unknown[]) => (c[1] as { reason: string }).reason);
|
|
899
|
+
expect(afterReasons).toEqual(resolvedReasons);
|
|
900
|
+
expect(afterReasons).not.toContain('timeout');
|
|
901
|
+
});
|
|
902
|
+
|
|
903
|
+
it('T-B11b: 连接断开(settlePendingOnDisconnect)收敛 pending 且不发 timeout', async () => {
|
|
904
|
+
const bridge = createBridge();
|
|
905
|
+
await bridge.start();
|
|
906
|
+
|
|
907
|
+
const promise = bridge.unstable_createElicitation({
|
|
908
|
+
mode: 'form',
|
|
909
|
+
message: 'Need input',
|
|
910
|
+
requestedSchema: { type: 'object', properties: {} },
|
|
911
|
+
} as never);
|
|
912
|
+
|
|
913
|
+
bridge.settlePendingOnDisconnect();
|
|
914
|
+
|
|
915
|
+
expect(await promise).toEqual({ action: 'cancel' });
|
|
916
|
+
await vi.advanceTimersByTimeAsync(65_000);
|
|
917
|
+
const reasons = mockSocket.emit.mock.calls
|
|
918
|
+
.filter((c: unknown[]) => c[0] === 'acp:elicitation:resolved')
|
|
919
|
+
.map((c: unknown[]) => (c[1] as { reason: string }).reason);
|
|
920
|
+
expect(reasons).toEqual(['cancelled']);
|
|
921
|
+
});
|
|
922
|
+
|
|
923
|
+
it('T-B11c: 收敛时发出 settled 事件(供请求级 Map 清理),响应/超时同样触发', async () => {
|
|
924
|
+
const bridge = createBridge();
|
|
925
|
+
const elicitationSettled = vi.fn();
|
|
926
|
+
const permissionSettled = vi.fn();
|
|
927
|
+
bridge.on('elicitation:settled', elicitationSettled);
|
|
928
|
+
bridge.on('permission:settled', permissionSettled);
|
|
929
|
+
|
|
930
|
+
const elicitationPromise = bridge.unstable_createElicitation({
|
|
931
|
+
mode: 'form',
|
|
932
|
+
message: 'Need input',
|
|
933
|
+
requestedSchema: { type: 'object', properties: {} },
|
|
934
|
+
} as never);
|
|
935
|
+
const elicitationId = (mockSocket.emit.mock.calls.find(
|
|
936
|
+
(c: unknown[]) => c[0] === 'acp:elicitation',
|
|
937
|
+
)![1] as { requestId: string }).requestId;
|
|
938
|
+
bridge.handleElicitationResponse(elicitationId, 'decline');
|
|
939
|
+
await elicitationPromise;
|
|
940
|
+
expect(elicitationSettled).toHaveBeenCalledWith(elicitationId);
|
|
941
|
+
|
|
942
|
+
const permissionPromise = bridge.requestPermission({
|
|
943
|
+
toolCall: { toolCallId: 'tc-1' },
|
|
944
|
+
options: [{ optionId: 'opt-1', name: 'Allow', kind: 'allow_once' }],
|
|
945
|
+
} as never);
|
|
946
|
+
const permissionId = (mockSocket.emit.mock.calls.find(
|
|
947
|
+
(c: unknown[]) => c[0] === 'acp:permission',
|
|
948
|
+
)![1] as { requestId: string }).requestId;
|
|
949
|
+
await vi.advanceTimersByTimeAsync(65_000);
|
|
950
|
+
await permissionPromise;
|
|
951
|
+
expect(permissionSettled).toHaveBeenCalledWith(permissionId);
|
|
952
|
+
});
|
|
953
|
+
|
|
954
|
+
it('T-B5e: Agent 自发 complete 时收敛 pending 并发 resolved(cancelled)', async () => {
|
|
955
|
+
const bridge = createBridge();
|
|
956
|
+
|
|
957
|
+
const promise = bridge.unstable_createElicitation({
|
|
958
|
+
mode: 'url',
|
|
959
|
+
message: 'Authorize',
|
|
960
|
+
elicitationId: 'gh-oauth-2',
|
|
961
|
+
url: 'https://example.com/auth',
|
|
962
|
+
} as never);
|
|
963
|
+
|
|
964
|
+
const requestId = (mockSocket.emit.mock.calls.find(
|
|
965
|
+
(c: unknown[]) => c[0] === 'acp:elicitation',
|
|
966
|
+
)![1] as { requestId: string }).requestId;
|
|
967
|
+
|
|
968
|
+
await bridge.unstable_completeElicitation({ elicitationId: 'gh-oauth-2' } as never);
|
|
969
|
+
|
|
970
|
+
expect(await promise).toEqual({ action: 'cancel' });
|
|
971
|
+
expect(mockSocket.emit).toHaveBeenCalledWith('acp:elicitation:complete', {
|
|
972
|
+
sessionId: 'sess-1',
|
|
973
|
+
elicitationId: 'gh-oauth-2',
|
|
974
|
+
});
|
|
975
|
+
expect(mockSocket.emit).toHaveBeenCalledWith('acp:elicitation:resolved', expect.objectContaining({
|
|
976
|
+
requestId,
|
|
977
|
+
reason: 'cancelled',
|
|
978
|
+
detail: 'agent completed elicitation',
|
|
979
|
+
}));
|
|
980
|
+
|
|
981
|
+
mockSocket.emit.mockClear();
|
|
982
|
+
await vi.advanceTimersByTimeAsync(65_000);
|
|
983
|
+
expect(mockSocket.emit).not.toHaveBeenCalledWith('acp:elicitation:resolved', expect.anything());
|
|
984
|
+
});
|
|
985
|
+
});
|