{"record":{"id":"5a7ec6fd9900dc08","repo":"langchain-ai/deepagents","slug":"task-requires-an-active-toolruntime-5a7ec6","errorCode":null,"errorMessage":"task() requires an active ToolRuntime","messagePattern":"task\\(\\) requires an active ToolRuntime","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"libs/partners/quickjs/langchain_quickjs/_subagent.py","lineNumber":207,"sourceCode":"\n\nasync def call_subagent_task_tool(\n    task_tool: BaseTool,\n    *,\n    description: str,\n    subagent_type: str,\n    response_schema: dict[str, Any] | None,\n    runtime: Any,\n    label: str | None = None,\n) -> Any:\n    \"\"\"Call the Deep Agents task tool and return a JavaScript-friendly value.\n\n    This also emits `start` then `complete`/`error` subagent lifecycle\n    events on the custom stream.\n    \"\"\"\n    if runtime is None:\n        msg = \"task() requires an active ToolRuntime\"\n        raise RuntimeError(msg)\n\n    parse_json_output = response_schema is not None\n    if response_schema is not None:\n        _validate_response_schema(response_schema)\n        response_schema = _ensure_schema_title(response_schema)\n        runtime = _runtime_with_response_format(runtime, response_schema)\n\n    eval_id = getattr(runtime, \"tool_call_id\", None)\n    stream_writer = getattr(runtime, \"stream_writer\", None)\n    subagent_id = f\"ptc_{task_tool.name}_{uuid.uuid4().hex[:8]}\"\n\n    runtime = _runtime_with_tool_call_id(runtime, subagent_id)\n\n    start_event: SubagentStartEvent = {\n        \"type\": SUBAGENT_STREAM_EVENT_TYPE,\n        \"phase\": \"start\",\n        \"id\": subagent_id,\n        \"subagent_type\": subagent_type,","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/partners/quickjs/langchain_quickjs/_subagent.py#L189-L225","documentation":"`call_subagent_task_tool` implements the sandbox's `task()` function and requires an active `ToolRuntime` to dispatch the subagent. It emits `start`/`complete`/`error` lifecycle events through that runtime, so calling `task()` without one is unsupported and raises `RuntimeError`.","triggerScenarios":"Invoking `task()` from JavaScript inside the sandbox when the ToolRuntime was not injected/bound — e.g. calling the task tool wrapper directly with `runtime=None`, or running sandbox code in a context where the subagent middleware did not supply a runtime.","commonSituations":"Calling the internal `task` tool handler in unit tests or scripts without constructing a ToolRuntime; using the subagent feature without the middleware that wires the runtime into the REPL; a refactor dropped the runtime argument.","solutions":["Run `task()` only through the eval tool wired by the middleware so a ToolRuntime is always bound.","In direct/test usage, construct and pass a real (or stub) ToolRuntime instead of None.","If subagents are not needed, avoid exposing/calling the `task` tool in that configuration."],"exampleFix":"// before\ncall_subagent_task_tool(prompt=\"do work\", runtime=None)\n\n// after\nfrom langchain_quickjs._runtime import ToolRuntime\nruntime = build_tool_runtime(tools=[...])\ncall_subagent_task_tool(prompt=\"do work\", runtime=runtime)","handlingStrategy":"type-guard","validationCode":"if runtime is None:\n    raise RuntimeError(\"task() called without a ToolRuntime; wire the middleware first\")","typeGuard":"def has_runtime(runtime) -> bool:\n    return runtime is not None and hasattr(runtime, \"run\")","tryCatchPattern":"try:\n    result = call_subagent_task_tool(prompt=prompt, runtime=runtime)\nexcept RuntimeError as e:\n    if \"ToolRuntime\" in str(e):\n        initialize_subagent_middleware()","preventionTips":["Always invoke `task()` through the middleware-wired eval tool.","In tests, construct a stub ToolRuntime rather than passing None.","Check the middleware wiring when subagent tooling is enabled."],"tags":["runtime","subagent","state","quickjs"],"backgroundTag":"missing-active-runtime","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}