pwn 0.5.670 → 0.5.673

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.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/README.md +9 -7
  4. data/documentation/Agent-Tool-Registry.md +17 -5
  5. data/documentation/Configuration.md +15 -8
  6. data/documentation/Diagrams.md +2 -2
  7. data/documentation/Home.md +2 -2
  8. data/documentation/How-PWN-Works.md +11 -9
  9. data/documentation/Installation.md +3 -1
  10. data/documentation/Mistakes.md +3 -0
  11. data/documentation/Persistence.md +4 -1
  12. data/documentation/Reinforcement-Learning.md +90 -73
  13. data/documentation/Skills-Memory-Learning.md +28 -4
  14. data/documentation/What-is-PWN.md +8 -7
  15. data/documentation/Why-PWN.md +3 -2
  16. data/documentation/diagrams/agent-tool-registry.svg +188 -160
  17. data/documentation/diagrams/dot/agent-tool-registry.dot +7 -4
  18. data/documentation/diagrams/dot/memory-skills-detailed.dot +12 -5
  19. data/documentation/diagrams/dot/overall-pwn-architecture.dot +4 -3
  20. data/documentation/diagrams/dot/persistence-filesystem.dot +2 -1
  21. data/documentation/diagrams/dot/pwn-ai-feedback-learning-loop.dot +11 -4
  22. data/documentation/diagrams/dot/reinforcement-learning.dot +8 -3
  23. data/documentation/diagrams/memory-skills-detailed.svg +252 -210
  24. data/documentation/diagrams/overall-pwn-architecture.svg +21 -12
  25. data/documentation/diagrams/persistence-filesystem.svg +127 -113
  26. data/documentation/diagrams/pwn-ai-feedback-learning-loop.svg +449 -397
  27. data/documentation/diagrams/reinforcement-learning.svg +276 -239
  28. data/documentation/pwn-ai-Agent.md +31 -19
  29. data/lib/pwn/ai/agent/curriculum.rb +13 -4
  30. data/lib/pwn/ai/agent/dispatch.rb +3 -0
  31. data/lib/pwn/ai/agent/learning.rb +100 -14
  32. data/lib/pwn/ai/agent/loop.rb +114 -26
  33. data/lib/pwn/ai/agent/metrics.rb +52 -4
  34. data/lib/pwn/ai/agent/mistakes.rb +158 -1
  35. data/lib/pwn/ai/agent/policy.rb +935 -0
  36. data/lib/pwn/ai/agent/prompt_builder.rb +26 -5
  37. data/lib/pwn/ai/agent/reflect.rb +11 -3
  38. data/lib/pwn/ai/agent/registry.rb +18 -3
  39. data/lib/pwn/ai/agent/reward.rb +360 -48
  40. data/lib/pwn/ai/agent/task_summarizer.rb +37 -16
  41. data/lib/pwn/ai/agent/tool_guard.rb +157 -0
  42. data/lib/pwn/ai/agent/tools/policy.rb +76 -0
  43. data/lib/pwn/ai/agent/tools/ruby_eval.rb +27 -1
  44. data/lib/pwn/ai/agent/tools/shell.rb +28 -32
  45. data/lib/pwn/ai/agent.rb +2 -0
  46. data/lib/pwn/config.rb +7 -0
  47. data/lib/pwn/version.rb +1 -1
  48. data/spec/integration/reinforced_feedback_loop_spec.rb +35 -9
  49. data/spec/lib/pwn/ai/agent/learning_spec.rb +119 -3
  50. data/spec/lib/pwn/ai/agent/loop_spec.rb +0 -1
  51. data/spec/lib/pwn/ai/agent/metrics_spec.rb +10 -0
  52. data/spec/lib/pwn/ai/agent/mistakes_spec.rb +33 -0
  53. data/spec/lib/pwn/ai/agent/policy_spec.rb +165 -0
  54. data/spec/lib/pwn/ai/agent/reward_spec.rb +75 -0
  55. data/spec/lib/pwn/ai/agent/signal_hygiene_spec.rb +118 -0
  56. data/spec/lib/pwn/ai/agent/tool_guard_spec.rb +61 -0
  57. data/spec/lib/pwn/ai/agent/tools/policy_spec.rb +18 -0
  58. data/spec/support/sandbox.rb +2 -0
  59. data/third_party/pwn_rdoc.jsonl +83 -2
  60. metadata +8 -1
