fix(server): keep a gotgenes foreground answer on replay

A foreground @gotgenes/pi-subagents child finds its session file only
through the live runtime marker, which history does not keep. Without a
file, emit the spawn result text as the child's assistant message.
This commit is contained in:
Mohamed Boudra
2026-10-30 12:01:23 +01:00
parent c1bcfd930b
commit bce306238b
2 changed files with 27 additions and 1 deletions
@@ -130,6 +130,18 @@ describe("@gotgenes/pi-subagents adapter", () => {
.filter((event) => event.event.type === "upsert")
.map((event) => (event.event.type === "upsert" ? event.event.status : null)),
).toEqual(["running", "completed"]);
expect(
events.flatMap((event) => (event.event.type === "timeline" ? [event.event] : [])),
).toEqual([
{
type: "timeline",
id: "call_502323",
item: {
type: "assistant_message",
text: "Agent completed in 4.7s (1 tool uses, 9.0k token).\nAgent ID: 848583f5-6749-4a1\n\nThe workspace is empty.",
},
},
]);
});
test("correlates a background notification and result collection", async () => {
const source = readSubagentFixture(new URL("./fixtures/background.json", import.meta.url));
@@ -1,4 +1,5 @@
import { z } from "zod";
import { extractTextFromToolResult } from "../../tool-call-mapper.js";
import { outputFileFromToolResult } from "../child-session.js";
import { GOTGENES_CHILD_SESSION_MARKER, gotgenesRuntimeBridge } from "./runtime-bridge.js";
import type { PiExtension, PiExtensionToolCall } from "../contract.js";
@@ -92,6 +93,10 @@ export const gotgenesPiSubagents: PiExtension = {
outputFileFromToolResult(call.result) ?? pendingFilesByAgent.get(details.data.agentId);
pendingFilesByAgent.delete(details.data.agentId);
const childSessions = childSession(call.callId, file);
const childStatus = status(details.data.status);
// Without a session file, as on replay, the child's answer is the tool result.
const answer =
!file && childStatus !== "running" ? extractTextFromToolResult(call.result)?.trim() : "";
return {
detail,
subagents: [
@@ -101,8 +106,17 @@ export const gotgenesPiSubagents: PiExtension = {
title,
description,
toolCallId: call.callId,
status: status(details.data.status),
status: childStatus,
},
...(answer
? [
{
type: "timeline" as const,
id: call.callId,
item: { type: "assistant_message" as const, text: answer },
},
]
: []),
],
childSessions,
};