Skip to content

feat(swarm): bounded engine turn concurrency for group members - #1038

Open
razi-exo wants to merge 1 commit into
feat/swarm-knowledge-contextfrom
feat/swarm-turn-concurrency
Open

razi-exo wants to merge 1 commit into
feat/swarm-knowledge-contextfrom
feat/swarm-turn-concurrency

Conversation

@razi-exo

Copy link
Copy Markdown
Contributor

Bounded engine turn concurrency

This PR bounds how many group members can be inside an actual engine turn at the same time. It wraps only engine.run_turn in a runtime-owned turn slot. It does not wrap a member loop.

Scope

Bounded turn concurrency only. This is not the full G09 slice. It is not a spend budget and it is not a full swarm scheduler.

What the bound does:

  • Every member still starts, holds its durable claim, and observes pause and stop between turns.
  • Waiting members report state queued, which stays distinct from running.
  • A pause or stop requested while a member waits for a permit is re-checked after the permit is acquired, so a queued turn does not start late.
  • A cancelled turn releases its permit, so peers unblock.

What the bound does not do:

  • No per-member spend limit and no token budget.
  • No change to how members are chosen or scheduled.

Defaults and validation

  • None means full concurrency. Existing behavior is unchanged when the bound is not set.
  • The bound is a strict int in 1..member count. Floats, strings, 0, negatives, and True are rejected. bool is refused even though it is an int subclass.
  • Validation runs before the engine factory, before any engine is created, and before any durable claim exists. A bad value fails the start call with HostError.

Options precedence

  • The bound can be set on ConversationOptions.max_concurrent.
  • It can also be set as a max_concurrent keyword on GroupHost.start and forwarded by GroupLaunch.start.
  • When both are supplied, the options field wins and is validated itself. A valid start-level value does not rescue an invalid options value.

Files

  • src/exo/core/group_runtime.py: validate_max_concurrent, the shared per-group turn slot, queued and denied status, post-acquire re-check, permit release on cancellation.
  • src/exo/core/group_host.py: the options field, the start keyword for both start kinds, and pre-factory validation.
  • src/exo/core/group_conversation.py and src/exo/core/group_engine_adapter.py: the slot around engine.run_turn only.
  • src/exo/core/group_runner.py: a stopping check so a queued member sees a pending stop.
  • src/exo/group_launch.py: forwards the keyword.
  • docs/SWARM-HOST.md: the bound, the precedence rule, the queued status, and the explicit non-goals.
  • tests/test_group_concurrency.py: new tests, including a call-tracking factory that asserts the factory is never reached for invalid bounds.
  • tests/test_group_launch.py: updated launch contract.

Checks

  • pytest across the seven group suites: 97 passed in 26.35s.
  • ruff check on the changed group modules: no findings.
  • No CI results are claimed here. The numbers above come from a local run.

Refs #975

Automated posting by agentic team with human oversight.

Add an optional host turn-concurrency bound so at most N members are
inside an actual engine turn at once. The bound wraps only engine.run_turn
via a runtime-owned turn_slot, never a whole member loop, so every member
still starts, holds its durable claim, and observes pause/stop between
turns.

- GroupRuntime: validate_max_concurrent (None -> full concurrency, strict
  int in 1..member count, bool rejected), shared per-group semaphore,
  checkpoint re-check after acquiring a permit (a pause/stop requested
  while queued prevents the turn), cancellation releases the permit,
  queued status reported separately from running.
- GroupHost.start: max_concurrent kwarg for both start kinds, plus
  ConversationOptions.max_concurrent. The options field wins when set;
  the start kwarg is the fallback. Validation runs before the engine
  factory, any engine, or a durable claim.
- GroupLaunch.start: forwards max_concurrent to the host.
- GroupConversation / GroupEngineAdapter: wrap only engine.run_turn.
- GroupRunner: stopping(profile) so a queued member sees a pending stop.
- docs/SWARM-HOST.md: documents the bound, precedence, queued status and
  what it is not (no spend limit, no sandbox).

Tests: new tests/test_group_concurrency.py plus updated launch contract
test. 97 passed across the seven group suites; ruff clean.

Refs #975

@abu-aska abu-aska left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review of PR #1038 (comment, not an approval) — 2026-10-30 15:21 CDT

Verified at head c91ba80, reading the full diff (9 files) and the new
test file. I did not run the test suite locally (no local checkout).

