forgeproof-cli 0.2.0__py3-none-any.whl
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.
- forge_gate/__init__.py +44 -0
- forge_gate/_deprecated_cli.py +16 -0
- forgeproof/__init__.py +3 -0
- forgeproof/catalog.py +110 -0
- forgeproof/cli.py +560 -0
- forgeproof/config.py +339 -0
- forgeproof/data/gate_catalog.json +66 -0
- forgeproof/data/host_sets.json +7 -0
- forgeproof/debug_caller.py +271 -0
- forgeproof/debug_core.py +368 -0
- forgeproof/fixture_engine.py +81 -0
- forgeproof/fixtures/__init__.py +19 -0
- forgeproof/fixtures/decision_tree_id3.py +90 -0
- forgeproof/fixtures/gradient_descent_momentum.py +73 -0
- forgeproof/fixtures/kmeans_clustering.py +98 -0
- forgeproof/fixtures/knn_classifier.py +79 -0
- forgeproof/fixtures/linear_regression_gd.py +77 -0
- forgeproof/fixtures/logistic_regression.py +85 -0
- forgeproof/fixtures/naive_bayes_text.py +114 -0
- forgeproof/fixtures/perceptron.py +84 -0
- forgeproof/fixtures/polynomial_regression.py +67 -0
- forgeproof/fixtures/ridge_regression_normal_eq.py +75 -0
- forgeproof/ledger.py +633 -0
- forgeproof/lens_tools/__init__.py +11 -0
- forgeproof/lens_tools/guard_lens.py +1250 -0
- forgeproof/lens_tools/invariant_contracts/linear_regression_gd.json +74 -0
- forgeproof/lens_tools/invariant_contracts/polynomial_regression.json +86 -0
- forgeproof/lens_tools/invariant_contracts/ridge_regression_normal_eq.json +86 -0
- forgeproof/lens_tools/invariant_gate.py +199 -0
- forgeproof/lens_tools/metamorphic/__init__.py +9 -0
- forgeproof/lens_tools/metamorphic/relations.py +962 -0
- forgeproof/lens_tools/metamorphic/runner.py +112 -0
- forgeproof/lens_tools/twin_consensus.py +395 -0
- forgeproof/lenses.py +923 -0
- forgeproof/paths.py +56 -0
- forgeproof/reference/ORIGIN.md +29 -0
- forgeproof/reference/decision_tree_id3.py +197 -0
- forgeproof/reference/gradient_descent_momentum.py +141 -0
- forgeproof/reference/kmeans_clustering.py +139 -0
- forgeproof/reference/knn_classifier.py +118 -0
- forgeproof/reference/linear_regression_gd.py +106 -0
- forgeproof/reference/logistic_regression.py +141 -0
- forgeproof/reference/naive_bayes_text.py +152 -0
- forgeproof/reference/perceptron.py +123 -0
- forgeproof/reference/polynomial_regression.py +125 -0
- forgeproof/reference/ridge_regression_normal_eq.py +165 -0
- forgeproof/runner.py +147 -0
- forgeproof/scout/__init__.py +7 -0
- forgeproof/scout/behaviour.py +365 -0
- forgeproof/scout/box.py +581 -0
- forgeproof/scout/cards.py +180 -0
- forgeproof/scout/contract.py +37 -0
- forgeproof/scout/corpus.py +104 -0
- forgeproof/scout/embed.py +217 -0
- forgeproof/scout/engines/__init__.py +1 -0
- forgeproof/scout/engines/chaos.py +454 -0
- forgeproof/scout/engines/chaos_port.py +1331 -0
- forgeproof/scout/engines/chaos_runner.py +178 -0
- forgeproof/scout/engines/executor_port.py +239 -0
- forgeproof/scout/envfit.py +54 -0
- forgeproof/scout/experience.py +161 -0
- forgeproof/scout/fetchproxy.py +368 -0
- forgeproof/scout/ghcache.py +395 -0
- forgeproof/scout/ghpool.py +253 -0
- forgeproof/scout/ghsearch.py +248 -0
- forgeproof/scout/ids.py +82 -0
- forgeproof/scout/intent_lens.py +170 -0
- forgeproof/scout/learn.py +583 -0
- forgeproof/scout/meaning_lens.py +231 -0
- forgeproof/scout/outcomes.py +42 -0
- forgeproof/scout/plant.py +327 -0
- forgeproof/scout/ratify.py +214 -0
- forgeproof/scout/report.py +448 -0
- forgeproof/scout/rule2.py +177 -0
- forgeproof/scout/shape.py +107 -0
- forgeproof/scout/shape_lens.py +71 -0
- forgeproof/scout/signature.py +52 -0
- forgeproof/scout/slots.py +39 -0
- forgeproof/scout/upstream_facts.py +536 -0
- forgeproof/unsupported.py +83 -0
- forgeproof/verifier.py +208 -0
- forgeproof/view_coverage.py +166 -0
- forgeproof_cli-0.2.0.dist-info/METADATA +285 -0
- forgeproof_cli-0.2.0.dist-info/RECORD +89 -0
- forgeproof_cli-0.2.0.dist-info/WHEEL +5 -0
- forgeproof_cli-0.2.0.dist-info/entry_points.txt +3 -0
- forgeproof_cli-0.2.0.dist-info/licenses/LICENSE +201 -0
- forgeproof_cli-0.2.0.dist-info/licenses/NOTICE +23 -0
- forgeproof_cli-0.2.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"pattern": "linear_regression_gd",
|
|
3
|
+
"n_samples": 100,
|
|
4
|
+
"ratified": true,
|
|
5
|
+
"note": "human sets ratified=true after reviewing contracts",
|
|
6
|
+
"invariants": {
|
|
7
|
+
"r2": {
|
|
8
|
+
"observed": [
|
|
9
|
+
0.995,
|
|
10
|
+
0.99916
|
|
11
|
+
],
|
|
12
|
+
"mean": 0.99769,
|
|
13
|
+
"sd": 0.00082,
|
|
14
|
+
"contract": [
|
|
15
|
+
0.9744,
|
|
16
|
+
1.021
|
|
17
|
+
],
|
|
18
|
+
"sign": "positive"
|
|
19
|
+
},
|
|
20
|
+
"weight_0": {
|
|
21
|
+
"observed": [
|
|
22
|
+
1.92725,
|
|
23
|
+
2.08325
|
|
24
|
+
],
|
|
25
|
+
"mean": 2.003,
|
|
26
|
+
"sd": 0.03363,
|
|
27
|
+
"contract": [
|
|
28
|
+
1.8485,
|
|
29
|
+
2.1575
|
|
30
|
+
],
|
|
31
|
+
"sign": "positive"
|
|
32
|
+
},
|
|
33
|
+
"weight_1": {
|
|
34
|
+
"observed": [
|
|
35
|
+
2.91519,
|
|
36
|
+
3.09289
|
|
37
|
+
],
|
|
38
|
+
"mean": 2.99906,
|
|
39
|
+
"sd": 0.03219,
|
|
40
|
+
"contract": [
|
|
41
|
+
2.8503,
|
|
42
|
+
3.1478
|
|
43
|
+
],
|
|
44
|
+
"sign": "positive"
|
|
45
|
+
},
|
|
46
|
+
"bias": {
|
|
47
|
+
"observed": [
|
|
48
|
+
-0.19178,
|
|
49
|
+
0.24467
|
|
50
|
+
],
|
|
51
|
+
"mean": 0.00017,
|
|
52
|
+
"sd": 0.0945,
|
|
53
|
+
"contract": [
|
|
54
|
+
-0.3979,
|
|
55
|
+
0.3982
|
|
56
|
+
],
|
|
57
|
+
"sign": "mixed"
|
|
58
|
+
},
|
|
59
|
+
"ratio_w0_w1": {
|
|
60
|
+
"observed": [
|
|
61
|
+
0.63285,
|
|
62
|
+
0.69464
|
|
63
|
+
],
|
|
64
|
+
"mean": 0.66795,
|
|
65
|
+
"sd": 0.01304,
|
|
66
|
+
"contract": [
|
|
67
|
+
0.5958,
|
|
68
|
+
0.7401
|
|
69
|
+
],
|
|
70
|
+
"sign": "positive"
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
"ratified_by": "opus+operator 2026-07-22"
|
|
74
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"pattern": "polynomial_regression",
|
|
3
|
+
"n_samples": 100,
|
|
4
|
+
"ratified": true,
|
|
5
|
+
"note": "human sets ratified=true after reviewing contracts",
|
|
6
|
+
"invariants": {
|
|
7
|
+
"mse": {
|
|
8
|
+
"observed": [
|
|
9
|
+
0.0025,
|
|
10
|
+
0.01797
|
|
11
|
+
],
|
|
12
|
+
"mean": 0.00844,
|
|
13
|
+
"sd": 0.0029,
|
|
14
|
+
"contract": [
|
|
15
|
+
-0.0232,
|
|
16
|
+
0.0401
|
|
17
|
+
],
|
|
18
|
+
"sign": "positive"
|
|
19
|
+
},
|
|
20
|
+
"coeff_0": {
|
|
21
|
+
"observed": [
|
|
22
|
+
1.86658,
|
|
23
|
+
2.19115
|
|
24
|
+
],
|
|
25
|
+
"mean": 2.00395,
|
|
26
|
+
"sd": 0.06316,
|
|
27
|
+
"contract": [
|
|
28
|
+
1.7313,
|
|
29
|
+
2.2766
|
|
30
|
+
],
|
|
31
|
+
"sign": "positive"
|
|
32
|
+
},
|
|
33
|
+
"coeff_1": {
|
|
34
|
+
"observed": [
|
|
35
|
+
2.90864,
|
|
36
|
+
3.06733
|
|
37
|
+
],
|
|
38
|
+
"mean": 2.99782,
|
|
39
|
+
"sd": 0.03137,
|
|
40
|
+
"contract": [
|
|
41
|
+
2.8523,
|
|
42
|
+
3.1433
|
|
43
|
+
],
|
|
44
|
+
"sign": "positive"
|
|
45
|
+
},
|
|
46
|
+
"coeff_2": {
|
|
47
|
+
"observed": [
|
|
48
|
+
0.49319,
|
|
49
|
+
0.50747
|
|
50
|
+
],
|
|
51
|
+
"mean": 0.50026,
|
|
52
|
+
"sd": 0.00314,
|
|
53
|
+
"contract": [
|
|
54
|
+
0.4677,
|
|
55
|
+
0.5328
|
|
56
|
+
],
|
|
57
|
+
"sign": "positive"
|
|
58
|
+
},
|
|
59
|
+
"ratio_c0_c1": {
|
|
60
|
+
"observed": [
|
|
61
|
+
0.61057,
|
|
62
|
+
0.75332
|
|
63
|
+
],
|
|
64
|
+
"mean": 0.66873,
|
|
65
|
+
"sd": 0.02735,
|
|
66
|
+
"contract": [
|
|
67
|
+
0.5393,
|
|
68
|
+
0.7981
|
|
69
|
+
],
|
|
70
|
+
"sign": "positive"
|
|
71
|
+
},
|
|
72
|
+
"ratio_c1_c2": {
|
|
73
|
+
"observed": [
|
|
74
|
+
5.73161,
|
|
75
|
+
6.20952
|
|
76
|
+
],
|
|
77
|
+
"mean": 5.99315,
|
|
78
|
+
"sd": 0.09928,
|
|
79
|
+
"contract": [
|
|
80
|
+
5.576,
|
|
81
|
+
6.4103
|
|
82
|
+
],
|
|
83
|
+
"sign": "positive"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"pattern": "ridge_regression_normal_eq",
|
|
3
|
+
"n_samples": 100,
|
|
4
|
+
"ratified": true,
|
|
5
|
+
"note": "human sets ratified=true after reviewing contracts",
|
|
6
|
+
"invariants": {
|
|
7
|
+
"r2": {
|
|
8
|
+
"observed": [
|
|
9
|
+
0.99535,
|
|
10
|
+
0.9992
|
|
11
|
+
],
|
|
12
|
+
"mean": 0.99785,
|
|
13
|
+
"sd": 0.00074,
|
|
14
|
+
"contract": [
|
|
15
|
+
0.9749,
|
|
16
|
+
1.0208
|
|
17
|
+
],
|
|
18
|
+
"sign": "positive"
|
|
19
|
+
},
|
|
20
|
+
"weight_0": {
|
|
21
|
+
"observed": [
|
|
22
|
+
1.9041,
|
|
23
|
+
2.11104
|
|
24
|
+
],
|
|
25
|
+
"mean": 1.99949,
|
|
26
|
+
"sd": 0.0354,
|
|
27
|
+
"contract": [
|
|
28
|
+
1.8379,
|
|
29
|
+
2.1611
|
|
30
|
+
],
|
|
31
|
+
"sign": "positive"
|
|
32
|
+
},
|
|
33
|
+
"weight_1": {
|
|
34
|
+
"observed": [
|
|
35
|
+
2.90466,
|
|
36
|
+
3.06997
|
|
37
|
+
],
|
|
38
|
+
"mean": 2.99705,
|
|
39
|
+
"sd": 0.03193,
|
|
40
|
+
"contract": [
|
|
41
|
+
2.8493,
|
|
42
|
+
3.1448
|
|
43
|
+
],
|
|
44
|
+
"sign": "positive"
|
|
45
|
+
},
|
|
46
|
+
"weight_2": {
|
|
47
|
+
"observed": [
|
|
48
|
+
-1.0783,
|
|
49
|
+
-0.90849
|
|
50
|
+
],
|
|
51
|
+
"mean": -1.00015,
|
|
52
|
+
"sd": 0.03174,
|
|
53
|
+
"contract": [
|
|
54
|
+
-1.1471,
|
|
55
|
+
-0.8532
|
|
56
|
+
],
|
|
57
|
+
"sign": "negative"
|
|
58
|
+
},
|
|
59
|
+
"ratio_w0_w1": {
|
|
60
|
+
"observed": [
|
|
61
|
+
0.62592,
|
|
62
|
+
0.72585
|
|
63
|
+
],
|
|
64
|
+
"mean": 0.66724,
|
|
65
|
+
"sd": 0.01449,
|
|
66
|
+
"contract": [
|
|
67
|
+
0.5893,
|
|
68
|
+
0.7452
|
|
69
|
+
],
|
|
70
|
+
"sign": "positive"
|
|
71
|
+
},
|
|
72
|
+
"ratio_w1_w2": {
|
|
73
|
+
"observed": [
|
|
74
|
+
-3.32097,
|
|
75
|
+
-2.76729
|
|
76
|
+
],
|
|
77
|
+
"mean": -2.99963,
|
|
78
|
+
"sd": 0.10047,
|
|
79
|
+
"contract": [
|
|
80
|
+
-3.4215,
|
|
81
|
+
-2.5777
|
|
82
|
+
],
|
|
83
|
+
"sign": "negative"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""ForgeProof — Wave 2 (#5): Daikon-style mined invariant contracts.
|
|
3
|
+
|
|
4
|
+
Mine likely invariants from a signed oracle (run it across many input
|
|
5
|
+
instances, observe outputs), propose ratifiable contracts, then check
|
|
6
|
+
candidates against the RATIFIED contract. Zero LLM in the verdict path.
|
|
7
|
+
|
|
8
|
+
Modes:
|
|
9
|
+
mine <pattern> -> write contracts/<pattern>.json (ratified=false)
|
|
10
|
+
verify <user_file> --pattern <p> -> PASS/FAIL/UNSUPPORTED against ratified contract
|
|
11
|
+
|
|
12
|
+
A contract is only enforced once a human sets "ratified": true. This adds
|
|
13
|
+
RELATIONAL catching power (e.g. coefficient ratios/signs) that scalar
|
|
14
|
+
property thresholds miss — proven: weights individually in-tolerance but
|
|
15
|
+
whose ratio is wrong are caught here, not by property-primary.
|
|
16
|
+
"""
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
import argparse, importlib.util, json, random, statistics, sys
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
# The ratified contracts ship next to this file, inside the package (KD-2). Only `mine` writes
|
|
22
|
+
# here, and only from a checkout: run it as `python -m forgeproof.lens_tools.invariant_gate`.
|
|
23
|
+
CONTRACTS = Path(__file__).resolve().parent / "invariant_contracts"
|
|
24
|
+
|
|
25
|
+
from forgeproof.catalog import reference_path # noqa: E402
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def load(path, name):
|
|
29
|
+
spec = importlib.util.spec_from_file_location(name, str(path))
|
|
30
|
+
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m); return m
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# ---- per-pattern adapters: (oracle_relpath, fn_name, gen(seed)->args, metrics(out,args)->dict) ----
|
|
34
|
+
def _gen_linreg(seed, n=30):
|
|
35
|
+
random.seed(seed)
|
|
36
|
+
X = [[random.uniform(-5, 5), random.uniform(-5, 5)] for _ in range(n)]
|
|
37
|
+
y = [2.0 * X[i][0] + 3.0 * X[i][1] + random.gauss(0, 0.5) for i in range(n)]
|
|
38
|
+
return {"X": X, "y": y, "learning_rate": 0.01, "iterations": 200}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _metrics_linreg(out, args):
|
|
42
|
+
w, b = out
|
|
43
|
+
X, y = args["X"], args["y"]
|
|
44
|
+
pred = [b + sum(w[j] * X[i][j] for j in range(2)) for i in range(len(X))]
|
|
45
|
+
my = sum(y) / len(y)
|
|
46
|
+
ssr = sum((pred[i] - y[i]) ** 2 for i in range(len(X)))
|
|
47
|
+
sst = sum((y[i] - my) ** 2 for i in range(len(X)))
|
|
48
|
+
r2 = 1 - ssr / sst if sst > 0 else 0.0
|
|
49
|
+
return {"r2": r2, "weight_0": w[0], "weight_1": w[1], "bias": b,
|
|
50
|
+
"ratio_w0_w1": w[0] / w[1] if abs(w[1]) > 1e-9 else float("nan")}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _gen_ridge(seed, n_samples=30, n_features=3):
|
|
54
|
+
random.seed(seed)
|
|
55
|
+
X = [[random.uniform(-5, 5) for _ in range(n_features)] for _ in range(n_samples)]
|
|
56
|
+
true_weights = [2.0, 3.0, -1.0]
|
|
57
|
+
y = [sum(true_weights[j] * X[i][j] for j in range(n_features)) + random.gauss(0, 0.5)
|
|
58
|
+
for i in range(n_samples)]
|
|
59
|
+
return {"X": X, "y": y, "lambda_reg": 0.01}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _metrics_ridge(out, args):
|
|
63
|
+
weights = out
|
|
64
|
+
X, y = args["X"], args["y"]
|
|
65
|
+
n_samples = len(X)
|
|
66
|
+
pred = [sum(weights[j] * X[i][j] for j in range(len(weights))) for i in range(n_samples)]
|
|
67
|
+
my = sum(y) / len(y)
|
|
68
|
+
ssr = sum((pred[i] - y[i]) ** 2 for i in range(n_samples))
|
|
69
|
+
sst = sum((y[i] - my) ** 2 for i in range(n_samples))
|
|
70
|
+
r2 = 1 - ssr / sst if sst > 0 else 0.0
|
|
71
|
+
w0, w1, w2 = weights[0], weights[1], weights[2]
|
|
72
|
+
return {"r2": r2,
|
|
73
|
+
"weight_0": w0, "weight_1": w1, "weight_2": w2,
|
|
74
|
+
"ratio_w0_w1": w0 / w1 if abs(w1) > 1e-9 else float("nan"),
|
|
75
|
+
"ratio_w1_w2": w1 / w2 if abs(w2) > 1e-9 else float("nan")}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _gen_poly(seed, n=20):
|
|
79
|
+
random.seed(seed)
|
|
80
|
+
true_coeffs = [2.0, 3.0, 0.5]
|
|
81
|
+
x_vals = [float(i) * 0.5 for i in range(n)]
|
|
82
|
+
y_vals = []
|
|
83
|
+
for x in x_vals:
|
|
84
|
+
y = true_coeffs[0] + true_coeffs[1] * x + true_coeffs[2] * x * x + random.gauss(0, 0.1)
|
|
85
|
+
y_vals.append(y)
|
|
86
|
+
return {"x_vals": x_vals, "y_vals": y_vals, "degree": 2}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _metrics_poly(out, args):
|
|
90
|
+
coeffs = out
|
|
91
|
+
x_vals, y_vals = args["x_vals"], args["y_vals"]
|
|
92
|
+
pred = []
|
|
93
|
+
for x in x_vals:
|
|
94
|
+
p = 0.0
|
|
95
|
+
power = 1.0
|
|
96
|
+
for c in coeffs:
|
|
97
|
+
p += c * power
|
|
98
|
+
power *= x
|
|
99
|
+
pred.append(p)
|
|
100
|
+
mse = sum((pred[i] - y_vals[i]) ** 2 for i in range(len(y_vals))) / len(y_vals)
|
|
101
|
+
c0, c1, c2 = coeffs[0], coeffs[1], coeffs[2]
|
|
102
|
+
return {"mse": mse,
|
|
103
|
+
"coeff_0": c0, "coeff_1": c1, "coeff_2": c2,
|
|
104
|
+
"ratio_c0_c1": c0 / c1 if abs(c1) > 1e-9 else float("nan"),
|
|
105
|
+
"ratio_c1_c2": c1 / c2 if abs(c2) > 1e-9 else float("nan")}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
ADAPTERS = {
|
|
109
|
+
"linear_regression_gd": {
|
|
110
|
+
"oracle": "python/data-science/core_ml/linear_regression_gd.py",
|
|
111
|
+
"fn": "linear_regression_gd", "gen": _gen_linreg, "metrics": _metrics_linreg,
|
|
112
|
+
},
|
|
113
|
+
"ridge_regression_normal_eq": {
|
|
114
|
+
"oracle": "python/data-science/core_ml/ridge_regression_normal_eq.py",
|
|
115
|
+
"fn": "ridge_regression_normal_eq", "gen": _gen_ridge, "metrics": _metrics_ridge,
|
|
116
|
+
},
|
|
117
|
+
"polynomial_regression": {
|
|
118
|
+
"oracle": "python/data-science/statistical_modeling/polynomial_regression.py",
|
|
119
|
+
"fn": "fit_polynomial", "gen": _gen_poly, "metrics": _metrics_poly,
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def mine(pattern, n=100):
|
|
125
|
+
a = ADAPTERS[pattern]
|
|
126
|
+
# The shipped reference copy (forgeproof/reference/, byte copy of a["oracle"] in the
|
|
127
|
+
# library; see reference/ORIGIN.md). mine() never reads the library itself.
|
|
128
|
+
oracle = load(reference_path(pattern), "oracle")
|
|
129
|
+
fn = getattr(oracle, a["fn"])
|
|
130
|
+
samples = []
|
|
131
|
+
for seed in range(n):
|
|
132
|
+
args = a["gen"](seed)
|
|
133
|
+
out = fn(**args)
|
|
134
|
+
samples.append(a["metrics"](out, args))
|
|
135
|
+
inv = {}
|
|
136
|
+
for k in samples[0]:
|
|
137
|
+
v = [s[k] for s in samples if s[k] == s[k]] # drop NaN
|
|
138
|
+
if not v:
|
|
139
|
+
continue
|
|
140
|
+
mean, sd = statistics.mean(v), statistics.pstdev(v)
|
|
141
|
+
inv[k] = {"observed": [round(min(v), 5), round(max(v), 5)],
|
|
142
|
+
"mean": round(mean, 5), "sd": round(sd, 5),
|
|
143
|
+
"contract": [round(mean - 4 * sd - 0.02, 4), round(mean + 4 * sd + 0.02, 4)],
|
|
144
|
+
"sign": "positive" if min(v) > 0 else ("negative" if max(v) < 0 else "mixed")}
|
|
145
|
+
doc = {"pattern": pattern, "n_samples": n, "ratified": False,
|
|
146
|
+
"note": "human sets ratified=true after reviewing contracts", "invariants": inv}
|
|
147
|
+
CONTRACTS.mkdir(parents=True, exist_ok=True)
|
|
148
|
+
(CONTRACTS / f"{pattern}.json").write_text(json.dumps(doc, indent=2))
|
|
149
|
+
return doc
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def verify(user_file, pattern):
|
|
153
|
+
cpath = CONTRACTS / f"{pattern}.json"
|
|
154
|
+
if pattern not in ADAPTERS or not cpath.exists():
|
|
155
|
+
return "UNSUPPORTED", "no adapter or contract for pattern"
|
|
156
|
+
doc = json.loads(cpath.read_text())
|
|
157
|
+
if not doc.get("ratified"):
|
|
158
|
+
return "UNSUPPORTED", "contract not yet ratified by a human"
|
|
159
|
+
a = ADAPTERS[pattern]
|
|
160
|
+
user = load(Path(user_file), "user")
|
|
161
|
+
fn = getattr(user, a["fn"], None)
|
|
162
|
+
if fn is None:
|
|
163
|
+
return "FAIL", f"user must define {a['fn']}"
|
|
164
|
+
# deterministic seed 0 instance for the check
|
|
165
|
+
args = a["gen"](0)
|
|
166
|
+
try:
|
|
167
|
+
m = a["metrics"](fn(**args), args)
|
|
168
|
+
except Exception as e:
|
|
169
|
+
return "FAIL", f"user raised {type(e).__name__}: {e}"
|
|
170
|
+
violations = []
|
|
171
|
+
for k, spec in doc["invariants"].items():
|
|
172
|
+
val = m.get(k)
|
|
173
|
+
if val is None or val != val:
|
|
174
|
+
continue
|
|
175
|
+
lo, hi = spec["contract"]
|
|
176
|
+
if not (lo <= val <= hi):
|
|
177
|
+
violations.append(f"{k}={val:.4f} outside [{lo},{hi}]")
|
|
178
|
+
if violations:
|
|
179
|
+
return "FAIL", "invariant violation: " + "; ".join(violations)
|
|
180
|
+
return "PASS", "all ratified invariants hold"
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def main():
|
|
184
|
+
ap = argparse.ArgumentParser()
|
|
185
|
+
sub = ap.add_subparsers(dest="mode", required=True)
|
|
186
|
+
m = sub.add_parser("mine"); m.add_argument("pattern")
|
|
187
|
+
v = sub.add_parser("verify"); v.add_argument("user_file"); v.add_argument("--pattern", required=True)
|
|
188
|
+
args = ap.parse_args()
|
|
189
|
+
if args.mode == "mine":
|
|
190
|
+
doc = mine(args.pattern)
|
|
191
|
+
print(json.dumps(doc, indent=2))
|
|
192
|
+
else:
|
|
193
|
+
status, reason = verify(args.user_file, args.pattern)
|
|
194
|
+
print(f"{status}: {args.pattern}\n reason: {reason}")
|
|
195
|
+
sys.exit(0 if status == "PASS" else 1)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
if __name__ == "__main__":
|
|
199
|
+
main()
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""Metamorphic testing support for ForgeProof.
|
|
2
|
+
|
|
3
|
+
Provides pattern-category-specific (transform, comparator) relations that verify
|
|
4
|
+
behavior instead of exact numeric trajectory.
|
|
5
|
+
"""
|
|
6
|
+
from .relations import Relation, evaluate_relations, registry
|
|
7
|
+
from .runner import run_metamorphic
|
|
8
|
+
|
|
9
|
+
__all__ = ["Relation", "evaluate_relations", "registry", "run_metamorphic"]
|