Skip to content

fix(agent): the reasoning a deepseek turn must echo back; a user message is not a reply - #1371

Merged
huichen merged 22 commits into
mainfrom
pr/huichen
Oct 26, 2026
Merged

huichen merged 22 commits into
mainfrom
pr/huichen

Conversation

@huichen

@huichen huichen commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

What this carries

Two fixes on top of the release that just landed (#1370).

1. fix(agent): the reasoning a deepseek turn must echo back

The bug, observed twice on this machine today, both mid-run:

LLM API request failed (HTTP 400): The `reasoning_content` in the thinking mode
must be passed back to the API.
fail  run-20261026-170438-ff0559   17:04:39   807 s of work discarded
fail  run_365d814f50c3441ebcfc84b30a386403   17:40:09    89 s discarded

Why: DeepSeek's thinking mode requires the previous assistant turn's reasoning_content to be echoed back on the follow-up request. The chat adapter only replayed it when compat.requiresReasoningContentOnAssistantMessages was set — and the registry entries for deepseek-flash / deepseek-v4-pro carry only thinkingFormat: "deepseek". So every follow-up went out stripped of the reasoning the API had just asked to keep, and a tool-call round trip was refused.

The change: thinkingFormat: "deepseek" now implies the replay, because it is a property of the wire format rather than of one registry entry. An explicit compat boolean still overrides it in either direction. agent/src/llm/schema.rs gains the implication plus the boolean parsing; agent/src/llm/adapters/openai_chat.rs gains a has_payload guard so the replay can never resurrect a reasoning-only assistant turn left behind by a crash — that shape is refused with a different 400 ("content or tool_calls must be set").

The guard is load-bearing: removing has_payload && turns the corresponding assertion red; verified by temporarily reverting it, then restoring.

Blast radius, checked rather than assumed. Six other registry entries also carry thinkingFormat: "deepseek" — kimi-k2.6, kimi-k2.7-code, kimi-k3, kimi-k3.1-pro, doubao-seed-2.1-pro — and they start replaying too. I checked whether that is safe instead of guessing: Moonshot's own platform documentation says, for thinking models, "For multi-turn conversations and tool calls, pass the complete assistant message returned by the API back to messages as-is (including reasoning_content)" (platform.kimi.ai/docs/api/models-overview, use-thinking-models). Independent reports of the same 400 against Moonshot (reasoning_content is missing in assistant tool call message) confirm these models were already at risk of the identical failure — the deepseek format simply was not replaying for them either.

That makes this a fix for those entries, not a regression introduced into them. If any one of them turns out to reject the field, requiresReasoningContentOnAssistantMessages: false on that entry reverts it alone — which is what keeping the explicit override is for.

2. fix(desktop): a user message is not a reply

MessageFrame.tsx treated both the same way, so a user message was framed as though the assistant had said it. Two files plus its test.

Verification

  • cargo test -p future-agent --lib llm: 202 passed, 0 failed (new cases: deepseek_chat_body_replays_assistant_reasoning, deepseek_thinking_format_implies_assistant_reasoning_replay, deepseek_thinking_replays_reasoning_or_the_request_is_refused).
  • cargo clippy -p future-agent --all-targets -- -D warnings: clean except the pre-existing chunks_exact_to_as_chunks lint in agent/src/sandbox/{mod,windows/token}.rs, untouched here (Linux CI does not compile that file).
  • Desktop: tsc --noEmit, eslint, and the MessageFrame behaviour test.
  • Full cargo test -p future-agent --lib: 2504 passed / 2 failed — grpc::tests::the_public_serve_wrappers_bind_serve_and_report_bind_failures and service::approval_tests::shutdown_releases_a_blocking_tool_approval ("Agent shutdown deadline exceeded during Windows sandbox permissions"). Both reproduce with this change stashed, so they are pre-existing and Windows-specific, not caused here.

Not verified

No real API call: the fix is validated against a mock server asserting the DeepSeek wire contract, not against the live future-os.cn endpoint. The two recorded failures are the evidence that the contract matters; this PR is the first place it is satisfied.

A remote peer that is switched off made the desktop log noisy on two layers,
both at once:

- `async_nats` logs `connected successfully ...`, `event: connected` and
  `event: closed` at INFO, and a dead peer makes the client connect and drop
  once per attempt. The embedded tracing filter already pins h2/rustls/tonic/
  tower to WARN for the same reason; `async_nats` now sits with them, so its
  WARNs (the ones that matter) still print and the churn does not.
- The reconnect supervisor printed one `remote_peer: reconnect attempt ...
  failed` line per backoff step: eighteen identical lines per retry window, a
  window every few minutes, for as long as the pairing exists. It now says the
  first failure of a window in full, counts identical repeats, and reports the
  count once when the message changes or the window closes. A different error
  still prints immediately, carrying the count of the run it interrupted.
The task was to make `ctrl-e` fold every settled reply, with a regression test
that first fails. The test that the cancelled worker wrote for exactly that
expectation **passes on this code**, so there is no red to show: the reading
level has applied to the whole transcript, not just the newest reply, since the
feature landed (`1d8a972c`), and `git log -p 1d8a972..HEAD -- chat_area.rs`
shows no change to the fold since.

Evidence that this is not a shallow pass:

* `reply_fold_layout` walks the whole message list and cuts a segment at every
  user/system boundary, so every settled reply is a candidate — the fold was
  never limited to the newest one;
* `prepend_messages` (how an older page arrives) calls the full `rerender()`,
  so history that lands after the toggle is folded with everything else;
* 8 further sequences were driven by hand and a 24-seed x 40-step fuzz over
  {stream a turn, prepend a stored page, toggle, render} checked after every
  step that no folded row is painted at the folded level. All green.

Three *deliberate* rules can still leave a turn expanded, and they are where the
report most likely comes from. They are documented behaviour, not this bug:

1. a reply with no prose stays whole (`fold_reply_segment` needs an answer);
2. an unsettled reply stays whole — and `Live::abort` only calls the agent, so a
   run that ends without `agent_end` leaves its turn `pending` for the session;
3. linear reading mode replays the transcript only when the session changes
   (`LinearReader::sync_session`), so a toggle after entering it cannot rewrite
   the scrollback already emitted.

So the test is kept as a pin rather than presented as a fix, and the next step
is the user's actual repro (linear mode? a cancelled run? a turn with no prose?),
not a speculative change to the fold.
…id in it

The workspace section's '+' on a paired machine's list registers a folder on that machine, and the folder then existed there with no conversation in it — so the merged list, which builds a workspace group from the rows under it, had nothing to build one from. The create succeeded, the host could list the workspace, and the list went on showing nothing; this machine's own empty folders were always drawn, from its own workspace store, which is what made the two lists disagree about a place that plainly exists.

The host's workspaces now travel with its sessions: read beside them on the same cadence in useRemotePeers (a failed read keeps the last list instead of emptying every heading), applied from the pushed state.workspaces snapshot the hook used to gate and discard, and carried through RemoteCatalog. groupMergedRows seeds a group for a host workspace with no rows — once, never beside the group its rows already built, and not for a temporary workspace, which holds chats rather than folders.
Typing on the home screen and pressing enter produced nothing: no session was
created and no prompt reached the agent. The same was true of every message sent
from the workbench, launchpad or not.

The driver cleared the surface's draft before handing `WbAction::Submit` to the
adapter, and `Live::submit` ignored the action's own text, re-reading the prompt
out of that draft. It therefore found an empty string, hit its `typed.is_empty()`
guard, and returned without touching the client — silently, because a submit with
nothing in it is a legitimate no-op. Clearing the draft first also threw away the
folded pastes and image attachments the draft was carrying.

Two changes, both about who owns the text:

* `submit` takes the action's text as the authority and uses the draft only while
  it still holds something, because the draft is what carries the folded pastes
  that `pending()` expands into the prompt;
* the driver no longer clears the draft at all. `submit` clears it itself once
  the prompt is on its way, which also means the reader's text survives a session
  that could not be started, so it can be sent again.

The regression test drives the real `Live` against the crate's agent test double
in the driver's exact order and asserts `new_session` then `prompt` are sent and
the user row is painted. Two companion tests pin the repaired paths: the ordinary
one with the draft intact, and the failed-session one where the draft must stay.

Red before the fix, from the same test:

    the first message must create a session,
    saw ["get_state", "list_sessions", "list_models", "get_session_entries"]

Green after: 1214 library tests pass, plus 6 CLI, 8 i18n and the perf probe;
`cargo fmt --check` and `clippy -D warnings --all-targets` clean.

`grpc_client.rs` changes are test-only: the agent test double and its constructor
are `pub(crate)` so a test outside that module can use them.
The workspace-level test only ever reached `render()` + `prepend_messages` +
toggle, so four paths the fold actually uses had no coverage. They do now, and
all four are green — no defect found in any of them:

* `incremental_streaming_keeps_every_turn_folded` — establish the width once,
  then drive turns through `add_message` / `append_to_thinking` / `add_tool_start`
  / `finish_tool` / `append_to_last_message`, re-reading after every step. Asking
  for a new width or leaving `dirty` set would rerender everything and hide the
  splice path, so neither happens.
* `toggling_after_incremental_streaming_folds_everything` — the level flipped
  *after* the incremental stream, which is the order that exercises
  `sync_compact_layout` deciding to rebuild.
* `resizing_while_folded_keeps_every_turn_folded` — 120/64/40/100 columns, each
  compared against a fresh layout at that same width.
* `a_stored_tool_row_is_never_running` — the premise behind the "unsettled tool"
  suspicion: `session_rows::rows_to_messages` gives every tool row `Complete` or
  `Error`, never `Running`, and never leaves one `pending`. A stored page cannot
  block its own fold that way.

Three mutations prove the coverage is real: making `sync_compact_layout` return
early, making the splice path ignore a grown layout, and making a width change
keep the previous wrap each turn exactly the test that claims that path red.

`an_interrupted_turn_keeps_its_slices_by_design` and its stored-page twin are
pins, not fixes. A turn that never produced an answer — an interrupted or
cancelled run, whose `agent_end` settles the messages without carrying text —
does not fold, because the fold isolates work *above an answer*. That is the
documented rule on `ReplyDetail::Default` and on `reply_fold_layout`, and it is
also the closest thing found to the report ("ctrl+e only collapses part of the
output; scrolling up I find the historical messages not collapsed"): one
interrupted turn in the history is exactly one stretch that stays expanded.
Folding it would leave a summary row with nothing under it, which is a product
decision. The tests say so in their names and comments so flipping the rule is a
deliberate act with an obvious place to edit.

No change to `abort()`; no change to the no-prose rule; no change outside
`chat_area.rs`.
…back

DeepSeek's thinking mode requires the previous assistant turn's
`reasoning_content` on the follow-up request; a tool-call round trip
without it is refused with HTTP 400 ("The `reasoning_content` in the
thinking mode must be passed back to the API"), which kills the whole
run mid-turn. The chat adapter only replayed reasoning when
`compat.requiresReasoningContentOnAssistantMessages` was set, and the
Future registry entries for deepseek-flash / deepseek-v4-pro carry just
`thinkingFormat: "deepseek"`, so every assistant turn went out stripped
of the reasoning the API had just asked to keep.

`thinkingFormat: "deepseek"` now implies the replay; an explicit compat
boolean still overrides it, and reasoning-only assistant turns (a crash
can leave one in the journal) are still dropped, so the replay cannot
emit {role, reasoning_content}`, which the same API refuses with
"content or tool_calls must be set".

Two failures recorded on this machine today, both mid-run:
  run-20261026-170438-ff0559 (807s) and
  run_365d814f50c3441ebcfc84b30a386403 (89s).
@huichen
huichen enabled auto-merge (squash) October 26, 2026 10:05
@huichen
huichen merged commit 436416b into main Oct 26, 2026
25 checks passed
@huichen
huichen deleted the pr/huichen branch October 26, 2026 10:18
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