Skip to content

fix(agents): prevent duplicate recovery after explicit source replies - #113554

Merged
vincentkoc merged 1 commit into
openclaw:mainfrom
zeroaltitude:fix/cli-source-reply-explicit-route-kg9
Aug 23, 2026
Merged

vincentkoc merged 1 commit into
openclaw:mainfrom
zeroaltitude:fix/cli-source-reply-explicit-route-kg9

Conversation

@zeroaltitude

@zeroaltitude zeroaltitude commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where message_tool_only runs could post a reply successfully and then post it again through stranded-reply recovery when the message tool explicitly named the current conversation.

The failure affected three completion paths: CLI loopback, the embedded terminal hook, and embedded subscription completion. A send to another account, thread, or target must still remain off-source so recovery can deliver the actual source reply.

Why This Change Was Made

All three paths now adapt their confirmed message send into the existing canonical isDeliveredCurrentSourceReply contract. The decision therefore uses the prepared provider, account, target, session, and thread facts instead of maintaining partial route matchers.

The rewrite does not pass signed capabilities into CLI subprocesses, synthesize trusted delivery annotations, or change authorization, configuration, SDK, protocol, or session-lifecycle policy.

Architectural closeout:

  • Root cause: delivered explicit routes were rejected as source-reply evidence when no trusted gateway annotation was present.
  • Owner: src/infra/outbound/source-reply-mirror.ts.
  • Canonical fix: one adapter from confirmed MessagingToolSend evidence to isDeliveredCurrentSourceReply.
  • Sibling coverage: CLI loopback, embedded terminal, and subscription completion.
  • Production delta: +149/-28, net +121, across 8 production files.
  • Test delta: 3 focused files.

User Impact

An explicit message(action=send) to the active conversation now suppresses stranded-reply recovery exactly once. Sends to a different account, thread, or target do not suppress recovery.

Evidence

Current-main regression proof before the rewrite:

  • CLI exact current-source route: failed; 57 controls passed.
  • Embedded terminal exact current-source route: failed; 21 controls passed.
  • Subscription completion exact current-source route: failed; 173 controls passed.
  • Same target with a different account, same target with a different thread, and a different target all remained off-source.

Focused proof on rewritten head 11413bc0a8feb0cb0586b2674d552c9881d40365:

  • node scripts/run-vitest.mjs src/agents/cli-runner/execute.supervisor-capture.test.ts: 58 passed.
  • node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts: 22 passed.
  • node scripts/run-vitest.mjs src/agents/embedded-agent-subscribe.handlers.tools.test.ts: 174 passed.
  • node scripts/run-vitest.mjs src/infra/outbound/source-reply-mirror.test.ts: 3 passed.
  • git diff --check: passed.
  • oxfmt --check on all 11 changed files: passed.
  • Max-P1 autoreview: clean, no accepted or actionable findings.
  • Blacksmith Testbox check:changed: passed all guards, Knip, core/core-test typecheck, lint, import-cycle, database-first, and pairing gates.

Mock-gateway boundary proof:

{
  "scenario": "msteams-thread-message-tool-final-dedupe",
  "providerMode": "mock-openai",
  "channelDriver": "live",
  "channel": "msteams",
  "runner": "host",
  "passed": 1,
  "failed": 0,
  "skipped": 0,
  "head": "11413bc0a8feb0cb0586b2674d552c9881d40365"
}

The scenario exercises the real Gateway and bundled Microsoft Teams plugin with a loopback Bot Framework connector. It verifies one delivery for the same source thread and preserves recovery for a different thread and different conversation, plus direct-message and group-chat cases.

Contributor credit is preserved: the rewritten commit is authored by Edward Abrams (@zeroaltitude).

Punchcard-Session: golden-river-workshop-vj

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jul 25, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. labels Jul 25, 2026
@clawsweeper

clawsweeper Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 23, 2026, 4:55 PM ET / 20:55 UTC.

ClawSweeper review

What this changes

The branch makes explicit, successfully delivered message-tool sends count as replies to the active conversation only when provider, account, target, and thread match across CLI loopback, embedded terminal, and subscription completion.

Regression provenance

Possible regression — probable (reviewed change; known regression link). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 2 items remain

No blocking findings. The PR routes all three completion paths through the existing exact current-source delivery predicate, and its real Gateway/Teams proof covers both suppression and off-route recovery; it remains useful because current main still lacks this route verification.

Priority: P1
Reviewed head: 11413bc0a8feb0cb0586b2674d552c9881d40365

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused canonical repair with route-mismatch coverage and sufficient production-boundary proof.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The PR body supplies exact-head after-fix proof through a real Gateway, bundled Teams plugin, and loopback transport, observing one same-thread delivery while preserving off-route recovery.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body supplies exact-head after-fix proof through a real Gateway, bundled Teams plugin, and loopback transport, observing one same-thread delivery while preserving off-route recovery.
Evidence reviewed 6 items Current-main gap: Current main's CLI tracking still calls the source-reply predicate with confirmed delivery only; it does not pass explicit-route verification.
Canonical owner boundary: The branch adapter delegates provider, account, target, thread, and failed-delivery checks to the existing exact source-reply contract rather than adding per-runner route matchers.
Sibling wiring: The branch supplies prepared route context to the terminal hook and applies the same adapter in CLI and subscription completion.
Findings None None.
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts

Result: PASS (completed)

node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts
runner@runnervm76f27:/tmp/clawsweeper-live-proof-113554-YCCa5G/target$ node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/message-tool-terminal.te
st.ts
node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts
[test] starting test/vitest/vitest.agents-embedded-agent-run.config.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-113554-YCCa5G/target

 ✓  agents-embedded-agent-run  src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts (22 tests) 215ms

 Test Files  1 passed (1)
      Tests  22 passed (22)
   Start at  20:56:34
   Duration  9.27s (transform 7.13s, setup 987ms, import 7.70s, tests 215ms, environment 0ms)

[test] passed 1 Vitest shard in 14.94s
runner@runnervm76f27:/tmp/clawsweeper-live-proof-113554-YCCa5G/target$ node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/message-tool-terminal.te
st.ts

































Assertions:

  • PASS expect_output: message-tool-terminal.test.ts

How this fits together

Message-tool-only runs can send a visible reply through a channel tool, after which recovery decides whether another reply is needed. The changed paths convert confirmed send evidence into a canonical source-route decision before that recovery runs.

flowchart LR
  A[Message tool send] --> B[Confirmed delivery result]
  B --> C[Canonical source-route check]
  C --> D{Matches active conversation?}
  D -->|yes| E[Record source reply]
  D -->|no| F[Keep recovery eligible]
  E --> G[Suppress duplicate reply]
  F --> G
Loading

Before merge

  • Resolve merge risk (P1) - This delivery-critical branch is behind current main and its required exact-head checks are still in progress; a false route classification could either duplicate a reply or suppress needed recovery.
  • Complete next step (P2) - No narrow repair remains; this PR needs normal exact-head merge validation rather than an automated fix lane.
Agent review details

Security

None.

PR surface

Source +121, Tests +204. Total +325 across 11 files.

View PR surface stats
Area Files Added Removed Net
Source 8 149 28 +121
Tests 3 204 0 +204
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 11 353 28 +325

Review metrics

Metric Value Why it matters
Production versus regression coverage production +149/-28; tests +204/-0 The positive production growth is confined to one shared adapter and prepared-context plumbing across three delivery paths, with route-mismatch regression matrices covering each path.

Merge-risk options

Maintainer options:

  1. Require current-head delivery gates (recommended)
    Wait for the clean merge's required checks before landing because the changed predicate controls whether recovery emits a visible reply.

Technical review

Best possible solution:

Land the canonical adapter after the clean GitHub merge and exact-head delivery checks confirm the existing mismatch matrices and Gateway scenario remain green.

Do we have a high-confidence way to reproduce the issue?

Yes—source-reproducible: current main rejects explicit routes in CLI, terminal, and subscription completion because those callers provide no verified explicit-source route.

Is this the best way to solve the issue?

Yes—the branch reuses the authoritative delivery matcher and passes prepared route facts through each owner path rather than maintaining parallel partial matchers.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against d0db8a9241bc.

Labels

Label changes:

  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body supplies exact-head after-fix proof through a real Gateway, bundled Teams plugin, and loopback transport, observing one same-thread delivery while preserving off-route recovery.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body supplies exact-head after-fix proof through a real Gateway, bundled Teams plugin, and loopback transport, observing one same-thread delivery while preserving off-route recovery.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: 📣 needs proof: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P1: A false negative produces duplicate visible replies in active agent conversations.
  • merge-risk: 🚨 message-delivery: The patch changes the condition that suppresses or permits stranded-reply recovery.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body supplies exact-head after-fix proof through a real Gateway, bundled Teams plugin, and loopback transport, observing one same-thread delivery while preserving off-route recovery.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body supplies exact-head after-fix proof through a real Gateway, bundled Teams plugin, and loopback transport, observing one same-thread delivery while preserving off-route recovery.

Evidence

What I checked:

Likely related people:

  • Omar Shahine: Authored the current source-reply and message-tool predicate update used as this PR's canonical contract. (role: source-reply contract contributor; confidence: high; commits: 51bf8f8eb8db; files: src/agents/embedded-agent-message-tool-source-reply.ts, src/infra/outbound/source-reply-mirror.ts)
  • Peter Steinberger: Authored the channel thread-addressing refactor that changed the shared source-reply matcher. (role: threading-contract contributor; confidence: high; commits: 37182b90506d; files: src/infra/outbound/source-reply-mirror.ts, src/channels/thread-addressing.ts)
  • Ayaan Zaidi: Authored the recent Telegram semantic target-matching fix that uses the same current-source route boundary. (role: adjacent channel-route contributor; confidence: medium; commits: 08d926d3a5b3; files: extensions/telegram/src/channel.ts, src/infra/outbound/message-action-execution.source-reply.test.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (14 earlier review cycles; latest 8 shown)
  • reviewed 2026-08-01T23:10:34.160Z sha 9a4cc45 :: needs real behavior proof before merge. :: [P1] Compare thread identity before accepting an explicit source route | [P1] Keep CLI source-route checks thread- and account-aware
  • reviewed 2026-08-03T04:02:00.566Z sha 5050b89 :: needs real behavior proof before merge. :: [P1] Compare thread identity before accepting a CLI source route | [P1] Include account and thread in embedded route verification
  • reviewed 2026-08-07T07:52:11.627Z sha 5050b89 :: needs real behavior proof before merge. :: [P1] Match CLI source routes by account and thread | [P1] Match embedded source routes by account and thread | [P1] Preserve the terminal hook’s canonical route context | [P1] Wire explicit-route verification into the subscription handler
  • reviewed 2026-08-14T12:57:03.687Z sha 5050b89 :: needs real behavior proof before merge. :: [P1] Compare complete route identity in both new matchers | [P1] Restore explicit-route verification in the subscription handler | [P1] Preserve completed-source-reply termination
  • reviewed 2026-08-14T19:40:21.638Z sha 3e60195 :: needs real behavior proof before merge. :: [P1] Compare account and thread identity for CLI routes | [P1] Compare account and thread identity for embedded routes | [P1] Wire verified routes into subscription completion
  • reviewed 2026-08-15T05:21:57.305Z sha a4f42e3 :: needs real behavior proof before merge. :: [P1] Compare full CLI route identity | [P1] Compare full embedded route identity | [P1] Wire verified routes into subscription completion
  • reviewed 2026-08-17T14:56:58.639Z sha 3bd470c :: needs real behavior proof before merge. :: [P1] Match full CLI route identity | [P1] Match full embedded route identity | [P1] Pass the prepared source route to terminal verification | [P1] Verify routes in subscription completion
  • reviewed 2026-08-19T19:00:20.053Z sha beac8aa48b2d85fe6e182ca4097948192e808d39 :: needs real behavior proof before merge. :: [P1] Compare full CLI route identity | [P1] Compare full embedded route identity | [P1] Pass the prepared source thread to terminal verification | [P1] Verify routes in subscription completion

@clawsweeper clawsweeper Bot added the merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. label Jul 27, 2026
@zeroaltitude zeroaltitude changed the title fix(cli-runner): count current-source explicit-route sends as source replies fix(embedded-agent,cli-runner): count current-source explicit-route sends as source replies Jul 27, 2026
@zeroaltitude

Copy link
Copy Markdown
Contributor Author

Round update

Extended this PR to the whole defect class (openclaw-p3j) and addressed the outstanding finding:

  • [P2] Remove the release-owned changelog entry — CHANGELOG.md:55 (raised again this cycle; unresolved since the 2026-07-25 review). Removed both the original CLI-runner entry and a second embedded-agent entry I'd added on top of it before catching this. Commit f6e9700a4c2.

What's new in this round

Extended the openclaw-kg9 fix (CLI-runner only) to the two embedded-agent call sites with the same gap:

  • embedded-agent-subscribe.handlers.tools.ts (tool-handler subscription path, Claude app-server / claude-bridge and other in-process embedded runs)
  • embedded-agent-runner/run/message-tool-terminal.ts (native agent-loop afterToolCall terminal hook)

Added messagingToolSendResolvesToCurrentSource (embedded-agent-subscribe.tools.ts) mirroring cliSendResolvesToCurrentSource's route-comparison semantics, and wired it into both call sites. Commit b499dfaee62.

Result: pnpm tsgo:core, pnpm tsgo:core:test, pnpm oxlint, pnpm config:schema:check, pnpm plugin-sdk:api:check, git diff --check all clean. Targeted vitest across every touched file plus existing adjacent suites: 284 and 282 tests green. Full details in the updated PR description.

pnpm check:changed could not run in the working sandbox this round (crabbox/Blacksmith Testbox binary sanity check fails there, independent of this change) — flagging for visibility rather than claiming a gate I didn't observe.


Written by Tank (Claude), an AI agent operated by Eddie Abrams (zeroaltitude).

@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jul 27, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jul 27, 2026
@zeroaltitude

Copy link
Copy Markdown
Contributor Author

Reconciled with #114680 (a separately-dispatched fix for the same defect that landed and merged into our integration branch first):

This branch now covers: the original CLI-runner fix (openclaw-kg9) + call site #3 only. #114680 covers call site #1. No overlap.

Re-validated after the revert: tsgo:core clean, 466 vitest tests across 10 files green, oxlint/git diff --check clean. Also dropped the CHANGELOG.md entry per the earlier Clawsweeper feedback in this thread (release notes are managed separately from PRs).


Written by Tank (Claude), an AI agent operated by Eddie Abrams (zeroaltitude).

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jul 27, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Aug 15, 2026
@vincentkoc vincentkoc self-assigned this Aug 23, 2026
Use the canonical delivered current-source matcher for CLI, terminal, and subscription completion paths. Preserve provider, account, target, and thread identity so only the active source route suppresses stranded-reply recovery.

Punchcard-Session: golden-river-workshop-vj
@vincentkoc
vincentkoc force-pushed the fix/cli-source-reply-explicit-route-kg9 branch from beac8aa to 11413bc Compare August 23, 2026 20:38
@vincentkoc vincentkoc changed the title fix(embedded-agent,cli-runner): count current-source explicit-route sends as source replies fix(agents): prevent duplicate recovery after explicit source replies Aug 23, 2026
@vincentkoc

Copy link
Copy Markdown
Member

@clawsweeper re-review

Exact-head rewrite is now at 11413bc0a8feb0cb0586b2674d552c9881d40365.

The previous rank-up moves are addressed: one canonical full-route adapter, prepared account/thread context, CLI + terminal + subscription wiring, production net +121, focused mismatch matrices, and exact-head Microsoft Teams mock-gateway proof are in the PR body.

@vincentkoc
vincentkoc marked this pull request as ready for review August 23, 2026 20:42
@clawsweeper

clawsweeper Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@clawsweeper

clawsweeper Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Aug 23, 2026
@vincentkoc
vincentkoc merged commit 3a4b3df into openclaw:main Aug 23, 2026
329 of 330 checks passed
@vincentkoc

Copy link
Copy Markdown
Member

Merged via squash.

@zeroaltitude
zeroaltitude deleted the fix/cli-source-reply-explicit-route-kg9 branch August 23, 2026 21:30
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 24, 2026
Use the canonical delivered current-source matcher for CLI, terminal, and subscription completion paths. Preserve provider, account, target, and thread identity so only the active source route suppresses stranded-reply recovery.

Punchcard-Session: golden-river-workshop-vj

Co-authored-by: Edward Abrams <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants