verify-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAgent Pattern Verification
Agent模式验证
Purpose
用途
Verify AI agent code for common anti-patterns that can cause infinite loops, runaway retries, tool mismatches, and context overflow. All analysis happens locally.
验证AI Agent代码中可能导致无限循环、失控重试、工具不匹配和上下文溢出的常见反模式。所有分析均在本地进行。
When to Use
使用场景
Trigger this skill when the user asks to:
- "verify agent patterns"
- "check agent loops"
- "verify tools"
- "check retry limits"
- "verify agent safety"
Note: For full verification including security, quality, and language-specific checks, tell the user to say "verify agent".
当用户提出以下请求时触发此技能:
- "验证Agent模式"
- "检查Agent循环"
- "验证工具"
- "检查重试限制"
- "验证Agent安全性"
注意: 如需包含安全、质量和特定语言检查的完整验证,请告知用户说出 "verify agent"。
Process
流程
Step 1: Detect Agent Framework
步骤1:检测Agent框架
Identify the agent framework by checking imports in Python/TypeScript files:
| Import Pattern | Framework |
|---|---|
| LangGraph |
| CrewAI |
| AutoGen |
| LangChain |
Direct | Custom |
Also check for framework config files: , .
langgraph.jsoncrew.yaml通过检查Python/TypeScript文件中的导入语句识别Agent框架:
| 导入模式 | 框架 |
|---|---|
| LangGraph |
| CrewAI |
| AutoGen |
| LangChain |
仅直接使用 | 自定义框架 |
同时检查框架配置文件:、。
langgraph.jsoncrew.yamlStep 2: Locate Agent Files
步骤2:定位Agent文件
Find files to analyze:
Priority files:
- ,
graph.py- Agent workflow definitionsgraph.ts - ,
tools.py,tools.ts,tools/*.py- Tool implementationstools/*.ts - ,
state.py- State schemasstate.ts - ,
prompts.py,prompts/*.md- Prompt templatessystem.md - ,
agent.py- Main agent logicagent.ts
Directories to check:
- ,
src/agent/,agent/, project rootsrc/ - ,
lib/,app/packages/
Exclude from analysis:
- directory — these are skill definitions, not agent system prompts
skills/
找到需要分析的文件:
优先文件:
- 、
graph.py- Agent工作流定义graph.ts - 、
tools.py、tools.ts、tools/*.py- 工具实现tools/*.ts - 、
state.py- 状态模式state.ts - 、
prompts.py、prompts/*.md- 提示词模板system.md - 、
agent.py- 主Agent逻辑agent.ts
需检查的目录:
- 、
src/agent/、agent/、项目根目录src/ - 、
lib/、app/packages/
排除分析的内容:
- 目录 —— 这些是技能定义,而非Agent系统提示词
skills/
Step 3: Run Pattern Checks
步骤3:运行模式检查
Check Tiers
检查层级
- — Mechanical check. Apply exactly as written.
[PATTERN] - — Judgment required. Mark findings clearly.
[HEURISTIC]
Tag every finding with for pattern or for heuristic.
[P][H]- —— 机械检查。严格按描述执行。
[PATTERN] - —— 需要主观判断。清晰标记发现的问题。
[HEURISTIC]
为每个发现的问题标记(模式匹配)或(启发式判断)。
[P][H]3.1 [PATTERN]
Loop Safety
[PATTERN]3.1 [PATTERN]
循环安全性
[PATTERN]Apply mechanically. Do not pass a loop because it "looks like it might terminate."
| Pattern to find | Pass condition | Severity |
|---|---|---|
| A | ⚠️ Warning if absent |
| A | ⚠️ Warning if absent |
| A | ⚠️ Warning if absent |
| Function calls itself recursively | A non-recursive return path exists (base case), OR a depth/counter parameter is present | ⚠️ Warning if absent |
[HEURISTIC]After applying the pattern table, also scan for:
- Loops where termination depends entirely on external/runtime state with no timeout
- Generator functions that indefinitely without documented exit
yield - Event/polling loops without timeout parameters
- Recursive call chains across multiple functions without depth tracking
Flag as ⚠️ Warning: "Potential unbounded loop not matching known patterns — verify termination condition manually"
机械执行检查。不要因为循环“看起来可能终止”就忽略问题。
| 需查找的模式 | 通过条件 | 严重程度 |
|---|---|---|
Python中的 | 同一代码块内存在 | ⚠️ 缺失则警告 |
Go中的 | 代码块内存在 | ⚠️ 缺失则警告 |
TS/JS中的 | 代码块内存在 | ⚠️ 缺失则警告 |
| 函数递归调用自身 | 存在非递归返回路径(基准情况),或包含深度/计数器参数 | ⚠️ 缺失则警告 |
[HEURISTIC]应用上述模式表后,还需扫描以下情况:
- 终止完全依赖外部/运行时状态且无超时的循环
- 无限 且未记录退出条件的生成器函数
yield - 无超时参数的事件/轮询循环
- 跨多个函数且无深度跟踪的递归调用链
标记为 ⚠️ 警告:"存在未匹配已知模式的潜在无限循环 —— 请手动验证终止条件"
3.2 [PATTERN]
Retry Limit Enforcement
[PATTERN]3.2 [PATTERN]
重试限制强制
[PATTERN]Apply mechanically. If required parameter is absent, flag as ❌ Issue.
Python — Decorator-based:
| Library/Pattern | Required parameter | Fail condition |
|---|---|---|
| | |
| | |
Python — HTTP client retry:
| Library/Pattern | Required parameter | Fail condition |
|---|---|---|
| | |
| Retry object must have | |
| | |
Python — AWS SDK (boto3):
| Library/Pattern | Required parameter | Fail condition |
|---|---|---|
| | |
Note: boto3 without explicit retry config uses SDK defaults (3 attempts) — do not flag absence.
JavaScript/TypeScript:
| Library/Pattern | Required parameter | Fail condition |
|---|---|---|
| | |
| | |
Custom retry loops (all languages):
| Pattern to find | Pass condition | Fail condition |
|---|---|---|
Loop + | Integer counter with max check | No counter → ❌ Issue |
[HEURISTIC]After applying pattern tables, scan for:
- Functions/decorators with "retry" in name not in tables above
- Imported modules with "retry" in package name (e.g. ,
stamina)aiohttp_retry - Loops with sleep + exception handling + re-invocation without visible counter
- Config keys like ,
max_retries,retry_countattempts
Flag as ⚠️ Warning: "Potential retry pattern not matching known libraries — verify retry bounds manually"
机械执行检查。若缺失必填参数,标记为 ❌ 问题。
Python —— 基于装饰器:
| 库/模式 | 必填参数 | 失败条件 |
|---|---|---|
| | 缺失 |
| | 缺失 |
Python —— HTTP客户端重试:
| 库/模式 | 必填参数 | 失败条件 |
|---|---|---|
| | 缺失 |
| Retry对象必须包含 | 缺失 |
| | 缺失 |
Python —— AWS SDK(boto3):
| 库/模式 | 必填参数 | 失败条件 |
|---|---|---|
| | 缺失 |
注意:未显式配置重试的boto3会使用SDK默认值(3次尝试)—— 无需标记缺失。
JavaScript/TypeScript:
| 库/模式 | 必填参数 | 失败条件 |
|---|---|---|
| 选项中包含 | 缺失 |
| 选项中包含 | 缺失 |
自定义重试循环(所有语言):
| 需查找的模式 | 通过条件 | 失败条件 |
|---|---|---|
循环 + | 包含带最大值检查的整数计数器 | 无计数器 → ❌ 问题 |
[HEURISTIC]应用上述模式表后,扫描以下情况:
- 名称包含“retry”且未在表中的函数/装饰器
- 包名包含“retry”的导入模块(如 、
stamina)aiohttp_retry - 包含sleep+异常处理+重新调用但无可见计数器的循环
- 类似 、
max_retries、retry_count的配置键attempts
标记为 ⚠️ 警告:"存在未匹配已知库的潜在重试模式 —— 请手动验证重试边界"
3.3 [PATTERN]
Tool Registry Consistency
[PATTERN]3.3 [PATTERN]
工具注册表一致性
[PATTERN]Step 1: Collect defined tools
Scan tool definition files. A name found by any pattern counts as registered.
Python — decorator patterns:
| Pattern | How to extract name |
|---|---|
| Function name below decorator |
| Function name below decorator |
| Use |
Python — dict/list patterns:
| Pattern | How to extract name |
|---|---|
| Value of |
| Top-level |
| Top-level |
| Each function name in list |
| Each identifier in list |
TypeScript/JavaScript:
| Pattern | How to extract name |
|---|---|
| |
| The |
| Value of |
| Value of |
Step 2: Collect tool references from prompts
Scan , , for backtick-quoted identifiers naming capabilities.
.md.txtprompts.pyStep 3: Cross-reference
| Finding | Severity |
|---|---|
| Reference not in definition list | ❌ Issue (hallucinated tool) |
| Defined tool not in any prompt | ⚠️ Warning (undocumented tool) |
[HEURISTIC]Find where tools are defined and where LLM is invoked. If tools exist but are never connected to the LLM call, flag as ❌ Issue: "Tools defined but never connected to LLM invocation"
[HEURISTIC]Scan for tool-like structures:
- Dicts with both and
"description"keys"parameters" - Functions with structured docstrings (name, params, return)
- Variables named ,
tools,tool_list,available_toolsfunctions - Classes with ,
run(), orexecute()methods__call__()
Include in count and note: "Tool detected via heuristic — verify this is an intended agent tool."
步骤1:收集已定义的工具
扫描工具定义文件。通过任意模式找到的名称均视为已注册。
Python —— 装饰器模式:
| 模式 | 名称提取方式 |
|---|---|
| 装饰器下方的函数名 |
| 装饰器下方的函数名 |
| 使用 |
Python —— 字典/列表模式:
| 模式 | 名称提取方式 |
|---|---|
| |
| 顶层的 |
| 顶层的 |
| 列表中的每个函数名 |
| 列表中的每个标识符 |
TypeScript/JavaScript:
| 模式 | 名称提取方式 |
|---|---|
| |
| |
| |
| |
步骤2:收集提示词中的工具引用
扫描 、、 文件,查找用反引号包裹的标识能力的名称。
.md.txtprompts.py步骤3:交叉引用
| 发现结果 | 严重程度 |
|---|---|
| 引用未出现在定义列表中 | ❌ 问题(幻觉工具) |
| 已定义工具未出现在任何提示词中 | ⚠️ 警告(未文档化工具) |
[HEURISTIC]查找工具定义位置和LLM调用位置。若存在工具但从未关联到LLM调用,标记为 ❌ 问题:"工具已定义但从未关联到LLM调用"
[HEURISTIC]扫描类工具结构:
- 同时包含 和
"description"键的字典"parameters" - 带有结构化文档字符串(名称、参数、返回值)的函数
- 名为 、
tools、tool_list、available_tools的变量functions - 包含 、
run()或execute()方法的类__call__()
将其计入总数并备注:"通过启发式检测到工具 —— 请验证这是否为预期的Agent工具。"
3.4 [PATTERN]
Context Size Awareness
[PATTERN]3.4 [PATTERN]
上下文大小感知
[PATTERN]Formula:
token_estimate = len(file_content_chars) / 4| Content | ⚠️ Warning threshold | ❌ Issue threshold |
|---|---|---|
| System prompt file | > 4,000 tokens | > 8,000 tokens |
| Single tool description | > 500 tokens | > 1,000 tokens |
| All tool descriptions combined | > 2,000 tokens | > 4,000 tokens |
Exclude: directories (loaded on demand, not embedded)
skills/[HEURISTIC]- Estimates within 20% of threshold → flag with tokenizer recommendation
- Dynamic prompts (f-strings, ) → flag if template alone is large
.format() - Multiple concatenated prompts → estimate combined size
- Prompts with includes → note effective size may be larger
计算公式:
token_estimate = len(file_content_chars) / 4| 内容 | ⚠️ 警告阈值 | ❌ 问题阈值 |
|---|---|---|
| 系统提示词文件 | > 4000 tokens | > 8000 tokens |
| 单个工具描述 | > 500 tokens | > 1000 tokens |
| 所有工具描述总和 | > 2000 tokens | > 4000 tokens |
排除: 目录(按需加载,不嵌入)
skills/[HEURISTIC]- 估算值在阈值的20%范围内 → 标记并推荐使用分词器
- 动态提示词(f-strings、)→ 若模板本身较大则标记
.format() - 多个拼接的提示词 → 估算总大小
- 包含引用的提示词 → 备注实际大小可能更大
3.5 [HEURISTIC]
Explicit Tool Listing
[HEURISTIC]3.5 [HEURISTIC]
工具显式列出
[HEURISTIC]Check system prompts for:
- Headers like "Available Tools", "You have access to"
- Tool capability descriptions
Flag if tools are defined but not documented in system prompt.
检查系统提示词是否包含:
- 类似“可用工具”“你可以访问”的标题
- 工具能力描述
若工具已定义但未在系统提示词中记录则标记。
3.6 [PATTERN]
LangGraph Graph Cycle Analysis
[PATTERN]3.6 [PATTERN]
LangGraph图循环分析
[PATTERN](Only when LangGraph is detected)
Detection steps:
a. Find graph file (, , or file with /)
graph.pygraph.tsStateGraphMessageGraphb. Build edge map:
- — unconditional edge
workflow.add_edge(source, dest) - — extract destinations from mapping
workflow.add_conditional_edges(source, fn, mapping)
c. Identify cycles: nodes reachable from themselves
d. For each cycle, check if (or ) is reachable via conditional edge
END"__end__"| Condition | Severity |
|---|---|
Cycle exists, | ✅ Pass |
Cycle exists, no path to | ❌ Issue |
Graph has no | ❌ Issue |
Node has no outgoing edges and is not | ⚠️ Warning (dead-end) |
Example — infinite cycle (❌ Issue):
python
workflow.add_edge("agent", "tools")
workflow.add_edge("tools", "agent") # no path to ENDExample — cycle with exit (✅ Pass):
python
workflow.add_conditional_edges("agent", should_continue, {
"continue": "tools",
"end": END
})
workflow.add_edge("tools", "agent")[HEURISTIC]Scan for graph-like control flow:
- State machines with transition tables
- Custom routing with implicit cycles
- LangGraph.js (camelCase methods)
- CrewAI/AutoGen agent handoffs
- Adjacency lists without termination path
Flag as ⚠️ Warning: "Potential cyclic control flow — verify termination condition exists"
(仅检测到LangGraph时执行)
检测步骤:
a. 找到图文件(、 或包含 / 的文件)
graph.pygraph.tsStateGraphMessageGraphb. 构建边映射:
- —— 无条件边
workflow.add_edge(source, dest) - —— 从映射中提取目标节点
workflow.add_conditional_edges(source, fn, mapping)
c. 识别循环:可从自身到达的节点
d. 对每个循环,检查是否可通过条件边到达 (或 )
END"__end__"| 条件 | 严重程度 |
|---|---|
存在循环,可通过条件边到达 | ✅ 通过 |
存在循环,无到达 | ❌ 问题 |
图中无 | ❌ 问题 |
节点无出边且不是 | ⚠️ 警告(死胡同) |
示例 —— 无限循环(❌ 问题):
python
workflow.add_edge("agent", "tools")
workflow.add_edge("tools", "agent") # 无到达END的路径示例 —— 带出口的循环(✅ 通过):
python
workflow.add_conditional_edges("agent", should_continue, {
"continue": "tools",
"end": END
})
workflow.add_edge("tools", "agent")[HEURISTIC]扫描类图控制流:
- 带有转换表的状态机
- 包含隐式循环的自定义路由
- LangGraph.js(驼峰命名方法)
- CrewAI/AutoGen Agent交接
- 无终止路径的邻接表
标记为 ⚠️ 警告:"存在潜在循环控制流 —— 请验证是否存在终止条件"
Step 4: Generate Report
步骤4:生成报告
markdown
undefinedmarkdown
undefinedAgent Pattern Verification Report
Agent模式验证报告
Project: [name or path]
Date: [current date]
Framework detected: [LangGraph | CrewAI | AutoGen | LangChain | Custom | None]
Files analyzed: [count]
项目: [名称或路径]
日期: [当前日期]
检测到的框架: [LangGraph | CrewAI | AutoGen | LangChain | 自定义 | 无]
分析文件数: [数量]
Summary
摘要
✅ X checks passed | ⚠️ Y warnings | ❌ Z issues
✅ X项检查通过 | ⚠️ Y项警告 | ❌ Z项问题
Loop Safety
循环安全性
- All loops have termination conditions
- ⚠️ Potential unbounded loop at
[file:line]
- 所有循环均有终止条件
- ⚠️ 处存在潜在无限循环
[file:line]
Retry Limits
重试限制
- All retry mechanisms have explicit limits
- ❌ Missing retry limit at
[file:line]
- 所有重试机制均有显式限制
- ❌ 处缺失重试限制
[file:line]
Tool Consistency
工具一致性
- Tool registry found: X tools defined
- ❌ Y hallucinated tool references
- ⚠️ Z undocumented tools
- 找到工具注册表:已定义X个工具
- ❌ Y个幻觉工具引用
- ⚠️ Z个未文档化工具
Context Size
上下文大小
- System prompt within limits (~X tokens)
- ⚠️ System prompt exceeds recommended size
- 系统提示词在限制范围内(约X tokens)
- ⚠️ 系统提示词超出推荐大小
Findings
发现结果
= pattern-matched ·[P]= heuristic[H]
= 模式匹配 ·[P]= 启发式判断[H]
✅ Passing
✅ 通过项
- [Check]: [confirmation]
[P]
- [检查项]:[确认内容]
[P]
⚠️ Warnings
⚠️ 警告项
- [Check]: [description]
[P|H]- Location: [file:line]
- Suggestion: [how to fix]
- [检查项]:[描述]
[P|H]- 位置: [file:line]
- 建议: [修复方法]
❌ Issues
❌ 问题项
- [Check]: [description]
[P|H]- Location: [file:line]
- Rule: [which rule violated]
- Fix: [specific remediation]
- [检查项]:[描述]
[P|H]- 位置: [file:line]
- 规则: [违反的规则]
- 修复方案: [具体整改措施]
Recommendations
建议
- [Priority recommendation]
- [Additional improvements]
---
*For full verification including security, quality, and language-specific checks, say "verify agent".*- [优先级建议]
- [其他改进建议]
---
*如需包含安全、质量和特定语言检查的完整验证,请说出 "verify agent"。*