已合并
[fix]修复AgentContext.template_vars未渲染到主Agent的System Prompt中问题 #44
yuliangbin创建于 5月27日
[fix]修复AgentContext.template_vars未渲染到主Agent的System Prompt中问题 #44
已合并
yuliangbin创建于 5月27日
yuliangbin成员
5月27日

一、问题现状:
environments.md 里的占位符没有被替换,LLM 收到的还是 {working_dir}、{platform}、{current_date_time_zoned} 这类字面量。
关联issue:
https://gitcode.com/Ascend/msagent/issues/24

二、问题原因:
template_vars 虽然已经有真实值,但主 Agent 在发起模型调用前没有正确渲染 system_message。
同时,prompt 中还混有 {worker}、{Rank_ID} 这类示例占位符,原来的整段 str.format(context) 一遇到未知变量就整段回退,导致本来能替换的变量也失效。

三、问题解决:

在 src/msagent/agents/factory.py 的 _SystemMessageMiddleware 中补充运行时 safe render 逻辑,用于在模型调用前渲染 system_message 中的模板变量。

  • 对 template_vars 中已知变量进行替换,例如 {working_dir}、{local_environment_context}。
  • 对未知占位符保持原样,例如 {worker}、{Rank_ID},避免因运行时缺少变量导致渲染失败。
  • 仅渲染纯文本内容或结构化 content block 中 type=text 的 text 字段,避免误改非文本字段。
  • 更新 SystemMessage 时保留原消息的 additional_kwargs、response_metadata、name、id 等元数据。

四、结果验证:
修复前日志里是:
Working directory: {working_dir}
Platform: {platform}
image.png
修复后日志里已经变成:
Working directory: /home/ylb/msagent
Platform: Linux
image.png

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 yuliangbin 的贡献)
Yyuliangbin成员
5月27日 强制推送  1 个提交:4027865d-fix template_vars
ascend-robot
ascend-robot成员
5月27日 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here
You can get sig-info at here


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-Ascend/msagent 刘鹏程, 张伟 (2/2) 刘鹏程, 张伟 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

yu_liangbin, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robot成员
5月27日 添加了label:ascend-cla/yes
Yyuliangbin成员
5月27日 修改标题为 “[fix]修复AgentContext.template_vars未渲染到主Agent的System Prompt中”,原标题为“fix template_vars”
此处折叠了58条消息 查看更多
ascend-robot
ascend-robot成员
6月2日 评论:

The following label is not ready.

lgtm: Please wait for reviewers to review the code.

likedislike
Mrtutu
Mrtutu成员
6月2日 评论:

/approve

likedislike
ascend-robot
ascend-robot成员
6月2日 评论:

The following users do not have permission to comment /lgtm or /approve on any module in this PR:
Xilun_Liu

likedislike
ascend-robotascend-robot成员
6月2日 添加了label:lgtm
ascend-robotascend-robot成员
6月2日 合入了pull request