Module: PWN::AI::Agent::Reward

Defined in:
lib/pwn/ai/agent/reward.rb

Overview

PWN::AI::Agent::Reward is the OUTCOME reward model for the pwn-ai reinforcement-learning loop. It replaces the regex-proxy reward that previously drove Learning.infer_success / Loop.record_metrics with four calibrated signals:

R1  .judge      — LLM Outcome Reward Model (ORM). Scores the FINAL
                answer against the user request → {score:0..1,
                verdict: :solved|:partial|:wrong|:refused,
                rationale:}. Scalar, not boolean.
R2  .prm        — Process Reward Model. Per-tool-call "did this
                step advance toward the goal?" → step_reward
                tagged onto every Sessions entry so credit is
                assignable INSIDE a trajectory, not just at its
                boundary. First PRM applied to security tooling.
R3  .sentinel   — Reward-hacking detector. Tracks proxy vs judge
                vs (1 - user_correction_rate); when they diverge
                by > SENTINEL_GAP the reward signal itself is
                fingerprinted as a Mistake so the operator sees
                "your success_rate is a lie" in KNOWN MISTAKES.
R4  .semantic_ok — Structured tool-result classifier. Knows that
                `grep exit 1` == "no match", not "failure";
                kills the phantom-mistake class (31f1871b8a15)
                that made the loop's #1 negative signal a false
                positive it created itself.

Reward also owns the PREFERENCE-PAIR ledger (~/.pwn/preferences.jsonl) that turns pwn's naturally-generated (rejected, chosen) pairs — from user corrections, mistakes_resolve, and Curriculum.counterfactual A/B branches — into a DPO export (W1). This is the ONLY path from in-context learning to weight-level policy improvement.

E3 .verify_as_reward — grounds any final containing a checkable claim (CVE / version / cited URL) via Extrospection.verify and maps the browser verdict onto the reward scalar. Hallucination becomes a measurable −reward, not just a warning.

Everything degrades gracefully: when module_reflection is off (no LLM judge available) .judge falls back to a calibrated heuristic over .semantic_ok + .verify_as_reward + Mistakes correction rate, which is STILL strictly better than the old regex.

Constant Summary collapse

PREFERENCES_FILE =
File.join(Dir.home, '.pwn', 'preferences.jsonl')
SENTINEL_FILE =
File.join(Dir.home, '.pwn', 'reward_sentinel.json')
DPO_DIR =
File.join(Dir.home, '.pwn', 'finetune')
SENTINEL_GAP =
0.15
SENTINEL_WINDOW =
40
VERDICTS =
{
  solved: 1.0, confirmed: 1.0, partial: 0.5,
  unknown: 0.5, wrong: 0.0, refused: 0.0, refuted: 0.0
}.freeze
BENIGN_EXIT =

Commands whose non-zero exit is INFORMATIONAL, not a failure. The regex-proxy treating these as failures was the single largest source of noise in Mistakes/Metrics (grep exit 1 = "no match").

