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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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
"""Fully local benchmark for Aureon's governed requirement-to-skill path.

The benchmark has two operations:

``build``
    Deterministically stage one provider-neutral GUI skill in a caller-owned
    state directory.  RequirementSkillBuilder performs compile, static, and
    simulated validation.  The resulting skill remains VALIDATED, pending an
    explicit approval, and live-disabled.

``verify``
    Read the receipt and skill library back from disk without compiling or
    executing the skill.  Every material artifact is bound to the canonical
    benchmark specification with SHA-256.

The public result and receipt contain lifecycle statuses and hashes only.  No
requirement text, generated source, state path, provider data, or secret value
is emitted.
"""

from __future__ import annotations

import argparse
import hashlib
import json
import os
import re
import tempfile
import time
from pathlib import Path
from typing import Any, Mapping, Sequence

from aureon.code_architect.requirement_skill_builder import RequirementSkillBuilder
from aureon.code_architect.skill import Skill, SkillLevel, SkillStatus
from aureon.code_architect.skill_library import SkillLibrary

SCHEMA_VERSION = "aureon-requirement-skill-benchmark-v1"
BENCHMARK_SKILL_NAME = "provider_neutral_sandbox_gui_probe"
RECEIPT_FILE = "requirement_skill_benchmark_receipt.json"

_BENCHMARK_REQUIREMENT = (
    "Create a deterministic provider-neutral sandbox GUI probe using only bounded "
    "mouse, keyboard, and observation primitives."
)
_BENCHMARK_DESCRIPTION = "Exercise bounded observation, pointer, click, and keyboard GUI primitives."
_HEX_64 = re.compile(r"^[0-9a-f]{64}$")
_EXPECTED_STATUSES = {
    "benchmark_status": "validated_pending_approval",
    "skill_status": SkillStatus.VALIDATED.value,
    "approval_status": "pending_explicit_approval",
    "live_execution_status": "disabled",
    "compile_status": "passed",
    "static_status": "passed",
    "simulation_status": "passed",
}
_HASH_FIELDS = {
    "state_dir_sha256",
    "skill_name_sha256",
    "requirement_sha256",
    "plan_sha256",
    "source_sha256",
    "skill_record_sha256",
    "library_sha256",
}
_RECEIPT_KEYS = {"schema_version", *_EXPECTED_STATUSES, *_HASH_FIELDS}


def _benchmark_plan() -> dict[str, Any]:
    """Return a fresh canonical plan; no service or GUI provider is named."""

    return {
        "name": BENCHMARK_SKILL_NAME,
        "description": _BENCHMARK_DESCRIPTION,
        "steps": [
            {"primitive": "screenshot", "params": {}},
            {"primitive": "get_screen_size", "params": {}},
            {"primitive": "mouse_move", "params": {"x": 160, "y": 120}},
            {"primitive": "left_click", "params": {"x": 160, "y": 120}},
            {"primitive": "press_key", "params": {"key": "escape"}},
            {"primitive": "get_cursor_position", "params": {}},
        ],
        "sample_inputs": {},
    }


def _canonical_json(value: Any) -> str:
    return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=True, allow_nan=False)


def _sha256_bytes(value: bytes) -> str:
    return hashlib.sha256(value).hexdigest()


def _sha256_text(value: str) -> str:
    return _sha256_bytes(value.encode("utf-8"))


def _resolve_state_dir(state_dir: str | Path) -> Path:
    value = str(state_dir or "").strip()
    if not value:
        raise ValueError("state_directory_required")
    absolute = Path(os.path.abspath(Path(value).expanduser()))
    if not _path_chain_is_link_free(absolute):
        raise ValueError("state_directory_link_rejected")
    return absolute.resolve()


def _base_expected_hashes(root: Path) -> dict[str, str]:
    plan = _benchmark_plan()
    source = RequirementSkillBuilder._render_source(plan)
    return {
        "state_dir_sha256": _sha256_text(os.path.normcase(str(root))),
        "skill_name_sha256": _sha256_text(BENCHMARK_SKILL_NAME),
        "requirement_sha256": _sha256_text(_BENCHMARK_REQUIREMENT),
        "plan_sha256": _sha256_text(_canonical_json(plan)),
        "source_sha256": _sha256_text(source),
    }


def _expected_skill(base_hashes: Mapping[str, str]) -> Skill:
    return Skill(
        skill_id=base_hashes["source_sha256"][:8],
        name=BENCHMARK_SKILL_NAME,
        description=_BENCHMARK_DESCRIPTION,
        level=SkillLevel.TASK,
        category="requirement_generated",
        code=RequirementSkillBuilder._render_source(_benchmark_plan()),
        entry_function=BENCHMARK_SKILL_NAME,
        params_schema={
            "type": "object",
            "properties": {"session_id": {"type": "string"}},
            "additionalProperties": False,
        },
        dependencies=[],
        created_at=0.0,
        created_by="requirement_skill_builder",
        observation_sources=[f"requirement_sha256:{base_hashes['requirement_sha256']}"],
        status=SkillStatus.VALIDATED,
        target="vm",
        tags=[
            "requirement_generated",
            "requires_explicit_approval",
            "live_execution_disabled",
            f"plan_sha256:{base_hashes['plan_sha256']}",
        ],
    )


def _expected_hashes(root: Path) -> dict[str, str]:
    expected = _base_expected_hashes(root)
    expected["skill_record_sha256"] = _sha256_text(
        _canonical_json(_expected_skill(expected).to_dict())
    )
    return expected


def _path_is_within(root: Path, candidate: Path) -> bool:
    try:
        candidate.resolve().relative_to(root)
        return True
    except (OSError, ValueError):
        return False


def _path_is_link(path: Path) -> bool:
    try:
        is_junction = getattr(path, "is_junction", None)
        return path.is_symlink() or bool(is_junction and is_junction())
    except OSError:
        return True


def _path_chain_is_link_free(path: Path) -> bool:
    for candidate in (path, *path.parents):
        try:
            if candidate.exists() and _path_is_link(candidate):
                return False
        except OSError:
            return False
    return True


def _artifact_paths_are_isolated(root: Path) -> bool:
    skills_dir = root / "skills"
    library_path = skills_dir / SkillLibrary.LIBRARY_FILE
    library_temp_path = library_path.with_suffix(".json.tmp")
    receipt_path = root / RECEIPT_FILE
    candidates = (root, skills_dir, library_path, library_temp_path, receipt_path)
    try:
        return all(
            _path_chain_is_link_free(path) and _path_is_within(root, path)
            for path in candidates
        )
    except OSError:
        return False


def _failure(root: Path, status: str) -> dict[str, Any]:
    return {
        "ok": False,
        "schema_version": SCHEMA_VERSION,
        "operation_status": str(status),
        "benchmark_status": "not_verified",
        "skill_status": "not_verified",
        "approval_status": "not_approved",
        "live_execution_status": "disabled",
        **_expected_hashes(root),
    }


def _atomic_write_receipt(path: Path, receipt: Mapping[str, Any]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    payload = (json.dumps(dict(receipt), sort_keys=True, indent=2) + "\n").encode("utf-8")
    fd, temp_name = tempfile.mkstemp(prefix=f".{path.name}.", suffix=".tmp", dir=str(path.parent))
    try:
        with os.fdopen(fd, "wb") as handle:
            handle.write(payload)
            handle.flush()
            os.fsync(handle.fileno())
        for attempt in range(3):
            try:
                os.replace(temp_name, path)
                break
            except PermissionError:
                if attempt == 2:
                    raise
                time.sleep(0.05 * (attempt + 1))
    finally:
        Path(temp_name).unlink(missing_ok=True)


def _public_success(
    receipt: Mapping[str, Any],
    *,
    operation_status: str,
    receipt_sha256: str,
) -> dict[str, Any]:
    return {
        "ok": True,
        "operation_status": operation_status,
        **dict(receipt),
        "receipt_sha256": receipt_sha256,
    }


def _valid_receipt_shape(receipt: Any) -> bool:
    if not isinstance(receipt, dict) or set(receipt) != _RECEIPT_KEYS:
        return False
    if receipt.get("schema_version") != SCHEMA_VERSION:
        return False
    if any(receipt.get(key) != value for key, value in _EXPECTED_STATUSES.items()):
        return False
    return all(isinstance(receipt.get(key), str) and _HEX_64.fullmatch(receipt[key]) for key in _HASH_FIELDS)


def verify_benchmark(state_dir: str | Path) -> dict[str, Any]:
    """Verify the receipt and library by read-back; never execute the skill."""

    try:
        root = _resolve_state_dir(state_dir)
    except Exception:
        root = Path.cwd().resolve()
        return _failure(root, "state_directory_invalid")

    receipt_path = root / RECEIPT_FILE
    library_path = root / "skills" / SkillLibrary.LIBRARY_FILE
    if not _artifact_paths_are_isolated(root):
        return _failure(root, "artifact_path_escape_rejected")
    if not receipt_path.is_file() or not library_path.is_file():
        return _failure(root, "benchmark_artifacts_missing")

    try:
        receipt_bytes = receipt_path.read_bytes()
        receipt = json.loads(receipt_bytes.decode("utf-8"))
    except Exception:
        return _failure(root, "receipt_read_failed")
    if not _artifact_paths_are_isolated(root):
        return _failure(root, "artifact_path_escape_rejected")
    if not _valid_receipt_shape(receipt):
        return _failure(root, "receipt_policy_mismatch")

    expected = _expected_hashes(root)
    if any(receipt.get(key) != value for key, value in expected.items()):
        return _failure(root, "specification_hash_mismatch")

    try:
        library_bytes = library_path.read_bytes()
    except Exception:
        return _failure(root, "library_read_failed")
    if not _artifact_paths_are_isolated(root):
        return _failure(root, "artifact_path_escape_rejected")
    if receipt.get("library_sha256") != _sha256_bytes(library_bytes):
        return _failure(root, "library_hash_mismatch")

    try:
        library_payload = json.loads(library_bytes.decode("utf-8"))
        if (
            not isinstance(library_payload, dict)
            or set(library_payload) != {"version", "saved_at", "count", "skills"}
            or library_payload.get("version") != 1
            or library_payload.get("count") != 1
        ):
            return _failure(root, "library_policy_mismatch")
        records = library_payload.get("skills")
        if not isinstance(records, list) or len(records) != 1 or not isinstance(records[0], dict):
            return _failure(root, "skill_record_mismatch")
        record = records[0]
        expected_record = _expected_skill(expected).to_dict()
        if (
            _sha256_text(_canonical_json(record)) != expected["skill_record_sha256"]
            or _canonical_json(record) != _canonical_json(expected_record)
        ):
            return _failure(root, "skill_record_hash_mismatch")
        skill = Skill.from_dict(record)
    except Exception:
        return _failure(root, "skill_readback_failed")

    policy_ok = (
        skill.status is SkillStatus.VALIDATED
        and skill.created_by == "requirement_skill_builder"
        and skill.category == "requirement_generated"
        and skill.target == "vm"
        and skill.tags == _expected_skill(expected).tags
        and not any(str(tag).startswith("approved_by:") for tag in skill.tags or [])
        and f"requirement_sha256:{expected['requirement_sha256']}" in skill.observation_sources
        and _sha256_text(skill.code) == expected["source_sha256"]
    )
    if not policy_ok:
        return _failure(root, "skill_policy_mismatch")

    return _public_success(
        receipt,
        operation_status="verified_readback",
        receipt_sha256=_sha256_bytes(receipt_bytes),
    )


def build_benchmark(state_dir: str | Path) -> dict[str, Any]:
    """Build and validate the canonical skill without approving or running it live."""

    try:
        root = _resolve_state_dir(state_dir)
    except Exception:
        root = Path.cwd().resolve()
        return _failure(root, "state_directory_invalid")

    receipt_path = root / RECEIPT_FILE
    library_path = root / "skills" / SkillLibrary.LIBRARY_FILE
    if root.exists():
        return _failure(root, "state_directory_already_exists")
    if not root.parent.is_dir() or not _path_chain_is_link_free(root.parent):
        return _failure(root, "state_directory_parent_invalid")

    try:
        root.mkdir(exist_ok=False)
        if not _artifact_paths_are_isolated(root):
            return _failure(root, "artifact_path_escape_rejected")
        (root / "skills").mkdir(exist_ok=False)
        if not _artifact_paths_are_isolated(root):
            return _failure(root, "artifact_path_escape_rejected")
        library = SkillLibrary(storage_dir=root / "skills")
        builder = RequirementSkillBuilder(library=library)
        result = builder.build(_BENCHMARK_REQUIREMENT, plan=_benchmark_plan())
    except Exception:
        return _failure(root, "local_build_failed")
    if result.get("ok") is not True or result.get("status") != "validated_pending_approval":
        return _failure(root, "governed_build_rejected")
    if not _artifact_paths_are_isolated(root):
        return _failure(root, "artifact_path_escape_rejected")

    expected = _expected_hashes(root)
    validation = result.get("validation") if isinstance(result.get("validation"), dict) else {}
    skill = library.get(BENCHMARK_SKILL_NAME)
    if (
        skill is None
        or skill.status is not SkillStatus.VALIDATED
        or "requires_explicit_approval" not in skill.tags
        or "live_execution_disabled" not in skill.tags
        or result.get("plan_digest") != expected["plan_sha256"]
        or result.get("source_digest") != expected["source_sha256"]
        or validation.get("compile_ok") is not True
        or validation.get("static_safe") is not True
        or validation.get("simulation_ok") is not True
    ):
        return _failure(root, "post_build_policy_mismatch")

    canonical_skill = _expected_skill(expected)
    skill.skill_id = canonical_skill.skill_id
    skill.created_at = canonical_skill.created_at
    committed, _rolled_back, _error = builder._persist_skill_transaction(skill)
    if not committed:
        cleanup = builder.reject_skill(
            BENCHMARK_SKILL_NAME,
            reviewer="aureon:requirement_skill_benchmark",
            reason="canonical_skill_persist_failed",
        )
        status = "canonical_skill_persist_failed_rolled_back" if cleanup.get("ok") else "canonical_skill_persist_failed"
        return _failure(root, status)
    if not _artifact_paths_are_isolated(root):
        return _failure(root, "artifact_path_escape_rejected")

    try:
        library_bytes = library_path.read_bytes()
        library_payload = json.loads(library_bytes.decode("utf-8"))
        records = library_payload.get("skills", []) if isinstance(library_payload, dict) else []
        if (
            not isinstance(records, list)
            or len(records) != 1
            or _canonical_json(records[0]) != _canonical_json(canonical_skill.to_dict())
        ):
            return _failure(root, "canonical_skill_persist_failed")
        library_sha256 = _sha256_bytes(library_bytes)
    except Exception:
        return _failure(root, "library_read_after_build_failed")
    receipt = {
        "schema_version": SCHEMA_VERSION,
        **_EXPECTED_STATUSES,
        **expected,
        "library_sha256": library_sha256,
    }
    try:
        _atomic_write_receipt(receipt_path, receipt)
    except Exception as exc:
        cleanup = builder.reject_skill(
            BENCHMARK_SKILL_NAME,
            reviewer="aureon:requirement_skill_benchmark",
            reason="receipt_write_failed",
        )
        exception_status = re.sub(r"[^a-z0-9]+", "_", type(exc).__name__.casefold()).strip("_")
        status = f"receipt_write_failed_{exception_status}"
        if cleanup.get("ok"):
            status += "_rolled_back"
        return _failure(root, status)
    if not _artifact_paths_are_isolated(root):
        return _failure(root, "artifact_path_escape_rejected")

    verified = verify_benchmark(root)
    if verified.get("ok") is not True:
        return verified
    verified["operation_status"] = "built_and_verified"
    return verified


def _build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="Run Aureon's fully local requirement-skill benchmark.")
    subparsers = parser.add_subparsers(dest="operation", required=True)
    for operation in ("build", "verify"):
        command = subparsers.add_parser(operation)
        command.add_argument("--state-dir", required=True)
    return parser


def main(argv: Sequence[str] | None = None) -> int:
    args = _build_parser().parse_args(argv)
    result = (
        build_benchmark(args.state_dir)
        if args.operation == "build"
        else verify_benchmark(args.state_dir)
    )
    print(json.dumps(result, sort_keys=True, separators=(",", ":")))
    return 0 if result.get("ok") is True else 1


if __name__ == "__main__":  # pragma: no cover
    raise SystemExit(main())


__all__ = [
    "BENCHMARK_SKILL_NAME",
    "RECEIPT_FILE",
    "SCHEMA_VERSION",
    "build_benchmark",
    "main",
    "verify_benchmark",
]