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.

@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
@wtz2333
wtz2333 force-pushed the feat/midturn-steer branch 2 times, most recently from 987529d to 0cfba12 Compare November 19, 2026 04:36
config = await asyncio.to_thread(configs.resolve, message['modelConfigId'], 'agent')
except Exception:
logger.warning('agent.queue.model_missing project_id=%s message_id=%s',
project_id, message.get('id'))
wtz2333 and others added 4 commits November 26, 2026 11:09
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
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
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.

This branch has not been deployed

No deployments
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.

2 participants