{
  /\b(?:e|f|z|rip|p)?grep\b/ => [1],
  /\bdiff\b/ => [1],
  /\bcmp\b/ => [1],
  /\btest\b|\[\s/ => [1],
  /\bls\b/ => [1, 2],
  /\bfind\b/ => [1],
  /\bwhich\b|\bcommand -v\b/ => [1],
  /\bpidof\b|\bpgrep\b|\bpkill\b/ => [1],
  /\bxargs\b/ => [123],
  /\btimeout\b/ => [124],
  /\bcurl\b/ => [22],
  /\brubocop\b/ => [1]
}.freeze
JUDGE_SYSTEM =
<<~SYS
  You are the pwn-ai Outcome Reward Model. Given a USER REQUEST, the
  agent's FINAL ANSWER, and a compressed TOOL TRACE, emit ONE line of
  strict JSON:
    {"score": <0.0-1.0>, "verdict": "solved|partial|wrong|refused",
     "rationale": "<≤140 chars>", "key_step": <int|-1>}
  score=1.0 only when the final DEMONSTRABLY satisfies the request
  (evidence in trace). score=0.5 for correct-direction-but-incomplete.
  score=0.0 for hallucinated / off-goal / refused. key_step is the
  1-indexed trace line most responsible for the outcome (credit
  assignment), or -1 if none. Output JSON ONLY.
SYS
PRM_SYSTEM =
<<~SYS
  You are the pwn-ai Process Reward Model. For EACH numbered tool
  step, output one integer per line: 1 (advanced toward the goal),
  0 (neutral / exploratory), -1 (regressed / wasted). Output ONLY
  the integers, one per line, same count as steps. No prose.
SYS
DPO_SOURCE_CAP =

Max share any single preference source may occupy in a DPO export. Without this cap, mistakes_resolve monoculture (often >80%) teaches the LoRA "emit fix prose" instead of trajectory preference (P5 enforce).

0.40

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. support@0dayinc.com



861
862
863
# File 'lib/pwn/ai/agent/reward.rb', line 861

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <support@0dayinc.com>\n"
end

.clear_proxy_distrustObject



281
282
283
284
285
286
287
288
289
290
# File 'lib/pwn/ai/agent/reward.rb', line 281

public_class_method def self.clear_proxy_distrust
  s = load_sentinel
  return if s[:proxy_distrust].to_f <= 0.0

  s[:proxy_distrust] = 0.0
  s[:distrust_cleared_at] = Time.now.utc.iso8601
  atomic_write(path: SENTINEL_FILE, body: JSON.generate(s))
rescue StandardError
  nil
end

.export_dpo(opts = {}) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/pwn/ai/agent/reward.rb', line 461

public_class_method def self.export_dpo(opts = {})
  fmt = (opts[:format] || :dpo).to_sym
  FileUtils.mkdir_p(DPO_DIR)
  out = opts[:out] || File.join(DPO_DIR, "pwn-dpo-#{Time.now.utc.strftime('%Y%m%d')}.jsonl")
  rows = preferences(limit: 100_000)
  # P5 — downsample so no single source exceeds DPO_SOURCE_CAP of the export.
  # opt-out with balance: false (raw dump for diagnostics).
  balance = opts.key?(:balance) ? opts[:balance] : true
  selected = balance ? balance_preference_rows(rows: rows, cap: (opts[:source_cap] || DPO_SOURCE_CAP).to_f) : rows
  dropped = rows.length - selected.length
  File.open(out, 'w') do |f|
    selected.each do |r|
      line = case fmt
             when :kto
               [{ prompt: r[:prompt], completion: r[:chosen], label: true },
                { prompt: r[:prompt], completion: r[:rejected], label: false }]
             else
               # Keep source for auditability / preference_balance post-export.
               { prompt: r[:prompt], chosen: r[:chosen], rejected: r[:rejected], source: r[:source] }
             end
      (line.is_a?(Array) ? line : [line]).each { |l| f.puts(JSON.generate(l)) }
    end
  end
  by_src = selected.group_by { |r| r[:source].to_s }.transform_values(&:length)
  {
    path: out, format: fmt, pairs: selected.length, bytes: File.size(out),
    balanced: balance, dropped: dropped, by_source: by_src,
    source_cap: balance ? (opts[:source_cap] || DPO_SOURCE_CAP).to_f : nil
  }
end

.helpObject

Display Usage for this Module



867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/pwn/ai/agent/reward.rb', line 867

public_class_method def self.help
  puts <<~USAGE
    USAGE:
      # Tier 1 — reward signal
      PWN::AI::Agent::Reward.judge(request: req, final: text, session_id: sid)     # R1 ORM → {score:, verdict:, rationale:}
      PWN::AI::Agent::Reward.prm(request: req, session_id: sid)                    # R2 PRM → per-step credit
      PWN::AI::Agent::Reward.sentinel                                              # R3 reward-hacking detector
      PWN::AI::Agent::Reward.reset_sentinel                                        # wipe corrupt window + distrust
      PWN::AI::Agent::Reward.semantic_ok(name: 'shell', raw: json, args: args)     # R4 kills phantom exit≠0 mistakes

      # Tier 5 — preference pairs → DPO
      PWN::AI::Agent::Reward.record_preference(prompt: p, rejected: r, chosen: c, source: :user_correction)
      PWN::AI::Agent::Reward.preferences(limit: 100)
      PWN::AI::Agent::Reward.export_dpo(format: :dpo)                              # W1 → ~/.pwn/finetune/pwn-dpo-*.jsonl (≤40%/source)
      PWN::AI::Agent::Reward.export_dpo(format: :dpo, balance: false)              # raw dump (diagnostics)

      # Tier 6 — grounded reward
      PWN::AI::Agent::Reward.verify_as_reward(final: text)                         # E3 browser-verified reward

      Config (PWN::Env[:ai][:agent]):
        :verify_as_reward   - Boolean/nil, ground finals via extro_verify (nil=auto)
        :reward_llm         - Boolean/nil, force ORM/PRM LLM teacher (nil=on for remote engines)

      #{self}.authors
  USAGE
end

.judge(opts = {}) ⇒ Object

Supported Method Parameters

v = PWN::AI::Agent::Reward.judge( request: 'required - original user request', final: 'required - assistant final answer', session_id: 'optional - PWN::Sessions id (adds tool trace)', trace: 'optional - Array of tool-result strings (overrides session_id)', commit: 'optional - write score into learning.jsonl / sentinel (default true)' )



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pwn/ai/agent/reward.rb', line 114

public_class_method def self.judge(opts = {})
  request = opts[:request].to_s
  final   = opts[:final].to_s
  trace   = Array(opts[:trace])
  trace   = load_trace(session_id: opts[:session_id]) if trace.empty? && opts[:session_id]
  commit  = opts.key?(:commit) ? opts[:commit] : true

  v = llm_judge(request: request, final: final, trace: trace)
  v ||= heuristic_judge(request: request, final: final, trace: trace)

  ground = verify_as_reward(final: final)
  unless ground.nil?
    # Ground-truth override: a browser-refuted claim caps score at
    # 0.2 regardless of how confident the judge was; a confirmed
    # claim floors it at 0.6. E3.
    v[:score] = [v[:score], 0.2].min if ground[:verdict] == :refuted
    v[:score] = [v[:score], 0.6].max if ground[:verdict] == :confirmed
    v[:grounded] = ground
  end

  v[:success] = v[:score] >= 0.6
  record_sentinel(proxy: opts[:proxy_ok], judge: v[:score]) if commit
  v
rescue StandardError => e
  { score: 0.5, verdict: :unknown, rationale: "judge error: #{e.class}", success: !final.strip.empty?, error: e.message }
end

.preferences(opts = {}) ⇒ Object

Supported Method Parameters

rows = PWN::AI::Agent::Reward.preferences(limit: 500, source: nil)



435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/pwn/ai/agent/reward.rb', line 435

public_class_method def self.preferences(opts = {})
  limit  = opts[:limit] || 500
  source = opts[:source].to_s
  return [] unless File.exist?(PREFERENCES_FILE)

  rows = File.readlines(PREFERENCES_FILE).map do |l|
    JSON.parse(l, symbolize_names: true)
  rescue StandardError
    nil
  end
  rows.compact!
  rows.select! { |r| r[:source] == source } unless source.empty?
  rows.reverse.first(limit)
end

.prm(opts = {}) ⇒ Object

Supported Method Parameters

steps = PWN::AI::Agent::Reward.prm( request: 'required - user goal', session_id: 'optional - session to score in place', trace: 'optional - Array of args:, result: or Strings' )

Returns [step:, reward: -1|0|1, ...] and, when session_id is given, rewrites each tool line in the transcript with a [step_reward=N] prefix so exemplars_for / distill_skill can keep only reward>0 steps (C4 minimal sufficient trace).



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/pwn/ai/agent/reward.rb', line 157

public_class_method def self.prm(opts = {})
  request = opts[:request].to_s
  trace   = Array(opts[:trace])
  sid     = opts[:session_id]
  trace   = load_trace(session_id: sid) if trace.empty? && sid

  rewards = llm_prm(request: request, trace: trace)
  rewards ||= heuristic_prm(trace: trace)

  out = trace.each_with_index.map do |s, i|
    { idx: i + 1, step: s.to_s[0, 200], reward: rewards[i] || 0 }
  end
  annotate_session(session_id: sid, rewards: rewards) if sid
  out
rescue StandardError
  []
end

.proxy_distrustObject

P4 — scalar 0.0..1.0 haircut applied to Metrics success / Registry β when the proxy is lying. 0.0 = trust proxy fully; 1.0 = ignore proxy rates.



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/pwn/ai/agent/reward.rb', line 246

public_class_method def self.proxy_distrust
  s = load_sentinel
  d = s[:proxy_distrust].to_f
  # auto-expire after 7d without refresh so a one-off gap doesn't stick
  if s[:distrust_at]
    age = Time.now.utc - Time.parse(s[:distrust_at].to_s)
    return 0.0 if age > 7 * 86_400
  end
  d.clamp(0.0, 1.0)
rescue StandardError
  0.0
end

.record_preference(opts = {}) ⇒ Object

Supported Method Parameters

PWN::AI::Agent::Reward.record_preference( prompt: 'required - the context / user request', rejected: 'required - the losing completion / action', chosen: 'required - the winning completion / action', source: 'optional - :user_correction | :mistakes_resolve | :counterfactual | :critic' )



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/pwn/ai/agent/reward.rb', line 411

public_class_method def self.record_preference(opts = {})
  prompt   = opts[:prompt].to_s
  rejected = opts[:rejected].to_s
  chosen   = opts[:chosen].to_s
  return nil if prompt.strip.empty? || chosen.strip.empty? || rejected.strip.empty?
  return nil if chosen.strip == rejected.strip

  entry = {
    id: Digest::SHA256.hexdigest("#{prompt}|#{rejected}|#{chosen}")[0, 12],
    prompt: prompt[0, 4_000],
    rejected: rejected[0, 4_000],
    chosen: chosen[0, 4_000],
    source: (opts[:source] || :unknown).to_s,
    engine: (PWN::Env.dig(:ai, :active) if defined?(PWN::Env)).to_s,
    timestamp: Time.now.utc.iso8601
  }
  FileUtils.mkdir_p(File.dirname(PREFERENCES_FILE))
  File.open(PREFERENCES_FILE, 'a') { |f| f.puts(JSON.generate(entry)) }
  entry
end

.recoverable_shape(opts = {}) ⇒ Object

2.2 — coarse recoverable shape beside the fingerprint. Paths are normalised away for counting; shape stays for repair routing (enoent → install/check path; exit127 → missing binary; …).



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/pwn/ai/agent/reward.rb', line 349

public_class_method def self.recoverable_shape(opts = {})
  err = "#{opts[:err]} #{opts[:stderr]}".downcase
  ec  = opts[:exit_code]
  return :exit127 if ec == 127 || err.include?('command not found')
  return :exit126 if ec == 126
  return :enoent if err.match?(/no such file|enoent|cannot access|not a directory/)
  return :eacces if err.match?(/permission denied|eacces|operation not permitted/)
  return :auth_required if err.match?(/auth|unauthorized|401|403|forbidden|login required|api.?key/)
  return :timeout if ec == 124 || err.include?('timed out') || err.include?('timeout')
  return :network if err.match?(/connection refused|name or service not known|could not resolve|network is unreachable/)
  return :syntax if err.match?(/syntax error|parse error|unexpected token|json::parser/)
  return :nonzero_exit if ec && ec != 0
  return :handler_error if err.strip.length.positive?

  :unknown
end

.resetObject



492
493
494
495
496
# File 'lib/pwn/ai/agent/reward.rb', line 492

public_class_method def self.reset
  FileUtils.rm_f(PREFERENCES_FILE)
  FileUtils.rm_f(SENTINEL_FILE)
  { cleared: true }
end

.reset_sentinelObject

One-shot: wipe sentinel window + distrust after deploying the ring-buffer arithmetic (or any time the live file is known-corrupt). Does NOT touch preferences / DPO exports (unlike .reset).



295
296
297
298
# File 'lib/pwn/ai/agent/reward.rb', line 295

public_class_method def self.reset_sentinel
  FileUtils.rm_f(SENTINEL_FILE)
  { cleared: true, path: SENTINEL_FILE }
end

.semantic_ok(opts = {}) ⇒ Object

Supported Method Parameters

h = PWN::AI::Agent::Reward.semantic_ok( name: 'required - tool name', raw: 'required - JSON string returned by Dispatch.call', args: 'optional - the tool call arguments (used for BENIGN_EXIT)' )

Returns { ok:, semantic_ok:, exit:, err:, benign: }. :ok is the old proxy (handler didn't raise); :semantic_ok additionally knows that grep/diff/find exit≠0 with empty stderr is not a failure. Loop.run records Metrics on :ok but only records Mistakes on !semantic_ok.



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/pwn/ai/agent/reward.rb', line 316

public_class_method def self.semantic_ok(opts = {})
  name = opts[:name].to_s
  raw  = opts[:raw].to_s
  ok   = raw.include?('"success":true')
  err  = raw[/"error":"([^"]{1,300})"/, 1]
  exit_code = raw[/"exit":(\d+)/, 1]&.to_i
  stderr    = raw[/"stderr":"([^"]{0,400})"/, 1].to_s

  benign = false
  shape  = nil
  if name == 'shell' && ok && exit_code && exit_code != 0
    cmd = extract_cmd(args: opts[:args])
    # 2.1 — ONLY BENIGN_EXIT regex × allowed codes. The old global
    # `stderr.empty? && exit==1 ⇒ benign` laundered real failures
    # (pipelines without pipefail, bare false, etc.) into "success".
    # For pipelines, match the LAST stage (post-pipe) first, then any.
    stages = cmd.split('|').map(&:strip)
    last   = stages.last.to_s
    benign = BENIGN_EXIT.any? { |rx, codes| last.match?(rx) && codes.include?(exit_code) }
    benign ||= stages.length > 1 && BENIGN_EXIT.any? { |rx, codes| stages.any? { |s| s.match?(rx) } && codes.include?(exit_code) && stderr.strip.empty? }
    shape = recoverable_shape(exit_code: exit_code, stderr: stderr, err: err)
  elsif !ok
    shape = recoverable_shape(exit_code: exit_code, stderr: stderr, err: err || raw[0, 200])
  end

  semantic = ok && (exit_code.nil? || exit_code.zero? || benign)
  err ||= raw[/"stderr":"([^"]{4,300})"/, 1] unless semantic
  { ok: ok, semantic_ok: semantic, exit: exit_code, err: err, benign: benign, shape: shape }
end

.sentinelObject

Supported Method Parameters

r = PWN::AI::Agent::Reward.sentinel



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/pwn/ai/agent/reward.rb', line 182

public_class_method def self.sentinel
  s = normalize_sentinel(raw: load_sentinel)
  window = s[:window]
  n = window.length
  return { samples: n, status: :insufficient } if n < SENTINEL_WINDOW

  means = window_means(window: window)
  proxy = means[:proxy]
  judge = means[:judge]
  # Refuse to act on corrupt arithmetic — proxy must be a rate in [0,1].
  if proxy.nil? || proxy < 0.0 || proxy > 1.0
    return {
      samples: n,
      status: :corrupt_proxy,
      proxy: proxy,
      judge: judge&.round(3),
      reward_hacked: false,
      proxy_distrust: proxy_distrust
    }
  end

  human = 1.0 - user_correction_rate
  gap_pj = (proxy - judge).abs
  gap_ph = (proxy - human).abs
  hacked = gap_pj > SENTINEL_GAP || gap_ph > SENTINEL_GAP
  if hacked
    # 1.1 — freeze auto-Mistakes.record on tool:reward_signal after the
    # first open sig per gap-bucket. Endless ×13 fingerprints were
    # the loudest scar in every prompt and taught nothing. Open a
    # calibration path instead; park the sig as needs_code_change.
    bucket = "gap_pj=#{gap_pj.round(2)}|gap_ph=#{gap_ph.round(2)}"
    open_sig = defined?(Mistakes) ? Mistakes.for_tool(tool: 'reward_signal', unresolved_only: true) : []
    if open_sig.empty? && defined?(Mistakes)
      m = Mistakes.record(
        tool: 'reward_signal',
        error: "proxy success_rate #{proxy.round(2)} diverges from judge #{judge.round(2)} / human #{human.round(2)} by >#{SENTINEL_GAP}",
        source: :model,
        needs_code_change: true,
        meta: { bucket: bucket, proxy: proxy, judge: judge, human: human }
      )
      Mistakes.park(signature: m[:signature], reason: 'reward_signal needs calibration, not practice') if m && Mistakes.respond_to?(:park)
    end
    Curriculum.calibrate(predicted: proxy, actual: judge, engine: :reward_sentinel) if defined?(Curriculum) && Curriculum.respond_to?(:calibrate)
    # P4 — make sentinel ACTIONABLE: persist a distrust factor so
    # Metrics.to_context / Registry.rank haircut proxy success instead of
    # just opening another Mistakes row the model learns to ignore.
    set_proxy_distrust(gap: [gap_pj, gap_ph].max, proxy: proxy, judge: judge)
  else
    clear_proxy_distrust
  end
  {
    samples: n,
    proxy: proxy.round(3),
    judge: judge.round(3),
    human: human.round(3),
    gap_proxy_judge: gap_pj.round(3),
    gap_proxy_human: gap_ph.round(3),
    reward_hacked: hacked,
    proxy_distrust: proxy_distrust
  }
end

.set_proxy_distrust(opts = {}) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/pwn/ai/agent/reward.rb', line 259

public_class_method def self.set_proxy_distrust(opts = {})
  s = normalize_sentinel(raw: load_sentinel)
  gap = opts[:gap].to_f
  proxy = opts[:proxy]
  # Guard: never set distrust from a nonsensical proxy (pre-ring-buffer
  # decay×to_i bug produced means ≫ 1.0 and hard-pegged distrust at 1.0).
  unless proxy.nil?
    pf = proxy.to_f
    return s[:proxy_distrust].to_f if pf < 0.0 || pf > 1.0
  end
  # map gap 0.15→0.4, 0.30→0.8, ≥0.40→1.0
  factor = ((((gap - SENTINEL_GAP) / SENTINEL_GAP) * 0.4) + 0.4).clamp(0.3, 1.0)
  s[:proxy_distrust] = factor
  s[:distrust_at] = Time.now.utc.iso8601
  s[:distrust_meta] = { proxy: opts[:proxy], judge: opts[:judge], gap: gap }
  FileUtils.mkdir_p(File.dirname(SENTINEL_FILE))
  atomic_write(path: SENTINEL_FILE, body: JSON.generate(s))
  factor
rescue StandardError
  nil
end

.verify_as_reward(opts = {}) ⇒ Object

Supported Method Parameters

g = PWN::AI::Agent::Reward.verify_as_reward(final: text)



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/pwn/ai/agent/reward.rb', line 373

public_class_method def self.verify_as_reward(opts = {})
  return nil unless defined?(Extrospection) && Extrospection.respond_to?(:verify)

  final = opts[:final].to_s
  claim = final[Learning::CLAIM_RX] if defined?(Learning)
  return nil if claim.to_s.empty?

  # 1.5 — sampled E3: always when flag true; never when false;
  # nil/auto → always on frontier, ~10% on local when CLAIM_RX hits.
  flag = agent_flag(key: :verify_as_reward, default: nil)
  eng  = (PWN::Env.dig(:ai, :active) if defined?(PWN::Env)).to_s.downcase
  local = eng == 'ollama'
  run = case flag
        when true then true
        when false then false
        else
          local ? (Digest::SHA256.hexdigest(claim.to_s)[0, 2].to_i(16) % 10).zero? : true
        end
  return nil unless run

  r = Extrospection.verify(claim: claim, commit: true)
  { claim: claim, verdict: r[:verdict], confidence: r[:confidence], reward: VERDICTS[r[:verdict]] || 0.5 }
rescue StandardError
  nil
end