World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
Keep late chat callbacks from finishing a follow-up answer (#763)
Maya Kid078c919fad3b
diff --git a/web/src/liveAnswer.stream.test.ts b/web/src/liveAnswer.stream.test.tsnew file mode 100644index 00000000..180e12ca--- /dev/null+++ b/web/src/liveAnswer.stream.test.ts@@ -0,0 +1,70 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import { streamChat } from "./api";+import { liveAnswer, type Turn } from "./liveAnswer";++const turns = (): Turn[] => [{ role: "assistant", content: "" }];++afterEach(() => {+ vi.unstubAllGlobals();+ for (const entry of liveAnswer.get()) {+ liveAnswer.stop(entry.kbId, entry.conversationId);+ }+});++describe("live answer generation ownership", () => {+ it("keeps a follow-up streaming when the previous SSE connection closes", async () => {+ let wire!: ReadableStreamDefaultController<Uint8Array>;+ const body = new ReadableStream<Uint8Array>({ start(c) { wire = c; } });+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(body)));+ const previous = liveAnswer.begin("kb", "conversation", turns(), () => {});+ const done = vi.fn(() => previous.finish());+ streamChat("kb", { conversation_id: "conversation", message: "first" }, {+ onConversation: (id) => previous.identify(id),+ onSources: () => {},+ onStep: () => {},+ onDelta: (text) => previous.patchLast((t) => ({ ...t, content: t.content + text })),+ onDone: done,+ onError: (message) => { throw new Error(message); },+ });+ wire.enqueue(new TextEncoder().encode('event: done\ndata: {}\n\n'));+ await vi.waitFor(() => expect(done).toHaveBeenCalledTimes(1));+ const followUp = liveAnswer.begin("kb", "conversation", turns(), () => {});+ wire.close();+ await vi.waitFor(() => expect(done).toHaveBeenCalledTimes(2));+ expect(liveAnswer.entry("kb", "conversation")?.streaming).toBe(true);+ followUp.finish();+ });++ it("ignores every stale handle operation after the conversation slot is replaced", () => {+ const old = liveAnswer.begin("kb", "same", turns(), () => {});+ old.finish();+ const abort = vi.fn();+ const current = liveAnswer.begin("kb", "same", turns(), abort);+ const staleAbort = vi.fn();+ old.patchLast((t) => ({ ...t, content: "old response" }));+ old.setAbort(staleAbort);+ old.identify("wrong");+ old.finish();+ current.patchLast((t) => ({ ...t, content: t.content + "new response" }));+ current.finish();+ expect(liveAnswer.entry("kb", "same")?.turns[0].content).toBe("new response");+ expect(liveAnswer.entry("kb", "wrong")).toBeNull();+ liveAnswer.stop("kb", "same");+ expect(abort).toHaveBeenCalledTimes(1);+ expect(staleAbort).not.toHaveBeenCalled();+ });++ it("keeps pending identification and other conversations independent", () => {+ const a = liveAnswer.begin("kb", null, turns(), () => {});+ const b = liveAnswer.begin("other-kb", null, turns(), () => {});+ a.identify("a");+ b.identify("b");+ a.patchLast((t) => ({ ...t, content: "A" }));+ b.patchLast((t) => ({ ...t, content: "B" }));+ a.finish();+ expect(liveAnswer.entry("kb", "a")?.turns[0].content).toBe("A");+ expect(liveAnswer.entry("other-kb", "b")?.turns[0].content).toBe("B");+ expect(liveAnswer.entry("other-kb", "b")?.streaming).toBe(true);+ b.finish();+ });+});diff --git a/web/src/liveAnswer.ts b/web/src/liveAnswer.tsindex 0d5b7490..e008b310 100644--- a/web/src/liveAnswer.ts+++ b/web/src/liveAnswer.ts@@ -161,9 +161,12 @@ export const liveAnswer = {const slot: Slot = { live: { kbId, conversationId, turns, streaming: true }, abort };lives.set(key, slot);flush();+ // A follow-up reuses the conversation key. Late callbacks from the old+ // stream must still belong to its original slot, not the replacement.+ const owned = () => (lives.get(key) === slot ? slot : undefined);return {identify: (id: string) => {- const current = lives.get(key);+ const current = owned();if (!current) return;lives.delete(key);key = id;@@ -172,7 +175,7 @@ export const liveAnswer = {flush();},patchLast: (f) => {- const current = lives.get(key);+ const current = owned();if (!current || current.live.turns.length === 0) return;const turns = [...current.live.turns];turns[turns.length - 1] = f(turns[turns.length - 1]);@@ -180,13 +183,13 @@ export const liveAnswer = {emit();},finish: () => {- const current = lives.get(key);+ const current = owned();if (!current || !current.live.streaming) return;current.live = { ...current.live, streaming: false };flush();},setAbort: (a) => {- const current = lives.get(key);+ const current = owned();if (current) current.abort = a;},};