-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(web): preserve manual session titles #2102
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd6035a
fix(web): allow state-only session edits while busy
wbxl2000 f4a7afe
docs(changelog): update release notes for busy session edits
wbxl2000 50cf65f
Merge remote-tracking branch 'origin/main' into web-session-busy-toast
wbxl2000 0a3924d
fix(web): preserve manual session titles
wbxl2000 91bf820
Merge remote-tracking branch 'origin/main' into web-session-busy-toast
wbxl2000 07f039d
fix(web): guard rename save against onBlur/Enter re-entry
wbxl2000 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
Some comments aren't visible on the classic Files Changed page.
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
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
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,116 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from types import SimpleNamespace | ||
| from typing import TYPE_CHECKING, cast | ||
| from uuid import UUID | ||
|
|
||
| import pytest | ||
| from kaos.path import KaosPath | ||
|
|
||
| from kimi_cli.session import Session | ||
| from kimi_cli.session_state import load_session_state, save_session_state | ||
| from kimi_cli.web.api import sessions as sessions_api | ||
| from kimi_cli.web.models import GenerateTitleRequest | ||
|
|
||
| if TYPE_CHECKING: | ||
| from kimi_cli.web.runner.process import KimiCLIRunner | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def isolated_share_dir(monkeypatch, tmp_path: Path) -> Path: | ||
| share_dir = tmp_path / "share" | ||
| share_dir.mkdir() | ||
|
|
||
| def _get_share_dir() -> Path: | ||
| share_dir.mkdir(parents=True, exist_ok=True) | ||
| return share_dir | ||
|
|
||
| monkeypatch.setattr("kimi_cli.share.get_share_dir", _get_share_dir) | ||
| monkeypatch.setattr("kimi_cli.metadata.get_share_dir", _get_share_dir) | ||
| return share_dir | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def work_dir(tmp_path: Path) -> KaosPath: | ||
| path = tmp_path / "work" | ||
| path.mkdir() | ||
| return KaosPath.unsafe_from_local_path(path) | ||
|
|
||
|
|
||
| class _FakeOAuthManager: | ||
| def __init__(self, _config: object) -> None: | ||
| pass | ||
|
|
||
| async def ensure_fresh(self) -> None: | ||
| return None | ||
|
|
||
|
|
||
| class _FakeRunner: | ||
| """Stand-in for ``KimiCLIRunner`` for tests that bypass FastAPI dependency injection.""" | ||
|
|
||
| def get_session(self, _session_id: UUID) -> None: | ||
| return None | ||
|
|
||
|
|
||
| class _FakeLLM: | ||
| chat_provider = object() | ||
|
|
||
|
|
||
| class _FakeMessage: | ||
| def __init__(self, text: str) -> None: | ||
| self._text = text | ||
|
|
||
| def extract_text(self) -> str: | ||
| return self._text | ||
|
|
||
|
|
||
| class _FakeResult: | ||
| def __init__(self, text: str) -> None: | ||
| self.message = _FakeMessage(text) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_generate_title_preserves_concurrent_manual_title( | ||
| isolated_share_dir: Path, | ||
| work_dir: KaosPath, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| session = await Session.create(work_dir) | ||
|
|
||
| config = SimpleNamespace( | ||
| default_model="test-model", | ||
| models={"test-model": SimpleNamespace(provider="test-provider")}, | ||
| providers={"test-provider": object()}, | ||
| ) | ||
|
|
||
| monkeypatch.setattr("kimi_cli.config.load_config", lambda: config) | ||
| monkeypatch.setattr( | ||
| "kimi_cli.llm.create_llm", | ||
| lambda provider_config, model_config, oauth=None: _FakeLLM(), | ||
| ) | ||
| monkeypatch.setattr("kimi_cli.auth.oauth.OAuthManager", _FakeOAuthManager) | ||
|
|
||
| async def fake_generate(*, chat_provider, system_prompt, tools, history): | ||
| state = load_session_state(session.dir) | ||
| state.custom_title = "Manual Title" | ||
| state.title_generated = True | ||
| save_session_state(state, session.dir) | ||
| return _FakeResult("AI Title") | ||
|
|
||
| monkeypatch.setattr("kosong.generate", fake_generate) | ||
|
|
||
| response = await sessions_api.generate_session_title( | ||
| UUID(session.id), | ||
| GenerateTitleRequest( | ||
| user_message="debug the flaky web session rename issue", | ||
| assistant_response="I'll inspect the session state writes.", | ||
| ), | ||
| runner=cast("KimiCLIRunner", _FakeRunner()), | ||
| ) | ||
|
|
||
| state = load_session_state(session.dir) | ||
| assert response.title == "Manual Title" | ||
| assert state.custom_title == "Manual Title" | ||
| assert state.title_generated is True | ||
| assert state.title_generate_attempts == 0 |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This endpoint is now callable while a worker is busy, but it still does an unlocked read-modify-write (
load_session_statethensave_session_state). Ingenerate_session_title, if the worker persists session changes (e.g., todos/approval/plan fields viaSession.save_state) afterfreshis loaded but before this save executes, this handler can write an older snapshot and silently roll back those worker updates. Because the busy check was removed for this route in this commit, this introduces a real state-loss race under concurrent activity.Useful? React with 👍 / 👎.