# Card #3416 narrow dispatcher locking review

VERIFIED: independent source/evidence review accepts the narrow code-only correction for guarded deployment. No blocking or required-correction findings. This is release-review acceptance, not a claim that production deployment or campaign delivery has occurred. Root retains final release, card and test acceptance authority.

Reviewed candidate: existing feature/automatic-replies-3416 workspace, baseline f289bc6f7530a0faff399c0f12f1a87126c9fba2. Root-provided exact diff in automatic-replies-3416-locking-diff.txt contains only the scoped Stat connection override and two priority count replacements; reviewed actual candidate matches it. No code/tests were edited by reviewer.

## Findings and physical behavior

- Blocking/high/medium findings: none.
- SendoutDispatcher.php:135-142 still acquires the company retention transaction and freshly locks the available, unreserved, incomplete company-owned job. CompanyRetention.php:88-100 holds Company/mysql FOR UPDATE through callback/commit and rejects missing/deleted companies. No lock release, deletion-barrier bypass or retry-policy change was introduced.
- SendoutDispatcher.php:245-249 constructs Stat with the company first, then selects the job connection. Stat.php:66-86 selects the fully qualified statistics-database/company table before the connection override. This preserves the statistics table while making the FK insert use the physical job transaction rather than a second session waiting on its own locked parent. Company/mysql, MailQueue/mysql and default/mysql job use align in reviewed source/config. Source config uses the same DB_HOST/DB_USERNAME/DB_PASSWORD for both schemas. Effective production configuration remains a root preflight, not independently read here.
- Duplicate check and save (dispatcher:293-313), queue creation/data save (339-362), job reservation/data/completion now participate in the same transaction under the normal mysql default. Exception rollback therefore restores the pending job and removes transactional stat/queue writes together. Company lock serializes competing dispatcher work for the company; existing job revalidation and stat duplicate check remain. This supports safe scheduler retry of the existing pending campaign without resubmission; it does not assert universal exactly-once SMTP delivery.
- Both priority paths (dispatcher:323,332) use the same stat connection and see its uncommitted inserts. Sendout.php:306-311 confirms old stats() used exactly company Stat + sendout_id, without extra count predicates. New queries preserve those predicates; fixture proves priorities [1,2] and duplicate receiver [1,2,1] creates two stats/queues.
- NewsletterScanner.php:20-34,253-307 consumes attributes plus receiver/sendout relations. Those related models explicitly use mysql; framework HasRelationships.php:1033-1039 preserves explicit related connections. Model.php:1598-1611,1711-1714 uses this model connection for newQuery, and newInstance:696-712 retains table/connection. No scanner-required relation or table propagation regression found.

## Verification evidence and limits

Reviewer performed native PowerShell fresh reads/searches, measured relevant files, inspected framework source and exact diff, and recomputed hashes. No app commands, DB probes, Git/network, SSH, credentials or production operations were executed by reviewer.

Read raw logs: automatic-replies-3416-locking-tests.txt reports PHP8.4.24, 99 tests/771 assertions PASS; automatic-replies-3416-locking-php83-tests.txt reports PHP8.3.31, 123 tests/1001 assertions PASS. Both have the existing PHPUnit XML deprecation only. Root reports lint/diff-check PASS and real opt-in MariaDB fixture completion/cleanup; log summary does not expose command/filter or per-test listing, so selection details come from root's stated evidence rather than independent execution.

Integration/SendoutDispatcherStatisticsMariaDbTest.php:17-129 creates disposable local main/statistics schemas with actual cross-schema InnoDB FKs, reproduces old 1205, injects a post-stat rendering failure and checks no stat/queue/reservation/attempt remains, retries the same job, verifies commit/priority/duplicate suppression, checks another session cannot observe uncommitted stats or mark company during dispatch, then marks after commit and proves deleted-company callback blocked. Schema cleanup names are constrained and absence read back. Root reports 18 assertions; source is consistent with that proof.

Coverage limits (nonblocking for this narrow change): MariaDB test invokes the real private worker under the real retention transaction but substitutes newsletter rendering/scanner, uses normal newsletter type1, and faults before queue creation. It does not exercise full handle job discovery, GDPR rendering or a second failure after queue insertion. Unchanged handle source, equivalent two priority expressions, explicit related-model connections and queue/mysql source cover the narrow questions. Existing AutomaticReplySafetyTest.php:109-126 exercises actual handle cooldown skip/replay behavior. SMTP/end-to-end receipt remains a separate production acceptance gate owned by root. Do not present fixture evidence as delivered email proof.

Reviewed SHA256:
- app/Console/Commands/SendoutDispatcher.php: 17b1823596914cfdb5489b050f6a56954c60e53ac9d48b3f68ac1b39b4fb7a1b
- tests/Integration/SendoutDispatcherStatisticsMariaDbTest.php: a631fbe0c1ae1e8d237808d3cf8ac0da9f2c0e016e08b2f2af93ab81127689ef

