@@ -33,6 +33,7 @@ import * as DateTime from "effect/DateTime";
3333import * as Effect from "effect/Effect" ;
3434import * as FileSystem from "effect/FileSystem" ;
3535import * as Layer from "effect/Layer" ;
36+ import * as MutableList from "effect/MutableList" ;
3637import * as Option from "effect/Option" ;
3738import * as Path from "effect/Path" ;
3839import * as Schema from "effect/Schema" ;
@@ -511,6 +512,30 @@ function extractDecodedCwd(record: DecodedTranscriptRecord): string | null {
511512 return cwd && cwd . length > 0 ? cwd : null ;
512513}
513514
515+ /** Whether a record is a visible user prompt or an assistant reply, using the parser's rules. */
516+ function conversationRole (
517+ source : AgentSessionSource ,
518+ record : DecodedTranscriptRecord ,
519+ ) : "user" | "assistant" | null {
520+ if ( source === "claudeAgent" ) {
521+ if ( record . isSidechain === true || record . isMeta === true || record . isCompactSummary === true ) {
522+ return null ;
523+ }
524+ if ( record . type === "assistant" ) return "assistant" ;
525+ return record . type === "user" && extractText ( record . message ?. content ) . length > 0
526+ ? "user"
527+ : null ;
528+ }
529+ if ( record . type === "event_msg" && record . payload ?. type === "user_message" ) {
530+ return record . payload . message ?. trim ( ) ? "user" : null ;
531+ }
532+ if ( record . type !== "response_item" || record . payload ?. type !== "message" ) return null ;
533+ if ( record . payload . role === "assistant" ) return "assistant" ;
534+ return record . payload . role === "user" && extractText ( record . payload . content ) . length > 0
535+ ? "user"
536+ : null ;
537+ }
538+
514539function shouldRetainDecodedRecord (
515540 source : AgentSessionSource ,
516541 record : DecodedTranscriptRecord ,
@@ -811,8 +836,10 @@ export const make = Effect.gen(function* () {
811836
812837 /**
813838 * Project history fields while reading, before allocating whole JSON records.
814- * Check the file identity on both sides of the read. A selected-history budget
815- * failure rejects the entire transcript before any imported messages persist.
839+ * Check the file identity on both sides of the read. Only the first prompt and
840+ * the newest messages are imported, so once selected history reaches its
841+ * budget, the oldest records after the opening exchange are dropped. A
842+ * transcript is rejected only when its opening exchange cannot fit.
816843 */
817844 const readTranscript = Effect . fn ( "AgentSessionScanner.readTranscript" ) ( function * (
818845 filePath : string ,
@@ -829,17 +856,29 @@ export const make = Effect.gen(function* () {
829856 if ( ! sameTranscriptIdentity ( expected , transcriptIdentity ( filePath , yield * file . stat ) ) ) {
830857 return null ;
831858 }
832- const records : Array < DecodedTranscriptRecord > = [ ] ;
859+ // The opening exchange, through the first reply to a visible prompt,
860+ // holds the first prompt, cwd, and Codex session ID.
861+ const opening : Array < DecodedTranscriptRecord > = [ ] ;
862+ const recent = MutableList . make < {
863+ readonly record : DecodedTranscriptRecord ;
864+ readonly bytes : number ;
865+ } > ( ) ;
866+ let promptSeen = false ;
867+ let openingComplete = false ;
833868 let historyBytes = 0 ;
834869 let recordBytes = 0 ;
835870 let recordCount = 0 ;
836871 let bytesRead = 0 ;
837872 const reserve = ( bytes : number ) => {
838873 recordBytes += bytes ;
839- if ( historyBytes + recordBytes > MAX_IMPORT_HISTORY_BYTES ) {
840- throw new TranscriptJsonLimitError (
841- "Transcript selected history exceeds the 32 MiB memory budget" ,
842- ) ;
874+ while ( historyBytes + recordBytes > MAX_IMPORT_HISTORY_BYTES ) {
875+ const oldest = MutableList . take ( recent ) ;
876+ if ( oldest === MutableList . Empty ) {
877+ throw new TranscriptJsonLimitError (
878+ "Transcript selected history exceeds the 32 MiB memory budget" ,
879+ ) ;
880+ }
881+ historyBytes -= oldest . bytes ;
843882 }
844883 } ;
845884 let reader = createTranscriptJsonReader ( reserve , selectTranscriptPath ) ;
@@ -852,7 +891,14 @@ export const make = Effect.gen(function* () {
852891 if ( recordCount > recordLimit ) return false ;
853892 const decoded = decodeTranscriptValue ( reader . finish ( ) ) ;
854893 if ( Option . isSome ( decoded ) && shouldRetainDecodedRecord ( source , decoded . value ) ) {
855- records . push ( decoded . value ) ;
894+ if ( openingComplete ) {
895+ MutableList . append ( recent , { record : decoded . value , bytes : recordBytes } ) ;
896+ } else {
897+ opening . push ( decoded . value ) ;
898+ }
899+ const role = conversationRole ( source , decoded . value ) ;
900+ promptSeen ||= role === "user" ;
901+ openingComplete ||= promptSeen && role === "assistant" ;
856902 historyBytes += recordBytes ;
857903 }
858904 recordBytes = 0 ;
@@ -889,7 +935,13 @@ export const make = Effect.gen(function* () {
889935
890936 if ( recordStarted && ! ( yield * Effect . try ( finishRecord ) ) ) return null ;
891937 return sameTranscriptIdentity ( expected , transcriptIdentity ( filePath , yield * file . stat ) )
892- ? { records, recordCount }
938+ ? {
939+ records : [
940+ ...opening ,
941+ ...MutableList . toArray ( recent ) . map ( ( entry ) => entry . record ) ,
942+ ] ,
943+ recordCount,
944+ }
893945 : null ;
894946 } ) ,
895947 ) ,
0 commit comments