Build diary Day 9

Day 9: Apps Share Code, Never Runtime

Built with Giridhar, Venkatakrishna and Preetha

Building the Caladrius PHR with agents Part 9 of 15

15 min read

Hand-drawn sketch of three separate, locked apps, each with its own API and data, built on a shared code foundation of Auth, Cally, Forms, UI and Server Core, with a note reading Share code. Keep runtime separate

The PHR is built by a small Caladrius team (Giri, Krishna, Preetha and me) working with AI agents; each post ends with what the day meant for each of our areas.

For three days, from 6 to 8 November, nothing merged. After a week that averaged about twelve pull requests a day into dev (nearly twenty with promotions), the merge log is empty for all three days, and almost all the commits sit on branches that merged later.

This wasn’t a holiday. It was the most consequential stretch of the project so far, and it produced mostly documents: five plans that turned a single app into the home of a Caladrius platform. The idea that holds them together is one line, now in the “Architecture direction” section of AGENTS.md:

Apps share code, never runtime.

This post is about why we stopped, what we proposed, and how those proposals were written so that agents could build from them the day after.


1. Why stop

By the end of Day 8 the PHR had almost everything the ABDM (what is ABDM?) milestones ask for, UHI search and booking built, and a demo of Personalised Intelligence. Three requests then arrived that didn’t fit the way we’d been working:

  1. Forms. Personalised Intelligence runs on questionnaires: a mental-health check, a heart-health check, a lung assessment, a biomarker panel. In the demo they were hand-built screens. They needed to be real FHIR SDC forms, with the answers stored, scored and trended. AI Studio, the earlier Caladrius product, already authors SDC forms.
  2. A rule about AI Studio. On 8 November I decided that nothing is pushed to AI Studio until the PHR is verified and certified. Anything we’d planned to build there, a “PHR admin module” for authoring forms, for example, would now be built here.
  3. People who run the PHR. Every user so far was a patient signing in with ABHA. Someone has to author forms, manage staff and read the audit trail, and none of that belongs in a patient’s phone app.

Building those three as PHR-only code would have been fastest that week. It would also have meant writing sign-in, sessions, an AI assistant and a form engine a second time the next time Caladrius built an app, and a third time after that. On 6 November I wrote down the parts every Caladrius product needs: Cally (our assistant), login (authentication and authorisation), SDC Forms and the Caladrius design system. The question for the pause was how to build those once, in this repository, without breaking the thing that made the PHR certifiable in the first place: it shares nothing with any other product at runtime.

2. The principle

The first plan, shared platform packages (7 November), starts with five principles. They’re short enough to quote almost in full, and every later plan inherits them:

# Principle What it rules out
1 Share code, not runtime. Every app keeps its own database, API, deployment, secrets, sessions, audit trail, NHA evidence and security audit. A shared login service, a shared database, a shared Cally backend
2 One direction only. Apps import packages. A package never imports an app; an app never imports another app. Circular dependencies; one app reaching into another
3 No product logic in a package. ABDM sign-in, the PHR’s Cally tools and the PHR’s form tables stay in the PHR. A “shared” package that is really the PHR in disguise
4 Extraction never changes behaviour. Moving code changes no response, header, cookie, log line or error message. “While I’m here” changes inside a security refactor
5 Packages ship TypeScript source, built by each app. A second build system; drift between a package’s build and its source

The mechanism that makes principle 1 work is ports. A package never reads an environment variable, opens a database connection, holds a secret or calls a network service on its own. The app hands it what it needs (config, a query function, a key-value store, an audit writer, a model client) through typed interfaces. A package’s library code literally cannot know which app it’s running in.

Why insist on this? Because of certification. The PHR goes through NHA’s functional tests and a WASA security audit as its own integrator. If two apps shared a session service, a change for one would put the other’s certification in question. If they share only code, each app takes a package version when it’s ready, re-runs its own gates and records the change in its own security changelog.

3. The map of packages

