Skip to content

Deduplicate member reply and failure notice delivery across steered follow-ups and part switches - #1682

Merged
Fei2411 merged 7 commits into
devfrom
fix/1319-dedup-delivery
Nov 21, 2026
Merged

Fei2411 merged 7 commits into
devfrom
fix/1319-dedup-delivery

Conversation

@Fei2411

@Fei2411 Fei2411 commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Issue

Fixes #1319

What changed / 改了什么

  • Failure notice deduplication by request_id (turns.js, delivery.js, threads.js): When a member Bot turn was joined mid-turn by steered message_bot follow-ups (entry.joined) and ended with a model error carrying a requestId (such as INVALID_REQUEST), turn/end previously invoked answerSender for both the primary request and every joined request. That queued multiple identical [Reply from ...] failure notices with the same request_id into the caller's nextTurn queue, causing the Main Bot to receive the same failure again on its next turn after already acting on the first notice. answerSender now deduplicates failure notices by (fromId, toId, conversation, failureRequestId) (and per turn/end pass) while settling open works for suppressed duplicates so parent handoff accounting (work.open / takenWork) never hangs.
  • Reply idempotency across part switches and host reconciliation (turns.js, delivery.js, resume.js, threads.js):
    • answerSender now checks both an in-memory delivery set and the durable pairBook ledger (wasReplyDelivered(fromId, toId, exchangeId)) before queueing a reply, while still allowing late replies after abandonWork (work.abandoned && !work.lateReplySent) when no reply was previously delivered.
    • reconcileEnded previously only checked logOf(where) (the recipient's current active part). In long conversations (500+ messages) where the Main Bot switched parts (moveOn / pushThreadPart) after receiving a reply or failure notice in an earlier part, answeredIn missed the earlier delivery and re-delivered the reply verbatim. reconcileEnded now checks wasReplyDelivered, the target part, and earlier segments of the recipient's thread.
    • resumeAll now checks whether the session log already recorded a terminal turn/end (completed, error, or non-disposed aborted) or whether the reply is already in pairBook before calling resumeTurn, preventing already-finished turns from being re-executed on reload.
  • Merged dev after feat(bot-tasks): auto-move busy foreground to background and spawn peer tasks (#783) #1892, preserving task routing. On restart, pending replies stay with their background task, and recovered attachment-backed replies include the full answer and any delivery warning.

Changelog

Bots no longer repeat the same delivery failure in one conversation; unread replies still reach the right background task after a restart.

How I checked it / 怎么验证的

  • npm test (3,505 passed, 4 skipped)

  • node scripts/build-client.mjs --check

  • npm run lint

  • Mock bench smoke test (56 steps)

  • Leak scan

  • Added ds-bot/test/delivery-dedup.test.js covering:

    1. Steered follow-ups joining a failing turn with INVALID_REQUEST (request_id) deliver only one failure notice to the caller conversation and settle open works cleanly.
    2. Steered requests from two distinct openParts still notify each independent conversation once per request_id.
    3. Direct answerSender idempotency per exchangeId and per failure.requestId, while preserving late replies after abandonWork.
    4. reconcileEnded across recipient part switches (pushThreadPart) does not re-deliver replies or request_id failures already delivered in an earlier part.
    5. resumeAll settles already-ended turns from disk without re-running the model or duplicating replies.
    6. Recovery keeps unread replies in background tasks and restores whole attachments with delivery warnings.

Checklist / 检查

  • One issue, one subject.
  • No keys, tokens or personal data.
  • No AI tool as author or Co-authored-by.

Agent: Devin (Cognition)

Link to Devin session: https://app.devin.ai/sessions/23347c537bbb4ec18f2f91c0e5076d7f
Link to Bot: https://app.devin.ai/org/feizhululu/bots/66f2a751b4f84ea6896e40b044f19644
Open in Devin Desktop: https://app.devin.ai/desktop/session/23347c537bbb4ec18f2f91c0e5076d7f?variant=devin


Devin Review

…ollow-ups and part switches

When a member Bot turn was joined mid-turn by steered message_bot follow-ups and failed (e.g. INVALID_REQUEST with a request_id), turn/end called answerSender once for the primary request and once per joined request, pushing duplicate failure notices with the same request_id into the sender's nextTurn queue. In long conversations that switched parts (or reconciled ended/inflight turns on reload), reconcileEnded only checked the sender's current session log rather than earlier thread segments or the pair delivery ledger, and resumeAll did not inspect whether the session log or pair ledger already recorded a terminal turn before re-running.

- Record failureRequestId on delivered pair-ledger entries and expose wasReplyDelivered / wasFailureRequestDelivered helpers in delivery.js
- Add sameConversation in threads.js to compare sender conversation identity across thread part switches while keeping parallel openParts and rooms isolated
- Make answerSender idempotent per (from, to, exchangeId), per (from, to, conversation, failureRequestId), and per turn/end pass, while settling open works on suppressed duplicates so parent handoffs never hang
- Check the pair delivery ledger and earlier thread segments in reconcileEnded and check terminal session logs + pair ledger in resumeAll before re-delivering or re-running
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T18:50:51.089096Z 06f63c9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 06f63c9cc9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ds-bot/src/host/turns.js
Comment on lines +83 to +84
const parent = rt.closeWork?.(exchangeId, name, text)
if (parent !== undefined) rt.takenWork?.(parent, exchangeId)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve continuation delivery for distinct parent works

When the same caller conversation sends requests for two different parent works into a member's running turn, a failure suppresses the second reply even though the first reply carries only the first parent's work. Here, closeWork records the second parent's result and takenWork removes its outstanding child, but neither schedules a continuation. In turns.js, that continuation normally comes from consuming the reply carrying source.work; consequently, the second parent can remain held indefinitely without answering its upstream requester. Restrict suppression to requests sharing the primary parent, or explicitly schedule settlement of every distinct suppressed parent instead of marking its result taken without a delivery.

Useful? React with 👍 / 👎.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

… resume

Prevent distinct parent works from stalling when steered into the same failed turn, and fix restart reconciliation for continuation turns:
- Scope in-memory and ledger failure deduplication in answerSender by parent work and require the primary child to still be pending in parent.open before coalescing a second child's failure.
- Settle steered child requests from distinct parent works in reconcileEnded as well.
- In resumeAll, resolve role=reply continuation triggers via rt.workOf(trigger) and the upstream asker rather than the child reply's exchangeId and senderSessionId.
- Clear stale child parent pointers in dropWork.

Fixes #1319.
devin-ai-integration[bot]

This comment was marked as resolved.

@Fei2411 Fei2411 added the review/re-review New commits pushed after last review — needs re-review label Nov 21, 2026
@Fei2411 Fei2411 added this to the v0.3.9 milestone Nov 21, 2026
Keep task handoffs intact while recovering already-ended turns. Restore full attachment-backed results when reconciling delivered replies.
devin-ai-integration[bot]

This comment was marked as resolved.

Recovery reads the original attachment, but delivery may add a warning only to its preview. Restore that prefix without duplicating the preview.
Do not finalize a recovered task until its queued input can reach that task rather than the foreground chat.

@Fei2411 Fei2411 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Reaper review (comments only) — head 61b5fbe

Verdict: close to ready. One P2 recovery gap worth fixing in this PR (or explicitly deferring to a follow-up issue); everything else checks out.

Conflict resolution with dev (merge 2a367e75): both sides kept. delivery.js keeps #1892's taskId/imOrigin and background-task headers alongside failureRequestId and durable dedupe; threads.js keeps task-aware lookup, part switching and successor routing (sameConversation uses the task-aware lookup); resume.js keeps task routing, replay destinations, keepTaskSessions and restart task reconciliation next to the new terminal-log settlement. Nothing from #1892 dropped. Earlier merges 276d8711, e4d6f606 had no conflict edits in these files.

P2 — completed-turn recovery can strand a steered request (ds-bot/src/host/resume.js ~453–475, 597–599)
Joined (steered) requests are enumerated only in the error/aborted branch. For a completed terminal turn/end, resumeAll now settles the primary request and skips resumeTurn, but never requeues or settles requests the turn joined.
Scenario: A sends B req-1 and req-2 (independent parents p1/p2); B consumes both, turn/end completed reaches disk, host stops before the live handler defers req-2. After restart, req-1 is settled, req-2 stays open with nothing to run it. Reproduced by switching the existing restart fixture's failed turn to completed:

caller replies: [ 'req-1' ]
member followups: []
req-2 still open: true
parent-2 open: [ 'req-2' ]

Caveat: dev's reconcileEnded has the same gap for successful joined requests, but this PR's new shortcut is what reaches it on cold restart. Suggested fix: in the completed branch, defer/requeue joined requests the same way turns.js does on live completion, plus a test.

Other checks: in-memory guards are capped (1,000) and per-runtime; failure suppression is scoped by conversation and live parent work; late replies after abandonWork still go through. Earlier Codex/Devin Review findings (parent-scoped continuations, attachment full text + warnings, unread task replies on restart) are addressed with tests.
Local: npm test 3505 pass / 0 fail / 4 skipped; build-client --check clean; npm run lint passes.

Overlaps (open PRs touching turns/delivery/threads/resume): #1855, #1817, #1201, #1074, #780 all conflict textually; none is the same fix (#780 changes deferred-message payloads, not reply/failure idempotency). This PR is green and mergeable, so it should land first and those rebase onto it.

Agent: Devin (Cognition)

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Code Reaper’s completed-turn steering finding is tracked in #1946. The successful-turn recovery gap also exists in dev’s ended-turn path; replay needs separate coverage for already-deferred requests and attachments. This PR keeps the focus on failure deduplication and handoff compatibility.

@Fei2411
Fei2411 dismissed devin-ai-integration[bot]’s stale review November 21, 2026 08:28

Fixed on current head 61b5fbe. Distinct parent works each receive a continuation; live and restart regression tests pass. The review thread is resolved and the current Devin Review has no findings.

@Fei2411 Fei2411 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Reaper re-review (comments only) — head 61b5fbe

Verdict: pass. Ready for lulu.

The head is the same commit I reviewed before, so my earlier findings still hold. The merge with dev kept both sides, including #1892's task handoffs. The earlier review findings are fixed, and tests, --check and lint pass. The one P2 (recovery after a restart can strand a steered request when the turn it joined had already completed, in resume.js) is now tracked in #1946. The same gap already exists in dev's reconcileEnded, so deferring it is reasonable. CI on this head is green and the PR merges cleanly.

Land order: this PR first. #1855, #1817, #1201, #1074 and #780 conflict with it as text, so they need to be updated on top of it.

Agent: Devin (Cognition)

@Fei2411
Fei2411 merged commit 171bc35 into dev Nov 21, 2026
15 checks passed
@Fei2411
Fei2411 deleted the fix/1319-dedup-delivery branch November 21, 2026 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review/re-review New commits pushed after last review — needs re-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant