@@ -359,6 +359,7 @@ interface ActivePiTurn {
359359 readonly streamItems : Map < string , PiStreamItemState > ;
360360 readonly toolArgs : Map < string , unknown > ;
361361 readonly toolStartedAt : Map < string , DateTime . Utc > ;
362+ readonly commandOutputBytes : Map < string , number > ;
362363 interrupted : boolean ;
363364 nativeAborted : boolean ;
364365 sawAgentActivity : boolean ;
@@ -1193,6 +1194,33 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
11931194 { discard : true } ,
11941195 ) ;
11951196
1197+ // Pi resends the whole output tail on every update; stored snapshots grow quadratically.
1198+ const emitCommandOutputDelta = ( turn : ActivePiTurn , toolCallId : string , result : unknown ) =>
1199+ Effect . suspend ( ( ) => {
1200+ const text = contentText ( recordField ( result , "content" ) ) ;
1201+ const tail = Buffer . from ( text , "utf8" ) ;
1202+ const totalBytes =
1203+ recordNumber ( recordField ( recordField ( result , "details" ) , "truncation" ) , "totalBytes" ) ??
1204+ tail . length ;
1205+ const previousBytes = turn . commandOutputBytes . get ( toolCallId ) ?? 0 ;
1206+ turn . commandOutputBytes . set ( toolCallId , totalBytes ) ;
1207+ if ( totalBytes <= previousBytes ) return Effect . void ;
1208+ let start = Math . max ( 0 , tail . length - ( totalBytes - previousBytes ) ) ;
1209+ while ( start < tail . length && ( tail [ start ] ! & 0xc0 ) === 0x80 ) start ++ ;
1210+ const chunk = tail . subarray ( start ) . toString ( "utf8" ) ;
1211+ if ( chunk . length === 0 ) return Effect . void ;
1212+ return emit ( {
1213+ type : "command_output.delta" ,
1214+ driver : PI_PROVIDER ,
1215+ threadId : turn . turnInput . threadId ,
1216+ itemId : idAllocator . derive . turnItemFromProviderItem ( {
1217+ driver : PI_PROVIDER ,
1218+ nativeItemId : toolCallId ,
1219+ } ) ,
1220+ chunk,
1221+ } ) ;
1222+ } ) ;
1223+
11961224 const emitToolItem = Effect . fnUntraced ( function * (
11971225 turn : ActivePiTurn ,
11981226 event : PiRpcRecord ,
@@ -1203,6 +1231,10 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
12031231 // Pi tags every phase of a call made through ctx.executeTool, such as from a codemode script.
12041232 const parentToolCallId = recordString ( event , "parentToolCallId" ) ;
12051233 if ( toolCallId === undefined ) return ;
1234+ if ( phase === "update" && ( toolName === "bash" || toolName === "powershell" ) ) {
1235+ yield * emitCommandOutputDelta ( turn , toolCallId , event [ "partialResult" ] ) ;
1236+ return ;
1237+ }
12061238 if ( phase === "start" ) {
12071239 turn . toolArgs . set ( toolCallId , event [ "args" ] ) ;
12081240 }
@@ -2906,6 +2938,7 @@ export const makePiAdapterV2 = Effect.fn("makePiAdapterV2")(function* (
29062938 streamItems : new Map ( ) ,
29072939 toolArgs : new Map ( ) ,
29082940 toolStartedAt : new Map ( ) ,
2941+ commandOutputBytes : new Map ( ) ,
29092942 interrupted : false ,
29102943 nativeAborted : false ,
29112944 sawAgentActivity : false ,
0 commit comments