From d77c5ffca2890885ad7db72b530b7cc8437733f4 Mon Sep 17 00:00:00 2001 From: Bolin Chen Date: Thu, 12 Nov 2026 06:33:21 +0000 Subject: [PATCH] fix: update a source checkout by git when no project dir is set Run outside its clone with no saved project dir, a source install has no KIROCREW_PROJECT_DIR, so `kirocrew update` treated it as a release install, compared its version with the stable feed and reported "Already on the latest version" while the clone was far behind origin. The update now falls back to the checkout the running package loads from; derive_capability still decides who owns the update. Closes #34953 --- docs/system-specs/modules/cli.md | 4 ++- src/kiro_crew/cli_server.py | 1 + src/kiro_crew/cli_server_update.py | 9 +++-- test/test_cli_server_more_coverage.py | 3 ++ test/test_update_wheel_dispatch.py | 49 +++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/docs/system-specs/modules/cli.md b/docs/system-specs/modules/cli.md index 3200cdc37a5a..78cb248b9079 100644 --- a/docs/system-specs/modules/cli.md +++ b/docs/system-specs/modules/cli.md @@ -1827,7 +1827,9 @@ One flag is a MODE rather than a check and short-circuits before the list above `kirocrew update` pulls the latest source and rebuilds: -1. `git fetch`, then `git reset --hard ` from `KIROCREW_PROJECT_DIR`, where +1. `git fetch`, then `git reset --hard ` from `KIROCREW_PROJECT_DIR`, or, when + it is unset (run outside the clone with no saved project dir), from the checkout + the running package loads from (`_bootstrap._source_checkout_root()`), where `` is `origin/` resolved ONCE right after the fetch (`git rev-parse --verify origin/^{commit}`). Every later judgment — "already up to date", the divergence counts, the interpreter floor below — and the diff --git a/src/kiro_crew/cli_server.py b/src/kiro_crew/cli_server.py index d230d1d4f9e9..aae797e14b59 100644 --- a/src/kiro_crew/cli_server.py +++ b/src/kiro_crew/cli_server.py @@ -834,6 +834,7 @@ def _tail_log_file(path: Path, lines: int, follow: bool) -> None: __all__ = sorted(n for n in set(globals()) | set(_EXPORTS) if not n.startswith("_")) if TYPE_CHECKING: # every owner-defined name in _EXPORTS, for type checkers and IDEs from kiro_crew import update_next_start # noqa: F401 + from kiro_crew._bootstrap import _source_checkout_root # noqa: F401 from kiro_crew.cli_server_peer import ( # noqa: F401 _CLI_LOOPBACK, _PEER_RESOLVE_RETRY_INTERVAL, diff --git a/src/kiro_crew/cli_server_update.py b/src/kiro_crew/cli_server_update.py index 72180c589e32..09951c04b170 100644 --- a/src/kiro_crew/cli_server_update.py +++ b/src/kiro_crew/cli_server_update.py @@ -25,6 +25,7 @@ from pathlib import Path from kiro_crew import dep_sync, platform_compat +from kiro_crew._bootstrap import _source_checkout_root from kiro_crew.beacon import distribution from kiro_crew.cli_server_peer import ( _CLI_LOOPBACK, @@ -250,7 +251,9 @@ def _update(force: bool = False) -> None: sys.exit(1) return - proj = os.environ.get("KIROCREW_PROJECT_DIR", "") + # Run outside the clone with no saved project dir, a source install has no + # KIROCREW_PROJECT_DIR; the checkout its code loads from is still the one to update. + proj = os.environ.get("KIROCREW_PROJECT_DIR", "") or str(_source_checkout_root() or "") # Dispatch on the shared derivation, not on the git probe alone. The probe # answers "is this a working tree", which is NOT the same question as "who # owns updating this install": a container or a desktop bundle pointed at a @@ -836,8 +839,8 @@ def _refresh_agent_config(proj: str) -> None: def _update_wheel(layout) -> None: """Update a wheel/cli.sh install by checking the release feed and re-running the installer. - This is the path taken when KIROCREW_PROJECT_DIR is unset or has no .git — - the standard state for ``curl | sh`` installs where the venv at + This is the path taken when ``derive_capability`` hands the update to Kiro Crew's + own installer — the standard state for ``curl | sh`` installs where the venv at ``~/.kiro/crew-venv`` has no source tree. """ diff --git a/test/test_cli_server_more_coverage.py b/test/test_cli_server_more_coverage.py index d1b43ed7dc7f..ab769ff9c3ce 100644 --- a/test/test_cli_server_more_coverage.py +++ b/test/test_cli_server_more_coverage.py @@ -901,6 +901,8 @@ class TestUpdateWheelDispatch: def test_layout_is_built_from_the_distribution(self, monkeypatch, capsys) -> None: monkeypatch.delenv("KIROCREW_PROJECT_DIR", raising=False) + # The suite runs from a checkout, which `_update` would update by git. + monkeypatch.setattr(cli_server, "_source_checkout_root", lambda: None) # Both bindings: the capability derives WHO owns the update from the # stamp, and cli_server names the layout kind from its own import. monkeypatch.setattr("kiro_crew.platform.update_capability.distribution", lambda: "wheel") @@ -915,6 +917,7 @@ def test_layout_is_built_from_the_distribution(self, monkeypatch, capsys) -> Non def test_unknown_distribution_defaults_to_wheel(self, monkeypatch) -> None: monkeypatch.delenv("KIROCREW_PROJECT_DIR", raising=False) + monkeypatch.setattr(cli_server, "_source_checkout_root", lambda: None) monkeypatch.setattr("kiro_crew.platform.update_capability.distribution", lambda: "") monkeypatch.setattr("kiro_crew.cli_server.distribution", lambda: "") seen: list[InstallLayout] = [] diff --git a/test/test_update_wheel_dispatch.py b/test/test_update_wheel_dispatch.py index eca5b68e00d8..39fa817ef61d 100644 --- a/test/test_update_wheel_dispatch.py +++ b/test/test_update_wheel_dispatch.py @@ -655,6 +655,55 @@ def fake_run(args, **kwargs): git_calls = [c for c in calls if c[0] == "git"] assert any("rev-parse" in c for c in git_calls) + def test_a_source_checkout_with_no_project_dir_takes_the_git_path( + self, monkeypatch, tmp_path + ) -> None: + """Run outside its clone with no saved project dir, a checkout still updates by git. + + With KIROCREW_PROJECT_DIR unset, the checkout the package loads from is the + install root, so the update fetches there instead of checking the release feed. + """ + proj = tmp_path / "project" + proj.mkdir() + _init_repo(proj) + monkeypatch.delenv("KIROCREW_PROJECT_DIR", raising=False) + + import kiro_crew.cli_server as cs + + # raising=False: without the fallback the test still reaches its assertion. + monkeypatch.setattr(cs, "_source_checkout_root", lambda: proj, raising=False) + monkeypatch.setattr( + "kiro_crew.platform.update_capability.running_from_checkout", + lambda root, **kw: root == str(proj), + ) + + def _no_wheel(_layout): # pragma: no cover - must not be called + raise AssertionError("a source checkout must not take the release-feed path") + + monkeypatch.setattr(cs, "_update_wheel", _no_wheel) + calls: list[tuple[list[str], object]] = [] + + def fake_run(args, **kwargs): + calls.append((args, kwargs.get("cwd"))) + result = MagicMock() + result.returncode = 0 + result.stdout = f"{proj}\n" if "--show-toplevel" in args else "main\n" + return result + + monkeypatch.setattr("subprocess.run", fake_run) + monkeypatch.setattr( + "kiro_crew.platform.update_governance.resolve_remote_url", lambda *a, **k: "" + ) + monkeypatch.setattr( + "kiro_crew.platform.update_governance.update_blocked_reason", lambda *a: "" + ) + monkeypatch.setattr( + "kiro_crew.platform.update_governance.repo_exec_config_reason", lambda proj, **_k: "" + ) + + cs._update() + assert any(args[:2] == ["git", "fetch"] and cwd == str(proj) for args, cwd in calls) + class TestUpdateDivergenceGuard: """The git update path refuses a hard reset on a DIVERGED checkout.