π The fast, Pythonic way to build MCP servers and clients.
fix(tasks): keep stored arguments alive with the other per-task keys (#5144)
IronMurphy0d4da5e56391
diff --git a/fastmcp_tasks/fastmcp_tasks/handlers.py b/fastmcp_tasks/fastmcp_tasks/handlers.pyindex d24b8cb90..c6c9a22f0 100644--- a/fastmcp_tasks/fastmcp_tasks/handlers.py+++ b/fastmcp_tasks/fastmcp_tasks/handlers.py@@ -47,6 +47,7 @@ from fastmcp_tasks.input_store import (load_task_args,mark_cancelled,read_outstanding_inputs,+ refresh_args_ttl,refresh_current_leg_ttl,release_update_lock,save_current_leg,@@ -165,6 +166,9 @@ async def _lookup_task(await redis.expire(created_at_key, refresh_ttl)await redis.expire(poll_key, refresh_ttl)await refresh_current_leg_ttl(docket, task_scope, task_id, refresh_ttl)+ # A re-entered leg re-reads the original arguments, so the args key has to+ # outlive the polling window for the same reason the keys above do.+ await refresh_args_ttl(docket, task_scope, task_id, refresh_ttl)# The snapshot must outlive the routing keys it serves: a re-entered leg# restores the submitting caller from it, and with encryption configured a# missing snapshot fails the task instead of degrading to an anonymous run.diff --git a/fastmcp_tasks/fastmcp_tasks/input_store.py b/fastmcp_tasks/fastmcp_tasks/input_store.pyindex 9dab545ea..6d2e20e3b 100644--- a/fastmcp_tasks/fastmcp_tasks/input_store.py+++ b/fastmcp_tasks/fastmcp_tasks/input_store.py@@ -153,6 +153,22 @@ async def save_task_args()+async def refresh_args_ttl(+ docket: Docket, task_scope: str | None, task_id: str, ttl_seconds: int+) -> None:+ """Extend the stored arguments' TTL (sliding expiration).++ The arguments are written once at create, but a re-entered leg re-reads them+ through ``load_task_args``. A task that waits out its arguments' wall-clock+ TTL β an in-task input answered much later, or a long gap between legs β+ would otherwise enqueue its next leg with empty arguments, because a missing+ key loads as ``{}`` rather than failing. Refreshing alongside the other+ per-task keys keeps them alive for as long as the task is.+ """+ async with docket.redis() as redis:+ await redis.expire(_args_key(docket, task_scope, task_id), ttl_seconds)++async def load_task_args(docket: Docket, task_scope: str | None, task_id: str) -> dict[str, Any]:diff --git a/tests/tasks/server/test_task_ttl.py b/tests/tasks/server/test_task_ttl.pyindex edffda25a..6eb64a4ad 100644--- a/tests/tasks/server/test_task_ttl.py+++ b/tests/tasks/server/test_task_ttl.py@@ -98,6 +98,32 @@ async def test_poll_refreshes_routing_key_ttl():assert await redis.ttl(key) > 60+async def test_poll_refreshes_args_ttl():+ """A poll extends the stored arguments' TTL alongside the routing keys.++ A re-entered leg re-reads the original arguments, and a missing args key+ loads as an empty argument dict rather than failing, so an actively polled+ task must never outlive them.+ """+ from fastmcp_tasks.input_store import _args_key++ mcp = _ttl_server()+ async with running_task_server(mcp):+ created = await submit_task(mcp, "slow_task", {})+ docket = mcp._docket+ assert docket is not None+ key = _args_key(docket, None, created.task_id)++ async with docket.redis() as redis:+ await redis.expire(key, 5)+ assert await redis.ttl(key) <= 5++ await get_task(mcp, created.task_id)++ async with docket.redis() as redis:+ assert await redis.ttl(key) > 60++async def test_poll_refreshes_snapshot_ttl():"""A poll extends the context snapshot's TTL alongside the routing keys.