Skip to content

Read CDA documents and stage clinical records with their source document time - #206

Draft
PSchmiedmayer wants to merge 11 commits into
healthkit-audiogramsfrom
healthkit-clinical-documents
Draft

PSchmiedmayer wants to merge 11 commits into
healthkit-audiogramsfrom
healthkit-clinical-documents

Conversation

@PSchmiedmayer

Copy link
Copy Markdown
Member

♻️ Current situation & Problem

This replaces #196, which closed when its branch was merged into the integration branch; the branch and commits are unchanged.
GroveHealthKitFHIR exports a clinical record or CDA document sample inline, but GroveHealthKit offered no way to query CDA documents or read their bytes.
HealthKit returns a CDA document's bytes only from an HKDocumentQuery that asks for them, and reading them needs its own authorization.
A clinical record's resource or a CDA document could also only be embedded in the record, while Grove already stages workout routes and heartbeat series in files that the record references.
The sample dates of both classes do not state the time of care.
A clinical record's dates state when HealthKit downloaded it, and a CDA document's dates are whatever the creating app supplied.
Grove dropped them, and a sample's time zone stayed in the source metadata without a time it belonged to.
SchmiedmayerLab/grove-fhir#88 adds a source document time extension on the conversion Provenance for these dates.

⚙️ Release Notes

  • HealthKit.queryCDADocuments(timeRange:anchor:source:limit:predicate:) runs an anchored query of CDA document samples without their contents.
  • HealthKit.readCDADocument(_:) reads one document with its bytes and stops its query when the task is cancelled.
  • readCDADocument(_:) throws HealthKit.CDADocumentError.unavailable when HealthKit returns no document, another document, or one without bytes.
  • queryCDADocuments throws HealthKit.CDADocumentError.unexpectedSample when HealthKit returns a sample that is not a CDA document.
  • HealthKitRecordingFile.clinicalRecord(_:relativePath:maximumByteCount:) stages a clinical record's FHIR resource byte for byte under its release's media type.
  • HealthKitRecordingFile.cdaDocument(_:relativePath:maximumByteCount:) stages a CDA document's bytes unchanged under the title the document states, else the default title.
  • Both admit only what the inline export admits, and export through the existing .fileRecording(_:) record, whose DocumentReference references the file.
  • RecordingFile.stage(data:maximumByteCount:) stages bytes already in memory.
  • The conversion Provenance of a clinical record or CDA document states the sample's start and end as the healthkit-source-document-time extension.
  • The extension's basis is clinical-record-download or cda-document-app-supplied, and its period names the sample's time zone on both endpoints when the sample states one.
  • No other element of the record states these dates.
  • A document whose sample dates form no valid period, or whose time zone is unreadable, is refused.
  • Since the Provenance carries the time zone of these samples, their source metadata extension no longer repeats it.
  • The CDA query and read and the two HealthKitRecordingFile factories are not available on watchOS.
  • GroveFHIRContract adds the generated HealthKitSourceDocumentTime with the extension URL and its two bases.
  • The generated contract follows grove-fhir 7a388c126e84554a91cd4c794a2e9ad0d63ddb84, the head of Carry HealthKit clinical document dates on Provenance grove-fhir#88, which Scripts/validate-fhir-conformance.sh and the test workflow pin by commit.
var anchor = QueryAnchor()
let (added, _) = try await healthKit.queryCDADocuments(timeRange: .ever, anchor: &anchor)
for sample in added {
    let document = try await healthKit.readCDADocument(sample)
    let recording = try await HealthKitRecordingFile.cdaDocument(
        document,
        relativePath: "documents/\(document.uuid).xml",
        maximumByteCount: documentByteLimit
    )
    try await exporter.export(records: [.fileRecording(recording)], at: conversionInstant) { result in
        // Store the record and recording.file together before persisting the anchor.
    }
}

📚 Documentation

  • The GroveHealthKit HealthKit topics list the two CDA document operations.
  • The GroveHealthKitFHIR DocC catalog describes the clinical record and CDA document file recordings, the CDA read, and the source document time on the conversion Provenance.
  • The new public API carries DocC comments.

✅ Testing

  • CDADocumentReadTests is new and covers delivery of the requested document with its bytes, a missing or foreign document, a query error, cancellation before the read starts, between registering and running its query, and while it runs, and the anchored query's descriptor.
  • HealthKitClinicalRecordingFileTests is new and covers staging both classes unchanged, refusal of what the inline export refuses, and the default title of a CDA document without one.
  • HealthKitSourceDocumentTimeTests is new and covers the basis and period of each class with and without a time zone, an unreadable time zone, and samples of other classes.
  • HealthKitDisclosureOptionsTests adds clinical record and CDA document file recordings to the disclosure presets.
  • RecordingFileStreamingTests adds a case for staging bytes from memory within and beyond the byte limit.
  • The clinical document goldens, including the R4 and DSTU2 clinical record exports, and the content corpus state the source document time.
  • Tests that run in parallel create validated CDA documents one at a time, since HealthKit's schema validation is not safe to run concurrently.
  • test_generate_grove_fhir_swift_contract.py covers the generated source document time bases and refuses a source document time the generator does not emit.

Code of Conduct & Contributing Guidelines

By creating and submitting this pull request, you agree to follow our Code of Conduct and Contributing Guidelines:

GroveHealthKit queries CDA document samples with
HealthKit.queryCDADocuments, an anchored query without the document
contents, and reads one document with its bytes with
HealthKit.readCDADocument, which stops its query when the task is
cancelled.

HealthKitRecordingFile stages a clinical record's FHIR resource and a CDA
document's bytes unchanged. The conversion Provenance of either states
the sample dates as the source document time, with the time zone the
sample names, since no element of the document states them. The
generator emits the source document time extension of the pinned guide.
HealthKit validates a new CDA document with libxml2, whose schema setup crashes when it runs concurrently, so the tests that run in parallel create their validated documents under one lock.
Pins grove-fhir to the clinical source time guide branch at 59b4ece, which
contains grove-fhir 0.9.0, and regenerates the contracts from it.

Clinical documents keep this branch's payload staging beside the clinical
record attachments: the attachment document sits with the clinical payload
functions, and the documentation names both the file recordings and the
attachments. The clinical goldens and content corpus vectors are
regenerated: the clinical record attachment golden now also states the
source document time on its conversion Provenance instead of the time zone
in its metadata. The clinical recording file test uses the renamed
provider-issued-fhir-json and cda-document formats.
A clinical record staged as a file recording carries only its resource,
so its documentation directs a record with files to the clinical record
export with attachments, and the article states the sample export for a
record without files.
A file recording carries only the clinical record's resource, so staging
a record that HealthKit stores files with would drop them. Staging now
takes the health store, lists the record's attachments, and refuses a
record with any as RecordingError.clinicalRecordHasAttachments; a failure
to list them throws.
@coderabbitai

coderabbitai Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@PSchmiedmayer
PSchmiedmayer added this pull request to stack #208 December 4, 2026 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant