基于Git的记忆策略和CLI工作流,适用于OpenClaw、Hermes、Claude Code、Codex等代理。在代理需要记住持久用户偏好时使用。
Git 当作内存是一项面向实际任务的技能,主要用于使用 git- as- memory 来为代理服务器提供持久且可审计的内存层;它将内存存储在专门的 Git 参考文献中, 而不是保存在工作树中;
使用时应结合输入条件选择合适的执行方式,核对必要参数、依赖环境与输出内容,并按原始要求处理异常情况。从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
使用 git-as-memory 作为代理(agents)的持久化、可审计的记忆层。它将记忆存储在专用的 Git 引用(ref)中,而非工作树(working tree)内。
默认引用(ref):
refs/git-as-memory/memory/v1
支持的代理:OpenClaw、Hermes、Claude Code、Codex,或任何能够执行 shell 命令的代理。
若已安装 gam,优先使用:
command -v gam
若未安装,任选其一进行安装:
npm install -g git-as-memory pip install git-as-memory
无需全局安装(按需运行):
npx git-as-memory --help python -m git_as_memory.cli --help
所有命令均需在 Git 仓库中执行。若未指定 --repo 参数,则默认使用当前工作目录所在的仓库。
每个仓库仅需初始化一次,该操作是幂等的:
gam init --repo /path/to/repo
仅对稳定、可复用的信息写入记忆。
应在以下情形写入记忆:
禁止写入以下内容:
--type hypothesis;写入前请执行以下步骤:
gam search "" --repo /path/to/repo
--source 参数说明该记忆的来源。写入完成后,请报告该记忆的键(key):
/
使用一组精简且保持一致的类型:
entity:用户、项目、组织、工具或其他实体的偏好/档案;semantic:持久性事实、规则、约定、决策或技术经验;episodic:任务/会话/事件的简洁摘要;working:生命周期较短的任务上下文,后续可能被清理;hypothesis:未经验证的推断,应在强依赖前完成确认。使用稳定、小写的 kebab-case 格式 ID,优先选用未来代理可合理推测出的名称。
推荐示例:
user-preference-concise-answers project-release-workflow openclaw-memory-policy git-as-memory-storage-layout
除非确实无法抽象出稳定概念,否则避免使用随机 ID。
gam write "User prefers concise technical answers with concrete commands." --repo /path/to/repo --type entity --id user-preference-concise-answers --tag user --tag preference --source "User corrected the agent for over-explaining and asked for practical output."
多行内容写入(通过 stdin):
printf '%sn' "Decision summary..." | gam write --stdin --repo /path/to/repo --type semantic --id project-decision-example --source "Summarized from the current implementation session."
当你已知确切记忆键(key)时,使用 read:
gam read entity/user-preference-concise-answers --repo /path/to/repo
show 在仅提供 ID 并可选指定 type 时效果等同:
gam show user-preference-concise-answers --type entity --repo /path/to/repo
使用 search 进行内容检索:
gam search "release workflow" --repo /path/to/repo gam search "concise answers" --repo /path/to/repo --json
搜索基于本地子字符串匹配,覆盖记忆正文、标签(tags)、元数据及 source 字段文本。
glob 用于记忆键(key)匹配,而非任意工作树文件。支持匹配 格式或裸 ID:
gam glob "entity/*" --repo /path/to/repo gam glob "user-*" --repo /path/to/repo gam glob "*release*" --repo /path/to/repo
--files 仅用于调试底层 Git 树结构:
gam glob "entity/*" --files --repo /path/to/repo
gam list --repo /path/to/repo gam list --repo /path/to/repo --json
gam history user-preference-concise-answers --type entity --repo /path/to/repo
直接通过 Git 检查:
git -C /path/to/repo log --oneline refs/git-as-memory/memory/v1 git -C /path/to/repo ls-tree -r --name-only refs/git-as-memory/memory/v1
优先使用软删除(soft delete):
gam delete user-preference-concise-answers --type entity --repo /path/to/repo
仅当用户明确要求移除当前可见文件时,才使用 purge:
gam purge user-preference-concise-answers --type entity --repo /path/to/repo
在仓库中启动新任务时:
gam search "" --repo /path/to/repo
当出现持久性经验时:
当用户询问“你记住了什么?”时:
gam list、gam glob 或 gam search 列出候选项;gam read 获取详情;gam history 输出。完成一次记忆写入后,请简要响应:
Remembered: entity/user-preference-concise-answers
若决定不写入记忆,仅在具备实际意义时说明原因:
Not writing memory: this is a one-off task detail.