Repository navigation
Description
Summary
When multiple follow-up messages are queued while an Agent turn is running, a dispatched turn can receive messages belonging to later queued turns, or an empty assistant streaming placeholder, as part of its prompt.
This is a context-boundary bug in the queue execution path. The current turn must see completed history plus its own user message, not future queued input.
Reproduction
Confirmed on a clean upstream main at 2e5cb380.
- Start a long-running turn with user message "一".
- While it is running, queue "二", "三", and "四".
- Observe the actual message list passed to the Agent runtime for each dispatched turn.
For the second dispatched turn (user message "二"), the runtime received the later queued user message "三" as the final user input, with the current model-preference instruction appended to that later message. The third turn's context ended with an empty assistant placeholder and the preference instruction attached to that placeholder. Only the final queued turn received the expected context.
Root cause
In apps/api/src/opendirector_api/agent.py, produce_one() builds the context using tail-relative slices such as saved['messages'][:-1] and [:-2], and attaches preference instructions through context[-1]. These operations assume the active streaming assistant placeholder is always the final message.
That assumption holds for a directly submitted turn, where begin() appends the user message and placeholder at the end. It does not hold for queued dispatch: dispatch() inserts the placeholder after the selected queued user message while later queued messages remain after it. Related last-message assumptions, including the native runtime path, should be checked as part of the fix.
Expected behavior
- A dispatched turn's context includes completed conversation history and its own user message exactly once.
- The current turn's empty streaming assistant placeholder is not treated as a prior assistant response.
- Later queued user messages are excluded until their own turn is dispatched.
- Message order and completed assistant replies are preserved.
- Model/agent preference instructions are attached to the current dispatched user message, not a later queued message or placeholder.
- Multiple queued turns behave consistently, independent of how many later messages are waiting.
Verification evidence
The fix is tracked separately on fork branch yefuyou:fix/queued-turn-context, commit 5243b27d136bd8f1f4b77868bb3c6d35ab1d87b8.
- Added 10 deterministic tests in
test_assistant_queued_context.py, using event barriers and assertions against the messages actually received by the runtime. - Three regression tests fail on the clean-main baseline; the fixed suite passes 10/10 across 10 repeated runs.
- Five mutation variants of the incorrect implementation are detected by the tests.
- Related assistant/agent tests: 161 passed, 13 skipped.
- Full CPU suite: 3,021 passed and one intermittent
test_creation_storage...[in_place]failure, also observed on clean main as a pre-existing flaky failure. lint-imports: all four contracts preserved.- Independent review: PASS; no future-message leakage, lost history, or identified regressions to Stop, Cancel, attach, or restart recovery.
- No paid provider calls.
Scope boundaries
This issue covers queued-turn context construction only. Queue behavior after a runtime or persistence failure, and the treatment of messages cancelled by Stop, are separate lifecycle/product-semantics questions and are not changed by this fix.
Related: #379 (queued follow-up messages), #502, #509, #476.
Activity