Class: Ask::Tools::Shell::FileLedger

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/tools/shell/file_ledger.rb

Overview

Records what Read has shown of each file, so Write can refuse to destroy content the model never saw. Entries are validated against the file's current mtime/size — a changed file invalidates its entry, so a stale ledger can never block a write it shouldn't.

Class-level on purpose: a partial view is a fact about the world, not about one tool instance (agent sessions new up fresh tool instances, and the invariant must survive across them).

Defined Under Namespace

Classes: Entry

Class Method Summary collapse

Class Method Details

.entry_for(path) ⇒ Object

The entry for a path, or nil when the file changed since the read.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ask/tools/shell/file_ledger.rb', line 43

def entry_for(path)
  path = File.expand_path(path)
  @mutex.synchronize do
    entry = @entries[path]
    next nil unless entry

    current = File.stat(path)
    (current.mtime == entry.mtime && current.size == entry.size) ? entry : nil
  end
rescue Errno::ENOENT
  nil
end

.partially_seen?(path) ⇒ Boolean

True when the file was only partially read and hasn't changed since. Write consults this before overwriting.

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/ask/tools/shell/file_ledger.rb', line 58

def partially_seen?(path)
  entry = entry_for(path)
  !entry.nil? && entry.partial
end

.record(path, partial:, lines_seen:) ⇒ Object

Record what a read showed of a file.

Parameters:

  • partial (Boolean)

    true when the view was clamped/truncated

  • lines_seen (Array(Integer, Integer))

    [start, stop) line indices shown, 0-indexed



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ask/tools/shell/file_ledger.rb', line 27

def record(path, partial:, lines_seen:)
  path = File.expand_path(path)
  @mutex.synchronize do
    @entries[path] = Entry.new(
      path: path,
      mtime: File.mtime(path),
      size: File.size(path),
      partial: partial,
      lines_seen: lines_seen
    )
  end
rescue Errno::ENOENT
  nil # file vanished mid-read; nothing to record
end

.reset!Object



63
64
65
# File 'lib/ask/tools/shell/file_ledger.rb', line 63

def reset!
  @mutex.synchronize { @entries.clear }
end