Guide Architectural governance 10 min read

Preventing Patient Data Boundary Violations in AI-Generated Code

HIPAA's access control standard covers software programs as well as people, and in a codebase a program reaches patient records through an import. AI coding agents add those imports in working code that passes its tests. This guide shows how to write a patient data boundary as a rule that a hook and a CI check enforce, and what that rule cannot see.

What the Security Rule asks of software

The Security Rule under the Health Insurance Portability and Accountability Act (HIPAA) covers electronic patient data. Most teams read it as a list of controls for people: who can log in, who can see a chart. Its access control standard reaches further. 45 CFR 164.312(a)(1), a section of the Code of Federal Regulations (CFR), requires policies and procedures that allow access to electronic protected health information (ePHI) "only to those persons or software programs" that have been granted access rights. The rule names software programs alongside people.

In a codebase, one module gets access to patient records in an ordinary way: it imports the code that reads them. So the import graph, the map of which modules import which, carries part of the team's decision about which components may touch ePHI. Add one import line and that decision changes.

The Security Rule also requires a risk analysis. The Department of Health and Human Services (HHS) explains what that involves in its Guidance on Risk Analysis, issued by its Office for Civil Rights (OCR). It says an organization "must identify where the e-PHI is stored, received, maintained or transmitted." The amendments HHS proposed in January 2025 go further. The proposed rule, published on January 6, 2025, would require a written technology asset inventory and a network map, so that a regulated entity understands how ePHI moves through its systems. It is still a proposal. The federal regulatory agenda lists the final action for July 2027.

None of this is legal advice, and no code check makes a system compliant. The point is narrower. A risk analysis done the way that guidance describes records where ePHI goes. When a coding agent adds a new path in an afternoon, the record and the system no longer match, and nobody decided that they should differ.

How an agent crosses a patient data boundary

Take a hypothetical case. A digital health team runs a clinical service that owns the patient repository, clinical.repositories, and an analytics service that reads approved aggregates through reporting.services. The team decided that analytics never reads identifiable records directly. An engineer asks a coding agent for a patient engagement dashboard. The agent searches the codebase, finds the patient repository, and writes this:

from clinical.repositories import patient_records


def engagement_metrics():
    return patient_records.fetch_all()

The code runs. The dashboard renders. The unit tests pass, because the tests check that the metrics are right, not where the rows came from. A reviewer reading a long diff sees a short, tidy function. Nothing in the change says that analytics now holds every patient record.

The version the team approved is as short:

from reporting.services import engagement_summary


def engagement_metrics():
    return engagement_summary.fetch()

Research on AI-generated code points the same way, though none of it measures architecture directly. Veracode's Spring 2026 GenAI Code Security Update reports that code from current models passes its syntax checks more than 95% of the time, while only about 55% of generation tasks produce secure code. Veracode measures known weakness classes, such as injection, not architecture. The likely parallel is plain: models are good at code that works, and nothing in the prompt steers them toward a constraint nobody stated.

The engineer working with the assistant may not catch the gap either. In a peer-reviewed user study presented at a computer security conference in 2023, Perry and colleagues found that participants with an AI assistant wrote significantly less secure code than those without one, and were more likely to believe their code was secure. A boundary that depends on someone noticing one import line depends on attention.

Three patient data boundary violations to watch

The direct import above is the simplest case. Two more come up in the same kind of change.

A static check reads source code without running it, and it handles these cases unevenly. The restricted import and the gateway bypass leave text in a source file: an import, a client library, a host name. Leaked identifiers in logs depend on what a variable holds when the program runs, and reading the file cannot tell you that.

Write the boundary as a rule a machine can check

Many teams already record decisions like this in an architecture decision record (ADR), a short document that states one decision and why it was made. An ADR explains the boundary to a person. A coding agent or a continuous integration (CI) job, the automated build that runs on every change, does not read it. The decision needs a second form: a rule with exact semantics.

Mneme, an open-source command-line tool, is one way to give it that form. It reads ADRs written in a set format and checks code against the rules they contain. The ADR and code in this article are illustrative; the results are from real runs of the released tool, the Python package mneme-hq at version 0.11.0. Here is the analytics decision in Mneme's format:

---
id: ADR-021
title: Analytics does not import the clinical patient repository
status: accepted
priority: foundational
date: "2026-12-01"
scope: analytics.patient_data
---
Analytics reads approved aggregates through reporting.services. The patient
repository in clinical.repositories holds identifiable records, and only the
clinical service is approved to read it.

## Constraints
- FORBID_LITERAL:
    value: clinical.repositories
    include_paths:
      - "analytics/**"
- FORBID_LITERAL:
    value: from clinical import repositories
    include_paths:
      - "analytics/**"

Importing the ADR compiles it into the decision file, .mneme/project_memory.json, a JSON file in the repository that holds every decision Mneme checks. We then ran mneme check on the agent's dashboard code as analytics/engagement.py. The check failed with exit code 2 and named the decision:

