fix(desktop): keep plugin warnings when saved history reloads

Finishing a turn replaces the live transcript with saved history, which
never contains plugin warnings (they come from the start reply and stream
notices), so the explanation of a missing plugin disappeared after the first
turn. Carry the session's live plugin-warning lines into canonical history
at their original position by time.
This commit is contained in:
abeatrix committed 2026-10-09 10:57:10 -07:00
1 parent ed981296cb
commit 90df99574b
4 files changed
+131 -5

No files matched your search

@@ -1,6 +1,9 @@
import { describe, expect, it } from "vitest";
import type { ChatMessage } from "@/lib/chat-schema";
import { canReplaceFailedTurn } from "./history-reconciliation";
import {
canReplaceFailedTurn,
withLivePluginIssues,
} from "./history-reconciliation";
const row = (
id: string,
@@ -44,3 +47,51 @@ describe("failed turn history reconciliation", () => {
);
});
});
describe("withLivePluginIssues", () => {
const message = (
id: string,
createdAt: number,
extra: Partial<ChatMessage> = {},
): ChatMessage => ({
id,
sessionId: "s1",
role: "assistant",
content: id,
createdAt,
...extra,
});
const warning = (id: string, createdAt: number) =>
message(id, createdAt, {
role: "status",
meta: { messageKind: "plugin_issue" },
});
it("keeps live plugin warnings in their place when history replaces the transcript", () => {
const history = [
message("user-1", 10, { role: "user" }),
message("answer-1", 30),
];
const merged = withLivePluginIssues(
[
warning("start-warning", 5),
message("streamed", 20),
warning("notice", 25),
],
history,
);
expect(merged.map((item) => item.id)).toEqual([
"start-warning",
"user-1",
"notice",
"answer-1",
]);
});
it("returns history unchanged when there are no live warnings", () => {
const history = [message("answer-1", 30)];
expect(withLivePluginIssues([message("streamed", 20)], history)).toBe(
history,
);
});
});
@@ -27,3 +27,34 @@ export function canReplaceFailedTurn(
// continuation without a new prompt). Its stable ID still identifies it.
return !current.slice(0, -1).some((message) => message.id === savedError.id);
}
/** Chat lines the webview shows but the saved transcript never contains. */
export const PLUGIN_ISSUE_MESSAGE_KIND = "plugin_issue";
/**
* Carries the live plugin-warning lines into canonical history, each at its
* original position by time. Plugin warnings come from the session start
* reply and stream notices, not from the persisted transcript, so replacing
* the live messages with saved history would otherwise drop them.
*/
export function withLivePluginIssues(
current: ChatMessage[],
history: ChatMessage[],
): ChatMessage[] {
const savedIds = new Set(history.map((message) => message.id));
const liveOnly = current.filter(
(message) =>
message.meta?.messageKind === PLUGIN_ISSUE_MESSAGE_KIND &&
!savedIds.has(message.id),
);
if (liveOnly.length === 0) return history;
const merged = [...history];
for (const message of liveOnly) {
const index = merged.findIndex(
(candidate) => candidate.createdAt > message.createdAt,
);
if (index === -1) merged.push(message);
else merged.splice(index, 0, message);
}
return merged;
}
@@ -5053,9 +5053,28 @@ describe("useChatSession", () => {
};
}
if (request?.action === "send") {
return { ok: true };
return { ok: true, queued: true };
}
}
if (command === "read_session_messages") {
// Saved history never contains the plugin warnings.
return [
{
id: "saved-user",
sessionId: args?.sessionId,
role: "user",
content: "First prompt",
createdAt: 1,
},
{
id: "saved-answer",
sessionId: args?.sessionId,
role: "assistant",
content: "Saved answer",
createdAt: Date.now() + 60_000,
},
];
}
return [];
},
);
@@ -5102,6 +5121,27 @@ describe("useChatSession", () => {
expect(
current.messages.some((message) => message.content === "compacting"),
).toBe(false);
// Finishing the turn replaces the live transcript with saved history;
// the warnings must survive that.
await act(async () => {
chatEventHandler?.({
sessionId: current.sessionId,
stream: "chat_done",
chunk: JSON.stringify({ reason: "completed" }),
ts: Date.now(),
index: 3,
});
await new Promise((resolve) => setTimeout(resolve, 400));
});
expect(
current.messages.some((message) => message.content === "Saved answer"),
).toBe(true);
expect(
current.messages
.filter((message) => message.meta?.messageKind === "plugin_issue")
.map((message) => message.content.slice(0, 22)),
).toEqual(['Plugin "broken-import"', 'Plugin "broken-setup" ']);
});
it("explains a failed turn with the latest core error log", async () => {
@@ -30,7 +30,11 @@ import {
resolveCredentialError,
resolveCredentialFailureHint,
} from "@/hooks/chat-session/helpers";
import { canReplaceFailedTurn } from "@/hooks/chat-session/history-reconciliation";
import {
canReplaceFailedTurn,
PLUGIN_ISSUE_MESSAGE_KIND,
withLivePluginIssues,
} from "@/hooks/chat-session/history-reconciliation";
import type {
AgentChunkEvent,
AskQuestionRequestItem,
@@ -150,7 +154,7 @@ function makePluginIssueMessage(
role: "status",
content,
createdAt: Date.now(),
meta: { messageKind: "plugin_issue" },
meta: { messageKind: PLUGIN_ISSUE_MESSAGE_KIND },
};
}
@@ -999,7 +1003,7 @@ export function useChatSession(environmentId: string) {
// until canonical history contains this run's terminal error.
if (!canReplaceFailedTurn(sessionMessages, historyMessages))
return prev;
return historyMessages;
return withLivePluginIssues(sessionMessages, historyMessages);
});
},
[],