refactor: move task attribute to function-based variants only [SEP-1686]#2560
refactor: move task attribute to function-based variants only [SEP-1686]#2560
Conversation
WalkthroughThe refactoring consolidates the Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Test Failure AnalysisSummary: The Root Cause: In When the test then calls This is a race condition that's more likely to occur under certain timing conditions (like Python 3.10 in CI vs Python 3.12 locally). Suggested Solution: After calling
Specifically, modify # Cancel via Docket (now sets CANCELLED state natively)
await docket.cancel(task_key)
# Get execution and sync to ensure cancellation has propagated
execution = await docket.get_execution(task_key)
if execution:
await execution.sync()
# Verify it's actually cancelled
mcp_state = DOCKET_TO_MCP_STATE.get(execution.state, "cancelled")
else:
mcp_state = "cancelled"Then use Detailed AnalysisFailure Log: Test Code (tests/server/tasks/test_task_methods.py:157-174): async def test_task_cancellation_workflow(endpoint_server):
async with Client(endpoint_server) as client:
task = await client.call_tool("slow_tool", {}, task=True)
await asyncio.sleep(0.1)
await task.cancel() # Calls tasks_cancel_handler
await asyncio.sleep(0.1)
status = await task.status() # Calls tasks_get_handler
assert status.status == "cancelled" # FAILS: status is "working"The test expects that after cancellation and a brief wait, the status should be "cancelled". However, Comparison with execution = await docket.get_execution(task_key)
await execution.sync() # Line 110
mcp_state = DOCKET_TO_MCP_STATE.get(execution.state, "failed") # Line 113The Related Files
|
The
taskattribute for SEP-1686 background task support was defined on base classes (Tool,Prompt,Resource,ResourceTemplate) but only function-based variants can actually be registered with Docket since they're the only ones with anfncallable. This led tohasattr(obj, "fn")guards in_docket_lifespan().This PR moves
taskto the function-based subclasses only (FunctionTool,FunctionPrompt,FunctionResource,FunctionResourceTemplate) and uses properisinstance()checks:Also moves
create_resource()fromResourceTemplatetoFunctionResourceTemplatesince it depends ontask.