Claude Code 源码分析Claude Code 源码分析
首页
源码统计
系统架构
UML 图表
工具系统
CodeGraph
首页
源码统计
系统架构
UML 图表
工具系统
CodeGraph
  • 概览

    • Claude Code 源码分析
    • 源码统计
    • CodeGraph 图谱
  • 架构

    • 系统架构
    • UML 图表索引
    • 查询引擎
    • 核心流程
    • 消息系统
    • 状态管理
  • 功能模块

    • 工具系统
    • 斜杠命令
    • 服务层
    • MCP 协议
    • Skills 技能
    • 子代理系统
  • 分层深度

    • 入口层
    • UI / Ink 层
    • utils 基础设施
    • 桥接 / 远程
    • 上下文压缩
  • 原理与安全

    • 底层原理
    • 技术难点
    • 权限与安全
    • 内部机制
    • 遥测与分析
  • 深度专题

    • Hooks 系统
    • 插件系统
    • 记忆系统
    • API 通信层
    • Ink 终端 UI
    • 认证系统
    • 构建与发布
    • 术语表
  • 调用分析

    • 调用链分析
    • 核心文件索引
  • 模块详解

    • utils

      • 模块: utils
      • messages · 消息工厂与规范化
      • session-storage · JSONL 会话持久化
      • permissions · 工具权限决策
      • shell-hooks · 用户 Shell Hook 系统
    • components

      • 模块: components
      • REPL · 主屏编排
      • messages · 消息行渲染
      • PermissionRequest · 权限弹窗
      • PromptInput · 底部输入
    • services

      • 模块: services
      • api-claude · Anthropic API 流式与重试
      • mcp-client · MCP 连接与工具调用
      • compact · 上下文压缩与自动触发
      • analytics · GrowthBook、Datadog 与 1P 事件
    • tools

      • 模块: tools
      • tool-interface · Tool 契约与注册表
      • bash-tool · Shell 执行与权限
      • streaming-executor · 流式工具并发调度
      • agent-tool · 子 Agent 委派
    • commands

      • 模块: commands
      • command-registry · commands.ts 注册与分派
      • model-command · /model 模型选择
      • mcp-commands · /mcp 服务器管理
      • compact-memory-commands · /compact 与 /memory
    • ink

      • 模块: ink
      • Ink 渲染管线 · Screen 与终端输出
      • 终端事件 · resize、paste、stdin
      • Ink Hooks · 输入、搜索、终端状态
      • Ink 组件 · Box、Text、ScrollBox 原语
    • hooks

      • 模块: hooks
      • useCanUseTool · 权限 UI 接缝
      • 输入与快捷键 Hook
      • 合并态 Hook(MCP + 本地)
      • notifs 通知 Hook
    • bridge

      • 模块: bridge
      • repl-bridge · REPL 桥初始化与传输
      • bridge-messaging · 桥消息路由与入站处理
      • remote-bridge-core · env-less 核心与守护主循环
      • bridge-permissions-ui · 权限、API 与 TUI
    • cli

      • 模块: cli
      • Structured IO · NDJSON SDK 协议
      • CLI Transports · Session Ingress 传输层
      • CLI Handlers · 子命令懒加载实现
      • Update & Upload · 自更新与串行上传原语
    • screens

      • 模块: screens
      • REPL 屏 · Screen 类型与顶层路由
      • ResumeConversation · 会话恢复选择器
      • Doctor · 安装诊断全屏
    • entrypoints

      • 模块: entrypoints
      • cli-entrypoint · Bootstrap 与快路径
      • sdk-types · core / control / runtime 类型体系
      • mcp-entrypoint · MCP stdio 服务器
      • sandbox-types · 沙箱配置单一真相源
    • skills

      • 模块: skills
      • skills-loading · 磁盘加载与 bundled 注册表
      • bundled-skills · 内置 skill 与 initBundledSkills
      • mcp-skills · MCP prompt 转 skill
      • skill-tool-integration · SkillTool 与命令注册
    • types

      • 模块: types
      • message-types · Message 联合与 content blocks
      • tool-permission-types · Tool、Permission、Command 类型
      • api-sdk-types · API 与 Hooks 协议类型
      • misc-types · ids、plugin、generated 与其余类型
    • tasks

      • 模块: tasks
      • local-agent-task · 本地 Agent 与主会话后台化
      • remote-agent-task · 远程 CCR 与 In-Process Teammate
      • shell-workflow-tasks · Bash 后台、Workflow 与 stopTask
      • dream-monitor-tasks · Dream、Monitor MCP 与 pill 文案
    • keybindings

      • 模块: keybindings
      • keybinding-registry · 注册、Provider 与 useKeybinding
      • default-bindings · 默认键位表与平台差异
      • command-bindings · command:* 动态斜杠命令绑定
      • vim-bindings · Vim 模式与 keybindings 边界
    • memdir

      • 模块: memdir
      • memdir-core · 路径、加载与 MEMORY.md
      • memory-extraction · extractMemories 与 SessionMemory
      • memdir-commands · /memory、/remember 与命令集成
    • state

      • 模块: state
      • app-state-core · store、AppState 类型与 Provider
      • app-state-selectors · selectors 与 onChangeAppState
      • teammate-state · 队友视图与 swarm 状态
      • state-boundaries · bootstrap、sessionStorage、FileStateCache
    • query

      • 模块: query
      • query config 与 deps · 配置快照与依赖注入
      • query tokenBudget · +500k 自动续跑
      • query transitions · Continue / Terminal 状态机
      • query stopHooks · Stop 事件与 turn 结束编排
  • 模块详解(扩展)

    • messages · 消息工厂与规范化
    • session-storage · JSONL 会话持久化
    • permissions · 工具权限决策
    • shell-hooks · 用户 Shell Hook 系统
    • REPL · 主屏编排
    • messages · 消息行渲染
    • PermissionRequest · 权限弹窗
    • PromptInput · 底部输入
    • api-claude · Anthropic API 流式与重试
    • mcp-client · MCP 连接与工具调用
    • compact · 上下文压缩与自动触发
    • analytics · GrowthBook、Datadog 与 1P 事件
    • tool-interface · Tool 契约与注册表
    • bash-tool · Shell 执行与权限
    • streaming-executor · 流式工具并发调度
    • agent-tool · 子 Agent 委派
    • command-registry · commands.ts 注册与分派
    • model-command · /model 模型选择
    • mcp-commands · /mcp 服务器管理
    • compact-memory-commands · /compact 与 /memory
    • Ink 渲染管线 · Screen 与终端输出
    • 终端事件 · resize、paste、stdin
    • Ink Hooks · 输入、搜索、终端状态
    • Ink 组件 · Box、Text、ScrollBox 原语
    • useCanUseTool · 权限 UI 接缝
    • 输入与快捷键 Hook
    • 合并态 Hook(MCP + 本地)
    • notifs 通知 Hook
    • repl-bridge · REPL 桥初始化与传输
    • bridge-messaging · 桥消息路由与入站处理
    • remote-bridge-core · env-less 核心与守护主循环
    • bridge-permissions-ui · 权限、API 与 TUI
    • Structured IO · NDJSON SDK 协议
    • CLI Transports · Session Ingress 传输层
    • CLI Handlers · 子命令懒加载实现
    • Update & Upload · 自更新与串行上传原语
    • REPL 屏 · Screen 类型与顶层路由
    • ResumeConversation · 会话恢复选择器
    • Doctor · 安装诊断全屏
    • cli-entrypoint · Bootstrap 与快路径
    • sdk-types · core / control / runtime 类型体系
    • mcp-entrypoint · MCP stdio 服务器
    • sandbox-types · 沙箱配置单一真相源
    • skills-loading · 磁盘加载与 bundled 注册表
    • bundled-skills · 内置 skill 与 initBundledSkills
    • mcp-skills · MCP prompt 转 skill
    • skill-tool-integration · SkillTool 与命令注册
    • message-types · Message 联合与 content blocks
    • tool-permission-types · Tool、Permission、Command 类型
    • api-sdk-types · API 与 Hooks 协议类型
    • misc-types · ids、plugin、generated 与其余类型
    • local-agent-task · 本地 Agent 与主会话后台化
    • remote-agent-task · 远程 CCR 与 In-Process Teammate
    • shell-workflow-tasks · Bash 后台、Workflow 与 stopTask
    • dream-monitor-tasks · Dream、Monitor MCP 与 pill 文案
    • keybinding-registry · 注册、Provider 与 useKeybinding
    • default-bindings · 默认键位表与平台差异
    • command-bindings · command:* 动态斜杠命令绑定
    • vim-bindings · Vim 模式与 keybindings 边界
    • memdir-core · 路径、加载与 MEMORY.md
    • memory-extraction · extractMemories 与 SessionMemory
    • memdir-commands · /memory、/remember 与命令集成
    • app-state-core · store、AppState 类型与 Provider
    • app-state-selectors · selectors 与 onChangeAppState
    • teammate-state · 队友视图与 swarm 状态
    • state-boundaries · bootstrap、sessionStorage、FileStateCache
    • query config 与 deps · 配置快照与依赖注入
    • query tokenBudget · +500k 自动续跑
    • query transitions · Continue / Terminal 状态机
    • query stopHooks · Stop 事件与 turn 结束编排
  • 工具详解

    • tool-interface · Tool 契约与注册表
    • tool-permission-types · Tool、Permission、Command 类型
    • 工具: Bash
    • 工具: PowerShell
    • 工具: Agent
    • 工具: LSP
    • 工具: FileEdit
    • 工具: FileRead
    • 工具: Skill
    • 工具: WebFetch
    • 工具: MCP
    • 工具: SendMessage
    • 工具: FileWrite
    • 工具: Config
    • 工具: Grep
    • 工具: Brief
    • 工具: ExitPlanMode
    • 工具: ToolSearch
    • 工具: NotebookEdit
    • 工具: TaskOutput
    • 工具: WebSearch
    • 工具: ScheduleCron

本章总览

先抓核心:memdir-core 决定了 Claude Code 记忆目录“放在哪里、以什么格式加载到系统提示、如何避免提示膨胀与路径风险”。围绕这条主线,paths.ts 负责路径与启用门控,memdir.ts 负责提示构建与索引截断,memoryTypes.ts 负责类型语言与写入规范。

学完本章你应该能

  • 复述 getAutoMemPath 与 getMemoryBaseDir 解析顺序
  • 说明 truncateEntrypointContent 行截断再字节截断算法
  • 列举 MEMORY_TYPES 四类型及 parseMemoryType
  • 理解 buildMemoryLines 两步保存(文件 + MEMORY.md 索引)
  • 知道 ensureMemoryDirExists 与 Write tool 的分工
  • 能在 QueryEngine 找到 loadMemoryPrompt 调用

核心概念(先读懂这些)

MEMORY.md 是索引不是记忆正文

buildMemoryLines 明确:topic .md 存 frontmatter+正文;MEMORY.md 仅 one-line 指针列表,无 frontmatter。超长索引先 line cap 再 byte cap,警告信息区分「行数过多」与「单行过长」。

paths 安全校验

validateMemoryPath 拒绝相对路径、根目录、UNC、null byte。settings 可 ~/ 展开;env CLAUDE_CODE_AUTO_MEM_PATH 不展开 ~。防止 allowlist 根目录落到 / 或 $HOME 全域。

TEAMMEM 懒加载

memdir.ts 用 feature(TEAMMEM) require teamMemPaths/teamMemPrompts,避免非 team 构建拉入团队同步逻辑。COMBINED mode 注入 TYPES_SECTION_COMBINED 与双目录 DIRS_EXIST_GUIDANCE。

