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')
ARCHIVE_FILE =
File.join(Dir.home, '.pwn', 'memory.archive.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
META_RECALL_USER_RX =

Meta prior-turn recall asks (not substantive conversational content). Used so nested "what did you say when I said that?" skips the intermediate recall Q/A and lands on the real user↔assistant pair.

/
  \A\s*(?:and\s+)?(?:
    what\s+did\s+i\s+|
    what\s+was\s+my\s+last|
    how\s+did\s+you\s+respond|
    how\s+did\s+you\s+(?:just\s+)?(?:answer|reply)|
    what\s+(?:was|is)\s+your\s+(?:last|previous|prior)\s+|
    what\s+did\s+you\s+(?:just\s+)?(?:say|answer|reply|respond)|
    remind\s+me\s+what\s+|
    repeat\s+(?:my\s+|your\s+)?(?:last|previous)|
    last\s+thing\s+i\s+said|
    say\s+that\s+again
  )
/ix
META_RECALL_ASST_RX =
/
  \A\s*(?:
    You\s+just\s+said:|
    Immediately\s+prior\s+(?:user\s+message|assistant\s+response):|
    I\s+do\s+not\s+have\s+a\s+prior\s+assistant\s+reply
  )
/ix

Class Method Summary collapse

Class Method Details

.authors ⇒ Object

Author(s)

0day Inc. [email protected]



701
702
703
# File 'lib/pwn/memory.rb', line 701

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <[email protected]>\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.



543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/pwn/memory.rb', line 543

public_class_method def self.clear(opts = {})
  return transaction(path: MEMORY_FILE) { clear(opts) } unless PWN::Memory.transaction_owned?(path: MEMORY_FILE)

  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

.current_session_id(opts = {}) ⇒ Object

Resolve the active pwn-ai session id (Env, then Pry config).



234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/pwn/memory.rb', line 234

public_class_method def self.current_session_id(opts = {})
  sid = opts[:session_id].to_s
  return sid unless sid.empty?

  if defined?(PWN::Env) && PWN::Env.is_a?(Hash)
    sid = PWN::Env.dig(:ai, :session_id).to_s
    return sid unless sid.empty?
  end

  return Pry.config.pwn_ai_session_id.to_s if defined?(Pry) && Pry.respond_to?(:config) && Pry.config.respond_to?(:pwn_ai_session_id)

  ''
end

.find_turn_pair(opts = {}) ⇒ Object

Find a user↔assistant pair in the active session. match: optional substring / quoted utterance to locate the user turn skip_meta: skip pure-recall intermediate pairs (default true) Returns the newest matching pair hash, or nil.



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/pwn/memory.rb', line 453

public_class_method def self.find_turn_pair(opts = {})
  match = opts[:match].to_s.strip
  skip_meta = opts.key?(:skip_meta) ? opts[:skip_meta] : true
  pairs = turn_pairs(
    session_id: opts[:session_id],
    pairs: opts[:pairs] || 12,
    max_chars: opts[:max_chars] || 4_000
  )
  return nil if pairs.empty?

  candidates = pairs
  if skip_meta
    candidates = pairs.reject do |p|
      meta_recall_user?(p[:user_content]) || meta_recall_assistant?(p[:assistant_content])
    end
    candidates = pairs if candidates.empty?
  end

  return candidates.last if match.empty?

  needle = normalize_utterance(match)
  return nil if needle.empty?

  # Prefer exact normalized equality, then include, newest first.
  exact = candidates.reverse.find { |p| normalize_utterance(p[:user_content]) == needle }
  return exact if exact

  candidates.reverse.find do |p|
    body = normalize_utterance(p[:user_content])
    body.include?(needle) || needle.include?(body)
  end
rescue StandardError
  nil
end

.forget(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Memory.forget( key: :some_key, force: 'optional - Boolean bypass protect policy (default false)' )

Refuses PROTECT_KEY_PREFIXES / PROTECT_CATEGORIES unless force:true.



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/pwn/memory.rb', line 523

public_class_method def self.forget(opts = {})
  return transaction(path: MEMORY_FILE) { forget(opts) } unless PWN::Memory.transaction_owned?(path: MEMORY_FILE)

  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



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# File 'lib/pwn/memory.rb', line 706

public_class_method def self.help
  puts "USAGE:
    # Serialize a complete read-modify-write operation.
    #{self}.transaction(path: 'optional - data store path, defaults to memory') { operation }

    # Check current process and thread ownership.
    #{self}.transaction_owned?(path: 'optional - data store path, defaults to memory')

    # Run load and return its result
    #{self}.load

    # Run save and return its result
    #{self}.save(
      mem: 'optional - mem value consumed by #save',
      force: 'optional - force value consumed by #save'
    )

    # Run remember and return its result
    #{self}.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)',
      namespace: 'optional - memory namespace string (defaults to default)'
    )

    # Run recall and return its result
    #{self}.recall(
      query: 'optional - string to search keys/values/categories (simple match)',
      category: 'optional - filter by category (:session = transcript only)',
      limit: 'optional - max results (default 50)',
      session_id: 'optional - current session (default Env/Pry active id)',
      include_session: 'optional - include session value consumed by #recall'
    )

    # Resolve the active pwn-ai session id (Env, then Pry config)
    #{self}.current_session_id(
      session_id: 'optional - session id value consumed by #current_session_id'
    )

    # Walk the current session transcript from the previous assistant response
    #{self}.session_turns(
      session_id: 'optional - session id value consumed by #session_turns',
      query: 'required - search query string',
      limit: 'optional - limit value consumed by #session_turns'
    )

    # Run recent dialog and return its result
    #{self}.recent_dialog(
      session_id: 'optional - session id value consumed by #recent_dialog',
      pairs: 'optional - max user/assistant pairs (default 2)',
      max_chars: 'optional - per-turn truncation (default 1200)'
    )

    # Previous non-empty user message in the active session (what the human
    #{self}.prior_user_message(
      pairs: 'optional - pairs value consumed by #prior_user_message',
      max_chars: 'optional - max chars value consumed by #prior_user_message (defaults to 4_000)',
      skip_meta: 'optional - skip meta value consumed by #prior_user_message',
      session_id: 'optional - session id value consumed by #prior_user_message'
    )

    # Previous non-empty assistant message in the active session
    #{self}.prior_assistant_message(
      pairs: 'optional - pairs value consumed by #prior_assistant_message',
      max_chars: 'optional - max chars value consumed by #prior_assistant_message (defaults to 4_000)',
      skip_meta: 'optional - skip meta value consumed by #prior_assistant_message',
      session_id: 'optional - session id value consumed by #prior_assistant_message'
    )

    # Build chronological user→assistant pairs from the active session
    #{self}.turn_pairs(
      pairs: 'optional - pairs value consumed by #turn_pairs',
      max_chars: 'optional - max chars value consumed by #turn_pairs',
      session_id: 'optional - session id value consumed by #turn_pairs',
      include_orphan_user: 'optional - include orphan user value consumed by #turn_pairs'
    )

    # Find a user↔assistant pair in the active session
    #{self}.find_turn_pair(
      match: 'required - match value consumed by #find_turn_pair',
      skip_meta: 'optional - skip meta value consumed by #find_turn_pair',
      session_id: 'optional - session id value consumed by #find_turn_pair',
      pairs: 'optional - pairs value consumed by #find_turn_pair (defaults to 12)',
      max_chars: 'optional - max chars value consumed by #find_turn_pair (defaults to 4_000)'
    )

    # Run meta recall user and return its result
    #{self}.meta_recall_user?(
      text: 'optional - text value consumed by #meta_recall_user?'
    )

    # Run meta recall assistant and return its result
    #{self}.meta_recall_assistant?(
      text: 'optional - text value consumed by #meta_recall_assistant?'
    )

    # Run normalize utterance and return its result
    #{self}.normalize_utterance(
      text: 'optional - text value consumed by #normalize_utterance'
    )

    # Run recall semantic and return its result
    #{self}.recall_semantic(
      query: 'optional - search query string',
      limit: 'optional - limit value consumed by #recall_semantic'
    )

    # Refuses PROTECT_KEY_PREFIXES / PROTECT_CATEGORIES unless force:true
    #{self}.forget(
      key: 'required - key value consumed by #forget',
      force: 'optional - Boolean bypass protect policy (default false)'
    )

    # Requires force:true — protected prefs/SOPs must not vanish via bare clear
    #{self}.clear(
      force: 'required - force value consumed by #clear'
    )

    # Run to context and return its result
    #{self}.to_context(
      limit: 'optional - limit value consumed by #to_context (defaults to 20)'
    )

    # True when a memory key must survive cap eviction / age GC
    #{self}.protected_entry?(
      key: 'optional - key value consumed by #protected_entry?',
      entry: 'optional - entry value consumed by #protected_entry?'
    )

    # Run lean and return its result
    #{self}.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'
    )

    # Print the AUTHOR(S) string for this module.
    #{self}.authors
  "
  constants.sort
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.



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/pwn/memory.rb', line 645

