Assistant: stop stale clients from resurrecting cleared threads
All checks were successful
Deploy to VPS / test (push) Successful in 1m37s
Deploy to VPS / deploy (push) Successful in 28s

Root cause of the "chunks" clear bug: conversation history is
server-owned, but MAF seeds a thread from whatever the client sends
when no snapshot exists. Any stale tab (open across a clear or from an
older session) still holds the old conversation in memory, and its next
question re-persisted the whole thing — clears appeared to peel off one
layer at a time.

The agent middleware now drops client-sent history for threads with no
stored snapshot, keeping only the last user turn (fresh conversations
and empty-messages hydration requests pass through untouched). Verified
live: simulated a cleared-thread stale tab, asked from it, and only the
new turn was persisted.

Also: clearThread surfaces DELETE failures as a toast instead of
silently reloading, and conftest sets a dummy OPENAI_API_KEY so tests
can import app.main.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Carlos Escalante
2026-07-04 20:06:35 -06:00
parent 15fa18411a
commit 700e1edbb4
4 changed files with 89 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ import {
} from "@copilotkit/react-core/v2";
import { useCopilotAction } from "@copilotkit/react-core";
import { MessageSquarePlus, Sparkles } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import ConfirmDialog from "@/components/ConfirmDialog";
import { PageHeader } from '@/components/page-header';
@@ -78,10 +79,13 @@ export default function Asistente() {
const clearThread = async () => {
setClearing(true);
try {
await fetch("/api/v1/chat/thread", {
const res = await fetch("/api/v1/chat/thread", {
method: "DELETE",
credentials: "include",
});
if (!res.ok) {
throw new Error(`DELETE /chat/thread → ${res.status}`);
}
// Full reload is the reliable reset: CopilotKit keeps chat state in
// more places than agent.messages (clearing it in place left the
// conversation rendered), while a fresh boot hydrates from the
@@ -89,7 +93,9 @@ export default function Asistente() {
window.location.reload();
} catch (err) {
console.error("[asistente] clear thread failed:", err);
toast.error("No se pudo borrar la conversación. Intenta de nuevo.");
setClearing(false);
setConfirmClear(false);
}
};