建议学习步骤

  1. 阅读 paths.ts isAutoMemoryEnabled 全链
  2. 阅读 truncateEntrypointContent 与 MAX_* 常量
  3. 浏览 memoryTypes MEMORY_TYPES 与 TYPES_SECTION
  4. 阅读 buildMemoryLines howToSave 两步
  5. 阅读 loadMemoryPrompt 与 systemPromptSection cache
  6. 查看 memoryAge.ts 消费点

常见误区

注意

hasAutoMemPathOverride 影响路径显示名

注意

buildMemoryPrompt 含 MEMORY 内容;loadMemoryPrompt 可能仅 lines

注意

KAIROS daily log 在 memdir.ts 后部 feature gate

注意

scan 在 memoryScan.ts 不在 memdir.ts

paths.ts 路径与门控

getMemoryBaseDir:CLAUDE_CODE_REMOTE_MEMORY_DIR 或 getClaudeConfigHomeDir()。

getAutoMemPath:join memoryBase, projects, sanitizePath(projectRoot), memory/。

isAutoMemoryEnabled 注释列出与 --bare、CCR、settings 关系——关闭时 prompts.ts 也 early-return 记忆 section,extractMemories、/remember、/dream 一并停止。

isExtractModeActive:GrowthBook + interactive session 判定,供 stopHooks 与 print.ts drain 使用。

源码引用: src/memdir/paths.ts · 第 21–55 行(共 279 行)

  21| /**
  22|  * Whether auto-memory features are enabled (memdir, agent memory, past session search).
  23|  * Enabled by default. Priority chain (first defined wins):
  24|  *   1. CLAUDE_CODE_DISABLE_AUTO_MEMORY env var (1/true → OFF, 0/false → ON)
  25|  *   2. CLAUDE_CODE_SIMPLE (--bare) → OFF
  26|  *   3. CCR without persistent storage → OFF (no CLAUDE_CODE_REMOTE_MEMORY_DIR)
  27|  *   4. autoMemoryEnabled in settings.json (supports project-level opt-out)
  28|  *   5. Default: enabled
  29|  */
  30| export function isAutoMemoryEnabled(): boolean {
  31|   const envVal = process.env.CLAUDE_CODE_DISABLE_AUTO_MEMORY
  32|   if (isEnvTruthy(envVal)) {
  33|     return false
  34|   }
  35|   if (isEnvDefinedFalsy(envVal)) {
  36|     return true
  37|   }
  38|   // --bare / SIMPLE: prompts.ts already drops the memory section from the
  39|   // system prompt via its SIMPLE early-return; this gate stops the other half
  40|   // (extractMemories turn-end fork, autoDream, /remember, /dream, team sync).
  41|   if (isEnvTruthy(process.env.CLAUDE_CODE_SIMPLE)) {
  42|     return false
  43|   }
  44|   if (
  45|     isEnvTruthy(process.env.CLAUDE_CODE_REMOTE) &&
  46|     !process.env.CLAUDE_CODE_REMOTE_MEMORY_DIR
  47|   ) {
  48|     return false
  49|   }
  50|   const settings = getInitialSettings()
  51|   if (settings.autoMemoryEnabled !== undefined) {
  52|     return settings.autoMemoryEnabled
  53|   }
  54|   return true
  55| }

源码引用: src/memdir/paths.ts · 第 57–90 行(共 279 行)

  57| /**
  58|  * Whether the extract-memories background agent will run this session.
  59|  *
  60|  * The main agent's prompt always has full save instructions regardless of
  61|  * this gate — when the main agent writes memories, the background agent
  62|  * skips that range (hasMemoryWritesSince in extractMemories.ts); when it
  63|  * doesn't, the background agent catches anything missed.
  64|  *
  65|  * Callers must also gate on feature('EXTRACT_MEMORIES') — that check cannot
  66|  * live inside this helper because feature() only tree-shakes when used
  67|  * directly in an `if` condition.
  68|  */
  69| export function isExtractModeActive(): boolean {
  70|   if (!getFeatureValue_CACHED_MAY_BE_STALE('tengu_passport_quail', false)) {
  71|     return false
  72|   }
  73|   return (
  74|     !getIsNonInteractiveSession() ||
  75|     getFeatureValue_CACHED_MAY_BE_STALE('tengu_slate_thimble', false)
  76|   )
  77| }
  78| 
  79| /**
  80|  * Returns the base directory for persistent memory storage.
  81|  * Resolution order:
  82|  *   1. CLAUDE_CODE_REMOTE_MEMORY_DIR env var (explicit override, set in CCR)
  83|  *   2. ~/.claude (default config home)
  84|  */
  85| export function getMemoryBaseDir(): string {
  86|   if (process.env.CLAUDE_CODE_REMOTE_MEMORY_DIR) {
  87|     return process.env.CLAUDE_CODE_REMOTE_MEMORY_DIR
  88|   }
  89|   return getClaudeConfigHomeDir()
  90| }

