Skip to content

Commit e33d8bb

Browse files
committed
fix(codex): preserve yielded subagent handoff
1 parent 1af8bdd commit e33d8bb

4 files changed

Lines changed: 91 additions & 5 deletions

File tree

‎extensions/codex/src/app-server/dynamic-tools.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,51 @@ describe("createCodexDynamicToolBridge", () => {
444444
expect(onAgentToolResult).toHaveBeenCalledWith(
445445
expect.objectContaining({ toolName: "sessions_spawn", isError: false }),
446446
);
447+
expect(bridge.telemetry.acceptedSessionSpawns).toEqual([
448+
{ runId: "run_5f3a9c", childSessionKey: "child-7b21" },
449+
]);
450+
});
451+
452+
it("preserves an accepted sessions_spawn after result middleware strips its details", async () => {
453+
const registry = createEmptyPluginRegistry();
454+
const handler = vi.fn(async (event: { result: AgentToolResult<unknown> }) => ({
455+
result: {
456+
...event.result,
457+
content: [{ type: "text" as const, text: "Child launch recorded." }],
458+
details: {},
459+
},
460+
}));
461+
registry.agentToolResultMiddlewares.push({
462+
pluginId: "result-compactor",
463+
pluginName: "Result Compactor",
464+
rawHandler: handler,
465+
handler,
466+
runtimes: ["codex"],
467+
source: "test",
468+
});
469+
setActivePluginRegistry(registry);
470+
const bridge = createBridgeWithToolResult(
471+
"sessions_spawn",
472+
textToolResult("Accepted: launching child session.", {
473+
status: "accepted",
474+
runId: "run_compacted",
475+
childSessionKey: "child-compacted",
476+
}),
477+
);
478+
479+
const result = await bridge.handleToolCall({
480+
threadId: "thread-1",
481+
turnId: "turn-1",
482+
callId: "call-compacted",
483+
namespace: null,
484+
tool: "sessions_spawn",
485+
arguments: { task: "scan logs" },
486+
});
487+
488+
expect(result).toEqual(expectInputText("Child launch recorded."));
489+
expect(bridge.telemetry.acceptedSessionSpawns).toEqual([
490+
{ runId: "run_compacted", childSessionKey: "child-compacted" },
491+
]);
447492
});
448493

449494
it("retains only MCP App preview details for OpenClaw transcript projection", async () => {
@@ -500,6 +545,7 @@ describe("createCodexDynamicToolBridge", () => {
500545
});
501546

502547
expect(result.success).toBe(false);
548+
expect(bridge.telemetry.acceptedSessionSpawns).toEqual([]);
503549
});
504550

