brianbellx commited on
Commit
bdd6188
·
verified ·
1 Parent(s): c7ad0b4

Reject cleanups that break references

Browse files

Adds check_reference_integrity (vendored from the prototype): a cleanup is rejected when it introduces undefined names, deletes a function that is still called, or calls a method that does not exist.

Files changed (2) hide show
  1. server/guards.py +9 -1
  2. server/vendored_guards.py +102 -0
server/guards.py CHANGED
@@ -4,7 +4,12 @@ from dataclasses import dataclass, field
4
  from typing import Any
5
 
6
  from .hygiene import score_dict
7
- from .vendored_guards import check_basic_syntax, is_bloated, is_gutted
 
 
 
 
 
8
  from .vendored_pipeline import apply_changes
9
 
10
 
@@ -48,6 +53,9 @@ def verify_patch(
48
  if filename and not check_basic_syntax(updated, filename):
49
  rejected.reason = "invalid_syntax"
50
  return rejected
 
 
 
51
  if is_gutted(content, updated):
52
  rejected.reason = "gutted_file"
53
  return rejected
 
4
  from typing import Any
5
 
6
  from .hygiene import score_dict
7
+ from .vendored_guards import (
8
+ check_basic_syntax,
9
+ check_reference_integrity,
10
+ is_bloated,
11
+ is_gutted,
12
+ )
13
  from .vendored_pipeline import apply_changes
14
 
15
 
 
53
  if filename and not check_basic_syntax(updated, filename):
54
  rejected.reason = "invalid_syntax"
55
  return rejected
56
+ if filename and not check_reference_integrity(content, updated, filename):
57
+ rejected.reason = "invalid_syntax"
58
+ return rejected
59
  if is_gutted(content, updated):
60
  rejected.reason = "gutted_file"
61
  return rejected
server/vendored_guards.py CHANGED
@@ -2,7 +2,40 @@
2
  from __future__ import annotations
3
 
4
  import ast
 
5
  from pathlib import Path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
 
8
  def infer_lang_from_name(filename: str) -> str:
@@ -28,6 +61,75 @@ def count_non_comment_lines(code: str) -> int:
28
  return total
29
 
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def check_basic_syntax(code: str, filename: str) -> bool:
32
  if not code.strip():
33
  return False
 
2
  from __future__ import annotations
3
 
4
  import ast
5
+ import builtins
6
  from pathlib import Path
7
+ import re
8
+ import symtable
9
+
10
+ _JS_METHOD_SKIP = {
11
+ "if",
12
+ "for",
13
+ "while",
14
+ "switch",
15
+ "catch",
16
+ "with",
17
+ "return",
18
+ "function",
19
+ "constructor",
20
+ "super",
21
+ }
22
+ _PY_BUILTINS = set(dir(builtins)) | {
23
+ "__file__",
24
+ "__name__",
25
+ "__doc__",
26
+ "__package__",
27
+ "__spec__",
28
+ "__annotations__",
29
+ }
30
+ _LOCAL_DECL_RE = re.compile(
31
+ r"(?:^\s*(?:export\s+)?(?:async\s+)?(?:function|class|fn|struct)\s+([A-Za-z_$][A-Za-z0-9_$]*)\b"
32
+ r"|^\s*func\s+(?:\([^)\n]+\)\s*)?([A-Za-z_$][A-Za-z0-9_$]*)\b"
33
+ r"|^\s{2,8}(?:private\s+|public\s+|protected\s+|readonly\s+|static\s+|async\s+|pub\s+)*"
34
+ r"(?:fn\s+)?([A-Za-z_$][A-Za-z0-9_$]*)(?:<[^>(\n]+>)?\s*\()",
35
+ re.MULTILINE,
36
+ )
37
+ _RECEIVER_CALL_RE = re.compile(r"\b(?:this|self)\.([A-Za-z_$][A-Za-z0-9_$]*)\s*\(")
38
+ _PY_DEF_RE = re.compile(r"^\s*(?:async\s+)?def\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(", re.MULTILINE)
39
 
40
 
41
  def infer_lang_from_name(filename: str) -> str:
 
61
  return total
62
 
63
 
64
+ def _unresolved_py_globals(code: str) -> set[str]:
65
+ try:
66
+ top = symtable.symtable(code, "<mem>", "exec")
67
+ except SyntaxError:
68
+ return set()
69
+ top_defs = {
70
+ sym.get_name()
71
+ for sym in top.get_symbols()
72
+ if sym.is_assigned() or sym.is_imported() or sym.is_namespace()
73
+ }
74
+ missing = {
75
+ sym.get_name()
76
+ for sym in top.get_symbols()
77
+ if sym.is_referenced()
78
+ and sym.get_name() not in top_defs
79
+ and sym.get_name() not in _PY_BUILTINS
80
+ }
81
+ stack = list(top.get_children())
82
+ while stack:
83
+ tbl = stack.pop()
84
+ stack.extend(tbl.get_children())
85
+ if tbl.get_type() != "function":
86
+ continue
87
+ for sym in tbl.get_symbols():
88
+ if not (sym.is_global() and sym.is_referenced()):
89
+ continue
90
+ name = sym.get_name()
91
+ if name not in top_defs and name not in _PY_BUILTINS:
92
+ missing.add(name)
93
+ return missing
94
+
95
+
96
+ def _extract_local_defs(code: str) -> set[str]:
97
+ found: set[str] = set()
98
+ for match in _LOCAL_DECL_RE.finditer(code):
99
+ sym = match.group(1) or match.group(2) or match.group(3) or ""
100
+ if sym and sym not in _JS_METHOD_SKIP:
101
+ found.add(sym)
102
+ return found
103
+
104
+
105
+ def check_reference_integrity(before_code: str, after_code: str, filename: str) -> bool:
106
+ lang = infer_lang_from_name(filename)
107
+ if lang == "python":
108
+ if _unresolved_py_globals(after_code) - _unresolved_py_globals(before_code):
109
+ return False
110
+ py_defs_b = set(_PY_DEF_RE.findall(before_code))
111
+ py_defs_a = set(_PY_DEF_RE.findall(after_code))
112
+ self_b = set(_RECEIVER_CALL_RE.findall(before_code))
113
+ self_a = set(_RECEIVER_CALL_RE.findall(after_code))
114
+ if (py_defs_b - py_defs_a) & self_a:
115
+ return False
116
+ if (self_a - self_b) - py_defs_a:
117
+ return False
118
+ return True
119
+
120
+ if lang in ("javascript", "typescript", "rust", "go"):
121
+ defs_b = _extract_local_defs(before_code)
122
+ defs_a = _extract_local_defs(after_code)
123
+ for dropped in defs_b - defs_a:
124
+ if re.search(rf"(?:^|[^.\w$]){re.escape(dropped)}\s*\(", after_code):
125
+ return False
126
+ recv_b = set(_RECEIVER_CALL_RE.findall(before_code))
127
+ recv_a = set(_RECEIVER_CALL_RE.findall(after_code))
128
+ if (recv_a - recv_b) - defs_a:
129
+ return False
130
+ return True
131
+
132
+
133
  def check_basic_syntax(code: str, filename: str) -> bool:
134
  if not code.strip():
135
  return False