[Bug]: MerkleAuditChain failed append can leave entries and Merkle state inconsistent
Package
agent-governance-toolkit-core
Description
MerkleAuditChain._add_entry_locked() adds an entry to _entries before completing the Merkle tree update. If that update raises an exception, the entry remains, but the tree and root may still represent the previous state or be partially updated.
The failed write can therefore leave persistent internal inconsistency. Later appends do not necessarily repair it. verify_integrity() can still return true because it checks entry hashes and hash-chain links, not whether the Merkle tree matches the entries.
@MohammadHaroonAbuomar identified this specific pre-existing problem in his review of PR #4135 and explicitly left it for a separate follow-up. This issue covers failed-append atomicity only, not the separate reader-consistency concern.
How does this impact your work?
Found while investigating audit-log failure handling following the review of #4135.
A caller receives an error indicating that the write failed, but the failed entry can remain in the audit log without a corresponding Merkle tree update. This makes subsequent audit exports and inclusion proofs unreliable.
Normal successful logging is not blocked, but recovery after a failed append is unsafe: retrying or adding another entry does not reliably restore consistency.
Timeline
No response
Steps to Reproduce
On the affected version, run this deterministic reproduction. The injected exception simulates a node-creation failure; it does not require concurrent threads or actual memory exhaustion.
`from unittest.mock import patch from agentmesh.governance.audit import AuditLog
log = AuditLog() for i in range(5): log.log("test", "did:mesh:test", f"entry-{i}")
root_before = log._chain.get_root_hash()
with patch( "agentmesh.governance.audit.MerkleNode", side_effect=MemoryError("injected node-creation failure"), ): try: log.log("test", "did:mesh:test", "failed-entry") except MemoryError: pass
print("Entry count:", len(log.query(limit=None))) print("Root unchanged:", log._chain.get_root_hash() == root_before) print("Integrity check:", log.verify_integrity())`
Actual result:
Entry count: 6 Root unchanged: True Integrity check: (True, None)
Expected: the failed append leaves the original five entries, tree, root, and inclusion proofs unchanged.
Failures during parent-node creation and capacity growth also reproduce the problem, including partially modified tree state.
Environment
- Repository: microsoft/agent-governance-toolkit
- Component: agent-governance-python/agent-mesh
- File: src/agentmesh/governance/audit.py
- Method: MerkleAuditChain._add_entry_locked()
- Affected upstream commit: b644e4aab6e77c981a87661db44dc44f4133f84f, the merge of #4135
- Platform: Windows
- Python: 3.12.10
- Reproduction: deterministic exception injection, without sleeps or timing-dependent scheduling
Logs / Error Output
Code of Conduct
- [x] I agree to follow the Microsoft Open Source Code of Conduct
@MohammadHaroonAbuomar The fix is ready in [PR #4138](https://github.com/microsoft/agent-governance-toolkit/pull/4138). It restores the previous audit-log state if the tree update fails, with tests confirming that existing records and proofs remain valid and retrying succeeds.
Focused validation: 235 passed, 3 skipped. This addresses failed-append handling only; reader consistency remains a separate follow-up.