World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
Add walker + tests for the recursive proof renderer (#827)
Royce69560db5969f
diff --git a/web/src/pages/Graph.test.ts b/web/src/pages/Graph.test.tsindex eba5c1e2..bc6434d2 100644--- a/web/src/pages/Graph.test.ts+++ b/web/src/pages/Graph.test.ts@@ -18,8 +18,13 @@ vi.hoisted(() => {}});-import { fmtInterval } from "./Graph";-import type { EntityFact } from "../api";+import {+ fmtInterval,+ proofIndentClass,+ PROOF_INDENT_CAP,+ walkProofSteps,+} from "./Graph";+import type { EntityFact, ProofStep } from "../api";const base: EntityFact = {id: "f",@@ -140,4 +145,145 @@ describe("fmtInterval", () => {it("state 什么都没有时返回空串", () => {expect(fmtInterval({ ...base, temporal: "state" })).toBe("");});+});++// 递归证明树(0030)的形状:服务端 proof() 返回的 ProofStep 现在带+// premises: ProofStep[],前端把它压平成 WalkedRow[] 喂给渲染组件。+// 这里断言三件事:+// 1. 深度单调 + 跨过 has_premises 边界才 +1(叶子的 premises 为空就停)+// 2. 三层嵌套的 9-节点树产出 9 行,深度序列为 [0,1,2,3,2,3,1,2,3],+// 即 DFS 前序:父 → 第一个子 → 第一个孙 → … → 第一个叶 → 兄弟 →+// 3. 叶子行的 has_premises=false;不是叶子的行它必为 true+// A+// / \+// B C+// / \ \+// D E F+// | | |+// G H I ← 叶子(premises 为空,walker 不下钻)++function step(+ id: string,+ premises: ProofStep[] = [],+ partial: Partial<ProofStep> = {},+): ProofStep {+ return {+ seq: 0,+ fact_id: id,+ subject_id: id,+ subject: id,+ predicate_id: null,+ predicate: "→",+ object_id: null,+ object: id,+ valid_from: null,+ valid_to: null,+ confidence: 1,+ retracted: false,+ evidence: [],+ premises,+ ...partial,+ };+}++describe("walkProofSteps", () => {+ it("叶子(premises 为空)只有一个深度 0 的行", () => {+ const tree = [step("leaf")];+ const rows = walkProofSteps(tree);+ expect(rows).toHaveLength(1);+ expect(rows[0].depth).toBe(0);+ expect(rows[0].has_premises).toBe(false);+ expect(rows[0].fact_id).toBe("leaf");+ });++ it("三层嵌套的树产出 9 行,深度 0..3 单调", () => {+ const tree = [+ step("A", [+ step("B", [step("D", [step("G")]), step("E", [step("H")])]),+ step("C", [step("F", [step("I")])]),+ ]),+ ];+ const rows = walkProofSteps(tree);+ expect(rows.map((r) => r.fact_id)).toEqual([+ "A", "B", "D", "G", "E", "H", "C", "F", "I",+ ]);+ expect(rows.map((r) => r.depth)).toEqual([+ 0, 1, 2, 3, 2, 3, 1, 2, 3,+ ]);+ // 深度从不超过 4(一个 `premises` 边只 +1);从不低于 0;+ // 同一行 `premises` 内的 depth 差只可能是 +1(刚下钻)或非正(爬回祖先或平移到同层)+ for (const r of rows) {+ expect(r.depth).toBeGreaterThanOrEqual(0);+ expect(r.depth).toBeLessThanOrEqual(3);+ }+ // 进入 premises 时 +1,退出时不强制 -1(可以一次回到祖先)+ for (let i = 1; i < rows.length; i++) {+ const diff = rows[i].depth - rows[i - 1].depth;+ expect(diff).toBeLessThanOrEqual(1);+ }+ });++ it("叶子行 has_premises=false;非叶子行必为 true", () => {+ const tree = [+ step("A", [step("B", [step("leaf")])]),+ ];+ const rows = walkProofSteps(tree);+ const byId = Object.fromEntries(rows.map((r) => [r.fact_id, r]));+ expect(byId["A"].has_premises).toBe(true);+ expect(byId["B"].has_premises).toBe(true);+ expect(byId["leaf"].has_premises).toBe(false);+ });++ it("深度起点偏移:depth=2 时整棵树每个节点的深度都比直接调用多 2", () => {+ const tree = [step("A", [step("B")])];+ const at0 = walkProofSteps(tree, 0);+ const at2 = walkProofSteps(tree, 2);+ expect(at0.map((r) => r.depth)).toEqual([0, 1]);+ expect(at2.map((r) => r.depth)).toEqual([2, 3]);+ });+});++// 缩进封顶:`cn` 是纯拼接、仓库里没有 tailwind-merge,所以同时发出 `ml-4`+// 与 `ml-0` 时谁生效由样式表顺序决定——那样的封顶形同虚设。这里钉住的是+// 「深到一定层数就不再发缩进类」,而不是「再发一个类把它盖掉」+describe("proofIndentClass", () => {+ it("封顶以内:带缩进、内边距与那条竖线", () => {+ for (let depth = 0; depth < PROOF_INDENT_CAP; depth++) {+ const cls = proofIndentClass(depth);+ expect(cls).toContain("ml-4");+ expect(cls).toContain("pl-3");+ expect(cls).toContain("border-l");+ }+ });++ it("到了封顶就不再发缩进类", () => {+ for (const depth of [PROOF_INDENT_CAP, PROOF_INDENT_CAP + 1, 12]) {+ const cls = proofIndentClass(depth);+ expect(cls).not.toContain("ml-4");+ expect(cls).not.toContain("pl-3");+ expect(cls).not.toContain("border-l");+ }+ });++ it("任何深度都不会同时发出互相冲突的两个类", () => {+ for (let depth = 0; depth <= 12; depth++) {+ const parts = proofIndentClass(depth).split(/\s+/).filter(Boolean);+ for (const [a, b] of [+ ["ml-4", "ml-0"],+ ["pl-3", "pl-0"],+ ["border-l", "border-l-0"],+ ]) {+ expect(parts.includes(a) && parts.includes(b)).toBe(false);+ }+ // 同一个属性组里也不该出现两个取值+ const margins = parts.filter((c) => /^ml-/.test(c));+ expect(margins.length).toBeLessThanOrEqual(1);+ }+ });++ it("每一层都留着 mt-1,封顶只去掉横向的缩进", () => {+ for (let depth = 0; depth <= 12; depth++) {+ expect(proofIndentClass(depth)).toContain("mt-1");+ }+ });});\ No newline at end of filediff --git a/web/src/pages/Graph.tsx b/web/src/pages/Graph.tsxindex 5d152fa6..b0c42d82 100644--- a/web/src/pages/Graph.tsx+++ b/web/src/pages/Graph.tsx@@ -2526,21 +2526,100 @@ function ProofChain({ kbId, d }: { kbId: string; d: DerivedFact }) {* `<ol>`。深度的视觉上限由服务器决定(0030 推理的同一道闸),不在* 客户端重画——叶子那一步 `premises` 为空,递归停在这里*/-function ProofSteps({ kbId, steps }: { kbId: string; steps: ProofStep[] }) {+export function ProofSteps({+ kbId,+ steps,+ depth = 0,+}: {+ kbId: string;+ steps: ProofStep[];+ /** 递归深度:顶层从 0 开始,子证明每层加 1。超过 6 不再缩进(窄屏) */+ depth?: number;+}) {return (<ol className="space-y-2">- {steps.map((st) => <ProofStepRow key={st.fact_id} kbId={kbId} step={st} />)}+ {steps.map((st) => (+ <ProofStepRow key={st.fact_id} kbId={kbId} step={st} depth={depth} />+ ))}</ol>);}+/** 证明树压平后的一行:保留深度与父链,单测据此断言 */+export interface WalkedRow {+ fact_id: string;+ depth: number;+ subject: string;+ predicate: string | null;+ object: string | null;+ retracted: boolean;+ has_premises: boolean;+ evidence_count: number;+}++/** 把递归 `premises` 树压平成一行行。深度优先,子证明排在父之后。+ *+ * 这是递归渲染的「骨架」:组件递归地走 `premises`,单测通过这个函数+ * 断言「三层嵌套时,叶子不再展开,深度单调递增,相邻行深度差不超过 1」。+ * 渲染是组件的事,shape 是 walker 的事+ */+export function walkProofSteps(+ steps: ProofStep[],+ depth = 0,+): WalkedRow[] {+ const out: WalkedRow[] = [];+ const visit = (nodes: ProofStep[], d: number) => {+ for (const s of nodes) {+ out.push({+ fact_id: s.fact_id,+ depth: d,+ subject: s.subject,+ predicate: s.predicate,+ object: s.object,+ retracted: s.retracted,+ has_premises: s.premises.length > 0,+ evidence_count: s.evidence.length,+ });+ if (s.premises.length > 0) {+ visit(s.premises, d + 1);+ }+ }+ };+ visit(steps, depth);+ return out;+}++/** 缩进到这一层为止。再深就不缩了——每层 ml-4 + pl-3 是 28 px,六层已经+ * 吃掉 168 px,而面板只有 384 px 宽。服务器那道递归上限(0030)管的是链+ * 能有多长,这里管的是窄屏里还看得清 */+export const PROOF_INDENT_CAP = 6;++/** 子证明那一块的缩进类。+ *+ * **按条件拼基础类,不叠覆盖类**:`cn` 是纯拼接(见 `ui/index.tsx`),仓库里+ * 也没有 tailwind-merge。`ml-4` 与 `ml-0` 同时出现时,谁生效由生成的样式表+ * 顺序决定,跟这里写的先后无关——那样的封顶等于没封 */+export function proofIndentClass(depth: number): string {+ return depth >= PROOF_INDENT_CAP+ ? "mt-1"+ : "mt-1 ml-4 pl-3 border-l border-edge";+}+/** 单条证明步:它的「子证明」在 `step.premises` 里再渲染一次 ProofSteps。** 拆出来是为了让递归有界——这一个组件自己只画一层,premises 是再调* 一次顶层组件(同一份渲染逻辑,不另写一份),靠 `premises` 数组* 自然终止。深度的递归深度上限由服务器(0030),不在客户端画*/-function ProofStepRow({ kbId, step }: { kbId: string; step: ProofStep }) {+function ProofStepRow({+ kbId,+ step,+ depth,+}: {+ kbId: string;+ step: ProofStep;+ depth: number;+}) {return (<li className="text-fine"><div className="flex items-baseline gap-2 flex-wrap">@@ -2594,8 +2673,8 @@ function ProofStepRow({ kbId, step }: { kbId: string; step: ProofStep }) {)}</div>{step.premises.length > 0 && (- <div className="mt-1 ml-4 border-l border-edge pl-3">- <ProofSteps kbId={kbId} steps={step.premises} />+ <div className={proofIndentClass(depth)}>+ <ProofSteps kbId={kbId} steps={step.premises} depth={depth + 1} /></div>)}</li>