Skip to content

Commit a1914a7

Browse files
authored
refactor: keep internal wakes out of message channels (#132986)
* refactor: keep internal wakes out of message channels * test: align internal wake integration expectations
1 parent e5f4f8c commit a1914a7

34 files changed

Lines changed: 665 additions & 289 deletions

‎docs/plugins/sdk-channel-inbound.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ dispatchers that keep platform delivery in the delivery adapter. New send
9393
paths should use message adapters and durable message helpers from
9494
`channel-outbound` instead.
9595

96+
## Internal turn sources
97+
98+
`MsgContext.InternalTurnSource` identifies an internal wake: `"heartbeat"`,
99+
`"cron"`, or `"exec"`. Leave it unset for ordinary channel messages. It keeps
100+
internal turns from resetting sessions or replacing the conversation binding;
101+
it does not grant execution authority or replace `InputProvenance`.
102+
103+
Keep `Provider` and `Surface` for transport identity, and keep the reply route in
104+
`OriginatingChannel` and `OriginatingTo`. An internal wake may have no transport
105+
or explicit reply target. Do not put a wake label in those channel fields.
106+
107+
For existing SDK callers, inbound finalization and session-recording entrypoints
108+
translate legacy `Provider` values `"heartbeat"`, `"cron-event"`, and
109+
`"exec-event"` into `InternalTurnSource`. They remove those labels from channel
110+
fields while preserving a real reply route. New callers should set the typed
111+
source directly.
112+
96113
## Delivery settlement contract
97114

98115
`ChannelInboundTurnPlan.delivery` owns the native send for each logical reply

‎extensions/qa-lab/src/cli.runtime.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,6 +2263,7 @@ describe("qa cli runtime", () => {
22632263
"runtime-tool-tavily-extract",
22642264
"runtime-tool-tavily-search",
22652265
"runtime-tool-tts",
2266+
"internal-event-subagent-spawn-live",
22662267
],
22672268
});
22682269
expectWriteContains(

‎extensions/qa-lab/src/scenario-catalog.runtime-pair.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("QA runtime-pair scenario catalog", () => {
2121
.length,
2222
]),
2323
);
24-
expect(laneCounts).toEqual({ core: 35, extended: 8, soak: 2 });
24+
expect(laneCounts).toEqual({ core: 35, extended: 9, soak: 2 });
2525
});
2626

