Repository navigation
Description
What are you really trying to do?
Run a deep agent durably with DeepAgentsPlugin and rely on Temporal for the
two things heartbeats provide: notice quickly when a worker dies mid-model-call
(so the call is retried), and stop spend by cancelling a workflow while a model
call is still in flight (budget stop, user abort).
Describe the bug
_model._resolve_activity_options defaults only start_to_close_timeout (5
minutes) and _tools.tool_as_activity / TemporalBackend set no heartbeat
timeout either, so every activity the plugin schedules carries
heartbeat_timeout = 0. _activity._auto_heartbeater then takes its
if heartbeat_timeout: fast path and never beats. Consequences:
- A worker that crashes (or is OOM-killed) during a model call is detected only
whenstart_to_close_timeoutexpires — up to 5 minutes per in-flight call —
because the server has no heartbeat to miss. - Cancellation is never delivered to the running call: a Python activity
observes cancellation solely through a heartbeat response. The workflow
completes asCANCELEDwithin ~1 s while the model call keeps running (and
billing) to its own timeout.
Observed on main (34fb981) with a scripted fake model that sleeps instead of
answering, default plugin options, workflow cancelled while the call ran:
workflow left RUNNING after 1.0s: CANCELED
observed=['started'] # no 'cancelled': the model call never learned
With a heartbeat_timeout of 4 s on the same schedule, the identical call
observed cancellation in 2.0 s (observed=['started', 'cancelled'],
asyncio.exceptions.CancelledError: Activity cancelled), confirming the
mechanism is the heartbeat, not the cancel request itself.
Minimal Reproduction
Self-contained script (public API only; a scripted fake model, a local dev
server, default plugin options). It runs one model call through
DeepAgentsPlugin and inspects the scheduled activity's options:
import asyncio
import uuid
from datetime import timedelta
from temporalio import workflow
from temporalio.api.enums.v1 import EventType
from temporalio.testing import WorkflowEnvironment
from temporalio.worker import Worker
with workflow.unsafe.imports_passed_through():
from langchain_core.messages import HumanMessage
from temporalio.deepagents import DeepAgentsPlugin, TemporalModel
from temporalio.deepagents.testing import mock_model_provider
@workflow.defn
class Ask:
@workflow.run
async def run(self, question: str) -> str:
model = TemporalModel(model="fake:model") # defaults only
reply = await model.ainvoke([HumanMessage(content=question)])
return str(reply.content)
async def main() -> int:
env = await WorkflowEnvironment.start_local()
try:
plugin = DeepAgentsPlugin(model_provider=mock_model_provider(["ok"]))
async with Worker(
env.client,
task_queue="repro-heartbeat",
workflows=[Ask],
plugins=[plugin],
):
handle = await env.client.start_workflow(
Ask.run,
"hi",
id=f"repro-heartbeat-{uuid.uuid4()}",
task_queue="repro-heartbeat",
)
await handle.result()
async for event in handle.fetch_history_events():
if event.event_type != EventType.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED:
continue
attrs = event.activity_task_scheduled_event_attributes
if attrs.activity_type.name != "deepagents.invoke_model":
continue
heartbeat = attrs.heartbeat_timeout.ToTimedelta()
print(f"deepagents.invoke_model heartbeat_timeout={heartbeat}")
return 0 if heartbeat > timedelta(0) else 1
return 1
finally:
await env.shutdown()
if __name__ == "__main__":
raise SystemExit(asyncio.run(main()))On main this prints deepagents.invoke_model heartbeat_timeout=0:00:00 and
exits 1. The same run with the workflow cancelled while a hanging model call
is in flight shows the behavioral consequence (the workflow completes as
CANCELED while the activity keeps running).
Workaround: pass heartbeat_timeout everywhere yourself, e.g.
DeepAgentsPlugin(model_activity_options={"start_to_close_timeout": timedelta(minutes=5), "heartbeat_timeout": timedelta(seconds=30)})
and the same for tool_activity_options / per-instance activity_options.
Environment/Versions
- temporalio 1.34.0, temporalio-deepagents built from repo main 34fb981
- Python 3.14.7, local dev server v1.8.3-server-1.32.0, Linux x86_64
Additional context
The plugin's own activities (invoke_model, invoke_model_streaming,
invoke_tool, backend_op) all run _auto_heartbeater, and their bodies
keep the event loop free (LangChain runs sync models and tools on worker
threads), so a default heartbeat timeout would not shorten slow calls — the
heartbeater beats at half the configured timeout. Considerations for any fix:
backend_opcurrently calls backend methods synchronously inline
(LocalShellBackend.executeissubprocess.run(shell=True)). Inline, those
calls block the event loop, so a heartbeat timeout would starve the
heartbeater and fail previously healthy long ops; a fix must dispatch them
off-loop (or leavebackend_opout of the default).- The default should be capped by the schedule's own start-to-close /
schedule-to-close timeout so short activities get proportionally short
heartbeat timeouts. - Activities exposed through
activity_as_tool(...)are user-defined and may
never heartbeat; a default timeout there would fail them spuriously, so their
schedule must stay untouched. - Activity options are not compared during command matching, so adding a
heartbeat timeout should be replay-safe for in-flight workflows on upgrade
(worth a Replayer test on a pre-change history).
Activity
[python/deepagents] give model activities a default heartbeat timeout