memoryrules_improved

A memory-management rule written in Chinese that requires each interaction to follow a fixed order: obtain a session, retrieve relevant memory, answer, and store memory.

In plain words
What is it for?
Use it to enforce session creation, context retrieval, project analysis when requested, and memory storage for each user interaction.
Why use it?
It provides a prescribed process for carrying information between interactions, though the input does not describe the underlying memory system in more detail.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/redleaves/context-keeper/memoryrules_improved
Clone the repo
git clone --depth 1 https://github.com/redleaves/context-keeper

Made for: Cursor.

Per session 14,460 This file is loaded in full into every session.
When invoked 14,460 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.14460 $0.14460
Opus 5 $0.07230 $0.07230
Sonnet 5 $0.02892 $0.02892
Haiku 4.5 $0.01446 $0.01446

Measured yesterday against content hash 7cbb5d36439d, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

memoryrules_improved scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/memoryrules_improved.mdc · 1,547 lines

How it starts

The opening of the file, as written. The whole thing — 1,547 lines — stays where its author put it; the contents beside it link to each section on GitHub.

🧠 Context-Keeper 智能记忆管理规则

⚡ 三步执行流程(MANDATORY)

每次用户交互必须严格按顺序执行:会话获取 → 记忆检索 → 智能回答 → 记忆存储

🔥 Step 1: 会话获取(第一优先级)

何时执行:用户发送任何消息时立即执行 必须完成:获取有效sessionId才能继续

核心工具调用

mcp_context_keeper_session_management({
  action: "get_or_create",
  userId: "从配置文件获取的用户ID",
  workspaceRoot: "当前工作空间路径"
})

⚠️ 关键点

  • 必须传入userId和workspaceRoot
  • 获取成功后提取sessionId和analysisPrompt
  • 一定要从 ~/Library/Application Support/context-keeper/user-config.json中读取userId信息

🧠 Step 2: 智能记忆检索(第二优先级)

何时执行:用户提问题、执行命令、讨论技术、引用历史时 检索增强:结合工程感知分析

标准检索流程

// 1. 基础记忆检索
mcp_context_keeper_retrieve_context({
  sessionId: sessionId,
  query: userMessage,
  // 🆕 工程感知集成
  projectAnalysis: analysisResult // 来自Step1的analysisPrompt要求分析结果
})

⚡ 工程感知触发(关键流程)

// Step 1: 会话管理后检查
if (sessionResult.analysisPrompt) {
  // Step 2: 按analysisPrompt要求执行工程分析
  const projectAnalysis = performProjectAnalysis(sessionResult.analysisPrompt);
  
  // Step 3: 传入检索工具
  mcp_context_keeper_retrieve_context({
    sessionId: sessionId,
    query: userMessage,
    projectAnalysis: projectAnalysis  // 🆕 关键:工程分析结果
  });
}

工程感知执行要求

  • 必须条件:会话管理返回analysisPrompt ≠ 空
  • 执行步骤:严格按analysisPrompt的指引分析当前工作空间项目特征
  • 输出要求:结构化的项目分析结果(技术栈、架构、组件等)
  • 传递方式:projectAnalysis字段传入retrieve_context
  • ⚠️ 重要原则:以下代码是通用模板,不是具体实现。必须根据当前实际工作空间动态分析和填充,绝不能硬编码特定项目信息!

🔥 具体实现指导

  1. 工程分析函数实现模板
function performProjectAnalysis(analysisPrompt) {
  // 🎯 根据analysisPrompt的要求分析当前工作空间的项目特征
  // 这是一个通用模板,需要根据实际项目动态填充
  
  // Step 1: 分析项目基础信息
  const projectBasics = analyzeProjectBasics(); // 分析go.mod、package.json、pom.xml等
  const directoryStructure = analyzeDirectoryStructure(); // 分析目录结构
  const recentChanges = analyzeRecentChanges(); // 分析Git历史、最近提交
  
  // Step 2: 构建分析结果对象(根据实际项目动态生成)
  const analysis = {
    project_name: projectBasics.name,              // 从实际项目获取
    description: projectBasics.description,        // 从README、配置文件获取
    project_type: projectBasics.type,              // "go", "nodejs", "python", "java", etc.
    primary_language: projectBasics.primaryLang,   // 主要编程语言
    tech_stack: projectBasics.techStack,           // 技术栈组合
    architecture: projectBasics.architecture,      // 架构模式
    main_framework: projectBasics.framework,       // 主要框架
    database: projectBasics.database,              // 数据库方案
    key_dependencies: projectBasics.dependencies,  // 核心依赖
    recent_focus: recentChanges.focus,             // 近期开发重点
    current_pain_points: recentChanges.issues,    // 当前痛点
    active_requirements: recentChanges.requirements, // 活跃需求
    main_components: directoryStructure.components, // 主要组件
    important_files: directoryStructure.keyFiles,   // 重要文件
    current_phase: projectBasics.phase,            // 开发阶段
    confidence_level: calculateConfidence()        // 分析置信度
  };
  
  // Step 3: 返回JSON字符串格式
  return JSON.stringify(analysis);
}

// 🔧 辅助分析函数示例(需要根据实际工作空间实现)
function analyzeProjectBasics() {
  // 从go.mod、package.json、README等文件提取基础信息
  // 这里只是示例,实际需要读取当前工作空间的文件
  return {
    name: "从实际项目配置文件获取",
    description: "从README或项目描述获取",
    type: "从配置文件判断:go.mod->go, package.json->nodejs",
    // ... 其他字段
  };
}

Read the full file on GitHub · 1,547 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 1,547 lines · 14,460 tokens per session scan A 7cbb5d36439d

Subscribe to this mod's changes

memoryrules_improved is a cursor rule published in the GitHub repository redleaves/context-keeper (153 stars, last pushed 7mo ago), licensed MIT. It adds 14,460 tokens to every session, about $0.0723 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.