重构轮次警告机制并修复路径问题

- 提取next_prompt_patcher回调让handler自定义警告策略
- 修复turn显示不一致问题
- 每10轮注入全局记忆防止上下文丢失
- 强化autonomous_reports路径警告
- 新增subagent测试驱动SOP优化方法
This commit is contained in:
Jiaqing Liang
2026-02-27 21:41:52 +08:00
parent 75f8ef2564
commit bb2d595fb6
4 changed files with 31 additions and 7 deletions

View File

@@ -16,6 +16,7 @@ def try_call_generator(func, *args, **kwargs):
class BaseHandler:
def tool_before_callback(self, tool_name, args, response): pass
def tool_after_callback(self, tool_name, args, response, ret): pass
def next_prompt_patcher(self, next_prompt, outcome, turn): return next_prompt
def dispatch(self, tool_name, args, response):
method_name = f"do_{tool_name}"
if hasattr(self, method_name):
@@ -85,9 +86,6 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema,
datastr = json.dumps(outcome.data, ensure_ascii=False, default=json_default) if type(outcome.data) in [dict, list] else str(outcome.data)
next_prompt += f"<tool_result>\n{datastr}\n</tool_result>\n\n"
next_prompt += outcome.next_prompt
if (turn+1) % 7 == 0:
next_prompt += f"\n\n[DANGER] 已连续执行第 {turn+1} 轮。禁止无效重试。若无有效进展必须切换策略1. 探测物理边界 2. 请求用户协助。如有需要,可调用 update_working_checkpoint 保存关键上下文。"
if (turn+1) % 30 == 0:
next_prompt += f"\n\n### [DANGER] 已连续执行第 {turn+1} 轮。你必须总结情况进行ask_user不允许继续重试。"
next_prompt = handler.next_prompt_patcher(next_prompt, outcome, turn+1)
messages = [{"role": "user", "content": next_prompt}]
return {'result': 'MAX_TURNS_EXCEEDED'}

10
ga.py
View File

@@ -467,13 +467,21 @@ class GenericAgentHandler(BaseHandler):
except: pass
return prompt
def next_prompt_patcher(self, next_prompt, outcome, turn):
if turn % 30 == 0:
next_prompt += f"\n\n[DANGER] 已连续执行第 {turn} 轮。你必须总结情况进行ask_user不允许继续重试。"
elif turn % 7 == 0:
next_prompt += f"\n\n[DANGER] 已连续执行第 {turn} 轮。禁止无效重试。若无有效进展必须切换策略1. 探测物理边界 2. 请求用户协助。如有需要,可调用 update_working_checkpoint 保存关键上下文。"
elif turn % 10 == 0: next_prompt += get_global_memory()
return next_prompt
def get_global_memory():
prompt = "\n"
try:
with open('memory/global_mem_insight.txt', 'r', encoding='utf-8') as f: insight = f.read()
with open('assets/insight_fixed_structure.txt', 'r', encoding='utf-8') as f: structure = f.read()
prompt += f"\n[Memory]\n"
prompt += f'cwd = {os.path.abspath("./temp")} (用./引用)\n'
prompt += f'cwd = {os.path.abspath("./temp")} (用./引用)\n'
prompt += structure + '\n../memory/global_mem_insight.txt:\n'
prompt += insight + "\n"
except FileNotFoundError: pass

View File

@@ -1,9 +1,11 @@
# 自主行动 SOP (探测-报告-执行)
⚠️ **路径警告**autonomous_reports 在 temp/ 下,用`./autonomous_reports/`访问,**不是**`../memory/autonomous_reports/``../autonomous_reports/`
授权你进行自主行动,只要不对环境造成副作用都可进行。
请先选择核心目标,再选择一个小目标进行。最终探测结果形成报告(含操作申请),待用户回来确认后再进行可能的写入或修改操作。
> **must call update_working_memory**`自主探索≤15回合只有cwd内可写用户不在(问题存报告)|收尾:重读本SOP确认报告目录+更新报告目录内history产出=报告+记忆提案`
> **must call update_working_checkpoint**`自主探索≤15回合只有cwd内可写用户不在(问题存报告)报告目录:./autonomous_reports/收尾:重读本SOP确认报告目录+更新history产出=报告+记忆提案`
## 📋 大纲
- 报告目录与规则
@@ -12,7 +14,8 @@
## 报告目录与规则
⚠️ 历史记录唯一位置:`./autonomous_reports/history.txt` 在此目录内,禁止写到其他任何路径。
⚠️ 历史记录唯一位置:`./autonomous_reports/history.txt`
报告存于 `./autonomous_reports/`,文件名 `RXX_简短描述.md`XX从 history.txt 推断自增)。
完成后在 history.txt 首行 prepend 一条:`RXX | 日期 | 类型 | 主题 | 结论`(严格单行)。已处理报告归档至 `archived/`

View File

@@ -13,6 +13,21 @@
- 测SOP质量input指定SOP名如"用ezgmail_sop查看最近3封未读邮件"排除导航干扰失败即SOP问题
- 测导航能力input只写目标验证subagent能自主从insight找到正确SOP。禁止内联SOP内容
### 测试驱动SOP优化方法
**场景**发现SOP存在路径/参数/步骤问题,需验证修改效果
**方法**
1. 创建test_path测试环境`temp/test_path/`写入input.txt描述测试目标
2. 启动subagent`python agentmain.py --task test_path`(从代码根启动)
3. 观察output.txt和实际文件操作验证SOP修改是否生效
4. 清理test_path重复测试直到验证通过
**关键发现**(已验证):
- **Insight优先级>SOP**subagent优先读Insight索引Insight中的警告/约束会覆盖SOP中的说明
- **subagent的cwd**和主agent相同都在temp/subagent不知道自己是subagent
- **路径引用**从temp/访问memory/用`../memory/`访问autonomous_reports/用`./autonomous_reports/`在temp/下)
**标准流程map-reduce**
1. 主agent准备阶段爬取/dump数据存为多个独立输入文件
2. 分发对每个文件启动一个subagent处理主agent自己也可以处理其中一个