fix(wecom): degrade stream replies to proactive msg when callback req_id stale

Turns that outlive the WeCom callback req_id window (e.g. approval
round-trips >5min) had their delta/finish frames silently rejected by
the gateway, so the user saw 'clicked approve, nothing happened' while
the agent kept working. Track req_id recv time; past a 240s TTL, stream
updates fall back to proactive aibot_send_msg. Also log non-zero
errcode ack frames so future gateway rejections are visible.
This commit is contained in:
openbot committed 2026-10-24 09:18:19 +08:00
1 parent e07f37fbb4
commit 59c44e94ca
3 files changed
+55 -21

No files matched your search

+14 -2
View File
@@ -77,7 +77,7 @@ def test_wecom_dispatch_text_and_permission():
"from": {"userid": "u1"}, "text": {"content": "hello\u200b"}}}
ch._dispatch(frame, ws)
assert len(got) == 1 and got[0].text == "hello" and got[0].chat_id == "c1"
assert ch._req_ids["c1"] == "r1" # captured for the streaming reply
assert ch._req_ids["c1"][0] == "r1" # captured for the streaming reply
def test_wecom_allowed_users_drops_others():
@@ -91,7 +91,8 @@ def test_wecom_allowed_users_drops_others():
def test_wecom_streaming_delta_coalesce_and_final():
ch, ws = _make_wecom()
ch._ws = ws # pretend connected (stream sends go through the socket)
ch._req_ids["c1"] = "req-9" # as set by an inbound callback
ch._req_ids["c1"] = ("req-9", __import__("time").time()) # fresh inbound callback
ch._req_ids["stale"] = ("req-old", __import__("time").time() - 600) # expired callback
ch.push_event("c1", "delta", {"text": "hello "})
ch.push_event("c1", "delta", {"text": "world"})
ch.push_event("c1", "final", {"text": "hello world"})
@@ -171,3 +172,14 @@ def test_weixin_no_edit_deltas_dropped_final_sent():
assert not http.posts
ch.push_event("u", "final", {"text": "full text"})
assert http.posts[-1][1]["text"]["content"] == "full text"
def test_wecom_stream_falls_back_when_callback_req_stale():
ch, ws = _make_wecom()
ch._ws = ws
ch._req_ids["c1"] = ("req-old", __import__("time").time() - 600) # beyond TTL
ch.push_event("c1", "final", {"text": "late answer"})
respond = [m for m in ws.sent if m["cmd"] == "aibot_respond_msg"]
send = [m for m in ws.sent if m["cmd"] == "aibot_send_msg"]
assert respond == [] # stale req_id must NOT be reused
assert send and "late answer" in json.dumps(send[-1])
+2 -2
View File
@@ -37,7 +37,7 @@ def _channel() -> tuple[WecomAibotChannel, MockWS]:
def test_small_final_single_stream_frame():
ch, ws = _channel()
ch._req_ids["c1"] = "r1"
ch._req_ids["c1"] = ("r1", __import__("time").time())
ch._stream_close("c1", "hello")
streams = [m for m in ws.sent if m.get("cmd") == "aibot_respond_msg"]
assert len(streams) == 1
@@ -47,7 +47,7 @@ def test_small_final_single_stream_frame():
def test_large_final_splits_into_stream_plus_markdown():
ch, ws = _channel()
ch._req_ids["c1"] = "r1"
ch._req_ids["c1"] = ("r1", __import__("time").time())
big = "字" * 12000 # 3 bytes/char -> 36000 bytes > 19000 limit
ch._stream_close("c1", big)
streams = [m for m in ws.sent if m.get("cmd") == "aibot_respond_msg"]