Repository navigation
Expand file tree
/
Copy pathobservations.py
More file actions
198 lines (175 loc) · 7.56 KB
/
Copy pathobservations.py
File metadata and controls
198 lines (175 loc) · 7.56 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
"""Opt-in, bounded Python call observations for independent checkers.
Observations explain a failure; they never set acceptance or expected values.
Install only inside an isolated checker process. No oracle-specific function names.
"""
import functools
import inspect
from pathlib import Path
import sys
import builtins
import threading
from contextlib import contextmanager
def describe(value, depth=0):
kind = type(value).__name__
if value is None or isinstance(value, (bool, int, float)):
return {"type": kind, "value": value}
if isinstance(value, str):
return {"type": kind, "value": value[:300], "length": len(value)}
if depth < 2 and isinstance(value, dict):
return {
"type": kind,
"length": len(value),
"items": {str(k)[:100]: describe(v, depth + 1) for k, v in list(value.items())[:12]},
}
if depth < 2 and isinstance(value, (list, tuple)):
return {
"type": kind,
"length": len(value),
"items": [describe(v, depth + 1) for v in value[:8]],
}
return {"type": kind}
class PythonObservationRecorder:
def __init__(self, line_limit=100000):
if isinstance(line_limit, bool) or not isinstance(line_limit, int) or line_limit < 1:
raise ValueError("line_limit must be a positive int")
self.line_limit, self.last = line_limit, None
@contextmanager
def imports(self, workspace):
"""Observe normally imported workspace modules in a single checker process."""
root, seen = Path(workspace).resolve(), set()
original = builtins.__import__
guard = threading.local()
def observe_import(*args, **kwargs):
if getattr(guard, "active", False):
return original(*args, **kwargs)
guard.active = True
try:
result = original(*args, **kwargs)
for module in list(sys.modules.values()):
filename = getattr(module, "__file__", None)
if not filename or id(module) in seen:
continue
if getattr(getattr(module, "__spec__", None), "_initializing", False):
continue
seen.add(id(module))
path = Path(filename).resolve()
if path.suffix == ".py" and path.is_relative_to(root):
self.instrument(module, path)
return result
finally:
guard.active = False
builtins.__import__ = observe_import
try:
yield
finally:
builtins.__import__ = original
def instrument(self, module, source_path):
source_path = str(Path(source_path).resolve())
for name, fn in list(vars(module).items()):
if inspect.isfunction(fn) and fn.__module__ == module.__name__:
setattr(module, name, self.wrap(fn, source_path))
return module
def wrap(self, fn, source_path):
@functools.wraps(fn)
def invoke(*args, **kwargs):
observation = {
"function": fn.__name__,
"arguments": describe(args),
"keywords": describe(kwargs),
}
try:
observation["parameters"] = {
k: describe(v)
for k, v in inspect.signature(fn).bind(*args, **kwargs).arguments.items()
}
except (ValueError, TypeError):
pass
count, prior = 0, sys.gettrace()
def trace(frame, event, arg):
nonlocal count
if event == "line" and frame.f_code.co_filename == source_path:
count += 1
if count > self.line_limit:
raise RuntimeError(
"candidate exceeded Python line-event budget; check loop progress"
)
return trace
try:
sys.settrace(trace)
result = fn(*args, **kwargs)
observation["returned"] = describe(result)
return result
except Exception as error:
observation["exception"] = {
"type": type(error).__name__,
"message": str(error)[:500],
}
tb = error.__traceback__
while tb:
if tb.tb_frame.f_code.co_filename == source_path:
observation["location"] = {
"path": Path(source_path).name,
"line": tb.tb_lineno,
"locals": {
k: describe(v)
for k, v in sorted(
tb.tb_frame.f_locals.items(),
key=lambda pair: isinstance(pair[1], (list, tuple, dict)),
)[:20]
},
}
tb = tb.tb_next
raise
finally:
sys.settrace(prior)
observation["line_events"] = count
self.last = observation
return invoke
def render_observation(observation):
"""Flatten typed values for small-model consumption without deriving a fix."""
import json
lines = ["Observed call: " + str(observation.get("function", "?"))]
def emit(path, value):
if not isinstance(value, dict):
return
suffix = " = " + json.dumps(value["value"], ensure_ascii=False) if "value" in value else ""
if "length" in value:
suffix += " (length " + str(value["length"]) + ")"
lines.append(path + ": " + str(value.get("type", "?")) + suffix)
if isinstance(value.get("items"), dict):
for key, child in value["items"].items():
emit(path + "." + key, child)
for name, value in observation.get("parameters", {}).items():
emit(name, value)
if observation.get("returned"):
emit("RETURN", observation["returned"])
if observation.get("exception"):
lines.append("EXCEPTION: " + json.dumps(observation["exception"]))
location = observation.get("location") or {}
if location:
lines.append("LOCATION: " + str(location.get("path")) + ":" + str(location.get("line")))
for name, value in location.get("locals", {}).items():
emit("local." + name, value)
return "\n".join(lines)[:3500]
def compact_observation(observation):
"""Keep type/size/scalar evidence before shedding large example buffers."""
def compact(value):
if not isinstance(value, dict):
return value
result = {k: value[k] for k in ("type", "value", "length") if k in value}
if isinstance(value.get("items"), dict):
result["items"] = {k: compact(v) for k, v in value["items"].items()}
return result
result = {
k: observation[k] for k in ("function", "exception", "line_events") if k in observation
}
result["parameters"] = {k: compact(v) for k, v in observation.get("parameters", {}).items()}
if observation.get("returned"):
result["returned"] = compact(observation["returned"])
if observation.get("location"):
location = observation["location"]
result["location"] = {k: location[k] for k in ("path", "line") if k in location}
result["location"]["locals"] = {
k: compact(v) for k, v in location.get("locals", {}).items()
}
return result