<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Devendra Gurubilli</title>
    <description>The latest articles on DEV Community by Devendra Gurubilli (@devendra_gurubilli_b2dc69).</description>
    <link>https://dev.to/devendra_gurubilli_b2dc69</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4149219%2Fb48a709a-5cbc-4beb-8b02-b010e2c5a2ab.png</url>
      <title>DEV Community: Devendra Gurubilli</title>
      <link>https://dev.to/devendra_gurubilli_b2dc69</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devendra_gurubilli_b2dc69"/>
    <language>en</language>
    <item>
      <title># How Hindsight Turned Fragmented Visit Data Into a Usable Timeline</title>
      <dc:creator>Devendra Gurubilli</dc:creator>
      <pubDate>Tue, 29 Sep 2026 09:35:08 +0000</pubDate>
      <link>https://dev.to/devendra_gurubilli_b2dc69/-how-hindsight-turned-fragmented-visit-data-into-a-usable-timeline-ajc</link>
      <guid>https://dev.to/devendra_gurubilli_b2dc69/-how-hindsight-turned-fragmented-visit-data-into-a-usable-timeline-ajc</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxknj9s75pchh4dshhyr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxknj9s75pchh4dshhyr4.png" alt=" " width="800" height="231"&gt;&lt;/a&gt;How Hindsight Turned Fragmented Visit Data Into a Usable Timeline&lt;/p&gt;

&lt;p&gt;Seven visits. Seven Firestore documents. Seven correct AI summaries. Zero sense of what had changed across them — and no way to tell without reading all seven in order.&lt;/p&gt;

&lt;h2&gt;
  
  
  What MediPass Does and How It Fits Together
&lt;/h2&gt;

&lt;p&gt;MediPass is a digital health passport built around the physical workflow of a hospital outpatient department. It doesn't try to replace the paper prescription or the face-to-face consultation. It wraps around them.&lt;/p&gt;

&lt;p&gt;The flow is concrete: a patient arrives, shows their phone number at the reception desk, a receptionist verifies identity via OTP and generates a transactional visit number, the patient enters the doctor's queue, the doctor runs a read-only consultation, the receptionist uploads the paper prescription and any new lab reports at checkout, and the patient's phone gets a plain-English AI explanation of every document the moment it lands. That's the loop. It closes at every visit without requiring anyone to change how they already work.&lt;/p&gt;

&lt;p&gt;Four roles, four portals, one Firestore database. The stack: React 18 with TypeScript, Vite, Firebase Firestore with real-time &lt;code&gt;onSnapshot&lt;/code&gt; listeners, Firebase Auth phone OTP, Cloudinary for document CDN, and Groq's Qwen 27B vision model for multimodal document parsing. No backend server — Firestore security rules enforce role isolation at the database layer. The doctor portal has no search bar, no upload button, no edit access. Those aren't hidden UI elements; they're absent because the underlying Firestore rules reject any write from a doctor role except the single narrow status transition that marks a session as consulted.&lt;/p&gt;

&lt;p&gt;The Firestore schema has one document per uploaded file in the &lt;code&gt;records&lt;/code&gt; collection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// lib/firestore.ts — records collection interface&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;MedicalRecord&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;patientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;sessionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;hospitalId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;uploadedByStaffId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;prescription&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lab_report&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;imaging_report&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;discharge_summary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;sourceFileUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Cloudinary CDN URL&lt;/span&gt;
  &lt;span class="nl"&gt;extractedText&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// Tesseract.js OCR output&lt;/span&gt;
  &lt;span class="nl"&gt;aiExplanation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// patient-facing plain-English summary&lt;/span&gt;
  &lt;span class="nl"&gt;vitals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;bloodPressure&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;heartRate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;spo2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="nl"&gt;createdAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Timestamp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;aiExplanation&lt;/code&gt; field is the output of the Groq vision pipeline — an accurate, plain-English description of whatever document was uploaded during that visit. The problem I'm about to describe is not about that field being wrong. It's about what that field can't tell you on its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Fragmentation Problem
&lt;/h2&gt;

