@@ -5,9 +5,9 @@ import type { DatabaseSync } from "node:sqlite";
55import { TextDecoder } from "node:util" ;
66import { isRecord } from "@openclaw/normalization-core/record-coerce" ;
77import {
8+ classifySessionFileEntry ,
89 migrateSessionFileEntryToCurrentVersion ,
910 normalizeLoadedFileEntry ,
10- partitionSessionFileEntries ,
1111 type SessionFileEntryMigrationState ,
1212} from "../agents/sessions/session-manager-codec.js" ;
1313import type { FileEntry } from "../agents/sessions/session-manager-types.js" ;
@@ -112,13 +112,6 @@ function readTranscriptEventsForImport(
112112 // through commit and archive. Fingerprints catch non-cooperating external edits.
113113 const plan = planTranscriptImport ( transcriptPath , allowMalformedPrefix ) ;
114114 assertTranscriptFileUnchanged ( transcriptPath , sourceFingerprint ) ;
115- const classificationHeader = {
116- id : sessionId ,
117- type : "session" ,
118- version : plan . sourceVersion ,
119- timestamp : "" ,
120- cwd : "" ,
121- } satisfies FileEntry ;
122115 // V1 compactions refer to original row indexes. Stable index-derived IDs let
123116 // the second pass resolve those links without retaining the transcript.
124117 const idPrefix = createHash ( "sha256" )
@@ -143,6 +136,9 @@ function readTranscriptEventsForImport(
143136 allowMalformedPrefix ,
144137 ) ) {
145138 let event = loadedEvent ;
139+ if ( plan . sourceVersion >= 2 ) {
140+ recordLegacyCompactionTarget ( event , plan . compactionTargetIndexes ) ;
141+ }
146142 let recognizedEvent : FileEntry | undefined ;
147143 if ( originalIndex === plan . headerIndex ) {
148144 const canonicalHeader = {
@@ -156,15 +152,15 @@ function readTranscriptEventsForImport(
156152 event = canonicalHeader ;
157153 recognizedEvent = event ;
158154 } else {
159- // Reuse the runtime partition contract one row at a time. The
160- // synthetic header carries the source version without being emitted.
161- recognizedEvent = partitionSessionFileEntries ( [ classificationHeader , event ] )
162- . fileEntriesByOriginalIndex [ 1 ] ;
155+ const classified = classifySessionFileEntry ( event , plan . sourceVersion ) ;
156+ // Runtime retains normalized opaque rows; import preserves their loaded bytes.
157+ recognizedEvent = classified . recognized ? classified . entry : undefined ;
163158 }
164159
165160 if ( recognizedEvent ) {
166161 migrateSessionFileEntryToCurrentVersion ( recognizedEvent , originalIndex , migrationState ) ;
167162 if (
163+ plan . sourceVersion < 2 &&
168164 recognizedEvent . type !== "session" &&
169165 plan . compactionTargetIndexes . has ( originalIndex )
170166 ) {
@@ -240,28 +236,34 @@ function planTranscriptImport(
240236 if ( plan . headerIndex < 0 && isRecord ( event ) && event . type === "session" ) {
241237 plan . headerIndex = originalIndex ;
242238 plan . sourceVersion = typeof event . version === "number" ? event . version : 1 ;
243- }
244- if (
245- isRecord ( event ) &&
246- event . type === "compaction" &&
247- Number . isInteger ( event . firstKeptEntryIndex ) &&
248- Number ( event . firstKeptEntryIndex ) >= 0
249- ) {
250- const targetIndex = Number ( event . firstKeptEntryIndex ) ;
251- if (
252- ! plan . compactionTargetIndexes . has ( targetIndex ) &&
253- plan . compactionTargetIndexes . size >= MAX_LEGACY_COMPACTION_TARGETS
254- ) {
255- throw new TranscriptImportLimitError (
256- `Transcript has more than ${ MAX_LEGACY_COMPACTION_TARGETS } legacy compaction targets` ,
257- ) ;
239+ // Only v1 needs future row indexes before migration. Newer files validate
240+ // the same target limit while streaming, before any canonical write.
241+ if ( plan . sourceVersion >= 2 ) {
242+ return plan ;
258243 }
259- plan . compactionTargetIndexes . add ( targetIndex ) ;
260244 }
245+ recordLegacyCompactionTarget ( event , plan . compactionTargetIndexes ) ;
261246 }
262247 return plan ;
263248}
264249
250+ function recordLegacyCompactionTarget ( event : FileEntry , targets : Set < number > ) : void {
251+ if (
252+ isRecord ( event ) &&
253+ event . type === "compaction" &&
254+ Number . isInteger ( event . firstKeptEntryIndex ) &&
255+ Number ( event . firstKeptEntryIndex ) >= 0
256+ ) {
257+ const targetIndex = Number ( event . firstKeptEntryIndex ) ;
258+ if ( ! targets . has ( targetIndex ) && targets . size >= MAX_LEGACY_COMPACTION_TARGETS ) {
259+ throw new TranscriptImportLimitError (
260+ `Transcript has more than ${ MAX_LEGACY_COMPACTION_TARGETS } legacy compaction targets` ,
261+ ) ;
262+ }
263+ targets . add ( targetIndex ) ;
264+ }
265+ }
266+
265267function * iterateTranscriptEvents (
266268 transcriptPath : string ,
267269 allowMalformedPrefix : boolean ,
0 commit comments