memory

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Memory Management

内存管理

Efficient context and knowledge management.
高效的上下文与知识管理。

Instructions

操作指南

1. Working Memory Model

1. 工作内存模型

┌─────────────────────────────────────────┐
│           WORKING MEMORY                │
├─────────────────────────────────────────┤
│ • Current task goal                     │
│ • Relevant file contents                │
│ • Recent decisions                      │
│ • Active constraints                    │
└─────────────────────────────────────────┘
          ↑ Load        ↓ Store
┌─────────────────────────────────────────┐
│          LONG-TERM MEMORY               │
├─────────────────────────────────────────┤
│ • Project structure                     │
│ • User preferences                      │
│ • Past solutions                        │
│ • Domain knowledge                      │
└─────────────────────────────────────────┘
┌─────────────────────────────────────────┐
│           WORKING MEMORY                │
├─────────────────────────────────────────┤
│ • Current task goal                     │
│ • Relevant file contents                │
│ • Recent decisions                      │
│ • Active constraints                    │
└─────────────────────────────────────────┘
          ↑ Load        ↓ Store
┌─────────────────────────────────────────┐
│          LONG-TERM MEMORY               │
├─────────────────────────────────────────┤
│ • Project structure                     │
│ • User preferences                      │
│ • Past solutions                        │
│ • Domain knowledge                      │
└─────────────────────────────────────────┘

2. Context Prioritization

2. 上下文优先级排序

Order of importance for context:
PriorityContentAction
🔴 CriticalCurrent task, active fileAlways keep
🟠 HighRelated files, typesKeep if relevant
🟡 MediumProject structureSummarize
🟢 LowHistory, logsForget if needed
上下文重要性排序:
优先级内容操作
🔴 关键当前任务、活跃文件始终保留
🟠 高相关文件、类型相关时保留
🟡 中项目结构进行总结
🟢 低历史记录、日志必要时丢弃

3. Information Retention

3. 信息保留规则

markdown
undefined
markdown
undefined

What to Remember

需要保留的内容

✅ Keep in context:
  • Current task objective
  • File being modified
  • Type definitions in use
  • Recent error messages
  • User preferences
❌ Safe to forget:
  • Already processed files
  • Resolved errors
  • Intermediate calculations
  • Verbose logs
undefined
✅ 保留在上下文中:
  • 当前任务目标
  • 正在修改的文件
  • 正在使用的类型定义
  • 近期错误信息
  • 用户偏好
❌ 可安全丢弃的内容:
  • 已处理完成的文件
  • 已解决的错误
  • 中间计算过程
  • 冗长的日志
undefined

4. Context Summarization

4. 上下文总结

When context grows too large:
markdown
undefined
当上下文规模过大时:
markdown
undefined

Summarization Rules

总结规则

  1. Files: Keep imports, types, key functions
  2. Errors: Keep message, remove stack trace
  3. Logs: Keep last 10 lines
  4. History: Keep decisions, remove process
  1. 文件:保留导入语句、类型、核心函数
  2. 错误:保留错误信息,移除堆栈跟踪
  3. 日志:保留最后10行
  4. 历史记录:保留决策结果,移除过程细节

Example

示例

Before (verbose): "I looked at file A, then file B, noticed pattern X, then explored file C, found issue Y, traced it to..."
After (summarized): "Analyzed A, B, C. Found: pattern X, issue Y in C."
undefined
总结前(冗长): "我查看了文件A,然后是文件B,发现了模式X, then explored file C,found issue Y,traced it to..."
总结后(精简): "分析了A、B、C文件。发现:模式X,文件C中的问题Y。"
undefined

5. Session State Pattern

5. 会话状态模式

typescript
// Conceptual session state
interface SessionMemory {
  // Always retain
  task: {
    goal: string;
    status: 'planning' | 'executing' | 'verifying';
    progress: number;
  };
  
  // Retain while relevant
  context: {
    activeFiles: string[];
    recentDecisions: string[];
    constraints: string[];
  };
  
  // Summarize or forget
  history: {
    summary: string;
    keyInsights: string[];
  };
}
typescript
// Conceptual session state
interface SessionMemory {
  // Always retain
  task: {
    goal: string;
    status: 'planning' | 'executing' | 'verifying';
    progress: number;
  };
  
  // Retain while relevant
  context: {
    activeFiles: string[];
    recentDecisions: string[];
    constraints: string[];
  };
  
  // Summarize or forget
  history: {
    summary: string;
    keyInsights: string[];
  };
}

6. Knowledge Retrieval

6. 知识检索

markdown
undefined
markdown
undefined

Before Starting New Task

开始新任务前

  1. Check: Have I seen this before?
  2. Recall: What approach worked?
  3. Apply: Use proven patterns
  4. Adapt: Modify for current context
undefined
  1. 检查:我之前处理过类似任务吗?
  2. 回忆:之前的解决方法是什么?
  3. 应用:使用已验证的模式
  4. 调整:根据当前上下文修改方案
undefined

7. Memory Hygiene

7. 内存维护

markdown
undefined
markdown
undefined

Per-Turn Cleanup

每轮步骤完成后清理

After completing a step:
  1. ✅ Task still relevant? Keep
  2. ❓ Might need later? Summarize
  3. ❌ No longer needed? Forget
完成一个步骤后:
  1. ✅ 任务仍相关?保留
  2. ❓ 后续可能需要?进行总结
  3. ❌ 不再需要?丢弃

End of Task

任务结束后

  1. Extract learnings
  2. Update knowledge base
  3. Clear working memory
undefined
  1. 提取经验教训
  2. 更新知识库
  3. 清空工作内存
undefined

8. Context Window Management

8. 上下文窗口管理

markdown
undefined
markdown
undefined

Token Budget Allocation

Token 预算分配

CategoryBudget
System prompt10%
Task context30%
Active code40%
Conversation20%
类别预算占比
系统提示词10%
任务上下文30%
活跃代码40%
对话内容20%

When Near Limit

接近预算上限时

  1. Summarize conversation history
  2. Remove resolved issues
  3. Keep only relevant code sections
  4. Preserve critical context
undefined
  1. 总结对话历史
  2. 移除已解决的问题
  3. 仅保留相关代码片段
  4. 保留关键上下文
undefined

References

参考资料