&lt;p&gt;Every &lt;code&gt;records&lt;/code&gt; document in Firestore is a correct, self-contained description of one document from one visit. That's exactly what you want from a per-document store. It's also exactly the wrong unit of information for answering clinical questions about a patient's trajectory.&lt;/p&gt;

&lt;p&gt;A patient with seven OPD visits for a chronic condition — hypertension managed with a beta-blocker, quarterly BP readings, a medication adjustment at visit four — has seven &lt;code&gt;records&lt;/code&gt; documents. Each one has an accurate &lt;code&gt;aiExplanation&lt;/code&gt; field. What those seven fields don't contain, in aggregate, is the answer to the question a patient actually wants answered when they open their dashboard: &lt;em&gt;What's changed? Is my condition improving? Am I still on the same medication? When did that change?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;My first attempt to solve this was naive: load the last N records, concatenate their &lt;code&gt;aiExplanation&lt;/code&gt; fields into a single string, pass it to the model, ask it to synthesize a longitudinal summary. It worked for N=2. At N=5 it started producing vague output. At N=7 across a patient with multi-page lab reports and prescription uploads mixed together, it hit token limits and the model spent its capacity organizing the data dump rather than reasoning over it. The output was technically correct and clinically useless.&lt;/p&gt;

&lt;p&gt;The problem with concatenation-as-context is that you're handing the model raw chronological material and asking it to do the distillation work at read time, for every patient, on every dashboard load. That doesn't scale — not in tokens, not in latency, not in coherence.&lt;/p&gt;

&lt;p&gt;The real problem was architectural: I had invested nothing at write time and was paying for it entirely at read time.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Hindsight Changed the Write-Time Investment
&lt;/h2&gt;

&lt;p&gt;The framing that shifted my thinking came from reading about &lt;a href="https://vectorize.io/what-is-agent-memory" rel="noopener noreferrer"&gt;structured agent memory&lt;/a&gt; as a first-class architectural concern rather than a retrieval optimization. The distinction is this: retrieval assumes the source documents are the right unit to query against. Memory assumes you need a separate, maintained representation of what's clinically significant — updated incrementally as new information arrives, structured by category, queryable by recency — so that reads are targeted and fast rather than exhaustive and slow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/vectorize-io/hindsight" rel="noopener noreferrer"&gt;Hindsight&lt;/a&gt; implements this as a namespace-per-entity model. Each patient gets a memory namespace. Clinical facts extracted from new records are upserted into that namespace with source record references and timestamps. Prior values for the same fact aren't overwritten — they're preserved with their timestamps, building a time series that the dashboard can query directly rather than reconstructing from raw document history.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://hindsight.vectorize.io/" rel="noopener noreferrer"&gt;Hindsight documentation&lt;/a&gt; describes the upsert semantics clearly: a fact with a matching key gets a new entry appended to its history rather than the old entry replaced. For medication tracking, that means a medication change at visit four doesn't erase the original prescription from visit one — it appends a new entry with the updated dosage and a pointer back to the visit four record. The full change history is in the namespace, queryable as a time series.&lt;/p&gt;

&lt;p&gt;I inserted the memory extraction step into the upload pipeline between the AI explanation call and the Firestore write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// lib/uploadPipeline.ts — the write sequence&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processUpload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;File&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UploadContext&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// 1. Upload to Cloudinary — real URL before anything proceeds&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sourceFileUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;uploadToCloudinary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// 2. OCR (Tesseract.js, runs client-side in a Web Worker)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;extractedText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;runOCR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// 3. AI explanation — Groq Qwen 27B via API&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;aiExplanation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;runAIExplainer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;extractedText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recordType&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// 4. Memory extraction — upsert facts to Hindsight before Firestore write&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;recordId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;records&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;hindsight&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upsert&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;namespace&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`patient:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;patientId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;facts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;extractClinicalFacts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;aiExplanation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recordType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;recordId&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="c1"&gt;// 5. Firestore write — last, after all upstream steps confirmed&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;setDoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;records&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;recordId&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;patientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;patientId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;sessionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sessionId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;hospitalId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hospitalId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;uploadedByStaffId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;staffId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recordType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;sourceFileUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;extractedText&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;aiExplanation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;vitals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;vitals&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;createdAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;serverTimestamp&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;extractClinicalFacts&lt;/code&gt; parses the already-generated &lt;code&gt;aiExplanation&lt;/code&gt; — not the raw OCR text — and returns a typed array of facts by category: &lt;code&gt;medications&lt;/code&gt;, &lt;code&gt;vitals_trend&lt;/code&gt;, &lt;code&gt;lab_trend&lt;/code&gt;, &lt;code&gt;allergies&lt;/code&gt;, &lt;code&gt;diagnoses&lt;/code&gt;. Each fact carries the &lt;code&gt;recordId&lt;/code&gt; as a source reference so the timeline can link back to the original document.&lt;/p&gt;

&lt;p&gt;The Firestore write is last because a record document with a missing &lt;code&gt;sourceFileUrl&lt;/code&gt; or empty &lt;code&gt;aiExplanation&lt;/code&gt; is worse than no record. The memory upsert happens before the Firestore write so the two are in sync: by the time the record is visible to the patient's dashboard listener, the memory namespace already reflects its contents.&lt;/p&gt;

&lt;h2&gt;
  
  
  Querying the Timeline
&lt;/h2&gt;

&lt;p&gt;On the patient dashboard, the context for the longitudinal timeline panel comes from two parallel reads:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// lib/patientContext.ts — parallel reads at dashboard load&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;buildPatientDashboardContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;patientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;recentRecords&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;patientMemory&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="c1"&gt;// Firestore: last 3 records for the current visit detail cards&lt;/span&gt;
    &lt;span class="nf"&gt;getRecentRecords&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;patientId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;

    &lt;span class="c1"&gt;// Hindsight: full structured memory for the longitudinal timeline&lt;/span&gt;
    &lt;span class="nx"&gt;hindsight&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;memory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;namespace&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`patient:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;patientId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;medications&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vitals_trend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lab_trend&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;allergies&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;diagnoses&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;recencyWeight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.65&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;recentRecords&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;timeline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;buildTimeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;patientMemory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;facts&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;buildTimeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;facts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HindsightFact&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="nx"&gt;TimelineEntry&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Group by category, then sort each group by recordedAt ascending&lt;/span&gt;
  &lt;span class="c1"&gt;// to produce per-category time series suitable for trend display&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;grouped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;groupBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;facts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;grouped&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(([&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;entries&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recordedAt&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;getTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recordedAt&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;getTime&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;sourceUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;resolveRecordUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sourceRecordId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;})),&lt;/span&gt;
  &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;recentRecords&lt;/code&gt; from Firestore feed the current visit panel — the detailed per-document cards at the bottom of the dashboard. The &lt;code&gt;timeline&lt;/code&gt; from Hindsight feeds the longitudinal summary panel at the top. They answer different questions and are built from different sources. Neither read blocks the other.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;recencyWeight: 0.65&lt;/code&gt; on the Hindsight query means more recent facts rank higher in the results, but older facts — a drug allergy documented two years ago, a chronic diagnosis from the first visit — aren't excluded. A static allergy flag is more important than a recent-but-normal blood pressure reading. The weighting keeps both in context without requiring hand-tuning of which specific facts to always include.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the Patient Actually Sees
&lt;/h2&gt;

&lt;p&gt;For the hypertension patient with seven visits, the timeline panel renders something like this above the individual visit cards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;📋 Health Timeline

Diagnoses
  • Essential hypertension — since Jun 2024 (Visit 1)

Medications
  Jun 2024  Amlodipine 5mg — once daily
  Sep 2024  Amlodipine 5mg — once daily  [no change]
  Dec 2024  Amlodipine 10mg — once daily  [dose increased ↑]
  Mar 2025  Amlodipine 10mg + Telmisartan 40mg  [second agent added]
  Jun 2025  Amlodipine 10mg + Telmisartan 40mg  [no change]

BP Trend (last 7 visits)
  Jun → Sep → Dec → Mar → Jun → Sep → Dec
  158/98 → 152/94 → 148/90 → 142/88 → 136/84 → 132/82 → 130/80
  ↓ controlled improvement

⚠️  Flags
  • Allergy: ACE inhibitors (documented Sep 2024 — persistent)
  • Creatinine borderline elevated at last visit [VERIFY WITH DOCTOR]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That panel is built entirely from the Hindsight memory namespace — no Firestore reads, no re-summarization, no token cost beyond the initial writes at upload time. The medication history is a time series of &lt;code&gt;medication&lt;/code&gt; facts, each with a timestamp and a link back to the source record. The BP trend is a time series of &lt;code&gt;vitals_trend&lt;/code&gt; facts. The allergy flag is a &lt;code&gt;allergies&lt;/code&gt; fact from visit two that has been present in the namespace ever since, surfacing at the top of the context because persistent flags rank high regardless of recency weighting.&lt;/p&gt;

&lt;p&gt;Below the timeline panel, the last three visit records render their full per-document AI explanations as detail cards. A patient who wants to read exactly what was in last month's prescription can tap through to the original document — the &lt;code&gt;sourceUrl&lt;/code&gt; in each timeline entry links back to the Cloudinary CDN URL stored on the &lt;code&gt;records&lt;/code&gt; document.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Invest at write time or pay at read time.&lt;/strong&gt; Concatenating past summaries into a context window at read time is the deferred cost version of the same work. It's expensive in tokens, slow for patients with long histories, and produces unfocused output because the model is organizing data rather than reasoning over it. Write-time extraction from the already-generated AI explanation is cheap; it runs against structured text, not raw documents. The per-patient cost is paid once per upload and amortized across every subsequent dashboard load.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory and documents are different data structures for different questions.&lt;/strong&gt; A Firestore &lt;code&gt;records&lt;/code&gt; document answers: what was in this specific file on this specific date? A Hindsight memory namespace answers: what is true about this patient across their clinical history? Those are different questions that need different data structures. Building one to do the work of both produces a system that technically has all the data and practically can't surface the right context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Firestore write should always be last.&lt;/strong&gt; Without a backend transaction that wraps the full pipeline, write order is your consistency boundary. If the OCR step fails after the Cloudinary upload but before the Firestore write, the patient has no record of the upload in their timeline — that's fine, the file exists in Cloudinary and the receptionist can retry. If the Firestore write succeeds before the Hindsight upsert, the record exists in the patient's dashboard but isn't reflected in their longitudinal timeline — that's a silent inconsistency that's much harder to recover from. Memory upsert before Firestore write, always.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time series needs source references to be navigable.&lt;/strong&gt; The first version of the Hindsight upsert didn't carry &lt;code&gt;sourceRecordId&lt;/code&gt; on each fact. The timeline showed a BP trend but there was no way to navigate from a data point in the trend back to the original document. Adding &lt;code&gt;sourceRecordId&lt;/code&gt; to every fact at write time — before the Firestore write, so the ID is available — turned the timeline from a read-only display into a navigable history. A patient who sees an unexpected value in their trend can tap it and read the original document it came from.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recency weighting is not recency filtering.&lt;/strong&gt; A drug allergy flag from three years ago is more important than a normal blood pressure reading from last week. Any memory query for clinical context needs to keep both in scope — recent events for current context, persistent flags regardless of age. The &lt;code&gt;recencyWeight&lt;/code&gt; parameter on the Hindsight query does this without hand-coding which specific fact categories to always include; it weights recent facts higher without excluding old ones, and persistent flags naturally rank high because they're present in every query result.&lt;/p&gt;

&lt;p&gt;The seven visit records were accurate from the start. What was missing wasn't accuracy — it was continuity. Getting continuity required a different write path, a different query, and a different data structure from the one Firestore naturally provides. The Firestore records didn't change; they're still there, still correct, still the source of truth for what was in each document. What changed was the layer that sits between those records and the question a patient actually wants answered when they open their dashboard.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
