diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6d936ed..9e630db 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -16,9 +16,9 @@ For each reply, `history_for()` presents the current speaker's earlier replies as `assistant` messages and the other speaker's replies as `user` messages. It keeps the original topic and recent messages within a 12,000-character request-history budget. The full conversation remains in the UI and exports. -Requests go to `{base_url}/chat/completions` with the speaker's system prompt, model ID, temperature `0.7`, and `max_tokens` of `2000`. Streaming reads server-sent `data:` events and requests usage reporting. If a stream fails or is empty, the app attempts a regular JSON completion. Transport and empty-response failures receive up to two retries. The same message bubble is reused during fallback, replacing partial stream content with the final reply. +Requests go to `{base_url}/chat/completions` with the speaker's system prompt, model ID, temperature `0.7`, and `max_tokens` of `2000`. Streaming reads server-sent `data:` events, requests usage reporting, and requires a final `[DONE]` event. If a stream fails, is incomplete, or is empty, the app attempts a regular JSON completion. Transport and empty-response failures receive up to two retries. The same message bubble is reused during fallback; partial stream content is cleared before the fallback request and cannot enter later conversation history. -`settings.py` writes JSON through a temporary file and atomic replace. Use an `env:VARIABLE` API-key reference for hosted-provider secrets. `.env`, `sloppy.json`, screenshots, and transcripts are ignored by Git. +`settings.py` validates the shape and field types of loaded settings. It writes JSON through an exclusively created temporary file and atomic replace, and removes its own temporary file if the write fails. On Unix, newly written settings are private to the current user (`0600`). Use an `env:VARIABLE` API-key reference for hosted-provider secrets. `.env`, `sloppy.json`, screenshots, and transcripts are ignored by Git. ## Tests diff --git a/README.md b/README.md index 7d6dc0c..e78886b 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ The sharing menu saves the full conversation or visible screen as SVG in `screen - **No connection in the picker:** Complete the base URL and key in `.env`, or add a saved connection with **Ctrl+M**. - **Missing API key:** Set the environment variable named by a saved `env:NAME` reference before launch. -- **API error:** Check the API root, model ID, server availability, and credentials. A failed stream falls back to a non-streaming request; transport and empty-response failures receive up to two retries. -- **Invalid settings:** Fix malformed `sloppy.json` in the current working directory. Its top-level `models` value must be an object. +- **API error:** Check the API root, model ID, server availability, and credentials. A failed or incomplete stream falls back to a non-streaming request; transport and empty-response failures receive up to two retries. +- **Invalid settings:** Fix malformed `sloppy.json` in the current working directory. `models` and `selected` must be objects; connection fields and selections must be strings. The error identifies the invalid field. See [CONTRIBUTING.md](CONTRIBUTING.md) for code structure and tests. diff --git a/settings.py b/settings.py index eb6df69..17aa4d1 100644 --- a/settings.py +++ b/settings.py @@ -36,13 +36,16 @@ def save_settings(data, path=DEFAULT_PATH): flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL if hasattr(os, 'O_BINARY'): flags |= os.O_BINARY + created = False try: descriptor = os.open(temporary, flags, 0o600) + created = True with os.fdopen(descriptor, 'w', encoding='utf-8') as output: output.write(json.dumps(data, indent=2, ensure_ascii=False) + '\n') os.replace(temporary, path) except BaseException: - temporary.unlink(missing_ok=True) + if created: + temporary.unlink(missing_ok=True) raise diff --git a/test_ai_chat.py b/test_ai_chat.py index cb78191..83895a6 100644 --- a/test_ai_chat.py +++ b/test_ai_chat.py @@ -199,6 +199,18 @@ class ChatTests(unittest.IsolatedAsyncioTestCase): self.assertEqual([m.content for m in app.messages], ['Start here', 'complete reply', 'second reply']) self.assertNotIn('partial secret', json.dumps(requests[2])) + async def test_invalid_settings_picker_reports_error_and_chat_remains_usable(self): + app = await self.make_app(lambda request: httpx.Response(200, text='data: {"choices":[{"delta":{"content":"working"}}]}\n\ndata: [DONE]\n\n')) + with tempfile.TemporaryDirectory() as directory: + app.settings_path = Path(directory) / 'sloppy.json' + app.settings_path.write_text('{"models": {"x": null}}', encoding='utf-8') + async with app.run_test() as pilot: + with patch.object(app, 'notify') as notify: + app.action_model_settings() + self.assertIn('models.x', notify.call_args.args[0]) + await self.wait_finished(app, pilot) + self.assertEqual(app.messages[-1].content, 'working') + async def test_empty_stream_uses_fallback(self): calls = [] def handler(request):