|
| 1 | +import path from "node:path"; |
| 2 | +import { afterEach, beforeEach, describe, expect, test } from "vitest"; |
| 3 | +import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js"; |
| 4 | +import { replaceTranscriptEvents } from "../config/sessions/session-accessor.js"; |
| 5 | +import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js"; |
| 6 | +import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js"; |
| 7 | +import { captureEnv, setTestEnvValue } from "../test-utils/env.js"; |
| 8 | +import { readSessionMessagesAroundIdWithStatsAsync } from "./session-transcript-anchor-reader.js"; |
| 9 | +import { |
| 10 | + readRecentSessionMessagesWithStatsAsync, |
| 11 | + readSessionMessageByIdAsync, |
| 12 | + readSessionMessageCountAsync, |
| 13 | + readSessionMessagesAsync, |
| 14 | + readSessionMessagesPageWithStatsAsync, |
| 15 | + type SessionTranscriptReadScope, |
| 16 | +} from "./session-transcript-readers.js"; |
| 17 | + |
| 18 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 19 | + |
| 20 | +describe("session transcript reader marker projection", () => { |
| 21 | + let tempDir: string; |
| 22 | + let storePath: string; |
| 23 | + let envSnapshot: ReturnType<typeof captureEnv>; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]); |
| 27 | + tempDir = tempDirs.make("openclaw-transcript-markers-"); |
| 28 | + storePath = path.join(tempDir, "sessions.json"); |
| 29 | + setTestEnvValue("OPENCLAW_STATE_DIR", tempDir); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + closeOpenClawAgentDatabasesForTest(); |
| 34 | + closeOpenClawStateDatabaseForTest(); |
| 35 | + envSnapshot.restore(); |
| 36 | + }); |
| 37 | + |
| 38 | + async function writeTranscript( |
| 39 | + sessionId: string, |
| 40 | + events: unknown[], |
| 41 | + ): Promise<SessionTranscriptReadScope> { |
| 42 | + const scope = { |
| 43 | + agentId: "main", |
| 44 | + sessionId, |
| 45 | + sessionKey: `agent:main:${sessionId}`, |
| 46 | + storePath, |
| 47 | + }; |
| 48 | + await replaceTranscriptEvents(scope, events); |
| 49 | + return scope; |
| 50 | + } |
| 51 | + |
| 52 | + test.each([ |
| 53 | + { |
| 54 | + name: "compaction", |
| 55 | + sessionId: "reader-compaction-boundary", |
| 56 | + markerId: "compaction-boundary", |
| 57 | + markerKind: "compaction", |
| 58 | + markerText: "Compaction", |
| 59 | + events: (sessionId: string) => [ |
| 60 | + { type: "session", version: 3, id: sessionId }, |
| 61 | + { |
| 62 | + type: "message", |
| 63 | + id: "before-compaction", |
| 64 | + parentId: null, |
| 65 | + message: { role: "user", content: "before compaction" }, |
| 66 | + }, |
| 67 | + { |
| 68 | + type: "compaction", |
| 69 | + id: "compaction-boundary", |
| 70 | + parentId: "before-compaction", |
| 71 | + timestamp: "2026-08-11T18:00:00.000Z", |
| 72 | + summary: "summary", |
| 73 | + firstKeptEntryId: "before-compaction", |
| 74 | + tokensBefore: 100, |
| 75 | + }, |
| 76 | + { |
| 77 | + type: "message", |
| 78 | + id: "after-compaction", |
| 79 | + parentId: "compaction-boundary", |
| 80 | + message: { role: "assistant", content: "after compaction" }, |
| 81 | + }, |
| 82 | + ], |
| 83 | + expected: ["before compaction", "compaction", "after compaction"], |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "reset", |
| 87 | + sessionId: "reader-reset-boundary", |
| 88 | + markerId: "reset-boundary", |
| 89 | + markerKind: "reset", |
| 90 | + markerText: "Reset", |
| 91 | + events: (sessionId: string) => [ |
| 92 | + { type: "session", version: 3, id: sessionId }, |
| 93 | + { |
| 94 | + type: "message", |
| 95 | + id: "old", |
| 96 | + parentId: null, |
| 97 | + message: { role: "user", content: "hidden old turn" }, |
| 98 | + }, |
| 99 | + { |
| 100 | + type: "message", |
| 101 | + id: "kept-user", |
| 102 | + parentId: "old", |
| 103 | + message: { role: "user", content: "kept question" }, |
| 104 | + }, |
| 105 | + { |
| 106 | + type: "message", |
| 107 | + id: "kept-assistant", |
| 108 | + parentId: "kept-user", |
| 109 | + message: { role: "assistant", content: "kept answer" }, |
| 110 | + }, |
| 111 | + { |
| 112 | + type: "reset", |
| 113 | + id: "reset-boundary", |
| 114 | + parentId: "kept-assistant", |
| 115 | + timestamp: "2026-08-11T18:00:00.000Z", |
| 116 | + reason: "reset", |
| 117 | + firstKeptEntryId: "kept-user", |
| 118 | + }, |
| 119 | + { |
| 120 | + type: "message", |
| 121 | + id: "post-reset", |
| 122 | + parentId: "reset-boundary", |
| 123 | + message: { role: "assistant", content: "new answer" }, |
| 124 | + }, |
| 125 | + ], |
| 126 | + expected: ["kept question", "kept answer", "reset", "new answer"], |
| 127 | + }, |
| 128 | + ])("projects $name boundaries through every SQLite history read", async (fixture) => { |
| 129 | + const scope = await writeTranscript(fixture.sessionId, fixture.events(fixture.sessionId)); |
| 130 | + const summarize = (messages: unknown[]) => |
| 131 | + messages.map((message) => { |
| 132 | + const record = message as { content?: unknown; __openclaw?: { kind?: string } }; |
| 133 | + return record["__openclaw"]?.kind ?? record.content; |
| 134 | + }); |
| 135 | + |
| 136 | + const full = await readSessionMessagesAsync(scope, { |
| 137 | + mode: "full", |
| 138 | + reason: `${fixture.name} boundary projection test`, |
| 139 | + }); |
| 140 | + const recent = await readRecentSessionMessagesWithStatsAsync(scope, { |
| 141 | + maxBytes: 16_384, |
| 142 | + maxLines: 10, |
| 143 | + maxMessages: 10, |
| 144 | + }); |
| 145 | + const markerIndex = fixture.expected.indexOf(fixture.markerKind); |
| 146 | + const page = await readSessionMessagesPageWithStatsAsync(scope, { |
| 147 | + maxMessages: 1, |
| 148 | + offset: fixture.expected.length - markerIndex - 1, |
| 149 | + }); |
| 150 | + const byId = await readSessionMessageByIdAsync(scope, fixture.markerId); |
| 151 | + const anchored = await readSessionMessagesAroundIdWithStatsAsync(scope, { |
| 152 | + messageId: fixture.markerId, |
| 153 | + maxMessages: 10, |
| 154 | + }); |
| 155 | + |
| 156 | + expect(summarize(full)).toEqual(fixture.expected); |
| 157 | + expect(summarize(recent.messages)).toEqual(fixture.expected); |
| 158 | + expect(recent.totalMessages).toBe(fixture.expected.length); |
| 159 | + expect(summarize(page.messages)).toEqual([fixture.markerKind]); |
| 160 | + expect(page.totalMessages).toBe(fixture.expected.length); |
| 161 | + expect(await readSessionMessageCountAsync(scope)).toBe(fixture.expected.length); |
| 162 | + expect(byId).toMatchObject({ |
| 163 | + found: true, |
| 164 | + message: { |
| 165 | + role: "system", |
| 166 | + content: [{ type: "text", text: fixture.markerText }], |
| 167 | + timestamp: Date.parse("2026-08-11T18:00:00.000Z"), |
| 168 | + __openclaw: { |
| 169 | + kind: fixture.markerKind, |
| 170 | + id: fixture.markerId, |
| 171 | + seq: markerIndex + 1, |
| 172 | + }, |
| 173 | + }, |
| 174 | + seq: markerIndex + 1, |
| 175 | + }); |
| 176 | + expect(anchored.found).toBe(true); |
| 177 | + expect(summarize(anchored.messages)).toEqual(fixture.expected); |
| 178 | + expect(anchored.totalMessages).toBe(fixture.expected.length); |
| 179 | + }); |
| 180 | +}); |
0 commit comments