feat(labs): keep the reference range a report prints beside a value

A lab runs its own method on its own device, so the window a physician
evaluates a value against is the one printed next to it on that report,
not a catalog band averaged over every lab. Reading a report pulled the
value and dropped that range: the extraction asked for it, the commit
handed it to the biomarker resolver, and the resolver only uses it when
it mints a NEW marker. For every analyte the user already had — the
normal case after a first panel — the printed range was read and then
thrown away.

Add `sourceReferenceLow` / `sourceReferenceHigh` / `sourceReferenceText`
to `LabResult` and carry the range from the report to the reading. The
text is stored whether or not numbers can be read out of it, because
"bis 5,0" and "negativ" state a window too; a range that could not be
parsed is kept verbatim, never replaced with invented bounds.

`resolveEffectiveReferenceRange` is the one place the precedence lives:
the source window wins for that single reading, and the catalog stays
the net for readings without one. A printed string with no derivable
bound does not displace the catalog. Every surface that judges a
reading routes through it — the API verdict, the doctor report, the
insights blocks and per-marker assessment, the coach snapshot, the MCP
read and the FHIR `referenceRange` — so a value cannot read in range on
one surface and out on another.

The API keeps `referenceLow` / `referenceHigh` as the window in force,
so a client that reads only those two renders the right range and the
right verdict with no change of its own; the catalog band, the source
window, its origin and a divergence flag ride alongside for clients
that want to show both.

Read side: the chart draws the source window as a second, visually
distinct band with the reading it came from named beneath it, the
reading rows name the printed range and the catalog band where the two
disagree, and the doctor-report PDF prints the report's own string with
a footnote naming the saved range. Entry side: one optional field on
the lab form, folded away until asked for, and the printed range is
editable on the scan review screen.

The range parser covers the notations German and English reports
actually print — two-sided, one-sided, comma or dot decimals, thousands
separators, trailing units — reads units without ever converting them,
and refuses to guess on anything ambiguous.
This commit is contained in:
Marc-André Bombeck committed 2026-08-07 21:40:41 +02:00
1 parent 3778e70ff5
commit fd2bd67ff3
57 files changed
+2281 -175

No files matched your search

