Skip to content

Commit 0e792b6

Browse files
authored
refactor(channels): centralize inbound orchestration and remove internal compat (#109716)
* refactor(channels): centralize inbound turn orchestration * refactor(runtime): remove stale compatibility paths * chore(guards): reject internal deprecated API use * refactor(channels): simplify core turn planning * chore(guards): keep deprecated checks boundary-focused * refactor(memory): keep modern config off compat barrel * fix(msteams): preserve feedback learning * test(channels): align modern inbound fixtures * refactor(channels): finish modern inbound migration * refactor(channels): tighten core inbound kernel * fix(channels): preserve turn assembly narrowing * test(sdk): keep runtime mock binding immutable * test(matrix): isolate read policy runtime * test(msteams): mock canonical reply factory * test(slack): mock core inbound turn dispatch * test(telegram): inject core session recorder * test(signal): inject core session recorder * test(googlechat): assert canonical inbound routing * test(synology-chat): align core turn fixture * fix(sdk): preserve direct DM runtime compat * refactor(channels): own inbound envelope compat in core * refactor(channels): trim inbound dispatch seams * refactor(channels): remove redundant async wrappers * test(synology-chat): type canonical dispatcher mock * refactor(channels): remove remaining dead compat seams * chore(sdk): refresh API baseline after rebase * fix(channels): preserve direct DM identity metadata
1 parent 765bb37 commit 0e792b6

159 files changed

Lines changed: 3858 additions & 4306 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
cd7189431f2805258afc4ecb993fe286a2606035636a5f4055b1158a76c27d62 plugin-sdk-api-baseline.json
2-
660dbaa276792415bf7eb87c19240b19decd400b7e58745e87fe0f1f1cc33353 plugin-sdk-api-baseline.jsonl
1+
7e2ff4dedb7b220a133cb419ee91e67585b9b9f2799706f336cdea973be80bfb plugin-sdk-api-baseline.json
2+
0a85effc98fb17a65d463956704bcb2dab31076cf0fa0d85b463a765ec9dc439 plugin-sdk-api-baseline.jsonl

‎docs/channels/matrix.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ Matrix inherits global defaults from `session.threadBindings` and supports per-c
571571
- `threadBindings.idleHours`
572572
- `threadBindings.maxAgeHours`
573573
- `threadBindings.spawnSessions`: gates both subagent and ACP thread spawns.
574-
- `threadBindings.spawnSubagentSessions` / `threadBindings.spawnAcpSessions`: narrower overrides for subagent-only or ACP-only spawns.
574+
- Deprecated `threadBindings.spawnSubagentSessions` / `threadBindings.spawnAcpSessions` keys are migrated to `spawnSessions` by `openclaw doctor --fix`.
575575
- `threadBindings.defaultSpawnContext`
576576

577577
Matrix thread-bound session spawns default on. Set `threadBindings.spawnSessions: false` to block top-level `/focus` and `/acp spawn --thread auto|here` from creating/binding Matrix threads. Set `threadBindings.defaultSpawnContext: "isolated"` when native subagent thread spawns should not fork the parent transcript.

‎extensions/clickclack/src/inbound.test.ts‎

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe("handleClickClackInbound", () => {
174174
correlationId: "fakeco.case_1",
175175
});
176176

177-
expect(runtime.channel.inbound.dispatchReply).not.toHaveBeenCalled();
177+
expect(runtime.channel.inbound.dispatch).not.toHaveBeenCalled();
178178
expect(runtime.agent.runEmbeddedAgent).not.toHaveBeenCalled();
179179
const completionRequest = (runtime.llm.complete as LlmCompleteMock).mock.calls[0]?.[0];
180180
expect(completionRequest?.agentId).toBe("service-bot");
@@ -270,9 +270,9 @@ describe("handleClickClackInbound", () => {
270270
message: createMessage(),
271271
});
272272

273-
const dispatchReply = vi.mocked(runtime.channel.inbound.dispatchReply);
274-
expect(dispatchReply).toHaveBeenCalledTimes(1);
275-
expect(dispatchReply.mock.calls[0]?.[0].ctxPayload.CommandAuthorized).toBe(true);
273+
const dispatchTurn = vi.mocked(runtime.channel.inbound.dispatch);
274+
expect(dispatchTurn).toHaveBeenCalledTimes(1);
275+
expect(dispatchTurn.mock.calls[0]?.[0].ctxPayload.CommandAuthorized).toBe(true);
276276
});
277277

