pwn 0.5.804 → 0.5.806
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 +8 -8
- data/documentation/AI-Integration.md +27 -1
- data/documentation/Contributing.md +1 -1
- data/documentation/Diagrams.md +1 -1
- data/documentation/General-PWN-Usage.md +1 -1
- data/documentation/Home.md +2 -2
- data/documentation/How-PWN-Works.md +1 -1
- data/documentation/Mission-Cancellation.md +4 -3
- data/documentation/Persistence.md +1 -1
- data/documentation/Plugins.md +3 -2
- data/documentation/Runtime-Source-Protection.md +20 -2
- data/documentation/Skills-Memory-Learning.md +15 -1
- data/documentation/What-is-PWN.md +1 -1
- data/documentation/diagrams/dot/plugin-ecosystem.dot +1 -1
- data/documentation/diagrams/plugin-ecosystem.svg +1 -1
- data/etc/default_skills/pwn/ai/agent/submission/SKILL.md +23 -0
- data/etc/default_skills/pwn/ai/sandbox/SKILL.md +59 -0
- data/lib/pwn/ai/agent/confirmation.rb +1 -1
- data/lib/pwn/ai/agent/dispatch.rb +55 -2
- data/lib/pwn/ai/agent/learning.rb +7 -3
- data/lib/pwn/ai/agent/loop.rb +388 -251
- data/lib/pwn/ai/agent/mission_namespace.rb +52 -14
- data/lib/pwn/ai/agent/open_goal.rb +36 -9
- data/lib/pwn/ai/agent/policy.rb +37 -10
- data/lib/pwn/ai/agent/prompt_builder.rb +15 -8
- data/lib/pwn/ai/agent/request_runtime.rb +106 -13
- data/lib/pwn/ai/agent/submission.rb +367 -0
- data/lib/pwn/ai/agent/swarm.rb +2 -2
- data/lib/pwn/ai/agent/task_dag.rb +1 -1
- data/lib/pwn/ai/agent/task_summarizer.rb +47 -13
- data/lib/pwn/ai/agent/tools/memory.rb +6 -11
- data/lib/pwn/ai/agent/tools/ruby_eval.rb +1 -1
- data/lib/pwn/ai/agent/tools/shell.rb +1 -1
- data/lib/pwn/ai/agent/tools/skills.rb +5 -5
- data/lib/pwn/ai/agent/turn_finalizer.rb +659 -27
- data/lib/pwn/ai/agent.rb +1 -0
- data/lib/pwn/ai/cli.rb +2 -0
- data/lib/pwn/ai/grok.rb +106 -1
- data/lib/pwn/{plugins/ai_sandbox.rb → ai/sandbox.rb} +116 -32
- data/lib/pwn/ai.rb +1 -0
- data/lib/pwn/config.rb +36 -32
- data/lib/pwn/memory.rb +9 -4
- data/lib/pwn/plugins/log.rb +15 -1
- data/lib/pwn/plugins/repl/ai.rb +3 -7
- data/lib/pwn/plugins/repl/ai_console.rb +234 -41
- data/lib/pwn/plugins/repl/ai_swarm.rb +1 -1
- data/lib/pwn/plugins.rb +0 -1
- data/lib/pwn/sessions.rb +9 -1
- data/lib/pwn/version.rb +1 -1
- data/pwn_upgrade-aarch64.txt +9533 -0
- data/spec/conventions_spec.rb +9 -7
- data/spec/integration/controller_spill_spec.rb +8 -2
- data/spec/integration/harness_evaluation_spec.rb +16 -24
- data/spec/integration/harness_package_spec.rb +17 -6
- data/spec/integration/mission_cancel_spec.rb +2 -2
- data/spec/integration/reinforced_feedback_loop_spec.rb +1 -1
- data/spec/landlock_capability_spec.rb +23 -0
- data/spec/lib/pwn/ai/agent/chat_deliverable_spec.rb +188 -0
- data/spec/lib/pwn/ai/agent/dispatch_spec.rb +82 -2
- data/spec/lib/pwn/ai/agent/loop_artifacts_spec.rb +102 -1
- data/spec/lib/pwn/ai/agent/loop_partial_delivery_spec.rb +62 -0
- data/spec/lib/pwn/ai/agent/loop_permission_spec.rb +152 -0
- data/spec/lib/pwn/ai/agent/loop_spec.rb +100 -23
- data/spec/lib/pwn/ai/agent/loop_submission_spec.rb +271 -0
- data/spec/lib/pwn/ai/agent/mission_namespace_spec.rb +78 -4
- data/spec/lib/pwn/ai/agent/policy_spec.rb +3 -2
- data/spec/lib/pwn/ai/agent/provider_window_timing_spec.rb +63 -0
- data/spec/lib/pwn/ai/agent/request_runtime_spec.rb +142 -1
- data/spec/lib/pwn/ai/agent/result_spec.rb +6 -1
- data/spec/lib/pwn/ai/agent/reward_dispatch_review_spec.rb +1 -1
- data/spec/lib/pwn/ai/agent/solve_spec.rb +1 -1
- data/spec/lib/pwn/ai/agent/submission_spec.rb +81 -0
- data/spec/lib/pwn/ai/agent/task_summarizer_spec.rb +29 -0
- data/spec/lib/pwn/ai/agent/tools/memory_spec.rb +7 -0
- data/spec/lib/pwn/ai/agent/tools/skills_spec.rb +23 -0
- data/spec/lib/pwn/ai/agent/turn_finalizer_spec.rb +384 -1
- data/spec/lib/pwn/ai/cli_spec.rb +17 -0
- data/spec/lib/pwn/ai/grok_spec.rb +153 -0
- data/spec/lib/pwn/ai/sandbox_boundary_review_spec.rb +231 -0
- data/spec/lib/pwn/{plugins/ai_sandbox_spec.rb → ai/sandbox_spec.rb} +29 -14
- data/spec/lib/pwn/ai_spec.rb +5 -0
- data/spec/lib/pwn/banner_spec.rb +67 -38
- data/spec/lib/pwn/config_skills_regression_spec.rb +78 -20
- data/spec/lib/pwn/memory_spec.rb +46 -0
- data/spec/lib/pwn/plugins/android_review_fixes_spec.rb +15 -8
- 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/log_spec.rb +95 -0
- data/spec/lib/pwn/plugins/plugins_b_operational_regression_spec.rb +1 -1
- data/spec/lib/pwn/plugins/repl/ai_console_spec.rb +551 -40
- data/spec/lib/pwn/plugins/repl/ai_legacy_containment_spec.rb +25 -10
- data/spec/lib/pwn/sdr/decoder/lora_spec.rb +36 -27
- data/spec/lib/pwn/sessions_lifecycle_supplement_spec.rb +50 -0
- data/spec/spec_helper.rb +8 -4
- data/spec/support/harness/cli_faults.rb +2 -1
- data/spec/support/harness/controller_spill.rb +2 -2
- data/spec/support/harness/controller_state.rb +3 -3
- data/spec/support/harness/evaluate.rb +21 -15
- data/spec/support/harness/mission_dispatch.rb +2 -2
- data/spec/support/harness/mission_job_lifetime.rb +2 -2
- data/spec/support/harness/mission_normal_exit.rb +1 -1
- data/spec/support/harness/mission_unsupported.rb +2 -2
- data/spec/support/harness/package_acceptance.rb +25 -4
- data/spec/support/harness/package_sandbox_rejection.rb +21 -0
- data/spec/support/harness/swarm_control.rb +5 -5
- data/spec/support/landlock.rb +23 -0
- data/spec/support/mission_pipe_queue.rb +12 -1
- data/spec/support/sandbox.rb +4 -0
- data/third_party/pwn_rdoc.jsonl +138 -48
- metadata +34 -22
- data/etc/default_skills/pwn/plugins/ai_sandbox/SKILL.md +0 -52
- data/spec/lib/pwn/plugins/ai_sandbox_boundary_review_spec.rb +0 -57
|
@@ -21,6 +21,541 @@ module PWN
|
|
|
21
21
|
# Direct Learning.auto_introspect calls (specs, cron, tools) stay inline
|
|
22
22
|
# so existing ORM contracts still get a synchronous return value.
|
|
23
23
|
module TurnFinalizer
|
|
24
|
+
PATH_LITERAL = %r{["'`]((?:~/|/|\./|\.\./)[^"'`\n]+|[^"'`\n]+\.[A-Za-z0-9]+)["'`]|(?<![\w:/])((?:~/|/|\./|\.\./)[\w.+/-]+|[\w+-]+\.[A-Za-z0-9]+)}
|
|
25
|
+
WRITE_VERBS = %w[write save store output export create update modify edit generate put produce deliver mv move copy cp rename].freeze
|
|
26
|
+
TO_DESTINATION = %w[summarize summarise translate convert extract append redirect].freeze
|
|
27
|
+
FILE_CONTEXT = /\b(?:#{Regexp.union(WRITE_VERBS + TO_DESTINATION).source}|read|compare|inspect|review|analy[sz]e|from|using|into|to)\b/i
|
|
28
|
+
|
|
29
|
+
# A directory is not a file artifact. Only the operator's frozen skill
|
|
30
|
+
# destination can use this witness. Inventory metadata, never catalog
|
|
31
|
+
# bodies; a final check opens only the documents in the host receipt.
|
|
32
|
+
class SkillDirectory
|
|
33
|
+
MAX_ENTRIES = 8192
|
|
34
|
+
MAX_DEPTH = 12
|
|
35
|
+
MAX_DOCUMENTS = 16
|
|
36
|
+
MAX_BYTES = 1 << 20
|
|
37
|
+
FLAGS = File::RDONLY | File::NOFOLLOW | File::NONBLOCK
|
|
38
|
+
EXCLUDED = %w[assets scripts templates].freeze
|
|
39
|
+
|
|
40
|
+
# Read-only fd-relative traversal. Never mkdir, follow a symlink, or
|
|
41
|
+
# cross a mount below HOME (including a same-device bind mount).
|
|
42
|
+
class PinnedTree
|
|
43
|
+
def at(path, missing: false, &block)
|
|
44
|
+
raise IOError if path.count('/') > 128
|
|
45
|
+
|
|
46
|
+
if path == '/'
|
|
47
|
+
@mount = nil
|
|
48
|
+
return yield('/')
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
at(File.dirname(path), missing: missing) do |parent|
|
|
52
|
+
child = File.join(parent, File.basename(path))
|
|
53
|
+
begin
|
|
54
|
+
stat = File.lstat(child)
|
|
55
|
+
rescue Errno::ENOENT
|
|
56
|
+
raise unless missing
|
|
57
|
+
|
|
58
|
+
next
|
|
59
|
+
end
|
|
60
|
+
descend(child, stat) do |pinned, file|
|
|
61
|
+
@mount = mount_id(file) if path == Dir.home
|
|
62
|
+
block.call(pinned)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def descend(path, stat)
|
|
68
|
+
raise IOError unless stat.directory?
|
|
69
|
+
|
|
70
|
+
File.open(path, FLAGS) do |file|
|
|
71
|
+
actual = file.stat
|
|
72
|
+
raise IOError unless actual.directory? && actual.dev == stat.dev && actual.ino == stat.ino
|
|
73
|
+
raise IOError if @mount && !same_mount?(file)
|
|
74
|
+
|
|
75
|
+
result = yield "/proc/self/fd/#{file.fileno}", file
|
|
76
|
+
named = File.lstat(path)
|
|
77
|
+
raise IOError unless named.directory? && named.dev == actual.dev && named.ino == actual.ino
|
|
78
|
+
|
|
79
|
+
result
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def same_mount?(file)
|
|
84
|
+
mount_id(file) == @mount
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
def mount_id(file)
|
|
90
|
+
File.read("/proc/self/fdinfo/#{file.fileno}", 4096)[/^mnt_id:\s*(\d+)$/, 1] || raise(IOError)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
private_constant :PinnedTree
|
|
94
|
+
|
|
95
|
+
def initialize(path, library, contract)
|
|
96
|
+
@path = path
|
|
97
|
+
@library = library
|
|
98
|
+
@contract = contract
|
|
99
|
+
@tree = PinnedTree.new
|
|
100
|
+
@entries = {}
|
|
101
|
+
@remaining = MAX_ENTRIES
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
attr_reader :path, :contract, :entries, :action, :identity
|
|
105
|
+
|
|
106
|
+
def snapshot
|
|
107
|
+
# Only the inner dispatcher brackets a single action. Do not use
|
|
108
|
+
# the wider Loop watch window or a baseline from another action.
|
|
109
|
+
@action = Thread.current[:pwn_request_runtime].events.last
|
|
110
|
+
return unless @action && @action[:phase] == 'start' && !@action[:action_id].to_s.empty?
|
|
111
|
+
return unless %w[shell pwn_eval].include?(@action[:tool]) && direct_turn?
|
|
112
|
+
|
|
113
|
+
@tree.at(@path, missing: true) do |pinned|
|
|
114
|
+
@identity = directory_id(pinned)
|
|
115
|
+
inventory(pinned, '', 0)
|
|
116
|
+
end
|
|
117
|
+
@entries.freeze
|
|
118
|
+
self
|
|
119
|
+
rescue SystemCallError, IOError, ArgumentError, EncodingError
|
|
120
|
+
nil # An incomplete inventory is NOT an empty pre-write directory.
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def changes(before, action_id, tool)
|
|
124
|
+
return unless before.is_a?(SkillDirectory) && before.contract.equal?(@contract) && before.path == @path
|
|
125
|
+
return unless before.action == @action && @action.values_at(:action_id, :tool) == [action_id, tool]
|
|
126
|
+
# Moving a tree of existing documents is not a document write.
|
|
127
|
+
return if before.identity && before.identity != @identity
|
|
128
|
+
return unless before.entries.each_key.all? { |name| @entries.key?(name) }
|
|
129
|
+
|
|
130
|
+
changed = @entries.reject { |name, stat| before.entries[name] == stat }
|
|
131
|
+
# No pre-write digest for an entire catalog: a same-inode, same-size
|
|
132
|
+
# edit cannot be distinguished from touch. Name the file for that case.
|
|
133
|
+
return if changed.any? { |name, stat| before.entries[name]&.first(3) == stat.first(3) }
|
|
134
|
+
|
|
135
|
+
readback(changed)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def matches?(observed)
|
|
139
|
+
docs = observed[:skill_documents]
|
|
140
|
+
return false unless direct_turn? && docs.is_a?(Array) && docs.size.between?(1, MAX_DOCUMENTS) && docs.all?(Hash)
|
|
141
|
+
|
|
142
|
+
events = Thread.current[:pwn_request_runtime].events.select { |event| event[:action_id] == observed[:action_id] }
|
|
143
|
+
return false unless events.map { |e| e[:phase] } == %w[start end] && events.last[:state] == 'completed' && events.all? { |e| e[:tool] == observed[:tool] }
|
|
144
|
+
|
|
145
|
+
current = readback(docs.to_h { |doc| [doc[:path], doc[:stat]] })
|
|
146
|
+
current && current == observed.slice(*current.keys)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
private
|
|
150
|
+
|
|
151
|
+
def direct_turn?
|
|
152
|
+
!Thread.current[:pwn_loop_nested] && !Thread.current[:pwn_solve_tools] && !Thread.current[:pwn_swarm_depth].to_i.positive?
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def signature(stat)
|
|
156
|
+
[stat.dev, stat.ino, stat.size, stat.mtime.to_r.to_s, stat.ctime.to_r.to_s]
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def directory_id(pinned)
|
|
160
|
+
stat = File.stat(pinned)
|
|
161
|
+
[stat.dev, stat.ino]
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def inventory(pinned, prefix, depth)
|
|
165
|
+
raise IOError if depth > MAX_DEPTH
|
|
166
|
+
|
|
167
|
+
stamp = signature(File.stat(pinned))
|
|
168
|
+
Dir.each_child(pinned) do |name|
|
|
169
|
+
raise IOError if (@remaining -= 1).negative?
|
|
170
|
+
next if name.start_with?('.') || EXCLUDED.include?(name)
|
|
171
|
+
|
|
172
|
+
child = File.join(pinned, name)
|
|
173
|
+
relative = prefix + name
|
|
174
|
+
stat = File.lstat(child)
|
|
175
|
+
if stat.directory?
|
|
176
|
+
@tree.descend(child, stat) { |dir| inventory(dir, "#{relative}/", depth + 1) }
|
|
177
|
+
else
|
|
178
|
+
raise IOError unless stat.file? && stat.nlink == 1
|
|
179
|
+
|
|
180
|
+
@entries[relative] = signature(stat) if owner(relative)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
raise IOError unless signature(File.stat(pinned)) == stamp
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# A canonical entry or a text reference, never an arbitrary script,
|
|
187
|
+
# attachment, hidden path or directory named by tool stdout.
|
|
188
|
+
def owner(name)
|
|
189
|
+
return unless name.is_a?(String) && name.valid_encoding? && !name.match?(/[[:cntrl:]]/)
|
|
190
|
+
|
|
191
|
+
parts = File.join(@path, name).delete_prefix("#{@library}/").split('/', -1)
|
|
192
|
+
return if name.start_with?('/') || parts.any? { |part| part.empty? || part.start_with?('.') || EXCLUDED.include?(part) }
|
|
193
|
+
|
|
194
|
+
refs = parts.index('references')
|
|
195
|
+
return parts[0...refs] if refs&.positive? && %w[.md .txt].include?(File.extname(parts.last).downcase)
|
|
196
|
+
|
|
197
|
+
parts[0...-1] if !refs && parts.size > 1 && parts.last == 'SKILL.md'
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def readback(changed)
|
|
201
|
+
return unless changed.size.between?(1, MAX_DOCUMENTS)
|
|
202
|
+
|
|
203
|
+
owners = changed.keys.map { |name| owner(name) }.uniq
|
|
204
|
+
return unless owners.size == 1 && owners.first
|
|
205
|
+
|
|
206
|
+
identity = @tree.at(@path) { |pinned| directory_id(pinned) }
|
|
207
|
+
return if @identity && @identity != identity
|
|
208
|
+
|
|
209
|
+
docs = changed.sort.map { |name, stat| document(name, stat) }
|
|
210
|
+
# The runtime persists head/tail for file receipts. Never attach
|
|
211
|
+
# samples from a directory or publish an inventory of unrelated docs.
|
|
212
|
+
{ skill_directory_id: identity, skill_documents: docs, sha256: Digest::SHA256.hexdigest(JSON.generate(docs)),
|
|
213
|
+
skill_request: Digest::SHA256.hexdigest(@contract.fetch(:request)), head: '', tail: '' }
|
|
214
|
+
rescue SystemCallError, IOError, ArgumentError, EncodingError
|
|
215
|
+
nil
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def document(name, expected)
|
|
219
|
+
path = File.join(@path, name)
|
|
220
|
+
@tree.at(File.dirname(path)) do |dir|
|
|
221
|
+
child = File.join(dir, File.basename(path))
|
|
222
|
+
stat = File.lstat(child)
|
|
223
|
+
raise IOError unless matching_file?(stat, expected) && stat.size.between?(1, MAX_BYTES)
|
|
224
|
+
|
|
225
|
+
File.open(child, FLAGS) do |file|
|
|
226
|
+
raise IOError unless @tree.same_mount?(file) && matching_file?(file.stat, expected)
|
|
227
|
+
|
|
228
|
+
bytes = file.binmode.read(stat.size + 1).to_s.force_encoding(Encoding::UTF_8)
|
|
229
|
+
raise IOError unless bytes.bytesize == stat.size && bytes.valid_encoding? && !bytes.include?("\0") && bytes.match?(/\S/)
|
|
230
|
+
raise IOError unless matching_file?(file.stat, expected) && matching_file?(File.lstat(child), expected)
|
|
231
|
+
|
|
232
|
+
{ path: name, stat: expected, sha256: Digest::SHA256.hexdigest(bytes) }
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def matching_file?(stat, expected)
|
|
238
|
+
stat.file? && stat.nlink == 1 && signature(stat) == expected
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
private_constant :SkillDirectory
|
|
242
|
+
|
|
243
|
+
# Bounded, local document work. Only a verb and its file operands qualify;
|
|
244
|
+
# an additional action or an ambiguous target stays on the ordinary path.
|
|
245
|
+
# Keep coverage in the controller for this turn, never in the model prompt.
|
|
246
|
+
class InputPages
|
|
247
|
+
LIST = /\A\s*(?:please\s+)?(?:read|compare|inspect|review|summari[sz]e|analy[sz]e)\s+
|
|
248
|
+
(?:(?:the|these|both)\s+)?(?<files>files?\s*:?\s*)?\x00
|
|
249
|
+
(?:\s*(?:,\s*(?:and\s+)?|and|to|with|against|versus|vs\.?)\s*\x00)*
|
|
250
|
+
(?:\s*[,;]?\s*(?:and\s+)?(?:reply|answer|respond)\s+in\s+(?:(?:the|this)\s+)?chat)?\s*[.!?]?\s*\z/ix
|
|
251
|
+
|
|
252
|
+
public_class_method def self.for_turn(opts = {})
|
|
253
|
+
runtime = opts[:runtime]
|
|
254
|
+
return if opts[:nested] || opts[:unattended] || opts[:solve_handoff]
|
|
255
|
+
return unless runtime && runtime.request == opts[:request] && runtime.contract[:required_artifacts].empty?
|
|
256
|
+
|
|
257
|
+
for_request(request: opts[:request], cwd: runtime.contract.fetch(:cwd))
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
public_class_method def self.for_request(opts = {})
|
|
261
|
+
paths = operands(request: opts.fetch(:request))
|
|
262
|
+
new(paths, opts.fetch(:cwd)) if paths
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
public_class_method def self.operands(opts = {})
|
|
266
|
+
request = opts.fetch(:request)
|
|
267
|
+
return if request.include?("\0") || request.match?(%r{\b[a-z][a-z\d+.-]*://}i)
|
|
268
|
+
|
|
269
|
+
paths = []
|
|
270
|
+
bare_name = false
|
|
271
|
+
shape = request.gsub(PATH_LITERAL) do
|
|
272
|
+
quoted, bare = Regexp.last_match.captures
|
|
273
|
+
bare_name ||= !quoted && !bare.start_with?('/', './', '../', '~/')
|
|
274
|
+
paths << (quoted || bare.sub(/[.,;:]+\z/, ''))
|
|
275
|
+
"\0"
|
|
276
|
+
end
|
|
277
|
+
list = LIST.match(shape)
|
|
278
|
+
# A bare dotted name is ambiguous unless the operator called it a file.
|
|
279
|
+
paths if list && (!bare_name || list[:files])
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def initialize(paths, cwd)
|
|
283
|
+
@cwd = cwd
|
|
284
|
+
@pages = paths.to_h { |path| [File.expand_path(path, cwd), nil] }
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Only the registered reader's successful result counts. A proposed
|
|
288
|
+
# call, a grep preview, or an unrelated tool printing JSON is not a page.
|
|
289
|
+
def observe(name, raw)
|
|
290
|
+
return unless name == 'artifact_read'
|
|
291
|
+
|
|
292
|
+
result = JSON.parse(raw, symbolize_names: true)
|
|
293
|
+
return unless result.is_a?(Hash) && result[:success] == true && result[:effect] == 'read'
|
|
294
|
+
|
|
295
|
+
page = result[:result]
|
|
296
|
+
return unless page.is_a?(Hash) && page[:path].is_a?(String) && page[:body].is_a?(String)
|
|
297
|
+
|
|
298
|
+
path = File.expand_path(page[:path], @cwd)
|
|
299
|
+
first, last, size = page.values_at(:offset, :next_offset, :total_bytes)
|
|
300
|
+
return unless @pages.key?(path) && [first, last, size].all?(Integer) && first.between?(0, last) && last <= size && page[:bytes] == last - first
|
|
301
|
+
|
|
302
|
+
state = @pages[path]
|
|
303
|
+
state = @pages[path] = { size: size, ranges: {} } unless state && state[:size] == size
|
|
304
|
+
state[:ranges][first] = [last, state[:ranges].fetch(first, first)].max
|
|
305
|
+
rescue JSON::ParserError, ArgumentError
|
|
306
|
+
nil
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
def unmet
|
|
310
|
+
@pages.filter_map do |path, state|
|
|
311
|
+
offset = next_missing(state)
|
|
312
|
+
"input_unread:#{path} (next byte #{offset})" if offset
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
private
|
|
317
|
+
|
|
318
|
+
def next_missing(state)
|
|
319
|
+
return 0 unless state
|
|
320
|
+
|
|
321
|
+
offset = 0
|
|
322
|
+
state[:ranges].sort.each do |first, last|
|
|
323
|
+
break if first > offset
|
|
324
|
+
|
|
325
|
+
offset = [offset, last].max
|
|
326
|
+
end
|
|
327
|
+
offset unless offset == state[:size]
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# A visible reply that does not certify the requested work. A partial
|
|
332
|
+
# chat draft is presentation only, never an action or a learning label.
|
|
333
|
+
class TurnNotice < String
|
|
334
|
+
CHAT_DESTINATION = /\b(?:in (?:this |the )?chat|in (?:your|the) (?:reply|response)|here in (?:your|the) reply)\b/i
|
|
335
|
+
|
|
336
|
+
# Another prose answer isn't a changed observation of a missing file.
|
|
337
|
+
class Gap
|
|
338
|
+
def initialize(opts, nested)
|
|
339
|
+
@public_draft = !nested && !opts[:solve_handoff] && !opts[:force_tools] && !opts[:verification_contract]
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
def stop_reason(unmet, no_tools, runtime)
|
|
343
|
+
return 'no_available_tools' if no_tools
|
|
344
|
+
|
|
345
|
+
paths = runtime.contract[:required_artifacts]
|
|
346
|
+
return unless unmet.any? { |row| row.start_with?('input_unread:') || paths.include?(row.to_s.partition(':').last) }
|
|
347
|
+
|
|
348
|
+
observed = runtime.observations.slice(*paths).transform_values { |row| row[:sha256] }
|
|
349
|
+
state = [unmet.uniq.sort, observed]
|
|
350
|
+
return 'unchanged_evidence' if @last == state
|
|
351
|
+
|
|
352
|
+
@last = state
|
|
353
|
+
nil
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def notice(runtime, unmet, reason, msg)
|
|
357
|
+
draft = msg[:content].to_s if @public_draft && !msg[:_thinking_promoted]
|
|
358
|
+
TurnNotice.new.unfinished(runtime: runtime, unmet: unmet, reason: reason, candidate: draft)
|
|
359
|
+
end
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def unfinished(runtime:, unmet:, reason:, candidate: nil)
|
|
363
|
+
summary = case reason
|
|
364
|
+
when 'no_available_tools' then 'No tools are available in this request.'
|
|
365
|
+
when 'unchanged_evidence' then 'The outstanding requirement is unchanged; there is no new evidence to check.'
|
|
366
|
+
else 'No specific unmet requirement was identified to retry.'
|
|
367
|
+
end
|
|
368
|
+
outstanding = Array(runtime.contract[:required_artifacts]).select do |path|
|
|
369
|
+
unmet.any? { |row| row.to_s.partition(':').last == path }
|
|
370
|
+
end
|
|
371
|
+
self << "[pwn-ai] blocked: #{summary}"
|
|
372
|
+
unread = unmet.grep(/\Ainput_unread:/).map { |row| row.delete_prefix('input_unread:') }
|
|
373
|
+
self << if unread.any?
|
|
374
|
+
" Input not fully read: #{unread.join(', ')}."
|
|
375
|
+
elsif outstanding.empty?
|
|
376
|
+
' The requested work was not verified.'
|
|
377
|
+
else
|
|
378
|
+
" File delivery was not verified: #{outstanding.join(', ')}."
|
|
379
|
+
end
|
|
380
|
+
partial = outstanding.any? && candidate && runtime.request.match?(CHAT_DESTINATION)
|
|
381
|
+
if partial
|
|
382
|
+
self << "\n\nChat draft (not evidence of file delivery):\n"
|
|
383
|
+
self << candidate.lines.map { |line| "> #{line}" }.join
|
|
384
|
+
end
|
|
385
|
+
runtime.finish!(status: 'blocked', goal_status: partial ? 'partial' : 'blocked', stage: 'finalization', unmet: unmet + [reason])
|
|
386
|
+
self
|
|
387
|
+
end
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
# An operator-facing reply is a deliverable, not an observation of an
|
|
391
|
+
# external effect. The same object governs the main hop and its final.
|
|
392
|
+
class Conversation
|
|
393
|
+
TEXT = /\A(?:compose|draft|translate|paraphrase|rephrase|convert|write (?:me|us)|how many|what is the number of)\b/i
|
|
394
|
+
# A stated writing format specifies the medium, not the topic. A
|
|
395
|
+
# bare 'write a report' remains ambiguous about the destination.
|
|
396
|
+
WRITING_FORMAT = /\Awrite\b[^;\n]*?\b(?:words?|sentences?|paragraphs?|notes?|poems?|letters?|captions?)\b/i
|
|
397
|
+
QUESTION = /\A(?:what|which|when|where|why|who|how|is|are|will|does|do|can (?:we|I)|should (?:we|I))\b/i
|
|
398
|
+
EXTERNAL = %r{https?://|[/\\]|\b(?:files?|folder|directory|repo(?:sitory)?|on\s+disk|hostname|machine|subnet|shell|server|processes|devices|installed|running|logs?|backups?|database|configuration|settings|calendar)\b|
|
|
399
|
+
\b(?:save|store|export|publish|send|upload|download|email|execute|install|schedule|scan|wait|monitor)\b}ix
|
|
400
|
+
INSTRUCTIONS = <<~TEXT
|
|
401
|
+
The operator's request is the goal; a proposed plan is not an additional deliverable.
|
|
402
|
+
Return your public reply as JSON: {"pwn_turn":"answer", "text":"the answer"}.
|
|
403
|
+
If an essential input is missing, use "needs_input"; if only part is ready, use "partial".
|
|
404
|
+
For either, give the useful portion and a precise question or next requirement in text, and name it in "remaining".
|
|
405
|
+
Use the supplied conversation. Do not ask twice, invent a current source, or claim an external action from prose.
|
|
406
|
+
TEXT
|
|
407
|
+
|
|
408
|
+
def select(opts)
|
|
409
|
+
return if %i[nested force_tools verification_contract runtime unattended mission_id solve_handoff min_seconds steering_history require_resumable request_id].any? { |key| opts[key] }
|
|
410
|
+
return if opts[:trusted_context] || opts[:core_only] == false
|
|
411
|
+
return if Thread.current[:pwn_request_runtime] || Thread.current[:pwn_swarm_depth].to_i.positive? || TurnFinalizer.user_path?
|
|
412
|
+
|
|
413
|
+
text = opts[:request].to_s.strip
|
|
414
|
+
return if text.empty? || text.match?(OpenGoal::CONTINUE_RX) || text.match?(EXTERNAL) || text.match?(Loop::BROWSER_REQUEST_RX)
|
|
415
|
+
return if InputPages.operands(request: text)
|
|
416
|
+
return unless Loop.request_intent(request: text) == :act && !Loop.catalog_lookup?(request: text)
|
|
417
|
+
return unless TurnFinalizer.output_paths(request: text).empty?
|
|
418
|
+
|
|
419
|
+
clause = text.sub(/\A(?:(?:could|can|would) you )?(?:please )?/i, '')
|
|
420
|
+
@no_sources = opts[:enabled_toolsets] == []
|
|
421
|
+
question = QUESTION.match?(clause)
|
|
422
|
+
# A chat destination says where to answer, not where a fact comes from.
|
|
423
|
+
@text_only = TEXT.match?(clause) || WRITING_FORMAT.match?(clause) || (!question && clause.match?(TurnNotice::CHAT_DESTINATION))
|
|
424
|
+
effects = @text_only ? clause.sub(/\A(?:write|create)\b/i, '') : clause
|
|
425
|
+
return if effects.match?(Loop::ACT_REQUEST_RX) || effects.sub(/\Ahow many\b/i, '').match?(Loop::LOOKUP_REQUEST_RX)
|
|
426
|
+
return if Mission.active_id
|
|
427
|
+
|
|
428
|
+
sid = opts[:session_id]
|
|
429
|
+
current = OpenGoal.for_session(session_id: sid) unless sid.to_s.empty?
|
|
430
|
+
return if current && !current[:reply_question]
|
|
431
|
+
|
|
432
|
+
if current && follows_question?(current, sid)
|
|
433
|
+
@pending = current
|
|
434
|
+
@text_only ||= current[:reply_medium] == 'text'
|
|
435
|
+
end
|
|
436
|
+
self if @pending || @text_only || question
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
def text_only?
|
|
440
|
+
@text_only
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
def permits_reply?(runtime)
|
|
444
|
+
runtime && !runtime.verification_contract && runtime.contract[:required_artifacts].empty? && runtime.blocker.nil?
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
def unprocessed?(msg)
|
|
448
|
+
msg[:_thinking_promoted] || Array(msg[:tool_calls]).any?
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# Only already-configured provider retrieval, never local functions
|
|
452
|
+
# or a code interpreter. A source is optional, not a plan obligation.
|
|
453
|
+
def sources(engine, config, model)
|
|
454
|
+
return [] if text_only? || @no_sources
|
|
455
|
+
return [] unless engine == :grok || PWN::AI::ServerTools.mixable?(engine: engine, model: model)
|
|
456
|
+
|
|
457
|
+
Array(config[:server_tools]).map(&:to_s) & PWN::AI::ServerTools::WIRE.fetch(engine, {}).keys & %w[web_search web_fetch google_search x_search]
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
def with_instructions(messages, sources:)
|
|
461
|
+
medium = if text_only?
|
|
462
|
+
'The requested result is text. No external actions are available.'
|
|
463
|
+
elsif sources.empty?
|
|
464
|
+
'No online source is enabled for this turn. If current information is needed and missing, return partial and ask for the relevant source text.'
|
|
465
|
+
else
|
|
466
|
+
"Provider-hosted sources available now: #{sources.join(', ')}. Use them when the answer needs a lookup and cite the source used. No local functions are available."
|
|
467
|
+
end
|
|
468
|
+
follow_up = ' If this message supplies the requested input and your reply answers or advances that earlier goal, also set "continues":true.' if @pending
|
|
469
|
+
first_turn = messages.take_while { |row| %w[system developer].include?((row[:role] || row['role']).to_s) }.length
|
|
470
|
+
messages.dup.insert(first_turn, { role: 'system', content: "#{INSTRUCTIONS}#{medium}#{follow_up}" })
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
def unprocessed(runtime, msg)
|
|
474
|
+
text = msg[:content].to_s unless msg[:_thinking_promoted]
|
|
475
|
+
visible = text.to_s.match?(Loop::DisplayReply::BLANK) ? 'No public answer was available.' : decode(text).fetch(:text)
|
|
476
|
+
notice = TurnNotice.new(visible)
|
|
477
|
+
pending = Array(msg[:tool_calls]).any?
|
|
478
|
+
notice << "\n\nAn action from this reply was not processed." if pending
|
|
479
|
+
save(runtime, 'notice', notice)
|
|
480
|
+
runtime.finish!(status: 'incomplete', stage: 'provider', unmet: [pending ? 'unprocessed_action' : 'public_reply_missing'])
|
|
481
|
+
notice
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# Preview contains public text and a pending bit, never a runnable provider message.
|
|
485
|
+
def present_preview(runtime, preview, on_notice)
|
|
486
|
+
return finish(runtime, preview.text) if preview.answer?
|
|
487
|
+
|
|
488
|
+
runtime.check_cancelled!
|
|
489
|
+
visible = decode(preview.text).fetch(:text)
|
|
490
|
+
save(runtime, 'notice', preview.present(nil, public_text: visible))
|
|
491
|
+
runtime.finish!(status: 'incomplete', stage: 'provider', unmet: [preview.pending ? 'unprocessed_action' : 'public_reply_missing'])
|
|
492
|
+
preview.present(on_notice, public_text: visible)
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
def finish(runtime, content)
|
|
496
|
+
runtime.check_cancelled!
|
|
497
|
+
disposition = decode(content)
|
|
498
|
+
reply = disposition.fetch(:text)
|
|
499
|
+
state = disposition.fetch(:pwn_turn)
|
|
500
|
+
sid = runtime.session_id
|
|
501
|
+
continues = @pending && disposition[:continues] == true
|
|
502
|
+
row = save(runtime, 'assistant', reply)
|
|
503
|
+
if state == 'answer'
|
|
504
|
+
runtime.observe_evidence!(kind: 'answer', requirement: runtime.original_request, locator: 'chat:reply', sha256: Digest::SHA256.hexdigest(row.fetch(:content))) if row && text_only?
|
|
505
|
+
runtime.finish!(status: 'complete')
|
|
506
|
+
OpenGoal.clear!(answered_id: @pending[:request_id]) if continues
|
|
507
|
+
else
|
|
508
|
+
awaiting = state == 'needs_input'
|
|
509
|
+
runtime.finish!(status: 'incomplete', goal_status: awaiting ? 'awaiting_input' : 'partial', stage: awaiting ? 'clarification' : 'finalization', unmet: [disposition[:remaining]])
|
|
510
|
+
unless sid.empty?
|
|
511
|
+
OpenGoal.begin!(request: runtime.original_request, session_id: sid, answered_id: (@pending[:request_id] if continues),
|
|
512
|
+
reply_question: (Digest::SHA256.hexdigest(row.fetch(:content)) if awaiting && row), reply_medium: text_only? ? 'text' : 'information')
|
|
513
|
+
end
|
|
514
|
+
end
|
|
515
|
+
reply
|
|
516
|
+
end
|
|
517
|
+
|
|
518
|
+
private
|
|
519
|
+
|
|
520
|
+
# Direct callers need not have a transcript. A returned string and a
|
|
521
|
+
# persisted row are different observations; never invent the latter.
|
|
522
|
+
def save(runtime, role, text)
|
|
523
|
+
PWN::Sessions.append(session_id: runtime.session_id, role: role, content: text) unless runtime.session_id.empty?
|
|
524
|
+
rescue IOError, SystemCallError
|
|
525
|
+
nil
|
|
526
|
+
end
|
|
527
|
+
|
|
528
|
+
def follows_question?(goal, sid)
|
|
529
|
+
rows = PWN::Sessions.load(session_id: sid).last(2)
|
|
530
|
+
return false unless rows in [{ role: 'user', content: String }, { role: 'assistant', content: String }]
|
|
531
|
+
|
|
532
|
+
previous = goal[:reply_after] || Digest::SHA256.hexdigest(goal.fetch(:request))
|
|
533
|
+
Digest::SHA256.hexdigest(rows.first[:content].strip) == previous && Digest::SHA256.hexdigest(rows.last[:content]) == goal[:reply_question]
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
def decode(text)
|
|
537
|
+
if text.lstrip.start_with?('{')
|
|
538
|
+
row = begin
|
|
539
|
+
JSON.parse(text, symbolize_names: true)
|
|
540
|
+
rescue JSON::ParserError
|
|
541
|
+
nil # Unframed JSON can itself be the requested answer.
|
|
542
|
+
end
|
|
543
|
+
return row if framed?(row)
|
|
544
|
+
end
|
|
545
|
+
# Older models can return ordinary prose. Display it verbatim, but
|
|
546
|
+
# don't certify an unlabelled question as the answer it asks for.
|
|
547
|
+
return { pwn_turn: 'needs_input', text: text, remaining: text.strip } if text.rstrip.end_with?('?')
|
|
548
|
+
|
|
549
|
+
{ pwn_turn: 'answer', text: text }
|
|
550
|
+
end
|
|
551
|
+
|
|
552
|
+
def framed?(row)
|
|
553
|
+
return false unless row.is_a?(Hash) && row[:text].is_a?(String) && !row[:text].match?(Loop::DisplayReply::BLANK)
|
|
554
|
+
|
|
555
|
+
row[:pwn_turn] == 'answer' || (%w[needs_input partial].include?(row[:pwn_turn]) && row[:remaining].is_a?(String) && !row[:remaining].strip.empty?)
|
|
556
|
+
end
|
|
557
|
+
end
|
|
558
|
+
|
|
24
559
|
MUTEX = Mutex.new
|
|
25
560
|
THREADS = [] # rubocop:disable Style/MutableConstant
|
|
26
561
|
MAX_TRACKED = 16
|
|
@@ -208,8 +743,9 @@ module PWN
|
|
|
208
743
|
request = opts[:request].to_s
|
|
209
744
|
paths = []
|
|
210
745
|
intent = nil
|
|
746
|
+
operation = nil
|
|
211
747
|
previous_end = 0
|
|
212
|
-
request.to_enum(:scan,
|
|
748
|
+
request.to_enum(:scan, PATH_LITERAL).each do
|
|
213
749
|
match = Regexp.last_match
|
|
214
750
|
literal = match[1] || match[2].sub(/[.,;:]+\z/, '')
|
|
215
751
|
# Unquoted dotted initials followed by a period are prose (i.e.,
|
|
@@ -218,19 +754,34 @@ module PWN
|
|
|
218
754
|
|
|
219
755
|
prefix = request[previous_end...match.begin(0)]
|
|
220
756
|
previous_end = match.end(0)
|
|
221
|
-
|
|
222
|
-
|
|
757
|
+
prefix.downcase.scan(FILE_CONTEXT).each do |word|
|
|
758
|
+
case word
|
|
759
|
+
when 'from', 'using' then intent = :input
|
|
760
|
+
when 'into', 'to' then intent = operation
|
|
761
|
+
when *TO_DESTINATION
|
|
762
|
+
operation = :output
|
|
763
|
+
intent = :input
|
|
764
|
+
else intent = operation = WRITE_VERBS.include?(word) ? :output : :input
|
|
765
|
+
end
|
|
766
|
+
end
|
|
767
|
+
next unless intent == :output
|
|
223
768
|
|
|
224
769
|
paths << File.expand_path(literal, opts[:cwd] || Dir.pwd)
|
|
225
770
|
end
|
|
226
771
|
paths.uniq
|
|
227
772
|
end
|
|
228
773
|
|
|
229
|
-
#
|
|
774
|
+
# Only committed destinations may be sampled during a turn. A known
|
|
775
|
+
# missing file has a nil baseline; an unobservable file has NO baseline.
|
|
230
776
|
public_class_method def self.artifact_snapshot(opts = {})
|
|
231
|
-
|
|
777
|
+
contract = Thread.current[:pwn_artifact_contract]
|
|
778
|
+
Array(opts[:paths]).each_with_object({}) do |literal, rows|
|
|
232
779
|
path = File.expand_path(literal.to_s)
|
|
233
|
-
[
|
|
780
|
+
next if contract && !contract[:required_artifacts].include?(path)
|
|
781
|
+
|
|
782
|
+
directory = skill_directory_for(path: path)
|
|
783
|
+
row = directory ? directory.snapshot : artifact_readback(path: path)
|
|
784
|
+
rows[path] = row unless row == :unavailable
|
|
234
785
|
end
|
|
235
786
|
end
|
|
236
787
|
|
|
@@ -240,9 +791,12 @@ module PWN
|
|
|
240
791
|
return {} unless opts[:success] == true && opts[:effect].to_s == 'write'
|
|
241
792
|
|
|
242
793
|
before = opts[:before] || {}
|
|
243
|
-
observed = artifact_snapshot(paths: opts[:paths]).
|
|
244
|
-
row && before.key?(path) && before[path] != row
|
|
245
|
-
|
|
794
|
+
observed = artifact_snapshot(paths: opts[:paths]).filter_map do |path, row|
|
|
795
|
+
next unless row && before.key?(path) && before[path] != row
|
|
796
|
+
|
|
797
|
+
row = row.changes(before[path], opts[:action_id], opts[:tool]) if row.is_a?(SkillDirectory)
|
|
798
|
+
[path, row] if row
|
|
799
|
+
end.to_h
|
|
246
800
|
observed = observed.transform_values do |row|
|
|
247
801
|
contract = Thread.current[:pwn_artifact_contract]
|
|
248
802
|
row = row.merge(action_id: opts[:action_id], tool: opts[:tool]) if opts[:action_id]
|
|
@@ -253,27 +807,75 @@ module PWN
|
|
|
253
807
|
observed
|
|
254
808
|
end
|
|
255
809
|
|
|
810
|
+
private_class_method def self.skill_directory_for(opts = {})
|
|
811
|
+
path = opts.fetch(:path)
|
|
812
|
+
contract = Thread.current[:pwn_artifact_contract]
|
|
813
|
+
return unless contract && contract.dig(:skill_destinations, path) == :directory
|
|
814
|
+
return unless Thread.current[:pwn_request_runtime]&.contract.equal?(contract) && contract[:required_artifacts].include?(path)
|
|
815
|
+
|
|
816
|
+
library = File.join(Dir.home, '.pwn', 'skills')
|
|
817
|
+
SkillDirectory.new(path, library, contract) if path == library || path.start_with?("#{library}/")
|
|
818
|
+
end
|
|
819
|
+
|
|
256
820
|
private_class_method def self.artifact_readback(opts = {})
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
821
|
+
path = opts.fetch(:path)
|
|
822
|
+
begin
|
|
823
|
+
stat = File.lstat(path)
|
|
824
|
+
rescue Errno::ENOENT, Errno::ENOTDIR
|
|
825
|
+
return nil
|
|
826
|
+
end
|
|
827
|
+
return :unavailable unless stat.file?
|
|
828
|
+
return nil unless stat.size.positive?
|
|
260
829
|
|
|
830
|
+
with_artifact_file(path: path, stat: stat) do |file|
|
|
831
|
+
file.binmode
|
|
261
832
|
head = file.read(4096).to_s
|
|
262
833
|
file.seek([stat.size - 4096, 0].max)
|
|
263
834
|
tail = file.read(4096).to_s
|
|
264
835
|
file.rewind
|
|
265
836
|
digest = Digest::SHA256.new
|
|
266
|
-
|
|
837
|
+
remaining = stat.size
|
|
838
|
+
while remaining.positive?
|
|
839
|
+
chunk = file.read([remaining, 65_536].min)
|
|
840
|
+
return :unavailable unless chunk && !chunk.empty?
|
|
841
|
+
|
|
267
842
|
digest.update(chunk)
|
|
843
|
+
remaining -= chunk.bytesize
|
|
268
844
|
end
|
|
269
|
-
|
|
270
|
-
return nil unless [current.ino, current.size, current.mtime, current.ctime] == [stat.ino, stat.size, stat.mtime, stat.ctime]
|
|
845
|
+
next :unavailable unless same_artifact?(expected: stat, actual: file.stat) && same_artifact?(expected: stat, actual: File.lstat(path))
|
|
271
846
|
|
|
272
847
|
{ stat: { size: stat.size, ino: stat.ino, dev: stat.dev, mtime: stat.mtime.to_r.to_s, ctime: stat.ctime.to_r.to_s },
|
|
273
848
|
head: head, tail: tail, sha256: digest.hexdigest }
|
|
274
849
|
end
|
|
275
|
-
rescue
|
|
276
|
-
|
|
850
|
+
rescue SystemCallError, IOError
|
|
851
|
+
:unavailable # No receipt for this path; independent outputs still count.
|
|
852
|
+
end
|
|
853
|
+
|
|
854
|
+
private_class_method def self.same_artifact?(opts = {})
|
|
855
|
+
expected = opts.fetch(:expected)
|
|
856
|
+
actual = opts.fetch(:actual)
|
|
857
|
+
actual.file? && expected.dev == actual.dev && expected.ino == actual.ino && expected.size == actual.size &&
|
|
858
|
+
expected.mtime == actual.mtime && expected.ctime == actual.ctime
|
|
859
|
+
end
|
|
860
|
+
|
|
861
|
+
private_class_method def self.with_artifact_file(opts = {})
|
|
862
|
+
path = opts.fetch(:path)
|
|
863
|
+
stat = opts.fetch(:stat)
|
|
864
|
+
flags = File::RDONLY | File::NONBLOCK
|
|
865
|
+
read_regular = lambda do |file|
|
|
866
|
+
next :unavailable unless same_artifact?(expected: stat, actual: file.stat)
|
|
867
|
+
|
|
868
|
+
yield file
|
|
869
|
+
end
|
|
870
|
+
return File.open(path, flags | File::NOFOLLOW, &read_regular) unless RUBY_PLATFORM.include?('linux')
|
|
871
|
+
|
|
872
|
+
# O_PATH pins the inode without opening a device driver. Reopen
|
|
873
|
+
# that descriptor for bytes, never a potentially replaced pathname.
|
|
874
|
+
File.open(path, 0x200000 | File::NOFOLLOW) do |pin|
|
|
875
|
+
next :unavailable unless same_artifact?(expected: stat, actual: pin.stat)
|
|
876
|
+
|
|
877
|
+
File.open("/proc/self/fd/#{pin.fileno}", flags, &read_regular)
|
|
878
|
+
end
|
|
277
879
|
end
|
|
278
880
|
|
|
279
881
|
public_class_method def self.arbitrate(opts = {})
|
|
@@ -283,15 +885,19 @@ module PWN
|
|
|
283
885
|
paths = Array(opts[:paths]).map { |path| File.expand_path(path.to_s) }
|
|
284
886
|
paths += required_artifacts(request: request)
|
|
285
887
|
paths.uniq!
|
|
286
|
-
ledger = evidence_ledger(messages: messages)
|
|
888
|
+
ledger = evidence_ledger(messages: messages, request: request, paths: paths)
|
|
287
889
|
unmet = []
|
|
288
890
|
paths.each do |path|
|
|
289
891
|
row = ledger[path]
|
|
892
|
+
if skill_directory_for(path: path)
|
|
893
|
+
unmet << { criterion: 'skill_document_unverified', detail: path } unless row&.dig(:evidence, :skill_documents)
|
|
894
|
+
next
|
|
895
|
+
end
|
|
290
896
|
unless File.file?(path)
|
|
291
897
|
unmet << { criterion: 'artifact_missing', detail: path }
|
|
292
898
|
next
|
|
293
899
|
end
|
|
294
|
-
unmet << { criterion: 'empty_artifact', detail: path } if File.
|
|
900
|
+
unmet << { criterion: 'empty_artifact', detail: path } if File.empty?(path)
|
|
295
901
|
unmet << { criterion: 'write_missing', detail: path } unless row && row[:write]
|
|
296
902
|
unmet << { criterion: 'readback_missing', detail: path } unless row && row[:read]
|
|
297
903
|
# Filesystem timestamps can lag Time.now by a clock tick. A bound
|
|
@@ -330,23 +936,32 @@ module PWN
|
|
|
330
936
|
|
|
331
937
|
public_class_method def self.evidence_ledger(opts = {})
|
|
332
938
|
ledger = {}
|
|
939
|
+
contract = Thread.current[:pwn_artifact_contract]
|
|
940
|
+
paths = Array(opts[:paths] || contract&.dig(:required_artifacts) || required_artifacts(request: opts[:request]))
|
|
941
|
+
paths &= contract[:required_artifacts] if contract
|
|
333
942
|
runtime = Thread.current[:pwn_request_runtime]
|
|
334
943
|
messages = Array(opts[:messages])
|
|
335
|
-
|
|
944
|
+
host = { role: 'tool', artifact_observations: runtime.observations } if runtime
|
|
945
|
+
messages += [host] if host
|
|
336
946
|
messages.each do |msg|
|
|
337
947
|
next unless msg.is_a?(Hash) && msg[:role].to_s == 'tool'
|
|
338
948
|
|
|
339
949
|
next unless msg[:artifact_observations].is_a?(Hash)
|
|
340
950
|
|
|
341
951
|
msg[:artifact_observations].each do |path, observed|
|
|
342
|
-
next unless observed.is_a?(Hash)
|
|
343
|
-
|
|
344
|
-
contract = Thread.current[:pwn_artifact_contract]
|
|
952
|
+
next unless paths.include?(path) && observed.is_a?(Hash)
|
|
345
953
|
next if contract && observed[:artifact_turn_id] != contract[:id]
|
|
346
954
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
955
|
+
directory = skill_directory_for(path: path)
|
|
956
|
+
if directory
|
|
957
|
+
next unless msg.equal?(host) && opts.fetch(:request, contract[:request]) == contract[:request] && directory.matches?(observed)
|
|
958
|
+
else
|
|
959
|
+
next if observed.key?(:skill_documents)
|
|
960
|
+
|
|
961
|
+
current = artifact_readback(path: path)
|
|
962
|
+
readback = observed.except(:artifact_turn_id, :action_id, :tool, :child_request_id, :child_turn_id, :child_provenance)
|
|
963
|
+
next unless current && current == readback
|
|
964
|
+
end
|
|
350
965
|
|
|
351
966
|
ledger[path] = { write: true, read: true, evidence: observed }
|
|
352
967
|
end
|
|
@@ -409,6 +1024,19 @@ module PWN
|
|
|
409
1024
|
# Extract explicit output literals, excluding source/read paths.
|
|
410
1025
|
#{self}.output_paths(request: 'required - original user request', cwd: 'optional - relative path base')
|
|
411
1026
|
|
|
1027
|
+
# Track input pages for an interactive turn with no file delivery.
|
|
1028
|
+
#{self}::InputPages.for_turn(
|
|
1029
|
+
request: 'required - original operator request', runtime: 'required - runtime that owns the request',
|
|
1030
|
+
nested: 'optional - true for a nested turn', unattended: 'optional - true for an unattended turn',
|
|
1031
|
+
solve_handoff: 'optional - true when handing work to the solver'
|
|
1032
|
+
)
|
|
1033
|
+
|
|
1034
|
+
# Build a page tracker for an explicit local file read.
|
|
1035
|
+
#{self}::InputPages.for_request(request: 'required - original operator request', cwd: 'required - directory for resolving input paths')
|
|
1036
|
+
|
|
1037
|
+
# Return the literal input filenames in an unambiguous read request.
|
|
1038
|
+
#{self}::InputPages.operands(request: 'required - original operator request')
|
|
1039
|
+
|
|
412
1040
|
# Precommit immutable required_artifacts for this turn before tools run.
|
|
413
1041
|
#{self}.commit_artifacts!(request: 'required - original user request', cwd: 'optional - initial relative path base', inherit: 'optional - reuse the owning turn contract for a nested call')
|
|
414
1042
|
|
|
@@ -416,9 +1044,11 @@ module PWN
|
|
|
416
1044
|
#{self}.required_artifacts(request: 'required - original user request', cwd: 'optional - relative path base outside a turn')
|
|
417
1045
|
|
|
418
1046
|
# Snapshot named destinations immediately before a tool runs.
|
|
1047
|
+
# Operator-named default skill directories inventory bounded metadata only.
|
|
419
1048
|
#{self}.artifact_snapshot(paths: 'required - destination paths')
|
|
420
1049
|
|
|
421
1050
|
# Host-only post-dispatch write delta and stat/head-tail readback.
|
|
1051
|
+
# Skill directory receipts contain only changed document paths, metadata and digests.
|
|
422
1052
|
# Keep this result in internal tool-message artifact_observations.
|
|
423
1053
|
#{self}.observe_artifacts(
|
|
424
1054
|
paths: 'required - destination paths', before: 'required - artifact_snapshot result',
|
|
@@ -437,7 +1067,9 @@ module PWN
|
|
|
437
1067
|
|
|
438
1068
|
# Build path → {write,read} from host-owned artifact_observations only.
|
|
439
1069
|
#{self}.evidence_ledger(
|
|
440
|
-
messages: 'optional - Array of role/content hashes'
|
|
1070
|
+
messages: 'optional - Array of role/content hashes',
|
|
1071
|
+
request: 'optional - original request defining output paths outside a turn',
|
|
1072
|
+
paths: 'optional - absolute output paths to recheck, limited by the active turn'
|
|
441
1073
|
)
|
|
442
1074
|
|
|
443
1075
|
# True when write-then-readback evidence satisfies the original request.
|