Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(agent): filter prior assistant runtime errors from outbound history (#19763)

p0ny9749c179be2f
diff --git a/frontend/src/plugins/agent/components/AgentInput.test.ts b/frontend/src/plugins/agent/components/AgentInput.test.ts
index 8b5b959b94..487fdb9dc4 100644
--- a/frontend/src/plugins/agent/components/AgentInput.test.ts
+++ b/frontend/src/plugins/agent/components/AgentInput.test.ts
@@ -415,6 +415,171 @@ describe("AgentInput", () => {
});
});
+ test("filters stored assistant runtime errors from outbound history without mutating the transcript", async () => {
+ const store = useAgentStore();
+ const chatId = store.currentChatId!;
+
+ store.addMessage({
+ chatId,
+ role: "user",
+ content: "inspect this page",
+ });
+ store.addMessage({
+ chatId,
+ role: "assistant",
+ content: "Error: upstream timeout",
+ metadata: {
+ error: "upstream timeout",
+ route: "/projects/demo",
+ },
+ });
+
+ mockRunAgentLoop.mockResolvedValue({
+ kind: "completed",
+ text: "Done",
+ success: true,
+ });
+
+ const wrapper = mount(AgentInput, {
+ global: {
+ plugins: [pinia, i18n],
+ },
+ });
+
+ await wrapper.find("textarea").setValue("try again");
+ await findButtonByText(wrapper, "Send")!.trigger("click");
+ await flushPromises();
+
+ const [messages] = mockRunAgentLoop.mock.calls.at(-1) ?? [];
+ expect(messages).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ role: "system", content: "system-prompt" }),
+ expect.objectContaining({ role: "user", content: "inspect this page" }),
+ expect.objectContaining({ role: "user", content: "try again" }),
+ ])
+ );
+ expect(messages).not.toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ role: "assistant",
+ content: "Error: upstream timeout",
+ }),
+ ])
+ );
+ expect(
+ store.getMessages(chatId).some((message) => {
+ return (
+ message.role === "assistant" &&
+ message.content === "Error: upstream timeout" &&
+ message.metadata?.error === "upstream timeout"
+ );
+ })
+ ).toBe(true);
+ });
+
+ test("keeps assistant messages that start with Error when they are not tagged as runtime failures", async () => {
+ const store = useAgentStore();
+ const chatId = store.currentChatId!;
+
+ store.addMessage({
+ chatId,
+ role: "assistant",
+ content: "Error: validation failed for the previous request.",
+ });
+
+ mockRunAgentLoop.mockResolvedValue({
+ kind: "completed",
+ text: "Done",
+ success: true,
+ });
+
+ const wrapper = mount(AgentInput, {
+ global: {
+ plugins: [pinia, i18n],
+ },
+ });
+
+ await wrapper.find("textarea").setValue("what should I fix?");
+ await findButtonByText(wrapper, "Send")!.trigger("click");
+ await flushPromises();
+
+ const [messages] = mockRunAgentLoop.mock.calls.at(-1) ?? [];
+ expect(messages).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ role: "assistant",
+ content: "Error: validation failed for the previous request.",
+ }),
+ expect.objectContaining({
+ role: "user",
+ content: "what should I fix?",
+ }),
+ ])
+ );
+ });
+
+ test("keeps tool errors and normal history in outbound history", async () => {
+ const store = useAgentStore();
+ const chatId = store.currentChatId!;
+
+ store.addMessage({
+ chatId,
+ role: "user",
+ content: "open settings",
+ });
+ store.addMessage({
+ chatId,
+ role: "assistant",
+ content: "Trying the settings button.",
+ });
+ store.addMessage({
+ chatId,
+ role: "tool",
+ toolCallId: "tool-click",
+ content: "Error: element not found",
+ metadata: {
+ runId: "run-1",
+ },
+ });
+
+ mockRunAgentLoop.mockResolvedValue({
+ kind: "completed",
+ text: "Done",
+ success: true,
+ });
+
+ const wrapper = mount(AgentInput, {
+ global: {
+ plugins: [pinia, i18n],
+ },
+ });
+
+ await wrapper.find("textarea").setValue("retry with the menu");
+ await findButtonByText(wrapper, "Send")!.trigger("click");
+ await flushPromises();
+
+ const [messages] = mockRunAgentLoop.mock.calls.at(-1) ?? [];
+ expect(messages).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ role: "system", content: "system-prompt" }),
+ expect.objectContaining({ role: "user", content: "open settings" }),
+ expect.objectContaining({
+ role: "assistant",
+ content: "Trying the settings button.",
+ }),
+ expect.objectContaining({
+ role: "tool",
+ toolCallId: "tool-click",
+ content: "Error: element not found",
+ }),
+ expect.objectContaining({
+ role: "user",
+ content: "retry with the menu",
+ }),
+ ])
+ );
+ });
+
test("shows retry CTA and reruns interrupted turns from the last stable input", async () => {
const store = useAgentStore();
const chatId = store.currentChatId!;
diff --git a/frontend/src/plugins/agent/components/AgentInput.vue b/frontend/src/plugins/agent/components/AgentInput.vue
index b918d7bd89..cfd866baca 100644
--- a/frontend/src/plugins/agent/components/AgentInput.vue
+++ b/frontend/src/plugins/agent/components/AgentInput.vue
@@ -7,6 +7,7 @@ import { useRoute, useRouter } from "vue-router";
import type { DomRefSuggestion } from "../dom";
import { lazyExtractDomRefSuggestions } from "../dom";
import { runAgentLoop } from "../logic/agentLoop";
+import { buildOutboundHistory } from "../logic/outboundHistory";
import { buildSystemPrompt } from "../logic/prompt";
import { createToolExecutor, getToolDefinitions } from "../logic/tools";
import type { AgentAskUserOption, Message } from "../logic/types";
@@ -230,10 +231,7 @@ const getCurrentPageSnapshot = () => ({
});
const buildChatHistory = (chatId: string, systemPrompt: string): Message[] => {
- return [
- { role: "system", content: systemPrompt },
- ...agentStore.getMessages(chatId),
- ];
+ return buildOutboundHistory(systemPrompt, agentStore.getMessages(chatId));
};
const handleOutcome = (
diff --git a/frontend/src/plugins/agent/logic/outboundHistory.ts b/frontend/src/plugins/agent/logic/outboundHistory.ts
new file mode 100644
index 0000000000..7e83e5bc9c
--- /dev/null
+++ b/frontend/src/plugins/agent/logic/outboundHistory.ts
@@ -0,0 +1,39 @@
+import type { AgentMessage, Message } from "./types";
+
+const isHistoricalAssistantError = (message: AgentMessage): boolean => {
+ return (
+ message.role === "assistant" &&
+ typeof message.metadata?.error === "string" &&
+ Boolean(message.metadata.error.trim())
+ );
+};
+
+const cloneMessage = ({
+ role,
+ content,
+ toolCallId,
+ toolCalls,
+}: Message): Message => ({
+ role,
+ content,
+ toolCallId,
+ toolCalls: toolCalls?.map((toolCall) => ({ ...toolCall })),
+});
+
+export const sanitizeOutboundHistory = (
+ messages: AgentMessage[]
+): Message[] => {
+ return messages
+ .filter((message) => !isHistoricalAssistantError(message))
+ .map(cloneMessage);
+};
+
+export const buildOutboundHistory = (
+ systemPrompt: string,
+ messages: AgentMessage[]
+): Message[] => {
+ return [
+ { role: "system", content: systemPrompt },
+ ...sanitizeOutboundHistory(messages),
+ ];
+};