src/assumptions.js
// Assumption log: normalise, score risk (impact x uncertainty), check evidence against sources, flag.
import { md } from "./text.js";
const CONF = { low: 0.25, medium: 0.5, high: 0.8 };
const IMPACT = { low: 1, medium: 2, high: 3 };
const round2 = (x) => Math.round(x * 100) / 100;
const confFromScore = (s) => (s >= 0.7 ? "high" : s >= 0.4 ? "medium" : "low");
export function normalizeAssumption(a, fallbackId, sourceUrls) {
// confidence_stated: false survives a round trip (market_size output -> assumption_log), so an
// assumption whose confidence was never stated is not later shown as if it had been.
const stated = a.confidence_stated === false ? false : a.confidence !== undefined || a.confidence_score !== undefined;
const confidence = a.confidence ?? (a.confidence_score !== undefined ? confFromScore(a.confidence_score) : "low");
const confidence_score = a.confidence_score ?? CONF[confidence];
const impact = a.impact ?? "medium";
const evidence = [...new Set(a.evidence ?? [])];
const missing = sourceUrls ? evidence.filter((u) => !sourceUrls.has(u)) : [];
const evidence_status = !evidence.length ? "none" : !sourceUrls ? "unchecked" : missing.length ? "not_in_sources" : "in_sources";
const status = a.status ?? "open";
const flags = [];
if (status === "invalidated") flags.push("Invalidated: revisit anything that depends on it.");
if (status !== "validated" && impact === "high" && confidence_score < 0.5) flags.push("High impact and low confidence: validate this first.");
if (!evidence.length) flags.push("No evidence linked.");
if (missing.length) flags.push(`Evidence not in the provided sources: ${missing.slice(0, 5).join(", ")}`);
if (confidence === "high" && !evidence.length) flags.push("Marked high confidence without evidence.");
if (!stated) flags.push("Confidence not stated; treated as low.");
const out = {
id: a.id ?? fallbackId,
statement: String(a.statement).replace(/\s+/g, " ").trim(),
category: a.category ?? "other",
confidence,
confidence_score: round2(confidence_score),
confidence_stated: stated,
impact,
risk_score: round2(IMPACT[impact] * (1 - confidence_score) * (status === "validated" ? 0.25 : 1)),
evidence,
evidence_status,
flags,
test: a.test ?? null,
owner: a.owner ?? null,
status,
};
for (const k of ["value", "basis", "source_url"]) if (a[k] !== undefined && a[k] !== null) out[k] = a[k];
return out;
}
export function buildLog(list, sources) {
const sourceUrls = sources ? new Set(sources.map((s) => s.url)) : null;
const warnings = [];
const ids = new Set();
const items = list.map((a, i) => {
const n = normalizeAssumption(a, `A${i + 1}`, sourceUrls);
if (ids.has(n.id)) {
let k = 2;
while (ids.has(`${n.id}-${k}`)) k++;
warnings.push(`Duplicate assumption id ${n.id} renamed to ${n.id}-${k}.`);
n.id = `${n.id}-${k}`;
}
ids.add(n.id);
return n;
});
const statusRank = { open: 0, invalidated: 0, validated: 1 };
items.sort((a, b) => statusRank[a.status] - statusRank[b.status] || b.risk_score - a.risk_score || IMPACT[b.impact] - IMPACT[a.impact] || a.id.localeCompare(b.id));
const count = (f) => items.filter(f).length;
const by = (k) => items.reduce((m, x) => ({ ...m, [x[k]]: (m[x[k]] ?? 0) + 1 }), {});
const summary = {
total: items.length,
validate_first: count((x) => x.flags.some((f) => f.startsWith("High impact"))),
without_evidence: count((x) => !x.evidence.length),
by_category: by("category"),
by_status: by("status"),
};
if (!sourceUrls && items.some((x) => x.evidence.length)) warnings.push("No sources were given, so evidence URLs were not checked (evidence_status: unchecked).");
return { assumptions: items, summary, warnings };
}
export function logMarkdown(items, title = "Assumption log") {
const lines = [`## ${md(title)}`, "", "| # | Assumption | Category | Impact | Confidence | Risk | Evidence | Flags |", "| --- | --- | --- | --- | --- | --- | --- | --- |"];
for (const x of items) {
lines.push(`| ${md(x.id)} | ${md(x.statement)} | ${md(x.category)} | ${x.impact} | ${x.confidence} (${x.confidence_score}) | ${x.risk_score} | ${x.evidence.length ? x.evidence.map(md).join("; ") : "none"} | ${md(x.flags.join(" "))} |`);
}
if (!items.length) lines.push("| \u2013 | _No assumptions._ | | | | | | |");
lines.push("", "_Risk = impact weight (low 1, medium 2, high 3) x (1 - confidence score); validated assumptions are scaled by 0.25._");
return lines.join("\n");
}