Skip to content

Commit 8a7c214

Browse files
scotthuangsteipete
authored andcommitted
fix(agents): gate sessions_send A2A skip on requester ownership
Greptile/Codex review follow-ups on #69817: - Narrow skipA2AFlow from target-only detection to a combined check that the caller is the parent of the target (new isRequesterParentOfBackgroundAcpSession helper). Under tools.sessions.visibility=all a non-parent sender can see the same oneshot ACP session; the previous guard would have suppressed their only follow-up delivery path. With requester ownership required, those senders continue through the normal A2A flow. - When the A2A flow is skipped, return delivery.status="skipped" instead of "pending" so the parent LLM does not wait for a second result that will never arrive. - Add unit tests for resolveAcpSessionInteractionMode and isRequesterParentOfBackgroundAcpSession covering both the new ownership gate and the existing target-type branches.
1 parent 1c3fbbd commit 8a7c214

3 files changed

Lines changed: 149 additions & 9 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, expect, it } from "vitest";
2+
import {
3+
isParentOwnedBackgroundAcpSession,
4+
isRequesterParentOfBackgroundAcpSession,
5+
resolveAcpSessionInteractionMode,
6+
} from "./session-interaction-mode.js";
7+
8+
const parentKey = "agent:main:main";
9+
const otherKey = "agent:peer:some-other";
10+
11+
describe("resolveAcpSessionInteractionMode", () => {
12+
it("returns interactive when entry is undefined", () => {
13+
expect(resolveAcpSessionInteractionMode(undefined)).toBe("interactive");
14+
});
15+
16+
it("returns interactive for non-oneshot ACP sessions", () => {
17+
expect(
18+
resolveAcpSessionInteractionMode({
19+
acp: { mode: "persistent" } as never,
20+
spawnedBy: parentKey,
21+
}),
22+
).toBe("interactive");
23+
});
24+
25+
it("returns parent-owned-background for oneshot sessions with spawnedBy set", () => {
26+
expect(
27+
resolveAcpSessionInteractionMode({
28+
acp: { mode: "oneshot" } as never,
29+
spawnedBy: parentKey,
30+
}),
31+
).toBe("parent-owned-background");
32+
});
33+
34+
it("returns parent-owned-background for oneshot sessions with parentSessionKey set", () => {
35+
expect(
36+
resolveAcpSessionInteractionMode({
37+
acp: { mode: "oneshot" } as never,
38+
parentSessionKey: parentKey,
39+
}),
40+
).toBe("parent-owned-background");
41+
});
42+
43+
it("returns interactive for a oneshot session without any parent linkage", () => {
44+
expect(
45+
resolveAcpSessionInteractionMode({
46+
acp: { mode: "oneshot" } as never,
47+
}),
48+
).toBe("interactive");
49+
});
50+
});
51+
52+
describe("isRequesterParentOfBackgroundAcpSession", () => {
53+
const backgroundEntry = {
54+
acp: { mode: "oneshot" } as never,
55+
spawnedBy: parentKey,
56+
parentSessionKey: parentKey,
57+
};
58+
59+
it("returns true when requester matches spawnedBy", () => {
60+
expect(
61+
isRequesterParentOfBackgroundAcpSession(
62+
{ acp: { mode: "oneshot" } as never, spawnedBy: parentKey },
63+
parentKey,
64+
),
65+
).toBe(true);
66+
});
67+
68+
it("returns true when requester matches parentSessionKey", () => {
69+
expect(
70+
isRequesterParentOfBackgroundAcpSession(
71+
{ acp: { mode: "oneshot" } as never, parentSessionKey: parentKey },
72+
parentKey,
73+
),
74+
).toBe(true);
75+
});
76+
77+
it("returns false when requester is a different session (not the parent)", () => {
78+
expect(isRequesterParentOfBackgroundAcpSession(backgroundEntry, otherKey)).toBe(false);
79+
});
80+
81+
it("returns false when requester key is missing", () => {
82+
expect(isRequesterParentOfBackgroundAcpSession(backgroundEntry, undefined)).toBe(false);
83+
expect(isRequesterParentOfBackgroundAcpSession(backgroundEntry, "")).toBe(false);
84+
});
85+
86+
it("returns false when target is not a parent-owned background ACP session", () => {
87+
expect(
88+
isRequesterParentOfBackgroundAcpSession(
89+
{ acp: { mode: "persistent" } as never, spawnedBy: parentKey },
90+
parentKey,
91+
),
92+
).toBe(false);
93+
});
94+
95+
it("delegates to isParentOwnedBackgroundAcpSession for target-only checks", () => {
96+
expect(isParentOwnedBackgroundAcpSession(backgroundEntry)).toBe(true);
97+
});
98+
});

