Refactor queue core and fix orphaned task cleanup #7
+1,196
−496
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
queue_core.pyfor code reuse between MCP server and CLIserver_id) to detect orphaned tasks from crashed/restarted processesChanges
New:
queue_core.pyShared infrastructure used by both
task_queue.py(MCP server) andtq.py(CLI):QueuePathsdataclass for consistent path managementget_db(),init_db(),ensure_db()is_process_alive(),is_task_queue_process(),kill_process_tree()cleanup_queue()with orphan detectionBug Fix: Orphaned Task Cleanup
Problem: When sub-agents are cancelled, their queued tasks remained in the database forever, blocking subsequent tasks.
Solution:
is_task_queue_process(pid)- Checks if a PID is actually running task_queue vs being reused by Chrome/Safari/etc.SERVER_INSTANCE_ID/CLI_INSTANCE_ID- Unique ID per process instance to detect stale tasks even if PID is reused_active_task_idstracking - Server tracks which tasks are actively being processedasyncio.CancelledErrorhandling - Properly cleans up when MCP clients disconnectOther Improvements
server_idcolumn to queue table (with migration for existing DBs)POLL_INTERVAL_WAITINGinstead of hardcoded valueTest plan
demo/test_pid_reuse.pyverifies PID reuse detection works correctly🤖 Generated with Claude Code