11import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce" ;
2+ import {
3+ isSyntheticSourceReplyTurn ,
4+ resolveSourceReplyDeliveryMode ,
5+ } from "../../auto-reply/reply/source-reply-delivery-mode.js" ;
26import {
37 formatThinkingLevels ,
48 normalizeThinkLevel ,
@@ -27,6 +31,10 @@ import {
2731 resolveAgentHarnessSessionContextError ,
2832} from "../../sessions/agent-harness-session-key.js" ;
2933import { resolveUserPath } from "../../utils.js" ;
34+ import {
35+ sessionDeliveryChannel ,
36+ sessionDeliveryOrigin ,
37+ } from "../../utils/delivery-context.shared.js" ;
3038import { isDeliverableMessageChannel , resolveMessageChannel } from "../../utils/message-channel.js" ;
3139import { resolveAgentRuntimeConfig } from "../agent-runtime-config.js" ;
3240import {
@@ -37,6 +45,7 @@ import {
3745 resolveAgentWorkspaceDir ,
3846} from "../agent-scope.js" ;
3947import { DEFAULT_MODEL , DEFAULT_PROVIDER } from "../defaults.js" ;
48+ import { selectAgentHarness } from "../harness/selection.js" ;
4049import { AGENT_LANE_SUBAGENT } from "../lanes.js" ;
4150import type { ModelManifestNormalizationContext } from "../model-ref-shared.js" ;
4251import { buildConfiguredModelCatalog , resolveConfiguredModelRef } from "../model-selection.js" ;
@@ -233,7 +242,7 @@ export async function prepareAgentCommandExecution(opts: AgentCommandOpts, runti
233242 if ( explicitRecipientSession ?. error ) {
234243 throw explicitRecipientSession . error ;
235244 }
236- const commandOpts = explicitRecipientSession ?. sessionKey
245+ let commandOpts : AgentCommandOpts = explicitRecipientSession ?. sessionKey
237246 ? {
238247 ...selectedCommandOpts ,
239248 channel : explicitRecipientSession . channel ,
@@ -328,6 +337,54 @@ export async function prepareAgentCommandExecution(opts: AgentCommandOpts, runti
328337 sessionKey,
329338 sessionEntry : sessionEntryRaw ,
330339 } ) ;
340+ if (
341+ sessionEntryRaw &&
342+ commandOpts . cliSessionBindingFacts === undefined &&
343+ isSyntheticSourceReplyTurn ( {
344+ inputProvenance : commandOpts . inputProvenance ,
345+ isHeartbeat : commandOpts . bootstrapContextRunKind === "heartbeat" ,
346+ } )
347+ ) {
348+ // Lifecycle turns keep their effective delivery mode, but CLI reuse belongs
349+ // to the existing session's normal source-reply policy.
350+ const stableReplyContext = {
351+ CommandAuthorized : false ,
352+ ChatType : sessionEntryRaw . chatType ,
353+ Provider : sessionDeliveryOrigin ( sessionEntryRaw ) ?. provider ,
354+ Surface : sessionDeliveryChannel ( sessionEntryRaw ) ,
355+ InputProvenance : commandOpts . inputProvenance ,
356+ } ;
357+ const stableProvider = sessionEntryRaw . modelProvider ?? configuredModel . provider ;
358+ const stableModel = sessionEntryRaw . model ?? configuredModel . model ;
359+ const stableRuntime = resolveEffectiveAgentRuntime ( {
360+ cfg,
361+ provider : stableProvider ,
362+ modelId : stableModel ,
363+ agentId : sessionAgentId ,
364+ sessionKey,
365+ sessionEntry : sessionEntryRaw ,
366+ } ) ;
367+ const harness = selectAgentHarness ( {
368+ provider : stableProvider ,
369+ modelId : stableModel ,
370+ config : cfg ,
371+ agentId : sessionAgentId ,
372+ sessionKey,
373+ agentHarnessRuntimeOverride : stableRuntime ,
374+ } ) ;
375+ const defaultVisibleReplies =
376+ harness . deliveryDefaults ?. visibleReplies ?? harness . deliveryDefaults ?. sourceVisibleReplies ;
377+ commandOpts = {
378+ ...commandOpts ,
379+ cliSessionBindingFacts : {
380+ sourceReplyDeliveryMode : resolveSourceReplyDeliveryMode ( {
381+ cfg,
382+ ctx : stableReplyContext ,
383+ defaultVisibleReplies,
384+ } ) ,
385+ } ,
386+ } ;
387+ }
331388 const thinkingLevelsHint = formatThinkingLevels (
332389 configuredModel . provider ,
333390 configuredModel . model ,
0 commit comments