FAIL  [ADR-021] FORBID_LITERAL "clinical.repositories" -- trigger: clinical.repositories
      Analytics does not import the clinical patient repository
      path: analytics/engagement.py via analytics/**

The approved version, which imports reporting.services, passed. The same direct import placed under clinical/ also passed, because the rule applies only under analytics/. The clinical service is allowed to read its own repository.

What an exact-text rule catches and misses

A rule that matches exact text is predictable. It gives the same answer for the same file every time, and you can read why it fired. It also sees only the text you wrote down. We ran five variants of the dashboard against ADR-021:

Variant in analytics/engagement.pyResult
from clinical.repositories import patient_recordsFAIL
import clinical.repositories.patient_records as prFAIL
from clinical import repositoriesFAIL (second rule)
A module name built from strings at runtime with importlibPASS
An import of a reporting module that itself imports the patient repository and passes it on (a re-export)PASS

Plan for the dynamic import and the re-export. A dynamic import never contains the forbidden text. A re-export hides the patient repository behind an approved module, and a check that reads one file at a time cannot follow it. Following it takes a tool that builds the import graph and asks whether analytics can reach the patient repository through any chain of imports, which is called transitive reachability. For Python, the Import Linter forbidden contract stops one set of modules importing another and, per its documentation, "Indirect imports will also be checked." Java teams use architecture test libraries such as ArchUnit for the same job; architecture as code compares the options.

Leaked identifiers in logs need different controls again: a logging layer that redacts known fields, and tests that assert identifiers never reach a log line. A forbidden-text rule can block a specific pattern, such as printing a whole patient object, but it cannot decide whether a variable holds an identifier.

So split the work three ways. The import graph tool catches chains of imports. Runtime redaction and tests cover what the data holds. The Mneme rule covers the named crossings an agent is most likely to make in one edit, and each failure names the decision behind it.

Check at two points: while the agent works and before the merge

A CI check finds the import after the agent wrote it, and someone has to send the work back. A check inside the agent finds it while the agent is still working.

While the agent works. Claude Code is Anthropic's coding agent, and it changes files through tools named Edit, Write and MultiEdit. It also runs hooks: commands it calls at fixed points. The Mneme plugin's hook checks each Edit, Write and MultiEdit call before the file changes. We sent it a Write event carrying the direct import for analytics/engagement.py. It exited with code 2, which Claude Code treats as a block, and printed the same ADR-021 failure, so the agent sees which decision it broke and why.

Shell commands that write files are different: the hook cannot check most of them in advance. It checks those files when the agent finishes its turn and hands control back, and sends the agent back to fix a violation. The hook does not undo the write, so the file exists until the agent fixes it.

The hook also lets a change through if the check crashes or times out. Stop an AI coding agent from crossing an architectural boundary covers the setup and these limits.

Before the merge. GitHub Actions is GitHub's built-in CI service. A required status check is a CI job that a branch rule says must pass before a pull request can merge. Run as one, the same Mneme rule checks every file a pull request adds or modifies, whichever agent or person wrote it. Fail a pull request that breaks an ADR has the workflow.

The workflow keeps one JSON verdict per file as a build artifact. A failing file's verdict names the decision, its rule, the matched text, the path selector that applied and an identifier for the version of the decision that applied. A passing file's verdict lists no violations, but it still names each decision and rule that was checked against the file.

The check is only as strong as the files that define it. An agent that can edit the ADR, the decision file or the CI workflow can weaken the check it is judged by. List those paths in GitHub's CODEOWNERS file, which names the people who must approve changes to them, turn on required review from code owners, and keep exceptions human-owned.

Where Mneme fits, and where it stops

Mneme compiles a team's ADRs into the decision file and enforces the decisions that carry a typed rule. Every Mneme step shown above runs on the released version, mneme-hq 0.11.0: the exact-text rule scoped to paths, the Claude Code hook, the CI check and the JSON verdict. A decision without a typed rule is guidance only; it blocks nothing. The Architecture Protection Audit shows which of a repository's recorded decisions a typed rule protects today and which are guidance only.

Mneme does not make a system HIPAA compliant, and it does not isolate data at runtime. It does not trace data flows, inspect database permissions or know what a variable holds. It checks source text against the boundaries a team wrote down.

The Security Rule's audit controls standard, 164.312(b), asks for mechanisms that record and examine activity in systems that contain or use ePHI. The JSON verdicts record what each pull request was checked against. Whether they help meet that standard is for your compliance team to decide. The decision file does not record who approved a decision.

What Mneme adds is narrow and useful in a regulated codebase: an approved data boundary stops being a sentence in a document and becomes a check that runs on every agent edit and every pull request, and names the decision when it fails. The wider case for decision provenance in regulated software is in AI governance for regulated industries; the validation-history argument for teams regulated by the Food and Drug Administration (FDA) is in AI coding agents in life sciences.

What to do before agents touch patient data code

  1. List the boundaries. From your risk analysis and data flow documentation, write down which modules may read identifiable records, which may only read aggregates, and which outbound services may receive patient data.
  2. Give each boundary an owner and an ADR. One decision per boundary, with its own scope, its rationale and the paths it covers.
  3. Encode the import forms. Add exact-text rules for every way your language spells the forbidden import, and test them against variants, as in the table above.
  4. Add an import graph check. Catch chains of imports with a contract tool such as Import Linter, and keep runtime redaction and log tests for what no static check can see.
  5. Check while the agent works and before the merge. Turn on the agent hook where your tools support it, and make the CI check required.
  6. Protect the governance files. Require code owner review for ADRs, the decision file and the workflow, and keep the JSON verdicts with your build records.

Frequently asked questions

What is a patient data boundary violation in code?+
A change that gives a component access to patient records the team never approved it to have, such as an analytics module importing the clinical patient repository directly, or a debug line writing identifiers to a widely readable log. The code can work and pass its tests while crossing the boundary.
Does the HIPAA Security Rule apply to software components?+
Its access control standard, 45 CFR 164.312(a)(1), refers to access by persons or software programs that have been granted access rights. How that applies to a given system is a question for your compliance and legal teams; this article covers the engineering controls.
Can a static check stop every patient data leak?+
No. An exact-text rule catches the import forms you list, but not dynamic imports or indirect paths through an approved module, and it cannot tell what a variable holds at runtime. Pair it with an import graph tool, runtime redaction and tests.
Does Mneme make a healthcare application HIPAA compliant?+
No. Mneme checks source files against the architecture decisions a team has written as typed rules, when Claude Code edits a file and in CI. It does not isolate data at runtime, trace data flows or determine compliance.