Conversation
The Weixin iLink bot only ever answered messages it received: the sole send path was the inbound forward (handle_incoming_message -> execute_forwarded_turn -> send_text). Assistant output produced by a turn the bot did not start - a scheduled job, the desktop window, another controller - was never delivered, so a scheduled job in the peer's bound session could run to completion while the user's phone stayed silent. Subscribe the bot to AgenticEvent::DialogTurnCompleted and deliver the turn's text to the peer bound to that session. A turn is skipped when this bot started it, because the inbound path already answered it and pushing it again would deliver every inbound message twice. Failed turns are skipped too; whether there is anything to send is decided by the turn's own text, so a degraded turn that still carries an explanation is not lost. The text is read through the existing replay-safe observe_turn projection, so bot delivery agrees with what the other remote surfaces show. Delivery respects the channel reply quota. The OpenBitFun product copy for this channel records that WeChat ClawBot allows the bot at most 10 replies in the 24 hours after the user sends a message, and that a split long reply spends one reply per part. That quota is also what answers the user's own messages, so this path: - merges a peer's whole backlog into a single reply instead of spending one reply per turn, keeps the turns in time order, and separates them readably; - caps the merged payload at one channel part, dropping the oldest output first and truncating the newest on a character boundary only when it alone exceeds the cap; - keeps its own share of 3 replies per rolling 24 hour window, which it can never overspend, leaving the rest of the channel quota untouched; - counts the replies a push will actually spend - the parts the channel splits it into - and refuses to send when the share cannot cover them, holding the backlog until the window rolls instead; - still queues when no context_token is available and keeps the backlog when a send fails on a stale token, so output waits for the next inbound message rather than being dropped. The backlog is bounded per peer, by age, and by merged payload size. One worker per bot drains it, which preserves order and keeps two triggers from delivering the same queued message. The subscriber spawns its work instead of running it inline: the event router awaits every subscriber while this path reads the session and sends over the network. is_lifecycle_current() is checked on entry, after the session read, and in the worker loop, so a replaced bot cannot push, and the subscription id is unique per instance so a retired bot unsubscribes only its own hook. Reuses send_text / send_text_chunks and the existing context_tokens cache; no protocol change. weixin_reply_count() is added next to chunk_text_for_weixin so the predicted reply count cannot drift from what send_text_chunks sends. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
TunaTung
force-pushed
the
feat/weixin-proactive-push
branch
from
September 21, 2026 00:56
42a9b4d to
44c62d3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Weixin iLink bot only ever answered messages it received. The single send path was the inbound forward:
A turn the bot did not start — a scheduled job, the desktop window, another controller — produced assistant output that no code path delivered. A scheduled job in the peer's bound session ran to completion (
lastRunStatus: "ok") while the user's phone stayed silent, and the local relay showed the transcript fully drained, so the loss was not a transport failure.The channel constrains how much we may send
The OpenBitFun product copy for this channel records the restriction (i18n key
botWeixinRestriction):This is a product-level channel restriction, not a wire-protocol field:
Tencent/openclaw-weixindocuments only that the inboundcontext_tokenmust be passed back on a reply. The same quota answers the user's own messages, so a proactive push that spends it freely would break interactive replies. That constraint shapes the design below.Change
Subscribe the bot to
AgenticEvent::DialogTurnCompletedand deliver the completed turn's text to the peer bound to that session.A turn is skipped when this bot started it. The bot records the
turn_idit generates for an inbound forward (ForwardRequest::turn_id) before submitting, and the same id appears on completion, so the inbound path keeps owning its own reply and nothing is delivered twice. Failed turns are skipped as well; whether there is anything to send is decided by the turn's own text, so a degraded turn that still carries an explanation is not lost. The text comes from the existing replay-safeobserve_turnprojection, so bot delivery agrees with what the other remote surfaces show.Saving the reply quota
send_text, which still splits at one channel part.weixin_reply_count(), which is defined on top of the same splittersend_text_chunksuses so the count cannot drift from what is actually sent. A push that does not fit the share waits instead of overspending, and the backlog is held until the window rolls.context_token(remember_context_tokenwakes the worker as soon as the user next talks to the bot) and a send that fails on a stale token (send_textstill drops the stale token exactly as before, and the retry succeeds once a fresh one arrives).Bounds
Per peer: at most 20 queued entries, at most 1 hour old, merged payload capped at one channel part. One worker per bot drains the backlog, which preserves order and keeps two triggers from delivering the same queued message. The subscriber spawns its work instead of running it inline, because the event router awaits every subscriber while this path reads the session and sends over the network.
is_lifecycle_current()is checked on entry, after the session read, and in the worker loop, so a replaced bot cannot push; the subscription id is unique per instance, so a retired bot unsubscribes only its own hook.Before / after
context_tokencachedremember_context_tokennext firesScope
src/crates/assembly/core/src/service/remote_connect/bot/weixin.rs— the subscription, the merge, the quota accounting, the backlog.src/crates/services/services-integrations/src/remote_connect/bot/weixin.rs—MAX_TEXT_CHUNKbecomespubandweixin_reply_count()is added next to the existing splitter.Purely additive on the provider side:
send_text,send_text_chunksand thecontext_tokenscache are unchanged.Note for reviewers
Because the proactive push covers the whole bound session, turns the user drives from the desktop window are pushed too. That follows from "output of the session this peer is bound to", but it does change desktop behaviour, so binding the bot to a dedicated assistant session is recommended. A follow-up could gate this behind a per-connection setting.
The proactive share is self-imposed and does not observe what the inbound reply path already spent, which is exactly why it is small. If the platform rejects a push because the shared quota is exhausted, it surfaces as the existing
context_tokensend failure, and the content stays queued rather than being lost.The main runtime assumption worth a second pair of eyes:
observe_turnmust already see the turn's assistant message whenDialogTurnCompletedfires. The inbound path relies on the same ordering, and the projection is the one it uses, but this is the piece that decides whether the feature has anything to send.Testing
Unit tests cover the ownership/failure predicate, the pre-merge and retention bounds, merging (order, separator, blank entries), the byte cap (drop-oldest and single-oversized truncation, character boundary safety), the reply-share budget (sufficient, insufficient, exactly-spent, oversized single push) and the rolling window. Two tests in
services-integrationscover the reply-count prediction at the 4000/4001 byte boundary and the byte-vs-character semantics. The text projection reuses the existing testedobserve_turn.AI assistance and testing level
This change was authored with AI assistance and reviewed by a human before submission.
Testing level: not compiled or executed locally. The machine used to write this patch has no Rust toolchain and no MSVC linker available, so
cargo checkandcargo testcould not be run; the unit tests above have not been executed. Please treat the first CI run as the compile gate, and expect a possible format/borrow-check follow-up.One part of the premise was verified manually against the live channel: a direct
POST /ilink/bot/sendmessageusing the official wire shape ({"msg": {...}, "base_info": {...}}plus theiLink-App-Id/iLink-App-ClientVersionheaders) and a cachedcontext_tokenreturnedHTTP 200 {"message_id": ...}, confirming that a non-reply send is accepted inside an open window. That verifies the channel, not this code path. An end-to-end run of this patch on a bound session with a scheduled job has not been performed.