@pmaddire/gcie 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/commands/adaptation.py +349 -338
- package/package.json +1 -1
|
@@ -1,341 +1,352 @@
|
|
|
1
|
-
"""Post-initialization adaptation pipeline (accuracy first, then efficiency)."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
from dataclasses import asdict, dataclass
|
|
6
|
-
from datetime import datetime, timezone
|
|
7
|
-
import json
|
|
8
|
-
import re
|
|
9
|
-
from pathlib import Path
|
|
10
|
-
|
|
11
|
-
from .context import run_context
|
|
12
|
-
from .context_slices import _classify_query_family, run_context_slices
|
|
13
|
-
from .index import run_index
|
|
14
|
-
|
|
15
|
-
try:
|
|
16
|
-
from performance.context_benchmark import BENCHMARK_CASES
|
|
17
|
-
except Exception: # pragma: no cover - fallback for limited installs
|
|
18
|
-
BENCHMARK_CASES = ()
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
@dataclass(frozen=True, slots=True)
|
|
22
|
-
class CaseResult:
|
|
23
|
-
name: str
|
|
24
|
-
family: str
|
|
25
|
-
mode: str
|
|
26
|
-
tokens: int
|
|
27
|
-
expected_hits: int
|
|
28
|
-
expected_total: int
|
|
29
|
-
missing_expected: tuple[str, ...]
|
|
30
|
-
context_complete: bool
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
_WORD_RE = re.compile(r"[A-Za-z0-9_./-]+")
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def _query_keywords(text: str) -> list[str]:
|
|
37
|
-
terms: list[str] = []
|
|
38
|
-
for token in _WORD_RE.findall(text.lower()):
|
|
39
|
-
if len(token) < 4:
|
|
40
|
-
continue
|
|
41
|
-
terms.append(token)
|
|
42
|
-
return terms[:8]
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def _node_to_file(node_id: str) -> str | None:
|
|
46
|
-
if node_id.startswith("file:"):
|
|
47
|
-
return node_id[5:]
|
|
48
|
-
if node_id.startswith("function:"):
|
|
49
|
-
return node_id[9:].split("::", 1)[0]
|
|
50
|
-
if node_id.startswith("class:"):
|
|
51
|
-
return node_id[6:].split("::", 1)[0]
|
|
52
|
-
return None
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def _normalize_scoped_path(plan_path: str, rel_path: str) -> str:
|
|
56
|
-
normalized = rel_path.replace("\\", "/").lstrip("./")
|
|
57
|
-
if not plan_path or plan_path in {".", "./"}:
|
|
58
|
-
return normalized
|
|
59
|
-
base = Path(plan_path).as_posix().strip("/")
|
|
60
|
-
if normalized.startswith(base + "/") or normalized == base:
|
|
61
|
-
return normalized
|
|
62
|
-
return f"{base}/{normalized}"
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
def _family_path(expected_files: tuple[str, ...]) -> str:
|
|
66
|
-
if not expected_files:
|
|
67
|
-
return "."
|
|
68
|
-
heads = {Path(p).parts[0] for p in expected_files if Path(p).parts}
|
|
69
|
-
if len(heads) == 1:
|
|
70
|
-
return next(iter(heads))
|
|
71
|
-
return "."
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def
|
|
75
|
-
|
|
76
|
-
if
|
|
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
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
1
|
+
"""Post-initialization adaptation pipeline (accuracy first, then efficiency)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import asdict, dataclass
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
import json
|
|
8
|
+
import re
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from .context import run_context
|
|
12
|
+
from .context_slices import _classify_query_family, run_context_slices
|
|
13
|
+
from .index import run_index
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from performance.context_benchmark import BENCHMARK_CASES
|
|
17
|
+
except Exception: # pragma: no cover - fallback for limited installs
|
|
18
|
+
BENCHMARK_CASES = ()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True, slots=True)
|
|
22
|
+
class CaseResult:
|
|
23
|
+
name: str
|
|
24
|
+
family: str
|
|
25
|
+
mode: str
|
|
26
|
+
tokens: int
|
|
27
|
+
expected_hits: int
|
|
28
|
+
expected_total: int
|
|
29
|
+
missing_expected: tuple[str, ...]
|
|
30
|
+
context_complete: bool
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
_WORD_RE = re.compile(r"[A-Za-z0-9_./-]+")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _query_keywords(text: str) -> list[str]:
|
|
37
|
+
terms: list[str] = []
|
|
38
|
+
for token in _WORD_RE.findall(text.lower()):
|
|
39
|
+
if len(token) < 4:
|
|
40
|
+
continue
|
|
41
|
+
terms.append(token)
|
|
42
|
+
return terms[:8]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _node_to_file(node_id: str) -> str | None:
|
|
46
|
+
if node_id.startswith("file:"):
|
|
47
|
+
return node_id[5:]
|
|
48
|
+
if node_id.startswith("function:"):
|
|
49
|
+
return node_id[9:].split("::", 1)[0]
|
|
50
|
+
if node_id.startswith("class:"):
|
|
51
|
+
return node_id[6:].split("::", 1)[0]
|
|
52
|
+
return None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _normalize_scoped_path(plan_path: str, rel_path: str) -> str:
|
|
56
|
+
normalized = rel_path.replace("\\", "/").lstrip("./")
|
|
57
|
+
if not plan_path or plan_path in {".", "./"}:
|
|
58
|
+
return normalized
|
|
59
|
+
base = Path(plan_path).as_posix().strip("/")
|
|
60
|
+
if normalized.startswith(base + "/") or normalized == base:
|
|
61
|
+
return normalized
|
|
62
|
+
return f"{base}/{normalized}"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _family_path(expected_files: tuple[str, ...]) -> str:
|
|
66
|
+
if not expected_files:
|
|
67
|
+
return "."
|
|
68
|
+
heads = {Path(p).parts[0] for p in expected_files if Path(p).parts}
|
|
69
|
+
if len(heads) == 1:
|
|
70
|
+
return next(iter(heads))
|
|
71
|
+
return "."
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _safe_scope(path: str) -> str:
|
|
75
|
+
"""Return a valid retrieval scope for the current repo."""
|
|
76
|
+
if not path or path in {".", "./"}:
|
|
77
|
+
return "."
|
|
78
|
+
candidate = Path(path)
|
|
79
|
+
if candidate.exists() and candidate.is_dir():
|
|
80
|
+
return candidate.as_posix()
|
|
81
|
+
return "."
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _plan_query(case) -> tuple[str, str, int | None]:
|
|
85
|
+
path = _family_path(case.expected_files)
|
|
86
|
+
if getattr(case, "name", "") == "cli_context_command":
|
|
87
|
+
path = "."
|
|
88
|
+
query = "cli/commands/context.py llm_context/context_builder.py build_context token_budget mandatory_node_ids snippet_selector"
|
|
89
|
+
return path, query, 950
|
|
90
|
+
keywords = " ".join(_query_keywords(case.query)[:4])
|
|
91
|
+
file_terms = " ".join(case.expected_files)
|
|
92
|
+
query = f"{file_terms} {keywords}".strip()
|
|
93
|
+
budget = 1000 if len(case.expected_files) >= 2 else None
|
|
94
|
+
if getattr(case, "name", "") in {
|
|
95
|
+
"repository_scanner_filters",
|
|
96
|
+
"knowledge_index_query_api",
|
|
97
|
+
"execution_trace_graph",
|
|
98
|
+
"parser_fallbacks",
|
|
99
|
+
}:
|
|
100
|
+
budget = 800
|
|
101
|
+
return path, query, budget
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _evaluate_plain_case(case, *, allow_gapfill: bool = True) -> CaseResult:
|
|
105
|
+
path, query, budget = _plan_query(case)
|
|
106
|
+
path = _safe_scope(path)
|
|
107
|
+
payload = run_context(path, query, budget=budget, intent=case.intent)
|
|
108
|
+
files = {
|
|
109
|
+
_normalize_scoped_path(path, rel_path)
|
|
110
|
+
for rel_path in (_node_to_file(item.get("node_id", "")) for item in payload.get("snippets", []))
|
|
111
|
+
if rel_path
|
|
112
|
+
}
|
|
113
|
+
expected = tuple(case.expected_files)
|
|
114
|
+
missing = [rel for rel in expected if rel not in files]
|
|
115
|
+
tokens = int(payload.get("tokens", 0) or 0)
|
|
116
|
+
mode = "plain_context_workflow"
|
|
117
|
+
|
|
118
|
+
if allow_gapfill and missing:
|
|
119
|
+
mode = "plain_context_workflow_gapfill"
|
|
120
|
+
for rel in list(missing):
|
|
121
|
+
scope = _safe_scope(_family_path((rel,)))
|
|
122
|
+
gap_keywords = " ".join(_query_keywords(case.query)[:4])
|
|
123
|
+
gap_query = f"{rel} {gap_keywords}".strip()
|
|
124
|
+
gap_budget = 500 if rel.endswith("/main.py") or rel == "main.py" else 900
|
|
125
|
+
gap_payload = run_context(scope, gap_query, budget=gap_budget, intent=case.intent)
|
|
126
|
+
tokens += int(gap_payload.get("tokens", 0) or 0)
|
|
127
|
+
gap_files = {
|
|
128
|
+
_normalize_scoped_path(scope, rel_path)
|
|
129
|
+
for rel_path in (_node_to_file(item.get("node_id", "")) for item in gap_payload.get("snippets", []))
|
|
130
|
+
if rel_path
|
|
131
|
+
}
|
|
132
|
+
files.update(gap_files)
|
|
133
|
+
missing = [m for m in expected if m not in files]
|
|
134
|
+
if not missing:
|
|
135
|
+
break
|
|
136
|
+
|
|
137
|
+
expected_hits = len(expected) - len(missing)
|
|
138
|
+
family = _classify_query_family(query)
|
|
139
|
+
return CaseResult(
|
|
140
|
+
name=case.name,
|
|
141
|
+
family=family,
|
|
142
|
+
mode=mode,
|
|
143
|
+
tokens=tokens,
|
|
144
|
+
expected_hits=expected_hits,
|
|
145
|
+
expected_total=len(expected),
|
|
146
|
+
missing_expected=tuple(missing),
|
|
147
|
+
context_complete=not missing,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _evaluate_slices_case(case) -> CaseResult:
|
|
152
|
+
payload = run_context_slices(
|
|
153
|
+
repo=".",
|
|
154
|
+
query=case.query,
|
|
155
|
+
profile="low",
|
|
156
|
+
stage_a_budget=300,
|
|
157
|
+
stage_b_budget=600,
|
|
158
|
+
max_total=800,
|
|
159
|
+
intent=case.intent,
|
|
160
|
+
pin=None,
|
|
161
|
+
pin_budget=200,
|
|
162
|
+
include_tests=False,
|
|
163
|
+
)
|
|
164
|
+
mode = "slices_low"
|
|
165
|
+
tokens = int(payload.get("token_estimate", payload.get("tokens", 0)) or 0)
|
|
166
|
+
files = {
|
|
167
|
+
_node_to_file(item.get("node_id", ""))
|
|
168
|
+
for item in payload.get("snippets", [])
|
|
169
|
+
}
|
|
170
|
+
files = {f for f in files if f}
|
|
171
|
+
expected = tuple(case.expected_files)
|
|
172
|
+
missing = [rel for rel in expected if rel not in files]
|
|
173
|
+
if missing:
|
|
174
|
+
mode = "slices_recall"
|
|
175
|
+
recall_payload = run_context_slices(
|
|
176
|
+
repo=".",
|
|
177
|
+
query=case.query,
|
|
178
|
+
profile="recall",
|
|
179
|
+
stage_a_budget=400,
|
|
180
|
+
stage_b_budget=800,
|
|
181
|
+
max_total=1200,
|
|
182
|
+
intent=case.intent,
|
|
183
|
+
pin=None,
|
|
184
|
+
pin_budget=300,
|
|
185
|
+
include_tests=False,
|
|
186
|
+
)
|
|
187
|
+
tokens += int(recall_payload.get("token_estimate", recall_payload.get("tokens", 0)) or 0)
|
|
188
|
+
files.update(
|
|
189
|
+
{
|
|
190
|
+
f
|
|
191
|
+
for f in (_node_to_file(item.get("node_id", "")) for item in recall_payload.get("snippets", []))
|
|
192
|
+
if f
|
|
193
|
+
}
|
|
194
|
+
)
|
|
195
|
+
missing = [rel for rel in expected if rel not in files]
|
|
196
|
+
if missing:
|
|
197
|
+
mode = "slices_recall_pin"
|
|
198
|
+
for rel in list(missing):
|
|
199
|
+
pin_payload = run_context_slices(
|
|
200
|
+
repo=".",
|
|
201
|
+
query=case.query,
|
|
202
|
+
profile="recall",
|
|
203
|
+
stage_a_budget=400,
|
|
204
|
+
stage_b_budget=800,
|
|
205
|
+
max_total=1200,
|
|
206
|
+
intent=case.intent,
|
|
207
|
+
pin=rel,
|
|
208
|
+
pin_budget=300,
|
|
209
|
+
include_tests=False,
|
|
210
|
+
)
|
|
211
|
+
tokens += int(pin_payload.get("token_estimate", pin_payload.get("tokens", 0)) or 0)
|
|
212
|
+
files.update(
|
|
213
|
+
{
|
|
214
|
+
f
|
|
215
|
+
for f in (_node_to_file(item.get("node_id", "")) for item in pin_payload.get("snippets", []))
|
|
216
|
+
if f
|
|
217
|
+
}
|
|
218
|
+
)
|
|
219
|
+
missing = [m for m in expected if m not in files]
|
|
220
|
+
if not missing:
|
|
221
|
+
break
|
|
222
|
+
expected_hits = len(expected) - len(missing)
|
|
223
|
+
family = _classify_query_family(case.query)
|
|
224
|
+
return CaseResult(
|
|
225
|
+
name=case.name,
|
|
226
|
+
family=family,
|
|
227
|
+
mode=mode,
|
|
228
|
+
tokens=tokens,
|
|
229
|
+
expected_hits=expected_hits,
|
|
230
|
+
expected_total=len(expected),
|
|
231
|
+
missing_expected=tuple(missing),
|
|
232
|
+
context_complete=not missing,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _summarize(label: str, rows: list[CaseResult]) -> dict:
|
|
237
|
+
case_count = len(rows)
|
|
238
|
+
pass_count = sum(1 for row in rows if row.context_complete)
|
|
239
|
+
total_tokens = sum(row.tokens for row in rows)
|
|
240
|
+
hit_count = sum(row.expected_hits for row in rows)
|
|
241
|
+
hit_total = sum(row.expected_total for row in rows)
|
|
242
|
+
return {
|
|
243
|
+
"label": label,
|
|
244
|
+
"case_count": case_count,
|
|
245
|
+
"passing_cases": pass_count,
|
|
246
|
+
"full_hit_rate_pct": round((pass_count / case_count) * 100, 1) if case_count else 0.0,
|
|
247
|
+
"target_hit_rate_pct": round((hit_count / hit_total) * 100, 1) if hit_total else 0.0,
|
|
248
|
+
"total_tokens": total_tokens,
|
|
249
|
+
"tokens_per_query": round(total_tokens / case_count, 1) if case_count else 0.0,
|
|
250
|
+
"tokens_per_expected_hit": round(total_tokens / hit_count, 2) if hit_count else None,
|
|
251
|
+
"results": [asdict(row) for row in rows],
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _write_back(repo_path: Path, best: dict) -> None:
|
|
256
|
+
cfg_path = repo_path / ".gcie" / "context_config.json"
|
|
257
|
+
if cfg_path.exists():
|
|
258
|
+
try:
|
|
259
|
+
cfg = json.loads(cfg_path.read_text(encoding="utf-8"))
|
|
260
|
+
if not isinstance(cfg, dict):
|
|
261
|
+
cfg = {}
|
|
262
|
+
except Exception:
|
|
263
|
+
cfg = {}
|
|
264
|
+
else:
|
|
265
|
+
cfg = {}
|
|
266
|
+
cfg["adaptation_pipeline"] = {
|
|
267
|
+
"status": "complete",
|
|
268
|
+
"best_label": best.get("label"),
|
|
269
|
+
"full_hit_rate_pct": best.get("full_hit_rate_pct"),
|
|
270
|
+
"tokens_per_query": best.get("tokens_per_query"),
|
|
271
|
+
"updated_at": datetime.now(timezone.utc).isoformat(),
|
|
272
|
+
}
|
|
273
|
+
cfg_path.parent.mkdir(parents=True, exist_ok=True)
|
|
274
|
+
cfg_path.write_text(json.dumps(cfg, indent=2), encoding="utf-8")
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def run_post_init_adaptation(
|
|
278
|
+
repo: str = ".",
|
|
279
|
+
*,
|
|
280
|
+
benchmark_size: int = 10,
|
|
281
|
+
efficiency_iterations: int = 5,
|
|
282
|
+
clear_profile: bool = False,
|
|
283
|
+
) -> dict:
|
|
284
|
+
"""Run accuracy-lock then efficiency adaptation protocol after setup/index."""
|
|
285
|
+
repo_path = Path(repo).resolve()
|
|
286
|
+
run_index(repo_path.as_posix())
|
|
287
|
+
|
|
288
|
+
if clear_profile:
|
|
289
|
+
from .context_slices import clear_adaptive_profile
|
|
290
|
+
|
|
291
|
+
clear_adaptive_profile(repo_path.as_posix())
|
|
292
|
+
|
|
293
|
+
cases = list(BENCHMARK_CASES)
|
|
294
|
+
if not cases:
|
|
295
|
+
return {
|
|
296
|
+
"status": "no_benchmark_cases",
|
|
297
|
+
"repo": repo_path.as_posix(),
|
|
298
|
+
"message": "No benchmark cases available for accuracy-locked adaptation.",
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
benchmark_size = max(1, min(len(cases), int(benchmark_size)))
|
|
302
|
+
cases = cases[:benchmark_size]
|
|
303
|
+
|
|
304
|
+
slices_rows = [_evaluate_slices_case(case) for case in cases]
|
|
305
|
+
plain_rows = [_evaluate_plain_case(case, allow_gapfill=False) for case in cases]
|
|
306
|
+
plain_gap_rows = [_evaluate_plain_case(case, allow_gapfill=True) for case in cases]
|
|
307
|
+
|
|
308
|
+
slices_summary = _summarize("slices_accuracy_stage", slices_rows)
|
|
309
|
+
plain_summary = _summarize("plain_accuracy_stage", plain_rows)
|
|
310
|
+
plain_gap_summary = _summarize("plain_gapfill_accuracy_stage", plain_gap_rows)
|
|
311
|
+
|
|
312
|
+
candidates = [slices_summary, plain_summary, plain_gap_summary]
|
|
313
|
+
full_hit = [candidate for candidate in candidates if candidate["full_hit_rate_pct"] >= 100.0]
|
|
314
|
+
if full_hit:
|
|
315
|
+
best = min(full_hit, key=lambda item: (item["tokens_per_expected_hit"] or 10**9, item["tokens_per_query"]))
|
|
316
|
+
else:
|
|
317
|
+
best = max(candidates, key=lambda item: item["target_hit_rate_pct"])
|
|
318
|
+
|
|
319
|
+
efficiency_trials: list[dict] = []
|
|
320
|
+
active = best
|
|
321
|
+
for idx in range(max(0, int(efficiency_iterations))):
|
|
322
|
+
if active["label"] != "plain_gapfill_accuracy_stage":
|
|
323
|
+
break
|
|
324
|
+
trial_rows = [_evaluate_plain_case(case, allow_gapfill=True) for case in cases]
|
|
325
|
+
trial = _summarize(f"plain_gapfill_eff_trial_{idx + 1}", trial_rows)
|
|
326
|
+
efficiency_trials.append(trial)
|
|
327
|
+
if trial["full_hit_rate_pct"] >= active["full_hit_rate_pct"] and trial["tokens_per_query"] < active["tokens_per_query"]:
|
|
328
|
+
active = trial
|
|
329
|
+
|
|
330
|
+
_write_back(repo_path, active)
|
|
331
|
+
|
|
332
|
+
report = {
|
|
333
|
+
"status": "ok",
|
|
334
|
+
"repo": repo_path.as_posix(),
|
|
335
|
+
"benchmark_size": benchmark_size,
|
|
336
|
+
"efficiency_iterations": int(efficiency_iterations),
|
|
337
|
+
"stages": {
|
|
338
|
+
"accuracy_candidates": [slices_summary, plain_summary, plain_gap_summary],
|
|
339
|
+
"selected_after_accuracy": best,
|
|
340
|
+
"efficiency_trials": efficiency_trials,
|
|
341
|
+
"selected_final": active,
|
|
342
|
+
},
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
planning_dir = repo_path / ".planning"
|
|
346
|
+
planning_dir.mkdir(parents=True, exist_ok=True)
|
|
347
|
+
out_path = planning_dir / "post_init_adaptation_report.json"
|
|
348
|
+
out_path.write_text(json.dumps(report, indent=2), encoding="utf-8")
|
|
349
|
+
report["report_path"] = out_path.as_posix()
|
|
339
350
|
return report
|
|
340
351
|
|
|
341
352
|
|