perf(background-task): reduce output poll latency
This commit is contained in:
@@ -0,0 +1,71 @@
|
|||||||
|
/// <reference types="bun-types" />
|
||||||
|
import { describe, expect, test } from "bun:test"
|
||||||
|
import type { BackgroundTask } from "../../features/background-agent"
|
||||||
|
import { unsafeTestValue } from "../../../test-support/unsafe-test-value"
|
||||||
|
import { createBackgroundOutput } from "./create-background-output"
|
||||||
|
import type { BackgroundOutputClient, BackgroundOutputManager } from "./clients"
|
||||||
|
|
||||||
|
const mockContext = unsafeTestValue<Parameters<ReturnType<typeof createBackgroundOutput>["execute"]>[1]>({
|
||||||
|
sessionID: "ses_parent",
|
||||||
|
messageID: "msg_parent",
|
||||||
|
agent: "sisyphus",
|
||||||
|
abort: new AbortController().signal,
|
||||||
|
})
|
||||||
|
|
||||||
|
function createTask(): BackgroundTask {
|
||||||
|
return {
|
||||||
|
id: "bg_fast_poll",
|
||||||
|
sessionId: "ses_fast_poll",
|
||||||
|
parentSessionId: "ses_parent",
|
||||||
|
parentMessageId: "msg_parent",
|
||||||
|
description: "fast poll",
|
||||||
|
prompt: "run",
|
||||||
|
agent: "sisyphus-junior",
|
||||||
|
status: "running",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const client: BackgroundOutputClient = {
|
||||||
|
session: {
|
||||||
|
messages: async () => ({
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
info: { role: "assistant", time: "2026-01-01T00:00:00Z" },
|
||||||
|
parts: [{ type: "text", text: "completed result" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("background_output blocking poll interval", () => {
|
||||||
|
test("#given a short blocking timeout and a task that completes on retry #when fetching output #then it does not sleep for the legacy one second interval", async () => {
|
||||||
|
// given
|
||||||
|
let pollCount = 0
|
||||||
|
const task = createTask()
|
||||||
|
const manager: BackgroundOutputManager = {
|
||||||
|
getTask: (id: string) => {
|
||||||
|
if (id !== task.id) return undefined
|
||||||
|
pollCount += 1
|
||||||
|
if (pollCount >= 3) {
|
||||||
|
task.status = "completed"
|
||||||
|
}
|
||||||
|
return task
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const tool = createBackgroundOutput(manager, client)
|
||||||
|
const startedAt = Date.now()
|
||||||
|
|
||||||
|
// when
|
||||||
|
const output = await tool.execute({
|
||||||
|
task_id: task.id,
|
||||||
|
block: true,
|
||||||
|
timeout: 30,
|
||||||
|
}, mockContext)
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(Date.now() - startedAt).toBeLessThan(200)
|
||||||
|
expect(pollCount).toBeGreaterThanOrEqual(3)
|
||||||
|
expect(output).toContain("completed result")
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -15,6 +15,7 @@ import { recordBackgroundOutputConsumption } from "../../shared/background-outpu
|
|||||||
|
|
||||||
const SISYPHUS_JUNIOR_AGENT = getAgentDisplayName("sisyphus-junior")
|
const SISYPHUS_JUNIOR_AGENT = getAgentDisplayName("sisyphus-junior")
|
||||||
const MISSING_BACKGROUND_TASK_RETRY_DELAY_MS = 100
|
const MISSING_BACKGROUND_TASK_RETRY_DELAY_MS = 100
|
||||||
|
const BACKGROUND_OUTPUT_POLL_INTERVAL_MS = 100
|
||||||
|
|
||||||
type ToolContextWithMetadata = {
|
type ToolContextWithMetadata = {
|
||||||
sessionID: string
|
sessionID: string
|
||||||
@@ -140,7 +141,8 @@ export function createBackgroundOutput(manager: BackgroundOutputManager, client:
|
|||||||
if (shouldBlock && isTaskActiveStatus(task.status)) {
|
if (shouldBlock && isTaskActiveStatus(task.status)) {
|
||||||
const startTime = Date.now()
|
const startTime = Date.now()
|
||||||
while (Date.now() - startTime < timeoutMs) {
|
while (Date.now() - startTime < timeoutMs) {
|
||||||
await delay(1000)
|
const remainingMs = timeoutMs - (Date.now() - startTime)
|
||||||
|
await delay(Math.min(BACKGROUND_OUTPUT_POLL_INTERVAL_MS, Math.max(1, remainingMs)))
|
||||||
|
|
||||||
const currentTask = await getTaskWithMissingRetry(manager, args.task_id)
|
const currentTask = await getTaskWithMissingRetry(manager, args.task_id)
|
||||||
if (!currentTask) {
|
if (!currentTask) {
|
||||||
|
|||||||
Reference in New Issue
Block a user