The plan lists what moves where. @caladrius/* is the platform scope (the design system already used it):

Package Holds Stays in the app
@caladrius/server-core Logger with field redaction, AES-256-GCM field crypto, audit sealing, the key-value client, the static-SQL guard, env-validation helpers, the error handler, security headers Each app’s env schema, keys, database pool, audit table
@caladrius/auth Sessions and refresh rotation, cookies, CSRF, the three-tier rate limiter, RBAC, request validation, RS256 token checks How a person signs in (the PHR: ABDM P1), the user table, the permission rows
@caladrius/auth-react The browser session client, session context, guarded routes Sign-in screens
@caladrius/cally, cally-react Model providers, the guard chain, the personal-data filter, thread ownership; the chat surface The tools an app exposes, its instructions, its consent table, which models it allows
@caladrius/forms FHIR SDC forms: schema, enable-when, validation, scoring, serialisation; the React renderer Form tables, encryption, consent, who may fill what
@caladrius/abdm The ABDM client: gateway session, headers, error decoding (later split, so the PHR’s own flows stay PHR-side)
@caladrius/ui, tokens, schemas Already shared: components, the base theme, types Brand tokens and white-label themes

Databases stay with each app. A package may document a reference schema in its README; each app writes its own migrations, in its own folder, with its own table prefix.

4. How certification stays per app

The part of the plan I spent longest on is the table that answers “if a package is shared, how is each app still certified on its own?”

Concern How
What an app ships Each app’s SBOM lists the workspace packages it includes, with version and commit. Security audit evidence names that SBOM.
A package change reaching a certified app CI works out which apps depend on the changed package and runs those apps’ lint, tests, build, NHA screen cases and security gates.
Functional tests Each app keeps its own NHA catalogue and specs. A package never carries NHA cases; it has unit tests of its own.
Security audit scope Each app is deployed and attacked on its own origin. A package has no endpoint of its own; its middleware is tested inside each app that mounts it.
Security findings Fixed once in the package; every consuming app re-runs its gates and records the fix in its own changelog.

The second row extended an existing script, nha-affected.mjs: given a change, it lists the NHA case ids of every screen that imports a changed package, so a one-line change in a shared component re-runs exactly the certified cases it can affect.

5. Two kinds of user, and a console for one of them

The second plan, authentication and authorisation, and the PHR Console (8 November), answered the third request.

The database had been pointing this way since Day 0: every user has an is_patient flag; every role has an audience, patient or operator; and a trigger makes it impossible for a patient to hold an operator role or the reverse. What was missing was the operator half: a way to sign in, roles, and somewhere to work.

  PHR user (is_patient = true) Application user (is_patient = false)
Who A person, their family, a citizen Caladrius staff today; a hospital’s staff if it hosts the PHR later
Signs in with ABDM, as today The installation’s identity provider: workspace single sign-on, plus an emergency account
Where The PHR app, portrait phone or iPad The PHR Console, a desktop app on its own origin
Sees health data Their own, and what was shared with them None by default
Session As today Shorter, multi-factor, with step-up re-authentication for sensitive actions

The split is enforced at four layers, so no single mistake opens it: the database trigger on roles; a kind claim in the session token, checked by middleware; the routes (operator routes refuse patient sessions, and patient routes refuse operator sessions); and the apps (the console has no patient screens, the PHR no admin screens).

One recommendation in that plan I’d underline for anyone building health software: can Support ever see a person’s health data? No. If it is ever needed, it will be with the person’s consent, time-boxed, audited and shown to the person. Writing the “no” down before the first support ticket arrives is much easier than after.

The plan also introduced an installation profile: one row per deployment that says who runs this PHR (Caladrius hosted now; hospital hosted possibly later), with the operator’s legal details, privacy notice version and branding. It’s read at runtime, not baked into a build. Hosting mode is chosen when an installation is created and is not a toggle; changing it later is a migration with its own checklist. Nothing hospital-specific gets built until two open questions are answered, one for counsel (who is the data fiduciary) and one for NHA (whose ABDM identity a hospital-run copy uses).

6. Forms: a plan revised the same day

The third and fourth plans were about forms, and they’re a good example of a plan changing within hours.

The SDC forms package plan (8 November, morning) set out @caladrius/forms and how a person fills in forms in the PHR:

In that version, authoring stayed in AI Studio. That afternoon the “nothing pushed to AI Studio” rule made it impossible, so the forms studio plan brought authoring here: a forms server package behind a repository port, a forms studio package for the editor, and a live preview that uses the same renderer the person sees, at phone and iPad sizes. Then the auth plan (§5 above) arrived the same day and folded the studio into the PHR Console as one section. The forms studio plan records the change (“Update (same day): …”), so an agent reading the forms studio plan finds out that “the studio” now means a console page before it builds a separate app.

The forms studio plan also lists the gaps it would fill rather than copy: AI Studio has no editor for conditional questions (enable-when) and strips scores, while the PHR needs both for PHQ-9, GAD-7 and the lung check.

7. Terminology: what may leave the API

The fifth plan, @caladrius/terminology, makes code lookups (SNOMED CT concepts, LOINC codes, value sets) a common package, so a form’s questions can be linked to standard codes. The default server is BHTS, India’s national terminology service run by NRCeS.

Its most important section is the shortest: what may leave the API.

8. Copy first, then adapt

Much of what the plans describe already existed in AI Studio. The obvious move, “port it”, hides a problem: after a few rounds of adaptation nobody can tell which lines came from where, or what changed and why. So on 8 November I added a rule:

Code from AI Studio is copied first, then adapted. Its files are copied unchanged into vendor/ai-studio/<commit>/ (same relative paths, a MANIFEST.md naming the repo, branch, commit and each file), and adapted from there … in later steps, so every change from the original stays visible in git. vendor/ is never built, linted, typechecked or imported.

That gives three things at once. Provenance: every adapted file can cite the copy it came from. Reviewability: the adaptation is a diff against the original. And safety: the AI Studio repository itself is never touched. The manifest records a SHA-256 for each file, and states what was deliberately left out: anything holding personal data or secret material was excluded and replaced, where needed, by just the table definitions.

The rules went into tooling the same night. scripts/check-boundaries.mjs, now the first step of npm run lint, fails on a package importing an app, an app importing another app, a package’s library code reading the environment, and any import from vendor/. Its --self-test plants a violation of each rule in a temporary tree and proves each one is caught. Security packages got code owners, so changes to them ask for my review.

9. Sunday evening: the first floors go up

The plans were written for agents to build from, with steps numbered across all five documents (S0 for the ground rules; S1 server-core; S2 auth; F1–F8 for forms filling; A0–A8 for authoring; T0–T4 for terminology; C0–C2 for the console), each with its class and a “done when” condition.

By the evening of the 8th, agents were building on branches. Between 5 pm and 10:30 pm the forms package went from its core rules (F1), through the renderer (F2), scores and safety rules (F3), trends (F3b), storage and API (F4), the PHR’s Health forms screens (F5) and reading biomarker values from reports (F5b), to Cally making forms within guardrails (F6). At 11 pm the ground rules (S0) were committed, and fifteen minutes later the verbatim copy of AI Studio’s auth, forms authoring and terminology code.

One small commit from that evening shows a new rule at work: “sections as a sticky top strip, never a side rail”. That afternoon I’d made it a rule that the PHR is always portrait, phone or iPad, even on a desktop browser, with navigation in a top strip or a sheet and never a width-based side rail. The renderer’s first version had a rail beside the form on wide screens. An agent read the new rule and took it out before anything merged.

None of it merged until the next day. Almost all of it landed on Day 10.

10. What I learned on Day 9

Next, Day 10 (9 November): 62 merges in one day. Forms, auth, Cally, terminology and the server core become packages, the PHR Console appears, and staff get a way to sign in.

The team behind Day 9

The PHR is built by a small Caladrius team working with AI agents:

Cally: taxonomy and intent (Giri)

Day 9’s plans decided where Cally’s understanding of a request lives. The shared package holds what any app needs in the same form: model providers, the guard chain, the personal-data filter and thread ownership. The tools an app exposes, its instructions and which models it allows stay in the app. For the Cally taxonomy and intent framework, which is Giri’s area, that is the important line: the categories of request Cally can act on in the PHR are the PHR’s tools, written and certified with the PHR, while the guardrails around them are shared. The forms plan added a first explicit limit on what Cally may make: forms, but only within guardrails on which kinds of form, and the person never builds forms themselves.

Development, infrastructure and go-live (Krishna)

Nothing merged for three days, and on the infrastructure side, which Krishna leads, that made the plans more important, not less. Every step was written with its class and a “done when” condition, so it could be built, reviewed and released in order. Certification stays per app because CI will work out which apps depend on a changed package and re-runs only their gates and NHA cases, and each app’s SBOM names the packages it ships. Each app keeps its own deployment and secrets. The boundary check became the first step of lint, with a self-test, and an installation’s hosting mode became something chosen once, with a migration and a checklist to change it, never a toggle.

Narrative and personas (Preetha)

The plans named, for the first time, two kinds of people. A PHR user is a person, their family, a citizen, on a phone or an iPad held upright. An application user is someone who runs the PHR, Caladrius staff today, at a desk in the console. Preetha’s area, the people the product serves and how it speaks to them, starts from that split. The forms plan wrote the person into the design: a mental-health or lung check shown one question, one section or the whole form at a time, the author’s default with the person free to switch. And the plan wrote down one sentence a patient should be able to rely on: Support does not see their health data.

Product and technology direction (Manish)

I chose to stop. Building forms, staff sign-in and an assistant as PHR-only code would have been faster that week and slower every week after. On 6 November I wrote down the parts every Caladrius product needs; on 8 November I decided that nothing goes to AI Studio until the PHR is verified, which moved forms authoring here the same afternoon. I added the copy-first rule for anything taken from AI Studio, and the rule that the PHR is always portrait. The trade-off I accepted was three days without a merge, in exchange for one principle every later plan inherits: apps share code, never runtime.

Further reading

See all 16 parts