Developer docs + quickstart
Browse files
README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# CORTX β Self-Editing Reasoning Engine (open source)
|
| 2 |
+
|
| 3 |
+
**CORTX** turns any instruct-tuned base model into a *self-editing reasoning
|
| 4 |
+
engine*: it verifies its own math with executed code, revises its answers
|
| 5 |
+
against an objective checker, rewrites its own operating rules when it finds
|
| 6 |
+
better ones β and every one of those rewrites passes through a **hard security
|
| 7 |
+
boundary** enforced in code, not prompts.
|
| 8 |
+
|
| 9 |
+
Built by **ChatPBC Labs**. Apache-2.0.
|
| 10 |
+
|
| 11 |
+
> Base model: `Qwen/Qwen2.5-7B-Instruct` + QLoRA adapter (trainable on a
|
| 12 |
+
> single T4). The scaffold is model-agnostic β swap the base to any Qwen
|
| 13 |
+
> (32B / 72B / 235B-A22B) or other instruct model by changing one config line.
|
| 14 |
+
|
| 15 |
+
## What's in this repo
|
| 16 |
+
|
| 17 |
+
| File | Purpose |
|
| 18 |
+
|---|---|
|
| 19 |
+
| `cortx_secure.py` | **The security engine**: whitelist + locked sections + size caps + rate limit + hash-chained audit + rollback. Drop-in module. |
|
| 20 |
+
| `gen_synthetic.py` | Synthetic data generator (reasoning + self-edit + security-refusal + correction rows) |
|
| 21 |
+
| `train_qwen.py` | QLoRA training kernel (single T4, 4-bit) |
|
| 22 |
+
| `test_security.py` | Security self-test (6/6 passing) |
|
| 23 |
+
| `SECURITY.md` | Threat model + defense layers + audit verification guide |
|
| 24 |
+
| `serve/` | Reference serving kernel (OpenAI-compatible API + admin console, admin token-gated) |
|
| 25 |
+
|
| 26 |
+
## Quickstart β use the security engine in your own project
|
| 27 |
+
|
| 28 |
+
```python
|
| 29 |
+
from cortx_secure import SecureEditor, AuditLog
|
| 30 |
+
|
| 31 |
+
config = {"version": 1, "self_edit_enabled": True,
|
| 32 |
+
"constitution": "...", "tool_rules": "...",
|
| 33 |
+
"metacognitive_rules": "...", "refusal_rules": "...",
|
| 34 |
+
"playbook": "..."}
|
| 35 |
+
editor = SecureEditor(config) # audit log auto-created
|
| 36 |
+
|
| 37 |
+
# model emits [cortex_edit]{...}[/cortex_edit] blocks in its reply:
|
| 38 |
+
clean_text, applied, rejected = editor.apply_edits(model_reply)
|
| 39 |
+
|
| 40 |
+
print(applied) # [{'section': 'playbook', 'chars': 120}]
|
| 41 |
+
print(rejected) # [{'section': 'identity', 'reason': 'locked section...'}]
|
| 42 |
+
|
| 43 |
+
# tamper-evident audit β verify anytime:
|
| 44 |
+
ok, n = editor.audit.verify()
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
The model **cannot** edit: identity, security_rules, weights, code, or the
|
| 48 |
+
admin token. Attempts are rejected and logged. That boundary is code.
|
| 49 |
+
|
| 50 |
+
## The self-improvement loop
|
| 51 |
+
|
| 52 |
+
```
|
| 53 |
+
user question
|
| 54 |
+
β
|
| 55 |
+
βΌ
|
| 56 |
+
gate: fast path vs deliberate (System-2)
|
| 57 |
+
β
|
| 58 |
+
βΌ
|
| 59 |
+
generate βββΊ parse [calc:]/[verify:] markers βββΊ sandboxed AST evaluator
|
| 60 |
+
β β fails? β
|
| 61 |
+
β ββββΊ REVISION request βββββββββ
|
| 62 |
+
β β
|
| 63 |
+
βΌ βΌ
|
| 64 |
+
[cortex_edit] blocks? βββΊ SecureEditor: validate β whitelist β caps β rate limit
|
| 65 |
+
β β
|
| 66 |
+
βΌ βΌ
|
| 67 |
+
answer to user config versioned + hash-chained audit log
|
| 68 |
+
(admin can verify the chain or rollback)
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Training your own
|
| 72 |
+
|
| 73 |
+
```bash
|
| 74 |
+
python gen_synthetic.py 600 # -> synthetic_train.jsonl / synthetic_val.jsonl
|
| 75 |
+
# upload to Kaggle, run train_qwen.py on a T4 (QLoRA, 4-bit)
|
| 76 |
+
# adapter pushed to chatpbc1/CORTX/adapter-qwen
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
## Serving
|
| 80 |
+
|
| 81 |
+
The reference server is an OpenAI-compatible FastAPI kernel (see `serve/`):
|
| 82 |
+
`POST /v1/chat/completions` with a `cortex` option dict
|
| 83 |
+
(`{"system2": true, "self_improve": true}`), plus `/cortex/config`,
|
| 84 |
+
`/cortex/reset`, `/admin/audit` (token-gated), `/admin/rollback` (token-gated).
|
| 85 |
+
|
| 86 |
+
## Honest limits
|
| 87 |
+
|
| 88 |
+
- 7B base: strong at tool-fired math and instruction following; still a small
|
| 89 |
+
model on open-ended reasoning. The scaffold buys *verifiability and auditability*,
|
| 90 |
+
not raw IQ.
|
| 91 |
+
- Self-edit *quality* is unvalidated by outcomes β review the audit log in
|
| 92 |
+
production. Security is enforced; edit wisdom is not.
|
| 93 |
+
|
| 94 |
+
## License
|
| 95 |
+
|
| 96 |
+
Apache-2.0 Β· ChatPBC Labs
|