File size: 3,138 Bytes
bc9a504
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from huggingface_hub import HfApi

SRC = r"C:\Users\micha\AppData\Local\Cline\cortx_src\qwen"
api = HfApi(token=os.environ["HF_TOKEN"])
REPO = "chatpbc1/CORTX"

files = [
    ("cortx_secure.py", "The security engine: whitelist, locked sections, caps, "
                        "rate limit, hash-chained audit, rollback"),
    ("gen_synthetic.py", "Synthetic data generator (reasoning, self-edit, "
                         "security-refusal, correction rows)"),
    ("train_qwen.py", "QLoRA training kernel for Qwen2.5-7B-Instruct on a T4"),
    ("test_security.py", "Security self-test (6/6 passing)"),
    ("README.md", "Developer docs + quickstart"),
    ("synthetic_train.jsonl", "Synthetic training data (messages format)"),
    ("synthetic_val.jsonl", "Synthetic validation data"),
]
for name, msg in files:
    api.upload_file(path_or_fileobj=os.path.join(SRC, name),
                    path_in_repo=name, repo_id=REPO,
                    commit_message=msg)
    print("uploaded", name)
print("ALL PUSHED to", REPO)

from cortx_secure import SecureEditor, AuditLog, LOCKED_SECTIONS, EDITABLE_SECTIONS

cfg = {"version": 1, "self_edit_enabled": True,
       "constitution": "seed", "tool_rules": "seed" * 10,
       "metacognitive_rules": "seed", "refusal_rules": "seed", "playbook": "seed"}
ed = SecureEditor(cfg)

tests = [
    # (name, model_text, expect_applied, expect_rejected_section)
    ("legit edit", '[cortex_edit]{"section":"playbook","content":"'
     + "x" * 100 + '"}[/cortex_edit]', 1, None),
    ("identity attack", '[cortex_edit]{"section":"identity","content":"'
     + "y" * 100 + '"}[/cortex_edit]', 0, "identity"),
    ("weights attack", '[cortex_edit]{"section":"weights","content":"'
     + "z" * 100 + '"}[/cortex_edit]', 0, "weights"),
    ("security_rules attack", '[cortex_edit]{"section":"security_rules","content":"'
     + "w" * 100 + '"}[/cortex_edit]', 0, "security_rules"),
    ("unknown section", '[cortex_edit]{"section":"etc_shadow","content":"'
     + "v" * 100 + '"}[/cortex_edit]', 0, "etc_shadow"),
    ("oversize content", '[cortex_edit]{"section":"playbook","content":"'
     + "q" * 9000 + '"}[/cortex_edit]', 0, "playbook"),
]
passed = 0
for name, text, exp_applied, exp_rej in tests:
    clean, applied, rejected = ed.apply_edits(text)
    ok = len(applied) == exp_applied
    if exp_rej:
        ok = ok and any(r["section"] == exp_rej for r in rejected)
    print(("PASS" if ok else "FAIL"), name, "| applied:", len(applied),
          "| rejected:", [r["section"] for r in rejected])
    passed += int(ok)

# rate limit test
for i in range(45):
    ed.apply_edits('[cortex_edit]{"section":"playbook","content":"'
                   + str(i) + "a" * 100 + '"}[/cortex_edit]')
ok_rate = any("rate limit" in str(r) for r in [])
print("rate limit entries in audit:", sum(1 for e in ed.audit.entries
                                          if "rate limit" in e["detail"]))

valid, n = ed.audit.verify()
print("audit chain valid:", valid, "| entries:", n)
print(f"RESULT: {passed}/6 security tests passed")