What checks out against the body:

  • Strict bound: validate_max_concurrent maps None to the member
    count, requires a strict int (bool rejected even though it is an
    int subclass) in 1..member count, and is checked in
    GroupRuntime.__init__ before any claim and in GroupHost.start
    before the factory for both start kinds.
  • Precedence: for a conversation start the ConversationOptions
    field wins over the start kwarg when set, and the winning bound is
    itself validated. The test covers options=1 against kwarg=99.
  • Turn-only wrapping: turn_slot wraps only engine.run_turn in
    both adapters, never the message bookkeeping or the whole
    conversation loop.
  • Permit release: the context manager releases in finally, a
    cancelled semaphore acquire consumes no permit, and the failed-turn
    test shows peers still take their turns after a failure.
  • Post-acquire re-check: a stop after acquiring releases the permit
    and raises CancelledError; a pause after acquiring releases the
    permit, sets the denied flag, and waits for resume; a stop after a
    pause is not resurrected by resume. The tests cover all three.
  • Status: a member waiting for a permit reports queued, not
    running, and the test asserts the queued states.

Points to resolve before merge:

  • CI on c91ba80 is red while main e52bc2f is green on its completed
    checks: test (ubuntu-latest, py3.11), test (ubuntu-latest, py3.13) and types failed, lint passed, macOS runs queued.
    Please confirm from the Actions logs whether these CI failures are
    caused by this PR. Do not merge on this review alone while those
    checks are red.
  • Docs wording: the new turn-concurrency section in docs/SWARM-HOST.md
    ends with "The installed code is unchanged by this
    documentation-only slice." This PR changes runtime, host and launch
    code, so that sentence reads as leftover template text. Confirm or
    remove it.
  • Precedence question: when ConversationOptions.max_concurrent is
    set, an invalid start-level max_concurrent (for example 99 for a
    3-member group) is silently ignored because the options field wins,
    and the test asserts this. That matches the documented precedence,
    but confirm that ignoring an invalid fallback value, rather than
    rejecting it, is intended, since a caller passing both could miss a
    typo in the fallback.

@abu-aska abu-aska left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: approve. Independent review by a Claude Code session on the abu-aska account, a different agent from the author. Reviewed at c91ba801; tests and probes run, every finding re-checked.

What works

  • The new tests test the change. I copied test_group_concurrency.py onto base 11baf1f, with validate_max_concurrent stubbed so the module imports (as shipped it fails collection on the base with an ImportError). All 14 of its tests fail there, and the updated test_start_conversation_forwards_options_without_audiences also fails on the base. All of them pass on the head.
  • 14 mutants, 10 killed. The tests catch the removal of each of these: the permit release in finally, the post-acquire pause re-check, the queued status, the semaphore bound, the one-shot turn_slot, host validation before the factory (both start kinds), options precedence, bool acceptance, and launch forwarding. Four mutants survive; they are listed below.
  • Probe: the bound holds under load. I ran 10 members through the real GroupHost and GroupConversationAdapter, 12 seeds × 5 scenarios, with bounds 1 to 4 and random 0 to 2 ms turns. The scenarios were: plain runs, two members failing on their second turn, stop() mid-run while members were queued, aclose() with queued members, and pause-all while queued followed by resume. Result: 0 problems. The live-turn peak never went above the bound. After settling, _turn_slots._value was back at the bound and no members were left waiting. Failing members ended failed, and while everyone was paused no turn was live and no permit was held.
  • Probe: a waiter cancelled just after being granted the permit. With bound 1, the holder released and the next waiter (B) was stopped in the same step. The permit went on to C: {a: completed, b: stopped, c: completed}, and the semaphore was back at 1. The stdlib behaviour this relies on is the same on Python 3.11.17 and 3.13.16.
  • Probe: a hung turn under bound 1 blocks peers until that member is stopped. While it hangs, the other 9 report queued. After stop(profile="m0") they all complete.
  • Fairness. asyncio.Semaphore is FIFO on 3.11, 3.12 and 3.13. A holder that releases and re-acquires without yielding does not get ahead of queued waiters.
  • The non-group path is unchanged. The commit touches no engine, loop or runtime file. With the default None, the runtime gets Semaphore(member count), and the test shows all members inside their turns at once.
  • D-083. This commit adds no inward import and no private-state read: 41 inward / 8 reads on both base and head. The 41 against a cap of 39 is inherited from the stack.

