tzero-mcp 10.7.3 → 11.0.0
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.
- package/CHANGELOG.md +44 -577
- package/CONTRIBUTING.md +71 -71
- package/README.md +64 -64
- package/bin/tzero-mcp.js +62 -65
- package/bin/tzero.js +61 -64
- package/index.d.ts +25 -25
- package/index.js +15 -15
- package/package.json +88 -67
- package/src/addmcp.py +14 -11
- package/src/app.py +1810 -0
- package/src/main.py +1 -1
- package/src/pc_fixer_mcp.py +25 -0
- package/src/tzero/__init__.py +54 -0
- package/src/tzero/__main__.py +6 -0
- package/src/tzero/siber_akademi.ico +0 -0
- package/src/tzero/tzero_arch_test.py +463 -0
- package/src/{tzero_benchmark.py → tzero/tzero_benchmark.py} +475 -475
- package/src/tzero/tzero_blackboard.py +257 -0
- package/src/{tzero_bridge.py → tzero/tzero_bridge.py} +546 -362
- package/src/tzero/tzero_budget.py +195 -0
- package/src/{tzero_ci.py → tzero/tzero_ci.py} +153 -153
- package/src/{tzero_commit.py → tzero/tzero_commit.py} +192 -192
- package/src/tzero/tzero_contracts.py +1093 -0
- package/src/{tzero_cost.py → tzero/tzero_cost.py} +166 -163
- package/src/{tzero_dashboard.py → tzero/tzero_dashboard.py} +531 -19
- package/src/tzero/tzero_debt.py +461 -0
- package/src/{tzero_deps.py → tzero/tzero_deps.py} +179 -4
- package/src/{tzero_drift.py → tzero/tzero_drift.py} +208 -208
- package/src/{tzero_explain.py → tzero/tzero_explain.py} +73 -7
- package/src/{tzero_features.py → tzero/tzero_features.py} +19 -17
- package/src/tzero/tzero_federation.py +299 -0
- package/src/{tzero_fix.py → tzero/tzero_fix.py} +228 -228
- package/src/{tzero_fixer → tzero/tzero_fixer}/__init__.py +63 -63
- package/src/{tzero_fixer → tzero/tzero_fixer}/ast_syntax_healer.py +464 -464
- package/src/{tzero_fixer → tzero/tzero_fixer}/ast_type_modernizer.py +288 -288
- package/src/{tzero_fixer → tzero/tzero_fixer}/async_io_modernizer.py +127 -127
- package/src/{tzero_fixer → tzero/tzero_fixer}/automated_regression_suite.py +600 -600
- package/src/{tzero_fixer → tzero/tzero_fixer}/benchmarks_and_diagnostics.py +174 -174
- package/src/{tzero_fixer → tzero/tzero_fixer}/ci_cd_self_healing_gate.py +246 -246
- package/src/{tzero_fixer → tzero/tzero_fixer}/code_duplicity_and_refactorer.py +151 -151
- package/src/{tzero_fixer → tzero/tzero_fixer}/code_smells_healer.py +260 -260
- package/src/{tzero_fixer → tzero/tzero_fixer}/cwe_remediator.py +171 -171
- package/src/{tzero_fixer → tzero/tzero_fixer}/dead_code_eliminator.py +150 -150
- package/src/{tzero_fixer → tzero/tzero_fixer}/dependency_vulnerability_auditor.py +309 -309
- package/src/{tzero_fixer → tzero/tzero_fixer}/git_advanced_surgeon.py +1 -1
- package/src/{tzero_fixer → tzero/tzero_fixer}/hardware_and_disk_doctor.py +1 -1
- package/src/{tzero_fixer → tzero/tzero_fixer}/import_graph_healer.py +412 -412
- package/src/{tzero_fixer → tzero/tzero_fixer}/interactive_tui.py +91 -91
- package/src/{tzero_fixer → tzero/tzero_fixer}/knowledge_base.py +295 -295
- package/src/{tzero_fixer → tzero/tzero_fixer}/knowledge_base_extended.py +940 -940
- package/src/{tzero_fixer → tzero/tzero_fixer}/linters_and_static_analyzer.py +307 -307
- package/src/{tzero_fixer → tzero/tzero_fixer}/mcp_server.py +49 -7
- package/src/{tzero_fixer → tzero/tzero_fixer}/os_doctor.py +502 -502
- package/src/{tzero_fixer → tzero/tzero_fixer}/packaging_abi_surgeon.py +1 -1
- package/src/{tzero_fixer → tzero/tzero_fixer}/pc_fixer_doctor.py +600 -599
- package/src/{tzero_fixer → tzero/tzero_fixer}/performance_and_memory_profiler.py +274 -274
- package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_ast_parsers.py +313 -313
- package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_build_healer.py +1 -1
- package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_config_healer.py +318 -318
- package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_doctor.py +372 -372
- package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_linters.py +1 -1
- package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_schema_validator.py +305 -305
- package/src/{tzero_fixer → tzero/tzero_fixer}/security_cwe_database.py +444 -444
- package/src/{tzero_fixer → tzero/tzero_fixer}/security_deep_rules.py +114 -114
- package/src/{tzero_fixer → tzero/tzero_fixer}/security_rules_catalog.py +110 -110
- package/src/{tzero_fixer → tzero/tzero_fixer}/security_zero_leak.py +326 -326
- package/src/{tzero_fixer → tzero/tzero_fixer}/smart_patch_recommender.py +223 -223
- package/src/{tzero_fixer → tzero/tzero_fixer}/synthetic_workspace_generator.py +220 -220
- package/src/{tzero_fixer → tzero/tzero_fixer}/system_diagnostics_extended.py +1 -1
- package/src/{tzero_fixer → tzero/tzero_fixer}/system_repair_recipes.py +447 -447
- package/src/{tzero_fixer → tzero/tzero_fixer}/venv_doctor.py +309 -309
- package/src/tzero/tzero_golden.py +319 -0
- package/src/tzero/tzero_graph_hud.html +338 -0
- package/src/tzero/tzero_graph_rag.py +263 -0
- package/src/{tzero_guard.py → tzero/tzero_guard.py} +243 -243
- package/src/tzero/tzero_hooks.py +565 -0
- package/src/{tzero_infra.py → tzero/tzero_infra.py} +274 -274
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/__init__.py +124 -124
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/causal_graph.py +310 -310
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/graph_visualizer.py +395 -395
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/hybrid_search.py +214 -214
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/latent_press.py +137 -137
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/memory_consolidation.py +212 -212
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/minhash_lsh.py +184 -184
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/orchestrator.py +335 -335
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/self_healing.py +308 -308
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/semantic_compressor.py +887 -887
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/swarm.py +261 -261
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/swarm_bft.py +144 -144
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/workspace_orchestrator.py +1167 -1167
- package/src/{tzero_jarvis → tzero/tzero_jarvis}/zero_key_router.py +173 -173
- package/src/tzero/tzero_lsp.py +634 -0
- package/src/{tzero_mcp.py → tzero/tzero_mcp.py} +479 -100
- package/src/tzero/tzero_mutation.py +358 -0
- package/src/tzero/tzero_packer.py +577 -0
- package/src/{tzero_patch.py → tzero/tzero_patch.py} +624 -624
- package/src/{tzero_pc_fixer.py → tzero/tzero_pc_fixer.py} +12699 -12657
- package/src/{tzero_pipe.py → tzero/tzero_pipe.py} +95 -95
- package/src/tzero/tzero_polyglot.py +799 -0
- package/src/tzero/tzero_proxy.py +127 -0
- package/src/{tzero_reachability.py → tzero/tzero_reachability.py} +272 -272
- package/src/{tzero_replay.py → tzero/tzero_replay.py} +141 -141
- package/src/tzero/tzero_rust_bridge.py +198 -0
- package/src/tzero/tzero_scip.py +368 -0
- package/src/{tzero_security.py → tzero/tzero_security.py} +5 -2
- package/src/tzero/tzero_semantic_merge.py +189 -0
- package/src/tzero/tzero_snapshot.py +471 -0
- package/src/{tzero_stubs.py → tzero/tzero_stubs.py} +313 -313
- package/src/tzero/tzero_subagent_engine.py +459 -0
- package/src/{tzero_superdesign.py → tzero/tzero_superdesign.py} +774 -774
- package/src/{tzero_sync.py → tzero/tzero_sync.py} +241 -241
- package/src/{tzero_task_benchmark.py → tzero/tzero_task_benchmark.py} +458 -363
- package/src/{tzero_tui.py → tzero/tzero_tui.py} +91 -22
- package/src/tzero/tzero_turbo.py +264 -0
- package/src/{tzero_v3.py → tzero/tzero_v3.py} +628 -163
- package/src/tzero.py +2 -54
- package/test/package.test.js +55 -19
- package/src/tzero/jarvis/semantic_compressor.py +0 -373
- package/src/tzero_gateway.py +0 -885
- package/src/tzero_packer.py +0 -370
- package/src/tzero_polyglot.py +0 -422
- package/src/tzero_snapshot.py +0 -258
- /package/src/{siber_akademi_icon.py → tzero/siber_akademi_icon.py} +0 -0
- /package/src/{tzero_fixer → tzero/tzero_fixer}/amalgamation.py +0 -0
- /package/src/{tzero_fixer → tzero/tzero_fixer}/models.py +0 -0
- /package/src/{tzero_fixer → tzero/tzero_fixer}/orchestrator.py +0 -0
- /package/src/{tzero_neural_cortex.py → tzero/tzero_neural_cortex.py} +0 -0
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""T-Zero Autonomous AST Mutation Testing & Test Quality Engine.
|
|
3
|
+
|
|
4
|
+
Evaluates test suite robustness and detects phantom/tautological tests
|
|
5
|
+
by injecting subtle AST mutations into code and validating if existing tests fail (kill the mutant).
|
|
6
|
+
|
|
7
|
+
Features:
|
|
8
|
+
- Pure AST mutation: Comparison, boolean, arithmetic, and constant operators.
|
|
9
|
+
- 100% Offline & Zero-Dependency: Uses Python standard library (ast, unittest, subprocess).
|
|
10
|
+
- In-memory & isolated tempfile execution without modifying git working tree.
|
|
11
|
+
- Calculates Mutation Score (0-100%) and recommends precise assertion patches.
|
|
12
|
+
|
|
13
|
+
Author: Toprak Ahmet Aydoğmuş (Siber Akademi)
|
|
14
|
+
Part of T-Zero Context Architect v10.8.0 Master Edition.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import ast
|
|
18
|
+
import copy
|
|
19
|
+
import os
|
|
20
|
+
import subprocess
|
|
21
|
+
import sys
|
|
22
|
+
import tempfile
|
|
23
|
+
import time
|
|
24
|
+
from dataclasses import dataclass, field
|
|
25
|
+
from typing import Dict, List, Optional, Tuple, Any
|
|
26
|
+
|
|
27
|
+
if sys.stdout and hasattr(sys.stdout, "reconfigure"):
|
|
28
|
+
try:
|
|
29
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
30
|
+
except Exception:
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class Mutant:
|
|
36
|
+
mutant_id: str
|
|
37
|
+
line_no: int
|
|
38
|
+
operator: str
|
|
39
|
+
original_snippet: str
|
|
40
|
+
mutated_snippet: str
|
|
41
|
+
mutated_code: str
|
|
42
|
+
status: str = "PENDING" # KILLED, SURVIVED, TIMEOUT, ERROR
|
|
43
|
+
killed_by: Optional[str] = None
|
|
44
|
+
output_snippet: Optional[str] = None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class MutationReport:
|
|
49
|
+
file_path: str
|
|
50
|
+
total_mutants: int
|
|
51
|
+
killed: int
|
|
52
|
+
survived: int
|
|
53
|
+
timeouts: int
|
|
54
|
+
errors: int
|
|
55
|
+
mutation_score: float # (killed / total) * 100
|
|
56
|
+
duration_seconds: float
|
|
57
|
+
mutants: List[Mutant] = field(default_factory=list)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class ASTMutationTransformer(ast.NodeTransformer):
|
|
61
|
+
"""Generates targeted single-point mutations in an AST tree."""
|
|
62
|
+
|
|
63
|
+
COMP_SWAPS = {
|
|
64
|
+
ast.Eq: ast.NotEq,
|
|
65
|
+
ast.NotEq: ast.Eq,
|
|
66
|
+
ast.Lt: ast.GtE,
|
|
67
|
+
ast.LtE: ast.Gt,
|
|
68
|
+
ast.Gt: ast.LtE,
|
|
69
|
+
ast.GtE: ast.Lt,
|
|
70
|
+
ast.Is: ast.IsNot,
|
|
71
|
+
ast.IsNot: ast.Is,
|
|
72
|
+
ast.In: ast.NotIn,
|
|
73
|
+
ast.NotIn: ast.In
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
BOOL_SWAPS = {
|
|
77
|
+
ast.And: ast.Or,
|
|
78
|
+
ast.Or: ast.And
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
BIN_SWAPS = {
|
|
82
|
+
ast.Add: ast.Sub,
|
|
83
|
+
ast.Sub: ast.Add,
|
|
84
|
+
ast.Mult: ast.Div,
|
|
85
|
+
ast.Div: ast.Mult
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
def __init__(self, target_mutation_index: int):
|
|
89
|
+
super().__init__()
|
|
90
|
+
self.target_index = target_mutation_index
|
|
91
|
+
self.current_index = 0
|
|
92
|
+
self.applied_mutation: Optional[Tuple[int, str, str, str]] = None # (line, op, orig, mut)
|
|
93
|
+
|
|
94
|
+
def visit_Compare(self, node: ast.Compare) -> ast.AST:
|
|
95
|
+
self.generic_visit(node)
|
|
96
|
+
for i, op in enumerate(node.ops):
|
|
97
|
+
op_type = type(op)
|
|
98
|
+
if op_type in self.COMP_SWAPS:
|
|
99
|
+
if self.current_index == self.target_index:
|
|
100
|
+
new_op_class = self.COMP_SWAPS[op_type]
|
|
101
|
+
orig_name = op_type.__name__
|
|
102
|
+
new_name = new_op_class.__name__
|
|
103
|
+
node.ops[i] = new_op_class()
|
|
104
|
+
self.applied_mutation = (
|
|
105
|
+
getattr(node, "lineno", 1),
|
|
106
|
+
"ComparisonSwap",
|
|
107
|
+
orig_name,
|
|
108
|
+
new_name
|
|
109
|
+
)
|
|
110
|
+
self.current_index += 1
|
|
111
|
+
return node
|
|
112
|
+
|
|
113
|
+
def visit_BoolOp(self, node: ast.BoolOp) -> ast.AST:
|
|
114
|
+
self.generic_visit(node)
|
|
115
|
+
op_type = type(node.op)
|
|
116
|
+
if op_type in self.BOOL_SWAPS:
|
|
117
|
+
if self.current_index == self.target_index:
|
|
118
|
+
new_op_class = self.BOOL_SWAPS[op_type]
|
|
119
|
+
orig_name = op_type.__name__
|
|
120
|
+
new_name = new_op_class.__name__
|
|
121
|
+
node.op = new_op_class()
|
|
122
|
+
self.applied_mutation = (
|
|
123
|
+
getattr(node, "lineno", 1),
|
|
124
|
+
"BooleanOperatorSwap",
|
|
125
|
+
orig_name,
|
|
126
|
+
new_name
|
|
127
|
+
)
|
|
128
|
+
self.current_index += 1
|
|
129
|
+
return node
|
|
130
|
+
|
|
131
|
+
def visit_BinOp(self, node: ast.BinOp) -> ast.AST:
|
|
132
|
+
self.generic_visit(node)
|
|
133
|
+
op_type = type(node.op)
|
|
134
|
+
if op_type in self.BIN_SWAPS:
|
|
135
|
+
if self.current_index == self.target_index:
|
|
136
|
+
new_op_class = self.BIN_SWAPS[op_type]
|
|
137
|
+
orig_name = op_type.__name__
|
|
138
|
+
new_name = new_op_class.__name__
|
|
139
|
+
node.op = new_op_class()
|
|
140
|
+
self.applied_mutation = (
|
|
141
|
+
getattr(node, "lineno", 1),
|
|
142
|
+
"ArithmeticOperatorSwap",
|
|
143
|
+
orig_name,
|
|
144
|
+
new_name
|
|
145
|
+
)
|
|
146
|
+
self.current_index += 1
|
|
147
|
+
return node
|
|
148
|
+
|
|
149
|
+
def visit_Constant(self, node: ast.Constant) -> ast.AST:
|
|
150
|
+
if isinstance(node.value, bool):
|
|
151
|
+
if self.current_index == self.target_index:
|
|
152
|
+
orig_val = str(node.value)
|
|
153
|
+
node.value = not node.value
|
|
154
|
+
self.applied_mutation = (
|
|
155
|
+
getattr(node, "lineno", 1),
|
|
156
|
+
"BooleanConstantInversion",
|
|
157
|
+
orig_val,
|
|
158
|
+
str(node.value)
|
|
159
|
+
)
|
|
160
|
+
self.current_index += 1
|
|
161
|
+
return node
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class ASTMutationEngine:
|
|
165
|
+
"""Orchestrates mutation generation, test execution, and mutation score computation."""
|
|
166
|
+
|
|
167
|
+
@classmethod
|
|
168
|
+
def count_mutation_points(cls, code: str) -> int:
|
|
169
|
+
"""Counts total possible mutation opportunities in code."""
|
|
170
|
+
try:
|
|
171
|
+
tree = ast.parse(code)
|
|
172
|
+
except Exception:
|
|
173
|
+
return 0
|
|
174
|
+
transformer = ASTMutationTransformer(target_mutation_index=-1)
|
|
175
|
+
transformer.visit(tree)
|
|
176
|
+
return transformer.current_index
|
|
177
|
+
|
|
178
|
+
@classmethod
|
|
179
|
+
def generate_mutants(cls, code: str, max_mutants: int = 15) -> List[Mutant]:
|
|
180
|
+
"""Generates up to max_mutants distinct AST mutations."""
|
|
181
|
+
total_points = cls.count_mutation_points(code)
|
|
182
|
+
if total_points == 0:
|
|
183
|
+
return []
|
|
184
|
+
|
|
185
|
+
# Sample or iterate mutation indices
|
|
186
|
+
step = max(1, total_points // max_mutants)
|
|
187
|
+
indices = list(range(0, total_points, step))[:max_mutants]
|
|
188
|
+
|
|
189
|
+
mutants: List[Mutant] = []
|
|
190
|
+
for idx in indices:
|
|
191
|
+
try:
|
|
192
|
+
tree = ast.parse(code)
|
|
193
|
+
transformer = ASTMutationTransformer(target_mutation_index=idx)
|
|
194
|
+
mutated_tree = transformer.visit(tree)
|
|
195
|
+
ast.fix_missing_locations(mutated_tree)
|
|
196
|
+
|
|
197
|
+
if transformer.applied_mutation:
|
|
198
|
+
line, op_name, orig_str, mut_str = transformer.applied_mutation
|
|
199
|
+
mutated_code = ast.unparse(mutated_tree)
|
|
200
|
+
m = Mutant(
|
|
201
|
+
mutant_id=f"MUT_{idx+1:03d}",
|
|
202
|
+
line_no=line,
|
|
203
|
+
operator=op_name,
|
|
204
|
+
original_snippet=orig_str,
|
|
205
|
+
mutated_snippet=mut_str,
|
|
206
|
+
mutated_code=mutated_code
|
|
207
|
+
)
|
|
208
|
+
mutants.append(m)
|
|
209
|
+
except Exception:
|
|
210
|
+
continue
|
|
211
|
+
|
|
212
|
+
return mutants
|
|
213
|
+
|
|
214
|
+
@classmethod
|
|
215
|
+
def run_mutation_analysis(
|
|
216
|
+
cls,
|
|
217
|
+
target_file: str,
|
|
218
|
+
test_command: Optional[str] = None,
|
|
219
|
+
max_mutants: int = 10,
|
|
220
|
+
timeout_seconds: float = 6.0
|
|
221
|
+
) -> MutationReport:
|
|
222
|
+
"""Executes full mutation testing on target_file against test_command."""
|
|
223
|
+
start_time = time.time()
|
|
224
|
+
abs_target = os.path.abspath(target_file)
|
|
225
|
+
if not os.path.isfile(abs_target):
|
|
226
|
+
raise FileNotFoundError(f"Target file not found: {target_file}")
|
|
227
|
+
|
|
228
|
+
with open(abs_target, "r", encoding="utf-8", errors="replace") as f:
|
|
229
|
+
original_code = f.read()
|
|
230
|
+
|
|
231
|
+
mutants = cls.generate_mutants(original_code, max_mutants=max_mutants)
|
|
232
|
+
if not mutants:
|
|
233
|
+
return MutationReport(
|
|
234
|
+
file_path=target_file,
|
|
235
|
+
total_mutants=0,
|
|
236
|
+
killed=0,
|
|
237
|
+
survived=0,
|
|
238
|
+
timeouts=0,
|
|
239
|
+
errors=0,
|
|
240
|
+
mutation_score=100.0,
|
|
241
|
+
duration_seconds=round(time.time() - start_time, 2),
|
|
242
|
+
mutants=[]
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
# Infer test command if not provided
|
|
246
|
+
if not test_command:
|
|
247
|
+
base_name = os.path.splitext(os.path.basename(target_file))[0]
|
|
248
|
+
# Check for candidate test file
|
|
249
|
+
candidate_test = os.path.join(os.path.dirname(abs_target), "tests", f"test_{base_name}.py")
|
|
250
|
+
if not os.path.isfile(candidate_test):
|
|
251
|
+
candidate_test = os.path.join(".", "tests", f"test_{base_name}.py")
|
|
252
|
+
|
|
253
|
+
if os.path.isfile(candidate_test):
|
|
254
|
+
test_command = f"{sys.executable} -m unittest {candidate_test}"
|
|
255
|
+
else:
|
|
256
|
+
test_command = f"{sys.executable} -m unittest discover -s tests -p 'test_*.py'"
|
|
257
|
+
|
|
258
|
+
killed = 0
|
|
259
|
+
survived = 0
|
|
260
|
+
timeouts = 0
|
|
261
|
+
errors = 0
|
|
262
|
+
|
|
263
|
+
# Backup original content
|
|
264
|
+
backup_path = abs_target + ".tzero_mut_bak"
|
|
265
|
+
with open(backup_path, "w", encoding="utf-8") as f:
|
|
266
|
+
f.write(original_code)
|
|
267
|
+
|
|
268
|
+
env = os.environ.copy()
|
|
269
|
+
target_dir = os.path.dirname(abs_target)
|
|
270
|
+
existing_pythonpath = env.get("PYTHONPATH", "")
|
|
271
|
+
env["PYTHONPATH"] = f"{target_dir}{os.pathsep}{existing_pythonpath}" if existing_pythonpath else target_dir
|
|
272
|
+
|
|
273
|
+
try:
|
|
274
|
+
for m in mutants:
|
|
275
|
+
# Write mutant to disk
|
|
276
|
+
with open(abs_target, "w", encoding="utf-8") as f:
|
|
277
|
+
f.write(m.mutated_code)
|
|
278
|
+
|
|
279
|
+
try:
|
|
280
|
+
proc = subprocess.run(
|
|
281
|
+
test_command,
|
|
282
|
+
shell=True,
|
|
283
|
+
capture_output=True,
|
|
284
|
+
text=True,
|
|
285
|
+
timeout=timeout_seconds,
|
|
286
|
+
env=env,
|
|
287
|
+
cwd=target_dir
|
|
288
|
+
)
|
|
289
|
+
if proc.returncode != 0:
|
|
290
|
+
m.status = "KILLED"
|
|
291
|
+
killed += 1
|
|
292
|
+
m.output_snippet = (proc.stderr or proc.stdout).strip().splitlines()[-1] if (proc.stderr or proc.stdout) else "AssertionError"
|
|
293
|
+
else:
|
|
294
|
+
m.status = "SURVIVED"
|
|
295
|
+
survived += 1
|
|
296
|
+
except subprocess.TimeoutExpired:
|
|
297
|
+
m.status = "TIMEOUT"
|
|
298
|
+
timeouts += 1
|
|
299
|
+
killed += 1 # Infinite loop is considered caught
|
|
300
|
+
except Exception as ex:
|
|
301
|
+
m.status = "ERROR"
|
|
302
|
+
errors += 1
|
|
303
|
+
m.output_snippet = str(ex)
|
|
304
|
+
|
|
305
|
+
finally:
|
|
306
|
+
# Always restore original code cleanly
|
|
307
|
+
if os.path.isfile(backup_path):
|
|
308
|
+
with open(backup_path, "r", encoding="utf-8") as f:
|
|
309
|
+
restored = f.read()
|
|
310
|
+
with open(abs_target, "w", encoding="utf-8") as f:
|
|
311
|
+
f.write(restored)
|
|
312
|
+
os.remove(backup_path)
|
|
313
|
+
|
|
314
|
+
total = len(mutants)
|
|
315
|
+
score = round((killed / total) * 100.0, 1) if total > 0 else 100.0
|
|
316
|
+
duration = round(time.time() - start_time, 2)
|
|
317
|
+
|
|
318
|
+
return MutationReport(
|
|
319
|
+
file_path=target_file,
|
|
320
|
+
total_mutants=total,
|
|
321
|
+
killed=killed,
|
|
322
|
+
survived=survived,
|
|
323
|
+
timeouts=timeouts,
|
|
324
|
+
errors=errors,
|
|
325
|
+
mutation_score=score,
|
|
326
|
+
duration_seconds=duration,
|
|
327
|
+
mutants=mutants
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
@classmethod
|
|
331
|
+
def format_markdown_report(cls, report: MutationReport) -> str:
|
|
332
|
+
"""Formats the mutation analysis into clean GitHub-style Markdown."""
|
|
333
|
+
score_badge = "🟢" if report.mutation_score >= 80 else ("🟡" if report.mutation_score >= 50 else "🔴")
|
|
334
|
+
lines = [
|
|
335
|
+
f"## 🧬 T-Zero AST Mutation & Test Quality Report",
|
|
336
|
+
f"**Target File:** `{report.file_path}`",
|
|
337
|
+
f"**Mutation Score:** {score_badge} **{report.mutation_score}%** ({report.killed}/{report.total_mutants} mutants killed)",
|
|
338
|
+
f"**Duration:** `{report.duration_seconds}s` | **Surviving Mutants:** `{report.survived}` | **Timeouts:** `{report.timeouts}`",
|
|
339
|
+
"",
|
|
340
|
+
"| ID | Line | Operator | Mutation | Result |",
|
|
341
|
+
"| :--- | :---: | :--- | :--- | :--- |"
|
|
342
|
+
]
|
|
343
|
+
|
|
344
|
+
for m in report.mutants:
|
|
345
|
+
res_icon = "🗡️ KILLED" if m.status in ("KILLED", "TIMEOUT") else "⚠️ SURVIVED"
|
|
346
|
+
lines.append(
|
|
347
|
+
f"| `{m.mutant_id}` | L{m.line_no} | {m.operator} | `{m.original_snippet}` -> `{m.mutated_snippet}` | {res_icon} |"
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
if report.survived > 0:
|
|
351
|
+
lines.extend([
|
|
352
|
+
"",
|
|
353
|
+
"### ⚠️ Recommendation for Surviving Mutants",
|
|
354
|
+
"Surviving mutants indicate code branches that lack assertions or are tautological.",
|
|
355
|
+
"Review the surviving lines above and add unit tests specifically asserting those conditions."
|
|
356
|
+
])
|
|
357
|
+
|
|
358
|
+
return "\n".join(lines)
|