refactor(delegate-task): extract background session registration helpers
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -12,6 +12,18 @@ import { stripAgentListSortPrefix } from "../../shared/agent-display-names"
|
|||||||
import { buildTaskMetadataBlock } from "../../features/tool-metadata-store/task-metadata-contract"
|
import { buildTaskMetadataBlock } from "../../features/tool-metadata-store/task-metadata-contract"
|
||||||
import { resolveMetadataModel } from "./resolve-metadata-model"
|
import { resolveMetadataModel } from "./resolve-metadata-model"
|
||||||
|
|
||||||
|
function registerBackgroundSessionContext(args: {
|
||||||
|
sessionId: string
|
||||||
|
fallbackChain?: FallbackEntry[]
|
||||||
|
category?: string
|
||||||
|
modelFallbackControllerAccessor?: ExecutorContext["modelFallbackControllerAccessor"]
|
||||||
|
}): void {
|
||||||
|
args.modelFallbackControllerAccessor?.setSessionFallbackChain(args.sessionId, args.fallbackChain)
|
||||||
|
if (args.category) {
|
||||||
|
SessionCategoryRegistry.register(args.sessionId, args.category)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function continueSessionSetup(args: {
|
function continueSessionSetup(args: {
|
||||||
taskID: string
|
taskID: string
|
||||||
manager: ExecutorContext["manager"]
|
manager: ExecutorContext["manager"]
|
||||||
@@ -41,15 +53,50 @@ function continueSessionSetup(args: {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
args.modelFallbackControllerAccessor?.setSessionFallbackChain(sessionId, args.fallbackChain)
|
registerBackgroundSessionContext({
|
||||||
if (args.category) {
|
sessionId,
|
||||||
SessionCategoryRegistry.register(sessionId, args.category)
|
fallbackChain: args.fallbackChain,
|
||||||
}
|
category: args.category,
|
||||||
|
modelFallbackControllerAccessor: args.modelFallbackControllerAccessor,
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function waitForBackgroundSessionStart(args: {
|
||||||
|
taskId: string
|
||||||
|
initialSessionId?: string
|
||||||
|
manager: ExecutorContext["manager"]
|
||||||
|
timing: ReturnType<typeof getTimingConfig>
|
||||||
|
abortSignal?: AbortSignal
|
||||||
|
onAbort: () => void
|
||||||
|
}): Promise<string | undefined> {
|
||||||
|
const waitStart = Date.now()
|
||||||
|
let sessionId = args.initialSessionId
|
||||||
|
|
||||||
|
while (!sessionId && Date.now() - waitStart < args.timing.WAIT_FOR_SESSION_TIMEOUT_MS) {
|
||||||
|
const updated = args.manager.getTask(args.taskId)
|
||||||
|
if (updated?.status === "error" || updated?.status === "cancelled" || updated?.status === "interrupt") {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionId = updated?.sessionID
|
||||||
|
if (sessionId) {
|
||||||
|
return sessionId
|
||||||
|
}
|
||||||
|
|
||||||
|
if (args.abortSignal?.aborted) {
|
||||||
|
args.onAbort()
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
await new Promise(resolve => setTimeout(resolve, args.timing.WAIT_FOR_SESSION_INTERVAL_MS))
|
||||||
|
}
|
||||||
|
|
||||||
|
return sessionId
|
||||||
|
}
|
||||||
|
|
||||||
export async function executeBackgroundTask(
|
export async function executeBackgroundTask(
|
||||||
args: DelegateTaskArgs,
|
args: DelegateTaskArgs,
|
||||||
ctx: ToolContextWithMetadata,
|
ctx: ToolContextWithMetadata,
|
||||||
@@ -88,18 +135,13 @@ export async function executeBackgroundTask(
|
|||||||
// BackgroundManager.launch() returns immediately (pending) before the session exists,
|
// BackgroundManager.launch() returns immediately (pending) before the session exists,
|
||||||
// so we must wait briefly for the session to be created to set metadata correctly.
|
// so we must wait briefly for the session to be created to set metadata correctly.
|
||||||
const timing = getTimingConfig()
|
const timing = getTimingConfig()
|
||||||
const waitStart = Date.now()
|
let sessionId = await waitForBackgroundSessionStart({
|
||||||
let sessionId = task.sessionID
|
taskId: task.id,
|
||||||
while (!sessionId && Date.now() - waitStart < timing.WAIT_FOR_SESSION_TIMEOUT_MS) {
|
initialSessionId: task.sessionID,
|
||||||
const updated = manager.getTask(task.id)
|
manager,
|
||||||
if (updated?.status === "error" || updated?.status === "cancelled" || updated?.status === "interrupt") {
|
timing,
|
||||||
return `Task failed to start (status: ${updated.status}).\n\nTask ID: ${task.id}`
|
abortSignal: ctx.abort,
|
||||||
}
|
onAbort: () => {
|
||||||
sessionId = updated?.sessionID
|
|
||||||
if (sessionId) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if (ctx.abort?.aborted) {
|
|
||||||
continueSessionSetup({
|
continueSessionSetup({
|
||||||
taskID: task.id,
|
taskID: task.id,
|
||||||
manager,
|
manager,
|
||||||
@@ -108,16 +150,23 @@ export async function executeBackgroundTask(
|
|||||||
category: args.category,
|
category: args.category,
|
||||||
modelFallbackControllerAccessor: executorCtx.modelFallbackControllerAccessor,
|
modelFallbackControllerAccessor: executorCtx.modelFallbackControllerAccessor,
|
||||||
})
|
})
|
||||||
break
|
},
|
||||||
}
|
})
|
||||||
await new Promise(resolve => setTimeout(resolve, timing.WAIT_FOR_SESSION_INTERVAL_MS))
|
|
||||||
|
const updatedTask = typeof manager.getTask === "function"
|
||||||
|
? manager.getTask(task.id)
|
||||||
|
: undefined
|
||||||
|
if (!sessionId && (updatedTask?.status === "error" || updatedTask?.status === "cancelled" || updatedTask?.status === "interrupt")) {
|
||||||
|
return `Task failed to start (status: ${updatedTask.status}).\n\nTask ID: ${task.id}`
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sessionId) {
|
if (sessionId) {
|
||||||
executorCtx.modelFallbackControllerAccessor?.setSessionFallbackChain(sessionId, fallbackChain)
|
registerBackgroundSessionContext({
|
||||||
if (args.category) {
|
sessionId,
|
||||||
SessionCategoryRegistry.register(sessionId, args.category)
|
fallbackChain,
|
||||||
}
|
category: args.category,
|
||||||
|
modelFallbackControllerAccessor: executorCtx.modelFallbackControllerAccessor,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolvedModel = resolveMetadataModel(categoryModel, parentContext.model)
|
const resolvedModel = resolveMetadataModel(categoryModel, parentContext.model)
|
||||||
|
|||||||
Reference in New Issue
Block a user