Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

Upon dequeue the current request immediately displays processing state instead of position 1 waiting

Alishahryar1697bc37faf07
diff --git a/.gitignore b/.gitignore
index 306918bc..6424fbed 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,7 +26,7 @@ wheels/
*.egg
.coverage
.ruff_cache
-.codex
+AGENTS.MD
# Virtual environments
venv/
diff --git a/api/app.py b/api/app.py
index 24133601..15e40a7f 100644
--- a/api/app.py
+++ b/api/app.py
@@ -109,6 +109,7 @@ async def lifespan(app: FastAPI):
"node_to_tree": session_store.get_node_mapping(),
},
queue_update_callback=message_handler._update_queue_positions,
+ node_started_callback=message_handler._mark_node_processing,
)
# Reconcile restored state - anything PENDING/IN_PROGRESS is lost across restart
if message_handler.tree_queue.cleanup_stale_nodes() > 0:
diff --git a/messaging/handler.py b/messaging/handler.py
index 5960b709..b32a1e93 100644
--- a/messaging/handler.py
+++ b/messaging/handler.py
@@ -246,7 +246,8 @@ class ClaudeMessageHandler:
self.cli_manager = cli_manager
self.session_store = session_store
self.tree_queue = TreeQueueManager(
- queue_update_callback=self._update_queue_positions
+ queue_update_callback=self._update_queue_positions,
+ node_started_callback=self._mark_node_processing,
)
async def handle_message(self, incoming: IncomingMessage) -> None:
@@ -376,6 +377,20 @@ class ClaudeMessageHandler:
)
)
+ async def _mark_node_processing(self, tree: MessageTree, node_id: str) -> None:
+ """Update the dequeued node's status to processing immediately."""
+ node = tree.get_node(node_id)
+ if not node or node.state == MessageState.ERROR:
+ return
+ self.platform.fire_and_forget(
+ self.platform.queue_edit_message(
+ node.incoming.chat_id,
+ node.status_message_id,
+ format_status("🔄", "Processing..."),
+ parse_mode="MarkdownV2",
+ )
+ )
+
async def _process_node(
self,
node_id: str,
diff --git a/messaging/tree_processor.py b/messaging/tree_processor.py
index 753e4d4c..a03f4d01 100644
--- a/messaging/tree_processor.py
+++ b/messaging/tree_processor.py
@@ -24,8 +24,12 @@ class TreeQueueProcessor:
queue_update_callback: Optional[
Callable[[MessageTree], Awaitable[None]]
] = None,
+ node_started_callback: Optional[
+ Callable[[MessageTree, str], Awaitable[None]]
+ ] = None,
):
self._queue_update_callback = queue_update_callback
+ self._node_started_callback = node_started_callback
def set_queue_update_callback(
self,
@@ -36,6 +40,15 @@ class TreeQueueProcessor:
"""Update the callback used to refresh queue positions."""
self._queue_update_callback = queue_update_callback
+ def set_node_started_callback(
+ self,
+ node_started_callback: Optional[
+ Callable[[MessageTree, str], Awaitable[None]]
+ ],
+ ) -> None:
+ """Update the callback used when a queued node starts processing."""
+ self._node_started_callback = node_started_callback
+
async def _notify_queue_updated(self, tree: MessageTree) -> None:
"""Invoke queue update callback if set."""
if not self._queue_update_callback:
@@ -45,6 +58,15 @@ class TreeQueueProcessor:
except Exception as e:
logger.warning(f"Queue update callback failed: {e}")
+ async def _notify_node_started(self, tree: MessageTree, node_id: str) -> None:
+ """Invoke node started callback if set."""
+ if not self._node_started_callback:
+ return
+ try:
+ await self._node_started_callback(tree, node_id)
+ except Exception as e:
+ logger.warning(f"Node started callback failed: {e}")
+
async def process_node(
self,
tree: MessageTree,
@@ -103,8 +125,9 @@ class TreeQueueProcessor:
self.process_node(tree, node, processor)
)
- # Queue positions changed after dequeue; refresh queued status messages.
+ # Notify that this node has started processing and refresh queue positions.
if next_node_id:
+ await self._notify_node_started(tree, next_node_id)
await self._notify_queue_updated(tree)
async def enqueue_and_start(
diff --git a/messaging/tree_queue.py b/messaging/tree_queue.py
index c151addc..a5441b60 100644
--- a/messaging/tree_queue.py
+++ b/messaging/tree_queue.py
@@ -41,9 +41,15 @@ class TreeQueueManager:
queue_update_callback: Optional[
Callable[[MessageTree], Awaitable[None]]
] = None,
+ node_started_callback: Optional[
+ Callable[[MessageTree, str], Awaitable[None]]
+ ] = None,
):
self._repository = TreeRepository()
- self._processor = TreeQueueProcessor(queue_update_callback)
+ self._processor = TreeQueueProcessor(
+ queue_update_callback=queue_update_callback,
+ node_started_callback=node_started_callback,
+ )
self._lock = asyncio.Lock()
logger.info("TreeQueueManager initialized")
@@ -333,6 +339,15 @@ class TreeQueueManager:
"""Set callback for queue position updates."""
self._processor.set_queue_update_callback(queue_update_callback)
+ def set_node_started_callback(
+ self,
+ node_started_callback: Optional[
+ Callable[[MessageTree, str], Awaitable[None]]
+ ],
+ ) -> None:
+ """Set callback for when a queued node starts processing."""
+ self._processor.set_node_started_callback(node_started_callback)
+
def register_node(self, node_id: str, root_id: str) -> None:
"""Register a node ID to a tree (for external mapping)."""
self._repository.register_node(node_id, root_id)
@@ -348,8 +363,14 @@ class TreeQueueManager:
queue_update_callback: Optional[
Callable[[MessageTree], Awaitable[None]]
] = None,
+ node_started_callback: Optional[
+ Callable[[MessageTree, str], Awaitable[None]]
+ ] = None,
) -> "TreeQueueManager":
"""Deserialize from dictionary."""
- manager = cls(queue_update_callback=queue_update_callback)
+ manager = cls(
+ queue_update_callback=queue_update_callback,
+ node_started_callback=node_started_callback,
+ )
manager._repository = TreeRepository.from_dict(data)
return manager
diff --git a/tests/test_handler.py b/tests/test_handler.py
index 31fafb64..253d0c7c 100644
--- a/tests/test_handler.py
+++ b/tests/test_handler.py
@@ -169,6 +169,48 @@ async def test_update_queue_positions(handler, mock_platform):
assert "position 2" in calls[1][0][2]
+async def test_mark_node_processing(handler, mock_platform):
+ root_incoming = IncomingMessage(
+ text="Root",
+ chat_id="chat_1",
+ user_id="user_1",
+ message_id="root",
+ platform="telegram",
+ )
+ root = MessageNode(
+ node_id="root",
+ incoming=root_incoming,
+ status_message_id="status_root",
+ )
+ tree = MessageTree(root)
+
+ child_incoming = IncomingMessage(
+ text="Child",
+ chat_id="chat_1",
+ user_id="user_1",
+ message_id="child",
+ platform="telegram",
+ reply_to_message_id="root",
+ )
+
+ await tree.add_node(
+ node_id="child",
+ incoming=child_incoming,
+ status_message_id="status_child",
+ parent_id="root",
+ )
+
+ await handler._mark_node_processing(tree, "child")
+
+ mock_platform.queue_edit_message.assert_called_once()
+ args, kwargs = mock_platform.queue_edit_message.call_args
+ assert args[0] == "chat_1"
+ assert args[1] == "status_child"
+ assert "Processing" in args[2]
+ assert kwargs["parse_mode"] == "MarkdownV2"
+
+
@pytest.mark.asyncio
async def test_stop_all_tasks(handler, mock_cli_manager, mock_platform):
mock_node = MagicMock()
diff --git a/tests/test_tree_processor.py b/tests/test_tree_processor.py
index 6b7560dd..4a8ae494 100644
--- a/tests/test_tree_processor.py
+++ b/tests/test_tree_processor.py
@@ -170,3 +170,16 @@ async def test_process_next_triggers_queue_update(sample_tree):
await processor._process_next(sample_tree, AsyncMock())
callback.assert_awaited_once_with(sample_tree)
+
+
+async def test_process_next_triggers_node_started(sample_tree):
+ node_started = AsyncMock()
+ processor = TreeQueueProcessor(node_started_callback=node_started)
+
+ await sample_tree._queue.put("next_node")
+ sample_tree.get_node = MagicMock(return_value=None)
+
+ await processor._process_next(sample_tree, AsyncMock())
+
+ node_started.assert_awaited_once_with(sample_tree, "next_node")