Skip to content

[feat] agent: insert a message into a reply that is already running - #402

Draft
wtz2333 wants to merge 4 commits into
ThinkFlowLab:mainfrom
wtz2333:feat/midturn-steer
Draft

wtz2333 wants to merge 4 commits into
ThinkFlowLab:mainfrom
wtz2333:feat/midturn-steer

Conversation

@wtz2333

@wtz2333 wtz2333 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A reply that is already running can now take one more line of text: POST /assistant/messages with delivery: "insert" hands the text to the running turn instead of waiting for it to end.

  • Codex now runs over codex app-server (JSON-RPC over stdio) instead of codex exec. exec reads one prompt and exits, so nothing could reach the model once a turn started; the app server keeps the turn open and exposes turn/steer and turn/interrupt.
  • appserver.py drives one turn: initialize → thread/resume or thread/start → turn/start → turn/steer while it runs. A saved thread that cannot be opened falls back to a fresh thread with the whole authoritative history, so a lost rollout never quietly drops the conversation.
  • RuntimeSpec.supportsSteering states who can do this; only Codex is true today. POST /messages refuses insert on a runtime that cannot steer (409, naming the runtime), when nothing is running (409), when the turn just ended (409), and when the message carries assets, skills or rich content — inserted text is text only.
  • Inserted messages are stored with status: "inserted" in the place they arrived (before the streaming reply), and the stream reports {"type": "steered", "message": ...}.

Depends on #381 (this branch is based on feat/midturn-queue); merge #381 first.

Refs #379.

Labels

The author cannot set labels on this repository (AddLabelsToLabelable is refused for wtz2333): type:feat, area:agent, area:canvas, area:harness.

Test plan

  • pytest tests/test_assistant_steer.py tests/test_assistant_queue.py tests/test_assistant_conversations.py -q → 41 passed
  • pytest tests/test_native_runtime.py -q → 25 passed, 5 skipped (the Codex CLI is exercised for real, over the new app-server protocol)
  • pytest tests/test_assistant_retract.py -q → 3 passed (on the stacked demo branch)
  • lint-imports → 4 contracts kept; python scripts/generate_contracts.py --check → verified

Evidence

Live check on the demo server (real Codex CLI, no mocked stream): first turn streaming → POST /assistant/messages delivery=insert → 202 with status: "inserted" → same turn finishes and its reply ends with the inserted instruction ((已插入)), stored as:

user      complete   用一句话介绍你自己。
user      inserted   补充:回答最后加上“(已插入)”。
assistant complete   …(已插入)

Two failures this transport found

Both only appear against the real binary, which is why they are covered by tests now:

  • codex app-server rejects the exec-only --ignore-user-config; the process exited before initialize and the turn failed with "app-server 没有响应". The server now gets a private CODEX_HOME (project-scoped, only auth.json copied), which is the same isolation and keeps the rollout with the project so the next turn can resume it.
  • The shared command tail appended --model, which the server also rejects (the model travels in turn/start). Codex no longer receives the flag.

Frontend

Follows in a separate PR: a running reply offers 「立即插入 / 排队 / 停止」, and the inserted line shows in the conversation as it arrives.

Provider

No provider request is made by these tests beyond the Codex CLI itself; attachments are refused for inserts, so no upload path is exercised.

Bilingual

The API returns Chinese errors like the rest of the assistant routes. UI strings ship with the frontend PR, zh-CN and en-US together.

## 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.
@github-actions github-actions Bot added area:agent Agent runtime, assistant chat and tools area:harness Harness and skill integration (Default, OpenCode, Codex, Claude Code, Jiuwen, DSH) type:feat New capability or behaviour labels Nov 19, 2026
@github-actions
github-actions Bot requested review from FayeSpica and wuzhf9 November 19, 2026 02:02
@wtz2333
wtz2333 marked this pull request as draft November 19, 2026 02:22
Codex now runs as an app-server session instead of a one-shot exec, so a turn has a
channel for extra text while it works: POST /assistant/messages with delivery
"insert" hands the words to turn/steer and the answer keeps the work it has
already done. Runtimes without a native channel refuse the insert and say so,
leaving the queued delivery as the fallback.
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 area:harness Harness and skill integration (Default, OpenCode, Codex, Claude Code, Jiuwen, DSH) type:feat New capability or behaviour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant