Skip to content

refactor: carve out misplaced helpers from monolith TUs - #6

Merged
shaohaozhi286 merged 4 commits into
masterfrom
archon/thread-60a47498
May 6, 2026
Merged

shaohaozhi286 merged 4 commits into
masterfrom
archon/thread-60a47498

Conversation

@shaohaozhi286

Copy link
Copy Markdown
Collaborator

Architectural Sweep

Focus: 我当前项目有几个文件和函数快成屎山了, 请按照业务逻辑解耦

Assessment

仓库内有 8 处屎山点按代价滚雪球速度排序:src/agent_loop.cpp (642 行 + 层级倒挂 import web/*)、src/web/server.cpp (2406 行 + 反向 include desktop/)、src/commands/builtin_commands.cpp (1082 行 / 16 命令挤一起)、src/config/config.cpp (956 行 / 7 个无关职责)、src/desktop/main.cpp (961 行)、前端 ChatView.jsx (16-prop God 组件)、Sidebar.jsx、sessionTranscript.js。横向问题:层级倒挂 2 处、Rule-of-Three 失败 5 处、通用 utils 被困在使用方文件、SessionEvent 协议没收口。

本轮选取 4 个高收益、低风险、相互独立的纯重组,零行为变更:把通用 path 工具搬到 utils/paths、/model 命令拆出独立 TU 跟随同目录约定、enable_desktop_dpi_awareness 抽离到 Win32 专属 TU、OriginParts/parse_origin 从 web/server.cpp 提取并加单测。剩余的 agent_loop 拆分、web↔core 反向依赖反转、config.cpp read_json_field<T> 模板化、前端 God 组件拆分等留作后续 P0 战役。

Changes

  • src/utils/paths.{hpp,cpp} ← src/config/config.cpp (commit 5b9d2cc): 把 expand_path 和 get_project_dirs_up_to_home 从 config.cpp 迁出。它们是通用路径工具,与 config schema 无关,放在 config 里既膨胀该 TU 也让其它模块被迫 include config.hpp 才能用路径工具。
  • src/commands/model_command.{hpp,cpp} ← src/commands/builtin_commands.cpp (commit ca257e6): 抽出 113 行 /model 派发逻辑到独立 TU,跟随同目录已有约定 (history_command.cpp / init_command.cpp / memory_command.cpp / proxy_command.cpp / websearch_command.cpp)。/model 是该约定的最后一个 outlier。
  • src/desktop/dpi_win.{hpp,cpp} ← src/desktop/main.cpp (commit 2e1e65d): 把 65 行 SetProcessDpiAwarenessContext → SetProcessDpiAwareness → SetProcessDPIAware Win32 fallback 链抽离到专属 TU,镜像同目录已有的 tray_icon_win.cpp / notifications_win.cpp / folder_picker_win.cpp 模式,把 <Windows.h> 的 LoadLibraryW/GetProcAddress 包袱关进单独的 Windows-only 编译单元。
  • src/web/origin.{hpp,cpp} + tests/web/origin_test.cpp ← src/web/server.cpp (commit 558d490): 把 OriginParts struct 和 parse_origin 从 anon namespace 提到独立 TU。这是纯字符串解析,与 Crow / daemon state 无任何耦合,提取后让 server.cpp -50 LOC 同时解锁单测覆盖 (well-formed http/https / 缺 scheme / IPv6 brackets / 默认端口填充 / 非法输入)。

Validation

  • Type check passes
  • Lint passes
  • Tests pass
  • Each change preserves existing behavior

tmoonlight added 4 commits May 6, 2026 21:06
These are general path utilities, not config schema concerns. Lifting them
out of src/config/config.cpp puts them next to the rest of the path helpers
in src/utils/paths.{hpp,cpp}. Pure code reorganization — no behavior change.

Callers updated: src/skills/skill_init.cpp and src/daemon/worker.cpp now
include <utils/paths.hpp> directly instead of relying on transitive include
through config.hpp.
Mirrors the convention already used by history_command, init_command,
memory_command, proxy_command, websearch_command. The /model dispatch
(picker, in-memory swap, --cwd / --default persistence) was the only
sibling still inlined in builtin_commands.cpp. Pure code reorganization,
no behavior change.

builtin_commands.cpp keeps the static 'trim' helper since cmd_page_step
still depends on it. The src/commands tree is glob-picked-up by the root
CMakeLists, so no CMake change is needed.
Self-contained 65-line Win32 fallback chain
(SetProcessDpiAwarenessContext -> SetProcessDpiAwareness -> SetProcessDPIAware)
with zero state shared with the rest of desktop/main.cpp. Mirrors the
existing tray_icon_win / notifications_win / folder_picker_win convention
(_WIN32-guarded TU under namespace acecode::desktop).

Pure code reorganization, no behavior change.
Lift the pure-string-manipulation OriginParts struct, parse_origin, and
split_host_port out of server.cpp's anon namespace into a dedicated
web/origin.{hpp,cpp} TU under namespace acecode::web. server.cpp shrinks
by ~50 LOC and the parsers become unit-testable.

ascii_lower stays in server.cpp — it has another caller (is_loopback_host)
that is not part of this carve-out. origin.cpp keeps its own private
ascii_lower.

Adds tests/web/origin_test.cpp covering well-formed http/https, missing
scheme, IPv6 bracketed hosts, bare-IPv6 multi-colon heuristic, default
port fill, and lowercase normalization. Picked up automatically by the
tests/*_test.cpp glob — no CMake change.

@shaohaozhi286 shaohaozhi286 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

ok

@shaohaozhi286
shaohaozhi286 merged commit fa9711a into master May 6, 2026
6 of 7 checks passed
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.

2 participants