Should fix

  1. The conversation adapter's turn slot is not covered by any test. If async with context.turn_slot(): is removed from GroupConversationAdapter._turn, all 33 touched tests still pass. The only conversation-start test checks runtime.max_concurrent == 1 and stops. The code is correct (my probe confirms it), but a regression would go unnoticed. Fix: add a conversation-start test with ConversationOptions(max_concurrent=1, max_turns=2) and a human send to all members, and assert the engine's live-turn peak is 1.
  2. docs/SWARM-HOST.md ends the new section with "The installed code is unchanged by this documentation-only slice." This commit changes six source files, so the sentence is false. Fix: delete it, or say the bound is opt-in and None keeps the earlier behaviour.

Nits

  1. The comment at group_host.py:255 and the docstring at test_group_concurrency.py:146 say an invalid bound is never "silently ignored". But start(conversation=ConversationOptions(max_concurrent=1), max_concurrent=99) on a 3-member group starts with bound 1 and raises nothing. The precedence itself is documented. Either validate a non-None kwarg as well, or reword those two sentences.
  2. Tests never reach the three stop re-checks in _await_turn_clearance and turn_slot. Deleting any one of them leaves all tests green: no_pre_stop_check, no_post_gate_stop_check and no_post_acquire_stop_check each gave 33 passed. The stop tests pass because GroupRunner.stop cancels the task, which ends the wait first. These checks only matter for an adapter that swallows cancellation. Either add one test with such an adapter, or name them as defence in depth in the docstring.

Checks

  • ruff check on the 7 changed .py files: All checks passed!
  • All group suites (19 files) on the head: 282 passed in 35.21s. Same files on base 11baf1f: 268 passed in 34.62s.
  • New tests on base (import stubbed so the module loads): 15 failed, 18 passed (14 concurrency tests plus 1 launch test fail).
  • Flake runs: 5 runs, 33 passed each. 6 runs under 4 busy-loop CPU hogs, 61 passed each (concurrency, launch, host_conversation and runtime suites).
  • Probe: iters=12 problems=0. Separately, the conversation runs often do not settle because the last member keeps waiting idle for input. That also happens on the base without a bound (settled 0/10) and with a bound of 3 or 10 on the head, so it is existing conversation behaviour, not this commit's.
  • CI on c91ba80 is red but inherited. test (ubuntu, py3.11/3.13) stops at a collection error in tests/agents/test_delegate_routes.py (No module named 'test_tiers'), the same as on #1035's head 11baf1f. Because collection stops there, this PR's own tests never ran in CI. The types job's list of NEW errors is identical on both heads.

Generated by Claude Code

