feat(confidence-engine): synthesize understanding from focused findings
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import React from "react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import {
|
||||
ScenarioResultPanels,
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
submitScenarioForStartCase,
|
||||
} from "@/components/scenario-form.jsx";
|
||||
import { deriveFindingsFromContributions, normalizeFindings, validateSingleFinding } from "@/lib/graph/finding-helpers.js";
|
||||
import { synthesizeFromFindings } from "@/components/scenario-form.jsx";
|
||||
|
||||
// ── Simulated appendFocusedContribution logic (mirrors ScenarioForm) ─────────
|
||||
|
||||
@@ -144,3 +145,156 @@ describe("Contribution → Finding derivation seam", () => {
|
||||
expect(typeof normalizeFindings).toBe("function");
|
||||
});
|
||||
});
|
||||
|
||||
// ── Synthesis trigger regressions (v0.50) ───────────────────
|
||||
|
||||
describe("Synthesis trigger — focused findings commit (STATE-B)", () => {
|
||||
let capturedFetchCalls;
|
||||
let capturedCU;
|
||||
|
||||
beforeEach(() => {
|
||||
capturedFetchCalls = [];
|
||||
capturedCU = "previous understanding";
|
||||
|
||||
global.fetch = vi.fn(async (url, init) => {
|
||||
if (url === "/api/cases/synthesis") {
|
||||
capturedFetchCalls.push(JSON.parse(init.body));
|
||||
return new Response(
|
||||
JSON.stringify({ currentUnderstanding: "Reconstructed understanding." }),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
}
|
||||
return new Response(JSON.stringify({}), { status: 200 });
|
||||
});
|
||||
});
|
||||
|
||||
function simulateAppendWithSynthesis(contribs, findings, contribution) {
|
||||
const seq = contribs.length + 1;
|
||||
const storedContribution = { ...contribution, sequence: seq, id: `contrib-${String(seq).padStart(4, "0")}` };
|
||||
const newFindingsRaw = deriveFindingsFromContributions([storedContribution]).findings;
|
||||
|
||||
// Simulate appendFocusedContribution body (synchronous snapshot)
|
||||
const completeNextFindings = normalizeFindings([...findings, ...newFindingsRaw]);
|
||||
const mergedContribs = [...contribs, storedContribution];
|
||||
const mergedFindings = [...findings, ...newFindingsRaw];
|
||||
|
||||
// Trigger synthesis if new findings exist
|
||||
if (newFindingsRaw.length === 0) return { contribs: mergedContribs, findings: completeNextFindings };
|
||||
|
||||
void synthesizeFromFindings(fetch, {
|
||||
situationGraph: { centralStatement: "x" },
|
||||
findings: completeNextFindings,
|
||||
}).then((res) => {
|
||||
if (res.ok && res.data?.currentUnderstanding) {
|
||||
capturedCU = res.data.currentUnderstanding;
|
||||
}
|
||||
});
|
||||
|
||||
return { contribs: mergedContribs, findings: completeNextFindings };
|
||||
}
|
||||
|
||||
/* ── A: new Finding triggers synthesis once ─────────────── */
|
||||
|
||||
it("A — one Contribution derives one Finding → synthesis called exactly once", () => {
|
||||
const contrib = { targetNodeId: "n-1", observations: ["Revenue dropped 22%"] };
|
||||
simulateAppendWithSynthesis([], [], contrib);
|
||||
|
||||
expect(capturedFetchCalls).toHaveLength(1);
|
||||
expect(capturedFetchCalls[0].findings).toHaveLength(1);
|
||||
});
|
||||
|
||||
/* ── B: complete explicit nextFindings ─────────────────── */
|
||||
|
||||
it("B — existing Finding + new Finding both present in synthesis request", () => {
|
||||
const existing = deriveFindingsFromContributions([
|
||||
{ ...{ targetNodeId: "n-a" }, observations: ["Existing fact"], sequence: 1, id: "contrib-0001" },
|
||||
]).findings;
|
||||
|
||||
simulateAppendWithSynthesis(
|
||||
[{ id: "contrib-0001", observations: ["Existing fact"], sequence: 1 }],
|
||||
normalizeFindings(existing),
|
||||
{ targetNodeId: "n-b", observations: ["New fact"] },
|
||||
);
|
||||
|
||||
expect(capturedFetchCalls[0].findings).toHaveLength(2);
|
||||
expect(capturedFetchCalls[0].findings[0].proposition).toContain("Existing");
|
||||
expect(capturedFetchCalls[0].findings[1].proposition).toContain("New");
|
||||
});
|
||||
|
||||
/* ── C: no stale React state ─────────────────────────── */
|
||||
|
||||
it("C — newly derived Finding is already present in synthesis request (not via post-setter read)", () => {
|
||||
// Simulate: old findings = empty, but contribution produces a new finding.
|
||||
// The synthesis request MUST contain the newly derived finding.
|
||||
simulateAppendWithSynthesis(
|
||||
[], // no existing findings — simulates stale pre-setter state
|
||||
[], // same — if we read stale state this would be wrong
|
||||
{ targetNodeId: "n-9", observations: ["Derive me now"] },
|
||||
);
|
||||
|
||||
expect(capturedFetchCalls[0].findings).toHaveLength(1);
|
||||
expect(capturedFetchCalls[0].findings[0].sourceObservation).toBe("Derive me now");
|
||||
});
|
||||
|
||||
/* ── D: multiple Findings still one call ─────────────── */
|
||||
|
||||
it("D — one Contribution derives multiple Findings → synthesis called exactly once", () => {
|
||||
const contrib = { targetNodeId: "n-2", observations: ["Fact A", "Fact B"] };
|
||||
simulateAppendWithSynthesis([], [], contrib);
|
||||
|
||||
expect(capturedFetchCalls).toHaveLength(1);
|
||||
expect(capturedFetchCalls[0].findings).toHaveLength(2);
|
||||
});
|
||||
|
||||
/* ── E: zero new Findings ───────────────────────────── */
|
||||
|
||||
it("E — Contribution with empty observations → synthesis NOT called", () => {
|
||||
const contrib = { targetNodeId: "n-3", observations: [] };
|
||||
simulateAppendWithSynthesis([], [], contrib);
|
||||
|
||||
expect(capturedFetchCalls).toHaveLength(0);
|
||||
});
|
||||
|
||||
/* ── F: narrative replacement ───────────────────────── */
|
||||
|
||||
it("F — synthesis response replaces CU exactly (no append)", async () => {
|
||||
capturedCU = "old understanding"; // pre-setter value
|
||||
|
||||
const contrib = { targetNodeId: "n-4", observations: ["Reconstruction fact"] };
|
||||
simulateAppendWithSynthesis([], [], contrib);
|
||||
|
||||
await new Promise((r) => setTimeout(r, 10)); // settle microtask
|
||||
|
||||
expect(capturedCU).toBe("Reconstructed understanding.");
|
||||
expect(capturedCU).not.toContain("old");
|
||||
});
|
||||
|
||||
/* ── G: synthesis failure ───────────────────────────── */
|
||||
|
||||
it("G — synthesis failure preserves Contribution, Findings, and previous CU", async () => {
|
||||
capturedCU = "previous understanding";
|
||||
|
||||
// Override fetch for this test to simulate failure
|
||||
global.fetch = vi.fn(async (url) => {
|
||||
if (url === "/api/cases/synthesis") {
|
||||
return new Response(
|
||||
JSON.stringify({ success: false, error: "provider timeout" }),
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
return new Response(JSON.stringify({}), { status: 200 });
|
||||
});
|
||||
|
||||
const contrib = { targetNodeId: "n-5", observations: ["Failure test fact"] };
|
||||
simulateAppendWithSynthesis([], [], contrib);
|
||||
|
||||
await new Promise((r) => setTimeout(r, 10)); // settle microtask
|
||||
|
||||
// CU unchanged
|
||||
expect(capturedCU).toBe("previous understanding");
|
||||
|
||||
// synthesis was called exactly once (we know from capturedFetchCalls)
|
||||
// We verified the failure scenario — no fallback append occurred.
|
||||
expect(global.fetch).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user