Skip to content

[feat] agent: take back a queued message and continue a stalled queue - #384

Draft
wtz2333 wants to merge 3 commits into
ThinkFlowLab:mainfrom
wtz2333:feat/midturn-retract
Draft

wtz2333 wants to merge 3 commits into
ThinkFlowLab:mainfrom
wtz2333:feat/midturn-retract

Conversation

@wtz2333

@wtz2333 wtz2333 commented Nov 17, 2026

Copy link
Copy Markdown

Summary

  • DELETE /assistant/messages/{id} takes back a message that is still waiting behind the active turn. It runs under the same admission lock the chain takes when it picks the next message up, so a message is either still waiting and goes away, or it has already started and the caller gets 409 telling it to stop the reply instead of pretending the message can vanish.
  • POST /assistant/queue/dispatch continues the queue when nothing is running. A queued message outlives a restart, and a turn that failed leaves the queue waiting rather than spending it, so there has to be an explicit way to start the oldest one without retyping it.
  • Retracting also emits a retracted frame on the chain's stream, so another open viewer drops the bubble instead of showing a message that is no longer there.

Stacked on #381 (feat/midturn-queue): this branch contains that commit too, because both touch the queue. Please review #381 first; once it lands I will rebase this one on main and it will show only the retract/dispatch commit.

Linked issue

Refs #379

  • Refs #379 is included

Labels

  • Not applied: wtz2333 cannot add labels here. Suggested: type:feat, area:agent, area:harness.

Test plan

  • tests/test_assistant_retract.py (new, 3 cases):
    • a queued message is withdrawn while the running turn keeps going and nothing runs after it;
    • a message that already started answers 409 and an unknown id answers 404;
    • a failed turn leaves the queue in queued, POST /queue/dispatch then runs it, and a second call answers 409.
  • Regression: pytest tests/test_assistant_retract.py tests/test_assistant_queue.py tests/test_assistant_conversations.py -q → 38 passed. lint-imports → 4 contracts kept.

Evidence that it works

  • Media attached, or N/A because: this is an API contract; the three cases above pin the behaviour, and the UI for it is a separate change.

Provider verification

  • Real-provider calls are described honestly, or none were made: none; the tests replace the runtime stream.

Bilingual UI

  • No user-facing copy changes in this PR; the English copy for the error messages is served by the existing localized_error_for_language table when the client sends Accept-Language.

## 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.
A message waiting behind the active turn could be changed by nothing but
sending it: withdrawing it meant stopping the whole chain, which also threw
away the turn that was running.

Retracting removes a message that has not started, under the same lock the
chain takes when it picks the next one up, so it is either still waiting and
goes away or it has started and the caller is told to stop it instead. A
failure leaves the queue waiting rather than spending it, and continuing by
hand starts the oldest message without retyping it.

Refs ThinkFlowLab#379
@wtz2333
wtz2333 force-pushed the feat/midturn-retract branch from 478bfb0 to 2539f5f Compare November 17, 2026 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant