Class: CardIndex
- Inherits:
-
Object
- Object
- CardIndex
- Defined in:
- lib/zillacore/card_index.rb
Constant Summary collapse
- SIMILARITY_THRESHOLD =
0.65- SEMANTIC_THRESHOLD =
0.65- SEMANTIC_COLLECTION =
"card-titles"- QMD_DEBOUNCE =
seconds
30
Instance Attribute Summary collapse
-
#index_file ⇒ Object
readonly
Returns the value of attribute index_file.
-
#titles_dir ⇒ Object
readonly
Returns the value of attribute titles_dir.
Instance Method Summary collapse
-
#[](key) ⇒ Object
— Hash-like access (thread-safe) —.
- #[]=(key, value) ⇒ Object
-
#backfill ⇒ Object
— Backfill from Fizzy API on startup —.
-
#backfill_project(project_key, config, seen_boards) ⇒ Object
Backfill cards for a single project.
-
#build_scope_map ⇒ Object
— Scope extraction for cross-project duplicate filtering —.
- #card_scopes(tags) ⇒ Object
- #delete(key) ⇒ Object
- #different_scopes?(tags_a, tags_b) ⇒ Boolean
- #dig(*keys) ⇒ Object
- #each ⇒ Object
-
#ensure_card_titles_collection ⇒ Object
Ensure the qmd collection exists, create if not.
- #evict_card(number) ⇒ Object
-
#find_semantic_similar_cards(title, exclude_number: nil) ⇒ Object
— Semantic search via qmd vsearch —.
-
#find_similar_cards(title, exclude_number: nil, tags: nil) ⇒ Object
— Merged search: trigram + semantic in parallel —.
-
#find_trigram_similar_cards(title, exclude_number: nil) ⇒ Object
— Trigram search —.
- #index_card(number:, title:, creator_name: nil, creator_id: nil, tags: [], closed: false) ⇒ Object
-
#initialize(index_file:, titles_dir:) ⇒ CardIndex
constructor
A new instance of CardIndex.
- #key?(key) ⇒ Boolean
-
#load ⇒ Object
— Index operations —.
- #remove_card_title_file(number) ⇒ Object
- #save ⇒ Object
-
#schedule_qmd_reindex ⇒ Object
Debounced qmd update + embed.
- #size ⇒ Object
-
#sync_card_title_file(number, title, closed: false) ⇒ Object
— Card title files for qmd collection —.
-
#sync_title_files ⇒ Object
— Startup —.
- #to_h ⇒ Object
- #to_json ⇒ Object
- #trigram_similarity(str_a, str_b) ⇒ Object
-
#trigrams(str) ⇒ Object
— Trigram similarity —.
Constructor Details
#initialize(index_file:, titles_dir:) ⇒ CardIndex
Returns a new instance of CardIndex.
28 29 30 31 32 33 34 35 36 |
# File 'lib/zillacore/card_index.rb', line 28 def initialize(index_file:, titles_dir:) @index_file = index_file @titles_dir = titles_dir @data = {} @mutex = Mutex.new @qmd_mutex = Mutex.new @qmd_last_run = nil @qmd_pending = false end |
Instance Attribute Details
#index_file ⇒ Object (readonly)
Returns the value of attribute index_file.
26 27 28 |
# File 'lib/zillacore/card_index.rb', line 26 def index_file @index_file end |
#titles_dir ⇒ Object (readonly)
Returns the value of attribute titles_dir.
26 27 28 |
# File 'lib/zillacore/card_index.rb', line 26 def titles_dir @titles_dir end |
Instance Method Details
#[](key) ⇒ Object
— Hash-like access (thread-safe) —
40 41 42 |
# File 'lib/zillacore/card_index.rb', line 40 def [](key) @mutex.synchronize { @data[key] } end |
#[]=(key, value) ⇒ Object
44 45 46 |
# File 'lib/zillacore/card_index.rb', line 44 def []=(key, value) @mutex.synchronize { @data[key] = value } end |
#backfill ⇒ Object
— Backfill from Fizzy API on startup —
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/zillacore/card_index.rb', line 304 def backfill Thread.new do LOG.info "[CardIndex] Starting backfill from Fizzy API..." backfilled = 0 seen_boards = Set.new PROJECTS.each do |project_key, config| result = backfill_project(project_key, config, seen_boards) backfilled += result if result end save LOG.info "[CardIndex] Backfill complete: #{backfilled} new cards indexed (#{size} total)" ensure_card_titles_collection schedule_qmd_reindex end end |
#backfill_project(project_key, config, seen_boards) ⇒ Object
Backfill cards for a single project. Returns count of new cards indexed, or nil if skipped.
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/zillacore/card_index.rb', line 324 def backfill_project(project_key, config, seen_boards) repo_path = config["repo_path"] return nil unless repo_path && File.directory?(repo_path) fizzy_yaml = File.join(repo_path, ".fizzy.yaml") unless File.exist?(fizzy_yaml) LOG.debug "[CardIndex] Skipping '#{project_key}' — no .fizzy.yaml" return nil end begin board_id = YAML.safe_load_file(fizzy_yaml)["board"] rescue StandardError => e LOG.warn "[CardIndex] Could not read .fizzy.yaml for '#{project_key}': #{e.}" return nil end if seen_boards.include?(board_id) LOG.debug "[CardIndex] Skipping '#{project_key}' — board #{board_id} already fetched" return nil end seen_boards << board_id count = 0 output = run_cmd("fizzy", "card", "list", "--all", chdir: repo_path, env: default_fizzy_env) cards = JSON.parse(output)["data"] || [] cards.each do |card| num = card["number"] next unless num next if key?(num.to_s) index_card( number: num, title: card["title"] || card["description"]&.slice(0, 80) || "untitled", creator_name: card.dig("creator", "name"), creator_id: card.dig("creator", "id"), tags: card["tags"] || [], closed: card["closed"] || false ) count += 1 end count rescue StandardError => e LOG.warn "[CardIndex] Backfill failed for project '#{project_key}': #{e.}" 0 end |
#build_scope_map ⇒ Object
— Scope extraction for cross-project duplicate filtering —
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/zillacore/card_index.rb', line 201 def build_scope_map return if @scope_map_built @scope_map ||= {} PROJECTS.each do |key, cfg| (cfg["fizzy_tags"] || []).each { |t| @scope_map[t.downcase] = key } (cfg["scope_tags"] || {}).each { |tag, scope| @scope_map[tag.downcase] = scope } end @scope_map_built = true end |
#card_scopes(tags) ⇒ Object
212 213 214 215 216 217 218 219 220 |
# File 'lib/zillacore/card_index.rb', line 212 def card_scopes() return Set.new if .nil? || .empty? build_scope_map tag_names = .map { |t| (t.is_a?(Hash) ? t["name"] : t).to_s.downcase } scopes = Set.new tag_names.each { |t| scopes << @scope_map[t] if @scope_map[t] } scopes end |
#delete(key) ⇒ Object
48 49 50 |
# File 'lib/zillacore/card_index.rb', line 48 def delete(key) @mutex.synchronize { @data.delete(key) } end |
#different_scopes?(tags_a, tags_b) ⇒ Boolean
222 223 224 225 226 |
# File 'lib/zillacore/card_index.rb', line 222 def different_scopes?(, ) scopes_a = card_scopes() scopes_b = card_scopes() scopes_a.any? && scopes_b.any? && !scopes_a.intersect?(scopes_b) end |
#dig(*keys) ⇒ Object
64 65 66 |
# File 'lib/zillacore/card_index.rb', line 64 def dig(*keys) @mutex.synchronize { @data.dig(*keys) } end |
#each ⇒ Object
60 61 62 |
# File 'lib/zillacore/card_index.rb', line 60 def each(&) @mutex.synchronize { @data.each(&) } end |
#ensure_card_titles_collection ⇒ Object
Ensure the qmd collection exists, create if not
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/zillacore/card_index.rb', line 112 def ensure_card_titles_collection FileUtils.mkdir_p(@titles_dir) output, _, status = Open3.capture3("qmd", "collection", "list") return if status.success? && output.include?(SEMANTIC_COLLECTION) LOG.info "[CardIndex] Creating qmd collection '#{SEMANTIC_COLLECTION}'" _, stderr, s = Open3.capture3("qmd", "collection", "add", @titles_dir, "--name", SEMANTIC_COLLECTION, "--mask", "*.md") LOG.warn "[CardIndex] Failed to create qmd collection: #{stderr}" unless s.success? end |
#evict_card(number) ⇒ Object
194 195 196 197 |
# File 'lib/zillacore/card_index.rb', line 194 def evict_card(number) delete(number.to_s) remove_card_title_file(number) end |
#find_semantic_similar_cards(title, exclude_number: nil) ⇒ Object
— Semantic search via qmd vsearch —
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/zillacore/card_index.rb', line 244 def find_semantic_similar_cards(title, exclude_number: nil) output, stderr, status = Open3.capture3("qmd", "vsearch", title, "-c", SEMANTIC_COLLECTION, "--json", "--min-score", SEMANTIC_THRESHOLD.to_s, "--all") unless status.success? LOG.warn "[CardIndex] qmd vsearch failed: #{stderr.lines.last&.strip}" return [] end clean = output.lines.reject { |l| l.start_with?("[node-llama-cpp]") }.join json_start = clean.index("[") return [] unless json_start results = JSON.parse(clean[json_start..]) results.filter_map do |r| num = r["file"]&.match(%r{/(\d+)\.md$})&.[](1) next unless num next if num == exclude_number.to_s entry = self[num] next if entry&.dig("closed") { number: num.to_i, title: entry&.dig("title") || r["snippet"]&.strip || "", score: r["score"], method: :semantic } end rescue JSON::ParserError => e LOG.warn "[CardIndex] Failed to parse qmd vsearch output: #{e.}" [] end |
#find_similar_cards(title, exclude_number: nil, tags: nil) ⇒ Object
— Merged search: trigram + semantic in parallel —
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/zillacore/card_index.rb', line 274 def find_similar_cards(title, exclude_number: nil, tags: nil) trigram_thread = Thread.new { find_trigram_similar_cards(title, exclude_number: exclude_number) } semantic_thread = Thread.new { find_semantic_similar_cards(title, exclude_number: exclude_number) } trigram_results = trigram_thread.value semantic_results = semantic_thread.value merged = {} (trigram_results + semantic_results).each do |match| key = match[:number] existing = merged[key] if existing.nil? || match[:score] > existing[:score] merged[key] = match elsif match[:score] == existing[:score] && existing[:method] != match[:method] merged[key] = existing.merge(method: :both) end end if && card_scopes().any? merged.reject! do |num, _match| = dig(num.to_s, "tags") different_scopes?(, ) end end merged.values.sort_by { |m| -m[:score] } end |
#find_trigram_similar_cards(title, exclude_number: nil) ⇒ Object
— Trigram search —
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/zillacore/card_index.rb', line 230 def find_trigram_similar_cards(title, exclude_number: nil) matches = [] each do |num, entry| next if num == exclude_number.to_s next if entry["closed"] score = trigram_similarity(title, entry["title"]) matches << { number: num.to_i, title: entry["title"], score: score, method: :trigram } if score >= SIMILARITY_THRESHOLD end matches end |
#index_card(number:, title:, creator_name: nil, creator_id: nil, tags: [], closed: false) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/zillacore/card_index.rb', line 180 def index_card(number:, title:, creator_name: nil, creator_id: nil, tags: [], closed: false) @mutex.synchronize do @data[number.to_s] = { "title" => title, "creator_name" => creator_name, "creator_id" => creator_id, "tags" => .map { |t| t.is_a?(Hash) ? t["name"] : t.to_s }, "closed" => closed, "indexed_at" => Time.now.iso8601 } end sync_card_title_file(number, title, closed: closed) end |
#key?(key) ⇒ Boolean
56 57 58 |
# File 'lib/zillacore/card_index.rb', line 56 def key?(key) @mutex.synchronize { @data.key?(key) } end |
#load ⇒ Object
— Index operations —
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/zillacore/card_index.rb', line 161 def load data = if File.exist?(@index_file) JSON.parse(File.read(@index_file)) else {} end @mutex.synchronize { @data.replace(data) } LOG.info "[CardIndex] Loaded #{size} cards from disk" rescue JSON::ParserError => e LOG.error "Failed to parse card index: #{e.}" @mutex.synchronize { @data.replace({}) } end |
#remove_card_title_file(number) ⇒ Object
107 108 109 |
# File 'lib/zillacore/card_index.rb', line 107 def remove_card_title_file(number) FileUtils.rm_f(File.join(@titles_dir, "#{number}.md")) end |
#save ⇒ Object
174 175 176 177 178 |
# File 'lib/zillacore/card_index.rb', line 174 def save @mutex.synchronize do File.write(@index_file, JSON.generate(@data)) end end |
#schedule_qmd_reindex ⇒ Object
Debounced qmd update + embed. Runs in background thread.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/zillacore/card_index.rb', line 124 def schedule_qmd_reindex @qmd_mutex.synchronize do @qmd_pending = true return if @qmd_last_run && (Time.now - @qmd_last_run) < QMD_DEBOUNCE @qmd_last_run = Time.now @qmd_pending = false end Thread.new do LOG.info "[CardIndex] Running qmd update for card titles..." _, stderr, s = Open3.capture3("qmd", "update") LOG.warn "[CardIndex] qmd update failed: #{stderr}" unless s.success? LOG.info "[CardIndex] Running qmd embed for card titles..." _, stderr, s = Open3.capture3("qmd", "embed") LOG.warn "[CardIndex] qmd embed failed: #{stderr}" unless s.success? LOG.info "[CardIndex] qmd reindex complete" needs_rerun = @qmd_mutex.synchronize do if @qmd_pending @qmd_pending = false @qmd_last_run = Time.now true else false end end schedule_qmd_reindex if needs_rerun rescue StandardError => e LOG.warn "[CardIndex] qmd reindex failed: #{e.}" end end |
#size ⇒ Object
52 53 54 |
# File 'lib/zillacore/card_index.rb', line 52 def size @mutex.synchronize { @data.size } end |
#sync_card_title_file(number, title, closed: false) ⇒ Object
— Card title files for qmd collection —
97 98 99 100 101 102 103 104 105 |
# File 'lib/zillacore/card_index.rb', line 97 def sync_card_title_file(number, title, closed: false) FileUtils.mkdir_p(@titles_dir) path = File.join(@titles_dir, "#{number}.md") if closed FileUtils.rm_f(path) else File.write(path, title) end end |
#sync_title_files ⇒ Object
— Startup —
373 374 375 376 377 378 |
# File 'lib/zillacore/card_index.rb', line 373 def sync_title_files FileUtils.mkdir_p(@titles_dir) each do |num, entry| sync_card_title_file(num, entry["title"], closed: entry["closed"]) end end |
#to_h ⇒ Object
72 73 74 |
# File 'lib/zillacore/card_index.rb', line 72 def to_h @mutex.synchronize { @data.dup } end |
#to_json ⇒ Object
68 69 70 |
# File 'lib/zillacore/card_index.rb', line 68 def to_json(...) @mutex.synchronize { @data.to_json(...) } end |
#trigram_similarity(str_a, str_b) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/zillacore/card_index.rb', line 85 def trigram_similarity(str_a, str_b) ta = trigrams(str_a) tb = trigrams(str_b) return 0.0 if ta.empty? || tb.empty? intersection = (ta & tb).size.to_f union = (ta | tb).size.to_f intersection / union end |
#trigrams(str) ⇒ Object
— Trigram similarity —
78 79 80 81 82 83 |
# File 'lib/zillacore/card_index.rb', line 78 def trigrams(str) normalized = str.downcase.gsub(/[^a-z0-9\s]/, "").strip return Set.new if normalized.length < 3 Set.new((0..(normalized.length - 3)).map { |i| normalized[i, 3] }) end |