fix(team-mode): register spawned sessions early

This commit is contained in:
YeonGyu-Kim
2026-05-04 12:51:18 +09:00
parent cb819307ff
commit 503df29195
10 changed files with 151 additions and 30 deletions
+1
View File
@@ -580,6 +580,7 @@ export class BackgroundManager {
return
}
await input.onSessionCreated?.(sessionID)
this.settlePreStartDescendantReservation(task)
subagentSessions.add(sessionID)
@@ -0,0 +1,65 @@
/// <reference types="bun-types" />
import { describe, expect, test } from "bun:test"
import { tmpdir } from "node:os"
import type { PluginInput } from "@opencode-ai/plugin"
import { BackgroundManager } from "./manager"
async function waitForEvent(events: readonly string[], eventName: string): Promise<void> {
const deadlineAt = Date.now() + 1_000
while (!events.includes(eventName)) {
if (Date.now() > deadlineAt) {
throw new Error(`timed out waiting for ${eventName}`)
}
await new Promise((resolve) => setTimeout(resolve, 10))
}
}
describe("BackgroundManager session created callback", () => {
test("fires onSessionCreated before the launch prompt is sent", async () => {
//#given
const events: string[] = []
const client = {
session: {
get: async ({ path }: { path: { id: string } }) => ({
data: { id: path.id, directory: tmpdir() },
}),
create: async () => {
events.push("session.create")
return { data: { id: "child-session" } }
},
promptAsync: async () => {
events.push("promptAsync")
return { data: {} }
},
},
}
const manager = new BackgroundManager({
pluginContext: { client, directory: tmpdir() } as PluginInput,
})
//#when
await manager.launch({
description: "Create child",
prompt: "Do work",
agent: "general",
parentSessionId: "parent-session",
parentMessageId: "parent-message",
onSessionCreated: (sessionId) => {
events.push(`onSessionCreated:${sessionId}`)
},
})
await waitForEvent(events, "promptAsync")
//#then
expect(events).toEqual([
"session.create",
"onSessionCreated:child-session",
"promptAsync",
])
manager.shutdown()
})
})
+1
View File
@@ -112,6 +112,7 @@ export async function startTask(
}
const sessionID = createResult.data.id
await input.onSessionCreated?.(sessionID)
subagentSessions.add(sessionID)
task.status = "running"
+1
View File
@@ -117,6 +117,7 @@ export interface LaunchInput {
skillContent?: string
category?: string
sessionPermission?: SessionPermissionRule[]
onSessionCreated?: (sessionId: string) => void | Promise<void>
}
export interface ResumeInput {