T02 · Agent Memory Poisoning
- Location
scripts/experience_logger.py:40- Finding
Persistent Prompt Injection Through Untrusted Experience Records
- Content
View full analysis
dict: """记录一条经验""" exp_file = get_experience_file(agent_id, base_path) exp_json = get_experience_json(agent_id, base_path) # 确保目录存在 exp_file.parent.mkdir(parents=True, exist_ok=True) # 当前时间 now = datetime.now() date_str = now.strftime("%Y-%m-%d") time_str = now.strftime("%H:%M") # 记录到 JSON(结构化) experiences = [] if exp_json.exists(): try: experiences = json.loads(exp_json.read_text()) except: experiences = [] new_exp = { "id": f"exp_{now.strftime('%Y%m%d%H%M%S')}", "content": experience, "task": task, "category": category, "created": now.isoformat(), "used_count": 0 } experiences.append(new_exp) # 保留最近 MAX_EXPERIENCES 条 if len(experiences) > MAX_EXPERIENCES: experiences = experiences[-MAX_EXPERIENCES:] exp_json.write_text(json.dumps(experiences, indent=2, ensure_ascii=False)) # 同时更新 Markdown 文件(人类可读) task_info = f" ({task})" if task else "" new_line = f"- [{date_str}] {experience}{task_info}\n" ``` ```python def inject_experiences(agent_id: str, limit: int = 5, base_path: str = DEFAULT_AGENTS_PATH) -> str: """ 输出可注入到 prompt 的经验片段 用于在 spawn 时注入相关经验 """ experiences = show_experiences(agent_id, limit=limit, base_path=base_path) if not experiences: return "" lines = ["## 历史经验(供参考)\n"] for exp in experiences: task_info = f" (来自: {exp['task']})" if exp.get('task') else "" lin ...[truncated 3119 chars]- Remediation
View remediation
