Repository navigation
Description
Problem
Target: rust-preview, inspected at 606a11c27658484d52aa43c4f92066e68e6623f2.
The documented Bot flow separates publishing a message from ending the Turn:
- The model calls
SendUserto publish its final reply. - Another model request is made.
- The model calls
sleepto end the Turn.
This separation is useful when the Bot sends an intermediate update and still has work to do. However, when the reply is final, the additional request may serve only to select sleep, carrying the conversation context again.
The current tool registration gives SendUser ordinary exclusive-step semantics and sleep direct-only FinishTurn semantics. The standing instructions explicitly require separate steps.
For motivation, I inspected a 24-hour local TypeScript-based run associated with #6633:
| Observation | Result |
|---|---|
| Bot model calls | 331 |
| Send steps immediately followed by a separate sleep step | 93 |
| Input tokens in those 93 sleep requests | 58,061,472 |
| Cache-hit share of that input | 99.89% |
| Median duration of those sleep requests | 6.1 seconds |
If all 93 pairs could be combined without changing other behavior, that would remove about 28.1% of the Bot’s calls in this sample. This is a static estimate, not a measured Rust improvement. Most of the input was cached, so cost savings would be substantially smaller than the raw-token reduction. The latency is also tail latency after the reply was published, not time until the user first sees the reply.
Desired outcome
Provide an explicit way for the Bot to publish its final reply and request Turn completion in one model step.
The existing behaviors should remain available:
- Send an intermediate message and continue working.
- End silently when there is nothing worth sending.
The combined path should preserve the current guarantees:
- Commit messages through the existing durable outbox before reporting successful completion.
- Reuse the existing
messageIddeduplication; recovery must not publish the same reply twice. - Preserve steering and queue behavior. New input must remain available for the current or next Turn and must not be marked consumed merely because a reply was published.
- Preserve Bot-only access and the existing restrictions on Turn-ending tools.
- A failed or uncertain publication must not be treated as successful “send and finish.”
Verification should cover message arrival before, during and after publication, as well as interruption after the outbox commit but before Turn completion.
Alternatives or workarounds
The current workaround is to keep using SendUser followed by sleep.
One possible interface, open for discussion, is an optional reply argument on the existing direct-only sleep tool. For example:
{
"reply": {
"messageId": "result-1",
"messages": [
{ "text": "The task is complete." }
]
}
}sleep({}) would retain silent completion, while ordinary SendUser would retain intermediate publication. This could reuse the existing outbox and FinishTurn mechanisms without adding a sleep duration or another scheduling layer. The exact interface is open to maintainer preference.
Related discussion:
- feat(agent): give the Bot a sleep tool that waits for events #6505 introduced explicit event-based sleep.
- The maintainer’s reply in #6711 proposes requiring tool calls where supported. This proposal is compatible with that direction: final publication would still be an explicit tool action. It addresses the extra round trip after publication, while Bot: a Turn that answers in plain text without SendUser or sleep leaves the answer out of Chat #6711 addresses replies that never reach the user.
No Rust implementation or runtime validation of the proposed combined path has been performed.
AI assistance: OpenAI Codex assisted with source/log inspection and drafting.
简体中文
问题
目标分支:rust-preview,检查的提交为 606a11c27658484d52aa43c4f92066e68e6623f2。
当前 Bot 文档中的流程将发送消息与结束本轮分开:
- 模型调用
SendUser,发送最终回复。 - 再次发起模型请求。
- 模型调用
sleep,结束本轮。
当 Bot 发出中间进展、后面仍需继续工作时,这种分离是有用的。但如果已经是最终回复,额外请求可能只是为了选择 sleep,同时再次携带整份对话上下文。
当前工具注册为 SendUser 使用普通的独占步骤语义,为 sleep 使用只能直接调用的 FinishTurn 语义。系统提示词也明确要求分成不同步骤执行。
作为优化动机,检查了与 #6633 相关的 TypeScript 本地版本最近 24 小时运行记录:
| 观察项 | 结果 |
|---|---|
| Bot 模型调用 | 331 次 |
| 发送后紧接着单独执行 sleep | 93 组 |
| 这 93 次 sleep 请求的输入 token | 58,061,472 |
| 这些输入的缓存命中比例 | 99.89% |
| 这些 sleep 请求的耗时中位数 | 6.1 秒 |
假设这 93 组都能合并,其他行为不变,则可以减少该样本中约 28.1% 的 Bot 模型调用。这是静态估算,不是已经测得的 Rust 优化效果。由于大部分输入命中了缓存,实际费用下降会明显小于原始 token 的下降比例。这里的耗时也是回复发出后的收尾等待,不是用户首次看到回复前的等待。
期望结果
提供一种显式方式,让 Bot 在一次模型步骤中发送最终回复,并请求结束当前轮次。
同时保留现有能力:
- 发送中间消息后继续工作。
- 没有值得发送的内容时,直接静默结束。
合并后的路径应保持现有保证:
- 通过现有持久 outbox 提交消息,成功后才报告正常完成。
- 复用现有
messageId去重,恢复时不能重复发送同一条回复。 - 保留追加消息和队列语义。新输入应留给当前或下一轮处理,不能因为提交了回复就被标记为已消费。
- 保留 Bot 专属权限及结束轮次工具的调用限制。
- 消息提交失败或结果不明时,不能当作“发送并结束”成功。
验证应覆盖消息在提交前、提交中、提交后到达,以及 outbox 已提交但轮次尚未结束时发生中断的情况。
替代方案或临时做法
目前可以继续使用 SendUser 后再调用 sleep 的方式。
一个可讨论的接口是:给现有只能直接调用的 sleep 增加可选 reply 参数。例如:
{
"reply": {
"messageId": "result-1",
"messages": [
{ "text": "任务已经完成。" }
]
}
}sleep({}) 保留静默结束,普通 SendUser 保留中途发送。这可以复用现有 outbox 和 FinishTurn 机制,不增加休眠时长参数或新的调度层。具体接口形式可以由维护者讨论决定。
相关讨论:
- feat(agent): give the Bot a sleep tool that waits for events #6505 引入了显式的事件驱动休眠。
- #6711 中维护者的回复建议,在支持的情况下要求模型调用工具。本建议与该方向兼容:最终发送仍然通过显式工具操作完成。这里关注的是发送之后多出的一次往返,而 Bot: a Turn that answers in plain text without SendUser or sleep leaves the answer out of Chat #6711 关注的是回复没有真正发给用户。
目前尚未实现 Rust 合并路径,也未对该方案进行运行验证。
AI 使用说明:OpenAI Codex 协助检查源码、日志和起草本 issue。
Activity