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.
Files changed (127) hide show
  1. package/CHANGELOG.md +44 -577
  2. package/CONTRIBUTING.md +71 -71
  3. package/README.md +64 -64
  4. package/bin/tzero-mcp.js +62 -65
  5. package/bin/tzero.js +61 -64
  6. package/index.d.ts +25 -25
  7. package/index.js +15 -15
  8. package/package.json +88 -67
  9. package/src/addmcp.py +14 -11
  10. package/src/app.py +1810 -0
  11. package/src/main.py +1 -1
  12. package/src/pc_fixer_mcp.py +25 -0
  13. package/src/tzero/__init__.py +54 -0
  14. package/src/tzero/__main__.py +6 -0
  15. package/src/tzero/siber_akademi.ico +0 -0
  16. package/src/tzero/tzero_arch_test.py +463 -0
  17. package/src/{tzero_benchmark.py → tzero/tzero_benchmark.py} +475 -475
  18. package/src/tzero/tzero_blackboard.py +257 -0
  19. package/src/{tzero_bridge.py → tzero/tzero_bridge.py} +546 -362
  20. package/src/tzero/tzero_budget.py +195 -0
  21. package/src/{tzero_ci.py → tzero/tzero_ci.py} +153 -153
  22. package/src/{tzero_commit.py → tzero/tzero_commit.py} +192 -192
  23. package/src/tzero/tzero_contracts.py +1093 -0
  24. package/src/{tzero_cost.py → tzero/tzero_cost.py} +166 -163
  25. package/src/{tzero_dashboard.py → tzero/tzero_dashboard.py} +531 -19
  26. package/src/tzero/tzero_debt.py +461 -0
  27. package/src/{tzero_deps.py → tzero/tzero_deps.py} +179 -4
  28. package/src/{tzero_drift.py → tzero/tzero_drift.py} +208 -208
  29. package/src/{tzero_explain.py → tzero/tzero_explain.py} +73 -7
  30. package/src/{tzero_features.py → tzero/tzero_features.py} +19 -17
  31. package/src/tzero/tzero_federation.py +299 -0
  32. package/src/{tzero_fix.py → tzero/tzero_fix.py} +228 -228
  33. package/src/{tzero_fixer → tzero/tzero_fixer}/__init__.py +63 -63
  34. package/src/{tzero_fixer → tzero/tzero_fixer}/ast_syntax_healer.py +464 -464
  35. package/src/{tzero_fixer → tzero/tzero_fixer}/ast_type_modernizer.py +288 -288
  36. package/src/{tzero_fixer → tzero/tzero_fixer}/async_io_modernizer.py +127 -127
  37. package/src/{tzero_fixer → tzero/tzero_fixer}/automated_regression_suite.py +600 -600
  38. package/src/{tzero_fixer → tzero/tzero_fixer}/benchmarks_and_diagnostics.py +174 -174
  39. package/src/{tzero_fixer → tzero/tzero_fixer}/ci_cd_self_healing_gate.py +246 -246
  40. package/src/{tzero_fixer → tzero/tzero_fixer}/code_duplicity_and_refactorer.py +151 -151
  41. package/src/{tzero_fixer → tzero/tzero_fixer}/code_smells_healer.py +260 -260
  42. package/src/{tzero_fixer → tzero/tzero_fixer}/cwe_remediator.py +171 -171
  43. package/src/{tzero_fixer → tzero/tzero_fixer}/dead_code_eliminator.py +150 -150
  44. package/src/{tzero_fixer → tzero/tzero_fixer}/dependency_vulnerability_auditor.py +309 -309
  45. package/src/{tzero_fixer → tzero/tzero_fixer}/git_advanced_surgeon.py +1 -1
  46. package/src/{tzero_fixer → tzero/tzero_fixer}/hardware_and_disk_doctor.py +1 -1
  47. package/src/{tzero_fixer → tzero/tzero_fixer}/import_graph_healer.py +412 -412
  48. package/src/{tzero_fixer → tzero/tzero_fixer}/interactive_tui.py +91 -91
  49. package/src/{tzero_fixer → tzero/tzero_fixer}/knowledge_base.py +295 -295
  50. package/src/{tzero_fixer → tzero/tzero_fixer}/knowledge_base_extended.py +940 -940
  51. package/src/{tzero_fixer → tzero/tzero_fixer}/linters_and_static_analyzer.py +307 -307
  52. package/src/{tzero_fixer → tzero/tzero_fixer}/mcp_server.py +49 -7
  53. package/src/{tzero_fixer → tzero/tzero_fixer}/os_doctor.py +502 -502
  54. package/src/{tzero_fixer → tzero/tzero_fixer}/packaging_abi_surgeon.py +1 -1
  55. package/src/{tzero_fixer → tzero/tzero_fixer}/pc_fixer_doctor.py +600 -599
  56. package/src/{tzero_fixer → tzero/tzero_fixer}/performance_and_memory_profiler.py +274 -274
  57. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_ast_parsers.py +313 -313
  58. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_build_healer.py +1 -1
  59. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_config_healer.py +318 -318
  60. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_doctor.py +372 -372
  61. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_linters.py +1 -1
  62. package/src/{tzero_fixer → tzero/tzero_fixer}/polyglot_schema_validator.py +305 -305
  63. package/src/{tzero_fixer → tzero/tzero_fixer}/security_cwe_database.py +444 -444
  64. package/src/{tzero_fixer → tzero/tzero_fixer}/security_deep_rules.py +114 -114
  65. package/src/{tzero_fixer → tzero/tzero_fixer}/security_rules_catalog.py +110 -110
  66. package/src/{tzero_fixer → tzero/tzero_fixer}/security_zero_leak.py +326 -326
  67. package/src/{tzero_fixer → tzero/tzero_fixer}/smart_patch_recommender.py +223 -223
  68. package/src/{tzero_fixer → tzero/tzero_fixer}/synthetic_workspace_generator.py +220 -220
  69. package/src/{tzero_fixer → tzero/tzero_fixer}/system_diagnostics_extended.py +1 -1
  70. package/src/{tzero_fixer → tzero/tzero_fixer}/system_repair_recipes.py +447 -447
  71. package/src/{tzero_fixer → tzero/tzero_fixer}/venv_doctor.py +309 -309
  72. package/src/tzero/tzero_golden.py +319 -0
  73. package/src/tzero/tzero_graph_hud.html +338 -0
  74. package/src/tzero/tzero_graph_rag.py +263 -0
  75. package/src/{tzero_guard.py → tzero/tzero_guard.py} +243 -243
  76. package/src/tzero/tzero_hooks.py +565 -0
  77. package/src/{tzero_infra.py → tzero/tzero_infra.py} +274 -274
  78. package/src/{tzero_jarvis → tzero/tzero_jarvis}/__init__.py +124 -124
  79. package/src/{tzero_jarvis → tzero/tzero_jarvis}/causal_graph.py +310 -310
  80. package/src/{tzero_jarvis → tzero/tzero_jarvis}/graph_visualizer.py +395 -395
  81. package/src/{tzero_jarvis → tzero/tzero_jarvis}/hybrid_search.py +214 -214
  82. package/src/{tzero_jarvis → tzero/tzero_jarvis}/latent_press.py +137 -137
  83. package/src/{tzero_jarvis → tzero/tzero_jarvis}/memory_consolidation.py +212 -212
  84. package/src/{tzero_jarvis → tzero/tzero_jarvis}/minhash_lsh.py +184 -184
  85. package/src/{tzero_jarvis → tzero/tzero_jarvis}/orchestrator.py +335 -335
  86. package/src/{tzero_jarvis → tzero/tzero_jarvis}/self_healing.py +308 -308
  87. package/src/{tzero_jarvis → tzero/tzero_jarvis}/semantic_compressor.py +887 -887
  88. package/src/{tzero_jarvis → tzero/tzero_jarvis}/swarm.py +261 -261
  89. package/src/{tzero_jarvis → tzero/tzero_jarvis}/swarm_bft.py +144 -144
  90. package/src/{tzero_jarvis → tzero/tzero_jarvis}/workspace_orchestrator.py +1167 -1167
  91. package/src/{tzero_jarvis → tzero/tzero_jarvis}/zero_key_router.py +173 -173
  92. package/src/tzero/tzero_lsp.py +634 -0
  93. package/src/{tzero_mcp.py → tzero/tzero_mcp.py} +479 -100
  94. package/src/tzero/tzero_mutation.py +358 -0
  95. package/src/tzero/tzero_packer.py +577 -0
  96. package/src/{tzero_patch.py → tzero/tzero_patch.py} +624 -624
  97. package/src/{tzero_pc_fixer.py → tzero/tzero_pc_fixer.py} +12699 -12657
  98. package/src/{tzero_pipe.py → tzero/tzero_pipe.py} +95 -95
  99. package/src/tzero/tzero_polyglot.py +799 -0
  100. package/src/tzero/tzero_proxy.py +127 -0
  101. package/src/{tzero_reachability.py → tzero/tzero_reachability.py} +272 -272
  102. package/src/{tzero_replay.py → tzero/tzero_replay.py} +141 -141
  103. package/src/tzero/tzero_rust_bridge.py +198 -0
  104. package/src/tzero/tzero_scip.py +368 -0
  105. package/src/{tzero_security.py → tzero/tzero_security.py} +5 -2
  106. package/src/tzero/tzero_semantic_merge.py +189 -0
  107. package/src/tzero/tzero_snapshot.py +471 -0
  108. package/src/{tzero_stubs.py → tzero/tzero_stubs.py} +313 -313
  109. package/src/tzero/tzero_subagent_engine.py +459 -0
  110. package/src/{tzero_superdesign.py → tzero/tzero_superdesign.py} +774 -774
  111. package/src/{tzero_sync.py → tzero/tzero_sync.py} +241 -241
  112. package/src/{tzero_task_benchmark.py → tzero/tzero_task_benchmark.py} +458 -363
  113. package/src/{tzero_tui.py → tzero/tzero_tui.py} +91 -22
  114. package/src/tzero/tzero_turbo.py +264 -0
  115. package/src/{tzero_v3.py → tzero/tzero_v3.py} +628 -163
  116. package/src/tzero.py +2 -54
  117. package/test/package.test.js +55 -19
  118. package/src/tzero/jarvis/semantic_compressor.py +0 -373
  119. package/src/tzero_gateway.py +0 -885
  120. package/src/tzero_packer.py +0 -370
  121. package/src/tzero_polyglot.py +0 -422
  122. package/src/tzero_snapshot.py +0 -258
  123. /package/src/{siber_akademi_icon.py → tzero/siber_akademi_icon.py} +0 -0
  124. /package/src/{tzero_fixer → tzero/tzero_fixer}/amalgamation.py +0 -0
  125. /package/src/{tzero_fixer → tzero/tzero_fixer}/models.py +0 -0
  126. /package/src/{tzero_fixer → tzero/tzero_fixer}/orchestrator.py +0 -0
  127. /package/src/{tzero_neural_cortex.py → tzero/tzero_neural_cortex.py} +0 -0
@@ -1,272 +1,272 @@
1
- # -*- coding: utf-8 -*-
2
- """T-Zero Cross-Module AST Call-Graph Reachability & Dead-Code Pruning Engine.
3
-
4
- Performs static AST call-graph reachability analysis across multi-file codebases,
5
- discovering reachable symbols from specified entrypoints and pruning unreferenced
6
- functions and dead classes to assemble ultra-compact, Zero-Waste LLM prompt contexts.
7
-
8
- Developer: Toprak Ahmet Aydoğmuş (Siber Akademi)
9
- Part of T-Zero Context Architect v7.0.0 Master Edition.
10
- """
11
-
12
- import ast
13
- import os
14
- import re
15
- from typing import Dict, List, Set, Any, Optional
16
-
17
-
18
- class SymbolNode:
19
- """Represents a callable symbol (function or class) in the codebase AST."""
20
- def __init__(self, name: str, file_path: str, start_line: int, end_line: int, code: str, is_class: bool = False):
21
- self.name = name
22
- self.file_path = file_path
23
- self.start_line = start_line
24
- self.end_line = end_line
25
- self.code = code
26
- self.is_class = is_class
27
- self.calls: Set[str] = set()
28
-
29
- @property
30
- def symbol_id(self) -> str:
31
- return f"{self.file_path}:{self.name}"
32
-
33
- def to_dict(self) -> Dict[str, Any]:
34
- return {
35
- "id": self.symbol_id,
36
- "name": self.name,
37
- "file": self.file_path,
38
- "start_line": self.start_line,
39
- "end_line": self.end_line,
40
- "is_class": self.is_class,
41
- "calls": sorted(list(self.calls))
42
- }
43
-
44
-
45
- class CallExtractorVisitor(ast.NodeVisitor):
46
- """AST Visitor to extract invoked function and class names within a symbol body."""
47
- def __init__(self):
48
- self.invoked_names: Set[str] = set()
49
-
50
- def visit_Call(self, node: ast.Call):
51
- if isinstance(node.func, ast.Name):
52
- self.invoked_names.add(node.func.id)
53
- elif isinstance(node.func, ast.Attribute):
54
- self.invoked_names.add(node.func.attr)
55
- self.generic_visit(node)
56
-
57
-
58
- class CallGraphReachabilityEngine:
59
- """Enterprise AST graph reachability engine for zero-waste LLM prompt synthesis."""
60
-
61
- IGNORE_DIRS = {
62
- ".git", "__pycache__", "venv", ".venv", "env", "node_modules",
63
- "build", "dist", ".gemini", ".idea", ".vscode", "crates", "assets"
64
- }
65
-
66
- def __init__(self, workspace_root: str = "."):
67
- self.workspace_root = os.path.abspath(workspace_root)
68
- self.symbols: Dict[str, SymbolNode] = {} # symbol_id -> SymbolNode
69
- self.name_to_ids: Dict[str, List[str]] = {} # symbol_name -> list of symbol_ids
70
- self.file_contents: Dict[str, str] = {}
71
- self.module_top_level_calls: Dict[str, Set[str]] = {}
72
-
73
- def scan_workspace(self):
74
- """Discovers and parses all Python files in the workspace root."""
75
- self.symbols.clear()
76
- self.name_to_ids.clear()
77
- self.file_contents.clear()
78
- self.module_top_level_calls.clear()
79
-
80
- for root, dirs, files in os.walk(self.workspace_root):
81
- dirs[:] = [d for d in dirs if d not in self.IGNORE_DIRS]
82
- for file in files:
83
- if file.endswith(".py"):
84
- full_p = os.path.join(root, file)
85
- rel_p = os.path.relpath(full_p, self.workspace_root).replace("\\", "/")
86
- try:
87
- with open(full_p, "r", encoding="utf-8", errors="replace") as f:
88
- content = f.read()
89
- self.file_contents[rel_p] = content
90
- self._parse_file(rel_p, content)
91
- except Exception:
92
- continue
93
-
94
- def _parse_file(self, rel_path: str, content: str):
95
- """Parses a single Python file into AST and indexes classes, functions, and top-level calls."""
96
- try:
97
- tree = ast.parse(content)
98
- except Exception:
99
- return
100
-
101
- lines = content.splitlines()
102
- top_calls: Set[str] = set()
103
-
104
- for node in tree.body:
105
- if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
106
- name = node.name
107
- start = getattr(node, "lineno", 1)
108
- end = getattr(node, "end_lineno", start)
109
- code_snippet = "\n".join(lines[start - 1:end])
110
-
111
- is_cls = isinstance(node, ast.ClassDef)
112
- sym = SymbolNode(name, rel_path, start, end, code_snippet, is_class=is_cls)
113
-
114
- # Extract calls inside this function/class
115
- extractor = CallExtractorVisitor()
116
- extractor.visit(node)
117
- sym.calls = extractor.invoked_names
118
-
119
- self.symbols[sym.symbol_id] = sym
120
- self.name_to_ids.setdefault(name, []).append(sym.symbol_id)
121
- elif isinstance(node, ast.If):
122
- # Check for if __name__ == "__main__":
123
- if self._is_main_guard(node):
124
- extractor = CallExtractorVisitor()
125
- extractor.visit(node)
126
- top_calls.update(extractor.invoked_names)
127
- elif isinstance(node, ast.Expr) and isinstance(node.value, ast.Call):
128
- extractor = CallExtractorVisitor()
129
- extractor.visit(node)
130
- top_calls.update(extractor.invoked_names)
131
-
132
- self.module_top_level_calls[rel_path] = top_calls
133
-
134
- @staticmethod
135
- def _is_main_guard(node: ast.If) -> bool:
136
- """Checks if an AST If block represents if __name__ == '__main__':."""
137
- try:
138
- test = node.test
139
- if isinstance(test, ast.Compare):
140
- left = test.left
141
- if isinstance(left, ast.Name) and left.id == "__name__":
142
- for comparator in test.comparators:
143
- if isinstance(comparator, ast.Constant) and comparator.value == "__main__":
144
- return True
145
- except Exception:
146
- pass
147
- return False
148
-
149
- def build_reachability_graph(self, entrypoints: Optional[List[str]] = None) -> Dict[str, Any]:
150
- """Calculates reachable vs dead symbols using BFS graph traversal."""
151
- if not self.symbols:
152
- self.scan_workspace()
153
-
154
- entry_paths: Set[str] = set()
155
- if entrypoints:
156
- for ep in entrypoints:
157
- clean_ep = os.path.relpath(os.path.join(self.workspace_root, ep), self.workspace_root).replace("\\", "/")
158
- entry_paths.add(clean_ep)
159
- else:
160
- # Default to standard common entrypoints
161
- candidates = ["main.py", "app.py", "cli.py", "run.py", "tzero_v3.py"]
162
- for c in candidates:
163
- if c in self.file_contents:
164
- entry_paths.add(c)
165
- if not entry_paths and self.file_contents:
166
- # Pick first file
167
- entry_paths.add(sorted(list(self.file_contents.keys()))[0])
168
-
169
- # Seed queue with entrypoint roots
170
- queue: List[str] = []
171
- visited_ids: Set[str] = set()
172
-
173
- # Add top-level called symbols and main functions from entrypoints
174
- for ep in entry_paths:
175
- top_calls = self.module_top_level_calls.get(ep, set())
176
- for call_name in top_calls:
177
- for target_id in self.name_to_ids.get(call_name, []):
178
- if target_id not in visited_ids:
179
- visited_ids.add(target_id)
180
- queue.append(target_id)
181
-
182
- # Also add any function named 'main', 'run', 'cli' in the entrypoint file
183
- for sym_id, sym in self.symbols.items():
184
- if sym.file_path == ep and sym.name in {"main", "run", "cli", "app"}:
185
- if sym_id not in visited_ids:
186
- visited_ids.add(sym_id)
187
- queue.append(sym_id)
188
-
189
- # Fallback if no specific entrypoint call found: mark all top functions in entrypoints
190
- if not queue:
191
- for sym_id, sym in self.symbols.items():
192
- if sym.file_path in entry_paths:
193
- if sym_id not in visited_ids:
194
- visited_ids.add(sym_id)
195
- queue.append(sym_id)
196
-
197
- # Graph BFS traversal
198
- while queue:
199
- curr_id = queue.pop(0)
200
- curr_sym = self.symbols.get(curr_id)
201
- if not curr_sym:
202
- continue
203
-
204
- for call_name in curr_sym.calls:
205
- target_ids = self.name_to_ids.get(call_name, [])
206
- for t_id in target_ids:
207
- if t_id not in visited_ids:
208
- visited_ids.add(t_id)
209
- queue.append(t_id)
210
-
211
- reachable_symbols = [self.symbols[sid] for sid in visited_ids if sid in self.symbols]
212
- dead_symbols = [sym for sid, sym in self.symbols.items() if sid not in visited_ids]
213
-
214
- return {
215
- "entrypoints": sorted(list(entry_paths)),
216
- "reachable": reachable_symbols,
217
- "dead": dead_symbols
218
- }
219
-
220
- def generate_zero_waste_prompt(self, entrypoints: Optional[List[str]] = None) -> Dict[str, Any]:
221
- """Assembles a streamlined prompt context containing strictly reachable code."""
222
- graph = self.build_reachability_graph(entrypoints)
223
- reachable: List[SymbolNode] = graph["reachable"]
224
- dead: List[SymbolNode] = graph["dead"]
225
-
226
- # Calculate original raw characters vs pruned
227
- raw_chars = sum(len(c) for c in self.file_contents.values())
228
-
229
- # Group reachable symbols by file
230
- file_to_nodes: Dict[str, List[SymbolNode]] = {}
231
- for sym in reachable:
232
- file_to_nodes.setdefault(sym.file_path, []).append(sym)
233
-
234
- # Build clean pruned context
235
- prompt_lines = [
236
- "# ===================================================================",
237
- "# ⚡ T-ZERO ZERO-WASTE PROMPT CONTEXT (PRUNED REACHABILITY GRAPH)",
238
- f"# Entrypoints: {', '.join(graph['entrypoints'])}",
239
- f"# Active symbols: {len(reachable)} | Dead symbols pruned: {len(dead)}",
240
- "# ===================================================================\n"
241
- ]
242
-
243
- for file_p in sorted(file_to_nodes.keys()):
244
- nodes = sorted(file_to_nodes[file_p], key=lambda x: x.start_line)
245
- prompt_lines.append(f"## File: {file_p}")
246
- prompt_lines.append("```python")
247
- for node in nodes:
248
- prompt_lines.append(f"# [{node.file_path}:{node.name} L{node.start_line}-{node.end_line}]")
249
- prompt_lines.append(node.code)
250
- prompt_lines.append("")
251
- prompt_lines.append("```\n")
252
-
253
- zero_waste_prompt = "\n".join(prompt_lines)
254
-
255
- # Calculate code symbol tokens (raw vs pruned)
256
- raw_code_chars = sum(len(sym.code) for sym in reachable) + sum(len(sym.code) for sym in dead)
257
- pruned_code_chars = sum(len(sym.code) for sym in reachable)
258
- raw_tokens = max(1, raw_code_chars // 4)
259
- pruned_tokens = max(1, pruned_code_chars // 4)
260
- reduction_pct = round(max(0.0, (1.0 - (pruned_code_chars / max(1, raw_code_chars))) * 100), 2)
261
-
262
- return {
263
- "entrypoints": graph["entrypoints"],
264
- "reachable_symbols": [s.to_dict() for s in reachable],
265
- "dead_symbols": [s.to_dict() for s in dead],
266
- "reachable_count": len(reachable),
267
- "pruned_dead_count": len(dead),
268
- "raw_tokens": raw_tokens,
269
- "zero_waste_tokens": pruned_tokens,
270
- "token_reduction_pct": reduction_pct,
271
- "zero_waste_prompt": zero_waste_prompt
272
- }
1
+ # -*- coding: utf-8 -*-
2
+ """T-Zero Cross-Module AST Call-Graph Reachability & Dead-Code Pruning Engine.
3
+
4
+ Performs static AST call-graph reachability analysis across multi-file codebases,
5
+ discovering reachable symbols from specified entrypoints and pruning unreferenced
6
+ functions and dead classes to assemble ultra-compact, Zero-Waste LLM prompt contexts.
7
+
8
+ Developer: Toprak Ahmet Aydoğmuş (Siber Akademi)
9
+ Part of T-Zero Context Architect v7.0.0 Master Edition.
10
+ """
11
+
12
+ import ast
13
+ import os
14
+ import re
15
+ from typing import Dict, List, Set, Any, Optional
16
+
17
+
18
+ class SymbolNode:
19
+ """Represents a callable symbol (function or class) in the codebase AST."""
20
+ def __init__(self, name: str, file_path: str, start_line: int, end_line: int, code: str, is_class: bool = False):
21
+ self.name = name
22
+ self.file_path = file_path
23
+ self.start_line = start_line
24
+ self.end_line = end_line
25
+ self.code = code
26
+ self.is_class = is_class
27
+ self.calls: Set[str] = set()
28
+
29
+ @property
30
+ def symbol_id(self) -> str:
31
+ return f"{self.file_path}:{self.name}"
32
+
33
+ def to_dict(self) -> Dict[str, Any]:
34
+ return {
35
+ "id": self.symbol_id,
36
+ "name": self.name,
37
+ "file": self.file_path,
38
+ "start_line": self.start_line,
39
+ "end_line": self.end_line,
40
+ "is_class": self.is_class,
41
+ "calls": sorted(list(self.calls))
42
+ }
43
+
44
+
45
+ class CallExtractorVisitor(ast.NodeVisitor):
46
+ """AST Visitor to extract invoked function and class names within a symbol body."""
47
+ def __init__(self):
48
+ self.invoked_names: Set[str] = set()
49
+
50
+ def visit_Call(self, node: ast.Call):
51
+ if isinstance(node.func, ast.Name):
52
+ self.invoked_names.add(node.func.id)
53
+ elif isinstance(node.func, ast.Attribute):
54
+ self.invoked_names.add(node.func.attr)
55
+ self.generic_visit(node)
56
+
57
+
58
+ class CallGraphReachabilityEngine:
59
+ """Enterprise AST graph reachability engine for zero-waste LLM prompt synthesis."""
60
+
61
+ IGNORE_DIRS = {
62
+ ".git", "__pycache__", "venv", ".venv", "env", "node_modules",
63
+ "build", "dist", ".gemini", ".idea", ".vscode", "crates", "assets"
64
+ }
65
+
66
+ def __init__(self, workspace_root: str = "."):
67
+ self.workspace_root = os.path.abspath(workspace_root)
68
+ self.symbols: Dict[str, SymbolNode] = {} # symbol_id -> SymbolNode
69
+ self.name_to_ids: Dict[str, List[str]] = {} # symbol_name -> list of symbol_ids
70
+ self.file_contents: Dict[str, str] = {}
71
+ self.module_top_level_calls: Dict[str, Set[str]] = {}
72
+
73
+ def scan_workspace(self):
74
+ """Discovers and parses all Python files in the workspace root."""
75
+ self.symbols.clear()
76
+ self.name_to_ids.clear()
77
+ self.file_contents.clear()
78
+ self.module_top_level_calls.clear()
79
+
80
+ for root, dirs, files in os.walk(self.workspace_root):
81
+ dirs[:] = [d for d in dirs if d not in self.IGNORE_DIRS]
82
+ for file in files:
83
+ if file.endswith(".py"):
84
+ full_p = os.path.join(root, file)
85
+ rel_p = os.path.relpath(full_p, self.workspace_root).replace("\\", "/")
86
+ try:
87
+ with open(full_p, "r", encoding="utf-8", errors="replace") as f:
88
+ content = f.read()
89
+ self.file_contents[rel_p] = content
90
+ self._parse_file(rel_p, content)
91
+ except Exception:
92
+ continue
93
+
94
+ def _parse_file(self, rel_path: str, content: str):
95
+ """Parses a single Python file into AST and indexes classes, functions, and top-level calls."""
96
+ try:
97
+ tree = ast.parse(content)
98
+ except Exception:
99
+ return
100
+
101
+ lines = content.splitlines()
102
+ top_calls: Set[str] = set()
103
+
104
+ for node in tree.body:
105
+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
106
+ name = node.name
107
+ start = getattr(node, "lineno", 1)
108
+ end = getattr(node, "end_lineno", start)
109
+ code_snippet = "\n".join(lines[start - 1:end])
110
+
111
+ is_cls = isinstance(node, ast.ClassDef)
112
+ sym = SymbolNode(name, rel_path, start, end, code_snippet, is_class=is_cls)
113
+
114
+ # Extract calls inside this function/class
115
+ extractor = CallExtractorVisitor()
116
+ extractor.visit(node)
117
+ sym.calls = extractor.invoked_names
118
+
119
+ self.symbols[sym.symbol_id] = sym
120
+ self.name_to_ids.setdefault(name, []).append(sym.symbol_id)
121
+ elif isinstance(node, ast.If):
122
+ # Check for if __name__ == "__main__":
123
+ if self._is_main_guard(node):
124
+ extractor = CallExtractorVisitor()
125
+ extractor.visit(node)
126
+ top_calls.update(extractor.invoked_names)
127
+ elif isinstance(node, ast.Expr) and isinstance(node.value, ast.Call):
128
+ extractor = CallExtractorVisitor()
129
+ extractor.visit(node)
130
+ top_calls.update(extractor.invoked_names)
131
+
132
+ self.module_top_level_calls[rel_path] = top_calls
133
+
134
+ @staticmethod
135
+ def _is_main_guard(node: ast.If) -> bool:
136
+ """Checks if an AST If block represents if __name__ == '__main__':."""
137
+ try:
138
+ test = node.test
139
+ if isinstance(test, ast.Compare):
140
+ left = test.left
141
+ if isinstance(left, ast.Name) and left.id == "__name__":
142
+ for comparator in test.comparators:
143
+ if isinstance(comparator, ast.Constant) and comparator.value == "__main__":
144
+ return True
145
+ except Exception:
146
+ pass
147
+ return False
148
+
149
+ def build_reachability_graph(self, entrypoints: Optional[List[str]] = None) -> Dict[str, Any]:
150
+ """Calculates reachable vs dead symbols using BFS graph traversal."""
151
+ if not self.symbols:
152
+ self.scan_workspace()
153
+
154
+ entry_paths: Set[str] = set()
155
+ if entrypoints:
156
+ for ep in entrypoints:
157
+ clean_ep = os.path.relpath(os.path.join(self.workspace_root, ep), self.workspace_root).replace("\\", "/")
158
+ entry_paths.add(clean_ep)
159
+ else:
160
+ # Default to standard common entrypoints
161
+ candidates = ["main.py", "app.py", "cli.py", "run.py", "tzero_v3.py"]
162
+ for c in candidates:
163
+ if c in self.file_contents:
164
+ entry_paths.add(c)
165
+ if not entry_paths and self.file_contents:
166
+ # Pick first file
167
+ entry_paths.add(sorted(list(self.file_contents.keys()))[0])
168
+
169
+ # Seed queue with entrypoint roots
170
+ queue: List[str] = []
171
+ visited_ids: Set[str] = set()
172
+
173
+ # Add top-level called symbols and main functions from entrypoints
174
+ for ep in entry_paths:
175
+ top_calls = self.module_top_level_calls.get(ep, set())
176
+ for call_name in top_calls:
177
+ for target_id in self.name_to_ids.get(call_name, []):
178
+ if target_id not in visited_ids:
179
+ visited_ids.add(target_id)
180
+ queue.append(target_id)
181
+
182
+ # Also add any function named 'main', 'run', 'cli' in the entrypoint file
183
+ for sym_id, sym in self.symbols.items():
184
+ if sym.file_path == ep and sym.name in {"main", "run", "cli", "app"}:
185
+ if sym_id not in visited_ids:
186
+ visited_ids.add(sym_id)
187
+ queue.append(sym_id)
188
+
189
+ # Fallback if no specific entrypoint call found: mark all top functions in entrypoints
190
+ if not queue:
191
+ for sym_id, sym in self.symbols.items():
192
+ if sym.file_path in entry_paths:
193
+ if sym_id not in visited_ids:
194
+ visited_ids.add(sym_id)
195
+ queue.append(sym_id)
196
+
197
+ # Graph BFS traversal
198
+ while queue:
199
+ curr_id = queue.pop(0)
200
+ curr_sym = self.symbols.get(curr_id)
201
+ if not curr_sym:
202
+ continue
203
+
204
+ for call_name in curr_sym.calls:
205
+ target_ids = self.name_to_ids.get(call_name, [])
206
+ for t_id in target_ids:
207
+ if t_id not in visited_ids:
208
+ visited_ids.add(t_id)
209
+ queue.append(t_id)
210
+
211
+ reachable_symbols = [self.symbols[sid] for sid in visited_ids if sid in self.symbols]
212
+ dead_symbols = [sym for sid, sym in self.symbols.items() if sid not in visited_ids]
213
+
214
+ return {
215
+ "entrypoints": sorted(list(entry_paths)),
216
+ "reachable": reachable_symbols,
217
+ "dead": dead_symbols
218
+ }
219
+
220
+ def generate_zero_waste_prompt(self, entrypoints: Optional[List[str]] = None) -> Dict[str, Any]:
221
+ """Assembles a streamlined prompt context containing strictly reachable code."""
222
+ graph = self.build_reachability_graph(entrypoints)
223
+ reachable: List[SymbolNode] = graph["reachable"]
224
+ dead: List[SymbolNode] = graph["dead"]
225
+
226
+ # Calculate original raw characters vs pruned
227
+ raw_chars = sum(len(c) for c in self.file_contents.values())
228
+
229
+ # Group reachable symbols by file
230
+ file_to_nodes: Dict[str, List[SymbolNode]] = {}
231
+ for sym in reachable:
232
+ file_to_nodes.setdefault(sym.file_path, []).append(sym)
233
+
234
+ # Build clean pruned context
235
+ prompt_lines = [
236
+ "# ===================================================================",
237
+ "# ⚡ T-ZERO ZERO-WASTE PROMPT CONTEXT (PRUNED REACHABILITY GRAPH)",
238
+ f"# Entrypoints: {', '.join(graph['entrypoints'])}",
239
+ f"# Active symbols: {len(reachable)} | Dead symbols pruned: {len(dead)}",
240
+ "# ===================================================================\n"
241
+ ]
242
+
243
+ for file_p in sorted(file_to_nodes.keys()):
244
+ nodes = sorted(file_to_nodes[file_p], key=lambda x: x.start_line)
245
+ prompt_lines.append(f"## File: {file_p}")
246
+ prompt_lines.append("```python")
247
+ for node in nodes:
248
+ prompt_lines.append(f"# [{node.file_path}:{node.name} L{node.start_line}-{node.end_line}]")
249
+ prompt_lines.append(node.code)
250
+ prompt_lines.append("")
251
+ prompt_lines.append("```\n")
252
+
253
+ zero_waste_prompt = "\n".join(prompt_lines)
254
+
255
+ # Calculate code symbol tokens (raw vs pruned)
256
+ raw_code_chars = sum(len(sym.code) for sym in reachable) + sum(len(sym.code) for sym in dead)
257
+ pruned_code_chars = sum(len(sym.code) for sym in reachable)
258
+ raw_tokens = max(1, raw_code_chars // 4)
259
+ pruned_tokens = max(1, pruned_code_chars // 4)
260
+ reduction_pct = round(max(0.0, (1.0 - (pruned_code_chars / max(1, raw_code_chars))) * 100), 2)
261
+
262
+ return {
263
+ "entrypoints": graph["entrypoints"],
264
+ "reachable_symbols": [s.to_dict() for s in reachable],
265
+ "dead_symbols": [s.to_dict() for s in dead],
266
+ "reachable_count": len(reachable),
267
+ "pruned_dead_count": len(dead),
268
+ "raw_tokens": raw_tokens,
269
+ "zero_waste_tokens": pruned_tokens,
270
+ "token_reduction_pct": reduction_pct,
271
+ "zero_waste_prompt": zero_waste_prompt
272
+ }