FrankenTerm

Pattern Engine Architecture

384 words2 min read4 subsectionsFrom the README at ft 0.15.21

On this page4
Why this chain is fastCross-chunk subtletyRule pack compositionLinter checks

The pattern engine is a four-stage filter chain optimized for the common case (most text matches zero rules):

text chunk
  │
  ▼
┌────────────────────────────────────────────┐
│ Stage 1: Bloom prefilter                   │  ← reject 80–95% of chunks
│ (anchor strings from every rule)           │
└────────────────────────────────────────────┘
  │ pass
  ▼
┌────────────────────────────────────────────┐
│ Stage 2: Aho-Corasick anchor match         │  ← exact anchor confirmation
│ (single-pass over the buffer)              │
└────────────────────────────────────────────┘
  │ candidate rules
  ▼
┌────────────────────────────────────────────┐
│ Stage 3: Regex evaluation (fancy_regex)    │  ← only on candidates
│ (per-rule, with timeout guards)            │
└────────────────────────────────────────────┘
  │ matches
  ▼
┌────────────────────────────────────────────┐
│ Stage 4: Dedup context + severity scoring  │  ← suppress duplicate triggers
└────────────────────────────────────────────┘
  │
  ▼ emit DetectionEvent → event bus

Why this chain is fast

  • Bloom prefilter rejects most chunks for zero cost. A pack with 100 rules has 100–500 anchor strings; the Bloom filter computes ~8 hashes per chunk and rejects when none match.
  • Aho-Corasick is O(n) in chunk length regardless of pattern count. Anchors are exact substrings, so AC is the right tool; regexes are deferred to candidates only.
  • Regex evaluation is per-rule, with timeout guards. The substrate audit closed the regex-ReDoS class via ft robot rules lint --strict, which warns on nested wildcards, excessive length (>500 chars), and consecutive spaces.
  • Dedup context prevents the same trigger from firing twice when output is repeated within a configurable window (the rate-limit retry-duration regex was recently tightened to anchor near rate-limit markers, reducing false positives; see CHANGELOG 2026-04-26).

Cross-chunk subtlety

Aho-Corasick's LeftmostFirst non-overlapping mode is not composable across chunk boundaries. Earlier bytes consume match positions, preventing later patterns from matching. The live production engine — PatternEngine::detect_with_context (patterns.rs), driven per pane segment from runtime.rs — solves this with a bounded tail re-scan: each new segment is prepended with DetectionContext::tail_buffer (capped at PatternsTuning::DEFAULT_MAX_TAIL_SIZE_BYTES, 2 KiB) and only that overlap is re-scanned, with the common no-match case rejected up front by the Bloom quick_reject before Aho-Corasick runs at all (segments are themselves capped at IngestTuning::DEFAULT_MAX_PERSIST_SEGMENT_BYTES, 64 KiB).

Rule pack composition

A "rule pack" is a versioned, named collection of detection rules. The default pack is builtin:core (Codex + Claude Code + Gemini + terminal runtime events). Custom packs are TOML files discovered in <config_dir>/patterns/ (override with [patterns] user_packs_dir) and in the workspace's .ft/patterns/, or listed explicitly as file:/path/to/pack.toml in [patterns] packs. Each rule has:

  • id (stable, machine-readable, prefix-namespaced; e.g., codex.usage.reached)
  • agent_type (codex / claude_code / gemini / wezterm / unknown; must align with the id prefix)
  • event_type and description
  • anchors (Bloom-prefilter + AC input; short literal strings the rule needs to see)
  • regex (optional; confirms the match, only run if anchors fire)
  • severity (info / warning / critical)

Linter checks

ft robot rules lint --fixtures --strict enforces:

  • Naming — IDs start with codex., claude_code., gemini., or wezterm.
  • Agent-type alignment — Rule ID prefix matches its agent_type field
  • Regex safety — warns on nested wildcards (potential ReDoS), excessive length (>500 chars), consecutive spaces
  • Fixture coverage — every rule has at least one corpus fixture