experiment(confidence-engine): complete openai reconstruction apparatus
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
* with a deterministic inline double (zero live model calls).
|
||||
*/
|
||||
|
||||
import { describe, expect, it, beforeAll, afterAll } from "vitest";
|
||||
import { describe, expect, it, beforeAll, afterAll, vi } from "vitest";
|
||||
import { execFile } from "child_process";
|
||||
import { writeFile, unlink } from "fs/promises";
|
||||
import { join, dirname } from "path";
|
||||
@@ -159,6 +159,62 @@ describe("start-case-experiment-helper.cjs apparatus", () => {
|
||||
// ── D — Success output ──────────────────────────────────────────
|
||||
|
||||
describe("D — success output", () => {
|
||||
it("selects the existing OpenAI provider through the helper injection seam", async () => {
|
||||
const { runStartCaseExperiment } = require(helperPath);
|
||||
const previousProvider = process.env.START_CASE_EXPERIMENT_PROVIDER;
|
||||
const previousKey = process.env.OPENAI_API_KEY;
|
||||
process.env.START_CASE_EXPERIMENT_PROVIDER = "openai";
|
||||
process.env.OPENAI_API_KEY = "test-key";
|
||||
|
||||
try {
|
||||
const result = await runStartCaseExperiment(
|
||||
{ scenario: "OpenAI provider selection scenario" },
|
||||
null,
|
||||
{
|
||||
startCase: async (_input, dependencies) => {
|
||||
expect(typeof dependencies.reconstructionProvider?.generateReconstruction).toBe("function");
|
||||
expect(dependencies.reconstructionModelName).toBe("gpt-5.6-terra");
|
||||
return {
|
||||
success: true,
|
||||
situationGraph: { nodes: [], edges: [] },
|
||||
assessment: null,
|
||||
selectedQuestion: null,
|
||||
summary: "test",
|
||||
};
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
} finally {
|
||||
if (previousProvider === undefined) delete process.env.START_CASE_EXPERIMENT_PROVIDER;
|
||||
else process.env.START_CASE_EXPERIMENT_PROVIDER = previousProvider;
|
||||
if (previousKey === undefined) delete process.env.OPENAI_API_KEY;
|
||||
else process.env.OPENAI_API_KEY = previousKey;
|
||||
}
|
||||
});
|
||||
|
||||
it("fails before startCase when OpenAI is selected without a key", async () => {
|
||||
const { runStartCaseExperiment } = require(helperPath);
|
||||
const previousProvider = process.env.START_CASE_EXPERIMENT_PROVIDER;
|
||||
const previousKey = process.env.OPENAI_API_KEY;
|
||||
process.env.START_CASE_EXPERIMENT_PROVIDER = "openai";
|
||||
delete process.env.OPENAI_API_KEY;
|
||||
const startCase = vi.fn();
|
||||
|
||||
try {
|
||||
await expect(
|
||||
runStartCaseExperiment({ scenario: "Missing key scenario" }, null, { startCase }),
|
||||
).rejects.toThrow("START_CASE_EXPERIMENT_PROVIDER=openai requires OPENAI_API_KEY");
|
||||
expect(startCase).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
if (previousProvider === undefined) delete process.env.START_CASE_EXPERIMENT_PROVIDER;
|
||||
else process.env.START_CASE_EXPERIMENT_PROVIDER = previousProvider;
|
||||
if (previousKey === undefined) delete process.env.OPENAI_API_KEY;
|
||||
else process.env.OPENAI_API_KEY = previousKey;
|
||||
}
|
||||
});
|
||||
|
||||
it("forwards an explicit reconstruction provider through the startCase path", async () => {
|
||||
const { runStartCaseExperiment } = require(helperPath);
|
||||
const reconstructionProvider = { generateReconstruction() {} };
|
||||
|
||||
Reference in New Issue
Block a user