From 946c239c035adf0d75bc28532b0dfa5ea0a1340d Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Sat, 11 Apr 2026 18:30:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20rename=20mem=5Fscanner=E2=86=92proc?= =?UTF-8?q?mem=5Fscanner,=20add=20L4=20layer,=20remove=20make=5Fprompts.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 +- assets/global_mem_insight_template.txt | 3 +- assets/make_prompts.py | 142 ------------------ memory/memory_management_sop.md | 1 + memory/{mem_scanner.py => procmem_scanner.py} | 0 ..._scanner_sop.md => procmem_scanner_sop.md} | 6 +- memory/subagent.md | 2 +- 7 files changed, 10 insertions(+), 150 deletions(-) delete mode 100644 assets/make_prompts.py rename memory/{mem_scanner.py => procmem_scanner.py} (100%) rename memory/{mem_scanner_sop.md => procmem_scanner_sop.md} (96%) diff --git a/.gitignore b/.gitignore index 8d8c455..f40285f 100644 --- a/.gitignore +++ b/.gitignore @@ -52,9 +52,9 @@ memory/L4_raw_sessions/* !memory/ljqCtrl.py !memory/ljqCtrl_sop.md -# mem_scanner related tools -!memory/mem_scanner.py -!memory/mem_scanner_sop.md +# procmem_scanner related tools +!memory/procmem_scanner.py +!memory/procmem_scanner_sop.md # TMWebDriver SOP !memory/tmwebdriver_sop.md diff --git a/assets/global_mem_insight_template.txt b/assets/global_mem_insight_template.txt index dc16393..3e956d0 100644 --- a/assets/global_mem_insight_template.txt +++ b/assets/global_mem_insight_template.txt @@ -8,7 +8,8 @@ 需要时read L2 或 ls ../memory/ 查L3 L0(META-SOP): memory_management_sop L2: 现空 -L3: web_setup_sop | autonomous_operation_sop | scheduled_task_sop | ljqCtrl_sop+.py | tmwebdriver_sop | subagent_sop | plan_sop | mem_scanner.py | adb_ui.py +L3: web_setup_sop | autonomous_operation_sop | scheduled_task_sop | ljqCtrl_sop+.py | tmwebdriver_sop | subagent_sop | plan_sop | procmem_scanner.py | adb_ui.py +L4: ../memory/L4_raw_sessions/ [RULES] 1. 搜索先行: 信息尽量用google(必须web), 项目内os.listdir, 禁猜路径 diff --git a/assets/make_prompts.py b/assets/make_prompts.py deleted file mode 100644 index 1a46172..0000000 --- a/assets/make_prompts.py +++ /dev/null @@ -1,142 +0,0 @@ -import sys, os, re -import pyperclip -import json, time -from pathlib import Path -import subprocess -import tempfile -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) -from llmcore import SiderLLMSession, LLMSession, ToolClient - - -ask = SiderLLMSession().ask - - -def generate_tool_schema(): - """ - 通过代码内省,将 Handler 的逻辑映射为高语义的工具描述。 - """ - script_dir = os.path.dirname(os.path.abspath(__file__)) - with open(os.path.join(script_dir, '../ga.py'), 'r', encoding='utf-8') as f: - ga_code = f.read() - # 极简且具备高度概括能力的元 Prompt - meta_prompt = f""" -# Role -你是一个具备深度推理能力的 AI 系统架构师。你将通过阅读 `GenericAgentHandler` 源码,构建其对应的工具能力矩阵。 - -# Task -分析下方的源码,并输出 OpenAI Tool Schema。在输出 JSON 之前,你必须进行内部思考(Thinking Process)。 - -# Thinking Process Requirements -在 `` 标签中,请按顺序分析: -1. **核心工具链识别**:识别所有 `do_xxx` 方法,并分析它们依赖的底层 Utility 函数。 -2. **内容溯源审计**:重点分析哪些工具是从 `response.content` 提取核心逻辑(如代码块)的。对于这些工具,确认在 Schema 参数中排除掉对应的字段。 -3. **调用策略推导**:分析工具间的协作关系(例如 `file_read` 如何为 `file_patch` 提供定位)。 -4. **兜底逻辑确认**:明确某些特殊万能工具在系统中的保底角色,快速工具无法执行的操作由保底工具执行,但正常应优先使用方便的工具。 -5. **注释审阅**:结合函数注释,理解每个工具的使用限制,其中的重要信息务必反映在工具描述中(如长度限制等)。 -注释中的重要信息务必反映在工具描述中。 -注释中的重要信息务必反映在工具描述中。 - -# Tool Schema Formatting Rules -- **参数对齐**:仅包含 `do_xxx` 方法中通过 `args.get()` 显式获取的参数。 -- **高引导性描述**:描述应包含“何时调用”以及“如何根据反馈修正”,需要注意函数的注释事项。 -- **输出格式**:先输出 `` 块,然后输出 ```json 块。 - -# Source Code -{ga_code} - -# Output -请开始思考并生成: -""" - - # 假设 ask 是你已经封装好的 LLM 调用接口 - raw_response = ask(meta_prompt, model="gemini-3.0-flash") - print(raw_response) - - # --- 健壮的 JSON 解析逻辑 --- - try: - # 1. 清除 Markdown 围栏 - clean_json = raw_response.strip() - if clean_json.startswith("```"): - # 兼容 ```json 和 ``` - clean_json = re.sub(r'^```(?:json)?\s*', '', clean_json) - clean_json = re.sub(r'\s*```$', '', clean_json) - - # 2. 移除可能的非 JSON 前导/后缀文字(如果有的话) - start_idx = clean_json.find('[') - end_idx = clean_json.rfind(']') + 1 - if start_idx != -1 and end_idx != -1: - clean_json = clean_json[start_idx:end_idx] - - final_schema = json.loads(clean_json) - - if final_schema: - script_dir = os.path.dirname(os.path.abspath(__file__)) - with open(os.path.join(script_dir, 'tools_schema.json'), 'w', encoding='utf-8') as f: - json.dump(final_schema, f, indent=2, ensure_ascii=False) - print("✅ 成功从代码内省生成 Schema 并持久化。") - return final_schema - - except Exception as e: - print(f"❌ 解析 Schema 失败: {e}\n原始响应: {raw_response}") - return None - - -def make_system_prompt(ga_code_path=None): - script_dir = os.path.dirname(os.path.abspath(__file__)) - if ga_code_path is None: - ga_code_path = os.path.join(script_dir, '../ga.py') - with open(ga_code_path, 'r', encoding='utf-8') as f: - ga_code = f.read() - - # 这个元 Prompt 的目标是生成“世界观”而非“说明书” - meta_prompt = f""" -# Role -你是一个 AI 架构师。请阅读下方的工具库源码,为 Agent 生成一份【系统级认知指令 (System Prompt)】。 - -# Task -基于代码逻辑,定义 Agent 的“能力边界”和“行动协议”。 - -# Requirements (Crucial) -1. **打破预训练偏见**:针对模型常说的“我只是 AI,不能操作网页/文件”进行修正。明确告诉它:你现在拥有物理操作权限,工具设计保证了所有权限做所有事情。 -2. **避开冗余**:不要重复 Tool Schema 里的参数细节。 -3. **能力边界定义**: - - 网页操作:它不是通过“想象”上网,而是通过实时的浏览器读写。 - - 文件操作:它拥有物理文件读写权限,且遵循“先读后写”的稳健性原则。 - - 保底逻辑:当专用工具失效时,使用 `code_run` 编写脚本解决一切。 - - 特殊的update_plan(仅在复杂任务时使用)和ask_user(用户也是有效资源)工具。 -4. **行动协议**: - - 必须在行动前进行 - -我后面还会附上具体的工具描述和Schema,所以不要重复。 -主要以世界观为主,不要纠结于具体工具。 - -# Input Source Code -{ga_code} - -# Output -仅输出 System Prompt 的正文,语气要果断、指令化。 -""" - print("🧠 正在重塑 Agent 世界观 (Generating System Prompt)...") - # 调用你的 llmclient.ask - system_prompt_content = ask(meta_prompt) - print("📝 生成的 System Prompt 内容如下:\n") - print(system_prompt_content) - clean_content = re.sub(r'<[^>]+>', '', system_prompt_content) - with open(os.path.join(script_dir, 'sys_prompt.txt'), 'w', encoding='utf-8') as f: - f.write(clean_content) - return clean_content - -# --- 主逻辑 --- -if __name__ == "__main__": - if len(sys.argv) < 2: - print("Usage: python make_prompts.py [schema|prompt]") - sys.exit(1) - - cmd = sys.argv[1].lower() - if cmd == "schema": - generate_tool_schema() - elif cmd == "prompt": - make_system_prompt() - else: - print(f"Unknown command: {cmd}") - print("Available commands: schema, prompt") \ No newline at end of file diff --git a/memory/memory_management_sop.md b/memory/memory_management_sop.md index 1fcbd49..88999a2 100644 --- a/memory/memory_management_sop.md +++ b/memory/memory_management_sop.md @@ -20,6 +20,7 @@ L1: global_mem_insight.txt (极简索引层 - 严格控制 ≤30 行) L2: global_mem.txt (事实库层 - 现短但会膨胀) ↓ 详细引用 (Reference) L3: ../memory/ (记录库层 - 包含 .md/.py 等各类文件) +L4: ../memory/L4_raw_sessions/ (历史会话层 - scheduler反射自动收集,可定位过往上下文) ``` --- ## 各层职责与原则 diff --git a/memory/mem_scanner.py b/memory/procmem_scanner.py similarity index 100% rename from memory/mem_scanner.py rename to memory/procmem_scanner.py diff --git a/memory/mem_scanner_sop.md b/memory/procmem_scanner_sop.md similarity index 96% rename from memory/mem_scanner_sop.md rename to memory/procmem_scanner_sop.md index ac88231..39393fc 100644 --- a/memory/mem_scanner_sop.md +++ b/memory/procmem_scanner_sop.md @@ -7,7 +7,7 @@ ```python import sys sys.path.append('../memory') # 直接挂载工具目录 -from mem_scanner import scan_memory +from procmem_scanner import scan_memory # 示例:搜索特定 Hex 特征码,开启 llm_mode 以获取上下文 results = scan_memory(pid, "48 8b ?? ?? 00", mode="hex", llm_mode=True) @@ -16,10 +16,10 @@ results = scan_memory(pid, "48 8b ?? ?? 00", mode="hex", llm_mode=True) **CLI:** ```powershell # 基础搜索 -python ../memory/mem_scanner.py "pattern" --mode string +python ../memory/procmem_scanner.py "pattern" --mode string # LLM 增强模式(输出包含上下文的 JSON,推荐) -python ../memory/mem_scanner.py "pattern" --llm +python ../memory/procmem_scanner.py "pattern" --llm ``` ## 2. 典型场景:结构体或关键数据定位 diff --git a/memory/subagent.md b/memory/subagent.md index 2005dd0..36bf067 100644 --- a/memory/subagent.md +++ b/memory/subagent.md @@ -9,7 +9,7 @@ - 流程:启动 → 轮询 output.txt(`[ROUND END]`=该轮完成)→ 写 reply.txt 继续 → 不写则10min退出 - 干预文件:`_stop`(当轮结束退出) | `_keyinfo`(写入即注入下轮prompt的key_info) | `_intervene`(写入内容作为下轮追加指令) - output1/2/3.txt:reply后各轮输出(递增编号),同样append+`[ROUND END]` -- input.txt:目标+约束,可指定SOP名。禁写具体步骤(除非已读SOP确认)。大量数据给路径禁塞入 +- input.txt:目标+约束,可指定SOP名。**禁写具体步骤**。大量数据给路径禁塞入 - ⚠ `--input`走命令行,长文本/含引号会超时。超过一句话时:先写input.txt,启动时不带`--input` - --bg启动瞬间返回,可同一code_run内sleep后poll;非--bg方式禁止合并启动+轮询