@@ -54,6 +54,8 @@ type OriginRoutingMetadata = Pick<
5454 "originatingChannel" | "originatingTo" | "originatingAccountId" | "originatingThreadId"
5555> ;
5656
57+ type OriginMessageMetadata = Pick < FollowupRun , "messageId" | "originatingReplyToId" > ;
58+
5759function resolveOriginRoutingMetadata ( items : FollowupRun [ ] ) : OriginRoutingMetadata {
5860 const metadata : OriginRoutingMetadata = { } ;
5961 for ( const item of items ) {
@@ -86,6 +88,23 @@ function resolveOriginRoutingMetadata(items: FollowupRun[]): OriginRoutingMetada
8688 return metadata ;
8789}
8890
91+ function resolveOriginMessageMetadata ( items : FollowupRun [ ] ) : OriginMessageMetadata {
92+ const metadata : OriginMessageMetadata = { } ;
93+ for ( let index = items . length - 1 ; index >= 0 ; index -= 1 ) {
94+ const item = items [ index ] ;
95+ if ( ! metadata . messageId && item . messageId ) {
96+ metadata . messageId = item . messageId ;
97+ }
98+ if ( ! metadata . originatingReplyToId && item . originatingReplyToId ) {
99+ metadata . originatingReplyToId = item . originatingReplyToId ;
100+ }
101+ if ( metadata . messageId && metadata . originatingReplyToId ) {
102+ break ;
103+ }
104+ }
105+ return metadata ;
106+ }
107+
89108// Keep this key aligned with the fields that affect per-message authorization or
90109// exec-context propagation in collect-mode batching. Display-only sender fields
91110// stay out of the key so profile/name drift does not force conservative splits.
@@ -474,6 +493,7 @@ export function scheduleFollowupDrain(
474493 }
475494
476495 const routing = resolveOriginRoutingMetadata ( groupItems ) ;
496+ const messageMetadata = resolveOriginMessageMetadata ( groupItems ) ;
477497 const prompt = buildCollectPrompt ( {
478498 title : "[Queued messages while agent was busy]" ,
479499 items : groupItems ,
@@ -486,6 +506,7 @@ export function scheduleFollowupDrain(
486506 run,
487507 enqueuedAt : Date . now ( ) ,
488508 ...routing ,
509+ ...messageMetadata ,
489510 ...collectRuntimeMetadata ( groupItems ) ,
490511 ...collectQueuedImages ( groupItems ) ,
491512 } ) ;
@@ -525,10 +546,12 @@ export function scheduleFollowupDrain(
525546 prompt : summaryPrompt ,
526547 run,
527548 enqueuedAt : Date . now ( ) ,
549+ messageId : item . messageId ,
528550 originatingChannel : item . originatingChannel ,
529551 originatingTo : item . originatingTo ,
530552 originatingAccountId : item . originatingAccountId ,
531553 originatingThreadId : item . originatingThreadId ,
554+ originatingReplyToId : item . originatingReplyToId ,
532555 ...collectSummaryRuntimeMetadata ( [ item ] ) ,
533556 ...collectQueuedImages ( [ item ] ) ,
534557 } ) ;
0 commit comments