fix: handle unjustified unknown selection ties

This commit is contained in:
2026-08-02 16:09:00 +01:00
parent a1f6d0c2b9
commit 51ce356218
8 changed files with 731 additions and 145 deletions
+132 -23
View File
@@ -11,6 +11,13 @@ function sentenceCase(value) {
return trimmed.charAt(0).toLowerCase() + trimmed.slice(1);
}
function stripTrailingPunctuation(value) {
return String(value || "")
.trim()
.replace(/[.?!:;]+$/g, "")
.trim();
}
function buildNodeMap(graph) {
return new Map((graph?.nodes || []).map((node) => [node.id, node]));
}
@@ -52,8 +59,8 @@ function collectResolvedContextValues(graph) {
function extractMeaning(node) {
const raw = `${node?.label || ""} ${node?.description || ""}`.trim();
let meaning = String(
node?.label || node?.description || "this uncertainty",
let meaning = stripTrailingPunctuation(
String(node?.label || node?.description || "this uncertainty"),
).trim();
const lowered = normaliseText(raw);
@@ -79,6 +86,84 @@ function extractMeaning(node) {
return sentenceCase(meaning);
}
function isDefinitionLikeUnknown(nodeText, text) {
return (
/\b(define|definition|meaning|term|terminology)\b/.test(nodeText) ||
(/\bdefinition\b/.test(text) && /\bdisagreement\b/.test(text)) ||
(/\b(define|definition|meaning|term|terminology)\b/.test(text) &&
/\b(unclear|ambiguous|inconsistent|undefined|used inconsistently)\b/.test(
text,
))
);
}
function isClaimLikeUnknown(node, text) {
return (
node?.kind === "reported_claim" ||
node?.kind === "conclusion" ||
/\b(claim|assertion|true|false|correct|incorrect|happened|happening)\b/.test(
text,
) ||
/^whether\b/i.test(String(node?.label || "").trim())
);
}
function sanitizeQuestionText(question) {
return String(question || "")
.replace(/\)\.\s+/g, ") ")
.replace(/\s+/g, " ")
.trim();
}
function buildNeutralClarificationQuestion(meaning) {
return `What would clarify ${stripTrailingPunctuation(meaning)} in this situation?`;
}
function buildEvidenceFallbackQuestion(meaning) {
return `What evidence would confirm or rule out ${stripTrailingPunctuation(meaning)}?`;
}
function detectContradictionContext(graph) {
const central = stripTrailingPunctuation(
graph?.centralStatement || "this situation",
);
const contradictionNode = (graph?.nodes || []).find((node) => {
const text = normaliseText(`${node.label} ${node.description}`);
return (
node.kind === "relationship" &&
/\b(contradiction|conflict|inconsistent|mismatch|divergent|opposing)\b/.test(
text,
)
);
});
return {
centralStatement: central,
contradictionLabel: stripTrailingPunctuation(
contradictionNode?.label || "",
),
};
}
export function formulateTieResolutionQuestion({ graph }) {
const { centralStatement, contradictionLabel } =
detectContradictionContext(graph);
const focus =
centralStatement || contradictionLabel || "these conflicting signals";
const question = sanitizeQuestionText(
`What changed during the period that could explain why ${focus}?`,
);
return {
question,
reason:
"Formulated to distinguish between tied unresolved explanations without prematurely choosing one branch.",
strategy: null,
investigationStrategy: null,
selectionStatus: "ambiguous",
};
}
function extractActionPhrase(texts) {
for (const text of texts) {
const value = String(text || "").trim();
@@ -224,8 +309,7 @@ export function selectInvestigationStrategy({ node, graph, context = {} }) {
});
}
const hasDefinitionLanguage =
/\b(define|definition|meaning|term|terminology)\b/.test(text);
const hasDefinitionLanguage = isDefinitionLikeUnknown(nodeText, text);
const hasPrimaryDefinitionLanguage =
/\b(define|definition|meaning|term|terminology)\b/.test(nodeText);
const hasCriteriaLanguage =
@@ -243,9 +327,7 @@ export function selectInvestigationStrategy({ node, graph, context = {} }) {
);
const hasEvidenceLanguage =
/\b(evidence|proof|validate|validation|signal|demand)\b/.test(text) ||
node?.kind === "reported_claim" ||
node?.kind === "conclusion" ||
/\b(claim|assertion|true|false)\b/.test(text);
isClaimLikeUnknown(node, text);
const hasContradictionLanguage =
/\b(contradiction|contradict|conflict|inconsistent|inconsistency|disagree|mismatch)\b/.test(
`${text} ${relatedText}`,
@@ -323,16 +405,7 @@ export function selectInvestigationStrategy({ node, graph, context = {} }) {
});
}
return buildInvestigationStrategy({
key: "definition",
reason:
"Selected as the deterministic fallback because clarifying the exact meaning of the unknown is the narrowest first step.",
node,
graph,
relatedNodes,
meaning,
actionPhrase,
});
return null;
}
function buildQuestionFromStrategy(strategy) {
@@ -344,7 +417,7 @@ function buildQuestionFromStrategy(strategy) {
case "definition":
return `What does ${strategy.meaning} mean in this situation?`;
case "evidence_gathering":
return `What evidence would show whether ${strategy.meaning} is true?`;
return `What evidence would clarify ${stripTrailingPunctuation(strategy.meaning)}?`;
case "baseline_reconstruction":
return `What was the comparable state before ${strategy.meaning}?`;
case "contradiction_resolution":
@@ -382,6 +455,9 @@ function validateFormulatedQuestion(question, meaning) {
if (/^how should uncertainty regarding\b/i.test(trimmed)) return false;
if (/^what would resolve uncertainty regarding\b/i.test(trimmed))
return false;
if (/\)\.\s+[A-Z]/.test(trimmed)) return false;
if (/\bis true\?$/i.test(trimmed) && !/^whether\b/i.test(meaning))
return false;
if (
/\bprice|pricing|price point\b/i.test(trimmed) &&
!/\bprice\b/i.test(meaning)
@@ -399,22 +475,55 @@ function validateFormulatedQuestion(question, meaning) {
}
export function formulateQuestion({ node, graph, context = {} }) {
if (context.selectionState?.status === "ambiguous") {
return formulateTieResolutionQuestion({ graph });
}
const investigationStrategy = selectInvestigationStrategy({
node,
graph,
context,
});
let question = buildQuestionFromStrategy(investigationStrategy);
let question = investigationStrategy
? buildQuestionFromStrategy(investigationStrategy)
: buildNeutralClarificationQuestion(extractMeaning(node));
if (!validateFormulatedQuestion(question, investigationStrategy.meaning)) {
question = `What evidence would resolve whether ${investigationStrategy.meaning} is true?`;
question = sanitizeQuestionText(question);
const fallbackMeaning = extractMeaning(node);
if (
!validateFormulatedQuestion(
question,
investigationStrategy?.meaning || fallbackMeaning,
)
) {
question = sanitizeQuestionText(
investigationStrategy &&
isClaimLikeUnknown(
node,
normaliseText(
collectRelatedNodes(node, graph)
.map(
(relatedNode) =>
`${relatedNode.label} ${relatedNode.description}`,
)
.concat([node?.label, node?.description])
.filter(Boolean)
.join(" "),
),
)
? buildEvidenceFallbackQuestion(fallbackMeaning)
: buildNeutralClarificationQuestion(fallbackMeaning),
);
}
return {
question,
reason: `Formulated from graph context using the ${investigationStrategy.key} investigation strategy.`,
strategy: investigationStrategy.key,
reason: investigationStrategy
? `Formulated from graph context using the ${investigationStrategy.key} investigation strategy.`
: "Formulated as a neutral clarification question because no narrower investigation strategy clearly applied.",
strategy: investigationStrategy?.key ?? null,
investigationStrategy,
};
}