Repository navigation
Expand file tree
/
Copy pathtransition_reports.py
More file actions
178 lines (165 loc) · 7.65 KB
/
Copy pathtransition_reports.py
File metadata and controls
178 lines (165 loc) · 7.65 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
"""The existing immutable run/read store with finite revision probe budgets."""
from contextlib import contextmanager
import copy
import math
from pathlib import Path
import threading
import time
from infinicoder.runtime.agent_execution import CallerPublicOperation, UnsupportedCore
from infinicoder.verification.public_check_reports import PublicCheckReports
from infinicoder.verification.public_transition_probe import PublicTransitionContract, digest
class TransitionReports(PublicCheckReports):
def __init__(self, operation, directory, request, deadline, contract):
super().__init__(operation, directory, request, deadline)
self.transition_contract = contract
self.attempts = 0
self.spent_seconds = 0.0
self.revisions = {}
self.probe_gate = threading.Lock()
def run(self, *, absolute_deadline=None):
self._guard()
if absolute_deadline is not None and (
type(absolute_deadline) not in (int, float) or not math.isfinite(absolute_deadline)
):
raise ValueError("finite absolute probe deadline required")
entered = time.monotonic()
cutoff = min(
self.deadline,
entered + 30,
entered + max(0, 60 - self.spent_seconds),
self.deadline if absolute_deadline is None else absolute_deadline,
)
if not self.probe_gate.acquire(timeout=max(0, cutoff - time.monotonic())):
raise TimeoutError("transition probe busy")
try:
versions = self._versions(cutoff)
if any(r["kind"] != "file" for r in versions.values()):
return dict(
error="source_binding_unknown", source_currency="unknown", probe_executed=False
)
revision = digest(versions)
cached = self.revisions.get(revision)
if cached is not None:
return dict(copy.deepcopy(cached), reused_revision=True, probe_executed=False)
if self.attempts >= 2 or self.spent_seconds >= 60:
return dict(
error="transition_probe_budget_exhausted",
source_currency="unknown",
probe_executed=False,
)
cutoff = min(cutoff, time.monotonic() + 60 - self.spent_seconds)
self.attempts += 1
try:
result = super().run(absolute_deadline=cutoff)
result.update(probe_executed=True, reused_revision=False, source_revision=revision)
except Exception as error:
result = dict(
error=type(error).__name__ + ": " + str(error)[:500],
source_currency="unknown",
probe_executed=False,
reused_revision=False,
source_revision=revision,
)
finally:
self.spent_seconds += time.monotonic() - entered
self.revisions[revision] = copy.deepcopy(result)
return result
finally:
self.probe_gate.release()
class PublicTransitionCapability:
"""Caller supplies the public operation;the product supplies bounded reuse.
bind_reports() is opened by the caller's public acceptance context before
the original agent runs. ON/OFF share this same store and manual run/read
endpoint;only NativeTransitionProbe activates automatic edit feedback.
"""
def __init__(self, contract, operation):
if not isinstance(contract, PublicTransitionContract) or not isinstance(
operation, CallerPublicOperation
):
raise ValueError("explicit public transition contract and caller operation required")
data = contract.manifest()
if (
operation.source_mutation_allowed
or not operation.observed_paths
or tuple(operation.observed_paths) != tuple(data["observed_paths"])
or operation.timeout_seconds > 30
or operation.result_token_bound > 600
or operation.implementation_identity.get("transition_contract_sha256")
!= contract.identity
or data["execution_operation"].get("id") != operation.operation_id
):
raise ValueError(
"probe operation must preserve sources and match the bounded public contract"
)
self.contract, self.operation = contract, operation
self.store = None
def validate(self, request):
self.contract.manifest()
if Path(self.operation.workspace).resolve() != Path(request.workspace).resolve():
raise UnsupportedCore("public transition operation belongs to another workspace")
@contextmanager
def bind_reports(self, request, deadline, directory):
self.validate(request)
if self.store is not None:
raise ValueError("transition reports cannot nest")
self.store = TransitionReports(self.operation, directory, request, deadline, self.contract)
try:
yield self.store
finally:
self.store.active = False
from infinicoder.verification.public_transition_probe import encoded
(self.store.directory / "probe-lifecycle.json").write_bytes(
(
encoded(
dict(
attempts=self.store.attempts,
spent_seconds=self.store.spent_seconds,
revisions=self.store.revisions,
closed=True,
independent_final_invoked=False,
)
)
+ "\n"
).encode()
)
self.store = None
def run(self, cutoff):
if self.store is None:
raise UnsupportedCore("caller public transition reports are not bound")
reply = self.store.run(absolute_deadline=cutoff)
if not reply.get("report_id"):
return reply, dict(
status="unknown", reason=reply.get("error", "no actual public report")
)
record, _ = self.store._load(reply["report_id"])
if record.get("error"):
return reply, dict(
status="env_error",
reason="caller_diagnostic_operation_failed",
detail=record["error"],
)
current = self.store._versions(cutoff)
if self.store._currency(record, current) != "current_on_declared_paths":
return reply, dict(status="unknown", reason="candidate_source_changed_or_unknown")
raw = record.get("result")
process = raw.get("process", {}) if isinstance(raw, dict) else {}
if process.get("timed_out") or process.get("truncated"):
return reply, dict(status="env_error", reason="diagnostic_runner_timeout_or_truncation")
report = raw.get("transition_report") if isinstance(raw, dict) else None
if not isinstance(report, dict) or report.get("contract_sha256") != self.contract.identity:
return reply, dict(
status="unknown", reason="actual_transition_report_missing_or_wrong_contract"
)
if report.get("source_versions") != record["source_before"]:
return reply, dict(
status="unknown", reason="runner_candidate_binding_differs_from_observed_revision"
)
if report.get("status") not in (
"unknown",
"env_error",
"candidate_error",
"counterexample",
"observed_values_match",
):
return reply, dict(status="unknown", reason="unsupported_diagnostic_status")
return reply, copy.deepcopy(report)