fix(logging): keep detached work from renaming the request's wide event

Work a request starts and does not wait for runs inside the request's
context, so an annotate({ action }) from it landed on the request's event
whenever it ran before the event was emitted. The measurement tail's
status re-warm logged a hand-entered reading as
insights.status.invalidate.skipped on some requests and as
measurement.create on others.

runDetached starts such work so that its action is dropped for the event
it was detached from; meta, warnings and the auth it reads stay as they
were, and an event the work opens for itself keeps its own action.
fireAndForget now takes a function and starts it detached, and every
trackBackgroundTask handle on a request path is built with runDetached,
which a structural guard holds.
This commit is contained in:
Marc-André Bombeck committed 2026-10-23 16:32:10 +02:00
1 parent cfa5fe537b
commit b4166cdc84
25 files changed
+386 -104

No files matched your search

+5 -4
View File
@@ -74,10 +74,11 @@ export const POST = apiHandler(async (request: NextRequest) => {
// is rebuilt from a backup. Fire-and-forget; audit failures must
// never bubble into a user-facing 500.
fireAndForget(
auditLog("consent.ai.grant", {
userId: user.id,
details: { kind, receiptId: receipt.id },
}),
() =>
auditLog("consent.ai.grant", {
userId: user.id,
details: { kind, receiptId: receipt.id },
}),
{ action: "consent.ai.audit" },
);
+1 -1
View File
@@ -402,7 +402,7 @@ async function commitOcrRows(request: NextRequest) {
// A lab panel just landed — resolve any "annual blood panel" reminders now
// rather than waiting on the cron. Fire-and-forget.
if (inserted.length > 0) {
fireAndForget(enqueueReminderSatisfy(user.id), {
fireAndForget(() => enqueueReminderSatisfy(user.id), {
action: "reminder.satisfy.enqueue",
});
+1 -1
View File
@@ -373,7 +373,7 @@ async function postLabResult(request: NextRequest) {
// v1.18.1 — eventful Lab↔Vorsorge satisfaction. A lab panel just
// landed; resolve the user's free-text "annual blood panel" reminders
// now rather than waiting on the 15-min cron. Fire-and-forget.
fireAndForget(enqueueReminderSatisfy(user.id), {
fireAndForget(() => enqueueReminderSatisfy(user.id), {
action: "reminder.satisfy.enqueue",
});