|
| 1 | +# examples/demo_envelope_gate.py |
| 2 | + |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "primitives" / "envelope-gate")) |
| 7 | + |
| 8 | +from envelope_parser import Envelope, parse_envelope # noqa: E402 |
| 9 | +from gate import evaluate # noqa: E402 |
| 10 | + |
| 11 | + |
| 12 | +def demo_valid_request(): |
| 13 | + """A well-formed request envelope should get ALLOW.""" |
| 14 | + raw = """ALVIANTECH_ENVELOPE v0.1 |
| 15 | +PORTS: |
| 16 | + msg_id: "msg-0010" |
| 17 | + ts_utc: "2026-02-18T12:00:00Z" |
| 18 | + from: HUMAN |
| 19 | + to: TRINITY |
| 20 | + mode: TEST |
| 21 | + scope: NON_EXEC |
| 22 | +BODY: |
| 23 | + goal: Demo envelope for gate validation. |
| 24 | + inputs: |
| 25 | + - Demo input. |
| 26 | + constraints: |
| 27 | + must: |
| 28 | + - Must pass gate. |
| 29 | + must_not: |
| 30 | + - No violations. |
| 31 | + output_spec: |
| 32 | + type: NOTE |
| 33 | + format: MARKDOWN |
| 34 | + payload: |
| 35 | + Demo payload. |
| 36 | +RETURN: |
| 37 | + in_reply_to: "" |
| 38 | + exit: |
| 39 | + reason: |
| 40 | + - |
| 41 | + payload: |
| 42 | +""" |
| 43 | + env = parse_envelope(raw) |
| 44 | + result = evaluate(env) |
| 45 | + print(f"[VALID REQUEST] msg_id={result.msg_id} exit={result.exit} " |
| 46 | + f"violations={len(result.violations)} " |
| 47 | + f"rules={result.rules_checked}/{result.rules_total}") |
| 48 | + |
| 49 | + |
| 50 | +def demo_legacy_exit(): |
| 51 | + """Using PASS as exit should trigger HOLD (legacy enum violation).""" |
| 52 | + raw = """ALVIANTECH_ENVELOPE v0.1 |
| 53 | +PORTS: |
| 54 | + msg_id: "msg-0010-R" |
| 55 | + ts_utc: "2026-02-18T12:05:00Z" |
| 56 | + from: TRINITY |
| 57 | + to: HUMAN |
| 58 | + mode: TEST |
| 59 | + scope: NON_EXEC |
| 60 | +BODY: |
| 61 | + goal: Response with legacy exit. |
| 62 | + inputs: |
| 63 | + - Responding. |
| 64 | + constraints: |
| 65 | + must: |
| 66 | + - Must use correct enum. |
| 67 | + must_not: |
| 68 | + - No legacy values. |
| 69 | + output_spec: |
| 70 | + type: NOTE |
| 71 | + format: MARKDOWN |
| 72 | + payload: |
| 73 | + Response payload. |
| 74 | +RETURN: |
| 75 | + in_reply_to: "msg-0010" |
| 76 | + exit: PASS |
| 77 | + reason: |
| 78 | + - Test passed. |
| 79 | + payload: |
| 80 | + Done. |
| 81 | +""" |
| 82 | + env = parse_envelope(raw) |
| 83 | + result = evaluate(env) |
| 84 | + print(f"[LEGACY EXIT] msg_id={result.msg_id} exit={result.exit} " |
| 85 | + f"violation={result.violations[0].code if result.violations else 'none'}") |
| 86 | + |
| 87 | + |
| 88 | +def demo_missing_header(): |
| 89 | + """An envelope without the protocol header should get DENY.""" |
| 90 | + raw = """SOME_OTHER_PROTOCOL v2.0 |
| 91 | +PORTS: |
| 92 | + msg_id: "msg-0099" |
| 93 | + ts_utc: "2026-02-18T12:10:00Z" |
| 94 | + from: HUMAN |
| 95 | + to: TRINITY |
| 96 | + mode: TEST |
| 97 | + scope: NON_EXEC |
| 98 | +BODY: |
| 99 | + goal: This should fail. |
| 100 | + inputs: |
| 101 | + - Bad input. |
| 102 | + constraints: |
| 103 | + must: |
| 104 | + - Must fail. |
| 105 | + must_not: |
| 106 | + - Nothing. |
| 107 | + output_spec: |
| 108 | + type: NOTE |
| 109 | + format: MARKDOWN |
| 110 | + payload: |
| 111 | + Bad. |
| 112 | +RETURN: |
| 113 | + in_reply_to: "" |
| 114 | + exit: |
| 115 | + reason: |
| 116 | + - |
| 117 | + payload: |
| 118 | +""" |
| 119 | + env = parse_envelope(raw) |
| 120 | + result = evaluate(env) |
| 121 | + print(f"[MISSING HEADER] msg_id={result.msg_id} exit={result.exit} " |
| 122 | + f"violation={result.violations[0].code if result.violations else 'none'}") |
| 123 | + |
| 124 | + |
| 125 | +def demo_self_approve(): |
| 126 | + """An agent trying to self-approve EXEC should get HOLD.""" |
| 127 | + raw = """ALVIANTECH_ENVELOPE v0.1 |
| 128 | +PORTS: |
| 129 | + msg_id: "msg-0011-R" |
| 130 | + ts_utc: "2026-02-18T12:15:00Z" |
| 131 | + from: TRINITY |
| 132 | + to: HUMAN |
| 133 | + mode: EXEC |
| 134 | + scope: EXEC_CONFIRMED |
| 135 | +BODY: |
| 136 | + goal: Self-approve execution. |
| 137 | + inputs: |
| 138 | + - Attempting self-approval. |
| 139 | + constraints: |
| 140 | + must: |
| 141 | + - Must execute. |
| 142 | + must_not: |
| 143 | + - Nothing. |
| 144 | + output_spec: |
| 145 | + type: DECISION |
| 146 | + format: MARKDOWN |
| 147 | + payload: |
| 148 | + Executing. |
| 149 | +RETURN: |
| 150 | + in_reply_to: "msg-0011" |
| 151 | + exit: ALLOW |
| 152 | + reason: |
| 153 | + - Self-approved. |
| 154 | + payload: |
| 155 | + Done. |
| 156 | +""" |
| 157 | + env = parse_envelope(raw) |
| 158 | + result = evaluate(env) |
| 159 | + print(f"[SELF-APPROVE] msg_id={result.msg_id} exit={result.exit} " |
| 160 | + f"violation={result.violations[0].code if result.violations else 'none'}") |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + print("=" * 60) |
| 165 | + print("EnvelopeGate Demo") |
| 166 | + print("=" * 60) |
| 167 | + demo_valid_request() |
| 168 | + demo_legacy_exit() |
| 169 | + demo_missing_header() |
| 170 | + demo_self_approve() |
| 171 | + print("=" * 60) |
0 commit comments