@@ -1,4 +1,4 @@
1
- # Memory · Skills · Learning · Mistakes · Metrics - Introspection
1
+ # Memory · Skills · Learning · Mistakes · Metrics · Policy - Introspection
2
2
 
3
3
  The **inward-facing** half of the pwn-ai feedback loop: how the agent measures
4
4
  its own performance, turns wins into permanent capability, and - critically -
@@ -6,12 +6,13 @@ its own performance, turns wins into permanent capability, and - critically -
6
6
 
7
7
  ![Memory / Skills detail](diagrams/memory-skills-detailed.svg)
8
8
 
9
- ## The five stores
9
+ ## The six stores
10
10
 
11
11
  | Store | File | Write tool | Read tool | Injected as |
12
12
  |---|---|---|---|---|
13
13
  | **Memory** | `memory.json` (+ `memory.idx`) | `memory_remember` | `memory_recall` · `PWN::MemoryIndex.recall_semantic` | `MEMORY` block - durable facts / prefs / lessons / env. **Relevance-ranked** for the current request via a local embedding index when `ai.ollama.embed_model` is available; falls back to newest-first otherwise. |
14
14
  | **Skills** | `skills/<name>/SKILL.md` | `skill_create` · `skill_migrate_legacy` · `learning_distill_skill` | `skill_list` · `skill_view` | `SKILLS` list - reusable procedures + `references:` (CWE/CVE/ATT&CK/NIST/URL) |
15
+ | **Policy** | `policy.json` + `policy_traj.jsonl` | Loop `begin_episode` / `observe_step` / `finish` | `policy_stats` · `policy_evaluate` · `policy_recommend` | `POLICY` block - live tabular Q / REINFORCE. Advisory rank only. Disable with `ai.agent.policy: false`. |
15
16
  | **Learning** | `learning.jsonl` | `learning_note_outcome` · `learning_reflect` | `learning_outcomes` · `learning_stats` · `Learning.exemplars_for` | `LEARNING` block - recent outcomes + success_rate. Prior *successful* traces are also spliced in as **few-shot exemplars** for local models. |
16
17
  | **Mistakes** | `mistakes.json` | `mistakes_record` · `mistakes_resolve` · *auto on failure* | `mistakes_list` | `KNOWN MISTAKES` + `KNOWN FIXES` blocks - do-NOT-repeat + do-THIS-instead |
17
18
  | **Metrics** | `metrics.json` | *automatic* (every Dispatch) | `metrics_summary` | `TOOL EFFECTIVENESS` block - steer tool choice. **Segmented per engine** (`engine=...`) so a local model's telemetry never blends with a frontier model's. |
@@ -33,6 +34,7 @@ wiping durable facts, preferences, and lessons. Clearing is still available via
33
34
  abstract lessons for a small model.
34
35
  3. (local model) Loop.plan_first forces a numbered tool plan BEFORE dispatch.
35
36
  4. Dispatch runs a tool → Metrics.record(tool, ok?, ms, engine:)
37
+ ↳ Policy.observe_step → hygiene reward (semantic_ok) into the live MDP episode
36
38
  ↳ tool FAILED? → Mistakes.record(tool, error) (count++, cross-session)
37
39
  ↳ same sig ≥3×? → guard_repeated_failure + inline correction_hint
38
40
  ↳ (local) ≥ ESCALATE_AFTER_FAILS → Swarm.ask(escalation_persona) → 3-line frontier hint
@@ -40,6 +42,7 @@ wiping durable facts, preferences, and lessons. Clearing is still available via
40
42
  ↳ extro_verify → :refuted → Mistakes.record(tool:'assumption', ...) # proactive
