Skip to content

fix: hand a message back as a pointer, not the body again - #780

Open
neotechnet wants to merge 1 commit into
Fei2411:mainfrom
neotechnet:fix/hand-back-pointer
Open

neotechnet wants to merge 1 commit into
Fei2411:mainfrom
neotechnet:fix/hand-back-pointer

Conversation

@neotechnet

Copy link
Copy Markdown
Contributor

What happens

A message that arrives while a Bot is mid-work is delivered twice: once as a steer the
running turn reads at its next step, and again in full when that turn ends, so the Bot can
answer it in a turn of its own. The second delivery repeats the whole body, so the model
reads one message twice.

Root cause

Both hand-backs re-send the message text they were given:

  • src/host/turns.js (the joined loop at the end of turn/end) delivers
    text: request.text
  • src/host/turns.js (the later loop, parked results) delivers text: result.text

Both request.text and result.text come from a user/message event the model has
already been shown in that Session. Nothing catches it: deliver mints a fresh
exchangeId when the caller does not pass one, and a deferred delivery deliberately
skips the ledger write, so there is no check anywhere for "this exchange is already in
this Session".

Measured on a real session before the change: one sender send, one ledger record, two
user/message events carrying the same exchangeId under two different inbox item ids,
the second 51 characters longer than the first. The 51 characters are the header
annotation; the body is byte-identical.

The change

The first showing is kept. The hand-back keeps its identity (same exchangeId, same
role, same sender, still deferred) and carries a one-line pointer instead of the body:

You read its text during that task; it is above in this session. Answer it now.

deliver takes a new handedBack flag and stamps source.handedBack = true on the
delivery, so anything that needs to tell a pointer from the message itself can. The one
place that does is exchangeText, which reads a message back from the Session for the
exchanges dialog: it now skips a delivery marked handedBack, so the dialog still shows
the real text rather than the pointer.

Nothing else moves: the trigger, the ordering, the sender, the ledger shape, the Session
schema and the chat UI are unchanged. Every message still gets answered.

Test

test/handback.test.js is new and was written before the change. On the unpatched tree
three of its four cases fail, each on the body being re-sent; the fourth (a message to an
idle Bot, which was never read mid-turn) passes before and after, as it should.

Two existing tests asserted the old behaviour and were updated to the pointer: the four
sites in test/bot-messages.test.js and one in test/group-work.test.js that compared a
hand-back against the full body. One of them, "a request from a name with brackets keeps
its body when replayed", no longer had a body to check, so it now proves what still holds:
the bracketed sender name leaves a well-formed header and the hand-back carries the
pointer.

Trade-off

The model answers from what it read earlier in the same Session rather than re-reading the
body. That is the intended path (the text is still above it, and the header names the
sender), but it is a behaviour change for a model that was relying on the second copy, so
it is worth watching the first few hand-backs after release.

joined[].text is now unused by the relay. It is left in place deliberately: the part
switch carries joined across a handoff and the field is part of that payload, so
removing it is a separate change.

A message that lands while a Bot is mid-work is delivered twice: the running turn
reads it at its next step, then turn/end hands it back so the Bot can answer it in a
turn of its own. Both hand-back loops re-sent the whole text (request.text in the
joined loop, result.text in the later loop), and both of those come from a
user/message event the model has already been shown in that Session. So the model
read one message twice, under one exchangeId, with nothing anywhere to catch it.

The hand-back keeps its identity: same exchangeId, same role, same sender, still
deferred, so the sender still gets its answer and the ledger still records one send.
It now carries a one-line pointer instead of the body.

deliver takes a new handedBack flag and stamps source.handedBack on the delivery.
exchangeText is the one reader that needs to tell the two apart, so it skips a
delivery marked handedBack when it reads a message back from the Session for the
exchanges dialog, which still shows the real text.

test/handback.test.js covers a joined request, a reply handed back for other work,
one body across the steer and the hand-back together, and a message to an idle Bot
left untouched. Three of its four cases fail on the unpatched tree.

Five existing assertions in test/bot-messages.test.js and test/group-work.test.js
compared a hand-back against the full body; they now compare it against the pointer.
The "name with brackets" case no longer had a body to check, so it proves what still
holds: the bracketed sender name leaves a well-formed header.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T00:27:04.241817Z c428983 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: c428983761

ℹ️ 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
toId: rt.selfOf(id),
sessionId: id,
text: request.text,
text: HANDED_BACK,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve request bodies across Session switches

