fix(agents): prevent false billing error replacing valid response text (#40616)

Merged via squash.

Prepared head SHA: 05179362b4
Co-authored-by: ingyukoh <6015960+ingyukoh@users.noreply.github.com>
Co-authored-by: altaywtf <9790196+altaywtf@users.noreply.github.com>
Reviewed-by: @altaywtf
This commit is contained in:
ingyukoh
2026-03-12 04:00:11 +09:00
committed by GitHub
parent 78b9384aa7
commit 2a18cbb110
7 changed files with 54 additions and 14 deletions

View File

@@ -166,9 +166,9 @@ export function extractAssistantText(message: unknown): string | undefined {
normalizeText: (text) => text.trim(),
}) ?? "";
const stopReason = (message as { stopReason?: unknown }).stopReason;
const errorMessage = (message as { errorMessage?: unknown }).errorMessage;
const errorContext =
stopReason === "error" || (typeof errorMessage === "string" && Boolean(errorMessage.trim()));
// Gate on stopReason only — a non-error response with a stale/background errorMessage
// should not have its content rewritten with error templates (#13935).
const errorContext = stopReason === "error";
return joined ? sanitizeUserFacingText(joined, { errorContext }) : undefined;
}

View File

@@ -199,6 +199,16 @@ describe("extractAssistantText", () => {
"Firebase downgraded us to the free Spark plan. Check whether billing should be re-enabled.",
);
});
it("preserves successful turns with stale background errorMessage", () => {
const message = {
role: "assistant",
stopReason: "end_turn",
errorMessage: "insufficient credits for embedding model",
content: [{ type: "text", text: "Handle payment required errors in your API." }],
};
expect(extractAssistantText(message)).toBe("Handle payment required errors in your API.");
});
});
describe("resolveAnnounceTarget", () => {