Skip to content

fix(wechat): suppress buffered output after /stop - #748

Open
imMamdouhaboammar wants to merge 1 commit into
lsdefine:mainfrom
imMamdouhaboammar:fix/wechat-stop-suppresses-output-upstream
Open

fix(wechat): suppress buffered output after /stop#748
imMamdouhaboammar wants to merge 1 commit into
lsdefine:mainfrom
imMamdouhaboammar:fix/wechat-stop-suppresses-output-upstream

Conversation

@imMamdouhaboammar

Copy link
Copy Markdown

Problem

Upstream issue #396 reports that WeChat /stop aborts the agent but the handler keeps draining buffered turn output. Current main already labels the final state [已停止], but it still sends buffered text after the stop flag and can send files referenced by the cancelled result.

Root cause

on_message() used a per-user abort flag, but _handle() did not observe it until after its display-queue loop. That allowed buffered turn text and cancelled-result files to escape after /stop. Review also exposed two edge cases: an idle /stop could leave a stale flag for the next task, and setting the flag after agent.abort() left a small race where the worker could consume done before the abort became visible.

Fix

  • only arm the per-user abort flag when the agent is actually running; clear stale flags for idle /stop
  • arm _task_aborted[uid] before calling agent.abort()
  • refuse incremental _send() calls once the abort flag is set
  • stop draining the display queue as soon as an abort is observed
  • after sending one [已停止] acknowledgement, return immediately so cancelled result files are not delivered

The task queue, WeChat protocol, authentication, and agent abort implementation are unchanged.

Regression coverage

tests/test_wechat_stop.py uses a fake agent and fake bot, with no WeChat network/login or LLM calls. Import stubs exist only during wechatapp.py import and sys.path/HOME are restored immediately. It covers:

  1. buffered turn output queued after /stop is not sent
  2. a real file referenced by the cancelled done result is not sent
  3. /stop while idle does not cancel the next task
  4. the abort flag is already armed at the instant agent.abort() runs

TDD verification

Original issue #396 validation:

  • RED 31165947230: compile/import passed; both post-stop regressions failed on current main
  • GREEN 31166076487: minimal fix passed compile, both regressions, and git diff --check

Review finding, stale idle stop:

  • RED 31166842462: the two original tests passed; only the idle-stop regression failed
  • GREEN 31166942104: all three tests, compile, and git diff --check passed

Abort ordering race:

  • RED 31167274233: three existing tests passed; ordering test observed the flag as False inside agent.abort()
  • GREEN 31167408972: all four tests, compile, and git diff --check passed after arming the flag first

The final PR commit is rebuilt directly from main using the exact production/test blobs from the last GREEN validation, so the contribution remains one commit and two changed files.

Scope

  • frontends/wechatapp.py: focused /stop handling changes only
  • one focused unittest file
  • no new runtime dependencies

Fixes #396

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.

Bug: WeChat clawbot 中使用 /stop 命令无法真正停止任务,仍继续发送结果

1 participant