1+ import { createHash } from "node:crypto" ;
12import { sendDurableMessageBatch } from "../channels/message/runtime.js" ;
23import type { CliDeps } from "../cli/deps.types.js" ;
34import { createOutboundSendDeps } from "../cli/outbound-send-deps.js" ;
@@ -6,6 +7,7 @@ import { formatErrorMessage } from "../infra/errors.js";
67import { resolveAgentOutboundIdentity } from "../infra/outbound/identity.js" ;
78import { buildOutboundSessionContext } from "../infra/outbound/session-context.js" ;
89import { getChildLogger } from "../logging.js" ;
10+ import { parseAgentSessionKey , parseThreadSessionSuffix } from "../routing/session-key.js" ;
911import {
1012 resolveFailureDestination ,
1113 type CronFailureDeliveryPlan ,
@@ -30,6 +32,7 @@ export {
3032
3133const FAILURE_NOTIFICATION_TIMEOUT_MS = 30_000 ;
3234const cronDeliveryLogger = getChildLogger ( { subsystem : "cron-delivery" } ) ;
35+ const ROUTED_PEER_SESSION_KINDS = new Set ( [ "direct" , "dm" , "group" , "channel" ] ) ;
3336
3437export type CronAnnounceTarget = {
3538 channel ?: string ;
@@ -40,6 +43,48 @@ export type CronAnnounceTarget = {
4043
4144type SuccessfulDeliveryTarget = Extract < DeliveryTargetResolution , { ok : true } > ;
4245
46+ function buildCronAnnounceMirrorIdempotencyKey ( params : {
47+ jobId : string ;
48+ target : SuccessfulDeliveryTarget ;
49+ message : string ;
50+ } ) : string {
51+ const digest = createHash ( "sha256" )
52+ . update (
53+ JSON . stringify ( [
54+ params . target . channel ,
55+ params . target . accountId ?? "" ,
56+ params . target . to ,
57+ params . target . threadId ?? "" ,
58+ params . message ,
59+ ] ) ,
60+ )
61+ . digest ( "hex" )
62+ . slice ( 0 , 24 ) ;
63+ return `cron-announce-mirror:v1:${ params . jobId } :${ digest } ` ;
64+ }
65+
66+ function isRoutedPeerSessionKey ( sessionKey : string ) : boolean {
67+ const { baseSessionKey } = parseThreadSessionSuffix ( sessionKey ) ;
68+ const parsed = parseAgentSessionKey ( baseSessionKey ?? sessionKey ) ;
69+ if ( ! parsed ) {
70+ return false ;
71+ }
72+
73+ const parts = parsed . rest . split ( ":" ) . filter ( Boolean ) ;
74+ const peerKind = parts . length >= 2 ? parts [ parts . length - 2 ] : undefined ;
75+ return typeof peerKind === "string" && ROUTED_PEER_SESSION_KINDS . has ( peerKind ) ;
76+ }
77+
78+ function resolveCronAnnounceMirrorSessionKey ( sessionKey : string | undefined ) : string | undefined {
79+ const trimmed = sessionKey ?. trim ( ) ;
80+ if ( ! trimmed ) {
81+ return undefined ;
82+ }
83+ // Routed peer sessions can be active embedded conversations. Mirror only
84+ // into custom/main session keys here so cron delivery does not race locks.
85+ return isRoutedPeerSessionKey ( trimmed ) ? undefined : trimmed ;
86+ }
87+
4388async function resolveCronAnnounceDelivery ( params : {
4489 cfg : OpenClawConfig ;
4590 agentId : string ;
@@ -51,6 +96,7 @@ async function resolveCronAnnounceDelivery(params: {
5196 resolvedTarget : SuccessfulDeliveryTarget ;
5297 session : ReturnType < typeof buildOutboundSessionContext > ;
5398 identity : ReturnType < typeof resolveAgentOutboundIdentity > ;
99+ mirrorSessionKey ?: string ;
54100 }
55101 | { ok : false ; error : Error }
56102> {
@@ -74,12 +120,14 @@ async function resolveCronAnnounceDelivery(params: {
74120 sessionKey : params . target . sessionKey ,
75121 } ) ,
76122 } ) ;
123+ const mirrorSessionKey = resolveCronAnnounceMirrorSessionKey ( params . target . sessionKey ) ;
77124
78125 return {
79126 ok : true ,
80127 resolvedTarget,
81128 session,
82129 identity,
130+ ...( mirrorSessionKey ? { mirrorSessionKey } : { } ) ,
83131 } ;
84132}
85133
@@ -90,7 +138,10 @@ async function deliverCronAnnouncePayload(params: {
90138 resolvedTarget : SuccessfulDeliveryTarget ;
91139 session : ReturnType < typeof buildOutboundSessionContext > ;
92140 identity : ReturnType < typeof resolveAgentOutboundIdentity > ;
141+ mirrorSessionKey ?: string ;
93142 } ;
143+ agentId : string ;
144+ jobId : string ;
94145 message : string ;
95146 abortSignal : AbortSignal ;
96147} ) : Promise < void > {
@@ -106,6 +157,18 @@ async function deliverCronAnnouncePayload(params: {
106157 bestEffort : false ,
107158 deps : createOutboundSendDeps ( params . deps ) ,
108159 signal : params . abortSignal ,
160+ mirror : params . delivery . mirrorSessionKey
161+ ? {
162+ sessionKey : params . delivery . mirrorSessionKey ,
163+ agentId : params . agentId ,
164+ text : params . message ,
165+ idempotencyKey : buildCronAnnounceMirrorIdempotencyKey ( {
166+ jobId : params . jobId ,
167+ target : params . delivery . resolvedTarget ,
168+ message : params . message ,
169+ } ) ,
170+ }
171+ : undefined ,
109172 } ) ;
110173 if ( send . status === "failed" || send . status === "partial_failed" ) {
111174 throw send . error ;
@@ -129,6 +192,8 @@ export async function sendCronAnnouncePayloadStrict(params: {
129192 deps : params . deps ,
130193 cfg : params . cfg ,
131194 delivery,
195+ agentId : params . agentId ,
196+ jobId : params . jobId ,
132197 message : params . message ,
133198 abortSignal : params . abortSignal ,
134199 } ) ;
@@ -162,6 +227,8 @@ export async function sendFailureNotificationAnnounce(
162227 deps,
163228 cfg,
164229 delivery,
230+ agentId,
231+ jobId,
165232 message,
166233 abortSignal : abortController . signal ,
167234 } ) ;
0 commit comments