278278
it("propagates account toolsAllow into agent reply dispatch", async () => {
@@ -297,9 +297,9 @@ describe("handleClickClackInbound", () => {
297297
message: createMessage(),
298298
});
299299

300-
const dispatchReply = vi.mocked(runtime.channel.inbound.dispatchReply);
301-
expect(dispatchReply).toHaveBeenCalledTimes(1);
302-
const dispatchParams = dispatchReply.mock.calls[0]?.[0] as
300+
const dispatchTurn = vi.mocked(runtime.channel.inbound.dispatch);
301+
expect(dispatchTurn).toHaveBeenCalledTimes(1);
302+
const dispatchParams = dispatchTurn.mock.calls[0]?.[0] as
303303
| (Record<string, unknown> & {
304304
toolsAllow?: unknown;
305305
})
@@ -335,12 +335,12 @@ describe("handleClickClackInbound", () => {
335335
}),
336336
});
337337

338-
const dispatchReply = vi.mocked(runtime.channel.inbound.dispatchReply);
339-
expect(dispatchReply).toHaveBeenCalledTimes(2);
340-
const withoutOptIn = dispatchReply.mock.calls[0]?.[0] as {
338+
const dispatchTurn = vi.mocked(runtime.channel.inbound.dispatch);
339+
expect(dispatchTurn).toHaveBeenCalledTimes(2);
340+
const withoutOptIn = dispatchTurn.mock.calls[0]?.[0] as {
341341
replyOptions?: { runId?: unknown; onItemEvent?: unknown; onModelSelected?: unknown };
342342
};
343-
const withOptIn = dispatchReply.mock.calls[1]?.[0] as {
343+
const withOptIn = dispatchTurn.mock.calls[1]?.[0] as {
344344
replyOptions?: {
345345
onItemEvent?: unknown;
346346
onModelSelected?: unknown;
@@ -377,7 +377,7 @@ describe("handleClickClackInbound", () => {
377377
correlationId: "fakeco.case_2",
378378
});
379379

380-
const dispatchParams = vi.mocked(runtime.channel.inbound.dispatchReply).mock.calls[0]?.[0];
380+
const dispatchParams = vi.mocked(runtime.channel.inbound.dispatch).mock.calls[0]?.[0];
381381
expect(dispatchParams?.replyOptions?.runId).toBe(`clickclack:${VALID_MESSAGE_ID}`);
382382

383383
await dispatchParams?.delivery.deliver({ text: "correlated reply" }, {} as never);
@@ -404,7 +404,7 @@ describe("handleClickClackInbound", () => {
404404
}),
405405
});
406406

407-
const delivery = vi.mocked(runtime.channel.inbound.dispatchReply).mock.calls[0]?.[0].delivery;
407+
const delivery = vi.mocked(runtime.channel.inbound.dispatch).mock.calls[0]?.[0].delivery;
408408
if (typeof delivery?.durable !== "function") {
409409
throw new Error("expected ClickClack media durable delivery resolver");
410410
}
@@ -437,7 +437,7 @@ describe("handleClickClackInbound", () => {
437437
message: createMessage({ id: "msg_invalid" }),
438438
});
439439

440-
expect(vi.mocked(runtime.channel.inbound.dispatchReply).mock.calls[0]?.[0].replyOptions).toBe(
440+
expect(vi.mocked(runtime.channel.inbound.dispatch).mock.calls[0]?.[0].replyOptions).toBe(
441441
undefined,
442442
);
443443
});
@@ -461,15 +461,15 @@ describe("handleClickClackInbound", () => {
461461
}),
462462
config: cfg,
463463
message: createMessage({
464-
channel_id: undefined,
464+
channel_id: "",
465465
direct_conversation_id: "dcn_1",
466466
}),
467467
});
468468

469-
const dispatchReply = vi.mocked(runtime.channel.inbound.dispatchReply);
470-
expect(dispatchReply).toHaveBeenCalledTimes(1);
471-
expect(dispatchReply.mock.calls[0]?.[0].ctxPayload.ChatType).toBe("direct");
472-
expect(dispatchReply.mock.calls[0]?.[0].ctxPayload.CommandAuthorized).toBe(true);
469+
const dispatchTurn = vi.mocked(runtime.channel.inbound.dispatch);
470+
expect(dispatchTurn).toHaveBeenCalledTimes(1);
471+
expect(dispatchTurn.mock.calls[0]?.[0].ctxPayload.ChatType).toBe("direct");
472+
expect(dispatchTurn.mock.calls[0]?.[0].ctxPayload.CommandAuthorized).toBe(true);
473473
});
474474

475475
it("preserves session policy when an account overrides the routed agent", async () => {
@@ -503,8 +503,8 @@ describe("handleClickClackInbound", () => {
503503
}),
504504
});
505505

506-
const dispatchReply = vi.mocked(runtime.channel.inbound.dispatchReply);
507-
expect(dispatchReply.mock.calls[0]?.[0].routeSessionKey).toBe(
506+
const dispatchTurn = vi.mocked(runtime.channel.inbound.dispatch);
507+
expect(dispatchTurn.mock.calls[0]?.[0].route.sessionKey).toBe(
508508
"agent:service-bot:clickclack:direct:alice",
509509
);
510510
expect(runtime.channel.routing.buildAgentSessionKey).toHaveBeenCalledWith({
@@ -546,10 +546,12 @@ describe("handleClickClackInbound", () => {
546546
}),
547547
});
548548

549-
const dispatchReply = vi.mocked(runtime.channel.inbound.dispatchReply);
550-
expect(dispatchReply.mock.calls[0]?.[0]).toMatchObject({
551-
agentId: "service-bot",
552-
routeSessionKey: "agent:service-bot:clickclack:default:direct:dm:usr_owner",
549+
const dispatchTurn = vi.mocked(runtime.channel.inbound.dispatch);
550+
expect(dispatchTurn.mock.calls[0]?.[0]).toMatchObject({
551+
route: {
552+
agentId: "service-bot",
553+
sessionKey: "agent:service-bot:clickclack:default:direct:dm:usr_owner",
554+
},
553555
});
554556
});
555557

@@ -584,7 +586,7 @@ describe("handleClickClackInbound", () => {
584586
}),
585587
});
586588

587-
expect(runtime.channel.inbound.dispatchReply).not.toHaveBeenCalled();
589+
expect(runtime.channel.inbound.dispatch).not.toHaveBeenCalled();
588590
expect(runtime.channel.reply.dispatchReplyWithBufferedBlockDispatcher).not.toHaveBeenCalled();
589591
});
590592
});

‎extensions/clickclack/src/inbound.ts‎

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createChannelInboundEnvelopeBuilder } from "openclaw/plugin-sdk/channel-inbound";
12
import { deriveDurableFinalDeliveryRequirements } from "openclaw/plugin-sdk/channel-outbound";
23
/**
34
* Converts authorized ClickClack messages into OpenClaw agent/model replies and
@@ -150,6 +151,10 @@ export async function handleClickClackInbound(params: {
150151
if (!access.shouldDispatch) {
151152
return;
152153
}
154+
const conversationId = message.channel_id || message.direct_conversation_id;
155+
if (!conversationId) {
156+
return;
157+
}
153158
const isDirect = Boolean(message.direct_conversation_id);
154159
const target = buildClickClackTarget(
155160
isDirect
@@ -200,52 +205,53 @@ export async function handleClickClackInbound(params: {
200205
});
201206
}
202207
const senderName = message.author?.display_name || message.author_id;
203-
const previousTimestamp = runtime.channel.session.readSessionUpdatedAt({
204-
storePath: runtime.channel.session.resolveStorePath(params.config.session?.store, {
205-
agentId: route.agentId,
206-
}),
207-
sessionKey: route.sessionKey,
208-
});
209208
// Preserve both normalized channel fields and ClickClack-native ids so reply
210209
// routing, session recovery, and command authorization see the same message.
211-
const body = runtime.channel.reply.formatAgentEnvelope({
210+
const body = createChannelInboundEnvelopeBuilder({
211+
cfg: params.config as OpenClawConfig,
212+
route,
213+
})({
212214
channel: "ClickClack",
213215
from: senderName,
214216
timestamp: new Date(message.created_at),
215-
previousTimestamp,
216-
envelope: runtime.channel.reply.resolveEnvelopeFormatOptions(params.config as OpenClawConfig),
217217
body: message.body,
218218
});
219-
const storePath = runtime.channel.session.resolveStorePath(params.config.session?.store, {
220-
agentId: route.agentId,
221-
});
222-
const ctxPayload = runtime.channel.reply.finalizeInboundContext({
223-
Body: body,
224-
BodyForAgent: message.body,
225-
RawBody: message.body,
226-
CommandBody: message.body,
227-
From: target,
228-
To: target,
229-
SessionKey: route.sessionKey,
230-
AccountId: route.accountId ?? params.account.accountId,
231-
ChatType: isDirect ? "direct" : "group",
232-
WasMentioned: isDirect ? undefined : true,
233-
ConversationLabel: isDirect ? senderName : message.channel_id,
234-
GroupChannel: message.channel_id,
235-
NativeChannelId: message.channel_id || message.direct_conversation_id,
236-
MessageThreadId: message.parent_message_id ? message.thread_root_id : undefined,
237-
ThreadParentId: message.parent_message_id ? message.thread_root_id : undefined,
238-
SenderName: senderName,
239-
SenderId: message.author_id,
240-
Provider: CHANNEL_ID,
241-
Surface: CHANNEL_ID,
242-
MessageSid: message.id,
243-
MessageSidFull: message.id,
244-
ReplyToId: message.id,
245-
Timestamp: message.created_at,
246-
OriginatingChannel: CHANNEL_ID,
247-
OriginatingTo: target,
248-
CommandAuthorized: access.commandAuthorized,
219+
const ctxPayload = runtime.channel.inbound.buildContext({
220+
channel: CHANNEL_ID,
221+
accountId: route.accountId ?? params.account.accountId,
222+
messageId: message.id,
223+
messageIdFull: message.id,
224+
timestamp: new Date(message.created_at).getTime(),
225+
from: target,
226+
sender: { id: message.author_id, name: senderName },
227+
conversation: {
228+
kind: isDirect ? "direct" : "group",
229+
id: conversationId,
230+
label: isDirect ? senderName : message.channel_id,
231+
threadId: message.parent_message_id ? message.thread_root_id : undefined,
232+
nativeChannelId: conversationId,
233+
},
234+
route: {
235+
agentId: route.agentId,
236+
accountId: route.accountId,
237+
routeSessionKey: route.sessionKey,
238+
},
239+
reply: {
240+
to: target,
241+
originatingTo: target,
242+
replyToId: message.id,
243+
messageThreadId: message.parent_message_id ? message.thread_root_id : undefined,
244+
threadParentId: message.parent_message_id ? message.thread_root_id : undefined,
245+
},
246+
message: { body, bodyForAgent: message.body, rawBody: message.body, commandBody: message.body },
247+
access: {
248+
commands: { authorized: access.commandAuthorized },
249+
mentions: {
250+
canDetectMention: !isDirect,
251+
wasMentioned: !isDirect,
252+
},
253+
},
254+
extra: { GroupChannel: message.channel_id },
249255
});
250256
const runId = resolveClickClackAgentRunId(message.id);
251257
const activityReplyOptions = activity
@@ -266,17 +272,12 @@ export async function handleClickClackInbound(params: {
266272
allowProgressCallbacksWhenSourceDeliverySuppressed: true,
267273
}
268274
: undefined;
269-
const dispatchPromise = runtime.channel.inbound.dispatchReply({
275+
const dispatchPromise = runtime.channel.inbound.dispatch({
270276
cfg: params.config as OpenClawConfig,
271277
channel: CHANNEL_ID,
272278
accountId: params.account.accountId,
273-
agentId: route.agentId,
274-
routeSessionKey: route.sessionKey,
275-
storePath,
279+
route: { agentId: route.agentId, sessionKey: route.sessionKey },
276280
ctxPayload,
277-
recordInboundSession: runtime.channel.session.recordInboundSession,
278-
dispatchReplyWithBufferedBlockDispatcher:
279-
runtime.channel.reply.dispatchReplyWithBufferedBlockDispatcher,
280281
toolsAllow: params.account.toolsAllow,
281282
// Provenance stamping shares the agentActivity opt-in: with the flag off
282283
// the extension's wire payloads stay byte-identical to pre-activity

‎extensions/codex/harness.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function createCodexAppServerAgentHarness(options: {
6666
delegatedExecutionPluginIds: ["voice-call"],
6767
contextEngineHostCapabilities: CODEX_APP_SERVER_CONTEXT_ENGINE_HOST_CAPABILITIES,
6868
deliveryDefaults: {
69-
sourceVisibleReplies: "message_tool",
69+
visibleReplies: "message_tool",
7070
},
7171
authBootstrap: "harness",
7272
authBinding: {

‎extensions/codex/index.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe("codex plugin", () => {
121121
expect(agentHarnessRegistration.id).toBe("codex");
122122
expect(agentHarnessRegistration.label).toBe("Codex agent harness");
123123
expect(agentHarnessRegistration.deliveryDefaults).toEqual({
124-
sourceVisibleReplies: "message_tool",
124+
visibleReplies: "message_tool",
125125
});
126126
expect(typeof agentHarnessRegistration.dispose).toBe("function");
127127
expect(typeof agentHarnessRegistration.fetchUsageSnapshot).toBe("function");
@@ -419,7 +419,7 @@ describe("codex plugin", () => {
419419
bindingStore: testCodexAppServerBindingStore,
420420
});
421421

422-
expect(harness.deliveryDefaults?.sourceVisibleReplies).toBe("message_tool");
422+
expect(harness.deliveryDefaults?.visibleReplies).toBe("message_tool");
423423
expect(
424424
harness.supports({ provider: "codex", modelId: "gpt-5.4", requestedRuntime: "auto" })
425425
.supported,

‎extensions/codex/src/app-server/run-attempt-lifecycle-controller.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,7 @@ export function createCodexAttemptLifecycleController(
153153
startedAt: attemptStartedAt,
154154
endedAt: Date.now(),
155155
...data,
156-
...((params.deferTerminalLifecycle ?? params.deferTerminalLifecycleEnd)
157-
? { phase: "finishing" }
158-
: {}),
156+
...(params.deferTerminalLifecycle ? { phase: "finishing" } : {}),
159157
},
160158
});
161159
state.lifecycleTerminalEmitted = true;

‎extensions/codex/src/app-server/run-attempt.hooks.test.ts‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,15 @@ setupRunAttemptTestHooks();
5151

5252
describe("runCodexAppServerAttempt hooks and model diagnostics", () => {
5353
it.each([
54-
{ label: "completed", status: "completed" as const, error: undefined, legacy: false },
55-
{ label: "failed", status: "failed" as const, error: "codex exploded", legacy: false },
56-
{
57-
label: "completed legacy alias",
58-
status: "completed" as const,
59-
error: undefined,
60-
legacy: true,
61-
},
62-
])("defers $label lifecycle terminal ownership", async ({ status, error, legacy }) => {
54+
{ label: "completed", status: "completed" as const, error: undefined },
55+
{ label: "failed", status: "failed" as const, error: "codex exploded" },
56+
])("defers $label lifecycle terminal ownership", async ({ status, error }) => {
6357
const onRunAgentEvent = vi.fn();
6458
const sessionFile = path.join(tempDir, `deferred-${status}.jsonl`);
6559
const workspaceDir = path.join(tempDir, `workspace-${status}`);
6660
const harness = createStartedThreadHarness();
6761
const params = createParams(sessionFile, workspaceDir);
68-
if (legacy) {
69-
params.deferTerminalLifecycleEnd = true;
70-
} else {
71-
params.deferTerminalLifecycle = true;
72-
}
62+
params.deferTerminalLifecycle = true;
7363
params.onAgentEvent = onRunAgentEvent;
7464
const run = runCodexAppServerAttempt(params);
7565
await harness.waitForMethod("turn/start");

‎extensions/discord/src/actions/handle-action.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { resolveReactionMessageId } from "openclaw/plugin-sdk/channel-actions";
1010
import type { ChannelMessageActionContext } from "openclaw/plugin-sdk/channel-contract";
1111
import {
1212
adaptMessagePresentationForChannel,
13-
normalizeInteractiveReply,
13+
normalizeLegacyInteractiveReply,
1414
normalizeMessagePresentation,
1515
renderMessagePresentationFallbackText,
1616
} from "openclaw/plugin-sdk/interactive-runtime";
@@ -153,7 +153,7 @@ export async function handleDiscordMessageAction(
153153
? undefined
154154
: (params.components ??
155155
presentationComponents ??
156-
buildDiscordInteractiveComponents(normalizeInteractiveReply(params.interactive)));
156+
buildDiscordInteractiveComponents(normalizeLegacyInteractiveReply(params.interactive)));
157157
const hasComponents =
158158
Boolean(rawComponents) &&
159159
(typeof rawComponents === "function" || typeof rawComponents === "object");

‎extensions/discord/src/monitor/agent-components.dispatch.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,9 @@ export async function dispatchDiscordComponentEvent(params: {
178178

179179
const {
180180
createReplyReferencePlanner,
181-
dispatchReplyWithBufferedBlockDispatcher,
182181
finalizeInboundContext,
183182
resolveChunkMode,
184183
resolveTextChunkLimit,
185-
recordInboundSession,
186184
} = await (async () => {
187185
const conversationRuntime = await loadConversationRuntime();
188186
return {
@@ -273,12 +271,8 @@ export async function dispatchDiscordComponentEvent(params: {
273271
cfg: ctx.cfg,
274272
channel: "discord",
275273
accountId,
276-
agentId,
277-
routeSessionKey: sessionKey,
278-
storePath,
274+
route: { agentId, sessionKey },
279275
ctxPayload,
280-
recordInboundSession,
281-
dispatchReplyWithBufferedBlockDispatcher,
282276
record: {
283277
updateLastRoute: interactionCtx.isDirectMessage
284278
? {

0 commit comments

Comments
 (0)