-
Notifications
You must be signed in to change notification settings - Fork 843
fix(web): default session work_dir to startup directory #1845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bloodycoder
wants to merge
3
commits into
MoonshotAI:main
Choose a base branch
from
bloodycoder:autopilot/issue-1774-mention-workdir
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−3
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| from starlette.testclient import TestClient | ||
|
|
||
| from kimi_cli.web.app import create_app | ||
|
|
||
|
|
||
| def _build_client(monkeypatch, tmp_path: Path, startup_dir: Path) -> TestClient: | ||
| monkeypatch.setenv("KIMI_SHARE_DIR", str(tmp_path / "share")) | ||
| monkeypatch.chdir(startup_dir) | ||
| app = create_app(session_token="test-token") | ||
| client = TestClient(app) | ||
| client.headers.update({"Authorization": "Bearer test-token"}) | ||
| return client | ||
|
|
||
|
|
||
| def test_create_session_without_work_dir_uses_startup_dir(monkeypatch, tmp_path: Path) -> None: | ||
| startup_dir = tmp_path / "startup" | ||
| startup_dir.mkdir() | ||
|
|
||
| with _build_client(monkeypatch, tmp_path, startup_dir) as client: | ||
| # Simulate the existing web call path where request body can be omitted. | ||
| response = client.post("/api/sessions/") | ||
|
|
||
| assert response.status_code == 200 | ||
| payload = response.json() | ||
| assert Path(payload["work_dir"]).resolve() == startup_dir.resolve() | ||
|
|
||
|
|
||
| def test_create_session_fallbacks_to_home_when_startup_dir_invalid(monkeypatch, tmp_path: Path) -> None: | ||
| startup_dir = tmp_path / "startup" | ||
| startup_dir.mkdir() | ||
|
|
||
| with _build_client(monkeypatch, tmp_path, startup_dir) as client: | ||
| client.app.state.startup_dir = str(tmp_path / "missing-startup-dir") | ||
| with patch("kimi_cli.web.api.sessions.logger") as mock_logger: | ||
| response = client.post("/api/sessions/") | ||
|
|
||
| assert response.status_code == 200 | ||
| payload = response.json() | ||
| assert Path(payload["work_dir"]).resolve() == Path.home().resolve() | ||
| assert mock_logger.warning.call_count >= 1 | ||
| assert any( | ||
| call.kwargs.get("used_fallback_home") is True for call in mock_logger.info.call_args_list | ||
| ) | ||
|
|
||
|
|
||
| def test_create_session_explicit_work_dir_kept(monkeypatch, tmp_path: Path) -> None: | ||
| startup_dir = tmp_path / "startup" | ||
| startup_dir.mkdir() | ||
| explicit_dir = tmp_path / "explicit" | ||
| explicit_dir.mkdir() | ||
|
|
||
| with _build_client(monkeypatch, tmp_path, startup_dir) as client: | ||
| response = client.post("/api/sessions/", json={"work_dir": str(explicit_dir)}) | ||
|
|
||
| assert response.status_code == 200 | ||
| payload = response.json() | ||
| assert Path(payload["work_dir"]).resolve() == explicit_dir.resolve() | ||
|
|
||
|
|
||
| def test_session_file_endpoint_resolves_against_created_work_dir(monkeypatch, tmp_path: Path) -> None: | ||
| startup_dir = tmp_path / "startup" | ||
| project_dir = startup_dir / "project" | ||
| project_dir.mkdir(parents=True) | ||
| expected_content = "mention target" | ||
| (project_dir / "note.txt").write_text(expected_content, encoding="utf-8") | ||
|
|
||
| with _build_client(monkeypatch, tmp_path, startup_dir) as client: | ||
| create_response = client.post("/api/sessions/") | ||
| assert create_response.status_code == 200 | ||
| session_id = create_response.json()["session_id"] | ||
|
|
||
| file_response = client.get(f"/api/sessions/{session_id}/files/project/note.txt") | ||
|
|
||
| assert file_response.status_code == 200 | ||
| assert file_response.content.decode("utf-8") == expected_content |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.