|
| 1 | +import { createInMemorySessionStore } from "@openclaw/acp-core/session"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import type { GatewayClient } from "../gateway/client.js"; |
| 4 | +import { createInMemoryAcpEventLedger } from "./event-ledger.js"; |
| 5 | +import { AcpGatewayAgent } from "./translator.js"; |
| 6 | +import { createChatEvent, promptAgent } from "./translator.prompt-harness.test-support.js"; |
| 7 | +import type { AcpAgentWaitResult } from "./translator.prompt-state.js"; |
| 8 | +import { createAcpConnection, createAcpGateway } from "./translator.test-helpers.js"; |
| 9 | + |
| 10 | +async function createReconnectHarness(result: AcpAgentWaitResult) { |
| 11 | + const sessionId = "session-1"; |
| 12 | + const sessionKey = "agent:main:main"; |
| 13 | + const sessionStore = createInMemorySessionStore(); |
| 14 | + sessionStore.createSession({ sessionId, sessionKey, cwd: "/tmp" }); |
| 15 | + const eventLedger = createInMemoryAcpEventLedger(); |
| 16 | + await eventLedger.startSession({ sessionId, sessionKey, cwd: "/tmp", complete: true }); |
| 17 | + const connection = createAcpConnection(); |
| 18 | + let runId: string | undefined; |
| 19 | + const request = vi.fn(async (method: string, params?: Record<string, unknown>) => { |
| 20 | + if (method === "chat.send") { |
| 21 | + runId = typeof params?.idempotencyKey === "string" ? params.idempotencyKey : undefined; |
| 22 | + } |
| 23 | + return method === "agent.wait" ? result : {}; |
| 24 | + }) as GatewayClient["request"]; |
| 25 | + const agent = new AcpGatewayAgent(connection, createAcpGateway(request), { |
| 26 | + eventLedger, |
| 27 | + sessionStore, |
| 28 | + }); |
| 29 | + const promptPromise = promptAgent(agent, sessionId); |
| 30 | + promptPromise.catch(() => {}); |
| 31 | + await vi.waitFor(() => expect(runId).toBeTypeOf("string")); |
| 32 | + |
| 33 | + return { |
| 34 | + agent, |
| 35 | + connection, |
| 36 | + eventLedger, |
| 37 | + promptPromise, |
| 38 | + runId: runId as string, |
| 39 | + sessionId, |
| 40 | + sessionKey, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +async function streamText( |
| 45 | + harness: Awaited<ReturnType<typeof createReconnectHarness>>, |
| 46 | + text: string, |
| 47 | +) { |
| 48 | + await harness.agent.handleGatewayEvent( |
| 49 | + createChatEvent({ |
| 50 | + runId: harness.runId, |
| 51 | + sessionKey: harness.sessionKey, |
| 52 | + seq: 1, |
| 53 | + state: "delta", |
| 54 | + message: { content: [{ type: "text", text }] }, |
| 55 | + }), |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +function reconnect(harness: Awaited<ReturnType<typeof createReconnectHarness>>) { |
| 60 | + harness.agent.handleGatewayDisconnect("1006: connection lost"); |
| 61 | + harness.agent.handleGatewayReconnect(); |
| 62 | +} |
| 63 | + |
| 64 | +function messageChunks(harness: Awaited<ReturnType<typeof createReconnectHarness>>) { |
| 65 | + return harness.connection["__sessionUpdateMock"].mock.calls.flatMap(([notification]) => { |
| 66 | + const update = notification.update; |
| 67 | + return update.sessionUpdate === "agent_message_chunk" && update.content.type === "text" |
| 68 | + ? [update.content.text] |
| 69 | + : []; |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +describe("acp translator reconnect settlement", () => { |
| 74 | + it.each([ |
| 75 | + { |
| 76 | + name: "full reply", |
| 77 | + result: { |
| 78 | + status: "ok", |
| 79 | + terminalReply: { disposition: "visible", text: "final answer" }, |
| 80 | + } satisfies AcpAgentWaitResult, |
| 81 | + streamed: undefined, |
| 82 | + recovered: "final answer", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "sticky timeout suffix", |
| 86 | + result: { |
| 87 | + status: "timeout", |
| 88 | + terminalReply: { disposition: "visible", text: "final answer" }, |
| 89 | + } satisfies AcpAgentWaitResult, |
| 90 | + streamed: "final", |
| 91 | + recovered: " answer", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "trim-normalized suffix", |
| 95 | + result: { |
| 96 | + status: "ok", |
| 97 | + terminalReply: { disposition: "visible", text: "final answer" }, |
| 98 | + } satisfies AcpAgentWaitResult, |
| 99 | + streamed: " final", |
| 100 | + recovered: " answer", |
| 101 | + }, |
| 102 | + ])("recovers the $name before resolving", async ({ result, streamed, recovered }) => { |
| 103 | + const harness = await createReconnectHarness(result); |
| 104 | + if (streamed) { |
| 105 | + await streamText(harness, streamed); |
| 106 | + } |
| 107 | + |
| 108 | + reconnect(harness); |
| 109 | + |
| 110 | + await expect(harness.promptPromise).resolves.toEqual({ stopReason: "end_turn" }); |
| 111 | + expect(messageChunks(harness).filter((text) => text === recovered)).toHaveLength(1); |
| 112 | + const replay = await harness.eventLedger.readReplay({ |
| 113 | + sessionId: harness.sessionId, |
| 114 | + sessionKey: harness.sessionKey, |
| 115 | + }); |
| 116 | + expect( |
| 117 | + replay.events.some( |
| 118 | + (event) => |
| 119 | + event.update.sessionUpdate === "agent_message_chunk" && |
| 120 | + event.update.content.type === "text" && |
| 121 | + event.update.content.text === recovered, |
| 122 | + ), |
| 123 | + ).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it("recovers visible text before rejecting a failed run", async () => { |
| 127 | + const harness = await createReconnectHarness({ |
| 128 | + status: "error", |
| 129 | + error: "boom", |
| 130 | + terminalReply: { disposition: "visible", text: "final answer" }, |
| 131 | + }); |
| 132 | + |
| 133 | + reconnect(harness); |
| 134 | + |
| 135 | + await expect(harness.promptPromise).rejects.toThrow("boom"); |
| 136 | + expect(messageChunks(harness)).toEqual(["final answer", "[OpenClaw interruption] boom"]); |
| 137 | + }); |
| 138 | + |
| 139 | + it("claims recovery before a late final event can emit the suffix twice", async () => { |
| 140 | + const harness = await createReconnectHarness({ |
| 141 | + status: "ok", |
| 142 | + terminalReply: { disposition: "visible", text: "final answer" }, |
| 143 | + }); |
| 144 | + await streamText(harness, "final"); |
| 145 | + let releaseRecord!: () => void; |
| 146 | + const recordBlocked = new Promise<void>((resolve) => { |
| 147 | + releaseRecord = resolve; |
| 148 | + }); |
| 149 | + const recordUpdate = harness.eventLedger.recordUpdate.bind(harness.eventLedger); |
| 150 | + harness.eventLedger.recordUpdate = async (params) => { |
| 151 | + if ( |
| 152 | + params.update.sessionUpdate === "agent_message_chunk" && |
| 153 | + params.update.content.type === "text" && |
| 154 | + params.update.content.text === " answer" |
| 155 | + ) { |
| 156 | + await recordBlocked; |
| 157 | + } |
| 158 | + await recordUpdate(params); |
| 159 | + }; |
| 160 | + |
| 161 | + reconnect(harness); |
| 162 | + await vi.waitFor(() => expect(messageChunks(harness)).toContain(" answer")); |
| 163 | + const lateFinal = harness.agent.handleGatewayEvent( |
| 164 | + createChatEvent({ |
| 165 | + runId: harness.runId, |
| 166 | + sessionKey: harness.sessionKey, |
| 167 | + seq: 2, |
| 168 | + state: "final", |
| 169 | + message: { content: [{ type: "text", text: "final answer" }] }, |
| 170 | + }), |
| 171 | + ); |
| 172 | + releaseRecord(); |
| 173 | + await lateFinal; |
| 174 | + |
| 175 | + await expect(harness.promptPromise).resolves.toEqual({ stopReason: "end_turn" }); |
| 176 | + expect(messageChunks(harness).filter((text) => text === " answer")).toHaveLength(1); |
| 177 | + }); |
| 178 | + |
| 179 | + it("settles after recovered update delivery rejects", async () => { |
| 180 | + const harness = await createReconnectHarness({ |
| 181 | + status: "ok", |
| 182 | + terminalReply: { disposition: "visible", text: "final answer" }, |
| 183 | + }); |
| 184 | + harness.connection.sessionUpdate = vi.fn(async () => { |
| 185 | + throw new Error("client gone"); |
| 186 | + }) as typeof harness.connection.sessionUpdate; |
| 187 | + |
| 188 | + reconnect(harness); |
| 189 | + |
| 190 | + await expect(harness.promptPromise).resolves.toEqual({ stopReason: "end_turn" }); |
| 191 | + const replay = await harness.eventLedger.readReplay({ |
| 192 | + sessionId: harness.sessionId, |
| 193 | + sessionKey: harness.sessionKey, |
| 194 | + }); |
| 195 | + expect( |
| 196 | + replay.events.some( |
| 197 | + (event) => |
| 198 | + event.update.sessionUpdate === "agent_message_chunk" && |
| 199 | + event.update.content.type === "text" && |
| 200 | + event.update.content.text === "final answer", |
| 201 | + ), |
| 202 | + ).toBe(true); |
| 203 | + }); |
| 204 | +}); |
0 commit comments