Sami Bentaleb
All work

Keystra

A review tool for prescription shorthand. It shows what the parser could read and flags the rest for a pharmacist to fill in.

Year
2026
Stack
TypeScript, PostgreSQL, React
One prescription row: the shorthand as typed and the fields read from it.
Fixture data.

The problem

A pharmacy software client had to deal with the instruction line on every prescription, such as "1 tab orally 2 times a day". A parser or spreadsheet had already turned each line into fields, but the result was often half right: the wrong route, an inferred dose, a multi-step regimen squashed into one string. Someone still had to fix the fields against a controlled vocabulary, approve or skip each line, leave a record of who changed what, and stop two reviewers from editing the same line.

First built for a pharmacy software client in 2024, then rebuilt from scratch as Keystra.

Keystra takes on the same problem from the start. Prescribers write directions in shorthand, as in "take 1 tab po bid prn pain", and much of it is ambiguous. Before a pharmacy system can check a dose, print a label or keep a record, that line has to become structured fields, and a field filled wrongly can put the wrong dose on a label. So Keystra has to say what it did not understand, and a pharmacist has to approve every line.

My part

I was the only engineer on the 2024 version, from API design and database schema to deployment. I built Keystra from scratch, including the parser, review screens and release process. I also wrote the upload checks, tenant isolation and audit trail.

Architecture

TypeScript throughout, in a pnpm and Turborepo workspace: a Hono API with typed OpenAPI routes, a Vite and React review app, and packages for the domain, the rules, the parser, the upload gate and a scoring harness, with AWS CDK for the infrastructure. Data lives in PostgreSQL 18 through Kysely, and sign-in uses Better Auth.

The parser runs on a declared grammar and never guesses. It splits a line into steps, reads fields with a catalogue of rules, and fills a field only when every word in its clause is accounted for. Anything left over comes back as exact character offsets, which the review screen marks as unread. A finding can only describe a problem and never carries a value, and a rule that says something is missing doesn't fire on text nobody read.

Uploads go through a gate before anything is stored. The upload has one closed shape: the instruction text, an optional count and an optional dose form. Twenty checks run on every row, six of them for identifiers such as names and phone numbers, and a single hit refuses the whole file. There is no override and no quarantine, and nothing is written before the gate passes, so a refused file leaves an audit event and no rows.

Every tenant table carries a tenant id under forced row-level security, and request code is never handed a connection. It goes through one function that sets the tenant for a single transaction. The application role can only select and insert on the review tables, so history is append-only. A correction inserts a new version of a row's reading and never updates it, and a release pins the exact version a pharmacist approved. The database enforces that: a trigger refuses a release member that was never approved.

Hard parts

A pooled connection that leaks a tenant. A tenant setting made on a pooled connection outlives its request and shows one tenant another's rows. The tenant is now set with a transaction-local setting, together with a statement timeout, and row-level security is forced so even the table owner is policed. That bit the maintainers too: a migration that backfills data had to lift the force and restore it inside the same transaction.

Free text in an append-only table. A note explaining a correction can hold a name, but the app can't update or delete. So the note is checked by the gate's detectors before it is read, stored with the version of the detector that passed it, never copied into the audit log, and removable only by an owner-only command that clears it and records the removal without reading it.

A release that disagrees with itself. If someone reopens a row while a release is being cut, the count and the members could differ. Three layers prevent it: one query snapshot for the preview and the cut, an advisory lock with a version and fingerprint re-check at cut time, and a deferred constraint trigger that checks the member count at commit.

A rate limiter that can't be turned against the server. Sign-in refuses before any password is hashed, and the counter itself has a ceiling of 10,000 keys with oldest-first eviction, so someone choosing keys can't grow it without bound.

  • 10,000keys at most in the rate limiter's counter

Outcome

Nothing is deployed, so Keystra has no users and no live link. What exists is a review slice that works end to end: CSV upload through the gate, parsing, findings, correction, approval, an append-only audit trail, pinned releases and JSON and CSV export. It has about 1,600 test cases and a browser test that drives sign-in, a correction, an approval, a cut and a download. The vocabulary the parser reads from is still being written, so for now it abstains on nearly every line and the fields on screen are the pharmacist's work.

  • 1,600test cases

Video