Skip to content

Make duplicate reciept#1583

Open
Chigybillionz wants to merge 7 commits into
Stellar-Mail:mainfrom
Chigybillionz:make-duplicate-reciept
Open

Make duplicate reciept#1583
Chigybillionz wants to merge 7 commits into
Stellar-Mail:mainfrom
Chigybillionz:make-duplicate-reciept

Conversation

@Chigybillionz

Copy link
Copy Markdown

Close #1549

Summary of the issue

Receipt publication was not deterministic for duplicate delivery and read receipt calls. Clients that retried after timeouts could receive conflicts for otherwise identical duplicate requests, and concurrent retries could race on timestamp assignment.

Root cause

The receipt service performed duplicate handling with separate read-then-write logic. Duplicate delivery receipts were rejected with 409, and duplicate read receipts were rejected after the first read transition. Because timestamp assignment happened outside an atomic receipt transition, concurrent retries could observe stale state and attempt repeated receipt writes.

Solution implemented

This PR makes receipt publication idempotent and documents timestamp precedence:

  • Identical duplicate delivery receipts replay the stored receipt.
  • Duplicate read receipts replay the stored receipt.
  • The first persisted deliveredAt timestamp wins.
  • The first persisted readAt timestamp wins.
  • Duplicate delivery attempts for the same messageId with different participants still return 409 conflict.
  • Receipt publication performs only one storage/domain transition per delivery/read state change.

Key changes made

  • Added repository-level receipt transition methods:
    • createReceiptIfAbsent
    • markReceiptRead
  • Updated the in-memory repository to serialize per-message receipt transitions for deterministic concurrent behavior.
  • Updated the Cloudflare KV repository to coordinate receipt transitions through StealthCoordinator.
  • Added legacy KV hydration so existing receipts keep their original timestamps when first seen by the coordinator-backed flow.
  • Updated receipt service behavior to replay stored receipts for identical duplicates and preserve conflicts for participant mismatches.
  • Documented duplicate timestamp precedence in the receipts API README.
  • Updated protocol vectors and API/e2e expectations for idempotent duplicate receipt behavior.
  • Added tests for sequential duplicates, concurrent duplicates, timestamp precedence, and single KV mirror writes per receipt transition.

Trade-offs or considerations

The API now treats identical duplicate delivery/read publication as successful replay instead of conflict. Conflicting participant claims for an existing messageId remain conflicts to avoid silently changing receipt ownership.

Testing steps

  • Run focused unit tests:
    npm test -- --run tests/unit/api/receipt-service.test.ts tests/unit/api/stealth-coordinator.test.ts tests/unit/api/kv-repository.test.ts tests/unit/api/health.test.ts

  • Run the full unit suite:
    npm test

  • Optional API verification:

    1. Create a delivery receipt.
    2. Retry the same delivery receipt and confirm the stored receipt is replayed.
    3. Retry with the same messageId but different participants and confirm 409 conflict.
    4. Mark the receipt as read.
    5. Retry the read call and confirm the first readAt timestamp is preserved.
    6. Run concurrent duplicate delivery/read calls and confirm both responses converge on the same stored timestamps.

Please kindly review this task. If there are any corrections, improvements, adjustments, or merge conflicts that you notice regarding my implementation, I'd really appreciate your feedback. I'd also love to hear your overall review of my work on this branch.Thank you!

@Chigybillionz
Chigybillionz force-pushed the make-duplicate-reciept branch from 6a5041c to 34206dc Compare July 20, 2026 12:51
@kryputh

kryputh commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Format your code

@Chigybillionz

Chigybillionz commented Jul 20, 2026 via email

Copy link
Copy Markdown
Author

@Chigybillionz
Chigybillionz force-pushed the make-duplicate-reciept branch from 96b483f to 6ed7d83 Compare July 20, 2026 19:21
@kryputh

kryputh commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

@Chigybillionz tell your agent to run bun command with prettier to format the code

Then check ✅ it again with bun after with bun prettier check ✅...

That the case for all your issues. If you have CODEX try using it or just connect your GitHub with GPT and use chatGPT work with the GitHub mcp

It should also solve it

@kryputh

kryputh commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

@Chigybillionz tell your agent to run bun command with prettier to format the code

Then check ✅ it again with bun after with bun prettier check ✅...

That the case for all your issues. If you have CODEX try using it or just connect your GitHub with GPT and use chatGPT work with the GitHub mcp

It should also solve it

@Chigybillionz

Chigybillionz commented Jul 20, 2026 via email

Copy link
Copy Markdown
Author

@kryputh

kryputh commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

First which AI agent are you using second is how good is your prompt techniques because this CI is almost getting impossible for you to solve

@Chigybillionz

Chigybillionz commented Jul 21, 2026 via email

Copy link
Copy Markdown
Author

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.

Make duplicate receipt publication deterministic

2 participants