调用了不存在的方法
Called a method that doesn't exist
智能体用了某个方法、选项、配置项或命令行参数,它存在于别的语言或别的版本里,但不在你的项目里。
适用于: Claude Code · Cursor · Codex · Copilot agent
症状
JavaScript 里写 `haystack.contains(needle)`、框架会悄悄忽略的配置项,或运行时才报错的命令行参数。
为什么会这样
智能体会把不同语言、不同版本的 API 混为一谈;签名看起来合理,是因为确实有某种语言这么写。配置项更糟,未知的键常常被静默忽略。agent-review 列为 AR006;developertoolkit.ai 认为最早的控制点在会话内(让智能体自己跑类型检查和测试)。
示例
摘自 agent-review 分类(AR006)。
jsif (haystack.contains(needle)) { ... } // should be .includes
复制即用的修复
按你用的智能体挑选文件。规则片段用英文写,放进任何仓库都能用。
规则
Claude Code:加入 CLAUDE.md
CLAUDE.md## Check the API against the installed version
- Before using an unfamiliar method, option, config key or flag, confirm it in the installed
version: read the type definitions in node_modules / site-packages, run `<cli> --help`, or open the
docs for the exact version in the lockfile.
- Run the type checker and tests after every edit, not only at the end.
- When you change config, show evidence the setting took effect (log line, output, behavior).
规则
Codex、Copilot 编码智能体等:加入 AGENTS.md
AGENTS.md## Check the API against the installed version
- Before using an unfamiliar method, option, config key or flag, confirm it in the installed
version: read the type definitions in node_modules / site-packages, run `<cli> --help`, or open the
docs for the exact version in the lockfile.
- Run the type checker and tests after every edit, not only at the end.
- When you change config, show evidence the setting took effect (log line, output, behavior).
Cursor 规则
Cursor:项目规则
.cursor/rules/hallucinated-api-method.mdc---
description: "AgentFails: Called a method that doesn't exist"
alwaysApply: true
---
## Check the API against the installed version
- Before using an unfamiliar method, option, config key or flag, confirm it in the installed
version: read the type definitions in node_modules / site-packages, run `<cli> --help`, or open the
docs for the exact version in the lockfile.
- Run the type checker and tests after every edit, not only at the end.
- When you change config, show evidence the setting took effect (log line, output, behavior).
钩子
Claude Code PostToolUse 钩子:每次编辑后做类型检查
PostToolUse 钩子返回退出码 2 时,stderr 会展示给 Claude,让它立刻修。可把 tsc 换成 mypy、cargo check、go vet 等。
.claude/settings.json{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write|MultiEdit",
"hooks": [
{ "type": "command", "command": "npx tsc --noEmit -p . 1>&2 || exit 2" }
]
}
]
}
}