memorix icon indicating copy to clipboard operation
memorix copied to clipboard

claude code不会自动触发记忆

Open kendev93 opened this issue 9 months ago • 10 comments

在claude code cli不会自动触发记忆,但是在codex cli中会自动触发查询和保存的记忆,不知道是hooks的问题,还是因为我claude code cli里面安装了oh my claude code的问题,或者本来就这样?

kendev93 avatar Feb 25 '26 00:02 kendev93

另外还有发现点问题,不知道是不是glm这个模型的问题,我是在claude code里面使用glm,查询的时候经常出现参数错误。

⏺ memorix - memorix_detail (MCP)(ids: "[16]") ⎿  Error: MCP error -32602: Input validation error: Invalid arguments for tool memorix_detail: [ { "code": "invalid_type", "expected": "array", "received": "string", "path": [ "ids" ], "message": "Expected array, received string" } … +1 line (ctrl+o to see all)

⏺ 这证实了问题所在:

Claude Code CLI 的工具调用系统在传递参数给 MCP 服务器时,自动将所有值转换成字符串:

  • 我传递:{"ids": [16]}(JSON 数字数组)
  • Memorix 收到:"ids": "[16]"(字符串)
  • Memorix 期望:"ids": [16](真正的数组)

这是 Claude Code CLI 与 Memorix MCP 服务器之间的类型转换 bug,不是参数格式问题。

Codex 能正常工作,说明 Codex 的工具调用系统正确保留了参数类型。

kendev93 avatar Feb 25 '26 00:02 kendev93

感谢反馈!我深入调研了这两个问题,以下是分析和修复:

问题 1:参数类型错误(Expected array, received string)

根因:这是 Claude Code CLI 的已知 Bug + GLM 模型放大了问题。

Claude Code CLI 有一个已知的参数序列化 Bug(anthropics/claude-code#5504、#26027),会将 JSON 数组/对象序列化为字符串。例如 ids: [16] 变成 ids: '[16]'。

当使用非 Anthropic 模型(如 GLM)时,模型的 tool calling 输出对类型保真度不如 Claude/GPT 原生模型高,更容易触发此问题。Codex CLI 不受影响是因为它使用自己的参数传递管道 + o3/o4-mini 模型类型保真度更高。

修复(v0.9.1 已发布): 所有 24 个 MCP 工具现在都添加了防御性参数类型转换,能自动处理:

  • 字符串编码的数组:'[16]' [16]
  • 字符串编码的数字:'20' 20
  • 字符串编码的对象:'{key:val}' {key:'val'}

请更新到最新版本:npm install -g memorix@latest

问题 2:Claude Code 不会自动触发记忆

这不是 Bug,而是机制不同:

  • Codex 之所以「自动触发」,是因为 Codex 读取 AGENTS.md 规则文件,里面有指令告诉它主动调用 memorix 工具
  • Claude Code 读的是 CLAUDE.md,需要确认你的项目目录下是否有包含 memorix 指令的 CLAUDE.md 文件
  • 如果安装了 oh-my-claude-code,它可能覆盖了 .claude/settings.json 中的 hooks 配置

解决方案:

  1. 运行 memorix hooks install --agent claude --global 安装 hooks
  2. 检查 ~/.claude/settings.json 确认 hooks 字段存在且未被覆盖
  3. 如果使用 oh-my-claude-code,需要手动合并两者的 hooks 配置

AVIDS2 avatar Feb 25 '26 04:02 AVIDS2

Settings Error

/Users/chenken/.claude/settings.json └ hooks ├ PostToolUse │ └ 0 │ └ hooks: Expected array, but received undefined ├ PreCompact │ └ 0 │ └ hooks: Expected array, but received undefined ├ SessionStart │ └ 0 │ └ hooks: Expected array, but received undefined ├ Stop │ └ 0 │ └ hooks: Expected array, but received undefined └ UserPromptSubmit └ 0 └ hooks: Expected array, but received undefined

Hooks use a new format with matchers. Example: {"PostToolUse": [{"matcher": {"tools": ["BashTool"]}, "hooks": [{"type": "command", "command": "echo Done"}]}]} Learn more: https://code.claude.com/docs/en/hooks

Files with errors are skipped entirely, not just the invalid settings.

❯ 1. Exit and fix manually 2. Continue without these settings

Enter to confirm · Esc to cancel

当我执行memorix hooks install --agent claude --global后,再启动claude,报上面的错了,我安装的是最新版本的claude,是不是hooks的写法变了? @AVIDS2 麻烦看下,我把claude code完全删除,.claude文件都删了,重新安装的,只装了memorix

kendev93 avatar Feb 25 '26 06:02 kendev93

v0.9.3 更新 全部修复

感谢持续反馈!v0.9.3 已发布,修复了你报告的所有问题:

1. Claude Code hooks 格式错误 已修复

Claude Code 新版改了 hooks 格式,需要 {matcher: {}, hooks: [...]} 嵌套结构。v0.9.3 的 memorix hooks install --agent claude --global 现在生成正确格式,不会再报 'Expected array, but received undefined' 错误。

请先手动删除旧的 hooks 配置再重新安装: `ash

1. 编辑 ~/.claude/settings.json,删掉 hooks 字段

2. 重新安装

npm install -g memorix@latest memorix hooks install --agent claude --global `

2. 参数类型错误 已在 v0.9.1 修复

GLM 模型的 tool calling 类型问题已通过防御性参数转换解决。

3. auto-hooks 触发

确认 hooks 安装正确后(~/.claude/settings.json 中有新格式的 hooks 字段),重启 Claude Code 即可生效。

AVIDS2 avatar Feb 25 '26 08:02 AVIDS2

$ claude ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Settings Error

/Users/chenken/.claude/settings.json └ hooks ├ PostToolUse │ └ 0 │ └ matcher: Expected string, but received object ├ PreCompact │ └ 0 │ └ matcher: Expected string, but received object ├ SessionStart │ └ 0 │ └ matcher: Expected string, but received object ├ Stop │ └ 0 │ └ matcher: Expected string, but received object └ UserPromptSubmit └ 0 └ matcher: Expected string, but received object

Hooks use a new format with matchers. Example: {"PostToolUse": [{"matcher": {"tools": ["BashTool"]}, "hooks": [{"type": "command", "command": "echo Done"}]}]} Learn more: https://code.claude.com/docs/en/hooks

Files with errors are skipped entirely, not just the invalid settings.

❯ 1. Exit and fix manually 2. Continue without these settings

Enter to confirm · Esc to cancel

"hooks": { "SessionStart": [ { "matcher": {}, "hooks": [ { "type": "command", "command": "memorix hook", "timeout": 10 } ] } ], "PostToolUse": [ { "matcher": {}, "hooks": [ { "type": "command", "command": "memorix hook", "timeout": 10 } ] } ], "UserPromptSubmit": [ { "matcher": {}, "hooks": [ { "type": "command", "command": "memorix hook", "timeout": 10 } ] } ], "PreCompact": [ { "matcher": {}, "hooks": [ { "type": "command", "command": "memorix hook", "timeout": 10 } ] } ], "Stop": [ { "matcher": {}, "hooks": [ { "type": "command", "command": "memorix hook", "timeout": 10 } ] } ] }

还是报错

kendev93 avatar Feb 25 '26 08:02 kendev93

感谢反馈,这个是我们的问题。v0.9.3 生成的 hooks 配置把 matcher 写成了空对象 {},但 Claude Code 的 matcher 字段只接受字符串(比如 Bash、Edit|Write)。不需要过滤时应该直接省略 matcher 字段。已在 v0.9.5 修复。修复步骤:1. 编辑 ~/.claude/settings.json,把里面的 hooks 字段整个删掉 2. npm install -g memorix@latest 3. memorix hooks install --agent claude --global 4. 重启 Claude Code。如果还有问题随时说。

AVIDS2 avatar Feb 25 '26 08:02 AVIDS2

现在claude code的hooks识别了正常启动了,但是我开发半天了也没触发一次自动记忆和读取记忆,不会就我这样子吧

kendev93 avatar Feb 25 '26 09:02 kendev93

你好,已经更新到 0.9.8 版本测试通过了。

之前的问题确认是两个 bug 导致的:

  1. v0.9.7 修复:Claude Code 发送的 hook payload 用的是 hook_event_name(snake_case),但 memorix 内部读的是 hookEventName(camelCase),导致所有事件类型都被误判,自动记忆完全不触发。同时修复了 session_id、 ool_response 等字段的映射。

  2. v0.9.8 修复:hooks 自动安装器把配置写到了 .github/hooks/memorix.json,但 Claude Code 实际读取的是 .claude/settings.local.json,所以 hooks 虽然生成了但不会被加载。现在改为写入正确路径,并且用深度合并避免覆盖已有配置。

更新方法: ash npm install -g [email protected]

安装后重启 Claude Code,SessionStart hook 会自动触发并注入历史记忆。后续使用工具(Bash、Write 等)时 PostToolUse hook 也会自动捕获记忆,不需要手动配置。

目前我自己的 Claude Code 和朋友机器上的 CC 都跑过一遍测试了,确认 hooks 自动安装 + 自动触发记忆都正常,不需要再手动改配置文件。

感谢你的 issue,帮我们定位到了这个关键 bug

AVIDS2 avatar Feb 25 '26 12:02 AVIDS2

我发现只有打开的时候触发了一次 ⎿  SessionStart:resume says: Memorix is active. Your memories from previous sessions are available via memorix_search.

后面让开发的时候触发了一次搜索,但是都没触发保存,我重新安装了最新版本的,并且重新安装了hooks

经过我查看dashboard,原来是有静默保存的,这个跟codex使用体验差距比较明显。我还是希望能和codex那样知道在查什么在保存什么。而且我发现代码开发的 problem-solution并没有保存,还有自动压缩的时候会保存很多low quality信息,不知道是不是就这么设计的。

kendev93 avatar Feb 25 '26 16:02 kendev93

感谢你的反馈 @vinsmoke66!你的 issue 帮助我们发现并修复了多个关键问题,非常有价值。

你提到的四个问题在 v0.9.16v0.9.18 中已全部修复:

1. 「后续没触发保存」 根因是 Claude Code 发送的 Bash 命令格式为 cd /path && npm test 2>&1,被噪声过滤器的 /^cd\b/ 正则误杀,加上 extractContent() 丢失了 toolInput 数据。现在用 Store-first 管道 + extractRealCommand() 前缀剥离,Write/Edit/Bash 全部稳定捕获(6 轮实测 100%)。

2. 「静默保存,跟 Codex 体验差距大」 每次保存后现在会返回 systemMessage 反馈,例如: Memorix saved: Changed auth.ts [what-changed] Memorix saved: Ran: npm test [problem-solution] Memorix saved: 如何实现JWT认证 [discovery]

3. 「problem-solution 没保存」 命令类工具现在默认 always store,pattern detection 只决定类型分类(problem-solution / discovery / gotcha 等),不再拦截存储决策。

4. 「压缩时保存很多 low quality」 空壳 session_end 噪声已过滤(从 8.5% 0%),检查 memorix 自身数据的命令(如 node -e 读取 observations.json)也不再存储。

请升级到 0.9.18 测试: \\ash npm install -g [email protected] memorix hooks install \\

再次感谢你的详细反馈,正是这些真实使用场景的报告推动了 memorix 的质量提升!如果还有任何问题欢迎继续反馈。

AVIDS2 avatar Feb 25 '26 18:02 AVIDS2