|
| 1 | +import { beforeEach, expect, test, vi } from "vitest"; |
| 2 | +import { |
| 3 | + appendTranscriptEvent, |
| 4 | + appendTranscriptMessage, |
| 5 | + upsertSessionEntryCore, |
| 6 | +} from "../config/sessions/session-accessor.js"; |
| 7 | +import { rpcReq } from "./test-helpers.js"; |
| 8 | +import { |
| 9 | + sessionStoreEntry, |
| 10 | + setupGatewaySessionsTestHarness, |
| 11 | +} from "./test/server-sessions.test-helpers.js"; |
| 12 | + |
| 13 | +type LoadTranscriptEvents = |
| 14 | + (typeof import("../config/sessions/session-accessor.sqlite-read.js"))["loadTranscriptEvents"]; |
| 15 | + |
| 16 | +const transcriptReads = vi.hoisted(() => ({ |
| 17 | + actual: undefined as LoadTranscriptEvents | undefined, |
| 18 | + load: vi.fn<LoadTranscriptEvents>(), |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("../config/sessions/session-accessor.sqlite-read.js", async (importOriginal) => { |
| 22 | + const actual = |
| 23 | + await importOriginal<typeof import("../config/sessions/session-accessor.sqlite-read.js")>(); |
| 24 | + transcriptReads.actual = actual.loadTranscriptEvents; |
| 25 | + transcriptReads.load.mockImplementation(actual.loadTranscriptEvents); |
| 26 | + return { ...actual, loadTranscriptEvents: transcriptReads.load }; |
| 27 | +}); |
| 28 | + |
| 29 | +const { createSessionStoreDir, openClient } = setupGatewaySessionsTestHarness(); |
| 30 | + |
| 31 | +function requireTranscriptReader(): LoadTranscriptEvents { |
| 32 | + if (!transcriptReads.actual) { |
| 33 | + throw new Error("transcript reader mock was not initialized"); |
| 34 | + } |
| 35 | + return transcriptReads.actual; |
| 36 | +} |
| 37 | + |
| 38 | +beforeEach(() => { |
| 39 | + transcriptReads.load.mockReset(); |
| 40 | + transcriptReads.load.mockImplementation(requireTranscriptReader()); |
| 41 | +}); |
| 42 | + |
| 43 | +async function seedCompactionSession(params: { |
| 44 | + sessionId: string; |
| 45 | + storePath: string; |
| 46 | + nativeHarness?: boolean; |
| 47 | + withTranscript?: boolean; |
| 48 | +}) { |
| 49 | + const scope = { |
| 50 | + agentId: "main", |
| 51 | + sessionId: params.sessionId, |
| 52 | + sessionKey: "agent:main:main", |
| 53 | + storePath: params.storePath, |
| 54 | + }; |
| 55 | + await upsertSessionEntryCore( |
| 56 | + scope, |
| 57 | + sessionStoreEntry( |
| 58 | + params.sessionId, |
| 59 | + params.nativeHarness |
| 60 | + ? { |
| 61 | + agentHarnessId: "codex", |
| 62 | + cliSessionBindings: { "codex-cli": { sessionId: "thread-1" } }, |
| 63 | + cliSessionIds: { "codex-cli": "thread-1" }, |
| 64 | + modelSelectionLocked: true, |
| 65 | + } |
| 66 | + : {}, |
| 67 | + ), |
| 68 | + ); |
| 69 | + if (params.withTranscript === false) { |
| 70 | + return scope; |
| 71 | + } |
| 72 | + await appendTranscriptEvent(scope, { |
| 73 | + type: "session", |
| 74 | + version: 3, |
| 75 | + id: params.sessionId, |
| 76 | + timestamp: "2026-08-18T12:00:00.000Z", |
| 77 | + cwd: "/tmp", |
| 78 | + }); |
| 79 | + await appendTranscriptMessage(scope, { |
| 80 | + message: { role: "user", content: "compact me", timestamp: 1 }, |
| 81 | + now: Date.parse("2026-08-18T12:00:01.000Z"), |
| 82 | + }); |
| 83 | + return scope; |
| 84 | +} |
| 85 | + |
| 86 | +const transcriptReadError = () => |
| 87 | + new Error("SQLITE_IOERR: failed to read session transcript storage"); |
| 88 | + |
| 89 | +test("sessions.compact reports initial transcript read failures as unavailable", async () => { |
| 90 | + const { storePath } = await createSessionStoreDir(); |
| 91 | + await seedCompactionSession({ sessionId: "sess-read-failure", storePath }); |
| 92 | + transcriptReads.load.mockRejectedValueOnce(transcriptReadError()); |
| 93 | + |
| 94 | + const { ws } = await openClient(); |
| 95 | + try { |
| 96 | + const response = await rpcReq(ws, "sessions.compact", { key: "main" }); |
| 97 | + |
| 98 | + expect(response.ok).toBe(false); |
| 99 | + expect(response.error).toMatchObject({ |
| 100 | + code: "UNAVAILABLE", |
| 101 | + message: expect.stringContaining("failed to read session transcript storage"), |
| 102 | + }); |
| 103 | + } finally { |
| 104 | + ws.close(); |
| 105 | + } |
| 106 | +}); |
| 107 | + |
| 108 | +test("sessions.compact reports model compaction transcript re-read failures as unavailable", async () => { |
| 109 | + const { storePath } = await createSessionStoreDir(); |
| 110 | + const scope = await seedCompactionSession({ |
| 111 | + sessionId: "sess-model-read-failure", |
| 112 | + storePath, |
| 113 | + nativeHarness: true, |
| 114 | + }); |
| 115 | + const events = await requireTranscriptReader()(scope); |
| 116 | + transcriptReads.load.mockResolvedValueOnce(events).mockRejectedValueOnce(transcriptReadError()); |
| 117 | + |
| 118 | + const { ws } = await openClient(); |
| 119 | + try { |
| 120 | + const response = await rpcReq(ws, "sessions.compact", { key: "main" }); |
| 121 | + |
| 122 | + expect(response.ok).toBe(false); |
| 123 | + expect(response.error).toMatchObject({ |
| 124 | + code: "UNAVAILABLE", |
| 125 | + message: expect.stringContaining("failed to read session transcript storage"), |
| 126 | + }); |
| 127 | + } finally { |
| 128 | + ws.close(); |
| 129 | + } |
| 130 | +}); |
| 131 | + |
| 132 | +test("sessions.compact maxLines reports transcript preflight read failures as unavailable", async () => { |
| 133 | + const { storePath } = await createSessionStoreDir(); |
| 134 | + await seedCompactionSession({ sessionId: "sess-max-lines-read-failure", storePath }); |
| 135 | + transcriptReads.load.mockRejectedValueOnce(transcriptReadError()); |
| 136 | + |
| 137 | + const { ws } = await openClient(); |
| 138 | + try { |
| 139 | + const response = await rpcReq(ws, "sessions.compact", { key: "main", maxLines: 50 }); |
| 140 | + |
| 141 | + expect(response.ok).toBe(false); |
| 142 | + expect(response.error).toMatchObject({ |
| 143 | + code: "UNAVAILABLE", |
| 144 | + message: expect.stringContaining("failed to read session transcript storage"), |
| 145 | + }); |
| 146 | + } finally { |
| 147 | + ws.close(); |
| 148 | + } |
| 149 | +}); |
| 150 | + |
| 151 | +test.each([{ maxLines: undefined }, { maxLines: 50 }])( |
| 152 | + "sessions.compact keeps an empty transcript as a successful no-op (maxLines=$maxLines)", |
| 153 | + async ({ maxLines }) => { |
| 154 | + const { storePath } = await createSessionStoreDir(); |
| 155 | + await seedCompactionSession({ |
| 156 | + sessionId: `sess-empty-${maxLines ?? "model"}`, |
| 157 | + storePath, |
| 158 | + withTranscript: false, |
| 159 | + }); |
| 160 | + |
| 161 | + const { ws } = await openClient(); |
| 162 | + try { |
| 163 | + const response = await rpcReq<{ compacted: boolean; ok: true; reason: string }>( |
| 164 | + ws, |
| 165 | + "sessions.compact", |
| 166 | + { key: "main", ...(maxLines === undefined ? {} : { maxLines }) }, |
| 167 | + ); |
| 168 | + |
| 169 | + expect(response.ok).toBe(true); |
| 170 | + expect(response.payload).toMatchObject({ |
| 171 | + ok: true, |
| 172 | + compacted: false, |
| 173 | + reason: "no transcript", |
| 174 | + }); |
| 175 | + } finally { |
| 176 | + ws.close(); |
| 177 | + } |
| 178 | + }, |
| 179 | +); |
0 commit comments