Skip to content

fix(core): serialize concurrent agent instantiation per ID - #8353

Open
Winoren (Winoren) wants to merge 1 commit into
microsoft:mainfrom
Winoren:fix/serialize-agent-instantiation
Open

Winoren (Winoren) wants to merge 1 commit into
microsoft:mainfrom
Winoren:fix/serialize-agent-instantiation

Conversation

@Winoren

Copy link
Copy Markdown

Why are these changes needed?

SingleThreadedAgentRuntime._get_agent() checks its instance cache before awaiting the registered factory, then stores the result. If an async factory yields, concurrent lookups of the same AgentId all miss the cache and each construct a separate agent. Earlier returned instances are then replaced in the cache, splitting agent state and leaving those instances outside the runtime's normal lifecycle tracking.

Serialize construction per AgentId with an asyncio lock and recheck the cache after acquiring it. Different agent IDs can still initialize concurrently. Factory exceptions or cancellation release the lock, allowing a later lookup to retry; successful lookups continue to use the existing cache fast path.

Regression tests use the real runtime and agent factories to check:

  • Ten concurrent lookups share one instance, followed by ten messages updating that instance's state.
  • Factories for different keys can overlap.
  • A waiting lookup can retry after the first factory raises an exception or is cancelled.

Related issue number

Found while inspecting the current runtime implementation; no linked issue.

Validation

Based on upstream 027ecf0a379bcc1d09956d46d12d44a3ad9cee14, using Python 3.11 in an isolated uv environment with editable core/test-utils packages:

  • Before the fix, the concurrent-lookup regression fails: 10 instances created instead of 1.
  • python -m pytest python/packages/autogen-core/tests/test_runtime.py python/packages/autogen-core/tests/test_closure_agent.py python/packages/autogen-core/tests/test_cancellation.py python/packages/autogen-core/tests/test_intervention.py -q: 28 passed.
  • Ruff 0.4.8 lint and format checks on both changed files: passed.
  • Mypy 1.13.0, --config-file python/pyproject.toml --follow-imports=silent on both changed files: passed.
  • git diff --check: passed.

Full workspace checks, documentation builds and distributed-runtime integration tests were not run. This change addresses instance construction in the single-threaded runtime; it does not serialize message-handler execution.

Checks

  • I've included any doc changes needed for https://microsoft.github.io/autogen/. No documentation change is needed for retaining one instance per agent ID.
  • I've added tests (if relevant) corresponding to the changes introduced in this PR.
  • I've made sure all auto checks have passed. Local checks are listed above; upstream CI is pending.

Prepared with Codex assistance.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant