Class: ClaudeMemory::Commands::ExplainCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/claude_memory/commands/explain_command.rb

Overview

Explains a fact with provenance and relationships

Instance Attribute Summary

Attributes inherited from BaseCommand

#stderr, #stdin, #stdout

Instance Method Summary collapse

Methods inherited from BaseCommand

#initialize

Constructor Details

This class inherits a constructor from ClaudeMemory::Commands::BaseCommand

Instance Method Details

#call(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
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
52
# File 'lib/claude_memory/commands/explain_command.rb', line 7

def call(args)
  identifier = args.first
  unless identifier && !identifier.empty?
    stderr.puts "Usage: claude-memory explain <fact_id|docid> [--scope project|global]"
    return 1
  end

  # Accept integer IDs or 8-char docid strings
  fact_ref = identifier.match?(/\A\d+\z/) ? identifier.to_i : identifier

  opts = parse_options(args[1..] || [], {scope: "project"}) do |o|
    OptionParser.new do |parser|
      parser.on("--scope SCOPE", "Scope: project or global") { |v| o[:scope] = v }
    end
  end
  return 1 if opts.nil?

  manager = ClaudeMemory::Store::StoreManager.new
  recall = ClaudeMemory::Recall.new(manager)

  explanation = recall.explain(fact_ref, scope: opts[:scope])
  if explanation.is_a?(ClaudeMemory::Core::NullExplanation)
    stderr.puts "Fact #{identifier} not found in #{opts[:scope]} database."
    manager.close
    return 1
  end

  docid = explanation[:fact][:docid]
  label = docid ? "##{docid}" : "##{explanation[:fact][:id]}"
  stdout.puts "Fact #{label} (#{opts[:scope]}):"
  print_fact(explanation[:fact])
  print_receipts(explanation[:receipts])

  if explanation[:supersedes].any?
    stdout.puts "  Supersedes: #{explanation[:supersedes].join(", ")}"
  end
  if explanation[:superseded_by].any?
    stdout.puts "  Superseded by: #{explanation[:superseded_by].join(", ")}"
  end
  if explanation[:conflicts].any?
    stdout.puts "  Conflicts: #{explanation[:conflicts].map { |c| c[:id] }.join(", ")}"
  end

  manager.close
  0
end