test(confidence-engine): expose CU synthesis failure shape

This commit is contained in:
2026-10-07 06:59:43 +01:00
parent d6dbc17bdf
commit 0b84f8ad28
3 changed files with 274 additions and 7 deletions
@@ -295,8 +295,37 @@ export async function synthesizeCurrentUnderstanding(
// 5. Validate response
const validated = validateSynthesisResponse(providerResult.response);
if (!validated.valid) {
/* ── safe diagnostic: expose shape only, never content ── */
/* parse string responses for key inspection */
let parsedResponse = providerResult.response;
if (typeof providerResult.response === "string") {
try { parsedResponse = JSON.parse(providerResult.response); } catch {}
}
const hasCU = typeof parsedResponse === "object" &&
parsedResponse !== null &&
Object.prototype.hasOwnProperty.call(parsedResponse, "currentUnderstanding");
const diag = {
providerApiPath: providerResult.providerApiPath ?? null,
responseType: typeof providerResult.response,
responseIsNull: providerResult.response === null,
responseKeys: (typeof parsedResponse === "object" && parsedResponse !== null && !Array.isArray(parsedResponse))
? Object.keys(parsedResponse).sort()
: null,
hasCurrentUnderstanding: hasCU,
currentUnderstandingType: hasCU ? typeof parsedResponse.currentUnderstanding : null,
currentUnderstandingLength: hasCU
? (typeof parsedResponse.currentUnderstanding === "string"
? parsedResponse.currentUnderstanding.length
: null)
: null,
};
const err = new Error(`Synthesis validation failed: ${validated.error}`);
err.statusCode = 502;
err.sanitizedDiagnostic = diag;
throw err;
}