505551
it("treats accepted goal tool statuses (created / updated) as successful dynamic tool calls", async () => {

‎extensions/codex/src/app-server/dynamic-tools.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ import { emitTrustedDiagnosticEvent } from "openclaw/plugin-sdk/diagnostic-runti
4545
import { expectDefined } from "openclaw/plugin-sdk/expect-runtime";
4646
import type { ImageContent, TextContent } from "openclaw/plugin-sdk/llm";
4747
import { normalizeOpenAIToolSchemas } from "openclaw/plugin-sdk/provider-tools";
48-
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
48+
import {
49+
asOptionalRecord,
50+
isRecord,
51+
normalizeOptionalString,
52+
} from "openclaw/plugin-sdk/string-coerce-runtime";
4953
import {
5054
DEFAULT_MAX_LIVE_TOOL_RESULT_CHARS,
5155
estimateToolResultTextChars,
@@ -381,10 +385,24 @@ export type CodexDynamicToolBridge = {
381385
toolMediaUrls: string[];
382386
toolAudioAsVoice: boolean;
383387
successfulCronAdds?: number;
388+
acceptedSessionSpawns: Array<{ runId: string; childSessionKey: string }>;
384389
quarantinedTools: CodexDynamicToolSchemaQuarantine[];
385390
};
386391
};
387392

393+
function normalizeAcceptedSessionSpawn(result: unknown): {
394+
runId: string;
395+
childSessionKey: string;
396+
} | null {
397+
const details = asOptionalRecord(asOptionalRecord(result)?.details);
398+
if (!details || details.status !== "accepted") {
399+
return null;
400+
}
401+
const runId = normalizeOptionalString(details.runId);
402+
const childSessionKey = normalizeOptionalString(details.childSessionKey);
403+
return runId && childSessionKey ? { runId, childSessionKey } : null;
404+
}
405+
388406
/** Namespace attached to OpenClaw-owned dynamic tools exposed to Codex. */
389407
const CODEX_OPENCLAW_DYNAMIC_TOOL_NAMESPACE = "openclaw";
390408

@@ -490,6 +508,7 @@ export function createCodexDynamicToolBridge(params: {
490508
messagingToolSourceReplyPayloads: [],
491509
toolMediaUrls: [],
492510
toolAudioAsVoice: false,
511+
acceptedSessionSpawns: [],
493512
quarantinedTools,
494513
};
495514
const middlewareRunner = createAgentToolResultMiddlewareRunner({
@@ -654,6 +673,14 @@ export function createCodexDynamicToolBridge(params: {
654673
result: middlewareResult,
655674
});
656675
const resultIsError = rawIsError || isToolResultError(result);
676+
// A successful spawn is durable before presentation middleware can rewrite details.
677+
const acceptedSessionSpawn =
678+
toolName === "sessions_spawn" && !rawIsError
679+
? normalizeAcceptedSessionSpawn(telemetryRawResult)
680+
: null;
681+
if (acceptedSessionSpawn) {
682+
telemetry.acceptedSessionSpawns.push(acceptedSessionSpawn);
683+
}
657684
const finalResultFailureKind = resolveToolResultFailureKind(result);
658685
const resultFailureKind = rawResultFailureKind ?? finalResultFailureKind;
659686
const observerResult =

‎extensions/codex/src/app-server/event-projector.commentary.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ describe("CodexAppServerEventProjector commentary projection", () => {
647647
expect(result.lastAssistant).toBeUndefined();
648648
});
649649

650-
it("preserves sessions_yield detection in attempt results", () => {
650+
it("preserves accepted session spawns as yield continuation evidence", () => {
651651
const projector = new CodexAppServerEventProjector(
652652
{
653653
prompt: "hello",
@@ -664,8 +664,20 @@ describe("CodexAppServerEventProjector commentary projection", () => {
664664
TURN_ID,
665665
);
666666

667-
const result = projector.buildResult(buildEmptyToolTelemetry(), { yieldDetected: true });
667+
const result = projector.buildResult(
668+
{
669+
...buildEmptyToolTelemetry(),
670+
acceptedSessionSpawns: [
671+
{ runId: "child-run", childSessionKey: "agent:main:subagent:child" },
672+
],
673+
},
674+
{ yieldDetected: true },
675+
);
668676

669677
expect(result.yieldDetected).toBe(true);
678+
expect(result.acceptedSessionSpawns).toEqual([
679+
{ runId: "child-run", childSessionKey: "agent:main:subagent:child" },
680+
]);
681+
expect(result.replayMetadata).toEqual({ hadPotentialSideEffects: true, replaySafe: false });
670682
});
671683
});

‎extensions/codex/src/app-server/event-projector.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type CodexAppServerToolTelemetry = {
7373
toolMediaUrls?: string[];
7474
toolAudioAsVoice?: boolean;
7575
successfulCronAdds?: number;
76-
};
76+
} & Pick<EmbeddedRunAttemptResult, "acceptedSessionSpawns">;
7777

7878
export class CodexAppServerEventProjector {
7979
private readonly assistantProjection: CodexAssistantProjection;
@@ -403,7 +403,7 @@ export class CodexAppServerEventProjector {
403403
const toolMetas = this.toolProgressProjection.toolMetas;
404404
const hadPotentialSideEffects =
405405
toolTelemetry.didSendViaMessagingTool ||
406-
(toolTelemetry.successfulCronAdds ?? 0) > 0 ||
406+
Boolean(toolTelemetry.successfulCronAdds || toolTelemetry.acceptedSessionSpawns?.length) ||
407407
this.generatedMediaProjection.hasGeneratedMedia() ||
408408
this.toolProgressProjection.hasPotentialSideEffects;
409409
return {
@@ -439,6 +439,7 @@ export class CodexAppServerEventProjector {
439439
hostOwnedToolMediaUrls: this.generatedMediaProjection.buildHostOwnedMediaUrls(toolTelemetry),
440440
toolAudioAsVoice: toolTelemetry.toolAudioAsVoice,
441441
successfulCronAdds: toolTelemetry.successfulCronAdds,
442+
acceptedSessionSpawns: toolTelemetry.acceptedSessionSpawns,
442443
cloudCodeAssistFormatError: false,
443444
attemptUsage: projectedUsage,
444445
...(this.completedCompactionCount > 0

0 commit comments

Comments
 (0)