refactor(audit): split enqueue helpers out of auditContextWrappers#235
Merged
therealbrad merged 1 commit intomainfrom Apr 23, 2026
Merged
Conversation
Workers crashed at startup after c804cac because auditContextWrappers.ts pulled in next/headers, which isn't present in the workers Docker image. The hotfix used a dynamic import as a band-aid; this split addresses the underlying coupling. - New lib/auditContextEnqueue.ts: runtime-agnostic BullMQ helpers (enqueueWithAuditContext, ActorContextJobData, EnqueueSystemOptions), zero next/* imports, safe to load in any Node context. - auditContextWrappers.ts: now Next.js-only (withAuditContext, withActionAuditContext, enrichFromApiAuth) with next/headers back at the top level — the dynamic-import workaround is no longer needed. - Worker files, SyncService, scripts, and mixed-import app routes now import enqueue helpers from the new module. - Enqueue tests moved to auditContextEnqueue.test.ts (no next/headers mock) so test suite proves the module loads cleanly in a Next-free environment. Verified: grep -rn "next/" workers/*.ts returns zero hits for non-test worker files. The workers image can now load auditContextEnqueue.ts without any transitive Next.js requirement. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced Apr 22, 2026
Contributor
Author
|
🎉 This PR is included in version 0.22.8 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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.
Description
Splits
lib/auditContextWrappers.tsalong concern boundaries so BullMQ workers no longer transitively depend on Next.js.Background. On 2026-04-22 the multitenant-workers deployment crashlooped at startup with
Error: Cannot find module 'next/headers'. Root cause:workers/testmoImportWorker.tsimportedenqueueWithAuditContextfromlib/auditContextWrappers.ts, which had a module-topimport { headers } from "next/headers". The workers Docker image intentionally strips Next.js fromnode_modulesto save ~900MB (seeDockerfiledeps-workersstage), so the require failed.Hotfix c804cac lazy-loaded
next/headersinsidewithActionAuditContext. This PR replaces that band-aid with a real split.Changes
lib/auditContextEnqueue.ts— runtime-agnostic BullMQ helpers:enqueueWithAuditContext(function)ActorContextJobData(type)EnqueueSystemOptions(interface)next/*imports. Safe to load in any Node.js context.lib/auditContextWrappers.ts— now Next.js-only:withAuditContext,withActionAuditContext,enrichFromApiAuthawait import("next/headers")back to a normal top-levelimport { headers } from "next/headers"../auditContextEnqueue.workers/testmoImportWorker.ts(real imports)workers/copyMoveWorker.ts,workers/syncWorker.ts,workers/forecastWorker.ts(type-only imports)workers/testmoImportWorker.test.ts(both dynamicawait import(...)call sites)lib/integrations/services/SyncService.ts— pulled in bysyncWorker.ts; keeping the wrapper import here would have re-introduced the coupling.scripts/test-budget-alert.ts,scripts/trigger-forecast-recalc.ts,scripts/trigger-milestone-notifications.ts— run as tsx outside Next.app/api/repository/copy-move/route.ts,app/api/admin/elasticsearch/reindex/route.ts,app/api/imports/testmo/jobs/route.ts,app/api/imports/testmo/jobs/[jobId]/import/route.ts— these hadenqueueWithAuditContextalongside a Next HOF in one import block; split into two blocks so the wrapper file no longer has to re-export runtime-agnostic symbols.lib/auditContextEnqueue.test.ts(new) — enqueue tests, nonext/headersmock. Passing this file without any Next.js mock is the implicit proof that the module loads in a Next-free environment.lib/auditContextWrappers.test.ts— keeps thewithAuditContext/withActionAuditContext/enrichFromApiAuthtests and thenext/headersmock.Verification
Related Issue
Follow-up to hotfix c804cac (workers crashloop 2026-04-22 16:30 UTC).
Type of Change
How Has This Been Tested?
Ran locally:
Test Configuration:
pnpm(repo-pinned)Checklist
Additional Notes
This is PR 1 of 3 follow-ups from the 2026-04-22 workers crashloop post-mortem:
kubectl rollout undoactually works when a bad image does ship.