fix(agent): preserve streamed replies after model failures (#6494)

* fix(agent): preserve streamed replies after model failures

* test(webui): settle lazy imports and async settings saves

* test(agent): synchronize estimation checks at the operation boundary

* docs(webui): explain retained replies after stream failures
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-27 16:22:33 +08:00
1 parent b6536bfc5e
commit 219c8bb2bc
31 files changed
+462 -27

No files matched your search

@@ -12,7 +12,7 @@ describe("model request failure copy", () => {
expect(titles).toEqual([
"Model provider authorization failed",
"Model provider quota is unavailable",
"Could not connect to the model provider",
"Model connection failed",
"Model provider request timed out",
"Model provider rate limit reached",
"Model provider service error",
+29
View File
@@ -154,3 +154,32 @@ describe("RecoveryNotice", () => {
expect(screen.getByRole("button", { name: "Continue" })).toBeEnabled();
});
});
it("shows a saved reply without treating the failed request as completed", async () => {
const onContinue = vi.fn().mockResolvedValue(undefined);
const onDismiss = vi.fn().mockResolvedValue(undefined);
const { rerender } = render(
<RecoveryNotice
state={{ ...INTERRUPTED, reason: "model_request_failed", failure_error_kind: "connection", reply_preserved: true }}
onContinue={onContinue}
onDismiss={onDismiss}
/>,
);
expect(screen.getByRole("alert")).toHaveTextContent("Model connection failed");
expect(screen.getByRole("alert")).toHaveTextContent("Reply saved. You can continue.");
fireEvent.click(screen.getByRole("button", { name: "Continue" }));
await waitFor(() => expect(screen.queryByRole("alert")).not.toBeInTheDocument());
expect(onContinue).toHaveBeenCalledOnce();
rerender(
<RecoveryNotice
state={{ ...INTERRUPTED, recovery_id: "no-output", reason: "model_request_failed", failure_error_kind: "connection" }}
onContinue={onContinue}
onDismiss={onDismiss}
/>,
);
expect(screen.getByRole("alert")).toHaveTextContent("Model connection failed");
expect(screen.getByRole("alert")).toHaveTextContent("Try again later or choose another model.");
});
@@ -140,6 +140,7 @@ describe("Inline shared proxy setup", () => {
expect(input).toBeDisabled();
fireEvent.keyDown(setup, { key: "Escape" });
expect(setup).toBeInTheDocument();
await waitFor(() => expect(requestMutationMock).toHaveBeenCalledTimes(2));
await act(async () => { resolveSave(saved); });
await waitFor(() => expect(screen.queryByRole("dialog", { name: "Shared proxy" })).not.toBeInTheDocument());
expect(screen.getByRole("dialog", { name: "Docs MCP" })).toBe(parent);
+2 -2
View File
@@ -5207,7 +5207,7 @@ describe("ThreadShell", () => {
});
const banner = await screen.findByRole("alert");
expect(banner).toHaveTextContent("无法连接模型提供商");
expect(banner).toHaveTextContent("模型连接异常");
expect(banner).toHaveTextContent(
"已尝试 4 次,仍未成功。可稍后继续或切换模型。",
);
@@ -5224,7 +5224,7 @@ describe("ThreadShell", () => {
});
});
expect(screen.getAllByRole("alert")).toHaveLength(1);
expect(screen.getByRole("alert")).toHaveTextContent("无法连接模型提供商");
expect(screen.getByRole("alert")).toHaveTextContent("模型连接异常");
expect(screen.getByRole("button", { name: "继续" })).toBeEnabled();
}
+20
View File
@@ -822,6 +822,26 @@ describe("useNanobotStream", () => {
},
);
it.each([true, false, undefined])("keeps saved-reply facts consistent live and on attach (%s)", (preserved) => {
const fake = fakeClient();
const { result } = renderHook(() => useNanobotStream("chat-preserved", EMPTY_MESSAGES), {
wrapper: wrap(fake.client),
});
const state = {
recovery_id: "preserved-1", status: "awaiting_user" as const,
reason: "model_request_failed", failure_error_kind: "connection",
...(preserved === undefined ? {} : { reply_preserved: preserved }),
};
act(() => fake.emit("chat-preserved", {
event: "recovery_state", chat_id: "chat-preserved", ...state,
}));
expect(result.current.recoveryState).toEqual(state);
act(() => fake.emit("chat-preserved", {
event: "attached", chat_id: "chat-preserved", recovery_state: state,
}));
expect(result.current.recoveryState).toEqual(state);
});
it("does not let historical recovered state clear a later active turn", () => {
const fake = fakeClient();
const { result } = renderHook(