Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(hetero): keep Claude Code stdin open through the turn a background task's end wakes (#22070)

AmAzing-d0eb35810799
diff --git a/packages/heterogeneous-agents/src/spawn/claudeCodeLiveInput.test.ts b/packages/heterogeneous-agents/src/spawn/claudeCodeLiveInput.test.ts
index 1b174d0d5b3..ef4c4bb9024 100644
--- a/packages/heterogeneous-agents/src/spawn/claudeCodeLiveInput.test.ts
+++ b/packages/heterogeneous-agents/src/spawn/claudeCodeLiveInput.test.ts
@@ -57,7 +57,7 @@ describe('ClaudeCodeLiveInput', () => {
expect(ended).toHaveBeenCalled();
});
- it('keeps stdin open past the result while a background task runs', () => {
+ it('keeps stdin open past the result while a background task runs, until the turn its end wakes', () => {
const { live } = setup();
live.send(userLine('first'), 'u-1');
@@ -66,8 +66,51 @@ describe('ClaudeCodeLiveInput', () => {
live.observe(line({ subtype: 'success', type: 'result' }));
expect(live.isOpen).toBe(true);
- // Print mode still runs the notification turn after stdin closes.
live.observe(line({ subtype: 'task_notification', task_id: 't-1', type: 'system' }));
+ expect(live.isOpen).toBe(true);
+
+ live.observe(line({ subtype: 'init', type: 'system' }));
+ live.observe(line({ subtype: 'success', type: 'result' }));
+ expect(live.isOpen).toBe(false);
+ });
+
+ it('keeps stdin open for a task the wake-up turn starts or resumes', () => {
+ const { live } = setup();
+ live.send(userLine('first'), 'u-1');
+ live.observe(line({ isReplay: true, type: 'user', uuid: 'u-1' }));
+ live.observe(line({ subtype: 'task_started', task_id: 'runner', type: 'system' }));
+ live.observe(line({ subtype: 'success', type: 'result' }));
+
+ // The runner returns between turns; Claude Code reports both events.
+ live.observe(
+ line({
+ patch: { status: 'completed' },
+ subtype: 'task_updated',
+ task_id: 'runner',
+ type: 'system',
+ }),
+ );
+ live.observe(line({ subtype: 'task_notification', task_id: 'runner', type: 'system' }));
+ expect(live.isOpen).toBe(true);
+
+ // The wake-up turn starts a checker, ends, and the checker returns.
+ live.observe(line({ subtype: 'init', type: 'system' }));
+ live.observe(line({ type: 'assistant' }));
+ live.observe(line({ subtype: 'task_started', task_id: 'checker', type: 'system' }));
+ live.observe(line({ subtype: 'success', type: 'result' }));
+ live.observe(line({ subtype: 'task_notification', task_id: 'checker', type: 'system' }));
+ expect(live.isOpen).toBe(true);
+
+ // That wake-up turn resumes the runner with SendMessage (same task id).
+ live.observe(line({ subtype: 'init', type: 'system' }));
+ live.observe(line({ type: 'assistant' }));
+ live.observe(line({ subtype: 'task_started', task_id: 'runner', type: 'system' }));
+ live.observe(line({ subtype: 'success', type: 'result' }));
+ expect(live.isOpen).toBe(true);
+
+ live.observe(line({ subtype: 'task_notification', task_id: 'runner', type: 'system' }));
+ live.observe(line({ subtype: 'init', type: 'system' }));
+ live.observe(line({ subtype: 'success', type: 'result' }));
expect(live.isOpen).toBe(false);
});
@@ -84,8 +127,9 @@ describe('ClaudeCodeLiveInput', () => {
expect(live.isOpen).toBe(false);
});
- it('closes when the last background task ends between turns', () => {
- const { live } = setup();
+ it('closes when the last task ends between turns and no turn wakes up', () => {
+ vi.useFakeTimers();
+ const { live } = setup({ wakeUpGraceMs: 1000 });
live.send(userLine('first'), 'u-1');
live.observe(line({ isReplay: true, type: 'user', uuid: 'u-1' }));
live.observe(line({ subtype: 'task_started', task_id: 't-1', type: 'system' }));
@@ -100,6 +144,9 @@ describe('ClaudeCodeLiveInput', () => {
}),
);
+ vi.advanceTimersByTime(999);
+ expect(live.isOpen).toBe(true);
+ vi.advanceTimersByTime(1);
expect(live.isOpen).toBe(false);
});
@@ -180,7 +227,7 @@ describe('ClaudeCodeLiveInput', () => {
it('treats sub-agent output as background activity that keeps the idle timer fresh', () => {
vi.useFakeTimers();
- const { live } = setup({ idleCloseMs: 1000 });
+ const { live } = setup({ idleCloseMs: 1000, wakeUpGraceMs: 100 });
live.send(userLine('first'), 'u-1');
live.observe(line({ isReplay: true, type: 'user', uuid: 'u-1' }));
live.observe(line({ subtype: 'task_started', task_id: 't-1', type: 'system' }));
@@ -203,8 +250,10 @@ describe('ClaudeCodeLiveInput', () => {
live.observe(line({ subtype: 'task_notification', task_id: 't-2', type: 'system' }));
expect(live.isOpen).toBe(true);
- // The sub-agent opened no main turn, so its end leaves the session idle.
+ // The sub-agent opened no main turn, so once its end wakes none, the
+ // session is idle.
live.observe(line({ subtype: 'task_notification', task_id: 't-1', type: 'system' }));
+ vi.advanceTimersByTime(100);
expect(live.isOpen).toBe(false);
});
diff --git a/packages/heterogeneous-agents/src/spawn/claudeCodeLiveInput.ts b/packages/heterogeneous-agents/src/spawn/claudeCodeLiveInput.ts
index 217be8a7da4..1fda2cdbe6f 100644
--- a/packages/heterogeneous-agents/src/spawn/claudeCodeLiveInput.ts
+++ b/packages/heterogeneous-agents/src/spawn/claudeCodeLiveInput.ts
@@ -10,6 +10,14 @@ import type { Writable } from 'node:stream';
*/
const DEFAULT_IDLE_CLOSE_MS = 30 * 60 * 1000;
+/**
+ * How long a task that ends between turns may wait for the main-agent turn its
+ * notification wakes. Claude Code opens that turn within milliseconds; the
+ * grace only bounds an end that wakes nothing, such as a task killed between
+ * turns.
+ */
+const DEFAULT_WAKE_UP_GRACE_MS = 30 * 1000;
+
const TERMINAL_TASK_STATUSES = new Set(['completed', 'failed', 'killed']);
const readIdleCloseMs = (): number => {
@@ -33,6 +41,7 @@ const isAbortedResult = (message: any): boolean =>
export interface ClaudeCodeLiveInputOptions {
idleCloseMs?: number;
+ wakeUpGraceMs?: number;
}
/**
@@ -44,8 +53,13 @@ export interface ClaudeCodeLiveInputOptions {
* waiting for background tasks after `CLAUDE_CODE_PRINT_BG_WAIT_CEILING_MS`
* (10 minutes by default) and kills them. So stdin stays open until the session
* is between turns with no running task and no delivered message it has not
- * picked up yet. A notification for a task that finished during the last turn
- * is still delivered after the close, because print mode drains those itself.
+ * picked up yet.
+ *
+ * A task that ends between turns does not make the session idle: its
+ * notification wakes the main agent for a turn, and that turn can start a new
+ * task or resume a finished sub-agent with `SendMessage`. Closing at the end
+ * would put those tasks under the ceiling, so the decision waits for the
+ * wake-up turn's result.
*
* Requires `--replay-user-messages`: the replayed `uuid` is how a delivered
* message is known to have been picked up.
@@ -59,12 +73,15 @@ export class ClaudeCodeLiveInput {
private lineBuffer = '';
private turnOpen = true;
private readonly unconsumed = new Set<string>();
+ private readonly wakeUpGraceMs: number;
+ private wakeUpTimer: NodeJS.Timeout | undefined;
constructor(
private readonly stdin: Writable,
options: ClaudeCodeLiveInputOptions = {},
) {
this.idleCloseMs = options.idleCloseMs ?? readIdleCloseMs();
+ this.wakeUpGraceMs = options.wakeUpGraceMs ?? DEFAULT_WAKE_UP_GRACE_MS;
stdin.on('error', () => this.markClosed());
stdin.on('close', () => this.markClosed());
}
@@ -201,7 +218,7 @@ export class ClaudeCodeLiveInput {
TERMINAL_TASK_STATUSES.has(message.patch?.status as string));
if (ended && message.task_id) {
this.activeTasks.delete(message.task_id);
- this.closeIfIdle();
+ this.awaitWakeUpTurn();
}
}
@@ -209,6 +226,7 @@ export class ClaudeCodeLiveInput {
private openTurn(): void {
this.turnOpen = true;
this.lastOutcome = undefined;
+ this.clearWakeUpTimer();
}
private closeIfIdle(): void {
@@ -216,6 +234,26 @@ export class ClaudeCodeLiveInput {
this.close();
}
+ /**
+ * The last task ended between turns. The turn its notification wakes decides
+ * at its result; close here only if no turn opens within the grace.
+ */
+ private awaitWakeUpTurn(): void {
+ if (this.closed) return;
+ if (this.turnOpen || this.activeTasks.size > 0 || this.unconsumed.size > 0) return;
+
+ this.clearWakeUpTimer();
+ this.wakeUpTimer = setTimeout(() => this.closeIfIdle(), this.wakeUpGraceMs);
+ this.wakeUpTimer.unref?.();
+ }
+
+ private clearWakeUpTimer(): void {
+ if (!this.wakeUpTimer) return;
+
+ clearTimeout(this.wakeUpTimer);
+ this.wakeUpTimer = undefined;
+ }
+
private armIdleTimer(): void {
this.clearIdleTimer();
this.idleTimer = setTimeout(() => this.close(), this.idleCloseMs);
@@ -232,5 +270,6 @@ export class ClaudeCodeLiveInput {
private markClosed(): void {
this.closed = true;
this.clearIdleTimer();
+ this.clearWakeUpTimer();
}
}