|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + createMinimalRunAgentTurnParams, |
| 4 | + getRunAgentTurnWithFallback, |
| 5 | + setupAgentRunnerExecutionTestState, |
| 6 | +} from "./agent-runner-execution.test-support.js"; |
| 7 | +import { HEARTBEAT_EXTERNAL_RUN_FAILURE_TEXT } from "./agent-runner-failure-copy.js"; |
| 8 | +import { createReplyOperation, expireStaleReplyOperation } from "./reply-run-registry.js"; |
| 9 | + |
| 10 | +const state = setupAgentRunnerExecutionTestState(); |
| 11 | + |
| 12 | +function createStalledReplyOperation(sessionId: string) { |
| 13 | + const replyOperation = createReplyOperation({ |
| 14 | + sessionKey: `agent:main:${sessionId}`, |
| 15 | + sessionId, |
| 16 | + resetTriggered: false, |
| 17 | + }); |
| 18 | + replyOperation.setPhase("running"); |
| 19 | + return replyOperation; |
| 20 | +} |
| 21 | + |
| 22 | +describe("runAgentTurnWithFallback: stalled recovery", () => { |
| 23 | + it("surfaces a stalled reply operation after the embedded run returns no payload", async () => { |
| 24 | + const replyOperation = createStalledReplyOperation("stalled-empty-reply"); |
| 25 | + state.runEmbeddedAgentMock.mockImplementationOnce(async () => { |
| 26 | + expect(expireStaleReplyOperation(replyOperation, "no_activity")).toBe(true); |
| 27 | + expect(replyOperation.abortSignal.aborted).toBe(true); |
| 28 | + return { payloads: [], meta: {} }; |
| 29 | + }); |
| 30 | + let releaseToolTask: () => void = () => undefined; |
| 31 | + const pendingToolTask = new Promise<void>((resolve) => { |
| 32 | + releaseToolTask = resolve; |
| 33 | + }); |
| 34 | + |
| 35 | + const runAgentTurnWithFallback = await getRunAgentTurnWithFallback(); |
| 36 | + const pending = runAgentTurnWithFallback({ |
| 37 | + ...createMinimalRunAgentTurnParams({ replyOperation }), |
| 38 | + pendingToolTasks: new Set([pendingToolTask]), |
| 39 | + }); |
| 40 | + let settled = false; |
| 41 | + void pending.then(() => { |
| 42 | + settled = true; |
| 43 | + }); |
| 44 | + await new Promise<void>((resolve) => { |
| 45 | + setImmediate(resolve); |
| 46 | + }); |
| 47 | + expect(settled).toBe(false); |
| 48 | + releaseToolTask(); |
| 49 | + |
| 50 | + await expect(pending).resolves.toEqual({ |
| 51 | + kind: "final", |
| 52 | + payload: { |
| 53 | + text: "⚠️ This turn was interrupted because it stopped making progress. Please try again.", |
| 54 | + isError: true, |
| 55 | + }, |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("surfaces a stalled reply operation after its aborted embedded run rejects", async () => { |
| 60 | + const replyOperation = createStalledReplyOperation("stalled-rejected-reply"); |
| 61 | + state.runEmbeddedAgentMock.mockImplementationOnce(async () => { |
| 62 | + expect(expireStaleReplyOperation(replyOperation, "no_activity")).toBe(true); |
| 63 | + expect(replyOperation.abortSignal.aborted).toBe(true); |
| 64 | + throw new Error("embedded run aborted after stale recovery"); |
| 65 | + }); |
| 66 | + let releaseToolTask: () => void = () => undefined; |
| 67 | + const pendingToolTask = new Promise<void>((resolve) => { |
| 68 | + releaseToolTask = resolve; |
| 69 | + }); |
| 70 | + |
| 71 | + const runAgentTurnWithFallback = await getRunAgentTurnWithFallback(); |
| 72 | + const pending = runAgentTurnWithFallback({ |
| 73 | + ...createMinimalRunAgentTurnParams({ replyOperation }), |
| 74 | + pendingToolTasks: new Set([pendingToolTask]), |
| 75 | + }); |
| 76 | + let settled = false; |
| 77 | + void pending.then(() => { |
| 78 | + settled = true; |
| 79 | + }); |
| 80 | + await new Promise<void>((resolve) => { |
| 81 | + setImmediate(resolve); |
| 82 | + }); |
| 83 | + expect(settled).toBe(false); |
| 84 | + releaseToolTask(); |
| 85 | + |
| 86 | + await expect(pending).resolves.toEqual({ |
| 87 | + kind: "final", |
| 88 | + payload: { |
| 89 | + text: "⚠️ This turn was interrupted because it stopped making progress. Please try again.", |
| 90 | + isError: true, |
| 91 | + }, |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("uses heartbeat failure copy for a stalled heartbeat operation", async () => { |
| 96 | + const replyOperation = createStalledReplyOperation("stalled-heartbeat"); |
| 97 | + state.runEmbeddedAgentMock.mockImplementationOnce(async () => { |
| 98 | + expect(expireStaleReplyOperation(replyOperation, "no_activity")).toBe(true); |
| 99 | + return { payloads: [], meta: {} }; |
| 100 | + }); |
| 101 | + |
| 102 | + const runAgentTurnWithFallback = await getRunAgentTurnWithFallback(); |
| 103 | + const result = await runAgentTurnWithFallback({ |
| 104 | + ...createMinimalRunAgentTurnParams({ replyOperation }), |
| 105 | + isHeartbeat: true, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(result).toEqual({ |
| 109 | + kind: "final", |
| 110 | + payload: { |
| 111 | + text: HEARTBEAT_EXTERNAL_RUN_FAILURE_TEXT, |
| 112 | + isError: true, |
| 113 | + }, |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments