codegraphx 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 (44) hide show
  1. codegraph/__init__.py +37 -0
  2. codegraph/cli.py +106 -0
  3. codegraph/commands/__init__.py +1 -0
  4. codegraph/commands/conflicts.py +154 -0
  5. codegraph/commands/diff.py +189 -0
  6. codegraph/commands/export.py +141 -0
  7. codegraph/commands/graph.py +200 -0
  8. codegraph/commands/index.py +924 -0
  9. codegraph/commands/serve.py +62 -0
  10. codegraph/commands/why.py +645 -0
  11. codegraph/decisions/__init__.py +18 -0
  12. codegraph/decisions/conflicts.py +238 -0
  13. codegraph/decisions/extractor.py +698 -0
  14. codegraph/decisions/llm_extractor.py +161 -0
  15. codegraph/decisions/sources.py +298 -0
  16. codegraph/git/__init__.py +5 -0
  17. codegraph/git/diff.py +115 -0
  18. codegraph/git/history.py +156 -0
  19. codegraph/git/incremental.py +75 -0
  20. codegraph/git/refs.py +70 -0
  21. codegraph/models.py +211 -0
  22. codegraph/parser/__init__.py +1 -0
  23. codegraph/parser/base.py +164 -0
  24. codegraph/parser/common.py +147 -0
  25. codegraph/parser/parallel.py +96 -0
  26. codegraph/parser/registry.py +83 -0
  27. codegraph/parser/treesitter_cpp.py +102 -0
  28. codegraph/parser/treesitter_go.py +113 -0
  29. codegraph/parser/treesitter_java.py +79 -0
  30. codegraph/parser/treesitter_js.py +15 -0
  31. codegraph/parser/treesitter_py.py +163 -0
  32. codegraph/parser/treesitter_rust.py +120 -0
  33. codegraph/parser/treesitter_ts.py +178 -0
  34. codegraph/storage.py +767 -0
  35. codegraph/web/__init__.py +1 -0
  36. codegraph/web/app.py +271 -0
  37. codegraph/web/static/app.js +356 -0
  38. codegraph/web/static/index.html +59 -0
  39. codegraph/web/static/styles.css +92 -0
  40. codegraphx-0.1.0.dist-info/METADATA +262 -0
  41. codegraphx-0.1.0.dist-info/RECORD +44 -0
  42. codegraphx-0.1.0.dist-info/WHEEL +4 -0
  43. codegraphx-0.1.0.dist-info/entry_points.txt +2 -0
  44. codegraphx-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,18 @@
1
+ """Decision extraction package."""
2
+
3
+ from codegraph.decisions.extractor import DECISION_KEYWORDS, split_sentences
4
+ from codegraph.decisions.sources import (
5
+ extract_all,
6
+ extract_from_adrs,
7
+ extract_from_changelog,
8
+ extract_from_commits,
9
+ )
10
+
11
+ __all__ = [
12
+ "DECISION_KEYWORDS",
13
+ "split_sentences",
14
+ "extract_from_adrs",
15
+ "extract_from_changelog",
16
+ "extract_from_commits",
17
+ "extract_all",
18
+ ]
@@ -0,0 +1,238 @@
1
+ """Three-layer conflict detection for accepted decisions.
2
+
3
+ Matching is word-boundary based and topic-gated. An earlier version matched raw
4
+ substrings, so ``use`` fired inside ``because`` and ``sync`` inside ``async``,
5
+ and any two decisions in the same folder could be reported as contradictory.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import re
11
+ from difflib import SequenceMatcher
12
+
13
+ from codegraph.models import Conflict, Decision, DecisionStatus
14
+
15
+ # Each entry is a group of mutually exclusive terms. Generic verbs such as
16
+ # "use"/"avoid" are deliberately absent: they appear in almost every decision
17
+ # and produce contradictions out of unrelated documents.
18
+ OPPOSITE_PAIRS: list[tuple[str, ...]] = [
19
+ ("always", "never"),
20
+ ("enable", "disable"),
21
+ ("stateful", "stateless"),
22
+ ("sync", "async"),
23
+ ("blocking", "non-blocking"),
24
+ ("mutable", "immutable"),
25
+ ("allow", "deny"),
26
+ ("include", "exclude"),
27
+ ("strict", "lenient"),
28
+ ("eager", "lazy"),
29
+ ("retry", "fail fast"),
30
+ ("add", "remove"),
31
+ ("keep", "drop"),
32
+ ]
33
+
34
+ TEMPORAL_WINDOW_DAYS = 3
35
+ TOPIC_SIMILARITY = 0.25 # token overlap needed for same-folder decisions
36
+ RESTATEMENT_SIMILARITY = 0.8 # above this, one decision restates the other
37
+
38
+ # Terms specific enough to stand on their own. The weaker ones (add/remove,
39
+ # keep/drop, enable/disable …) also need the two decisions to share vocabulary,
40
+ # otherwise every changelog pair in one folder contradicts every other.
41
+ STRONG_TERMS = {
42
+ "stateful", "stateless", "sync", "async", "blocking", "non-blocking",
43
+ "mutable", "immutable", "strict", "lenient", "eager", "lazy", "fail fast",
44
+ }
45
+
46
+ _WORD_RE = re.compile(r"[a-z][a-z0-9_]{2,}")
47
+ _STOPWORDS = {
48
+ "the", "and", "for", "with", "that", "this", "from", "into", "not", "but",
49
+ "are", "was", "were", "must", "should", "can", "will", "has", "have", "its",
50
+ "use", "uses", "used", "using", "prefer", "prefers", "decide", "decided",
51
+ "our", "their", "than", "then", "also", "more", "most", "all", "any",
52
+ }
53
+
54
+
55
+ def _term_pattern(term: str) -> re.Pattern[str]:
56
+ """Build a word-boundary pattern for a single term or phrase."""
57
+ parts = [re.escape(part) for part in term.split()]
58
+ return re.compile(r"\b" + r"\s+".join(parts) + r"\b", re.IGNORECASE)
59
+
60
+
61
+ _PATTERNS: dict[str, re.Pattern[str]] = {
62
+ term: _term_pattern(term) for group in OPPOSITE_PAIRS for term in group
63
+ }
64
+
65
+
66
+ def _text(d: Decision) -> str:
67
+ body = f"{d.content} {d.reason} {' '.join(d.alternatives)} {' '.join(d.constraints)}"
68
+ return body.lower()
69
+
70
+
71
+ def _location(d: Decision) -> str:
72
+ path = d.file_path or ""
73
+ return path.rsplit("/", 1)[0] if "/" in path else (path or "(global)")
74
+
75
+
76
+ def _stem(word: str) -> str:
77
+ """Very small stemmer: enough to match session/sessions, token/tokens."""
78
+ if len(word) > 4 and word.endswith("s") and not word.endswith("ss"):
79
+ return word[:-1]
80
+ return word
81
+
82
+
83
+ def _tokens(d: Decision) -> set[str]:
84
+ words = {_stem(w) for w in _WORD_RE.findall(_text(d))}
85
+ return {w for w in words if w not in _STOPWORDS}
86
+
87
+
88
+ def _overlap(a: Decision, b: Decision) -> float:
89
+ """Overlap coefficient of two decisions' vocabulary."""
90
+ ta, tb = _tokens(a), _tokens(b)
91
+ if not ta or not tb:
92
+ return 0.0
93
+ return len(ta & tb) / min(len(ta), len(tb))
94
+
95
+
96
+ def _similarity(a: Decision, b: Decision) -> float:
97
+ return SequenceMatcher(None, _text(a), _text(b)).ratio()
98
+
99
+
100
+ def _hit_terms(decision: Decision) -> set[str]:
101
+ """Terms from OPPOSITE_PAIRS that occur as words in this decision."""
102
+ text = _text(decision)
103
+ return {term for term, pattern in _PATTERNS.items() if pattern.search(text)}
104
+
105
+
106
+ def _same_place(a: Decision, b: Decision) -> bool:
107
+ """Same file, or the same folder at all."""
108
+ if a.file_path and a.file_path == b.file_path:
109
+ return True
110
+ return _location(a) == _location(b) and bool(_location(a))
111
+
112
+
113
+ def _comparable(a: Decision, b: Decision, matched: list[str]) -> bool:
114
+ """Decide whether an opposite-term hit is trustworthy enough to report."""
115
+ if set(matched) & STRONG_TERMS:
116
+ return True
117
+ return _overlap(a, b) >= TOPIC_SIMILARITY
118
+
119
+
120
+ def _opposite_hits(a: Decision, b: Decision) -> list[str]:
121
+ """Return the opposite terms that put a and b on different sides."""
122
+ hits_a, hits_b = _hit_terms(a), _hit_terms(b)
123
+ matched: list[str] = []
124
+ for group in OPPOSITE_PAIRS:
125
+ side_a = {t for t in group if t in hits_a}
126
+ side_b = {t for t in group if t in hits_b}
127
+ if side_a and side_b and side_a != side_b:
128
+ matched.extend(sorted(side_a | side_b))
129
+ return sorted(set(matched))
130
+
131
+
132
+ def detect_word_opposites(decisions: list[Decision]) -> list[Conflict]:
133
+ """Find opposite-term conflicts on the same file or on the same topic."""
134
+ out: list[Conflict] = []
135
+ accepted = [d for d in decisions if d.status == DecisionStatus.ACCEPTED]
136
+ for i, a in enumerate(accepted):
137
+ for b in accepted[i + 1 :]:
138
+ if a.uid == b.uid or not _same_place(a, b):
139
+ continue
140
+ if _similarity(a, b) >= RESTATEMENT_SIMILARITY:
141
+ continue # the same decision written twice
142
+ matched = _opposite_hits(a, b)
143
+ if not matched or not _comparable(a, b, matched):
144
+ continue
145
+ out.append(
146
+ Conflict(
147
+ type="opposite",
148
+ decision_a=a,
149
+ decision_b=b,
150
+ location=_location(a),
151
+ matched_words=matched,
152
+ explanation=f"opposite terms {matched}",
153
+ )
154
+ )
155
+ return out
156
+
157
+
158
+ def detect_supersede_conflicts(decisions: list[Decision]) -> list[Conflict]:
159
+ """A supersedes B but B is still accepted."""
160
+ out: list[Conflict] = []
161
+ for a in decisions:
162
+ blob = f"{a.content} {a.reason} {a.source_ref}".lower()
163
+ if "supersede" not in blob and "取代" not in blob:
164
+ continue
165
+ for b in decisions:
166
+ if a.uid == b.uid:
167
+ continue
168
+ if b.status != DecisionStatus.ACCEPTED:
169
+ continue
170
+ if b.uid in blob or b.content[:20].lower() in blob:
171
+ out.append(
172
+ Conflict(
173
+ type="supersede",
174
+ decision_a=a,
175
+ decision_b=b,
176
+ location=_location(b),
177
+ matched_words=["supersede"],
178
+ explanation="claimed supersede but target still accepted",
179
+ )
180
+ )
181
+ return out
182
+
183
+
184
+ def detect_temporal_conflicts(decisions: list[Decision]) -> list[Conflict]:
185
+ """A decision reversed shortly afterwards in the same file.
186
+
187
+ Opposite terms are required: two unrelated decisions that merely happen to
188
+ be close in time are not a contradiction.
189
+ """
190
+ out: list[Conflict] = []
191
+ accepted = [d for d in decisions if d.status == DecisionStatus.ACCEPTED]
192
+ for i, a in enumerate(accepted):
193
+ for b in accepted[i + 1 :]:
194
+ if a.uid == b.uid or not a.file_path or a.file_path != b.file_path:
195
+ continue
196
+ delta = abs((a.timestamp - b.timestamp).total_seconds())
197
+ if delta > TEMPORAL_WINDOW_DAYS * 86400:
198
+ continue
199
+ matched = _opposite_hits(a, b)
200
+ if not matched:
201
+ continue
202
+ sim = _similarity(a, b)
203
+ if sim >= RESTATEMENT_SIMILARITY:
204
+ continue
205
+ days = delta / 86400
206
+ out.append(
207
+ Conflict(
208
+ type="temporal",
209
+ decision_a=a,
210
+ decision_b=b,
211
+ location=_location(a),
212
+ matched_words=matched,
213
+ explanation=(
214
+ f"reversed within {days:.1f}d (similarity {sim:.2f}) "
215
+ f"with opposites {matched}"
216
+ ),
217
+ confidence=0.8,
218
+ )
219
+ )
220
+ return out
221
+
222
+
223
+ def detect_all(decisions: list[Decision]) -> list[Conflict]:
224
+ """Run all detectors and keep the most specific report per decision pair."""
225
+ raw = (
226
+ detect_word_opposites(decisions)
227
+ + detect_supersede_conflicts(decisions)
228
+ + detect_temporal_conflicts(decisions)
229
+ )
230
+ rank = {"supersede": 0, "temporal": 1, "opposite": 2}
231
+ best: dict[tuple[str, str], Conflict] = {}
232
+ for conflict in raw:
233
+ first, second = sorted([conflict.decision_a.uid, conflict.decision_b.uid])
234
+ key = (first, second)
235
+ prev = best.get(key)
236
+ if prev is None or rank.get(conflict.type, 9) < rank.get(prev.type, 9):
237
+ best[key] = conflict
238
+ return sorted(best.values(), key=lambda c: (rank.get(c.type, 9), c.location))