Skip to content

feat: add ModeClassifier hook — deterministic mode routing#840

Open
rikitikitavi2012-debug wants to merge 1 commit intodanielmiessler:mainfrom
rikitikitavi2012-debug:feat/mode-classifier-hook
Open

feat: add ModeClassifier hook — deterministic mode routing#840
rikitikitavi2012-debug wants to merge 1 commit intodanielmiessler:mainfrom
rikitikitavi2012-debug:feat/mode-classifier-hook

Conversation

@rikitikitavi2012-debug
Copy link
Copy Markdown

Summary

  • Adds ModeClassifier.hook.ts — a deterministic UserPromptSubmit hook that classifies every prompt into ALGORITHM or MINIMAL mode using regex patterns (< 20ms, zero API calls)
  • Fixes mode classification regression where ALGORITHM mode activates only ~9% of the time due to CLAUDE.md template attraction bias (the LLM pattern-matches to NATIVE format over semantic instructions)
  • Registers the hook as first in UserPromptSubmit chain in settings.json (before RatingCapture)

Problem

The current mode classification relies on the LLM reading CLAUDE.md instructions and choosing the correct mode. In practice, the LLM latches onto the NATIVE format template (which appears first and is simpler) and uses it ~91% of the time, even for complex tasks that should trigger ALGORITHM mode.

Solution

Remove the LLM from mode classification entirely. A deterministic regex hook runs before the AI sees the prompt:

  • 39 regex patterns match greetings, ratings, thanks, acknowledgments (EN + RU/Cyrillic) → routes to MINIMAL
  • Everything else → routes to ALGORITHM (the default for actual work)
  • The Algorithm's Complexity Gate in OBSERVE phase then handles intelligent downshifting to NATIVE for genuinely simple tasks

Design Decisions

  • Regex over LLM: Deterministic, < 20ms, zero cost, zero hallucination
  • Conservative MINIMAL matching: Only unambiguous patterns (anchored ^...$). "ok" matches but "ok, do X" doesn't
  • Position 0 in hook chain: Must run before other UserPromptSubmit hooks for correct mode injection
  • Fail-open: Any error → defaults to ALGORITHM (safe fallback)

Test plan

  • Verify hook runs on UserPromptSubmit event
  • Test English greetings → MINIMAL (hi, hello, hey, thanks, good morning)
  • Test Russian greetings → MINIMAL (привет, здравствуй, спасибо, доброе утро)
  • Test ratings → MINIMAL (9/10, 8.5, "7/10 норм")
  • Test complex prompts → ALGORITHM (any actual work request)
  • Test edge cases: "ok" → MINIMAL, "ok, deploy to prod" → ALGORITHM
  • Verify < 20ms execution time (no API calls, no file I/O)
  • Verify fail-open behavior on malformed input

🤖 Generated with Claude Code

Fixes mode classification regression where ALGORITHM mode activates
only ~9% of the time due to CLAUDE.md template attraction bias.

The hook runs as first UserPromptSubmit hook and classifies prompts
using deterministic regex patterns (< 20ms, zero API calls):

- Greetings, ratings, thanks, acks (EN + RU) → MINIMAL mode
- Everything else → ALGORITHM mode (enforced)

Includes 39 regex patterns covering:
- English greetings, thanks, acks
- Russian/Cyrillic equivalents (comprehensive)
- Numeric rating patterns (9/10, 8.5, 7/10 норм, etc.)
- Short positive feedback

The Algorithm's Complexity Gate in OBSERVE phase handles
intelligent downshifting to NATIVE for simple tasks.

Targets Releases/v4.0.1/ (current upstream release).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rikitikitavi2012-debug
Copy link
Copy Markdown
Author

Friendly ping — this PR adds a ModeClassifier hook that handles deterministic mode routing (MINIMAL/NATIVE/ALGORITHM) via regex before LLM evaluation, saving tokens on simple inputs like greetings and ratings.

Happy to address any feedback or adjust the approach. Let me know if there's anything blocking review.

@rikitikitavi2012-debug
Copy link
Copy Markdown
Author

Friendly ping — this PR adds a ModeClassifier hook that routes requests to the correct mode (Algorithm/Native/Minimal) via fast regex before LLM processing, reducing unnecessary LLM calls. Mergeable, no conflicts. Happy to adjust if needed.

@rikitikitavi2012-debug
Copy link
Copy Markdown
Author

Friendly ping — this PR has been open for a few days without review. Happy to address any feedback or make changes if needed. Let me know if there's anything blocking the merge.

DevenDucommun added a commit to DevenDucommun/Personal_AI_Infrastructure that referenced this pull request Mar 6, 2026
Algorithm v3.8.0: merge v3.6.0 cognitive scaffolding (self-interrogation,
constraint extraction, confidence tags, priority classification, coverage
map, QG2-QG7 gates) into v3.7.0. Split ISC methodology, capability
selection, and examples into dedicated files. Standard PRD skip. Session-
safe context recovery. Voice curls fire-and-forget.

Hooks: consolidate 4 terminal hooks → TerminalState.hook.ts. Add
ModeClassifier (deterministic mode pre-classification, PR danielmiessler#840) and
PostCompactRecovery (context re-injection after compaction, PR danielmiessler#799).

Portability: replace hardcoded join(homedir(),'.claude') with paiPath()
across 22 TS files (PR danielmiessler#873).

Docs: slim SKILLSYSTEM.md and THEHOOKSYSTEM.md to ~80 lines each; move
full content to PAI/dev/. Move MEMORYSYSTEM migration history to
MEMORY-CHANGELOG.md. Add TELOS/DIGEST.md template to loadAtStartup.
Clean CLAUDE.md MINIMAL format.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant