Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): report subagent output token truncation as failure

Totopo275fa60e00b90a
diff --git a/packages/agent-runtime/src/subagent.test.ts b/packages/agent-runtime/src/subagent.test.ts
index 1b48777aa..63f91dc36 100644
--- a/packages/agent-runtime/src/subagent.test.ts
+++ b/packages/agent-runtime/src/subagent.test.ts
@@ -367,6 +367,62 @@ describe("SubagentRun reporting", () => {
expect(result.error?.code).toBe("SUBAGENT_NO_REPORT");
});
+ it("reports truncated output as a failure rather than a clean completion", async () => {
+ const { run } = createRun();
+ run.handleEvent({
+ type: "message_end",
+ message: assistantMessage({
+ content: [{ type: "text", text: "Analysis report cut off mid-sentence..." }],
+ stopReason: "length",
+ }),
+ });
+ run.agent = {
+ prompt: vi.fn().mockResolvedValue(undefined),
+ waitForIdle: vi.fn().mockResolvedValue(undefined),
+ abort: vi.fn(),
+ };
+
+ const result = await (run as unknown as SubagentRun).run();
+
+ expect(result.status).toBe("failed");
+ expect(result.error?.code).toBe("SUBAGENT_OUTPUT_TRUNCATED");
+ expect(result.outputTruncated).toBe(true);
+ expect(result.report).toContain("The explorer subagent failed");
+ expect(result.report).toContain(
+ "The subagent response exceeded the model's output token limit and was truncated.",
+ );
+ expect(result.report).toContain("Analysis report cut off mid-sentence...");
+ });
+
+ it("clears truncated output state if a subsequent turn finishes cleanly", async () => {
+ const { run } = createRun();
+ run.handleEvent({
+ type: "message_end",
+ message: assistantMessage({
+ content: [{ type: "text", text: "Initial attempt" }],
+ stopReason: "length",
+ }),
+ });
+ run.handleEvent({
+ type: "message_end",
+ message: assistantMessage({
+ content: [{ type: "text", text: "Complete final report." }],
+ stopReason: "stop",
+ }),
+ });
+ run.agent = {
+ prompt: vi.fn().mockResolvedValue(undefined),
+ waitForIdle: vi.fn().mockResolvedValue(undefined),
+ abort: vi.fn(),
+ };
+
+ const result = await (run as unknown as SubagentRun).run();
+
+ expect(result.status).toBe("completed");
+ expect(result.outputTruncated).toBeUndefined();
+ expect(result.report).toBe("Complete final report.");
+ });
+
it("returns aborted without prompting when the parent call is already aborted", async () => {
const controller = new AbortController();
controller.abort();
diff --git a/packages/agent-runtime/src/subagent.ts b/packages/agent-runtime/src/subagent.ts
index cfe8c5eb9..759b3cd86 100644
--- a/packages/agent-runtime/src/subagent.ts
+++ b/packages/agent-runtime/src/subagent.ts
@@ -110,6 +110,8 @@ export type SubagentRunResult = {
contextCompactions?: number;
/** True when the run had to discard working history without a summary. */
contextDegraded?: boolean;
+ /** True when the delegate response hit the model's output token limit. */
+ outputTruncated?: boolean;
error?: { code: string; message: string };
};
@@ -217,6 +219,7 @@ export class SubagentRun {
private readonly opts: SubagentRunOptions;
private currentAssistant?: UiMessage;
private lastReportText = "";
+ private lastReportTruncated = false;
private turns = 0;
private toolCalls = 0;
private usage?: MessageUsage;
@@ -375,6 +378,13 @@ export class SubagentRun {
message: "The subagent finished without writing a report.",
});
}
+ if (this.lastReportTruncated) {
+ return this.result("failed", this.lastReportText, {
+ code: "SUBAGENT_OUTPUT_TRUNCATED",
+ message:
+ "The subagent response exceeded the model's output token limit and was truncated.",
+ });
+ }
return this.result("completed", this.lastReportText);
}
@@ -648,6 +658,7 @@ export class SubagentRun {
...(this.modelFailures.length ? { modelFailures: [...this.modelFailures] } : {}),
...(this.contextCompactions > 0 ? { contextCompactions: this.contextCompactions } : {}),
...(this.contextDegraded ? { contextDegraded: true } : {}),
+ ...(this.lastReportTruncated ? { outputTruncated: true } : {}),
...(error ? { error } : {}),
};
}
@@ -826,6 +837,8 @@ export class SubagentRun {
// must not clear the text an earlier turn already produced.
if (content.hasText && content.text.trim() && !failed) {
this.lastReportText = content.text;
+ this.lastReportTruncated =
+ stopReason === "length" || stopReason === "max_tokens";
}
if (retryAttempt !== undefined) {
this.currentAssistant = {