#!/usr/bin/env python3
import json, shlex, sys
READ_ONLY="READ_ONLY"
MUTATING={"mv","rm","cp","touch","mkdir","rmdir","install","truncate","chmod","chown","chgrp","ln","tee"}
GIT_MUTATING={"add","apply","am","branch","checkout","cherry-pick","clean","commit","merge","mv","pull","push","rebase","reset","restore","revert","rm","stash","switch","tag"}
def classify(command):
    try: p=shlex.split(command)
    except ValueError: return "UNKNOWN"
    if not p: return "UNKNOWN"
    if p[0] in MUTATING: return "MUTATION"
    if p[0]=="sed" and "-i" in p[1:]: return "MUTATION"
    if p[0]=="git" and len(p)>1: return "MUTATION" if p[1] in GIT_MUTATING else "READ"
    if p[0] in {"pwd","ls","cat","head","tail","grep","find","stat","wc","printf"}: return "READ"
    return "UNKNOWN"
def evaluate(authority,command,source_ref,principal):
    cls=classify(command); conflict=authority==READ_ONLY and cls!="READ"
    inv={"time":True,"continuity":not conflict,"alignment":not conflict,"genesis":not conflict,"boundary":not conflict,"reference":bool(source_ref),"causality":not conflict,"consciousness":bool(principal)}
    coherent=all(inv.values())
    return {"authority":authority,"command":command,"command_class":cls,"invariants":inv,"coherence":coherent,"decision":"PERMIT" if coherent else "HOLD","reason":"ALL_INVARIANTS_SIMULTANEOUSLY_TRUE" if coherent else "ALL_INVARIANTS_NOT_SIMULTANEOUSLY_TRUE"}
SOURCE="https://github.com/openai/codex/issues/44130"; PRINCIPAL="SOVEREIGN_HUMAN"
vectors=[("readonly_status",READ_ONLY,"git status","PERMIT"),("readonly_move",READ_ONLY,"mv /tmp/x /tmp/y","HOLD"),("readonly_remove",READ_ONLY,"rm -f /tmp/x","HOLD")]
out=[]; ok=True
for name,auth,cmd,expected in vectors:
    r=evaluate(auth,cmd,SOURCE,PRINCIPAL); r["name"]=name; r["expected"]=expected; r["test_passed"]=r["decision"]==expected; ok &= r["test_passed"]; out.append(r)
print(json.dumps({"schema":"SCQOS-CODEX-READONLY-VERIFY-V1","source":SOURCE,"all_tests_passed":ok,"results":out},indent=2,sort_keys=True)); sys.exit(0 if ok else 1)
