Skip to content

fix(awareness-service): dedupe deliveries by content hash so envelope updates re-deliver#989

Merged
coodos merged 2 commits into
mainfrom
fix/delivery-content-hash-dedup-984
Jun 2, 2026
Merged

fix(awareness-service): dedupe deliveries by content hash so envelope updates re-deliver#989
coodos merged 2 commits into
mainfrom
fix/delivery-content-hash-dedup-984

Conversation

@coodos

@coodos coodos commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Description of change

Fixes the delivery dedup collision in awareness-service that silently dropped every MetaEnvelope update after the first ingest.

Delivery was uniquely keyed on (subscriptionId, packetId), and since Packet is upserted on the MetaEnvelope id, every re-ingest of an updated envelope reused the same packetId. IngestService.ingest inserts with .orIgnore(), so the second delivery hit the unique key and was silently discarded — a subscriber received each envelope exactly once, ever.

The MetaEnvelope id (packetId) is intentionally not part of the dedup decision anymore; content is. The fix adds a contentHash column to Delivery and changes the unique key to (subscriptionId, packetId, contentHash):

  • Re-ingest of the same payload → same hash → .orIgnore() still dedupes. ✅
  • Re-ingest of an updated payload → new hash → no collision → a new delivery is queued. ✅
  • Retry / dead-letter flow is untouched.

contentHash = sha256(stableStringify(payload.data)), where stableStringify sorts object keys recursively so key ordering does not affect the hash.

Includes a TypeORM-generated migration (1780404367748-AddDeliveryContentHash). The raw generated ADD ... NOT NULL would fail on a populated deliveries table, so it adds the column nullable → backfills legacy rows with a constant → sets NOT NULL, then swaps the unique constraint.

Issue Number

#984

Type of change

  • Fix (a change which fixes an issue)

How the change has been tested

  • npm run build (tsc) in services/awareness-service/api passes.
  • Ran the migration against a throwaway Postgres 16 with the Init schema applied and a pre-existing delivery row: migration:run succeeds, the legacy row is backfilled (contentHash = ''), the column ends up NOT NULL, and the unique constraint becomes (subscriptionId, packetId, contentHash). migration:revert cleanly drops the column and restores uq_delivery_subscription_packet.
  • Traced the issue's repro: first ingest queues 1 delivery; unchanged re-ingest is deduped by .orIgnore(); changed re-ingest produces a new contentHash and queues a new delivery, so the delivery count increments.

Change checklist

  • I have ensured that the CI Checks pass locally
  • I have removed any unnecessary logic
  • My code is well documented
  • I have signed my commits
  • My code follows the pattern of the application
  • I have self reviewed my code

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@coodos, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 46 minutes and 36 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1d38c749-c201-4b83-9afa-980df19e243a

📥 Commits

Reviewing files that changed from the base of the PR and between ca96956 and d48c5f7.

📒 Files selected for processing (4)
  • services/awareness-service/api/src/database/entities/Delivery.ts
  • services/awareness-service/api/src/database/migrations/1780404367748-AddDeliveryContentHash.ts
  • services/awareness-service/api/src/services/IngestService.ts
  • services/awareness-service/api/src/utils/contentHash.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/delivery-content-hash-dedup-984

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coodos coodos merged commit ec3fb2c into main Jun 2, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant