inject-memory
/inject-memory
Inject lightweight AI memory system into any project. Creates memory/ directory with structured templates, appends memory usage rules to CLAUDE.md, configures PostToolUse hook for auto-syncing index timestamps, and performs atomic git commit. Trigger on: /inject-memory, 'inject memory', 'init memory', 'set up AI memory', '给这个项目加记忆系统', '注入记忆系统'.
- Author
- darven-cs ↗
- Repository
- p8-engine ↗
- Licence
- MIT
- Last repository change
- 14.07.2026
Assessment and security
- Files in this package
- 11
- Scripts
- 1
- HTML comments
- 1
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 14.07.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
- 1 scripts in this package. Review them before installing, as Claude may execute them.
- SKILL.md zawiera komentarze HTML (1). Nie widać ich w podglądzie Markdown, a model je czyta.
Scripts: hooks/sync-memory-index.mjs
What it does and when to use it
Inject lightweight AI memory system into any project. Creates memory/ directory with structured templates, appends memory usage rules to CLAUDE.md, configures PostToolUse hook for auto-syncing index timestamps, and performs atomic git commit. Trigger on: /inject-memory, 'inject memory', 'init memory', 'set up AI memory', '给这个项目加记忆系统', '注入记忆系统'.
Triggers from the description
Original author description
Inject lightweight AI memory system into any project. Creates memory/ directory with structured templates, appends memory usage rules to CLAUDE.md, configures PostToolUse hook for auto-syncing index timestamps, and performs atomic git commit. Trigger on: /inject-memory, 'inject memory', 'init memory', 'set up AI memory', '给这个项目加记忆系统', '注入记忆系统'.
How it works
- 一、skill 目录定位
- 二、预检查
- 三、执行注入
- 四、配置 hook
- 五、原子 git commit
- 六、汇报结果
- 七、错误处理与回滚
- 八、幂等性保证
- 九、关键约束
SKILL.md preview
---
name: inject-memory
description: "Inject lightweight AI memory system into any project. Creates memory/ directory with structured templates, appends memory usage rules to CLAUDE.md, configures PostToolUse hook for auto-syncing index timestamps, and performs atomic git commit. Trigger on: /inject-memory, 'inject memory', 'init memory', 'set up AI memory', '给这个项目加记忆系统', '注入记忆系统'."
version: 1.0.0
license: MIT
---
# Inject Memory System
在**当前项目**注入一套轻量 AI 记忆系统(memory/ 目录 + CLAUDE.md 规范 + PostToolUse 自动同步 hook),并做原子 git commit。
目标是让 Claude 在多次会话间保持"项目记忆",通过索引 + 结构化模板 + 自动化同步三件套实现。
---
## 一、skill 目录定位
skill 执行时 cwd 是**目标项目根**,模板在 skill 自身目录。用下面的顺序找 `$SKILL_DIR`:
1. 先尝试项目级:`./.claude/skills/inject-memory`
2. 退回用户级:`$HOME/.claude/skills/inject-memory`
```bash
if [ -d "./.claude/skills/inject-memory/templates" ]; then
SKILL_DIR="./.claude/skills/inject-memory"
elif [ -d "$HOME/.claude/skills/inject-memory/templates" ]; then
SKILL_DIR="$HOME/.claude/skills/inject-memory"
else
echo "inject-memory skill not found"; exit 1
fi
```
在所有后续 Bash 命令中用这个变量引用模板。
---
## 二、预检查
按顺序执行,任何一步失败用 `AskUserQuestion` 向用户确认如何处理。
### 2.1 git 仓库
```bash
git rev-parse --git-dir 2>/dev/null
```
- 返回非 0:**不是 git 仓库**。用 AskUserQuestion 问是否 `git init`。拒绝则放弃注入。
- 返回 0:继续。
### 2.2 工作区干净
```bash
git status --porcelain
```
有未提交变更时,用 AskUserQuestion 给三个选项:
- 先让用户提交 / stash 后再来(推荐)
- 允许在现有变更上注入(注入失败时回滚只能影响注入文件)
- 取消
### 2.3 memory/ 是否已存在
```bash
[ -e memory ] && echo "exists"
```
已存在时,用 AskUserQuestion 问:
- 跳过注入(保留现有内容)
- 覆盖(删除后重建)——**危险操作,二次确认**
- 合并(仅复制不存在的模板文件,保留现有内容)——推荐
### 2.4 CLAUDE.md 是否已注入过
```bash
grep -l "MEMORY-SYSTEM:BEGIN" CLAUDE.md 2>/dev/null
```
- 已含标记:跳过 CLAUDE.md 追加步骤,但仍执行 memory/ 目录和 hook 注入
- 未含标记 / 文件不存在:继续(注入时会创建或追加)
---
## 三、执行注入
### 3.1 建立 memory/ 骨架
```bash
mkdir -p memory/modules memory/bugs memory/progress
```
### 3.2 复制核心文件到 memory/
用 Read 读取 `$SKILL_DIR/templates/` 下的模板内容,用 Write 写到目标:
| 源($SKILL_DIR/templates/) | 目标 |
|---|---|
| `_index.md` | `memory/_index.md` |
| `_tutorial.md` | `memory/_tutorial.md` |
| `modules/overview.template.md` | `memory/modules/overview.template.md` |
| `modules/design.template.md` | `memory/modules/design.template.md` |
| `modules/integration.template.md` | `memory/modules/integration.template.md` |
| `modules/known_issues.template.md` | `memory/modules/known_issues.template.md` |
| `bugs/bug.template.md` | `memory/bugs/bug.template.md` |
| `progress/current-status.template.md` | `memory/progress/current-status.template.md` |
**合并模式下**:只复制目标不存在的文件。
### 3.3 追加规范到 CLAUDE.md
读取 `$SKILL_DIR/templates/CLAUDE_INJECT.md`,用追加方式写入项目 `CLAUDE.md`:
- 如果 `CLAUDE.md` 不存在:创建,内容就是 CLAUDE_INJECT.md 的内容
- 如果已存在但没有 `<!-- MEMORY-SYSTEM:BEGIN -->` 标记:在文件末尾追加(前面加一个空行)
- 如果已有标记:跳过
用 Edit 工具做追加(读现有内容 → 拼接 → Write)。
---
## 四、配置 hook
### 4.1 复制 hook 脚本到项目
```bash
mkdir -p .claude/hooks
cp "$SKILL_DIR/hooks/sync-memory-index.mjs" .claude/hooks/sync-memory-index.mjs
```
(Windows 的 Git Bash 下 `cp` 可用。跨平台稳妥的话用 Read + Write 代替 cp。)
### 4.2 注册 hook 到 .claude/settings.json
读取 `.claude/settings.json`(不存在则新建 `{}`),确保下列条目存在:
```json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "node .claude/hooks/sync-memory-index.mjs"
}
]
}
]
}
}
```
**hook 说明**:
- `sync-memory-index.mjs`(PostToolUse):修改 memory/ 文件后,自动同步 _index.md 的最后更新日期(一次可更新所有匹配行)
**合并规则**:
- 如果已存在 `hooks.PostToolUse` 数组,检查是否已有相同 command 的条目;有则跳过,没有则追加一项。
- 保留用户其他 hook 配置不动。
写回时用**4 空格缩进**,保留 JSON 格式。
### 4.3 验证 Node 可用
```bash
node --version
```
无 Node 时提示用户安装 Node 16+,但不阻断注入(hook 可后补配置)。
---
## 五、原子 git commit
```bash
git add memory/ CLAUDE.md .claude/hooks/sync-memory-index.mjs .claude/settings.json
git status --short
git commit -m "chore: inject AI memory system
- memory/ directory skeleton with templates
- _tutorial.md as portable guide
- PostToolUse hook: auto-sync _index.md last-updated column
- CLAUDE.md: memory usage rules + startup protocol appended
Generated by inject-memory skill."
```
**禁止** `--no-verify`。如果 pre-commit hook 失败:
1. 报告失败原因给用户
2. **不** 自动撤销改动——让用户自己修复钩子或手动提交
3. 说明已完成的文件操作,用户可以 `git status` 查看
---
## 六、汇报结果
commit 成功后,向用户报告:
- 创建的目录和文件清单(精简,不列模板每个文件,只说"modules/+ bugs/ + progress/ + 4 个 modules 模板 + 1 个 bugs 模板 + 1 个 progress 模板")
- 追加到 CLAUDE.md 的章节名
- hook 注册的 settings.json 位置
- 验证方式:随便改一下 `memory/modules/X.md` 保存,应能看到 `_index.md` 的日期被同步(前提是表格里有对应条目和日期列)
- 下一步推荐:让用户看 `memory/_tutorial.md` 了解如何开始用
---
## 七、错误处理与回滚
**回滚触发条件**:阶段三/四出错且用户选择"回滚"。
**回滚动作**(需二次确认后执行,因为会删除新建文件):
```bash
rm -rf memory/
rm -f .claude/hooks/sync-memory-index.mjs
git checkout -- CLAUDE.md .claude/settings.json 2>/dev/null || rm -f CLAUDE.md .claude/settings.json
```
注意:`git checkout --` 只对已追踪文件有效;没被追踪过的新文件要 `rm`。
**永远不要**:
- 自动 `git reset --hard`
- 删除用户原本就有的 CLAUDE.md 内容
- 跳过 pre-commit hook
---
## 八、幂等性保证
此 skill 重复运行应当**无害**:
- memory/ 已存在 → 默认跳过或合并
- CLAUDE.md 有 MEMORY-SYSTEM 标记 → 跳过追加
- settings.json 已有对应 hook → 跳过注册
- hook 脚本(sync-memory-index.mjs)已存在且内容一致 → 跳过复制(用 Read 对比)
如果只部分已存在,只补齐缺的部分。
---
## 九、关键约束
1. **禁止强制覆盖**用户已有的 CLAUDE.md / settings.json 内容,只能追加或合并
2. **所有破坏性动作**(覆盖 memory/、删除文件)必须 AskUserQuestion 二次确认
3. **commit 信息**必须清楚说明是自动注入,方便以后 revert
4. **回滚**不要自动触发,问用户
5. 注入完成后不要自动做除了 commit 之外的动作(比如自动触发记忆系统的其他操作)
Files in this package (11)
- SKILL.md 8 KB
- hooks/sync-memory-index.mjs 5 KB
- templates/CLAUDE_INJECT.md 2 KB
- templates/_index.md 2 KB
- templates/_tutorial.md 15 KB
- templates/bugs/bug.template.md 1 KB
- templates/modules/design.template.md 1 KB
- templates/modules/integration.template.md 1 KB
- templates/modules/known_issues.template.md 1 KB
- templates/modules/overview.template.md 1 KB
- templates/progress/current-status.template.md 1 KB
Versions
The database has no other skills named inject-memory.
Database snapshot: 13.11.2026 · package version 6f0b7c472823 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100memory-init
在当前目录下初始化记忆系统,生成 CLAUDE.md(可选 AGENT.md 给 Cursor 用)、MEMORY.md 和 memory/ 目录。当用户说"初始化记忆"、"搭建记忆"、"memory init"、"/memory-init"时触发。 - 100memory-init
Initialize the Engram memory system — creates configuration, memory files, and CLAUDE.md integration. Trigger when the user sets up persistent memory for a project, or when `.claude/memory-settings.json` is missing and a memory feature is requested. - 95meta-hook-development
Use when the user asks to ”create a hook”, ”add a PreToolUse/PostToolUse/Stop hook”, or ”validate hooks”. - 100plugin-hooks
Authoritative guide to Claude Code hooks — event-driven scripts that execute before or after tool calls, session events, file changes, and more. Use when writing a PreToolUse hook to block dangerous commands, a PostToolUse hook to auto-format after edits, a SessionStart hook to… - 100memory-init
Scaffold a project-local memory directory and AGENTS.md guidance for reusable agent knowledge. Use when a user wants to initialize `.agents/memories/` or another project memory path so future agents can read and maintain durable project context. - 100hook-creator
Create and configure Claude Code hooks for customizing agent behavior. Use when the user wants to (1) create a new hook, (2) configure automatic formatting, logging, or notifications, (3) add file protection or custom permissions, (4) set up pre/post tool execution actions, or…