fix: correctly trigger idle preemption in streaming input mode

The original notifyIdle condition (!result.result) never fired in
streaming input mode because every result has non-null text content.
This caused due tasks to wait up to 30 minutes for the idle timer.

- Call notifyIdle for ALL successful results (not just null ones)
- Add isTaskContainer flag so user messages queue instead of being
  forwarded to task containers (which blocked notifyIdle from the
  message container's onOutput path)
- Reset idleWaiting in sendMessage so containers aren't preempted
  while actively working on a new incoming message
- Replace 30-min IDLE_TIMEOUT with 10s close timer for task containers
  since they are single-turn and should exit promptly after their result

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Gavriel Cohen
2026-02-21 22:11:42 +02:00
committed by gavrielc
parent 93bb94ff55
commit c6b69e87a9
3 changed files with 22 additions and 15 deletions

View File

@@ -89,16 +89,18 @@ async function runTask(
const sessionId =
task.context_mode === 'group' ? sessions[task.group_folder] : undefined;
// Idle timer: writes _close sentinel after IDLE_TIMEOUT of no output,
// so the container exits instead of hanging at waitForIpcMessage forever.
let idleTimer: ReturnType<typeof setTimeout> | null = null;
// After the task produces a result, close the container promptly.
// Tasks are single-turn — no need to wait IDLE_TIMEOUT (30 min) for the
// query loop to time out. A short delay handles any final MCP calls.
const TASK_CLOSE_DELAY_MS = 10000;
let closeTimer: ReturnType<typeof setTimeout> | null = null;
const resetIdleTimer = () => {
if (idleTimer) clearTimeout(idleTimer);
idleTimer = setTimeout(() => {
logger.debug({ taskId: task.id }, 'Scheduled task idle timeout, closing container stdin');
const scheduleClose = () => {
if (closeTimer) return; // already scheduled
closeTimer = setTimeout(() => {
logger.debug({ taskId: task.id }, 'Closing task container after result');
deps.queue.closeStdin(task.chat_jid);
}, IDLE_TIMEOUT);
}, TASK_CLOSE_DELAY_MS);
};
try {
@@ -118,10 +120,9 @@ async function runTask(
result = streamedOutput.result;
// Forward result to user (sendMessage handles formatting)
await deps.sendMessage(task.chat_jid, streamedOutput.result);
// Only reset idle timer on actual results, not session-update markers
resetIdleTimer();
scheduleClose();
}
if (!streamedOutput.result && streamedOutput.status === 'success') {
if (streamedOutput.status === 'success') {
deps.queue.notifyIdle(task.chat_jid);
}
if (streamedOutput.status === 'error') {
@@ -130,7 +131,7 @@ async function runTask(
},
);
if (idleTimer) clearTimeout(idleTimer);
if (closeTimer) clearTimeout(closeTimer);
if (output.status === 'error') {
error = output.error || 'Unknown error';
@@ -144,7 +145,7 @@ async function runTask(
'Task completed',
);
} catch (err) {
if (idleTimer) clearTimeout(idleTimer);
if (closeTimer) clearTimeout(closeTimer);
error = err instanceof Error ? err.message : String(err);
logger.error({ taskId: task.id, error }, 'Task failed');
}