|
| 1 | +import { describe, expect, test, spyOn } from "bun:test" |
| 2 | +import type { Agent } from "../../src/agent/agent" |
| 3 | +import { Agent as AgentApi } from "../../src/agent/agent" |
| 4 | +import { Instance } from "../../src/project/instance" |
| 5 | +import type { Provider } from "../../src/provider/provider" |
| 6 | +import { ModelID, ProviderID } from "../../src/provider/schema" |
| 7 | +import { Session } from "../../src/session" |
| 8 | +import { MessageV2 } from "../../src/session/message-v2" |
| 9 | +import * as LLMModule from "../../src/session/llm" |
| 10 | +import { SessionProcessor } from "../../src/session/processor" |
| 11 | +import * as RetryModule from "../../src/session/retry" |
| 12 | +import { MessageID } from "../../src/session/schema" |
| 13 | +import { Log } from "../../src/util/log" |
| 14 | +import { tmpdir } from "../fixture/fixture" |
| 15 | + |
| 16 | +Log.init({ print: false }) |
| 17 | + |
| 18 | +type Stream = Awaited<ReturnType<typeof LLMModule.LLM.stream>> |
| 19 | + |
| 20 | +function model() { |
| 21 | + return { |
| 22 | + id: ModelID.make("test-model"), |
| 23 | + providerID: ProviderID.make("test"), |
| 24 | + } as unknown as Provider.Model |
| 25 | +} |
| 26 | + |
| 27 | +function user(sessionID: Session.Info["id"]) { |
| 28 | + return { |
| 29 | + id: MessageID.ascending(), |
| 30 | + sessionID, |
| 31 | + role: "user" as const, |
| 32 | + time: { created: Date.now() }, |
| 33 | + agent: "build", |
| 34 | + model: { |
| 35 | + providerID: ProviderID.make("test"), |
| 36 | + modelID: ModelID.make("test-model"), |
| 37 | + }, |
| 38 | + tools: {}, |
| 39 | + } as MessageV2.User |
| 40 | +} |
| 41 | + |
| 42 | +function assistant(sessionID: Session.Info["id"], parentID: MessageV2.User["id"], dir: string) { |
| 43 | + return { |
| 44 | + id: MessageID.ascending(), |
| 45 | + sessionID, |
| 46 | + parentID, |
| 47 | + role: "assistant" as const, |
| 48 | + mode: "build", |
| 49 | + agent: "build", |
| 50 | + path: { |
| 51 | + cwd: dir, |
| 52 | + root: dir, |
| 53 | + }, |
| 54 | + cost: 0, |
| 55 | + tokens: { |
| 56 | + input: 0, |
| 57 | + output: 0, |
| 58 | + reasoning: 0, |
| 59 | + cache: { read: 0, write: 0 }, |
| 60 | + }, |
| 61 | + modelID: ModelID.make("test-model"), |
| 62 | + providerID: ProviderID.make("test"), |
| 63 | + time: { |
| 64 | + created: Date.now(), |
| 65 | + }, |
| 66 | + } as MessageV2.Assistant |
| 67 | +} |
| 68 | + |
| 69 | +function output(values: unknown[]) { |
| 70 | + return { |
| 71 | + fullStream: (async function* () { |
| 72 | + for (const value of values) yield value |
| 73 | + })(), |
| 74 | + } as unknown as Stream |
| 75 | +} |
| 76 | + |
| 77 | +async function run( |
| 78 | + fn: (input: { |
| 79 | + agent: Agent.Info |
| 80 | + msg: MessageV2.Assistant |
| 81 | + session: Session.Info |
| 82 | + usr: MessageV2.User |
| 83 | + }) => Promise<void>, |
| 84 | +) { |
| 85 | + await using tmp = await tmpdir({ git: true }) |
| 86 | + await Instance.provide({ |
| 87 | + directory: tmp.path, |
| 88 | + fn: async () => { |
| 89 | + const session = await Session.create({}) |
| 90 | + const agent = await AgentApi.get("build") |
| 91 | + const usr = (await Session.updateMessage(user(session.id))) as MessageV2.User |
| 92 | + const msg = (await Session.updateMessage(assistant(session.id, usr.id, tmp.path))) as MessageV2.Assistant |
| 93 | + await fn({ agent, msg, session, usr }) |
| 94 | + await Session.remove(session.id) |
| 95 | + }, |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +describe("session.processor", () => { |
| 100 | + test("retries unexpected aborts when the session is still active", async () => { |
| 101 | + await run(async ({ agent, msg, session, usr }) => { |
| 102 | + const ctl = new AbortController() |
| 103 | + const stream = spyOn(LLMModule.LLM, "stream") |
| 104 | + const sleep = spyOn(RetryModule.SessionRetry, "sleep").mockResolvedValue() |
| 105 | + let calls = 0 |
| 106 | + |
| 107 | + try { |
| 108 | + stream.mockImplementation(async (): Promise<Stream> => { |
| 109 | + calls++ |
| 110 | + if (calls === 1) throw new DOMException("The operation was aborted.", "AbortError") |
| 111 | + return output([{ type: "start" }]) |
| 112 | + }) |
| 113 | + |
| 114 | + const result = await SessionProcessor.create({ |
| 115 | + assistantMessage: msg, |
| 116 | + sessionID: session.id, |
| 117 | + model: model(), |
| 118 | + abort: ctl.signal, |
| 119 | + }).process({ |
| 120 | + user: usr, |
| 121 | + agent, |
| 122 | + abort: ctl.signal, |
| 123 | + sessionID: session.id, |
| 124 | + system: [], |
| 125 | + messages: [], |
| 126 | + tools: {}, |
| 127 | + model: model(), |
| 128 | + }) |
| 129 | + |
| 130 | + const stored = await MessageV2.get({ sessionID: session.id, messageID: msg.id }) |
| 131 | + if (stored.info.role !== "assistant") throw new Error("expected assistant") |
| 132 | + expect(result).toBe("continue") |
| 133 | + expect(stream).toHaveBeenCalledTimes(2) |
| 134 | + expect(sleep).toHaveBeenCalledTimes(1) |
| 135 | + expect(stored.info.error).toBeUndefined() |
| 136 | + expect(stored.info.time.completed).toBeDefined() |
| 137 | + } finally { |
| 138 | + stream.mockRestore() |
| 139 | + sleep.mockRestore() |
| 140 | + } |
| 141 | + }) |
| 142 | + }) |
| 143 | + |
| 144 | + test("stops on aborts after session cancellation", async () => { |
| 145 | + await run(async ({ agent, msg, session, usr }) => { |
| 146 | + const ctl = new AbortController() |
| 147 | + ctl.abort() |
| 148 | + const stream = spyOn(LLMModule.LLM, "stream") |
| 149 | + const sleep = spyOn(RetryModule.SessionRetry, "sleep").mockResolvedValue() |
| 150 | + |
| 151 | + try { |
| 152 | + stream.mockImplementation(async (): Promise<Stream> => { |
| 153 | + throw new DOMException("The operation was aborted.", "AbortError") |
| 154 | + }) |
| 155 | + |
| 156 | + const result = await SessionProcessor.create({ |
| 157 | + assistantMessage: msg, |
| 158 | + sessionID: session.id, |
| 159 | + model: model(), |
| 160 | + abort: ctl.signal, |
| 161 | + }).process({ |
| 162 | + user: usr, |
| 163 | + agent, |
| 164 | + abort: ctl.signal, |
| 165 | + sessionID: session.id, |
| 166 | + system: [], |
| 167 | + messages: [], |
| 168 | + tools: {}, |
| 169 | + model: model(), |
| 170 | + }) |
| 171 | + |
| 172 | + const stored = await MessageV2.get({ sessionID: session.id, messageID: msg.id }) |
| 173 | + if (stored.info.role !== "assistant") throw new Error("expected assistant") |
| 174 | + expect(result).toBe("stop") |
| 175 | + expect(stream).toHaveBeenCalledTimes(1) |
| 176 | + expect(sleep).not.toHaveBeenCalled() |
| 177 | + expect(stored.info.error?.name).toBe("MessageAbortedError") |
| 178 | + } finally { |
| 179 | + stream.mockRestore() |
| 180 | + sleep.mockRestore() |
| 181 | + } |
| 182 | + }) |
| 183 | + }) |
| 184 | +}) |
0 commit comments