rosett-ai 1.8.1 → 1.11.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +253 -0
- data/README.md +2 -2
- data/conf/schemas/rai_config_schema.json +10 -0
- data/lib/rosett_ai/compiler/backends/agents_md_backend.rb +10 -3
- data/lib/rosett_ai/compiler/backends/claude_backend.rb +11 -1
- data/lib/rosett_ai/compiler/compilation_pipeline.rb +25 -0
- data/lib/rosett_ai/compiler/lean_bundle.rb +150 -0
- data/lib/rosett_ai/compiler/lean_emitter.rb +76 -0
- data/lib/rosett_ai/desktop/gui_logger.rb +1 -1
- data/lib/rosett_ai/exit_codes.rb +2 -1
- data/lib/rosett_ai/init/mcp_registrar.rb +56 -19
- data/lib/rosett_ai/mcp/governance.rb +1 -0
- data/lib/rosett_ai/mcp/tools/compile_status_tool.rb +58 -13
- data/lib/rosett_ai/mcp/tools/config_compile_tool.rb +40 -7
- data/lib/rosett_ai/mcp/tools/optimize_tool.rb +73 -0
- data/lib/rosett_ai/optimize/corpus.rb +79 -0
- data/lib/rosett_ai/optimize/detector.rb +27 -0
- data/lib/rosett_ai/optimize/detectors/conflict.rb +50 -0
- data/lib/rosett_ai/optimize/detectors/overlap.rb +74 -0
- data/lib/rosett_ai/optimize/detectors/staleness.rb +61 -0
- data/lib/rosett_ai/optimize/detectors/token_cost.rb +44 -0
- data/lib/rosett_ai/optimize/finding.rb +36 -0
- data/lib/rosett_ai/optimize/proposal_writer.rb +54 -0
- data/lib/rosett_ai/optimize/report_writer.rb +72 -0
- data/lib/rosett_ai/optimize/runner.rb +38 -0
- data/lib/rosett_ai/optimize/thresholds.rb +52 -0
- data/lib/rosett_ai/optimize.rb +33 -0
- data/lib/rosett_ai/path_resolver.rb +25 -0
- data/lib/rosett_ai/plugins/registry.rb +45 -15
- data/lib/rosett_ai/provenance/entry.rb +1 -2
- data/lib/rosett_ai/rai_config.rb +3 -0
- data/lib/rosett_ai/rules/status_checker.rb +144 -0
- data/lib/rosett_ai/telemetry/json_lines_writer.rb +1 -1
- data/lib/rosett_ai/thor/cli.rb +7 -61
- data/lib/rosett_ai/thor/command_tree.rb +79 -0
- data/lib/rosett_ai/thor/tasks/compile.rb +137 -2
- data/lib/rosett_ai/thor/tasks/optimize.rb +103 -0
- data/lib/rosett_ai/thor/tasks/rules.rb +125 -0
- data/lib/rosett_ai/version.rb +1 -1
- data/lib/rosett_ai.rb +2 -0
- data/locales/en.yml +15 -0
- metadata +22 -3
|
@@ -7,54 +7,71 @@ require 'json'
|
|
|
7
7
|
|
|
8
8
|
module RosettAi
|
|
9
9
|
module Init
|
|
10
|
-
# Registers the rosett-ai MCP server in Claude Code
|
|
10
|
+
# Registers the rosett-ai MCP server in Claude Code's config file.
|
|
11
11
|
#
|
|
12
12
|
# Called by `raictl init --global` to enable AI client auto-discovery
|
|
13
13
|
# of rosett-ai tools via the MCP protocol (DES-RAI-MCP-002).
|
|
14
14
|
#
|
|
15
|
+
# Claude Code discovers MCP servers from the top-level `mcpServers` key
|
|
16
|
+
# of `~/.claude.json` — NOT from `~/.claude/settings.json` (which only
|
|
17
|
+
# holds permissions, env, and hooks). Both files happen to share the
|
|
18
|
+
# same flat-hash-with-an-`mcpServers`-key shape, so no format-specific
|
|
19
|
+
# branching is needed here beyond pointing at the right path.
|
|
20
|
+
#
|
|
15
21
|
# The registration is idempotent: if the rosett-ai entry already
|
|
16
|
-
# exists, no changes are made.
|
|
22
|
+
# exists, no changes are made. A legacy entry under the old `nncc`
|
|
23
|
+
# project name is removed when (re-)registering under `rosett-ai`.
|
|
17
24
|
#
|
|
18
25
|
# @author hugo
|
|
19
26
|
# @author claude
|
|
20
27
|
class McpRegistrar
|
|
21
|
-
# @return [String] MCP server name used in Claude Code
|
|
28
|
+
# @return [String] MCP server name used in Claude Code config.
|
|
22
29
|
MCP_SERVER_NAME = 'rosett-ai'
|
|
23
30
|
|
|
24
|
-
# @
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
# @return [Array<String>] legacy server names superseded by MCP_SERVER_NAME.
|
|
32
|
+
LEGACY_SERVER_NAMES = ['nncc'].freeze
|
|
33
|
+
|
|
34
|
+
# @param config_path [String] path to Claude Code's config file (~/.claude.json)
|
|
35
|
+
def initialize(config_path: nil)
|
|
36
|
+
@config_path = config_path || default_config_path
|
|
27
37
|
end
|
|
28
38
|
|
|
29
|
-
# Registers the rosett-ai MCP server in
|
|
39
|
+
# Registers the rosett-ai MCP server in the config file.
|
|
30
40
|
#
|
|
31
41
|
# @return [Hash] result with :action (:registered, :already_registered, :skipped)
|
|
32
42
|
# and :path
|
|
33
43
|
def register
|
|
34
|
-
return skip_result('Settings file not found') unless File.exist?(@
|
|
44
|
+
return skip_result('Settings file not found') unless File.exist?(@config_path)
|
|
35
45
|
|
|
36
46
|
settings = read_settings
|
|
37
|
-
|
|
47
|
+
legacy_removed = legacy_entries?(settings)
|
|
48
|
+
remove_legacy_entries!(settings) if legacy_removed
|
|
49
|
+
|
|
50
|
+
if registered?(settings)
|
|
51
|
+
write_settings(settings) if legacy_removed
|
|
52
|
+
return already_registered_result
|
|
53
|
+
end
|
|
38
54
|
|
|
39
55
|
settings['mcpServers'] ||= {}
|
|
40
56
|
settings['mcpServers'][MCP_SERVER_NAME] = server_entry
|
|
41
57
|
write_settings(settings)
|
|
42
58
|
|
|
43
|
-
{ action: :registered, path: @
|
|
59
|
+
{ action: :registered, path: @config_path }
|
|
44
60
|
rescue JSON::ParserError => e
|
|
45
61
|
skip_result("Invalid JSON in settings: #{e.message}")
|
|
46
62
|
end
|
|
47
63
|
|
|
48
64
|
private
|
|
49
65
|
|
|
50
|
-
# @return [String] default path to Claude Code
|
|
51
|
-
def
|
|
52
|
-
File.join(Dir.home, '.claude
|
|
66
|
+
# @return [String] default path to Claude Code's config file
|
|
67
|
+
def default_config_path
|
|
68
|
+
File.join(Dir.home, '.claude.json')
|
|
53
69
|
end
|
|
54
70
|
|
|
55
|
-
# @return [Hash] the MCP server entry for
|
|
71
|
+
# @return [Hash] the MCP server entry for the config file
|
|
56
72
|
def server_entry
|
|
57
73
|
{
|
|
74
|
+
'type' => 'stdio',
|
|
58
75
|
'command' => resolve_command,
|
|
59
76
|
'args' => ['mcp', 'serve']
|
|
60
77
|
}
|
|
@@ -84,26 +101,46 @@ module RosettAi
|
|
|
84
101
|
settings.dig('mcpServers', MCP_SERVER_NAME).is_a?(Hash)
|
|
85
102
|
end
|
|
86
103
|
|
|
87
|
-
# @
|
|
104
|
+
# @param settings [Hash] parsed settings
|
|
105
|
+
# @return [Boolean] true if a legacy-named MCP server entry is present
|
|
106
|
+
def legacy_entries?(settings)
|
|
107
|
+
servers = settings['mcpServers']
|
|
108
|
+
return false unless servers.is_a?(Hash)
|
|
109
|
+
|
|
110
|
+
LEGACY_SERVER_NAMES.any? { |name| servers.key?(name) }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Removes any legacy-named MCP server entries in place.
|
|
114
|
+
#
|
|
115
|
+
# @param settings [Hash] parsed settings
|
|
116
|
+
# @return [void]
|
|
117
|
+
def remove_legacy_entries!(settings)
|
|
118
|
+
servers = settings['mcpServers']
|
|
119
|
+
return unless servers.is_a?(Hash)
|
|
120
|
+
|
|
121
|
+
LEGACY_SERVER_NAMES.each { |name| servers.delete(name) }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# @return [Hash] parsed settings file
|
|
88
125
|
def read_settings
|
|
89
|
-
JSON.parse(File.read(@
|
|
126
|
+
JSON.parse(File.read(@config_path))
|
|
90
127
|
end
|
|
91
128
|
|
|
92
129
|
# @param settings [Hash] settings to write
|
|
93
130
|
# @return [void]
|
|
94
131
|
def write_settings(settings)
|
|
95
|
-
File.write(@
|
|
132
|
+
File.write(@config_path, "#{JSON.pretty_generate(settings)}\n")
|
|
96
133
|
end
|
|
97
134
|
|
|
98
135
|
# @return [Hash] result for already registered case
|
|
99
136
|
def already_registered_result
|
|
100
|
-
{ action: :already_registered, path: @
|
|
137
|
+
{ action: :already_registered, path: @config_path }
|
|
101
138
|
end
|
|
102
139
|
|
|
103
140
|
# @param reason [String] why registration was skipped
|
|
104
141
|
# @return [Hash] result for skip case
|
|
105
142
|
def skip_result(reason)
|
|
106
|
-
{ action: :skipped, path: @
|
|
143
|
+
{ action: :skipped, path: @config_path, reason: reason }
|
|
107
144
|
end
|
|
108
145
|
end
|
|
109
146
|
end
|
|
@@ -78,11 +78,31 @@ module RosettAi
|
|
|
78
78
|
# @param scope [String, nil]
|
|
79
79
|
# @return [Array<Pathname>]
|
|
80
80
|
def collect_source_files(source_dir, scope)
|
|
81
|
+
validate_scope!(source_dir, scope) if scope
|
|
81
82
|
categories = scope ? [scope] : ['behaviour', 'design']
|
|
82
|
-
categories.flat_map
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
categories.flat_map { |cat| yaml_files_in(source_dir, cat) }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# @param source_dir [Pathname]
|
|
87
|
+
# @param category [String]
|
|
88
|
+
# @return [Array<Pathname>]
|
|
89
|
+
def yaml_files_in(source_dir, category)
|
|
90
|
+
dir = source_dir.join(category)
|
|
91
|
+
dir.directory? ? dir.glob('*.yml').sort : []
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# An explicit scope must be a real category directory. Without this,
|
|
95
|
+
# a typo'd or non-category scope silently matches zero files and
|
|
96
|
+
# reports a cheerful all-clear instead of the error it actually is.
|
|
97
|
+
#
|
|
98
|
+
# @param source_dir [Pathname]
|
|
99
|
+
# @param scope [String]
|
|
100
|
+
# @raise [ArgumentError] when scope does not match a category directory
|
|
101
|
+
# @return [void]
|
|
102
|
+
def validate_scope!(source_dir, scope)
|
|
103
|
+
return if source_dir.join(scope).directory?
|
|
104
|
+
|
|
105
|
+
raise ArgumentError, "Unknown scope: #{scope.inspect} (valid: behaviour, design)"
|
|
86
106
|
end
|
|
87
107
|
|
|
88
108
|
# @param source_path [Pathname]
|
|
@@ -94,17 +114,15 @@ module RosettAi
|
|
|
94
114
|
compiled_name = "#{category}-#{basename}.md"
|
|
95
115
|
compiled_path = compiled_dir.join(compiled_name)
|
|
96
116
|
|
|
97
|
-
source_checksum = file_checksum(source_path)
|
|
98
117
|
compiled_exists = compiled_path.exist?
|
|
99
|
-
compiled_checksum = compiled_exists ? file_checksum(compiled_path) : nil
|
|
100
118
|
|
|
101
119
|
{
|
|
102
120
|
source: source_path.to_s,
|
|
103
121
|
compiled: compiled_path.to_s,
|
|
104
|
-
source_checksum:
|
|
105
|
-
compiled_checksum:
|
|
122
|
+
source_checksum: file_checksum(source_path),
|
|
123
|
+
compiled_checksum: compiled_exists ? file_checksum(compiled_path) : nil,
|
|
106
124
|
compiled_exists: compiled_exists,
|
|
107
|
-
stale:
|
|
125
|
+
stale: stale?(source_path, compiled_path, category)
|
|
108
126
|
}
|
|
109
127
|
end
|
|
110
128
|
|
|
@@ -114,11 +132,38 @@ module RosettAi
|
|
|
114
132
|
Digest::SHA256.hexdigest(path.read)
|
|
115
133
|
end
|
|
116
134
|
|
|
117
|
-
#
|
|
118
|
-
#
|
|
135
|
+
# Content-based staleness verdict: renders the source through the
|
|
136
|
+
# same engine backend the compiler uses and compares against the
|
|
137
|
+
# compiled file's checksum. mtime is consulted only as a cheap
|
|
138
|
+
# pre-filter to skip rendering when it is impossible for the
|
|
139
|
+
# content to have drifted — the compiler leaves a compiled file's
|
|
140
|
+
# mtime untouched on a no-op write, so a source edit that doesn't
|
|
141
|
+
# change the rendered output would otherwise read as permanently
|
|
142
|
+
# stale even after a successful compile.
|
|
143
|
+
#
|
|
144
|
+
# @param source_path [Pathname]
|
|
145
|
+
# @param compiled_path [Pathname]
|
|
146
|
+
# @param category [String]
|
|
119
147
|
# @return [Boolean]
|
|
120
|
-
def
|
|
121
|
-
|
|
148
|
+
def stale?(source_path, compiled_path, category)
|
|
149
|
+
return true unless compiled_path.exist?
|
|
150
|
+
return false if source_path.mtime <= compiled_path.mtime
|
|
151
|
+
|
|
152
|
+
rendered_checksum(source_path, category) != file_checksum(compiled_path)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# @param source_path [Pathname]
|
|
156
|
+
# @param category [String]
|
|
157
|
+
# @return [String] SHA-256 hex digest of the rendered output
|
|
158
|
+
def rendered_checksum(source_path, category)
|
|
159
|
+
data = RosettAi::YamlLoader.load_file(source_path.to_s)
|
|
160
|
+
rendered = backend.render(data.merge('category' => category))
|
|
161
|
+
Digest::SHA256.hexdigest(rendered)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# @return [RosettAi::Compiler::Backend] the default-engine rendering backend
|
|
165
|
+
def backend
|
|
166
|
+
@backend ||= RosettAi::Compiler::Backend.for(RosettAi.rai_config.default_engine)
|
|
122
167
|
end
|
|
123
168
|
end
|
|
124
169
|
end
|
|
@@ -53,15 +53,13 @@ module RosettAi
|
|
|
53
53
|
# @param engine [String] target engine
|
|
54
54
|
# @return [Hash] compilation results
|
|
55
55
|
def call(simulate: true, scope: nil, engine: 'claude')
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
unless compiler_class
|
|
59
|
-
return ResponseHelper.error(
|
|
60
|
-
"Engine '#{engine}' has no config compiler. " \
|
|
61
|
-
'Install an engine plugin to enable config compilation.'
|
|
62
|
-
)
|
|
56
|
+
unless RosettAi::Plugins::Registry.registered?(:engine, engine)
|
|
57
|
+
return ResponseHelper.error(engine_not_found_message(engine))
|
|
63
58
|
end
|
|
64
59
|
|
|
60
|
+
compiler_class = resolve_compiler_class(engine)
|
|
61
|
+
return missing_compiler_error(engine) unless compiler_class
|
|
62
|
+
|
|
65
63
|
results = compiler_class.new(simulate: simulate).compile_all(scope: scope)
|
|
66
64
|
{ simulate: simulate, scope: scope, engine: engine,
|
|
67
65
|
status: simulate ? 'dry-run' : 'compiled',
|
|
@@ -69,6 +67,41 @@ module RosettAi
|
|
|
69
67
|
rescue StandardError => e
|
|
70
68
|
ResponseHelper.error("Config compile failed: #{e.message}")
|
|
71
69
|
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
# @param engine [String] requested engine name
|
|
74
|
+
# @return [Class, nil] the engine's config compiler class, if any
|
|
75
|
+
def resolve_compiler_class(engine)
|
|
76
|
+
RosettAi::Plugins::Registry.plugin_module(:engine, engine)&.config_compiler_class
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# @param engine [String] requested engine name
|
|
80
|
+
# @return [Hash] an MCP error response
|
|
81
|
+
def missing_compiler_error(engine)
|
|
82
|
+
ResponseHelper.error(
|
|
83
|
+
"Engine '#{engine}' has no config compiler. " \
|
|
84
|
+
'Install an engine plugin to enable config compilation.'
|
|
85
|
+
)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Builds an actionable error message when the requested engine has
|
|
89
|
+
# no registered plugin at all, mirroring the CLI's
|
|
90
|
+
# RosettAi::Thor::Tasks::Config#engine_hint wording so both surfaces
|
|
91
|
+
# give the same guidance instead of leaking the registry's raw
|
|
92
|
+
# PluginNotFoundError text.
|
|
93
|
+
#
|
|
94
|
+
# @param engine [String] requested engine name
|
|
95
|
+
# @return [String] human-readable error message
|
|
96
|
+
def engine_not_found_message(engine)
|
|
97
|
+
available = RosettAi::Plugins::Registry.available(:engine)
|
|
98
|
+
hint = if available.empty?
|
|
99
|
+
"No engine plugins installed. Install with: apt install rosett-ai-engine-#{engine.tr('_', '-')}"
|
|
100
|
+
else
|
|
101
|
+
"Available engines: #{available.join(', ')}"
|
|
102
|
+
end
|
|
103
|
+
"Engine '#{engine}' not found. #{hint}"
|
|
104
|
+
end
|
|
72
105
|
end
|
|
73
106
|
end
|
|
74
107
|
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-only
|
|
4
|
+
# Copyright (C) 2026 Hugo Antonio Sepulveda Manriquez / NeatNerds
|
|
5
|
+
|
|
6
|
+
require 'time'
|
|
7
|
+
|
|
8
|
+
module RosettAi
|
|
9
|
+
module Mcp
|
|
10
|
+
module Tools
|
|
11
|
+
# MCP tool: run rai_optimize analysis.
|
|
12
|
+
#
|
|
13
|
+
# Mechanical (no LLM) detector suite over the behaviour corpus.
|
|
14
|
+
# Read-only — never modifies files. `propose: true` returns
|
|
15
|
+
# proposal patches inline; on-disk writes are the CLI's job.
|
|
16
|
+
#
|
|
17
|
+
# @author hugo
|
|
18
|
+
# @author claude
|
|
19
|
+
class OptimizeTool
|
|
20
|
+
TOOL_NAME = 'rai_optimize'
|
|
21
|
+
DESCRIPTION = 'Run mechanical self-optimizer over the rai behaviour corpus (4 detectors)'
|
|
22
|
+
|
|
23
|
+
ANNOTATIONS = {
|
|
24
|
+
'readOnlyHint' => true,
|
|
25
|
+
'destructiveHint' => false,
|
|
26
|
+
'idempotentHint' => true,
|
|
27
|
+
'openWorldHint' => false
|
|
28
|
+
}.freeze
|
|
29
|
+
|
|
30
|
+
INPUT_SCHEMA = {
|
|
31
|
+
type: 'object',
|
|
32
|
+
properties: {
|
|
33
|
+
behaviour_dir: { type: 'string',
|
|
34
|
+
description: 'Behaviour source dir (default: XDG rai_conf_dir/behaviour)' },
|
|
35
|
+
propose: { type: 'boolean', description: 'Include per-behaviour proposal patches (default: false)' }
|
|
36
|
+
}
|
|
37
|
+
}.freeze
|
|
38
|
+
|
|
39
|
+
# @param behaviour_dir [String, nil]
|
|
40
|
+
# @param propose [Boolean]
|
|
41
|
+
# @return [Hash]
|
|
42
|
+
def call(behaviour_dir: nil, propose: false)
|
|
43
|
+
dir = behaviour_dir || RosettAi.paths.rai_conf_dir.join('behaviour').to_s
|
|
44
|
+
findings = RosettAi::Optimize::Runner.new(
|
|
45
|
+
corpus: RosettAi::Optimize::Corpus.load(dir),
|
|
46
|
+
thresholds: RosettAi::Optimize::Thresholds.defaults
|
|
47
|
+
).run
|
|
48
|
+
build_response(findings, propose)
|
|
49
|
+
rescue StandardError => e
|
|
50
|
+
{ status: 'error', message: "rai_optimize failed: #{e.message}" }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def build_response(findings, propose)
|
|
56
|
+
{
|
|
57
|
+
status: findings.empty? ? 'clean' : 'findings',
|
|
58
|
+
generated_at: Time.now.utc.iso8601,
|
|
59
|
+
total: findings.size,
|
|
60
|
+
findings: findings.map(&:to_h),
|
|
61
|
+
proposals: propose ? proposals(findings) : nil
|
|
62
|
+
}.compact
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def proposals(findings)
|
|
66
|
+
findings.group_by(&:behaviour).map do |behaviour, items|
|
|
67
|
+
{ 'behaviour' => behaviour, 'proposals' => items.map(&:to_h) }
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-only
|
|
4
|
+
# Copyright (C) 2026 Hugo Antonio Sepulveda Manriquez / NeatNerds
|
|
5
|
+
|
|
6
|
+
require 'yaml'
|
|
7
|
+
require 'pathname'
|
|
8
|
+
|
|
9
|
+
module RosettAi
|
|
10
|
+
module Optimize
|
|
11
|
+
# In-memory view of the behaviour corpus for detector consumption.
|
|
12
|
+
#
|
|
13
|
+
# Loads each behaviour_*.yml (or *.yml) file in the directory into a
|
|
14
|
+
# normalised {Rule} list. Malformed files are skipped with a warning
|
|
15
|
+
# rather than failing the whole run — detectors should work on best-
|
|
16
|
+
# effort available data.
|
|
17
|
+
class Corpus
|
|
18
|
+
# Normalised rule view — one per rule entry in a behaviour file.
|
|
19
|
+
#
|
|
20
|
+
# @!attribute [r] behaviour File basename without .yml
|
|
21
|
+
# @!attribute [r] id Rule id string (e.g. "rule_007")
|
|
22
|
+
# @!attribute [r] description Rule description text
|
|
23
|
+
# @!attribute [r] priority Integer priority (0-100)
|
|
24
|
+
# @!attribute [r] source_bytes Byte length of the description
|
|
25
|
+
Rule = Data.define(:behaviour, :id, :description, :priority, :source_bytes)
|
|
26
|
+
|
|
27
|
+
attr_reader :rules, :behaviours
|
|
28
|
+
|
|
29
|
+
# @param rules [Array<Rule>]
|
|
30
|
+
# @param behaviours [Array<String>] file basenames
|
|
31
|
+
def initialize(rules:, behaviours:)
|
|
32
|
+
@rules = rules
|
|
33
|
+
@behaviours = behaviours
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Load a corpus from a directory of behaviour YAML files.
|
|
37
|
+
#
|
|
38
|
+
# @param dir [String, Pathname]
|
|
39
|
+
# @return [Corpus]
|
|
40
|
+
def self.load(dir)
|
|
41
|
+
path = Pathname.new(dir)
|
|
42
|
+
return new(rules: [], behaviours: []) unless path.directory?
|
|
43
|
+
|
|
44
|
+
files = path.glob('*.yml').sort
|
|
45
|
+
rules = []
|
|
46
|
+
behaviours = []
|
|
47
|
+
files.each do |file|
|
|
48
|
+
data = safe_load(file)
|
|
49
|
+
next unless data.is_a?(Hash)
|
|
50
|
+
|
|
51
|
+
behaviour_name = file.basename('.yml').to_s
|
|
52
|
+
behaviours << behaviour_name
|
|
53
|
+
Array(data['rules']).each { |r| rules << build_rule(behaviour_name, r) }
|
|
54
|
+
end
|
|
55
|
+
new(rules: rules, behaviours: behaviours)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# @!visibility private
|
|
59
|
+
def self.safe_load(file)
|
|
60
|
+
YAML.safe_load_file(file.to_s, permitted_classes: [Date, Time])
|
|
61
|
+
rescue Psych::SyntaxError => e
|
|
62
|
+
warn "[rai_optimize] skipping malformed YAML #{file}: #{e.message}"
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# @!visibility private
|
|
67
|
+
def self.build_rule(behaviour_name, raw)
|
|
68
|
+
description = raw['description'].to_s
|
|
69
|
+
Rule.new(
|
|
70
|
+
behaviour: behaviour_name,
|
|
71
|
+
id: raw['id'].to_s,
|
|
72
|
+
description: description,
|
|
73
|
+
priority: raw['priority'].to_i,
|
|
74
|
+
source_bytes: description.bytesize
|
|
75
|
+
)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-only
|
|
4
|
+
# Copyright (C) 2026 Hugo Antonio Sepulveda Manriquez / NeatNerds
|
|
5
|
+
|
|
6
|
+
module RosettAi
|
|
7
|
+
module Optimize
|
|
8
|
+
# Base class for mechanical detectors.
|
|
9
|
+
#
|
|
10
|
+
# Subclasses implement {#call} returning an Array<{Finding}>.
|
|
11
|
+
class Detector
|
|
12
|
+
attr_reader :corpus, :thresholds
|
|
13
|
+
|
|
14
|
+
# @param corpus [Corpus]
|
|
15
|
+
# @param thresholds [Thresholds]
|
|
16
|
+
def initialize(corpus:, thresholds:)
|
|
17
|
+
@corpus = corpus
|
|
18
|
+
@thresholds = thresholds
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# @return [Array<Finding>]
|
|
22
|
+
def call
|
|
23
|
+
raise NotImplementedError, "#{self.class} must implement #call"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-only
|
|
4
|
+
# Copyright (C) 2026 Hugo Antonio Sepulveda Manriquez / NeatNerds
|
|
5
|
+
|
|
6
|
+
module RosettAi
|
|
7
|
+
module Optimize
|
|
8
|
+
module Detectors
|
|
9
|
+
# Priority-inflation heuristic.
|
|
10
|
+
#
|
|
11
|
+
# Flags rules at or above {Thresholds#inflated_priority_min} that have
|
|
12
|
+
# neither a cited recurrence marker (an "On YYYY-MM-DD ..." incident
|
|
13
|
+
# line) nor an explicit "why" line. Such rules commonly represent
|
|
14
|
+
# aspirational escalations rather than earned high-priority mandates
|
|
15
|
+
# — see feedback-priority-inflation-discipline.md.
|
|
16
|
+
class Conflict < Detector
|
|
17
|
+
RECURRENCE_MARKER = /On\s+\d{4}-\d{2}-\d{2}/
|
|
18
|
+
WHY_MARKER = /(^|\n)\s*(Why|Rationale|Evidence):/i
|
|
19
|
+
private_constant :RECURRENCE_MARKER, :WHY_MARKER
|
|
20
|
+
|
|
21
|
+
# @return [Array<Finding>]
|
|
22
|
+
def call
|
|
23
|
+
corpus.rules.filter_map do |rule|
|
|
24
|
+
next if rule.priority < thresholds.inflated_priority_min
|
|
25
|
+
next if cited?(rule.description)
|
|
26
|
+
|
|
27
|
+
build_finding(rule)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def cited?(description)
|
|
34
|
+
description.match?(RECURRENCE_MARKER) || description.match?(WHY_MARKER)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def build_finding(rule)
|
|
38
|
+
Finding.new(
|
|
39
|
+
detector: :conflict,
|
|
40
|
+
severity: :medium,
|
|
41
|
+
behaviour: rule.behaviour,
|
|
42
|
+
rule_id: rule.id,
|
|
43
|
+
summary: "Priority #{rule.priority} without cited recurrence or Why: marker",
|
|
44
|
+
details: { priority: rule.priority }
|
|
45
|
+
)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-only
|
|
4
|
+
# Copyright (C) 2026 Hugo Antonio Sepulveda Manriquez / NeatNerds
|
|
5
|
+
|
|
6
|
+
module RosettAi
|
|
7
|
+
module Optimize
|
|
8
|
+
module Detectors
|
|
9
|
+
# Pairwise Jaccard similarity on rule-description tokens.
|
|
10
|
+
#
|
|
11
|
+
# Rule pairs whose Jaccard score meets or exceeds
|
|
12
|
+
# {Thresholds#jaccard_min} are flagged as candidate duplicates.
|
|
13
|
+
# Tokens are lowercased word characters length >= 3; ubiquitous
|
|
14
|
+
# stopwords are stripped to reduce false positives on function words.
|
|
15
|
+
class Overlap < Detector
|
|
16
|
+
STOPWORDS = [
|
|
17
|
+
'the', 'and', 'for', 'with', 'from', 'that', 'this', 'must',
|
|
18
|
+
'have', 'been', 'will', 'they', 'them', 'when', 'where', 'which',
|
|
19
|
+
'their', 'there', 'than', 'then', 'into', 'upon', 'over', 'such',
|
|
20
|
+
'more', 'also', 'each', 'only', 'very', 'just', 'some', 'like',
|
|
21
|
+
'other', 'same', 'about'
|
|
22
|
+
].freeze
|
|
23
|
+
private_constant :STOPWORDS
|
|
24
|
+
|
|
25
|
+
# @return [Array<Finding>]
|
|
26
|
+
def call
|
|
27
|
+
tokens_by_rule = corpus.rules.map { |r| [r, tokenise(r.description)] }
|
|
28
|
+
findings = []
|
|
29
|
+
tokens_by_rule.each_with_index do |(rule_a, tokens_a), i|
|
|
30
|
+
tokens_by_rule[(i + 1)..].each do |(rule_b, tokens_b)|
|
|
31
|
+
score = jaccard(tokens_a, tokens_b)
|
|
32
|
+
next if score < thresholds.jaccard_min
|
|
33
|
+
|
|
34
|
+
findings << build_finding(rule_a, rule_b, score)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
findings
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def tokenise(text)
|
|
43
|
+
text.downcase.scan(/[a-z][a-z_]{2,}/).to_set - STOPWORDS
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def jaccard(set_a, set_b)
|
|
47
|
+
return 0.0 if set_a.empty? && set_b.empty?
|
|
48
|
+
|
|
49
|
+
intersection = (set_a & set_b).size
|
|
50
|
+
union = (set_a | set_b).size
|
|
51
|
+
union.zero? ? 0.0 : intersection.to_f / union
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def build_finding(rule_a, rule_b, score)
|
|
55
|
+
Finding.new(
|
|
56
|
+
detector: :overlap,
|
|
57
|
+
severity: score >= 0.7 ? :high : :medium,
|
|
58
|
+
behaviour: rule_a.behaviour,
|
|
59
|
+
rule_id: rule_a.id,
|
|
60
|
+
summary: format(
|
|
61
|
+
'Overlaps %<b>s/%<id>s (Jaccard %<score>.2f)',
|
|
62
|
+
b: rule_b.behaviour, id: rule_b.id, score: score
|
|
63
|
+
),
|
|
64
|
+
details: {
|
|
65
|
+
peer_behaviour: rule_b.behaviour,
|
|
66
|
+
peer_rule_id: rule_b.id,
|
|
67
|
+
jaccard: score.round(3)
|
|
68
|
+
}
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-License-Identifier: GPL-3.0-only
|
|
4
|
+
# Copyright (C) 2026 Hugo Antonio Sepulveda Manriquez / NeatNerds
|
|
5
|
+
|
|
6
|
+
require 'yaml'
|
|
7
|
+
|
|
8
|
+
module RosettAi
|
|
9
|
+
module Optimize
|
|
10
|
+
module Detectors
|
|
11
|
+
# Flags rules whose description references retired substrate terms.
|
|
12
|
+
#
|
|
13
|
+
# Vocabulary is loaded from `conf/rai-optimize/substrate-vocabulary.yml`
|
|
14
|
+
# in the rosett-ai package. Absorbs the scruffy staleness facet.
|
|
15
|
+
class Staleness < Detector
|
|
16
|
+
DEFAULT_VOCABULARY_PATH = File.expand_path(
|
|
17
|
+
'../../../../conf/rai-optimize/substrate-vocabulary.yml', __dir__
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
# @param vocabulary_path [String, Pathname, nil] override for tests
|
|
21
|
+
def initialize(corpus:, thresholds:, vocabulary_path: nil)
|
|
22
|
+
super(corpus: corpus, thresholds: thresholds)
|
|
23
|
+
@vocabulary_path = vocabulary_path || DEFAULT_VOCABULARY_PATH
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @return [Array<Finding>]
|
|
27
|
+
def call
|
|
28
|
+
retired = load_retired_terms
|
|
29
|
+
return [] if retired.empty?
|
|
30
|
+
|
|
31
|
+
corpus.rules.filter_map do |rule|
|
|
32
|
+
hits = retired.select { |term| rule.description.downcase.include?(term) }
|
|
33
|
+
next if hits.empty?
|
|
34
|
+
|
|
35
|
+
build_finding(rule, hits)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def load_retired_terms
|
|
42
|
+
return [] unless File.exist?(@vocabulary_path)
|
|
43
|
+
|
|
44
|
+
raw = YAML.safe_load_file(@vocabulary_path) || {}
|
|
45
|
+
Array(raw['retired']).map { |term| term.to_s.downcase }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def build_finding(rule, hits)
|
|
49
|
+
Finding.new(
|
|
50
|
+
detector: :staleness,
|
|
51
|
+
severity: :medium,
|
|
52
|
+
behaviour: rule.behaviour,
|
|
53
|
+
rule_id: rule.id,
|
|
54
|
+
summary: "References retired substrate term(s): #{hits.join(', ')}",
|
|
55
|
+
details: { retired_terms: hits }
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|