nlp-refinement-cli 0.9.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.
- nlp_refinement/__init__.py +1 -0
- nlp_refinement/diagnostic_doe_support.py +84 -0
- nlp_refinement/doe_receipt_retention.py +81 -0
- nlp_refinement/format_contracts.py +157 -0
- nlp_refinement/host_client.py +505 -0
- nlp_refinement/host_diagnostics.py +430 -0
- nlp_refinement/host_execution_request.py +15 -0
- nlp_refinement/host_executor.py +104 -0
- nlp_refinement/host_refine.py +82 -0
- nlp_refinement/m8_doe_fixtures.py +28 -0
- nlp_refinement/m8_review_coordinator.py +370 -0
- nlp_refinement/patch_proposal_adapter.py +303 -0
- nlp_refinement/refine.py +372 -0
- nlp_refinement/repair_feasibility.py +327 -0
- nlp_refinement/resources/__init__.py +0 -0
- nlp_refinement/resources/prompts/patch-proposal-phase.v5.md +51 -0
- nlp_refinement/resources/prompts/review-phase.v5.md +57 -0
- nlp_refinement/resources/schemas/v5/acceptance-trace.schema.json +17 -0
- nlp_refinement/resources/schemas/v5/artifact.schema.json +17 -0
- nlp_refinement/resources/schemas/v5/finding.schema.json +27 -0
- nlp_refinement/resources/schemas/v5/patch-proposal-result.schema.json +66 -0
- nlp_refinement/resources/schemas/v5/phase-envelope.schema.json +18 -0
- nlp_refinement/resources/schemas/v5/retention-policy.schema.json +16 -0
- nlp_refinement/resources/schemas/v5/review-result.schema.json +37 -0
- nlp_refinement/resources/schemas/v5/run.schema.json +22 -0
- nlp_refinement/review_adapter.py +203 -0
- nlp_refinement/sensitive_data.py +98 -0
- nlp_refinement/state_store.py +1025 -0
- nlp_refinement_cli-0.9.0.dist-info/METADATA +300 -0
- nlp_refinement_cli-0.9.0.dist-info/RECORD +35 -0
- nlp_refinement_cli-0.9.0.dist-info/WHEEL +5 -0
- nlp_refinement_cli-0.9.0.dist-info/entry_points.txt +4 -0
- nlp_refinement_cli-0.9.0.dist-info/licenses/LICENSE +201 -0
- nlp_refinement_cli-0.9.0.dist-info/licenses/NOTICE +4 -0
- nlp_refinement_cli-0.9.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Local protocol primitives for NLP-refinement."""
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Shared disposable harness for opt-in M8 diagnostic DOE entrypoints only."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import tempfile
|
|
8
|
+
from typing import Any, Callable
|
|
9
|
+
|
|
10
|
+
from nlp_refinement.host_execution_request import HostExecutionRequest
|
|
11
|
+
from nlp_refinement.m8_review_coordinator import M8ReviewCoordinator
|
|
12
|
+
from nlp_refinement.host_diagnostics import FailureCapture
|
|
13
|
+
from nlp_refinement.host_executor import DiagnosticAppServerHostExecutor
|
|
14
|
+
from nlp_refinement.m8_doe_fixtures import fixture_for
|
|
15
|
+
from nlp_refinement.state_store import StateStore
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def run_isolated_diagnostic(
|
|
19
|
+
capture: FailureCapture,
|
|
20
|
+
diagnostic_mode: str,
|
|
21
|
+
coordinator_factory: Callable[[StateStore, FailureCapture], M8ReviewCoordinator] | None = None,
|
|
22
|
+
doe_kind: str = "protocol",
|
|
23
|
+
) -> tuple[int, dict[str, Any]]:
|
|
24
|
+
"""Run one shared Coordinator path in a temporary root; never persist it."""
|
|
25
|
+
with tempfile.TemporaryDirectory(prefix="nlp-refinement-m8-diagnostic-") as directory:
|
|
26
|
+
root = Path(directory)
|
|
27
|
+
target = root / "fixture"
|
|
28
|
+
target.mkdir(mode=0o700)
|
|
29
|
+
fixture = fixture_for(doe_kind)
|
|
30
|
+
artifact = target / f"{doe_kind}-fixture.md"
|
|
31
|
+
artifact.write_text(fixture, encoding="utf-8")
|
|
32
|
+
store = StateStore(root / "state" / "state.db")
|
|
33
|
+
store.initialize()
|
|
34
|
+
snapshot = _snapshot(fixture)
|
|
35
|
+
run_id = _ready_run(store, snapshot)
|
|
36
|
+
try:
|
|
37
|
+
(coordinator_factory or _default_coordinator)(store, capture).run(
|
|
38
|
+
request=HostExecutionRequest(run_id, artifact, snapshot)
|
|
39
|
+
)
|
|
40
|
+
status, code, error_class = "passed", 0, None
|
|
41
|
+
except Exception as error:
|
|
42
|
+
status, code, error_class = "failed", 1, type(error).__name__
|
|
43
|
+
return code, {
|
|
44
|
+
"status": status,
|
|
45
|
+
"error_class": error_class,
|
|
46
|
+
"diagnostic_mode": diagnostic_mode,
|
|
47
|
+
"diagnostic_signals": capture.snapshot(),
|
|
48
|
+
"artifact_kinds": [entry["kind"] for entry in store.list_artifacts(run_id)],
|
|
49
|
+
"fixture_unchanged": artifact.read_text(encoding="utf-8") == fixture,
|
|
50
|
+
"target_writes": [],
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _default_coordinator(store: StateStore, capture: FailureCapture) -> M8ReviewCoordinator:
|
|
55
|
+
return M8ReviewCoordinator(
|
|
56
|
+
store,
|
|
57
|
+
executor_factory=lambda: DiagnosticAppServerHostExecutor(capture.record_failure),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _snapshot(value: str) -> str:
|
|
62
|
+
return hashlib.sha256(value.encode("utf-8")).hexdigest()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _ready_run(store: StateStore, snapshot: str) -> str:
|
|
66
|
+
run_id = "run_m8diagnostic0001"
|
|
67
|
+
store.create_run(
|
|
68
|
+
{
|
|
69
|
+
"schema_version": "5.0", "run_id": run_id,
|
|
70
|
+
"project_key": hashlib.sha256(b"nlp-refinement/m8-diagnostic-doe").hexdigest(),
|
|
71
|
+
"target_snapshot_hash": snapshot, "current_snapshot_hash": snapshot,
|
|
72
|
+
"authorization_mode": "review-only", "state": "DRAFT", "outcome": "IN_PROGRESS",
|
|
73
|
+
"revision": 0, "repair_cycle_count": 0, "max_repair_cycles": 3,
|
|
74
|
+
"open_high_risk_assumptions": 0, "retention_policy_version": "5.0",
|
|
75
|
+
},
|
|
76
|
+
{"redacted_brief": "Run an isolated M8 host diagnostic DOE."},
|
|
77
|
+
now=1,
|
|
78
|
+
)
|
|
79
|
+
lease = store.acquire_lease(run_id, "m8-diagnostic-setup", now=1)
|
|
80
|
+
try:
|
|
81
|
+
store.transition(run_id, 0, lease["token"], "READY", "IN_PROGRESS", snapshot, now=1)
|
|
82
|
+
finally:
|
|
83
|
+
store.release_lease(run_id, lease["token"], now=1)
|
|
84
|
+
return run_id
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Owner-only lifecycle helpers for redacted M8 DOE failure receipts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
import re
|
|
10
|
+
import stat
|
|
11
|
+
import time
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
DEFAULT_FAILURE_EVIDENCE_ROOT = Path("~/.codex/refinement-loop/doe-failures").expanduser()
|
|
15
|
+
FAILURE_EVIDENCE_RETENTION_SECONDS = 24 * 60 * 60
|
|
16
|
+
_FAILURE_EVIDENCE_NAME = re.compile(r"failure-[0-9]{10}-[0-9a-f]{16}\.json\Z")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def prepare_failure_evidence_root(root: Path) -> Path:
|
|
20
|
+
"""Create or validate the owner-only root used by the DOE writer."""
|
|
21
|
+
root = root.expanduser()
|
|
22
|
+
if root.exists():
|
|
23
|
+
_validate_existing_root(root)
|
|
24
|
+
return root
|
|
25
|
+
root.mkdir(parents=True, mode=0o700)
|
|
26
|
+
return root
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def purge_expired_failure_receipts(root: Path, now: int) -> list[str]:
|
|
30
|
+
"""Remove only expired, parseable, regular receipts; never create a root."""
|
|
31
|
+
root = root.expanduser()
|
|
32
|
+
if not root.exists():
|
|
33
|
+
return []
|
|
34
|
+
_validate_existing_root(root)
|
|
35
|
+
removed: list[str] = []
|
|
36
|
+
for candidate in root.iterdir():
|
|
37
|
+
if not _FAILURE_EVIDENCE_NAME.fullmatch(candidate.name):
|
|
38
|
+
continue
|
|
39
|
+
info = candidate.lstat()
|
|
40
|
+
if not stat.S_ISREG(info.st_mode):
|
|
41
|
+
continue
|
|
42
|
+
try:
|
|
43
|
+
value = json.loads(candidate.read_text(encoding="utf-8"))
|
|
44
|
+
except (OSError, json.JSONDecodeError):
|
|
45
|
+
continue
|
|
46
|
+
if isinstance(value, dict) and isinstance(value.get("expires_at"), int) and value["expires_at"] <= now:
|
|
47
|
+
candidate.unlink()
|
|
48
|
+
removed.append(candidate.name)
|
|
49
|
+
return removed
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _validate_existing_root(root: Path) -> None:
|
|
53
|
+
info = root.lstat()
|
|
54
|
+
if stat.S_ISLNK(info.st_mode) or not stat.S_ISDIR(info.st_mode):
|
|
55
|
+
raise ValueError("DOE failure evidence root must be a non-symlink directory")
|
|
56
|
+
if info.st_uid != os.getuid():
|
|
57
|
+
raise PermissionError("DOE failure evidence root must be owned by the current user")
|
|
58
|
+
if stat.S_IMODE(info.st_mode) & 0o077:
|
|
59
|
+
raise PermissionError("DOE failure evidence root must be owner-only")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def main(argv: list[str] | None = None) -> int:
|
|
63
|
+
parser = argparse.ArgumentParser(
|
|
64
|
+
prog="nlp-refinement-doe-prune",
|
|
65
|
+
description="Remove expired, redacted M8 DOE failure receipts without invoking a model.",
|
|
66
|
+
)
|
|
67
|
+
parser.add_argument("--failure-evidence-root", type=Path, default=DEFAULT_FAILURE_EVIDENCE_ROOT)
|
|
68
|
+
args = parser.parse_args(argv)
|
|
69
|
+
removed = purge_expired_failure_receipts(args.failure_evidence_root, int(time.time()))
|
|
70
|
+
print(json.dumps({
|
|
71
|
+
"command": "doe-receipt-prune",
|
|
72
|
+
"removed": removed,
|
|
73
|
+
"removed_count": len(removed),
|
|
74
|
+
"retention_seconds": FAILURE_EVIDENCE_RETENTION_SECONDS,
|
|
75
|
+
"purge_trigger": "user-command",
|
|
76
|
+
}, ensure_ascii=False, sort_keys=True))
|
|
77
|
+
return 0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
if __name__ == "__main__":
|
|
81
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Static parity checks for externally produced phase-output contracts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
_EXPECTED_CONTRACT_IDS = {"review-result/v5", "patch-proposal-result/v5"}
|
|
11
|
+
_VALID_OWNERS = {"review_adapter", "patch_proposal_adapter"}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _load_json(path: Path) -> Any:
|
|
15
|
+
return json.loads(path.read_text(encoding="utf-8"))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _normalized_prompt_text(value: str) -> str:
|
|
19
|
+
return " ".join(value.replace("`", "").split())
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def load_format_contract_registry(root: Path) -> dict[str, Any]:
|
|
23
|
+
value = _load_json(root / "contracts/v5/phase-output-contracts.json")
|
|
24
|
+
if not isinstance(value, dict):
|
|
25
|
+
raise ValueError("format contract registry must be a JSON object")
|
|
26
|
+
return value
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def load_contract_cases(root: Path) -> dict[str, Any]:
|
|
30
|
+
value = _load_json(root / "fixtures/v5/format-contract-cases.json")
|
|
31
|
+
if not isinstance(value, dict):
|
|
32
|
+
raise ValueError("format contract cases must be a JSON object")
|
|
33
|
+
return value
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def validate_format_contract_registry(root: Path) -> list[str]:
|
|
37
|
+
"""Check traceability only; adapters remain the semantic authority."""
|
|
38
|
+
errors: list[str] = []
|
|
39
|
+
try:
|
|
40
|
+
registry = load_format_contract_registry(root)
|
|
41
|
+
cases = load_contract_cases(root)
|
|
42
|
+
except (OSError, ValueError, json.JSONDecodeError) as error:
|
|
43
|
+
return [f"format contract files are unreadable: {type(error).__name__}"]
|
|
44
|
+
if set(registry) != {"registry_version", "contracts"} or registry.get("registry_version") != "1.0":
|
|
45
|
+
return ["format contract registry has an unsupported shape or version"]
|
|
46
|
+
contracts = registry.get("contracts")
|
|
47
|
+
if not isinstance(contracts, list):
|
|
48
|
+
return ["format contract registry contracts must be an array"]
|
|
49
|
+
ids: set[str] = set()
|
|
50
|
+
declared_case_ids: set[str] = set()
|
|
51
|
+
declared_rules: dict[str, str] = {}
|
|
52
|
+
for contract in contracts:
|
|
53
|
+
if not isinstance(contract, dict) or set(contract) != {
|
|
54
|
+
"contract_id", "schema_path", "prompt_path", "valid_fixture_path", "semantic_rules"
|
|
55
|
+
}:
|
|
56
|
+
errors.append("format contract has unexpected or missing fields")
|
|
57
|
+
continue
|
|
58
|
+
contract_id = contract["contract_id"]
|
|
59
|
+
if not isinstance(contract_id, str) or contract_id in ids:
|
|
60
|
+
errors.append("format contract IDs must be unique strings")
|
|
61
|
+
continue
|
|
62
|
+
ids.add(contract_id)
|
|
63
|
+
for field in ("schema_path", "prompt_path", "valid_fixture_path"):
|
|
64
|
+
relative = contract[field]
|
|
65
|
+
if not isinstance(relative, str) or not relative or not (root / relative).is_file():
|
|
66
|
+
errors.append(f"{contract_id}: {field} does not resolve")
|
|
67
|
+
prompt_path = contract["prompt_path"]
|
|
68
|
+
prompt = (root / prompt_path).read_text(encoding="utf-8") if isinstance(prompt_path, str) and (root / prompt_path).is_file() else ""
|
|
69
|
+
rules = contract["semantic_rules"]
|
|
70
|
+
if not isinstance(rules, list) or not rules:
|
|
71
|
+
errors.append(f"{contract_id}: semantic_rules must be a non-empty array")
|
|
72
|
+
continue
|
|
73
|
+
rule_ids: set[str] = set()
|
|
74
|
+
for rule in rules:
|
|
75
|
+
if not isinstance(rule, dict) or set(rule) != {
|
|
76
|
+
"rule_id", "owner", "prompt_requirement", "negative_case_ids"
|
|
77
|
+
}:
|
|
78
|
+
errors.append(f"{contract_id}: semantic rule has unexpected or missing fields")
|
|
79
|
+
continue
|
|
80
|
+
rule_id = rule["rule_id"]
|
|
81
|
+
if not isinstance(rule_id, str) or rule_id in rule_ids:
|
|
82
|
+
errors.append(f"{contract_id}: semantic rule IDs must be unique strings")
|
|
83
|
+
continue
|
|
84
|
+
rule_ids.add(rule_id)
|
|
85
|
+
if rule["owner"] not in _VALID_OWNERS:
|
|
86
|
+
errors.append(f"{contract_id}: semantic rule owner is invalid")
|
|
87
|
+
elif rule_id in declared_rules:
|
|
88
|
+
errors.append("format contract semantic rule IDs must be globally unique")
|
|
89
|
+
else:
|
|
90
|
+
declared_rules[rule_id] = rule["owner"]
|
|
91
|
+
if f"<!-- format-rule: {rule_id} -->" not in prompt:
|
|
92
|
+
errors.append(f"{contract_id}: prompt omits marker for {rule_id}")
|
|
93
|
+
if (
|
|
94
|
+
not isinstance(rule["prompt_requirement"], str)
|
|
95
|
+
or _normalized_prompt_text(rule["prompt_requirement"])
|
|
96
|
+
not in _normalized_prompt_text(prompt)
|
|
97
|
+
):
|
|
98
|
+
errors.append(f"{contract_id}: prompt omits requirement for {rule_id}")
|
|
99
|
+
negative = rule["negative_case_ids"]
|
|
100
|
+
if not isinstance(negative, list) or not negative or not all(isinstance(case_id, str) for case_id in negative):
|
|
101
|
+
errors.append(f"{contract_id}: semantic rule needs negative case IDs")
|
|
102
|
+
else:
|
|
103
|
+
declared_case_ids.update(negative)
|
|
104
|
+
if ids != _EXPECTED_CONTRACT_IDS:
|
|
105
|
+
errors.append("format contract registry must declare exactly the supported phase outputs")
|
|
106
|
+
case_rows = cases.get("cases") if cases.get("schema_version") == "5.0" else None
|
|
107
|
+
if not isinstance(case_rows, list):
|
|
108
|
+
errors.append("format contract cases must declare schema version 5.0 and cases")
|
|
109
|
+
return errors
|
|
110
|
+
case_ids = {case.get("case_id") for case in case_rows if isinstance(case, dict)}
|
|
111
|
+
if len(case_ids) != len(case_rows) or not all(isinstance(case_id, str) for case_id in case_ids):
|
|
112
|
+
errors.append("format contract case IDs must be unique strings")
|
|
113
|
+
if case_ids != declared_case_ids:
|
|
114
|
+
errors.append("format contract negative cases and registry declarations differ")
|
|
115
|
+
for case in case_rows:
|
|
116
|
+
if not isinstance(case, dict) or set(case) != {"case_id", "rule_id", "owner", "base_fixture", "mutation"}:
|
|
117
|
+
errors.append("format contract case has unexpected or missing fields")
|
|
118
|
+
continue
|
|
119
|
+
if (
|
|
120
|
+
not isinstance(case["case_id"], str)
|
|
121
|
+
or case["owner"] not in _VALID_OWNERS
|
|
122
|
+
or not isinstance(case["rule_id"], str)
|
|
123
|
+
):
|
|
124
|
+
errors.append("format contract case owner or rule ID is invalid")
|
|
125
|
+
elif declared_rules.get(case["rule_id"]) != case["owner"]:
|
|
126
|
+
errors.append("format contract case does not match its declared semantic rule")
|
|
127
|
+
if not isinstance(case["base_fixture"], str) or not (root / case["base_fixture"]).is_file():
|
|
128
|
+
errors.append("format contract case base fixture does not resolve")
|
|
129
|
+
return errors
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def validate_state_schema_parity(root: Path) -> list[str]:
|
|
133
|
+
"""Ensure persisted-v5 enum surfaces match their StateStore authority."""
|
|
134
|
+
from nlp_refinement import state_store
|
|
135
|
+
|
|
136
|
+
checks = (
|
|
137
|
+
("artifact", "kind", state_store.ARTIFACT_KINDS),
|
|
138
|
+
("artifact", "actor", state_store.ARTIFACT_ACTORS),
|
|
139
|
+
("artifact", "redaction_status", state_store.REDACTION_STATUSES),
|
|
140
|
+
("finding", "origin_severity", state_store.FINDING_SEVERITIES),
|
|
141
|
+
("finding", "effective_gate", state_store.FINDING_GATES),
|
|
142
|
+
("finding", "status", state_store.FINDING_STATUSES),
|
|
143
|
+
("finding", "subject_kind", state_store.FINDING_SUBJECT_KINDS),
|
|
144
|
+
("finding", "evidence_plane", state_store.FINDING_EVIDENCE_PLANES),
|
|
145
|
+
("finding", "evidence_confidence", state_store.FINDING_EVIDENCE_CONFIDENCE),
|
|
146
|
+
("finding", "impact_scope", state_store.FINDING_IMPACT_SCOPES),
|
|
147
|
+
("finding", "claim_status", state_store.FINDING_CLAIM_STATUSES),
|
|
148
|
+
("run", "state", state_store.RUN_STATES),
|
|
149
|
+
("run", "outcome", state_store.RUN_OUTCOMES),
|
|
150
|
+
)
|
|
151
|
+
errors: list[str] = []
|
|
152
|
+
for schema_name, property_name, runtime_values in checks:
|
|
153
|
+
schema = _load_json(root / "nlp_refinement/resources/schemas/v5" / f"{schema_name}.schema.json")
|
|
154
|
+
values = schema.get("properties", {}).get(property_name, {}).get("enum")
|
|
155
|
+
if not isinstance(values, list) or set(values) != set(runtime_values):
|
|
156
|
+
errors.append(f"{schema_name}.{property_name} enum differs from StateStore")
|
|
157
|
+
return errors
|