Skip to content

Commit 3e9e1d6

Browse files
Jerry-Xin忻役steipete
authored
fix: route subagent announce to originating parent session instead of channel-bound peer session (#80242)
* fix: route subagent announce to originating parent session instead of channel-bound peer session When a subagent is spawned from agent:main:main while a Telegram DM is active, the completion announce was delivered to the parallel Telegram channel session instead of the originating parent. Two interacting bugs: 1. The spawn tool received the sandbox/policy session key (Telegram peer key) as the requester, instead of the real run session key. Fixed by passing runSessionKey to createSessionsSpawnTool so the registered requester points to the actual parent session. 2. resolveSubagentCompletionOrigin checked child session bindings before requester bindings. When both share the same channel+accountId (common for Telegram DMs), the child binding hijacked the delivery target. Fixed by checking requester binding first, with child as fallback. Fixes #80201 * fix: drop subagent_announce from mediated completion set The subagent_announce addition to AGENT_MEDIATED_COMPLETION_TOOLS was unrelated to the routing fix and could cause group/channel completions to fail silently when the subagent does not use the message tool. This should be addressed separately with proper message-tool-only guidance (tracked in #80223). * fix: separate sandbox policy from completion owner in sessions_spawn PR #80242 passed runSessionKey as agentSessionKey to createSessionsSpawnTool, which caused spawnSubagentDirect to use the run session key for sandbox policy checks (resolveSandboxRuntimeStatus). This could make a sandboxed channel run appear unsandboxed. Introduce completionOwnerKey as a separate field that is only used for registerSubagentRun routing (requesterSessionKey), keeping agentSessionKey for sandbox enforcement, callerDepth, activeChildren, and all other policy checks. * fix(agents): preserve subagent ownership routing --------- Co-authored-by: 忻役 <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent d1cd74b commit 3e9e1d6

10 files changed

Lines changed: 334 additions & 36 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Docs: https://docs.openclaw.ai
2626
- Feishu: detect SecretRef top-level credentials as a configured default account instead of treating object-backed app secrets as missing.
2727
- Providers/Google: preserve and recover Gemini 3 tool-call thought signatures during native replay so function-calling turns no longer fail with missing `thought_signature` 400s. Fixes #72879. (#80358) Thanks @abnershang.
2828
- Memory-core: distinguish sqlite-vec load failures from missing semantic vector embeddings in degraded `memory index` warnings, so vector recall diagnostics point at unresolved dimensions instead of blaming sqlite-vec when the store is ready. Fixes #75624. (#83056) Thanks @xuruiray and @Noah3521.
29+
- Agents/subagents: preserve sandbox-peer controller ownership while routing completion announcements back to the originating run session, keeping subagent control and completion delivery scoped correctly. Fixes #80201. (#80242) Thanks @Jerry-Xin.
2930
- Gateway/secrets: split the lightweight secrets runtime state and auth-store cache from the full secrets runtime and take a startup fast path when the gateway startup config has no SecretRef values, speeding up secrets startup while preserving cleanup and refresh semantics.
3031
- Codex app-server: rotate oversized native Codex threads before resume and cap dynamic tool-result text entering native Codex sessions, preventing stale oversized context from surviving OpenClaw compaction. (#82981) Thanks @hansolo949.
3132
- Gateway/restart: drain pending replies and active chat runs during restart shutdown before sockets and channels close, aborting timed-out chat runs through the normal cleanup path. (#69121) Thanks @alexlomt.

‎src/agents/openclaw-tools.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ export function createOpenClawTools(
428428
? [
429429
createSessionsSpawnTool({
430430
agentSessionKey: options?.agentSessionKey,
431+
completionOwnerKey: options?.runSessionKey,
431432
agentChannel: options?.agentChannel,
432433
agentAccountId: options?.agentAccountId,
433434
agentTo: options?.agentTo,

‎src/agents/subagent-announce-delivery.test.ts‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,112 @@ describe("resolveSubagentCompletionOrigin", () => {
436436
to: "channel:parent-main",
437437
});
438438
});
439+
440+
it("prefers requester binding when child and requester share the same channel and accountId", async () => {
441+
registerSessionBindingAdapter({
442+
channel: "telegram",
443+
accountId: "bot-1",
444+
listBySession: (targetSessionKey: string) => {
445+
if (targetSessionKey === "agent:main:telegram:default:direct:123") {
446+
return [
447+
{
448+
bindingId: "telegram:bot-1:child-dm",
449+
targetSessionKey,
450+
targetKind: "subagent",
451+
conversation: {
452+
channel: "telegram",
453+
accountId: "bot-1",
454+
conversationId: "direct:123",
455+
},
456+
status: "active",
457+
boundAt: 1,
458+
},
459+
];
460+
}
461+
if (targetSessionKey === "agent:main:main") {
462+
return [
463+
{
464+
bindingId: "telegram:bot-1:parent-main",
465+
targetSessionKey,
466+
targetKind: "session",
467+
conversation: {
468+
channel: "telegram",
469+
accountId: "bot-1",
470+
conversationId: "direct:789",
471+
},
472+
status: "active",
473+
boundAt: 1,
474+
},
475+
];
476+
}
477+
return [];
478+
},
479+
resolveByConversation: () => null,
480+
});
481+
482+
const origin = await resolveSubagentCompletionOrigin({
483+
childSessionKey: "agent:main:telegram:default:direct:123",
484+
requesterSessionKey: "agent:main:main",
485+
requesterOrigin: {
486+
channel: "telegram",
487+
accountId: "bot-1",
488+
to: "telegram:direct:789",
489+
},
490+
spawnMode: "run",
491+
expectsCompletionMessage: true,
492+
});
493+
494+
expect(origin).toEqual({
495+
channel: "telegram",
496+
accountId: "bot-1",
497+
to: "telegram:direct:789",
498+
});
499+
});
500+
501+
it("falls back to child binding when requester has no binding", async () => {
502+
registerSessionBindingAdapter({
503+
channel: "telegram",
504+
accountId: "bot-1",
505+
listBySession: (targetSessionKey: string) => {
506+
if (targetSessionKey === "agent:main:telegram:default:direct:123") {
507+
return [
508+
{
509+
bindingId: "telegram:bot-1:child-dm",
510+
targetSessionKey,
511+
targetKind: "subagent",
512+
conversation: {
513+
channel: "telegram",
514+
accountId: "bot-1",
515+
conversationId: "direct:123",
516+
},
517+
status: "active",
518+
boundAt: 1,
519+
},
520+
];
521+
}
522+
return [];
523+
},
524+
resolveByConversation: () => null,
525+
});
526+
527+
const origin = await resolveSubagentCompletionOrigin({
528+
childSessionKey: "agent:main:telegram:default:direct:123",
529+
requesterSessionKey: "agent:main:main",
530+
requesterOrigin: {
531+
channel: "telegram",
532+
accountId: "bot-1",
533+
to: "telegram:direct:123",
534+
},
535+
spawnMode: "run",
536+
expectsCompletionMessage: true,
537+
});
538+
539+
expect(origin).toEqual({
540+
channel: "telegram",
541+
accountId: "bot-1",
542+
to: "telegram:direct:123",
543+
});
544+
});
439545
});
440546

441547
describe("deliverSubagentAnnouncement active requester steering", () => {

‎src/agents/subagent-announce-delivery.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,33 +363,33 @@ export async function resolveSubagentCompletionOrigin(params: {
363363
channel && conversationId ? { channel, accountId, conversationId } : undefined;
364364

365365
const router = createBoundDeliveryRouter();
366-
const childRoute = router.resolveDestination({
366+
const requesterRoute = router.resolveDestination({
367367
eventKind: "task_completion",
368-
targetSessionKey: params.childSessionKey,
368+
targetSessionKey: params.requesterSessionKey,
369369
requester: requesterConversation,
370370
failClosed: true,
371371
});
372-
if (childRoute.mode === "bound" && childRoute.binding) {
372+
if (requesterRoute.mode === "bound" && requesterRoute.binding) {
373373
return mergeDeliveryContext(
374374
resolveBoundConversationOrigin({
375-
bindingConversation: childRoute.binding.conversation,
375+
bindingConversation: requesterRoute.binding.conversation,
376376
requesterConversation,
377377
requesterOrigin,
378378
}),
379379
requesterOrigin,
380380
);
381381
}
382382

383-
const route = router.resolveDestination({
383+
const childRoute = router.resolveDestination({
384384
eventKind: "task_completion",
385-
targetSessionKey: params.requesterSessionKey,
385+
targetSessionKey: params.childSessionKey,
386386
requester: requesterConversation,
387387
failClosed: true,
388388
});
389-
if (route.mode === "bound" && route.binding) {
389+
if (childRoute.mode === "bound" && childRoute.binding) {
390390
return mergeDeliveryContext(
391391
resolveBoundConversationOrigin({
392-
bindingConversation: route.binding.conversation,
392+
bindingConversation: childRoute.binding.conversation,
393393
requesterConversation,
394394
requesterOrigin,
395395
}),
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { OpenClawConfig } from "../config/types.openclaw.js";
2+
import {
3+
resolveDisplaySessionKey,
4+
resolveInternalSessionKey,
5+
resolveMainSessionAlias,
6+
} from "./tools/sessions-helpers.js";
7+
8+
export type SubagentSpawnOwnership = {
9+
controllerSessionKey: string;
10+
threadBindingRequesterSessionKey: string;
11+
completionRequesterSessionKey: string;
12+
completionRequesterDisplayKey: string;
13+
};
14+
15+
export function resolveSubagentSpawnOwnership(params: {
16+
cfg: OpenClawConfig;
17+
agentSessionKey?: string;
18+
completionOwnerKey?: string;
19+
}): SubagentSpawnOwnership {
20+
const { mainKey, alias } = resolveMainSessionAlias(params.cfg);
21+
const controllerSessionKey = params.agentSessionKey
22+
? resolveInternalSessionKey({
23+
key: params.agentSessionKey,
24+
alias,
25+
mainKey,
26+
})
27+
: alias;
28+
const completionOwnerKey = params.completionOwnerKey?.trim();
29+
const completionRequesterSessionKey = completionOwnerKey
30+
? resolveInternalSessionKey({
31+
key: completionOwnerKey,
32+
alias,
33+
mainKey,
34+
})
35+
: controllerSessionKey;
36+
const completionRequesterDisplayKey = resolveDisplaySessionKey({
37+
key: completionRequesterSessionKey,
38+
alias,
39+
mainKey,
40+
});
41+
42+
return {
43+
controllerSessionKey,
44+
threadBindingRequesterSessionKey: controllerSessionKey,
45+
completionRequesterSessionKey,
46+
completionRequesterDisplayKey,
47+
};
48+
}

‎src/agents/subagent-spawn.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,26 @@ describe("spawnSubagentDirect seam flow", () => {
259259
expect(agentParams.cleanupBundleMcpOnRunEnd).toBe(true);
260260
});
261261

262+
it("keeps controller ownership separate from completion ownership", async () => {
263+
await spawnSubagentDirect(
264+
{
265+
task: "background work",
266+
},
267+
{
268+
agentSessionKey: "agent:main:telegram:default:direct:456",
269+
completionOwnerKey: "agent:main:main",
270+
agentChannel: "telegram",
271+
agentAccountId: "default",
272+
agentTo: "telegram:direct:456",
273+
},
274+
);
275+
276+
const registerInput = firstRegisteredSubagentRun();
277+
expect(registerInput.controllerSessionKey).toBe("agent:main:telegram:default:direct:456");
278+
expect(registerInput.requesterSessionKey).toBe("agent:main:main");
279+
expect(registerInput.requesterDisplayKey).toBe("agent:main:main");
280+
});
281+
262282
it("omits requesterOrigin threadId when no requester thread is provided", async () => {
263283
hoisted.callGatewayMock.mockImplementation(async (request: { method?: string }) => {
264284
if (request.method === "agent") {

‎src/agents/subagent-spawn.thread-binding.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ const hoisted = vi.hoisted(() => ({
1919
}));
2020

2121
function firstRegisteredSubagentRun(): {
22+
controllerSessionKey?: string;
23+
requesterSessionKey?: string;
24+
requesterDisplayKey?: string;
2225
requesterOrigin?: { channel?: string; accountId?: string; to?: string };
2326
expectsCompletionMessage?: boolean;
2427
spawnMode?: string;
2528
} {
2629
const call = hoisted.registerSubagentRunMock.mock.calls[0]?.[0] as
2730
| {
31+
controllerSessionKey?: string;
32+
requesterSessionKey?: string;
33+
requesterDisplayKey?: string;
2834
requesterOrigin?: { channel?: string; accountId?: string; to?: string };
2935
expectsCompletionMessage?: boolean;
3036
spawnMode?: string;
@@ -187,6 +193,45 @@ describe("spawnSubagentDirect thread binding delivery", () => {
187193
expect(registeredRun?.spawnMode).toBe("session");
188194
});
189195

196+
it("uses controller ownership for thread binding while completion routes to owner", async () => {
197+
let hookRequesterSessionKey: string | undefined;
198+
hoisted.hookRunner.hasHooks.mockImplementation(
199+
(hookName?: string) => hookName === "subagent_spawning",
200+
);
201+
hoisted.hookRunner.runSubagentSpawning.mockImplementation(
202+
async (_event: unknown, ctx?: { requesterSessionKey?: string }) => {
203+
hookRequesterSessionKey = ctx?.requesterSessionKey;
204+
return {
205+
status: "ok",
206+
threadBindingReady: true,
207+
};
208+
},
209+
);
210+
211+
const result = await spawnSubagentDirect(
212+
{
213+
task: "reply with a marker",
214+
thread: true,
215+
mode: "session",
216+
context: "isolated",
217+
},
218+
{
219+
agentSessionKey: "agent:main:telegram:default:direct:456",
220+
completionOwnerKey: "agent:main:main",
221+
agentChannel: "telegram",
222+
agentAccountId: "default",
223+
agentTo: "telegram:direct:456",
224+
},
225+
);
226+
227+
expect(result.status).toBe("accepted");
228+
expect(hookRequesterSessionKey).toBe("agent:main:telegram:default:direct:456");
229+
const registeredRun = firstRegisteredSubagentRun();
230+
expect(registeredRun.controllerSessionKey).toBe("agent:main:telegram:default:direct:456");
231+
expect(registeredRun.requesterSessionKey).toBe("agent:main:main");
232+
expect(registeredRun.requesterDisplayKey).toBe("agent:main:main");
233+
});
234+
190235
it("keeps completion announcements when only a generic binding is available", async () => {
191236
hoisted.hookRunner.hasHooks.mockImplementation(
192237
(hookName?: string) => hookName === "subagent_spawning",

‎src/agents/subagent-spawn.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { getSubagentDepthFromSessionStore } from "./subagent-depth.js";
3535
import { buildSubagentInitialUserMessage } from "./subagent-initial-user-message.js";
3636
import { countActiveRunsForSession, registerSubagentRun } from "./subagent-registry.js";
3737
import { resolveSubagentSpawnAcceptedNote } from "./subagent-spawn-accepted-note.js";
38+
import { resolveSubagentSpawnOwnership } from "./subagent-spawn-ownership.js";
3839
import { resolveSubagentTargetPolicy } from "./subagent-target-policy.js";
3940
import { normalizeSubagentTaskName } from "./subagent-task-name.js";
4041
export {
@@ -66,7 +67,6 @@ import {
6667
resolveParentForkDecision,
6768
resolveAgentConfig,
6869
resolveContextEngine,
69-
resolveDisplaySessionKey,
7070
resolveGatewaySessionStoreTarget,
7171
resolveInternalSessionKey,
7272
resolveMainSessionAlias,
@@ -147,6 +147,8 @@ export type SpawnSubagentParams = {
147147

148148
export type SpawnSubagentContext = {
149149
agentSessionKey?: string;
150+
/** Separate key used only for completion routing, not sandbox policy. */
151+
completionOwnerKey?: string;
150152
agentChannel?: string;
151153
agentAccountId?: string;
152154
agentTo?: string;
@@ -769,10 +771,10 @@ export async function spawnSubagentDirect(
769771
mainKey,
770772
})
771773
: alias;
772-
const requesterDisplayKey = resolveDisplaySessionKey({
773-
key: requesterInternalKey,
774-
alias,
775-
mainKey,
774+
const ownership = resolveSubagentSpawnOwnership({
775+
cfg,
776+
agentSessionKey: ctx.agentSessionKey,
777+
completionOwnerKey: ctx.completionOwnerKey,
776778
});
777779

778780
const callerDepth = getSubagentDepthFromSessionStore(requesterInternalKey, { cfg });
@@ -980,7 +982,7 @@ export async function spawnSubagentDirect(
980982
agentId: targetAgentId,
981983
label: label || undefined,
982984
mode: spawnMode,
983-
requesterSessionKey: requesterInternalKey,
985+
requesterSessionKey: ownership.threadBindingRequesterSessionKey,
984986
requester: {
985987
channel: childSessionOrigin?.channel,
986988
accountId: childSessionOrigin?.accountId,
@@ -1245,10 +1247,10 @@ export async function spawnSubagentDirect(
12451247
registerSubagentRun({
12461248
runId: childRunId,
12471249
childSessionKey,
1248-
controllerSessionKey: requesterInternalKey,
1249-
requesterSessionKey: requesterInternalKey,
1250+
controllerSessionKey: ownership.controllerSessionKey,
1251+
requesterSessionKey: ownership.completionRequesterSessionKey,
12501252
requesterOrigin,
1251-
requesterDisplayKey,
1253+
requesterDisplayKey: ownership.completionRequesterDisplayKey,
12521254
task,
12531255
taskName,
12541256
cleanup,

0 commit comments

Comments
 (0)