Skip to content

pref(tools):优化tools调用提示词以及格式#232

Merged
phantom5099 merged 3 commits into
1024XEngineer:mainfrom
phantom5099:main
Apr 12, 2026
Merged

pref(tools):优化tools调用提示词以及格式#232
phantom5099 merged 3 commits into
1024XEngineer:mainfrom
phantom5099:main

Conversation

@phantom5099

Copy link
Copy Markdown
Collaborator

变更说明

本次 PR 聚焦解决 NeoCode 在任务执行过程中反复调用同一工具的问题,并优先落地两项最小、直接、可验证的优化。

第一项是升级 tool result 的回灌格式,不再只把裸 Content 返回给模型,而是把工具名称、执行状态、调用 ID、截断标记以及关键 metadata 一并整理成稳定的结构化文本,让模型能准确理解“这次工具调用到底发生了什么”。

第二项是补充参考 Codex 风格的 anti-loop prompt 约束,在默认系统提示中明确“最小必要工具”“不要重复相同参数调用”“写入成功后最多做一次定向验证”“结果已回答问题时应停止工具调用”等行为边界。

本次改动不引入新的顶层模块,也不提前加入 runtime 侧的硬拦截去重逻辑,而是先通过更强的结果表达和更明确的提示词约束,降低重复调用的发生概率。

主要改动

  • internal/tools 新增统一的模型侧 tool result 格式化逻辑,将工具结果收敛为包含 toolstatustool_call_idtruncatedmeta.*content 的结构化文本。
  • internal/runtime 中调整 tool message 回灌方式,写回 session 和 provider 上下文时不再只传递原始 ToolResult.Content,而是传递格式化后的结构化结果。
  • internal/context 的默认系统提示中补充 anti-loop 规则,明确最小必要工具原则、同参数重复调用限制、写入后验证上限以及何时应停止工具调用并直接回答用户。
  • 更新 internal/toolsinternal/contextinternal/runtime 相关测试,覆盖工具结果格式化、提示词规则和 runtime 回灌链路。

预期收益

  • 降低模型因只看到裸 ok 或弱结果而继续重复调用同一工具的概率。
  • 提高模型对工具成功、失败、截断和附带上下文信息的理解精度。
  • 在不改动 provider 协议和不新增复杂模块的前提下,先收敛一轮最常见的重复调用问题。
  • 为后续如果需要补 runtime 侧“同工具同参数重复调用保护”提供更稳定的输入基础。

@phantom5099

Copy link
Copy Markdown
Collaborator Author

/review -codex

@codecov

codecov Bot commented Apr 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.17391% with 32 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/tools/format.go 69.41% 19 Missing and 7 partials ⚠️
internal/context/compact/helpers.go 0.00% 3 Missing and 1 partial ⚠️
internal/context/builder.go 88.88% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

fennoai[bot]

This comment was marked as outdated.

@phantom5099

Copy link
Copy Markdown
Collaborator Author

/review -codex

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 1 issue in the new tool-result projection path.

continue
}
content := strings.TrimSpace(message.Content)
if content == "" || content == microCompactClearedMessage {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a tool succeeds with empty Content but meaningful ToolMetadata, this branch skips projection entirely. The provider adapters do not serialize ToolMetadata, so the next model round receives an empty tool message and loses the result semantics completely. That is reachable for generic ToolManager/adapter implementations even if most built-in tools currently return "ok". Please either project metadata-only tool results as well, or normalize successful empty-content results before they are persisted.

@phantom5099 phantom5099 merged commit b041d95 into 1024XEngineer:main Apr 12, 2026
1 of 2 checks passed
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