Skip to content

fix: stop repeated message_tool_only source replies#102179

Open
849261680 wants to merge 1 commit into
openclaw:mainfrom
849261680:fix/96827-message-tool-only-source-reply-loop
Open

fix: stop repeated message_tool_only source replies#102179
849261680 wants to merge 1 commit into
openclaw:mainfrom
849261680:fix/96827-message-tool-only-source-reply-loop

Conversation

@849261680

@849261680 849261680 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Closes #96827

AI-assisted: yes

What Problem This Solves

Fixes an issue where users in message_tool_only group-chat turns could see the agent send repeated visible source replies within one embedded run after the first message(action="send") delivery.

Why This Change Was Made

The source-reply terminal hook now reserves one implicit current-source message delivery slot per message_tool_only run. The first implicit source reply can still complete and the run can continue to non-message follow-up tools, but subsequent implicit current-source message(send) calls are returned to the model as suppressed terminal results instead of being delivered again.

The slot is finalized against the afterToolCall delivery classification. If a hook rewrites the first raw send as failed or non-delivered, the slot is released so a later valid implicit source reply can still send. Suppressed duplicate results do not release an in-flight reservation. Explicit routes, non-message tools, dry runs, failed/non-delivered sends, and deferred message-tool hydration remain outside the suppression path.

User Impact

Telegram and other message_tool_only source-reply surfaces should deliver at most one visible reply to the current source conversation for a run, avoiding self-reply cascades and extra model/channel churn while preserving follow-up tool execution.

Evidence

  • node_modules/.bin/oxfmt --write src/agents/embedded-agent-runner/run/message-tool-terminal.ts src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts
  • node_modules/.bin/oxlint src/agents/embedded-agent-runner/run/message-tool-terminal.ts src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts
  • git diff --check
  • OPENCLAW_HEAVY_CHECK_LOCK_SCOPE=worktree pnpm tsgo:core
  • OPENCLAW_HEAVY_CHECK_LOCK_SCOPE=worktree OPENCLAW_VITEST_FS_MODULE_CACHE_PATH=.vitest-cache-96827 OPENCLAW_VITEST_NO_OUTPUT_TIMEOUT_MS=300000 node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts packages/agent-core/src/agent-loop.test.ts -- --reporter=verbose - 2 shards passed; message-tool-terminal.test.ts 15 tests passed; agent-loop.test.ts 22 tests passed.
  • OPENCLAW_HEAVY_CHECK_LOCK_SCOPE=worktree OPENCLAW_VITEST_FS_MODULE_CACHE_PATH=.vitest-cache-96827-acceptance OPENCLAW_VITEST_NO_OUTPUT_TIMEOUT_MS=300000 node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/message-tool-terminal.test.ts packages/agent-core/src/agent-loop.test.ts src/agents/embedded-agent-subscribe.handlers.tools.test.ts src/agents/embedded-agent-subscribe.handlers.messages.test.ts extensions/telegram/src/bot-message-dispatch.test.ts -- --reporter=verbose - 3 shards passed; Telegram dispatch file reported 171 tests passed.
  • .agents/skills/autoreview/scripts/autoreview --mode local - clean, no accepted/actionable findings; overall patch correct.
  • Source API QA run against qa-channel + qa-lab bus + real gateway child + mock-openai provider:
    • command shape: OPENCLAW_BUILD_PRIVATE_QA=1 OPENCLAW_ENABLE_PRIVATE_QA_CLI=1 OPENCLAW_DISABLE_BUNDLED_PLUGINS=0 OPENCLAW_QA_SUITE_PROGRESS=1 node_modules/.bin/tsx --tsconfig tsconfig.json calling runQaSuite({ providerMode: "mock-openai", scenarioIds: ["group-visible-reply-tool"], concurrency: 1, fastMode: true, outputDir: ".artifacts/qa-e2e/source-api-group-visible-reply-tool" })
    • report: .artifacts/qa-e2e/source-api-group-visible-reply-tool/qa-suite-report.md
    • summary: .artifacts/qa-e2e/source-api-group-visible-reply-tool/qa-suite-summary.json
    • result: passed 1, failed 0; step details group:qa-visible-tool-room:QA-GROUP-TOOL-OK
    • scenario assertion in qa/scenarios/channels/group-visible-reply-tool.yaml verifies the mock provider planned message(action=send) and that the same group transcript contains exactly one outbound visible reply with QA-GROUP-TOOL-OK.

Redacted local message_tool_only terminal transcript from a temporary tsx harness that directly installed installMessageToolOnlyTerminalHook around a message tool:

{
  "mode": "message_tool_only",
  "cascadeGuard": {
    "visibleSends": [
      "first visible source reply"
    ],
    "visibleSendCount": 1,
    "duplicateSuppressed": true,
    "laterSuppressedAfterDelivery": true
  },
  "hookFailureRetry": {
    "visibleSends": [
      "hook-failed source reply",
      "successful retry source reply"
    ],
    "visibleSendCount": 2,
    "retryWasSent": true
  }
}

Additional follow-up-tool proof: message-tool-terminal.test.ts now includes keeps non-message follow-up tools available after source reply delivery, which fetches the read tool from agent.state.tools after afterToolCall records source delivery and verifies it still executes. agent-loop.test.ts also keeps the core loop contract covered with continues after a side-effect tool result when afterToolCall records it without terminate.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M labels Jul 8, 2026
@clawsweeper

clawsweeper Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 8, 2026, 10:19 AM ET / 14:19 UTC.

Summary
The PR wraps message_tool_only message tools to reserve one implicit current-source reply slot per run, suppress duplicate implicit sends, and adds focused terminal-hook tests.

PR surface: Source +125, Tests +376. Total +501 across 2 files.

Reproducibility: yes. at source level. Current main records delivered message_tool_only source replies without a terminate hint, the agent loop continues when results do not terminate, and the linked issue provides Telegram cascade logs.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: serialized state: src/agents/embedded-agent-runner/run/message-tool-terminal.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #96827
Summary: This PR is the current candidate fix for the canonical repeated message_tool_only self-reply issue and partially overlaps the earlier continuation PR that established the opposite no-terminate invariant.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🐚 platinum hermit
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • Wait for the failing compact shard and pending Telegram QA smoke to resolve or be triaged on this head.
  • [P2] Capture a Telegram Desktop proof if maintainers want visible-channel confirmation before merge.

Mantis proof suggestion
A Telegram Desktop proof would materially show the visible group-chat behavior, and the previous Mantis workflow was skipped rather than producing proof. A maintainer can ask Mantis to capture proof by posting this exact PR comment:

@openclaw-mantis telegram desktop proof: verify that a message_tool_only group-chat run sends one visible source reply, suppresses repeated self-replies, and still allows non-message follow-up tools.

Risk before merge

  • [P1] This changes the message_tool_only delivery invariant: a false positive implicit-source classification could suppress a legitimate second current-source message, while a false negative could leave the self-reply cascade in place.
  • [P1] Native Telegram proof was requested but the Mantis workflow skipped; the PR has source API/log proof, but a Telegram Desktop transcript would reduce visible-channel risk.
  • [P1] At check time, one exact-head CI shard was failing and Telegram QA smoke was still pending, so landing should wait for CI completion or maintainer triage of unrelated failure.

Maintainer options:

  1. Land after owner review and exact-head gates (recommended)
    Accept the bounded message-delivery change once the remaining CI and Telegram smoke state is green or explicitly triaged as unrelated.
  2. Request native Telegram proof first
    Ask for a redacted Telegram Desktop recording showing one visible group reply, no repeated self-replies, and continued non-message tool execution before accepting the delivery risk.

Next step before merge

  • No automated repair lane is identified; the PR has no blocking code finding and needs normal maintainer review, exact-head CI, and optional channel proof.

Security
Cleared: The diff changes only agent runtime TypeScript and tests, with no dependency, workflow, permission, credential, package-resolution, or supply-chain changes.

Review details

Best possible solution:

Land the targeted source-reply slot guard once exact-head CI and agent/channel owner review confirm it preserves follow-up tools while suppressing only duplicate implicit current-source sends.

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

Yes, at source level. Current main records delivered message_tool_only source replies without a terminate hint, the agent loop continues when results do not terminate, and the linked issue provides Telegram cascade logs.

Is this the best way to solve the issue?

Yes. The PR avoids the one-line terminate: true revert that would regress Slack follow-up tools, and instead adds the guard at the existing message-tool terminal hook while keeping explicit routed sends and non-message tools available.

AGENTS.md: found and applied where relevant.

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

Label changes

Label changes:

  • add merge-risk: 🚨 message-delivery: Merging changes when message_tool_only sends are delivered or suppressed, so incorrect classification could hide a valid message or fail to stop duplicates.
  • add proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes after-fix source API QA through qa-channel/qa-lab with a real gateway child plus redacted terminal output showing one visible reply, duplicate suppression, and retry behavior.
  • 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 includes after-fix source API QA through qa-channel/qa-lab with a real gateway child plus redacted terminal output showing one visible reply, duplicate suppression, and retry behavior.
  • add mantis: telegram-visible-proof: Mantis should capture Telegram visible proof. The generic runtime change affects visible Telegram group reply behavior and can be demonstrated in a short Telegram Desktop recording.

Label justifications:

  • P1: The PR targets a recent user-visible channel regression where a group chat can receive repeated agent self-replies within one run.
  • merge-risk: 🚨 message-delivery: Merging changes when message_tool_only sends are delivered or suppressed, so incorrect classification could hide a valid message or fail to stop duplicates.
  • 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 includes after-fix source API QA through qa-channel/qa-lab with a real gateway child plus redacted terminal output showing one visible reply, duplicate suppression, and retry behavior.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes after-fix source API QA through qa-channel/qa-lab with a real gateway child plus redacted terminal output showing one visible reply, duplicate suppression, and retry behavior.
  • mantis: telegram-visible-proof: Mantis should capture Telegram visible proof. The generic runtime change affects visible Telegram group reply behavior and can be demonstrated in a short Telegram Desktop recording.
Evidence reviewed

PR surface:

Source +125, Tests +376. Total +501 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 132 7 +125
Tests 1 380 4 +376
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 512 11 +501

What I checked:

