Repository navigation
Expand file tree
/
Copy pathworking_set.py
More file actions
181 lines (171 loc) · 6.5 KB
/
Copy pathworking_set.py
File metadata and controls
181 lines (171 loc) · 6.5 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
"""Put versioned implementation excerpts before optional memory metadata.
The existing TaskMemory selects/validates sources and relationships. Its bounded
local retrieval buffer is never sent to the model as-is. This renderer admits
whole JSON records against the caller tokenizer, retaining source locations and
uncertainty. It creates no read receipts, edits, commands or semantic proofs.
"""
import json
def _json(value):
return json.dumps(value, ensure_ascii=False, separators=(",", ":"))
def _windows(row, anchor=None):
"""Smaller literal source windows always retain an in-range anchor line."""
lines = row["excerpt"].splitlines()
first = row["excerpt_line"]
if not lines:
return
target = anchor if type(anchor) is int and first <= anchor < first + len(lines) else first
offset = target - first
for count in dict.fromkeys(
(len(lines), min(12, len(lines)), min(6, len(lines)), min(3, len(lines)), 1)
):
start = max(0, min(offset - count // 3, len(lines) - count))
end = start + count
# A partial window is an excerpt, never a rewritten/complete function.
yield dict(
symbol=row.get("symbol"),
path=row["path"],
source_sha256=row["source_sha256"],
excerpt="\n".join(lines[start:end]),
excerpt_line=first + start,
excerpt_end=first + end - 1,
implementation_excerpt_complete=bool(row.get("implementation_excerpt_complete"))
and start == 0
and end == len(lines),
**({"unparsed_source": True} if row.get("unparsed_source") else {}),
)
def pack(memory, choice, query, focus, max_tokens, estimator, exclude_paths=()):
header = dict(
working_code_question=dict(
view=choice["view"],
direction=choice["direction"],
basis=choice["selection_basis"],
selection_is_hypothesis=True,
policy="Source syntax only;alias/ownership/dynamic dispatch unknown. Excerpts grant no read/write authority.",
)
)
prefix = _json(header)
records = [prefix]
selected = []
omitted = 0
fallback_count = 0
report = dict(
tokens=0,
selected=[],
fallback_sources_included=0,
source_body_included=False,
included=False,
encoding="source_first_v1",
max_tokens=max_tokens,
)
if estimator.estimate(prefix) > max_tokens:
return "", dict(report, omitted_for_budget=True)
def offer(row):
text = "\n".join([*records, _json(row)])
if estimator.estimate(text) > max_tokens:
return False
records.append(_json(row))
return True
anchors = [
dict(symbol=row["symbol"], line=row["line"], reason=row["basis"])
for row in choice["anchors"]
]
locations = {row["symbol"]: row["line"] for row in choice["anchors"]}
# Bounded local search is separate from the actual model context budget.
# No additional neural request or new index/answer source is introduced.
scratch_budget = max(2400, min(8000, max_tokens * 4))
body, retrieval = memory.pack(
query,
scratch_budget,
estimator,
focus_paths=focus,
exclude_paths=exclude_paths,
view=choice["view"],
anchors=anchors,
direction=choice["direction"],
)
candidates = [json.loads(line) for line in body.splitlines()]
source_rows = [row for row in candidates if "excerpt" in row]
for fallback in choice["fallback_sources"]:
source_rows.insert(
0,
dict(
symbol=None,
path=fallback["path"],
source_sha256=fallback["source_sha256"],
excerpt=fallback["source"],
excerpt_line=fallback["start"],
excerpt_end=fallback["end"],
implementation_excerpt_complete=False,
unparsed_source=True,
anchor=fallback["line"],
),
)
for row in source_rows:
admitted = False
for window in _windows(row, row.get("anchor", locations.get(row.get("symbol")))):
visible = window
if row.get("unparsed_source"):
visible = dict(
current_unparsed_source=dict(
path=window["path"],
source_sha256=window["source_sha256"],
start=window["excerpt_line"],
end=window["excerpt_end"],
source=window["excerpt"],
relations="unknown",
)
)
if offer(visible):
selected.append({key: value for key, value in window.items() if key != "excerpt"})
fallback_count += bool(row.get("unparsed_source"))
admitted = True
break
if not admitted:
omitted += 1
source_count = len(selected)
# Attach only whole semantic observations, with their original uncertainty
# and source version. Do not shorten expressions or turn candidates into
# definite alias, value, dispatch or ownership statements.
facts = 0
for row in candidates:
if (
"excerpt" in row
or "task_memory" in row
or row.get("source_omitted")
or "memory_omissions" in row
):
continue
if "function_summary" in row or "use_def_of" in row:
if offer(row):
facts += 1
else:
omitted += 1
if not source_count and not facts:
return "", dict(
report,
omitted_for_budget=True,
retrieval_report=retrieval,
reason="No current source observation fits;routing metadata alone is not code memory.",
)
more = omitted + retrieval.get("omitted", 0) + retrieval.get("frontier", 0)
if more:
offer(
dict(
more_source_available=True,
expand="read/function_summary/use_def;omitted is not irrelevant",
)
)
text = "\n".join(records)
return text, dict(
report,
tokens=estimator.estimate(text),
selected=selected,
fallback_sources_included=fallback_count,
source_body_included=bool(source_count),
semantic_records=facts,
included=True,
omitted=more,
retrieval_report=retrieval,
local_retrieval_budget=scratch_budget,
source_records=source_count,
)