experiment(confidence-engine): complete openai reconstruction apparatus

This commit is contained in:
2026-09-06 10:06:06 +01:00
parent a45dd903cf
commit 215c783d11
5 changed files with 281 additions and 7 deletions
+75
View File
@@ -1,9 +1,13 @@
import { describe, expect, it, vi } from "vitest";
import {
__resetChatSupportForTests,
createOpenAIStrictSchema,
createOpenAIReconstructionProvider,
getProvider,
normaliseOpenAITransportResponse,
} from "@/lib/llm/provider.js";
import { reconstructionV2Schema } from "@/lib/reconstruction/schema.js";
import { z } from "zod";
describe("OllamaLlmProvider chat capability detection", () => {
it("uses the configured model for the chat probe and keeps the chat path", async () => {
@@ -121,6 +125,76 @@ describe("OllamaLlmProvider chat capability detection", () => {
});
describe("OpenAI reconstruction provider experiment seam", () => {
it("projects canonical optional fields as required but nullable", () => {
const nativeSchema = z.toJSONSchema(reconstructionV2Schema);
const projectedSchema = createOpenAIStrictSchema(
nativeSchema,
reconstructionV2Schema,
);
function resolveLocalRef(schema, root = projectedSchema) {
if (!schema.$ref) return schema;
return schema.$ref
.replace(/^#\//, "")
.split("/")
.reduce((value, key) => value?.[key], root);
}
function acceptsNull(schema, root = projectedSchema) {
const resolved = resolveLocalRef(schema, root);
return (
resolved?.type === "null" ||
(Array.isArray(resolved?.type) && resolved.type.includes("null")) ||
[...(resolved?.anyOf ?? []), ...(resolved?.oneOf ?? [])].some((branch) =>
acceptsNull(branch, root),
)
);
}
function assertAllPropertiesRequired(schema, root = projectedSchema) {
const resolved = resolveLocalRef(schema, root);
if (resolved?.properties) {
expect(resolved.required).toEqual(Object.keys(resolved.properties));
Object.values(resolved.properties).forEach((property) => assertAllPropertiesRequired(property, root));
}
(resolved?.anyOf ?? []).forEach((branch) => assertAllPropertiesRequired(branch, root));
(resolved?.oneOf ?? []).forEach((branch) => assertAllPropertiesRequired(branch, root));
if (resolved?.items) assertAllPropertiesRequired(resolved.items, root);
}
assertAllPropertiesRequired(projectedSchema);
const transition = projectedSchema.properties.reconstruction.properties.unexplainedTransitions.items;
expect(transition.required).toContain("entity");
expect(acceptsNull(transition.properties.entity)).toBe(true);
const inputClassification = resolveLocalRef(
projectedSchema.properties.inputClassification,
);
expect(inputClassification.required).toContain("secondaryTypes");
expect(acceptsNull(inputClassification.properties.secondaryTypes)).toBe(true);
expect(acceptsNull(transition.properties.id)).toBe(false);
});
it("removes only optional transport null placeholders", () => {
const schema = z.object({
optionalText: z.string().optional(),
nullableText: z.string().nullable(),
requiredText: z.string(),
children: z.array(z.object({ optionalChild: z.string().optional() })),
});
const nativeSchema = z.toJSONSchema(schema);
expect(normaliseOpenAITransportResponse({
optionalText: null,
nullableText: null,
requiredText: null,
children: [{ optionalChild: null }],
}, schema, nativeSchema)).toEqual({
nullableText: null,
requiredText: null,
children: [{}],
});
});
it("uses the Responses API with the canonical strict reconstruction schema", async () => {
const fetchSpy = vi.fn().mockResolvedValue({
ok: true,
@@ -158,6 +232,7 @@ describe("OpenAI reconstruction provider experiment seam", () => {
expect(schemaText).toContain("secondaryTypes");
expect(schemaText).toContain("confidence");
expect(schemaText).toContain("importance");
expect(request.text.format.schema).not.toEqual(z.toJSONSchema(reconstructionV2Schema));
expect(result).toEqual({
response: { reconstruction: "result" },
providerApiPath: "/v1/responses",