When a joined request crosses a Session switch, the destination Session is not guaranteed to contain its body: parts.js clips handoff history, and its job.bare recovery explicitly carries no previous messages (parts.js:894). In that recovery path, the full request enters the new Session only through handoffSource().joined metadata, which claimTrigger() restores for routing but never adds to model-visible content. Replacing request.text here therefore removes the delivery that previously supplied the pending instructions, leaving the answering turn with a false claim that they are above in this Session. Keep the stored body, or supply a concrete old-part lookup anchor, when the original delivery is absent from the destination context.

AGENTS.md reference: AGENTS.md:L14-L14

Useful? React with 👍 / 👎.

@AdamPlatin123

Copy link
Copy Markdown
Collaborator

Field data in support of this change, from a real install (ds-bot 0.2.7 in dsh web, 39 Bots, Session records from 2026-10-21 to 11-05, read only):

  • 152 deferred hand-backs: 125 message_bot requests and 27 group calls (role: 'room'), spread over about 20 Bot Sessions, up to 36 in one Session.
  • In all 152, the body is byte-identical to the copy the same Session had already been shown mid-turn. Together that is about 210k characters the model read twice.
  • In at least 27 cases the Bot answered that the work was already done and then checked it all over again. Over 67 such turns that cost 186 extra tool calls.
  • The group-call hand-backs carry the longest bodies (median about 3.6k characters). I checked the diff: the room path goes through the same joined loop and group-work.test.js now asserts the body is not repeated, so this PR covers it as well.

The counts come from user/message events with the same exchangeId in one Session. No message text from that install is quoted here.

@mrli222

mrli222 commented Nov 9, 2026

Copy link
Copy Markdown

One more data point on the cost, from a different angle: this hand-back is also how a single slow turn becomes four extra turns.

In today's run (DS Bot 0.3.1, DSH Desktop, Windows), a member turn was wrongly marked missed at 240 s (#781) while it was still working. When it finally ended at 689 s, turns 20–23 ran back to back, each only to answer a call that had queued during it, and each posted an "old replay, nothing to re-decide" line to the group. In that window the same exchangeId appears twice:

15:50:48  user/message  source=bot/room  exchange=31a7d842  deferred=false
15:51:37  user/message  source=bot/room  exchange=31a7d842  deferred=true

So besides the duplicated body, the hand-back multiplies turns after a false missed. If the pointer change lands, it would help to know whether a hand-back is still expected to produce a group post when the member already answered that exchange in the previous turn's text.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

REQUEST_CHANGES — use the pointer only when the original body is actually present in the answering Session.

The handback/bot-message suites pass (44 tests), but I reproduced the missing-body case with the turn-host fixture: start a fresh part from a handoff source carrying a pending request only in joined metadata, as bare recovery does, then finish the resumed turn. The pending request is handed back as the generic pointer, so its actual instructions never reach the fresh model context. parts.js explicitly omits earlier messages for job.bare, and claimTrigger restores metadata for routing, not model-visible content. Preserve/replay the body across that boundary, or deliver a specific usable old-part reference; add a bare-recovery regression.

This is also still targeted at main. Unless this is explicitly being released as a hotfix, retarget to dev and merge current dev; the reviewed tree conflicts in delivery, turns and bot-message tests. Recheck the deferred-pointer path alongside #1682 so durable dedup does not suppress the continuation.

Written by Devin

Comment thread ds-bot/src/host/turns.js
toId: rt.selfOf(id),
sessionId: id,
text: request.text,
text: HANDED_BACK,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Retain the request body when joined work crosses a part switch. handoffSource().joined carries pending instructions into the fresh part as source metadata, but claimTrigger only restores it for routing. In bare recovery (parts.js, readLines), no previous message body is carried into model-visible content. A reproduced handoff with joined: [{info, text: 'Build the unique pending report with chart Z.'}] therefore sends only this pointer at turn end; the quoted instruction is never given to that model. Gate the pointer on the original context still being present, otherwise replay the stored body or provide an exact recoverable old-part anchor.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

New integration point since the last review: #1751 is now on dev. It hides internal delegated-work continuation turns while leaving user-facing answers visible. When porting the pointer behavior to dev, please preserve that distinction and use #1751’s visibility regression when checking the deferred-result path.

Written by Devin

@Fei2411

Fei2411 commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks for the PR! A few blocking issues prevent merging as-is:

  1. Target branch & conflicts: This PR targets main instead of dev (per CONTRIBUTING.md, fixes go to dev) and currently conflicts with dev.
  2. Missing context on handoff / session change / compaction: Unconditionally replacing deferred message bodies with a pointer text breaks whenever the turn crosses a part switch (handoffSource moves into a fresh session where the earlier text does not exist), falls back to a different session, or undergoes compaction.

If you'd like to continue this, please retarget dev, sync with latest dev, and guard the pointer substitution strictly to the same uncompacted session without part switches.

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.

4 participants