Likely related people:

  • joshavant: Authored and merged the related continuation PR that changed the hook from terminating after a source reply to returning hookResult, creating the current invariant this PR narrows. (role: recent feature contributor; confidence: high; commits: 462092936a5c, bc3281644547; files: src/agents/embedded-agent-runner/run/message-tool-terminal.ts, packages/agent-core/src/agent-loop.test.ts)
  • mcaxtr: Authored the merged delivery-counting change that added onDeliveredSourceReply into this terminal-hook path while the earlier terminating behavior still existed. (role: introduced earlier delivery evidence behavior; confidence: medium; commits: 88dc177afc29, 20874c18f06c; files: src/agents/embedded-agent-runner/run/message-tool-terminal.ts)
  • vincentkoc: Current-main blame on the hook body points at a recent refactor touching this file, though the semantic history comes from the earlier delivery and continuation PRs. (role: recent adjacent contributor; confidence: low; commits: 99b24e741609, 0070371df875; files: src/agents/embedded-agent-runner/run/message-tool-terminal.ts)
  • steipete: Recent adjacent work documented the runner message helper surface, which is relevant to deciding the intended message-tool terminal contract. (role: adjacent owner by documentation history; confidence: medium; commits: e95e51a24f48; files: src/agents/embedded-agent-runner/run/message-tool-terminal.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.
Review history (5 earlier review cycles)
  • reviewed 2026-07-08T12:55:43.098Z sha e477939 :: needs real behavior proof before merge. :: [P2] Account for afterToolCall before reserving the source-reply slot
  • reviewed 2026-07-08T13:05:01.154Z sha e477939 :: needs real behavior proof before merge. :: [P2] Account for afterToolCall before reserving the source-reply slot
  • reviewed 2026-07-08T13:33:34.603Z sha aca795a :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-08T13:40:19.444Z sha aca795a :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-08T13:59:55.314Z sha 05d89d7 :: needs real behavior proof before merge. :: none

@849261680
849261680 force-pushed the fix/96827-message-tool-only-source-reply-loop branch from 9382a3b to e477939 Compare July 8, 2026 12:43
@849261680

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 8, 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 repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@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. mantis: telegram-visible-proof Mantis should capture Telegram visible proof. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. labels Jul 8, 2026
@849261680
849261680 force-pushed the fix/96827-message-tool-only-source-reply-loop branch from e477939 to aca795a Compare July 8, 2026 13:27
@849261680

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 8, 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 repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@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 8, 2026
@849261680
849261680 force-pushed the fix/96827-message-tool-only-source-reply-loop branch from aca795a to 05d89d7 Compare July 8, 2026 13:53
@849261680

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jul 8, 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 repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@849261680

Copy link
Copy Markdown
Contributor Author

@openclaw-mantis telegram desktop proof: verify that a message_tool_only group-chat run sends one visible source reply, suppresses repeated self-replies, and still allows non-message follow-up tools.

@849261680
849261680 force-pushed the fix/96827-message-tool-only-source-reply-loop branch from 05d89d7 to b084883 Compare July 8, 2026 14:09
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. and removed mantis: telegram-visible-proof Mantis should capture Telegram visible proof. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jul 8, 2026
@clawsweeper clawsweeper Bot added 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. mantis: telegram-visible-proof Mantis should capture Telegram visible proof. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. labels Jul 8, 2026
@Glucksberg

Glucksberg commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Thanks for this focused fix. I have production evidence from OpenClaw 2026.7.1-beta.2 on Telegram of an adjacent duplicate path: in three duplicate pairs, the first message(action=send) returned a gateway timeout after 30s; the gateway later recorded the first Telegram send as successful about 41–46s after initiation, and the model then issued a second, paraphrased send that also succeeded.

This PR correctly releases the reservation on definitive failures, but a client timeout is an ambiguous outcome: the server may still deliver after the caller gives up. In that case the reservation is released and a retry with changed text bypasses exact idempotency. I do not think that should block this PR's post-success cascade fix. Would you prefer a focused regression test/tri-state outcome here, or a separate transport-level PR that reconnects using the same idempotency key? Happy to prepare the latter and link it here; redacted timestamps are available.

@Glucksberg

Copy link
Copy Markdown
Contributor

Follow-up: I opened #104632 as the focused companion transport fix. It is intentionally not a change request for this PR. It reconnects only after a GatewayTransportError timeout with the same idempotency key and a bounded 60-second reconciliation window, so the Gateway can join the original in-flight delivery instead of returning an ambiguous failure to the model. The regression test also proves cancellation does not reconnect. This should cover the timeout -> late success -> paraphrased duplicate path while leaving your definitive-failure reservation behavior intact.

@Zjianru

Zjianru commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

+1 from another operator. Different channel but exact same symptom.

Channel: openclaw-weixin direct session (the bundled wechat plugin, not Telegram). OpenClaw 2026.7.1 (latest stable npm as of writing).

Reproduction trace from the affected session:

  • File: ~/.openclaw/agents/main/sessions/8cab40da-171e-46c7-b321-34e9b4ddd9b1.jsonl
  • Session key: agent:main:openclaw-weixin:direct:[email protected]
  • 1,628 literal "message":"test" sends to the same target in one conversation
  • Time window: 2026-07-10 13:57 → 23:07 Shanghai
  • Peak: 599 entries in a single hour (20:00 Shanghai), 437 the next hour
  • Each line is an assistant tool call message(action="send", target=openclaw-weixin, message="test") — no other tools interleaved

The session eventually drained on its own; the user had to manually intervene to stop the cascade.


Re @Glucksberg's 7/11 note about an adjacent duplicate path: my trace also contains provider:"openclaw", model:"delivery-mirror" side-branch entries that #100360 describes, so I now suspect the two share a code path in the embedded runner. Worth verifying when this lands.


Until this lands, our workaround is a hard rule on the agent side: no message(action=send) exploratory sends, and OPENCLAW_MESSAGE_DRYRUN=1 for any test. That is the wrong default for a public-facing channel — would love to see this in 2026.7.x stable so operators don't have to opt out of message_tool_only.

Two sanity questions on the slot reservation:

  1. Per-target, or per-channel+source? Some of our runs legitimately send to multiple distinct chat IDs in one run (different users); want to confirm suppression is per-source.
  2. Does delivery-mode=dry-run correctly stay outside the suppression path?

cc @clawsweeper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling mantis: telegram-visible-proof Mantis should capture Telegram visible proof. 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: L 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.

message_tool_only: agent does not terminate after delivering a source reply — cascade of self-replies within one run

3 participants