File size: 12,873 Bytes
37797b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
691880f
37797b9
 
 
691880f
 
 
 
 
 
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
from __future__ import annotations

import base64
import hashlib
import json
import os
import shutil
import subprocess
import tempfile
import time
import zipfile
from pathlib import Path
from typing import Iterator

import gradio as gr

DEFAULT_REPOSITORY = "toxzak-svg/starfire"
DEFAULT_COMMIT = "902c88e533a75076542726d5975880ef0234d54c"
DEFAULT_FEATURE = "omega-g2-shadow"
SCIENTIFIC_PATHS = (
    "lib/omega_g2_shadow.rs",
    "lib/prediction/mod.rs",
    "lib/examples/omega_g2_s0_real_trace_shadow.rs",
)
MAX_LOG_CHARS = 180_000
ARTIFACT_ROOT = Path("/tmp/starfire-verifier-artifacts")
ARTIFACT_ROOT.mkdir(parents=True, exist_ok=True)


def _append_log(log: str, line: str) -> str:
    combined = f"{log}{line}\n"
    if len(combined) > MAX_LOG_CHARS:
        combined = "[earlier output truncated]\n" + combined[-MAX_LOG_CHARS:]
    return combined


def _run_streaming(
    command: list[str],
    cwd: Path,
    env: dict[str, str],
    display: str,
) -> Iterator[tuple[str, int | None]]:
    yield f"\n$ {display}\n", None
    process = subprocess.Popen(
        command,
        cwd=cwd,
        env=env,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True,
        encoding="utf-8",
        errors="replace",
        bufsize=1,
    )
    assert process.stdout is not None
    for line in process.stdout:
        yield line, None
    code = process.wait()
    yield f"[exit {code}]\n", code


def _sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def _source_hashes(repo: Path) -> dict[str, str]:
    hashes: dict[str, str] = {}
    for relative in SCIENTIFIC_PATHS:
        target = repo / relative
        if not target.is_file():
            raise FileNotFoundError(f"required scientific source missing: {relative}")
        hashes[relative] = _sha256(target)
    return hashes


def _ensure_rust(env: dict[str, str], work: Path) -> Iterator[tuple[str, int | None]]:
    cargo = shutil.which("cargo", path=env.get("PATH"))
    if cargo:
        yield f"Using existing Cargo: {cargo}\n", 0
        return

    installer = work / "rustup-init.sh"
    download = [
        "curl",
        "--proto",
        "=https",
        "--tlsv1.2",
        "-sSf",
        "https://sh.rustup.rs",
        "-o",
        str(installer),
    ]
    yield from _run_streaming(download, work, env, "download rustup installer")
    if not installer.is_file():
        yield "rustup installer was not downloaded\n", 1
        return
    install = ["sh", str(installer), "-y", "--profile", "minimal", "--default-toolchain", "stable"]
    yield from _run_streaming(install, work, env, "install stable Rust")


def _clone_private_repo(
    repository: str,
    commit: str,
    token: str,
    work: Path,
    env: dict[str, str],
) -> Iterator[tuple[str, int | None]]:
    askpass = work / "git-askpass.sh"
    askpass.write_text(
        "#!/bin/sh\n"
        "case \"$1\" in\n"
        "  *Username*) printf '%s\\n' 'x-access-token' ;;\n"
        "  *) printf '%s\\n' \"$GITHUB_TOKEN\" ;;\n"
        "esac\n",
        encoding="utf-8",
    )
    askpass.chmod(0o700)
    env["GIT_ASKPASS"] = str(askpass)
    env["GIT_TERMINAL_PROMPT"] = "0"
    env["GITHUB_TOKEN"] = token

    repo_dir = work / "starfire"
    clone = ["git", "clone", "--no-checkout", "--filter=blob:none", f"https://github.com/{repository}.git", str(repo_dir)]
    yield from _run_streaming(clone, work, env, f"git clone --no-checkout https://github.com/{repository}.git starfire")
    if not repo_dir.is_dir():
        return
    fetch = ["git", "fetch", "--depth", "1", "origin", commit]
    yield from _run_streaming(fetch, repo_dir, env, f"git fetch --depth 1 origin {commit}")
    checkout = ["git", "checkout", "--detach", commit]
    yield from _run_streaming(checkout, repo_dir, env, f"git checkout --detach {commit}")


