1- import { afterEach , describe , expect , it } from "vitest" ;
1+ import { afterEach , describe , expect , it , vi } from "vitest" ;
22import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js" ;
3- import { closeOpenClawAgentDatabasesForTest } from "../../state/openclaw-agent-db.js" ;
3+ import {
4+ closeOpenClawAgentDatabasesForTest ,
5+ openOpenClawAgentDatabase ,
6+ } from "../../state/openclaw-agent-db.js" ;
47import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js" ;
58import {
69 deliveryContextFromSession ,
@@ -19,16 +22,52 @@ import {
1922 switchSessionBranch ,
2023 upsertSessionEntry ,
2124} from "./session-accessor.js" ;
25+ import { listSqliteSessionBranches } from "./session-accessor.sqlite.js" ;
2226
2327const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
2428const agentId = "main" ;
2529const sessionKey = "agent:main:message-cut" ;
2630
2731afterEach ( ( ) => {
32+ vi . restoreAllMocks ( ) ;
2833 closeOpenClawAgentDatabasesForTest ( ) ;
2934 closeOpenClawStateDatabaseForTest ( ) ;
3035} ) ;
3136
37+ function trackFullTranscriptLoads ( env : NodeJS . ProcessEnv ) : ( ) => number {
38+ const database = openOpenClawAgentDatabase ( { agentId, env } ) ;
39+ const prepare = vi . spyOn ( database . db , "prepare" ) ;
40+ return ( ) =>
41+ prepare . mock . calls . filter (
42+ ( [ sql ] ) =>
43+ sql . includes ( 'select "event_json" from "transcript_events"' ) &&
44+ sql . includes ( 'order by "seq" asc' ) ,
45+ ) . length ;
46+ }
47+
48+ async function createSiblingSession ( params : {
49+ env : NodeJS . ProcessEnv ;
50+ headline : string ;
51+ sessionId : string ;
52+ sessionKey : string ;
53+ } ) {
54+ const scope = { agentId, ...params } ;
55+ await upsertSessionEntry ( scope , { sessionId : params . sessionId , updatedAt : Date . now ( ) } ) ;
56+ await appendTranscriptEvent ( scope , {
57+ type : "session" ,
58+ id : params . sessionId ,
59+ version : 3 ,
60+ timestamp : "2026-07-18T01:00:00.000Z" ,
61+ } ) ;
62+ await appendTranscriptMessage ( scope , {
63+ eventId : `${ params . sessionId } -user` ,
64+ message : { role : "user" , content : params . headline } ,
65+ now : Date . parse ( "2026-07-18T01:00:01.000Z" ) ,
66+ parentId : null ,
67+ } ) ;
68+ return scope ;
69+ }
70+
3271async function createSession ( options : { activeLeafTarget ?: string } = { } ) {
3372 const stateDir = tempDirs . make ( "openclaw-message-cut-" ) ;
3473 const env = { ...process . env , OPENCLAW_STATE_DIR : stateDir } ;
@@ -127,6 +166,140 @@ async function createSession(options: { activeLeafTarget?: string } = {}) {
127166}
128167
129168describe ( "SQLite session message cuts" , ( ) => {
169+ it ( "reuses branch summaries while the transcript watermark is unchanged" , async ( ) => {
170+ const { env } = await createSession ( ) ;
171+ const fullTranscriptLoads = trackFullTranscriptLoads ( env ) ;
172+
173+ const first = await listSqliteSessionBranches ( { agentId, env, sessionKey } ) ;
174+ expect ( fullTranscriptLoads ( ) ) . toBe ( 1 ) ;
175+
176+ const second = await listSqliteSessionBranches ( { agentId, env, sessionKey } ) ;
177+ expect ( second ) . toEqual ( first ) ;
178+ expect ( fullTranscriptLoads ( ) ) . toBe ( 1 ) ;
179+ if ( second . status !== "ok" || ! second . branches [ 0 ] ) {
180+ throw new Error ( "expected cached branch list result" ) ;
181+ }
182+ second . branches [ 0 ] . headline = "caller mutation" ;
183+
184+ await expect ( listSqliteSessionBranches ( { agentId, env, sessionKey } ) ) . resolves . toEqual ( first ) ;
185+ expect ( fullTranscriptLoads ( ) ) . toBe ( 1 ) ;
186+ } ) ;
187+
188+ it ( "recomputes branch summaries after an append advances the watermark" , async ( ) => {
189+ const { env, scope } = await createSession ( ) ;
190+ const fullTranscriptLoads = trackFullTranscriptLoads ( env ) ;
191+
192+ const before = await listSqliteSessionBranches ( { agentId, env, sessionKey } ) ;
193+ expect ( fullTranscriptLoads ( ) ) . toBe ( 1 ) ;
194+ await appendTranscriptMessage ( scope , {
195+ eventId : "assistant-3" ,
196+ message : { role : "assistant" , content : "third answer" } ,
197+ now : Date . parse ( "2026-07-18T00:00:07.000Z" ) ,
198+ parentId : "assistant-2" ,
199+ } ) ;
200+
201+ const after = await listSqliteSessionBranches ( { agentId, env, sessionKey } ) ;
202+ expect ( fullTranscriptLoads ( ) ) . toBe ( 2 ) ;
203+ expect ( after ) . not . toEqual ( before ) ;
204+ expect ( after . status ) . toBe ( "ok" ) ;
205+ if ( after . status !== "ok" ) {
206+ throw new Error ( "expected branch list result" ) ;
207+ }
208+ expect ( after . branches . find ( ( branch ) => branch . active ) ) . toMatchObject ( {
209+ leafEntryId : "assistant-3" ,
210+ headline : "third answer" ,
211+ } ) ;
212+ } ) ;
213+
214+ it . each ( [ "rewind" , "switch" , "fork" ] as const ) (
215+ "%s invalidates the source cache and lists the resulting branch" ,
216+ async ( mode ) => {
217+ const { env, scope } = await createSession ( ) ;
218+ const aliasKey = `${ sessionKey } :alias` ;
219+ const targetKey = `${ sessionKey } :fork` ;
220+ const sourceEntry = loadSessionEntry ( scope ) ;
221+ if ( ! sourceEntry ) {
222+ throw new Error ( "expected source session entry" ) ;
223+ }
224+ await upsertSessionEntry ( { agentId, env, sessionKey : aliasKey } , sourceEntry ) ;
225+ const fullTranscriptLoads = trackFullTranscriptLoads ( env ) ;
226+ await listSqliteSessionBranches ( { agentId, env, sessionKey } ) ;
227+
228+ const result =
229+ mode === "rewind"
230+ ? await rewindSessionToMessage ( { agentId, env, entryId : "user-2" , sessionKey } )
231+ : mode === "switch"
232+ ? await switchSessionBranch ( {
233+ agentId,
234+ env,
235+ leafEntryId : "off-path-user" ,
236+ sessionKey,
237+ } )
238+ : await forkSessionAtMessage ( {
239+ agentId,
240+ env,
241+ entryId : "user-2" ,
242+ sessionKey,
243+ targetKey,
244+ } ) ;
245+ expect ( result . status ) . toBe ( "created" ) ;
246+
247+ const loadsBeforeAliasRead = fullTranscriptLoads ( ) ;
248+ await listSqliteSessionBranches ( { agentId, env, sessionKey : aliasKey } ) ;
249+ expect ( fullTranscriptLoads ( ) ) . toBe ( loadsBeforeAliasRead + 1 ) ;
250+
251+ const listed = await listSqliteSessionBranches ( {
252+ agentId,
253+ env,
254+ sessionKey : mode === "fork" ? targetKey : sessionKey ,
255+ } ) ;
256+ expect ( listed . status ) . toBe ( "ok" ) ;
257+ if ( listed . status !== "ok" ) {
258+ throw new Error ( "expected branch list result" ) ;
259+ }
260+ expect ( listed . branches . find ( ( branch ) => branch . active ) ) . toMatchObject ( {
261+ leafEntryId : mode === "switch" ? "off-path-user" : "assistant-1" ,
262+ } ) ;
263+ } ,
264+ ) ;
265+
266+ it ( "keeps branch summaries isolated between sessions in the same store" , async ( ) => {
267+ const { env } = await createSession ( ) ;
268+ const sibling = await createSiblingSession ( {
269+ env,
270+ headline : "sibling prompt" ,
271+ sessionId : "message-cut-sibling" ,
272+ sessionKey : `${ sessionKey } :sibling` ,
273+ } ) ;
274+ const fullTranscriptLoads = trackFullTranscriptLoads ( env ) ;
275+
276+ const source = await listSqliteSessionBranches ( { agentId, env, sessionKey } ) ;
277+ const other = await listSqliteSessionBranches ( sibling ) ;
278+ expect ( fullTranscriptLoads ( ) ) . toBe ( 2 ) ;
279+ expect ( source . status ) . toBe ( "ok" ) ;
280+ if ( source . status !== "ok" ) {
281+ throw new Error ( "expected source branch list result" ) ;
282+ }
283+ expect ( source . branches . find ( ( branch ) => branch . active ) ) . toMatchObject ( {
284+ leafEntryId : "assistant-2" ,
285+ headline : "second answer" ,
286+ } ) ;
287+ expect ( other ) . toEqual ( {
288+ status : "ok" ,
289+ branches : [
290+ {
291+ active : true ,
292+ headline : "sibling prompt" ,
293+ leafEntryId : "message-cut-sibling-user" ,
294+ messageCount : 1 ,
295+ updatedAt : "2026-07-18T01:00:01.000Z" ,
296+ } ,
297+ ] ,
298+ } ) ;
299+ await expect ( listSqliteSessionBranches ( { agentId, env, sessionKey } ) ) . resolves . toEqual ( source ) ;
300+ expect ( fullTranscriptLoads ( ) ) . toBe ( 2 ) ;
301+ } ) ;
302+
130303 it ( "lists every DAG tip with active state, headline, count, and timestamp" , async ( ) => {
131304 const { env } = await createSession ( { activeLeafTarget : "assistant-1" } ) ;
132305
0 commit comments