Class: ClaudeMemory::Core::FactGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_memory/core/fact_graph.rb

Overview

Builds a dependency graph of facts using BFS traversal. Queries fact_links and conflicts tables to build a graph of related facts with their relationships. Follows Functional Core pattern - pure query + transformation.

Constant Summary collapse

MAX_DEPTH =
5

Class Method Summary collapse

Class Method Details

.build(store, root_fact_id, depth: 2) ⇒ Hash

Build a fact dependency graph starting from a root fact

Parameters:

  • store (SQLiteStore)

    Database store

  • root_fact_id (Integer)

    Starting fact ID

  • depth (Integer) (defaults to: 2)

    Maximum BFS depth (1-5)

Returns:

  • (Hash)

    Graph with :nodes and :edges arrays



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/claude_memory/core/fact_graph.rb', line 17

def self.build(store, root_fact_id, depth: 2)
  depth = depth.clamp(1, MAX_DEPTH)

  visited = Set.new
  queue = [[root_fact_id, 0]]
  nodes = {}
  edges = []

  while queue.any?
    fact_id, current_depth = queue.shift
    next if visited.include?(fact_id)

    visited.add(fact_id)

    fact = FactQueryBuilder.find_fact(store, fact_id)
    next unless fact

    nodes[fact_id] = build_node(fact)

    next if current_depth >= depth

    discover_links(store, fact_id, current_depth, visited, queue, edges)
  end

  deduped = dedupe_edges(edges)

  {
    root_fact_id: root_fact_id,
    depth: depth,
    node_count: nodes.size,
    edge_count: deduped.size,
    nodes: nodes.values,
    edges: deduped
  }
end

.build_node(fact) ⇒ Hash

Build a minimal node representation of a fact

Parameters:

  • fact (Hash)

    Fact row from database

Returns:

  • (Hash)

    Node representation



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/claude_memory/core/fact_graph.rb', line 102

def self.build_node(fact)
  {
    id: fact[:id],
    docid: fact[:docid],
    subject: fact[:subject_name],
    predicate: fact[:predicate],
    object: fact[:object_literal],
    status: fact[:status],
    scope: fact[:scope]
  }
end

.dedupe_edges(edges) ⇒ Array<Hash>

Remove duplicate edges (same from/to/type)

Parameters:

  • edges (Array<Hash>)

    Edges to deduplicate

Returns:

  • (Array<Hash>)

    Deduplicated edges



117
118
119
# File 'lib/claude_memory/core/fact_graph.rb', line 117

def self.dedupe_edges(edges)
  edges.uniq { |e| [e[:from], e[:to], e[:type]] }
end

.discover_conflicts(store, fact_id, current_depth, visited, queue, edges) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/claude_memory/core/fact_graph.rb', line 79

def self.discover_conflicts(store, fact_id, current_depth, visited, queue, edges)
  store.conflicts
    .where(fact_a_id: fact_id)
    .select(:fact_b_id, :status)
    .all
    .each do |conflict|
      edges << {from: fact_id, to: conflict[:fact_b_id], type: "conflicts", status: conflict[:status]}
      queue << [conflict[:fact_b_id], current_depth + 1] unless visited.include?(conflict[:fact_b_id])
    end

  store.conflicts
    .where(fact_b_id: fact_id)
    .select(:fact_a_id, :status)
    .all
    .each do |conflict|
      edges << {from: conflict[:fact_a_id], to: fact_id, type: "conflicts", status: conflict[:status]}
      queue << [conflict[:fact_a_id], current_depth + 1] unless visited.include?(conflict[:fact_a_id])
    end
end


53
54
55
56
57
# File 'lib/claude_memory/core/fact_graph.rb', line 53

def self.discover_links(store, fact_id, current_depth, visited, queue, edges)
  discover_supersedes(store, fact_id, current_depth, visited, queue, edges)
  discover_superseded_by(store, fact_id, current_depth, visited, queue, edges)
  discover_conflicts(store, fact_id, current_depth, visited, queue, edges)
end

.discover_superseded_by(store, fact_id, current_depth, visited, queue, edges) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/claude_memory/core/fact_graph.rb', line 69

def self.discover_superseded_by(store, fact_id, current_depth, visited, queue, edges)
  store.fact_links
    .where(to_fact_id: fact_id, link_type: "supersedes")
    .select_map(:from_fact_id)
    .each do |source_id|
      edges << {from: source_id, to: fact_id, type: "supersedes"}
      queue << [source_id, current_depth + 1] unless visited.include?(source_id)
    end
end

.discover_supersedes(store, fact_id, current_depth, visited, queue, edges) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/claude_memory/core/fact_graph.rb', line 59

def self.discover_supersedes(store, fact_id, current_depth, visited, queue, edges)
  store.fact_links
    .where(from_fact_id: fact_id, link_type: "supersedes")
    .select_map(:to_fact_id)
    .each do |target_id|
      edges << {from: fact_id, to: target_id, type: "supersedes"}
      queue << [target_id, current_depth + 1] unless visited.include?(target_id)
    end
end