code-oracle 0.1.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.
Files changed (40) hide show
  1. code_oracle/__init__.py +30 -0
  2. code_oracle/cli.py +795 -0
  3. code_oracle/config.py +145 -0
  4. code_oracle/dataset.py +5325 -0
  5. code_oracle/dead_code/__init__.py +32 -0
  6. code_oracle/dead_code/detector.py +379 -0
  7. code_oracle/dead_code/entrypoints.py +333 -0
  8. code_oracle/dead_code/models.py +255 -0
  9. code_oracle/dead_code/semantics.py +416 -0
  10. code_oracle/decision.py +906 -0
  11. code_oracle/engine.py +430 -0
  12. code_oracle/export_onnx.py +436 -0
  13. code_oracle/hook.py +531 -0
  14. code_oracle/indexer.py +894 -0
  15. code_oracle/languages/__init__.py +114 -0
  16. code_oracle/languages/common.py +127 -0
  17. code_oracle/languages/go.py +395 -0
  18. code_oracle/languages/python.py +336 -0
  19. code_oracle/languages/rust.py +474 -0
  20. code_oracle/languages/typescript.py +775 -0
  21. code_oracle/linearizer.py +166 -0
  22. code_oracle/locator.py +301 -0
  23. code_oracle/models.py +237 -0
  24. code_oracle/perf_lint/__init__.py +38 -0
  25. code_oracle/perf_lint/engine.py +234 -0
  26. code_oracle/perf_lint/models.py +229 -0
  27. code_oracle/perf_lint/rules/__init__.py +31 -0
  28. code_oracle/perf_lint/rules/async_blocking.py +143 -0
  29. code_oracle/perf_lint/rules/n_plus_one.py +232 -0
  30. code_oracle/perf_lint/rules/nested_loops.py +137 -0
  31. code_oracle/perf_lint/rules/unclosed_res.py +494 -0
  32. code_oracle/perf_lint/visitor.py +299 -0
  33. code_oracle/server.py +184 -0
  34. code_oracle/slicer.py +225 -0
  35. code_oracle/symbolic.py +459 -0
  36. code_oracle-0.1.0.dist-info/METADATA +225 -0
  37. code_oracle-0.1.0.dist-info/RECORD +40 -0
  38. code_oracle-0.1.0.dist-info/WHEEL +4 -0
  39. code_oracle-0.1.0.dist-info/entry_points.txt +2 -0
  40. code_oracle-0.1.0.dist-info/licenses/LICENSE +190 -0
code_oracle/config.py ADDED
@@ -0,0 +1,145 @@
1
+ """
2
+ Configuration and state management for Code Oracle.
3
+ Maintains persistent settings in .code_oracle/config.json.
4
+ """
5
+
6
+ import json
7
+ import os
8
+ import subprocess
9
+ from pathlib import Path
10
+ from typing import Any, Dict, Optional
11
+
12
+ DEFAULT_CONFIG: Dict[str, Any] = {
13
+ "enabled": True,
14
+ "mode": "block",
15
+ }
16
+
17
+
18
+ def find_git_root(workspace_root: Optional[Path] = None) -> Optional[Path]:
19
+ """Find root directory of the git repository."""
20
+ ws = Path(workspace_root or Path.cwd()).resolve()
21
+ try:
22
+ res = subprocess.run(
23
+ ["git", "rev-parse", "--show-toplevel"],
24
+ cwd=str(ws),
25
+ capture_output=True,
26
+ text=True,
27
+ check=False,
28
+ )
29
+ if res.returncode == 0 and res.stdout.strip():
30
+ return Path(res.stdout.strip()).resolve()
31
+ except Exception:
32
+ pass
33
+
34
+ if (ws / ".git").exists():
35
+ return ws
36
+ for parent in ws.parents:
37
+ if (parent / ".git").exists():
38
+ return parent
39
+ return None
40
+
41
+
42
+ def resolve_workspace_root(workspace_root: Optional[Path] = None) -> Path:
43
+ """
44
+ Resolve the canonical workspace directory for configuration.
45
+ If workspace_root or cwd contains .git or .code_oracle, returns it.
46
+ Otherwise checks parent directories for .git or .code_oracle.
47
+ Falls back to workspace_root or Path.cwd().
48
+ """
49
+ ws = Path(workspace_root or Path.cwd()).resolve()
50
+ if (ws / ".git").exists() or (ws / ".code_oracle").exists():
51
+ return ws
52
+
53
+ for parent in ws.parents:
54
+ if (parent / ".git").exists() or (parent / ".code_oracle").exists():
55
+ return parent
56
+
57
+ git_root = find_git_root(ws)
58
+ if git_root:
59
+ return git_root
60
+
61
+ return ws
62
+
63
+
64
+ def get_config_path(workspace_root: Optional[Path] = None) -> Path:
65
+ """Return path to .code_oracle/config.json (or hook_config.json if existing)."""
66
+ ws = resolve_workspace_root(workspace_root)
67
+ cfg_file = ws / ".code_oracle" / "config.json"
68
+ legacy_file = ws / ".code_oracle" / "hook_config.json"
69
+ if not cfg_file.exists() and legacy_file.exists():
70
+ return legacy_file
71
+ return cfg_file
72
+
73
+
74
+ def load_config(workspace_root: Optional[Path] = None) -> Dict[str, Any]:
75
+ """
76
+ Load configuration from .code_oracle/config.json or hook_config.json.
77
+ Returns default config if file does not exist or is corrupted.
78
+ """
79
+ target_file = get_config_path(workspace_root)
80
+ config = dict(DEFAULT_CONFIG)
81
+ if target_file.exists():
82
+ try:
83
+ with open(target_file, "r", encoding="utf-8") as f:
84
+ data = json.load(f)
85
+ if isinstance(data, dict):
86
+ config.update(data)
87
+ except Exception:
88
+ # Corrupted config, return defaults
89
+ pass
90
+
91
+ # Ensure mode is valid
92
+ if config.get("mode") not in ("block", "warn"):
93
+ config["mode"] = "block"
94
+ config["enabled"] = bool(config.get("enabled", True))
95
+
96
+ return config
97
+
98
+
99
+ def save_config(workspace_root: Optional[Path], config: Dict[str, Any]) -> None:
100
+ """Atomically write configuration to .code_oracle/config.json (or hook_config.json)."""
101
+ cfg_file = get_config_path(workspace_root)
102
+ cfg_dir = cfg_file.parent
103
+ cfg_dir.mkdir(parents=True, exist_ok=True)
104
+ temp_file = cfg_dir / f"{cfg_file.name}.tmp"
105
+
106
+ try:
107
+ with open(temp_file, "w", encoding="utf-8") as f:
108
+ json.dump(config, f, indent=2)
109
+ os.replace(temp_file, cfg_file)
110
+ except Exception:
111
+ # Fallback direct write if atomic replace fails
112
+ try:
113
+ with open(cfg_file, "w", encoding="utf-8") as f:
114
+ json.dump(config, f, indent=2)
115
+ except Exception:
116
+ pass
117
+
118
+
119
+ def set_enabled(workspace_root: Optional[Path], enabled: bool) -> Dict[str, Any]:
120
+ """Update enabled flag in config."""
121
+ cfg = load_config(workspace_root)
122
+ cfg["enabled"] = bool(enabled)
123
+ save_config(workspace_root, cfg)
124
+ return cfg
125
+
126
+
127
+ def set_mode(workspace_root: Optional[Path], mode: str) -> Dict[str, Any]:
128
+ """Update mode in config ('block' or 'warn')."""
129
+ if mode not in ("block", "warn"):
130
+ raise ValueError(f"Invalid mode '{mode}'. Must be 'block' or 'warn'.")
131
+ cfg = load_config(workspace_root)
132
+ cfg["mode"] = mode
133
+ save_config(workspace_root, cfg)
134
+ return cfg
135
+
136
+
137
+ def is_bypassed(workspace_root: Optional[Path] = None) -> bool:
138
+ """
139
+ Fast check if verification is bypassed via CODE_ORACLE_SKIP=1 or enabled=False.
140
+ """
141
+ skip = os.environ.get("CODE_ORACLE_SKIP", "").strip().lower()
142
+ if skip in ("1", "true", "yes"):
143
+ return True
144
+ cfg = load_config(workspace_root)
145
+ return not cfg.get("enabled", True)