|
| 1 | +""" |
| 2 | +Bridge ESP-001 into the real commit_gate_core CommitGate. |
| 3 | +
|
| 4 | +This is still a synthetic, in-memory harness. |
| 5 | +The important difference from the local toy gate is that this bridge calls |
| 6 | +src.commit_gate_core.gate.CommitGate.execute directly. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from dataclasses import dataclass, field |
| 12 | +from datetime import datetime, timezone |
| 13 | +from typing import Any, Dict, List, Mapping, Optional |
| 14 | + |
| 15 | +from commit_gate_core.gate import CommitGate, GateResult |
| 16 | + |
| 17 | + |
| 18 | +@dataclass |
| 19 | +class StaticSignatureVerifier: |
| 20 | + valid: bool = True |
| 21 | + |
| 22 | + def verify(self, record: Mapping[str, Any]) -> bool: |
| 23 | + return self.valid |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class InMemoryNonceLedger: |
| 28 | + consumed: Dict[str, str] = field(default_factory=dict) |
| 29 | + |
| 30 | + def contains(self, nonce: str) -> bool: |
| 31 | + return nonce in self.consumed |
| 32 | + |
| 33 | + def consume(self, nonce: str, decision_id: str) -> None: |
| 34 | + self.consumed[nonce] = decision_id |
| 35 | + |
| 36 | + def rollback(self, nonce: str, decision_id: str) -> None: |
| 37 | + if self.consumed.get(nonce) == decision_id: |
| 38 | + del self.consumed[nonce] |
| 39 | + |
| 40 | + |
| 41 | +@dataclass |
| 42 | +class InMemoryAuditSink: |
| 43 | + events: List[Mapping[str, Any]] = field(default_factory=list) |
| 44 | + |
| 45 | + def append(self, event: Mapping[str, Any]) -> None: |
| 46 | + self.events.append(dict(event)) |
| 47 | + |
| 48 | + |
| 49 | +@dataclass |
| 50 | +class FixedClock: |
| 51 | + fixed: datetime = datetime(2026, 5, 12, 10, 1, 0, tzinfo=timezone.utc) |
| 52 | + |
| 53 | + def now(self) -> datetime: |
| 54 | + return self.fixed |
| 55 | + |
| 56 | + |
| 57 | +@dataclass |
| 58 | +class CommitGateBridgeResult: |
| 59 | + gate_result: GateResult |
| 60 | + audit_events: List[Mapping[str, Any]] |
| 61 | + downstream_send: bool |
| 62 | + send_call_count: int |
| 63 | + receipt_written: bool |
| 64 | + |
| 65 | + |
| 66 | +def build_gate(*, mutation_callback, audit: Optional[InMemoryAuditSink] = None) -> tuple[CommitGate, InMemoryAuditSink]: |
| 67 | + audit_sink = audit or InMemoryAuditSink() |
| 68 | + gate = CommitGate( |
| 69 | + verifier=StaticSignatureVerifier(valid=True), |
| 70 | + nonce_ledger=InMemoryNonceLedger(), |
| 71 | + audit=audit_sink, |
| 72 | + mutation_callback=mutation_callback, |
| 73 | + accepted_policy_versions=("esp-v0.1",), |
| 74 | + clock=FixedClock(), |
| 75 | + ) |
| 76 | + return gate, audit_sink |
| 77 | + |
| 78 | + |
| 79 | +def esp001_attempt() -> Dict[str, Any]: |
| 80 | + return { |
| 81 | + "actor_id": "agent://morpheus-draft-bot-v1", |
| 82 | + "action": "SEND_EXTERNAL_EMAIL", |
| 83 | + "object_id": "external:partner-domain.com", |
| 84 | + "environment": "enterprise-shaped-synthetic-harness", |
| 85 | + "commit_hash": "sha256:a3f8c2d1e7b904561ac38fd2e190bba47c2d3f8a1e056b7c9d4e2f1a8b3c7d90", |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +def esp001_missing_decision_record() -> None: |
| 90 | + return None |
| 91 | + |
| 92 | + |
| 93 | +def dispatch_esp001_through_real_gate(adapter) -> CommitGateBridgeResult: |
| 94 | + attempt = esp001_attempt() |
| 95 | + |
| 96 | + def mutation_callback(record: Mapping[str, Any]) -> None: |
| 97 | + adapter.send( |
| 98 | + recipient=attempt["object_id"], |
| 99 | + payload_hash=attempt["commit_hash"], |
| 100 | + actor=attempt["actor_id"], |
| 101 | + ) |
| 102 | + |
| 103 | + gate, audit = build_gate(mutation_callback=mutation_callback) |
| 104 | + gate_result = gate.execute( |
| 105 | + record=esp001_missing_decision_record(), |
| 106 | + actor_id=attempt["actor_id"], |
| 107 | + action=attempt["action"], |
| 108 | + object_id=attempt["object_id"], |
| 109 | + environment=attempt["environment"], |
| 110 | + commit_hash=attempt["commit_hash"], |
| 111 | + ) |
| 112 | + |
| 113 | + return CommitGateBridgeResult( |
| 114 | + gate_result=gate_result, |
| 115 | + audit_events=audit.events, |
| 116 | + downstream_send=adapter.send_call_count > 0, |
| 117 | + send_call_count=adapter.send_call_count, |
| 118 | + receipt_written=len(audit.events) == 1, |
| 119 | + ) |
0 commit comments