fix: auto-recover WeChat agent sessions poisoned by dangling tool_calls

When a dedicated WeChat session history leaves unmatched tool_calls, the next reply fails with a 400 LLM validation error; detect that pattern and recreate the session before retrying.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-07-04 09:53:59 +08:00
parent 42baa3e468
commit 7b09abeaa2
2 changed files with 188 additions and 2 deletions
+10 -2
View File
@@ -950,6 +950,15 @@ function isTopicResetIntent(text) {
);
}
export function isRecoverableWechatAgentSessionError(message) {
const normalized = String(message ?? '').trim();
if (!normalized) return false;
if (/stale_session_poisoned_completion/i.test(normalized)) return true;
if (/403|404|not found|无权访问/i.test(normalized)) return true;
if (/tool_calls|tool_call_id|insufficient tool messages/i.test(normalized)) return true;
return false;
}
function normalizeNumber(value) {
if (value === '' || value === null || value === undefined) return null;
const num = Number(value);
@@ -1932,8 +1941,7 @@ export function createWechatMpService({
return { sessionId };
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
const mayBeStaleSession =
/403|404|not found|无权访问|session|stale_session_poisoned_completion/i.test(message) && sessionId;
const mayBeStaleSession = sessionId && isRecoverableWechatAgentSessionError(message);
if (mayBeStaleSession) {
sessionId = await ensureWechatAgentSession({
userId: user.userId,