File size: 5,084 Bytes
2ceda9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""77_suite_v012.py — VEREDITO CONSOLIDADO v0.12 (r-44/r-45).

Gates:
  1. cargo test (lib + bins) — 3 execuções consecutivas;
  2. unwraps do lib.rs = 0 (regressão v0.11);
  3. gates REAIS do script 76 (resultados/treino_v012.json):
     escala por amostra, A/B v011-1, safetensors (RAM), RE-3, V-3, T-ADJ,
     elástico sound, SOM, H-SOM, fases;
  4. telemetria: painel SIMD + ASSEMBLY.
"""
import json, os, re, subprocess, sys


def main():
    pasta = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.insert(0, pasta)
    gates = {}

    # ---------------- 1. cargo (3x) ----------------
    print("=== CARGO TEST (3 execuções) ===", flush=True)
    ok_cargo = 0
    for i in range(3):
        cargo = os.path.expanduser("~/.cargo/bin/cargo")
        if not os.path.exists(cargo):
            cargo = "cargo"
        r = subprocess.run(
            [cargo, "test", "--release"], cwd=pasta,
            capture_output=True, text=True, timeout=1800)
        m = re.findall(r"test result: ok\. (\d+) passed", r.stdout)
        total = sum(int(x) for x in m)
        falhas = re.findall(r"test result: FAILED\. (\d+) failed", r.stdout)
        n_falhas = sum(int(x) for x in falhas)
        print(f"  corrida {i+1}: {total} passaram, {n_falhas} falharam", flush=True)
        if r.returncode == 0 and n_falhas == 0 and total >= 146:
            ok_cargo += 1
    gates["cargo_3x_146"] = ok_cargo == 3

    # ---------------- 2. unwraps no lib.rs ----------------
    src = open(os.path.join(pasta, "src", "lib.rs")).read()
    n_unwrap = len(re.findall(r"\.unwrap\(\)", src))
    gates["lib_rs_zero_unwrap"] = n_unwrap == 0
    print(f"=== UNWRAPS lib.rs: {n_unwrap} (esperado 0) ===", flush=True)

    # ---------------- 3. gates reais (script 76) ----------------
    print("=== GATES REAIS (resultados/treino_v012.json) ===", flush=True)
    d = json.load(open(os.path.join(pasta, "resultados", "treino_v012.json")))
    el = d["elastico_real"]
    q = el["quantizador"]
    es = q["escala_por_amostra"]
    r3 = el["elasticas"]["re3_prefixos"]
    ab = d["ab_v011_1"]["resumo"]
    ram = d["ram_s1"]
    tad = d["ab_v011_1"]["tad"]

    gates["a2_escala_melhorou"] = (
        es["melhorou"] and es["fracao_m_negativo"] < 0.01
        and es["contracao_medida_amostra"] <= q["contracao_cota_garantida"] + 0.05)
    gates["a2_contracao_abaixo_1"] = es["contracao_medida_amostra"] < 1.0
    gates["ab_v011_1_decorrel_10x"] = (
        ab["decorrel_bug"]["media"] > 10.0 * ab["decorrel_corrigido"]["media"])
    gates["ab_v011_1_perda_nao_piora"] = (
        ab["perda_pred_corrigido"]["media"] <= ab["perda_pred_bug"]["media"] * 1.05)
    gates["s1_ram_2x"] = (ram["razao_pico"] or 0) >= 2.0
    gates["s1_safetensors_menor"] = (
        ram["tamanho_mb"]["safetensors"] < ram["tamanho_mb"]["json"])
    gates["re3_emitidos"] = r3["emitidos"] >= 5
    gates["re3_sem_rollback_entregue"] = r3["rollbacks"] == 0
    gates["re3_compressao_90"] = r3["compressao_frac"] < 0.10
    gates["v3_curvas_completas"] = (
        len(el["elasticas"]["curva_gate_conf_calibracao"]) == 5
        and len(el["elasticas"]["curva_gate_conf_teste"]) == 5)
    gates["tad_lambda_dir_correta"] = tad["lambda_final"] < tad["lambda_inicial"]
    gates["elastico_sounds"] = (
        el["elasticas"]["respostas_entregues_sounds"] == 24
        and el["gates"]["gate_respostas_sounds"])
    gates["elastico_aceite_min"] = el["elasticas"]["aceites"] >= 6
    gates["fase_a_aprende"] = d["fases"]["a"]["perda_final"] < d["fases"]["a"]["perda_inicial"]
    gates["fase_b_aprende"] = d["fases"]["b"]["perda_final"] < d["fases"]["b"]["perda_inicial"]
    gates["fase_c_aprende"] = d["fases"]["c"]["ce_final"] < d["fases"]["c"]["ce_inicial"]
    gates["som_mediana_monotona"] = d["som_real"]["qe_mediana_monotono"]
    gates["som_mediana_melhora"] = (
        d["som_real"]["mediana_dura"]["qe_final"] < d["som_real"]["media_suave"]["qe_final"])
    gates["hsom_pin_teorema"] = d["hsom_real"]["pin_teorema_respeitado"]

    # telemetria: painel
    try:
        import khtst_rust as K
        painel = {"simd": K.simd_relatorio(), "asm": K.asm_relatorio()}
    except Exception as e:
        painel = {"erro": str(e)}

    n_ok = sum(1 for v in gates.values() if v)
    print(json.dumps(gates, indent=1, ensure_ascii=False), flush=True)
    print(f"painel: simd={painel.get('simd', {}).get('despacho', painel.get('simd'))} "
          f"asm={painel.get('asm')}", flush=True)
    print(f"VEREDITO v0.12: {n_ok}/{len(gates)} gates", flush=True)

    saida = {
        "versao": "0.12.0",
        "gates": gates,
        "n_ok": n_ok,
        "n_total": len(gates),
        "cargo_3x": ok_cargo,
        "unwraps_lib_rs": n_unwrap,
        "painel": painel,
    }
    with open(os.path.join(pasta, "resultados", "suite_v012_final.json"), "w") as f:
        json.dump(saida, f, indent=1, ensure_ascii=False)
    print("salvo: resultados/suite_v012_final.json", flush=True)
    if n_ok != len(gates):
        sys.exit(1)


if __name__ == "__main__":
    main()