Repository navigation
Expand file tree
/
Copy pathnative_single_history.py
More file actions
117 lines (104 loc) · 4.16 KB
/
Copy pathnative_single_history.py
File metadata and controls
117 lines (104 loc) · 4.16 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
"""Expose the existing per-phase history budget in one ordinary SDK run."""
from contextlib import contextmanager
import hashlib
import json
from pathlib import Path
from infinicoder.runtime.agent_execution import UnsupportedCore
from infinicoder.memory.native_phase_history import NativePhaseHistoryContext, PhaseHistoryBinding
from infinicoder.runtime.native_plain import NativePlainObserver
class NativeSinglePhaseHistory:
identity = "native_single_phase_history_v1"
def __init__(self, count_messages, *, tokenizer_identity, implementation_paths=()):
self.counter = count_messages
self.tokenizer_identity = tokenizer_identity
self.paths = tuple(implementation_paths)
self.history = None
self._used = self._active = False
def validate(self, request):
if any(
value is not None
for value in (
request.candidate_search,
request.repair_adapter,
request.planning_adapter,
)
):
raise UnsupportedCore("single-run history cannot enable search,repair or planning")
NativePlainObserver(
self.counter,
tokenizer_identity=self.tokenizer_identity,
implementation_paths=self.paths,
).validate(request)
def bind(self, *args, **kwargs):
raise UnsupportedCore(
"single-run history requires the resolved original public task binding"
)
@contextmanager
def bind_for_task(self, request, model, environment, deadline, *, public_task):
self.validate(request)
if (
self._used
or request.context_adapter is not self
or not isinstance(public_task, str)
or not public_task.strip()
):
raise UnsupportedCore("fresh single-run context and resolved public task required")
self._used = True
state = Path(request.state_dir).resolve()
phase = state / "plain"
phase.mkdir(exist_ok=False)
descriptor = PhaseHistoryBinding(
state,
phase,
"plain",
Path(request.workspace).resolve(),
hashlib.sha256(request.spec.task.encode()).hexdigest(),
hashlib.sha256(public_task.encode()).hexdigest(),
self.paths,
deadline,
self.counter,
self.tokenizer_identity,
)
self.history = NativePhaseHistoryContext(descriptor)
report = dict(
identity=self.identity,
phase=descriptor.manifest(),
original_trajectory_ref=str(state / "native-trajectory.json"),
original_public_task_ref=str(state / "native-public-task.json"),
history_report_ref=str(phase / "native-phase-history.json"),
extra_model_calls=0,
max_original_SDK_runs=1,
search_enabled=False,
source_enabled=False,
planning_enabled=False,
capability_improvement_proven=False,
)
try:
with self.history.bind_phase(request, model, environment):
self._active = True
try:
yield self
finally:
self._active = False
finally:
(state / "native-single-history.json").write_bytes(
(json.dumps(report, ensure_ascii=False, indent=2) + "\n").encode()
)
@contextmanager
def bind_public_operation(self, operation):
if not self._active:
raise UnsupportedCore("single-run public observation requires an active original phase")
with self.history.bind_public_operation(operation):
yield self
def _observer(self):
if not self._active:
raise UnsupportedCore(
"trusted evaluator callback requires the active single-run observer"
)
return self.history
def begin_check(self, **kwargs):
return self._observer().begin_check(**kwargs)
def end_check(self, token, **kwargs):
return self._observer().end_check(token, **kwargs)
def observer_failed(self, error):
return self._observer().observer_failed(error)