Class: RosettAi::Mcp::Enforcement::Validator

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

Overview

Validates enforcement blocks in behaviour rules.

Checks each rule's enforcement block for correctness:

  • pattern present and compilable as regex
  • pattern not degenerate (would match everything)
  • applies_to present and non-empty
  • action is a valid enum value

Invalid enforceable rules are downgraded to advisory with warnings. Rules without enforcement blocks are skipped (backward compatible).

Examples:

validator = Validator.new
result = validator.validate(behaviour_data)
result[:valid]       # => [{ rule_id: "rule_001", ... }]
result[:downgraded]  # => [{ rule_id: "rule_002", reason: "..." }]
result[:skipped]     # => [{ rule_id: "rule_003" }]

Author:

  • hugo

  • claude

Defined Under Namespace

Classes: RuleContext

Constant Summary collapse

VALID_TYPES =
['enforceable', 'advisory', 'informational'].freeze
VALID_ACTIONS =
['block', 'warn', 'log'].freeze
DEGENERATE_PATTERNS =

Patterns that match everything or nearly everything — unsafe for hooks.

[
  /\A\.\*\z/,       # .*
  /\A\.\+\z/,       # .+
  /\A\\s\*\z/,      # \s*
  /\A\\S\*\z/,      # \S*
  /\A\\s\+\z/,      # \s+
  /\A\\S\+\z/,      # \S+
  /\A\.\*\?\z/,     # .*?
  /\A\.\+\?\z/,     # .+?
  /\A\(\.\*\)\z/,   # (.*)
  /\A\(\.\+\)\z/    # (.+)
].freeze

Instance Method Summary collapse

Instance Method Details

#validate(behaviour) ⇒ Hash

Validates all enforcement blocks in a behaviour.

Parameters:

  • behaviour (Hash)

    parsed behaviour YAML data

Returns:

  • (Hash)

    result with :valid, :downgraded, :skipped, :errors keys



57
58
59
60
61
62
63
64
65
66
# File 'lib/rosett_ai/mcp/enforcement/validator.rb', line 57

def validate(behaviour)
  @result = { valid: [], downgraded: [], skipped: [], errors: [] }
  rules = behaviour['rules']
  return @result unless rules.is_a?(Array)

  behaviour_name = behaviour['name'].to_s
  rules.each { |rule| validate_rule(rule, behaviour_name) }

  @result
end