Cai
2026-08-09 9d2e633b4345c7e2e4764fb3d7a88e5277e64943
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
from __future__ import annotations
 
import argparse
import hashlib
import json
import os
from pathlib import Path
import stat
import sys
from typing import Any
 
from .manifests import utc_now
from .models import ContractError, ErrorCode
from .performance import (build_execution_context, validate_execution_context_bytes)
 
 
def _is_reparse(path: Path) -> bool:
    try:
        attributes = os.lstat(path).st_file_attributes
    except AttributeError:
        return path.is_symlink()
    except OSError as exc:
        raise ContractError(ErrorCode.RECOVERY_UNKNOWN, "path", "object stat failed") from exc
    return bool(attributes & getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0x400))
 
 
def _require_ordinary_directory(path: Path, field: str) -> None:
    try:
        valid = path.is_dir() and not path.is_symlink() and not _is_reparse(path)
    except OSError as exc:
        raise ContractError(ErrorCode.RECOVERY_UNKNOWN, field, "directory state unknown") from exc
    if not valid:
        raise ContractError(ErrorCode.PERSIST_LATE, field, "ordinary directory required")
 
 
def _lexical_absolute(path: Path) -> Path:
    lexical = Path(os.path.abspath(os.fspath(path)))
    if not lexical.is_absolute():
        raise ContractError(ErrorCode.TASK_SPEC_INVALID, "evidence_root", "absolute path required")
    try:
        str(lexical).encode("ascii", "strict")
    except UnicodeError as exc:
        raise ContractError(ErrorCode.TASK_SPEC_INVALID, "evidence_root", "ASCII path required") from exc
    return lexical
 
 
def _validate_lexical_ancestors(root: Path) -> None:
    # Walk from the filesystem anchor to the immediate parent without calling
    # resolve: resolving first would erase the very junction/reparse evidence
    # this gate must reject.
    for ancestor in reversed(root.parents):
        if not os.path.lexists(ancestor):
            raise ContractError(ErrorCode.PERSIST_LATE, "evidence_root_parent",
                                "existing ancestor required")
        if _is_reparse(ancestor):
            raise ContractError(ErrorCode.PERSIST_LATE, "evidence_root_parent",
                                "reparse ancestor rejected")
        _require_ordinary_directory(ancestor, "evidence_root_parent")
 
 
def _validate_lexical_identity(root: Path) -> None:
    try:
        resolved = root.resolve(strict=False)
    except OSError as exc:
        raise ContractError(ErrorCode.RECOVERY_UNKNOWN, "evidence_root", "resolve failed") from exc
    if os.path.normcase(str(resolved)) != os.path.normcase(str(root)):
        raise ContractError(ErrorCode.PERSIST_LATE, "evidence_root", "resolved path drift")
 
 
def _write_exclusive(path: Path, data: bytes) -> None:
    flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY | getattr(os, "O_BINARY", 0)
    fd = os.open(path, flags, 0o600)
    try:
        remaining = memoryview(data)
        while remaining:
            written = os.write(fd, remaining)
            if written <= 0:
                raise OSError("short write")
            remaining = remaining[written:]
        os.fsync(fd)
    finally:
        os.close(fd)
 
 
def materialize_execution_context(
        evidence_root: Path, execution_started_at_utc: str, *,
        preimage: bytes | None = None) -> dict[str, Any]:
    root = _lexical_absolute(evidence_root)
    _validate_lexical_ancestors(root)
    _validate_lexical_identity(root)
 
    # All semantic and byte validation is complete before the first mkdir/open.
    if preimage is None:
        context, data = build_execution_context(root, execution_started_at_utc)
    else:
        data = bytes(preimage)
        context = validate_execution_context_bytes(data, root, execution_started_at_utc)
 
    if os.path.lexists(root):
        raise ContractError(ErrorCode.PERSIST_LATE, "evidence_root", "must be absent")
 
    # Repeat the no-follow checks immediately before the first mutation so a
    # late ancestor replacement cannot be hidden by canonicalization.
    _validate_lexical_ancestors(root)
    _validate_lexical_identity(root)
    if os.path.lexists(root):
        raise ContractError(ErrorCode.PERSIST_LATE, "evidence_root", "must remain absent")
    os.mkdir(root)
    _require_ordinary_directory(root, "evidence_root")
    control = root / "control"
    os.mkdir(control)
    _require_ordinary_directory(control, "control")
 
    context_path = control / "execution_context.json"
    _write_exclusive(context_path, data)
    if (not context_path.is_file() or context_path.is_symlink() or
            _is_reparse(context_path)):
        raise ContractError(ErrorCode.PERSIST_LATE, "execution_context", "ordinary file required")
    try:
        reopened = context_path.read_bytes()
    except OSError as exc:
        raise ContractError(ErrorCode.RECOVERY_UNKNOWN, "execution_context", "reopen failed") from exc
    if reopened != data:
        raise ContractError(ErrorCode.PERSIST_LATE, "execution_context", "readback mismatch")
    observed = validate_execution_context_bytes(reopened, root, execution_started_at_utc)
    if observed != context:
        raise ContractError(ErrorCode.PERSIST_LATE, "execution_context", "semantic readback mismatch")
    return {
        "path": str(context_path),
        "bytes": len(data),
        "sha256": hashlib.sha256(data).hexdigest(),
        "execution_started_at_utc": execution_started_at_utc,
    }
 
 
def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(prog="hibor-fast-performance-coordinator")
    parser.add_argument("--evidence-root", type=Path, required=True)
    parser.add_argument("--started-at-utc", default=None)
    args = parser.parse_args(argv)
    started_at_utc = args.started_at_utc or utc_now()
    try:
        receipt = materialize_execution_context(args.evidence_root, started_at_utc)
        print(json.dumps(receipt, ensure_ascii=True, separators=(",", ":")))
        return 0
    except (OSError, UnicodeError, ValueError, ContractError) as exc:
        code = exc.code.value if isinstance(exc, ContractError) else "UNEXPECTED_EXCEPTION"
        print(json.dumps({"status": "STOPPED", "error_code": code},
                         ensure_ascii=True, separators=(",", ":")), file=sys.stderr)
        return 12
 
 
if __name__ == "__main__":
    raise SystemExit(main())