Skip to content

fix(agent): read a reasoning block back while its run is still writing - #1702

Merged
jiangzhuqing000 merged 3 commits into
mainfrom
pr/dev-jiangzhuqing000
Nov 26, 2026
Merged

jiangzhuqing000 merged 3 commits into
mainfrom
pr/dev-jiangzhuqing000

Conversation

@jiangzhuqing000

Copy link
Copy Markdown
Collaborator

问题

移动端在推理还在流式输出时展开思考块,看到的是「这段推理暂时读不到」;等这一轮回复结束,同样的展开又能读到内容。桌面端一直正常。

原因

移动端为了省流量不接收推理正文的增量,展开时才按需向 agent 读一次文本(get_reasoning)。而 agent 的这条读取路径此前只查已持久化的消息块——推理块跟随承载它的消息在轮次结束时才写入存储,所以流式期间读到 text = null,前端落到 thoughtUnavailable,轮次结束、块落库后自然就有内容了。桌面端不受影响,因为它自己就在消费流式增量。

改动

  • agent/src/rpc/broadcaster/reasoning.rs(新增):reasoning_blocks 从一次运行的 SSE 序列折叠出逐段推理正文,规则逐字对齐 packages/thread-projection/src/liveApply.ts(thinking_start 开启、thinking_delta/thinking_chunk 追加、thinking_end 关闭,兼顾匿名起始与孤立增量)。
  • agent/src/rpc/broadcaster/replay.rs:SseBroadcaster::live_reasoning_blocks,仅在 run id 命中、投影完整、未超预算且尚未 agent_end 时返回;读的是 trim_projection 之前的内存投影,不改变快照既有的「清空推理正文」行为。
  • agent/src/rpc/commands/mod.rs:get_reasoning 改为先读存储、存储无文本时才用实时投影兜底。已结束的轮次仍然以存储为准,行为不变。
  • 完整性优先:以 content_refs 形式传送的块(content_bytes > text.len())判定为不完整,返回 null 而不是截断的前缀;空块同样返回 null。
  • 客户端:desktop/src/features/agent/ThinkingBlock.tsx 与 mobile/src/features/chat/messages/ThinkingRow.tsx 给读回来的文本带上 live 标记,块平息后重读一次以替换成存储里的完整版本;已经展开的行保留原有文本,不跳变。desktop/src/features/remote-peer/remoteReasoning.ts 里 null 的语义注释一并纠正。

不涉及主机、远程协议或 capability(reasoning_fetch_v1 不变),也不需要重新生成 proto。

验证

  • cargo fmt -p future-agent --check、cargo clippy -p future-agent --all-targets -- -D warnings 通过;cargo test -p future-agent 3129 个通过。整跑中 3 个与本改动无关的用例偶发失败(两个 session 删除用例、一个 Windows 沙箱探针),单独运行均通过。
  • 桌面:tsc --noEmit、eslint 干净,vitest run src/features/agent src/features/remote-peer 2420 个通过。
  • 移动:tsc --noEmit 干净,jest 1394 个通过(MarkdownText.test.ts 仅在临时工作树中因 node_modules junction 解析失败,主检出通过)。
  • 新增测试:broadcaster/reasoning.rs 单元测试 4 个、broadcaster/tests/live_reasoning.rs 3 个、dispatcher_tests 中「运行还在写时能读到那段推理」、ThinkingBlock.test.tsx 与 TimelineCard.test.ts 中各一个「块平息后重读」用例。

A lean lane sends a reasoning row's boundaries and not its body, and the row
fetches the text when a reader opens it (`get_reasoning`). That read only ever
looked at settled entries, and a reasoning block reaches the store with the
entry that carries it — at the end of the run — so opening a thinking row
mid-reasoning read back nothing and the phone said the reasoning was
unreadable, while the desktop (which folds the run's own events) showed it.

The text is not missing: it is in the run's live projection, the fold a
streaming reply is drawn from. `get_reasoning` now answers from that projection
when the stored block is missing, folding it the way the reply is rendered
(start opens a block, deltas append to the block their id names or to the open
one, end closes it) so a row's index means the same thing on both sides. The
projection is read before `trim_projection`, which is what takes reasoning text
out of a snapshot meant for a client, and only for the run this process is
writing and whose projection covers the whole run: a finished run, an unknown
one, or a projection that is a tail answers nothing, as before. A fragment that
rode the wire as a content reference is a hole rather than a short thought, so
that block is reported unreadable instead of truncated.

Both clients keep such a body as a prefix: once the block settles, opening the
row again reads the complete text instead of presenting what was on screen
when the reader first opened it as the whole thought.
@jiangzhuqing000
jiangzhuqing000 enabled auto-merge (squash) November 26, 2026 09:32
@jiangzhuqing000
jiangzhuqing000 merged commit 8f2bea5 into main Nov 26, 2026
52 of 54 checks passed
@jiangzhuqing000
jiangzhuqing000 deleted the pr/dev-jiangzhuqing000 branch November 26, 2026 10:05
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