fix(confidence-engine): await focused deconstruction provider

This commit is contained in:
2026-10-06 18:18:10 +01:00
parent 814360e368
commit d6dbc17bdf
3 changed files with 47 additions and 45 deletions
+2 -2
View File
@@ -198,7 +198,7 @@ export function validateFocusedDeconstructSchema(result) {
* Validates result with validateFocusedDeconstructSchema. * Validates result with validateFocusedDeconstructSchema.
* Returns validated semantic payload on success, or { validation: false, errors } on schema failure. * Returns validated semantic payload on success, or { validation: false, errors } on schema failure.
*/ */
export function focusedDeconstruct({ export async function focusedDeconstruct({
provider, provider,
modelName, modelName,
targetNodeId, targetNodeId,
@@ -216,7 +216,7 @@ export function focusedDeconstruct({
answer, answer,
}); });
const wrapper = provider.generateReconstruction( const wrapper = await provider.generateReconstruction(
prompt, prompt,
modelName, modelName,
focusedDeconstructJsonSchema, focusedDeconstructJsonSchema,
+42 -42
View File
@@ -29,104 +29,104 @@ function makeInvalidResponse() {
} }
describe("focusedDeconstruct — success", () => { describe("focusedDeconstruct — success", () => {
it("calls provider.generateReconstruction exactly once and returns success", () => { it("calls provider.generateReconstruction exactly once and returns success", async () => {
let callCount = 0; let callCount = 0;
const fake = { generateReconstruction() { callCount++; return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction() { callCount++; return { response: makeValidResponse() }; } };
const result = focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const result = await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(callCount).toBe(1); expect(callCount).toBe(1);
expect(result.success).toBe(true); expect(result.success).toBe(true);
}); });
it("preserves observations from provider response", () => { it("preserves observations from provider response", async () => {
const fake = { generateReconstruction() { return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction() { return { response: makeValidResponse() }; } };
const result = focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const result = await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(Array.isArray(result.observations)).toBe(true); expect(Array.isArray(result.observations)).toBe(true);
expect(result.observations.length).toBeGreaterThan(0); expect(result.observations.length).toBeGreaterThan(0);
}); });
it("preserves uncertainties from provider response", () => { it("preserves uncertainties from provider response", async () => {
const fake = { generateReconstruction() { return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction() { return { response: makeValidResponse() }; } };
const result = focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const result = await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(Array.isArray(result.uncertainties)).toBe(true); expect(Array.isArray(result.uncertainties)).toBe(true);
expect(result.uncertainties.length).toBeGreaterThan(0); expect(result.uncertainties.length).toBeGreaterThan(0);
}); });
it("preserves assumptions from provider response", () => { it("preserves assumptions from provider response", async () => {
const fake = { generateReconstruction() { return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction() { return { response: makeValidResponse() }; } };
const result = focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const result = await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(Array.isArray(result.assumptions)).toBe(true); expect(Array.isArray(result.assumptions)).toBe(true);
expect(result.assumptions.length).toBeGreaterThan(0); expect(result.assumptions.length).toBeGreaterThan(0);
}); });
it("preserves relationships from provider response", () => { it("preserves relationships from provider response", async () => {
const fake = { generateReconstruction() { return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction() { return { response: makeValidResponse() }; } };
const result = focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const result = await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(Array.isArray(result.relationships)).toBe(true); expect(Array.isArray(result.relationships)).toBe(true);
expect(result.relationships.length).toBeGreaterThan(0); expect(result.relationships.length).toBeGreaterThan(0);
}); });
it("preserves possibleFollowUpQuestions from provider response", () => { it("preserves possibleFollowUpQuestions from provider response", async () => {
const fake = { generateReconstruction() { return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction() { return { response: makeValidResponse() }; } };
const result = focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const result = await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(Array.isArray(result.possibleFollowUpQuestions)).toBe(true); expect(Array.isArray(result.possibleFollowUpQuestions)).toBe(true);
expect(result.possibleFollowUpQuestions.length).toBeGreaterThan(0); expect(result.possibleFollowUpQuestions.length).toBeGreaterThan(0);
}); });
it("preserves caller target identity (result.targetNodeId === request targetNodeId)", () => { it("preserves caller target identity (result.targetNodeId === request targetNodeId)", async () => {
const fake = { generateReconstruction() { return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction() { return { response: makeValidResponse() }; } };
const result = focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const result = await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(result.targetNodeId).toBe(FAKE_TARGET_NODE_ID); expect(result.targetNodeId).toBe(FAKE_TARGET_NODE_ID);
}); });
it("passes correct prompt to provider", () => { it("passes correct prompt to provider", async () => {
let capturedPrompt = null; let capturedPrompt = null;
const fake = { generateReconstruction(prompt) { capturedPrompt = prompt; return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction(prompt) { capturedPrompt = prompt; return { response: makeValidResponse() }; } };
focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
const expected = buildFocusedDeconstructPrompt({ targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const expected = buildFocusedDeconstructPrompt({ targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(capturedPrompt).toBe(expected); expect(capturedPrompt).toBe(expected);
}); });
it("passes modelName unchanged to provider", () => { it("passes modelName unchanged to provider", async () => {
let capturedModel = null; let capturedModel = null;
const fake = { generateReconstruction(prompt, receivedModel) { capturedModel = receivedModel; return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction(prompt, receivedModel) { capturedModel = receivedModel; return { response: makeValidResponse() }; } };
focusedDeconstruct({ provider: fake, modelName: "my-custom-model-v2", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); await focusedDeconstruct({ provider: fake, modelName: "my-custom-model-v2", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(capturedModel).toBe("my-custom-model-v2"); expect(capturedModel).toBe("my-custom-model-v2");
}); });
it("passes focusedDeconstructJsonSchema as the schema argument", () => { it("passes focusedDeconstructJsonSchema as the schema argument", async () => {
let capturedSchema = null; let capturedSchema = null;
const fake = { generateReconstruction(prompt, modelName, schema) { capturedSchema = schema; return { response: makeValidResponse() }; } }; const fake = { async generateReconstruction(prompt, modelName, schema) { capturedSchema = schema; return { response: makeValidResponse() }; } };
focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(capturedSchema).toBe(focusedDeconstructJsonSchema); expect(capturedSchema).toBe(focusedDeconstructJsonSchema);
}); });
}) })
describe("focusedDeconstruct — validation failure", () => { describe("focusedDeconstruct — validation failure", () => {
it("returns failure result when provider response fails canonical validation", () => { it("returns failure result when provider response fails canonical validation", async () => {
const fake = { generateReconstruction() { return { response: makeInvalidResponse() }; } }; const fake = { async generateReconstruction() { return { response: makeInvalidResponse() }; } };
const result = focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const result = await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(result.success).toBe(false); expect(result.success).toBe(false);
}); });
it("validation errors remain available on failure result", () => { it("validation errors remain available on failure result", async () => {
const fake = { generateReconstruction() { return { response: makeInvalidResponse() }; } }; const fake = { async generateReconstruction() { return { response: makeInvalidResponse() }; } };
const result = focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); const result = await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(Array.isArray(result.errors)).toBe(true); expect(Array.isArray(result.errors)).toBe(true);
expect(result.errors.length).toBeGreaterThan(0); expect(result.errors.length).toBeGreaterThan(0);
}); });
it("no second provider call occurs on validation failure", () => { it("no second provider call occurs on validation failure", async () => {
let callCount = 0; let callCount = 0;
const fake = { generateReconstruction() { callCount++; return { response: makeInvalidResponse() }; } }; const fake = { async generateReconstruction() { callCount++; return { response: makeInvalidResponse() }; } };
focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER }); await focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER });
expect(callCount).toBe(1); expect(callCount).toBe(1);
}); });
}) })
describe("focusedDeconstruct — provider failure", () => { describe("focusedDeconstruct — provider failure", () => {
it("propagates error with original identity/metadata intact", () => { it("propagates error with original identity/metadata intact", async () => {
const fake = { const fake = {
generateReconstruction() { async generateReconstruction() {
const err = new Error("Connection refused to ollama endpoint"); const err = new Error("Connection refused to ollama endpoint");
err.name = "ProviderError"; err.name = "ProviderError";
err.providerApiPath = "/api/v1/completions"; err.providerApiPath = "/api/v1/completions";
@@ -134,6 +134,6 @@ describe("focusedDeconstruct — provider failure", () => {
throw err; throw err;
}, },
}; };
expect(() => focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER })).toThrow("Connection refused to ollama endpoint"); await expect(focusedDeconstruct({ provider: fake, modelName: "test-model", targetNodeId: FAKE_TARGET_NODE_ID, targetLabel: FAKE_LABEL, targetDescription: FAKE_DESCRIPTION, centralStatement: FAKE_CENTRAL, question: FAKE_QUESTION, answer: FAKE_ANSWER })).rejects.toThrow("Connection refused to ollama endpoint");
}); });
}) })
@@ -331,7 +331,7 @@ async function composeDeepInvestigationCheckpoint({ situationGraph, previousQues
const centralStatement = situationGraph.centralStatement ?? ""; const centralStatement = situationGraph.centralStatement ?? "";
// Call production-focused deconstruct — exactly one provider generation // Call production-focused deconstruct — exactly one provider generation
const focused = focusedDeconstruct({ const focused = await focusedDeconstruct({
provider, provider,
modelName: model, modelName: model,
targetNodeId, targetNodeId,
@@ -342,6 +342,8 @@ async function composeDeepInvestigationCheckpoint({ situationGraph, previousQues
answer, answer,
}); });
providerGenerateCalls++; // focusedDeconstruct now uses the same provider (1 call)
if (!focused.success) { if (!focused.success) {
throw new Error(`focusedDeconstruct failed for episode ${contribSeq}: ${JSON.stringify(focused.errors)}`); throw new Error(`focusedDeconstruct failed for episode ${contribSeq}: ${JSON.stringify(focused.errors)}`);
} }