Repository navigation
Expand file tree
/
Copy pathtask_source_evidence.py
More file actions
335 lines (320 loc) · 12.9 KB
/
Copy pathtask_source_evidence.py
File metadata and controls
335 lines (320 loc) · 12.9 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
"""Bounded, non-executing source evidence for initial workflow selection.
Import-call edges are static candidates, never runtime binding or edit scope.
Only task-mentioned qualified symbols seed this analysis. Nothing in a repository
is treated as a command, instruction, acceptance result or permission.
"""
import ast
import hashlib
from pathlib import Path
import re
VERSION = 1
REFERENCE = re.compile(r"(?<![\w./])([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)+)(?![\w/])")
def collect(
spec, workspace, *, max_files=24, max_bytes=524288, max_file_bytes=131072, max_depth=12
):
root = Path(workspace).resolve()
cache = {}
unknowns = []
edges = []
aliases = []
symbols = {}
entries = []
expanded = set()
bytes_read = 0
roots = [root]
if (root / "src").is_dir():
roots.append(root / "src")
def unknown(reason):
if reason not in unknowns:
unknowns.append(reason)
def read(path):
nonlocal bytes_read
if path in cache:
return cache[path]
if len(cache) >= max_files:
unknown("source file budget exhausted")
return None
try:
if path.is_symlink() or not path.resolve().is_relative_to(root):
unknown("source path is a symlink or outside workspace")
return None
# Also refuse symlinked parent directories, including symlinks into
# the workspace: their mutable resolution is not this snapshot.
if any(
p.is_symlink()
for p in (path, *path.parents)
if p != root and p.is_relative_to(root)
):
unknown("source path has a symlinked component")
return None
with path.open("rb") as stream:
data = stream.read(max_file_bytes + 1)
if len(data) > max_file_bytes or bytes_read + len(data) > max_bytes:
unknown("source byte budget exhausted")
return None
bytes_read += len(data)
text = data.decode("utf8")
tree = ast.parse(text)
if sum(1 for _ in ast.walk(tree)) > 24000:
unknown("source AST budget exhausted")
return None
except (OSError, UnicodeError, SyntaxError, ValueError, RecursionError):
unknown("source unavailable or not parseable Python")
return None
value = dict(
path=path.relative_to(root).as_posix(),
sha256=hashlib.sha256(data).hexdigest(),
tree=tree,
bindings={},
ambiguous=set(),
)
# Count every module-level store, including conditional imports and
# assignments. A later rebinding must not look like a stable import.
for node in tree.body:
names = []
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
names = [node.name]
elif isinstance(node, (ast.Import, ast.ImportFrom)):
names = [
a.asname or (a.name.split(".")[0] if isinstance(node, ast.Import) else a.name)
for a in node.names
]
else:
for child in ast.walk(node):
if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
names.append(child.name)
elif isinstance(child, ast.Name) and isinstance(
child.ctx, (ast.Store, ast.Del)
):
names.append(child.id)
elif isinstance(child, (ast.Import, ast.ImportFrom)):
names.extend(a.asname or a.name.split(".")[0] for a in child.names)
for name in names:
if name in value["bindings"]:
value["ambiguous"].add(name)
value["bindings"][name] = node
if not isinstance(
node,
(ast.Import, ast.ImportFrom, ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef),
):
value["ambiguous"].update(names)
if any(
isinstance(n, ast.ImportFrom) and any(a.name == "*" for a in n.names) for n in tree.body
):
value["ambiguous"].update(value["bindings"])
unknown("wildcard import may replace module bindings")
cache[path] = value
return value
def module(name):
if not name or any(not p.isidentifier() for p in name.split(".")):
return None
candidates = []
for base in roots:
part = base.joinpath(*name.split("."))
for path in (part.with_suffix(".py"), part / "__init__.py"):
if path.is_file():
candidates.append(path)
if len(candidates) != 1:
if candidates:
unknown("ambiguous Python module: " + name)
return None
path = candidates[0]
value = read(path)
return (name, value, path.name == "__init__.py") if value else None
def imported(record, binding, suffix=""):
name, value, package = record
node = value["bindings"].get(binding)
if binding in value["ambiguous"]:
return None
if isinstance(node, ast.ImportFrom):
parts = name.split(".") if package else name.split(".")[:-1]
if node.level:
if node.level > len(parts):
return None
base = parts[: len(parts) - node.level + 1]
else:
base = []
origin = ".".join(base + ([node.module] if node.module else []))
for alias in node.names:
if (alias.asname or alias.name) == binding and alias.name != "*":
return ".".join(x for x in (origin, alias.name, suffix) if x)
if isinstance(node, ast.Import):
for alias in node.names:
if (alias.asname or alias.name.split(".")[0]) == binding:
prefix = alias.name if alias.asname else alias.name.split(".")[0]
return ".".join(x for x in (prefix, suffix) if x)
return None
def resolve(qualified, seen=()):
if qualified in seen or len(seen) >= max_depth:
unknown("symbol alias cycle or depth limit")
return None
parts = qualified.split(".")
if len(parts) < 2:
return None
record = module(".".join(parts[:-1]))
if not record:
return None
value = record[1]
name = parts[-1]
if name in value["ambiguous"]:
unknown("ambiguous or conditional binding: " + qualified)
return None
node = value["bindings"].get(name)
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
key = value["path"] + "::" + node.name
if key not in symbols and len(symbols) >= 64:
unknown("symbol budget exhausted")
return None
symbols[key] = dict(
symbol=key,
path=value["path"],
source_sha256=value["sha256"],
start=node.lineno,
end=node.end_lineno,
analysis_version=VERSION,
)
return record, node, key
target = imported(record, name)
result = resolve(target, (*seen, qualified)) if target else None
if result:
fact = dict(
path=value["path"],
source_sha256=value["sha256"],
line=node.lineno,
binding=name,
target=target,
resolved_symbol=result[2],
relation="static_import_alias_candidate",
)
if fact not in aliases and len(aliases) < 64:
aliases.append(fact)
return result
def attribute(node):
if isinstance(node, ast.Name):
return [node.id]
if isinstance(node, ast.Attribute):
parent = attribute(node.value)
return parent + [node.attr] if parent else None
return None
def calls(node):
# Nested functions/classes have their own lexical scope. Do not infer
# their execution just because a containing function was referenced.
pending = list(node.body)
while pending:
child = pending.pop()
if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Lambda)):
continue
if isinstance(child, ast.Call):
yield child
pending.extend(ast.iter_child_nodes(child))
def expand(found, depth=0, seen=()):
record, node, key = found
if key in seen:
unknown("static call cycle")
return
if key in expanded:
return
if depth >= max_depth:
unknown("static call depth limit")
return
expanded.add(key)
bound = {a.arg for a in (*node.args.posonlyargs, *node.args.args, *node.args.kwonlyargs)}
if node.args.vararg:
bound.add(node.args.vararg.arg)
if node.args.kwarg:
bound.add(node.args.kwarg.arg)
for child in ast.walk(node):
if isinstance(child, ast.Name) and isinstance(child.ctx, (ast.Store, ast.Del)):
bound.add(child.id)
if child is not node and isinstance(
child, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)
):
bound.add(child.name)
if isinstance(child, ast.ExceptHandler) and child.name:
bound.add(child.name)
if isinstance(child, (ast.MatchAs, ast.MatchStar)) and child.name:
bound.add(child.name)
if isinstance(child, (ast.Import, ast.ImportFrom)):
bound.update(a.asname or a.name.split(".")[0] for a in child.names)
body = node.body
if (
body
and isinstance(body[0], ast.Expr)
and isinstance(body[0].value, ast.Constant)
and isinstance(body[0].value.value, str)
):
body = body[1:]
for call in calls(node):
parts = attribute(call.func)
if not parts or parts[0] in bound:
continue
target = imported(record, parts[0], ".".join(parts[1:]))
if not target:
continue
resolved = resolve(target)
if not resolved:
continue
to = resolved[2]
if to == key:
unknown("static call cycle")
continue
forward = len(body) == 1 and isinstance(body[0], ast.Return) and body[0].value is call
edge = dict(
caller=key,
callee=to,
line=call.lineno,
relation="static_import_call_candidate",
pure_return_call=forward,
caller_sha256=record[1]["sha256"],
callee_sha256=resolved[0][1]["sha256"],
)
if len(edges) >= 96:
unknown("edge budget exhausted")
return
if edge not in edges:
edges.append(edge)
expand(resolved, depth + 1, (*seen, key))
mentions = list(REFERENCE.finditer(spec.task[:24000]))[:12]
for match in mentions:
found = resolve(match.group(1))
if found:
entries.append(
dict(task_span=list(match.span(1)), text=match.group(1), symbol=found[2])
)
expand(found)
consistent = True
for path, value in cache.items():
try:
with path.open("rb") as stream:
data = stream.read(max_file_bytes + 1)
if hashlib.sha256(data).hexdigest() != value["sha256"]:
consistent = False
except OSError:
consistent = False
if not consistent:
unknown("source changed during analysis;all dependency labels withheld")
cross = consistent and (
any(e["caller"].split("::")[0] != e["callee"].split("::")[0] for e in edges)
or any(a["path"] != a["resolved_symbol"].split("::")[0] for a in aliases)
)
if not entries:
unknown("no task-mentioned qualified function resolved")
return dict(
version=VERSION,
task_sha256=hashlib.sha256(spec.task.encode()).hexdigest(),
entries=entries,
symbols=list(symbols.values()),
edges=edges,
aliases=aliases,
source_snapshot_consistent=consistent,
labels=["static_cross_module"] if cross else [],
unknowns=unknowns,
files_read=len(cache),
bytes_read=bytes_read,
model_calls=0,
interpretation="Bounded static import candidates;not runtime execution,semantic correctness,ownership or a modification plan.",
read_dependencies=sorted(
{s["path"] for s in symbols.values()} | {a["path"] for a in aliases}
),
write_set=None,
)