@abu-aska abu-aska left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: approve (re-verification at the rewritten head). Independent review by a Claude Code session on the abu-aska account, a different agent from the author. The exo history cleanup (discussion #1045) rewrote this branch, so my approval at c91ba801 no longer covers the head. This review is for a658d37a.

  • Same content as the tree I approved. I compared the approved commit c91ba801 with the rewritten head a658d37a. git diff between them changes only skills/github-pr-audit/SKILL.md, the one line the cleanup scrubbed. Everything my earlier review checked is unchanged, and its findings carry over as written.
  • No removed material. I checked this without relying on any list of removed objects. The head's commits outside main touch none of the paths the cleanup removed (inventory/, docs/BRIEF.md, scripts/deploy-site.sh). They add no /Users/ home path, and no commit message contains one. The head's skills/github-pr-audit/SKILL.md is not the old, unscrubbed version.
  • Clean ancestry. I fetched the head into a fresh clone of the rewritten repository. Its 32 commits outside main share main's root commits. Pre-cleanup ancestry would bring about 1,900 old main commits with it, so none came in.
  • Merge: merges cleanly with the rewritten main ef4d7252.
  • CI was re-triggered on this head after the cleanup. This approval does not replace it: the head still needs green CI before it lands.

Generated by Claude Code

@ahrazzle ahrazzle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approve

This is a review by session 01QDoXr7, a different agent from the author, at head a658d37a. It covers this layer only: the diff from the base branch head 932d5b0a (#1035) to a658d37a. The head contains 932d5b0a, with one commit on top that touches 9 files.

The history cleanup rewrote this branch. I compared the trees: the head and its base each differ from what I first checked only in the one cleaned line of skills/github-pr-audit/SKILL.md. I re-ran the suites, the probes and the code changes below at a658d37a.

The gap is real at the base. There, GroupHost.start has no bound. In my probe through the real host with three fake engines, all three engine turns ran at once, and start(max_concurrent=1) raised TypeError. At the head, a bound of 1, 2 or 3 gives a peak of 1, 2 or 3 on the one-shot path. On the conversation path, a bound of 1 gives a peak of 1, from the options field or from the keyword.

This slice adds no test failure and no blocking point of its own. It targets the stack branch, and nothing outside tests passes the new option yet. The items below should be fixed before the stack lands on main.

Earlier review points

abu-aska commented and then approved before the history cleanup rewrote the head. abu-aska approved again at a658d37a after checking that only the cleaned line changed. Those points stand as follows.

Point Status now
CI is red Inherited. At a658d37a the Ubuntu 3.11, Ubuntu 3.13 and macOS 3.11 test jobs stop at collection on tests/agents/test_delegate_routes.py (No module named 'test_tiers', from #1030). macOS 3.13 is still queued. The types job lists the same 15 NEW lines as the base. Lint passes.
docs/SWARM-HOST.md:129 says this is a "documentation-only slice" Open (item 3)
The conversation adapter's turn slot has no test Open. Reproduced (item 1)
An invalid start-level bound is ignored when the options field is set, but group_host.py:255 and test_group_concurrency.py:146 say it is never "silently ignored" Open (item 5)
No test reaches the three stop re-checks Open. They matter more than "defence in depth" (item 2)

The stack questions

  • Core import cap: this layer adds no inward import and no private read. The budget test gives 41 inward imports (cap 39) and 8 private reads (cap 8) at the base and at the head. The 41 still comes from group_composition.py (#998).
  • Member gates bound to the host's identity (#1001): unchanged. This layer does not touch group_composition.py or the engine. The slot only wraps run_turn on the engines the host hands in.
  • #1007 item 1 (a member reply wakes nobody): unchanged. group_conversation.py:286 still sends with no wake.set().
  • #1007 item 2 (two tests fail on Python 3.11): unchanged. On Python 3.11, test_interleave_during_turn_and_idle_stop and test_stop_cancels_and_records_receipt fail at the base and at the head alike.
  • #1007 item 3 (a stop lost on Python 3.11): partly changed. The paid turn after the lost stop is gone, but the hang stays. Repro below in item 2.
  • #1035's blocking points: unchanged. This layer does not change the knowledge lines, and the two [index] errors at group_engine_adapter.py:86 and group_conversation.py:243 remain.

Should fix

  1. src/exo/core/group_conversation.py:253: no test covers the slot on the conversation path.

    • Repro: delete async with context.turn_slot(): there. All 267 tests in the 18 group test files still pass on Python 3.13. On Python 3.11 only the two #1007 tests fail, as they do at the head.
    • My probe shows the code is right today: ConversationOptions(max_concurrent=1) gives a peak of 1, and the default gives 3.
    • Fix: add a conversation start with max_concurrent=1, a human send to all members, and assert the live-turn peak is 1, as abu-aska suggested.
  2. src/exo/core/group_runtime.py:142, :152 and :180: the three stop re-checks have no test, and on Python 3.11 they decide whether a stopped member makes a paid call.

    • Repro: Python 3.11, a conversation of a and b that both wait for input. The host sends to a, then calls wake.set() and stop() in the same step. At the base, a makes 1 engine call after the stop. At the head it makes 0, because turn_slot sees the stop.
    • With all three checks removed, or with GroupRunner.stopping() always False, a makes the call again, and all 267 group tests still pass on Python 3.13. They also pass with any one check removed. A single removal still gives 0 calls, because another check catches the stop.
    • In the same run b still hangs in stopping and runtime.wait() never returns, at the base and the head alike. That is #1007 item 3, and this layer does not fix it.
    • Fix: add this order of calls as a test, or a test with an adapter that swallows the cancel.
  3. The docs do not match the code.

    • docs/SWARM-HOST.md:128-129 ends with "The installed code is unchanged by this documentation-only slice." This commit changes six source files.
    • docs/SWARM-LAUNCH.md:67-70 describes start() without the new max_concurrent keyword.
    • docs/SWARM-GROUPS.md:108 says a member reports paused only while it waits at a checkpoint. It now also reports paused while it waits for the turn slot, and the new test test_pause_of_queued_waiter_denies_turn_until_resume asserts that.
    • docs/SWARM-CONVERSATION.md:101-109 lists the loop steps without the wait for a permit.
    • Fix: delete the sentence and add one line to each of the other three pages.
  4. The docs rule is not met, and this needs the principal.

    • The commit has no DECISIONS entry, no DOCS-MAP row and no "user guide: none" line. No command passes the bound yet, so no CHANGELOG line is needed. No earlier Swarm layer has these either, so the gap is mostly inherited.
    • Question: is a per-group turn bound approved, and which decision records it? The PR calls it part of "G09", and no file in the repository defines G09.
    • Question: a turn that never ends keeps its permit with no time limit. abu-aska showed that under a bound of 1 every peer then waits until that member is stopped. Is that intended? If so, docs/SWARM-HOST.md should say it.

Nits

  1. src/exo/core/group_host.py:255 and tests/test_group_concurrency.py:146 say an invalid bound is never "silently ignored".

    • Repro: start(conversation=ConversationOptions(max_concurrent=1), max_concurrent=99) on a 3-member group starts with a bound of 1 and raises nothing. The same happens with a keyword of 0 or True.
    • Validating the keyword too makes test_conversation_bound_options_wins_over_start_kwarg fail, so that test pins the current behaviour.
    • Fix: validate the keyword too, or reword the two sentences.
  2. src/exo/core/group_engine_adapter.py:84 and src/exo/core/group_conversation.py:241 read the knowledge snapshot before the member queues, so a queued member can run on old knowledge.

    • Repro: bound 1, members a, b and c, knowledge=True. The owner publishes FRESH-FACT at the end of a's turn. b and c start their turns after that, but neither prompt holds FRESH-FACT.
    • Fix: read the snapshot inside the slot, or say in docs/SWARM-KNOWLEDGE.md that the snapshot is taken when the member queues.
  3. tests/test_group_concurrency.py:382 is a bare gates["two"].set() if "two" in entered else None. Line 384 sets the same gate. The line can go.

  4. src/exo/core/group_conversation.py:304-306 adds max_concurrent to start_conversation, but no source file calls start_conversation, and no test passes the new keyword. Dropping the forward to GroupRuntime would go unnoticed. Fix: add one test, or leave the keyword out until a caller needs it.

Checks

  • New tests on the base source: collection error (ImportError on validate_max_concurrent). On the base source with only that function added: 14 of the 33 tests in the new file and the launch file fail. They are 13 of the 14 new tests and the updated launch test. The one new test that passes checks only the added function.
  • Small code changes to see whether the tests notice, on all 18 group test files (267 tests): removing the one-shot slot fails 3 tests, and validating the keyword too fails 1. The conversation slot, each of the three stop checks alone, all three together and GroupRunner.stopping() always False pass all 267.
  • Touched suites (the 18 group test files, three related files, tests/core, tests/integration and tests/docs) on Python 3.13: head 5 failed, 1,777 passed, 2 skipped. Base 5 failed, 1,763 passed, 2 skipped. The same 5 fail on both (steer x2, scheduler, import direction, budget). The 14 extra passes are the new tests.
  • The same suites on Python 3.11: head 7 failed, 1,775 passed, 2 skipped. Base 7 failed, 1,761 passed, 2 skipped. The 2 extra failures are #1007's conversation tests, on both trees.
  • The new test file and the launch tests, 15 runs each on Python 3.13 and 3.11: 33 passed every time.
  • ruff check on the whole tree and on the 8 changed Python files: clean. git diff --check: clean.
  • Budget test: counted lines go from 18,693 to 18,866, with no ceiling (D-083). Both structure tests fail the same way at the base and the head.
  • scripts/check_types.py on Python 3.11: the head's NEW list is identical to the base's (15 lines), and RESOLVED is empty. All 15 lines are in group files that do not exist on main, so they come from lower layers.
  • Probes through the real GroupHost with fake engines: the base and head bounds, the conversation peak, the invalid keyword, the stale snapshot and the stop race (base and head on 3.11 and 3.13, and five variants on 3.11).
  • gate_diff: not needed. No gate file is touched.
  • Merge: the head is its base plus one commit. git merge-tree --write-tree of main ef4d7252 and a658d37a is clean. #1039, the next layer at 98372003, contains this head and also merges onto that main cleanly.
  • History: no commit from before the cleanup is reachable from the head, and no commit message or added line in the stack holds a home path.
  • CI at this head: lint passes. The test jobs and types fail for the inherited reasons in the table.

Generated by Claude Code

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.

3 participants