@@ -51,7 +51,10 @@ import {
5151 normalizeOptionalLowercaseString ,
5252 normalizeOptionalString ,
5353} from "../../shared/string-coerce.js" ;
54- import { normalizeSessionDeliveryFields } from "../../utils/delivery-context.shared.js" ;
54+ import {
55+ normalizeDeliveryChannelRoute ,
56+ normalizeSessionDeliveryFields ,
57+ } from "../../utils/delivery-context.shared.js" ;
5558import { resolveCommandTurnTargetSessionKey } from "../command-turn-context.js" ;
5659import { normalizeCommandBody } from "../commands-registry.js" ;
5760import type { MsgContext , TemplateContext } from "../templating.js" ;
@@ -79,6 +82,15 @@ function loadSessionArchiveRuntime() {
7982 return sessionArchiveRuntimeLoader . load ( ) ;
8083}
8184
85+ function stripThreadFromSessionRoute ( route : SessionEntry [ "route" ] ) : SessionEntry [ "route" ] {
86+ const normalized = normalizeDeliveryChannelRoute ( route ) ;
87+ if ( ! normalized ?. thread ) {
88+ return normalized ;
89+ }
90+ const { thread : _drop , ...withoutThread } = normalized ;
91+ return Object . keys ( withoutThread ) . length > 0 ? withoutThread : undefined ;
92+ }
93+
8294type ReplySessionEndReason = Extract <
8395 PluginHookSessionEndReason ,
8496 "new" | "reset" | "idle" | "daily" | "unknown"
@@ -606,6 +618,7 @@ export async function initSessionState(params: {
606618 ( isThread ? baseEntry ?. lastThreadId : undefined ) ) ;
607619 const deliveryFields = isSystemEvent
608620 ? normalizeSessionDeliveryFields ( {
621+ route : isThread ? baseEntry ?. route : stripThreadFromSessionRoute ( baseEntry ?. route ) ,
609622 channel : baseEntry ?. channel ,
610623 lastChannel : baseEntry ?. lastChannel ,
611624 lastTo : baseEntry ?. lastTo ,
@@ -680,6 +693,7 @@ export async function initSessionState(params: {
680693 space : baseEntry ?. space ,
681694 groupActivation : entry ?. groupActivation ,
682695 groupActivationNeedsSystemIntro : entry ?. groupActivationNeedsSystemIntro ,
696+ route : deliveryFields . route ,
683697 deliveryContext : deliveryFields . deliveryContext ,
684698 // Track originating channel for subagent announce routing.
685699 lastChannel,
@@ -700,6 +714,7 @@ export async function initSessionState(params: {
700714 if ( isSystemEvent && ! isThread ) {
701715 sessionEntry = {
702716 ...sessionEntry ,
717+ route : stripThreadFromSessionRoute ( sessionEntry . route ) ,
703718 lastThreadId : undefined ,
704719 deliveryContext : stripThreadIdFromDeliveryContext ( sessionEntry . deliveryContext ) ,
705720 origin : stripThreadIdFromOrigin ( sessionEntry . origin ) ,
0 commit comments