fix(delegate-task): pause timeouts for active sessions
This commit is contained in:
@@ -79,6 +79,52 @@ describe("syncPollTimeoutMs threading", () => {
|
||||
expect(abortCount).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
test("#then active OpenCode statuses do not consume the inactivity timeout", async () => {
|
||||
const { pollSyncSession } = require("./sync-session-poller")
|
||||
let abortCount = 0
|
||||
let statusCallCount = 0
|
||||
let messageCallCount = 0
|
||||
const mockClient = {
|
||||
session: {
|
||||
abort: async () => {
|
||||
abortCount++
|
||||
},
|
||||
messages: async () => {
|
||||
messageCallCount++
|
||||
return {
|
||||
data: [
|
||||
{ info: { id: "msg_001", role: "user", time: { created: 1000 } } },
|
||||
{
|
||||
info: { id: "msg_002", role: "assistant", time: { created: 2000 }, finish: "stop" },
|
||||
parts: [{ type: "text", text: "done" }],
|
||||
},
|
||||
],
|
||||
}
|
||||
},
|
||||
status: async () => {
|
||||
statusCallCount++
|
||||
if (statusCallCount === 1) return { data: { ses_active: { type: "busy" } } }
|
||||
if (statusCallCount === 2) return { data: { ses_active: { type: "retry" } } }
|
||||
return { data: { ses_active: { type: "idle" } } }
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
await withMockedDateNow(60_000, async () => {
|
||||
const result = await pollSyncSession(createMockCtx(), mockClient, {
|
||||
sessionID: "ses_active",
|
||||
agentToUse: "oracle",
|
||||
toastManager: null,
|
||||
taskId: undefined,
|
||||
}, 120_000)
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(abortCount).toBe(0)
|
||||
expect(statusCallCount).toBe(3)
|
||||
expect(messageCallCount).toBe(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#when timeoutMs is omitted", () => {
|
||||
|
||||
@@ -7,6 +7,7 @@ import { extractErrorMessage } from "../../features/background-agent/error-class
|
||||
|
||||
const NON_TERMINAL_FINISH_REASONS = new Set(["tool-calls", "unknown"])
|
||||
const PENDING_TOOL_PART_TYPES = new Set(["tool", "tool_use", "tool-call"])
|
||||
const ACTIVE_SESSION_STATUSES = new Set(["busy", "retry", "running"])
|
||||
|
||||
function wait(milliseconds: number): Promise<void> {
|
||||
const sharedBuffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT)
|
||||
@@ -24,6 +25,10 @@ function abortSyncSession(client: OpencodeClient, sessionID: string, reason: str
|
||||
})
|
||||
}
|
||||
|
||||
function isActiveSessionStatus(status: { type: string } | undefined): boolean {
|
||||
return status !== undefined && ACTIVE_SESSION_STATUSES.has(status.type)
|
||||
}
|
||||
|
||||
async function fetchSessionMessages(
|
||||
client: OpencodeClient,
|
||||
sessionID: string
|
||||
@@ -84,6 +89,7 @@ export async function pollSyncSession(
|
||||
const maxPollTimeMs = Math.max(timeoutMs ?? getDefaultSyncPollTimeoutMs(), 50)
|
||||
const maxTurns = input.maxAssistantTurns ?? DEFAULT_MAX_ASSISTANT_TURNS
|
||||
const pollStart = Date.now()
|
||||
let inactiveStart = pollStart
|
||||
let pollCount = 0
|
||||
let timedOut = false
|
||||
let assistantTurnCount = 0
|
||||
@@ -91,7 +97,13 @@ export async function pollSyncSession(
|
||||
|
||||
log("[task] Starting poll loop", { sessionID: input.sessionID, agentToUse: input.agentToUse, maxTurns })
|
||||
|
||||
while (Date.now() - pollStart < maxPollTimeMs) {
|
||||
while (true) {
|
||||
const inactiveElapsedMs = Date.now() - inactiveStart
|
||||
if (inactiveElapsedMs >= maxPollTimeMs) {
|
||||
timedOut = true
|
||||
break
|
||||
}
|
||||
|
||||
if (ctx.abort?.aborted) {
|
||||
try {
|
||||
const messages = await fetchSessionMessages(client, input.sessionID)
|
||||
@@ -132,11 +144,13 @@ export async function pollSyncSession(
|
||||
sessionID: input.sessionID,
|
||||
pollCount,
|
||||
elapsed: Math.floor((Date.now() - pollStart) / 1000) + "s",
|
||||
inactiveElapsed: Math.floor(inactiveElapsedMs / 1000) + "s",
|
||||
sessionStatus: sessionStatus?.type ?? "not_in_status",
|
||||
})
|
||||
}
|
||||
|
||||
if (sessionStatus && sessionStatus.type !== "idle") {
|
||||
if (isActiveSessionStatus(sessionStatus)) {
|
||||
inactiveStart = Date.now()
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -199,8 +213,7 @@ export async function pollSyncSession(
|
||||
}
|
||||
}
|
||||
|
||||
if (Date.now() - pollStart >= maxPollTimeMs) {
|
||||
timedOut = true
|
||||
if (timedOut) {
|
||||
log("[task] Poll timeout reached", { sessionID: input.sessionID, pollCount })
|
||||
abortSyncSession(client, input.sessionID, "poll_timeout")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user