Class: RailVerdict::MCP::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/rail_verdict/mcp/cache.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

MAX_STATUS_BYTES =
1 * 1024 * 1024
MAX_DIRTY_FILES =
500
MAX_STATE_FILE_BYTES =
2 * 1024 * 1024

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



20
21
22
23
24
# File 'lib/rail_verdict/mcp/cache.rb', line 20

def initialize
  @mutex = Mutex.new
  @entry = nil
  @last_packets = {}
end

Instance Method Details

#fetch_handoffObject



124
125
126
# File 'lib/rail_verdict/mcp/cache.rb', line 124

def fetch_handoff
  @mutex.synchronize { @last_handoff }
end

#fetch_outcomeObject



79
80
81
82
# File 'lib/rail_verdict/mcp/cache.rb', line 79

def fetch_outcome
  entry = @mutex.synchronize { @entry }
  entry&.outcome
end

#fetch_packet(packet_id) ⇒ Object



114
115
116
# File 'lib/rail_verdict/mcp/cache.rb', line 114

def fetch_packet(packet_id)
  @mutex.synchronize { @last_packets[packet_id] }
end

#fresh_entry(current_state, current_environment = nil) ⇒ Object

Returns the cached entry only when the CURRENT observable identity (repository + environment) still matches what was verified. Never reruns analyzers, but does perform lightweight version probes for environment observation.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rail_verdict/mcp/cache.rb', line 46

def fresh_entry(current_state, current_environment = nil)
  entry = @mutex.synchronize { @entry }
  return nil if entry.nil? || entry.state_digest.nil?
  return nil if current_state.nil? || !current_state.available?
  return nil if current_state.digest != entry.state_digest

  current_env = current_environment || current_environment_for(entry.outcome)
  return nil if current_env.nil? || !current_env.available?
  return nil if current_env.digest != entry.environment_digest

  entry
end

#stale?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/rail_verdict/mcp/cache.rb', line 100

def stale?
  !valid?
end

#store_handoff(handoff) ⇒ Object



118
119
120
121
122
# File 'lib/rail_verdict/mcp/cache.rb', line 118

def store_handoff(handoff)
  @mutex.synchronize do
    @last_handoff = handoff
  end
end

#store_outcome(outcome) ⇒ Object

---- compatibility surface (existing tools/tests) ----



74
75
76
77
# File 'lib/rail_verdict/mcp/cache.rb', line 74

def store_outcome(outcome)
  store_verification(outcome: outcome)
  outcome
end

#store_packet(packet_hash) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/rail_verdict/mcp/cache.rb', line 104

def store_packet(packet_hash)
  @mutex.synchronize do
    @last_packets[packet_hash["packet_id"]] = packet_hash
    if @last_packets.size > 10
      oldest = @last_packets.keys.first
      @last_packets.delete(oldest)
    end
  end
end

#store_verification(outcome:, receipt_document: nil, pr_intelligence_document: nil) ⇒ Object

Canonical storage: one guarded verification outcome plus its derived machine contracts, keyed by the shared Repository State Identity.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rail_verdict/mcp/cache.rb', line 28

def store_verification(outcome:, receipt_document: nil, pr_intelligence_document: nil)
  state_digest = state_digest_for(outcome)
  environment_digest = environment_digest_for(outcome)
  @mutex.synchronize do
    @entry = Entry.new(
      outcome: outcome,
      state_digest: state_digest,
      environment_digest: environment_digest,
      receipt_document: receipt_document,
      pr_intelligence_document: pr_intelligence_document
    )
  end
  nil
end

#valid?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rail_verdict/mcp/cache.rb', line 84

def valid?
  entry = @mutex.synchronize { @entry }
  return false if entry.nil? || entry.state_digest.nil?

  current = current_state_for(entry.outcome)
  return false if current.nil? || !current.available?

  current_env = current_environment_for(entry.outcome)
  return false if current_env.nil? || !current_env.available?

  current.digest == entry.state_digest &&
    current_env.digest == entry.environment_digest
rescue StandardError
  false
end

#verification_state(current_state, current_environment = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rail_verdict/mcp/cache.rb', line 59

def verification_state(current_state, current_environment = nil)
  entry = @mutex.synchronize { @entry }
  return "verification_required" if entry.nil? || entry.state_digest.nil?
  return "state_unavailable" if current_state.nil? || !current_state.available?
  return "stale" if current_state.digest != entry.state_digest

  current_env = current_environment || current_environment_for(entry.outcome)
  return "state_unavailable" if current_env.nil? || !current_env.available?
  return "stale" if current_env.digest != entry.environment_digest

  "fresh"
end