## Guarded release acceptance

Accept code-only deployment after root freshly proves intended host, production HEAD ee4d973 and tracked-clean state, accepted candidate refs/hashes and merged GitHub master ancestry. Verify the transferred SFTP bundle and use only the approved fast-forward deployment path; preserve untracked production assets. This review calls for no migration, environment/system setting, cache configuration or broad connection change. Verify deployed file hash from fresh state plus relevant schema/default mysql connection/privileges, existing job9103/campaign4969 and normal scheduler progress. Preserve once-only test ledger; do not resubmit, create a duplicate campaign or manually invoke dispatcher/intake/forwarder. Complete native Thunderbird receipt and remaining authorized correlated-reply/forward checks before claiming the controlled E2E is complete. Stop/report unexpected host/ref/hash/schema/job state rather than expanding this release.

## Deferred organization recommendation

Relevant app/Models/Company.php measures 1330 lines/40197 bytes. Its retention entry points and database ownership are interleaved with statistics summaries, billing renewal/package actions, queries and relationships; targeted locking review requires navigating unrelated business methods. Deferred outside correction scope: first extract app/Services/Company/CompanyStatistics.php (summary/month/event queries, preserve existing public wrappers and per-company table selection); then app/Services/Company/CompanyBilling.php (package/expiry/suspend/payment behavior, explicit Company argument). Keep lifecycle gateway, mysql ownership and relationships in Company. Migrate one wrapper group at a time, preserving return contracts; run existing statistics/billing regressions and company retention/access suites after each group. No refactor performed; root should consolidate with already recorded deferred proposals.

Ownership released: only this report was written; fresh UTF8 readback and hash recheck completed. No reviewer blocker, successor need or further worker. Root owns release and test continuation.

Remaining uncommitted features known from assignment:
1. Narrow dispatcher transaction/priority correction and disposable MariaDB integration test; task evidence/report accompany them. Repository-wide dirty inventory remains root-owned.
## Independent post-deploy acceptance - 2026-10-09

VERIFIED: narrow correction release/source and existing normal campaign-job progress accepted. Complete automatic-reply/forward E2E remains unverified. Root's saved native Inbox receipt is consistent with independently observed production delivery reference; reviewer did not operate Thunderbird or independently inspect its Inbox.

Fresh read of locking-release.json records PR1458 merged at10:01:21Z, candidate dc864f3134fa481589c833b14cadb0864229a413, merged/deployed03c2eebd7bc8bef7daeb9387e181c60374415be1, guarded activation10:04:25Z from expected ee4d973, verified bundle SHA2560b58a6ca5f17de1b2b2e90473837cdfd62fb9adfc4f43175e9a6beea0a4d1b4d and fresh remote verification10:07:26Z. Host/source/clean-state, default/mysql/same-host/schema identity, qualified statistics access and preserved untracked data guards are recorded satisfied. No migration, global/environment/system change, manual dispatcher or resubmission recorded. Reviewed app/test SHA256s recomputed unchanged. Remote deployment/source evidence is root-provided ledger, not a reviewer SSH check.

Reviewer independently called native read-only Nuhet db.query with exact bounded SELECTs (limit5):
- sendout_jobs id9103/company1/sendout4969 returns one row: completed_at2026-10-09 12:05:03, reserved_at same, attempts1. Completion with retained reserved timestamp matches unchanged complete() behavior.
- nuhet_stats.1_stats company1/sendout4969 returns exactly one row: stat9008, receiver1894941, status0, send_time2026-10-09 12:05:16.
- outgoing_delivery_references company1/sendout4969 returns exactly one row: reference1/stat9008/profile138; Message-ID109a916b20d3da835f2fd0100b2bf0088f1f5a89b16f348f@post.nuhet.se matches saved original native Inbox receipt. An initial reference SELECT incorrectly requested receiver_id and returned generic MCP request failed. Migration source confirms stat_id instead; one evidence-backed corrected SELECT passed. No repeated failing probe or mutation.

Fresh strictUTF8 parsing of both ledgers passed; original_receipt.body contains expected Nordic sequence and test marker3416-20261009-0629. Saved receipt has exact expected subject/recipient, count1, nontruncated body and matching random Message-ID. Campaign claim remains submitted once with one send attempt; return send attempts0 and e2e_verified=false. Historical blocked-state fields inside the E2E ledger remain historical; current release state/original_receipt/correlation supersede them. Do not summarize stale root_final_acceptance or delivery_blocker.fresh_end_state as current zero-delivery state.

Remaining gate: finish one authorized native correlated return draft/user send and prove retained correlated automatic notice plus exactly one actual forwarded receipt. No new release correction required. Root retains final acceptance and owns all production/card/test decisions. Reviewer wrote only this appendix, freshly read it back and releases report ownership again.