Skip to content

Commit 2333bd5

Browse files
fix: keep yielded parent turns nonterminal (#95996)
* fix: keep yielded parent turns nonterminal * fix: type chat final stop reasons * test: isolate yielded session reconciliation * fix: refresh yielded lifecycle generated surfaces * test: register Codex lifecycle test shard --------- Co-authored-by: FullerStackDev <[email protected]>
1 parent 57d926b commit 2333bd5

22 files changed

Lines changed: 742 additions & 20 deletions

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13230,6 +13230,7 @@ public struct ChatFinalEvent: Codable, Sendable {
1323013230
public let message: AnyCodable?
1323113231
public let usage: AnyCodable?
1323213232
public let stopreason: String?
13233+
public let yielded: Bool?
1323313234

1323413235
public init(
1323513236
runid: String,
@@ -13240,7 +13241,8 @@ public struct ChatFinalEvent: Codable, Sendable {
1324013241
state: String,
1324113242
message: AnyCodable? = nil,
1324213243
usage: AnyCodable? = nil,
13243-
stopreason: String? = nil)
13244+
stopreason: String? = nil,
13245+
yielded: Bool? = nil)
1324413246
{
1324513247
self.runid = runid
1324613248
self.sessionkey = sessionkey
@@ -13251,6 +13253,7 @@ public struct ChatFinalEvent: Codable, Sendable {
1325113253
self.message = message
1325213254
self.usage = usage
1325313255
self.stopreason = stopreason
13256+
self.yielded = yielded
1325413257
}
1325513258

1325613259
private enum CodingKeys: String, CodingKey {
@@ -13263,6 +13266,7 @@ public struct ChatFinalEvent: Codable, Sendable {
1326313266
case message
1326413267
case usage
1326513268
case stopreason = "stopReason"
13269+
case yielded
1326613270
}
1326713271
}
1326813272

‎extensions/codex/src/app-server/run-attempt-finalize.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,11 @@ export async function finalizeCodexAttempt(
447447
}
448448
: {
449449
phase: "end",
450-
...buildLifecycleTerminalMeta({ aborted: finalAborted, timedOut: effectiveTimedOut }),
450+
...buildLifecycleTerminalMeta({
451+
aborted: finalAborted,
452+
timedOut: effectiveTimedOut,
453+
yielded: toolState.yieldDetected,
454+
}),
451455
},
452456
);
453457
return {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it } from "vitest";
2+
import { buildCodexLifecycleTerminalMeta } from "./run-attempt-lifecycle-terminal.js";
3+
4+
describe("buildCodexLifecycleTerminalMeta", () => {
5+
it("marks sessions_yield as a paused parent continuation", () => {
6+
expect(
7+
buildCodexLifecycleTerminalMeta({
8+
aborted: false,
9+
timedOut: false,
10+
yielded: true,
11+
}),
12+
).toEqual({
13+
yielded: true,
14+
livenessState: "paused",
15+
stopReason: "end_turn",
16+
});
17+
});
18+
19+
it("keeps ordinary successful turns terminal", () => {
20+
expect(
21+
buildCodexLifecycleTerminalMeta({
22+
aborted: false,
23+
timedOut: false,
24+
yielded: false,
25+
}),
26+
).toBeUndefined();
27+
});
28+
29+
it("keeps cancellation stronger than a stale yield signal", () => {
30+
expect(
31+
buildCodexLifecycleTerminalMeta({
32+
aborted: true,
33+
timedOut: false,
34+
yielded: true,
35+
}),
36+
).toEqual({
37+
aborted: true,
38+
status: "cancelled",
39+
stopReason: "stop",
40+
});
41+
});
42+
});

‎extensions/codex/src/app-server/run-attempt-lifecycle-controller.ts‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
CodexDynamicToolCallResponse,
2222
CodexServerNotification,
2323
} from "./protocol.js";
24+
import { buildCodexLifecycleTerminalMeta } from "./run-attempt-lifecycle-terminal.js";
2425
import { emitCodexAppServerEvent } from "./run-attempt-lifecycle.js";
2526
import type { CodexAttemptResources } from "./run-attempt-resources.js";
2627
import type { CodexAttemptTurnState } from "./run-attempt-turn-state.js";
@@ -146,22 +147,18 @@ export function createCodexAttemptLifecycleController(
146147
});
147148
state.lifecycleTerminalEmitted = true;
148149
};
149-
const buildLifecycleTerminalMeta = (input: { aborted: boolean; timedOut: boolean }) => {
150+
const buildLifecycleTerminalMeta = (input: {
151+
aborted: boolean;
152+
timedOut: boolean;
153+
yielded?: boolean;
154+
}) => {
150155
const abortFields = input.aborted
151156
? resolveAgentRunAbortLifecycleFields(runAbortController.signal)
152157
: undefined;
153-
if (input.timedOut || abortFields?.stopReason === "timeout") {
154-
return {
155-
aborted: true,
156-
status: "timed_out",
157-
stopReason: "timeout",
158-
timeoutPhase: "provider",
159-
providerStarted: true,
160-
} as const;
161-
}
162-
return input.aborted
163-
? ({ aborted: true, status: "cancelled", stopReason: "stop" } as const)
164-
: undefined;
158+
return buildCodexLifecycleTerminalMeta({
159+
...input,
160+
abortStopReason: abortFields?.stopReason,
161+
});
165162
};
166163
const executionPhaseKeys = new Set<string>();
167164
const emitExecutionPhaseOnce = (
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export function buildCodexLifecycleTerminalMeta(input: {
2+
aborted: boolean;
3+
timedOut: boolean;
4+
yielded?: boolean;
5+
abortStopReason?: string;
6+
}) {
7+
if (input.timedOut || input.abortStopReason === "timeout") {
8+
return {
9+
aborted: true,
10+
status: "timed_out",
11+
stopReason: "timeout",
12+
timeoutPhase: "provider",
13+
providerStarted: true,
14+
} as const;
15+
}
16+
if (input.yielded && !input.aborted) {
17+
return {
18+
yielded: true,
19+
livenessState: "paused",
20+
stopReason: "end_turn",
21+
} as const;
22+
}
23+
return input.aborted
24+
? ({ aborted: true, status: "cancelled", stopReason: "stop" } as const)
25+
: undefined;
26+
}

‎packages/gateway-protocol/src/index.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,19 @@ describe("validateWakeParams", () => {
849849
});
850850

851851
describe("validateChatEvent", () => {
852+
it("accepts an explicitly yielded final turn", () => {
853+
expect(
854+
validateChatEvent({
855+
runId: "run-yielded",
856+
sessionKey: "agent:main:main",
857+
seq: 1,
858+
state: "final",
859+
stopReason: "end_turn",
860+
yielded: true,
861+
}),
862+
).toBe(true);
863+
});
864+
852865
it("accepts v4 chat delta text and replacement markers", () => {
853866
expect(
854867
validateChatEvent({

‎packages/gateway-protocol/src/schema/logs-chat.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export const ChatFinalEventSchema = closedObject({
160160
message: Type.Optional(Type.Unknown()),
161161
usage: Type.Optional(Type.Unknown()),
162162
stopReason: Type.Optional(Type.String()),
163+
yielded: Type.Optional(Type.Literal(true)),
163164
});
164165

165166
/** Terminal event for user-initiated or coordinator-initiated cancellation. */
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, it } from "vitest";
2+
import { isAgentLifecycleYieldedWaiting } from "./agent-lifecycle-parent-state.js";
3+
4+
describe("agent lifecycle parent state", () => {
5+
it("recognizes the explicit yielded waiting contract", () => {
6+
expect(
7+
isAgentLifecycleYieldedWaiting({
8+
phase: "end",
9+
yielded: true,
10+
livenessState: "paused",
11+
stopReason: "end_turn",
12+
}),
13+
).toBe(true);
14+
});
15+
16+
it.each([
17+
{ phase: "error", yielded: true, livenessState: "paused", stopReason: "end_turn" },
18+
{ phase: "end", yielded: false, livenessState: "paused", stopReason: "end_turn" },
19+
{ phase: "end", yielded: true, livenessState: "working", stopReason: "end_turn" },
20+
{ phase: "end", yielded: true, livenessState: "paused", stopReason: "completed" },
21+
{
22+
phase: "end",
23+
yielded: true,
24+
livenessState: "paused",
25+
stopReason: "end_turn",
26+
aborted: true,
27+
},
28+
{
29+
phase: "end",
30+
yielded: true,
31+
livenessState: "paused",
32+
stopReason: "end_turn",
33+
status: "cancelled",
34+
},
35+
{
36+
phase: "end",
37+
yielded: true,
38+
livenessState: "paused",
39+
stopReason: "end_turn",
40+
timeoutPhase: "provider",
41+
},
42+
{
43+
phase: "end",
44+
yielded: true,
45+
livenessState: "paused",
46+
stopReason: "end_turn",
47+
error: "continuation failed",
48+
},
49+
])("keeps incomplete or terminal variants out of waiting state", (event) => {
50+
expect(isAgentLifecycleYieldedWaiting(event)).toBe(false);
51+
});
52+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/** Shared classification for an ended model turn whose parent task is still waiting. */
2+
type AgentLifecycleParentStateEvent = {
3+
phase?: unknown;
4+
yielded?: unknown;
5+
livenessState?: unknown;
6+
stopReason?: unknown;
7+
aborted?: unknown;
8+
status?: unknown;
9+
timeoutPhase?: unknown;
10+
error?: unknown;
11+
};
12+
13+
export function isAgentLifecycleYieldedWaiting(event: AgentLifecycleParentStateEvent): boolean {
14+
return (
15+
event.phase === "end" &&
16+
event.yielded === true &&
17+
event.livenessState === "paused" &&
18+
event.stopReason === "end_turn" &&
19+
event.aborted !== true &&
20+
event.status !== "cancelled" &&
21+
event.status !== "timed_out" &&
22+
event.timeoutPhase == null &&
23+
event.error == null
24+
);
25+
}

‎src/gateway/server-chat.agent-events.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4640,6 +4640,84 @@ describe("agent event handler", () => {
46404640
});
46414641
});
46424642

4643+
it("marks a yielded final as waiting instead of parent-task completion", () => {
4644+
const { broadcast, handler, chatRunState } = createHarness({
4645+
resolveSessionKeyForRun: () => "agent:main:main",
4646+
});
4647+
4648+
chatRunState.registry.add("run-yielded", {
4649+
sessionKey: "agent:main:main",
4650+
clientRunId: "client-yielded",
4651+
});
4652+
handler({
4653+
runId: "run-yielded",
4654+
seq: 1,
4655+
stream: "assistant",
4656+
ts: Date.now(),
4657+
data: { text: "Waiting for registered continuation work." },
4658+
});
4659+
handler({
4660+
runId: "run-yielded",
4661+
seq: 2,
4662+
stream: "lifecycle",
4663+
ts: Date.now(),
4664+
data: {
4665+
phase: "end",
4666+
yielded: true,
4667+
livenessState: "paused",
4668+
stopReason: "end_turn",
4669+
},
4670+
});
4671+
4672+
const finalCall = requireCall(
4673+
chatBroadcastCalls(broadcast).find(([, payload]) => payload.state === "final"),
4674+
"yielded final chat call",
4675+
);
4676+
expectPayloadFields(finalCall[1], {
4677+
runId: "client-yielded",
4678+
sessionKey: "agent:main:main",
4679+
state: "final",
4680+
stopReason: "end_turn",
4681+
yielded: true,
4682+
});
4683+
});
4684+
4685+
it("does not let stale yield metadata override an aborted lifecycle", () => {
4686+
const { broadcast, handler, chatRunState } = createHarness({
4687+
resolveSessionKeyForRun: () => "agent:main:main",
4688+
});
4689+
4690+
chatRunState.registry.add("run-aborted", {
4691+
sessionKey: "agent:main:main",
4692+
clientRunId: "client-aborted",
4693+
});
4694+
handler({
4695+
runId: "run-aborted",
4696+
seq: 1,
4697+
stream: "lifecycle",
4698+
ts: Date.now(),
4699+
data: {
4700+
phase: "end",
4701+
aborted: true,
4702+
yielded: true,
4703+
livenessState: "paused",
4704+
stopReason: "end_turn",
4705+
},
4706+
});
4707+
4708+
const finalCall = requireCall(
4709+
chatBroadcastCalls(broadcast).find(([, payload]) => payload.state === "error"),
4710+
"aborted final chat call",
4711+
);
4712+
expectPayloadFields(finalCall[1], {
4713+
runId: "client-aborted",
4714+
sessionKey: "agent:main:main",
4715+
state: "error",
4716+
stopReason: "end_turn",
4717+
});
4718+
expect(finalCall[1]).not.toHaveProperty("yielded");
4719+
});
4720+
46434721
it("omits spawnedBy from chat broadcasts for non-subagent sessions", () => {
46444722
vi.mocked(loadGatewaySessionRow).mockReturnValue({
46454723
key: "agent:main:main",

0 commit comments

Comments
 (0)