Skip to content

Refactor: Consolidate duplicate nextAgentForRole methods #210

@drewdrewthis

Description

@drewdrewthis

Problem

In src/execution/scenario-execution.ts, there are two nearly identical methods:

// Lines 1074-1089
private nextAgentForRole(role: AgentRole): { idx: number; agent: AgentAdapter | null; } {
  for (const agent of this.agents) {
    if (agent.role === role && this.pendingAgentsOnTurn.has(agent) && this.pendingRolesOnTurn.includes(role)) {
      return { idx: this.agents.indexOf(agent), agent };
    }
  }
  return { idx: -1, agent: null };
}

// Lines 1151-1161
private getNextAgentForRole(role: AgentRole): { index: number; agent: AgentAdapter } | null {
  for (let i = 0; i < this.agents.length; i++) {
    const agent = this.agents[i];
    if (agent.role === role && this.pendingAgentsOnTurn.has(agent)) {
      return { index: i, agent };
    }
  }
  return null;
}

These do almost the same thing with slightly different return types. This violates DRY.

Proposed Solution

Consolidate into one method with consistent naming:

private findNextAgentForRole(role: AgentRole): { index: number; agent: AgentAdapter } | null {
  for (let i = 0; i < this.agents.length; i++) {
    const agent = this.agents[i];
    if (agent.role === role && this.pendingAgentsOnTurn.has(agent)) {
      return { index: i, agent };
    }
  }
  return null;
}

Files Affected

  • src/execution/scenario-execution.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions