Skip to content

Commit 793669c

Browse files
authored
fix: targetless replies retain conversation correlation (#125283)
* fix(agents): preserve contextual reply correlation Carry prepared reply policy into embedded and CLI message tools so targetless direct replies retain transport correlation and Reef thread identity.\n\nCloses #125279 * fix(reef): preserve unthreaded reply correlation Keep reply-only inbound messages unthreaded unless the transport supplies a thread, while still anchoring new exchanges to their initiating envelope.\n\nRefs #125279
1 parent eb38d5e commit 793669c

16 files changed

Lines changed: 169 additions & 7 deletions

‎extensions/reef/src/channel.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe("Reef inbound dispatch content", () => {
4444
ReefProvenance: "Untrusted third-party data from @clanky's agent.",
4545
ReefEnvelopeId: "message-1",
4646
SenderIsBot: true,
47+
MessageThreadId: "message-1",
4748
},
4849
});
4950
});
@@ -66,6 +67,70 @@ describe("Reef inbound dispatch content", () => {
6667
MessageThreadId: "thread-1",
6768
});
6869
});
70+
71+
it("does not invent a thread for an explicitly correlated unthreaded reply", () => {
72+
const content = resolveReefInboundDispatchContent({
73+
id: "message-2",
74+
peer: "clanky",
75+
text: "correlated reply",
76+
provenance: "Untrusted third-party data from @clanky's agent.",
77+
autonomy: "bounded",
78+
replyTo: "message-1",
79+
});
80+
81+
expect(content.extraContext).toMatchObject({
82+
ReplyToId: "message-1",
83+
ReplyToIdFull: "message-1",
84+
});
85+
expect(content.extraContext).not.toHaveProperty("MessageThreadId");
86+
});
87+
});
88+
89+
describe("Reef message-tool threading", () => {
90+
it("keeps contextual replies on the inbound message thread", () => {
91+
const threading = reefPlugin.threading;
92+
if (!threading?.buildToolContext || !threading.resolveAutoThreadId) {
93+
throw new Error("expected Reef threading adapter");
94+
}
95+
const toolContext = threading.buildToolContext({
96+
cfg: {},
97+
accountId: "default",
98+
context: {
99+
Channel: "reef",
100+
To: "reef:remote-agent",
101+
ChatType: "direct",
102+
CurrentMessageId: "message-1",
103+
ReplyToMode: "all",
104+
MessageThreadId: "message-1",
105+
},
106+
});
107+
108+
expect(toolContext).toMatchObject({
109+
currentChannelId: "reef:remote-agent",
110+
currentMessagingTarget: "reef:remote-agent",
111+
currentMessageId: "message-1",
112+
currentThreadTs: "message-1",
113+
replyToMode: "all",
114+
});
115+
expect(
116+
threading.resolveAutoThreadId({
117+
cfg: {},
118+
accountId: "default",
119+
to: "@remote-agent",
120+
toolContext,
121+
replyToId: "message-1",
122+
}),
123+
).toBe("message-1");
124+
expect(
125+
threading.resolveAutoThreadId({
126+
cfg: {},
127+
accountId: "default",
128+
to: "reef:another-agent",
129+
toolContext,
130+
replyToId: "message-1",
131+
}),
132+
).toBeUndefined();
133+
});
69134
});
70135