+172 -9
View File
@@ -9147,6 +9147,16 @@ paths:
type: number
referenceHigh:
type: number
sourceReferenceLow:
type: number
sourceReferenceHigh:
type: number
sourceReferenceText:
anyOf:
- type: string
maxLength: 120
- type: string
const: ""
takenAt:
type: string
format: date-time
@@ -9254,6 +9264,22 @@ paths:
anyOf:
- type: number
- type: "null"
sourceReferenceLow:
anyOf:
- type: number
- type: "null"
sourceReferenceHigh:
anyOf:
- type: number
- type: "null"
sourceReferenceText:
anyOf:
- anyOf:
- type: string
maxLength: 120
- type: string
const: ""
- type: "null"
takenAt:
type: string
format: date-time
@@ -9430,6 +9456,12 @@ paths:
type: number
referenceHigh:
type: number
referenceText:
anyOf:
- type: string
maxLength: 120
- type: string
const: ""
takenAt:
type: string
format: date-time
@@ -11616,6 +11648,11 @@ paths:
anyOf:
- type: number
- type: "null"
referenceText:
anyOf:
- type: string
maxLength: 120
- type: "null"
effectiveDate:
anyOf:
- type: string
@@ -32970,6 +33007,30 @@ components:
anyOf:
- type: number
- type: "null"
catalogReferenceLow:
anyOf:
- type: number
- type: "null"
catalogReferenceHigh:
anyOf:
- type: number
- type: "null"
sourceReferenceLow:
anyOf:
- type: number
- type: "null"
sourceReferenceHigh:
anyOf:
- type: number
- type: "null"
sourceReferenceText:
anyOf:
- type: string
- type: "null"
referenceOrigin:
$ref: "#/components/schemas/LabReferenceRangeOrigin"
referenceDivergesFromCatalog:
type: boolean
takenAt:
type: string
source:
@@ -32992,6 +33053,13 @@ components:
- unit
- referenceLow
- referenceHigh
- catalogReferenceLow
- catalogReferenceHigh
- sourceReferenceLow
- sourceReferenceHigh
- sourceReferenceText
- referenceOrigin
- referenceDivergesFromCatalog
- takenAt
- source
- hasNote
@@ -32999,12 +33067,31 @@ components:
- createdAt
- updatedAt
additionalProperties: false
description: A stored lab result. Exactly one of `value` (numeric) or `valueText` (qualitative, e.g. "negativ") is
non-null; `rangeStatus` is always `unknown` for a qualitative row (no number to classify). `biomarkerId` links
the user-scoped catalog marker (null for legacy free-text rows); when linked, `analyte` / `unit` /
`referenceLow` / `referenceHigh` are RESOLVED server-side from the Biomarker — the client renders them and never
recomputes the range. The encrypted note is never echoed in list rows; `hasNote` flags its presence and the
single-resource GET returns the decrypted `note`.
description: >-
A stored lab result. Exactly one of `value` (numeric) or `valueText` (qualitative, e.g. "negativ") is non-null;
`rangeStatus` is always `unknown` for a qualitative row (no number to classify). `biomarkerId` links the
user-scoped catalog marker (null for legacy free-text rows); when linked, `analyte` / `unit` are RESOLVED
server-side from the Biomarker.
Three reference windows ride on the row and the server says which one applies. `referenceLow` / `referenceHigh`
are the window IN FORCE and the one `rangeStatus` was computed from — read these to render the range and you are
correct without doing any resolution of your own. `sourceReferenceLow` / `sourceReferenceHigh` /
`sourceReferenceText` are the range this reading's own report printed (the text verbatim, kept even when no
numbers could be read out of it). `catalogReferenceLow` / `catalogReferenceHigh` are the Biomarker band.
`referenceOrigin` names the winner and `referenceDivergesFromCatalog` is true when the report's window and the
catalog band state different limits — a surface showing only one of the two is showing a partial answer. The
client renders these values and never recomputes the range. The encrypted note is never echoed in list rows;
`hasNote` flags its presence and the single-resource GET returns the decrypted `note`.
LabReferenceRangeOrigin:
type: string
enum:
- source
- catalog
- none
description: Which window `referenceLow` / `referenceHigh` came from. `source` = the range printed on this reading's own
report, which wins for that reading because it is the window the physician evaluated the value against.
`catalog` = the user's Biomarker band, the net for readings with no printed range. `none` = no window on file.
LabReferenceRangeStatus:
type: string
enum:
@@ -33012,9 +33099,9 @@ components:
- below
- above
- unknown
description: "Server-computed, NEUTRAL reference-range verdict. `unknown` when the lab reported no usable bounds.
Inclusive bounds: a value on the limit reads in-range. The badge that renders this must stay calm and
informative — not an alarming red."
description: "Server-computed, NEUTRAL reference-range verdict, always against `referenceLow` / `referenceHigh` (the
window in force for that reading). `unknown` when no usable bounds are on file. Inclusive bounds: a value on the
limit reads in-range. The badge that renders this must stay calm and informative — not an alarming red."
CreateLabResultResponse:
type: object
properties:
@@ -33082,6 +33169,30 @@ components:
anyOf:
- type: number
- type: "null"
catalogReferenceLow:
anyOf:
- type: number
- type: "null"
catalogReferenceHigh:
anyOf:
- type: number
- type: "null"
sourceReferenceLow:
anyOf:
- type: number
- type: "null"
sourceReferenceHigh:
anyOf:
- type: number
- type: "null"
sourceReferenceText:
anyOf:
- type: string
- type: "null"
referenceOrigin:
$ref: "#/components/schemas/LabReferenceRangeOrigin"
referenceDivergesFromCatalog:
type: boolean
takenAt:
type: string
source:
@@ -33106,6 +33217,13 @@ components:
- unit
- referenceLow
- referenceHigh
- catalogReferenceLow
- catalogReferenceHigh
- sourceReferenceLow
- sourceReferenceHigh
- sourceReferenceText
- referenceOrigin
- referenceDivergesFromCatalog
- takenAt
- source
- rangeStatus
@@ -33288,6 +33406,14 @@ components:
anyOf:
- type: number
- type: "null"
referenceText:
anyOf:
- type: string
- type: "null"
description: The reference range EXACTLY as printed on the report, verbatim ("3,5 - 5,0", "< 116", "bis 5,0",
"negativ"). `referenceLow` / `referenceHigh` carry the same window reduced to numbers WHEN it reduces to
numbers; this field carries it either way, so a window stated in words is not lost between the report and
the reading.
takenAt:
anyOf:
- type: string
@@ -33325,6 +33451,7 @@ components:
- unit
- referenceLow
- referenceHigh
- referenceText
- takenAt
- confidence
- biomarkerMatch
@@ -33422,6 +33549,34 @@ components:
anyOf:
- type: number
- type: "null"
catalogReferenceLow:
anyOf:
- type: number
- type: "null"
catalogReferenceHigh:
anyOf:
- type: number
- type: "null"
sourceReferenceLow:
anyOf:
- type: number
- type: "null"
sourceReferenceHigh:
anyOf:
- type: number
- type: "null"
sourceReferenceText:
anyOf:
- type: string
- type: "null"
referenceOrigin:
type: string
enum:
- source
- catalog
- none
referenceDivergesFromCatalog:
type: boolean
takenAt:
type: string
source:
@@ -33449,6 +33604,13 @@ components:
- unit
- referenceLow
- referenceHigh
- catalogReferenceLow
- catalogReferenceHigh
- sourceReferenceLow
- sourceReferenceHigh
- sourceReferenceText
- referenceOrigin
- referenceDivergesFromCatalog
- takenAt
- source
- hasNote
@@ -33456,6 +33618,7 @@ components:
- createdAt
- updatedAt
additionalProperties: false
description: A written reading. Its reference window resolves exactly as it does on `/api/labs` — see `LabResult`.
ListBiomarkersEnvelope:
type: object
properties: