@@ -9,6 +9,7 @@ import { onSessionTranscriptUpdate } from "../../sessions/transcript-events.js";
99import { openOpenClawAgentDatabase } from "../../state/openclaw-agent-db.js" ;
1010import { appendSqliteTrajectoryRuntimeEvents } from "../../trajectory/runtime-store.sqlite.js" ;
1111import type { TrajectoryEvent } from "../../trajectory/types.js" ;
12+ import { readSessionArchiveContentSync } from "./archive-compression.js" ;
1213import {
1314 applySessionEntryReplacements ,
1415 appendTranscriptMessage ,
@@ -47,10 +48,12 @@ import {
4748 upsertSessionEntry ,
4849} from "./session-accessor.js" ;
4950import {
51+ appendSqliteTranscriptEventSync ,
5052 importSqliteSessionRows ,
5153 loadExactSqliteSessionEntry ,
5254 replaceSqliteSessionEntrySync ,
5355 replaceSqliteTranscriptEvents ,
56+ trimSqliteTranscriptForManualCompact ,
5457} from "./session-accessor.sqlite.js" ;
5558import { resolveSqliteTargetFromSessionStorePath } from "./session-sqlite-target.js" ;
5659import { withOwnedSessionTranscriptWrites } from "./transcript-write-context.js" ;
@@ -2246,7 +2249,16 @@ describe("session accessor seam", () => {
22462249 unsubscribe ( ) ;
22472250 expect ( result ) . toMatchObject ( { compacted : true , kept : 3 } ) ;
22482251 const archived = result . compacted ? result . archived : "" ;
2249- expect ( archived ) . toContain ( `sqlite:main:${ sessionId } :` ) ;
2252+ expect ( path . basename ( archived ) ) . toMatch (
2253+ new RegExp ( `^${ sessionId } \\.jsonl\\.bak\\.\\d{4}-\\d{2}-\\d{2}T` ) ,
2254+ ) ;
2255+ expect ( fs . realpathSync ( path . dirname ( archived ) ) ) . toBe ( fs . realpathSync ( tempDir ) ) ;
2256+ expect ( fs . existsSync ( archived ) ) . toBe ( true ) ;
2257+ const archivedRecords = readSessionArchiveContentSync ( archived )
2258+ . split ( "\n" )
2259+ . filter ( ( line ) => line . length > 0 )
2260+ . map ( ( line ) => JSON . parse ( line ) as Record < string , unknown > ) ;
2261+ expect ( archivedRecords ) . toEqual ( transcriptRecords ) ;
22502262 const trimmedRecords = ( await loadTranscriptEvents ( scope ) ) as Array < Record < string , unknown > > ;
22512263 expect ( trimmedRecords ) . toMatchObject ( [
22522264 { type : "session" , id : sessionId } ,
@@ -2267,6 +2279,130 @@ describe("session accessor seam", () => {
22672279 expect ( updates ) . toEqual ( [ ] ) ;
22682280 } ) ;
22692281
2282+ it ( "keeps every transcript row when the manual compact backup cannot be written" , async ( ) => {
2283+ const sessionId = "44444444-4444-4444-8444-444444444444" ;
2284+ const stateDir = path . join ( tempDir , "state-root" ) ;
2285+ const scope = {
2286+ agentId : "main" ,
2287+ env : { ...process . env , OPENCLAW_STATE_DIR : stateDir } ,
2288+ sessionId,
2289+ sessionKey : "agent:main:main" ,
2290+ } ;
2291+ const records = [
2292+ { type : "session" , version : 3 , id : sessionId , timestamp : "2026-06-19T12:00:00.000Z" } ,
2293+ ...[ 1 , 2 , 3 , 4 ] . map ( ( index ) => ( {
2294+ type : "message" ,
2295+ id : `entry-${ index } ` ,
2296+ parentId : index === 1 ? null : `entry-${ index - 1 } ` ,
2297+ timestamp : `2026-06-19T12:00:0${ index } .000Z` ,
2298+ message : { role : "user" , content : `message ${ index } ` , timestamp : index } ,
2299+ } ) ) ,
2300+ ] ;
2301+ await upsertSessionEntry ( scope , { sessionId, updatedAt : 1 } ) ;
2302+ await replaceSqliteTranscriptEvents (
2303+ scope ,
2304+ records as Parameters < typeof replaceSqliteTranscriptEvents > [ 1 ] ,
2305+ ) ;
2306+ const archiveDirPath = path . join ( stateDir , "agents" , "main" , "sessions" ) ;
2307+ fs . writeFileSync ( archiveDirPath , "not a directory" ) ;
2308+
2309+ await expect ( trimSessionTranscriptForManualCompact ( scope , { maxLines : 3 } ) ) . rejects . toThrow ( ) ;
2310+
2311+ expect ( ( await loadTranscriptEvents ( scope ) ) . length ) . toBe ( 5 ) ;
2312+ expect ( await loadTranscriptEvents ( scope ) ) . toEqual ( records ) ;
2313+ } ) ;
2314+
2315+ it ( "preserves the backup and rows written after the manual compact snapshot" , async ( ) => {
2316+ const sessionId = "55555555-5555-4555-8555-555555555555" ;
2317+ const scope = {
2318+ agentId : "main" ,
2319+ sessionId,
2320+ sessionKey : "agent:main:main" ,
2321+ storePath,
2322+ } ;
2323+ const records = [
2324+ { type : "session" , version : 3 , id : sessionId , timestamp : "2026-06-19T12:00:00.000Z" } ,
2325+ ...[ 1 , 2 , 3 , 4 ] . map ( ( index ) => ( {
2326+ type : "message" ,
2327+ id : `entry-${ index } ` ,
2328+ parentId : index === 1 ? null : `entry-${ index - 1 } ` ,
2329+ timestamp : `2026-06-19T12:00:0${ index } .000Z` ,
2330+ message : { role : "user" , content : `message ${ index } ` , timestamp : index } ,
2331+ } ) ) ,
2332+ ] ;
2333+ const lateEvent = {
2334+ type : "custom" ,
2335+ id : "late-append" ,
2336+ timestamp : "2026-06-19T12:00:09.000Z" ,
2337+ } ;
2338+ await upsertSessionEntry ( scope , { sessionId, updatedAt : 1 } ) ;
2339+ await replaceSqliteTranscriptEvents (
2340+ scope ,
2341+ records as Parameters < typeof replaceSqliteTranscriptEvents > [ 1 ] ,
2342+ ) ;
2343+
2344+ await expect (
2345+ trimSqliteTranscriptForManualCompact ( scope , ( lines ) => {
2346+ appendSqliteTranscriptEventSync ( scope , lateEvent ) ;
2347+ return lines . slice ( 0 , 1 ) ;
2348+ } ) ,
2349+ ) . rejects . toThrow ( `SQLite transcript changed while preparing rewrite for ${ sessionId } ` ) ;
2350+
2351+ const remaining = ( await loadTranscriptEvents ( scope ) ) as Array < Record < string , unknown > > ;
2352+ expect ( remaining ) . toHaveLength ( 6 ) ;
2353+ expect ( remaining . slice ( 0 , 5 ) ) . toEqual ( records ) ;
2354+ expect ( remaining [ 5 ] ) . toMatchObject ( { id : "late-append" } ) ;
2355+ const archiveNames = fs . readdirSync ( tempDir ) . filter ( ( name ) => name . includes ( ".bak." ) ) ;
2356+ expect ( archiveNames ) . toHaveLength ( 1 ) ;
2357+ expect (
2358+ readSessionArchiveContentSync (
2359+ path . join ( tempDir , expectDefined ( archiveNames [ 0 ] , "manual compact archive name" ) ) ,
2360+ ) ,
2361+ ) . toBe ( `${ records . map ( ( record ) => JSON . stringify ( record ) ) . join ( "\n" ) } \n` ) ;
2362+ } ) ;
2363+
2364+ it ( "preserves a reused manual compact backup when the rewrite conflicts" , async ( ) => {
2365+ const sessionId = "66666666-6666-4666-8666-666666666666" ;
2366+ const scope = {
2367+ agentId : "main" ,
2368+ sessionId,
2369+ sessionKey : "agent:main:main" ,
2370+ storePath,
2371+ } ;
2372+ const records = [
2373+ { type : "session" , version : 3 , id : sessionId , timestamp : "2026-06-19T12:00:00.000Z" } ,
2374+ ...[ 1 , 2 , 3 , 4 ] . map ( ( index ) => ( {
2375+ type : "message" ,
2376+ id : `entry-${ index } ` ,
2377+ parentId : index === 1 ? null : `entry-${ index - 1 } ` ,
2378+ timestamp : `2026-06-19T12:00:0${ index } .000Z` ,
2379+ message : { role : "user" , content : `message ${ index } ` , timestamp : index } ,
2380+ } ) ) ,
2381+ ] ;
2382+ const existingArchive = path . join ( tempDir , `${ sessionId } .jsonl.bak.preexisting` ) ;
2383+ const archiveContent = `${ records . map ( ( record ) => JSON . stringify ( record ) ) . join ( "\n" ) } \n` ;
2384+ await upsertSessionEntry ( scope , { sessionId, updatedAt : 1 } ) ;
2385+ await replaceSqliteTranscriptEvents (
2386+ scope ,
2387+ records as Parameters < typeof replaceSqliteTranscriptEvents > [ 1 ] ,
2388+ ) ;
2389+ fs . writeFileSync ( existingArchive , archiveContent ) ;
2390+
2391+ await expect (
2392+ trimSqliteTranscriptForManualCompact ( scope , ( lines ) => {
2393+ appendSqliteTranscriptEventSync ( scope , {
2394+ type : "custom" ,
2395+ id : "late-append" ,
2396+ timestamp : "2026-06-19T12:00:09.000Z" ,
2397+ } ) ;
2398+ return lines . slice ( 0 , 1 ) ;
2399+ } ) ,
2400+ ) . rejects . toThrow ( `SQLite transcript changed while preparing rewrite for ${ sessionId } ` ) ;
2401+
2402+ expect ( fs . existsSync ( existingArchive ) ) . toBe ( true ) ;
2403+ expect ( readSessionArchiveContentSync ( existingArchive ) ) . toBe ( archiveContent ) ;
2404+ } ) ;
2405+
22702406 it ( "repairs a retained compaction boundary when its first kept entry was trimmed" , async ( ) => {
22712407 const sessionId = "33333333-3333-4333-8333-333333333333" ;
22722408 const sessionFile = path . join ( tempDir , `${ sessionId } .jsonl` ) ;
0 commit comments