Class: Ace::PromptPrep::Molecules::BundleLoader
- Inherits:
-
Object
- Object
- Ace::PromptPrep::Molecules::BundleLoader
- Defined in:
- lib/ace/prompt_prep/molecules/bundle_loader.rb
Overview
Load bundle via ace-bundle Ruby API
This module calls Ace::Bundle.load_file and returns the result.
Constant Summary collapse
- VALID_FORMATS =
Valid bundle format options
["markdown-xml", "markdown", "xml"].freeze
Class Method Summary collapse
-
.call(prompt_path, options = {}) ⇒ String
Load bundle from prompt file with validation.
- .canonical_missing_path(path) ⇒ Object
- .canonical_path(path) ⇒ Object
-
.debug_log(message, category = nil) ⇒ Object
Debug logging for troubleshooting and development.
- .path_within_root?(path, root) ⇒ Boolean
-
.valid_options?(options) ⇒ Boolean
Validate options hash.
-
.valid_prompt_path?(prompt_path) ⇒ Boolean
Validate prompt path input.
Class Method Details
.call(prompt_path, options = {}) ⇒ String
Load bundle from prompt file with validation
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ace/prompt_prep/molecules/bundle_loader.rb', line 20 def self.call(prompt_path, = {}) # Input validation return "" unless valid_prompt_path?(prompt_path) return "" unless () debug_log("Loading bundle from: #{prompt_path}", :bundle_loading) begin require "ace/bundle" rescue LoadError warn "Error: ace-bundle gem not available" return "" end begin # Validate that the prompt file exists and is readable unless File.exist?(prompt_path) warn "Error: Prompt file does not exist: #{prompt_path}" return "" end unless File.readable?(prompt_path) warn "Error: Prompt file is not readable: #{prompt_path}" return "" end # Resolve symlinks and validate project boundaries project_root = Ace::Support::Fs::Molecules::ProjectRootFinder.find_or_current unless path_within_root?(prompt_path, project_root) warn "Error: File path resolves outside project: #{canonical_path(prompt_path)}" return "" end # Check file size to prevent processing extremely large files file_size = File.size(prompt_path) max_size_bytes = (Ace::PromptPrep.config.dig("security", "max_file_size_mb") || 10) * 1024 * 1024 debug_log("File size: #{file_size} bytes (limit: #{max_size_bytes / 1024 / 1024}MB)", :bundle_loading) if file_size > max_size_bytes warn "Error: Prompt file too large (#{file_size} bytes), exceeds limit of #{max_size_bytes / 1024 / 1024}MB" return "" end bundle_data = Ace::Bundle.load_file( prompt_path, format: [:format] || "markdown-xml", embed_source: [:embed_source].nil? || [:embed_source] ) # Validate bundle data structure if bundle_data.nil? warn "Warning: Bundle data is nil" return "" end # Extract content with validation content = bundle_data.content if content.nil? warn "Warning: Bundle content is nil" return "" end # Validate content type unless content.is_a?(String) warn "Warning: Bundle content is not a string (#{content.class})" return "" end # Validate content length if content.empty? warn "Warning: Bundle content is empty" return "" end debug_log("Bundle loaded successfully, content length: #{content.length} characters", :bundle_loading) content rescue => e warn "Error: Failed to load bundle: #{e.}" "" end end |
.canonical_missing_path(path) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ace/prompt_prep/molecules/bundle_loader.rb', line 186 def self.canonical_missing_path(path) parent = path suffix = [] until File.exist?(parent) || parent == File.dirname(parent) suffix.unshift(File.basename(parent)) parent = File.dirname(parent) end return path unless File.exist?(parent) File.join(File.realpath(parent), *suffix) end |
.canonical_path(path) ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'lib/ace/prompt_prep/molecules/bundle_loader.rb', line 177 def self.canonical_path(path) = File.(path) return File.realpath() if File.exist?() canonical_missing_path() rescue File.(path) end |
.debug_log(message, category = nil) ⇒ Object
Debug logging for troubleshooting and development
108 109 110 111 112 113 |
# File 'lib/ace/prompt_prep/molecules/bundle_loader.rb', line 108 def self.debug_log(, category = nil) return unless Ace::PromptPrep.config.dig("debug", "enabled") return if category && !Ace::PromptPrep.config.dig("debug", category.to_s) warn "[DEBUG] #{}" end |
.path_within_root?(path, root) ⇒ Boolean
171 172 173 174 175 |
# File 'lib/ace/prompt_prep/molecules/bundle_loader.rb', line 171 def self.path_within_root?(path, root) canonical = canonical_path(path) canonical_root = canonical_path(root) canonical == canonical_root || canonical.start_with?("#{canonical_root}#{File::SEPARATOR}") end |
.valid_options?(options) ⇒ Boolean
Validate options hash
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ace/prompt_prep/molecules/bundle_loader.rb', line 148 def self.() return true unless .is_a?(Hash) # Validate format option if provided if .key?(:format) format = [:format] unless format.is_a?(String) && VALID_FORMATS.include?(format) warn "Warning: Invalid format '#{format}', using default 'markdown-xml'" [:format] = "markdown-xml" end end # Validate embed_source option if provided if .key?(:embed_source) unless [true, false].include?([:embed_source]) warn "Warning: Invalid embed_source value, using default true" [:embed_source] = true end end true end |
.valid_prompt_path?(prompt_path) ⇒ Boolean
Validate prompt path input
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ace/prompt_prep/molecules/bundle_loader.rb', line 119 def self.valid_prompt_path?(prompt_path) return false if prompt_path.nil? return false if prompt_path.to_s.strip.empty? path_str = prompt_path.to_s # Check for various path traversal patterns return false if path_str.include?("../") return false if path_str.include?("..\\") return false if path_str.include?("%2e%2e") # URL encoded return false if path_str.include?("..%2f") return false if path_str.include?("%2e%2e%2f") # Reject absolute paths (should be relative to project) return false if File.absolute_path?(path_str) && !path_str.start_with?("/") # Check for shell escape patterns return false if path_str.include?(";") return false if path_str.include?("&") return false if path_str.include?("|") return false if path_str.include?("`") true end |