Module: PWN::AI::Agent::ToolGuard
- Defined in:
- lib/pwn/ai/agent/tool_guard.rb
Overview
Shared pre-dispatch guards for the two high-volume runtime tools (shell / pwn_eval). Rejects placeholder payloads, aliases wrong schema keys, and names the shell that will actually run the command.
Constant Summary collapse
- ALIASES =
{ 'command' => %w[value cmd input], 'code' => %w[value source ruby input], 'query' => %w[value q text] }.freeze
- PLACEHOLDER_RX =
Token-level junk the model keeps emitting instead of a real command.
/ \A\s*(?:\.{3}|…|\{\s*\.{3}\s*\}|\{\s*…\s*\}|<\.{3}>)\s*\z |(?:^|[\s;|&])(?:\.{3}|…|\{\s*\.{3}\s*\}|\{\s*…\s*\})(?:$|[\s;|&]) /x- BASHISM_RX =
Conservative bash-only constructs. POSIX
$(())is allowed. / \bPIPESTATUS\b |\$\{?RANDOM\}?\b |\[\[(?:\s|\z) |(?:^|[\s;|&])source\s+\S |<\([^)] |&> /x- CORE_CONSTS =
RestClient uses HTTP::CookieJar. pwn_eval in TOPLEVEL_BINDING can assign HTTP = "/path/http" or Digest = "(self.we" and then every provider hop / payload_sig TypeErrors.
%i[HTTP Digest JSON URI Timeout].freeze
- TIMEOUT_STEP_S =
180- TIMEOUT_MAX_S =
10_800- MUTATION_MAX =
10
Class Method Summary collapse
- .authors ⇒ Object
- .bashism?(opts = {}) ⇒ Boolean
-
.coerce_args(opts = {}) ⇒ Object
Coerce common wrong keys onto the first required schema field.
-
.deadline_s(opts = {}) ⇒ Object
Conservative wall-clock seconds for shell / pwn_eval.
- .help ⇒ Object
- .host_load(opts = {}) ⇒ Object
- .invalid_payload(opts = {}) ⇒ Object
- .mutation_count(opts = {}) ⇒ Object
- .next_timeout(opts = {}) ⇒ Object
- .note_timeout!(opts = {}) ⇒ Object
- .payload_spent(opts = {}) ⇒ Object
- .placeholder?(opts = {}) ⇒ Boolean
- .present?(opts = {}) ⇒ Boolean
- .protect_core_constants! ⇒ Object
- .protect_http! ⇒ Object
- .reset_timeout_budget(opts = {}) ⇒ Object
- .reset_timeout_budget! ⇒ Object
- .shell_bash? ⇒ Boolean
- .shell_name ⇒ Object
-
.timeout_lesson(opts = {}) ⇒ Object
Timeout policy (loop-law, not a skill): 1.
- .timeout_prior_count(opts = {}) ⇒ Object
- .timeout_result(opts = {}) ⇒ Object
Class Method Details
.authors ⇒ Object
336 337 338 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 336 public_class_method def self. "AUTHOR(S):\n 0day Inc. <support@0dayinc.com>\n" end |
.bashism?(opts = {}) ⇒ Boolean
45 46 47 48 49 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 45 public_class_method def self.bashism?(opts = {}) BASHISM_RX.match?(opts[:text].to_s) rescue StandardError false end |
.coerce_args(opts = {}) ⇒ Object
Coerce common wrong keys onto the first required schema field. Returns the args hash; sets :__schema_error when still missing.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 97 public_class_method def self.coerce_args(opts = {}) args = (opts[:args] || {}).dup args = args.each_with_object({}) { |(k, v), m| m[k.to_sym] = v } unless args.empty? req = Array(opts[:required]).map(&:to_s) req.each do |key| next if present?(value: args[key.to_sym]) hit = Array(ALIASES[key]).find { |a| present?(value: args[a.to_sym]) } args[key.to_sym] = args[hit.to_sym] if hit end missing = req.reject { |k| present?(value: args[k.to_sym]) } unless missing.empty? args[:__schema_error] = "missing required #{missing.join(', ')}" args[:__expected] = req args[:__schema_hint] = "Expected keys: #{req.join(', ')}. " \ 'Do not send value/placeholder/ellipsis. ' \ 'Example: shell(command="uname -r") or pwn_eval(code="1+1").' end args rescue StandardError opts[:args] || {} end |
.deadline_s(opts = {}) ⇒ Object
Conservative wall-clock seconds for shell / pwn_eval.
Explicit timeout is honored for any payload (1..TIMEOUT_MAX_S).
Omit → host-derived default from loadavg / ncpu / MemAvailable.
No tool-name sniffing: a 65k scan and ls use the same math.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 162 public_class_method def self.deadline_s(opts = {}) kind = opts[:kind].to_s.to_sym asked = opts[:timeout] || opts[:timeout_s] asked_i = asked.to_i return asked_i.clamp(1, TIMEOUT_MAX_S) if asked_i.positive? snap = host_load ncpu = [snap[:ncpu].to_i, 1].max load1 = snap[:load1].to_f mem = snap[:mem_avail_mb].to_i default_max = kind == :shell ? 180 : 90 base = kind == :shell ? 30 : 20 base += 15 if load1 > ncpu base += 10 if load1 > (ncpu * 1.5) base += 10 if mem.positive? && mem < 512 base.clamp(8, default_max) end |
.help ⇒ Object
340 341 342 343 344 345 346 347 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 340 public_class_method def self.help puts <<~USAGE USAGE: PWN::AI::Agent::ToolGuard.placeholder?(text: '...') PWN::AI::Agent::ToolGuard.coerce_args(args: { value: 'id' }, required: %w[command]) #{self}.authors USAGE end |
.host_load(opts = {}) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 133 public_class_method def self.host_load(opts = {}) return { ncpu: 1, load1: 0.0, mem_avail_mb: 0 } unless opts.is_a?(Hash) ncpu = File.readable?('/proc/cpuinfo') ? File.read('/proc/cpuinfo').scan(/^processor/).size : 0 ncpu = 1 if ncpu < 1 load1 = 0.0 load1 = File.read('/proc/loadavg').to_s.split[0].to_f if File.readable?('/proc/loadavg') avail = 0 if File.readable?('/proc/meminfo') File.foreach('/proc/meminfo') do |ln| next unless ln.start_with?('MemAvailable:') avail = ln.split[1].to_i / 1024 break end end { ncpu: ncpu, load1: load1, mem_avail_mb: avail } rescue StandardError { ncpu: 1, load1: 0.0, mem_avail_mb: 0 } end |
.invalid_payload(opts = {}) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 121 public_class_method def self.invalid_payload(opts = {}) hint = opts[:hint].to_s { stdout: '', stderr: hint, exit: 2, error: 'invalid_payload', hint: hint, shell: opts[:shell] || shell_name } end |
.mutation_count(opts = {}) ⇒ Object
193 194 195 196 197 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 193 public_class_method def self.mutation_count(opts = {}) return 0 unless opts.is_a?(Hash) timeout_mutations[task_key(opts)].to_i end |
.next_timeout(opts = {}) ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 220 public_class_method def self.next_timeout(opts = {}) base = opts[:timeout].to_i base = 1 if base < 1 spent = opts.key?(:spent) ? opts[:spent].to_i : payload_spent(opts) remaining = TIMEOUT_MAX_S - spent remaining = 0 if remaining.negative? [base + TIMEOUT_STEP_S, remaining, TIMEOUT_MAX_S].min end |
.note_timeout!(opts = {}) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 205 public_class_method def self.note_timeout!(opts = {}) return 0 unless opts.is_a?(Hash) timeout = opts[:timeout].to_i timeout = 1 if timeout < 1 key = payload_key(opts) timeout_spent[key] = timeout_spent[key].to_i + timeout if budget_exhausted?(opts.merge(spent: timeout_spent[key])) && !timeout_mutated[key] timeout_mutated[key] = true tkey = task_key(opts) timeout_mutations[tkey] = timeout_mutations[tkey].to_i + 1 end timeout_spent[key] end |
.payload_spent(opts = {}) ⇒ Object
199 200 201 202 203 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 199 public_class_method def self.payload_spent(opts = {}) return 0 unless opts.is_a?(Hash) timeout_spent[payload_key(opts)].to_i end |
.placeholder?(opts = {}) ⇒ Boolean
39 40 41 42 43 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 39 public_class_method def self.placeholder?(opts = {}) PLACEHOLDER_RX.match?(opts[:text].to_s) rescue StandardError false end |
.present?(opts = {}) ⇒ Boolean
34 35 36 37 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 34 public_class_method def self.present?(opts = {}) value = opts.is_a?(Hash) ? opts[:value] : opts !value.nil? && !value.to_s.strip.empty? end |
.protect_core_constants! ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 71 public_class_method def self.protect_core_constants! @core_mods ||= {} CORE_CONSTS.each do |name| if Object.const_defined?(name, false) cur = Object.const_get(name, false) @core_mods[name] = cur if cur.is_a?(Module) && @core_mods[name].nil? next if cur.is_a?(Module) Object.send(:remove_const, name) end next unless @core_mods[name].is_a?(Module) next if Object.const_defined?(name, false) && Object.const_get(name, false).equal?(@core_mods[name]) Object.const_set(name, @core_mods[name]) end unless Object.const_defined?(:HTTP, false) && Object.const_get(:HTTP, false).is_a?(Module) require 'http/cookie_jar' @core_mods[:HTTP] = Object.const_get(:HTTP) if Object.const_defined?(:HTTP) && Object.const_get(:HTTP).is_a?(Module) end @core_mods rescue StandardError nil end |
.protect_http! ⇒ Object
67 68 69 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 67 public_class_method def self.protect_http! protect_core_constants! end |
.reset_timeout_budget(opts = {}) ⇒ Object
180 181 182 183 184 185 186 187 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 180 public_class_method def self.reset_timeout_budget(opts = {}) return :noop unless opts.is_a?(Hash) @timeout_spent = {} @timeout_mutations = {} @timeout_mutated = {} :reset end |
.reset_timeout_budget! ⇒ Object
189 190 191 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 189 public_class_method def self.reset_timeout_budget! reset_timeout_budget end |
.shell_bash? ⇒ Boolean
51 52 53 54 55 56 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 51 public_class_method def self.shell_bash? v = (PWN::Env.dig(:ai, :agent, :shell_bash) if defined?(PWN::Env)) v == true || v.to_s.match?(/\A(1|true|yes|on)\z/i) rescue StandardError false end |
.shell_name ⇒ Object
58 59 60 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 58 public_class_method def self.shell_name shell_bash? ? 'bash -lc' : '/bin/sh' end |
.timeout_lesson(opts = {}) ⇒ Object
Timeout policy (loop-law, not a skill):
- Same payload: timeout += 180 until the 3-hour budget is gone.
- At the 3-hour cap: rewrite ruby/command for the same goal (one mutation). Max MUTATION_MAX mutations per task.
- After MUTATION_MAX mutations: stop (exhausted).
234 235 236 237 238 239 240 241 242 243 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 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 234 public_class_method def self.timeout_lesson(opts = {}) return { scenario: :construction, error: '', hint: '' } unless opts.is_a?(Hash) tool = opts[:tool].to_s timeout = opts[:timeout].to_i spent = payload_spent(opts) spent_after = spent >= timeout && timeout.positive? ? spent : spent + [timeout, 1].max nxt = next_timeout(timeout: timeout, spent: spent_after) mutations = mutation_count(opts) if budget_exhausted?(opts.merge(timeout: timeout, spent: spent_after)) if mutations >= MUTATION_MAX { scenario: :exhausted, error: "#{tool} timeout: #{MUTATION_MAX} mutations exhausted for this task", hint: "This task hit the mutation cap (#{MUTATION_MAX} rewrites after " \ '3-hour budgets). Do not retry the same payload. Report what ' \ 'was tried and what remains blocked.' } else { scenario: :construction, error: "#{tool} timeout: 3-hour budget exhausted; reconstruct payload to same goal", hint: "The #{tool} payload used its 3-hour budget. Generate different " \ 'ruby/command for the same goal. Mutation ' \ "#{[mutations, 1].max}/#{MUTATION_MAX}." } end else { scenario: :deadline, error: "#{tool} timeout: deadline too short; retry with timeout += 180", hint: "Keep the same #{tool} payload. This timeout (#{timeout}s) was too " \ "short. Retry with timeout += 180 (next_timeout=#{nxt})." } end end |
.timeout_prior_count(opts = {}) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 324 public_class_method def self.timeout_prior_count(opts = {}) return 0 unless opts.is_a?(Hash) return 0 unless defined?(PWN::AI::Agent::Mistakes) tool = opts[:tool].to_s PWN::AI::Agent::Mistakes.for_tool(tool: tool, unresolved_only: true).count do |m| m[:shape].to_s == 'timeout' || m[:error].to_s.match?(/timeout/) end rescue StandardError 0 end |
.timeout_result(opts = {}) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 271 public_class_method def self.timeout_result(opts = {}) return { stdout: '', stderr: '', exit: nil, error: 'timeout', scenario: :deadline, hint: '', next_timeout: TIMEOUT_STEP_S, shell: shell_name } unless opts.is_a?(Hash) timeout = opts[:timeout].to_i note_timeout!(opts) lesson = timeout_lesson( tool: opts[:tool], payload: opts[:payload], timeout: timeout, task: opts[:task] ) { stdout: opts[:stdout].to_s, stderr: opts[:stderr].to_s, exit: nil, error: "timeout after #{timeout}s", scenario: lesson[:scenario], hint: lesson[:hint], next_timeout: next_timeout(timeout: timeout, spent: payload_spent(opts)), mutations: mutation_count(opts), shell: opts[:shell] || shell_name } end |