task_manager
Fire-and-forget task manager for tool execution.
Wraps tool handler coroutines with a configurable timeout. If a tool
completes within the timeout its result is returned inline. Otherwise
the coroutine continues as a background asyncio.Task and a
JSON envelope containing a task ID is returned so the LLM can poll for
results later via the check_task tool.
Background tasks may still hold the ToolContext
injected into the tool and mutate it after the enclosing tool-calling
loop has moved on. Prefer no_background on tools that perform
persistent ctx side effects the model must observe in-order.
Output redirect
Any backgrounded task can have its result automatically delivered to a
channel on any platform when it finishes. Call
TaskManager.set_output_redirect() (or use the redirect_task
tool) to configure this.
- class task_manager.TaskStatus(*values)[source]
-
Lifecycle states for a tracked background tool task.
A string-valued enum (so members serialize directly into JSON envelopes and Redis fields as their plain values) covering the three terminal-or-not states a task can be in:
RUNNINGwhile the coroutine is in flight,COMPLETEDon success, andFAILEDon cancellation or exception. Used throughoutTaskManagerand persisted into thesg:task:{task_id}Redis hash and the legacy JSON envelopes that thecheck_tasktool reads back.- RUNNING = 'running'
- COMPLETED = 'completed'
- FAILED = 'failed'
- class task_manager.TaskRecord(task_id, tool_name, status, created_at=<factory>, result=None, error=None, user_id='', channel_id='', platform='', asyncio_task=None, redirect_channel_id='', redirect_platform='', redirect_adapter=None, redirect_max_chars=0)[source]
Bases:
objectIn-memory bookkeeping for one tracked background tool task.
Captures everything
TaskManagerneeds to track, persist, and deliver a backgrounded tool invocation: identity and origin (task_id,tool_name,user_id,channel_id,platform), the liveasyncio.Taskhandle, the currentTaskStatus, and the capturedresultorerroronce it finishes. The trailing output-redirect fields hold an optional delivery target — channel, platform, resolvedPlatformAdapter, and a character cap — so a finished task’s result can be pushed to a chat channel. Created byTaskManager.execute()and mutated in place byTaskManager._on_task_done().- Parameters:
- status: TaskStatus
- class task_manager.TaskManager(timeout=10.0, redis=None)[source]
Bases:
objectManage fire-and-forget tool execution with timeout.
- Parameters:
- __init__(timeout=10.0, redis=None)[source]
Configure the timeout budget and Redis handle for task tracking.
Stores the inline-vs-background
timeoutthreshold and the optional async Redis client used to persist task state, and initializes the emptyself._tasksregistry that maps each task ID to itsTaskRecord. Performs no I/O. Constructed once per worker by the inference and web services (inference_mainandweb_main, both withtimeout=10.0and the shared Redis client) and exposed to tools through the injectedToolContext.
- async execute(coro, tool_name='', user_id='', channel_id='', platform='')[source]
Run coro with a timeout; background it if it takes too long.
Returns the tool result string directly when the coroutine finishes within
timeout, or a JSON envelope with atask_idwhen it does not.
- async get_result(task_id, user_id=None)[source]
Return the result for task_id, or a status update.
If user_id is set, only tasks owned by that user are returned.
- async await_result(task_id, timeout=300.0, user_id=None)[source]
Block until task_id completes and return its result.
Unlike
get_result()which returns immediately with a status update, this method awaits the underlyingasyncio.Taskso the caller’s coroutine is suspended until the work finishes.If user_id is set, only tasks owned by that user are awaited or returned (same semantics as
get_result()).
- async list_tasks(user_id=None)[source]
Return a JSON summary of tracked tasks.
If user_id is provided, only tasks belonging to that user are returned. Pass
Noneto list all tasks.