JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix: address branch switch review feedback
guozirui17071626e35f3
diff --git a/docs/en/Project-Session-Management.md b/docs/en/Project-Session-Management.mdindex 313a66b744..95bd1e5615 100644--- a/docs/en/Project-Session-Management.md+++ b/docs/en/Project-Session-Management.md@@ -400,7 +400,7 @@ Runs `git init`, refreshes the project snapshot, and wakes Diff watchers.### project.git.switch_branch - Switch branch-Switches to an existing local branch. `require_clean=true` rejects dirty worktrees.+Switches to an existing local branch. `require_clean=true` rejects worktrees with uncommitted changes to tracked files; untracked files do not block the switch.**Request params:** `project_id`, `branch`, optional `require_clean`.diff --git "a/docs/zh/Gateway Websocket\346\216\245\345\217\243\346\226\207\346\241\243.md" "b/docs/zh/Gateway Websocket\346\216\245\345\217\243\346\226\207\346\241\243.md"index 016c999572..ac38ada8c3 100644--- "a/docs/zh/Gateway Websocket\346\216\245\345\217\243\346\226\207\346\241\243.md"+++ "b/docs/zh/Gateway Websocket\346\216\245\345\217\243\346\226\207\346\241\243.md"@@ -3392,7 +3392,7 @@ Gateway 转发 AgentServer。`skilldev.start`、`skilldev.respond` 为**流式**| ------------- | --- | ------ | ---------------------------- || project_id | 是 | string | || branch | 是 | string | |-| require_clean | 否 | bool | true 时工作区脏则 `WORKTREE_DIRTY` |+| require_clean | 否 | bool | true 时已跟踪文件存在未提交修改则 `WORKTREE_DIRTY`;未跟踪文件不阻止切换 |**出参**(`payload`,`ok=true`):`switched`(true)、`previous_branch`、`current_branch`、`status`(Git 状态 payload)diff --git "a/docs/zh/\351\241\271\347\233\256\344\270\216\344\274\232\350\257\235\347\256\241\347\220\206.md" "b/docs/zh/\351\241\271\347\233\256\344\270\216\344\274\232\350\257\235\347\256\241\347\220\206.md"index bb6196dd27..af4625b741 100644--- "a/docs/zh/\351\241\271\347\233\256\344\270\216\344\274\232\350\257\235\347\256\241\347\220\206.md"+++ "b/docs/zh/\351\241\271\347\233\256\344\270\216\344\274\232\350\257\235\347\256\241\347\220\206.md"@@ -538,7 +538,7 @@ Diff 接口中的 `current` 表示当前工作区相对 Git 基线的变更,`las|------|------|------|------|| `project_id` | string | 是 | code 项目 ID || `branch` | string | 是 | 目标本地分支名 |-| `require_clean` | boolean | 否 | 是否要求工作区干净,默认 `false` |+| `require_clean` | boolean | 否 | 是否要求已跟踪文件无未提交修改,默认 `false`;未跟踪文件不阻止切换 |**响应 payload:**diff --git a/jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py b/jiuwenswarm/gateway/channel_manager/web/app_web_handlers.pyindex 972d46d31e..03fa91a276 100644--- a/jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py+++ b/jiuwenswarm/gateway/channel_manager/web/app_web_handlers.py@@ -5379,7 +5379,8 @@ def _register_web_handlers(bind: WebHandlersBindParams) -> None:async def _project_git_switch_branch(ws, req_id, params, session_id):"""切换 Git 分支(设计文档 §4.1.14)。- ``require_clean=true`` 时工作区不干净返回 ``WORKTREE_DIRTY``。成功后+ ``require_clean=true`` 时已跟踪文件存在未提交修改返回+ ``WORKTREE_DIRTY``;未跟踪文件不阻止切换。成功后调 ``mark_dirty`` 触发 /ws/git 立即重算。中间状态返回``GIT_TRANSIENT_STATE``。"""diff --git "a/jiuwenswarm/gateway/docs/Gateway\346\216\245\345\217\243\346\226\207\346\241\243.md" "b/jiuwenswarm/gateway/docs/Gateway\346\216\245\345\217\243\346\226\207\346\241\243.md"index 5975547506..53b0e8cc8a 100644--- "a/jiuwenswarm/gateway/docs/Gateway\346\216\245\345\217\243\346\226\207\346\241\243.md"+++ "b/jiuwenswarm/gateway/docs/Gateway\346\216\245\345\217\243\346\226\207\346\241\243.md"@@ -2974,7 +2974,7 @@ Gateway 转发 AgentServer。`skilldev.start`、`skilldev.respond` 为**流式**| ------------- | --- | ------ | ---------------------------- || project_id | 是 | string | || branch | 是 | string | |-| require_clean | 否 | bool | true 时工作区脏则 `WORKTREE_DIRTY` |+| require_clean | 否 | bool | true 时已跟踪文件存在未提交修改则 `WORKTREE_DIRTY`;未跟踪文件不阻止切换**出参**(`payload`,`ok=true`):`switched`(true)、`previous_branch`、`current_branch`、`status`(Git 状态 payload)diff --git a/jiuwenswarm/server/runtime/session/project_git.py b/jiuwenswarm/server/runtime/session/project_git.pyindex 8e26661ccf..a37fd3f4af 100644--- a/jiuwenswarm/server/runtime/session/project_git.py+++ b/jiuwenswarm/server/runtime/session/project_git.py@@ -1285,6 +1285,9 @@ class ProjectGitService:elif held:msg = "分支被其他 worktree 占用"hint = "请先解散占用该分支的团队,或手动处理对应 worktree 后重试"+ elif "untracked working tree files would be overwritten" in cp_co.stderr:+ msg = "切换分支失败:未跟踪文件会被目标分支覆盖"+ hint = "请移走冲突的未跟踪文件,或执行 git stash -u 后重试"elif "would be overwritten" in cp_co.stderr:msg = "切换分支失败:本地改动阻止切换"hint = "请先提交或 stash 改动后重试"diff --git a/tests/unit_tests/server/test_project_git_safe_directory.py b/tests/unit_tests/server/test_project_git_safe_directory.pyindex 3dfa3708ef..e5803c81a9 100644--- a/tests/unit_tests/server/test_project_git_safe_directory.py+++ b/tests/unit_tests/server/test_project_git_safe_directory.py@@ -397,6 +397,60 @@ def test_switch_branch_allows_untracked_files_when_clean_is_required(monkeypatchassert ["checkout", "feature"] in calls+def test_switch_branch_reports_untracked_checkout_collision(monkeypatch, tmp_path):+ """目标分支会覆盖未跟踪文件时,应给出可执行的处理提示。"""+ from jiuwenswarm.server.runtime.session import project_git++ project_dir = tmp_path / "project"+ project_dir.mkdir()+ project = Project(+ project_id="proj_test",+ name="test",+ project_dir=str(project_dir),+ work_mode="code",+ )++ def fake_run_git(args, *, cwd, timeout=project_git.GIT_COMMAND_TIMEOUT_SEC):+ if args == ["check-ref-format", "--branch", "feature"]:+ return _cp(["git", *args], 0, stdout="feature\n")+ if args == ["show-ref", "--verify", "refs/heads/feature"]:+ return _cp(["git", *args], 0)+ if args == ["checkout", "feature"]:+ return _cp(+ ["git", *args],+ 1,+ stderr=(+ "error: The following untracked working tree files would be "+ "overwritten by checkout:\n conflict.txt\n"+ ),+ )+ return _cp(["git", *args], 0)++ pre_status = project_git.GitRepoStatus(+ is_git=True,+ repo_root=str(project_dir),+ branch="main",+ is_dirty=True,+ untracked=1,+ local_branches=["feature", "main"],+ )+ monkeypatch.setattr(project_git, "_run_git", fake_run_git)+ monkeypatch.setattr(+ project_git,+ "_git_to_repo_status",+ lambda project, *, persist=False: pre_status,+ )++ result = project_git.ProjectGitService.switch_branch(+ project, "feature", require_clean=True,+ )++ assert result.success is False+ assert result.error is not None+ assert "未跟踪文件" in result.error.message+ assert "git stash -u" in result.error.hint++@pytest.mark.parametrize("tracked_state", ["staged", "unstaged", "conflicted"])def test_switch_branch_still_rejects_tracked_changes_when_clean_is_required(monkeypatch,