Skip to content

Commit dd97309

Browse files
mp-orkesclaude
andcommitted
Fix example 76 dropping the last task
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent df5de38 commit dd97309

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

examples/agents/76_wait_for_message_streaming.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
- wait_for_message_tool with streaming: push messages in and see the agent react
55
- Using handle.stream() to observe WAITING → processing → WAITING cycles
66
- runtime.send_message() to push payloads into the Workflow Message Queue
7+
- handle.stop() ending the loop deterministically
78
8-
The agent starts, immediately waits for a message, processes whatever it
9-
receives (by calling wait_for_message again), then waits again. The caller
10-
drives the conversation by sending messages and reading streamed events.
9+
The agent starts, immediately waits for a message, answers it with respond(),
10+
then loops back to wait_for_message. The caller drives the conversation from a
11+
background thread — sending a task every 8 seconds — while the main thread reads
12+
streamed events.
13+
14+
The agent's instructions tell it to never stop, so the loop only ends when the
15+
sender calls handle.stop() — after giving the last task time to be answered.
16+
That sets the ``_stop_requested`` workflow variable
17+
checked by the DoWhile condition and pushes a ``{"_signal": "stop"}`` message to
18+
unblock the pending PULL_WORKFLOW_MESSAGES. stream() then yields DONE.
1119
1220
Requirements:
1321
- Conductor server running at http://localhost:8080
@@ -66,15 +74,16 @@ def main() -> None:
6674
print(f"Agent started: {handle.execution_id}\n")
6775

6876
# Push messages from a background thread while we stream events on the main thread.
69-
# Wait long enough between sends for the agent to finish processing each message.
70-
# No sleep after the last send — handle.stream() on the main thread is already the
71-
# barrier: it blocks until DONE, which only fires once the workflow reaches a
72-
# terminal state (after stop() sets the flag and the current iteration completes).
77+
# Wait long enough between sends for the agent to finish processing each message
78+
# including after the last one. Calling stop() immediately after the final send
79+
# would set _stop_requested while the agent is still mid-turn on that task, and the
80+
# DoWhile would exit before it ever answers.
7381
def sender():
7482
for task in TASKS:
7583
time.sleep(8)
7684
print(f"\n [caller] sending -> {task!r}")
7785
runtime.send_message(handle.execution_id, {"task": task})
86+
time.sleep(8)
7887
handle.stop()
7988

8089
threading.Thread(target=sender, daemon=True).start()

0 commit comments

Comments
 (0)