Skip to content

Personal AI, On Personal Devices

Python

fix(cli): write init config and memory files as UTF-8 (#821)

Mohammed Alkindib196cdfdc969
diff --git a/src/openjarvis/cli/init_cmd.py b/src/openjarvis/cli/init_cmd.py
index e9c53d47..35cb51bc 100644
--- a/src/openjarvis/cli/init_cmd.py
+++ b/src/openjarvis/cli/init_cmd.py
@@ -494,9 +494,9 @@ sources = ["gcalendar"]
sources = ["hackernews", "news_rss"]
"""
target = DEFAULT_CONFIG_PATH
- existing = target.read_text()
- target.write_text(existing + digest_section)
- toml_content = target.read_text()
+ existing = target.read_text(encoding="utf-8")
+ target.write_text(existing + digest_section, encoding="utf-8")
+ toml_content = target.read_text(encoding="utf-8")
console.print(
"[green]Morning Digest config added.[/green] "
"Run [bold]jarvis connect gdrive[/bold] to connect "
@@ -509,16 +509,17 @@ sources = ["hackernews", "news_rss"]
soul_path = DEFAULT_CONFIG_DIR / "SOUL.md"
if not soul_path.exists():
soul_path.write_text(
- "# Agent Persona\n\nYou are Jarvis, a helpful personal AI assistant.\n"
+ "# Agent Persona\n\nYou are Jarvis, a helpful personal AI assistant.\n",
+ encoding="utf-8",
)
memory_path = DEFAULT_CONFIG_DIR / "MEMORY.md"
if not memory_path.exists():
- memory_path.write_text("# Agent Memory\n\n")
+ memory_path.write_text("# Agent Memory\n\n", encoding="utf-8")
user_path = DEFAULT_CONFIG_DIR / "USER.md"
if not user_path.exists():
- user_path.write_text("# User Profile\n\n")
+ user_path.write_text("# User Profile\n\n", encoding="utf-8")
skills_dir = DEFAULT_CONFIG_DIR / "skills"
skills_dir.mkdir(exist_ok=True)
diff --git a/tests/cli/test_init_digest.py b/tests/cli/test_init_digest.py
new file mode 100644
index 00000000..4a47db7d
--- /dev/null
+++ b/tests/cli/test_init_digest.py
@@ -0,0 +1,68 @@
+"""Tests for ``jarvis init --digest`` config writing."""
+
+from __future__ import annotations
+
+from pathlib import Path
+from unittest import mock
+
+from click.testing import CliRunner
+
+from openjarvis.cli import cli
+
+_NO_DL = "--no-download"
+_READ_TEXT = Path.read_text
+_WRITE_TEXT = Path.write_text
+
+
+def _cp1252_read_text(
+ path: Path,
+ encoding: str | None = None,
+ errors: str | None = None,
+) -> str:
+ return _READ_TEXT(path, encoding=encoding or "cp1252", errors=errors)
+
+
+def _cp1252_write_text(
+ path: Path,
+ data: str,
+ encoding: str | None = None,
+ errors: str | None = None,
+ newline: str | None = None,
+) -> int:
+ return _WRITE_TEXT(
+ path,
+ data,
+ encoding=encoding or "cp1252",
+ errors=errors,
+ newline=newline,
+ )
+
+
+class TestInitDigestEncoding:
+ def test_init_digest_round_trips_utf8(self, tmp_path: Path) -> None:
+ """The digest section's U+2500 rule must survive on any locale (#769).
+
+ Without explicit ``encoding=`` the write uses the locale codec, which
+ is cp1252 on a default Windows install and cannot encode U+2500, so
+ ``jarvis init --digest`` crashed with UnicodeEncodeError there.
+ """
+ config_dir = tmp_path / ".openjarvis"
+ config_path = config_dir / "config.toml"
+ with (
+ mock.patch("openjarvis.cli.init_cmd.DEFAULT_CONFIG_DIR", config_dir),
+ mock.patch("openjarvis.cli.init_cmd.DEFAULT_CONFIG_PATH", config_path),
+ mock.patch("openjarvis.cli.init_cmd.PrivacyScanner"),
+ # Emulate Windows' non-UTF default locale on every platform. Any
+ # init-path read/write that omits encoding now uses cp1252 and the
+ # box-drawing rule raises exactly the original Unicode error.
+ mock.patch.object(Path, "read_text", _cp1252_read_text),
+ mock.patch.object(Path, "write_text", _cp1252_write_text),
+ ):
+ result = CliRunner().invoke(
+ cli,
+ ["init", "--engine", "ollama", "--digest", _NO_DL],
+ )
+ assert result.exit_code == 0, result.output
+ content = config_path.read_text(encoding="utf-8")
+ assert "[digest]" in content
+ assert "─" in content