Class: Crimson::TrustManager

Inherits:
Object
  • Object
show all
Defined in:
lib/crimson/trust_manager.rb

Overview

Manages directory trust state for loading project context files. Persists trust decisions to a JSON file and prompts users for untrusted directories.

Constant Summary collapse

CONTEXT_FILE_NAMES =

File names considered as project context files.

%w[
  AGENTS.md AGENTS.MD
  CLAUDE.md CLAUDE.MD
  GEMINI.md GEMINI.MD
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(trust_file: nil) ⇒ TrustManager

Returns a new instance of TrustManager.

Parameters:

  • trust_file (String, nil) (defaults to: nil)

    path to the trust JSON file



18
19
20
21
# File 'lib/crimson/trust_manager.rb', line 18

def initialize(trust_file: nil)
  @trust_file = trust_file || File.join(Crimson::CONFIG_DIR, "trust.json")
  @trust_data = load_trust_data
end

Instance Method Details

#has_context_files?(dir) ⇒ Boolean

Check whether a directory contains any project context files.

Parameters:

  • dir (String)

    directory path

Returns:

  • (Boolean)


67
68
69
# File 'lib/crimson/trust_manager.rb', line 67

def has_context_files?(dir)
  CONTEXT_FILE_NAMES.any? { |name| File.exist?(File.join(dir, name)) }
end

#prompt_trust(cwd) ⇒ Boolean

Prompt the user to trust a directory that has context files.

Parameters:

  • cwd (String)

    directory path

Returns:

  • (Boolean)

    whether the directory is now trusted



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/crimson/trust_manager.rb', line 34

def prompt_trust(cwd)
  expanded = File.expand_path(cwd)
  return true unless has_context_files?(expanded)
  return true if trusted?(expanded)

  prompt = TTY::Prompt.new
  puts
  choice = prompt.select("Trust this project?\n#{expanded}\n\nThis allows loading AGENTS.md and project settings.", [
    { name: "Trust", value: :trust },
    { name: "Trust parent folder (#{File.dirname(expanded)})", value: :trust_parent },
    { name: "Trust (this session only)", value: :session_only },
    { name: "Don't trust", value: :deny }
  ])

  case choice
  when :trust
    save_trust(expanded, true)
    true
  when :trust_parent
    save_trust(File.dirname(expanded), true)
    save_trust(expanded, nil)
    true
  when :session_only
    true
  when :deny
    save_trust(expanded, false)
    false
  end
end

#trusted?(cwd) ⇒ Boolean

Check whether a directory (or an ancestor) is trusted.

Parameters:

  • cwd (String)

    directory path

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/crimson/trust_manager.rb', line 26

def trusted?(cwd)
  entry = find_nearest_trust(File.expand_path(cwd))
  entry == true
end