pwn 0.5.804 → 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 +1 -1
- 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/memory.rb +9 -4
- data/lib/pwn/plugins/repl/ai_console.rb +148 -13
- 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/banner_spec.rb +67 -38
- 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 +301 -1
- data/spec/lib/pwn/sdr/decoder/lora_spec.rb +36 -27
- 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/third_party/pwn_rdoc.jsonl +12 -0
- metadata +3 -3
|
@@ -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
|
data/spec/lib/pwn/banner_spec.rb
CHANGED
|
@@ -543,8 +543,13 @@ describe PWN::Banner do
|
|
|
543
543
|
|
|
544
544
|
it 'keeps every active tetromino color beside the stack throughout its fall and lock' do
|
|
545
545
|
rotations = Hash.new { |hash, key| hash[key] = [] }
|
|
546
|
-
|
|
546
|
+
# Finish on default seeded, branded boards; the following examples can read them through the real cache.
|
|
547
|
+
[1, 74, 73].product([false, true], [5, 8, 16, 20]).each do |seed, branding, size|
|
|
547
548
|
args = { name: :falling_blocks, width: size, height: size, seed: seed, branding: branding }
|
|
549
|
+
decoded = {}
|
|
550
|
+
stack_colors = Hash.new do |cache, row|
|
|
551
|
+
cache[row] = row.chars.map { |pixel| pixel == ' ' ? :black : described_class::MINI_PIXELS.fetch(pixel)[1] }.freeze
|
|
552
|
+
end
|
|
548
553
|
frames = described_class.send(:mini_replay, **args, height: size + (branding ? 0 : 1))
|
|
549
554
|
frames.each_with_index do |frame, index|
|
|
550
555
|
next unless frame[:state][:active]
|
|
@@ -553,9 +558,10 @@ describe PWN::Banner do
|
|
|
553
558
|
type = frame[:state][:type]
|
|
554
559
|
cells = described_class.mini_cells(**args, frame: index)
|
|
555
560
|
color = %i[cyan yellow magenta blue red green green][type]
|
|
556
|
-
expected = frame[:state][:stack].map { |row| row.
|
|
561
|
+
expected = frame[:state][:stack].map { |row| stack_colors[row].dup }
|
|
557
562
|
points.each { |x, y| expected[y][x] = color }
|
|
558
|
-
|
|
563
|
+
actual_rows = frame[:rows].drop(1).flat_map { |row| decoded[row] ||= block_colors([row]) }
|
|
564
|
+
expect(actual_rows).to eq(expected)
|
|
559
565
|
points.each do |x, y|
|
|
560
566
|
expect(x).to be_between(0, size - 1)
|
|
561
567
|
expect(y).to be_between(0, ((size - (branding ? 1 : 0)) * 2) - 1)
|
|
@@ -617,20 +623,27 @@ describe PWN::Banner do
|
|
|
617
623
|
it 'packs blocks into two quadrants and snake segments into one without losing ink' do
|
|
618
624
|
%i[falling_blocks snake].product((5..16).to_a).each do |name, size|
|
|
619
625
|
frames = described_class.send(:mini_replay, name: name, width: size, height: size, seed: 73)
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
626
|
+
# A packed row's ink depends only on its contents, not its place in the replay.
|
|
627
|
+
row_ink = {}
|
|
628
|
+
widths = [size] * size
|
|
629
|
+
label = 'PWN'.center(size)
|
|
630
|
+
frames.each_with_index do |frame, index|
|
|
631
|
+
area = frame[:rows].drop(1).sum do |row|
|
|
632
|
+
row_ink[row] ||= begin
|
|
633
|
+
pixels = row.delete(' ').codepoints
|
|
634
|
+
expect(pixels.find { |code| code < 256 }).to be_nil, -> { "Unpacked pixel in #{name} size #{size} frame #{index}: #{row.inspect}" }
|
|
635
|
+
pixels.sum { |code| ((code - 256) % 16).digits(2).sum }
|
|
636
|
+
end
|
|
637
|
+
end
|
|
624
638
|
state = frame[:state]
|
|
625
639
|
expected = if state[:body]
|
|
626
640
|
state[:body].length + (state[:food] ? 1 : 0)
|
|
627
641
|
elsif state[:active]
|
|
628
642
|
(state[:stack].sum { |row| row.count('^ ') } + 4) * 2
|
|
629
643
|
end
|
|
630
|
-
expect(area).to eq(expected) if expected
|
|
631
|
-
expect(frame[:rows].length).to eq(
|
|
632
|
-
expect(frame[:rows].
|
|
633
|
-
expect(frame[:rows].first).to eq('PWN'.center(size))
|
|
644
|
+
expect(area).to eq(expected), -> { "#{name} size #{size} frame #{index}: expected #{expected} ink, got #{area}" } if expected
|
|
645
|
+
expect(frame[:rows].map(&:length)).to eq(widths)
|
|
646
|
+
expect(frame[:rows].first).to eq(label)
|
|
634
647
|
end
|
|
635
648
|
end
|
|
636
649
|
end
|
|
@@ -739,7 +752,8 @@ describe PWN::Banner do
|
|
|
739
752
|
end
|
|
740
753
|
|
|
741
754
|
it 'spawns rocks clear of the ship instead of starting with overlapping sprites' do
|
|
742
|
-
|
|
755
|
+
# Walk back through the same cases so the real bounded cache can serve recent replays.
|
|
756
|
+
[5, 8, 16].product([1, 73, 74]).reverse_each do |size, seed|
|
|
743
757
|
frames = described_class.send(:mini_replay, name: :asteroids, width: size, height: size, seed: seed)
|
|
744
758
|
state = frames.find { |frame| frame[:state][:ship] }[:state]
|
|
745
759
|
state[:rocks].each do |rock|
|
|
@@ -834,23 +848,28 @@ describe PWN::Banner do
|
|
|
834
848
|
end
|
|
835
849
|
|
|
836
850
|
it 'keeps every frame bounded, single-column, named-color, deterministic and side-effect free' do
|
|
851
|
+
colors = %i[black red green yellow blue magenta cyan white]
|
|
852
|
+
glyphs = /\A[ PWN█▀▄▌▐░▒·●▘▝▖▗▞▛▚▜▙▟ᗧᗤᗢᗣ│•✹═≈▰▱\u2800-\u28ff]\z/
|
|
853
|
+
# Exercise cache initialization and replay construction under the guards, even when run last.
|
|
854
|
+
described_class.instance_variable_set(:@mini_replays, nil)
|
|
837
855
|
expect(Random).not_to receive(:rand)
|
|
838
856
|
expect(Time).not_to receive(:now)
|
|
839
857
|
expect(Process).not_to receive(:clock_gettime)
|
|
840
858
|
expect(Thread).not_to receive(:new)
|
|
841
859
|
expect(described_class).not_to receive(:sleep)
|
|
842
860
|
expect do
|
|
843
|
-
described_class.mini_names.
|
|
861
|
+
described_class.mini_names.reverse_each do |name|
|
|
844
862
|
(5..16).each do |size|
|
|
845
863
|
args = { name: name, width: size, height: size }
|
|
846
864
|
frames = Array.new(60) { |frame| described_class.mini_cells(**args, frame: frame) }
|
|
847
865
|
expect(frames.uniq.length).to be >= 4
|
|
848
866
|
expect(frames.map(&:length)).to all(eq(size))
|
|
849
867
|
expect(frames.flatten(1).map(&:length)).to all(eq(size))
|
|
850
|
-
frames.
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
868
|
+
frames.each_with_index do |board, frame|
|
|
869
|
+
board.each_with_index do |row, y|
|
|
870
|
+
x = row.index { |cell| !valid_mini_cell?(cell, glyphs, colors) }
|
|
871
|
+
raise "Invalid #{name} cell (size #{size}, frame #{frame}, x #{x}, y #{y}): #{row[x].inspect}" if x
|
|
872
|
+
end
|
|
854
873
|
end
|
|
855
874
|
expect(described_class.mini_cells(**args, frame: described_class::MINI_FRAME_COUNT)).to eq(frames.first)
|
|
856
875
|
expect(described_class.mini_cells(**args, frame: -1)).to eq(described_class.mini_cells(**args, frame: described_class::MINI_FRAME_COUNT - 1))
|
|
@@ -866,22 +885,29 @@ describe PWN::Banner do
|
|
|
866
885
|
expect { described_class.mini_cells(name: :unknown) }.to raise_error(ArgumentError, /Unknown mini-banner/)
|
|
867
886
|
end
|
|
868
887
|
|
|
888
|
+
def valid_mini_cell?(cell, glyphs, colors)
|
|
889
|
+
cell.is_a?(Hash) && cell.size == 3 && cell.key?(:glyph) && cell.key?(:foreground) && cell.key?(:background) &&
|
|
890
|
+
cell[:glyph].is_a?(String) && glyphs.match?(cell[:glyph]) && colors.include?(cell[:foreground]) && colors.include?(cell[:background])
|
|
891
|
+
end
|
|
892
|
+
|
|
869
893
|
def replay(name, size, seed = 73)
|
|
870
894
|
described_class.send(:mini_replay, name: name, width: size, height: size, seed: seed)
|
|
871
895
|
end
|
|
872
896
|
|
|
873
|
-
def logical_ink(frame, full: false)
|
|
897
|
+
def logical_ink(frame, row_cache, full: false)
|
|
874
898
|
width = frame[:rows].first.length
|
|
875
899
|
height = frame[:rows].length - 1
|
|
876
900
|
height *= 2 if full
|
|
901
|
+
halves = row_cache[[width, full]] ||= [{}, {}]
|
|
877
902
|
Array.new(height) do |y|
|
|
878
|
-
|
|
903
|
+
py = y + (full ? 0 : height / 2)
|
|
904
|
+
row = frame[:rows][1 + (py / 2)]
|
|
905
|
+
halves[py % 2][row] ||= Array.new(width) do |x|
|
|
879
906
|
px = full ? x * 2 : x + (width / 2)
|
|
880
|
-
|
|
881
|
-
pixel = frame[:rows][1 + (py / 2)][px / 2]
|
|
907
|
+
pixel = row[px / 2]
|
|
882
908
|
mask = pixel == ' ' ? 0 : (pixel.ord - 256) % 16
|
|
883
909
|
mask[(py % 2 * 2) + (px % 2)] == 1 ? '*' : ' '
|
|
884
|
-
end.join
|
|
910
|
+
end.join.freeze
|
|
885
911
|
end
|
|
886
912
|
end
|
|
887
913
|
|
|
@@ -900,22 +926,23 @@ describe PWN::Banner do
|
|
|
900
926
|
end
|
|
901
927
|
|
|
902
928
|
it 'stacks all seven four-cell tetrominoes with legal gravity, rotations and line collapse' do
|
|
903
|
-
|
|
929
|
+
# Start with the larger, more expensive boards which may still be in the production cache.
|
|
930
|
+
(5..16).reverse_each do |size|
|
|
904
931
|
frames = replay(:falling_blocks, size)
|
|
932
|
+
row_cache = {}
|
|
905
933
|
active = frames.select { |frame| frame[:state][:active] }
|
|
906
934
|
expect(active.map { |frame| frame[:state][:type] }.uniq.length).to eq(7)
|
|
907
935
|
expect(frames.map { |frame| frame[:event] }).to include(:lock, :clear, :collapse)
|
|
908
936
|
active.each do |frame|
|
|
909
937
|
points, stack = frame[:state].values_at(:active, :stack)
|
|
910
938
|
expect(points.uniq.length).to eq(4)
|
|
911
|
-
points.
|
|
912
|
-
|
|
913
|
-
expect(y).to be_between(0, ((size - 1) * 2) - 1)
|
|
914
|
-
expect(stack[y][x]).to eq(' ')
|
|
939
|
+
blocked = points.find do |x, y|
|
|
940
|
+
!x.between?(0, size - 1) || !y.between?(0, ((size - 1) * 2) - 1) || stack[y][x] != ' '
|
|
915
941
|
end
|
|
942
|
+
expect(blocked).to be_nil, -> { "Active piece outside the board or overlapping the stack: #{blocked.inspect} at size #{size}" }
|
|
916
943
|
expected = stack.map { |row| row.gsub(/[^ ]/, '*') }
|
|
917
944
|
points.each { |x, y| expected[y][x] = '*' }
|
|
918
|
-
expect(logical_ink(frame, full: true)).to eq(expected)
|
|
945
|
+
expect(logical_ink(frame, row_cache, full: true)).to eq(expected)
|
|
919
946
|
end
|
|
920
947
|
frames.each_cons(2) do |before, after|
|
|
921
948
|
a = before[:state][:active]
|
|
@@ -930,9 +957,10 @@ describe PWN::Banner do
|
|
|
930
957
|
expect(after[:rows]).to eq(before[:rows])
|
|
931
958
|
elsif before[:event] == :clear && after[:event] == :collapse
|
|
932
959
|
lines = before[:state][:lines]
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
960
|
+
ink = logical_ink(before, row_cache, full: true)
|
|
961
|
+
expect(lines.map { |y| ink[y] }).to all(eq('*' * size))
|
|
962
|
+
survivors = ink.each_with_index.filter_map { |row, y| row unless lines.include?(y) }
|
|
963
|
+
expect(logical_ink(after, row_cache, full: true)).to eq(Array.new(lines.length, ' ' * size) + survivors)
|
|
936
964
|
end
|
|
937
965
|
end
|
|
938
966
|
end
|
|
@@ -941,16 +969,16 @@ describe PWN::Banner do
|
|
|
941
969
|
it 'moves a contiguous non-overlapping snake toward real food and grows only on pickup' do
|
|
942
970
|
(5..16).each do |size|
|
|
943
971
|
frames = replay(:snake, size)
|
|
972
|
+
row_cache = {}
|
|
944
973
|
states = frames.filter_map { |frame| frame[:state] if frame[:state][:body] }
|
|
945
974
|
expect(states.map { |state| state[:body].length }.max).to be > 6
|
|
946
975
|
states.each do |state|
|
|
947
976
|
body, food = state.values_at(:body, :food)
|
|
948
977
|
expect(body.uniq.length).to eq(body.length)
|
|
949
|
-
body.
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
body.each_cons(2) { |a, b| expect(a.zip(b).sum { |x, y| (x - y).abs }).to eq(1) }
|
|
978
|
+
outside = body.find { |x, y| !x.between?(0, size - 1) || !y.between?(0, size - 2) }
|
|
979
|
+
expect(outside).to be_nil, -> { "Snake outside #{size}x#{size - 1} board: #{outside.inspect}" }
|
|
980
|
+
gap = body.each_cons(2).find { |a, b| a.zip(b).sum { |x, y| (x - y).abs } != 1 }
|
|
981
|
+
expect(gap).to be_nil, -> { "Disconnected snake segments at size #{size}: #{gap.inspect}" }
|
|
954
982
|
expect(body).not_to include(food) if food
|
|
955
983
|
end
|
|
956
984
|
frames.each do |frame|
|
|
@@ -959,7 +987,7 @@ describe PWN::Banner do
|
|
|
959
987
|
|
|
960
988
|
expected = Array.new(size - 1) { ' ' * size }
|
|
961
989
|
(body + [food].compact).each { |x, y| expected[y][x] = '*' }
|
|
962
|
-
expect(logical_ink(frame)).to eq(expected)
|
|
990
|
+
expect(logical_ink(frame, row_cache)).to eq(expected)
|
|
963
991
|
end
|
|
964
992
|
frames.each_cons(2) do |before, after|
|
|
965
993
|
a = before[:state]
|
|
@@ -1059,11 +1087,12 @@ describe PWN::Banner do
|
|
|
1059
1087
|
described_class.mini_names.each do |name|
|
|
1060
1088
|
(5..16).each do |size|
|
|
1061
1089
|
args = { name: name, width: size, height: size }
|
|
1062
|
-
frames = Array.new(described_class::
|
|
1090
|
+
frames = Array.new(described_class::MINI_ASCII_FRAME_COUNT) { |frame| described_class.mini_frame(**args, frame: frame) }
|
|
1063
1091
|
expect(frames.uniq.length).to be >= 4
|
|
1064
1092
|
expect(frames.flatten).to all(match(/\A[ -~]{#{size}}\z/))
|
|
1065
1093
|
expect(frames.map(&:length)).to all(eq(size))
|
|
1066
1094
|
expect(frames.map(&:first)).to all(include('PWN'))
|
|
1095
|
+
expect(described_class.mini_frame(**args, frame: 60)).to eq(frames.first)
|
|
1067
1096
|
expect(described_class.mini_frame(**args, frame: described_class::MINI_FRAME_COUNT)).to eq(frames.first)
|
|
1068
1097
|
expect(described_class.mini_frame(**args, name: name.to_s, frame: -1)).to eq(frames.last)
|
|
1069
1098
|
expect(described_class.mini_frame(**args, frame: 3)).to eq(frames[3])
|
data/spec/lib/pwn/memory_spec.rb
CHANGED
|
@@ -5,6 +5,52 @@ require 'tmpdir'
|
|
|
5
5
|
require 'fileutils'
|
|
6
6
|
|
|
7
7
|
RSpec.describe PWN::Memory do
|
|
8
|
+
describe 'atomic publication' do
|
|
9
|
+
include_context 'pwn tmp sandbox'
|
|
10
|
+
|
|
11
|
+
let(:path) { described_class::MEMORY_FILE }
|
|
12
|
+
|
|
13
|
+
it 'commits the reviewed entry without consuming an existing temporary file' do
|
|
14
|
+
old_tmp = File.join(@tmp, ".memory.json.#{Process.pid}.tmp")
|
|
15
|
+
File.write(old_tmp, 'unrelated contents')
|
|
16
|
+
File.write(path, '{"before":{"value":"original"}}')
|
|
17
|
+
|
|
18
|
+
described_class.remember(key: :reviewed, value: 'approved')
|
|
19
|
+
|
|
20
|
+
expect(described_class.load.transform_values { |entry| entry[:value] }).to eq(before: 'original', reviewed: 'approved')
|
|
21
|
+
expect(File).to exist(old_tmp)
|
|
22
|
+
expect(File.read(old_tmp)).to eq('unrelated contents')
|
|
23
|
+
expect(Dir.children(@tmp)).to contain_exactly('memory.json', 'memory.json.write.lock', File.basename(old_tmp))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'leaves an occupied candidate and the current store untouched if exclusive creation fails' do
|
|
27
|
+
allow(SecureRandom).to receive(:hex).and_return('reserved')
|
|
28
|
+
candidate = File.join(@tmp, '.memory.json.reserved.tmp')
|
|
29
|
+
File.write(candidate, 'reserved contents')
|
|
30
|
+
original = '{"before":{"value":"original"}}'
|
|
31
|
+
File.write(path, original)
|
|
32
|
+
|
|
33
|
+
expect { described_class.remember(key: :reviewed, value: 'approved') }.to raise_error(Errno::EEXIST)
|
|
34
|
+
expect(File.read(candidate)).to eq('reserved contents')
|
|
35
|
+
expect(File.read(path)).to eq(original)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'discards the private temporary file and leaves the original intact when rename fails' do
|
|
39
|
+
original = '{"before":{"value":"original"}}'
|
|
40
|
+
File.write(path, original)
|
|
41
|
+
allow(File).to receive(:rename).with(anything, path) do |tmp, _path|
|
|
42
|
+
expect(File.dirname(tmp)).to eq(File.dirname(path))
|
|
43
|
+
expect(File.stat(tmp).mode & 0o077).to eq(0)
|
|
44
|
+
expect(JSON.parse(File.read(tmp)).dig('reviewed', 'value')).to eq('approved')
|
|
45
|
+
raise Errno::EIO
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
expect { described_class.remember(key: :reviewed, value: 'approved') }.to raise_error(Errno::EIO)
|
|
49
|
+
expect(File.read(path)).to eq(original)
|
|
50
|
+
expect(Dir.children(@tmp)).to contain_exactly('memory.json', 'memory.json.write.lock')
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
8
54
|
describe 'store transactions' do
|
|
9
55
|
include_context 'pwn tmp sandbox'
|
|
10
56
|
|
|
@@ -7,13 +7,22 @@ require 'rbconfig'
|
|
|
7
7
|
require 'timeout'
|
|
8
8
|
|
|
9
9
|
RSpec.describe PWN::Plugins::Android do
|
|
10
|
+
def write_fixture_adb(script)
|
|
11
|
+
# These standalone children need no test bundle; leave the runner's RUBYOPT alone.
|
|
12
|
+
File.write(@adb, "#!#{RbConfig.ruby} --disable=gems,rubyopt\n#{script}")
|
|
13
|
+
File.chmod(0o700, @adb)
|
|
14
|
+
end
|
|
15
|
+
|
|
10
16
|
around do |example|
|
|
11
17
|
Dir.mktmpdir('android-review-') do |root|
|
|
12
18
|
@root = root
|
|
13
19
|
@adb = File.join(root, 'adb literal ; executable')
|
|
14
20
|
@calls = File.join(root, 'calls.jsonl')
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
write_fixture_adb(<<~RUBY)
|
|
22
|
+
require 'json'
|
|
23
|
+
File.open(#{@calls.inspect}, 'a') { |f| f.puts(JSON.generate(ARGV)) }
|
|
24
|
+
puts 'synthetic'
|
|
25
|
+
RUBY
|
|
17
26
|
example.run
|
|
18
27
|
end
|
|
19
28
|
end
|
|
@@ -21,8 +30,7 @@ RSpec.describe PWN::Plugins::Android do
|
|
|
21
30
|
it 'cancels and reaps only its owned process group, including TERM-resistant descendants' do
|
|
22
31
|
pidfile = File.join(@root, 'pids')
|
|
23
32
|
release = File.join(@root, 'release')
|
|
24
|
-
|
|
25
|
-
#!#{RbConfig.ruby}
|
|
33
|
+
write_fixture_adb(<<~RUBY)
|
|
26
34
|
child = fork do
|
|
27
35
|
trap('TERM') {}
|
|
28
36
|
File.write(#{"#{pidfile}.ready".inspect}, '')
|
|
@@ -76,7 +84,7 @@ RSpec.describe PWN::Plugins::Android do
|
|
|
76
84
|
end
|
|
77
85
|
|
|
78
86
|
it 'bounds an owned child that closes stdout but never exits, without exposing its output' do
|
|
79
|
-
|
|
87
|
+
write_fixture_adb("STDOUT.close\nsleep 20\n")
|
|
80
88
|
stub_const('PWN::Plugins::Android::COMMAND_TIMEOUT', 1)
|
|
81
89
|
owned = nil
|
|
82
90
|
allow(Process).to receive(:spawn).and_wrap_original do |original, *args, **kwargs|
|
|
@@ -89,7 +97,7 @@ RSpec.describe PWN::Plugins::Android do
|
|
|
89
97
|
end
|
|
90
98
|
|
|
91
99
|
it 'bounds capture output and suppresses private child stderr on failure' do
|
|
92
|
-
|
|
100
|
+
write_fixture_adb("STDERR.puts 'synthetic-private'\nprint 'x' * 2048\n")
|
|
93
101
|
stub_const('PWN::Plugins::Android::MAX_OUTPUT_BYTES', 1024)
|
|
94
102
|
spawn_options = nil
|
|
95
103
|
allow(Process).to receive(:spawn).and_wrap_original do |original, *args, **kwargs|
|
|
@@ -14,8 +14,9 @@ RSpec.describe PWN::Plugins::Android do
|
|
|
14
14
|
@exit = File.join(root, 'exit')
|
|
15
15
|
File.write(@output, '')
|
|
16
16
|
File.write(@exit, '0')
|
|
17
|
+
# Run this argv recorder without loading the parent test bundle in every child.
|
|
17
18
|
File.write(@adb, <<~RUBY)
|
|
18
|
-
#!#{RbConfig.ruby}
|
|
19
|
+
#!#{RbConfig.ruby} --disable=gems,rubyopt
|
|
19
20
|
require 'json'
|
|
20
21
|
File.open(#{@calls.inspect}, 'a') { |f| f.puts(JSON.generate(ARGV)) }
|
|
21
22
|
print File.binread(#{@output.inspect})
|
|
@@ -38,13 +38,17 @@ describe PWN::Plugins::ArtifactRegistry do
|
|
|
38
38
|
stored = described_class.spill(bytes: bytes, session_id: 'large')
|
|
39
39
|
expected = Digest::SHA256.hexdigest(bytes)
|
|
40
40
|
digest = Digest::SHA256.new
|
|
41
|
+
metadata = [stored[:handle], bytes.bytesize, 'base64', 'base64']
|
|
41
42
|
offset = 0
|
|
42
43
|
pages = 0
|
|
43
44
|
loop do
|
|
44
45
|
page = described_class.read_page(handle: stored[:handle], offset: offset, length: 1_000_000)
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
raise "Invalid page metadata at byte #{offset}" unless page.values_at(:handle, :total_bytes, :encoding, :mode) == metadata
|
|
47
|
+
|
|
47
48
|
decoded = Base64.strict_decode64(page[:body])
|
|
49
|
+
valid_size = (1..2048).cover?(page[:bytes]) && decoded.bytesize == page[:bytes]
|
|
50
|
+
raise "Invalid page bounds at byte #{offset}" unless valid_size && page[:next_offset] == offset + page[:bytes]
|
|
51
|
+
|
|
48
52
|
digest.update(decoded)
|
|
49
53
|
offset = page[:next_offset]
|
|
50
54
|
pages += 1
|
|
@@ -52,7 +56,7 @@ describe PWN::Plugins::ArtifactRegistry do
|
|
|
52
56
|
end
|
|
53
57
|
expect(offset).to eq(bytes.bytesize)
|
|
54
58
|
expect(digest.hexdigest).to eq(expected)
|
|
55
|
-
|
|
59
|
+
expect(pages).to be >= (bytes.bytesize / 2048)
|
|
56
60
|
end
|
|
57
61
|
|
|
58
62
|
it 'preserves split UTF-8 bytes, explicit encodings, refs and EOF metadata' do
|