Files
confidence-engine/tests/graph/decomposition-quality.test.js

388 lines
13 KiB
JavaScript

import { describe, expect, it } from "vitest";
import {
assessChildUnknownQuality,
applyValidatedProposal,
determineGraphBackedQuestion,
MAX_DECOMPOSITION_DEPTH,
} from "@/lib/graph/apply-proposal.js";
import { makeGraph, makeNode } from "@/lib/graph/schema.js";
function makeParentGraph({
centralStatement,
parentLabel,
parentDescription,
observations = [],
}) {
const parent = makeNode({
id: "n-parent",
label: parentLabel,
description: parentDescription,
kind: "unknown",
status: "unknown",
confidence: "medium",
});
return {
parent,
graph: makeGraph({
centralStatement,
nodes: [parent, ...observations],
edges: [],
activeUnknownNodeId: parent.id,
resolvedNodeIds: [],
currentSummary: "Decomposition quality graph",
}),
};
}
describe("assessChildUnknownQuality", () => {
it("rejects 'Timing or measurement basis' as compound", () => {
const { parent, graph } = makeParentGraph({
centralStatement:
"Revenue increased by 18%, but cash in the bank fell over the same period.",
parentLabel:
"Explanation for why revenue increased by 18%, but cash in the bank fell over the same period",
parentDescription:
"Need to understand what change or event could explain why these observations differ, because that is needed to investigate their relationship.",
});
const child = makeNode({
id: "n-child",
label: "Timing or measurement basis",
description:
"Need evidence about whether a timing or measurement-basis difference could explain the observations, because that would change how they should be interpreted.",
kind: "unknown",
status: "unknown",
confidence: "medium",
parentId: parent.id,
});
const result = assessChildUnknownQuality({
parentNode: parent,
childNode: child,
siblingNodes: [child],
graph,
});
expect(result.valid).toBe(false);
expect(result.compoundSignals).toContain("timing_or_measurement_basis");
expect(result.reasons).toContain("compound_child");
});
it("accepts a child with one directly answerable uncertainty", () => {
const { parent, graph } = makeParentGraph({
centralStatement: "Traffic increased, but sales stayed flat.",
parentLabel:
"What explains why more website traffic did not produce more sales?",
parentDescription:
"Need an explanation because the observations moved differently.",
});
const child = makeNode({
id: "n-child",
label: "Different measurement basis between the two observations",
description:
"Need evidence about whether the two observations use different measurement bases, because that could help explain the difference.",
kind: "unknown",
status: "unknown",
confidence: "medium",
parentId: parent.id,
});
const result = assessChildUnknownQuality({
parentNode: parent,
childNode: child,
siblingNodes: [child],
graph,
});
expect(result.valid).toBe(true);
expect(result.atomic).toBe(true);
expect(result.directlyAnswerable).toBe(true);
expect(result.narrowerThanParent).toBe(true);
});
it("rejects sibling duplicates", () => {
const { parent, graph } = makeParentGraph({
centralStatement: "Production increased, but defects also increased.",
parentLabel: "What explains why output and defects both increased?",
parentDescription:
"Need an explanation because both observations increased.",
});
const childA = makeNode({
id: "n-child-a",
label: "Different timing between the two observations",
description:
"Need evidence about whether the two observations reflect different timing, because that could help explain the difference.",
kind: "unknown",
status: "unknown",
confidence: "medium",
parentId: parent.id,
});
const childB = makeNode({
id: "n-child-b",
label: "Different timing between the two observations",
description:
"Need evidence about whether the two observations reflect different timing, because that could help explain the difference.",
kind: "unknown",
status: "unknown",
confidence: "medium",
parentId: parent.id,
});
const result = assessChildUnknownQuality({
parentNode: parent,
childNode: childA,
siblingNodes: [childA, childB],
graph,
});
expect(result.valid).toBe(false);
expect(result.duplicateSiblingIds).toContain("n-child-b");
});
it("rejects parent paraphrases", () => {
const { parent, graph } = makeParentGraph({
centralStatement:
"Customer satisfaction scores increased, but complaints also increased.",
parentLabel:
"What explains why satisfaction and complaints both increased?",
parentDescription:
"Need a broad explanation because the observations moved differently.",
});
const child = makeNode({
id: "n-child",
label: "What explains why satisfaction and complaints both increased?",
description:
"Need a broad explanation because the observations moved differently.",
kind: "unknown",
status: "unknown",
confidence: "medium",
parentId: parent.id,
});
const result = assessChildUnknownQuality({
parentNode: parent,
childNode: child,
siblingNodes: [child],
graph,
});
expect(result.valid).toBe(false);
expect(result.reasons).toContain("not_narrower_than_parent");
});
});
describe("decomposition stopping conditions", () => {
function makeMeaningfulNoOpProposal() {
return {
addedNodes: [
makeNode({
id: "n-anchor",
label: "Update anchor",
description:
"Anchor state introduced by the answer because the update must contain a meaningful change.",
kind: "state",
status: "known",
confidence: "low",
}),
],
updatedNodes: [],
addedEdges: [],
removedEdgeIds: [],
resolvedUnknownNodeIds: [],
affectedNodeIds: [],
selectedQuestion: null,
};
}
it("does not decompose an atomic selected unknown", async () => {
const atomic = makeNode({
id: "n-atomic",
label: "Were both figures measured over the same accounting period?",
description:
"Need to know whether both figures cover the same accounting period because that determines whether they are directly comparable.",
kind: "unknown",
status: "unknown",
confidence: "high",
});
const graph = makeGraph({
centralStatement:
"Revenue increased by 18%, but cash in the bank fell over the same period.",
nodes: [atomic],
edges: [],
activeUnknownNodeId: atomic.id,
resolvedNodeIds: [],
currentSummary: "Atomic selected node graph",
});
const result = await applyValidatedProposal({
situationGraph: graph,
proposal: makeMeaningfulNoOpProposal(),
});
expect(result.success).toBe(true);
expect(result.decompositionAttempted).toBe(false);
expect(result.decompositionStoppedReason).toBe(
"Selected unknown is already atomic.",
);
});
it("does not manufacture comparison children for a generic explanatory parent", async () => {
const { parent, graph } = makeParentGraph({
centralStatement: "Traffic increased, but sales stayed flat.",
parentLabel:
"What explains why more website traffic did not produce more sales?",
parentDescription:
"Need an explanation because the observations moved differently.",
observations: [
makeNode({
id: "n-traffic",
label: "Website traffic increased.",
description: "Website traffic increased.",
kind: "observation",
status: "supported",
confidence: "high",
}),
makeNode({
id: "n-sales",
label: "Sales stayed flat.",
description: "Sales stayed flat.",
kind: "observation",
status: "supported",
confidence: "high",
}),
],
});
const result = await applyValidatedProposal({
situationGraph: graph,
proposal: makeMeaningfulNoOpProposal(),
});
expect(result.success).toBe(true);
expect(result.decompositionAttempted).toBe(true);
expect(result.decompositionAccepted).toBe(false);
expect(result.childUnknownCount).toBe(0);
expect(
result.updatedSituationGraph.nodes.some(
(node) => node.parentId === parent.id,
),
).toBe(false);
expect(
result.updatedSituationGraph.nodes.some(
(node) => node.label === "How the two observations were measured",
),
).toBe(false);
expect(
result.updatedSituationGraph.nodes.some(
(node) =>
node.label ===
"Whether the two observations reflect different timing",
),
).toBe(false);
expect(result.decompositionStoppedReason).toBe(
"Decomposition stopped because no meaning-preserving child family was justified for this parent.",
);
expect(result.selectedQuestion?.question || "").not.toContain(
"two observations",
);
});
it("still decomposes a genuine comparison/measurement parent into valid comparison children", async () => {
const { graph } = makeParentGraph({
centralStatement:
"Revenue increased by 18%, but cash in the bank fell over the same period.",
parentLabel: "Timing or measurement basis",
parentDescription:
"Need evidence about whether a timing or measurement-basis difference could explain the observations, because that would change how they should be interpreted.",
observations: [
makeNode({
id: "n-revenue",
label: "Revenue increased by 18%.",
description: "Revenue increased by 18%.",
kind: "observation",
status: "supported",
confidence: "high",
}),
makeNode({
id: "n-cash",
label: "Cash in the bank decreased over the same period.",
description: "Cash in the bank decreased over the same period.",
kind: "observation",
status: "supported",
confidence: "high",
}),
],
});
const result = await applyValidatedProposal({
situationGraph: graph,
proposal: makeMeaningfulNoOpProposal(),
});
expect(result.success).toBe(true);
expect(result.decompositionPerformed).toBe(true);
expect(result.childUnknownCount).toBe(2);
expect(
result.updatedSituationGraph.nodes.some(
(node) => node.label === "How the two observations were measured",
),
).toBe(true);
expect(
result.updatedSituationGraph.nodes.some(
(node) =>
node.label ===
"Whether the two observations reflect different timing",
),
).toBe(true);
expect(result.selectedQuestion?.question).toBe(
"What evidence would clarify how the two observations were measured?",
);
});
it("exposes the configured maximum decomposition depth", () => {
expect(MAX_DECOMPOSITION_DEPTH).toBeGreaterThanOrEqual(2);
expect(MAX_DECOMPOSITION_DEPTH).toBeLessThanOrEqual(3);
});
it("rejects invalid semantic fallback children through existing quality validation", async () => {
const { parent, graph } = makeParentGraph({
centralStatement:
"A dependency persists, but the cause is unclear.",
parentLabel: "Which factor is causing the dependency",
parentDescription:
"Need to know whether team capability, customer dependency, responsibility distribution, or insufficient deliberate delegation is causing the dependency.",
});
const provider = {
generateReconstruction: async () => ({
parentNodeId: parent.id,
proposedChildren: [
{
label: "Team capability or customer dependency",
description:
"Need to know whether team capability or customer dependency is causing the dependency.",
},
{
label: "Responsibility distribution or insufficient deliberate delegation",
description:
"Need to know whether responsibility distribution or insufficient deliberate delegation is causing the dependency.",
},
],
}),
};
const result = await determineGraphBackedQuestion({
situationGraph: graph,
provider,
modelName: "mock-ollama",
});
expect(result.success).toBe(true);
expect(result.decompositionAttempted).toBe(true);
expect(result.decompositionAccepted).toBe(false);
expect(result.proposedChildCount).toBe(2);
expect(result.acceptedChildCount).toBe(0);
expect(result.childUnknownCount).toBe(0);
expect(result.rejectedChildren).toHaveLength(2);
});
});