|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
| 3 | +import { closeOpenClawAgentDatabasesForTest } from "../../state/openclaw-agent-db.js"; |
| 4 | +import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js"; |
| 5 | +import { |
| 6 | + appendTranscriptEvent, |
| 7 | + appendTranscriptMessage, |
| 8 | + forkSessionAtMessage, |
| 9 | + loadSessionEntry, |
| 10 | + loadTranscriptEvents, |
| 11 | + readSessionTranscriptMessageEventCount, |
| 12 | + rewindSessionToMessage, |
| 13 | + upsertSessionEntry, |
| 14 | +} from "./session-accessor.js"; |
| 15 | + |
| 16 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 17 | +const agentId = "main"; |
| 18 | +const sessionKey = "agent:main:message-cut"; |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + closeOpenClawAgentDatabasesForTest(); |
| 22 | + closeOpenClawStateDatabaseForTest(); |
| 23 | +}); |
| 24 | + |
| 25 | +async function createSession() { |
| 26 | + const stateDir = tempDirs.make("openclaw-message-cut-"); |
| 27 | + const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir }; |
| 28 | + const sessionId = "message-cut-source"; |
| 29 | + const scope = { agentId, env, sessionId, sessionKey }; |
| 30 | + await upsertSessionEntry(scope, { |
| 31 | + agentHarnessId: "embedded", |
| 32 | + claudeCliSessionId: "claude-conversation", |
| 33 | + cliSessionBindings: { "claude-cli": { sessionId: "claude-conversation" } }, |
| 34 | + cliSessionIds: { "claude-cli": "claude-conversation" }, |
| 35 | + compactionCount: 2, |
| 36 | + contextTokens: 100_000, |
| 37 | + deliveryContext: { channel: "telegram", to: "chat-123" }, |
| 38 | + lastChannel: "telegram", |
| 39 | + lastTo: "chat-123", |
| 40 | + lifecycleRevision: "source-lifecycle-revision", |
| 41 | + modelOverride: "gpt-5", |
| 42 | + modelOverrideSource: "user", |
| 43 | + providerOverride: "openai", |
| 44 | + sessionId, |
| 45 | + updatedAt: Date.now(), |
| 46 | + }); |
| 47 | + for (const event of [ |
| 48 | + { type: "session", id: sessionId, version: 3, timestamp: "2026-07-18T00:00:00.000Z" }, |
| 49 | + { |
| 50 | + type: "message", |
| 51 | + id: "user-1", |
| 52 | + parentId: null, |
| 53 | + timestamp: "2026-07-18T00:00:01.000Z", |
| 54 | + message: { role: "user", content: "first prompt" }, |
| 55 | + }, |
| 56 | + { |
| 57 | + type: "message", |
| 58 | + id: "assistant-1", |
| 59 | + parentId: "user-1", |
| 60 | + timestamp: "2026-07-18T00:00:02.000Z", |
| 61 | + message: { role: "assistant", content: "first answer" }, |
| 62 | + }, |
| 63 | + { |
| 64 | + type: "message", |
| 65 | + id: "user-2", |
| 66 | + parentId: "assistant-1", |
| 67 | + timestamp: "2026-07-18T00:00:03.000Z", |
| 68 | + message: { role: "user", content: [{ type: "text", text: "second prompt" }] }, |
| 69 | + }, |
| 70 | + { |
| 71 | + type: "message", |
| 72 | + id: "assistant-2", |
| 73 | + parentId: "user-2", |
| 74 | + timestamp: "2026-07-18T00:00:04.000Z", |
| 75 | + message: { role: "assistant", content: "second answer" }, |
| 76 | + }, |
| 77 | + { |
| 78 | + type: "message", |
| 79 | + id: "off-path-user", |
| 80 | + parentId: "user-1", |
| 81 | + timestamp: "2026-07-18T00:00:05.000Z", |
| 82 | + message: { role: "user", content: "inactive prompt" }, |
| 83 | + }, |
| 84 | + { |
| 85 | + type: "leaf", |
| 86 | + id: "active-leaf", |
| 87 | + parentId: "off-path-user", |
| 88 | + timestamp: "2026-07-18T00:00:06.000Z", |
| 89 | + targetId: "assistant-2", |
| 90 | + }, |
| 91 | + ]) { |
| 92 | + if (event.type === "message") { |
| 93 | + await appendTranscriptMessage(scope, { |
| 94 | + eventId: event.id, |
| 95 | + message: event.message, |
| 96 | + parentId: event.parentId, |
| 97 | + }); |
| 98 | + } else { |
| 99 | + await appendTranscriptEvent(scope, event); |
| 100 | + } |
| 101 | + } |
| 102 | + return { env, scope }; |
| 103 | +} |
| 104 | + |
| 105 | +describe("SQLite session message cuts", () => { |
| 106 | + it("rewinds by repointing the active leaf and returns the editor text", async () => { |
| 107 | + const { env } = await createSession(); |
| 108 | + |
| 109 | + const result = await rewindSessionToMessage({ |
| 110 | + agentId, |
| 111 | + env, |
| 112 | + entryId: "user-2", |
| 113 | + sessionKey, |
| 114 | + }); |
| 115 | + |
| 116 | + expect(result).toMatchObject({ |
| 117 | + status: "created", |
| 118 | + key: sessionKey, |
| 119 | + editorText: "second prompt", |
| 120 | + }); |
| 121 | + if (result.status !== "created") { |
| 122 | + throw new Error("expected rewind result"); |
| 123 | + } |
| 124 | + expect( |
| 125 | + readSessionTranscriptMessageEventCount({ agentId, env, sessionId: result.entry.sessionId }), |
| 126 | + ).toBe(2); |
| 127 | + expect(loadSessionEntry({ agentId, env, sessionKey })?.sessionId).toBe(result.entry.sessionId); |
| 128 | + expect(result.entry).toMatchObject({ |
| 129 | + agentHarnessId: undefined, |
| 130 | + claudeCliSessionId: undefined, |
| 131 | + cliSessionBindings: undefined, |
| 132 | + cliSessionIds: undefined, |
| 133 | + compactionCount: undefined, |
| 134 | + contextTokens: undefined, |
| 135 | + }); |
| 136 | + expect(result.entry.deliveryContext).toEqual({ channel: "telegram", to: "chat-123" }); |
| 137 | + }); |
| 138 | + |
| 139 | + it("rewinds the stored row when its canonical key differs", async () => { |
| 140 | + const { env } = await createSession(); |
| 141 | + const canonicalKey = "agent:main:canonical-message-cut"; |
| 142 | + |
| 143 | + const result = await rewindSessionToMessage({ |
| 144 | + agentId, |
| 145 | + env, |
| 146 | + entryId: "user-2", |
| 147 | + sessionKey: canonicalKey, |
| 148 | + sessionStoreKey: sessionKey, |
| 149 | + }); |
| 150 | + |
| 151 | + expect(result).toMatchObject({ status: "created", key: sessionKey }); |
| 152 | + if (result.status !== "created") { |
| 153 | + throw new Error("expected rewind result"); |
| 154 | + } |
| 155 | + expect(loadSessionEntry({ agentId, env, sessionKey })?.sessionId).toBe(result.entry.sessionId); |
| 156 | + expect(loadSessionEntry({ agentId, env, sessionKey: canonicalKey })).toBeUndefined(); |
| 157 | + }); |
| 158 | + |
| 159 | + it("forks an exact active-path prefix without changing the source", async () => { |
| 160 | + const { env, scope } = await createSession(); |
| 161 | + const canonicalSourceKey = "agent:main:canonical-message-cut-source"; |
| 162 | + const targetKey = "agent:main:dashboard:message-cut-fork"; |
| 163 | + |
| 164 | + const result = await forkSessionAtMessage({ |
| 165 | + agentId, |
| 166 | + env, |
| 167 | + entryId: "user-2", |
| 168 | + sessionKey: canonicalSourceKey, |
| 169 | + sessionStoreKey: sessionKey, |
| 170 | + targetKey, |
| 171 | + }); |
| 172 | + |
| 173 | + expect(result).toMatchObject({ |
| 174 | + status: "created", |
| 175 | + key: targetKey, |
| 176 | + editorText: "second prompt", |
| 177 | + }); |
| 178 | + if (result.status !== "created") { |
| 179 | + throw new Error("expected fork result"); |
| 180 | + } |
| 181 | + const forkEvents = await loadTranscriptEvents({ |
| 182 | + agentId, |
| 183 | + env, |
| 184 | + sessionId: result.entry.sessionId, |
| 185 | + sessionKey: targetKey, |
| 186 | + }); |
| 187 | + expect( |
| 188 | + forkEvents.flatMap((event) => |
| 189 | + event && typeof event === "object" && "id" in event ? [event.id] : [], |
| 190 | + ), |
| 191 | + ).toEqual([result.entry.sessionId, "user-1", "assistant-1"]); |
| 192 | + expect(loadSessionEntry(scope)?.sessionId).toBe(scope.sessionId); |
| 193 | + expect(result.entry.lifecycleRevision).not.toBe("source-lifecycle-revision"); |
| 194 | + expect(result.entry.cliSessionBindings).toBeUndefined(); |
| 195 | + expect(result.entry.deliveryContext).toBeUndefined(); |
| 196 | + expect(result.entry.lastChannel).toBeUndefined(); |
| 197 | + expect(result.entry.lastTo).toBeUndefined(); |
| 198 | + expect(result.entry.parentSessionKey).toBe(canonicalSourceKey); |
| 199 | + expect(result.entry).toMatchObject({ |
| 200 | + modelOverride: "gpt-5", |
| 201 | + modelOverrideSource: "user", |
| 202 | + providerOverride: "openai", |
| 203 | + }); |
| 204 | + expect(loadSessionEntry(scope)?.lifecycleRevision).toBe("source-lifecycle-revision"); |
| 205 | + }); |
| 206 | + |
| 207 | + it.each([ |
| 208 | + ["unknown", "missing-entry"], |
| 209 | + ["assistant-1", "not-user-message"], |
| 210 | + ["off-path-user", "off-active-path"], |
| 211 | + ])("rejects %s with %s", async (entryId, status) => { |
| 212 | + const { env } = await createSession(); |
| 213 | + |
| 214 | + await expect( |
| 215 | + rewindSessionToMessage({ agentId, env, entryId, sessionKey }), |
| 216 | + ).resolves.toMatchObject({ status }); |
| 217 | + }); |
| 218 | + |
| 219 | + it("returns a typed error for legacy JSONL transcript storage", async () => { |
| 220 | + const { env } = await createSession(); |
| 221 | + await upsertSessionEntry( |
| 222 | + { agentId, env, sessionKey }, |
| 223 | + { |
| 224 | + sessionFile: "/tmp/legacy-session.jsonl", |
| 225 | + }, |
| 226 | + ); |
| 227 | + |
| 228 | + await expect( |
| 229 | + rewindSessionToMessage({ agentId, env, entryId: "user-2", sessionKey }), |
| 230 | + ).resolves.toMatchObject({ status: "unsupported-storage" }); |
| 231 | + }); |
| 232 | +}); |
0 commit comments