diff --git a/agent_loop.py b/agent_loop.py index b52423a..26cbebe 100644 --- a/agent_loop.py +++ b/agent_loop.py @@ -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"\n{datastr}\n\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'} \ No newline at end of file diff --git a/ga.py b/ga.py index e23a285..7e6b1e0 100644 --- a/ga.py +++ b/ga.py @@ -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 diff --git a/memory/autonomous_operation_sop.md b/memory/autonomous_operation_sop.md index d13d094..8367c74 100644 --- a/memory/autonomous_operation_sop.md +++ b/memory/autonomous_operation_sop.md @@ -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/`。 diff --git a/memory/subagent_sop.md b/memory/subagent_sop.md index dcddb94..38828c0 100644 --- a/memory/subagent_sop.md +++ b/memory/subagent_sop.md @@ -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自己也可以处理其中一个)