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- 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_http! ⇒ Object
RestClient uses HTTP::CookieJar.
- .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
325 326 327 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 325 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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 86 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.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 151 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
329 330 331 332 333 334 335 336 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 329 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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 122 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
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 110 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
182 183 184 185 186 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 182 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
209 210 211 212 213 214 215 216 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 209 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
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 194 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
188 189 190 191 192 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 188 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_http! ⇒ Object
RestClient uses HTTP::CookieJar. pwn_eval in TOPLEVEL_BINDING can assign HTTP = "/path/http" (a mkdir) and then every provider hop TypeErrors: "path is not a class/module".
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 65 public_class_method def self.protect_http! if Object.const_defined?(:HTTP, false) cur = Object.const_get(:HTTP) @http_mod = cur if cur.is_a?(Module) && @http_mod.nil? return cur if cur.is_a?(Module) Object.send(:remove_const, :HTTP) end if @http_mod.is_a?(Module) Object.const_set(:HTTP, @http_mod) return @http_mod end require 'http/cookie_jar' @http_mod = Object.const_get(:HTTP) if Object.const_defined?(:HTTP) && Object.const_get(:HTTP).is_a?(Module) @http_mod rescue StandardError nil end |
.reset_timeout_budget(opts = {}) ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 169 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
178 179 180 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 178 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).
223 224 225 226 227 228 229 230 231 232 233 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 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 223 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
313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 313 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
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/pwn/ai/agent/tool_guard.rb', line 260 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 |