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:
+10
-2
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user