Skip to content

fix(agent-mesh): keep audit state consistent during concurrent writes - #4147

Merged
MohammadHaroonAbuomar merged 1 commit into
microsoft:mainfrom
electricwolfemarshmallowhypertext:codex/audit-state-consistency-4146
Sep 25, 2026
Merged

MohammadHaroonAbuomar merged 1 commit into
microsoft:mainfrom
electricwolfemarshmallowhypertext:codex/audit-state-consistency-4146

Conversation

@electricwolfemarshmallowhypertext

Copy link
Copy Markdown
Contributor

Related Issue

Fixes #4146

Problem & Solution

Concurrent writes can expose a pending entry during verification, leave agent/type indexes inconsistent with the committed chain, or produce service responses whose count and Merkle root describe different states.

This change:

  • Captures committed entries before verifying hash and link integrity, with verification outside the chain lock.
  • Publishes each chain entry and both indexes under the same lock, rolling back index changes if publication fails.
  • Captures indexed-reader state under that lock while preserving limits and ordering.
  • Makes AuditService.entry_count wait for committed state and builds summary count, root, and verification from one snapshot.
  • Makes the collector verification endpoint consume that combined result without changing its response shape.

Sink calls remain outside the chain lock. Public signatures and response shapes are unchanged.

Type of Change

Non-breaking bug fix.

Package Affected

AgentMesh (agent-mesh).

Testing

  • Focused AgentMesh audit, service, sink, and collector suite: 345 passed, 3 POSIX-only skips.
  • All 71 new regression cases pass.
  • Event-driven concurrency cases repeated ten times: 270 passed.
  • Ruff: new test files and audit_collector.py pass. The production audit and service modules retain 47 unchanged pre-existing findings, zero new findings.
  • Python compilation, changed-line spell check, and git diff --check passed.

Regression coverage includes append rollback, concurrent index-bucket creation, reversed sink completion, sink failures and callbacks, index-publication failures, consistent service/collector snapshots, and existing corruption detection.

AI Assistance

Codex assisted with implementation, regression tests, and validation.

Capture verification snapshots and publish audit entries with their
agent/type indexes under the chain lock. Use one snapshot for service
summaries and collector verification responses.

Add deterministic regression coverage for concurrent appends,
rollback, index publication, and response consistency.

Signed-off-by: Electric Wolfe Marshmallow Hypertext <tionne@gmail.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@github-actions github-actions Bot added tests agent-mesh agent-mesh package size/XL Extra large PR (500+ lines) labels Sep 25, 2026
@github-actions

Copy link
Copy Markdown

PR Review Summary

Check Status Details
🔍 Code Review ⚠️ Missing No current-run comment
🛡️ Security Scan ⚠️ Missing No current-run comment
🔄 Breaking Changes ⚠️ Missing No current-run comment
📝 Docs Sync ⚠️ Missing No current-run comment
🧪 Test Coverage ⚠️ Missing No current-run comment

Verdict: ⚠️ AI review incomplete; ready for human review

AI review comments are untrusted advisory output. The summary reports workflow-generated completion status only, not model-authored pass/fail claims.

@MohammadHaroonAbuomar MohammadHaroonAbuomar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 8c241a6. Verified in code and by probe: each entry and both indexes now publish under the one chain lock, and a failure in either the tree update or an index append rolls all of it back together (probed with the very first append and with the second index failing after the first succeeded: chain, both indexes and root end exactly as before, and the sink is not called); verify_chain, entry_count and summary() read one committed snapshot and verify outside the lock, so a rollback in progress can no longer produce a false integrity failure, which main did; the collector responses are byte-identical for the same log. Under two appenders and 43k summary samples, main reported a root that did not match its count 795 times; this branch never did. The 71 new tests are event-driven, and 40 of them fail against main's sources. No path takes the lock while holding it, sink writes stay outside it, and after this change I find no remaining unlocked reader of chain or index state in agent-mesh. All 123 checks are green.

Three optional follow-ups:

  • audit.py:669 and :683: the indexed readers copy the whole entry list under the lock and then filter, which is O(n·limit); at 10k entries one read costs about 14 ms and two hot readers hold appends to about 540/s. Walking the list from the end with a set of the wanted ids, stopping once all are found, gives the same order and limit semantics at about 16 µs per read and appends near 1240/s.
  • services/audit/init.py:138: summary() reaches through _log._chain._snapshot() and _verify_entries; a public verify_snapshot() on AuditLog returning entries, root and validity would let the service and collector stop depending on chain internals.
  • audit.py:726: the verify_integrity docstring still says "Always valid."

@MohammadHaroonAbuomar
MohammadHaroonAbuomar merged commit f263eff into microsoft:main Sep 25, 2026
136 checks passed
@electricwolfemarshmallowhypertext
Tionne Smith (electricwolfemarshmallowhypertext) deleted the codex/audit-state-consistency-4146 branch September 25, 2026 17:15
Yuvraj Singh (yuvrajsingh2428) pushed a commit to yuvrajsingh2428/agent-governance-toolkit that referenced this pull request Oct 1, 2026
…microsoft#4147)

Capture verification snapshots and publish audit entries with their
agent/type indexes under the chain lock. Use one snapshot for service
summaries and collector verification responses.

Add deterministic regression coverage for concurrent appends,
rollback, index publication, and response consistency.

Signed-off-by: Electric Wolfe Marshmallow Hypertext <tionne@gmail.com>
Signed-off-by: yuvrajsingh2428 <offcyuvi2428@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-mesh agent-mesh package size/XL Extra large PR (500+ lines) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: AuditLog can expose inconsistent chain, index, and service state during concurrent writes

2 participants