Skip to content

feature: improve agent code reusability by extracting shared boilerplate - #12

Draft
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1781554388-agent-reusability
Draft

devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1781554388-agent-reusability

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

Summary

Removes copy-pasted boilerplate that was duplicated across the chat-based agents in src/agentlab/agents/. Behavior is unchanged — this is a pure refactor to centralize reusable logic, validated against the existing agent test suite.

Two sources of duplication were consolidated:

1. The get_action epilogue — GenericAgent and VisualAgent had byte-for-byte identical code to build retry stats + AgentInfo, and to produce the parse-failure fallback dict. Extracted into agents/agent_utils.py:

def busted_retry_ans_dict(max_retry) -> dict:
    return dict(action=None, n_retry=max_retry + 1, busted_retry=1)

def make_agent_info(chat_llm, ans_dict, chat_messages, chat_model_args) -> AgentInfo:
    stats = chat_llm.get_stats()
    stats["n_retry"] = ans_dict["n_retry"]
    stats["busted_retry"] = ans_dict["busted_retry"]
    return AgentInfo(think=ans_dict.get("think"), chat_messages=chat_messages,
                     stats=stats, extra_info={"chat_model_args": asdict(chat_model_args)})

So each agent's get_action collapses from ~15 lines to:

except ParseError:
    ans_dict = busted_retry_ans_dict(self.max_retry)
...
agent_info = make_agent_info(self.chat_llm, ans_dict, chat_messages, self.chat_model_args)

2. prepare() / close() — these 4×-identical methods (return self.chat_model_args.{prepare,close}_server()) moved into a new ChatModelAgentArgs(AgentArgs) base in agents/agent_args.py. GenericAgentArgs, VisualAgentArgs, MostBasicAgentArgs, and VisualWebArenaAgentArgs now inherit it and drop their own copies.

Scope notes

Deliberately left out to avoid behavior changes:

  • ToolUseAgentArgs uses model_args (not chat_model_args), so it keeps its own prepare/close.
  • TapeAgentArgs / DebugAgentArgs have no LLM server and rely on the no-op prepare/close from the base — the new base is opt-in, so they are unaffected. The shared methods were intentionally not added to AgentArgs itself (which would have broken these two).
  • ChatModelAgentArgs declares no new dataclass fields, so subclass field ordering / positional construction is preserved.

Testing

  • New tests/agents/test_agent_utils.py covers make_agent_info, busted_retry_ans_dict, and ChatModelAgentArgs.prepare/close delegation.
  • Full tests/agents/ suite passes (24 passed, 1 skipped) against the MiniWoB server, confirming GenericAgent end-to-end behavior is preserved.
  • black src/ (pinned 25.1.0) and darglint clean on changed files.

Link to Devin session: https://app.devin.ai/sessions/5bea0615dcb648abb0e44224fac5c507
Requested by: @dr-phil


Devin Review

Status Commit
⚪ Not started —

Run Devin Review

Open in Devin Review (Staging)

Extract duplicated get_action epilogue (LLM stats + AgentInfo construction
and the parse-failure fallback dict) into reusable helpers in agent_utils,
and add a ChatModelAgentArgs base providing the shared prepare/close server
delegation. GenericAgent, VisualAgent, MostBasicAgent and VisualWebArenaAgent
now reuse these instead of copy-pasting them.

Co-Authored-By: Phil Bedford <phil.bedford@cognition.ai>
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

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