from database.db import DatabaseManager from schemas.models import TaskNegotiationProposal, TaskNegotiationResult from telemetry.event_bus import EventBus class TaskNegotiationEngine: """Evaluates proposed agent tasks to eliminate duplicated work and optimize resource allocation.""" def __init__(self, db: DatabaseManager, event_bus: EventBus): self.db = db self.event_bus = event_bus async def evaluate_proposal(self, proposal: TaskNegotiationProposal) -> TaskNegotiationResult: # Search memory to see if task was already completed existing_memories = await self.db.search_memories(proposal.task_description) is_duplicate = len(existing_memories) > 0 if is_duplicate and proposal.proposed_action != "SKIP_DUPLICATE": res = TaskNegotiationResult( proposal_id=proposal.proposal_id, accepted=True, assigned_agent=proposal.proposing_agent, resolution_notes="Duplicate work detected in memory vault. Task skipped to conserve budget.", ) else: res = TaskNegotiationResult( proposal_id=proposal.proposal_id, accepted=True, assigned_agent=proposal.proposing_agent, resolution_notes="Task negotiation approved. Proceeding with execution.", ) await self.event_bus.emit( "TaskNegotiated", proposal.mission_id, proposal.proposing_agent, {"proposal_id": proposal.proposal_id, "notes": res.resolution_notes} ) return res