41
43
  ↳ extro_verify → :confirmed → observe(:intel, ttl:30d)
42
44
  6. Final answer produced → Learning.auto_introspect(session_id)
45
+ ↳ Reward.judge (cheap LLM ORM) → Policy.finish (terminal reward; Q + REINFORCE update)
43
46
  ↳ (local) fact_check_local_final → auto extro_verify every CVE/version claim in the answer
44
47
  ↳ if auto_extrospect enabled → Extrospection.auto_extrospect # AUTO_SECTIONS only
45
48
  7. Reflect.on(engine: reflect_engine)→ Memory.remember(lesson_xxxx, ...) # teacher-student: a
@@ -51,7 +54,7 @@ wiping durable facts, preferences, and lessons. Clearing is still available via
51
54
  model via Curriculum.train_and_gate (resolved margin + mean judge + smoke) - the ONLY step that
52
55
  changes weights, not just the scaffold. Without a trainer this stays export-ready.
53
56
  11. Next launch: PromptBuilder injects the budgeted blocks → the model already knows:
54
- MEMORY · SKILLS · LEARNING · KNOWN MISTAKES/FIXES · TOOL EFFECTIVENESS · EXTROSPECTION
57
+ MEMORY · SKILLS · LEARNING · KNOWN MISTAKES/FIXES · TOOL EFFECTIVENESS · POLICY · EXTROSPECTION · RECENT TURNS
55
58
  ```
56
59
 
57
60
  `extro_correlate` is the **join** - it tells the agent whether a failure was
@@ -190,6 +193,7 @@ when deciding which side of the loop to exercise.
190
193
  | "Reflect on this session / extract lessons" | `learning_reflect` / `sessions_current` |
191
194
  | "Don't do that again / that was wrong / resolve..." | `mistakes_record` / `mistakes_resolve` / `mistakes_list` |
192
195
  | "Which tools are unhealthy / avg duration" | `metrics_summary` |
196
+ | "What did the live policy learn / suggest a tool" | `policy_stats` / `policy_evaluate` / `policy_recommend` |
193
197
  | "What did we run in session X / active session" | `sessions_view` / `sessions_current` |
194
198
  | "Disable reflection while we fuzz" | `learning_auto_introspect_toggle` |
195
199
 
@@ -203,8 +207,28 @@ and which procedures to promote permanently.
203
207
  [Sessions](Sessions.md) · [Persistence](Persistence.md)
204
208
 
205
209
 
210
+ ## Live Policy (advisory Q / REINFORCE)
211
+
212
+ When `ai.agent.policy` is on (the default), every Loop turn is one learning
213
+ episode:
214
+
215
+ 1. `begin_episode` opens before the first tool rank.
216
+ 2. `observe_step` writes a hygiene reward after each tool (`semantic_ok`).
217
+ 3. `finish` (from `Learning.auto_introspect`, or Loop if introspect is skipped)
218
+ adds the `Reward.judge` terminal score and updates Q and REINFORCE.
219
+
220
+ State is a short key: request kind, a hash of the active English task, last
221
+ action, fail count bin, and engine. Files:
222
+
223
+ - `~/.pwn/policy.json` - Q table, REINFORCE logits, visit counts, recent returns
224
+ - `~/.pwn/policy_traj.jsonl` - episode log
225
+
226
+ Tools `policy_stats`, `policy_evaluate`, and `policy_recommend` are read-only. Reset is Ruby-only (`PWN::AI::Agent::Policy.reset`) so a tool call cannot wipe the table. `Registry.rank` may add a small Q-advantage after a pair has been visited at least twice. Planning still owns the task list.
227
+
228
+ Turn it off with `ai.agent.policy: false` in `~/.pwn/pwn.yaml`.
229
+
206
230
  ## Task briefs vs learning
207
231
 
208
- `TaskSummarizer` is **UX**, not a persistence layer. Plan/`about_to` lines are ephemeral TUI briefs (deduped by `last_brief_fp`). Durable learning still flows through Learning · Mistakes · Reward · sessions only. See [pwn-ai Agent § Task summaries](pwn-ai-Agent.md#task-summaries-long-autonomous-turns).
232
+ `TaskSummarizer` is **UX**, not a persistence layer. Plan/`about_to` lines are ephemeral TUI briefs (deduped by `last_brief_fp`). Durable learning still flows through Learning · Mistakes · Reward · Policy · sessions. See [pwn-ai Agent § Task summaries](pwn-ai-Agent.md#task-summaries-long-autonomous-turns).
209
233
 
210
234
  [← Home](Home.md)
@@ -20,7 +20,7 @@ with a **tool-calling AI agent** on top that can run the same methods.
20
20
  | `PWN::FFI::*` | **8** | Native DSP/RF backends: Volk · Liquid · FFTW · RTLSdr · HackRF · AdalmPluto · SoapySDR · Stdio |
21
21
  | `PWN::AI::*` | **6** engines | OpenAI, Anthropic, Grok (OAuth device-flow), Gemini, Ollama, Open WebUI |
22
22
  | `bin/pwn_*` | **53** | Headless CLI drivers for CI/CD |
23
- | Agent toolsets | **12** · **82 tools** | terminal · pwn · memory · skills · sessions · learning · metrics · extrospection · cron · swarm · reward · curriculum |
23
+ | Agent toolsets | **13** · **85 tools** | terminal · pwn · memory · skills · sessions · learning · metrics · policy · extrospection · cron · swarm · reward · curriculum |
24
24
 
25
25
  ## Three ways to use it
26
26
 
@@ -36,12 +36,13 @@ with a **tool-calling AI agent** on top that can run the same methods.
36
36
  - **Everything is Ruby, everything is a method.** No YAML DSLs, no black-box
37
37
  plugins. If you can call it in the REPL, the AI agent can call it, a driver
38
38
  can call it, and a cron job can call it.
39
- - **Closed feedback loop.** Metrics + Learning + **Reward** (ORM/PRM judge,
40
- sentinel) + **Curriculum** (mistake-driven self-play, HER, regression-gated
41
- LoRA) on the introspection side; Snapshot + Drift + Intel + Verify on the
42
- extrospection side; joined by `extro_correlate`, which tells the agent
43
- whether a failure was *its* fault or *the world* changed - and writes the
44
- lesson back into the next prompt.
39
+ - **Closed feedback loop.** Metrics + Learning + **Reward** (outcome and process
40
+ judges, sentinel) + **Curriculum** (mistake-driven self-play, hindsight relabel,
41
+ export-ready LoRA gate) + **Policy** (live tabular Q-learning and REINFORCE on
42
+ each turn, advisory rank only) on the introspection side; Snapshot + Drift +
43
+ Intel + Verify on the extrospection side; joined by `extro_correlate`, which
44
+ tells the agent whether a failure was *its* fault or *the world* changed - and
45
+ writes the lesson back into the next prompt.
45
46
  - **Native multi-agent.** `PWN::AI::Agent::Swarm` runs personas (each a full
46
47
  tool-calling agent, optionally on a *different* LLM engine) that debate,
47
48
  broadcast, and share an append-only bus - no IRC daemon, no external service.
@@ -39,8 +39,9 @@ critical when the caller is an autonomous agent.
39
39
 
40
40
  A pentest framework that does not learn repeats the same dead-end scans
41
41
  forever. PWN records **per-tool success rate**, **per-task outcome**, **host
42
- drift**, and **external CVE intel**, then correlates them so tomorrow's run
43
- can start where today's left off. See
42
+ drift**, **external CVE intel**, and a **live Policy** table (Q / REINFORCE
43
+ advice next to planning), then correlates them so tomorrow's run can start
44
+ where today's left off. See
44
45
  [Skills, Memory & Learning](Skills-Memory-Learning.md),
45
46
  [Reinforcement Learning](Reinforcement-Learning.md), and
46
47
  [Extrospection](Extrospection.md).