public_class_method def self.lean!(opts = {})
  return transaction(path: MEMORY_FILE) { lean!(opts) } unless PWN::Memory.transaction_owned?(path: MEMORY_FILE)

  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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pwn/memory.rb', line 54

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.message}); refusing empty fallback for #{File.size(MEMORY_FILE)}B file"
  raise
rescue StandardError => e
  warn "[pwn-ai/memory] load failed: #{e.class}: #{e.message}"
  raise
end

.meta_recall_assistant?(opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


493
494
495
496
# File 'lib/pwn/memory.rb', line 493

public_class_method def self.meta_recall_assistant?(opts = {})
  text = opts.is_a?(Hash) ? opts[:text] : opts
  text.to_s.match?(META_RECALL_ASST_RX)
end

.meta_recall_user?(opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


488
489
490
491
# File 'lib/pwn/memory.rb', line 488

public_class_method def self.meta_recall_user?(opts = {})
  text = opts.is_a?(Hash) ? opts[:text] : opts
  text.to_s.match?(META_RECALL_USER_RX)
end

.normalize_utterance(opts = {}) ⇒ Object



498
499
500
501
# File 'lib/pwn/memory.rb', line 498

public_class_method def self.normalize_utterance(opts = {})
  text = opts.is_a?(Hash) ? opts[:text] : opts
  text.to_s.downcase.gsub(/[`"'“”‘’]/, '').gsub(/\s+/, ' ').strip
end

.prior_assistant_message(opts = {}) ⇒ Object

Previous non-empty assistant message in the active session. skip_meta:true walks past canned recall answers so the substantive reply stays findable after nested "what did you say when…" follow-ups.



395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/pwn/memory.rb', line 395

public_class_method def self.prior_assistant_message(opts = {})
  pairs = (opts[:pairs] || 6).to_i
  max_chars = opts[:max_chars] || 4_000
  skip_meta = opts.key?(:skip_meta) ? opts[:skip_meta] : false
  dialog = recent_dialog(session_id: opts[:session_id], pairs: pairs, max_chars: max_chars)
  dialog.reverse.find do |t|
    next false unless t[:role].to_s == 'assistant'
    next false if skip_meta && meta_recall_assistant?(t[:content])

    true
  end
rescue StandardError
  nil
end

.prior_user_message(opts = {}) ⇒ Object

Previous non-empty user message in the active session (what the human "just said" before the current request). Nil when none. skip_meta:true walks past pure-recall intermediate asks.



377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/pwn/memory.rb', line 377

public_class_method def self.prior_user_message(opts = {})
  pairs = (opts[:pairs] || 6).to_i
  max_chars = opts[:max_chars] || 4_000
  skip_meta = opts.key?(:skip_meta) ? opts[:skip_meta] : false
  dialog = recent_dialog(session_id: opts[:session_id], pairs: pairs, max_chars: max_chars)
  dialog.reverse.find do |t|
    next false unless t[:role].to_s == 'user'
    next false if skip_meta && meta_recall_user?(t[:content])

    true
  end
rescue StandardError
  nil
end

.protected_entry?(opts = {}) ⇒ Boolean

True when a memory key must survive cap eviction / age GC.

Returns:

  • (Boolean)


627
628
629
630
631
632
633
634
# File 'lib/pwn/memory.rb', line 627

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 (:session = transcript only)', limit: 'optional - max results (default 50)', session_id: 'optional - current session (default Env/Pry active id)' )

Returns session turns first (previous assistant response -> older, current session only, empty/invalid skipped), then durable memory newest-first.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/pwn/memory.rb', line 176

public_class_method def self.recall(opts = {})
  query = opts[:query].to_s.downcase
  category = opts[:category]
  limit = (opts[:limit] || 50).to_i
  limit = 50 if limit <= 0
  session_id = opts[:session_id]
  session_id = current_session_id if session_id.to_s.empty?
  # Default ON for memory_recall tool UX. to_context passes false so the
  # injected MEMORY block stays durable cross-session facts only.
  include_session = if opts.key?(:include_session)
                      opts[:include_session] ? true : false
                    else
                      true
                    end

  ordered = {}

  # 1) Current-session turns first: start at previous response, walk older.
  #    Skips empty/invalid turns. Stays scoped to this session only.
  if include_session && !(category && category.to_sym != :session)
    session_turns(session_id: session_id, query: query, limit: limit).each do |entry|
      break if ordered.length >= limit

      key = entry[:key]
      ordered[key] = entry.except(:key)
    end
  end

  # 2) Durable ~/.pwn/memory.json (newest timestamp first) fills remaining slots.
  if ordered.length < limit && !(category && category.to_sym == :session)
    mem = load
    durable = mem.select do |k, v|
      match = true
      if query && !query.empty?
        match &&= k.to_s.downcase.include?(query) ||
                  v[:value].to_s.downcase.include?(query) ||
                  v[:category].to_s.downcase.include?(query)
      end
      match &&= (v[:category].to_s == category.to_s) if category
      match
    end
    durable_sorted = durable.sort_by do |k, v|
      [-Time.parse(v[:timestamp].to_s).to_i, k.to_s]
    rescue StandardError
      [0, k.to_s]
    end
    durable_sorted.each do |k, v|
      break if ordered.length >= limit
      next if ordered.key?(k)

      ordered[k] = v
    end
  end

  ordered
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.


509
510
511
512
513
514
515
# File 'lib/pwn/memory.rb', line 509

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

.recent_dialog(opts = {}) ⇒ Object

Supported Method Parameters

dialog = PWN::Memory.recent_dialog( session_id: 'optional', pairs: 'optional - max user/assistant pairs (default 2)', max_chars: 'optional - per-turn truncation (default 1200)' )

Chronological user/assistant turns ending at the previous assistant response (current session only). Empty/invalid/tool noise skipped.



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/pwn/memory.rb', line 324

public_class_method def self.recent_dialog(opts = {})
  pairs = (opts[:pairs] || 2).to_i
  pairs = 2 if pairs <= 0
  max_chars = (opts[:max_chars] || 1_200).to_i
  max_chars = 1_200 if max_chars <= 0
  turns = session_turns(session_id: opts[:session_id], limit: pairs * 6)
  dialog = turns.select { |t| %w[user assistant].include?(t[:role].to_s) }
  # session_turns is previous-assistant-first (newest → older). Take the
  # newest 2*pairs dialog turns and flip to chronological for prompts.
  slice = dialog.first(pairs * 2).reverse
  slice.map do |t|
    body = t[:value].to_s
    body = "#{body[0, max_chars]}…[truncated]" if body.length > max_chars
    {
      role: t[:role].to_s,
      content: body,
      timestamp: t[:timestamp],
      session_id: t[:session_id]
    }
  end
rescue StandardError
  []
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)' )



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/pwn/memory.rb', line 134

public_class_method def self.remember(opts = {})
  return transaction(path: MEMORY_FILE) { remember(opts) } unless PWN::Memory.transaction_owned?(path: MEMORY_FILE)

  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,
    namespace: (opts[:namespace] || 'default').to_s,
    last_hit: Time.now.utc.iso8601,
    hits: 1
  }.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)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pwn/memory.rb', line 75

public_class_method def self.save(opts = {})
  return transaction(path: MEMORY_FILE) { save(opts) } unless PWN::Memory.transaction_owned?(path: MEMORY_FILE)

  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
  atomic_write(path: path, body: JSON.pretty_generate(mem))
  mem
end

.session_turns(opts = {}) ⇒ Object

Walk the current session transcript from the previous assistant response backward (previous -> older). Empty/invalid turns are skipped. Returns Array of hashes with :key plus entry fields (role/value/...).



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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/pwn/memory.rb', line 251

public_class_method def self.session_turns(opts = {})
  sid = opts[:session_id].to_s
  sid = current_session_id if sid.empty?
  return [] if sid.empty?
  return [] unless defined?(PWN::Sessions)

  query = opts[:query].to_s.downcase
  limit = (opts[:limit] || 50).to_i
  limit = 50 if limit <= 0

  rows = PWN::Sessions.load(session_id: sid)
  return [] if rows.nil? || rows.empty?

  valid = []
  rows.each_with_index do |e, idx|
    next unless e.is_a?(Hash)

    role = e[:role].to_s
    content = e[:content].to_s
    next if content.strip.empty?
    next unless %w[assistant user tool system observation].include?(role)
    # Skip the bootstrap system line when it has no useful body.
    next if role == 'system' && content.match?(/\ASession started:/i)

    valid << {
      idx: idx,
      role: role,
      content: content,
      timestamp: e[:timestamp]
    }
  end
  return [] if valid.empty?

  # Previous response = most recent non-empty assistant turn in-session.
  last_asst = valid.rindex { |e| e[:role] == 'assistant' }
  # If no assistant yet, still walk backward from the latest valid turn.
  start_at = last_asst || (valid.length - 1)
  backward = valid[0..start_at].reverse

  hits = []
  backward.each do |e|
    break if hits.length >= limit

    content = e[:content]
    role = e[:role]
    if query && !query.empty? && !(content.downcase.include?(query) ||
                  role.include?(query) ||
                  "session_#{sid}".include?(query))
      next
    end

    hits << {
      key: :"session_turn_#{sid}_#{e[:idx]}",
      value: content,
      category: :session,
      role: role,
      timestamp: e[:timestamp],
      session_id: sid,
      source: 'session_backward'
    }
  end
  hits
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)



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/pwn/memory.rb', line 561

public_class_method def self.to_context(opts = {})
  return transaction(path: MEMORY_FILE) { to_context(opts) } unless PWN::Memory.transaction_owned?(path: MEMORY_FILE)

  limit = (opts[:limit] || 12).to_i
  limit = 12 if limit <= 0
  mem = load
  now = Time.now.utc
  live = {}
  expired = {}
  mem.each do |k, v|
    next unless v.is_a?(Hash)

    ttl = v[:ttl].to_i
    ts = begin
      Time.parse(v[:timestamp].to_s)
    rescue StandardError
      now
    end
    if ttl.positive? && (now - ts) > ttl
      expired[k] = v
    else
      live[k] = v
    end
  end
  archive_expired!(rows: expired) unless expired.empty?
  ranked = live.sort_by do |_k, v|
    hits = v[:hits].to_i
    hits = 1 if hits <= 0
    rec = begin
      Time.parse((v[:last_hit] || v[:timestamp]).to_s).to_f
    rescue StandardError
      0
    end
    -(hits * rec)
  end.first(limit)
  return '' if ranked.empty?

  ctx = "\n\nPERSISTENT MEMORY (cross-session facts, prefs, lessons - use PWN::Memory.remember to store new ones):\n"
  ranked.each do |k, v|
    ctx += "- #{k} [#{v[:category]} @ #{v[:timestamp]}]: #{v[:value].to_s[0, 300]}\n"
  end
  ctx
end

.transaction(opts = {}) ⇒ Object

Permanent writer inode; nested calls share ownership only in this thread and process. Never unlink this lock when replacing a data file.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pwn/memory.rb', line 29

public_class_method def self.transaction(opts = {})
  path = File.expand_path(opts[:path] || MEMORY_FILE)
  return yield if transaction_owned?(path: path)

  FileUtils.mkdir_p(File.dirname(path))
  File.open("#{path}.write.lock", File::RDWR | File::CREAT | File::NOFOLLOW, 0o600) do |lock|
    lock.flock(File::LOCK_EX)
    owners = (Thread.current[:pwn_store_transactions] ||= {})
    owners[path] = Process.pid
    begin
      yield
    ensure
      owners.delete(path)
    end
  end
end

.transaction_owned?(opts = {}) ⇒ Boolean

Test ownership without mistaking an inherited post-fork marker for a lock.

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/pwn/memory.rb', line 47

public_class_method def self.transaction_owned?(opts = {})
  path = File.expand_path(opts[:path] || MEMORY_FILE)
  Thread.current[:pwn_store_transactions]&.fetch(path, nil) == Process.pid
end

.turn_pairs(opts = {}) ⇒ Object

Build chronological user→assistant pairs from the active session. Each pair is { user:, assistant:, user_content:, assistant_content: }. Solo user turns (no reply yet) are omitted unless include_orphan_user:true.



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/pwn/memory.rb', line 413

public_class_method def self.turn_pairs(opts = {})
  pairs_n = (opts[:pairs] || 8).to_i
  pairs_n = 8 if pairs_n <= 0
  max_chars = (opts[:max_chars] || 4_000).to_i
  dialog = recent_dialog(session_id: opts[:session_id], pairs: pairs_n, max_chars: max_chars)
  out = []
  i = 0
  while i < dialog.length
    t = dialog[i]
    if t[:role].to_s == 'user'
      nxt = dialog[i + 1]
      if nxt && nxt[:role].to_s == 'assistant'
        out << {
          user: t,
          assistant: nxt,
          user_content: t[:content].to_s,
          assistant_content: nxt[:content].to_s
        }
        i += 2
        next
      elsif opts[:include_orphan_user]
        out << {
          user: t,
          assistant: nil,
          user_content: t[:content].to_s,
          assistant_content: nil
        }
      end
    end
    i += 1
  end
  out
rescue StandardError
  []
end