Repository navigation
Expand file tree
/
Copy pathnative_repair_run.py
More file actions
279 lines (272 loc) · 13.1 KB
/
Copy pathnative_repair_run.py
File metadata and controls
279 lines (272 loc) · 13.1 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
"""Run two bounded repair phases with shared transport and independent final acceptance."""
from dataclasses import asdict, replace
import hashlib
import json
from pathlib import Path
import signal
import sys
import threading
import time
from infinicoder.runtime.agent_execution import (
CoreOutcome,
FinalAcceptance,
ModelBinding,
NativeTaskBinding,
NATIVE_CORE,
UnsupportedCore,
)
from infinicoder.runtime.native_run import CoreDeadline, _deadline_binding, _initial_messages
from infinicoder.runtime.extensions.native_repair import RepairHandoff, RepairStagnation
def run_with_repair(request):
if sys.platform != "linux" or threading.current_thread() is not threading.main_thread():
raise UnsupportedCore(
"native core requires a Linux main-thread process for strict deadlines"
)
if signal.getitimer(signal.ITIMER_REAL)[0]:
raise UnsupportedCore("native core cannot replace an existing wall alarm")
state = Path(request.state_dir).resolve()
state.mkdir(parents=True, exist_ok=True)
marker = state / "native-run.json"
with marker.open("x", encoding="utf-8") as stream:
json.dump(
dict(
status="started", backend=NATIVE_CORE, continuation=request.repair_adapter.identity
),
stream,
)
deadline = time.monotonic() + request.budget.wall_seconds
from infinicoder.runtime.native_agent import NativeAgent
from infinicoder.models.infinilm_client import InfiniLMTextModel, default_config
config = default_config()
memory, repair = request.context_adapter, request.repair_adapter
manifest = state / "native-phases.json"
phases = []
final = None
error = acceptance_error = None
result = {}
total_calls = 0
ledger_ref = ""
trajectory = state / "ordinary/native-trajectory.json"
interrupted = False
try:
# Open each original caller context exactly once for the whole task.
with request.transport_adapter.open(request, deadline) as binding:
if not isinstance(binding, ModelBinding) or not binding.usage_ledger_ref:
raise ValueError("transport must yield the original ModelBinding and usage ledger")
ledger_ref = binding.usage_ledger_ref
with request.execution_adapter.open(request, deadline) as environment:
with request.acceptance_adapter.public_command(request, deadline) as command:
if isinstance(command, NativeTaskBinding):
if command.task != request.spec.task:
raise UnsupportedCore(
"verbatim binding changed the public TaskSpec task"
)
public_task, mode, identity = (
command.task,
"caller_verbatim",
command.identity,
)
elif isinstance(command, str) and command.strip():
public_task = (
request.spec.task
+ "\nWorking directory: /workspace. Independent acceptance command:\n"
+ command
)
mode, identity = (
"legacy_public_command",
request.acceptance_adapter.identity,
)
else:
raise UnsupportedCore(
"public task context must yield a command or NativeTaskBinding"
)
(state / "native-public-task.json").write_text(
json.dumps(
dict(
mode=mode,
identity=identity,
task=public_task,
task_sha256=hashlib.sha256(public_task.encode()).hexdigest(),
spec_task_sha256=hashlib.sha256(
request.spec.task.encode()
).hexdigest(),
final_acceptance="Independent verifier required;this binding proves no result.",
),
ensure_ascii=False,
indent=2,
),
encoding="utf-8",
)
task = public_task
for phase in ("ordinary", "repair"):
remaining_calls = request.budget.max_model_calls - total_calls
remaining_wall = deadline - time.monotonic()
if remaining_calls <= 0 or remaining_wall <= 0:
result = dict(
exit_status="LimitsExceeded"
if remaining_calls <= 0
else "TaskDeadline",
submission="",
)
break
phase_state = state / phase
phase_state.mkdir(exist_ok=False)
phase_request = replace(request, state_dir=phase_state)
trajectory = phase_state / "native-trajectory.json"
kwargs = (
dict(config["model"].get("model_kwargs", {})) | binding.model_kwargs
)
kwargs.update(
max_tokens=request.model_profile.output_reserve,
timeout=min(request.budget.request_timeout, remaining_wall),
num_retries=0,
)
model = InfiniLMTextModel(
**{
k: v
for k, v in config["model"].items()
if k not in ("model_kwargs", "model_name")
},
model_name=binding.model_name,
model_kwargs=kwargs,
)
agent = NativeAgent(
model,
environment,
**(
config["agent"]
| dict(
step_limit=remaining_calls,
wall_time_limit_seconds=max(1, int(remaining_wall)),
cost_limit=0,
output_path=trajectory,
)
),
)
def preview(repair_task):
left = request.budget.max_model_calls - total_calls - agent.n_calls
if left <= 0 or deadline <= time.monotonic():
return False, dict(
status="not_applicable;no_original_budget_remaining"
)
prepared = _initial_messages(agent, repair_task)
tokens = memory._count(prepared)
return tokens <= memory.limit, dict(
input_tokens=tokens,
input_limit=memory.limit,
remaining_agent_calls=left,
status="fits"
if tokens <= memory.limit
else "not_applicable;complete_repair_contract_does_not_fit",
)
handed_off = False
phase_result = {}
started = time.monotonic()
try:
with repair.bind_query(
phase_request,
model,
memory,
phase=phase,
public_task=public_task,
preview=preview,
):
with memory.bind(phase_request, model, environment, deadline):
with _deadline_binding(model, environment, deadline):
phase_result = agent.run(task)
except RepairHandoff as transfer:
if phase != "ordinary":
raise RuntimeError("a repair phase cannot hand off again")
# All hooks and the outer SIGALRM have unwound.
if signal.getitimer(signal.ITIMER_REAL)[0]:
raise RuntimeError("parent alarm survived repair handoff")
handed_off = True
task = transfer.task
phase_result = dict(
exit_status="NativeRepairHandoff",
submission="",
seed_ref=str(state / "native-repair-seed.json"),
no_HTTP_for_handoff_query=True,
)
except RepairStagnation as exc:
phase_result = dict(
exit_status="NativeRepairStagnation", submission="", reason=str(exc)
)
except BaseException as exc:
phase_result = dict(exit_status=type(exc).__name__, submission="")
raise
finally:
agent.save(trajectory)
total_calls += agent.n_calls
phases.append(
dict(
phase=phase,
sdk_agent_calls=agent.n_calls,
sdk_step_limit=remaining_calls,
shared_deadline=deadline,
trajectory=str(trajectory),
state_dir=str(phase_state),
result=phase_result,
wall_seconds=time.monotonic() - started,
usage_ledger_ref=ledger_ref,
physical_requests="read_original_shared_ledger;not_agent_calls",
)
)
manifest.write_text(
json.dumps(
dict(
phases=phases,
total_agent_calls=total_calls,
max_agent_calls=request.budget.max_model_calls,
shared_deadline=deadline,
conservative_handoff_call_charge=True,
usage_ledger_ref=ledger_ref,
),
ensure_ascii=False,
indent=2,
),
encoding="utf-8",
)
result = phase_result
if not handed_off:
break
except CoreDeadline as exc:
error = f"{type(exc).__name__}: {exc}"
result = dict(exit_status="TaskDeadline", submission="")
except KeyboardInterrupt as exc:
interrupted = True
error = f"{type(exc).__name__}: {exc}"
result = dict(exit_status="Interrupted", submission="")
except Exception as exc:
error = f"{type(exc).__name__}: {exc}"
result = dict(exit_status=type(exc).__name__, submission="")
if not interrupted:
try:
final = request.acceptance_adapter.verify(request)
if not isinstance(final, FinalAcceptance):
raise TypeError("independent verifier must return FinalAcceptance")
except Exception as exc:
final = None
acceptance_error = f"{type(exc).__name__}: {exc}"
result = dict(
result,
phase_manifest_ref=str(manifest),
repair_handoff_requested=repair.used,
repair_activated=any(p["phase"] == "repair" for p in phases),
phase_count=len(phases),
handoff_calls_are_not_HTTP=True,
)
outcome = CoreOutcome(
NATIVE_CORE,
result.get("exit_status", "Unknown"),
str(Path(request.workspace).resolve()),
str(trajectory),
ledger_ref,
result,
final,
total_calls,
error,
acceptance_error,
)
marker.write_text(json.dumps(asdict(outcome), ensure_ascii=False, indent=2), encoding="utf-8")
return outcome