← The Machine Room · Case file 003: EHR Behavioral Tested end to end on synthetic sessions · Sheet 1 of 9
Case file: EHR Behavioral, from a therapy session to a signed record Drawing no. AH-CS-003

Therapy notes are often written from memory, hours later. Here the session itself becomes a signed, tamper-evident record.

Built for a New York practice that treats substance use, and for 42 CFR Part 2, the strictest privacy rule in American healthcare. The clinician reviews and signs a transcript labelled by speaker, and substance-use records live in databases of their own.

Consent → record → transcript by speaker → review → hold to sign
Every step of the session → a tamper-evident audit log
Audio deleted within 30 days of signing; the signed transcript is the record
42 CFR Part 2, HIPAA and New York law Designed for
3
Physically separate databases. Substance-use records never share one with the rest of the chart Verified
Drawn byAbu Huraira Mukhtar
RoleArchitect, sole developer
BuiltApril to May 2026
Designed for42 CFR Part 2, HIPAA, NY law
OrganisationWithheld; NY counseling practice
StatusTested end to end
DataSynthetic sessions
SourceSource code and project records
Sheet 2 of 9: In briefThe case in four parts

01 · The short version

The problem
Therapy notes are often written from memory, hours after the session, and that reconstruction becomes the record of care. In a practice that treats substance use, the record also falls under 42 CFR Part 2, which guards it more strictly than HIPAA does.
What I built
A system that records the session in the browser once the client has signed a standalone AI consent. Deepgram returns a transcript labelled by speaker; the clinician reviews it and holds to sign, and the transcript locks. Substance-use records are stored apart, and every step lands in a tamper-evident audit log.
How it went
Built in April and May 2026 and tested end to end, from the consent to a password-protected export, with 505 automated tests behind it. A real two-voice recording came back as five segments, each labelled with its speaker.
What you’d get
The same machinery for your practice, on HIPAA-eligible infrastructure under your BAAs: consent before recording, a transcript your clinicians sign, substance-use records kept apart and an audit trail you can show. Your privacy counsel reviews it before the first real session.
Sheet 3 of 9: Field notesThe before-state

02 · The chart is written from memory, at 11 p.m.

A counselor sees clients back to back. The notes wait until the last one leaves, and the two o’clock session is written up at the kitchen table as the third note of the evening.

Nothing was recorded, so the record of care is a reconstruction: what the counselor remembers hours later, after two more sessions and dinner. The sector has a name for it, pajama-time charting, and that after-hours load is a known driver of clinician burnout.

The reconstruction is the document a court, an auditor or the next clinician will treat as what happened. In a practice that treats substance use it carries a second weight: under 42 CFR Part 2, nearly every disclosure needs the client’s consent, even one to their own doctor outside the program.

The practice needed both at once: a faithful record of each session, and privacy controls it could defend.

General notes
1. A New York counseling practice; more than half its clients are protected by 42 CFR Part 2
2. One record under three rules: 42 CFR Part 2, HIPAA and New York law
3. Organisation withheld: detail available in interview
Fig. 1: Where the record of a session came from, by handAn illustrative day
Sheet 4 of 9: Before and afterOne session, step by step

03 · The clinician signs what was said

With the system, every step from consent to signature leaves a record, and the clinician keeps the final word.

StepBeforeWith the system
ConsentNothing was recorded, so there was nothing to consent toThe client signs a standalone AI consent before anything records. It names the vendors and the 988 crisis line, and any change to it asks for consent again
RecordingThe counselor listens and remembersThe browser records the session and sends the audio straight to secure storage, never through the app’s own server
Who said whatRebuilt from memoryDeepgram returns the transcript labelled by speaker. A transcript without speaker labels is refused
The recordTyped from memory, hours laterThe clinician plays back any segment and fixes a word or a speaker. The first draft and every edit are kept, with what changed
SignatureThe note is signed as rememberedThe clinician holds to sign, the transcript locks, and the signature is written to the audit log
Substance-use recordsIn most systems, one database with the rest of the chart, split by a flagTheir own databases and their own audio storage. An unclassified client is treated as protected
What the client and the clinician sawScreens from the system, redrawn in outline with fictional details
ConsentFacsimile
Consent to AI transcription of your session

Version 1.1.0. Please read this before signing, and ask your clinician any question about it.

Speech to textDeepgram Nova-3, U.S. region
StorageCloudflare R2, U.S. region, encrypted
AudioDeleted within 30 days after your transcript is signed
TranscriptKept with your clinical record, at least 6 years
TrainingNothing you say is used to train AI
Your choiceYou can say no and still receive care
CrisisCall or text 988, any time
I agree to AI transcription
Client’s full name, typed (fictional)
Capture consent
1. The consent. A standalone document the client signs before anything records. Any edit to it raises the version and asks for consent again.
TranscriptFacsimile
Review what was said.

This is a draft. Edit anything that needs your touch. When it reads right, sign it at the bottom to add it to the record.

Length 66 sModel deepgram:nova-3Segments 5
SPEAKER_00▶ 0:02
SPEAKER_01▶ 0:15
SPEAKER_00▶ 0:31
Speaker ▾
SPEAKER_01▶ 0:40
SPEAKER_00▶ 0:55
Prepare to sign
2. The review. A real 66-second recording of two voices came back from Deepgram as five segments, alternating between the speakers. The clinician can play back any segment, fix its words or reassign its speaker. Segment text is drawn as bars. Verified 2026-04-21
Signed transcriptFacsimile
Signed & on record

This transcript is locked. It can be read but not changed.

Export as encrypted PDF
Password: ••••••••••
Generate encrypted PDF

Press and hold until the bar fills. The download link works for 15 minutes, and the password opens the PDF.

3. The signature. The clinician types their name and holds the button until the bar fills. The transcript locks, and any copy leaves as a password-protected PDF behind a 15-minute link.
Sheet 5 of 9: Approval plateWhat the case rests on

04 · From consent to signature, every step is on record

3 + 2Databases, plus two separate stores for audio. Substance-use records are kept apart from the rest of the chart, in storage an auditor can point toVerified
13 event typesEvery step of a session, from consent to audio deletion, is written to a tamper-evident audit log, so any later edit showsVerified
≤ 30 daysAudio lifetime after signing. A retention sweep purges it, and a legal hold stops it. The signed transcript is the recordVerified: mechanism
Part 2The storage split and the audit trail are designed for 42 CFR Part 2, the federal rule for substance-use recordsDesigned for

How I’d start in your practice

I’d start by following one session through your practice, from the consent form to the signed note in the chart.

That shows which records fall under Part 2 and who may read each one, and those answers set the storage split and the access rules. The first version runs in the browser beside the EHR your clinicians already use, on infrastructure covered by your BAAs, and your privacy counsel reviews it before it records a real session.

Discuss a system like this
Sheet 6 of 9: General arrangementDrawing 003-A

05 · Every vendor sits behind a port

The system is a sixteen-package TypeScript monorepo built as ports and adapters. Its domain core holds sixteen use cases, from capturing consent to purging audio, and does no input or output of its own.

Transcription, storage, records, sign-in, export and the audit log each sit behind a port with an in-memory fake and a real adapter. One contract suite per port runs against both, so the fakes obey the same rules as the vendors. Four run modes, from fully in-memory to real Cloudflare R2, Deepgram and Cloudflare Access, are chosen by environment variables in one composition root.

The linter enforces the dependency direction, from contracts to core to app, and a Next.js 15 app serves 18 routes. The planned move to services under a BAA, such as AWS Transcribe Medical, RDS and Cognito, is a new adapter and one composition-root line per port.

As built
Next.js 15, strict TypeScript, pnpm and Turborepo
Drizzle over SQLite; the same schema deploys to Cloudflare D1
Vitest contract suites, Playwright, axe-core
Lint 16 of 16, typecheck 30 of 30, 505 tests Verified
Fig. 2: The general arrangement: one core, every vendor behind a portClass names are the source code’s
Detail A, see fig. 2, the ports

The compliance bill, deferred by an adapter

The prototype had to prove itself before anyone paid for HIPAA-grade infrastructure and BAAs, and the design had to survive that move without a rewrite.

Chose ports and adapters on free tiers with synthetic data, because the move to services under a BAA then becomes a new adapter file and one composition-root line per port. Cost: indirection everywhere, and sixteen packages for one module. Gained: the transcription vendor changed from Groq to Deepgram with no change to domain code (sheet 8, row A). ADR-009
Sheet 7 of 9: Section A-A, one sessionDrawing 003-B: cut through the privacy machinery

06 · The Part 2 boundary is built into the storage

Follow one synthetic session from the client’s consent to the chain that records it. The left edge of the drawing shows who acts at each step.

Fig. 3: Section A-A, one session through the privacy machinerySynthetic session; the steps and names are the code’s
Beat 1: consent.captured

1Consent, before anything listens

Nothing records until the client signs a standalone, versioned AI-use consent: plain English, the vendors named, the 988 crisis line, and a promise that nothing said trains an AI. An edit to the document raises its version and asks every client to consent again, so the consent works as a gate.

Beat 2: session.created

2The right store, from the start

Each client is classified at intake, and the session opens in that class’s store, part2_db or non_part2_db, with its audio bound for the matching bucket. An unclassified client is treated as Part 2. shared_db holds the client registry and the meta-chain.

Beat 3: session.recorded → transcript.transcribed

3Audio that skips the server

The browser records opus/webm and uploads it to R2 on a presigned URL, so audio bytes never pass through the application server. Deepgram Nova-3 fetches the file from storage and returns segments labelled by speaker. A response without speaker labels fails the use case: in a two-person room, who spoke is part of the record.

Beat 4: clinician.signed

4Hold to sign, and the transcript locks

The clinician fixes a word or reassigns a speaker, and each edit is chained per transcript beside the machine’s first draft. Then they type their name and hold to sign: the transcript locks and becomes the record. Export is an AES-128 password-protected PDF on a 15-minute link, and the audio is purged within thirty days.

Beat 5: the chain checks itself

5An audit log that shows any edit

All thirteen session event types append to a hash chain, where each entry’s SHA-256 covers its sequence number, the previous hash and its own canonical form. Each store keeps its own chain, and a meta-chain in shared_db anchors both. The dashed arc is the check on every read: store and flag must agree, and a mismatch is written into the chain it protects.

Detail B, see fig. 3, step 2

Three physical stores for the Part 2 boundary

The industry default for Part 2 is logical segregation: one database, a flag and discipline in every query. This practice treats substance use first, and more than half its clients are Part 2-protected, so a flag would guard most rows of every table.

Chose physical segregation, with three databases, two buckets and a routing audit log, because a boundary engineers cross every day belongs in the architecture: a connection-string mistake stops with an error, and an auditor can be shown the store itself. Cost: about three times the milestone, no transactions across databases (event-bus sagas carry that work) and twice the backup planning. Revisit when the substance-use share falls below 25%, or when the sagas outgrow the gain in audit readability. ADR-011
Detail C, the scope of the first phase

A transcript, and no AI-written note

AI-drafted clinical notes were the obvious demo. New York’s S.8484 bill welcomes AI for administrative work with consent, and bars AI from acting as a therapist or reading emotion; a note finalized without the clinician’s sign-off is a line this design never crosses.

Chose a verbatim, speaker-labelled transcript for the first phase, because consent, storage, audit and retention had to be proven before any model drafts clinical text. Cost: a quieter demo. Gained: the clinician stays the author, and the system stays in the administrative work the bill welcomes. Project context §4
Sheet 8 of 9: Revision historyAll entries traceable

07 · Each finding became a rule

Each row is a finding from review or browser verification, settled at its cause.

In a privacy system the lasting fix is a rule written down, so the same class of mistake cannot recur. Every row traces to an architecture decision record or the repository’s status log.

