Python API Reference¶
Top-level exports¶
agentcrdt
¶
agentcrdt - Semantic-causal CRDT for agent-mutable world state.
ClosedLoopError
¶
Bases: ValueError
Raised when the gate refuses empty, constant-only, or unusable state.
GateOutcome(ok, verdict, reason, exit_code, fact_count=0, mutable_count=0, constant_count=0, constant_domains=(), refused_writes=0, conflict_count=0, divergence_count=0, human_required=False, contested_keys=())
dataclass
¶
Result of a closed-loop read of an agentcrdt world store.
Attributes:
| Name | Type | Description |
|---|---|---|
ok |
bool
|
True only when the pipeline may continue (PASS). |
verdict |
str
|
|
reason |
str
|
Always non-empty explanation. |
exit_code |
int
|
0 PASS, 1 FAIL (policy), 2 FAIL_LOUD (empty/unusable). |
fact_count |
int
|
Facts examined. |
mutable_count |
int
|
Facts in non-constant domains. |
constant_count |
int
|
Facts in constant-only domains. |
constant_domains |
tuple[str, ...]
|
Distinct constant domains seen. |
refused_writes |
int
|
Count of writes refused (when gating a write batch). |
conflict_count |
int
|
Unresolved ContradictionEvents (MAST path). |
divergence_count |
int
|
Silent multi-agent value divergences detected. |
human_required |
bool
|
True when multi-agent conflict needs human resolve. |
contested_keys |
tuple[str, ...]
|
Sample of entity.attribute keys that diverged. |
ValueDivergence(fact_id, domain, entity, attribute, agents, values, version_count)
dataclass
¶
A fact key where ≥2 agents wrote ≥2 distinct values (MAST silent LWW).
AgentTraceEvent(agent_id, tool, timestamp, payload_fp='', side_channel='', meta=dict())
dataclass
¶
One black-box behavioural event from an agent (tool/timing/payload).
CollusionReport(event_count, agent_count, signals, shared_payload_groups, sync_pairs, side_channel_groups)
dataclass
¶
Aggregate collusion analysis over a population of agent events.
CollusionSignal(kind, agents, detail, score=1.0)
dataclass
¶
One detected collusion pattern.
AgentMessage(msg_id, sender, receiver, content='', channel='peer', role='peer', architecture='')
dataclass
¶
One inter-agent coordination message.
Attributes:
| Name | Type | Description |
|---|---|---|
msg_id |
str
|
Stable message id. |
sender |
str
|
Sending agent id. |
receiver |
str
|
Target agent id (or |
content |
str
|
Message body (natural language or structured text). |
channel |
str
|
Logical channel name (peer, control, …). |
role |
str
|
Declared sender role (peer, planner, external, …). |
architecture |
str
|
Optional architecture tag for this hop. |
CommAttackReport(message_count, agent_count, signals, external_entry_count, privileged_spoof_count, injection_count, architecture, details=dict())
dataclass
¶
Aggregate communication integrity analysis.
CommAttackSignal(kind, msg_id, detail, score=1.0)
dataclass
¶
One communication-attack finding.
ContradictionEvent(rule, facts_involved, agent_a, agent_b, timestamp=time.time())
dataclass
¶
WorldFact(domain, entity, attribute, value, version=0, agent_id='', timestamp=time.time())
dataclass
¶
An immutable, content-addressed fact about world state.
The id field is derived from domain|entity|attribute so two
agents recording the same fact key always get the same ID.
__post_init__()
¶
to_dict()
¶
Serialise to a plain dict.
Source code in src/agentcrdt/fact.py
from_dict(d)
classmethod
¶
Deserialise from a plain dict produced by to_dict.
Source code in src/agentcrdt/fact.py
FactHistory(store)
¶
Source code in src/agentcrdt/history.py
get_history(entity, attribute)
¶
Return all versions of a fact, newest first.
Source code in src/agentcrdt/history.py
get_at_time(entity, attribute, timestamp)
¶
Return the fact that was current at a given timestamp.
Source code in src/agentcrdt/history.py
diff_entity(entity)
¶
Return full history of all attributes for an entity.
Source code in src/agentcrdt/history.py
MergeResult(merged_count, conflicts=list())
dataclass
¶
Summary of a completed merge operation.
to_dict()
¶
Serialise to a plain dict suitable for JSON output.
WorldMerger(rule_engine=None)
¶
Merge a remote :class:WorldStore into a local one using CRDT semantics.
Uses Last-Write-Wins (LWW) per fact key. After merging, optionally runs
the provided :class:~agentcrdt.rules.RuleEngine to detect semantic
contradictions and records them as :class:~agentcrdt.fact.ContradictionEvent
objects.
Initialise with an optional rule engine for contradiction detection.
Source code in src/agentcrdt/merger.py
merge(local, remote)
¶
Merge remote into local using LWW CRDT semantics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local
|
WorldStore
|
The target store (modified in-place). |
required |
remote
|
WorldStore
|
The source store (read-only). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
MergeResult
|
class: |
MergeResult
|
contradiction events detected by the rule engine. |
Source code in src/agentcrdt/merger.py
RuleEngine(rules)
¶
Evaluates a set of SemanticRule objects against a snapshot of world facts.
Initialise with a list of semantic rules to enforce.
Source code in src/agentcrdt/rules.py
check(facts)
¶
Check all semantic rules and return ContradictionEvents for violations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
facts
|
dict[str, WorldFact]
|
Mapping of |
required |
Returns:
| Type | Description |
|---|---|
list[ContradictionEvent]
|
A list of :class: |
Source code in src/agentcrdt/rules.py
SemanticRule(name, trigger_domain, trigger_attribute, trigger_value, implies_domain, implies_entity_same=True, implies_attribute='', implies_value=None)
dataclass
¶
A first-order semantic implication rule between two world facts.
When a fact matching trigger_domain / trigger_attribute / trigger_value
exists, the rule asserts that a related fact in implies_domain must have
implies_value. If the implied fact disagrees, a ContradictionEvent
is emitted.
WorldStore(path)
¶
Persistent store backed by a single SQLite database file.
Supports context-manager usage::
with WorldStore("world.db") as store:
store.set_fact(fact)
Open (or create) a WorldStore at path.
Source code in src/agentcrdt/store.py
close()
¶
__enter__()
¶
__exit__(*args)
¶
set_fact(fact)
¶
Store or update a fact using LWW semantics (higher version wins, then timestamp).
Source code in src/agentcrdt/store.py
get_fact(fact_id)
¶
Return a single :class:WorldFact by id, or None if not found.
Source code in src/agentcrdt/store.py
get_fact_by_key(domain, entity, attribute)
¶
Return a fact looked up by its natural key (domain, entity, attribute).
Convenience alternative to :meth:get_fact when you don't have the
SHA-256 fact_id at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
str
|
Fact domain, e.g. |
required |
entity
|
str
|
Entity name, e.g. |
required |
attribute
|
str
|
Attribute name, e.g. |
required |
Returns:
| Type | Description |
|---|---|
WorldFact | None
|
The matching :class: |
Source code in src/agentcrdt/store.py
list_facts(domain=None)
¶
Return all stored facts, optionally filtered by domain.
Source code in src/agentcrdt/store.py
add_event(event)
¶
Persist a :class:ContradictionEvent.
Source code in src/agentcrdt/store.py
list_events()
¶
Return all stored contradiction events ordered by timestamp.
Source code in src/agentcrdt/store.py
list_fact_history(fact_id)
¶
Return all historical rows for a fact_id ordered by recorded_at ASC.
Source code in src/agentcrdt/store.py
list_fact_history_by_entity_attr(entity, attribute)
¶
Return all historical rows for entity+attribute, ordered by recorded_at ASC.
Source code in src/agentcrdt/store.py
list_fact_history_by_entity(entity)
¶
Return all historical rows for an entity, ordered by recorded_at ASC.
Source code in src/agentcrdt/store.py
ChangeWatcher(store)
¶
Watch a WorldStore for changes to specific entities or attributes.
Source code in src/agentcrdt/watch.py
on_change(entity=None, attribute=None)
¶
Decorator: register a callback for when a matching fact changes.
Source code in src/agentcrdt/watch.py
check()
¶
Check for new facts since last check. Returns changed facts and fires callbacks.
Source code in src/agentcrdt/watch.py
assert_multi_agent_ok(store, **kwargs)
¶
Raise :class:ClosedLoopError unless :func:gate_multi_agent is ok.
Source code in src/agentcrdt/closed_loop.py
assert_mutable_write(fact, **kwargs)
¶
Raise :class:ClosedLoopError if fact is a constant-domain write.
Source code in src/agentcrdt/closed_loop.py
assert_world_state_ok(store, **kwargs)
¶
Gate and raise :class:ClosedLoopError unless outcome is ok.
Source code in src/agentcrdt/closed_loop.py
detect_silent_divergences(store)
¶
Find fact keys with multi-agent multi-value history and no contradiction event.
MAST / ICLR multi-agent failure class: agents write incompatible values for
the same domain.entity.attribute; LWW keeps one winner and no
:class:~agentcrdt.fact.ContradictionEvent is recorded - consumers see a
single "truth" that is actually contested.
Uses fact_history rows. A divergence requires:
- ≥ 2 distinct agent_ids (non-empty preferred)
- ≥ 2 distinct values
- no store event that lists this
fact_idinfacts_involved
Source code in src/agentcrdt/closed_loop.py
gate_merge_result(result, *, max_conflicts=0, min_merged=0)
¶
Gate a :class:~agentcrdt.merger.MergeResult after multi-agent merge.
merged_count < min_merged→ FAIL_LOUD (empty merge ornament)len(conflicts) > max_conflicts→ FAIL (human_required)- clean merge → PASS
Source code in src/agentcrdt/closed_loop.py
gate_multi_agent(store, *, max_unresolved_events=0, refuse_silent_divergence=True, require_facts=True)
¶
Gate a world store for MAST multi-agent coordination failures.
Load-bearing controls:
- Empty store → FAIL_LOUD (when
require_facts). - Unresolved :class:
~agentcrdt.fact.ContradictionEventcount abovemax_unresolved_events→ FAIL (human_required). - Silent value divergences (history multi-agent multi-value, no event) →
FAIL when
refuse_silent_divergence(MAST silent LWW).
Pair with :func:gate_world_state for CONST-AS-STATE domain checks.
Source code in src/agentcrdt/closed_loop.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |
gate_world_state(store, *, extra_constant_domains=None, allow_mixed=False, require_mutable=True)
¶
Gate a world store / fact list for CONST-AS-STATE discipline.
- Empty →
FAIL_LOUD(exit 2). - Only constant domains →
FAIL(exit 1) - refuse constant-only CRDT. - Constants mixed with mutable →
FAILunlessallow_mixed=True. - Mutable present (and no banned constants if not allow_mixed) →
PASS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
WorldStore | list[WorldFact]
|
:class: |
required |
extra_constant_domains
|
Iterable[str] | None
|
Additional domain names to treat as constants. |
None
|
allow_mixed
|
bool
|
If True, constant facts are counted but do not fail when mutable facts also exist (still refuse constant-only). |
False
|
require_mutable
|
bool
|
If True, at least one mutable fact is required. |
True
|
Source code in src/agentcrdt/closed_loop.py
is_constant_domain(domain, *, extra=None)
¶
True if domain is a constant/recipe/code domain (CONST-AS-STATE).
Source code in src/agentcrdt/closed_loop.py
refuse_constant_write(fact, *, extra_constant_domains=None)
¶
Gate a single write: constant domains FAIL (do not set_fact).
Returns PASS only for mutable-domain facts.
Source code in src/agentcrdt/closed_loop.py
set_fact_if_mutable(store, fact, *, extra_constant_domains=None)
¶
Write fact only if domain is mutable; otherwise refuse (CONST-AS-STATE).
Source code in src/agentcrdt/closed_loop.py
assert_no_covert_collusion(events, **kwargs)
¶
Raise :class:ClosedLoopError unless :func:gate_covert_collusion is ok.
Source code in src/agentcrdt/collusion.py
detect_covert_collusion(events, *, sync_window=1.0, min_agents_shared_payload=2, min_payload_rarity=3, min_side_channel_agents=2)
¶
Black-box collusion detector over tool/timing/payload traces.
Signals (paper-inspired, behavioural only):
- shared_payload — same non-empty
payload_fpappears for ≥min_agents_shared_payloaddistinct agents, and the fingerprint is rare overall (count of events with that fp ≤ population / rarity). - sync_timing — two agents fire the same tool within
sync_windowseconds more than once (pair-level). - side_channel_code — identical non-empty
side_channeltoken used by ≥min_side_channel_agentsagents.
Source code in src/agentcrdt/collusion.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
gate_covert_collusion(events, *, require_events=True, min_agents=2, max_signals=0, sync_window=1.0, refuse_shared_payload=True, refuse_sync_timing=True, refuse_side_channel=True)
¶
Refuse multi-agent populations with covert collusion signals.
Rules:
- No events when required → FAIL_LOUD
- Fewer than
min_agents→ FAIL_LOUD (need a population) - Any signal kind over budget (default 0) → FAIL
- Clean independent traces → PASS
Source code in src/agentcrdt/collusion.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
analyze_comm_attacks(messages, *, system_agents=None, trusted_external_senders=None, privileged_senders=None, architecture='dmas', injection_phrases=None)
¶
Detect External Entry Point and Privileged In-System attack signals.
Does not gate; use :func:gate_comm_integrity.
Source code in src/agentcrdt/comm_attack.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
assert_comm_integrity(messages, **kwargs)
¶
Raise :class:ClosedLoopError unless :func:gate_comm_integrity is ok.
Source code in src/agentcrdt/comm_attack.py
detect_comm_injection_phrases(text, *, phrases=None)
¶
Return coordination-injection phrases found in text.
Source code in src/agentcrdt/comm_attack.py
gate_comm_integrity(messages, *, system_agents=None, trusted_external_senders=None, privileged_senders=None, architecture='dmas', claim_coordinated=False, require_messages=True, refuse_external_entry=True, refuse_privileged_spoof=True, refuse_content_injection=True, max_signals=0, injection_phrases=None)
¶
Refuse multi-agent coordination when communication channel is attacked.
Public case: arXiv 2608.06830 — External Entry Point and Privileged In-System attacks against LLM-controlled multi-robot / multi-agent systems under DMAS / HMAS architectures.
Rules:
claim_coordinatedwith zero messages → FAIL_LOUD- Empty inventory when required → FAIL_LOUD
- External Entry Point (untrusted external sender) → FAIL
- Privileged In-System spoof (non-privileged on control plane) → FAIL
- Content injection phrases in messages → FAIL
- Signal count above
max_signals(default 0) → FAIL - Clean internal mesh → PASS
Source code in src/agentcrdt/comm_attack.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | |
conflicts_for_entity(store, entity)
¶
Get all conflicts for a specific entity.