@@ -142,14 +142,38 @@ export class AgentTaskPersistence {
142142 taskId : string ,
143143 maxPreviewBytes : number ,
144144 ) : Promise < AgentTaskStoredOutputSnapshot | undefined > {
145- const output = await this . readTaskOutputData ( taskId ) ;
146- if ( output === undefined ) return undefined ;
147- const preview = utf8TailPreview ( output . data , maxPreviewBytes ) ;
145+ let root = this . primaryRoot ( ) ;
146+ let outputSizeBytes = await this . bytes . size ( this . taskOutputScope ( taskId , root ) , OUTPUT_LOG_KEY ) ;
147+ if ( outputSizeBytes === undefined ) {
148+ const fallbackRoot = this . fallbackRoot ;
149+ if ( fallbackRoot === undefined ) return undefined ;
150+ root = fallbackRoot ;
151+ outputSizeBytes = await this . bytes . size ( this . taskOutputScope ( taskId , root ) , OUTPUT_LOG_KEY ) ;
152+ if ( outputSizeBytes === undefined ) return undefined ;
153+ }
154+
155+ const previewLimit = Math . min ( outputSizeBytes , Math . max ( 0 , Math . trunc ( maxPreviewBytes ) ) ) ;
156+ const data = new Uint8Array ( previewLimit ) ;
157+ let offset = 0 ;
158+ if ( previewLimit > 0 ) {
159+ const start = outputSizeBytes - previewLimit ;
160+ for await ( const chunk of this . bytes . readStream (
161+ this . taskOutputScope ( taskId , root ) ,
162+ OUTPUT_LOG_KEY ,
163+ { start, end : outputSizeBytes - 1 } ,
164+ ) ) {
165+ const slice = chunk . subarray ( 0 , previewLimit - offset ) ;
166+ data . set ( slice , offset ) ;
167+ offset += slice . byteLength ;
168+ if ( offset === previewLimit ) break ;
169+ }
170+ }
171+ const preview = utf8TailPreview ( data . subarray ( 0 , offset ) , previewLimit ) ;
148172 return {
149- outputPath : this . taskOutputFileAt ( taskId , output . root ) ,
150- outputSizeBytes : output . data . byteLength ,
173+ outputPath : this . taskOutputFileAt ( taskId , root ) ,
174+ outputSizeBytes,
151175 previewBytes : preview . bytes ,
152- truncated : output . data . byteLength > preview . bytes ,
176+ truncated : outputSizeBytes > preview . bytes ,
153177 preview : preview . text ,
154178 } ;
155179 }
0 commit comments