LoopForge icon indicating copy to clipboard operation
LoopForge copied to clipboard

[Bug]: 已成功提交回复的轮次被诊断记为 cancelled / context canceled

Open pkyes54 opened this issue 2 weeks ago • 2 comments

[Bug]: 已成功提交回复的轮次被诊断记为 cancelled / context canceled

Component / host: Platform — web / API Edition: Platform LoopForge version: v0.4.0(自托管镜像,诊断中平台版本显示为 (devel))

What happened?

一次正常完成的 AI 对话:终态工具 submit_assistant_reply 返回成功、界面正常输出答案、答案已落库。但导出的诊断文件把整轮记为"已取消":

{
  "status": "cancelled",
  "error": "context canceled",
  "attempts": [
    { "phase": "execution", "status": "cancelled", "error": "context canceled" }
  ],
  "assessment": { "outcome": "completed", "summary": "本轮已完成" }
}

时间线末尾可以直接看到先后顺序:

05:27:43.984359  gateway   submit_assistant_reply   completed
05:27:43.999111  platform  cli.cancelled            "context canceled"

即:先成功提交了本轮终态回复,15 毫秒后进程被平台自己结束,然后整轮被记成"已取消"。

原因链(代码定位)

  1. internal/httpapi/agent_tool_session.go:437-457 的 commitFinal() 在标记提交成功后, 主动取消模型进程:

    s.committed.Store(true)
    if s.CancelModel != nil {
        s.CancelModel()
    }
    

    这是既定语义——终态工具成功后立即结束本轮,提示词里也写着"该工具成功后立即结束本轮"。

  2. 这次取消让调用链返回 context.Canceled,进入 internal/httpapi/assistant_diagnostics.go:232-251 的 finish(err):

    attempt.Status = "completed"
    if err != nil {
        attempt.Status = "failed"
        attempt.Error = sanitizeAssistantDiagnostic(err.Error(), a.user, 4096)
        if errors.Is(err, context.Canceled) {
            attempt.Status = "cancelled"
        }
    }
    
  3. finish() 没有检查"本轮是否已经提交过终态结果"这个已知的成功信号 (AgentToolSession.committed)。于是一次平台自己发起的正常收尾, 与"用户点了停止""上游超时""部署重启"被记录成同一个状态。

影响

  • 用户与运维看到 cancelled 会去排查"谁取消的、为什么中断",但本次根本没有中断。
  • 诊断文件内部自相矛盾:status=cancelled 与 assessment.outcome=completed 并存, 排查时只能人肉读时间线才能判断这一轮到底成没成。
  • 任何基于诊断文件的自动判定(是否被用户中断、是否超时)都会把正常收尾误判为中断。

期望行为

finish() 区分"平台主动收尾"与"外部取消":

  • AgentToolSession.committed == true 时记 completed,可另加字段说明终止来源 (例如 terminatedBy: "terminal_tool");
  • 只有用户中断、上游超时、部署重启等外部原因才记 cancelled。

Reproduction

  1. 平台 v0.4.0,在 AI 对话里提一个会产生待确认草稿的问题(任何需要 submit_assistant_reply 的写操作)。
  2. 等终态工具提交成功、回答正常显示。
  3. 立即导出该轮诊断,观察顶层 status / error 与 attempts[].status。

Sanitized diagnostics

{
  "status": "cancelled",
  "error": "context canceled",
  "attempts": [
    { "id": "<attempt>/1", "phase": "execution", "status": "cancelled",
      "error": "context canceled", "endpoint": "<redacted>",
      "process": { "state": "not_started", "stdoutLines": 355 } }
  ],
  "timeline": [
    { "at": "...43.984359Z", "kind": "tool.completed", "source": "gateway",
      "tool": "submit_assistant_reply" },
    { "at": "...43.999111Z", "kind": "cli.cancelled", "source": "platform",
      "phase": "execution", "message": "context canceled" }
  ],
  "assessment": { "outcome": "completed", "summary": "本轮已完成" }
}

Safety checks

  • [x] 已移除凭证、私有端点、内部标识与个人数据。
  • [x] 已检索现有 issue,未发现重复。

pkyes54 avatar Nov 19 '26 05:11 pkyes54

感谢反馈。

我理解的情况是:一轮 AI 对话已经成功提交回复并正常显示,但导出的诊断仍把这一轮记成 cancelled,错误信息是 context canceled。

已经看到这条 Issue,会按现有 Issue 排期查看。

cocofhu00 avatar Nov 19 '26 06:11 cocofhu00

I will work on preserving the completed diagnostic outcome when a committed terminal reply deliberately cancels the model process, while keeping external cancellations and timeouts distinguishable. I will add regression coverage for both cases.

wcy12378 avatar Nov 19 '26 10:11 wcy12378