Skip to content

Commit 3997cac

Browse files
authored
fix(config): prevent config save from overwriting unrelated settings (#614)
Signed-off-by: Richard Chien <stdrc@outlook.com>
1 parent f4e7f2a commit 3997cac

9 files changed

Lines changed: 51 additions & 51 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Only write entries that are worth mentioning to users.
1212
## Unreleased
1313

1414
- Shell: Use `/model` to toggle thinking mode instead of Tab key
15-
- Config: Add `default_thinking` config option (auto-migrated from metadata)
15+
- Config: Add `default_thinking` config option (need to run `/model` to select thinking mode after upgrade)
1616
- LLM: Add `always_thinking` capability for models that always use thinking mode
1717
- CLI: Rename `--command`/`-c` to `--prompt`/`-p`, keep `--command`/`-c` as alias, remove `--query`/`-q`
1818

docs/en/release-notes/breaking-changes.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ This page documents breaking changes in Kimi CLI releases and provides migration
44

55
## Unreleased
66

7+
### Thinking mode setting migration change
8+
9+
After upgrading from `0.76`, the thinking mode setting is no longer automatically preserved. The previous `thinking` state stored in `~/.kimi/kimi.json` is no longer used; instead, thinking mode is now managed via the `default_thinking` configuration option in `~/.kimi/config.toml`, but values are not automatically migrated from legacy `metadata`.
10+
11+
- **Affected**: Users who previously had thinking mode enabled
12+
- **Migration**: Reconfigure thinking mode after upgrading:
13+
- Use the `/model` command to select model and set thinking mode (interactive)
14+
- Or manually add to `~/.kimi/config.toml`:
15+
16+
```toml
17+
default_thinking = true # Set to true if you want thinking mode enabled by default
18+
```
19+
720
### `--query` option removed
821

922
The `--query` (`-q`) option has been removed. Use `--prompt` as the primary option, with `--command` as an alias.

docs/en/release-notes/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This page documents the changes in each Kimi CLI release.
55
## Unreleased
66

77
- Shell: Use `/model` to toggle thinking mode instead of Tab key
8-
- Config: Add `default_thinking` config option (auto-migrated from metadata)
8+
- Config: Add `default_thinking` config option (need to run `/model` to select thinking mode after upgrade)
99
- LLM: Add `always_thinking` capability for models that always use thinking mode
1010
- CLI: Rename `--command`/`-c` to `--prompt`/`-p`, keep `--command`/`-c` as alias, remove `--query`/`-q`
1111

docs/zh/release-notes/breaking-changes.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55
## 未发布
66

7+
### Thinking 模式设置迁移调整
8+
9+
`0.76` 升级后,Thinking 模式设置不再自动保留。此前保存在 `~/.kimi/kimi.json` 中的 `thinking` 状态不再使用,改为通过 `~/.kimi/config.toml` 中的 `default_thinking` 配置项管理,但不会自动从旧版 `metadata` 迁移。
10+
11+
- **受影响**:此前启用 Thinking 模式的用户
12+
- **迁移**:升级后需重新设置 Thinking 模式:
13+
- 使用 `/model` 命令选择模型时设置 Thinking 模式(交互式)
14+
- 或手动在 `~/.kimi/config.toml` 中添加:
15+
16+
```toml
17+
default_thinking = true # 如需默认启用 Thinking 模式
18+
```
19+
720
### `--query` 选项移除
821

922
`--query`(`-q`)已移除,改用 `--prompt` 作为主推参数,`--command` 作为别名。

docs/zh/release-notes/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## 未发布
66

77
- Shell:使用 `/model` 命令切换 Thinking 模式,取代 Tab 键
8-
- Config:添加 `default_thinking` 配置项(从 metadata 自动迁移
8+
- Config:添加 `default_thinking` 配置项(升级后需运行 `/model` 选择 Thinking 模式
99
- LLM:为始终使用 Thinking 模式的模型添加 `always_thinking` 能力
1010
- CLI:将 `--command`/`-c` 重命名为 `--prompt`/`-p`,保留 `--command`/`-c` 作为别名,移除 `--query`/`-q`
1111

src/kimi_cli/acp/server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from kimi_cli.acp.tools import replace_tools
1616
from kimi_cli.acp.types import ACPContentBlock, MCPServer
1717
from kimi_cli.app import KimiCLI
18-
from kimi_cli.config import LLMModel, save_config
18+
from kimi_cli.config import LLMModel, load_config, save_config
1919
from kimi_cli.constant import NAME, VERSION
2020
from kimi_cli.llm import create_llm, derive_model_capabilities
2121
from kimi_cli.session import Session
@@ -256,7 +256,10 @@ async def set_session_model(self, model_id: str, session_id: str, **kwargs: Any)
256256
config.default_model = model_id_conv.model_key
257257
config.default_thinking = model_id_conv.thinking
258258
assert config.is_from_default_location, "`kimi acp` must use the default config location"
259-
save_config(cli_instance.soul.runtime.config)
259+
config_for_save = load_config()
260+
config_for_save.default_model = model_id_conv.model_key
261+
config_for_save.default_thinking = model_id_conv.thinking
262+
save_config(config_for_save)
260263

261264
async def authenticate(self, method_id: str, **kwargs: Any) -> acp.AuthenticateResponse | None:
262265
raise NotImplementedError

src/kimi_cli/config.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -201,50 +201,9 @@ def load_config(config_file: Path | None = None) -> Config:
201201
except ValidationError as e:
202202
raise ConfigError(f"Invalid configuration file: {e}") from e
203203
config.is_from_default_location = is_default_config_file
204-
205-
# Migrate thinking setting from metadata to config (one-time migration)
206-
if is_default_config_file:
207-
_migrate_thinking_from_metadata(config, config_file)
208-
209204
return config
210205

211206

212-
def _migrate_thinking_from_metadata(config: Config, config_file: Path) -> None:
213-
"""Migrate thinking setting from metadata.json to config.toml (one-time migration)."""
214-
from kimi_cli.share import get_share_dir
215-
216-
metadata_file = get_share_dir() / "kimi.json"
217-
if not metadata_file.exists():
218-
return
219-
220-
try:
221-
with open(metadata_file, encoding="utf-8") as f:
222-
metadata = json.load(f)
223-
except (json.JSONDecodeError, OSError):
224-
return
225-
226-
try:
227-
if "thinking" not in metadata:
228-
return
229-
thinking_value = metadata.pop("thinking")
230-
except (KeyError, TypeError):
231-
return
232-
233-
logger.info(
234-
"Migrating thinking setting from metadata to config: {thinking}",
235-
thinking=thinking_value,
236-
)
237-
238-
config.default_thinking = bool(thinking_value)
239-
save_config(config, config_file)
240-
241-
try:
242-
with open(metadata_file, "w", encoding="utf-8") as f:
243-
json.dump(metadata, f, indent=2, ensure_ascii=False)
244-
except OSError as e:
245-
logger.warning("Failed to update metadata after migration: {error}", error=e)
246-
247-
248207
def load_config_from_string(config_string: str) -> Config:
249208
"""
250209
Load configuration from a TOML or JSON string.

src/kimi_cli/platforms.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import aiohttp
66
from pydantic import BaseModel
77

8-
from kimi_cli.config import Config, LLMModel, save_config
8+
from kimi_cli.config import Config, LLMModel, load_config, save_config
99
from kimi_cli.llm import ModelCapability
1010
from kimi_cli.utils.aiohttp import new_client_session
1111
from kimi_cli.utils.logging import logger
@@ -113,6 +113,7 @@ async def refresh_managed_models(config: Config) -> bool:
113113
return False
114114

115115
changed = False
116+
updates: list[tuple[str, str, list[ModelInfo]]] = []
116117
for provider_key, provider in managed_providers.items():
117118
platform_id = parse_managed_provider_key(provider_key)
118119
if not platform_id:
@@ -132,11 +133,18 @@ async def refresh_managed_models(config: Config) -> bool:
132133
)
133134
continue
134135

136+
updates.append((provider_key, platform_id, models))
135137
if _apply_models(config, provider_key, platform_id, models):
136138
changed = True
137139

138140
if changed:
139-
save_config(config)
141+
config_for_save = load_config()
142+
save_changed = False
143+
for provider_key, platform_id, models in updates:
144+
if _apply_models(config_for_save, provider_key, platform_id, models):
145+
save_changed = True
146+
if save_changed:
147+
save_config(config_for_save)
140148
return changed
141149

142150

src/kimi_cli/ui/shell/slash.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from prompt_toolkit.shortcuts.choice_input import ChoiceInput
77

88
from kimi_cli.cli import Reload
9-
from kimi_cli.config import save_config
9+
from kimi_cli.config import load_config, save_config
10+
from kimi_cli.exception import ConfigError
1011
from kimi_cli.platforms import get_platform_name_for_provider, refresh_managed_models
1112
from kimi_cli.session import Session
1213
from kimi_cli.soul.kimisoul import KimiSoul
@@ -244,8 +245,11 @@ async def model(app: Shell, args: str):
244245
config.default_model = selected_model_name
245246
config.default_thinking = new_thinking
246247
try:
247-
save_config(config)
248-
except OSError as exc:
248+
config_for_save = load_config()
249+
config_for_save.default_model = selected_model_name
250+
config_for_save.default_thinking = new_thinking
251+
save_config(config_for_save)
252+
except (ConfigError, OSError) as exc:
249253
config.default_model = prev_model
250254
config.default_thinking = prev_thinking
251255
console.print(f"[red]Failed to save config: {exc}[/red]")

0 commit comments

Comments
 (0)