2727
it("declares every release agentic scenario in the core lane", () => {

‎qa/scenarios/scheduling/internal-event-subagent-spawn-live.yaml‎

Lines changed: 295 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ function buildGrant(overrides: Partial<RunCliAgentParams> = {}) {
2929
}
3030

3131
describe("buildCliMcpGrantContext source-reply authority", () => {
32+
it.each(["heartbeat", "cron-event", "exec-event"])(
33+
"keeps the reply channel separate from the %s turn source",
34+
(messageProvider) => {
35+
expect(buildGrant({ messageProvider, messageChannel: "telegram" }).messageProvider).toBe(
36+
"telegram",
37+
);
38+
expect(buildGrant({ messageProvider }).messageProvider).toBeUndefined();
39+
},
40+
);
41+
3242
it("stamps only trusted, message-capped subagent completion grants", () => {
3343
expect(buildGrant().sourceReplyOnly).toBe(true);
3444
});

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { uniqueStrings } from "@openclaw/normalization-core/string-normalization
22
import { canonicalizeMainSessionAlias } from "../../config/sessions/main-session.js";
33
import type { OpenClawConfig } from "../../config/types.openclaw.js";
44
import type { McpLoopbackRequestContext } from "../../gateway/mcp-grant-store.js";
5-
import { normalizeMessageChannel } from "../../utils/message-channel.js";
5+
import { resolveGatewayMessageChannel } from "../../utils/message-channel.js";
66
import { SESSION_PERMISSION_BY_EXEC_MODE } from "../session-permission-exec-mode.js";
77
import type { RunCliAgentParams } from "./types.js";
88

@@ -79,12 +79,6 @@ function buildCliMcpChannelContext(
7979
};
8080
}
8181

82-
function resolveCliMcpMessageProvider(
83-
run: Pick<RunCliAgentParams, "messageProvider" | "messageChannel">,
84-
): string | undefined {
85-
return normalizeMessageChannel(run.messageProvider ?? run.messageChannel) ?? undefined;
86-
}
87-
8882
function resolveCliMcpSessionKey(
8983
run: Pick<RunCliAgentParams, "sessionKey">,
9084
config: OpenClawConfig,
@@ -127,7 +121,9 @@ export function buildCliMcpGrantContext(params: {
127121
const groupChannel = normalizeOptionalMcpContextValue(params.run.groupChannel ?? undefined);
128122
const groupSpace = normalizeOptionalMcpContextValue(params.run.groupSpace ?? undefined);
129123
const spawnedBy = normalizeOptionalMcpContextValue(params.run.spawnedBy ?? undefined);
130-
const messageProvider = resolveCliMcpMessageProvider(params.run);
124+
const messageProvider = resolveGatewayMessageChannel(
125+
params.run.messageChannel ?? params.run.messageProvider,
126+
);
131127
const currentChannelId = normalizeOptionalMcpContextValue(params.run.currentChannelId);
132128
const grantedToolsAllow = params.run.cliToolAvailability?.openClaw ?? params.toolsAllow;
133129
// Trusted message-only completions stay restricted even when source routing

‎src/agents/cli-runner/prepare.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3374,7 +3374,7 @@ describe("prepareCliRunContext", () => {
33743374
workspaceDir: context.workspaceDir,
33753375
modelProvider: "anthropic",
33763376
modelId: "test-model",
3377-
messageProvider: "discord",
3377+
messageProvider: "telegram",
33783378
clientCaps: ["tool-events", "inline-widgets"],
33793379
currentChannelId: "telegram:-100123:topic:42",
33803380
currentThreadTs: "42",
@@ -3478,7 +3478,7 @@ describe("prepareCliRunContext", () => {
34783478
senderName: "Canonical Name",
34793479
senderUsername: "canonical-user",
34803480
senderE164: "+15551234567",
3481-
messageProvider: "discord",
3481+
messageProvider: "telegram",
34823482
groupId: "chat123",
34833483
groupChannel: "ops",
34843484
groupSpace: "workspace-a",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { isStringOption } from "../utils/string-readers.js";
2+
import type { MsgContext } from "./templating.js";
3+
4+
type InternalTurnContext = Pick<
5+
MsgContext,
6+
"InternalTurnSource" | "Provider" | "Surface" | "OriginatingChannel"
7+
>;
8+
9+
function legacyInternalTurnSource(value: string | undefined): MsgContext["InternalTurnSource"] {
10+
switch (value) {
11+
case "heartbeat":
12+
return "heartbeat";
13+
case "cron-event":
14+
return "cron";
15+
case "exec-event":
16+
return "exec";
17+
default:
18+
return undefined;
19+
}
20+
}
21+
22+
/** Fold shipped SDK source labels at ingress; runtime channels describe transport only. */
23+
export function normalizeInternalTurnContext(ctx: InternalTurnContext): void {
24+
const source = isStringOption(ctx.InternalTurnSource, ["heartbeat", "cron", "exec"] as const)
25+
? ctx.InternalTurnSource
26+
: legacyInternalTurnSource(ctx.Provider);
27+
if (source) {
28+
ctx.InternalTurnSource = source;
29+
} else {
30+
delete ctx.InternalTurnSource;
31+
}
32+
for (const field of ["Provider", "Surface", "OriginatingChannel"] as const) {
33+
if (legacyInternalTurnSource(ctx[field])) {
34+
delete ctx[field];
35+
}
36+
}
37+
}

‎src/auto-reply/reply/effective-reply-route.test.ts‎

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { normalizeLegacySessionEntryDelivery } from "../../infra/state-migration
55
import type { ChannelRouteRef } from "../../plugin-sdk/channel-route.js";
66
import { normalizeSessionDeliveryState } from "../../utils/delivery-context.shared.js";
77
import type { DeliveryContext } from "../../utils/delivery-context.types.js";
8-
import { isSystemEventProvider, resolveEffectiveReplyRoute } from "./effective-reply-route.js";
8+
import { resolveEffectiveReplyRoute } from "./effective-reply-route.js";
99

1010
type EffectiveReplyRouteParams = Parameters<typeof resolveEffectiveReplyRoute>[0];
1111
type EffectiveReplyRouteContext = EffectiveReplyRouteParams["ctx"];
@@ -53,10 +53,14 @@ describe("resolveEffectiveReplyRoute", () => {
5353
});
5454
});
5555

56-
it("does not use persisted fallbacks for normal providers", () => {
56+
it.each<EffectiveReplyRouteContext>([
57+
{ Provider: "slack" },
58+
{ InputProvenance: { kind: "internal_system", sourceTool: "restart-sentinel" } },
59+
{ InputProvenance: { kind: "internal_system", sourceTool: "main_session_restart_recovery" } },
60+
])("does not inherit a route without an internal wake source (%j)", (context) => {
5761
expect(
5862
resolveEffectiveReplyRoute({
59-
ctx: ctx({ Provider: "slack" }),
63+
ctx: context,
6064
entry: entry({
6165
deliveryContext: {
6266
channel: "telegram",
@@ -300,7 +304,7 @@ describe("resolveEffectiveReplyRoute", () => {
300304
expect(
301305
resolveEffectiveReplyRoute({
302306
ctx: ctx({
303-
Provider: "exec-event",
307+
InternalTurnSource: "exec",
304308
OriginatingChannel: "telegram",
305309
OriginatingTo: "chat:live",
306310
AccountId: "live-account",
@@ -323,31 +327,34 @@ describe("resolveEffectiveReplyRoute", () => {
323327
});
324328
});
325329

326-
it("falls back to deliveryContext for exec-event replies", () => {
327-
expect(
328-
resolveEffectiveReplyRoute({
329-
ctx: ctx({ Provider: "exec-event" }),
330-
entry: {
331-
delivery: normalizeSessionDeliveryState({
332-
context: {
333-
channel: "telegram",
334-
to: "chat:persisted",
335-
accountId: "persisted-account",
336-
},
337-
}),
338-
},
339-
}),
340-
).toEqual({
341-
channel: "telegram",
342-
to: "chat:persisted",
343-
accountId: "persisted-account",
344-
});
345-
});
330+
it.each(["heartbeat", "cron", "exec"] as const)(
331+
"inherits session delivery for %s replies",
332+
(source) => {
333+
expect(
334+
resolveEffectiveReplyRoute({
335+
ctx: ctx({ InternalTurnSource: source }),
336+
entry: {
337+
delivery: normalizeSessionDeliveryState({
338+
context: {
339+
channel: "telegram",
340+
to: "chat:persisted",
341+
accountId: "persisted-account",
342+
},
343+
}),
344+
},
345+
}),
346+
).toEqual({
347+
channel: "telegram",
348+
to: "chat:persisted",
349+
accountId: "persisted-account",
350+
});
351+
},
352+
);
346353

347354
it("falls back to legacy last route fields for exec-event replies", () => {
348355
expect(
349356
resolveEffectiveReplyRoute({
350-
ctx: ctx({ Provider: "exec-event" }),
357+
ctx: ctx({ InternalTurnSource: "exec" }),
351358
entry: entry({
352359
lastChannel: "slack",
353360
lastTo: "last-to",
@@ -365,7 +372,7 @@ describe("resolveEffectiveReplyRoute", () => {
365372
expect(
366373
resolveEffectiveReplyRoute({
367374
ctx: ctx({
368-
Provider: "exec-event",
375+
InternalTurnSource: "exec",
369376
OriginatingChannel: "telegram",
370377
OriginatingTo: "chat:live",
371378
}),
@@ -388,7 +395,7 @@ describe("resolveEffectiveReplyRoute", () => {
388395
expect(
389396
resolveEffectiveReplyRoute({
390397
ctx: ctx({
391-
Provider: "exec-event",
398+
InternalTurnSource: "exec",
392399
OriginatingChannel: "telegram",
393400
OriginatingTo: "chat:live",
394401
}),
@@ -409,13 +416,3 @@ describe("resolveEffectiveReplyRoute", () => {
409416
});
410417
});
411418
});
412-
413-
describe("isSystemEventProvider", () => {
414-
it("recognizes persisted-delivery event providers", () => {
415-
expect(isSystemEventProvider("heartbeat")).toBe(true);
416-
expect(isSystemEventProvider("cron-event")).toBe(true);
417-
expect(isSystemEventProvider("exec-event")).toBe(true);
418-
expect(isSystemEventProvider("slack")).toBe(false);
419-
expect(isSystemEventProvider(undefined)).toBe(false);
420-
});
421-
});

‎src/auto-reply/reply/effective-reply-route.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type EffectiveReplyRouteContext = Pick<
2020
| "OriginatingTo"
2121
| "AccountId"
2222
| "InputProvenance"
23+
| "InternalTurnSource"
2324
| "ChatType"
2425
>;
2526

@@ -36,11 +37,6 @@ type EffectiveReplyRoute = {
3637
inheritedExternalRoute?: boolean;
3738
};
3839

39-
/** Returns true for synthetic providers that should not define a user channel route. */
40-
export function isSystemEventProvider(provider?: string): boolean {
41-
return provider === "heartbeat" || provider === "cron-event" || provider === "exec-event";
42-
}
43-
4440
function isSessionsSendInterSessionHandoff(inputProvenance: InputProvenance | undefined): boolean {
4541
return (
4642
inputProvenance?.kind === "inter_session" &&
@@ -103,7 +99,7 @@ export function resolveEffectiveReplyRoute(params: {
10399
inheritedExternalRoute: true,
104100
};
105101
}
106-
if (!isSystemEventProvider(params.ctx.Provider)) {
102+
if (params.ctx.InternalTurnSource === undefined) {
107103
return {
108104
channel: params.ctx.OriginatingChannel,
109105
to: params.ctx.OriginatingTo,

0 commit comments

Comments
 (0)