360 Secure SkillHub
360 安全技能中心

Memory Guardian

这是专为AI代理打造的内存管理工具,采用工作-永久-归档三层架构。核心功能包含自动健康检查(监控文件大小、过期、重复等异常)、会话内存到永久内存的智能提升、大内存量自动迁移至向量库,以及通过Git实现紧急恢复。适用于AI代理长期运行场景,可防止内存丢失、提前检测上下文溢出,通过脚本触发操作,输出预警并执行优化或恢复动作。

dodge1218
dodge1218
热度 47.31
最近更新 2026-05-18 12:39:42
分类:自动化依赖:代码执行 / 读写文件 / 第三方 API应用场景:自动化脚本 / 工作流 / 数据处理

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add memory-guardian
Metadata
name
memory-guardian
version
1.0.0
description
Memory health monitoring, integrity checks, and 3-layer memory architecture for AI agents. Use when agents need to prevent memory loss, detect context overflow before it happens, manage session-to-permanent memory promotion, and maintain a clean memory state across long-running workflows. Includes automated health checks (file size, staleness, duplicates, orphans), migration triggers for vector stores, and emergency recovery via git history.

Memory Guardian

Prevent memory loss and context overflow. 3-layer architecture with automated health checks.

Architecture

Layer 1: Working Memory (session files, 7-day retention)
    ↓ promote durable facts before deletion
Layer 2: Permanent Memory (never pruned, manual only)
    ↓ migrate to vector store at 5,000+ lines
Layer 3: Archive (batch docs, value stacks — disk forever)

Health Checks

Run python3 scripts/memory_check.py on heartbeat or manually. Catches:

  • Files over 300 lines (split needed)
  • Total memory over 3,000 lines (yellow alert → prune)
  • Stale session files (>7 days → promote + delete)
  • Duplicate content across files
  • MEMORY.md index inconsistency
  • Orphan files not referenced anywhere

Danger Zones

Total Lines Risk Action
< 2,000 🟢 Green Normal operations
2,000-3,000 🟡 Yellow Prune sessions, compress old entries
3,000-5,000 🟠 Orange Aggressive promotion to permanent, archive sessions
5,000+ 🔴 Red Semantic search returns noise → migrate to FAISS

Promotion Protocol

Before deleting ANY session file, extract:

  1. Credentials/keys → permanent (NEVER lose these)
  2. Architecture decisions → permanent/business-strategy.md
  3. Infrastructure changes → permanent/outreach-infrastructure.md
  4. New project summaries → permanent/projects/[name].md
  5. User preferences → permanent/user-system.md

Everything else (debug logs, intermediate results) → delete.

Migration Triggers

Trigger Action
permanent/ > 5,000 lines Migrate to FAISS vector store
Batch value-stack > 100 items Add embeddings for semantic retrieval
ChatGPT corpus loaded FAISS mandatory
Cross-batch connections > 50 Consider Neo4j knowledge graph

Emergency Recovery

Everything is git-tracked. If memory corrupts:

  1. git log memory/ → find last good state
  2. git checkout <hash> -- memory/ → restore
  3. Rebuild MEMORY.md index from ls memory/permanent/

Context Budget

Component Tokens Notes
System prompt ~2,000 Fixed
MEMORY.md ~1,500 Keep lean
Active session ~1,000 Today only
memory_search ~500 On-demand
Total overhead ~5,000 Of 200K+ available