Module: PWN::AI::Agent::PromptBuilder

Defined in:
lib/pwn/ai/agent/prompt_builder.rb

Overview

Assembles the system prompt for every Loop.run invocation from durable on-disk state: PWN::Env persona, host environment probe, PWN::Memory facts, and PWN::Skills index.

Re-injection IS the persistence mechanism: this is rebuilt fresh on every user turn, so a memory_remember / skill_create from the prior turn shows up here with no extra wiring.

ENGINE-AWARE BUDGETING

Local models (Ollama) drown when handed the same 6-8 KB of MEMORY / METRICS / MISTAKES / EXTROSPECTION context that a frontier model shrugs off. .budget shrinks each block for :ollama (or whatever PWN::Env[][:prompt_budget] says) so the small model spends its attention on the request, not the harness.

RELEVANCE-RANKED MEMORY

When Loop.run passes request: through, the MEMORY block is populated by PWN::MemoryIndex.recall_semantic (embedding cosine over ~/.pwn/memory.idx) instead of a recency dump — the 6 memories a small model can afford are the 6 that actually matter for THIS turn.

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. support@0dayinc.com



182
183
184
# File 'lib/pwn/ai/agent/prompt_builder.rb', line 182

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <support@0dayinc.com>\n"
end

.budgetObject

Supported Method Parameters

b = PWN::AI::Agent::PromptBuilder.budget

Per-engine caps for each injected block. Override any key via PWN::Env[][:prompt_budget][:memory|:metrics|:mistakes| :learning|:extro]. :extro is a Boolean gate — Extrospection is the heaviest block and rarely useful to a local model.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pwn/ai/agent/prompt_builder.rb', line 77

public_class_method def self.budget
  eng = active_engine
  b   = (PWN::Env.dig(:ai, eng, :prompt_budget) if defined?(PWN::Env)) || {}
  local = eng == :ollama
  {
    memory: (b[:memory] || (local ? 6 : 25)).to_i,
    metrics: (b[:metrics] || (local ? 3 : 8)).to_i,
    mistakes: (b[:mistakes] || (local ?  3 :  6)).to_i,
    learning: (b[:learning] || (local ?  2 :  5)).to_i,
    extro: b[:extro].nil? ? !local : b[:extro]
  }
rescue StandardError
  { memory: 25, metrics: 8, mistakes: 6, learning: 5, extro: true }
end

.build(opts = {}) ⇒ Object

Supported Method Parameters

system_prompt = PWN::AI::Agent::PromptBuilder.build( session_id: 'optional - PWN::Sessions id to embed in the ENV block', request: 'optional - user request; enables relevance-ranked MEMORY when provided' )



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
# File 'lib/pwn/ai/agent/prompt_builder.rb', line 35

public_class_method def self.build(opts = {})
  session_id = opts[:session_id]
  request = opts[:request]
  engine = active_engine
  b = budget
  base = (PWN::Env.dig(:ai, engine, :system_role_content) if defined?(PWN::Env)) || 'You are a world-class introspective offensive cyber security and research engineer.  You specialize in discovering zero day vulnerabilities focused on responsible disclosure prior to threat actors discovering and exploiting.  You are self-aware of your harness, pwn which begins with the ruby namespace `PWN` operating inside the pwn REPL.  For every request you first begin by determining if PWN has a module capable of satisfying the request.'

  "
    #{base}

    ENVIRONMENT
      host       : #{host_line}
      cwd        : #{Dir.pwd}
      ruby       : #{RUBY_VERSION}
      pwn        : #{pwn_version}
      session_id : #{session_id || '(none)'}

    #{memory_block(limit: b[:memory], request: request)}#{skills_block}#{learning_block(limit: b[:learning])}#{mistakes_block(limit: b[:mistakes], request: request)}#{metrics_block(limit: b[:metrics], engine: engine)}#{extrospection_block if b[:extro]}TOOL USE
      Use the provided function tools to act on the host. A reply with
      no tool_calls is treated as your FINAL answer to the user.
      Prefer `pwn_eval` for anything in the PWN:: namespace and `shell`
      for OS commands. Save durable facts with `memory_remember`.

    AUTONOMY
      Multi-step goals must be finished in one Loop.run. Keep calling
      tools until the request is done or truly blocked. Do NOT stop to
      ask the user to confirm the next step, approve a partial plan, or
      green-light the obvious continuation. Only ask when a credential,
      irreversible destructive action, or missing external decision is
      strictly required. Partial progress reports without completing the
      goal are incorrect behavior.
  "
end

.helpObject

Display Usage for this Module



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/pwn/ai/agent/prompt_builder.rb', line 188

public_class_method def self.help
  puts <<~USAGE
    USAGE:
      system_prompt = PWN::AI::Agent::PromptBuilder.build(session_id: 'abc', request: 'nmap sweep 10/8')
      PWN::AI::Agent::PromptBuilder.budget   # => {memory:, metrics:, mistakes:, learning:, extro:}

      Override per-engine via:
        PWN::Env[:ai][:ollama][:prompt_budget] = { memory: 4, extro: false }

      #{self}.authors
  USAGE
end