Module: PWN::Memory
- Defined in:
- lib/pwn/memory.rb
Overview
PWN::Memory provides persistent cross-session memory for the pwn-ai agent. Facts, user preferences, environment details, lessons learned, and task state are stored in ~/.pwn/memory.json and survive across REPL restarts / pwn-ai sessions.
The pwn-ai agent (in agent mode) automatically receives relevant memory injected into its system prompt. The agent can also call remember/recall via ruby code blocks during execution loops.
Constant Summary collapse
- MEMORY_FILE =
File.join(Dir.home, '.pwn', 'memory.json')
- VALUE_MAX_CHARS =
Lean retention — keep RL-quality signal, drop ephemeral bulk.
2_000- PROTECT_KEY_PREFIXES =
%w[operator_pref_ process_sop_ mistake_fix_ memory_].freeze
- PROTECT_CATEGORIES =
%i[preference].freeze
- EPHEMERAL_KEY_PREFIXES =
%w[session_].freeze
- EPHEMERAL_TTL_SECS =
7 * 86_400
Class Method Summary collapse
-
.authors ⇒ Object
- Author(s)
0day Inc.
-
.clear(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::Memory.clear(force: true) Requires force:true — protected prefs/SOPs must not vanish via bare clear.
-
.forget(opts = {}) ⇒ Object
rubocop:disable Naming/PredicateMethod.
-
.help ⇒ Object
Display Usage for this Module.
-
.lean!(opts = {}) ⇒ Object
- Supported Method Parameters
result = PWN::Memory.lean!( dry_run: 'optional - Boolean plan only (default false)', value_max_chars: 'optional - truncate values (default VALUE_MAX_CHARS)', ephemeral_ttl_secs: 'optional - drop expired session_* keys' ).
-
.load ⇒ Object
- Supported Method Parameters
memory = PWN::Memory.load.
-
.protected_entry?(opts = {}) ⇒ Boolean
True when a memory key must survive cap eviction / age GC.
-
.recall(opts = {}) ⇒ Object
- Supported Method Parameters
results = PWN::Memory.recall( query: 'optional - string to search keys/values/categories (simple match)', category: 'optional - filter by category', limit: 'optional - max results (default 50)' ).
-
.recall_semantic(opts = {}) ⇒ Object
- Supported Method Parameters
hits = PWN::Memory.recall_semantic(query: 'nmap sweep', limit: 6).
-
.remember(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::Memory.remember( key: 'required - Symbol or String key for the memory fact', value: 'required - The value (any JSON serializable)', category: 'optional - e.g. :fact, :preference, :lesson, :env (default: :fact)', source: 'optional - :human | :reflect | :heuristic | :resolve | :consolidate (M3 provenance)', confidence: 'optional - 0.0..1.0 how sure the writer was (M3)', importance: 'optional - 0.0..1.0 retrieval/eviction weight (M2/M3)', ttl: 'optional - seconds until stale (M3; consolidate evicts stale low-conf first)' ).
-
.save(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::Memory.save(mem: memory_hash).
-
.to_context(opts = {}) ⇒ Object
- Supported Method Parameters
context = PWN::Memory.to_context(limit: 20) (used internally by pwn-ai hook to inject into system prompt).
Class Method Details
.authors ⇒ Object
- Author(s)
0day Inc. support@0dayinc.com
282 283 284 |
# File 'lib/pwn/memory.rb', line 282 public_class_method def self. "AUTHOR(S):\n 0day Inc. <support@0dayinc.com>\n" end |
.clear(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::Memory.clear(force: true)
Requires force:true — protected prefs/SOPs must not vanish via bare clear.
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/pwn/memory.rb', line 181 public_class_method def self.clear(opts = {}) force = if opts.is_a?(Hash) opts[:force] ? true : false else false end raise 'ERROR: refusing Memory.clear without force:true (would drop PROTECT_KEY_PREFIXES/PROTECT_CATEGORIES)' unless force FileUtils.rm_f(MEMORY_FILE) save(mem: {}, force: true) # recreate empty file atomically {} end |
.forget(opts = {}) ⇒ Object
rubocop:disable Naming/PredicateMethod
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/pwn/memory.rb', line 163 public_class_method def self.forget(opts = {}) # rubocop:disable Naming/PredicateMethod key = opts[:key] raise 'ERROR: key is required' if key.nil? force = opts[:force] ? true : false mem = load entry = mem[key.to_sym] raise "ERROR: refusing to forget protected memory key #{key.inspect} (matches PROTECT_KEY_PREFIXES/PROTECT_CATEGORIES; pass force:true)" if entry && protected_entry?(key: key, entry: entry) && !force mem.delete(key.to_sym) # Last-key delete legitimately yields {}; force so empty-guard does not revive it. save(mem: mem, force: mem.empty?) true end |
.help ⇒ Object
Display Usage for this Module
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/pwn/memory.rb', line 287 public_class_method def self.help puts <<~USAGE USAGE: mem = PWN::Memory.load PWN::Memory.remember(key: :user_prefers_ruby, value: 'Always prefer pure Ruby + RestClient patterns', category: :preference) facts = PWN::Memory.recall(query: 'recon', category: :fact, limit: 10) hits = PWN::Memory.recall_semantic(query: 'recon', limit: 6) # embedding-ranked PWN::Memory.forget(key: :some_key) PWN::Memory.forget(key: :operator_pref_x) rescue puts('protected') PWN::Memory.clear(force: true) context_str = PWN::Memory.to_context PWN::Memory.lean!(dry_run: true) # drop expired session_* + truncate values #{self}.authors USAGE end |
.lean!(opts = {}) ⇒ Object
- Supported Method Parameters
result = PWN::Memory.lean!( dry_run: 'optional - Boolean plan only (default false)', value_max_chars: 'optional - truncate values (default VALUE_MAX_CHARS)', ephemeral_ttl_secs: 'optional - drop expired session_* keys' )
Compact overlong values and drop expired ephemeral session_* keys. Never removes protected prefs/SOPs/fix lessons. Safe with empty-save guard.
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/pwn/memory.rb', line 228 public_class_method def self.lean!(opts = {}) dry = opts[:dry_run] ? true : false vmax = (opts[:value_max_chars] || VALUE_MAX_CHARS).to_i ttl_secs = (opts[:ephemeral_ttl_secs] || EPHEMERAL_TTL_SECS).to_i mem = load before_bytes = File.exist?(MEMORY_FILE) ? File.size(MEMORY_FILE) : 0 removed = [] truncated = [] now = Time.now.utc mem.each do |k, v| key = k.to_s next if protected_entry?(key: key, entry: v) if EPHEMERAL_KEY_PREFIXES.any? { |p| key.start_with?(p) } age = begin now - Time.parse(v[:timestamp].to_s) rescue StandardError ttl_secs + 1 end exp = v[:ttl].to_i.positive? ? v[:ttl].to_i : ttl_secs if age > exp removed << key next end end next unless v[:value].is_a?(String) && v[:value].to_s.bytesize > vmax truncated << key v[:value] = "#{v[:value].to_s[0, vmax]}…[compacted]" unless dry end removed.each { |k| mem.delete(k.to_sym) } unless dry save(mem: mem, force: mem.empty?) unless dry || (removed.empty? && truncated.empty?) { removed: removed.length, truncated: truncated.length, remaining: mem.size, removed_keys: removed.first(20), truncated_keys: truncated.first(20), bytes_before: before_bytes, bytes_after: if dry before_bytes else (File.exist?(MEMORY_FILE) ? File.size(MEMORY_FILE) : 0) end, dry_run: dry } end |
.load ⇒ Object
- Supported Method Parameters
memory = PWN::Memory.load
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pwn/memory.rb', line 27 public_class_method def self.load FileUtils.mkdir_p(File.dirname(MEMORY_FILE)) return {} unless File.exist?(MEMORY_FILE) raw = File.read(MEMORY_FILE) return {} if raw.strip.empty? || raw.strip == '{}' JSON.parse(raw, symbolize_names: true) rescue JSON::ParserError, EncodingError => e # Never silently return {} for a non-trivial file — that turns the next # consolidate/save into a full wipe. Quarantine + empty is safer only # when the caller opted in; default is keep last good and warn. warn "[pwn-ai/memory] load parse failed (#{e.class}: #{e.}); refusing empty fallback for #{File.size(MEMORY_FILE)}B file" raise rescue StandardError => e warn "[pwn-ai/memory] load failed: #{e.class}: #{e.}" raise end |
.protected_entry?(opts = {}) ⇒ Boolean
True when a memory key must survive cap eviction / age GC.
210 211 212 213 214 215 216 217 |
# File 'lib/pwn/memory.rb', line 210 public_class_method def self.protected_entry?(opts = {}) key = opts[:key].to_s entry = opts[:entry] || {} return true if PROTECT_KEY_PREFIXES.any? { |p| key.start_with?(p) } return true if PROTECT_CATEGORIES.map(&:to_s).include?(entry[:category].to_s) false end |
.recall(opts = {}) ⇒ Object
- Supported Method Parameters
results = PWN::Memory.recall( query: 'optional - string to search keys/values/categories (simple match)', category: 'optional - filter by category', limit: 'optional - max results (default 50)' )
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/pwn/memory.rb', line 127 public_class_method def self.recall(opts = {}) query = opts[:query].to_s.downcase category = opts[:category] limit = opts[:limit] || 50 mem = load results = mem.select do |k, v| match = true match &&= k.to_s.downcase.include?(query) || v[:value].to_s.downcase.include?(query) || v[:category].to_s.downcase.include?(query) if query && !query.empty? match &&= (v[:category] == category.to_sym) if category match end results.to_a.first(limit).to_h end |
.recall_semantic(opts = {}) ⇒ Object
- Supported Method Parameters
hits = PWN::Memory.recall_semantic(query: 'nmap sweep', limit: 6)
Relevance-ranked recall via PWN::MemoryIndex (local Ollama embeddings
- cosine over ~/.pwn/memory.idx). Falls back to substring .recall when no embedding backend is configured.
149 150 151 152 153 154 155 |
# File 'lib/pwn/memory.rb', line 149 public_class_method def self.recall_semantic(opts = {}) return recall(query: opts[:query], limit: opts[:limit]) unless defined?(PWN::MemoryIndex) && PWN::MemoryIndex.available? PWN::MemoryIndex.recall_semantic(query: opts[:query], limit: opts[:limit]) rescue StandardError recall(query: opts[:query], limit: opts[:limit]) end |
.remember(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::Memory.remember( key: 'required - Symbol or String key for the memory fact', value: 'required - The value (any JSON serializable)', category: 'optional - e.g. :fact, :preference, :lesson, :env (default: :fact)', source: 'optional - :human | :reflect | :heuristic | :resolve | :consolidate (M3 provenance)', confidence: 'optional - 0.0..1.0 how sure the writer was (M3)', importance: 'optional - 0.0..1.0 retrieval/eviction weight (M2/M3)', ttl: 'optional - seconds until stale (M3; consolidate evicts stale low-conf first)' )
94 95 96 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/memory.rb', line 94 public_class_method def self.remember(opts = {}) key = opts[:key] value = opts[:value] category = opts[:category] || :fact raise 'ERROR: key and value are required' if key.nil? || value.nil? mem = load val = value.is_a?(String) ? value.to_s : value val = "#{val.to_s[0, VALUE_MAX_CHARS]}…[compacted]" if val.is_a?(String) && val.bytesize > VALUE_MAX_CHARS entry = { value: val, category: category.to_sym, timestamp: Time.now.utc.iso8601, # M3 — provenance & scoring so Learning.consolidate evicts by # (age/ttl)/(importance×confidence) instead of oldest-first, and # MemoryIndex.recall_semantic ranks by sim × recency × importance. source: (opts[:source] || 'pwn-ai').to_s, confidence: opts[:confidence]&.to_f&.clamp(0.0, 1.0), importance: opts[:importance]&.to_f&.clamp(0.0, 1.0), ttl: opts[:ttl]&.to_i }.compact mem[key.to_sym] = entry save(mem: mem) mem[key.to_sym] end |
.save(opts = {}) ⇒ Object
- Supported Method Parameters
PWN::Memory.save(mem: memory_hash)
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 |
# File 'lib/pwn/memory.rb', line 48 public_class_method def self.save(opts = {}) mem = opts[:mem] ||= {} force = opts[:force] ? true : false FileUtils.mkdir_p(File.dirname(MEMORY_FILE)) # 4.4 — flock + atomic rename (nightly practice × interactive) path = MEMORY_FILE # Guard: never clobber a non-empty memory.json with {} unless force. # Root cause of 2026-08-05 wipe: load-rescue→{} then consolidate/save. if !force && mem.respond_to?(:empty?) && mem.empty? && File.exist?(path) && File.size(path) > 4 warn "[pwn-ai/memory] refusing empty overwrite of #{File.size(path)}B #{path} (pass force:true to clear)" return load_raw_or_empty(path: path) end tmp = File.join(File.dirname(path), ".#{File.basename(path)}.#{Process.pid}.tmp") body = JSON.pretty_generate(mem) File.open(tmp, File::WRONLY | File::CREAT | File::TRUNC, 0o644) do |f| f.flock(File::LOCK_EX) f.write(body) f.flush f.fsync end File.rename(tmp, path) mem ensure FileUtils.rm_f(tmp) if defined?(tmp) && tmp && File.exist?(tmp) end |
.to_context(opts = {}) ⇒ Object
- Supported Method Parameters
context = PWN::Memory.to_context(limit: 20) (used internally by pwn-ai hook to inject into system prompt)
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/pwn/memory.rb', line 197 public_class_method def self.to_context(opts = {}) limit = opts[:limit] || 20 mem = recall(limit: limit) return '' if mem.empty? ctx = "\n\nPERSISTENT MEMORY (cross-session facts, prefs, lessons - use PWN::Memory.remember to store new ones):\n" mem.each do |k, v| ctx += "- #{k} [#{v[:category]} @ #{v[:timestamp]}]: #{v[:value].to_s[0, 300]}\n" end ctx end |