‎src/acp/session-interaction-mode.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,30 @@ export function resolveAcpSessionInteractionMode(
2323
export function isParentOwnedBackgroundAcpSession(entry?: SessionInteractionEntry | null): boolean {
2424
return resolveAcpSessionInteractionMode(entry) === "parent-owned-background";
2525
}
26+
27+
/**
28+
* Returns true when `entry` is a parent-owned background ACP session AND the
29+
* given `requesterSessionKey` is the session that spawned/owns it. This is a
30+
* strictly narrower check than {@link isParentOwnedBackgroundAcpSession}: the
31+
* target must match *and* the caller must be the parent.
32+
*
33+
* Used to gate behaviors that only make sense for the parent↔own-child pair
34+
* (e.g. skipping the A2A ping-pong flow in `sessions_send`), so that an
35+
* unrelated session with broad visibility (e.g. `tools.sessions.visibility=all`)
36+
* sending to the same target is still routed through the normal A2A path.
37+
*/
38+
export function isRequesterParentOfBackgroundAcpSession(
39+
entry: SessionInteractionEntry | null | undefined,
40+
requesterSessionKey: string | null | undefined,
41+
): boolean {
42+
if (!isParentOwnedBackgroundAcpSession(entry)) {
43+
return false;
44+
}
45+
const requester = normalizeOptionalString(requesterSessionKey);
46+
if (!requester) {
47+
return false;
48+
}
49+
const spawnedBy = normalizeOptionalString(entry?.spawnedBy);
50+
const parentSessionKey = normalizeOptionalString(entry?.parentSessionKey);
51+
return requester === spawnedBy || requester === parentSessionKey;
52+
}

‎src/agents/tools/sessions-send-tool.ts‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import crypto from "node:crypto";
22
import { Type } from "@sinclair/typebox";
3-
import { isParentOwnedBackgroundAcpSession } from "../../acp/session-interaction-mode.js";
3+
import { isRequesterParentOfBackgroundAcpSession } from "../../acp/session-interaction-mode.js";
44
import type { OpenClawConfig } from "../../config/types.openclaw.js";
55
import { callGateway } from "../../gateway/call.js";
66
import { formatErrorMessage } from "../../infra/errors.js";
@@ -290,18 +290,33 @@ export function createSessionsSendTool(opts?: {
290290
const requesterSessionKey = opts?.agentSessionKey;
291291
const requesterChannel = opts?.agentChannel;
292292
const maxPingPongTurns = resolvePingPongTurns(cfg);
293-
const delivery = { status: "pending", mode: "announce" as const };
294293

295-
// Skip the A2A ping-pong + announce flow when the target is a
296-
// parent-owned background ACP subagent. Such sessions already report
297-
// their results back to the parent through the
298-
// `[Internal task completion event]` announcement path, and treating
294+
// Skip the A2A ping-pong + announce flow when the current caller is the
295+
// parent of a parent-owned background ACP subagent it spawned itself.
296+
// Such sessions already report their results back to the parent through
297+
// the `[Internal task completion event]` announcement path, and treating
299298
// them as a peer agent causes the parent to be woken with the child's
300299
// reply, generate a user-facing response, and have that response
301-
// forwarded back to the child as a new message — producing an infinite
302-
// ping-pong loop between parent and ACP child.
300+
// forwarded back to the child as a new message — producing a
301+
// ping-pong loop between parent and ACP child (bounded by
302+
// maxPingPongTurns, but user-visible as a runaway conversation).
303+
//
304+
// The skip is gated on requester ownership, not just target type: an
305+
// unrelated sender that can see the same target (e.g. under
306+
// `tools.sessions.visibility=all`) must still go through the normal A2A
307+
// path so it actually receives a follow-up delivery.
303308
const targetSessionEntry = loadSessionEntryByKey(resolvedKey);
304-
const skipA2AFlow = isParentOwnedBackgroundAcpSession(targetSessionEntry);
309+
const skipA2AFlow = isRequesterParentOfBackgroundAcpSession(
310+
targetSessionEntry,
311+
effectiveRequesterKey,
312+
);
313+
// When the A2A flow is skipped, no follow-up announcement will fire and
314+
// the reply (when present) is returned inline via the `reply` field.
315+
// Reflect that in the metadata so the parent LLM does not wait for a
316+
// second result that will never arrive.
317+
const delivery = skipA2AFlow
318+
? ({ status: "skipped", mode: "announce" } as const)
319+
: ({ status: "pending", mode: "announce" } as const);
305320

306321
const startA2AFlow = (roundOneReply?: string, waitRunId?: string) => {
307322
if (skipA2AFlow) {

0 commit comments

Comments
 (0)