Skip to main content
prioritize() in lib/pipeline/prioritize.ts takes an array of PrioritizeInput objects and today’s ISO date, then returns a sorted PrioritizedLine[]. Every calculation is deterministic arithmetic based on the notice date, billed amount, and recovery probability from triage — no OpenAI calls occur in this step. Because prioritization is pure computation, it is straightforward to unit test and verify independently of any model behavior.

Exported functions

calculateAppealDeadline

Computes the Medicare redetermination deadline from the remittance notice date. The calculation adds RECEIPT_PRESUMPTION_DAYS (5) to the notice date to get the presumed receipt date, then adds REDETERMINATION_DAYS (120) to get the filing deadline. The result is an ISO 8601 date string. Example: a notice date of 2025-01-01 produces a presumed receipt date of 2025-01-06 and a deadline of 2025-05-06.

calculateUrgency

Returns a 0–1 urgency score. The formula clamps (URGENCY_SCALE_DAYS − daysRemaining) / URGENCY_SCALE_DAYS between 0 and 1. When the deadline is URGENCY_SCALE_DAYS (125) or more days away, urgency is 0. When the deadline has passed, urgency is 1.

adaptForPrioritization

A join helper used by the route handler to assemble a PrioritizeInput from the full parsed claim, the specific service line being worked, and its triage result. It promotes notice_date from the remittance header and group_code, carc, rarc, billed_amount, description, and notes from the service line into a flat AdjustmentLine, then pairs that with the DenialTriage.

prioritize

Maps each input to a PrioritizedLine, then sorts the result. For each item it calls calculateAppealDeadline() and calculateUrgency(), then computes priority_score.

Source

Priority score formula

Each factor contributes a distinct dimension of importance:
  • billed_amount — the financial exposure. Higher billed amounts produce higher scores, reflecting the revenue at stake.
  • recovery_probability — from DenialTriage. A 0–1 float representing how likely working this denial leads to payment. A write-off line carries 0; a strong timely-filing appeal may carry 0.7 or higher.
  • urgency — from calculateUrgency(). A 0–1 float that rises as the deadline approaches. Lines with far-off deadlines score near 0; lines whose deadlines have passed score 1.
A line with a high billed amount, high recovery probability, and an imminent deadline will float to the top of the queue. A line with a low billed amount or a write-off probability of 0 will score 0 regardless of urgency.

Sort order

Lines are sorted by two keys in sequence:
  1. deadline ascending — using ISO string .localeCompare(). Lines whose deadlines arrive first are worked first, regardless of score.
  2. priority_score descending — within lines sharing the same deadline, higher-scoring lines appear first.
This means deadline always takes precedence. A low-value line due tomorrow ranks above a high-value line due next month.

Constants

All date arithmetic uses UTC calendar days via Date.setUTCDate() and toISOString().slice(0, 10). This prevents timezone drift from shifting a deadline by a day when the server runs in a non-UTC timezone.