fix: resolve session race condition and follow-up turn output loss
Two bugs fixed: 1. agent-runner error path race condition: when agent process exited with error, the promise resolved immediately without waiting for outputChain, allowing a late wrappedOnOutput to re-persist a stale session ID after clearSession had already run. 2. follow-up turn state not resetting: when a follow-up IPC message joined an active run, turn-level flags (finalOutputSentToUser, sawNonProgressOutput) from the first turn prevented the last progress from being promoted to a final message. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
67
src/index.ts
67
src/index.ts
@@ -298,6 +298,8 @@ async function processGroupMessages(chatJid: string): Promise<boolean> {
|
||||
let progressTicker: ReturnType<typeof setInterval> | null = null;
|
||||
let finalOutputSentToUser = false;
|
||||
let progressOutputSentToUser = false;
|
||||
let latestModelProgressTextForFinalFallback: string | null = null;
|
||||
let sawNonProgressOutput = false;
|
||||
let poisonedSessionDetected = false;
|
||||
const isClaudeCodeAgent =
|
||||
(group.agentType || 'claude-code') === 'claude-code';
|
||||
@@ -309,6 +311,14 @@ async function processGroupMessages(chatJid: string): Promise<boolean> {
|
||||
}
|
||||
};
|
||||
|
||||
const resetProgressState = () => {
|
||||
clearProgressTicker();
|
||||
latestProgressText = null;
|
||||
latestProgressRendered = null;
|
||||
progressMessageId = null;
|
||||
progressStartedAt = null;
|
||||
};
|
||||
|
||||
const renderProgressMessage = (text: string) => {
|
||||
const elapsedSeconds =
|
||||
progressStartedAt === null
|
||||
@@ -356,8 +366,12 @@ async function processGroupMessages(chatJid: string): Promise<boolean> {
|
||||
};
|
||||
|
||||
const finalizeProgressMessage = async () => {
|
||||
logger.info(
|
||||
{ group: group.name, chatJid, progressMessageId, latestProgressText },
|
||||
'Finalizing tracked progress message',
|
||||
);
|
||||
await syncTrackedProgressMessage();
|
||||
clearProgressTicker();
|
||||
resetProgressState();
|
||||
};
|
||||
|
||||
const sendProgressMessage = async (text: string) => {
|
||||
@@ -365,6 +379,7 @@ async function processGroupMessages(chatJid: string): Promise<boolean> {
|
||||
return;
|
||||
}
|
||||
|
||||
latestModelProgressTextForFinalFallback = text;
|
||||
latestProgressText = text;
|
||||
if (progressStartedAt === null) {
|
||||
progressStartedAt = Date.now();
|
||||
@@ -372,6 +387,10 @@ async function processGroupMessages(chatJid: string): Promise<boolean> {
|
||||
const rendered = renderProgressMessage(text);
|
||||
|
||||
if (progressMessageId && channel.editMessage) {
|
||||
logger.info(
|
||||
{ group: group.name, chatJid, progressMessageId, text },
|
||||
'Updating tracked progress message',
|
||||
);
|
||||
await syncTrackedProgressMessage();
|
||||
progressOutputSentToUser = true;
|
||||
return;
|
||||
@@ -383,6 +402,10 @@ async function processGroupMessages(chatJid: string): Promise<boolean> {
|
||||
|
||||
progressMessageId = await channel.sendAndTrack(chatJid, rendered);
|
||||
if (progressMessageId) {
|
||||
logger.info(
|
||||
{ group: group.name, chatJid, progressMessageId, text },
|
||||
'Created tracked progress message',
|
||||
);
|
||||
latestProgressRendered = rendered;
|
||||
ensureProgressTicker();
|
||||
progressOutputSentToUser = true;
|
||||
@@ -413,17 +436,41 @@ async function processGroupMessages(chatJid: string): Promise<boolean> {
|
||||
? result.result
|
||||
: JSON.stringify(result.result);
|
||||
const text = formatOutbound(raw);
|
||||
logger.info({ group: group.name }, `Agent output: ${raw.slice(0, 200)}`);
|
||||
logger.info(
|
||||
{
|
||||
group: group.name,
|
||||
chatJid,
|
||||
resultStatus: result.status,
|
||||
resultPhase: result.phase,
|
||||
progressMessageId,
|
||||
},
|
||||
`Agent output: ${raw.slice(0, 200)}`,
|
||||
);
|
||||
if (result.phase === 'progress') {
|
||||
if (text) {
|
||||
await sendProgressMessage(text);
|
||||
}
|
||||
return;
|
||||
}
|
||||
sawNonProgressOutput = true;
|
||||
if (text) {
|
||||
await finalizeProgressMessage();
|
||||
await channel.sendMessage(chatJid, text);
|
||||
finalOutputSentToUser = true;
|
||||
latestModelProgressTextForFinalFallback = null;
|
||||
} else {
|
||||
logger.info(
|
||||
{
|
||||
group: group.name,
|
||||
chatJid,
|
||||
resultStatus: result.status,
|
||||
resultPhase: result.phase,
|
||||
progressMessageId,
|
||||
},
|
||||
'Agent output became empty after formatting; resetting tracked progress state',
|
||||
);
|
||||
await finalizeProgressMessage();
|
||||
latestModelProgressTextForFinalFallback = null;
|
||||
}
|
||||
} else {
|
||||
await finalizeProgressMessage();
|
||||
@@ -450,6 +497,22 @@ async function processGroupMessages(chatJid: string): Promise<boolean> {
|
||||
hadError = true;
|
||||
}
|
||||
|
||||
if (
|
||||
output === 'success' &&
|
||||
!hadError &&
|
||||
!finalOutputSentToUser &&
|
||||
!sawNonProgressOutput &&
|
||||
latestModelProgressTextForFinalFallback
|
||||
) {
|
||||
logger.info(
|
||||
{ group: group.name, chatJid },
|
||||
'Promoting last progress output to final message after agent completion',
|
||||
);
|
||||
await channel.sendMessage(chatJid, latestModelProgressTextForFinalFallback);
|
||||
finalOutputSentToUser = true;
|
||||
latestModelProgressTextForFinalFallback = null;
|
||||
}
|
||||
|
||||
clearProgressTicker();
|
||||
|
||||
if (idleTimer) clearTimeout(idleTimer);
|
||||
|
||||
Reference in New Issue
Block a user