Class: RosettAi::Mcp::Enforcement::HookGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rosett_ai/mcp/enforcement/hook_generator.rb

Overview

Generates self-contained Ruby PreToolUse hook scripts from validated enforcement rules.

Generated scripts are standalone — they require only Ruby stdlib (json) and have zero runtime dependency on rosett-ai. Each script includes:

  • YARD documentation
  • Human-readable header with generation metadata
  • AI client instructions comment block
  • Traceability (source behaviour, rule ID, version, generator version)
  • Enforcement logic: regex match on tool input, block/warn/log action

Examples:

Generate a hook script for validated rules

generator = HookGenerator.new
script = generator.generate(validated_rules, behaviour_name: 'session_optimization',
                                             behaviour_version: '1.0.0')

Author:

  • hugo

  • claude

Instance Method Summary collapse

Instance Method Details

#generate(rules, behaviour_name:, behaviour_version:) ⇒ String

Generates a complete PreToolUse hook script from validated rules.

Parameters:

  • rules (Array<Hash>)

    validated rules from Validator, each with :rule_id, :behaviour, :pattern, :applies_to, :action, :message, :priority, :description

  • behaviour_name (String)

    source behaviour name

  • behaviour_version (String)

    source behaviour version

Returns:

  • (String)

    complete Ruby hook script



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rosett_ai/mcp/enforcement/hook_generator.rb', line 38

def generate(rules, behaviour_name:, behaviour_version:)
  timestamp = Time.now.utc.iso8601
  parts = []
  parts << shebang_and_magic
  parts << yard_documentation(rules)
  parts << traceability_header(behaviour_name, behaviour_version, rules, timestamp)
  parts << ai_client_instructions
  parts << rule_constant(rules)
  parts << enforcement_body
  parts.join("\n")
end