-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
3849 lines (3401 loc) · 169 KB
/
server.py
File metadata and controls
3849 lines (3401 loc) · 169 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
consequencegraph visual server.
Serves the knowledge graph as a REST API + interactive D3.js frontend.
Run:
python server.py --path ./neural_lam --preset neural_lam
then open http://localhost:7842
"""
import json
import os
import re
import ast
import sys
import argparse
import subprocess
import time
from collections import defaultdict
from pathlib import Path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from core.graph import KnowledgeGraph
from core.indexer import Indexer
from core.enricher import Enricher
from core.query import QueryEngine
from output.llm_context import format_impact_as_context
try:
from fastapi import FastAPI, HTTPException, Request, Query
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
FASTAPI_AVAILABLE = True
except ImportError:
FASTAPI_AVAILABLE = False
# ── Config ────────────────────────────────────────────────────────────────────
PRODUCTION = os.environ.get("CONSEQUENCEGRAPH_ENV") == "production"
RATE_LIMIT_WINDOW = 60 # seconds
RATE_LIMIT_MAX = 60 # requests per window per IP
RATE_BUCKETS_MAX = 10_000 # cap tracked IPs to prevent memory leak (H2)
CORS_ORIGIN = os.environ.get("CORS_ALLOW_ORIGIN", "*") # H3: restrict in prod via render.yaml
# ── Rate limiter ──────────────────────────────────────────────────────────────
_rate_buckets: dict = defaultdict(list)
def _get_real_ip(request) -> str:
"""Real client IP behind proxy/LB (H2: X-Forwarded-For)."""
xff = request.headers.get("X-Forwarded-For")
if xff:
return xff.split(",")[0].strip()
return request.client.host if request.client else "unknown"
def check_rate_limit(ip: str) -> bool:
now = time.time()
bucket = _rate_buckets[ip]
_rate_buckets[ip] = [t for t in bucket if now - t < RATE_LIMIT_WINDOW]
if len(_rate_buckets[ip]) >= RATE_LIMIT_MAX:
return False
_rate_buckets[ip].append(now)
if len(_rate_buckets) > RATE_BUCKETS_MAX: # H2: evict oldest
oldest = next(iter(_rate_buckets))
del _rate_buckets[oldest]
return True
# ── Graph state ───────────────────────────────────────────────────────────────
_graph: KnowledgeGraph = None
_engine: QueryEngine = None
_index_path: str = None
def get_graph() -> KnowledgeGraph:
return _graph
def get_engine() -> QueryEngine:
return _engine
def build_or_load_graph(path: str, preset: str = None, force_reindex: bool = False):
global _graph, _engine, _index_path
_index_path = os.path.abspath(path)
# C4: prefer <path>/cache.json (pre-built on Render); fall back to dev-mode path
_path_cache = os.path.join(_index_path, "cache.json")
cache = _path_cache if os.path.isfile(_path_cache) else os.path.join(os.getcwd(), ".consequencegraph", "cache.json")
_graph = KnowledgeGraph()
_graph.CACHE_FILE = cache
if not force_reindex and _graph.load(cache):
print(f"[consequencegraph server] Loaded from cache: {_graph.node_count()} nodes")
else:
print(f"[consequencegraph server] Indexing: {path}")
Indexer(_graph, path).index_path(path)
Enricher(_graph).run()
if preset == "neural_lam":
from presets.neural_lam import apply
apply(_graph)
elif preset == "wmg":
from presets.wmg import apply
apply(_graph)
elif preset == "wmg_neural_lam":
# Both repos indexed together — apply neural-lam domain knowledge
# then wmg format contracts + cross-repo edges on top.
from presets.neural_lam import apply as apply_nl
from presets.wmg import apply as apply_wmg
apply_nl(_graph)
apply_wmg(_graph)
_graph.save(cache)
print(f"[consequencegraph server] Done: {_graph.node_count()} nodes, {_graph.edge_count()} edges")
_engine = QueryEngine(_graph)
# ── API ───────────────────────────────────────────────────────────────────────
app = FastAPI(title="consequencegraph", description="Consequence-aware code knowledge graph")
app.add_middleware(CORSMiddleware, allow_origins=[CORS_ORIGIN], allow_methods=["GET", "POST"]) # H3
@app.middleware("http")
async def rate_limit_middleware(request: Request, call_next):
if PRODUCTION and request.url.path.startswith("/api/"):
ip = _get_real_ip(request) # H2: real IP behind Render LB
if not check_rate_limit(ip):
return JSONResponse({"error": "Rate limit exceeded. Try again in a minute."}, status_code=429)
return await call_next(request)
@app.get("/api/graph")
def api_graph():
"""Return full graph as D3-compatible node-link JSON."""
g = get_graph()
nodes = []
for nid, data in g.g.nodes(data=True):
nodes.append({
"id": nid,
"name": data.get("name", nid.split(".")[-1]),
"type": data.get("node_type", "unknown"),
"module": data.get("module", ""),
"file": data.get("file_path", ""),
"line": data.get("line_no", 0),
"docstring": data.get("docstring", ""),
"is_lightning_hook": data.get("is_lightning_hook", False),
"tensor_shapes": data.get("tensor_shapes", {}),
"config_keys": data.get("config_keys", []),
"in_degree": g.in_degree(nid),
"out_degree": g.out_degree(nid),
# wmg preset fields
"is_cross_repo": data.get("is_cross_repo", False),
"mismatch_risk": data.get("mismatch_risk", ""),
"is_lossiness_boundary": data.get("is_lossiness_boundary", False),
"is_critical_preset": data.get("is_critical_preset", False),
})
edges = []
for u, v, data in g.g.edges(data=True):
edges.append({
"source": u,
"target": v,
"type": data.get("edge_type", "unknown"),
"severity_weight": data.get("severity_weight", 0),
"reason": data.get("reason", ""),
})
return {"nodes": nodes, "edges": edges, "stats": g.stats()}
@app.get("/api/impact/{node_id:path}")
def api_impact(node_id: str, depth: int = Query(None, ge=1, le=6)): # L2: cap depth
"""Return impact report for a node."""
engine = get_engine()
result = engine.impact(node_id, depth=depth)
return result
@app.get("/api/impact_text/{node_id:path}")
def api_impact_text(node_id: str, depth: int = Query(None, ge=1, le=6)): # L2
"""Return human-readable impact report."""
engine = get_engine()
result = engine.impact(node_id, depth=depth)
return {"text": format_impact_as_context(result)}
@app.get("/api/search")
def api_search(q: str = Query(..., min_length=1, max_length=200), limit: int = Query(20, ge=1, le=100)): # H4
"""Search nodes by name."""
g = get_graph()
results = []
q_lower = q.lower()
for nid, data in g.g.nodes(data=True):
if q_lower in nid.lower():
results.append({
"id": nid,
"name": data.get("name", ""),
"type": data.get("node_type", ""),
"file": data.get("file_path", ""),
})
if len(results) >= limit:
break
return {"results": results}
@app.get("/api/stats")
def api_stats():
return get_graph().stats()
@app.post("/api/reindex")
def api_reindex():
if PRODUCTION:
raise HTTPException(status_code=403, detail="Reindex disabled in production demo.")
build_or_load_graph(_index_path, force_reindex=True)
return {"status": "ok", "nodes": get_graph().node_count(), "edges": get_graph().edge_count()}
@app.post("/api/consequence")
async def api_consequence(request: Request):
"""
Unified consequence analysis. Accepts a free-form query + optional anchor node.
Classifies intent, routes to the right analysis strategy, returns adaptive output:
- specific_change → step-by-step plan in dependency order
- decision → A vs B comparison with change counts
- removal → ordered cleanup plan
- exploration → structural paragraph + intersection breakdown
"""
body = await request.json()
query_text = body.get("query", "").strip()
anchor_id = body.get("anchor_node_id") # set when user clicked a node first
if not query_text:
raise HTTPException(status_code=400, detail="query field required")
if len(query_text) > 1000: # H4: cap to prevent O(n×|q|) regex DoS
raise HTTPException(status_code=400, detail="query too long (max 1000 chars)")
# C2: validate anchor_id before trusting it
if anchor_id is not None and (not isinstance(anchor_id, str) or len(anchor_id) > 500):
anchor_id = None
graph = get_graph()
engine = get_engine()
# 1. Extract mentioned nodes — anchor always included and weighted higher
mentioned = _cq_extract_nodes(query_text, graph)
if anchor_id and graph.has_node(anchor_id) and anchor_id not in mentioned: # C2
mentioned.insert(0, anchor_id)
if not mentioned:
# Try to suggest real node names by extracting code-identifier-shaped tokens.
# Only use words that look like identifiers: contain underscores, are camelCase,
# or are long enough to be specific (≥8 chars). Skip plain English words.
_ENGLISH_STOPWORDS = {
"what", "which", "where", "when", "does", "will", "break", "change",
"function", "functions", "class", "classes", "method", "methods",
"read", "write", "return", "call", "calls", "use", "uses", "used",
"with", "from", "into", "this", "that", "their", "there", "these",
"those", "have", "should", "would", "could", "affect", "affects",
"impact", "impacts", "downstream", "upstream", "depend", "depends",
"feature", "features", "encoding", "positional", "spatial", "global",
"local", "static", "input", "output", "tensor", "model", "data",
"node", "nodes", "graph", "layer", "layers", "module", "modules",
"train", "training", "forward", "backward", "loss", "batch",
}
def _looks_like_identifier(word: str) -> bool:
"""True if word looks like a code symbol rather than plain English."""
if "_" in word: return True # snake_case: lat_lon_static
if word[0].isupper() and any(c.islower() for c in word[1:]) \
and any(c.isupper() for c in word[1:]):
return True # CamelCase: WeatherDataset
if len(word) >= 10: return True # long enough to be specific
return False
raw_words = [w.strip('.,?!()[]') for w in query_text.split()]
candidate_words = [
w for w in raw_words
if len(w) >= 4
and w.lower() not in _ENGLISH_STOPWORDS
and _looks_like_identifier(w)
]
suggestions = []
seen_names: set = set()
for word in candidate_words[:6]:
word_lower = word.lower()
for nid, data in graph.g.nodes(data=True):
name = data.get("name", "")
if name in seen_names:
continue
if any(nid.startswith(p) for p in ("config::", "hook::", "tensor_contract::")):
continue
# Require the match to be on a meaningful boundary
name_lower = name.lower()
nid_lower = nid.lower()
match = (
word_lower == name_lower # exact name match
or word_lower in name_lower.split("_") # snake part match
or f"_{word_lower}" in nid_lower # suffix in ID
or nid_lower.endswith(f".{word_lower}") # module.name
or (len(word_lower) >= 6 and word_lower in name_lower) # substring, long only
)
if match:
suggestions.append({"id": nid, "name": name, "file": data.get("file_path","")})
seen_names.add(name)
if len(suggestions) >= 6:
break
if len(suggestions) >= 6:
break
no_identifiers = len(candidate_words) == 0
hint = (
"This query uses descriptive language but no code identifiers. "
"Try searching for the function or class name directly — e.g. use the search box above to find it, then click the node to pre-fill the query."
if no_identifiers else
"Mention function or class names from the codebase directly — e.g. 'WeatherDataset.__getitem__' or 'training_step'."
)
return {
"error": "No known nodes found in query.",
"hint": hint,
"suggestions": suggestions[:6],
}
# 2. Classify intent
intent = _cq_classify_intent(query_text)
change_type = _cq_classify_change_type(query_text)
# 3. Compute blast radius per mentioned node
# engine.impact() excludes config::, hook::, tensor_contract:: nodes from resolution.
# For those, fall back to direct graph traversal so comparison queries on config nodes work.
_SYNTHETIC_PREFIXES = ("config::", "hook::", "tensor_contract::", "data_format::")
impacts = {}
for nid in mentioned:
r = engine.impact(nid, depth=3)
if "error" not in r and "ambiguous" not in r:
impacts[nid] = r
elif graph.has_node(nid) and any(nid.startswith(p) for p in _SYNTHETIC_PREFIXES):
# Direct traversal fallback for synthetic nodes.
# Config nodes are LEAF nodes — edges go function→config::key, never the reverse.
# So "downstream" for a config key = all functions that read/write it = upstream().
# We swap: use upstream for the "what does this affect" count on config nodes.
node_data = graph.get_node(nid) or {}
readers_raw = graph.upstream(nid, depth=3) # functions that use this config key
writers_raw = graph.downstream(nid, depth=3) # typically empty for config nodes
all_raw = readers_raw + writers_raw
score = len(all_raw)
impacts[nid] = {
"target": nid,
"target_meta": {
"type": node_data.get("node_type"),
"file": node_data.get("file_path", ""),
"line": node_data.get("line_no", 0),
"is_lightning_hook": False,
"tensor_shapes": {},
"config_keys": [],
},
"severity": "medium" if score > 5 else "low",
"severity_score": score,
"blast_radius": {
"upstream": readers_raw,
"downstream": readers_raw, # expose readers as "downstream" so comparison logic finds them
"upstream_count": len(readers_raw),
"downstream_count": len(readers_raw),
},
"critical_path": [],
"llm_context_hint": "",
}
if not impacts:
return {"error": "Could not compute impact for any mentioned nodes.", "mentioned": mentioned}
# 3b. Post-process: config nodes are leaf nodes (edges go function→config::key,
# never config→function). engine.impact() resolves them via exact match but returns
# downstream_count=0 because they have no successors. For these nodes, the upstream
# (functions that read/write the key) IS the blast radius — swap it in.
_SYNTHETIC_PREFIXES = ("config::", "hook::", "tensor_contract::", "data_format::")
for nid, impact in impacts.items():
if any(nid.startswith(p) for p in _SYNTHETIC_PREFIXES):
br = impact.get("blast_radius", {})
if br.get("downstream_count", 0) == 0 and br.get("upstream_count", 0) > 0:
readers = br.get("upstream", [])
impact["blast_radius"]["downstream"] = readers
impact["blast_radius"]["downstream_count"] = len(readers)
# 4. Score all nodes by structural relevance
intersection_scores: dict = {}
edge_types_by_node: dict = {}
reasons_by_node: dict = {}
depth_by_node: dict = {}
for origin_id, impact in impacts.items():
for direction in ("downstream", "upstream"):
for entry in impact.get("blast_radius", {}).get(direction, []):
nid = entry["node"]
if any(nid.startswith(p) for p in ("hook::", "config::", "tensor_contract::")):
continue
if nid in mentioned:
continue
intersection_scores[nid] = intersection_scores.get(nid, 0) + 1
edge_types_by_node.setdefault(nid, set()).add(entry.get("edge_type", ""))
depth_by_node[nid] = min(depth_by_node.get(nid, 99), entry.get("depth", 99))
reasons_by_node.setdefault(nid, []).append({
"origin": origin_id,
"edge_type": entry.get("edge_type", ""),
"reason": entry.get("reason", ""),
"depth": entry.get("depth", 1),
"direction": direction,
})
# 5. Tier classification
HIGH_RISK = {"produces_tensor_for", "overrides_hook", "inherits"}
MED_RISK = {"consumes_format", "reads_config", "calls"}
# Bug 6: edge types that indicate real breakage vs. mere proximity
PROXIMITY_ONLY = {"imports", "defined_in", "named_reference"}
will_break = []
likely_need = []
be_aware = []
for nid, score in sorted(intersection_scores.items(), key=lambda x: -x[1]):
node_data = graph.get_node(nid) or {}
file_path = node_data.get("file_path", "")
edges = edge_types_by_node.get(nid, set())
depth = depth_by_node.get(nid, 3)
reasons = reasons_by_node.get(nid, [])
# Bug 6: never put external library nodes in will_break or likely_need
if _is_external_node(nid, file_path):
continue
# Bug 6: proximity-only edges (imports, defined_in) don't indicate breakage
if edges and edges.issubset(PROXIMITY_ONLY):
continue
# Bug 6: pure upstream inheritance from a base class doesn't mean it breaks
all_directions = {r["direction"] for r in reasons}
upstream_only = all_directions == {"upstream"}
if upstream_only and edges.issubset({"inherits", "imports", "defined_in"}):
continue
# Compute tier score
tier_score = score * 2
if edges & HIGH_RISK: tier_score += 3
if edges & MED_RISK: tier_score += 1
if depth == 1: tier_score += 2
if node_data.get("is_lightning_hook"): tier_score += 2
if node_data.get("tensor_shapes"): tier_score += 1
if nid == anchor_id: tier_score += 3
consequence = _cq_consequence_sentence(
intent, change_type, nid, node_data, edges, reasons, depth
)
entry = {
"node": nid,
"name": node_data.get("name", nid.split(".")[-1]),
"type": node_data.get("node_type", ""),
"file": file_path,
"line": node_data.get("line_no", 0),
"is_hook": node_data.get("is_lightning_hook", False),
"shapes": node_data.get("tensor_shapes", {}),
"edge_types": list(edges),
"depth": depth,
"tier_score": tier_score,
"intersection_count": score,
"via": list({r["origin"].split(".")[-1] for r in reasons})[:3],
"consequence": consequence,
}
if tier_score >= 6:
will_break.append(entry)
elif tier_score >= 3:
likely_need.append(entry)
else:
be_aware.append(entry)
# Hard caps per tier
will_break = sorted(will_break, key=lambda x: -x["tier_score"])[:5]
likely_need = sorted(likely_need, key=lambda x: -x["tier_score"])[:4]
be_aware = sorted(be_aware, key=lambda x: -x["tier_score"])[:3]
# 6. Build adaptive lead
lead = _cq_build_lead(
intent, change_type, query_text, mentioned, impacts,
will_break, likely_need, graph
)
# 7. Highlighted node IDs for graph
highlighted = set(mentioned) | {n["node"] for n in will_break + likely_need + be_aware}
# 8. Tuple arity mismatch analysis — for return format / tensor addition changes
arity_warnings = []
if change_type in ("return_format_change", "add_tensor_component") and mentioned:
primary = mentioned[0]
new_arity = _infer_arity_delta(query_text, primary, graph)
if new_arity is not None:
mismatches = _check_arity_mismatches(primary, graph, new_arity)
for m in mismatches:
highlighted.add(m["node"])
arity_warnings = mismatches
return {
"intent": intent,
"change_type": change_type,
"query": query_text,
"anchor": anchor_id if (anchor_id and graph.has_node(anchor_id)) else None, # C2: only echo valid
"mentioned": mentioned,
"mentioned_details": [
{
"node": nid,
"name": (graph.get_node(nid) or {}).get("name", nid.split(".")[-1]),
"file": (graph.get_node(nid) or {}).get("file_path", ""),
"line": (graph.get_node(nid) or {}).get("line_no", 0),
"severity": impacts.get(nid, {}).get("severity", ""),
}
for nid in mentioned
],
"lead": lead,
"will_break": will_break,
"likely_need": likely_need,
"be_aware": be_aware,
"arity_warnings": arity_warnings,
"highlighted": list(highlighted),
}
# ── Consequence helpers ───────────────────────────────────────────────────────
# Bug 4: NLP stopwords — common English narrative words and domain terms that
# appear in natural-language queries but should never be matched as code nodes.
_NLP_STOPWORDS = {
# English narrative / intent words
"downstream", "upstream", "change", "changes", "modify", "modification",
"update", "updates", "approach", "design", "idea", "concept", "feature",
"implement", "implementation", "add", "remove", "refactor", "integrate",
"handle", "handling", "compute", "calculation", "dynamic", "static",
"should", "could", "would", "will", "think", "since", "also", "either",
"rather", "already", "during", "every", "inside", "across", "between",
"instead", "feels", "natural", "extension", "strict", "highly",
# ML domain narrative words that happen to match node names
"forcing", "batch", "loss", "mask", "weight", "buffer", "graph",
"model", "layer", "module", "step", "forward", "backward", "train",
"valid", "test", "data", "output", "input", "result", "value",
"tensor", "shape", "size", "index", "node", "edge", "mesh",
# Python keywords that appear as node fragments
"init", "self", "true", "false", "none", "type", "class", "base",
}
# Bug 6: External library prefixes — nodes from these are never in the "will break" tier
_EXTERNAL_NODE_PREFIXES = {
"torch", "nn", "numpy", "np", "pytorch_lightning", "pl",
"scipy", "sklearn", "pandas", "matplotlib", "wandb",
"omegaconf", "hydra", "einops", "jaxtyping",
}
def _is_external_node(node_id: str, file_path: str) -> bool:
"""Return True if this node belongs to an external library, not target codebase."""
if file_path == "<external>":
return True
prefix = node_id.split(".")[0]
if prefix in _EXTERNAL_NODE_PREFIXES:
return True
return False
# ── Tuple arity analysis ──────────────────────────────────────────────────────
def _parse_return_arity(file_path: str, func_name: str) -> int | None:
"""
Re-parse the source file and find the dominant return tuple arity for func_name.
Returns the most common tuple length across all return statements, or None if
the function doesn't return a tuple or the file can't be parsed.
"""
if not file_path or file_path.startswith("<") or not os.path.isfile(file_path):
return None
try:
with open(file_path, encoding="utf-8", errors="ignore") as f:
source = f.read()
tree = ast.parse(source, filename=file_path)
except SyntaxError:
return None
arities = []
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == func_name:
for stmt in ast.walk(node):
if isinstance(stmt, ast.Return) and stmt.value is not None:
if isinstance(stmt.value, ast.Tuple):
arities.append(len(stmt.value.elts))
if not arities:
return None
# Return the most common arity (handles functions with multiple return paths)
from collections import Counter
return Counter(arities).most_common(1)[0][0]
def _parse_unpack_arity(file_path: str, func_name: str) -> int | None:
"""
Re-parse the source file and find tuple unpacking patterns in func_name that
receive from a variable named 'batch', or the first argument after 'self'.
Returns the unpack arity, or None if no unpacking found.
Detects patterns like:
a, b, c, d = batch
(a, b, c) = batch
init_states, target_states, forcing, static = batch
"""
if not file_path or file_path.startswith("<") or not os.path.isfile(file_path):
return None
try:
with open(file_path, encoding="utf-8", errors="ignore") as f:
source = f.read()
tree = ast.parse(source, filename=file_path)
except SyntaxError:
return None
# Known batch-carrying variable names in PyTorch Lightning
_BATCH_VARS = {"batch", "batch_data", "data", "sample", "item"}
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == func_name:
# Also collect parameter names (first non-self arg is often the batch)
params = [a.arg for a in node.args.args if a.arg != "self"]
batch_names = _BATCH_VARS | (set(params[:1]) if params else set())
for stmt in ast.walk(node):
if isinstance(stmt, ast.Assign):
# LHS is a tuple unpack
if len(stmt.targets) == 1 and isinstance(stmt.targets[0], ast.Tuple):
# RHS is one of the batch variable names
rhs = stmt.value
rhs_name = None
if isinstance(rhs, ast.Name):
rhs_name = rhs.id
elif isinstance(rhs, ast.Subscript) and isinstance(rhs.value, ast.Name):
rhs_name = rhs.value.id
if rhs_name and rhs_name in batch_names:
return len(stmt.targets[0].elts)
return None
def _check_arity_mismatches(
source_node_id: str,
graph: KnowledgeGraph,
simulated_new_arity: int | None,
) -> list[dict]:
"""
For a given source node (e.g. __getitem__), find all downstream consumes_format
nodes, compute their unpack arity, and report mismatches against the simulated
new return arity.
Returns a list of mismatch dicts:
{ node, name, file, line, expected_arity, actual_arity, variables, message }
"""
if simulated_new_arity is None:
return []
mismatches = []
# Walk downstream edges for consumes_format
for _, dst, edge_data in graph.g.out_edges(source_node_id, data=True):
if edge_data.get("edge_type") != "consumes_format":
continue
dst_data = graph.get_node(dst) or {}
dst_file = dst_data.get("file_path", "")
dst_name = dst_data.get("name", dst.split(".")[-1])
dst_line = dst_data.get("line_no", 0)
unpack_arity = _parse_unpack_arity(dst_file, dst_name)
if unpack_arity is None:
continue
if unpack_arity == simulated_new_arity:
continue # no mismatch
# Mismatch — build a precise message
if simulated_new_arity > unpack_arity:
msg = (
f"`{dst_name}` unpacks {unpack_arity} values from batch "
f"but the modified source now returns {simulated_new_arity} — "
f"`ValueError: too many values to unpack` at runtime."
)
else:
msg = (
f"`{dst_name}` expects {unpack_arity} values from batch "
f"but the modified source now returns {simulated_new_arity} — "
f"`ValueError: not enough values to unpack` at runtime."
)
mismatches.append({
"node": dst,
"name": dst_name,
"file": dst_file,
"line": dst_line,
"consumer_arity": unpack_arity,
"source_arity": simulated_new_arity,
"message": msg,
})
return mismatches
def _infer_arity_delta(query: str, source_node_id: str, graph: KnowledgeGraph) -> int | None:
"""
Infer what the new return arity would be after the described change.
Returns the new arity if we can determine it, else None.
Cases:
- "add a tensor" / "add ... to return" → current + 1
- "remove tensor" / "remove ... from return" → current - 1
- explicit number mentioned → use that
"""
node_data = graph.get_node(source_node_id) or {}
file_path = node_data.get("file_path", "")
func_name = node_data.get("name", source_node_id.split(".")[-1])
current_arity = _parse_return_arity(file_path, func_name)
if current_arity is None:
return None
q = query.lower()
# Explicit "N tensors" / "N elements" — rare but handle it
explicit = re.search(r'\b(\d)\s*(?:tensor|item|element|value|field)', q)
if explicit:
return int(explicit.group(1))
if any(w in q for w in ["add", "append", "include", "extra", "additional", "new tensor",
"new field", "introduce"]):
return current_arity + 1
if any(w in q for w in ["remove", "drop", "delete", "strip", "exclude"]):
return max(1, current_arity - 1)
return None
def _cq_extract_nodes(query: str, graph: KnowledgeGraph) -> list[str]:
"""
Extract codebase node IDs mentioned in free-form query text.
Bug 1 fix: polymorphic methods (same name, multiple class implementations)
are grouped. Only the most structurally central representative is returned
per name, annotated with a sibling count so the UI can note "and N others".
This prevents get_xy appearing 3 times in the mentioned list.
"""
found, seen = [], set()
# Build candidates, skipping externals and stopwords
candidates = []
for nid, data in graph.g.nodes(data=True):
if _is_external_node(nid, data.get("file_path", "")):
continue
name = data.get("name", "")
if name and len(name) > 2 and name.lower() not in _NLP_STOPWORDS:
candidates.append((nid, name, data))
parts = nid.split(".")
for part in parts[-2:]:
if part and len(part) > 2 and part.lower() not in _NLP_STOPWORDS:
candidates.append((nid, part, data))
# Deduplicate (nid, name) pairs
deduped_map: dict = {}
for nid, name, data in candidates:
key = (nid, name.lower())
if key not in deduped_map:
deduped_map[key] = (nid, name, data)
deduped = list(deduped_map.values())
# Longer names first — more specific matches win
deduped.sort(key=lambda x: -len(x[1]))
# Group matches by name to detect polymorphic implementations
# name_lower → list of (nid, data)
name_matches: dict[str, list] = {}
for nid, name, data in deduped:
nl = name.lower()
if re.search(r'\b' + re.escape(name) + r'\b', query, re.IGNORECASE):
name_matches.setdefault(nl, []).append((nid, data))
# For each matched name, pick the most structurally central node as representative
for nl, group in name_matches.items():
# Sort by graph degree descending — most connected = most representative
group.sort(key=lambda x: -(graph.in_degree(x[0]) + graph.out_degree(x[0])))
best_nid, best_data = group[0]
if best_nid in seen:
continue
found.append(best_nid)
seen.add(best_nid)
return found[:10]
def _cq_classify_intent(query: str) -> str:
q = query.lower()
# Decision: two approaches being weighed
if any(p in q for p in ["either", "or", "vs", "versus", "approach", "should i", "which"]):
return "decision"
# Removal
if any(w in q for w in ["remove", "delete", "deprecate", "drop"]):
return "removal"
# Specific targeted change
if any(w in q for w in ["add", "rename", "change", "modify", "replace", "refactor",
"implement", "introduce"]):
return "specific_change"
return "exploration"
def _cq_classify_change_type(query: str) -> str:
q = query.lower()
if any(w in q for w in ["return", "tuple", "output", "yield", "add tensor", "new tensor"]):
return "return_format_change"
if any(w in q for w in ["rename", "move"]):
return "rename"
if any(w in q for w in ["param", "argument", "signature", "kwarg"]):
return "signature_change"
if any(w in q for w in ["dim", "shape", "hidden", "channels", "d_h", "d_model"]):
return "dimension_change"
if any(w in q for w in ["buffer", "tensor", "weight", "mask", "register"]):
return "add_tensor_component"
if any(w in q for w in ["config", "hparam", "yaml", "setting"]):
return "config_change"
if any(w in q for w in ["remove", "delete", "deprecate"]):
return "removal"
return "modification"
def _cq_consequence_sentence(
intent: str, change_type: str, node_id: str,
node_data: dict, edges: set, reasons: list, depth: int
) -> str:
name = node_data.get("name", node_id.split(".")[-1])
is_hook = node_data.get("is_lightning_hook", False)
shapes = node_data.get("tensor_shapes", {})
ntype = node_data.get("node_type", "")
n_origins = len({r["origin"] for r in reasons})
file = node_data.get("file_path", "")
fname = file.split("\\")[-1].split("/")[-1] if file else ""
loc = f" ({fname}:{node_data.get('line_no','')})" if fname else ""
origin = reasons[0]["origin"].split(".")[-1] if reasons else "a mentioned node"
# Shape hint
shape_hint = ""
if shapes:
k, v = next(iter(shapes.items()))
sv = v.get("shape", v) if isinstance(v, dict) else v
shape_hint = f" — current contract: `{k}: {sv}`"
# ── Decision / comparison intent ─────────────────────────────────────────
# For comparison queries, describe *why this node matters* for the decision.
if intent == "decision":
if is_hook:
return f"`{name}`{loc} is a Lightning hook shared by both paths — whichever option you choose, this hook must be updated to match the new contract."
if "tests" in node_id or name.startswith("test_"):
origins = list({r["origin"].split(".")[-1] for r in reasons})[:2]
return f"`{name}`{loc} exercises {'both' if n_origins > 1 else 'this'} path{'s' if n_origins > 1 else ''} — it will need updating regardless of which option you choose."
if "consumes_format" in edges or "produces_tensor_for" in edges:
return f"`{name}`{loc} sits in the tensor data flow from `{origin}` — the format contract here changes under either option."
if n_origins > 1:
origins_str = ", ".join(list({r["origin"].split(".")[-1] for r in reasons})[:2])
return f"`{name}`{loc} is affected by both `{origins_str}` — this is shared coupling that both options carry."
return f"`{name}`{loc} is downstream of `{origin}` at depth {depth} — affected by this path."
# ── Specific change types ─────────────────────────────────────────────────
if change_type == "return_format_change":
if "consumes_format" in edges:
return f"`{name}`{loc} unpacks this return value directly. Adding a tensor shifts the tuple — destructuring breaks here."
if "produces_tensor_for" in edges:
return f"`{name}`{loc} has an explicit tensor shape contract{shape_hint}. The contract must be updated to reflect the new output."
if is_hook:
return f"Lightning hook `{name}`{loc} receives this as its batch input — a format change causes a runtime mismatch at the framework boundary."
if change_type == "add_tensor_component":
if "__init__" in node_id:
return f"`{name}`{loc} is where `register_buffer(...)` must be called — buffers registered here persist across devices, checkpoints, and `.to()` calls."
if "consumes_format" in edges or "produces_tensor_for" in edges:
return f"`{name}`{loc} sits in the tensor data flow{shape_hint}. The new weight tensor must be threaded through here explicitly."
if is_hook:
return f"Lightning hook `{name}`{loc} — this is where the buffer gets applied to the loss. It must be in scope here."
if change_type == "signature_change":
if "calls" in edges:
return f"`{name}`{loc} calls this directly at depth {depth} — all argument lists at this call site need updating."
if "inherits" in edges:
return f"`{name}`{loc} inherits this method — the override signature must remain compatible or the MRO breaks."
if "overrides_hook" in edges:
return f"`{name}`{loc} implements a Lightning hook — the framework enforces the signature contract, changing it breaks training."
if change_type == "rename":
if "named_reference" in edges:
return f"`{name}`{loc} references this name in a docstring or string literal — won't break at runtime but becomes stale documentation."
if "calls" in edges:
return f"`{name}`{loc} calls this by name — call sites break immediately on rename."
if "imports" in edges:
return f"`{name}`{loc} imports this symbol — the import path breaks on rename."
if change_type == "removal":
if "inherits" in edges:
return f"`{name}`{loc} inherits from this class — removing it collapses the entire class hierarchy at this point."
if "calls" in edges:
return f"`{name}`{loc} calls this directly — removal raises `AttributeError` here at runtime."
return f"`{name}`{loc} depends on this existing — removal causes `NameError` or `ImportError` at this file."
if change_type == "dimension_change":
if shapes:
return f"`{name}`{loc} has shape contract{shape_hint} — the dimension change propagates into this contract."
if "produces_tensor_for" in edges or "consumes_format" in edges:
return f"`{name}`{loc} is in the tensor flow path — a dimension change cascades here via shape-dependent operations."
if change_type == "config_change":
if "reads_config" in edges:
return f"`{name}`{loc} reads this config key — a rename or removal breaks the lookup silently (returns None, not an exception)."
# ── Generic fallback — role-specific, never generic ──────────────────────
# Use node role + edge type to produce a specific sentence rather than the
# "appears in blast radius" canned phrase.
if is_hook:
return f"Lightning hook `{name}`{loc} — framework contract at this boundary will be affected."
if name.startswith("test_") or "tests" in node_id:
return f"`{name}`{loc} tests the behaviour of `{origin}` — will need updating to match the new contract."
if ntype == "class":
return f"`{name}`{loc} is a class connected via `{'|'.join(list(edges)[:2]) or 'dependency'}` from `{origin}` — check constructor and method signatures."
if "reads_config" in edges:
return f"`{name}`{loc} reads config from `{origin}` — verify the key still exists and has the expected shape."
if "calls" in edges:
depth_word = "directly" if depth == 1 else f"at depth {depth}"
return f"`{name}`{loc} calls into `{origin}` {depth_word} — update this call site."
if "inherits" in edges:
return f"`{name}`{loc} inherits from `{origin}` — verify compatibility with the new interface."
if "consumes_format" in edges:
return f"`{name}`{loc} consumes output from `{origin}` — the format contract here changes."
if "produces_tensor_for" in edges:
return f"`{name}`{loc} produces tensors consumed by `{origin}` — shape contracts may need updating."
if n_origins > 1:
origins_str = ", ".join(list({r["origin"].split(".")[-1] for r in reasons})[:2])
return f"`{name}`{loc} is in the shared blast radius of `{origins_str}` — verify it handles the new interface."
edge_desc = next(iter(edges), "dependency")
return f"`{name}`{loc} is connected to `{origin}` via `{edge_desc}` at depth {depth}."
n_origins = len({r["origin"] for r in reasons})
file = node_data.get("file_path", "")
fname = file.split("\\")[-1].split("/")[-1] if file else ""
loc = f" ({fname}:{node_data.get('line_no','')})" if fname else ""
# Shape hint for the sentence
shape_hint = ""
if shapes:
k, v = next(iter(shapes.items()))
sv = v.get("shape", v) if isinstance(v, dict) else v
shape_hint = f" — current contract: `{k}: {sv}`"
if change_type == "return_format_change":
if "consumes_format" in edges:
return f"`{name}`{loc} unpacks this return value directly. Adding a tensor shifts the tuple — destructuring breaks here."
if "produces_tensor_for" in edges:
return f"`{name}`{loc} has an explicit tensor shape contract{shape_hint}. The contract must be updated to reflect the new output."
if is_hook:
return f"Lightning hook `{name}`{loc} receives this as its batch input — a format change causes a runtime mismatch at the framework boundary."
if change_type == "add_tensor_component":
if "__init__" in node_id:
return f"`{name}`{loc} is where `register_buffer(...)` must be called — buffers registered here persist across devices, checkpoints, and `.to()` calls."
if "consumes_format" in edges or "produces_tensor_for" in edges:
return f"`{name}`{loc} sits in the tensor data flow{shape_hint}. The new weight tensor must be threaded through here explicitly."
if is_hook:
return f"Lightning hook `{name}`{loc} — this is where the buffer gets applied to the loss. It must be in scope here."
if change_type == "signature_change":
if "calls" in edges:
return f"`{name}`{loc} calls this directly at depth {depth} — all argument lists at this call site need updating."
if "inherits" in edges:
return f"`{name}`{loc} inherits this method — the override signature must remain compatible or the MRO breaks."
if "overrides_hook" in edges:
return f"`{name}`{loc} implements a Lightning hook — the framework enforces the signature contract, changing it breaks training."
if change_type == "rename":
if "named_reference" in edges:
return f"`{name}`{loc} references this name in a docstring or string literal — won't break at runtime but becomes stale documentation."
if "calls" in edges:
return f"`{name}`{loc} calls this by name — call sites break immediately on rename."
if "imports" in edges:
return f"`{name}`{loc} imports this symbol — the import path breaks on rename."
if change_type == "removal":
if "inherits" in edges:
return f"`{name}`{loc} inherits from this class — removing it collapses the entire class hierarchy at this point."
if "calls" in edges:
return f"`{name}`{loc} calls this directly — removal raises `AttributeError` here at runtime."
return f"`{name}`{loc} depends on this existing — removal causes `NameError` or `ImportError` at this file."
if change_type == "dimension_change":
if shapes:
return f"`{name}`{loc} has shape contract{shape_hint} — the dimension change propagates into this contract."
if "produces_tensor_for" in edges or "consumes_format" in edges: