fix(subagents): include tool role in subagent completion output

This commit is contained in:
Peter Steinberger
2026-02-18 02:57:25 +01:00
parent 985ec71c55
commit 0dd97feb41
3 changed files with 40 additions and 5 deletions

View File

@@ -232,6 +232,36 @@ describe("subagent announce formatting", () => {
expect(msg).toContain("tool output line 1");
});
it("falls back to latest tool output when assistant reply is empty", async () => {
const { runSubagentAnnounceFlow } = await import("./subagent-announce.js");
chatHistoryMock.mockResolvedValueOnce({
messages: [
{
role: "assistant",
content: [{ type: "text", text: "" }],
},
{
role: "tool",
content: [{ type: "text", text: "tool output line 2" }],
},
],
});
readLatestAssistantReplyMock.mockResolvedValue("");
await runSubagentAnnounceFlow({
childSessionKey: "agent:main:subagent:worker",
childRunId: "run-tool-fallback-2",
requesterSessionKey: "agent:main:main",
requesterDisplayKey: "main",
...defaultOutcomeAnnounce,
waitForCompletion: false,
});
const call = agentSpy.mock.calls[0]?.[0] as { params?: { message?: string } };
const msg = call?.params?.message as string;
expect(msg).toContain("tool output line 2");
});
it("keeps full findings and includes compact stats", async () => {
const { runSubagentAnnounceFlow } = await import("./subagent-announce.js");
sessionStore = {

View File

@@ -37,6 +37,14 @@ type ToolResultMessage = {
content?: unknown;
};
function isToolResultMessage(msg: unknown): boolean {
if (!msg || typeof msg !== "object") {
return false;
}
const role = (msg as { role?: unknown }).role;
return role === "toolResult" || role === "tool";
}
function extractToolResultText(content: unknown): string {
if (typeof content === "string") {
return sanitizeTextContent(content);
@@ -60,13 +68,10 @@ async function readLatestToolResult(sessionKey: string): Promise<string | undefi
const messages = Array.isArray(history?.messages) ? history.messages : [];
for (let i = messages.length - 1; i >= 0; i -= 1) {
const msg = messages[i];
if (!msg || typeof msg !== "object") {
if (!isToolResultMessage(msg)) {
continue;
}
const candidate = msg as ToolResultMessage;
if (candidate.role !== "toolResult") {
continue;
}
const text = extractToolResultText(candidate.content);
if (text) {
return text;

View File

@@ -127,7 +127,7 @@ export function stripToolMessages(messages: unknown[]): unknown[] {
return true;
}
const role = (msg as { role?: unknown }).role;
return role !== "toolResult";
return role !== "toolResult" && role !== "tool";
});
}