Class: RailsAiBridge::Fingerprinter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/fingerprinter.rb,
lib/rails_ai_bridge/fingerprinter/cached_snapshot.rb

Overview

Computes a SHA256 fingerprint of key application files to detect changes. Used by BaseTool to invalidate cached introspection when files change.

Defined Under Namespace

Classes: CachedSnapshot

Constant Summary collapse

WATCHED_FILES =
%w[
  db/schema.rb
  config/routes.rb
  config/database.yml
  Gemfile.lock
].freeze
WATCHED_DIRS =
%w[
  app/models
  app/controllers
  app/views
  app/jobs
  app/mailers
  app/channels
  app/javascript/controllers
  app/middleware
  config/initializers
  db/migrate
  lib/tasks
].freeze

Class Method Summary collapse

Class Method Details

.changed?(app, previous) ⇒ Boolean

Checks whether the application has changed since a previous snapshot.

Parameters:

  • app (Rails::Application)

    the Rails application

  • previous (String)

    previous hex digest from +snapshot+

Returns:

  • (Boolean)

    +true+ if the application has changed



69
70
71
# File 'lib/rails_ai_bridge/fingerprinter.rb', line 69

def changed?(app, previous)
  snapshot(app) != previous
end

.compute(app) ⇒ String

Alias for +snapshot+. Used internally for a consistent API surface.

Parameters:

  • app (Rails::Application)

    the Rails application

Returns:

  • (String)

    hex digest fingerprint



60
61
62
# File 'lib/rails_ai_bridge/fingerprinter.rb', line 60

def compute(app)
  snapshot(app)
end

.snapshot(app) ⇒ String

Computes a snapshot hash of file mtimes for watched files and directories.

Parameters:

  • app (Rails::Application)

    the Rails application

Returns:

  • (String)

    hex digest of the combined mtime fingerprint



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rails_ai_bridge/fingerprinter.rb', line 35

def snapshot(app)
  root = app.root.to_s
  digest = Digest::SHA256.new

  WATCHED_FILES.each do |file|
    path = File.join(root, file)
    digest.update(File.mtime(path).to_f.to_s) if File.exist?(path)
  end

  WATCHED_DIRS.each do |dir|
    full_dir = File.join(root, dir)
    next unless Dir.exist?(full_dir)

    Dir.glob(File.join(full_dir, '**/*.{rb,rake,js,ts,erb,haml,slim,yml}')).each do |path|
      digest.update(File.mtime(path).to_f.to_s)
    end
  end

  digest.hexdigest
end

.source_fingerprint(app) ⇒ String

Computes a short content-based fingerprint (12 hex chars) from schema and routes.

Parameters:

  • app (Rails::Application)

    the Rails application

Returns:

  • (String)

    12-character hex fingerprint



77
78
79
80
81
# File 'lib/rails_ai_bridge/fingerprinter.rb', line 77

def source_fingerprint(app)
  root = app.root
  paths = [schema_path(root), File.join(root, 'config/routes.rb')]
  Digest::SHA256.hexdigest(read_source_content(paths))[0...12]
end