Rev.
Date
What we found
What changed
Trace
A
2026-04-20
VendorThe planned speech-to-text service, Groq Whisper, returns no speaker labels, and the module requires them: a transcript that cannot say who spoke fails the use case.
Swap at the portDeepgram Nova-3 with diarize=true became the primary adapter, with no change to domain code. The full accuracy benchmark became a gate before any pilot.
ADR-009
B
2026-04-24
Export and formsBrowser verification of the encrypted PDF export found pdfkit’s font files missing under the bundler. The same pass found the shared primary button defaulting to type="button", which swallowed form submissions.
Codify the fixpdfkit loads as a webpack external, and the button default became a written convention, so no later form fails the same way. In the same session the PDF opened with its password and no other way.
Status M11
ADR-010
C
2026-04-30
PrivacySupervisors needed to see exactly what changed in an edited transcript, and before-and-after text in the audit log would carry client words to every firm-wide auditor.
Keep the words with the transcriptChange history moved to a table that inherits the transcript’s access rules, with a hash chain per transcript. The audit log holds pointers, so its threat model survives the feature.
ADR-014
ADR-003
D
2026-05-05
TopologyOne database held both record classes, with a non_part2_flag as the wall between them: the kind of boundary an engineer under deadline pressure crosses without noticing.
Make the boundary physicalThree stores and two buckets landed across milestones M14a to M14c. The flag stays as an independent second check: every read asserts that store and flag agree, and a mismatch fires audit.tamper_signal.
ADR-011
M14a to M14c
Engineering recordChecked in the repository and its status log
505Tests passing across eleven packages. Each contract suite runs against the in-memory fake and the real adapter, so both obey the same rulesVerified
WCAG 2.1 AAAn axe-core scan of four key screens, from consent capture to the sessions list, finds no serious or critical violations. It runs beside the golden-path test, about 40 seconds in allVerified
<$1Deepgram cost of a ten-session synthetic pilot, extrapolated from the verified runs. Storage stays inside the free tiers by an order of magnitudeEstimate
Sheet 9 of 9: Close-outFor your practice

08 · What a version for your practice adds

The system takes a session from the client’s consent to a signed, locked transcript, with substance-use records stored apart and every step in a tamper-evident log.

A version for your practice keeps that machinery, moves it onto infrastructure covered by your BAAs and adds the Part 2 controls your records need. Your privacy counsel reviews it before the first real session.

Scope
Module: session transcription
Data: synthetic sessions
Designed for: 42 CFR Part 2, HIPAA, NY law
Built: April to May 2026
Row
Part
As built
Your version adds
A
Infrastructure
Swappable vendorsFree tiers of Cloudflare and Deepgram, each one replaceable
Your BAAsHIPAA-eligible services under your BAAs and QSOAs, swapped in without a rewrite
B
Exports
Encrypted PDFA password-protected PDF behind a 15-minute link
The Part 2 noticeThe redisclosure notice on every exported record
C
Disclosures
AI consentA standalone, versioned consent, asked again whenever it changes
Part 2 consentDisclosure consent under Part 2, and an accounting of every disclosure on request
D
Audit trail
Tamper-evidentA chained log for each record class, anchored in a shared third
A durable audit storeThe same logs in durable storage, with reports your auditors can run
E
Sign-in
RolesClinician and admin roles behind Cloudflare Access
Your identity providerYour sign-in, with every token verified and multi-factor login
F
Pilot
Tested end to endThe full path on synthetic sessions, and a real two-voice recording labelled by speaker
A supervised pilotYour clinicians on real sessions, with transcription accuracy and the time saved on notes measured
Discuss a system like this
Drawn & built by
Abu Huraira Mukhtar, architect and sole developer, 2026
A. H. M.
Sheet 9 of 9
End of case file 003
The Drawing Office: after hours

When the chart is written from memory, make the session itself the signed record.

If your clinicians write notes at night and your substance-use records sit behind a flag, the work starts with one session traced from consent to signature. Bring your consent form.

Contact me Back to the Machine Room