Repository navigation
Expand file tree
/
Copy pathinspection_memory.py
More file actions
181 lines (169 loc) · 6.7 KB
/
Copy pathinspection_memory.py
File metadata and controls
181 lines (169 loc) · 6.7 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
"""Bounded,source-versioned inspection coverage;never model-authored facts."""
import hashlib, json
def bounded_read(result, max_text_chars=3400):
"""Bound serialized text before recording coverage,keeping JSON metadata.
Complete visible lines are observations. A prefix of an oversized single
line is explicitly partial and never counts as having inspected that line.
The full-file hash remains the edit authority.
"""
metadata = {k: v for k, v in result.items() if k != "text"}
max_text_chars = min(
max_text_chars, max(32, 5000 - len(json.dumps(metadata, ensure_ascii=False)))
)
if (
"text" not in result
or len(json.dumps(result["text"], ensure_ascii=False)) <= max_text_chars
):
return result
result = dict(result)
lines = result["text"].splitlines()
visible = []
for line in lines:
if len(json.dumps("\n".join([*visible, line]), ensure_ascii=False)) > max_text_chars:
break
visible.append(line)
result.update(
text="\n".join(visible),
read_truncated=True,
requested_lines=len(lines),
covered_line_count=len(visible),
visible_end=result["start"] + len(visible) - 1 if visible else None,
)
if not visible and lines:
low, high = 0, len(lines[0])
while low < high:
middle = (low + high + 1) // 2
if len(json.dumps(lines[0][:middle], ensure_ascii=False)) <= max_text_chars:
low = middle
else:
high = middle - 1
result.update(
text=lines[0][:low],
partial_line=result["start"],
guidance="Only a prefix of this oversized line is visible;use a focused symbol/text query. This line is not covered.",
)
return result
def bounded_workpack(result):
"""Preserve source identity/progress instead of cutting the outer JSON."""
result = dict(result)
result["source"] = bounded_read(result["source"], 2800)
interfaces = list(result.get("interfaces", []))
while (
interfaces
and len(json.dumps(dict(result, interfaces=interfaces), ensure_ascii=False)) > 4800
):
interfaces.pop()
if len(interfaces) < len(result.get("interfaces", [])):
result["interfaces_truncated"] = True
result["interfaces"] = interfaces
return result
def remember_read(state, result):
if not all(key in result for key in ("path", "sha256", "total_lines", "start", "text")):
return
path = result["path"]
memory = state.setdefault("read_coverage", {})
previous = memory.pop(path, None)
spans = previous["spans"] if previous and previous["sha256"] == result["sha256"] else []
count = result.get("covered_line_count", len(result["text"].splitlines()))
begin = result["start"]
end = min(result["total_lines"], begin + count - 1)
repeated = bool(count and any(a <= begin and b >= end for a, b in spans))
merged = []
for a, b in sorted(spans + ([[begin, end]] if count else [])):
if merged and a <= merged[-1][1] + 1:
merged[-1][1] = max(merged[-1][1], b)
else:
merged.append([a, b])
memory[path] = dict(
sha256=result["sha256"], total_lines=result["total_lines"], spans=merged[-32:]
)
while len(memory) > 16:
del memory[next(iter(memory))]
next_line = 1
for a, b in merged:
if a > next_line:
break
next_line = max(next_line, b + 1)
result["inspection_progress"] = dict(
repeated_unchanged_range=repeated,
next_unread_line=next_line if next_line <= result["total_lines"] else None,
guidance="Already inspected at this source version;reuse its evidence or inspect a different relevant range."
if repeated
else "Coverage records observations only,not task completion.",
)
def current_coverage(state, workspace, max_chars=1600):
memory = state.get("read_coverage", {})
rows = []
for path, value in list(memory.items()):
try:
valid = hashlib.sha256(workspace.path(path).read_bytes()).hexdigest() == value["sha256"]
except (OSError, ValueError):
valid = False
if not valid:
del memory[path]
continue
rows.append(
dict(
path=path,
source_sha256=value["sha256"],
lines=value["total_lines"],
read_spans=value["spans"],
)
)
while rows and len(json.dumps(rows, ensure_ascii=False)) > max_chars:
rows.pop(0)
return rows
def retain_current_reads(recent, workspace, deduplicate=False):
"""Keep only read observations whose file bytes still match their receipt."""
if type(deduplicate) is not bool:
raise ValueError("deduplicate must be bool")
kept = []
for pair in recent:
try:
observation = json.loads(pair[-1]["content"])
if not isinstance(observation, dict):
continue
if isinstance(observation.get("source"), dict):
observation = observation["source"]
if not all(k in observation for k in ("path", "sha256", "text")):
continue
if (
hashlib.sha256(workspace.path(observation["path"]).read_bytes()).hexdigest()
== observation["sha256"]
):
kept.append(pair)
except (KeyError, TypeError, ValueError, OSError):
continue
if not deduplicate:
return kept[-3:]
# Keep newest observations of each exact visible read. Different ranges,
# partial lines,and compound workpack/analysis observations stay distinct.
# Raw paid turns remain in recent/history/events;this only selects context.
selected = []
seen = set()
for pair in reversed(kept):
try:
proposal = json.loads(pair[0]["content"])
observation = json.loads(pair[-1]["content"])
if (
isinstance(proposal, dict)
and proposal.get("tool") == "read"
and all(k in observation for k in ("path", "sha256", "start", "text"))
):
key = (
observation["path"],
observation["sha256"],
observation["start"],
observation["text"],
observation.get("covered_line_count"),
observation.get("partial_line"),
)
if key in seen:
continue
seen.add(key)
except (KeyError, TypeError, ValueError):
pass
selected.append(pair)
if len(selected) == 3:
break
return list(reversed(selected))