Skip to content

fix: AgentClientTCP#1184

Merged
MistEO merged 2 commits intomainfrom
fix/tcp
Mar 6, 2026
Merged

fix: AgentClientTCP#1184
MistEO merged 2 commits intomainfrom
fix/tcp

Conversation

@MistEO
Copy link
Member

@MistEO MistEO commented Mar 6, 2026

fix #1149

Summary by Sourcery

将数值类型的 AgentClient 标识符视为 TCP 端口,并扩展测试和文档以涵盖新的 TCP 选择行为。

Bug Fixes(错误修复):

  • 确保当 AgentClient 使用数值标识符构造时,通过将该标识符解析为 TCP 端口并相应地初始化 TCP 套接字,从而使用 TCP 模式。

Enhancements(增强):

  • 将 TCP 端口解析重构为可复用的 Transceiver::parse_tcp_port 辅助函数,以在客户端逻辑中共享。
  • 改进 AgentClient 的套接字创建逻辑,在回退到 IPC 或在不兼容的 Windows 系统上使用自动 TCP 回退之前,更优先使用显式的 TCP 标识符。

Documentation(文档):

  • 记录 MaaAgentClientCreateV2 和 Python AgentClient 会将数值标识符视作 127.0.0.1 上的 TCP 端口,并在英文和中文文档中澄清 IPC/TCP 回退行为。

Tests(测试):

  • 扩展代理 TCP 集成测试,使用一个保留的本地端口,同时覆盖显式 TCP 创建和由数值标识符触发的 TCP 模式。
Original summary in English

Summary by Sourcery

Treat numeric AgentClient identifiers as TCP ports and extend tests and documentation to cover the new TCP selection behavior.

Bug Fixes:

  • Ensure AgentClient uses TCP mode when constructed with a numeric identifier by parsing it as a TCP port and initializing a TCP socket accordingly.

Enhancements:

  • Refactor TCP port parsing into a reusable Transceiver::parse_tcp_port helper shared by client logic.
  • Improve AgentClient socket creation logic to prefer explicit TCP identifiers before falling back to IPC or automatic TCP fallback on incompatible Windows systems.

Documentation:

  • Document that MaaAgentClientCreateV2 and the Python AgentClient treat numeric identifiers as TCP ports on 127.0.0.1 and clarify IPC/TCP fallback behavior in both English and Chinese docs.

Tests:

  • Extend agent TCP integration tests to cover both explicit TCP creation and numeric-identifier-triggered TCP mode using a reserved local port.

Copilot AI review requested due to automatic review settings March 6, 2026 06:25
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我发现了 1 个问题,并给出了一些整体性的反馈:

  • 数字端口的解析逻辑现在分散在 parse_port_from_endpoint 和新的 Transceiver::parse_tcp_port 之间;建议把它们合并到一个单独的辅助函数中,以避免验证逻辑出现细微差异以及未来产生偏移。
  • 在 Python 测试中,reserve_tcp_port() 会在 AgentClient 绑定之前先预留并立即释放一个端口;如果这个测试在高负载下变得不稳定,你可能需要改为使用端口 0,并从 agent.identifier 中读取实际端口,而不是事先预留。
给 AI 代理的提示
Please address the comments from this code review:

## Overall Comments
- 数字端口的解析逻辑现在分散在 `parse_port_from_endpoint` 和新的 `Transceiver::parse_tcp_port` 之间;建议把它们合并到一个单独的辅助函数中,以避免验证逻辑出现细微差异以及未来产生偏移。
- 在 Python 测试中,`reserve_tcp_port()` 会在 `AgentClient` 绑定之前先预留并立即释放一个端口;如果这个测试在高负载下变得不稳定,你可能需要改为使用端口 0,并从 `agent.identifier` 中读取实际端口,而不是事先预留。

## Individual Comments

### Comment 1
<location path="source/AgentCommon/Transceiver.cpp" line_range="131" />
<code_context>
 static uint16_t parse_port_from_endpoint(const std::string& endpoint)
</code_context>
<issue_to_address>
**suggestion:** Port parsing and range checking logic is duplicated between `parse_port_from_endpoint` and `parse_tcp_port`; consider factoring out a shared helper.

Both `parse_port_from_endpoint` and `Transceiver::parse_tcp_port` implement nearly identical digit/`strtoul`/range checks. A shared helper (e.g. `static std::optional<uint16_t> parse_port(const char* str, size_t len)`) would remove duplication and keep future changes to port validation consistent.
</issue_to_address>

Sourcery 对开源项目免费——如果你喜欢我们的代码评审,欢迎分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • The numeric-port parsing logic is now spread between parse_port_from_endpoint and the new Transceiver::parse_tcp_port; consider consolidating these into a single helper to avoid subtle differences in validation and future drift.
  • In the Python test reserve_tcp_port() reserves and immediately releases a port before AgentClient binds to it; if this test becomes flaky under load, you may want to switch to using port 0 and reading back the actual port from agent.identifier instead of pre-reserving.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The numeric-port parsing logic is now spread between `parse_port_from_endpoint` and the new `Transceiver::parse_tcp_port`; consider consolidating these into a single helper to avoid subtle differences in validation and future drift.
- In the Python test `reserve_tcp_port()` reserves and immediately releases a port before `AgentClient` binds to it; if this test becomes flaky under load, you may want to switch to using port 0 and reading back the actual port from `agent.identifier` instead of pre-reserving.

## Individual Comments

### Comment 1
<location path="source/AgentCommon/Transceiver.cpp" line_range="131" />
<code_context>
 static uint16_t parse_port_from_endpoint(const std::string& endpoint)
</code_context>
<issue_to_address>
**suggestion:** Port parsing and range checking logic is duplicated between `parse_port_from_endpoint` and `parse_tcp_port`; consider factoring out a shared helper.

Both `parse_port_from_endpoint` and `Transceiver::parse_tcp_port` implement nearly identical digit/`strtoul`/range checks. A shared helper (e.g. `static std::optional<uint16_t> parse_port(const char* str, size_t len)`) would remove duplication and keep future changes to port validation consistent.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #1149 where AgentClient did not automatically recognize numeric string identifiers as TCP port numbers. The fix moves the parse_tcp_port function from AgentServer to the shared Transceiver base class and adds numeric-identifier auto-detection logic to AgentClient's constructor and create_socket method.

Changes:

  • Moved parse_tcp_port from AgentServer::start_up (file-local static) to Transceiver (class static method), making it reusable by both AgentClient and AgentServer.
  • Added numeric-identifier TCP auto-detection in AgentClient's constructor and create_socket, so passing e.g. "12345" automatically uses TCP mode.
  • Updated both Chinese and English documentation, Python binding docstrings, and test to cover the new behavior.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
source/AgentCommon/Transceiver.cpp Added Transceiver::parse_tcp_port as a shared static method with necessary includes
source/include/MaaAgent/Transceiver.h Declared parse_tcp_port as a protected static method
source/MaaAgentClient/Client/AgentClient.cpp Added numeric-identifier TCP detection in constructor and create_socket
source/MaaAgentServer/Server/AgentServer.cpp Removed local parse_tcp_port and unused includes (now uses inherited method)
source/binding/Python/maa/agent_client.py Updated docstrings to document numeric-string TCP behavior
docs/en_us/2.2-IntegratedInterfaceOverview.md Updated MaaAgentClientCreateV2 docs, moved Windows fallback note
test/agent/agent_tcp_main_test.py Refactored test into reusable functions, added numeric-identifier test scenario

@MistEO MistEO merged commit f05ca12 into main Mar 6, 2026
19 checks passed
@MistEO MistEO deleted the fix/tcp branch March 6, 2026 10:37
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.

数字identifier下client没有自动识别使用tcp

2 participants