鱼皮AI导航
鱼皮AI导航
  • 主页
  • 社区
  • AI工具
  • AI知识库
  • AI提示词
  • MCP
  • Skills
  • AI学习资源
  • AI资讯
  • GitHub开源
登录/注册

error-pattern-safety

确保正则表达式错误匹配模式的安全性,避免因零宽度匹配导致的无限循环。通过要求至少一个字符匹配、禁止裸用 .* 及测试空字符串匹配等方式,保障 JavaScript 中使用 g 标志时的稳定性。

软件开发
访问 GitHub
文档Skills.MD
name
error-pattern-safety
description
Error Pattern Safety Guidelines for Agentic Engines

Error Pattern Safety Guidelines

This document outlines the safety guidelines for error pattern regex in agentic engines to prevent infinite loops in JavaScript.

The Problem

When using regex patterns with the JavaScript global flag (/pattern/g), patterns that can match zero-width (empty strings) can cause infinite loops. This happens because:

  1. JavaScript's regex.exec() with the g flag uses lastIndex to track position
  2. When a pattern matches zero-width, lastIndex doesn't advance
  3. The same position is matched repeatedly, causing an infinite loop

Dangerous Pattern Examples

❌ NEVER USE THESE PATTERNS:

▼
javascript
复制代码
// Pure .* - matches everything including empty string at end /.*/g // Single character with * - matches zero or more (including zero) /a*/g // Patterns that can match empty string /(x|y)*/g

Safe Pattern Examples

✅ ALWAYS USE PATTERNS LIKE THESE:

▼
javascript
复制代码
// Required prefix before .* /error.*/gi /error.*permission.*denied/gi // Specific structure with required content /\[(\d{4}-\d{2}-\d{2})\]\s+(ERROR):\s+(.+)/g // Required characters throughout /access denied.*user.*not authorized/gi

Pattern Safety Rules

  1. Always require at least one character match

    • Use .+ instead of .* when you need "something"
    • Ensure pattern has required prefix/suffix
  2. Never use bare .* as the entire pattern

    • Always combine with required text: error.*
    • Never just .* or .*?
  3. Test patterns against empty string

    ▼
    javascript
    复制代码
    const regex = /your-pattern/g; if (regex.test("")) { throw new Error("Pattern matches empty string - DANGEROUS!"); }
  4. Use specific anchors when possible

    • Start: ^error.*
    • End: .*error$
    • Word boundaries: \berror\b

Validation Tests

All error patterns must pass these tests:

Go Tests (pkg/workflow/engine_error_patterns_infinite_loop_test.go)

▼
go
复制代码
// Test that pattern doesn't match empty string func TestPatternSafety(t *testing.T) { pattern := "your-pattern" regex := regexp.MustCompile(pattern) if regex.MatchString("") { t.Error("Pattern matches empty string!") } }

JavaScript Tests (pkg/workflow/js/validate_errors.test.cjs)

▼
javascript
复制代码
test("should not match empty string", () => { const regex = new RegExp("your-pattern", "g"); expect(regex.test("")).toBe(false); });

Safety Mechanisms in validate_errors.cjs

The validate_errors.cjs script has built-in protections:

  1. Zero-width detection: Checks if regex.lastIndex stops advancing
  2. Iteration warning: Warns at 1000 iterations
  3. Hard limit: Stops at 10,000 iterations to prevent hang
▼
javascript
复制代码
// Safety check in validate_errors.cjs if (regex.lastIndex === lastIndex) { core.error(`Infinite loop detected! Pattern: ${pattern.pattern}`); break; }

Adding New Error Patterns

When adding new error patterns to engines:

  1. Write the pattern with required content

    ▼
    go
    复制代码
    { Pattern: `(?i)error.*permission.*denied`, LevelGroup: 0, MessageGroup: 0, Description: "Permission denied error", }
  2. Test against empty string

    • Run: make test-unit
    • Checks: TestAllEnginePatternsSafe
  3. Test with actual log samples

    • Ensure it matches real errors
    • Ensure it doesn't match informational text
  4. Document the pattern

    • Add clear description
    • Note what it's designed to catch

Pattern Conversion: Go to JavaScript

Patterns are converted from Go to JavaScript:

▼
go
复制代码
// Go pattern (case-insensitive flag) Pattern: `(?i)error.*permission.*denied` // Converted to JavaScript new RegExp("error.*permission.*denied", "gi")

The (?i) prefix is removed because JavaScript uses the i flag instead.

Examples from Current Codebase

✅ Safe Patterns

▼
go
复制代码
// Requires "error" prefix Pattern: `(?i)error.*permission.*denied` // Requires specific timestamp format Pattern: `(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)\s+\[(ERROR)\]\s+(.+)` // Requires "access denied" prefix Pattern: `(?i)access denied.*user.*not authorized`

How to Fix Unsafe Patterns

If you find a pattern that matches empty string:

Before (unsafe):

▼
go
复制代码
Pattern: `.*error.*` // Can match empty at start/end

After (safe):

▼
go
复制代码
Pattern: `error.*` // Requires "error" at start // OR Pattern: `.*error.+` // Requires "error" and at least one char after // OR Pattern: `\berror\b.*` // Requires word "error"

Testing Checklist

Before committing pattern changes:

  • Run make test-unit
  • Check TestAllEnginePatternsSafe passes
  • Check TestErrorPatternsNoInfiniteLoopPotential passes
  • Run JavaScript tests: cd pkg/workflow/js && npm test
  • Verify pattern matches intended error messages
  • Verify pattern doesn't match informational text

References

  • Go regex syntax: https://pkg.go.dev/regexp/syntax
  • JavaScript regex: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
  • Test files:
    • pkg/workflow/engine_error_patterns_infinite_loop_test.go
    • pkg/workflow/js/validate_errors.test.cjs
    • pkg/workflow/error_pattern_tuning_test.go
软件开发

快捷安装

复制到终端去安装 Skill

npxskillsaddgithubnext/gh-aw--skill"error-pattern-safety"
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
🔥热门工具
朱雀AI检测朱雀AI检测
朱雀AI检测助手朱雀AI检测助手
LovartLovart
老鱼简历老鱼简历
WritePass官网WritePass官网
面多多面多多
CursorCursor
星月写作星月写作
朱雀AI检测助手朱雀AI检测助手
零感AI零感AI
Skills 推荐
debug
debug 是一个系统化定位软件缺陷根因的技能,适用于用户报告 'fix'、'broken'、'error' 等问题或测试意外失败时。它通过可验证的假设、最小复现、因果链分析和回归测试,确保在修复前准确定位根本原因。
retrieving-developer-knowledge
retrieving-developer-knowledge 用于搜索、获取和整合 Google 官方开发者文档,覆盖 Google Cloud、AI/Gemini、Android、Chrome、Web、Flutter、Go、Firebase 等平台,适用于查询 gcloud CLI 命令、API 语法、IAM 权限或架构对比等场景。
diff-intake
diff-intake 读取 PR 或分支差异,生成结构化 YAML 变更摘要,用于下游 analytics instrumentation 技能。适用于用户提交 PR 链接、分支对比或原始 diff 后需理解变更内容、评估分析覆盖或启动埋点流程的场景。
implement
实现 Linus Dashboard 的功能开发与集成,基于 TypeScript 和 Python 构建前后端组件,遵循严格的代码规范与架构模式,完成视图、卡片、服务及传感器等模块的创建与验证,确保高质量交付并兼容 Home Assistant 生态。
pypi-server
支持从源码构建 Python 软件包,并搭建本地 PyPI 服务,实现私有包的托管与分发,使团队可通过 pip 直接安装内部开发的模块。