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

@@ -17,6 +17,7 @@ const BASE_RETRY_MS = 5000;
interface GroupState {
active: boolean;
idleWaiting: boolean;
isTaskContainer: boolean;
pendingMessages: boolean;
pendingTasks: QueuedTask[];
process: ChildProcess | null;
@@ -39,6 +40,7 @@ export class GroupQueue {
state = {
active: false,
idleWaiting: false,
isTaskContainer: false,
pendingMessages: false,
pendingTasks: [],
process: null,
@@ -142,7 +144,8 @@ export class GroupQueue {
*/
sendMessage(groupJid: string, text: string): boolean {
const state = this.getGroup(groupJid);
if (!state.active || !state.groupFolder) return false;
if (!state.active || !state.groupFolder || state.isTaskContainer) return false;
state.idleWaiting = false; // Agent is about to receive work, no longer idle
const inputDir = path.join(DATA_DIR, 'ipc', state.groupFolder, 'input');
try {
@@ -181,6 +184,7 @@ export class GroupQueue {
const state = this.getGroup(groupJid);
state.active = true;
state.idleWaiting = false;
state.isTaskContainer = false;
state.pendingMessages = false;
this.activeCount++;
@@ -215,6 +219,7 @@ export class GroupQueue {
const state = this.getGroup(groupJid);
state.active = true;
state.idleWaiting = false;
state.isTaskContainer = true;
this.activeCount++;
logger.debug(
@@ -228,6 +233,7 @@ export class GroupQueue {
logger.error({ groupJid, taskId: task.id, err }, 'Error running task');
} finally {
state.active = false;
state.isTaskContainer = false;
state.process = null;
state.containerName = null;
state.groupFolder = null;