def _write_evidence(
    artifact_dir: Path,
    report: dict[str, object],
    log: str,
    before: dict[str, str],
    after: dict[str, str],
) -> Path:
    artifact_dir.mkdir(parents=True, exist_ok=True)
    (artifact_dir / "report.json").write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")
    (artifact_dir / "validation.log").write_text(log, encoding="utf-8")
    (artifact_dir / "source-before.sha256.json").write_text(json.dumps(before, indent=2, sort_keys=True) + "\n", encoding="utf-8")
    (artifact_dir / "source-after.sha256.json").write_text(json.dumps(after, indent=2, sort_keys=True) + "\n", encoding="utf-8")
    archive = artifact_dir.with_suffix(".zip")
    with zipfile.ZipFile(archive, "w", compression=zipfile.ZIP_DEFLATED) as bundle:
        for path in sorted(artifact_dir.iterdir()):
            bundle.write(path, arcname=path.name)
    return archive


def validate(repository: str, commit: str) -> Iterator[tuple[str, str, str | None]]:
    repository = repository.strip()
    commit = commit.strip()
    token = os.environ.get("GITHUB_TOKEN", "").strip()
    if not token:
        message = (
            "BLOCKED: Space secret GITHUB_TOKEN is missing. Add a fine-grained, read-only token "
            "for toxzak-svg/starfire under Settings → Variables and secrets, then run again."
        )
        yield message, "BLOCKED", None
        return
    if repository != DEFAULT_REPOSITORY:
        yield "BLOCKED: repository must remain toxzak-svg/starfire for this frozen verifier.", "BLOCKED", None
        return
    if len(commit) != 40 or any(ch not in "0123456789abcdefABCDEF" for ch in commit):
        yield "BLOCKED: commit must be a full 40-character hexadecimal Git SHA.", "BLOCKED", None
        return

    started = int(time.time())
    run_id = f"omega-g2-s0-{started}"
    artifact_dir = ARTIFACT_ROOT / run_id
    log = ""
    status = "RUNNING"
    archive: Path | None = None
    before: dict[str, str] = {}
    after: dict[str, str] = {}
    command_results: list[dict[str, object]] = []
    terminal_probe_pass = False

    with tempfile.TemporaryDirectory(prefix="starfire-hf-") as temp:
        work = Path(temp)
        env = os.environ.copy()
        env["CARGO_HOME"] = str(work / ".cargo")
        env["RUSTUP_HOME"] = str(work / ".rustup")
        env["CARGO_TERM_COLOR"] = "never"
        env["RUST_BACKTRACE"] = "1"
        env["PATH"] = f"{env['CARGO_HOME']}/bin:{env.get('PATH', '')}"

        try:
            for chunk, code in _ensure_rust(env, work):
                log = _append_log(log, chunk.rstrip("\n"))
                yield log, status, None
                if code not in (None, 0):
                    raise RuntimeError("stable Rust installation failed")

            for chunk, code in _clone_private_repo(repository, commit, token, work, env):
                log = _append_log(log, chunk.rstrip("\n"))
                yield log, status, None
                if code not in (None, 0):
                    raise RuntimeError("private repository checkout failed")

            repo = work / "starfire"
            resolved = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=repo, env=env, text=True).strip()
            if resolved.lower() != commit.lower():
                raise RuntimeError(f"checkout mismatch: expected {commit}, got {resolved}")
            before = _source_hashes(repo)
            log = _append_log(log, f"Pinned source confirmed: {resolved}")
            log = _append_log(log, "Recorded pre-execution scientific source hashes")
            yield log, status, None

            commands = [
                (
                    ["cargo", "check", "-p", "star", "--all-targets", "--locked"],
                    "cargo check -p star --all-targets --locked",
                ),
                (
                    ["cargo", "check", "-p", "star", "--all-targets", "--features", DEFAULT_FEATURE, "--locked"],
                    f"cargo check -p star --all-targets --features {DEFAULT_FEATURE} --locked",
                ),
                (
                    ["cargo", "test", "-p", "star", "omega_g2_shadow", "--features", DEFAULT_FEATURE, "--locked"],
                    f"cargo test -p star omega_g2_shadow --features {DEFAULT_FEATURE} --locked",
                ),
                (
                    ["cargo", "run", "-p", "star", "--example", "omega_g2_s0_real_trace_shadow", "--features", DEFAULT_FEATURE, "--locked"],
                    f"cargo run -p star --example omega_g2_s0_real_trace_shadow --features {DEFAULT_FEATURE} --locked",
                ),
            ]

            for index, (command, display) in enumerate(commands):
                output_for_command = ""
                final_code: int | None = None
                for chunk, code in _run_streaming(command, repo, env, display):
                    output_for_command += chunk
                    log = _append_log(log, chunk.rstrip("\n"))
                    yield log, status, None
                    if code is not None:
                        final_code = code
                command_results.append({"command": display, "exit_code": final_code})
                if final_code != 0:
                    raise RuntimeError(f"validation command failed: {display}")
                if index == 3:
                    terminal_probe_pass = '"terminal_classification": "PASS"' in output_for_command
                    if not terminal_probe_pass:
                        raise RuntimeError("probe completed but did not emit terminal PASS")

            after = _source_hashes(repo)
            if before != after:
                raise RuntimeError("scientific source hashes changed during execution")
            diff = subprocess.run(
                ["git", "diff", "--exit-code", "--", *SCIENTIFIC_PATHS],
                cwd=repo,
                env=env,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                text=True,
            )
            log = _append_log(log, diff.stdout.rstrip("\n")) if diff.stdout else log
            if diff.returncode != 0:
                raise RuntimeError("git source-immutability check failed")

            status = "PASS"
            log = _append_log(log, "terminal_classification: PASS")
        except Exception as exc:  # surfaced into evidence, never hidden
            status = "FAIL"
            log = _append_log(log, f"terminal_classification: FAIL")
            log = _append_log(log, f"error: {type(exc).__name__}: {exc}")
        finally:
            env.pop("GITHUB_TOKEN", None)
            report: dict[str, object] = {
                "terminal_classification": status,
                "repository": repository,
                "requested_commit": commit,
                "started_at_unix": started,
                "finished_at_unix": int(time.time()),
                "feature": DEFAULT_FEATURE,
                "commands": command_results,
                "probe_emitted_pass": terminal_probe_pass,
                "source_immutable": bool(before) and before == after,
                "scientific_paths": list(SCIENTIFIC_PATHS),
                "authority": {
                    "response_influence": False,
                    "routing": False,
                    "persistence_mutation": False,
                    "belief_or_ontology_promotion": False,
                    "tool_selection": False,
                    "external_action": False,
                    "source_modification": False,
                },
            }
            archive = _write_evidence(artifact_dir, report, log, before, after)

    yield log, status, str(archive) if archive else None


with gr.Blocks(title="Starfire ΩG2-S0 Verifier") as demo:
    gr.Markdown(
        """
# Starfire ΩG2-S0 Exact-Source Verifier

Runs the frozen, default-off shadow integration checks against one exact private Git commit.  
The verifier **observes and reports only**. It has no live response, routing, persistence, ontology, tool, or action authority.
"""
    )
    with gr.Row():
        repository = gr.Textbox(label="Repository", value=DEFAULT_REPOSITORY, interactive=False)
        commit = gr.Textbox(label="Exact commit SHA", value=DEFAULT_COMMIT)
    run = gr.Button("Run exact-source validation", variant="primary")
    classification = gr.Textbox(label="Terminal classification", value="NOT RUN", interactive=False)
    logs = gr.Textbox(label="Streaming validation log", lines=28, max_lines=40, interactive=False)
    evidence = gr.File(label="Evidence bundle")
    run.click(validate, inputs=[repository, commit], outputs=[logs, classification, evidence], concurrency_limit=1)

demo.queue(default_concurrency_limit=1, max_size=2).launch(
    server_name="0.0.0.0",
    server_port=7860,
    show_error=True,
    allowed_paths=[str(ARTIFACT_ROOT)],
)