perf(delegate-task): skip terminal poll waits

This commit is contained in:
YeonGyu-Kim
2026-05-28 18:41:50 +09:00
parent 2327eba341
commit 8644817506
2 changed files with 75 additions and 2 deletions
@@ -0,0 +1,67 @@
/// <reference types="bun-types" />
import { describe, expect, test } from "bun:test"
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
import { executeUnstableAgentTask } from "./unstable-agent-task"
import type { DelegateTaskArgs } from "./types"
const args: DelegateTaskArgs = {
description: "terminal task",
prompt: "do it",
category: "quick",
load_skills: [],
run_in_background: false,
}
describe("executeUnstableAgentTask terminal fast path", () => {
test("#given launched task is already interrupted #when monitoring starts #then it returns without waiting for the poll interval", async () => {
// given
const startedAt = Date.now()
// when
const output = await executeUnstableAgentTask(
args,
{
sessionID: "ses_parent",
messageID: "msg_parent",
agent: "sisyphus",
abort: new AbortController().signal,
},
unsafeTestValue({
manager: {
launch: async () => ({
id: "bg_terminal",
sessionId: "ses_terminal",
description: "terminal task",
agent: "sisyphus-junior",
status: "interrupt",
error: "already stopped",
}),
getTask: () => ({
id: "bg_terminal",
sessionId: "ses_terminal",
description: "terminal task",
agent: "sisyphus-junior",
status: "interrupt",
error: "already stopped",
}),
},
client: {
session: {
status: async () => ({ data: {} }),
messages: async () => ({ data: [] }),
},
},
}),
{ sessionID: "ses_parent", messageID: "msg_parent", agent: "sisyphus" },
"sisyphus-junior",
undefined,
undefined,
"test-model",
)
// then
expect(Date.now() - startedAt).toBeLessThan(200)
expect(output).toContain("SUPERVISED TASK FAILED")
expect(output).toContain("already stopped")
})
})
@@ -108,13 +108,19 @@ export async function executeUnstableAgentTask(
return `Task aborted (was running in background mode).\n\nSession ID: ${sessionID}`
}
await new Promise(resolve => setTimeout(resolve, timingCfg.POLL_INTERVAL_MS))
const currentTask = manager.getTask(task.id)
if (currentTask && (currentTask.status === "interrupt" || currentTask.status === "error" || currentTask.status === "cancelled")) {
terminalStatus = { status: currentTask.status, error: currentTask.error }
break
}
if (currentTask?.status === "completed") {
completedDuringMonitoring = true
break
}
const timeoutBudgetMs = syncPollTimeoutMs ?? DEFAULT_SYNC_POLL_TIMEOUT_MS
const remainingBudgetMs = timeoutBudgetMs - (Date.now() - pollStart)
await new Promise(resolve => setTimeout(resolve, Math.min(timingCfg.POLL_INTERVAL_MS, Math.max(1, remainingBudgetMs))))
const statusResult = await client.session.status()
const allStatuses = normalizeSDKResponse(statusResult, {} as Record<string, { type: string }>)