feat(medications,measurements): derive write provenance from transport and surface it

Root-cause the intake source attribution and give the HealthKit batch a
diagnostic sync-trigger, one attribution surface for both.

AuthContext now carries a read-only authMethod ("cookie" | "bearer"),
derived from the same branch that sets the wide event's auth_method. The
medication-intake route maps it to the stored IntakeSource: a Bearer
(native) call records API, a cookie (browser) call records WEB. The other
producer values are untouched — a converge onto a pending reminder row
updates through applyCanonicalSlotWrite, which never rewrites source, so
REMINDER / IMPORT / APPLE_HEALTH provenance is preserved. The value is
never client-asserted, the same posture as userId being narrowed from auth.

The dose-history ledger now selects and returns source per intake row so a
client can label how each dose was recorded. The endpoint gains its first
OpenAPI entry (path plus response schema) so the field lands in the
iOS-consumed contract.

The measurements batch accepts an optional per-request syncTrigger
(foreground | background | push). It is diagnostic-only: recorded on the
measurement.batch.ingest wide event and nothing else — never persisted,
never part of the dedup key, never an input to attribution or how a sample
is stored. Sending it or omitting it produces byte-identical row outcomes.

Regenerated docs/api/openapi.yaml.
This commit is contained in:
Marc-André Bombeck committed 2026-07-23 21:15:17 +02:00
1 parent d321391024
commit 27b548ab13
18 files changed
+426 -9

No files matched your search

+14 -4
View File
@@ -294,6 +294,15 @@ export function apiHandler<T extends (...args: any[]) => Promise<Response>>(
export type AuthContext = {
session: { id: string; expiresAt: Date };
user: User;
/**
* Transport the caller authenticated with, derived from the SAME branch that
* sets the wide event's `auth_method`: the cookie-session path resolves
* `"cookie"`, the Bearer-token path resolves `"bearer"`. Read-only and never
* client-asserted — it is the transport fact, not a request field. A route
* that must record write provenance (e.g. the medication-intake `source`)
* maps it server-side, the same posture as `userId` being narrowed from auth.
*/
readonly authMethod: "cookie" | "bearer";
};
/**
@@ -326,7 +335,7 @@ export async function requireAuth(
auth_method: "session",
});
}
return sessionData;
return { ...sessionData, authMethod: "cookie" };
}
// 2. Bearer-token path.
@@ -453,6 +462,7 @@ async function authenticateBearer(
return {
session: { id: tokenId, expiresAt },
user,
authMethod: "bearer",
};
}
@@ -477,7 +487,7 @@ export async function requireAdmin(): Promise<AuthContext> {
if (sessionData.user.role !== "ADMIN") {
throw new HttpError(403, "Admin access required");
}
return sessionData;
return { ...sessionData, authMethod: "cookie" };
}
/**
@@ -502,7 +512,7 @@ export async function requireCookieAuth(): Promise<AuthContext> {
auth_method: "session",
});
}
return sessionData;
return { ...sessionData, authMethod: "cookie" };
}
/**
@@ -585,7 +595,7 @@ export async function requireFreshMfa(
throw new StepUpRequiredError();
}
return { ...sessionData, mfaVerifiedAt: verifiedAt };
return { ...sessionData, authMethod: "cookie", mfaVerifiedAt: verifiedAt };
}
/**