Skip to content

s08: compact 工具在替换历史后追加了孤立的 tool_result——下一次 API 请求报 400 并导致进程崩溃 #500

Description

@lys849

您好,我在阅读s08的代码时发现488行处如果模型想要主动调用compact_historyresults.append({"type": "tool_result", "tool_use_id": block.id, ...})仍然引用了已经删除的block.id,导致可能的崩溃。

具体复现步骤如下:

  1. 运行 python s08_context_compact/code.py
  2. 让模型调用 compact 工具(如"请调用 compact 工具压缩上下文")
  3. 观察到 > compact、transcript 落盘后崩溃
  4. 实际报错信息有:anthropic.BadRequestError: Error code: 400 - {'error': {'message':
    'unexpected messages.1.content.0: tool_use_id found in tool_result blocks: call_00_8u7tPGlQ6bLAxnweyCk24860.
    Each tool_result block must have a corresponding tool_use block in the previous message.', ...}}

我注意到s20相应的地方已经修改,推测已经注意到这个地方的错误,只不过s8没有进行对应的修改?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions