mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
fix(labs): keep the printed range on a marker minted from a text result
A report that prints a text result such as "SEE NOTE" with a numeric range beside it minted the marker without bounds: the document commit, the scan commit, the free-text lab write and the CSV import each passed null bounds for a qualitative reading, and the CSV row reader and the scan review payload dropped the range before that. The marker's first number then had nothing to be judged against. Every mint path now passes the printed window. A range that is text itself yields no bounds, an existing marker keeps its own, a transposed window is not minted, and the reading itself still records no window. The scan review shows the range on a text row that mints a new marker. Refs #1313
This commit is contained in:
1 parent
e9886ee554
commit
ef61c7d5d1
13 files changed
+520
-39
No files matched your search
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$comment": "Production functions over 200 lines, keyed \"file :: name\", and their length. Maintained by src/__tests__/source-size-ratchet.test.ts.",
|
||||
"count": 345,
|
||||
"total": 111902,
|
||||
"total": 111889,
|
||||
"entries": {
|
||||
"app/achievements/page.tsx :: AchievementsPage": 206,
|
||||
"app/api/admin/backups/upload/route.ts :: apiHandler(arg)": 275,
|
||||
@@ -24,7 +24,7 @@
|
||||
"app/api/integrations/status/route.ts :: apiHandler(arg)": 476,
|
||||
"app/api/labs/[id]/route.ts :: apiHandler(arg) #2": 239,
|
||||
"app/api/labs/ocr/commit/route.ts :: commitOcrRows": 280,
|
||||
"app/api/labs/route.ts :: postLabResult": 242,
|
||||
"app/api/labs/route.ts :: postLabResult": 241,
|
||||
"app/api/mcp/oauth/token/route.ts :: POST": 250,
|
||||
"app/api/mcp/oauth/token/route.ts :: POST > withBackgroundEvent(arg)": 248,
|
||||
"app/api/measurement-reminders/[id]/route.ts :: apiHandler(arg) #2": 239,
|
||||
@@ -139,7 +139,7 @@
|
||||
"components/labs/lab-history-list.tsx :: LabHistoryList": 534,
|
||||
"components/labs/lab-list.tsx :: LabList": 308,
|
||||
"components/labs/ocr-review-dialog.tsx :: OcrReviewDialog": 359,
|
||||
"components/labs/ocr-row-editor.tsx :: OcrRowEditor": 386,
|
||||
"components/labs/ocr-row-editor.tsx :: OcrRowEditor": 378,
|
||||
"components/layout/auth-shell.tsx :: AuthShell": 518,
|
||||
"components/layout/bottom-nav.tsx :: BottomNav": 262,
|
||||
"components/layout/capture-picker.tsx :: CapturePicker": 235,
|
||||
@@ -286,8 +286,8 @@
|
||||
"lib/google-health/data-points.ts :: forEachDataPointPage > walk": 223,
|
||||
"lib/google-health/sync-core.ts :: upsertGoogleHealthMeasurements": 452,
|
||||
"lib/illness/correlation.ts :: computeIllnessCorrelation": 233,
|
||||
"lib/import/labs-csv.ts :: importLabsCsv": 309,
|
||||
"lib/import/labs-csv.ts :: importLabsCsv > flush": 231,
|
||||
"lib/import/labs-csv.ts :: importLabsCsv": 307,
|
||||
"lib/import/labs-csv.ts :: importLabsCsv > flush": 229,
|
||||
"lib/insights/comprehensive-aggregator.ts :: buildFromRollups": 217,
|
||||
"lib/insights/comprehensive-generate.ts :: generateComprehensiveInsight": 876,
|
||||
"lib/insights/features.ts :: extractFeatures": 796,
|
||||
|
||||
@@ -249,10 +249,10 @@ async function commitOcrRows(request: NextRequest) {
|
||||
user.id,
|
||||
{
|
||||
analyte: row.analyte,
|
||||
// A qualitative reading has no numeric unit / range.
|
||||
// A text result has no unit; its printed window still seeds.
|
||||
unit: isQualitative ? (row.unit ?? "") : (row.unit as string),
|
||||
referenceLow: isQualitative ? null : sourceLow,
|
||||
referenceHigh: isQualitative ? null : sourceHigh,
|
||||
referenceLow: sourceLow,
|
||||
referenceHigh: sourceHigh,
|
||||
panel: row.panel ?? null,
|
||||
},
|
||||
{ index: markerIndex },
|
||||
|
||||
@@ -234,12 +234,11 @@ async function postLabResult(request: NextRequest) {
|
||||
} else {
|
||||
biomarker = await resolveOrMintBiomarker(user.id, {
|
||||
analyte: analyte as string,
|
||||
// A qualitative reading has no numeric unit / range — mint the catalog
|
||||
// marker with an empty unit label and no bounds. A numeric reading
|
||||
// supplies its unit (Zod requires it on the free-text numeric path).
|
||||
// A qualitative reading mints with an empty unit but the printed range;
|
||||
// a numeric one supplies its unit (Zod requires it on this path).
|
||||
unit: isQualitative ? (unit ?? "") : (unit as string),
|
||||
referenceLow: isQualitative ? null : (referenceLow ?? null),
|
||||
referenceHigh: isQualitative ? null : (referenceHigh ?? null),
|
||||
referenceLow: referenceLow ?? null,
|
||||
referenceHigh: referenceHigh ?? null,
|
||||
panel: panel ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -115,6 +115,27 @@ describe("toCommitRow agrees with validateReviewRow", () => {
|
||||
it("anchors the date at noon UTC", () => {
|
||||
expect(toCommitRow(row())?.takenAt).toBe("2026-06-10T12:00:00.000Z");
|
||||
});
|
||||
|
||||
it("sends the range printed beside a text result", () => {
|
||||
expect(
|
||||
toCommitRow(
|
||||
row({
|
||||
value: null,
|
||||
valueText: "SEE NOTE",
|
||||
unit: null,
|
||||
referenceLow: 10,
|
||||
referenceHigh: 20,
|
||||
referenceText: "10 - 20",
|
||||
biomarkerMatch: "new",
|
||||
}),
|
||||
),
|
||||
).toMatchObject({
|
||||
valueText: "SEE NOTE",
|
||||
referenceLow: 10,
|
||||
referenceHigh: 20,
|
||||
referenceText: "10 - 20",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("collectRowErrors", () => {
|
||||
|
||||
@@ -125,6 +125,44 @@ describe("<OcrRowEditor> layout", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("<OcrRowEditor> range beside a text result", () => {
|
||||
const text: OcrReviewRow = {
|
||||
...baseRow,
|
||||
value: null,
|
||||
valueText: "SEE NOTE",
|
||||
unit: null,
|
||||
referenceLow: 10,
|
||||
referenceHigh: 20,
|
||||
referenceText: "10 - 20",
|
||||
};
|
||||
|
||||
it("offers the range for review when the row mints a new marker", () => {
|
||||
const html = render(undefined, "en", { ...text, biomarkerMatch: "new" });
|
||||
for (const id of ["-lo", "-hi", "-refText"]) {
|
||||
expect(html, `field ${id}`).toContain(`${id}"`);
|
||||
}
|
||||
expect(html).toContain("Reference range taken from the scan");
|
||||
});
|
||||
|
||||
it("does not ask to verify a range a new text marker never had", () => {
|
||||
const html = render(undefined, "en", {
|
||||
...text,
|
||||
biomarkerMatch: "new",
|
||||
referenceLow: null,
|
||||
referenceHigh: null,
|
||||
referenceText: null,
|
||||
});
|
||||
expect(html).not.toContain("Verify the reference range");
|
||||
});
|
||||
|
||||
it("leaves it out when the row goes to an existing marker", () => {
|
||||
const html = render(undefined, "en", text);
|
||||
for (const id of ["-lo", "-hi", "-refText"]) {
|
||||
expect(html, `field ${id}`).not.toContain(`${id}"`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("<OcrRowEditor> unit differs from the marker's", () => {
|
||||
const mismatched: OcrReviewRow = {
|
||||
...baseRow,
|
||||
|
||||
@@ -157,6 +157,9 @@ export function toCommitRow(row: OcrReviewRow): OcrCommitRowInput | null {
|
||||
analyte,
|
||||
valueText,
|
||||
takenAt: takenAt.toISOString(),
|
||||
// A text result has no window of its own, but a new marker minted from
|
||||
// it takes the one printed beside it.
|
||||
...printedRange(row),
|
||||
...chosen(row),
|
||||
};
|
||||
}
|
||||
@@ -170,10 +173,19 @@ export function toCommitRow(row: OcrReviewRow): OcrCommitRowInput | null {
|
||||
value: row.value,
|
||||
unit,
|
||||
takenAt: takenAt.toISOString(),
|
||||
...printedRange(row),
|
||||
...chosen(row),
|
||||
};
|
||||
}
|
||||
|
||||
/** The reviewed window: the two bounds and the string they were read from. */
|
||||
function printedRange(
|
||||
row: OcrReviewRow,
|
||||
): Pick<OcrCommitRowInput, "referenceLow" | "referenceHigh" | "referenceText"> {
|
||||
return {
|
||||
...(row.referenceLow !== null ? { referenceLow: row.referenceLow } : {}),
|
||||
...(row.referenceHigh !== null ? { referenceHigh: row.referenceHigh } : {}),
|
||||
...(row.referenceText ? { referenceText: row.referenceText } : {}),
|
||||
...chosen(row),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -75,6 +75,33 @@ function unitMismatchMessage(
|
||||
: ["labs.ocr.errorUnitMismatch", { markerUnit: row.markerUnit ?? "" }];
|
||||
}
|
||||
|
||||
/**
|
||||
* v1.18.10 (#5) — a NEW biomarker mints its catalog reference range from THIS
|
||||
* extracted row, so the range is load-bearing and worth a second look. Surface
|
||||
* the range confidence for a new numeric marker, and call it out when the
|
||||
* model was unsure (or set no bounds at all) so the user verifies the range it
|
||||
* will be judged against. A text result ("SEE NOTE") records no window of its
|
||||
* own, but a new marker minted from it takes the one printed beside it, so the
|
||||
* range is shown there too, and only called out when there is one: a text
|
||||
* result often has no numeric range, and that is not a gap.
|
||||
*/
|
||||
function rangeReview(row: OcrReviewRow): {
|
||||
showRange: boolean;
|
||||
showRangeBadge: boolean;
|
||||
flagRange: boolean;
|
||||
} {
|
||||
const isQualitative = row.valueText !== null && row.valueText !== undefined;
|
||||
const isNew = row.biomarkerMatch === "new";
|
||||
const lowRangeConfidence = row.confidence.range < CONFIDENCE_THRESHOLD;
|
||||
const hasBounds = row.referenceLow !== null || row.referenceHigh !== null;
|
||||
const showRangeBadge = isNew && (!isQualitative || hasBounds);
|
||||
return {
|
||||
showRange: !isQualitative || isNew,
|
||||
showRangeBadge,
|
||||
flagRange: showRangeBadge && (lowRangeConfidence || !hasBounds),
|
||||
};
|
||||
}
|
||||
|
||||
export function OcrRowEditor({
|
||||
row,
|
||||
onChange,
|
||||
@@ -138,15 +165,7 @@ export function OcrRowEditor({
|
||||
? row.confidence.value < CONFIDENCE_THRESHOLD
|
||||
: lowValueConfidence);
|
||||
|
||||
// v1.18.10 (#5) — a NEW numeric biomarker mints its catalog reference range
|
||||
// from THIS extracted row. The range is therefore load-bearing and worth a
|
||||
// second look. Surface the range confidence prominently for new numeric
|
||||
// markers, and call it out explicitly when the model was unsure (or set no
|
||||
// bounds at all) so the user verifies the range it will be judged against.
|
||||
const isNewNumericMarker = !isQualitative && row.biomarkerMatch === "new";
|
||||
const lowRangeConfidence = row.confidence.range < CONFIDENCE_THRESHOLD;
|
||||
const hasBounds = row.referenceLow !== null || row.referenceHigh !== null;
|
||||
const flagRange = isNewNumericMarker && (lowRangeConfidence || !hasBounds);
|
||||
const { showRange, showRangeBadge, flagRange } = rangeReview(row);
|
||||
|
||||
return (
|
||||
// `@container` makes the row follow the width of the dialog, not the
|
||||
@@ -353,11 +372,11 @@ export function OcrRowEditor({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* v1.18.10 (#5) — for a NEW numeric biomarker the reference range below
|
||||
{/* v1.18.10 (#5) — for a NEW biomarker the reference range below
|
||||
becomes the marker's catalog range. Make that consequence visible, and
|
||||
flag low range-confidence / a missing range so the user verifies it
|
||||
before saving. A calm `secondary` badge, not an alarm. */}
|
||||
{isNewNumericMarker ? (
|
||||
{showRangeBadge ? (
|
||||
<div className="flex flex-wrap items-center gap-1.5">
|
||||
<Badge variant={flagRange ? "secondary" : "outline"}>
|
||||
<Ruler aria-hidden />
|
||||
@@ -368,7 +387,7 @@ export function OcrRowEditor({
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{!isQualitative ? (
|
||||
{showRange ? (
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor={`${fieldId}-lo`} className="text-xs">
|
||||
@@ -424,7 +443,7 @@ export function OcrRowEditor({
|
||||
reading of this string, and correcting the string is how a range the
|
||||
parser could not read ("bis 5,0", "negativ") still reaches the row
|
||||
instead of being dropped. */}
|
||||
{!isQualitative ? (
|
||||
{showRange ? (
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor={`${fieldId}-refText`} className="text-xs">
|
||||
{t("labs.ocr.refTextLabel")}
|
||||
|
||||
@@ -197,8 +197,10 @@ async function commitObservation(
|
||||
biomarker = await resolveOrMintBiomarker(userId, {
|
||||
analyte: data.label,
|
||||
unit: isQualitative ? (data.unit ?? "") : (data.unit as string),
|
||||
referenceLow: isQualitative ? null : sourceLow,
|
||||
referenceHigh: isQualitative ? null : sourceHigh,
|
||||
// A text result mints with the window printed beside it as well: the
|
||||
// marker's next number is judged against it.
|
||||
referenceLow: sourceLow,
|
||||
referenceHigh: sourceHigh,
|
||||
panel: null,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -106,6 +106,26 @@ describe("lab rows", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the numeric range beside a text result, and no text range", () => {
|
||||
expect(
|
||||
lab(["BUN/Creatinine ratio", "SEE NOTE", "", "2025-03-01", "10 - 20"]),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
row: {
|
||||
valueText: "SEE NOTE",
|
||||
sourceReferenceLow: 10,
|
||||
sourceReferenceHigh: 20,
|
||||
sourceReferenceText: null,
|
||||
},
|
||||
});
|
||||
expect(
|
||||
lab(["Urine nitrite", "neg", "", "2025-03-01", "NEGATIVE"]),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
row: { sourceReferenceLow: null, sourceReferenceHigh: null },
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[["", "5", "%", "2025-03-01", ""], "missing_value", "analyte"],
|
||||
[["HbA1c", "", "%", "2025-03-01", ""], "missing_value", "value"],
|
||||
|
||||
@@ -74,6 +74,8 @@ export interface LabCsvRow {
|
||||
valueText: string | null;
|
||||
unit: string | null;
|
||||
takenAt: Date;
|
||||
/** The file's window. On a text result it only seeds a new marker: the
|
||||
* reading itself records no window. */
|
||||
sourceReferenceLow: number | null;
|
||||
sourceReferenceHigh: number | null;
|
||||
sourceReferenceText: string | null;
|
||||
@@ -200,8 +202,8 @@ export function normaliseLabRow(
|
||||
valueText,
|
||||
unit,
|
||||
takenAt: date.instant,
|
||||
sourceReferenceLow: valueText === null ? low : null,
|
||||
sourceReferenceHigh: valueText === null ? high : null,
|
||||
sourceReferenceLow: low,
|
||||
sourceReferenceHigh: high,
|
||||
sourceReferenceText: valueText === null ? rangeText : null,
|
||||
panel,
|
||||
note,
|
||||
@@ -473,10 +475,8 @@ export async function importLabsCsv(args: {
|
||||
{
|
||||
analyte: row.analyte,
|
||||
unit: row.unit ?? "",
|
||||
referenceLow:
|
||||
row.valueText === null ? row.sourceReferenceLow : null,
|
||||
referenceHigh:
|
||||
row.valueText === null ? row.sourceReferenceHigh : null,
|
||||
referenceLow: row.sourceReferenceLow,
|
||||
referenceHigh: row.sourceReferenceHigh,
|
||||
panel: row.panel,
|
||||
},
|
||||
{ index: working },
|
||||
@@ -484,8 +484,8 @@ export async function importLabsCsv(args: {
|
||||
if (!marker) created.add(row.analyte);
|
||||
|
||||
let storedValue = row.value;
|
||||
let storedLow = row.sourceReferenceLow;
|
||||
let storedHigh = row.sourceReferenceHigh;
|
||||
let storedLow = row.valueText === null ? row.sourceReferenceLow : null;
|
||||
let storedHigh = row.valueText === null ? row.sourceReferenceHigh : null;
|
||||
let convertedFrom: { sourceValue: number; sourceUnit: string } | null =
|
||||
null;
|
||||
if (row.value !== null) {
|
||||
|
||||
@@ -86,6 +86,10 @@ export interface MintedBiomarker {
|
||||
* reused as-is: its unit / range are authoritative and the incoming free-text
|
||||
* unit / bounds are NOT allowed to silently rewrite it. When nothing resolves,
|
||||
* a marker is minted from the reading's own spelling / unit / bounds. The
|
||||
* bounds are the report's printed window whether the reading is a number or a
|
||||
* text result ("SEE NOTE" beside "10 - 20"): the marker outlives the reading,
|
||||
* and its next number is judged against them. A window that cannot be true
|
||||
* (low above high) is not minted; nothing says which of the two is wrong. The
|
||||
* `@@unique([userId, name])` index is the structural backstop; a concurrent
|
||||
* double-write converges via the catch-and-refetch.
|
||||
*
|
||||
@@ -109,14 +113,18 @@ export async function resolveOrMintBiomarker(
|
||||
const hit = resolveInIndex(index, name, input.unit || null);
|
||||
if (hit) return toMinted(hit.marker);
|
||||
|
||||
const transposed =
|
||||
input.referenceLow !== null &&
|
||||
input.referenceHigh !== null &&
|
||||
input.referenceLow > input.referenceHigh;
|
||||
try {
|
||||
const created = await prisma.biomarker.create({
|
||||
data: {
|
||||
userId,
|
||||
name,
|
||||
unit: input.unit,
|
||||
lowerBound: input.referenceLow,
|
||||
upperBound: input.referenceHigh,
|
||||
lowerBound: transposed ? null : input.referenceLow,
|
||||
upperBound: transposed ? null : input.referenceHigh,
|
||||
panel: input.panel,
|
||||
analyteKey: matchAnalyteKey({ name, unit: input.unit }),
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user