World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
While a source pushes, the graph overview recounts the base every few seconds instead of three times a second (#1311)
Szabo Bogdanfa77a956797e
diff --git a/web/src/pages/Graph.tsx b/web/src/pages/Graph.tsxindex a878a785..fc4e4cb9 100644--- a/web/src/pages/Graph.tsx+++ b/web/src/pages/Graph.tsx@@ -397,7 +397,9 @@ export function Graph() {canUpload: kb?.my_role !== "viewer",});const data = useQuery({- queryKey: ["graph", kb?.id, focusEntity, nodeBudget],+ queryKey: focusEntity+ ? ["graph", kb?.id, "neighborhood", focusEntity]+ : ["graph", kb?.id, "overview", nodeBudget],queryFn: () =>focusEntity? api.graphNeighborhood(kb!.id, focusEntity)diff --git a/web/src/useEventReconnect.test.ts b/web/src/useEventReconnect.test.tsindex 2e37fe5c..c54d22f1 100644--- a/web/src/useEventReconnect.test.ts+++ b/web/src/useEventReconnect.test.ts@@ -1,7 +1,7 @@import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { QueryClient, QueryObserver, type QueryKey } from "@tanstack/react-query";import { DEFAULT_STALE_MS, STREAM_KEYS, STREAM_STALE_MS, applyQueryDefaults } from "./queryDefaults";-import { SETTLE_MS, useKbEvents } from "./useKbEvents";+import { OVERVIEW_SETTLE_MS, SETTLE_MS, useKbEvents } from "./useKbEvents";import { useAlertEvents } from "./useAlertEvents";import { HIDDEN_RELEASE_MS } from "./eventStream";@@ -178,6 +178,7 @@ describe("KB stream reconnect", () => {it("flushes a pending burst together with reconnect instead of scheduling another refetch", async () => {const client = createClient();const graph = await watch(client, ["graph", "kb-a"]);+ const overview = await watch(client, ["graph", "kb-a", "overview", 200]);const documents = await watch(client, ["documents", "kb-a"]);const badge = await watch(client, ["alerts", "unread"]);const list = await watch(client, ["alerts", "list", "", 0]);@@ -190,11 +191,13 @@ describe("KB stream reconnect", () => {source.emit("error");source.emit("open");expect(graph.fetch).toHaveBeenCalledTimes(2);+ expect(overview.fetch).toHaveBeenCalledTimes(2);expect(documents.fetch).toHaveBeenCalledTimes(2);expect(badge.fetch).toHaveBeenCalledTimes(2);expect(list.fetch).toHaveBeenCalledTimes(2);- await vi.advanceTimersByTimeAsync(SETTLE_MS);+ await vi.advanceTimersByTimeAsync(OVERVIEW_SETTLE_MS);expect(graph.fetch).toHaveBeenCalledTimes(2);+ expect(overview.fetch).toHaveBeenCalledTimes(2);expect(documents.fetch).toHaveBeenCalledTimes(2);expect(badge.fetch).toHaveBeenCalledTimes(2);expect(list.fetch).toHaveBeenCalledTimes(2);@@ -203,7 +206,7 @@ describe("KB stream reconnect", () => {it("keeps normal business events coalesced", async () => {const client = createClient();- const graph = await watch(client, ["graph", "kb-a"]);+ const graph = await watch(client, ["graph", "kb-a", "neighborhood", "e1"]);const documents = await watch(client, ["documents", "kb-a"]);const { source } = mount(() => useKbEvents("kb-a"));source.emit("open");@@ -219,6 +222,26 @@ describe("KB stream reconnect", () => {expect(documents.fetch).toHaveBeenCalledTimes(2);});+ it("a burst of graph events refetches the overview once after OVERVIEW_SETTLE_MS and the neighbourhood after SETTLE_MS", async () => {+ const client = createClient();+ const overview = await watch(client, ["graph", "kb-a", "overview", 200]);+ const neighborhood = await watch(client, ["graph", "kb-a", "neighborhood", "e1"]);+ const { source } = mount(() => useKbEvents("kb-a"));+ source.emit("open");+ source.emit("graph");+ await vi.advanceTimersByTimeAsync(SETTLE_MS);+ expect(neighborhood.fetch).toHaveBeenCalledTimes(2);+ expect(overview.fetch).toHaveBeenCalledTimes(1);+ for (let elapsed = SETTLE_MS; elapsed < 1_000; elapsed += 100) {+ source.emit("graph");+ await vi.advanceTimersByTimeAsync(100);+ }+ await vi.advanceTimersByTimeAsync(OVERVIEW_SETTLE_MS - 1_000 - 1);+ expect(overview.fetch).toHaveBeenCalledTimes(1);+ await vi.advanceTimersByTimeAsync(1);+ expect(overview.fetch).toHaveBeenCalledTimes(2);+ });+it("ignores late callbacks from the old connection after switching KB", async () => {const client = createClient();const oldKb = await watch(client, ["graph", "kb-a"]);diff --git a/web/src/useKbEvents.ts b/web/src/useKbEvents.tsindex 8ca6a47f..ea7c39fe 100644--- a/web/src/useKbEvents.ts+++ b/web/src/useKbEvents.ts@@ -18,6 +18,9 @@ import { STREAM_KEYS } from "./queryDefaults";/** 一阵事件之间的静默期。抽取落事实的间隔远小于它,人眼看不出这点延迟 */export const SETTLE_MS = 300;+/** 全图 overview 要数遍整个库:推送期间几秒重数一次就够了(#1296) */+export const OVERVIEW_SETTLE_MS = 3_000;+/*** 把一阵失效合并成一次:`push` 只记键,静默 `settleMs` 后一起交给 `invalidate`,* 同一个键及其前缀只失效一次;`flush` 给卸载与重连用,攒着的刷掉而不是丢掉。@@ -37,7 +40,7 @@ export function createInvalidationCoalescer(const keys = [...pending.values()];pending.clear();for (const key of keys) {- // 正常 graph 事件可能已排了 ["graph"],重连又补上 ["graph", kbId]:+ // 正常 graph 事件可能已排了 ["graph", kbId, "neighborhood"],重连又补上 ["graph", kbId]:// 前缀已经覆盖后者,不能取消刚开始的请求再重取一遍。if (keys.some((prefix) => prefix.length < key.length && partialMatchKey(key, prefix))) continue;invalidate(key);@@ -59,12 +62,31 @@ export function useKbEvents(kbId: string | undefined) {(key) => queryClient.invalidateQueries({ queryKey: key }),SETTLE_MS,);+ // overview 到点也经快的那层刷:重连补上的 ["graph", kbId] 已覆盖它,不再多取一遍+ const overview = createInvalidationCoalescer((key) => {+ push(key);+ flush();+ }, OVERVIEW_SETTLE_MS);+ const pushGraph = () => {+ push(["graph", kbId, "neighborhood"]);+ overview.push(["graph", kbId, "overview"]);+ };+ const flushAll = () => {+ overview.flush();+ flush();+ };const unsubscribe = subscribeEvents(`/api/v1/kbs/${kbId}/events`,{- document: () => push(["documents", kbId], ["graph"]),- graph: () => push(["graph"], ["ontologyRegressions", kbId]),+ document: () => {+ push(["documents", kbId]);+ pushGraph();+ },+ graph: () => {+ push(["ontologyRegressions", kbId]);+ pushGraph();+ },// 映射探索跑完发的也是 review:Pending 那一栏得跟着刷新review: () => push(["review", kbId], ["mappings", kbId], ["ontologyRegressions", kbId]),// 一句记忆抽出了等人点头的事实(0015):对话里那张确认卡跟着长出来@@ -77,13 +99,13 @@ export function useKbEvents(kbId: string | undefined) {// 事件没有回放,staleTime 到期也不会自己发请求。断线后恢复必须补刷;// 首次正常连接交给页面已有的读取,首次连接失败再恢复则同样可能漏事件。push(...STREAM_KEYS.map((head) => [head, kbId]), ["alerts"]);- flush();+ flushAll();},);return () => {unsubscribe();// 卸载时把攒着的刷掉而不是丢掉:换页回来看到的必须是新数据- flush();+ flushAll();};}, [kbId, queryClient]);}