Repository navigation
Prepare SensorKit usage reports, snapshots and file-backed evidence - #163
Open
PSchmiedmayer wants to merge 10 commits into
Open
PSchmiedmayer wants to merge 10 commits into
PSchmiedmayer wants to merge 10 commits into
Conversation
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
Messages-usage and phone-usage reports gain safe representations that copy every public field, and SensorKitPreparedStructuredRecord prepares them like the other structured samples. SensorKitBatchSnapshot retains the application, host and source-device facts, time zone and conversion instant of a batch's first conversion in a bounded, versioned encoding, so a retry states the same facts. SensorKitPreparedStructuredFile writes the native evidence of a device-usage report or an ECG session directly into a recording file, and SensorKitOutputDefinition describes the outputs a cataloged converter implements. An ECG session's retry evidence now ends with the start, frequency, lead and guidance its record states, so one digest covers every fact. RegisteredRecordingFormat validates a payload or file within a caller's byte limit.
`SensorKitBatchSnapshot` now throws only `SnapshotError`, and decoding maps malformed bytes and invalid application or host facts to `invalidFacts`. Both registered payload validators accept a caller limit between 1 and `Int32.max`, the usage report representations document their fields, and the structured file's `sensorKitRecord` states that the generated JSON is not parsed again, which the validation override now guarantees.
Keep the shared caller-limit check of the registered payload validators out of the instance methods, as the type order rule requires.
`SensorKitBatchSnapshot(encoded:maximumByteCount:)` decodes the version first, so another version reports `unsupportedVersion` even when its shape differs, and `encoded(maximumByteCount:)` throws only `SnapshotError`.
`SensorKitBatchSnapshot.init` throws `capacityExceeded` for more than 128 source devices, as decoding does, instead of `invalidFacts`. The device usage file test adds category names whose order depends on case and digit handling, which pins the member order of the file to that of the in-memory encoding.
The file and Data validation tests of a registered payload now expect the reader error that invalidTabularPayload carries: the invalid timestamp and the invalid value of their rows.
PSchmiedmayer
force-pushed
the
sensorkit-preparation
branch
from
November 28, 2026 00:17
099c850 to
967d2a5
Compare
This was referenced Nov 28, 2026
PSchmiedmayer
marked this pull request as ready for review
December 6, 2026 06:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
♻️ Current situation & Problem
This PR builds on #162.
A study client such as Trove converts SensorKit batches into FHIR and must convert the same batch again after a crash or an interrupted upload, with the same resource identities and content. GroveSensorKitFHIR lacked several pieces for that. Messages-usage and phone-usage reports had only the generic safe representation, which wraps the non-Sendable SensorKit object, and no prepared record with retry evidence. A retry had no way to state the same devices, time zone and conversion instant as the first conversion. Structured native evidence (the JSON of an ECG session or a device-usage report) existed only as one
Datavalue, so a large one sat in memory twice. ECG retry evidence did not cover the start, sampling frequency, lead and guidance that the record states. A client had no list of the streams the converter actually produces, and a receiver had no public way to validate an uploaded recording against its registered format within a limit of its own.Trove persists a
SensorKitBatchSnapshotin its batch receipt, writes ECG sessions and device-usage reports throughSensorKitPreparedStructuredFile, derives its collection capabilities fromimplementedOutput, and validates uploads on its server withvalidatePayload(_:maximumByteCount:).⚙️ Release Notes
Usage reports:
SRMessagesUsageReport.SafeRepresentationandSRPhoneUsageReport.SafeRepresentationare dedicated structs that copy every public field on the callback thread.SensorKitPreparedStructuredRecord(messagesUsage:)and(phoneUsage:)prepare them with retry evidence over every field.Breaking: those two
SafeRepresentationtypes replace theDefaultSensorKitSampleSafeRepresentationtypealias, so the wrappedsampleproperty is gone.Batch snapshot:
SensorKitBatchSnapshotretains the application, host, recording and source devices, source time zone and conversion instant of a batch's first conversion. The caller persists it before the first conversion and converts every redelivery with the decoded snapshot. The encoding is versioned JSON with sorted keys, bounded by a caller byte limit, at most 128 source devices and 2,048 UTF-8 bytes per string; every failure is aSnapshotError(unsupportedVersion,invalidFacts,capacityExceeded).File-backed evidence:
SensorKitPreparedStructuredFile(electrocardiogram:maximumByteCount:)and(deviceUsage:maximumByteCount:)write the native JSON into aRecordingFileone element at a time. The bytes equal the in-memory preparation, so digests match, and a failure discards the file.readRetryEvidenceChunks(_:)streams the retry evidence, andsensorKitRecord(sourceRecordID:title:sidecarPath:admission:)attaches the file at an archive-relative path.Behavior change: an ECG session's
retryEvidencenow ends with its start, sampling frequency, lead and guidance as fixed-width big-endian words, so one digest covers every fact the record states. ECG digests stored before this change no longer match.SensorKitCatalogEntry.implementedOutputreturns aSensorKitOutputDefinition(source token, Observation contract and profiles, effective and value shape, recording profiles and formats, required companion profiles) for each stream the converter produces, andnilotherwise. It describes output and grants no permission to collect.RegisteredRecordingFormat.validatePayload(_:maximumByteCount:)validatesDataor aRecordingFileagainst its registered format within a caller limit between 1 andInt32.max; the registry's decode limit still applies, and CSV files are validated in chunks.The preparations take the shared
RecordingPayloadAdmissionfrom Stage recordings in owned files and read them in bounded chunks #159. Package-internalRecordingFile.stage(maximumByteCount:producing:)builds a file from a synchronous producer.📚 Documentation
GroveSensorKitFHIR.md describes the usage reports,
SensorKitPreparedStructuredFile, the extended ECG retry evidence and persisting aSensorKitBatchSnapshot, and lists the new types in its topics. The new types,SnapshotErrorand bothvalidatePayloadoverloads have symbol documentation.✅ Testing
SensorKitUsagePreparationTests(new): usage reports keep every field, and the phone call duration enters the record and the retry evidence.SensorKitBatchSnapshotTests(new): device facts and a fractional instant round-trip; byte limits, other versions, unknown time zones, invalid facts, malformed bytes and too many source devices are refused with the right error.SensorKitECGRetryEvidenceTests(new): each of the four ECG facts changes the evidence, and the file preparation matches the in-memory bytes and evidence.SensorKitStructuredFileTests(new): the device-usage file keeps every nested field and the in-memory member order.SensorKitOutputDefinitionTests(new): deferred entries have no output, and the on-wrist definition matches the emitted Observations.RegisteredRecordingFileValidationTests(new): the caller limit is inclusive and checked first, the decode limit still holds, and file and in-memory payloads follow the same grammar.RecordingFileProducerTests(new, GroveFHIRTests):RecordingFile.stagekeeps every byte across large appends, refuses an empty producer and discards the file on failure.Code of Conduct & Contributing Guidelines
By creating and submitting this pull request, you agree to follow our Code of Conduct and Contributing Guidelines: