Skip to content

Commit 601ffa2

Browse files
authored
refactor(channels): move inbound turn execution into core (#110095)
* refactor(channels): centralize turn execution lifecycle * refactor(extensions): adopt core channel turn plans * test(channels): ratchet modern dispatch ownership * chore(channels): drop stale max-lines exemption * fix(channels): allow async delivery error cleanup * test(channels): await async delivery cleanup * test(channels): await delivery error callbacks * fix(channels): preserve delivery error contract * fix(plugin-sdk): preserve channel inbound declarations * test(channels): exercise core delivery plans * fix(channels): await delivery error cleanup * fix(channels): preserve async cleanup contract * refactor(channels): trim dispatch boilerplate * fix(channels): mask observe-only dispatch results * refactor(channels): delete unused dispatch exports * fix(msteams): preserve resolver config overrides * refactor(channels): keep Teams turn config internal * test(plugins): drop retired Teams runtime export guard
1 parent 9a32315 commit 601ffa2

63 files changed

Lines changed: 2673 additions & 2495 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.

‎config/max-lines-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,6 @@ src/channels/plugins/setup-wizard-helpers.ts
640640
src/channels/plugins/types.adapters.ts
641641
src/channels/streaming.ts
642642
src/channels/turn/kernel.test.ts
643-
src/channels/turn/kernel.ts
644643
src/cli/capability-cli.test.ts
645644
src/cli/command-secret-gateway.test.ts
646645
src/cli/command-secret-gateway.ts

‎extensions/feishu/src/bot.broadcast.test.ts‎

Lines changed: 36 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,20 @@ const {
1010
builtInboundContextCalls,
1111
mockCreateFeishuReplyDispatcher,
1212
mockCreateFeishuClient,
13-
mockDispatchInboundMessage,
13+
mockDispatchReply,
1414
mockRecordInboundSession,
1515
mockResolveAgentRoute,
1616
mockResolveStorePath,
1717
} = vi.hoisted(() => ({
1818
builtInboundContextCalls: [] as Array<Record<string, unknown>>,
1919
mockCreateFeishuReplyDispatcher: vi.fn((_params?: unknown) => ({
20-
dispatcher: {
21-
sendToolResult: vi.fn(),
22-
sendBlockReply: vi.fn(),
23-
sendFinalReply: vi.fn(),
24-
waitForIdle: vi.fn(),
25-
getQueuedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
26-
getFailedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
27-
markComplete: vi.fn(),
28-
},
20+
dispatcherOptions: {},
21+
delivery: { deliver: vi.fn(async () => undefined) },
2922
replyOptions: {},
30-
markDispatchIdle: vi.fn(),
3123
ensureNoVisibleReplyFallback: vi.fn(),
3224
})),
3325
mockCreateFeishuClient: vi.fn(),
34-
mockDispatchInboundMessage: vi
35-
.fn()
36-
.mockResolvedValue({ queuedFinal: false, counts: { final: 1 } }),
26+
mockDispatchReply: vi.fn().mockResolvedValue({ queuedFinal: false, counts: { final: 1 } }),
3727
mockRecordInboundSession: vi.fn().mockResolvedValue(undefined),
3828
mockResolveAgentRoute: vi.fn(),
3929
mockResolveStorePath: vi.fn(() => "/tmp/feishu-session-store.json"),
@@ -58,13 +48,6 @@ vi.mock("openclaw/plugin-sdk/channel-inbound", async () => {
5848
};
5949
});
6050

61-
vi.mock("openclaw/plugin-sdk/reply-runtime", async () => {
62-
const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/reply-runtime")>(
63-
"openclaw/plugin-sdk/reply-runtime",
64-
);
65-
return { ...actual, dispatchInboundMessage: mockDispatchInboundMessage };
66-
});
67-
6851
vi.mock("openclaw/plugin-sdk/session-store-runtime", async () => {
6952
const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/session-store-runtime")>(
7053
"openclaw/plugin-sdk/session-store-runtime",
@@ -133,15 +116,12 @@ describe("broadcast dispatch", () => {
133116
canStartAgentTurn: true,
134117
};
135118
const turn = await params.adapter.resolveTurn(input, eventClass, {});
136-
if (!("runDispatch" in turn)) {
137-
throw new Error("feishu broadcast test runtime only supports prepared turns");
119+
if (!("route" in turn) || !("delivery" in turn)) {
120+
throw new Error("expected assembled Feishu channel turn plan");
138121
}
139-
const routeSessionKey = "route" in turn ? turn.route.sessionKey : turn.routeSessionKey;
140-
const storePath = "storePath" in turn ? turn.storePath : mockResolveStorePath();
141-
const recordInboundSession =
142-
"recordInboundSession" in turn ? turn.recordInboundSession : mockRecordInboundSession;
143-
await recordInboundSession({
144-
storePath,
122+
const routeSessionKey = turn.route.sessionKey;
123+
await mockRecordInboundSession({
124+
storePath: mockResolveStorePath(),
145125
sessionKey: turn.ctxPayload.SessionKey ?? routeSessionKey,
146126
ctx: turn.ctxPayload,
147127
groupResolution: turn.record?.groupResolution,
@@ -150,11 +130,15 @@ describe("broadcast dispatch", () => {
150130
onRecordError: turn.record?.onRecordError ?? (() => undefined),
151131
});
152132
return {
153-
admission: { kind: "dispatch" as const },
133+
admission: turn.admission ?? { kind: "dispatch" as const },
154134
dispatched: true,
155135
ctxPayload: turn.ctxPayload,
156136
routeSessionKey,
157-
dispatchResult: await turn.runDispatch(),
137+
dispatchResult: await mockDispatchReply({
138+
ctx: turn.ctxPayload,
139+
cfg: turn.cfg,
140+
replyOptions: turn.replyOptions,
141+
}),
158142
};
159143
}),
160144
},
@@ -224,7 +208,7 @@ describe("broadcast dispatch", () => {
224208

225209
beforeEach(() => {
226210
vi.clearAllMocks();
227-
mockDispatchInboundMessage.mockReset().mockResolvedValue({
211+
mockDispatchReply.mockReset().mockResolvedValue({
228212
queuedFinal: false,
229213
counts: { final: 1 },
230214
});
@@ -240,17 +224,9 @@ describe("broadcast dispatch", () => {
240224
matchedBy: "default",
241225
});
242226
mockCreateFeishuReplyDispatcher.mockReturnValue({
243-
dispatcher: {
244-
sendToolResult: vi.fn(),
245-
sendBlockReply: vi.fn(),
246-
sendFinalReply: vi.fn(),
247-
waitForIdle: vi.fn(),
248-
getQueuedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
249-
getFailedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
250-
markComplete: vi.fn(),
251-
},
227+
dispatcherOptions: {},
228+
delivery: { deliver: vi.fn(async () => undefined) },
252229
replyOptions: {},
253-
markDispatchIdle: vi.fn(),
254230
ensureNoVisibleReplyFallback: vi.fn(),
255231
});
256232
mockCreateFeishuClient.mockReturnValue({
@@ -286,7 +262,7 @@ describe("broadcast dispatch", () => {
286262
runtime: createRuntimeEnv(),
287263
});
288264

289-
expect(mockDispatchInboundMessage).toHaveBeenCalledTimes(2);
265+
expect(mockDispatchReply).toHaveBeenCalledTimes(2);
290266
const sessionKeys = builtInboundContextCalls.map((call) => call.SessionKey);
291267
expect(sessionKeys).toContain("agent:susan:feishu:group:oc-broadcast-group");
292268
expect(sessionKeys).toContain("agent:main:feishu:group:oc-broadcast-group");
@@ -356,7 +332,7 @@ describe("broadcast dispatch", () => {
356332
});
357333

358334
it("sends no-visible-reply fallback for active broadcast zero-final dispatch", async () => {
359-
mockDispatchInboundMessage
335+
mockDispatchReply
360336
.mockResolvedValueOnce({ queuedFinal: false, counts: { final: 1 } })
361337
.mockResolvedValueOnce({
362338
queuedFinal: false,
@@ -365,17 +341,9 @@ describe("broadcast dispatch", () => {
365341
});
366342
const ensureNoVisibleReplyFallback = vi.fn();
367343
mockCreateFeishuReplyDispatcher.mockReturnValueOnce({
368-
dispatcher: {
369-
sendToolResult: vi.fn(),
370-
sendBlockReply: vi.fn(),
371-
sendFinalReply: vi.fn(),
372-
waitForIdle: vi.fn(),
373-
getQueuedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
374-
getFailedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
375-
markComplete: vi.fn(),
376-
},
344+
dispatcherOptions: {},
345+
delivery: { deliver: vi.fn(async () => undefined) },
377346
replyOptions: {},
378-
markDispatchIdle: vi.fn(),
379347
ensureNoVisibleReplyFallback,
380348
});
381349
const cfg = createBroadcastConfig();
@@ -398,25 +366,18 @@ describe("broadcast dispatch", () => {
398366
});
399367

400368
it("sends no-visible-reply fallback for active broadcast failed final delivery", async () => {
401-
mockDispatchInboundMessage
369+
mockDispatchReply
402370
.mockResolvedValueOnce({ queuedFinal: false, counts: { final: 1 } })
403371
.mockResolvedValueOnce({
404372
queuedFinal: true,
405373
counts: { final: 1 },
374+
failedCounts: { tool: 0, block: 0, final: 1 },
406375
});
407376
const ensureNoVisibleReplyFallback = vi.fn();
408377
mockCreateFeishuReplyDispatcher.mockReturnValueOnce({
409-
dispatcher: {
410-
sendToolResult: vi.fn(),
411-
sendBlockReply: vi.fn(),
412-
sendFinalReply: vi.fn(),
413-
waitForIdle: vi.fn(),
414-
getQueuedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
415-
getFailedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 1 })),
416-
markComplete: vi.fn(),
417-
},
378+
dispatcherOptions: {},
379+
delivery: { deliver: vi.fn(async () => undefined) },
418380
replyOptions: {},
419-
markDispatchIdle: vi.fn(),
420381
ensureNoVisibleReplyFallback,
421382
});
422383
const cfg = createBroadcastConfig();
@@ -439,7 +400,7 @@ describe("broadcast dispatch", () => {
439400
});
440401

441402
it("skips no-visible-reply fallback for source-suppressed active broadcast dispatch", async () => {
442-
mockDispatchInboundMessage
403+
mockDispatchReply
443404
.mockResolvedValueOnce({ queuedFinal: false, counts: { final: 1 } })
444405
.mockResolvedValueOnce({
445406
queuedFinal: false,
@@ -449,17 +410,9 @@ describe("broadcast dispatch", () => {
449410
});
450411
const ensureNoVisibleReplyFallback = vi.fn();
451412
mockCreateFeishuReplyDispatcher.mockReturnValueOnce({
452-
dispatcher: {
453-
sendToolResult: vi.fn(),
454-
sendBlockReply: vi.fn(),
455-
sendFinalReply: vi.fn(),
456-
waitForIdle: vi.fn(),
457-
getQueuedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
458-
getFailedCounts: vi.fn(() => ({ tool: 0, block: 0, final: 0 })),
459-
markComplete: vi.fn(),
460-
},
413+
dispatcherOptions: {},
414+
delivery: { deliver: vi.fn(async () => undefined) },
461415
replyOptions: {},
462-
markDispatchIdle: vi.fn(),
463416
ensureNoVisibleReplyFallback,
464417
});
465418
const cfg = createBroadcastConfig();
@@ -493,7 +446,7 @@ describe("broadcast dispatch", () => {
493446
runtime: createRuntimeEnv(),
494447
});
495448

496-
expect(mockDispatchInboundMessage).not.toHaveBeenCalled();
449+
expect(mockDispatchReply).not.toHaveBeenCalled();
497450
expect(mockCreateFeishuReplyDispatcher).not.toHaveBeenCalled();
498451
expect(mockGetChatInfo).not.toHaveBeenCalled();
499452
});
@@ -511,7 +464,7 @@ describe("broadcast dispatch", () => {
511464
runtime: createRuntimeEnv(),
512465
});
513466

514-
expect(mockDispatchInboundMessage).not.toHaveBeenCalled();
467+
expect(mockDispatchReply).not.toHaveBeenCalled();
515468
expect(mockCreateFeishuReplyDispatcher).not.toHaveBeenCalled();
516469
expect(mockGetChatInfo).not.toHaveBeenCalled();
517470
});
@@ -548,7 +501,7 @@ describe("broadcast dispatch", () => {
548501
runtime: createRuntimeEnv(),
549502
});
550503

551-
expect(mockDispatchInboundMessage).toHaveBeenCalledTimes(1);
504+
expect(mockDispatchReply).toHaveBeenCalledTimes(1);
552505
expect(mockCreateFeishuReplyDispatcher).toHaveBeenCalledTimes(1);
553506
expect(builtInboundContextCalls).toHaveLength(1);
554507
expect(builtInboundContextCalls[0]?.SessionKey).toBe(
@@ -593,9 +546,9 @@ describe("broadcast dispatch", () => {
593546
runtime: createRuntimeEnv(),
594547
accountId: "account-A",
595548
});
596-
expect(mockDispatchInboundMessage).toHaveBeenCalledTimes(2);
549+
expect(mockDispatchReply).toHaveBeenCalledTimes(2);
597550

598-
mockDispatchInboundMessage.mockClear();
551+
mockDispatchReply.mockClear();
599552
mockGetChatInfo.mockClear();
600553
builtInboundContextCalls.length = 0;
601554

@@ -605,7 +558,7 @@ describe("broadcast dispatch", () => {
605558
runtime: createRuntimeEnv(),
606559
accountId: "account-B",
607560
});
608-
expect(mockDispatchInboundMessage).not.toHaveBeenCalled();
561+
expect(mockDispatchReply).not.toHaveBeenCalled();
609562
expect(mockGetChatInfo).not.toHaveBeenCalled();
610563
});
611564

@@ -643,7 +596,7 @@ describe("broadcast dispatch", () => {
643596
runtime: createRuntimeEnv(),
644597
});
645598

646-
expect(mockDispatchInboundMessage).toHaveBeenCalledTimes(1);
599+
expect(mockDispatchReply).toHaveBeenCalledTimes(1);
647600
const sessionKey =
648601
typeof builtInboundContextCalls[0]?.SessionKey === "string"
649602
? builtInboundContextCalls[0].SessionKey

0 commit comments

Comments
 (0)