Class: RosettAi::Composition::Lockfile

Inherits:
Object
  • Object
show all
Defined in:
lib/rosett_ai/composition/lockfile.rb

Overview

Manages the .rosett-ai-lock.yml lockfile for reproducible behaviour composition. Writes the exact composition result so that --frozen compilations can reproduce the same output.

Constant Summary collapse

FILENAME =
'.rosett-ai-lock.yml'

Instance Method Summary collapse

Constructor Details

#initialize(root:) ⇒ Lockfile

Returns a new instance of Lockfile.

Parameters:

  • root (Pathname)

    project root directory



18
19
20
# File 'lib/rosett_ai/composition/lockfile.rb', line 18

def initialize(root:)
  @root = Pathname.new(root)
end

Instance Method Details

#exist?Boolean

Returns true if the lockfile exists.

Returns:

  • (Boolean)

    true if the lockfile exists



28
29
30
# File 'lib/rosett_ai/composition/lockfile.rb', line 28

def exist?
  path.exist?
end

#fresh?(behaviour_files) ⇒ Boolean

Checks if behaviour files have changed since the lockfile was written.

Parameters:

  • behaviour_files (Array<String>)

    current behaviour file paths

Returns:

  • (Boolean)

    true if files match the lockfile



57
58
59
60
61
62
63
64
65
# File 'lib/rosett_ai/composition/lockfile.rb', line 57

def fresh?(behaviour_files)
  return false unless exist?

  data = read
  locked_checksums = data.fetch('behaviours', {})
  current_checksums = compute_checksums(behaviour_files)

  locked_checksums == current_checksums
end

#pathPathname

Returns full path to the lockfile.

Returns:

  • (Pathname)

    full path to the lockfile



23
24
25
# File 'lib/rosett_ai/composition/lockfile.rb', line 23

def path
  @root.join(FILENAME)
end

#readHash

Reads and parses the lockfile.

Returns:

  • (Hash)

    lockfile data

Raises:



47
48
49
50
51
# File 'lib/rosett_ai/composition/lockfile.rb', line 47

def read
  raise RosettAi::CompositionError, "Lockfile not found: #{path}" unless exist?

  RosettAi::YamlLoader.load_file(path.to_s)
end

#write(result, behaviours:) ⇒ Pathname

Writes the lockfile from a CompositionResult.

Parameters:

  • result (CompositionResult)

    composition result

  • behaviours (Array<Hash>)

    source behaviour metadata

Returns:

  • (Pathname)

    path to the written lockfile



37
38
39
40
41
# File 'lib/rosett_ai/composition/lockfile.rb', line 37

def write(result, behaviours:)
  data = build_lockfile_data(result, behaviours)
  path.write(YAML.dump(data))
  path
end