fix(confidence-engine): supply focused deconstruction schema

This commit is contained in:
2026-09-06 16:21:26 +01:00
parent 188dd04ab9
commit a93b6798cc
6 changed files with 110 additions and 17 deletions
+49 -6
View File
@@ -6,7 +6,8 @@
* API response targetNodeId regardless of what the model returns.
*/
import { describe, it, expect, vi } from "vitest";
import { afterEach, beforeEach, describe, it, expect, vi } from "vitest";
import { focusedDeconstructJsonSchema } from "@/lib/graph/focused-investigation";
// ── helpers ──────────────────────────────────────────────────────────────
@@ -36,6 +37,14 @@ function makeMockProvider(inventedTargetNodeId) {
// ── Boundary test ────────────────────────────────────────────────────────
describe("focused-deconstruct targetNodeId identity boundary", () => {
beforeEach(() => {
vi.resetModules();
});
afterEach(() => {
vi.doUnmock("@/lib/llm/provider");
});
it("request targetNodeId overrides model-invented targetNodeId", async () => {
const requestTargetNodeId = "nk04xvk"; // original graph node ID
const inventedModelId = "invented-model-id";
@@ -73,6 +82,45 @@ describe("focused-deconstruct targetNodeId identity boundary", () => {
expect(json.targetNodeId).not.toBe(inventedModelId);
});
it("supplies the focused-deconstruction schema through the provider seam", async () => {
const generateReconstruction = vi.fn().mockResolvedValue({
response: {
targetNodeId: "model-id",
observations: [],
uncertainties: [],
assumptions: [],
relationships: [],
possibleFollowUpQuestions: [],
},
providerApiPath: "/api/chat",
providerExecution: { chatRequestAttempted: true },
});
vi.doMock("@/lib/llm/provider", () => ({
getProvider: () => ({ generateReconstruction }),
}));
const { POST } = await import("../app/api/focused-investigation/deconstruct/route.js");
const response = await POST(new Request("http://localhost/api/focused-investigation/deconstruct", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
targetNodeId: "node-id",
targetLabel: "label",
targetDescription: "description",
centralStatement: "central statement",
question: "question?",
answer: "answer.",
}),
}));
expect(response.status).toBe(200);
expect(generateReconstruction).toHaveBeenCalledWith(
expect.any(String),
process.env.OLLAMA_MODEL,
focusedDeconstructJsonSchema,
);
});
it("semantic fields pass through unchanged from model", async () => {
const mockObs = ["doc is minimal", "processes in founder's head"];
const mockUnc = ["whether formal docs can capture tacit knowledge"];
@@ -157,9 +205,6 @@ describe("focused-deconstruct targetNodeId identity boundary", () => {
});
it("full identity path: request → response → contribution", async () => {
// Reset modules to avoid mock leakage from earlier tests
vi.resetModules();
const originalNodeId = "nk04xvk";
const modelInventedId = "investigation_node_responsibility_distribution_autonomy";
@@ -231,8 +276,6 @@ describe("focused-deconstruct targetNodeId identity boundary", () => {
});
it("provider envelope fields do not leak into API response", async () => {
vi.resetModules();
vi.doMock("@/lib/llm/provider", () => ({
getProvider: () => ({
generateReconstruction: vi.fn().mockResolvedValue({
+26 -5
View File
@@ -5,6 +5,7 @@ import {
createOpenAIReconstructionProvider,
getProvider,
normaliseOpenAITransportResponse,
reconstructionJsonSchema,
} from "@/lib/llm/provider.js";
import { reconstructionV2Schema } from "@/lib/reconstruction/schema.js";
import { z } from "zod";
@@ -40,11 +41,7 @@ describe("OllamaLlmProvider chat capability detection", () => {
messages: [{ role: "user", content: "prompt" }],
stream: false,
});
expect(chatRequest.format).toBeTypeOf("object");
expect(chatRequest.format).not.toBe("json");
const formatText = JSON.stringify(chatRequest.format);
expect(formatText).toContain("relationship");
expect(formatText).toContain("evidenceType");
expect(chatRequest.format).toEqual(reconstructionJsonSchema);
expect(result).toMatchObject({
response: {},
providerApiPath: "/api/chat",
@@ -63,6 +60,30 @@ describe("OllamaLlmProvider chat capability detection", () => {
}
});
it("uses a supplied structured-output schema for the chat request", async () => {
const originalBaseUrl = process.env.OLLAMA_BASE_URL;
const outputSchema = {
type: "object",
properties: { focused: { type: "string" } },
required: ["focused"],
};
const fetchSpy = vi.fn()
.mockResolvedValueOnce({ ok: true, text: async () => "" })
.mockResolvedValueOnce({ ok: true, json: async () => ({ message: { content: "{}" } }) });
vi.stubGlobal("fetch", fetchSpy);
process.env.OLLAMA_BASE_URL = "http://ollama.test";
try {
__resetChatSupportForTests();
await getProvider().generateReconstruction("prompt", "configured-model", outputSchema);
expect(JSON.parse(fetchSpy.mock.calls[1][1].body).format).toEqual(outputSchema);
} finally {
vi.unstubAllGlobals();
if (originalBaseUrl === undefined) delete process.env.OLLAMA_BASE_URL;
else process.env.OLLAMA_BASE_URL = originalBaseUrl;
}
});
it("reports chat-skipped generate fallback execution", async () => {
const originalBaseUrl = process.env.OLLAMA_BASE_URL;
const fetchSpy = vi.fn()