Repository navigation
Expand file tree
/
Copy pathacceptance.py
More file actions
424 lines (403 loc) · 19.3 KB
/
Copy pathacceptance.py
File metadata and controls
424 lines (403 loc) · 19.3 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
"""Acceptance executor: frozen source, checker identity and full receipts.
Independent from model routing/context. The process callback owns resource
accounting; this executor owns acceptance integrity and snapshot cleanup.
"""
import hashlib
import json
import time
from pathlib import Path
from infinicoder.models.budget import BudgetExceeded
from infinicoder.verification.check_identity import checker_files
from infinicoder.execution.workspace_tools import Workspace
from infinicoder.planning.diagnostics import compiler_diagnostics
from infinicoder.verification.verification import (
Snapshot,
environment_id,
outcome_acceptable,
parse_test_summary,
)
def _stdout_report(text):
"""Retain a bounded JSON observation without changing its acceptance gate."""
if not isinstance(text, str) or len(text.encode("utf-8")) > 65536:
return None
try:
report = json.loads(text)
except (ValueError, TypeError):
return None
if isinstance(report, dict) and report.get("status") in ("passed", "failed"):
return report
return None
class VerificationExecutionError(Exception):
"""The trusted process callback raised; this is not a candidate verdict."""
def __init__(self, evidence):
self.evidence = evidence
super().__init__("Independent verification executor did not return a result")
class VerificationBudgetError(VerificationExecutionError):
"""A controller budget exception, kept separate from environment faults."""
class VerificationControlStop(VerificationExecutionError):
def __init__(self, control, evidence):
self.control = control
super().__init__(evidence)
class AcceptanceRunner:
def __init__(
self,
state_dir,
store,
state,
process,
execution_profile,
verifier=None,
diagnostic_namespaces=None,
):
self.state_dir, self.store, self.state = state_dir, store, state
self.process, self.execution_profile, self.verifier = process, execution_profile, verifier
from infinicoder.planning.diagnostic_namespaces import normalize
self.diagnostic_namespaces = normalize(diagnostic_namespaces)
self._snapmarks = []
# Only the synchronous process invocation may consume this lease.
# Historical snapshot names are not execution permissions.
self._active_execution = None
def matches_active_execution(self, argv, cwd, *, check_id, check_argv):
"""A trusted adapter may admit exactly the currently invoked check.
This grants no model tool or workspace permission. A prior snapshot,
matching directory prefix, or live-workspace command is insufficient.
This matcher currently supports commands without a {report} placeholder.
"""
lease = self._active_execution
if not lease or lease["check_id"] != check_id or lease["check_argv"] != list(check_argv):
return False
expected = self.state_dir / "verify" / ("snap-" + str(lease["snapshot_sequence"]))
snapshot = Path(lease["snapshot"])
try:
if (
snapshot != expected.absolute()
or not snapshot.is_dir()
or expected.resolve() != expected.absolute()
or expected.parent.resolve() != expected.parent.absolute()
or self.state_dir.resolve() != self.state_dir.absolute()
or str(cwd) != lease["cwd"]
or Path(cwd).absolute() != snapshot
):
return False
expanded = [str(snapshot) if a == "{workspace}" else a for a in check_argv]
if list(argv) != lease["argv"] or list(argv) != expanded:
return False
if lease["checker_files"] != self.state.get("checker_files", {}):
return False
for name, digest in lease["checker_files"].items():
path = Path(name)
if (
not path.is_file()
or path.is_symlink()
or path.resolve() != path.absolute()
or hashlib.sha256(path.read_bytes()).hexdigest() != digest
):
return False
return Workspace(snapshot).revision() == lease["execution_revision"]
except (OSError, ValueError):
return False
def active_execution_timeout(self, argv, cwd, timeout, *, check_id, check_argv):
"""Recheck control and the original deadline after expensive admission."""
if not self.matches_active_execution(argv, cwd, check_id=check_id, check_argv=check_argv):
return None
lease = self._active_execution
receipt = dict(lease, executed_argv=list(argv), execution_outcome="not_started")
control = self.store.take_control()
if control:
receipt["failure_origin"] = "verification_control"
raise VerificationControlStop(control, [receipt])
available = min(timeout, lease["deadline_monotonic"] - time.monotonic())
if available <= 0:
receipt.update(
failure_origin="verification_budget",
error_type="BudgetExceeded",
error="Original verification deadline expired during binding",
)
raise VerificationBudgetError([receipt])
return available
def verify(self, spec, ws, check_ids, remaining, require_artifacts=True):
"""frozen-snapshot acceptance with structured reports.
Checks run against a snapshot copy; live-workspace edits during
verification cannot change the outcome, and check side effects
cannot touch the user's files. Evidence binds snapshot revision,
checker identity and environment."""
from infinicoder.planning.diagnostic_namespaces import validate_checks
validate_checks(self.diagnostic_namespaces, spec)
self.last_process_outcomes = {}
live_before = ws.revision()
if remaining() <= 0:
return False, [{"check_id": "deadline", "error": "No verification budget remains"}]
if checker_files(spec, ws) != self.state.get("checker_files", {}):
return False, [
{
"check_id": "checker_identity",
"error": "Acceptance files changed since session start",
}
]
snap_seq = len(self._snapmarks)
snap_dir = self.state_dir / "verify" / f"snap-{snap_seq}"
output_paths = list(
dict.fromkeys(p for c in spec.checks if c.id in check_ids for p in c.output_paths)
)
snapshot = Snapshot(ws.root, snap_dir, excluded_paths=output_paths)
self._snapmarks.append(str(snap_dir))
snap_rev = snapshot.revision()
protected_revision = snapshot.revision(output_paths)
env = environment_id()
evidence = []
try:
for check in spec.checks:
if check.id not in check_ids:
continue
report_file = (
Workspace(snapshot.dir).path(check.report_path) if check.report_path else None
)
if report_file:
if report_file.exists():
report_file.unlink() # A source-supplied/stale report is never evidence.
report_file.parent.mkdir(parents=True, exist_ok=True)
executed_argv = [
str(snapshot.dir) if arg == "{workspace}" else arg for arg in check.argv
]
executed_argv = [
str(report_file) if arg == "{report}" and report_file else arg
for arg in executed_argv
]
timeout = min(check.timeout, self.execution_profile.max_timeout, remaining())
if timeout <= 0:
return False, evidence + [
{
"check_id": "deadline",
"error": "No verification budget remains",
"execution_outcome": "not_started",
}
]
lease = dict(
check_id=check.id,
snapshot=str(snapshot.dir.resolve()),
cwd=str(snapshot.dir),
argv=list(executed_argv),
check_argv=list(check.argv),
checker_files=dict(self.state.get("checker_files", {})),
revision=snap_rev,
execution_revision=snapshot.revision(),
snapshot_sequence=snap_seq,
)
receipt = dict(
lease,
failure_origin="verification_executor",
execution_outcome="unknown",
timeout_s=timeout,
environment=env,
)
control = self.store.take_control()
if control:
receipt.update(
failure_origin="verification_control", execution_outcome="not_started"
)
raise VerificationControlStop(control, evidence + [receipt])
timeout = min(timeout, remaining())
if timeout <= 0:
return False, evidence + [
{
"check_id": "deadline",
"error": "No verification budget remains",
"execution_outcome": "not_started",
}
]
receipt["timeout_s"] = timeout
lease["deadline_monotonic"] = time.monotonic() + timeout
started = time.monotonic()
self._active_execution = lease
try:
result = self.process(executed_argv, str(snapshot.dir), timeout)
except VerificationExecutionError as error:
error.evidence = evidence + error.evidence
raise
except Exception as error:
receipt.update(
error_type=type(error).__name__,
error=str(error)[:2000],
wall_s=time.monotonic() - started,
executed_argv=list(executed_argv),
)
for name, observe in (
("snapshot_after", snapshot.revision),
("observed_current_revision", ws.revision),
):
try:
receipt[name] = observe()
except Exception as observation_error:
receipt[name + "_unavailable"] = type(observation_error).__name__
error_type = (
VerificationBudgetError
if isinstance(error, BudgetExceeded)
else VerificationExecutionError
)
if isinstance(error, BudgetExceeded):
receipt["failure_origin"] = "verification_budget"
raise error_type(evidence + [receipt]) from error
finally:
self._active_execution = None
self.last_process_outcomes[check.id] = dict(
completed_normally=(
type(result.timed_out) is bool
and not result.timed_out
and type(result.exit_code) is int
and 0 <= result.exit_code < 124
),
exit_code=result.exit_code,
timed_out=result.timed_out,
truncated=result.truncated,
)
control = self.store.take_control()
if control:
receipt.update(
failure_origin="verification_control",
execution_outcome="completed",
exit_code=result.exit_code,
wall_s=time.monotonic() - started,
executed_argv=list(executed_argv),
)
raise VerificationControlStop(control, evidence + [receipt])
report, report_error = None, None
if report_file:
try:
if (
report_file.is_symlink()
or not report_file.is_file()
or report_file.stat().st_size > 65536
):
raise ValueError("Missing, linked or oversized report")
report = json.loads(report_file.read_text(encoding="utf-8"))
if not isinstance(report, dict) or report.get("status") not in (
"passed",
"failed",
):
raise ValueError("Report must be an object with passed/failed status")
except (OSError, ValueError, UnicodeError) as e:
report_error = str(e)
report_text = (
json.dumps(report, ensure_ascii=False) if report is not None else result.output
)
summary = parse_test_summary(report_text)
ok_struct, why = outcome_acceptable(summary)
diagnostic_text = report_text
try:
detail = json.loads(report_text).get("case_detail")
if isinstance(detail, str):
diagnostic_text = detail
except (ValueError, AttributeError):
pass
item = {
"check_id": check.id,
"exit_code": result.exit_code,
"stdout_tail": (result.output or "")[-800:],
"summary": summary,
"revision": snap_rev,
"checker": hashlib.sha256(json.dumps(check.argv).encode()).hexdigest()[:16],
"environment": env,
"output_paths": check.output_paths,
"executed_argv": executed_argv,
"diagnostics": compiler_diagnostics(diagnostic_text, Workspace(snapshot.dir)),
"snapshot_after": snapshot.revision(),
}
item["checker_files"] = self.state.get("checker_files", {})
prefix = (self.diagnostic_namespaces or {}).get(check.id)
if prefix is not None:
from infinicoder.planning.diagnostic_namespaces import observe
item["diagnostics"] = observe(
diagnostic_text,
Workspace(snapshot.dir),
prefix,
check_id=check.id,
argv=check.argv,
checker_files=item["checker_files"],
snapshot_revision=snap_rev,
protected_revision=protected_revision,
output_paths=output_paths,
)
if report is not None:
item["report"] = report
elif not report_file:
# Most CLI checks return the same structured report on
# stdout rather than through {report}. An 800-character
# tail can lose its failure identity/numerical witness and
# cease to be JSON. Preserve the bounded returned report
# for feedback;summary/exit-code gates above are unchanged.
observed = _stdout_report(result.output)
if observed is not None:
item["report"] = observed
item["report_origin"] = {
"kind": "process_output",
"sha256": hashlib.sha256(result.output.encode("utf-8")).hexdigest(),
}
if report_error:
item["error"] = "Invalid structured report: " + report_error
outputs = {}
for folder in check.output_paths:
target = Workspace(snapshot.dir).path(folder)
paths = target.rglob("*") if target.is_dir() else [target]
for path in paths:
if path.is_file() and not path.is_symlink():
with path.open("rb") as stream:
digest = hashlib.sha256()
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
outputs[path.relative_to(snapshot.dir).as_posix()] = digest.hexdigest()
item["output_hashes"] = outputs
evidence.append(item)
if result.exit_code != 0 or report_error:
return False, evidence
if not ok_struct:
item["rejected_because"] = why
return False, evidence
# checks must be read-only observers; a check that edits
# the code under test (fake success) is rejected.
if snapshot.revision(output_paths) != protected_revision:
evidence.append(
{
"check_id": "revision",
"error": "Checks mutated the verification snapshot; "
"acceptance must not rewrite the code under test.",
}
)
return False, evidence
if self.verifier:
# Custom verifier owns its own deadline contract; CLI uses bounded Check commands.
ok, reason = self.verifier(str(snapshot.dir))
evidence.append(
{
"check_id": "callback",
"ok": bool(ok),
"reason": str(reason),
"revision": snap_rev,
}
)
if not ok:
return False, evidence
if snapshot.revision(output_paths) != protected_revision:
evidence.append(
{"check_id": "revision", "error": "Custom verifier mutated protected source"}
)
return False, evidence
for path in spec.required_artifacts if require_artifacts else []:
if not Workspace(snapshot.dir).path(path).is_file():
evidence.append({"check_id": "artifact", "missing": path})
return False, evidence
if remaining() <= 0:
evidence.append(
{"check_id": "deadline", "error": "Deadline expired during verification"}
)
return False, evidence
if ws.revision() != live_before:
evidence.append(
{"check_id": "revision", "error": "Live workspace changed during verification."}
)
return False, evidence
if not evidence or not check_ids and self.verifier is None:
return False, evidence
self.store.event("verification", {"accepted": True, "evidence": evidence})
return True, evidence
finally:
snapshot.cleanup()