refactor(background-task): stop cancelling launched tasks during session wait
This commit is contained in:
@@ -5,7 +5,13 @@ import type { PluginInput } from "@opencode-ai/plugin"
|
||||
import { executeBackground } from "./background-executor"
|
||||
|
||||
describe("executeBackground", () => {
|
||||
const launchMock = mock(() => Promise.resolve({
|
||||
const launchMock = mock(async (_input?: { fallbackChain?: unknown }): Promise<{
|
||||
id: string
|
||||
sessionID: string | null
|
||||
description: string
|
||||
agent: string
|
||||
status: string
|
||||
}> => ({
|
||||
id: "test-task-id",
|
||||
sessionID: null,
|
||||
description: "Test task",
|
||||
@@ -83,7 +89,96 @@ describe("executeBackground", () => {
|
||||
await executeBackground(testArgs, testContext, mockManager, mockClient, fallbackChain)
|
||||
|
||||
//#then
|
||||
const launchArgs = launchMock.mock.calls.at(-1)?.[0]
|
||||
const latestCall = [...launchMock.mock.calls].pop()
|
||||
if (!latestCall) {
|
||||
throw new Error("Expected background manager launch to be called")
|
||||
}
|
||||
const launchArgs = latestCall[0]
|
||||
if (!launchArgs) {
|
||||
throw new Error("Expected launch arguments")
|
||||
}
|
||||
expect(launchArgs.fallbackChain).toEqual(fallbackChain)
|
||||
})
|
||||
|
||||
test("keeps launched background task alive when parent aborts before session id resolves", async () => {
|
||||
//#given - parent abort after launch should stop waiting, not fail the background task
|
||||
const abortController = new AbortController()
|
||||
launchMock.mockResolvedValueOnce({
|
||||
id: "test-task-id",
|
||||
sessionID: null,
|
||||
description: "Test task",
|
||||
agent: "test-agent",
|
||||
status: "pending",
|
||||
})
|
||||
getTaskMock.mockImplementationOnce(() => {
|
||||
abortController.abort()
|
||||
return { id: "test-task-id", sessionID: null, description: "Test task", agent: "test-agent", status: "pending" }
|
||||
})
|
||||
|
||||
//#when
|
||||
const result = await executeBackground(
|
||||
testArgs,
|
||||
{
|
||||
...testContext,
|
||||
abort: abortController.signal,
|
||||
},
|
||||
mockManager,
|
||||
mockClient
|
||||
)
|
||||
|
||||
//#then - background launch should still be reported as launched
|
||||
expect(result).toContain("Background agent task launched successfully")
|
||||
expect(result).toContain("Task ID: test-task-id")
|
||||
expect(result).not.toContain("Task aborted while waiting for session to start")
|
||||
})
|
||||
|
||||
test("keeps sibling background launch alive when two tasks start concurrently", async () => {
|
||||
//#given - one aborted parent call should not interrupt a sibling launch from the same parent session
|
||||
const firstAbortController = new AbortController()
|
||||
const secondAbortController = new AbortController()
|
||||
const states = new Map([
|
||||
["task-1", { reads: 0, abortOnFirstRead: true, sessionID: "ses-1" }],
|
||||
["task-2", { reads: 0, abortOnFirstRead: false, sessionID: "ses-2" }],
|
||||
])
|
||||
let launchCount = 0
|
||||
launchMock.mockImplementation(async () => {
|
||||
launchCount += 1
|
||||
return launchCount === 1
|
||||
? { id: "task-1", sessionID: null, description: "Task 1", agent: "test-agent", status: "pending" }
|
||||
: { id: "task-2", sessionID: null, description: "Task 2", agent: "test-agent", status: "pending" }
|
||||
})
|
||||
getTaskMock.mockImplementation((taskID: string) => {
|
||||
const state = states.get(taskID)
|
||||
if (!state) return undefined
|
||||
state.reads += 1
|
||||
if (state.abortOnFirstRead && state.reads === 1) {
|
||||
firstAbortController.abort()
|
||||
}
|
||||
return state.reads >= 2
|
||||
? { id: taskID, sessionID: state.sessionID, description: "Task", agent: "test-agent", status: "pending" }
|
||||
: { id: taskID, sessionID: null, description: "Task", agent: "test-agent", status: "pending" }
|
||||
})
|
||||
|
||||
//#when
|
||||
const [firstResult, secondResult] = await Promise.all([
|
||||
executeBackground(
|
||||
testArgs,
|
||||
{ ...testContext, abort: firstAbortController.signal },
|
||||
mockManager,
|
||||
mockClient,
|
||||
),
|
||||
executeBackground(
|
||||
testArgs,
|
||||
{ ...testContext, abort: secondAbortController.signal },
|
||||
mockManager,
|
||||
mockClient,
|
||||
),
|
||||
])
|
||||
|
||||
//#then - both launches still succeed and the sibling is not marked interrupted
|
||||
expect(firstResult).toContain("Background agent task launched successfully")
|
||||
expect(secondResult).toContain("Background agent task launched successfully")
|
||||
expect(secondResult).toContain("Task ID: task-2")
|
||||
expect(secondResult).not.toContain("interrupt")
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user