Skip to content

Commit 1cb1198

Browse files
committed
fix(agents): reject sessions_yield without child completion
1 parent 4589d85 commit 1cb1198

6 files changed

Lines changed: 104 additions & 18 deletions

‎src/agents/agent-tools.runtime.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ describe("wrapToolWithAbortSignal", () => {
9797
const wrapped = wrapToolWithAbortSignal(
9898
createSessionsYieldTool({
9999
sessionId: "requester",
100-
onBeforeYield: beforeYield,
100+
claimYield: () => {
101+
beforeYield();
102+
return true;
103+
},
101104
onYield: () => {
102105
runAbort.abort(handoffReason);
103106
},
@@ -126,6 +129,7 @@ describe("wrapToolWithAbortSignal", () => {
126129
const yieldTool = wrapToolWithAbortSignal(
127130
createSessionsYieldTool({
128131
sessionId: "requester",
132+
claimYield: () => true,
129133
onYield: () => {
130134
runAbort.abort(handoffReason);
131135
},
@@ -146,6 +150,7 @@ describe("wrapToolWithAbortSignal", () => {
146150
const wrapped = wrapToolWithAbortSignal(
147151
createSessionsYieldTool({
148152
sessionId: "requester",
153+
claimYield: () => true,
149154
onYield: () => {
150155
runAbort.abort(handoffReason);
151156
callAbort.abort(handoffReason);
@@ -230,6 +235,7 @@ describe("wrapToolWithAbortSignal", () => {
230235
const wrapped = wrapToolWithAbortSignal(
231236
createSessionsYieldTool({
232237
sessionId: "requester",
238+
claimYield: () => true,
233239
onYield: () => {
234240
runAbort.abort({ code: "sessions_yield", turnHandoff: true });
235241
throw yieldError;

‎src/agents/openclaw-tools.registration.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,42 @@ describe("sessions_yield completion ownership", () => {
639639
markRequesterTurnYielded.mockRestore();
640640
}
641641
});
642+
643+
it("keeps the turn active when it owns no pending child completion", async () => {
644+
const registry = await import("./subagents/registry/subagent-registry.js");
645+
const markRequesterTurnYielded = vi
646+
.spyOn(registry, "markRequesterTurnYielded")
647+
.mockReturnValue(0);
648+
const onYield = vi.fn(async () => undefined);
649+
650+
try {
651+
const tool = expectToolNamed(
652+
createTestOpenClawTools({
653+
agentSessionKey: controllerSessionKey,
654+
runSessionKey: "agent:main:main",
655+
sessionId: "requester-session",
656+
runId: "run-requester",
657+
onYield,
658+
disableMessageTool: true,
659+
disablePluginTools: true,
660+
wrapBeforeToolCallHook: false,
661+
}),
662+
"sessions_yield",
663+
);
664+
665+
const result = await tool.execute("yield-requester", {});
666+
667+
expect(result.details).toMatchObject({
668+
status: "error",
669+
error:
670+
"No pending child completion is owned by this turn. Continue working because independent background operations complete separately.",
671+
});
672+
expect(markRequesterTurnYielded).toHaveBeenCalledOnce();
673+
expect(onYield).not.toHaveBeenCalled();
674+
} finally {
675+
markRequesterTurnYielded.mockRestore();
676+
}
677+
});
642678
});
643679

644680
function hasTool(tools: readonly { name: string }[], name: string): boolean {

‎src/agents/openclaw-tools.requester-yield.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ export function createRequesterYieldCallback(params: {
22
requesterSessionKey?: string;
33
requesterAgentId: string;
44
requesterTurnRunId?: string;
5-
}): (() => Promise<void>) | undefined {
5+
}): (() => Promise<boolean>) | undefined {
66
if (!params.requesterSessionKey || !params.requesterTurnRunId) {
77
return undefined;
88
}
99
return async () => {
1010
const { markRequesterTurnYielded } = await import("./subagents/registry/subagent-registry.js");
11-
markRequesterTurnYielded({
12-
requesterSessionKey: params.requesterSessionKey as string,
13-
requesterAgentId: params.requesterAgentId,
14-
requesterTurnRunId: params.requesterTurnRunId as string,
15-
});
11+
return (
12+
markRequesterTurnYielded({
13+
requesterSessionKey: params.requesterSessionKey as string,
14+
requesterAgentId: params.requesterAgentId,
15+
requesterTurnRunId: params.requesterTurnRunId as string,
16+
}) > 0
17+
);
1618
};
1719
}

‎src/agents/openclaw-tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ export function createOpenClawTools(
690690
...swarmToolGroups.agentsWait,
691691
createSessionsYieldTool({
692692
sessionId: options?.sessionId,
693-
onBeforeYield: createRequesterYieldCallback({
693+
claimYield: createRequesterYieldCallback({
694694
requesterSessionKey: trimmedRunSessionKey || options?.agentSessionKey,
695695
requesterAgentId: sessionAgentId,
696696
requesterTurnRunId: options?.runId,

‎src/agents/tools/sessions-yield-tool.test.ts‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ describe("sessions_yield tool", () => {
2323

2424
it("invokes onYield callback with default message", async () => {
2525
const onYield = vi.fn();
26-
const tool = createSessionsYieldTool({ sessionId: "test-session", onYield });
26+
const tool = createSessionsYieldTool({
27+
sessionId: "test-session",
28+
claimYield: () => true,
29+
onYield,
30+
});
2731
const result = await tool.execute("call-1", {});
2832
const details = result.details as SessionsYieldDetails;
2933
expect(details.status).toBe("yielded");
@@ -36,7 +40,11 @@ describe("sessions_yield tool", () => {
3640
// The callback message becomes operator-visible scheduler context, so the
3741
// tool must not replace a supplied reason with the default text.
3842
const onYield = vi.fn();
39-
const tool = createSessionsYieldTool({ sessionId: "test-session", onYield });
43+
const tool = createSessionsYieldTool({
44+
sessionId: "test-session",
45+
claimYield: () => true,
46+
onYield,
47+
});
4048
const result = await tool.execute("call-1", { message: "Waiting for fact-checker" });
4149
const details = result.details as SessionsYieldDetails;
4250
expect(details.status).toBe("yielded");
@@ -47,7 +55,11 @@ describe("sessions_yield tool", () => {
4755

4856
it("keeps private context separate from the user-facing acknowledgment", async () => {
4957
const onYield = vi.fn();
50-
const tool = createSessionsYieldTool({ sessionId: "test-session", onYield });
58+
const tool = createSessionsYieldTool({
59+
sessionId: "test-session",
60+
claimYield: () => true,
61+
onYield,
62+
});
5163
const result = await tool.execute("call-1", {
5264
message: "Resume after the fact-checker replies",
5365
acknowledgment: "Research started; results will follow.",
@@ -65,12 +77,13 @@ describe("sessions_yield tool", () => {
6577
);
6678
});
6779

68-
it("persists yield intent before aborting the requester run", async () => {
80+
it("claims completion ownership before aborting the requester run", async () => {
6981
const order: string[] = [];
7082
const tool = createSessionsYieldTool({
7183
sessionId: "test-session",
72-
onBeforeYield: () => {
73-
order.push("persist");
84+
claimYield: () => {
85+
order.push("claim");
86+
return true;
7487
},
7588
onYield: () => {
7689
order.push("abort");
@@ -79,15 +92,15 @@ describe("sessions_yield tool", () => {
7992

8093
await tool.execute("call-1", {});
8194

82-
expect(order).toEqual(["persist", "abort"]);
95+
expect(order).toEqual(["claim", "abort"]);
8396
});
8497

8598
it("does not abort the requester when yield intent cannot persist", async () => {
8699
const failure = new Error("sqlite unavailable");
87100
const onYield = vi.fn();
88101
const tool = createSessionsYieldTool({
89102
sessionId: "test-session",
90-
onBeforeYield: () => {
103+
claimYield: () => {
91104
throw failure;
92105
},
93106
onYield,
@@ -97,6 +110,27 @@ describe("sessions_yield tool", () => {
97110
expect(onYield).not.toHaveBeenCalled();
98111
});
99112

113+
it.each([
114+
{ name: "the claim callback is unavailable" },
115+
{ name: "the turn owns no pending child completion", claimYield: () => false },
116+
])("keeps the turn active when $name", async ({ claimYield }) => {
117+
const onYield = vi.fn();
118+
const tool = createSessionsYieldTool({
119+
sessionId: "test-session",
120+
...(claimYield ? { claimYield } : {}),
121+
onYield,
122+
});
123+
124+
const result = await tool.execute("call-1", {});
125+
126+
expect(result.details).toMatchObject({
127+
status: "error",
128+
error:
129+
"No pending child completion is owned by this turn. Continue working because independent background operations complete separately.",
130+
});
131+
expect(onYield).not.toHaveBeenCalled();
132+
});
133+
100134
it("returns error without onYield callback", async () => {
101135
const tool = createSessionsYieldTool({ sessionId: "test-session" });
102136
const result = await tool.execute("call-1", {});

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { Type } from "typebox";
77
import type { AnyAgentTool } from "./common.js";
88
import { jsonResult, readToolStringParam } from "./common.js";
99

10+
const NO_PENDING_CHILD_COMPLETION_ERROR =
11+
"No pending child completion is owned by this turn. Continue working because independent background operations complete separately.";
12+
1013
const SessionsYieldToolSchema = Type.Object({
1114
message: Type.Optional(
1215
Type.String({ description: "Private context for the resumed turn; not sent to the user." }),
@@ -21,7 +24,7 @@ const SessionsYieldToolSchema = Type.Object({
2124
/** Creates the sessions_yield tool for runtimes that support yield callbacks. */
2225
export function createSessionsYieldTool(opts?: {
2326
sessionId?: string;
24-
onBeforeYield?: () => Promise<void> | void;
27+
claimYield?: () => boolean | Promise<boolean>;
2528
onYield?: (message: string, acknowledgment?: string) => Promise<void> | void;
2629
}): AnyAgentTool {
2730
return {
@@ -43,7 +46,12 @@ export function createSessionsYieldTool(opts?: {
4346
if (!opts?.onYield) {
4447
return jsonResult({ status: "error", error: "Yield not supported in this context" });
4548
}
46-
await opts.onBeforeYield?.();
49+
if (!(await opts.claimYield?.())) {
50+
return jsonResult({
51+
status: "error",
52+
error: NO_PENDING_CHILD_COMPLETION_ERROR,
53+
});
54+
}
4755
// The runtime owns the actual pause/end-turn behavior; this tool records intent.
4856
await opts.onYield(message, acknowledgment);
4957
return jsonResult({

0 commit comments

Comments
 (0)