-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add cost-optimized, hierarchical, and auction assignment strategies #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
43f19fb
feat: add cost-optimized, hierarchical, and auction assignment strate…
Aureliolo 542d9e2
refactor: address pre-PR review findings (22 items from 9 agents)
Aureliolo 31709c6
fix: resolve mypy union-attr error in LLM decomposition retry logic
Aureliolo 64d4e51
fix: address PR review findings from local agents and external reviewers
Aureliolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """Strategy registry and factory for task assignment. | ||
|
|
||
| ``STRATEGY_MAP`` provides five pre-built strategies as an | ||
| immutable mapping. ``build_strategy_map`` is the preferred | ||
| factory when a ``HierarchyResolver`` is available (adds the | ||
| sixth strategy) or a custom ``AgentTaskScorer`` is needed. | ||
| """ | ||
|
|
||
| from types import MappingProxyType | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ai_company.engine.assignment.strategies import ( | ||
| STRATEGY_NAME_AUCTION, | ||
| STRATEGY_NAME_COST_OPTIMIZED, | ||
| STRATEGY_NAME_HIERARCHICAL, | ||
| STRATEGY_NAME_LOAD_BALANCED, | ||
| STRATEGY_NAME_MANUAL, | ||
| STRATEGY_NAME_ROLE_BASED, | ||
| AuctionAssignmentStrategy, | ||
| CostOptimizedAssignmentStrategy, | ||
| HierarchicalAssignmentStrategy, | ||
| LoadBalancedAssignmentStrategy, | ||
| ManualAssignmentStrategy, | ||
| RoleBasedAssignmentStrategy, | ||
| ) | ||
| from ai_company.engine.routing.scorer import AgentTaskScorer | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ai_company.communication.delegation.hierarchy import ( | ||
| HierarchyResolver, | ||
| ) | ||
| from ai_company.engine.assignment.protocol import ( | ||
| TaskAssignmentStrategy, | ||
| ) | ||
|
|
||
| _DEFAULT_SCORER = AgentTaskScorer() | ||
|
|
||
| # Excludes HierarchicalAssignmentStrategy — it requires a | ||
| # HierarchyResolver at construction. Use | ||
| # build_strategy_map(hierarchy=...) to get a complete map | ||
| # with all six strategies. | ||
| STRATEGY_MAP: MappingProxyType[str, TaskAssignmentStrategy] = MappingProxyType( | ||
| { | ||
| STRATEGY_NAME_MANUAL: ManualAssignmentStrategy(), | ||
| STRATEGY_NAME_ROLE_BASED: RoleBasedAssignmentStrategy( | ||
| _DEFAULT_SCORER, | ||
| ), | ||
| STRATEGY_NAME_LOAD_BALANCED: LoadBalancedAssignmentStrategy( | ||
| _DEFAULT_SCORER, | ||
| ), | ||
| STRATEGY_NAME_COST_OPTIMIZED: CostOptimizedAssignmentStrategy( | ||
| _DEFAULT_SCORER, | ||
| ), | ||
| STRATEGY_NAME_AUCTION: AuctionAssignmentStrategy( | ||
| _DEFAULT_SCORER, | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def build_strategy_map( | ||
| *, | ||
| hierarchy: HierarchyResolver | None = None, | ||
| scorer: AgentTaskScorer | None = None, | ||
| ) -> MappingProxyType[str, TaskAssignmentStrategy]: | ||
| """Build a strategy map, optionally including hierarchical. | ||
|
|
||
| When ``hierarchy`` is provided, includes the | ||
| ``HierarchicalAssignmentStrategy`` in the returned map. | ||
| Otherwise, returns the same five strategies as the static | ||
| ``STRATEGY_MAP``. | ||
|
|
||
| Args: | ||
| hierarchy: Optional hierarchy resolver for the | ||
| hierarchical strategy. | ||
| scorer: Optional custom scorer. Defaults to a new | ||
| ``AgentTaskScorer``. | ||
|
|
||
| Returns: | ||
| Immutable mapping of strategy names to instances. | ||
| """ | ||
| effective_scorer = scorer if scorer is not None else AgentTaskScorer() | ||
|
|
||
| strategies: dict[str, TaskAssignmentStrategy] = { | ||
| STRATEGY_NAME_MANUAL: ManualAssignmentStrategy(), | ||
| STRATEGY_NAME_ROLE_BASED: RoleBasedAssignmentStrategy( | ||
| effective_scorer, | ||
| ), | ||
| STRATEGY_NAME_LOAD_BALANCED: LoadBalancedAssignmentStrategy( | ||
| effective_scorer, | ||
| ), | ||
| STRATEGY_NAME_COST_OPTIMIZED: CostOptimizedAssignmentStrategy( | ||
| effective_scorer, | ||
| ), | ||
| STRATEGY_NAME_AUCTION: AuctionAssignmentStrategy( | ||
| effective_scorer, | ||
| ), | ||
| } | ||
|
|
||
| if hierarchy is not None: | ||
| strategies[STRATEGY_NAME_HIERARCHICAL] = HierarchicalAssignmentStrategy( | ||
| effective_scorer, | ||
| hierarchy, | ||
| ) | ||
|
|
||
| return MappingProxyType(strategies) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation creates a new
AgentTaskScorerinstance every timebuild_strategy_mapis called without ascorerargument. This is inconsistent with the module-levelSTRATEGY_MAP, which uses a single shared_DEFAULT_SCORERinstance.This can lead to surprising behavior where strategies returned by
build_strategy_map()(with no arguments) use a different scorer instance than those in the globalSTRATEGY_MAP. Reusing the module-level_DEFAULT_SCORERwould ensure consistency and avoid creating unnecessary objects.I suggest changing this line to use the shared scorer. You'll also want to update the docstring on line 77 to reflect this change from "Defaults to a new
AgentTaskScorer" to something like "Defaults to the shared module-levelAgentTaskScorerinstance".