71136
describe("Reef conversation directory", () => {

‎extensions/reef/src/channel.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ChannelThreadingToolContext } from "openclaw/plugin-sdk/channel-contract";
12
import {
23
dispatchInboundDirectDm,
34
recordChannelBotPairLoopAndCheckSuppression,
@@ -94,6 +95,12 @@ function replyText(payload: unknown): string {
9495
: "";
9596
}
9697

98+
function matchesReefToolTarget(target: string, toolContext?: ChannelThreadingToolContext): boolean {
99+
const currentTarget = toolContext?.currentMessagingTarget ?? toolContext?.currentChannelId;
100+
const normalizedCurrent = normalizeReefTarget(currentTarget ?? "");
101+
return normalizedCurrent !== undefined && normalizeReefTarget(target) === normalizedCurrent;
102+
}
103+
97104
export const reefPlugin: ChannelPlugin<ReefAccount> = {
98105
id: "reef",
99106
meta: {
@@ -168,6 +175,26 @@ export const reefPlugin: ChannelPlugin<ReefAccount> = {
168175
: null;
169176
},
170177
},
178+
threading: {
179+
threadAddressing: "message",
180+
matchesToolContextTarget: ({ target, toolContext }) =>
181+
matchesReefToolTarget(target, toolContext),
182+
resolveReplyToMode: () => "all",
183+
buildToolContext: ({ context, hasRepliedRef }) => {
184+
const currentTarget = context.To?.trim() || undefined;
185+
return {
186+
currentChannelId: currentTarget,
187+
currentMessagingTarget: currentTarget,
188+
currentMessageId: context.CurrentMessageId,
189+
currentThreadTs:
190+
context.MessageThreadId != null ? String(context.MessageThreadId) : undefined,
191+
replyToMode: context.ReplyToMode ?? "all",
192+
hasRepliedRef,
193+
};
194+
},
195+
resolveAutoThreadId: ({ to, toolContext }) =>
196+
matchesReefToolTarget(to, toolContext) ? toolContext?.currentThreadTs : undefined,
197+
},
171198
directory: createChannelDirectoryAdapter({
172199
listPeers: async ({ cfg, query, limit }) =>
173200
listTrustedPeerDirectoryEntries({

‎extensions/reef/src/inbound.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { ReefIngressMessage } from "./types.js";
22

33
export function resolveReefInboundDispatchContent(message: ReefIngressMessage) {
4+
// Reef promotes a new unthreaded exchange to its initiating envelope id.
5+
// A reply with no thread stays unthreaded so replyTo remains the sole correlation fact.
6+
const threadId = message.thread ?? (message.replyTo ? undefined : message.id);
47
return {
58
rawBody: message.text,
69
extraContext: {
@@ -9,7 +12,7 @@ export function resolveReefInboundDispatchContent(message: ReefIngressMessage) {
912
ReefEnvelopeId: message.id,
1013
SenderIsBot: true,
1114
...(message.replyTo ? { ReplyToId: message.replyTo, ReplyToIdFull: message.replyTo } : {}),
12-
...(message.thread ? { MessageThreadId: message.thread } : {}),
15+
...(threadId ? { MessageThreadId: threadId } : {}),
1316
},
1417
};
1518
}

‎src/agents/cli-runner/execute-tool-tracking.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ export function createCliToolTracking(context: PreparedCliRunContext) {
316316
currentChannelId: context.params.currentChannelId,
317317
currentThreadTs: context.params.currentThreadTs,
318318
currentMessageId: context.params.currentMessageId,
319+
replyToMode: context.params.replyToMode,
319320
},
320321
},
321322
call.args,

‎src/agents/cli-runner/mcp-grant-context.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ describe("buildCliMcpGrantContext source-reply authority", () => {
3737
expect(buildGrant({ modelHasVision: true }).modelHasVision).toBe(true);
3838
});
3939

40+
it("carries the prepared reply mode into loopback message tools", () => {
41+
expect(buildGrant({ replyToMode: "all" }).replyToMode).toBe("all");
42+
});
43+
4044
it.each([
4145
{ label: "the provider", overrides: { messageProvider: undefined } },
4246
{ label: "the destination", overrides: { currentChannelId: undefined } },

‎src/agents/cli-runner/mcp-grant-context.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export function buildCliMcpGrantContext(params: {
160160
params.run.currentMessageId == null
161161
? undefined
162162
: normalizeOptionalMcpContextValue(String(params.run.currentMessageId)),
163+
replyToMode: params.run.replyToMode,
163164
currentInboundAudio: params.run.currentInboundAudio === true ? true : undefined,
164165
accountId: normalizeOptionalMcpContextValue(params.run.agentAccountId),
165166
inboundEventKind: params.run.currentInboundEventKind,

‎src/agents/cli-runner/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ export type RunCliAgentParams = {
208208
channelContext?: PluginHookChannelContext;
209209
currentThreadTs?: string;
210210
currentMessageId?: string | number;
211+
replyToMode?: "off" | "first" | "all" | "batched";
211212
currentInboundAudio?: boolean;
212213
agentAccountId?: string;
213214
/** Sender identity for channel-originated runs when available. */

‎src/auto-reply/reply/agent-runner-cli-candidate.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ export async function runCliFallbackCandidate(params: {
448448
channelContext: turn.followupRun.run.channelContext,
449449
currentThreadTs: cliCurrentThreadId != null ? String(cliCurrentThreadId) : undefined,
450450
currentMessageId: cliCurrentMessageId,
451+
replyToMode: turn.followupRun.originatingReplyToMode ?? turn.sessionCtx.ReplyToMode,
451452
currentInboundAudio: hasInboundAudio(turn.sessionCtx),
452453
agentAccountId: turn.followupRun.run.agentAccountId,
453454
senderIsOwner: turn.followupRun.run.senderIsOwner,

‎src/auto-reply/reply/agent-runner-run.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,14 @@ export async function runReplyAgent(
351351
originatingChannel: sessionCtx.OriginatingChannel,
352352
provider: sessionCtx.Surface ?? sessionCtx.Provider,
353353
}) as OriginatingChannelType | undefined;
354-
const replyToMode = resolveReplyToMode(
355-
followupRun.run.config,
356-
replyToChannel,
357-
sessionCtx.AccountId,
358-
sessionCtx.ChatType,
359-
);
354+
const replyToMode =
355+
followupRun.originatingReplyToMode ??
356+
resolveReplyToMode(
357+
followupRun.run.config,
358+
replyToChannel,
359+
sessionCtx.AccountId,
360+
sessionCtx.ChatType,
361+
);
360362
const applyReplyToMode = createReplyToModeFilterForChannel(replyToMode, replyToChannel);
361363
const cfg = followupRun.run.config;
362364
const replyMediaContext = createReplyMediaContext({

‎src/auto-reply/reply/agent-runner-utils.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,39 @@ describe("agent-runner-utils", () => {
445445
expect(resolved.embeddedContext.replyToMode).toBe("off");
446446
});
447447

448+
it("carries a prepared direct-message reply mode into generic message tools", () => {
449+
const run = makeRun();
450+
const replyRoute = {
451+
originatingChannel: "reef",
452+
originatingTo: "reef:remote-agent",
453+
originatingReplyToMode: "all",
454+
} satisfies Pick<
455+
FollowupRun,
456+
"originatingChannel" | "originatingTo" | "originatingReplyToMode"
457+
>;
458+
459+
const resolved = buildEmbeddedRunExecutionParams({
460+
run,
461+
replyRoute,
462+
sessionCtx: {
463+
Provider: "reef",
464+
To: "reef:local-agent",
465+
MessageSid: "message-1",
466+
},
467+
hasRepliedRef: undefined,
468+
provider: "openai",
469+
model: "gpt-4.1-mini",
470+
runId: "run-1",
471+
});
472+
473+
expect(resolved.embeddedContext).toMatchObject({
474+
currentChannelId: "reef:remote-agent",
475+
currentChannelProvider: "reef",
476+
currentMessageId: "message-1",
477+
replyToMode: "all",
478+
});
479+
});
480+
448481
it("carries inbound audio context into embedded message tools", () => {
449482
const run = makeRun();
450483

0 commit comments

Comments
 (0)