@@ -834,18 +834,43 @@ describe("tui-event-handlers: handleAgentEvent", () => {
|
834 | 834 | ); |
835 | 835 | }); |
836 | 836 | |
837 | | -it("refreshes history after a non-local chat final", () => { |
838 | | -const { state, loadHistory, handleChatEvent } = createHandlersHarness({ |
| 837 | +it("does not reload history on final with displayable text for external runs (#87922)", () => { |
| 838 | +const { state, chatLog, loadHistory, handleChatEvent } = createHandlersHarness({ |
839 | 839 | state: { activeChatRunId: null }, |
840 | 840 | }); |
841 | 841 | |
| 842 | +// Simulate an external (non-local) run delivering a final event with text. |
| 843 | +// loadHistory() must NOT be called because it does clearAll() + rebuild |
| 844 | +// from server data, and the server may not have persisted this message |
| 845 | +// yet, causing the just-rendered message to vanish. |
842 | 846 | handleChatEvent({ |
843 | | -runId: "external-run", |
| 847 | +runId: "run-external", |
| 848 | +sessionKey: state.currentSessionKey, |
| 849 | +state: "final", |
| 850 | +message: { content: [{ type: "text", text: "assistant reply" }] }, |
| 851 | +}); |
| 852 | + |
| 853 | +expect(chatLog.finalizeAssistant).toHaveBeenCalledWith( |
| 854 | +expect.stringContaining("assistant reply"), |
| 855 | +"run-external", |
| 856 | +); |
| 857 | +expect(loadHistory).not.toHaveBeenCalled(); |
| 858 | +}); |
| 859 | + |
| 860 | +it("reloads history on final when external run has no message", () => { |
| 861 | +const { state, chatLog, loadHistory, handleChatEvent } = createHandlersHarness({ |
| 862 | +state: { activeChatRunId: null }, |
| 863 | +}); |
| 864 | + |
| 865 | +// When the final event has no message, the reload is needed to sync |
| 866 | +// with server state since there is no local content to preserve. |
| 867 | +handleChatEvent({ |
| 868 | +runId: "run-external-empty", |
844 | 869 | sessionKey: state.currentSessionKey, |
845 | 870 | state: "final", |
846 | | -message: { content: [{ type: "text", text: "done" }] }, |
847 | 871 | }); |
848 | 872 | |
| 873 | +expect(chatLog.dropAssistant).toHaveBeenCalledWith("run-external-empty"); |
849 | 874 | expect(loadHistory).toHaveBeenCalledTimes(1); |
850 | 875 | }); |
851 | 876 | |
|