源码引用: src/memdir/paths.ts · 第 95–130 行(共 279 行)

  95| /**
  96|  * Normalize and validate a candidate auto-memory directory path.
  97|  *
  98|  * SECURITY: Rejects paths that would be dangerous as a read-allowlist root
  99|  * or that normalize() doesn't fully resolve:
 100|  * - relative (!isAbsolute): "../foo" — would be interpreted relative to CWD
 101|  * - root/near-root (length < 3): "/" → "" after strip; "/a" too short
 102|  * - Windows drive-root (C: regex): "C:\" → "C:" after strip
 103|  * - UNC paths (\\server\share): network paths — opaque trust boundary
 104|  * - null byte: survives normalize(), can truncate in syscalls
 105|  *
 106|  * Returns the normalized path with exactly one trailing separator,
 107|  * or undefined if the path is unset/empty/rejected.
 108|  */
 109| function validateMemoryPath(
 110|   raw: string | undefined,
 111|   expandTilde: boolean,
 112| ): string | undefined {
 113|   if (!raw) {
 114|     return undefined
 115|   }
 116|   let candidate = raw
 117|   // Settings.json paths support ~/ expansion (user-friendly). The env var
 118|   // override does not (it's set programmatically by Cowork/SDK, which should
 119|   // always pass absolute paths). Bare "~", "~/", "~/.", "~/..", etc. are NOT
 120|   // expanded — they would make isAutoMemPath() match all of $HOME or its
 121|   // parent (same class of danger as "/" or "C:\").
 122|   if (
 123|     expandTilde &&
 124|     (candidate.startsWith('~/') || candidate.startsWith('~\\'))
 125|   ) {
 126|     const rest = candidate.slice(2)
 127|     // Reject trivial remainders that would expand to $HOME or an ancestor.
 128|     // normalize('') = '.', normalize('.') = '.', normalize('foo/..') = '.',
 129|     // normalize('..') = '..', normalize('foo/../..') = '..'
 130|     const restNorm = normalize(rest || '.')

truncateEntrypointContent

常量:MAX_ENTRYPOINT_LINES=200,MAX_ENTRYPOINT_BYTES=25000。

算法:trim → 统计行/字节 → 超 line 则 slice 前 200 行 → 仍超 byte 则在 MAX 前 lastIndexOf newline 截断 → 追加 WARNING 段落说明触发 cap 原因。

EntrypointTruncation 返回 wasLineTruncated/wasByteTruncated 供 analytics tengu_memdir_loaded。

源码引用: src/memdir/memdir.ts · 第 34–103 行(共 508 行)

  34| export const ENTRYPOINT_NAME = 'MEMORY.md'
  35| export const MAX_ENTRYPOINT_LINES = 200
  36| // ~125 chars/line at 200 lines. At p97 today; catches long-line indexes that
  37| // slip past the line cap (p100 observed: 197KB under 200 lines).
  38| export const MAX_ENTRYPOINT_BYTES = 25_000
  39| const AUTO_MEM_DISPLAY_NAME = 'auto memory'
  40| 
  41| export type EntrypointTruncation = {
  42|   content: string
  43|   lineCount: number
  44|   byteCount: number
  45|   wasLineTruncated: boolean
  46|   wasByteTruncated: boolean
  47| }
  48| 
  49| /**
  50|  * Truncate MEMORY.md content to the line AND byte caps, appending a warning
  51|  * that names which cap fired. Line-truncates first (natural boundary), then
  52|  * byte-truncates at the last newline before the cap so we don't cut mid-line.
  53|  *
  54|  * Shared by buildMemoryPrompt and claudemd getMemoryFiles (previously
  55|  * duplicated the line-only logic).
  56|  */
  57| export function truncateEntrypointContent(raw: string): EntrypointTruncation {
  58|   const trimmed = raw.trim()
  59|   const contentLines = trimmed.split('\n')
  60|   const lineCount = contentLines.length
  61|   const byteCount = trimmed.length
  62| 
  63|   const wasLineTruncated = lineCount > MAX_ENTRYPOINT_LINES
  64|   // Check original byte count — long lines are the failure mode the byte cap
  65|   // targets, so post-line-truncation size would understate the warning.
  66|   const wasByteTruncated = byteCount > MAX_ENTRYPOINT_BYTES
  67| 
  68|   if (!wasLineTruncated && !wasByteTruncated) {
  69|     return {
  70|       content: trimmed,
  71|       lineCount,
  72|       byteCount,
  73|       wasLineTruncated,
  74|       wasByteTruncated,
  75|     }
  76|   }
  77| 
  78|   let truncated = wasLineTruncated
  79|     ? contentLines.slice(0, MAX_ENTRYPOINT_LINES).join('\n')
  80|     : trimmed
  81| 
  82|   if (truncated.length > MAX_ENTRYPOINT_BYTES) {
  83|     const cutAt = truncated.lastIndexOf('\n', MAX_ENTRYPOINT_BYTES)
  84|     truncated = truncated.slice(0, cutAt > 0 ? cutAt : MAX_ENTRYPOINT_BYTES)
  85|   }
  86| 
  87|   const reason =
  88|     wasByteTruncated && !wasLineTruncated
  89|       ? `${formatFileSize(byteCount)} (limit: ${formatFileSize(MAX_ENTRYPOINT_BYTES)}) — index entries are too long`
  90|       : wasLineTruncated && !wasByteTruncated
  91|         ? `${lineCount} lines (limit: ${MAX_ENTRYPOINT_LINES})`
  92|         : `${lineCount} lines and ${formatFileSize(byteCount)}`
  93| 
  94|   return {
  95|     content:
  96|       truncated +
  97|       `\n\n> WARNING: ${ENTRYPOINT_NAME} is ${reason}. Only part of it was loaded. Keep index entries to one line under ~200 chars; move detail into topic files.`,
  98|     lineCount,
  99|     byteCount,
 100|     wasLineTruncated,
 101|     wasByteTruncated,
 102|   }
 103| }

memoryTypes 四类型 taxonomy

MEMORY_TYPES = user | feedback | project | reference。

parseMemoryType 无效值返回 undefined——legacy 无 type 字段仍可读。

TYPES_SECTION_COMBINED 含 scope private/team;TYPES_SECTION_INDIVIDUAL 单目录模式。

另导出 WHAT_NOT_TO_SAVE_SECTION、WHEN_TO_ACCESS_SECTION、TRUSTING_RECALL_SECTION、MEMORY_FRONTMATTER_EXAMPLE 拼进 buildMemoryLines。

源码引用: src/memdir/memoryTypes.ts · 第 14–31 行(共 272 行)

  14| export const MEMORY_TYPES = [
  15|   'user',
  16|   'feedback',
  17|   'project',
  18|   'reference',
  19| ] as const
  20| 
  21| export type MemoryType = (typeof MEMORY_TYPES)[number]
  22| 
  23| /**
  24|  * Parse a raw frontmatter value into a MemoryType.
  25|  * Invalid or missing values return undefined — legacy files without a
  26|  * `type:` field keep working, files with unknown types degrade gracefully.
  27|  */
  28| export function parseMemoryType(raw: unknown): MemoryType | undefined {
  29|   if (typeof raw !== 'string') return undefined
  30|   return MEMORY_TYPES.find(t => t === raw)
  31| }

源码引用: src/memdir/memoryTypes.ts · 第 37–60 行(共 272 行)

  37| export const TYPES_SECTION_COMBINED: readonly string[] = [
  38|   '## Types of memory',
  39|   '',
  40|   'There are several discrete types of memory that you can store in your memory system. Each type below declares a <scope> of `private`, `team`, or guidance for choosing between the two.',
  41|   '',
  42|   '<types>',
  43|   '<type>',
  44|   '    <name>user</name>',
  45|   '    <scope>always private</scope>',
  46|   "    <description>Contain information about the user's role, goals, responsibilities, and knowledge. Great user memories help you tailor your future behavior to the user's preferences and perspective. Your goal in reading and writing these memories is to build up an understanding of who the user is and how you can be most helpful to them specifically. For example, you should collaborate with a senior software engineer differently than a student who is coding for the very first time. Keep in mind, that the aim here is to be helpful to the user. Avoid writing memories about the user that could be viewed as a negative judgement or that are not relevant to the work you're trying to accomplish together.</description>",
  47|   "    <when_to_save>When you learn any details about the user's role, preferences, responsibilities, or knowledge</when_to_save>",
  48|   "    <how_to_use>When your work should be informed by the user's profile or perspective. For example, if the user is asking you to explain a part of the code, you should answer that question in a way that is tailored to the specific details that they will find most valuable or that helps them build their mental model in relation to domain knowledge they already have.</how_to_use>",
  49|   '    <examples>',
  50|   "    user: I'm a data scientist investigating what logging we have in place",
  51|   '    assistant: [saves private user memory: user is a data scientist, currently focused on observability/logging]',
  52|   '',
  53|   "    user: I've been writing Go for ten years but this is my first time touching the React side of this repo",
  54|   "    assistant: [saves private user memory: deep Go expertise, new to React and this project's frontend — frame frontend explanations in terms of backend analogues]",
  55|   '    </examples>',
  56|   '</type>',
  57|   '<type>',
  58|   '    <name>feedback</name>',
  59|   '    <scope>default to private. Save as team only when the guidance is clearly a project-wide convention that every contributor should follow (e.g., a testing policy, a build invariant), not a personal style preference.</scope>',
  60|   "    <description>Guidance the user has given you about how to approach work — both what to avoid and what to keep doing. These are a very important type of memory to read and write as they allow you to remain coherent and responsive to the way you should approach work in the project. Record from failure AND success: if you only save corrections, you will avoid past mistakes but drift away from approaches the user has already validated, and may grow overly cautious. Before saving a private feedback memory, check that it doesn't contradict a team feedback memory — if it does, either don't save it or note the override explicitly.</description>",

buildMemoryLines 与 buildMemoryPrompt

buildMemoryLines(displayName, memoryDir, extraGuidelines?, skipIndex?) 组装行为指南,不含 MEMORY 正文。

buildMemoryPrompt 同步 readFileSync entrypoint,truncate 后 append ## MEMORY.md 段。

DIR_EXISTS_GUIDANCE / DIRS_EXIST_GUIDANCE:告诉模型目录已存在,勿 mkdir 浪费 turn。

ensureMemoryDirExists:loadMemoryPrompt cache 路径调用,mkdir 失败 log debug 不 block prompt。

源码引用: src/memdir/memdir.ts · 第 111–147 行(共 508 行)

 111| /**
 112|  * Shared guidance text appended to each memory directory prompt line.
 113|  * Shipped because Claude was burning turns on `ls`/`mkdir -p` before writing.
 114|  * Harness guarantees the directory exists via ensureMemoryDirExists().
 115|  */
 116| export const DIR_EXISTS_GUIDANCE =
 117|   'This directory already exists — write to it directly with the Write tool (do not run mkdir or check for its existence).'
 118| export const DIRS_EXIST_GUIDANCE =
 119|   'Both directories already exist — write to them directly with the Write tool (do not run mkdir or check for their existence).'
 120| 
 121| /**
 122|  * Ensure a memory directory exists. Idempotent — called from loadMemoryPrompt
 123|  * (once per session via systemPromptSection cache) so the model can always
 124|  * write without checking existence first. FsOperations.mkdir is recursive
 125|  * by default and already swallows EEXIST, so the full parent chain
 126|  * (~/.claude/projects/<slug>/memory/) is created in one call with no
 127|  * try/catch needed for the happy path.
 128|  */
 129| export async function ensureMemoryDirExists(memoryDir: string): Promise<void> {
 130|   const fs = getFsImplementation()
 131|   try {
 132|     await fs.mkdir(memoryDir)
 133|   } catch (e) {
 134|     // fs.mkdir already handles EEXIST internally. Anything reaching here is
 135|     // a real problem (EACCES/EPERM/EROFS) — log so --debug shows why. Prompt
 136|     // building continues either way; the model's Write will surface the
 137|     // real perm error (and FileWriteTool does its own mkdir of the parent).
 138|     const code =
 139|       e instanceof Error && 'code' in e && typeof e.code === 'string'
 140|         ? e.code
 141|         : undefined
 142|     logForDebugging(
 143|       `ensureMemoryDirExists failed for ${memoryDir}: ${code ?? String(e)}`,
 144|       { level: 'debug' },
 145|     )
 146|   }
 147| }

源码引用: src/memdir/memdir.ts · 第 199–266 行(共 508 行)

 199| export function buildMemoryLines(
 200|   displayName: string,
 201|   memoryDir: string,
 202|   extraGuidelines?: string[],
 203|   skipIndex = false,
 204| ): string[] {
 205|   const howToSave = skipIndex
 206|     ? [
 207|         '## How to save memories',
 208|         '',
 209|         'Write each memory to its own file (e.g., `user_role.md`, `feedback_testing.md`) using this frontmatter format:',
 210|         '',
 211|         ...MEMORY_FRONTMATTER_EXAMPLE,
 212|         '',
 213|         '- Keep the name, description, and type fields in memory files up-to-date with the content',
 214|         '- Organize memory semantically by topic, not chronologically',
 215|         '- Update or remove memories that turn out to be wrong or outdated',
 216|         '- Do not write duplicate memories. First check if there is an existing memory you can update before writing a new one.',
 217|       ]
 218|     : [
 219|         '## How to save memories',
 220|         '',
 221|         'Saving a memory is a two-step process:',
 222|         '',
 223|         '**Step 1** — write the memory to its own file (e.g., `user_role.md`, `feedback_testing.md`) using this frontmatter format:',
 224|         '',
 225|         ...MEMORY_FRONTMATTER_EXAMPLE,
 226|         '',
 227|         `**Step 2** — add a pointer to that file in \`${ENTRYPOINT_NAME}\`. \`${ENTRYPOINT_NAME}\` is an index, not a memory — each entry should be one line, under ~150 characters: \`- [Title](file.md) — one-line hook\`. It has no frontmatter. Never write memory content directly into \`${ENTRYPOINT_NAME}\`.`,
 228|         '',
 229|         `- \`${ENTRYPOINT_NAME}\` is always loaded into your conversation context — lines after ${MAX_ENTRYPOINT_LINES} will be truncated, so keep the index concise`,
 230|         '- Keep the name, description, and type fields in memory files up-to-date with the content',
 231|         '- Organize memory semantically by topic, not chronologically',
 232|         '- Update or remove memories that turn out to be wrong or outdated',
 233|         '- Do not write duplicate memories. First check if there is an existing memory you can update before writing a new one.',
 234|       ]
 235| 
 236|   const lines: string[] = [
 237|     `# ${displayName}`,
 238|     '',
 239|     `You have a persistent, file-based memory system at \`${memoryDir}\`. ${DIR_EXISTS_GUIDANCE}`,
 240|     '',
 241|     "You should build up this memory system over time so that future conversations can have a complete picture of who the user is, how they'd like to collaborate with you, what behaviors to avoid or repeat, and the context behind the work the user gives you.",
 242|     '',
 243|     'If the user explicitly asks you to remember something, save it immediately as whichever type fits best. If they ask you to forget something, find and remove the relevant entry.',
 244|     '',
 245|     ...TYPES_SECTION_INDIVIDUAL,
 246|     ...WHAT_NOT_TO_SAVE_SECTION,
 247|     '',
 248|     ...howToSave,
 249|     '',
 250|     ...WHEN_TO_ACCESS_SECTION,
 251|     '',
 252|     ...TRUSTING_RECALL_SECTION,
 253|     '',
 254|     '## Memory and other forms of persistence',
 255|     'Memory is one of several persistence mechanisms available to you as you assist the user in a given conversation. The distinction is often that memory can be recalled in future conversations and should not be used for persisting information that is only useful within the scope of the current conversation.',
 256|     '- When to use or update a plan instead of memory: If you are about to start a non-trivial implementation task and would like to reach alignment with the user on your approach you should use a Plan rather than saving this information to memory. Similarly, if you already have a plan within the conversation and you have changed your approach persist that change by updating the plan rather than saving a memory.',
 257|     '- When to use or update tasks instead of memory: When you need to break your work in current conversation into discrete steps or keep track of your progress use tasks instead of saving to memory. Tasks are great for persisting information about the work that needs to be done in the current conversation, but memory should be reserved for information that will be useful in future conversations.',
 258|     '',
 259|     ...(extraGuidelines ?? []),
 260|     '',
 261|   ]
 262| 
 263|   lines.push(...buildSearchingPastContextSection(memoryDir))
 264| 
 265|   return lines
 266| }

源码引用: src/memdir/memdir.ts · 第 272–316 行(共 508 行)

 272| export function buildMemoryPrompt(params: {
 273|   displayName: string
 274|   memoryDir: string
 275|   extraGuidelines?: string[]
 276| }): string {
 277|   const { displayName, memoryDir, extraGuidelines } = params
 278|   const fs = getFsImplementation()
 279|   const entrypoint = memoryDir + ENTRYPOINT_NAME
 280| 
 281|   // Directory creation is the caller's responsibility (loadMemoryPrompt /
 282|   // loadAgentMemoryPrompt). Builders only read, they don't mkdir.
 283| 
 284|   // Read existing memory entrypoint (sync: prompt building is synchronous)
 285|   let entrypointContent = ''
 286|   try {
 287|     // eslint-disable-next-line custom-rules/no-sync-fs
 288|     entrypointContent = fs.readFileSync(entrypoint, { encoding: 'utf-8' })
 289|   } catch {
 290|     // No memory file yet
 291|   }
 292| 
 293|   const lines = buildMemoryLines(displayName, memoryDir, extraGuidelines)
 294| 
 295|   if (entrypointContent.trim()) {
 296|     const t = truncateEntrypointContent(entrypointContent)
 297|     const memoryType = displayName === AUTO_MEM_DISPLAY_NAME ? 'auto' : 'agent'
 298|     logMemoryDirCounts(memoryDir, {
 299|       content_length: t.byteCount,
 300|       line_count: t.lineCount,
 301|       was_truncated: t.wasLineTruncated,
 302|       was_byte_truncated: t.wasByteTruncated,
 303|       memory_type:
 304|         memoryType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
 305|     })
 306|     lines.push(`## ${ENTRYPOINT_NAME}`, '', t.content)
 307|   } else {
 308|     lines.push(
 309|       `## ${ENTRYPOINT_NAME}`,
 310|       '',
 311|       `Your ${ENTRYPOINT_NAME} is currently empty. When you save new memories, they will appear here.`,
 312|     )
 313|   }
 314| 
 315|   return lines.join('\n')
 316| }

loadMemoryPrompt 与会话注入

loadMemoryPrompt 被 QueryEngine、constants/prompts.ts 调用,结果 cache 在 systemPromptSection。

流程:isAutoMemoryEnabled → getAutoMemPath → ensureMemoryDirExists → buildMemoryLines + 读 MEMORY.md truncate → 可选 teamMemPrompts COMBINED 段 → logMemoryDirCounts 异步 telemetry。

hasAutoMemPathOverride 影响 displayName 与 analytics metadata。

源码引用: src/memdir/memdir.ts · 第 350–450 行(共 508 行)

 350|     '## What to log',
 351|     '- User corrections and preferences ("use bun, not npm"; "stop summarizing diffs")',
 352|     '- Facts about the user, their role, or their goals',
 353|     '- Project context that is not derivable from the code (deadlines, incidents, decisions and their rationale)',
 354|     '- Pointers to external systems (dashboards, Linear projects, Slack channels)',
 355|     '- Anything the user explicitly asks you to remember',
 356|     '',
 357|     ...WHAT_NOT_TO_SAVE_SECTION,
 358|     '',
 359|     ...(skipIndex
 360|       ? []
 361|       : [
 362|           `## ${ENTRYPOINT_NAME}`,
 363|           `\`${ENTRYPOINT_NAME}\` is the distilled index (maintained nightly from your logs) and is loaded into your context automatically. Read it for orientation, but do not edit it directly — record new information in today's log instead.`,
 364|           '',
 365|         ]),
 366|     ...buildSearchingPastContextSection(memoryDir),
 367|   ]
 368| 
 369|   return lines.join('\n')
 370| }
 371| 
 372| /**
 373|  * Build the "Searching past context" section if the feature gate is enabled.
 374|  */
 375| export function buildSearchingPastContextSection(autoMemDir: string): string[] {
 376|   if (!getFeatureValue_CACHED_MAY_BE_STALE('tengu_coral_fern', false)) {
 377|     return []
 378|   }
 379|   const projectDir = getProjectDir(getOriginalCwd())
 380|   // Ant-native builds alias grep to embedded ugrep and remove the dedicated
 381|   // Grep tool, so give the model a real shell invocation there.
 382|   // In REPL mode, both Grep and Bash are hidden from direct use — the model
 383|   // calls them from inside REPL scripts, so the grep shell form is what it
 384|   // will write in the script anyway.
 385|   const embedded = hasEmbeddedSearchTools() || isReplModeEnabled()
 386|   const memSearch = embedded
 387|     ? `grep -rn "<search term>" ${autoMemDir} --include="*.md"`
 388|     : `${GREP_TOOL_NAME} with pattern="<search term>" path="${autoMemDir}" glob="*.md"`
 389|   const transcriptSearch = embedded
 390|     ? `grep -rn "<search term>" ${projectDir}/ --include="*.jsonl"`
 391|     : `${GREP_TOOL_NAME} with pattern="<search term>" path="${projectDir}/" glob="*.jsonl"`
 392|   return [
 393|     '## Searching past context',
 394|     '',
 395|     'When looking for past context:',
 396|     '1. Search topic files in your memory directory:',
 397|     '```',
 398|     memSearch,
 399|     '```',
 400|     '2. Session transcript logs (last resort — large files, slow):',
 401|     '```',
 402|     transcriptSearch,
 403|     '```',
 404|     'Use narrow search terms (error messages, file paths, function names) rather than broad keywords.',
 405|     '',
 406|   ]
 407| }
 408| 
 409| /**
 410|  * Load the unified memory prompt for inclusion in the system prompt.
 411|  * Dispatches based on which memory systems are enabled:
 412|  *   - auto + team: combined prompt (both directories)
 413|  *   - auto only: memory lines (single directory)
 414|  * Team memory requires auto memory (enforced by isTeamMemoryEnabled), so
 415|  * there is no team-only branch.
 416|  *
 417|  * Returns null when auto memory is disabled.
 418|  */
 419| export async function loadMemoryPrompt(): Promise<string | null> {
 420|   const autoEnabled = isAutoMemoryEnabled()
 421| 
 422|   const skipIndex = getFeatureValue_CACHED_MAY_BE_STALE(
 423|     'tengu_moth_copse',
 424|     false,
 425|   )
 426| 
 427|   // KAIROS daily-log mode takes precedence over TEAMMEM: the append-only
 428|   // log paradigm does not compose with team sync (which expects a shared
 429|   // MEMORY.md that both sides read + write). Gating on `autoEnabled` here
 430|   // means the !autoEnabled case falls through to the tengu_memdir_disabled
 431|   // telemetry block below, matching the non-KAIROS path.
 432|   if (feature('KAIROS') && autoEnabled && getKairosActive()) {
 433|     logMemoryDirCounts(getAutoMemPath(), {
 434|       memory_type:
 435|         'auto' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
 436|     })
 437|     return buildAssistantDailyLogPrompt(skipIndex)
 438|   }
 439| 
 440|   // Cowork injects memory-policy text via env var; thread into all builders.
 441|   const coworkExtraGuidelines =
 442|     process.env.CLAUDE_COWORK_MEMORY_EXTRA_GUIDELINES
 443|   const extraGuidelines =
 444|     coworkExtraGuidelines && coworkExtraGuidelines.trim().length > 0
 445|       ? [coworkExtraGuidelines]
 446|       : undefined
 447| 
 448|   if (feature('TEAMMEM')) {
 449|     if (teamMemPaths!.isTeamMemoryEnabled()) {
 450|       const autoDir = getAutoMemPath()

源码引用: src/QueryEngine.ts · 第 33–35 行(共 1296 行)

  33| import { loadMemoryPrompt } from './memdir/memdir.js'
  34| import { hasAutoMemPathOverride } from './memdir/paths.js'
  35| import { query } from './query.js'

源码引用: src/constants/prompts.ts · 第 60–65 行(共 915 行)

  60| import { loadMemoryPrompt } from '../memdir/memdir.js'
  61| import { isUndercover } from '../utils/undercover.js'
  62| import { isMcpInstructionsDeltaEnabled } from '../utils/mcpInstructionsDelta.js'
  63| 
  64| // Dead code elimination: conditional imports for feature-gated modules
  65| /* eslint-disable @typescript-eslint/no-require-imports */

memoryAge 与 findRelevantMemories 前置

memoryAge.ts 提供记忆文件年龄描述字符串,供 UI 或 prompt 附加 freshness hint。

findRelevantMemories(独立文件)依赖 memoryScan.scanMemoryFiles,exclude MEMORY.md,Sonnet 选最多 5 个 topic 文件。memdir-core 提供目录与 header scan;召回逻辑在 findRelevantMemories 章与 memory-extraction 交叉引用。

源码引用: src/memdir/memoryAge.ts · 第 1–50 行(共 54 行)

   1| /**
   2|  * Days elapsed since mtime.  Floor-rounded — 0 for today, 1 for
   3|  * yesterday, 2+ for older.  Negative inputs (future mtime, clock skew)
   4|  * clamp to 0.
   5|  */
   6| export function memoryAgeDays(mtimeMs: number): number {
   7|   return Math.max(0, Math.floor((Date.now() - mtimeMs) / 86_400_000))
   8| }
   9| 
  10| /**
  11|  * Human-readable age string.  Models are poor at date arithmetic —
  12|  * a raw ISO timestamp doesn't trigger staleness reasoning the way
  13|  * "47 days ago" does.
  14|  */
  15| export function memoryAge(mtimeMs: number): string {
  16|   const d = memoryAgeDays(mtimeMs)
  17|   if (d === 0) return 'today'
  18|   if (d === 1) return 'yesterday'
  19|   return `${d} days ago`
  20| }
  21| 
  22| /**
  23|  * Plain-text staleness caveat for memories >1 day old.  Returns ''
  24|  * for fresh (today/yesterday) memories — warning there is noise.
  25|  *
  26|  * Use this when the consumer already provides its own wrapping
  27|  * (e.g. messages.ts relevant_memories → wrapMessagesInSystemReminder).
  28|  *
  29|  * Motivated by user reports of stale code-state memories (file:line
  30|  * citations to code that has since changed) being asserted as fact —
  31|  * the citation makes the stale claim sound more authoritative, not less.
  32|  */
  33| export function memoryFreshnessText(mtimeMs: number): string {
  34|   const d = memoryAgeDays(mtimeMs)
  35|   if (d <= 1) return ''
  36|   return (
  37|     `This memory is ${d} days old. ` +
  38|     `Memories are point-in-time observations, not live state — ` +
  39|     `claims about code behavior or file:line citations may be outdated. ` +
  40|     `Verify against current code before asserting as fact.`
  41|   )
  42| }
  43| 
  44| /**
  45|  * Per-memory staleness note wrapped in <system-reminder> tags.
  46|  * Returns '' for memories ≤ 1 day old.  Use this for callers that
  47|  * don't add their own system-reminder wrapper (e.g. FileReadTool output).
  48|  */
  49| export function memoryFreshnessNote(mtimeMs: number): string {
  50|   const text = memoryFreshnessText(mtimeMs)

源码引用: src/memdir/findRelevantMemories.ts · 第 26–75 行(共 142 行)

  26| /**
  27|  * Find memory files relevant to a query by scanning memory file headers
  28|  * and asking Sonnet to select the most relevant ones.
  29|  *
  30|  * Returns absolute file paths + mtime of the most relevant memories
  31|  * (up to 5). Excludes MEMORY.md (already loaded in system prompt).
  32|  * mtime is threaded through so callers can surface freshness to the
  33|  * main model without a second stat.
  34|  *
  35|  * `alreadySurfaced` filters paths shown in prior turns before the
  36|  * Sonnet call, so the selector spends its 5-slot budget on fresh
  37|  * candidates instead of re-picking files the caller will discard.
  38|  */
  39| export async function findRelevantMemories(
  40|   query: string,
  41|   memoryDir: string,
  42|   signal: AbortSignal,
  43|   recentTools: readonly string[] = [],
  44|   alreadySurfaced: ReadonlySet<string> = new Set(),
  45| ): Promise<RelevantMemory[]> {
  46|   const memories = (await scanMemoryFiles(memoryDir, signal)).filter(
  47|     m => !alreadySurfaced.has(m.filePath),
  48|   )
  49|   if (memories.length === 0) {
  50|     return []
  51|   }
  52| 
  53|   const selectedFilenames = await selectRelevantMemories(
  54|     query,
  55|     memories,
  56|     signal,
  57|     recentTools,
  58|   )
  59|   const byFilename = new Map(memories.map(m => [m.filename, m]))
  60|   const selected = selectedFilenames
  61|     .map(filename => byFilename.get(filename))
  62|     .filter((m): m is MemoryHeader => m !== undefined)
  63| 
  64|   // Fires even on empty selection: selection-rate needs the denominator,
  65|   // and -1 ages distinguish "ran, picked nothing" from "never ran".
  66|   if (feature('MEMORY_SHAPE_TELEMETRY')) {
  67|     /* eslint-disable @typescript-eslint/no-require-imports */
  68|     const { logMemoryRecallShape } =
  69|       require('./memoryShapeTelemetry.js') as typeof import('./memoryShapeTelemetry.js')
  70|     /* eslint-enable @typescript-eslint/no-require-imports */
  71|     logMemoryRecallShape(memories, selected)
  72|   }
  73| 
  74|   return selected.map(m => ({ path: m.filePath, mtimeMs: m.mtimeMs }))
  75| }

teamMemPaths(TEAMMEM)

teamMemPaths.ts 定义 team memory 目录 memory/team/ 与 TEAM MEMORY.md entrypoint。

teamMemPrompts.ts 生成 COMBINED 双目录 prompt 段,与 buildMemoryLines extraGuidelines 合并。

feature 关闭时 bundle 不含 require,memdir.ts 内 teamMemPaths 为 null。

源码引用: src/memdir/teamMemPaths.ts · 第 75–95 行(共 293 行)

  75|     return false
  76|   }
  77|   return getFeatureValue_CACHED_MAY_BE_STALE('tengu_herring_clock', false)
  78| }
  79| 
  80| /**
  81|  * Returns the team memory path: <memoryBase>/projects/<sanitized-project-root>/memory/team/
  82|  * Lives as a subdirectory of the auto-memory directory, scoped per-project.
  83|  */
  84| export function getTeamMemPath(): string {
  85|   return (join(getAutoMemPath(), 'team') + sep).normalize('NFC')
  86| }
  87| 
  88| /**
  89|  * Returns the team memory entrypoint: <memoryBase>/projects/<sanitized-project-root>/memory/team/MEMORY.md
  90|  * Lives as a subdirectory of the auto-memory directory, scoped per-project.
  91|  */
  92| export function getTeamMemEntrypoint(): string {
  93|   return join(getAutoMemPath(), 'team', 'MEMORY.md')
  94| }
  95| 

源码引用: src/memdir/teamMemPrompts.ts · 第 1–40 行(共 101 行)

   1| import {
   2|   buildSearchingPastContextSection,
   3|   DIRS_EXIST_GUIDANCE,
   4|   ENTRYPOINT_NAME,
   5|   MAX_ENTRYPOINT_LINES,
   6| } from './memdir.js'
   7| import {
   8|   MEMORY_DRIFT_CAVEAT,
   9|   MEMORY_FRONTMATTER_EXAMPLE,
  10|   TRUSTING_RECALL_SECTION,
  11|   TYPES_SECTION_COMBINED,
  12|   WHAT_NOT_TO_SAVE_SECTION,
  13| } from './memoryTypes.js'
  14| import { getAutoMemPath } from './paths.js'
  15| import { getTeamMemPath } from './teamMemPaths.js'
  16| 
  17| /**
  18|  * Build the combined prompt when both auto memory and team memory are enabled.
  19|  * Closed four-type taxonomy (user / feedback / project / reference) with
  20|  * per-type <scope> guidance embedded in XML-style <type> blocks.
  21|  */
  22| export function buildCombinedMemoryPrompt(
  23|   extraGuidelines?: string[],
  24|   skipIndex = false,
  25| ): string {
  26|   const autoDir = getAutoMemPath()
  27|   const teamDir = getTeamMemPath()
  28| 
  29|   const howToSave = skipIndex
  30|     ? [
  31|         '## How to save memories',
  32|         '',
  33|         "Write each memory to its own file in the chosen directory (private or team, per the type's scope guidance) using this frontmatter format:",
  34|         '',
  35|         ...MEMORY_FRONTMATTER_EXAMPLE,
  36|         '',
  37|         '- Keep the name, description, and type fields in memory files up-to-date with the content',
  38|         '- Organize memory semantically by topic, not chronologically',
  39|         '- Update or remove memories that turn out to be wrong or outdated',
  40|         '- Do not write duplicate memories. First check if there is an existing memory you can update before writing a new one.',

加载路径与写入指导的分工

memdir-core 的核心边界是“系统提示负责教会模型怎样写,运行时代码负责提供安全路径与索引内容”。paths.ts 决定 auto memory 是否启用、目录在哪里、路径是否安全;memdir.ts 的 ensureMemoryDirExists 保证目录已经存在;buildMemoryLines 把保存规则、类型 taxonomy、frontmatter 示例、what not to save、when to access 等指南拼进系统提示;buildMemoryPrompt 才读取 MEMORY.md 内容并做 200 行/25KB 截断。这样的拆分让模型不需要先 ls/mkdir 才知道记忆目录,也避免 MEMORY.md 过长时把系统提示撑爆。

MEMORY.md 作为 entrypoint 只保存 topic 文件索引,是为了让召回、提取和人工编辑都围绕小文件工作。模型保存新记忆时先写具体 topic.md,再更新 MEMORY.md 一行链接;loadMemoryPrompt 注入的是“如何保存”和“索引里已有内容”,findRelevantMemories 或 extractMemories 再按需要扫描 topic 文件。TEAMMEM 开启时,auto memory 与 team memory 的 prompt 组合成双目录指导,但路径和权限仍分开;关闭时 require 不进入 bundle。读代码时若看到 SessionMemory、compact 或 commands/memory,不要把它们混作 memdir-core:SessionMemory 是会话内摘要文件,/memory 是 UI 编辑入口,memdir-core 只管 auto-memory 目录、prompt 与入口索引。

路径安全是另一个教学重点。validateMemoryPath 拒绝相对路径、根目录、UNC 和 null byte,hasAutoMemPathOverride 又影响展示名与 analytics,说明记忆目录既是模型可写目标,也是用户可配置的敏感路径。任何新增入口都应复用 paths.ts,而不是手动拼 ~/.claude。否则轻则 prompt 展示和实际写入不一致,重则把模型写权限扩到错误目录。

源码引用: src/memdir/memdir.ts · 第 122–147 行(共 508 行)

 122|  * Ensure a memory directory exists. Idempotent — called from loadMemoryPrompt
 123|  * (once per session via systemPromptSection cache) so the model can always
 124|  * write without checking existence first. FsOperations.mkdir is recursive
 125|  * by default and already swallows EEXIST, so the full parent chain
 126|  * (~/.claude/projects/<slug>/memory/) is created in one call with no
 127|  * try/catch needed for the happy path.
 128|  */
 129| export async function ensureMemoryDirExists(memoryDir: string): Promise<void> {
 130|   const fs = getFsImplementation()
 131|   try {
 132|     await fs.mkdir(memoryDir)
 133|   } catch (e) {
 134|     // fs.mkdir already handles EEXIST internally. Anything reaching here is
 135|     // a real problem (EACCES/EPERM/EROFS) — log so --debug shows why. Prompt
 136|     // building continues either way; the model's Write will surface the
 137|     // real perm error (and FileWriteTool does its own mkdir of the parent).
 138|     const code =
 139|       e instanceof Error && 'code' in e && typeof e.code === 'string'
 140|         ? e.code
 141|         : undefined
 142|     logForDebugging(
 143|       `ensureMemoryDirExists failed for ${memoryDir}: ${code ?? String(e)}`,
 144|       { level: 'debug' },
 145|     )
 146|   }
 147| }

源码引用: src/memdir/memdir.ts · 第 199–316 行(共 508 行)

 199| export function buildMemoryLines(
 200|   displayName: string,
 201|   memoryDir: string,
 202|   extraGuidelines?: string[],
 203|   skipIndex = false,
 204| ): string[] {
 205|   const howToSave = skipIndex
 206|     ? [
 207|         '## How to save memories',
 208|         '',
 209|         'Write each memory to its own file (e.g., `user_role.md`, `feedback_testing.md`) using this frontmatter format:',
 210|         '',
 211|         ...MEMORY_FRONTMATTER_EXAMPLE,
 212|         '',
 213|         '- Keep the name, description, and type fields in memory files up-to-date with the content',
 214|         '- Organize memory semantically by topic, not chronologically',
 215|         '- Update or remove memories that turn out to be wrong or outdated',
 216|         '- Do not write duplicate memories. First check if there is an existing memory you can update before writing a new one.',
 217|       ]
 218|     : [
 219|         '## How to save memories',
 220|         '',
 221|         'Saving a memory is a two-step process:',
 222|         '',
 223|         '**Step 1** — write the memory to its own file (e.g., `user_role.md`, `feedback_testing.md`) using this frontmatter format:',
 224|         '',
 225|         ...MEMORY_FRONTMATTER_EXAMPLE,
 226|         '',
 227|         `**Step 2** — add a pointer to that file in \`${ENTRYPOINT_NAME}\`. \`${ENTRYPOINT_NAME}\` is an index, not a memory — each entry should be one line, under ~150 characters: \`- [Title](file.md) — one-line hook\`. It has no frontmatter. Never write memory content directly into \`${ENTRYPOINT_NAME}\`.`,
 228|         '',
 229|         `- \`${ENTRYPOINT_NAME}\` is always loaded into your conversation context — lines after ${MAX_ENTRYPOINT_LINES} will be truncated, so keep the index concise`,
 230|         '- Keep the name, description, and type fields in memory files up-to-date with the content',
 231|         '- Organize memory semantically by topic, not chronologically',
 232|         '- Update or remove memories that turn out to be wrong or outdated',
 233|         '- Do not write duplicate memories. First check if there is an existing memory you can update before writing a new one.',
 234|       ]
 235| 
 236|   const lines: string[] = [
 237|     `# ${displayName}`,
 238|     '',
 239|     `You have a persistent, file-based memory system at \`${memoryDir}\`. ${DIR_EXISTS_GUIDANCE}`,
 240|     '',
 241|     "You should build up this memory system over time so that future conversations can have a complete picture of who the user is, how they'd like to collaborate with you, what behaviors to avoid or repeat, and the context behind the work the user gives you.",
 242|     '',
 243|     'If the user explicitly asks you to remember something, save it immediately as whichever type fits best. If they ask you to forget something, find and remove the relevant entry.',
 244|     '',
 245|     ...TYPES_SECTION_INDIVIDUAL,
 246|     ...WHAT_NOT_TO_SAVE_SECTION,
 247|     '',
 248|     ...howToSave,
 249|     '',
 250|     ...WHEN_TO_ACCESS_SECTION,
 251|     '',
 252|     ...TRUSTING_RECALL_SECTION,
 253|     '',
 254|     '## Memory and other forms of persistence',
 255|     'Memory is one of several persistence mechanisms available to you as you assist the user in a given conversation. The distinction is often that memory can be recalled in future conversations and should not be used for persisting information that is only useful within the scope of the current conversation.',
 256|     '- When to use or update a plan instead of memory: If you are about to start a non-trivial implementation task and would like to reach alignment with the user on your approach you should use a Plan rather than saving this information to memory. Similarly, if you already have a plan within the conversation and you have changed your approach persist that change by updating the plan rather than saving a memory.',
 257|     '- When to use or update tasks instead of memory: When you need to break your work in current conversation into discrete steps or keep track of your progress use tasks instead of saving to memory. Tasks are great for persisting information about the work that needs to be done in the current conversation, but memory should be reserved for information that will be useful in future conversations.',
 258|     '',
 259|     ...(extraGuidelines ?? []),
 260|     '',
 261|   ]
 262| 
 263|   lines.push(...buildSearchingPastContextSection(memoryDir))
 264| 
 265|   return lines
 266| }
 267| 
 268| /**
 269|  * Build the typed-memory prompt with MEMORY.md content included.
 270|  * Used by agent memory (which has no getClaudeMds() equivalent).
 271|  */
 272| export function buildMemoryPrompt(params: {
 273|   displayName: string
 274|   memoryDir: string
 275|   extraGuidelines?: string[]
 276| }): string {
 277|   const { displayName, memoryDir, extraGuidelines } = params
 278|   const fs = getFsImplementation()
 279|   const entrypoint = memoryDir + ENTRYPOINT_NAME
 280| 
 281|   // Directory creation is the caller's responsibility (loadMemoryPrompt /
 282|   // loadAgentMemoryPrompt). Builders only read, they don't mkdir.
 283| 
 284|   // Read existing memory entrypoint (sync: prompt building is synchronous)
 285|   let entrypointContent = ''
 286|   try {
 287|     // eslint-disable-next-line custom-rules/no-sync-fs
 288|     entrypointContent = fs.readFileSync(entrypoint, { encoding: 'utf-8' })
 289|   } catch {
 290|     // No memory file yet
 291|   }
 292| 
 293|   const lines = buildMemoryLines(displayName, memoryDir, extraGuidelines)
 294| 
 295|   if (entrypointContent.trim()) {
 296|     const t = truncateEntrypointContent(entrypointContent)
 297|     const memoryType = displayName === AUTO_MEM_DISPLAY_NAME ? 'auto' : 'agent'
 298|     logMemoryDirCounts(memoryDir, {
 299|       content_length: t.byteCount,
 300|       line_count: t.lineCount,
 301|       was_truncated: t.wasLineTruncated,
 302|       was_byte_truncated: t.wasByteTruncated,
 303|       memory_type:
 304|         memoryType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
 305|     })
 306|     lines.push(`## ${ENTRYPOINT_NAME}`, '', t.content)
 307|   } else {
 308|     lines.push(
 309|       `## ${ENTRYPOINT_NAME}`,
 310|       '',
 311|       `Your ${ENTRYPOINT_NAME} is currently empty. When you save new memories, they will appear here.`,
 312|     )
 313|   }
 314| 
 315|   return lines.join('\n')
 316| }

源码引用: src/memdir/memdir.ts · 第 419–496 行(共 508 行)

 419| export async function loadMemoryPrompt(): Promise<string | null> {
 420|   const autoEnabled = isAutoMemoryEnabled()
 421| 
 422|   const skipIndex = getFeatureValue_CACHED_MAY_BE_STALE(
 423|     'tengu_moth_copse',
 424|     false,
 425|   )
 426| 
 427|   // KAIROS daily-log mode takes precedence over TEAMMEM: the append-only
 428|   // log paradigm does not compose with team sync (which expects a shared
 429|   // MEMORY.md that both sides read + write). Gating on `autoEnabled` here
 430|   // means the !autoEnabled case falls through to the tengu_memdir_disabled
 431|   // telemetry block below, matching the non-KAIROS path.
 432|   if (feature('KAIROS') && autoEnabled && getKairosActive()) {
 433|     logMemoryDirCounts(getAutoMemPath(), {
 434|       memory_type:
 435|         'auto' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
 436|     })
 437|     return buildAssistantDailyLogPrompt(skipIndex)
 438|   }
 439| 
 440|   // Cowork injects memory-policy text via env var; thread into all builders.
 441|   const coworkExtraGuidelines =
 442|     process.env.CLAUDE_COWORK_MEMORY_EXTRA_GUIDELINES
 443|   const extraGuidelines =
 444|     coworkExtraGuidelines && coworkExtraGuidelines.trim().length > 0
 445|       ? [coworkExtraGuidelines]
 446|       : undefined
 447| 
 448|   if (feature('TEAMMEM')) {
 449|     if (teamMemPaths!.isTeamMemoryEnabled()) {
 450|       const autoDir = getAutoMemPath()
 451|       const teamDir = teamMemPaths!.getTeamMemPath()
 452|       // Harness guarantees these directories exist so the model can write
 453|       // without checking. The prompt text reflects this ("already exists").
 454|       // Only creating teamDir is sufficient: getTeamMemPath() is defined as
 455|       // join(getAutoMemPath(), 'team'), so recursive mkdir of the team dir
 456|       // creates the auto dir as a side effect. If the team dir ever moves
 457|       // out from under the auto dir, add a second ensureMemoryDirExists call
 458|       // for autoDir here.
 459|       await ensureMemoryDirExists(teamDir)
 460|       logMemoryDirCounts(autoDir, {
 461|         memory_type:
 462|           'auto' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
 463|       })
 464|       logMemoryDirCounts(teamDir, {
 465|         memory_type:
 466|           'team' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
 467|       })
 468|       return teamMemPrompts!.buildCombinedMemoryPrompt(
 469|         extraGuidelines,
 470|         skipIndex,
 471|       )
 472|     }
 473|   }
 474| 
 475|   if (autoEnabled) {
 476|     const autoDir = getAutoMemPath()
 477|     // Harness guarantees the directory exists so the model can write without
 478|     // checking. The prompt text reflects this ("already exists").
 479|     await ensureMemoryDirExists(autoDir)
 480|     logMemoryDirCounts(autoDir, {
 481|       memory_type:
 482|         'auto' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
 483|     })
 484|     return buildMemoryLines(
 485|       'auto memory',
 486|       autoDir,
 487|       extraGuidelines,
 488|       skipIndex,
 489|     ).join('\n')
 490|   }
 491| 
 492|   logEvent('tengu_memdir_disabled', {
 493|     disabled_by_env_var: isEnvTruthy(
 494|       process.env.CLAUDE_CODE_DISABLE_AUTO_MEMORY,
 495|     ),
 496|     disabled_by_setting:

为何采用“索引+主题文件”而非单大文件

memdir 选择 MEMORY.md 只做索引、topic 文件承载正文,本质上是把写入成本与检索成本分开。写入时,模型只需追加或更新单个 topic.md 与一行索引,不会频繁重写整份长文档;读取时,loadMemoryPrompt 只注入索引和指南,避免把全部历史记忆塞进系统提示。这样既降低 token 开销,也减少并发写入时的冲突面。

这种设计和 memoryScan/findRelevantMemories 的分工一致:核心 prompt 始终轻量,真正需要深入内容时再按 topic 文件扫描。若未来要扩展团队记忆或分类标签,索引层可先演进格式而不破坏已有 topic 正文。

源码引用: src/memdir/memdir.ts · 第 199–316 行(共 508 行)

 199| export function buildMemoryLines(
 200|   displayName: string,
 201|   memoryDir: string,
 202|   extraGuidelines?: string[],
 203|   skipIndex = false,
 204| ): string[] {
 205|   const howToSave = skipIndex
 206|     ? [
 207|         '## How to save memories',
 208|         '',
 209|         'Write each memory to its own file (e.g., `user_role.md`, `feedback_testing.md`) using this frontmatter format:',
 210|         '',
 211|         ...MEMORY_FRONTMATTER_EXAMPLE,
 212|         '',
 213|         '- Keep the name, description, and type fields in memory files up-to-date with the content',
 214|         '- Organize memory semantically by topic, not chronologically',
 215|         '- Update or remove memories that turn out to be wrong or outdated',
 216|         '- Do not write duplicate memories. First check if there is an existing memory you can update before writing a new one.',
 217|       ]
 218|     : [
 219|         '## How to save memories',
 220|         '',
 221|         'Saving a memory is a two-step process:',
 222|         '',
 223|         '**Step 1** — write the memory to its own file (e.g., `user_role.md`, `feedback_testing.md`) using this frontmatter format:',
 224|         '',
 225|         ...MEMORY_FRONTMATTER_EXAMPLE,
 226|         '',
 227|         `**Step 2** — add a pointer to that file in \`${ENTRYPOINT_NAME}\`. \`${ENTRYPOINT_NAME}\` is an index, not a memory — each entry should be one line, under ~150 characters: \`- [Title](file.md) — one-line hook\`. It has no frontmatter. Never write memory content directly into \`${ENTRYPOINT_NAME}\`.`,
 228|         '',
 229|         `- \`${ENTRYPOINT_NAME}\` is always loaded into your conversation context — lines after ${MAX_ENTRYPOINT_LINES} will be truncated, so keep the index concise`,
 230|         '- Keep the name, description, and type fields in memory files up-to-date with the content',
 231|         '- Organize memory semantically by topic, not chronologically',
 232|         '- Update or remove memories that turn out to be wrong or outdated',
 233|         '- Do not write duplicate memories. First check if there is an existing memory you can update before writing a new one.',
 234|       ]
 235| 
 236|   const lines: string[] = [
 237|     `# ${displayName}`,
 238|     '',
 239|     `You have a persistent, file-based memory system at \`${memoryDir}\`. ${DIR_EXISTS_GUIDANCE}`,
 240|     '',
 241|     "You should build up this memory system over time so that future conversations can have a complete picture of who the user is, how they'd like to collaborate with you, what behaviors to avoid or repeat, and the context behind the work the user gives you.",
 242|     '',
 243|     'If the user explicitly asks you to remember something, save it immediately as whichever type fits best. If they ask you to forget something, find and remove the relevant entry.',
 244|     '',
 245|     ...TYPES_SECTION_INDIVIDUAL,
 246|     ...WHAT_NOT_TO_SAVE_SECTION,
 247|     '',
 248|     ...howToSave,
 249|     '',
 250|     ...WHEN_TO_ACCESS_SECTION,
 251|     '',
 252|     ...TRUSTING_RECALL_SECTION,
 253|     '',
 254|     '## Memory and other forms of persistence',
 255|     'Memory is one of several persistence mechanisms available to you as you assist the user in a given conversation. The distinction is often that memory can be recalled in future conversations and should not be used for persisting information that is only useful within the scope of the current conversation.',
 256|     '- When to use or update a plan instead of memory: If you are about to start a non-trivial implementation task and would like to reach alignment with the user on your approach you should use a Plan rather than saving this information to memory. Similarly, if you already have a plan within the conversation and you have changed your approach persist that change by updating the plan rather than saving a memory.',
 257|     '- When to use or update tasks instead of memory: When you need to break your work in current conversation into discrete steps or keep track of your progress use tasks instead of saving to memory. Tasks are great for persisting information about the work that needs to be done in the current conversation, but memory should be reserved for information that will be useful in future conversations.',
 258|     '',
 259|     ...(extraGuidelines ?? []),
 260|     '',
 261|   ]
 262| 
 263|   lines.push(...buildSearchingPastContextSection(memoryDir))
 264| 
 265|   return lines
 266| }
 267| 
 268| /**
 269|  * Build the typed-memory prompt with MEMORY.md content included.
 270|  * Used by agent memory (which has no getClaudeMds() equivalent).
 271|  */
 272| export function buildMemoryPrompt(params: {
 273|   displayName: string
 274|   memoryDir: string
 275|   extraGuidelines?: string[]
 276| }): string {
 277|   const { displayName, memoryDir, extraGuidelines } = params
 278|   const fs = getFsImplementation()
 279|   const entrypoint = memoryDir + ENTRYPOINT_NAME
 280| 
 281|   // Directory creation is the caller's responsibility (loadMemoryPrompt /
 282|   // loadAgentMemoryPrompt). Builders only read, they don't mkdir.
 283| 
 284|   // Read existing memory entrypoint (sync: prompt building is synchronous)
 285|   let entrypointContent = ''
 286|   try {
 287|     // eslint-disable-next-line custom-rules/no-sync-fs
 288|     entrypointContent = fs.readFileSync(entrypoint, { encoding: 'utf-8' })
 289|   } catch {
 290|     // No memory file yet
 291|   }
 292| 
 293|   const lines = buildMemoryLines(displayName, memoryDir, extraGuidelines)
 294| 
 295|   if (entrypointContent.trim()) {
 296|     const t = truncateEntrypointContent(entrypointContent)
 297|     const memoryType = displayName === AUTO_MEM_DISPLAY_NAME ? 'auto' : 'agent'
 298|     logMemoryDirCounts(memoryDir, {
 299|       content_length: t.byteCount,
 300|       line_count: t.lineCount,
 301|       was_truncated: t.wasLineTruncated,
 302|       was_byte_truncated: t.wasByteTruncated,
 303|       memory_type:
 304|         memoryType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
 305|     })
 306|     lines.push(`## ${ENTRYPOINT_NAME}`, '', t.content)
 307|   } else {
 308|     lines.push(
 309|       `## ${ENTRYPOINT_NAME}`,
 310|       '',
 311|       `Your ${ENTRYPOINT_NAME} is currently empty. When you save new memories, they will appear here.`,
 312|     )
 313|   }
 314| 
 315|   return lines.join('\n')
 316| }

源码引用: src/memdir/memoryScan.ts · 第 21–77 行(共 95 行)

  21| const MAX_MEMORY_FILES = 200
  22| const FRONTMATTER_MAX_LINES = 30
  23| 
  24| /**
  25|  * Scan a memory directory for .md files, read their frontmatter, and return
  26|  * a header list sorted newest-first (capped at MAX_MEMORY_FILES). Shared by
  27|  * findRelevantMemories (query-time recall) and extractMemories (pre-injects
  28|  * the listing so the extraction agent doesn't spend a turn on `ls`).
  29|  *
  30|  * Single-pass: readFileInRange stats internally and returns mtimeMs, so we
  31|  * read-then-sort rather than stat-sort-read. For the common case (N ≤ 200)
  32|  * this halves syscalls vs a separate stat round; for large N we read a few
  33|  * extra small files but still avoid the double-stat on the surviving 200.
  34|  */
  35| export async function scanMemoryFiles(
  36|   memoryDir: string,
  37|   signal: AbortSignal,
  38| ): Promise<MemoryHeader[]> {
  39|   try {
  40|     const entries = await readdir(memoryDir, { recursive: true })
  41|     const mdFiles = entries.filter(
  42|       f => f.endsWith('.md') && basename(f) !== 'MEMORY.md',
  43|     )
  44| 
  45|     const headerResults = await Promise.allSettled(
  46|       mdFiles.map(async (relativePath): Promise<MemoryHeader> => {
  47|         const filePath = join(memoryDir, relativePath)
  48|         const { content, mtimeMs } = await readFileInRange(
  49|           filePath,
  50|           0,
  51|           FRONTMATTER_MAX_LINES,
  52|           undefined,
  53|           signal,
  54|         )
  55|         const { frontmatter } = parseFrontmatter(content, filePath)
  56|         return {
  57|           filename: relativePath,
  58|           filePath,
  59|           mtimeMs,
  60|           description: frontmatter.description || null,
  61|           type: parseMemoryType(frontmatter.type),
  62|         }
  63|       }),
  64|     )
  65| 
  66|     return headerResults
  67|       .filter(
  68|         (r): r is PromiseFulfilledResult<MemoryHeader> =>
  69|           r.status === 'fulfilled',
  70|       )
  71|       .map(r => r.value)
  72|       .sort((a, b) => b.mtimeMs - a.mtimeMs)
  73|       .slice(0, MAX_MEMORY_FILES)
  74|   } catch {
  75|     return []
  76|   }
  77| }

源码引用: src/memdir/findRelevantMemories.ts · 第 26–75 行(共 142 行)

  26| /**
  27|  * Find memory files relevant to a query by scanning memory file headers
  28|  * and asking Sonnet to select the most relevant ones.
  29|  *
  30|  * Returns absolute file paths + mtime of the most relevant memories
  31|  * (up to 5). Excludes MEMORY.md (already loaded in system prompt).
  32|  * mtime is threaded through so callers can surface freshness to the
  33|  * main model without a second stat.
  34|  *
  35|  * `alreadySurfaced` filters paths shown in prior turns before the
  36|  * Sonnet call, so the selector spends its 5-slot budget on fresh
  37|  * candidates instead of re-picking files the caller will discard.
  38|  */
  39| export async function findRelevantMemories(
  40|   query: string,
  41|   memoryDir: string,
  42|   signal: AbortSignal,
  43|   recentTools: readonly string[] = [],
  44|   alreadySurfaced: ReadonlySet<string> = new Set(),
  45| ): Promise<RelevantMemory[]> {
  46|   const memories = (await scanMemoryFiles(memoryDir, signal)).filter(
  47|     m => !alreadySurfaced.has(m.filePath),
  48|   )
  49|   if (memories.length === 0) {
  50|     return []
  51|   }
  52| 
  53|   const selectedFilenames = await selectRelevantMemories(
  54|     query,
  55|     memories,
  56|     signal,
  57|     recentTools,
  58|   )
  59|   const byFilename = new Map(memories.map(m => [m.filename, m]))
  60|   const selected = selectedFilenames
  61|     .map(filename => byFilename.get(filename))
  62|     .filter((m): m is MemoryHeader => m !== undefined)
  63| 
  64|   // Fires even on empty selection: selection-rate needs the denominator,
  65|   // and -1 ages distinguish "ran, picked nothing" from "never ran".
  66|   if (feature('MEMORY_SHAPE_TELEMETRY')) {
  67|     /* eslint-disable @typescript-eslint/no-require-imports */
  68|     const { logMemoryRecallShape } =
  69|       require('./memoryShapeTelemetry.js') as typeof import('./memoryShapeTelemetry.js')
  70|     /* eslint-enable @typescript-eslint/no-require-imports */
  71|     logMemoryRecallShape(memories, selected)
  72|   }
  73| 
  74|   return selected.map(m => ({ path: m.filePath, mtimeMs: m.mtimeMs }))
  75| }

从系统提示拼装看 memdir-core 的稳定契约

memdir-core 还能从“提示拼装契约”来理解:它不是把任意文本塞进 system prompt,而是按固定片段顺序构建。先给出保存位置与目录存在提示,再给出保存规则与类型分类,再给 frontmatter 示例和不要保存的内容,最后才附 MEMORY.md 截断内容。这个顺序保证模型先拿到操作规范,再看到历史索引,减少把索引误当规则或把规则误当正文的风险。

在工程上,这个契约让未来扩展更可控。比如新增 memory type、团队目录提示或 recall 策略时,只需扩展 memoryTypes.ts 文案或 buildMemoryLines 的片段,而不必重写 loadMemoryPrompt 主流程。truncateEntrypointContent 的双阈值又保证即便索引增长,提示仍在可控范围内。也就是说,memdir-core 的价值不仅是“能加载记忆”,更是把加载行为做成稳定、可演进、可诊断的提示模板。

调试相关问题时,也可沿着这份契约定位:若模型不知道目录路径,先查 paths.ts 与 buildMemoryLines 首段;若模型保存格式混乱,查 memoryTypes 与 frontmatter 示例段;若提示过长或缺失历史索引,查 truncateEntrypointContent 与 buildMemoryPrompt 读取路径。把问题映射到片段级别,比直接在大段系统提示里盲查更高效。

源码引用: src/memdir/memdir.ts · 第 34–103 行(共 508 行)

  34| export const ENTRYPOINT_NAME = 'MEMORY.md'
  35| export const MAX_ENTRYPOINT_LINES = 200
  36| // ~125 chars/line at 200 lines. At p97 today; catches long-line indexes that
  37| // slip past the line cap (p100 observed: 197KB under 200 lines).
  38| export const MAX_ENTRYPOINT_BYTES = 25_000
  39| const AUTO_MEM_DISPLAY_NAME = 'auto memory'
  40| 
  41| export type EntrypointTruncation = {
  42|   content: string
  43|   lineCount: number
  44|   byteCount: number
  45|   wasLineTruncated: boolean
  46|   wasByteTruncated: boolean
  47| }
  48| 
  49| /**
  50|  * Truncate MEMORY.md content to the line AND byte caps, appending a warning
  51|  * that names which cap fired. Line-truncates first (natural boundary), then
  52|  * byte-truncates at the last newline before the cap so we don't cut mid-line.
  53|  *
  54|  * Shared by buildMemoryPrompt and claudemd getMemoryFiles (previously
  55|  * duplicated the line-only logic).
  56|  */
  57| export function truncateEntrypointContent(raw: string): EntrypointTruncation {
  58|   const trimmed = raw.trim()
  59|   const contentLines = trimmed.split('\n')
  60|   const lineCount = contentLines.length
  61|   const byteCount = trimmed.length
  62| 
  63|   const wasLineTruncated = lineCount > MAX_ENTRYPOINT_LINES
  64|   // Check original byte count — long lines are the failure mode the byte cap
  65|   // targets, so post-line-truncation size would understate the warning.
  66|   const wasByteTruncated = byteCount > MAX_ENTRYPOINT_BYTES
  67| 
  68|   if (!wasLineTruncated && !wasByteTruncated) {
  69|     return {
  70|       content: trimmed,
  71|       lineCount,
  72|       byteCount,
  73|       wasLineTruncated,
  74|       wasByteTruncated,
  75|     }
  76|   }
  77| 
  78|   let truncated = wasLineTruncated
  79|     ? contentLines.slice(0, MAX_ENTRYPOINT_LINES).join('\n')
  80|     : trimmed
  81| 
  82|   if (truncated.length > MAX_ENTRYPOINT_BYTES) {
  83|     const cutAt = truncated.lastIndexOf('\n', MAX_ENTRYPOINT_BYTES)
  84|     truncated = truncated.slice(0, cutAt > 0 ? cutAt : MAX_ENTRYPOINT_BYTES)
  85|   }
  86| 
  87|   const reason =
  88|     wasByteTruncated && !wasLineTruncated
  89|       ? `${formatFileSize(byteCount)} (limit: ${formatFileSize(MAX_ENTRYPOINT_BYTES)}) — index entries are too long`
  90|       : wasLineTruncated && !wasByteTruncated
  91|         ? `${lineCount} lines (limit: ${MAX_ENTRYPOINT_LINES})`
  92|         : `${lineCount} lines and ${formatFileSize(byteCount)}`
  93| 
  94|   return {
  95|     content:
  96|       truncated +
  97|       `\n\n> WARNING: ${ENTRYPOINT_NAME} is ${reason}. Only part of it was loaded. Keep index entries to one line under ~200 chars; move detail into topic files.`,
  98|     lineCount,
  99|     byteCount,
 100|     wasLineTruncated,
 101|     wasByteTruncated,
 102|   }
 103| }

源码引用: src/memdir/memdir.ts · 第 199–266 行(共 508 行)

 199| export function buildMemoryLines(
 200|   displayName: string,
 201|   memoryDir: string,
 202|   extraGuidelines?: string[],
 203|   skipIndex = false,
 204| ): string[] {
 205|   const howToSave = skipIndex
 206|     ? [
 207|         '## How to save memories',
 208|         '',
 209|         'Write each memory to its own file (e.g., `user_role.md`, `feedback_testing.md`) using this frontmatter format:',
 210|         '',
 211|         ...MEMORY_FRONTMATTER_EXAMPLE,
 212|         '',
 213|         '- Keep the name, description, and type fields in memory files up-to-date with the content',
 214|         '- Organize memory semantically by topic, not chronologically',
 215|         '- Update or remove memories that turn out to be wrong or outdated',
 216|         '- Do not write duplicate memories. First check if there is an existing memory you can update before writing a new one.',
 217|       ]
 218|     : [
 219|         '## How to save memories',
 220|         '',
 221|         'Saving a memory is a two-step process:',
 222|         '',
 223|         '**Step 1** — write the memory to its own file (e.g., `user_role.md`, `feedback_testing.md`) using this frontmatter format:',
 224|         '',
 225|         ...MEMORY_FRONTMATTER_EXAMPLE,
 226|         '',
 227|         `**Step 2** — add a pointer to that file in \`${ENTRYPOINT_NAME}\`. \`${ENTRYPOINT_NAME}\` is an index, not a memory — each entry should be one line, under ~150 characters: \`- [Title](file.md) — one-line hook\`. It has no frontmatter. Never write memory content directly into \`${ENTRYPOINT_NAME}\`.`,
 228|         '',
 229|         `- \`${ENTRYPOINT_NAME}\` is always loaded into your conversation context — lines after ${MAX_ENTRYPOINT_LINES} will be truncated, so keep the index concise`,
 230|         '- Keep the name, description, and type fields in memory files up-to-date with the content',
 231|         '- Organize memory semantically by topic, not chronologically',
 232|         '- Update or remove memories that turn out to be wrong or outdated',
 233|         '- Do not write duplicate memories. First check if there is an existing memory you can update before writing a new one.',
 234|       ]
 235| 
 236|   const lines: string[] = [
 237|     `# ${displayName}`,
 238|     '',
 239|     `You have a persistent, file-based memory system at \`${memoryDir}\`. ${DIR_EXISTS_GUIDANCE}`,
 240|     '',
 241|     "You should build up this memory system over time so that future conversations can have a complete picture of who the user is, how they'd like to collaborate with you, what behaviors to avoid or repeat, and the context behind the work the user gives you.",
 242|     '',
 243|     'If the user explicitly asks you to remember something, save it immediately as whichever type fits best. If they ask you to forget something, find and remove the relevant entry.',
 244|     '',
 245|     ...TYPES_SECTION_INDIVIDUAL,
 246|     ...WHAT_NOT_TO_SAVE_SECTION,
 247|     '',
 248|     ...howToSave,
 249|     '',
 250|     ...WHEN_TO_ACCESS_SECTION,
 251|     '',
 252|     ...TRUSTING_RECALL_SECTION,
 253|     '',
 254|     '## Memory and other forms of persistence',
 255|     'Memory is one of several persistence mechanisms available to you as you assist the user in a given conversation. The distinction is often that memory can be recalled in future conversations and should not be used for persisting information that is only useful within the scope of the current conversation.',
 256|     '- When to use or update a plan instead of memory: If you are about to start a non-trivial implementation task and would like to reach alignment with the user on your approach you should use a Plan rather than saving this information to memory. Similarly, if you already have a plan within the conversation and you have changed your approach persist that change by updating the plan rather than saving a memory.',
 257|     '- When to use or update tasks instead of memory: When you need to break your work in current conversation into discrete steps or keep track of your progress use tasks instead of saving to memory. Tasks are great for persisting information about the work that needs to be done in the current conversation, but memory should be reserved for information that will be useful in future conversations.',
 258|     '',
 259|     ...(extraGuidelines ?? []),
 260|     '',
 261|   ]
 262| 
 263|   lines.push(...buildSearchingPastContextSection(memoryDir))
 264| 
 265|   return lines
 266| }

源码引用: src/memdir/memoryTypes.ts · 第 37–118 行(共 272 行)

  37| export const TYPES_SECTION_COMBINED: readonly string[] = [
  38|   '## Types of memory',
  39|   '',
  40|   'There are several discrete types of memory that you can store in your memory system. Each type below declares a <scope> of `private`, `team`, or guidance for choosing between the two.',
  41|   '',
  42|   '<types>',
  43|   '<type>',
  44|   '    <name>user</name>',
  45|   '    <scope>always private</scope>',
  46|   "    <description>Contain information about the user's role, goals, responsibilities, and knowledge. Great user memories help you tailor your future behavior to the user's preferences and perspective. Your goal in reading and writing these memories is to build up an understanding of who the user is and how you can be most helpful to them specifically. For example, you should collaborate with a senior software engineer differently than a student who is coding for the very first time. Keep in mind, that the aim here is to be helpful to the user. Avoid writing memories about the user that could be viewed as a negative judgement or that are not relevant to the work you're trying to accomplish together.</description>",
  47|   "    <when_to_save>When you learn any details about the user's role, preferences, responsibilities, or knowledge</when_to_save>",
  48|   "    <how_to_use>When your work should be informed by the user's profile or perspective. For example, if the user is asking you to explain a part of the code, you should answer that question in a way that is tailored to the specific details that they will find most valuable or that helps them build their mental model in relation to domain knowledge they already have.</how_to_use>",
  49|   '    <examples>',
  50|   "    user: I'm a data scientist investigating what logging we have in place",
  51|   '    assistant: [saves private user memory: user is a data scientist, currently focused on observability/logging]',
  52|   '',
  53|   "    user: I've been writing Go for ten years but this is my first time touching the React side of this repo",
  54|   "    assistant: [saves private user memory: deep Go expertise, new to React and this project's frontend — frame frontend explanations in terms of backend analogues]",
  55|   '    </examples>',
  56|   '</type>',
  57|   '<type>',
  58|   '    <name>feedback</name>',
  59|   '    <scope>default to private. Save as team only when the guidance is clearly a project-wide convention that every contributor should follow (e.g., a testing policy, a build invariant), not a personal style preference.</scope>',
  60|   "    <description>Guidance the user has given you about how to approach work — both what to avoid and what to keep doing. These are a very important type of memory to read and write as they allow you to remain coherent and responsive to the way you should approach work in the project. Record from failure AND success: if you only save corrections, you will avoid past mistakes but drift away from approaches the user has already validated, and may grow overly cautious. Before saving a private feedback memory, check that it doesn't contradict a team feedback memory — if it does, either don't save it or note the override explicitly.</description>",
  61|   '    <when_to_save>Any time the user corrects your approach ("no not that", "don\'t", "stop doing X") OR confirms a non-obvious approach worked ("yes exactly", "perfect, keep doing that", accepting an unusual choice without pushback). Corrections are easy to notice; confirmations are quieter — watch for them. In both cases, save what is applicable to future conversations, especially if surprising or not obvious from the code. Include *why* so you can judge edge cases later.</when_to_save>',
  62|   '    <how_to_use>Let these memories guide your behavior so that the user and other users in the project do not need to offer the same guidance twice.</how_to_use>',
  63|   '    <body_structure>Lead with the rule itself, then a **Why:** line (the reason the user gave — often a past incident or strong preference) and a **How to apply:** line (when/where this guidance kicks in). Knowing *why* lets you judge edge cases instead of blindly following the rule.</body_structure>',
  64|   '    <examples>',
  65|   "    user: don't mock the database in these tests — we got burned last quarter when mocked tests passed but the prod migration failed",
  66|   '    assistant: [saves team feedback memory: integration tests must hit a real database, not mocks. Reason: prior incident where mock/prod divergence masked a broken migration. Team scope: this is a project testing policy, not a personal preference]',
  67|   '',
  68|   '    user: stop summarizing what you just did at the end of every response, I can read the diff',
  69|   "    assistant: [saves private feedback memory: this user wants terse responses with no trailing summaries. Private because it's a communication preference, not a project convention]",
  70|   '',
  71|   "    user: yeah the single bundled PR was the right call here, splitting this one would've just been churn",
  72|   '    assistant: [saves private feedback memory: for refactors in this area, user prefers one bundled PR over many small ones. Confirmed after I chose this approach — a validated judgment call, not a correction]',
  73|   '    </examples>',
  74|   '</type>',
  75|   '<type>',
  76|   '    <name>project</name>',
  77|   '    <scope>private or team, but strongly bias toward team</scope>',
  78|   '    <description>Information that you learn about ongoing work, goals, initiatives, bugs, or incidents within the project that is not otherwise derivable from the code or git history. Project memories help you understand the broader context and motivation behind the work users are working on within this working directory.</description>',
  79|   '    <when_to_save>When you learn who is doing what, why, or by when. These states change relatively quickly so try to keep your understanding of this up to date. Always convert relative dates in user messages to absolute dates when saving (e.g., "Thursday" → "2026-03-05"), so the memory remains interpretable after time passes.</when_to_save>',
  80|   "    <how_to_use>Use these memories to more fully understand the details and nuance behind the user's request, anticipate coordination issues across users, make better informed suggestions.</how_to_use>",
  81|   '    <body_structure>Lead with the fact or decision, then a **Why:** line (the motivation — often a constraint, deadline, or stakeholder ask) and a **How to apply:** line (how this should shape your suggestions). Project memories decay fast, so the why helps future-you judge whether the memory is still load-bearing.</body_structure>',
  82|   '    <examples>',
  83|   "    user: we're freezing all non-critical merges after Thursday — mobile team is cutting a release branch",
  84|   '    assistant: [saves team project memory: merge freeze begins 2026-03-05 for mobile release cut. Flag any non-critical PR work scheduled after that date]',
  85|   '',
  86|   "    user: the reason we're ripping out the old auth middleware is that legal flagged it for storing session tokens in a way that doesn't meet the new compliance requirements",
  87|   '    assistant: [saves team project memory: auth middleware rewrite is driven by legal/compliance requirements around session token storage, not tech-debt cleanup — scope decisions should favor compliance over ergonomics]',
  88|   '    </examples>',
  89|   '</type>',
  90|   '<type>',
  91|   '    <name>reference</name>',
  92|   '    <scope>usually team</scope>',
  93|   '    <description>Stores pointers to where information can be found in external systems. These memories allow you to remember where to look to find up-to-date information outside of the project directory.</description>',
  94|   '    <when_to_save>When you learn about resources in external systems and their purpose. For example, that bugs are tracked in a specific project in Linear or that feedback can be found in a specific Slack channel.</when_to_save>',
  95|   '    <how_to_use>When the user references an external system or information that may be in an external system.</how_to_use>',
  96|   '    <examples>',
  97|   '    user: check the Linear project "INGEST" if you want context on these tickets, that\'s where we track all pipeline bugs',
  98|   '    assistant: [saves team reference memory: pipeline bugs are tracked in Linear project "INGEST"]',
  99|   '',
 100|   "    user: the Grafana board at grafana.internal/d/api-latency is what oncall watches — if you're touching request handling, that's the thing that'll page someone",
 101|   '    assistant: [saves team reference memory: grafana.internal/d/api-latency is the oncall latency dashboard — check it when editing request-path code]',
 102|   '    </examples>',
 103|   '</type>',
 104|   '</types>',
 105|   '',
 106| ]
 107| 
 108| /**
 109|  * `## Types of memory` section for INDIVIDUAL-ONLY mode (single directory).
 110|  * No <scope> tags. Examples use plain `[saves X memory: …]`. Prose that
 111|  * only makes sense with a private/team split is reworded.
 112|  */
 113| export const TYPES_SECTION_INDIVIDUAL: readonly string[] = [
 114|   '## Types of memory',
 115|   '',
 116|   'There are several discrete types of memory that you can store in your memory system:',
 117|   '',
 118|   '<types>',

本章小结与延伸

memdir-core = 路径 + taxonomy + prompt 注入。下一章 memory-extraction 读回合末提取管线。 继续学习:

  • memory-extraction
  • memdir 总览
Prev
模块: memdir
Next
memory-extraction · extractMemories 与 SessionMemory