pwn 0.5.803 → 0.5.805
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +4 -4
- data/documentation/Skills-Memory-Learning.md +7 -0
- data/lib/pwn/ai/agent/dispatch.rb +54 -1
- data/lib/pwn/ai/agent/loop.rb +3 -3
- data/lib/pwn/ai/agent/mission_namespace.rb +26 -4
- data/lib/pwn/ai/agent/request_runtime.rb +7 -0
- data/lib/pwn/ai/agent/tools/memory.rb +6 -11
- data/lib/pwn/ai/ollama.rb +10 -2
- data/lib/pwn/ai/open_web_ui.rb +7 -32
- data/lib/pwn/ai.rb +44 -0
- data/lib/pwn/memory.rb +9 -4
- data/lib/pwn/plugins/repl/ai_console.rb +152 -17
- data/lib/pwn/version.rb +1 -1
- data/spec/conventions_spec.rb +9 -7
- data/spec/integration/harness_evaluation_spec.rb +16 -24
- data/spec/lib/pwn/ai/agent/dispatch_spec.rb +80 -0
- data/spec/lib/pwn/ai/agent/mission_namespace_spec.rb +2 -1
- data/spec/lib/pwn/ai/agent/policy_spec.rb +3 -2
- data/spec/lib/pwn/ai/agent/result_spec.rb +6 -1
- data/spec/lib/pwn/ai/agent/tools/memory_spec.rb +7 -0
- data/spec/lib/pwn/ai/ollama_spec.rb +27 -0
- data/spec/lib/pwn/ai/open_web_ui_spec.rb +5 -5
- data/spec/lib/pwn/ai/provider_review_history_stream_regression_spec.rb +32 -0
- data/spec/lib/pwn/banner_spec.rb +67 -38
- data/spec/lib/pwn/cron_spec.rb +2 -39
- data/spec/lib/pwn/memory_spec.rb +46 -0
- data/spec/lib/pwn/plugins/android_review_fixes_spec.rb +14 -6
- data/spec/lib/pwn/plugins/android_transport_audit_spec.rb +2 -1
- data/spec/lib/pwn/plugins/artifact_registry_spec.rb +7 -3
- data/spec/lib/pwn/plugins/jobs_spec.rb +10 -1
- data/spec/lib/pwn/plugins/repl/ai_console_spec.rb +335 -15
- data/spec/lib/pwn/sdr/decoder/lora_spec.rb +36 -27
- data/spec/lib/pwn/setup_spec.rb +4 -19
- data/spec/spec_helper.rb +8 -4
- data/spec/support/harness/cli_faults.rb +2 -1
- data/spec/support/mission_pipe_queue.rb +12 -1
- data/spec/support/sandbox.rb +27 -0
- data/third_party/pwn_rdoc.jsonl +15 -1
- metadata +9 -9
|
@@ -7,6 +7,7 @@ require 'fcntl'
|
|
|
7
7
|
require 'digest'
|
|
8
8
|
require 'fileutils'
|
|
9
9
|
require 'json'
|
|
10
|
+
require 'socket'
|
|
10
11
|
require 'tmpdir'
|
|
11
12
|
require 'tempfile'
|
|
12
13
|
require 'yaml'
|
|
@@ -19,6 +20,8 @@ module PWN
|
|
|
19
20
|
module REPL
|
|
20
21
|
# Single-owner fullscreen console: wrapped completion and private /vault edit; Ctrl+S saves encrypted, Esc cancels.
|
|
21
22
|
module AIConsole
|
|
23
|
+
MEMORY_DECISION = %r{\A[[:space:]]*/memory[[:space:]]+(?:approve|decline)\b}
|
|
24
|
+
|
|
22
25
|
public_class_method def self.run(opts = {})
|
|
23
26
|
input = opts[:input] || $stdin
|
|
24
27
|
output = opts[:output] || $stdout
|
|
@@ -386,7 +389,7 @@ module PWN
|
|
|
386
389
|
|
|
387
390
|
def preload(lines)
|
|
388
391
|
Array(lines).each do |line|
|
|
389
|
-
next if line.to_s.strip.empty? || line.match?(%r{\A\s*/input(?:\s|\z)}) || @history.last == line
|
|
392
|
+
next if line.to_s.strip.empty? || line.match?(%r{\A\s*/input(?:\s|\z)}) || line.match?(MEMORY_DECISION) || @history.last == line
|
|
390
393
|
|
|
391
394
|
@history << line.dup
|
|
392
395
|
end
|
|
@@ -457,7 +460,7 @@ module PWN
|
|
|
457
460
|
|
|
458
461
|
def submit
|
|
459
462
|
line = @text.dup
|
|
460
|
-
@history << line.dup unless line.strip.empty? || @history.last == line || line.match?(%r{\A\s*/input(?:\s|\z)})
|
|
463
|
+
@history << line.dup unless line.strip.empty? || @history.last == line || line.match?(%r{\A\s*/input(?:\s|\z)}) || line.match?(MEMORY_DECISION)
|
|
461
464
|
@index = @history.length
|
|
462
465
|
replace('')
|
|
463
466
|
line
|
|
@@ -481,7 +484,62 @@ module PWN
|
|
|
481
484
|
end
|
|
482
485
|
end
|
|
483
486
|
|
|
484
|
-
#
|
|
487
|
+
# Child-side rendezvous for one exact memory_remember call, never tool stdin.
|
|
488
|
+
class MemoryReview
|
|
489
|
+
WAIT_SECONDS = 180
|
|
490
|
+
|
|
491
|
+
def initialize(events, generation)
|
|
492
|
+
@events = events
|
|
493
|
+
@generation = generation
|
|
494
|
+
@lock = Mutex.new
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
def call(fields)
|
|
498
|
+
return false unless fields.is_a?(Hash) && fields.size == 3 && %i[key value category].all? { |key| fields[key].is_a?(String) }
|
|
499
|
+
return false unless PWN::Redaction.redact(value: fields) == fields
|
|
500
|
+
|
|
501
|
+
# No indentation: wrap_command must not insert spaces into a continued value.
|
|
502
|
+
display = JSON.pretty_generate(fields, ascii_only: true, indent: '').gsub("\x7f", '\\u007f')
|
|
503
|
+
return false unless PWN::Redaction.redact(value: display) == display
|
|
504
|
+
|
|
505
|
+
ticket = SecureRandom.hex(8)
|
|
506
|
+
reply = Queue.new
|
|
507
|
+
@lock.synchronize do
|
|
508
|
+
return false if @closed || @reply
|
|
509
|
+
|
|
510
|
+
@ticket = ticket
|
|
511
|
+
@reply = reply
|
|
512
|
+
end
|
|
513
|
+
begin
|
|
514
|
+
@events << [:memory_review, { ticket: ticket, display: display }]
|
|
515
|
+
approved = reply.pop(timeout: WAIT_SECONDS) == true
|
|
516
|
+
@events << [:memory_review_closed, ticket]
|
|
517
|
+
approved
|
|
518
|
+
ensure
|
|
519
|
+
@lock.synchronize { @reply = @ticket = nil }
|
|
520
|
+
end
|
|
521
|
+
rescue IOError, SystemCallError, JSON::GeneratorError, ArgumentError
|
|
522
|
+
false
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
def decide(frame)
|
|
526
|
+
@lock.synchronize do
|
|
527
|
+
return unless @reply && !@reply.closed? && frame[:type] == 'memory_decision' && frame[:generation] == @generation && frame[:ticket] == @ticket && %w[approve decline].include?(frame[:decision])
|
|
528
|
+
|
|
529
|
+
@reply << (frame[:decision] == 'approve')
|
|
530
|
+
@reply.close
|
|
531
|
+
end
|
|
532
|
+
end
|
|
533
|
+
|
|
534
|
+
def close
|
|
535
|
+
@lock.synchronize do
|
|
536
|
+
@closed = true
|
|
537
|
+
@reply&.close
|
|
538
|
+
end
|
|
539
|
+
end
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
# Parent-side control endpoint; cancellation belongs to the process owner.
|
|
485
543
|
class MissionControl
|
|
486
544
|
def initialize(io)
|
|
487
545
|
@io = io
|
|
@@ -492,6 +550,20 @@ module PWN
|
|
|
492
550
|
@io.flush
|
|
493
551
|
end
|
|
494
552
|
|
|
553
|
+
def memory_decision(generation, ticket, decision)
|
|
554
|
+
frame = "#{JSON.generate(type: 'memory_decision', generation: generation, ticket: ticket, decision: decision)}\n"
|
|
555
|
+
return if @io.write_nonblock(frame, exception: false) == frame.bytesize
|
|
556
|
+
|
|
557
|
+
close
|
|
558
|
+
raise IOError, 'Incomplete memory decision'
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
def close
|
|
562
|
+
@io.shutdown(Socket::SHUT_WR) unless @io.closed?
|
|
563
|
+
ensure
|
|
564
|
+
@io.close unless @io.closed?
|
|
565
|
+
end
|
|
566
|
+
|
|
495
567
|
def phase
|
|
496
568
|
'mission process'
|
|
497
569
|
end
|
|
@@ -657,6 +729,15 @@ module PWN
|
|
|
657
729
|
next if generation && generation != @mission_generation && generation != @swarm_generation
|
|
658
730
|
|
|
659
731
|
case type
|
|
732
|
+
when :memory_review, :memory_review_closed
|
|
733
|
+
next unless generation && generation == @mission_generation && @running && !@command && !@cancelling && !@leaving
|
|
734
|
+
|
|
735
|
+
if type == :memory_review
|
|
736
|
+
present_memory_review(text, generation)
|
|
737
|
+
elsif @memory_review == [generation, text]
|
|
738
|
+
@memory_review = nil
|
|
739
|
+
add(:command_notice, 'Memory review closed without a decision.')
|
|
740
|
+
end
|
|
660
741
|
when :command_state
|
|
661
742
|
if text[:ai].is_a?(Hash)
|
|
662
743
|
text[:ai][:active] = text[:ai][:active].to_sym if PWN::Env.dig(:ai, :active).is_a?(Symbol) && text[:ai][:active].is_a?(String)
|
|
@@ -671,9 +752,10 @@ module PWN
|
|
|
671
752
|
@worker.join
|
|
672
753
|
finish_request_metrics
|
|
673
754
|
@running = false
|
|
755
|
+
@memory_review = nil
|
|
756
|
+
@control&.close
|
|
674
757
|
@control = nil
|
|
675
758
|
@mission = nil
|
|
676
|
-
@mission_commands&.close unless @mission_commands&.closed?
|
|
677
759
|
when :operator_loaded
|
|
678
760
|
generation, data = text
|
|
679
761
|
if @operator_view && @operator_view[:generation] == generation
|
|
@@ -729,11 +811,12 @@ module PWN
|
|
|
729
811
|
@title_worker.report_on_exception = false
|
|
730
812
|
end
|
|
731
813
|
|
|
732
|
-
def add(type, text)
|
|
814
|
+
def add(type, text, exact: false)
|
|
733
815
|
command_notice = type == :command_notice
|
|
734
816
|
type = :notice if command_notice
|
|
735
817
|
clean = PWN::Redaction.redact(value: text.to_s)
|
|
736
818
|
clean = clean.gsub(/\e\][^\a]*(?:\a|\e\\)/, '').gsub(%r{\e\[[0-?]*[ -/]*[@-~]}, '').gsub(/[\x00-\x08\x0b-\x1f\x7f]/, '')
|
|
819
|
+
return false if exact && clean != text
|
|
737
820
|
return if clean.strip.empty?
|
|
738
821
|
|
|
739
822
|
@event_count += 1
|
|
@@ -749,6 +832,7 @@ module PWN
|
|
|
749
832
|
@scroll = [@scroll - removed[3].fetch(:rows, []).length, 0].max if @scroll
|
|
750
833
|
end
|
|
751
834
|
PWN::Plugins::Log.mirror_tui!(msg: "#{type.to_s.upcase}\n#{clean}") if @pry.config.pwn_ai_debug && type != :notice
|
|
835
|
+
true
|
|
752
836
|
end
|
|
753
837
|
|
|
754
838
|
private_class_method def self.mission_entry(opts = {})
|
|
@@ -771,16 +855,16 @@ module PWN
|
|
|
771
855
|
end
|
|
772
856
|
|
|
773
857
|
def with_stream_preview(events)
|
|
774
|
-
previous = Thread.current[:
|
|
775
|
-
Thread.current[:
|
|
858
|
+
previous = Thread.current[:pwn_stream_preview]
|
|
859
|
+
Thread.current[:pwn_stream_preview] = lambda do |provider, text, pending|
|
|
776
860
|
visible = text.gsub(/[\x00-\x08\x0B-\x1F\x7F-\u009F]/, '')
|
|
777
861
|
events << [:assistant, visible] unless visible.empty?
|
|
778
862
|
status = pending ? 'A requested action was not processed.' : 'No actions from this reply were processed.'
|
|
779
|
-
raise Control::PreviewShown, "
|
|
863
|
+
raise Control::PreviewShown, "#{provider} reply recovered for display only. #{status} This turn has ended."
|
|
780
864
|
end
|
|
781
865
|
yield
|
|
782
866
|
ensure
|
|
783
|
-
Thread.current[:
|
|
867
|
+
Thread.current[:pwn_stream_preview] = previous
|
|
784
868
|
end
|
|
785
869
|
|
|
786
870
|
def mission_request_child(data, writer, commands)
|
|
@@ -793,8 +877,20 @@ module PWN
|
|
|
793
877
|
control = Control.new(input: @input, output: output)
|
|
794
878
|
Thread.current[:pwn_steering_input] = control
|
|
795
879
|
Thread.current[:pwn_usage_observer] = ->(usage) { events << [:usage, usage] }
|
|
796
|
-
|
|
797
|
-
|
|
880
|
+
review = MemoryReview.new(events, generation)
|
|
881
|
+
command_reader = Thread.new do
|
|
882
|
+
commands.each_line do |line|
|
|
883
|
+
break unless line.end_with?("\n")
|
|
884
|
+
|
|
885
|
+
frame = JSON.parse(line, symbolize_names: true)
|
|
886
|
+
control.submit(frame) if frame.is_a?(String)
|
|
887
|
+
review.decide(frame) if frame.is_a?(Hash)
|
|
888
|
+
end
|
|
889
|
+
rescue IOError, SystemCallError, JSON::ParserError
|
|
890
|
+
# A lost or invalid command channel cannot grant approval.
|
|
891
|
+
nil
|
|
892
|
+
ensure
|
|
893
|
+
review.close
|
|
798
894
|
end
|
|
799
895
|
begin
|
|
800
896
|
workspace = File.realpath(Dir.pwd)
|
|
@@ -812,10 +908,14 @@ module PWN
|
|
|
812
908
|
end
|
|
813
909
|
end
|
|
814
910
|
final = with_stream_preview(events) do
|
|
911
|
+
previous = Thread.current[:pwn_review_memory]
|
|
912
|
+
Thread.current[:pwn_review_memory] = review.method(:call)
|
|
815
913
|
PWN::AI::Agent::Loop.run(request: request, session_id: session_id,
|
|
816
914
|
enabled_toolsets: PWN::Env.dig(:ai, :agent, :toolsets),
|
|
817
915
|
on_tool: on_tool, steering: control, debug: @pry.config.pwn_ai_debug,
|
|
818
916
|
trace: @pry.config.pwn_ai_trace, debug_tee: output)
|
|
917
|
+
ensure
|
|
918
|
+
Thread.current[:pwn_review_memory] = previous
|
|
819
919
|
end
|
|
820
920
|
promoted = Thread.current[:pwn_thinking_promoted] && final.to_s.strip == Thread.current[:pwn_last_thinking].to_s.strip
|
|
821
921
|
events << [:assistant, final.to_s] unless promoted
|
|
@@ -835,6 +935,8 @@ module PWN
|
|
|
835
935
|
end
|
|
836
936
|
events << [:warning, warning]
|
|
837
937
|
ensure
|
|
938
|
+
review.close
|
|
939
|
+
command_reader.kill.join
|
|
838
940
|
output.flush
|
|
839
941
|
writer.close
|
|
840
942
|
end
|
|
@@ -887,20 +989,22 @@ module PWN
|
|
|
887
989
|
begin_request_metrics
|
|
888
990
|
add(:operator, request)
|
|
889
991
|
reader, writer = IO.pipe
|
|
890
|
-
commands,
|
|
992
|
+
commands, sender = PWN::AI::Agent::MissionNamespace.control_socket_pair
|
|
993
|
+
commands.shutdown(Socket::SHUT_WR)
|
|
994
|
+
@control = MissionControl.new(sender)
|
|
891
995
|
@mission = PWN::Plugins::AISandbox::MissionProcess.new(
|
|
892
996
|
descriptor: { operation: :console, request: request, generation: generation, session_id: session_id, title_config: title_config, config: mission_config },
|
|
893
997
|
fds: { 4 => writer, 5 => @input, 6 => commands }
|
|
894
998
|
)
|
|
895
999
|
writer.close
|
|
896
1000
|
commands.close
|
|
897
|
-
|
|
1001
|
+
|
|
898
1002
|
@running = true
|
|
899
1003
|
mission = @mission
|
|
900
1004
|
@worker = Thread.new do
|
|
901
1005
|
reader.each_line do |line|
|
|
902
1006
|
tag, type, text = JSON.parse(line, symbolize_names: true)
|
|
903
|
-
next unless tag == generation && %w[task thinking tool result assistant notice warning session_title usage].include?(type)
|
|
1007
|
+
next unless tag == generation && %w[task thinking tool result assistant notice warning session_title usage memory_review memory_review_closed].include?(type)
|
|
904
1008
|
|
|
905
1009
|
@events << [type.to_sym, text, tag]
|
|
906
1010
|
end
|
|
@@ -913,9 +1017,9 @@ module PWN
|
|
|
913
1017
|
end
|
|
914
1018
|
@worker.report_on_exception = false
|
|
915
1019
|
rescue StandardError => e
|
|
916
|
-
[reader, writer, commands,
|
|
1020
|
+
[reader, writer, commands, sender].compact.each { |io| io.close unless io.closed? }
|
|
917
1021
|
@mission&.close
|
|
918
|
-
@mission = nil
|
|
1022
|
+
@mission = @control = nil
|
|
919
1023
|
@running = false
|
|
920
1024
|
add(:warning, "Mission not started: #{e.message}")
|
|
921
1025
|
end
|
|
@@ -925,9 +1029,10 @@ module PWN
|
|
|
925
1029
|
# reaps namespace PID 1, including all provider and tool descendants.
|
|
926
1030
|
@cancelling = true
|
|
927
1031
|
@mission_generation = nil
|
|
1032
|
+
@memory_review = nil
|
|
928
1033
|
@mission.close
|
|
929
1034
|
@worker&.join
|
|
930
|
-
@
|
|
1035
|
+
@control&.close
|
|
931
1036
|
@tool_input&.close unless @tool_input&.closed?
|
|
932
1037
|
@mission = @control = nil
|
|
933
1038
|
@running = false
|
|
@@ -1029,6 +1134,7 @@ module PWN
|
|
|
1029
1134
|
# Preserve normal request text and the exact internal title bytes.
|
|
1030
1135
|
line = line.gsub(/\A[[:space:]]+|[[:space:]]+\z/, '') if line.match?(%r{\A[[:space:]]*/})
|
|
1031
1136
|
return if line.strip.empty?
|
|
1137
|
+
return decide_memory(line) if line.match?(MEMORY_DECISION)
|
|
1032
1138
|
|
|
1033
1139
|
# Pry owns the existing pwn_history format, append and deduplication.
|
|
1034
1140
|
# Curses bypasses Pry's input evaluator, so record once here, not on recall.
|
|
@@ -1087,6 +1193,35 @@ module PWN
|
|
|
1087
1193
|
add(:warning, "#{e.class}: #{e.message}")
|
|
1088
1194
|
end
|
|
1089
1195
|
|
|
1196
|
+
def present_memory_review(event, generation)
|
|
1197
|
+
return if @memory_review || !event.is_a?(Hash)
|
|
1198
|
+
|
|
1199
|
+
ticket = event[:ticket]
|
|
1200
|
+
return unless ticket.is_a?(String) && ticket.match?(/\A[0-9a-f]{16}\z/)
|
|
1201
|
+
|
|
1202
|
+
display = event[:display]
|
|
1203
|
+
prompt = "Review this one memory_remember write (escaped JSON):\n#{display}\nType /memory approve #{ticket} or /memory decline #{ticket}."
|
|
1204
|
+
if display.is_a?(String) && display.match?(/\A[\x20-\x7e\n]+\z/) && add(:command_notice, prompt, exact: true)
|
|
1205
|
+
@memory_review = [generation, ticket]
|
|
1206
|
+
else
|
|
1207
|
+
@control.memory_decision(generation, ticket, 'decline')
|
|
1208
|
+
add(:command_notice, 'Memory write declined: the exact proposal could not be displayed.')
|
|
1209
|
+
end
|
|
1210
|
+
rescue IOError, SystemCallError
|
|
1211
|
+
add(:warning, 'Memory review closed: mission control channel disconnected.')
|
|
1212
|
+
end
|
|
1213
|
+
|
|
1214
|
+
def decide_memory(line)
|
|
1215
|
+
decision, ticket = line.match(%r{\A/memory +(approve|decline) +([0-9a-f]{16})\z})&.captures
|
|
1216
|
+
return add(:command_notice, 'No matching pending memory review.') unless ticket && @running && @memory_review == [@mission_generation, ticket]
|
|
1217
|
+
|
|
1218
|
+
@memory_review = nil
|
|
1219
|
+
@control.memory_decision(@mission_generation, ticket, decision)
|
|
1220
|
+
add(:command_notice, 'Memory decision sent.')
|
|
1221
|
+
rescue IOError, SystemCallError
|
|
1222
|
+
add(:warning, 'Memory decision not delivered: mission control channel closed.')
|
|
1223
|
+
end
|
|
1224
|
+
|
|
1090
1225
|
def busy_command(line)
|
|
1091
1226
|
if line.match?(%r{\A/steer(?:\s|$)})
|
|
1092
1227
|
if @control.nil?
|
data/lib/pwn/version.rb
CHANGED
data/spec/conventions_spec.rb
CHANGED
|
@@ -153,9 +153,10 @@ module PWNConventions
|
|
|
153
153
|
|
|
154
154
|
def method_body_excerpt(lines, idx)
|
|
155
155
|
indent = lines[idx][/^[ \t]*/]
|
|
156
|
+
terminator = /^#{Regexp.escape(indent)}end\b/
|
|
156
157
|
buf = []
|
|
157
158
|
((idx + 1)...lines.length).each do |i|
|
|
158
|
-
break if lines[i].match?(
|
|
159
|
+
break if lines[i].match?(terminator)
|
|
159
160
|
|
|
160
161
|
buf << lines[i]
|
|
161
162
|
break if buf.length >= 120
|
|
@@ -235,9 +236,10 @@ module PWNConventions
|
|
|
235
236
|
return '' unless idx
|
|
236
237
|
|
|
237
238
|
indent = lines[idx][/^[ \t]*/]
|
|
239
|
+
terminator = /^#{Regexp.escape(indent)}end\b/
|
|
238
240
|
buf = []
|
|
239
241
|
((idx + 1)...lines.length).each do |i|
|
|
240
|
-
break if lines[i].match?(
|
|
242
|
+
break if lines[i].match?(terminator)
|
|
241
243
|
|
|
242
244
|
buf << lines[i]
|
|
243
245
|
end
|
|
@@ -262,7 +264,8 @@ module PWNConventions
|
|
|
262
264
|
|
|
263
265
|
def purpose_above?(help, name)
|
|
264
266
|
lines = help.to_s.lines
|
|
265
|
-
|
|
267
|
+
signature = /\#\{self\}(?:::[A-Z]\w*)*\.#{Regexp.escape(name)}(?!\w)/
|
|
268
|
+
idx = lines.find_index { |l| l.match?(signature) }
|
|
266
269
|
return false unless idx
|
|
267
270
|
|
|
268
271
|
prev = nil
|
|
@@ -357,15 +360,14 @@ describe 'PWN module conventions' do
|
|
|
357
360
|
# ---------------------------------------------------------------- 6 --
|
|
358
361
|
it '6) every public class method is documented in help with required/optional options (Google.help shape)' do
|
|
359
362
|
misses = []
|
|
363
|
+
methods_by_file = c::ALL_METHODS.group_by { |m| m[:file] }
|
|
360
364
|
c::MODULE_FILES.each do |path|
|
|
361
365
|
next if c::NAMESPACE_INDEX_FILES.include?(c.rel(path))
|
|
362
366
|
|
|
363
367
|
help = c.help_text(path)
|
|
364
368
|
misses << "#{c.rel(path)} missing method authors in help" unless help.match?(/\.authors(?!\w)/)
|
|
365
|
-
pubs =
|
|
366
|
-
m[:
|
|
367
|
-
m[:decorator] == 'public_class_method' &&
|
|
368
|
-
!c::HELP_SKIP.include?(m[:name])
|
|
369
|
+
pubs = methods_by_file.fetch(path, []).select do |m|
|
|
370
|
+
m[:decorator] == 'public_class_method' && !c::HELP_SKIP.include?(m[:name])
|
|
369
371
|
end
|
|
370
372
|
pubs.each do |meth|
|
|
371
373
|
name = meth[:name]
|
|
@@ -4,7 +4,6 @@ require 'spec_helper'
|
|
|
4
4
|
require 'tmpdir'
|
|
5
5
|
require_relative '../support/harness/evaluate'
|
|
6
6
|
require_relative '../support/harness/experiments'
|
|
7
|
-
require_relative '../support/harness/cli_faults'
|
|
8
7
|
|
|
9
8
|
RSpec.describe 'offline harness evaluation' do
|
|
10
9
|
it 'refuses unknown or unequal comparison identities before launching anything' do
|
|
@@ -50,7 +49,7 @@ RSpec.describe 'offline harness evaluation' do
|
|
|
50
49
|
end
|
|
51
50
|
end
|
|
52
51
|
|
|
53
|
-
it 'runs real CLI/Loop/tool effects and independently rejects a lying artifact claim' do
|
|
52
|
+
it 'runs real CLI/Loop/tool effects and independently rejects a lying artifact claim', :aggregate_failures do
|
|
54
53
|
Dir.mktmpdir('pwn-harness-spec-', '/tmp') do |root|
|
|
55
54
|
report = JSON.parse(JSON.generate(HarnessEvaluation.run(output: root)))
|
|
56
55
|
results = report.fetch('results').to_h { |row| [row.fetch('task'), row] }
|
|
@@ -72,9 +71,8 @@ RSpec.describe 'offline harness evaluation' do
|
|
|
72
71
|
expect(row).to include('outcome' => row.fetch('expected_outcome'), 'false_success' => false, 'repeated_effects' => 0)
|
|
73
72
|
end
|
|
74
73
|
expect(report.fetch('runtime_fault_process')).to include('exit_status' => 0, 'resource_cap' => false)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
expect(report.fetch('cli_faults').fetch('results')).to all(include('outcome' => 'PASS'))
|
|
74
|
+
expect_frozen_experiments(report.fetch('experiments'))
|
|
75
|
+
expect_cli_faults(report.fetch('cli_faults'))
|
|
78
76
|
expect(report.fetch('runtime_faults').fetch('results').map { |row| row.fetch('outcome') }).to eq(%w[PASS PASS PASS PASS])
|
|
79
77
|
expect(results.values).to all(include('tokens' => nil, 'cost' => nil))
|
|
80
78
|
ensure
|
|
@@ -101,27 +99,21 @@ RSpec.describe 'offline harness evaluation' do
|
|
|
101
99
|
end
|
|
102
100
|
end
|
|
103
101
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
expect(report.fetch('model_quality')).to eq('NOT_RUN')
|
|
110
|
-
end
|
|
102
|
+
def expect_cli_faults(report)
|
|
103
|
+
rows = report.fetch('results')
|
|
104
|
+
expect(rows.map { |row| row.fetch('id') }).to eq(%w[compaction-steering provider-retry cancel-during-provider checkpoint-restart resource-cap-restart])
|
|
105
|
+
expect(rows).to all(include('outcome' => 'PASS', 'tokens' => nil, 'cost' => nil))
|
|
106
|
+
expect(report.fetch('model_quality')).to eq('NOT_RUN')
|
|
111
107
|
end
|
|
112
108
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
expect(report.fetch('results').select { |row| row['arm'] == 'single-policy' }.map { |row| row['policy_episodes'] }).to all(be > 0)
|
|
122
|
-
ensure
|
|
123
|
-
Dir.glob(File.join(root, '**', '*'), File::FNM_DOTMATCH).each { |path| File.chmod(0o700, path) if File.directory?(path) }
|
|
124
|
-
end
|
|
109
|
+
def expect_frozen_experiments(report)
|
|
110
|
+
expect(report).to include('model_quality' => 'NOT_RUN', 'snapshot_unchanged' => true, 'input_manifests_unchanged' => true)
|
|
111
|
+
rows = report.fetch('results')
|
|
112
|
+
expect(rows.length).to eq(8)
|
|
113
|
+
expect(rows).to all(include('outcome' => 'PASS', 'inputs_unchanged' => true, 'false_success' => false, 'repeated_effects' => 0, 'tokens' => nil, 'cost' => nil))
|
|
114
|
+
arms = rows.group_by { |row| row.fetch('arm') }
|
|
115
|
+
expect(arms.fetch('swarm-snapshot')).to all(include('nested_provider_calls' => 3))
|
|
116
|
+
expect(arms.fetch('single-policy').map { |row| row.fetch('policy_episodes') }).to all(be > 0)
|
|
125
117
|
end
|
|
126
118
|
|
|
127
119
|
it 'requires explicit live approval before importing the reference agent or reading credentials' do
|
|
@@ -4,6 +4,86 @@ require 'spec_helper'
|
|
|
4
4
|
require 'json'
|
|
5
5
|
|
|
6
6
|
describe PWN::AI::Agent::Dispatch do
|
|
7
|
+
describe 'reviewed cross-session memory saves' do
|
|
8
|
+
include_context 'pwn tmp sandbox'
|
|
9
|
+
|
|
10
|
+
let(:proposal) { { key: 'display_units', value: 'Use °C', category: 'preference' } }
|
|
11
|
+
let(:runtime_options) { { request: 'Remember my display preference', session_id: 'review', request_id: 'memory', artifact_root: File.join(@tmp, 'requests'), trace: false } }
|
|
12
|
+
let(:runtime) { PWN::AI::Agent::RequestRuntime.new(runtime_options) }
|
|
13
|
+
|
|
14
|
+
around do |example|
|
|
15
|
+
previous = [Thread.current[:pwn_review_memory], Thread.current[:pwn_request_runtime]]
|
|
16
|
+
Thread.current[:pwn_review_memory] = nil
|
|
17
|
+
example.run
|
|
18
|
+
ensure
|
|
19
|
+
Thread.current[:pwn_review_memory], Thread.current[:pwn_request_runtime] = previous
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
before do
|
|
23
|
+
allow(Dir).to receive(:home).and_return(@tmp)
|
|
24
|
+
PWN::AI::Agent::Registry.discover
|
|
25
|
+
Thread.current[:pwn_request_runtime] = runtime
|
|
26
|
+
expect(PWN::Plugins::Vault).not_to receive(:expand)
|
|
27
|
+
expect(PWN::Plugins::Vault).not_to receive(:redact)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def save_memory(fields)
|
|
31
|
+
call = { id: 'save-preference', function: { name: 'memory_remember', arguments: fields } }
|
|
32
|
+
JSON.parse(described_class.call(tool_call: call), symbolize_names: true)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 'saves only the reviewed fields, with a durable completion and independent file readback' do
|
|
36
|
+
seen = []
|
|
37
|
+
Thread.current[:pwn_review_memory] = lambda do |fields|
|
|
38
|
+
seen << fields
|
|
39
|
+
expect(fields).to be_frozen
|
|
40
|
+
expect(fields.values).to all(be_frozen)
|
|
41
|
+
expect(File).not_to exist(PWN::Memory::MEMORY_FILE)
|
|
42
|
+
true
|
|
43
|
+
end
|
|
44
|
+
expect(save_memory(proposal)).to eq(success: true, effect: 'store', result: { saved: true })
|
|
45
|
+
expect(seen).to eq([proposal])
|
|
46
|
+
expect(JSON.parse(File.read(PWN::Memory::MEMORY_FILE)).fetch('display_units')).to include('value' => 'Use °C', 'category' => 'preference')
|
|
47
|
+
expect(runtime.events.last).to include(phase: 'end', state: 'completed', action_id: 'save-preference')
|
|
48
|
+
expect(runtime.blocker).to be_nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'does not start a write on missing review, decline, or a self-approved tool payload' do
|
|
52
|
+
expect(save_memory(proposal)).to include(success: false, result: include(execution_state: 'not_started'))
|
|
53
|
+
Thread.current[:pwn_review_memory] = ->(_fields) { false }
|
|
54
|
+
expect(save_memory(proposal)).to include(success: false, result: include(execution_state: 'not_started'))
|
|
55
|
+
reviewer = double('operator review')
|
|
56
|
+
expect(reviewer).not_to receive(:call)
|
|
57
|
+
Thread.current[:pwn_review_memory] = reviewer
|
|
58
|
+
expect(save_memory(proposal.merge(operator_ack: true))).to include(success: false)
|
|
59
|
+
expect(save_memory(proposal.merge(value: '{{vault:fixture}}'))).to include(success: false)
|
|
60
|
+
expect(File).not_to exist(PWN::Memory::MEMORY_FILE)
|
|
61
|
+
expect(runtime.events).to be_empty
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it 'distinguishes lock denial before mutation from a lost receipt after a write and never replays the latter' do
|
|
65
|
+
Thread.current[:pwn_review_memory] = ->(_fields) { true }
|
|
66
|
+
allow(File).to receive(:open).and_call_original
|
|
67
|
+
allow(File).to receive(:open).with("#{PWN::Memory::MEMORY_FILE}.write.lock", anything, anything).and_raise(Errno::EACCES)
|
|
68
|
+
expect(save_memory(proposal)).to include(success: false, result: include(execution_state: 'not_started'))
|
|
69
|
+
expect(runtime.blocker).to be_nil
|
|
70
|
+
expect(File).not_to exist(PWN::Memory::MEMORY_FILE)
|
|
71
|
+
|
|
72
|
+
allow(File).to receive(:open).with("#{PWN::Memory::MEMORY_FILE}.write.lock", anything, anything).and_call_original
|
|
73
|
+
allow(PWN::Memory).to receive(:remember).and_wrap_original do |writer, **options|
|
|
74
|
+
writer.call(**options)
|
|
75
|
+
raise Errno::EACCES, 'after write'
|
|
76
|
+
end
|
|
77
|
+
expect(save_memory(proposal)).to include(success: false)
|
|
78
|
+
expect(PWN::Memory.load.fetch(:display_units)).to include(value: 'Use °C')
|
|
79
|
+
restored = PWN::AI::Agent::RequestRuntime.new(runtime_options.merge(resume: true))
|
|
80
|
+
Thread.current[:pwn_request_runtime] = restored
|
|
81
|
+
expect(restored.blocker).to include('Uncertain side effect: memory_remember')
|
|
82
|
+
expect(save_memory(proposal)).to include(success: false, result: include(error: 'uncertain_side_effect'))
|
|
83
|
+
expect(PWN::Memory).to have_received(:remember).once
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
7
87
|
it 'does not replay a timed-out real shell mutation after restoring its request checkpoint' do
|
|
8
88
|
Dir.mktmpdir do |root|
|
|
9
89
|
work = File.join(root, 'work')
|
|
@@ -101,9 +101,10 @@ RSpec.describe PwnSpecNamespaceControllerFixture do
|
|
|
101
101
|
it 'keeps the ten-second payload deadline when the constructor returns without readiness' do
|
|
102
102
|
Dir.mktmpdir('mission-no-marker-') do |root|
|
|
103
103
|
allow(PWN::AI::Agent::MissionNamespace).to receive(:new).and_return(nil)
|
|
104
|
-
expect(Timeout).to receive(:timeout).with(10).and_call_original
|
|
105
104
|
owner = described_class.new(descriptor: {})
|
|
106
105
|
pid = owner.pid
|
|
106
|
+
# Verify the requested deadline, but exercise expiry without waiting ten seconds.
|
|
107
|
+
expect(Timeout).to(receive(:timeout).with(10).and_wrap_original { |timeout, _, &wait| timeout.call(0.1, &wait) })
|
|
107
108
|
expect { owner.wait_ready(File.join(root, 'never-ready')) }.to raise_error(Timeout::Error)
|
|
108
109
|
owner.close
|
|
109
110
|
expect(owner.status.termsig).to eq(Signal.list.fetch('KILL'))
|
|
@@ -424,10 +424,11 @@ describe PWN::AI::Agent::Policy do
|
|
|
424
424
|
described_class.begin_episode(request: 'inspect')
|
|
425
425
|
steps = %w[noop producer noop].each_with_index.map do |action, index|
|
|
426
426
|
before = File.file?(artifact) ? Digest::SHA256.file(artifact).hexdigest : nil
|
|
427
|
+
# These are real, independent Ruby actions, but neither needs to boot the project's bundle.
|
|
427
428
|
if action == 'producer'
|
|
428
|
-
expect(system(RbConfig.ruby, '-e', 'File.write(ARGV.fetch(0), "verified artifact")', artifact)).to be(true)
|
|
429
|
+
expect(system(RbConfig.ruby, '--disable=gems,rubyopt', '-e', 'File.write(ARGV.fetch(0), "verified artifact")', artifact)).to be(true)
|
|
429
430
|
else
|
|
430
|
-
expect(system(RbConfig.ruby, '-e', 'exit 0')).to be(true)
|
|
431
|
+
expect(system(RbConfig.ruby, '--disable=gems,rubyopt', '-e', 'exit 0')).to be(true)
|
|
431
432
|
end
|
|
432
433
|
after = File.file?(artifact) ? Digest::SHA256.file(artifact).hexdigest : nil
|
|
433
434
|
step = described_class.observe_step(action: action, action_id: "private-call-#{index}", ok: true)
|
|
@@ -101,11 +101,13 @@ describe PWN::AI::Agent::Result do
|
|
|
101
101
|
PWN::AI::Agent::Registry.discover
|
|
102
102
|
allow(PWN::Env).to receive(:dig).and_call_original
|
|
103
103
|
allow(PWN::Env).to receive(:dig).with(:ai, :agent, :artifact_max_tokens).and_return(1024)
|
|
104
|
-
raw = ("\x00\xff".b *
|
|
104
|
+
raw = ("\x00\xff".b * 512) + "needle-at-end\n".b
|
|
105
|
+
expect(raw.bytesize).to be > described_class.token_limit
|
|
105
106
|
summary = JSON.parse(described_class.condition(content: raw, session_id: 'roundtrip'))
|
|
106
107
|
handle = summary.fetch('artifact').fetch('handle')
|
|
107
108
|
reconstructed = +''.b
|
|
108
109
|
offset = 0
|
|
110
|
+
compacted = false
|
|
109
111
|
messages = [{ role: 'system', content: 'Read the saved bytes.' }, { role: 'user', content: 'Inspect the dump.' }]
|
|
110
112
|
loop do
|
|
111
113
|
tc = { id: "page-#{offset}", function: { name: 'artifact_read', arguments: { handle: handle, offset: offset, length: 50_000_000 } } }
|
|
@@ -117,13 +119,16 @@ describe PWN::AI::Agent::Result do
|
|
|
117
119
|
reconstructed << Base64.strict_decode64(page.fetch('body'))
|
|
118
120
|
messages << { role: 'assistant', content: '', tool_calls: [tc] }
|
|
119
121
|
messages << { role: 'tool', tool_call_id: tc[:id], content: safe }
|
|
122
|
+
prior_length = messages.length
|
|
120
123
|
PWN::AI::Agent::Loop.send(:compact_history!, messages: messages)
|
|
124
|
+
compacted ||= messages.length < prior_length
|
|
121
125
|
expect(messages.length).to be <= 14
|
|
122
126
|
break if page.fetch('eof')
|
|
123
127
|
|
|
124
128
|
expect(page.fetch('next_offset')).to be > offset
|
|
125
129
|
offset = page.fetch('next_offset')
|
|
126
130
|
end
|
|
131
|
+
expect(compacted).to be(true)
|
|
127
132
|
expect(reconstructed).to eq(raw)
|
|
128
133
|
response = PWN::AI::Agent::Dispatch.call(
|
|
129
134
|
tool_call: { function: { name: 'artifact_grep', arguments: { handle: handle, regex: 'needle-at-end' } } },
|
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
|
|
5
5
|
describe 'PWN::AI::Agent::Tools memory' do
|
|
6
|
+
it 'does not let a registry call write around the request review' do
|
|
7
|
+
PWN::AI::Agent::Registry.discover
|
|
8
|
+
expect(PWN::Memory).not_to receive(:remember)
|
|
9
|
+
result = PWN::AI::Agent::Registry.lookup(name: 'memory_remember').handler.call(key: 'units', value: 'metric')
|
|
10
|
+
expect(result).to include(success: false, saved: false, execution_state: 'not_started')
|
|
11
|
+
end
|
|
12
|
+
|
|
6
13
|
it 'registers the memory_remember tool' do
|
|
7
14
|
PWN::AI::Agent::Registry.discover
|
|
8
15
|
expect(PWN::AI::Agent::Registry.lookup(name: 'memory_remember')).not_to be_nil
|
|
@@ -338,3 +338,30 @@ describe PWN::AI::Ollama do
|
|
|
338
338
|
expect(described_class).not_to respond_to(:get_plan_usage)
|
|
339
339
|
end
|
|
340
340
|
end
|
|
341
|
+
|
|
342
|
+
describe PWN::AI::Ollama, 'binary stream display recovery' do
|
|
343
|
+
it 'previews complete UTF-8 SSE without returning a provider response or changing the wire bytes' do
|
|
344
|
+
event = JSON.generate(choices: [{ delta: { role: 'assistant', content: 'Café', reasoning_content: 'not for display' } }])
|
|
345
|
+
wire = "\uFEFFdata: #{event}\r\n\r\ndata: [DONE]\r\n\r\n".b.freeze
|
|
346
|
+
previews = []
|
|
347
|
+
previous = Thread.current[:pwn_stream_preview]
|
|
348
|
+
Thread.current[:pwn_stream_preview] = ->(*args) { previews << args }
|
|
349
|
+
expect { described_class.send(:assemble_ollama_stream, body: wire, rest_call: 'v1/chat/completions') }.to raise_error(RuntimeError, /Ollama.*display-only/)
|
|
350
|
+
expect(previews).to eq([['Ollama', 'Café', false]])
|
|
351
|
+
expect(wire.encoding).to eq(Encoding::BINARY)
|
|
352
|
+
ensure
|
|
353
|
+
Thread.current[:pwn_stream_preview] = previous
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
it 'rejects invalid bytes, incomplete native frames and provider errors before showing any text' do
|
|
357
|
+
wire = JSON.generate(message: { role: 'assistant', content: 'Café' }, done: false).b
|
|
358
|
+
parse = ->(body) { described_class.send(:assemble_ollama_stream, body: body, rest_call: 'api/chat') }
|
|
359
|
+
previous = Thread.current[:pwn_stream_preview]
|
|
360
|
+
Thread.current[:pwn_stream_preview] = ->(*) { raise 'Unexpected preview' }
|
|
361
|
+
expect { parse.call(wire + "\xFF".b) }.to raise_error(RuntimeError, /Ollama.*invalid UTF-8/)
|
|
362
|
+
expect { parse.call(wire) }.to raise_error(RuntimeError, /incomplete native stream/)
|
|
363
|
+
expect { parse.call("#{wire}\n{\"error\":\"private detail\"}".b) }.to raise_error(RuntimeError, 'ERROR: Ollama stream error for api/chat')
|
|
364
|
+
ensure
|
|
365
|
+
Thread.current[:pwn_stream_preview] = previous
|
|
366
|
+
end
|
|
367
|
+
end
|