Skip to content

Commit 47640e0

Browse files
authored
fix: preserve agent delivery identity (#120108)
1 parent b47fb95 commit 47640e0

18 files changed

Lines changed: 416 additions & 104 deletions

‎src/agents/command/prepare.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
2+
import {
3+
isSyntheticSourceReplyTurn,
4+
resolveSourceReplyDeliveryMode,
5+
} from "../../auto-reply/reply/source-reply-delivery-mode.js";
26
import {
37
formatThinkingLevels,
48
normalizeThinkLevel,
@@ -27,6 +31,10 @@ import {
2731
resolveAgentHarnessSessionContextError,
2832
} from "../../sessions/agent-harness-session-key.js";
2933
import { resolveUserPath } from "../../utils.js";
34+
import {
35+
sessionDeliveryChannel,
36+
sessionDeliveryOrigin,
37+
} from "../../utils/delivery-context.shared.js";
3038
import { isDeliverableMessageChannel, resolveMessageChannel } from "../../utils/message-channel.js";
3139
import { resolveAgentRuntimeConfig } from "../agent-runtime-config.js";
3240
import {
@@ -37,6 +45,7 @@ import {
3745
resolveAgentWorkspaceDir,
3846
} from "../agent-scope.js";
3947
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "../defaults.js";
48+
import { selectAgentHarness } from "../harness/selection.js";
4049
import { AGENT_LANE_SUBAGENT } from "../lanes.js";
4150
import type { ModelManifestNormalizationContext } from "../model-ref-shared.js";
4251
import { 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,

‎src/agents/main-session-restart-dispatch.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export function resolveRestartRecoveryDeliveryContext(params: {
137137
entry: SessionEntry;
138138
includeSessionDeliveryFallback?: boolean;
139139
sessionKey: string;
140-
}): DeliveryContext | undefined {
140+
}): (DeliveryContext & { channel: string; to: string }) | undefined {
141141
const activeRunDeliveryContext = normalizeDeliveryContext(
142142
params.entry.restartRecoveryDeliveryContext,
143143
);

‎src/agents/main-session-restart-recovery-notice.ts‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
21
import type { InternalSessionEntry as SessionEntry } from "../config/sessions.js";
32
import type {
43
SessionTranscriptTurnExpectedState,
@@ -21,36 +20,24 @@ import {
2120
} from "./main-session-restart-recovery-shared.js";
2221

2322
export async function sendUnresumableSessionNotice(params: {
24-
deliveryContext: DeliveryContext;
23+
deliveryContext: DeliveryContext & { channel: string; to: string };
2524
entry: SessionEntry;
2625
gatewayRuntime: GatewayRecoveryRuntime;
2726
reason: string;
2827
sessionKey: string;
2928
text: string;
3029
}): Promise<void> {
31-
const messageParams: Record<string, unknown> = {
32-
to: params.deliveryContext.to,
33-
message: params.text,
34-
bestEffort: true,
35-
};
36-
if (params.deliveryContext.threadId != null) {
37-
messageParams.threadId = params.deliveryContext.threadId;
38-
}
39-
const actionParams: Record<string, unknown> = {
30+
const notice = {
4031
channel: params.deliveryContext.channel,
41-
action: "send",
42-
sessionKey: params.sessionKey,
43-
sessionId: params.entry.sessionId,
32+
to: params.deliveryContext.to,
33+
accountId: params.deliveryContext.accountId,
34+
threadId: params.deliveryContext.threadId,
35+
text: params.text,
4436
idempotencyKey: buildUnresumableSessionNoticeIdempotencyKey(params.entry),
45-
params: messageParams,
4637
};
47-
const accountId = normalizeOptionalString(params.deliveryContext.accountId);
48-
if (accountId) {
49-
actionParams.accountId = accountId;
50-
}
5138

5239
try {
53-
await params.gatewayRuntime.sendRecoveryNotice(actionParams, 10_000);
40+
await params.gatewayRuntime.sendRecoveryNotice(notice);
5441
log.info(
5542
`sent interrupted main session recovery notice: ${params.sessionKey} (${params.reason})`,
5643
);

0 commit comments

Comments
 (0)