Skip to content

[feat] agent: run a message sent while a turn is running - #381

Open
wtz2333 wants to merge 2 commits into
ThinkFlowLab:mainfrom
wtz2333:feat/midturn-queue
Open

wtz2333 wants to merge 2 commits into
ThinkFlowLab:mainfrom
wtz2333:feat/midturn-queue

Conversation

@wtz2333

@wtz2333 wtz2333 commented Nov 17, 2026

Copy link
Copy Markdown

Summary

  • Replace the single-per-project active slot with a Chain that covers the running turn together with the messages queued behind it, so the moment between one turn ending and the next starting stays closed.
  • POST /assistant/messages accepts a message while a turn runs: when the project is idle it starts a turn exactly like POST /turns, and when a turn is running it answers 202 with the queued message, stores it in the conversation as queued, and starts it when the active turn ends. This is the backend half of [feat] assistant: accept a follow-up message while a turn is still running / 运行中插入补充消息 #379.
  • Events now carry seq, GET /turns/{id}/events?after= resumes from a cursor, and one stream spans turns: an end frame with nextTurnId means the next reply continues on the same connection, so a client can queue without polling.
  • Stopping ends the whole chain and marks messages that never started as cancelled, so a stopped run cannot leave work that begins later by surprise.

POST /turns keeps its current contract, including reporting a stale conversationId in the stream rather than as a status code.

Linked issue

Refs #379 — the issue stays open until the composer and the runtime-native steering land.

  • Refs #379 is included

Labels

  • Not applied: the account opening this pull request (wtz2333) cannot add labels here — GitHub refuses AddLabelsToLabelable for this repository. Suggested: type:feat, area:agent, area:harness, needs-design.

Test plan

  • Local:
    • python -m pytest tests/test_assistant_queue.py tests/test_assistant_conversations.py -q → 35 passed.
    • python -m pytest tests -q --ignore=tests/browser → the failures that remain are host-environment ones (this machine's ffmpeg lacks libx264 and some options); the same set fails identically with the change stashed.
    • lint-imports → 4 contracts kept. python scripts/generate_contracts.py --check → verified.
  • Left to CI: the Release checks Python and frontend lanes, layer audit, and the browser suites.

Evidence that it works

  • Media attached, or N/A because: no UI in this pull request; the queue is exercised end to end by tests/test_assistant_queue.py (queue while running, chained stream, cursor resume, stop cancels the chain, rejection before queueing, idle start). A screen recording will accompany the composer pull request.

Provider verification

  • Real-provider calls are described honestly, or none were made: none. Tests replace AgentRuntimes.stream with a model double, as the existing assistant tests do.

Bilingual UI

  • Both locales are updated, or no user-facing copy changed: no user-facing copy changes here. The new 409 detail and code follow the pattern the existing assistant routes already use; the localized composer copy ships with the frontend pull request in both locales.

## summary

- Replace the single-per-project `active` slot with a `Chain` that covers the
  running turn together with the messages queued behind it, so the moment
  between one turn ending and the next starting stays closed.
- Accept a supplementary message while a turn runs: `POST /assistant/messages`
  starts a turn when the project is idle and answers 202 with the queued
  message when one is running. The message is stored in the conversation at
  once, shows up as `queued`, and starts when the active turn ends.
- Give every event a `seq`, let `GET /turns/{id}/events?after=` resume from a
  cursor, and keep one stream across turns: an `end` frame that carries
  `nextTurnId` means the next reply continues on the same connection.
- Stopping ends the whole chain and marks messages that never started as
  `cancelled`, so a stopped run cannot leave work that begins later by surprise.

## validation

- `python -m pytest tests/test_assistant_queue.py tests/test_assistant_conversations.py -q`
  → 35 passed.
- Full `tests/` run against the same environment: the failures that remain
  (missing libx264 / ffmpeg options on this host) are identical with and
  without this change.
- `lint-imports` and `python scripts/generate_contracts.py --check` pass.

Refs: ThinkFlowLab#379
A viewer that lost the connection between two turns holds the turn it saw
last, which is already finished while its chain keeps writing. Asking for that
turn's stream answered 404, so the only way back was a reload.

The turn is looked up in the conversation it belongs to and compared with the
running chain's conversation, so a stale turn of the same thread finds the
stream while a turn from anywhere else is still refused.
@wtz2333

wtz2333 commented Nov 17, 2026

Copy link
Copy Markdown
Author

Pushed one more commit to this branch rather than opening a follow-up PR, because it fixes something this PR itself introduces.

What broke. Before chains, one reply meant one stream: a viewer that lost the connection either got the run back or got 404 and re-read the conversation. With a chain, the stream outlives the turn, so a viewer that reconnects between two turns holds a turnId that is already finished. GET /turns/{turn_id}/events matched only the chain's current turn, so that viewer got 404, the client treated the reply as ended, and the only way back was a reload — exactly the moment when a long chain is worth watching.

What changed.

  • Conversations.conversation_of(project_id, message_id) finds the conversation that holds a message, current or archived.
  • GET /turns/{turn_id}/events still matches the chain's current turn and id first; when that fails it resolves the turn's conversation and compares it with the running chain's conversation. Only a turn of the same thread opens the stream, so a stray id is still 404.

Test. test_a_viewer_that_lost_the_stream_resumes_through_the_turn_it_remembers starts a turn, queues a second one, finishes the first, then reconnects with the first turn id while the second is running: the replay equals the frames the original stream received. The same test asserts a random id is still 404. Local: 39 passed across tests/test_assistant_retract.py, tests/test_assistant_queue.py, tests/test_assistant_conversations.py; lint-imports 4 contracts kept.

Also: the test fixtures now release their gates on teardown, which took ~10s off the queue suite (a cancelled run was leaving a worker thread waiting).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent runtime, assistant chat and tools type:feat New capability or behaviour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant