active-protocol-discovery 0.1.0__tar.gz
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.
- active_protocol_discovery-0.1.0/PKG-INFO +231 -0
- active_protocol_discovery-0.1.0/README.md +200 -0
- active_protocol_discovery-0.1.0/active_protocol_discovery.egg-info/PKG-INFO +231 -0
- active_protocol_discovery-0.1.0/active_protocol_discovery.egg-info/SOURCES.txt +23 -0
- active_protocol_discovery-0.1.0/active_protocol_discovery.egg-info/dependency_links.txt +1 -0
- active_protocol_discovery-0.1.0/active_protocol_discovery.egg-info/entry_points.txt +2 -0
- active_protocol_discovery-0.1.0/active_protocol_discovery.egg-info/requires.txt +9 -0
- active_protocol_discovery-0.1.0/active_protocol_discovery.egg-info/top_level.txt +2 -0
- active_protocol_discovery-0.1.0/apd/__init__.py +21 -0
- active_protocol_discovery-0.1.0/apd/apd.py +136 -0
- active_protocol_discovery-0.1.0/apd/models.py +137 -0
- active_protocol_discovery-0.1.0/apd/policy.py +128 -0
- active_protocol_discovery-0.1.0/apd/sprt.py +87 -0
- active_protocol_discovery-0.1.0/apd/utils.py +58 -0
- active_protocol_discovery-0.1.0/experiments/__init__.py +1 -0
- active_protocol_discovery-0.1.0/experiments/adversarial_agent_demo.py +109 -0
- active_protocol_discovery-0.1.0/experiments/benchmark.py +96 -0
- active_protocol_discovery-0.1.0/experiments/gaussian_demo.py +207 -0
- active_protocol_discovery-0.1.0/pyproject.toml +50 -0
- active_protocol_discovery-0.1.0/setup.cfg +7 -0
- active_protocol_discovery-0.1.0/tests/test_apd.py +84 -0
- active_protocol_discovery-0.1.0/tests/test_models.py +81 -0
- active_protocol_discovery-0.1.0/tests/test_policy.py +58 -0
- active_protocol_discovery-0.1.0/tests/test_sprt.py +63 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: active-protocol-discovery
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Active Protocol Discovery — detect hidden adaptive structure via sequential KL-optimal probing. 45x faster than passive monitoring, mathematically guaranteed error rates.
|
|
5
|
+
Author-email: Mikoshi Ltd <darren@mikoshi.co.uk>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/DarrenEdwards111/active-protocol-discovery
|
|
8
|
+
Project-URL: Repository, https://github.com/DarrenEdwards111/active-protocol-discovery
|
|
9
|
+
Keywords: adversarial-detection,active-testing,sequential-testing,hypothesis-testing,SPRT,KL-divergence,anomaly-detection,bot-detection,AI-safety,multi-agent,adaptive-systems,probe,security,deception-detection
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Classifier: Topic :: Security
|
|
21
|
+
Classifier: Topic :: System :: Networking :: Monitoring
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
Requires-Dist: numpy>=1.21
|
|
25
|
+
Requires-Dist: scipy>=1.7
|
|
26
|
+
Provides-Extra: plot
|
|
27
|
+
Requires-Dist: matplotlib>=3.5; extra == "plot"
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
30
|
+
Requires-Dist: matplotlib>=3.5; extra == "dev"
|
|
31
|
+
|
|
32
|
+
<p align="center">
|
|
33
|
+
<img src="apd-logo.jpg" alt="Mikoshi Active Protocol Discovery" width="600">
|
|
34
|
+
</p>
|
|
35
|
+
|
|
36
|
+
# Active Protocol Discovery (APD)
|
|
37
|
+
|
|
38
|
+
A lightweight framework for detecting hidden adaptive structure in noisy environments via sequential, KL-optimal probing.
|
|
39
|
+
|
|
40
|
+
Most detection systems are **passive** — they watch and wait. APD is **active** — it probes, measures, adapts, and decides. This makes it fundamentally faster at detecting hidden adaptive behaviour.
|
|
41
|
+
|
|
42
|
+
Rather than passively monitoring a system, APD:
|
|
43
|
+
|
|
44
|
+
1. **Injects structured perturbations** into the target environment
|
|
45
|
+
2. **Measures statistical deviation** between expected and observed responses
|
|
46
|
+
3. **Updates posterior belief** via sequential likelihood ratios
|
|
47
|
+
4. **Selects the next probe** to maximise expected evidence gain (KL-optimal)
|
|
48
|
+
5. **Decides** when statistical confidence is reached — no fixed sample size required
|
|
49
|
+
|
|
50
|
+
## Why APD Over Existing Approaches?
|
|
51
|
+
|
|
52
|
+
| Approach | Method | Weakness | APD Advantage |
|
|
53
|
+
|----------|--------|----------|---------------|
|
|
54
|
+
| **Passive monitoring** | Watch logs/traffic for anomalies | Fails against agents that stay quiet until activated | APD *forces* adaptive agents to reveal themselves |
|
|
55
|
+
| **Fixed probing** (honeypots, canaries) | Deploy known traps | Predictable — sophisticated agents learn to avoid them | APD *adapts* its probes based on what it learns |
|
|
56
|
+
| **Threshold detectors** | Alert on metric > threshold | Requires pre-set thresholds, high false positive rate | APD uses *sequential testing* — mathematically controlled error rates |
|
|
57
|
+
| **Batch hypothesis testing** | Collect N samples, then decide | Wastes samples, fixed sample size regardless of signal strength | APD *stops early* when evidence is sufficient — up to 45× faster |
|
|
58
|
+
| **Anomaly detection (ML)** | Train model on "normal", flag outliers | Needs training data, can't detect novel adaptive behaviour | APD is *model-free* — works with any response distribution |
|
|
59
|
+
|
|
60
|
+
### Key Strengths
|
|
61
|
+
|
|
62
|
+
- **45× faster than passive monitoring** — empirically demonstrated (see demo results below)
|
|
63
|
+
- **2× faster than best fixed-probe methods** — KL-optimal selection always finds the most informative probe
|
|
64
|
+
- **Mathematically guaranteed error rates** — false positive and false negative rates are configurable via Wald SPRT, not heuristic
|
|
65
|
+
- **No training data required** — works from first observation, no labelled dataset needed
|
|
66
|
+
- **Probe-agnostic** — plug in any environment, any probe set, any response distribution
|
|
67
|
+
- **Lightweight** — pure NumPy/SciPy, no deep learning frameworks, runs on a Raspberry Pi
|
|
68
|
+
|
|
69
|
+
## Applications
|
|
70
|
+
|
|
71
|
+
### AI Safety & Adversarial Detection
|
|
72
|
+
- **Deceptive AI agents** — detect if an autonomous agent is strategically hiding capabilities or intentions
|
|
73
|
+
- **LLM prompt injection** — inject structured perturbations, monitor output entropy shifts to detect hidden tool-use loops
|
|
74
|
+
- **Sleeper agents** — probe for trigger-activated behaviour in fine-tuned models
|
|
75
|
+
- **Multi-agent deception** — detect if agents in a sandbox are colluding or behaving adaptively
|
|
76
|
+
|
|
77
|
+
### Network Security
|
|
78
|
+
- **Bot detection** — send structured timing probes, observe latency distribution shifts to distinguish bots from humans
|
|
79
|
+
- **Adversarial infrastructure** — detect command-and-control servers that adapt responses based on probe patterns
|
|
80
|
+
- **DDoS source identification** — probe suspect sources with varied request patterns to detect coordinated adaptive behaviour
|
|
81
|
+
|
|
82
|
+
### Distributed Systems
|
|
83
|
+
- **Hidden node discovery** — detect undeclared adaptive nodes in a distributed network
|
|
84
|
+
- **Byzantine fault detection** — identify nodes that respond strategically rather than honestly
|
|
85
|
+
- **Protocol compliance testing** — probe services to detect non-standard adaptive behaviour
|
|
86
|
+
|
|
87
|
+
### Research
|
|
88
|
+
- **Multi-agent systems** — test whether agents develop emergent adaptive strategies
|
|
89
|
+
- **Reinforcement learning** — detect if an RL agent has learned to game its environment
|
|
90
|
+
- **Cognitive science** — sequential optimal experiment design for detecting adaptive structure in human/animal behaviour
|
|
91
|
+
|
|
92
|
+
## Installation
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
pip install active-protocol-discovery
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Or from source:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
git clone https://github.com/DarrenEdwards111/active-protocol-discovery.git
|
|
102
|
+
cd active-protocol-discovery
|
|
103
|
+
pip install -e ".[dev]"
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Or clone and install locally:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
git clone https://github.com/DarrenEdwards111/active-protocol-discovery.git
|
|
110
|
+
cd active-protocol-discovery
|
|
111
|
+
pip install -e ".[dev]"
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Quick Start
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from apd import APDEngine, GaussianWorld, KLOptimalPolicy, WaldSPRT
|
|
118
|
+
|
|
119
|
+
world = GaussianWorld(sigma=1.0, adaptive=True)
|
|
120
|
+
policy = KLOptimalPolicy(probes=[0.2, 0.5, 1.0])
|
|
121
|
+
sprt = WaldSPRT(alpha=0.01, beta=0.01)
|
|
122
|
+
|
|
123
|
+
engine = APDEngine(world, policy, sprt)
|
|
124
|
+
result = engine.run(max_steps=1000)
|
|
125
|
+
|
|
126
|
+
print(f"Decision: {'Adaptive' if result.decision else 'Null'}")
|
|
127
|
+
print(f"Steps: {result.steps}")
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## How It Works
|
|
131
|
+
|
|
132
|
+
APD implements a sequential likelihood ratio framework (Wald SPRT) with probe selection based on KL-separability. At each step the engine:
|
|
133
|
+
|
|
134
|
+
1. **Selects a probe** — the perturbation that maximises expected KL divergence between H0 and H1
|
|
135
|
+
2. **Observes the response** — samples from the true environment
|
|
136
|
+
3. **Updates the log-likelihood ratio** — accumulates evidence for/against adaptive structure
|
|
137
|
+
4. **Decides or continues** — stops when the SPRT threshold is crossed
|
|
138
|
+
|
|
139
|
+
## Demo Results
|
|
140
|
+
|
|
141
|
+
Results from `experiments/gaussian_demo.py` (1000 trials, σ=1.0, α=β=0.01):
|
|
142
|
+
|
|
143
|
+
### H1: Adaptive world (adversary present)
|
|
144
|
+
|
|
145
|
+
| Method | Mean Steps | Detection Rate | Undecided |
|
|
146
|
+
|---------------------------|------------|----------------|-----------|
|
|
147
|
+
| Passive (u=0) | 10000.0 | 0.000 | 1.000 |
|
|
148
|
+
| Weak beacon (u=0.2) | 231.5 | 0.987 | 0.000 |
|
|
149
|
+
| Strong beacon (u=1.0) | 10.4 | 0.996 | 0.000 |
|
|
150
|
+
| APD KL-optimal {0.2, 1.0} | 10.4 | 0.996 | 0.000 |
|
|
151
|
+
| APD KL-optimal {0.1..1.5} | 5.1 | 0.993 | 0.000 |
|
|
152
|
+
|
|
153
|
+
### H0: Null world (false positive check)
|
|
154
|
+
|
|
155
|
+
| Method | Mean Steps | False Positive Rate | Correct Null |
|
|
156
|
+
|---------------------------|------------|---------------------|--------------|
|
|
157
|
+
| Strong beacon (u=1.0) | 10.5 | 0.003 | 0.997 |
|
|
158
|
+
| APD KL-optimal {0.1..1.5} | 5.1 | 0.002 | 0.998 |
|
|
159
|
+
|
|
160
|
+
**Key takeaway:** APD automatically selects the strongest available probe (u=1.5), achieving 2× faster detection than a strong fixed beacon while maintaining false positive rates below α.
|
|
161
|
+
|
|
162
|
+
## Theory
|
|
163
|
+
|
|
164
|
+
### Wald SPRT
|
|
165
|
+
|
|
166
|
+
The Sequential Probability Ratio Test accumulates log-likelihood ratios until crossing a decision boundary:
|
|
167
|
+
|
|
168
|
+
- **Log-likelihood ratio:** Λ(y|u) = log p(y|H₁,u) − log p(y|H₀)
|
|
169
|
+
- **Upper threshold (accept H₁):** A = log((1−β)/α)
|
|
170
|
+
- **Lower threshold (accept H₀):** B = log(β/(1−α))
|
|
171
|
+
|
|
172
|
+
### KL Divergence as Probe Quality
|
|
173
|
+
|
|
174
|
+
For the Gaussian case, the KL divergence between H₁ and H₀ given probe u is:
|
|
175
|
+
|
|
176
|
+
D(u) = μ(u)² / (2σ²)
|
|
177
|
+
|
|
178
|
+
This quantifies how much information a single observation provides about the hypothesis. APD selects the probe maximising D(u), achieving the fastest possible sequential detection.
|
|
179
|
+
|
|
180
|
+
### Expected Sample Complexity
|
|
181
|
+
|
|
182
|
+
Under H₁, Wald's approximation gives:
|
|
183
|
+
|
|
184
|
+
E[N|H₁] ≈ ((1−β)·A + β·B) / D(u)
|
|
185
|
+
|
|
186
|
+
Stronger probes (higher D(u)) yield fewer required samples.
|
|
187
|
+
|
|
188
|
+
## Package Structure
|
|
189
|
+
|
|
190
|
+
```
|
|
191
|
+
apd/
|
|
192
|
+
├── models.py — World models (Gaussian, AdaptiveAgent, Network)
|
|
193
|
+
├── policy.py — Probe selection (KL-optimal, fixed, random, ε-greedy)
|
|
194
|
+
├── sprt.py — Wald Sequential Probability Ratio Test
|
|
195
|
+
├── apd.py — Main APD engine
|
|
196
|
+
└── utils.py — Statistics helpers
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Running Experiments
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
# Gaussian demo (the killer demo)
|
|
203
|
+
python -m experiments.gaussian_demo
|
|
204
|
+
|
|
205
|
+
# Adversarial agent in multi-dimensional space
|
|
206
|
+
python -m experiments.adversarial_agent_demo
|
|
207
|
+
|
|
208
|
+
# Full benchmark sweep (outputs CSV)
|
|
209
|
+
python -m experiments.benchmark > results.csv
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Tests
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
pytest tests/ -v
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Citation
|
|
219
|
+
|
|
220
|
+
```bibtex
|
|
221
|
+
@software{apd2026,
|
|
222
|
+
author = {Mikoshi Ltd},
|
|
223
|
+
title = {Active Protocol Discovery},
|
|
224
|
+
year = {2026},
|
|
225
|
+
url = {https://github.com/DarrenEdwards111/active-protocol-discovery}
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Licence
|
|
230
|
+
|
|
231
|
+
Apache 2.0 — Mikoshi Ltd, 2026
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="apd-logo.jpg" alt="Mikoshi Active Protocol Discovery" width="600">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# Active Protocol Discovery (APD)
|
|
6
|
+
|
|
7
|
+
A lightweight framework for detecting hidden adaptive structure in noisy environments via sequential, KL-optimal probing.
|
|
8
|
+
|
|
9
|
+
Most detection systems are **passive** — they watch and wait. APD is **active** — it probes, measures, adapts, and decides. This makes it fundamentally faster at detecting hidden adaptive behaviour.
|
|
10
|
+
|
|
11
|
+
Rather than passively monitoring a system, APD:
|
|
12
|
+
|
|
13
|
+
1. **Injects structured perturbations** into the target environment
|
|
14
|
+
2. **Measures statistical deviation** between expected and observed responses
|
|
15
|
+
3. **Updates posterior belief** via sequential likelihood ratios
|
|
16
|
+
4. **Selects the next probe** to maximise expected evidence gain (KL-optimal)
|
|
17
|
+
5. **Decides** when statistical confidence is reached — no fixed sample size required
|
|
18
|
+
|
|
19
|
+
## Why APD Over Existing Approaches?
|
|
20
|
+
|
|
21
|
+
| Approach | Method | Weakness | APD Advantage |
|
|
22
|
+
|----------|--------|----------|---------------|
|
|
23
|
+
| **Passive monitoring** | Watch logs/traffic for anomalies | Fails against agents that stay quiet until activated | APD *forces* adaptive agents to reveal themselves |
|
|
24
|
+
| **Fixed probing** (honeypots, canaries) | Deploy known traps | Predictable — sophisticated agents learn to avoid them | APD *adapts* its probes based on what it learns |
|
|
25
|
+
| **Threshold detectors** | Alert on metric > threshold | Requires pre-set thresholds, high false positive rate | APD uses *sequential testing* — mathematically controlled error rates |
|
|
26
|
+
| **Batch hypothesis testing** | Collect N samples, then decide | Wastes samples, fixed sample size regardless of signal strength | APD *stops early* when evidence is sufficient — up to 45× faster |
|
|
27
|
+
| **Anomaly detection (ML)** | Train model on "normal", flag outliers | Needs training data, can't detect novel adaptive behaviour | APD is *model-free* — works with any response distribution |
|
|
28
|
+
|
|
29
|
+
### Key Strengths
|
|
30
|
+
|
|
31
|
+
- **45× faster than passive monitoring** — empirically demonstrated (see demo results below)
|
|
32
|
+
- **2× faster than best fixed-probe methods** — KL-optimal selection always finds the most informative probe
|
|
33
|
+
- **Mathematically guaranteed error rates** — false positive and false negative rates are configurable via Wald SPRT, not heuristic
|
|
34
|
+
- **No training data required** — works from first observation, no labelled dataset needed
|
|
35
|
+
- **Probe-agnostic** — plug in any environment, any probe set, any response distribution
|
|
36
|
+
- **Lightweight** — pure NumPy/SciPy, no deep learning frameworks, runs on a Raspberry Pi
|
|
37
|
+
|
|
38
|
+
## Applications
|
|
39
|
+
|
|
40
|
+
### AI Safety & Adversarial Detection
|
|
41
|
+
- **Deceptive AI agents** — detect if an autonomous agent is strategically hiding capabilities or intentions
|
|
42
|
+
- **LLM prompt injection** — inject structured perturbations, monitor output entropy shifts to detect hidden tool-use loops
|
|
43
|
+
- **Sleeper agents** — probe for trigger-activated behaviour in fine-tuned models
|
|
44
|
+
- **Multi-agent deception** — detect if agents in a sandbox are colluding or behaving adaptively
|
|
45
|
+
|
|
46
|
+
### Network Security
|
|
47
|
+
- **Bot detection** — send structured timing probes, observe latency distribution shifts to distinguish bots from humans
|
|
48
|
+
- **Adversarial infrastructure** — detect command-and-control servers that adapt responses based on probe patterns
|
|
49
|
+
- **DDoS source identification** — probe suspect sources with varied request patterns to detect coordinated adaptive behaviour
|
|
50
|
+
|
|
51
|
+
### Distributed Systems
|
|
52
|
+
- **Hidden node discovery** — detect undeclared adaptive nodes in a distributed network
|
|
53
|
+
- **Byzantine fault detection** — identify nodes that respond strategically rather than honestly
|
|
54
|
+
- **Protocol compliance testing** — probe services to detect non-standard adaptive behaviour
|
|
55
|
+
|
|
56
|
+
### Research
|
|
57
|
+
- **Multi-agent systems** — test whether agents develop emergent adaptive strategies
|
|
58
|
+
- **Reinforcement learning** — detect if an RL agent has learned to game its environment
|
|
59
|
+
- **Cognitive science** — sequential optimal experiment design for detecting adaptive structure in human/animal behaviour
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install active-protocol-discovery
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Or from source:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
git clone https://github.com/DarrenEdwards111/active-protocol-discovery.git
|
|
71
|
+
cd active-protocol-discovery
|
|
72
|
+
pip install -e ".[dev]"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Or clone and install locally:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
git clone https://github.com/DarrenEdwards111/active-protocol-discovery.git
|
|
79
|
+
cd active-protocol-discovery
|
|
80
|
+
pip install -e ".[dev]"
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Quick Start
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from apd import APDEngine, GaussianWorld, KLOptimalPolicy, WaldSPRT
|
|
87
|
+
|
|
88
|
+
world = GaussianWorld(sigma=1.0, adaptive=True)
|
|
89
|
+
policy = KLOptimalPolicy(probes=[0.2, 0.5, 1.0])
|
|
90
|
+
sprt = WaldSPRT(alpha=0.01, beta=0.01)
|
|
91
|
+
|
|
92
|
+
engine = APDEngine(world, policy, sprt)
|
|
93
|
+
result = engine.run(max_steps=1000)
|
|
94
|
+
|
|
95
|
+
print(f"Decision: {'Adaptive' if result.decision else 'Null'}")
|
|
96
|
+
print(f"Steps: {result.steps}")
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## How It Works
|
|
100
|
+
|
|
101
|
+
APD implements a sequential likelihood ratio framework (Wald SPRT) with probe selection based on KL-separability. At each step the engine:
|
|
102
|
+
|
|
103
|
+
1. **Selects a probe** — the perturbation that maximises expected KL divergence between H0 and H1
|
|
104
|
+
2. **Observes the response** — samples from the true environment
|
|
105
|
+
3. **Updates the log-likelihood ratio** — accumulates evidence for/against adaptive structure
|
|
106
|
+
4. **Decides or continues** — stops when the SPRT threshold is crossed
|
|
107
|
+
|
|
108
|
+
## Demo Results
|
|
109
|
+
|
|
110
|
+
Results from `experiments/gaussian_demo.py` (1000 trials, σ=1.0, α=β=0.01):
|
|
111
|
+
|
|
112
|
+
### H1: Adaptive world (adversary present)
|
|
113
|
+
|
|
114
|
+
| Method | Mean Steps | Detection Rate | Undecided |
|
|
115
|
+
|---------------------------|------------|----------------|-----------|
|
|
116
|
+
| Passive (u=0) | 10000.0 | 0.000 | 1.000 |
|
|
117
|
+
| Weak beacon (u=0.2) | 231.5 | 0.987 | 0.000 |
|
|
118
|
+
| Strong beacon (u=1.0) | 10.4 | 0.996 | 0.000 |
|
|
119
|
+
| APD KL-optimal {0.2, 1.0} | 10.4 | 0.996 | 0.000 |
|
|
120
|
+
| APD KL-optimal {0.1..1.5} | 5.1 | 0.993 | 0.000 |
|
|
121
|
+
|
|
122
|
+
### H0: Null world (false positive check)
|
|
123
|
+
|
|
124
|
+
| Method | Mean Steps | False Positive Rate | Correct Null |
|
|
125
|
+
|---------------------------|------------|---------------------|--------------|
|
|
126
|
+
| Strong beacon (u=1.0) | 10.5 | 0.003 | 0.997 |
|
|
127
|
+
| APD KL-optimal {0.1..1.5} | 5.1 | 0.002 | 0.998 |
|
|
128
|
+
|
|
129
|
+
**Key takeaway:** APD automatically selects the strongest available probe (u=1.5), achieving 2× faster detection than a strong fixed beacon while maintaining false positive rates below α.
|
|
130
|
+
|
|
131
|
+
## Theory
|
|
132
|
+
|
|
133
|
+
### Wald SPRT
|
|
134
|
+
|
|
135
|
+
The Sequential Probability Ratio Test accumulates log-likelihood ratios until crossing a decision boundary:
|
|
136
|
+
|
|
137
|
+
- **Log-likelihood ratio:** Λ(y|u) = log p(y|H₁,u) − log p(y|H₀)
|
|
138
|
+
- **Upper threshold (accept H₁):** A = log((1−β)/α)
|
|
139
|
+
- **Lower threshold (accept H₀):** B = log(β/(1−α))
|
|
140
|
+
|
|
141
|
+
### KL Divergence as Probe Quality
|
|
142
|
+
|
|
143
|
+
For the Gaussian case, the KL divergence between H₁ and H₀ given probe u is:
|
|
144
|
+
|
|
145
|
+
D(u) = μ(u)² / (2σ²)
|
|
146
|
+
|
|
147
|
+
This quantifies how much information a single observation provides about the hypothesis. APD selects the probe maximising D(u), achieving the fastest possible sequential detection.
|
|
148
|
+
|
|
149
|
+
### Expected Sample Complexity
|
|
150
|
+
|
|
151
|
+
Under H₁, Wald's approximation gives:
|
|
152
|
+
|
|
153
|
+
E[N|H₁] ≈ ((1−β)·A + β·B) / D(u)
|
|
154
|
+
|
|
155
|
+
Stronger probes (higher D(u)) yield fewer required samples.
|
|
156
|
+
|
|
157
|
+
## Package Structure
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
apd/
|
|
161
|
+
├── models.py — World models (Gaussian, AdaptiveAgent, Network)
|
|
162
|
+
├── policy.py — Probe selection (KL-optimal, fixed, random, ε-greedy)
|
|
163
|
+
├── sprt.py — Wald Sequential Probability Ratio Test
|
|
164
|
+
├── apd.py — Main APD engine
|
|
165
|
+
└── utils.py — Statistics helpers
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Running Experiments
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
# Gaussian demo (the killer demo)
|
|
172
|
+
python -m experiments.gaussian_demo
|
|
173
|
+
|
|
174
|
+
# Adversarial agent in multi-dimensional space
|
|
175
|
+
python -m experiments.adversarial_agent_demo
|
|
176
|
+
|
|
177
|
+
# Full benchmark sweep (outputs CSV)
|
|
178
|
+
python -m experiments.benchmark > results.csv
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Tests
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
pytest tests/ -v
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Citation
|
|
188
|
+
|
|
189
|
+
```bibtex
|
|
190
|
+
@software{apd2026,
|
|
191
|
+
author = {Mikoshi Ltd},
|
|
192
|
+
title = {Active Protocol Discovery},
|
|
193
|
+
year = {2026},
|
|
194
|
+
url = {https://github.com/DarrenEdwards111/active-protocol-discovery}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Licence
|
|
199
|
+
|
|
200
|
+
Apache 2.0 — Mikoshi Ltd, 2026
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: active-protocol-discovery
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Active Protocol Discovery — detect hidden adaptive structure via sequential KL-optimal probing. 45x faster than passive monitoring, mathematically guaranteed error rates.
|
|
5
|
+
Author-email: Mikoshi Ltd <darren@mikoshi.co.uk>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/DarrenEdwards111/active-protocol-discovery
|
|
8
|
+
Project-URL: Repository, https://github.com/DarrenEdwards111/active-protocol-discovery
|
|
9
|
+
Keywords: adversarial-detection,active-testing,sequential-testing,hypothesis-testing,SPRT,KL-divergence,anomaly-detection,bot-detection,AI-safety,multi-agent,adaptive-systems,probe,security,deception-detection
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Classifier: Topic :: Security
|
|
21
|
+
Classifier: Topic :: System :: Networking :: Monitoring
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
Requires-Dist: numpy>=1.21
|
|
25
|
+
Requires-Dist: scipy>=1.7
|
|
26
|
+
Provides-Extra: plot
|
|
27
|
+
Requires-Dist: matplotlib>=3.5; extra == "plot"
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
30
|
+
Requires-Dist: matplotlib>=3.5; extra == "dev"
|
|
31
|
+
|
|
32
|
+
<p align="center">
|
|
33
|
+
<img src="apd-logo.jpg" alt="Mikoshi Active Protocol Discovery" width="600">
|
|
34
|
+
</p>
|
|
35
|
+
|
|
36
|
+
# Active Protocol Discovery (APD)
|
|
37
|
+
|
|
38
|
+
A lightweight framework for detecting hidden adaptive structure in noisy environments via sequential, KL-optimal probing.
|
|
39
|
+
|
|
40
|
+
Most detection systems are **passive** — they watch and wait. APD is **active** — it probes, measures, adapts, and decides. This makes it fundamentally faster at detecting hidden adaptive behaviour.
|
|
41
|
+
|
|
42
|
+
Rather than passively monitoring a system, APD:
|
|
43
|
+
|
|
44
|
+
1. **Injects structured perturbations** into the target environment
|
|
45
|
+
2. **Measures statistical deviation** between expected and observed responses
|
|
46
|
+
3. **Updates posterior belief** via sequential likelihood ratios
|
|
47
|
+
4. **Selects the next probe** to maximise expected evidence gain (KL-optimal)
|
|
48
|
+
5. **Decides** when statistical confidence is reached — no fixed sample size required
|
|
49
|
+
|
|
50
|
+
## Why APD Over Existing Approaches?
|
|
51
|
+
|
|
52
|
+
| Approach | Method | Weakness | APD Advantage |
|
|
53
|
+
|----------|--------|----------|---------------|
|
|
54
|
+
| **Passive monitoring** | Watch logs/traffic for anomalies | Fails against agents that stay quiet until activated | APD *forces* adaptive agents to reveal themselves |
|
|
55
|
+
| **Fixed probing** (honeypots, canaries) | Deploy known traps | Predictable — sophisticated agents learn to avoid them | APD *adapts* its probes based on what it learns |
|
|
56
|
+
| **Threshold detectors** | Alert on metric > threshold | Requires pre-set thresholds, high false positive rate | APD uses *sequential testing* — mathematically controlled error rates |
|
|
57
|
+
| **Batch hypothesis testing** | Collect N samples, then decide | Wastes samples, fixed sample size regardless of signal strength | APD *stops early* when evidence is sufficient — up to 45× faster |
|
|
58
|
+
| **Anomaly detection (ML)** | Train model on "normal", flag outliers | Needs training data, can't detect novel adaptive behaviour | APD is *model-free* — works with any response distribution |
|
|
59
|
+
|
|
60
|
+
### Key Strengths
|
|
61
|
+
|
|
62
|
+
- **45× faster than passive monitoring** — empirically demonstrated (see demo results below)
|
|
63
|
+
- **2× faster than best fixed-probe methods** — KL-optimal selection always finds the most informative probe
|
|
64
|
+
- **Mathematically guaranteed error rates** — false positive and false negative rates are configurable via Wald SPRT, not heuristic
|
|
65
|
+
- **No training data required** — works from first observation, no labelled dataset needed
|
|
66
|
+
- **Probe-agnostic** — plug in any environment, any probe set, any response distribution
|
|
67
|
+
- **Lightweight** — pure NumPy/SciPy, no deep learning frameworks, runs on a Raspberry Pi
|
|
68
|
+
|
|
69
|
+
## Applications
|
|
70
|
+
|
|
71
|
+
### AI Safety & Adversarial Detection
|
|
72
|
+
- **Deceptive AI agents** — detect if an autonomous agent is strategically hiding capabilities or intentions
|
|
73
|
+
- **LLM prompt injection** — inject structured perturbations, monitor output entropy shifts to detect hidden tool-use loops
|
|
74
|
+
- **Sleeper agents** — probe for trigger-activated behaviour in fine-tuned models
|
|
75
|
+
- **Multi-agent deception** — detect if agents in a sandbox are colluding or behaving adaptively
|
|
76
|
+
|
|
77
|
+
### Network Security
|
|
78
|
+
- **Bot detection** — send structured timing probes, observe latency distribution shifts to distinguish bots from humans
|
|
79
|
+
- **Adversarial infrastructure** — detect command-and-control servers that adapt responses based on probe patterns
|
|
80
|
+
- **DDoS source identification** — probe suspect sources with varied request patterns to detect coordinated adaptive behaviour
|
|
81
|
+
|
|
82
|
+
### Distributed Systems
|
|
83
|
+
- **Hidden node discovery** — detect undeclared adaptive nodes in a distributed network
|
|
84
|
+
- **Byzantine fault detection** — identify nodes that respond strategically rather than honestly
|
|
85
|
+
- **Protocol compliance testing** — probe services to detect non-standard adaptive behaviour
|
|
86
|
+
|
|
87
|
+
### Research
|
|
88
|
+
- **Multi-agent systems** — test whether agents develop emergent adaptive strategies
|
|
89
|
+
- **Reinforcement learning** — detect if an RL agent has learned to game its environment
|
|
90
|
+
- **Cognitive science** — sequential optimal experiment design for detecting adaptive structure in human/animal behaviour
|
|
91
|
+
|
|
92
|
+
## Installation
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
pip install active-protocol-discovery
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Or from source:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
git clone https://github.com/DarrenEdwards111/active-protocol-discovery.git
|
|
102
|
+
cd active-protocol-discovery
|
|
103
|
+
pip install -e ".[dev]"
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Or clone and install locally:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
git clone https://github.com/DarrenEdwards111/active-protocol-discovery.git
|
|
110
|
+
cd active-protocol-discovery
|
|
111
|
+
pip install -e ".[dev]"
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Quick Start
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from apd import APDEngine, GaussianWorld, KLOptimalPolicy, WaldSPRT
|
|
118
|
+
|
|
119
|
+
world = GaussianWorld(sigma=1.0, adaptive=True)
|
|
120
|
+
policy = KLOptimalPolicy(probes=[0.2, 0.5, 1.0])
|
|
121
|
+
sprt = WaldSPRT(alpha=0.01, beta=0.01)
|
|
122
|
+
|
|
123
|
+
engine = APDEngine(world, policy, sprt)
|
|
124
|
+
result = engine.run(max_steps=1000)
|
|
125
|
+
|
|
126
|
+
print(f"Decision: {'Adaptive' if result.decision else 'Null'}")
|
|
127
|
+
print(f"Steps: {result.steps}")
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## How It Works
|
|
131
|
+
|
|
132
|
+
APD implements a sequential likelihood ratio framework (Wald SPRT) with probe selection based on KL-separability. At each step the engine:
|
|
133
|
+
|
|
134
|
+
1. **Selects a probe** — the perturbation that maximises expected KL divergence between H0 and H1
|
|
135
|
+
2. **Observes the response** — samples from the true environment
|
|
136
|
+
3. **Updates the log-likelihood ratio** — accumulates evidence for/against adaptive structure
|
|
137
|
+
4. **Decides or continues** — stops when the SPRT threshold is crossed
|
|
138
|
+
|
|
139
|
+
## Demo Results
|
|
140
|
+
|
|
141
|
+
Results from `experiments/gaussian_demo.py` (1000 trials, σ=1.0, α=β=0.01):
|
|
142
|
+
|
|
143
|
+
### H1: Adaptive world (adversary present)
|
|
144
|
+
|
|
145
|
+
| Method | Mean Steps | Detection Rate | Undecided |
|
|
146
|
+
|---------------------------|------------|----------------|-----------|
|
|
147
|
+
| Passive (u=0) | 10000.0 | 0.000 | 1.000 |
|
|
148
|
+
| Weak beacon (u=0.2) | 231.5 | 0.987 | 0.000 |
|
|
149
|
+
| Strong beacon (u=1.0) | 10.4 | 0.996 | 0.000 |
|
|
150
|
+
| APD KL-optimal {0.2, 1.0} | 10.4 | 0.996 | 0.000 |
|
|
151
|
+
| APD KL-optimal {0.1..1.5} | 5.1 | 0.993 | 0.000 |
|
|
152
|
+
|
|
153
|
+
### H0: Null world (false positive check)
|
|
154
|
+
|
|
155
|
+
| Method | Mean Steps | False Positive Rate | Correct Null |
|
|
156
|
+
|---------------------------|------------|---------------------|--------------|
|
|
157
|
+
| Strong beacon (u=1.0) | 10.5 | 0.003 | 0.997 |
|
|
158
|
+
| APD KL-optimal {0.1..1.5} | 5.1 | 0.002 | 0.998 |
|
|
159
|
+
|
|
160
|
+
**Key takeaway:** APD automatically selects the strongest available probe (u=1.5), achieving 2× faster detection than a strong fixed beacon while maintaining false positive rates below α.
|
|
161
|
+
|
|
162
|
+
## Theory
|
|
163
|
+
|
|
164
|
+
### Wald SPRT
|
|
165
|
+
|
|
166
|
+
The Sequential Probability Ratio Test accumulates log-likelihood ratios until crossing a decision boundary:
|
|
167
|
+
|
|
168
|
+
- **Log-likelihood ratio:** Λ(y|u) = log p(y|H₁,u) − log p(y|H₀)
|
|
169
|
+
- **Upper threshold (accept H₁):** A = log((1−β)/α)
|
|
170
|
+
- **Lower threshold (accept H₀):** B = log(β/(1−α))
|
|
171
|
+
|
|
172
|
+
### KL Divergence as Probe Quality
|
|
173
|
+
|
|
174
|
+
For the Gaussian case, the KL divergence between H₁ and H₀ given probe u is:
|
|
175
|
+
|
|
176
|
+
D(u) = μ(u)² / (2σ²)
|
|
177
|
+
|
|
178
|
+
This quantifies how much information a single observation provides about the hypothesis. APD selects the probe maximising D(u), achieving the fastest possible sequential detection.
|
|
179
|
+
|
|
180
|
+
### Expected Sample Complexity
|
|
181
|
+
|
|
182
|
+
Under H₁, Wald's approximation gives:
|
|
183
|
+
|
|
184
|
+
E[N|H₁] ≈ ((1−β)·A + β·B) / D(u)
|
|
185
|
+
|
|
186
|
+
Stronger probes (higher D(u)) yield fewer required samples.
|
|
187
|
+
|
|
188
|
+
## Package Structure
|
|
189
|
+
|
|
190
|
+
```
|
|
191
|
+
apd/
|
|
192
|
+
├── models.py — World models (Gaussian, AdaptiveAgent, Network)
|
|
193
|
+
├── policy.py — Probe selection (KL-optimal, fixed, random, ε-greedy)
|
|
194
|
+
├── sprt.py — Wald Sequential Probability Ratio Test
|
|
195
|
+
├── apd.py — Main APD engine
|
|
196
|
+
└── utils.py — Statistics helpers
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Running Experiments
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
# Gaussian demo (the killer demo)
|
|
203
|
+
python -m experiments.gaussian_demo
|
|
204
|
+
|
|
205
|
+
# Adversarial agent in multi-dimensional space
|
|
206
|
+
python -m experiments.adversarial_agent_demo
|
|
207
|
+
|
|
208
|
+
# Full benchmark sweep (outputs CSV)
|
|
209
|
+
python -m experiments.benchmark > results.csv
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Tests
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
pytest tests/ -v
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Citation
|
|
219
|
+
|
|
220
|
+
```bibtex
|
|
221
|
+
@software{apd2026,
|
|
222
|
+
author = {Mikoshi Ltd},
|
|
223
|
+
title = {Active Protocol Discovery},
|
|
224
|
+
year = {2026},
|
|
225
|
+
url = {https://github.com/DarrenEdwards111/active-protocol-discovery}
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Licence
|
|
230
|
+
|
|
231
|
+
Apache 2.0 — Mikoshi Ltd, 2026
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
setup.cfg
|
|
4
|
+
active_protocol_discovery.egg-info/PKG-INFO
|
|
5
|
+
active_protocol_discovery.egg-info/SOURCES.txt
|
|
6
|
+
active_protocol_discovery.egg-info/dependency_links.txt
|
|
7
|
+
active_protocol_discovery.egg-info/entry_points.txt
|
|
8
|
+
active_protocol_discovery.egg-info/requires.txt
|
|
9
|
+
active_protocol_discovery.egg-info/top_level.txt
|
|
10
|
+
apd/__init__.py
|
|
11
|
+
apd/apd.py
|
|
12
|
+
apd/models.py
|
|
13
|
+
apd/policy.py
|
|
14
|
+
apd/sprt.py
|
|
15
|
+
apd/utils.py
|
|
16
|
+
experiments/__init__.py
|
|
17
|
+
experiments/adversarial_agent_demo.py
|
|
18
|
+
experiments/benchmark.py
|
|
19
|
+
experiments/gaussian_demo.py
|
|
20
|
+
tests/test_apd.py
|
|
21
|
+
tests/test_models.py
|
|
22
|
+
tests/test_policy.py
|
|
23
|
+
tests/test_sprt.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|