Class: Senren::Rails::AgentRulesWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/senren/rails/agent_rules_writer.rb

Overview

Generates a single source-of-truth rules file plus adapter files for Copilot, Cursor, Claude, and Codex.

Adapter files are marker-managed to avoid overwriting existing project instructions outside Senren's generated block.

Constant Summary collapse

START_MARKER =
'<!-- senren:agent:start -->'
END_MARKER =
'<!-- senren:agent:end -->'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry: Registry.load!, paths: HostPaths.new) ⇒ AgentRulesWriter

Returns a new instance of AgentRulesWriter.



20
21
22
23
# File 'lib/senren/rails/agent_rules_writer.rb', line 20

def initialize(registry: Registry.load!, paths: HostPaths.new)
  @registry = registry
  @paths    = paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



18
19
20
# File 'lib/senren/rails/agent_rules_writer.rb', line 18

def paths
  @paths
end

#registryObject (readonly)

Returns the value of attribute registry.



18
19
20
# File 'lib/senren/rails/agent_rules_writer.rb', line 18

def registry
  @registry
end

Instance Method Details

#assert_distinct_adapters!Object

Each adapter gets different content, so two of them resolving to the same file means the last write silently wins.

ln -s AGENTS.md CLAUDE.md is a normal way to keep one set of agent instructions, and now that in-repo symlinks are allowed it reaches here rather than being refused as an escape.

Public so ComponentInstaller can run it as a preflight. The first version of this check lived inside sync!, which runs after the copier, so senren:add failed with components already on disk and the ledger already written.

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
# File 'lib/senren/rails/agent_rules_writer.rb', line 48

def assert_distinct_adapters!
  collisions = adapter_targets.group_by { |path, _| SafeWrite.real_target(path) }
                              .select { |_, group| group.size > 1 }
  return if collisions.empty?

  raise ArgumentError, collision_message(collisions)
end

#sync!Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/senren/rails/agent_rules_writer.rb', line 25

def sync!
  assert_distinct_adapters!
  paths.ensure_agent_dirs!
  files = []
  files << write_full_file(paths.agent_rules_file, render_source_rules)
  files << write_adapter_file(paths.codex_agents_md, render_codex_adapter)
  files << write_adapter_file(paths.claude_md, render_claude_adapter)
  files << write_adapter_file(paths.copilot_instructions, render_copilot_adapter)
  files << write_adapter_file(paths.cursor_rule_file, render_cursor_adapter, prefix: cursor_frontmatter)
  files
end