build(agent): new-agents#a6e6ec iteration

This commit is contained in:
agent-a6e6ec231c5f7801 2026-04-21 11:04:54 +02:00
parent b65b0cf247
commit 6f1d56c697
2 changed files with 60 additions and 15 deletions

View File

@ -38,8 +38,8 @@ class TLSChannel:
"cipher": self.tls_config.get("cipher", "AES-256-GCM"), "cipher": self.tls_config.get("cipher", "AES-256-GCM"),
}, },
} }
with self._lock: # Do not mark outbound nonces as seen here to allow immediate
self._seen_nonces.add(nonce) # local send/receive (loopback) without falsely triggering replay protection.
return envelope return envelope
def receive(self, envelope: Dict[str, Any]) -> Dict[str, Any]: def receive(self, envelope: Dict[str, Any]) -> Dict[str, Any]:

View File

@ -1,17 +1,62 @@
import time import time
from energiamesh.core import LocalProblem, SharedVariables, PlanDelta, DualVariables, AuditLog from datetime import datetime
import pytest
from energiamesh.core import LocalProblem, SharedVariables, PlanDelta, AuditLog, DualVariables
from energiamesh.transport import TLSChannel
from energiamesh.bridge import EnergiBridge
def test_core_dataclasses_simple(): def test_local_problem_basic():
lp = LocalProblem(site_id="site-1", problem_id="p1", description="test", data={"a": 1}) lp = LocalProblem(site_id="site-001", description="test", data={"load": 1.0})
sv = SharedVariables(signals={"forecast": 10}, version=1) assert lp.site_id == "site-001"
dv = DualVariables(multipliers={"lambda": 0.5}, version=1) assert lp.data == {"load": 1.0}
pd = PlanDelta(delta={"dx": 1}, metadata={"source": "test"}, timestamp=time.time()) assert isinstance(lp.parameters, dict)
log = AuditLog()
log.log("created", {"obj": lp.problem_id})
assert lp.site_id == "site-1"
assert sv.version == 1 def test_shared_variables_update_and_timestamp():
assert dv.multipliers["lambda"] == 0.5 sv = SharedVariables(signals={"forecast_load": 10.0})
assert "dx" in pd.delta old_ts = sv.timestamp
assert len(log.entries) == 1 time.sleep(0.01)
sv.update("forecast_load", 12.0)
assert sv.signals["forecast_load"] == 12.0
assert sv.timestamp >= old_ts
def test_plan_delta_and_metadata():
lp = LocalProblem(site_id="site-01")
sv = SharedVariables(signals={"forecast": 5.0})
delta = PlanDelta(delta={"update": True}, delta_id="d-1", source="unit-test")
assert delta.delta["update"] is True
assert delta.delta_id == "d-1"
assert delta.source == "unit-test"
def test_audit_log_basic():
al = AuditLog()
al.add("first entry")
al.log("event", {"k": 1})
assert len(al.entries) == 2
assert "ts" in al.entries[0]
def test_tls_channel_replay_protection():
chan = TLSChannel(peer_id="tester")
payload = {"hello": "world"}
envelope = chan.send(payload)
# First receive should succeed and return the payload
received = chan.receive(envelope)
assert received == payload
# Replay a second time should raise
with pytest.raises(ValueError):
chan.receive(envelope)
def test_energi_bridge_translation_basic():
bridge = EnergiBridge(version="0.1")
lp = LocalProblem(site_id="site-bridge", description="test bridge")
translated = bridge.to_catopt(lp)
assert translated["type"] == "LocalProblem"
assert translated["site_id"] == "site-bridge"
assert translated["version"] == "0.1"