diff --git a/cli/src/agy/utils/agyAcpBackend.test.ts b/cli/src/agy/utils/agyAcpBackend.test.ts new file mode 100644 index 00000000..c7a7c1d7 --- /dev/null +++ b/cli/src/agy/utils/agyAcpBackend.test.ts @@ -0,0 +1,68 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { AgentMessage } from '@/agent/types' + +const constructorCalls: Array> = [] + +vi.mock('@/agent/backends/acp', async () => { + const actual = await vi.importActual('@/agent/backends/acp') + return { + ...actual, + AcpSdkBackend: vi.fn().mockImplementation(function ( + this: unknown, + opts: Record + ) { + constructorCalls.push(opts) + return { __opts: opts } + }) + } +}) + +import { AcpMessageHandler, type AcpTextChunkMode } from '@/agent/backends/acp' +import { createAgyAcpBackend } from './agyAcpBackend' + +// agy streams agent_message_chunk as pure deltas (verified against agy 1.5.3), +// so the repeated digit at the chunk boundary must not be read as an overlap. +function assemble(chunks: string[], textChunkMode: AcpTextChunkMode | undefined): string { + const messages: AgentMessage[] = [] + const handler = new AcpMessageHandler((message) => messages.push(message), { textChunkMode }) + for (const text of chunks) { + handler.handleUpdate({ + sessionUpdate: 'agent_message_chunk', + content: { type: 'text', text } + }) + } + handler.flushText() + return messages.map((message) => (message.type === 'text' ? message.text : '')).join('') +} + +function configuredMode(): AcpTextChunkMode | undefined { + constructorCalls.length = 0 + createAgyAcpBackend() + expect(constructorCalls).toHaveLength(1) + return constructorCalls[0]?.textChunkMode as AcpTextChunkMode | undefined +} + +describe('createAgyAcpBackend text chunk assembly', () => { + beforeEach(() => { + constructorCalls.length = 0 + }) + + it('keeps the repeated 8 when a number is split across chunks', () => { + expect(assemble(['Call 73-8', '800 now'], configuredMode())).toBe('Call 73-8800 now') + }) + + it('keeps repeated characters at chunk boundaries (agy 1.5.3 capture)', () => { + expect(assemble( + ['73-8800 880', '0 5566 qq rr 3322 ee'], + configuredMode() + )).toBe('73-8800 8800 5566 qq rr 3322 ee') + expect(assemble( + ['8800 9999 33322', '2 ababab', ' ab', 'ab'], + configuredMode() + )).toBe('8800 9999 333222 ababab abab') + }) + + it('sets the delta text chunk mode explicitly', () => { + expect(configuredMode()).toBe('delta') + }) +}) diff --git a/cli/src/agy/utils/agyAcpBackend.ts b/cli/src/agy/utils/agyAcpBackend.ts index 56426fe9..986a522d 100644 --- a/cli/src/agy/utils/agyAcpBackend.ts +++ b/cli/src/agy/utils/agyAcpBackend.ts @@ -24,6 +24,9 @@ export function createAgyAcpBackend(): AcpSdkBackend { args: ['acp'], env: buildAgyAcpEnv(), flavor: 'agy', + // agy streams agent_message_chunk as pure deltas; overlap-dedupe would eat + // a repeated character at a chunk boundary (73-8 + 800 -> 73-800). + textChunkMode: 'delta', // agy then asks its ask_question questions as form elicitations. elicitationForm: true });