Autonomous AI sales agent powered by Claude Code or Codex. Finds prospects, writes cold emails, sends campaigns, handles replies, books meetings. No API keys needed — runs on your Claude or Codex subscriptions.
Windows: UTF-8 for every text file and console output; never run Claude via claude.cmd
Ethan951da174d716
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c1828e8..b5a0d9b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -21,6 +21,15 @@ minor versions can still change behaviour.### Fixed+- **Windows:** text files (harvey.yaml, .env, prompts, skills, trained+ output, tokens, exports) are read and written as UTF-8 instead of the+ locale code page, so `harvey train` no longer crashes on characters like+ "★" and prompts aren't garbled; command output is UTF-8 too, so the+ dashboard's log of `harvey run` can't crash on "⚠". An npm install's+ `claude.cmd` shim is resolved to the native `claude.exe` it wraps and is+ never run through cmd.exe, which would re-parse Harvey's isolation flags;+ if the executable is missing, Harvey says how to fix it. Based on #18 by+ Tariz Shahid.- Replies are read without the email they quote. The model, the stored threadand the answer get the reply without the quoted email, and any line thatrepeats what Harvey sent that person is set aside, so Harvey's own wordsdiff --git a/harvey/agents/analyst.py b/harvey/agents/analyst.pyindex a37d6be..b8f8ada 100644--- a/harvey/agents/analyst.py+++ b/harvey/agents/analyst.py@@ -41,7 +41,7 @@ class Analyst:report_path = data_dir / "analytics.json"try:tmp_path = report_path.with_suffix(".json.tmp")- with open(tmp_path, "w") as f:+ with open(tmp_path, "w", encoding="utf-8") as f:json.dump(report, f, indent=2)tmp_path.replace(report_path)logger.info(f"Analyst: Report written to {report_path}")diff --git a/harvey/brain.py b/harvey/brain.pyindex 57a4e85..18aad30 100644--- a/harvey/brain.py+++ b/harvey/brain.py@@ -675,7 +675,7 @@ class Brain:"""Load a prompt template from the prompts/ directory and fill in variables."""prompt_file = PROMPTS_DIR / f"{prompt_name}.md"try:- template = prompt_file.read_text()+ template = prompt_file.read_text(encoding="utf-8")except FileNotFoundError:logger.warning(f"Prompt file not found: {prompt_file}")return ""@@ -696,7 +696,7 @@ class Brain:"""Load a skill knowledge file from the skills/ directory."""skill_file = SKILLS_DIR / f"{skill_name}.md"try:- return skill_file.read_text()+ return skill_file.read_text(encoding="utf-8")except FileNotFoundError:logger.warning(f"Skill file not found: {skill_file}")return ""diff --git a/harvey/claude_runtime.py b/harvey/claude_runtime.pyindex c0026bb..197eb9e 100644--- a/harvey/claude_runtime.py+++ b/harvey/claude_runtime.py@@ -37,6 +37,7 @@ import osimport reimport shutilimport subprocess+import sysimport tempfileimport threadingimport uuid@@ -77,10 +78,38 @@ class ClaudeRuntimeError(Exception):"""The Claude CLI can't be used safely until the operator acts."""-def find_claude(env: dict | None = None) -> str | None:- """Full path of the ``claude`` executable on the PATH Harvey will use."""+_PLATFORM = sys.platform+++def _locate(env: dict | None) -> tuple[str | None, Path | None]:+ """(what PATH finds, the native exe an npm shim wraps or None)."""path = (env if env is not None else os.environ).get("PATH")- return shutil.which("claude", path=path)+ found = shutil.which("claude", path=path)+ if not found or _PLATFORM != "win32" or not found.lower().endswith((".cmd", ".bat")):+ return found, None+ return found, (Path(found).parent / "node_modules" / "@anthropic-ai" / "claude-code"+ / "bin" / "claude.exe")+++def find_claude(env: dict | None = None) -> str | None:+ """Full path of the ``claude`` executable on the PATH Harvey will use.++ On Windows an npm install puts a ``claude.cmd`` shim on PATH. Running it+ goes through cmd.exe, which re-parses every argument, and the isolation+ flags include empty ones (``--tools ""``) whose meaning changes if one is+ dropped. So the shim is resolved to the native ``claude.exe`` it wraps,+ and refused if that isn't there.+ """+ found, native = _locate(env)+ if native is None:+ return found+ if native.is_file():+ return str(native)+ raise ClaudeRuntimeError(+ f"Found only npm's claude.cmd shim ({found}), which Harvey won't run: "+ "cmd.exe would re-parse the flags that keep each call isolated. Install "+ "the native Claude Code for Windows (https://claude.com/download), or "+ "update the npm package so it includes claude.exe, then try again.")def runtime_env(cwd: str, base: dict | None = None) -> dict:@@ -147,7 +176,10 @@ def _fingerprint(executable: str) -> tuple:def fingerprint(env: dict | None = None) -> tuple:"""Changes when the installed CLI changes (used to re-check a pause)."""- exe = find_claude(env)+ found, native = _locate(env)+ # A refused npm shim is tracked by the exe it should wrap: installing it+ # changes the fingerprint, so a pause is re-checked.+ exe = str(native) if native is not None else foundreturn _fingerprint(exe) if exe else ("", None, None)diff --git a/harvey/cli.py b/harvey/cli.pyindex c46361f..b7b6a58 100644--- a/harvey/cli.py+++ b/harvey/cli.py@@ -712,7 +712,19 @@ def cmd_sending(args):asyncio.run(_run())+def _utf8_stdio(*streams):+ """Write output as UTF-8. On Windows, output going to a file or pipe (the+ dashboard's log of `harvey run`) uses the locale code page, where "⚠" or+ "→" in a message would crash print()."""+ for stream in streams or (sys.stdout, sys.stderr):+ try:+ stream.reconfigure(encoding="utf-8", errors="replace")+ except (AttributeError, ValueError, OSError):+ pass # not a reconfigurable text stream (tests, embedded use)++def main():+ _utf8_stdio()from harvey.paths import PROJECT_ROOTproject_root = str(PROJECT_ROOT)diff --git a/harvey/config.py b/harvey/config.pyindex 5148535..b0bd74b 100644--- a/harvey/config.py+++ b/harvey/config.py@@ -455,7 +455,7 @@ def load_config(config_path: str | None = None) -> HarveyConfig:config_path = _find_config_file()try:- with open(config_path) as f:+ with open(config_path, encoding="utf-8") as f:data = yaml.safe_load(f)except FileNotFoundError:raise ConfigFileNotFoundError(@@ -502,7 +502,7 @@ def load_brain_config(config_path: str | None = None) -> BrainConfig:except ConfigFileNotFoundError:return BrainConfig()try:- with open(config_path) as f:+ with open(config_path, encoding="utf-8") as f:data = yaml.safe_load(f)except FileNotFoundError:return BrainConfig()diff --git a/harvey/dashboard.py b/harvey/dashboard.pyindex 61d001f..5984f73 100644--- a/harvey/dashboard.py+++ b/harvey/dashboard.py@@ -90,7 +90,7 @@ def _read_env_file() -> dict[str, str]:"""Read .env file and return as dict."""env_vars = {}if ENV_FILE.exists():- for line in ENV_FILE.read_text().splitlines():+ for line in ENV_FILE.read_text(encoding="utf-8").splitlines():line = line.strip()if line and not line.startswith("#") and "=" in line:key, _, value = line.partition("=")@@ -162,7 +162,7 @@ def _check_harvey_pid() -> int | None:return _harvey_process.pidif PID_FILE.exists():try:- pid = int(PID_FILE.read_text().strip())+ pid = int(PID_FILE.read_text(encoding="utf-8").strip())os.kill(pid, 0) # Check if process existsreturn pidexcept (ValueError, ProcessLookupError, PermissionError):@@ -241,7 +241,7 @@ async def get_setup_status():config_path = _resolve_config_file()if config_path.exists():try:- with open(config_path) as f:+ with open(config_path, encoding="utf-8") as f:cfg = yaml.safe_load(f)company = cfg.get("persona", {}).get("company", "")product = cfg.get("product", {}).get("name", "")@@ -301,7 +301,7 @@ async def get_setup_status():def _current_provider() -> str:"""Read channels.email.provider from the active config (best-effort)."""try:- with open(_resolve_config_file()) as f:+ with open(_resolve_config_file(), encoding="utf-8") as f:cfg = yaml.safe_load(f) or {}return ((cfg.get("channels") or {}).get("email") or {}).get("provider", "instantly")except Exception:@@ -515,7 +515,7 @@ async def start_harvey():(PROJECT_ROOT / "data").mkdir(parents=True, exist_ok=True)try:- log_handle = open(LOG_FILE, "a")+ log_handle = open(LOG_FILE, "a", encoding="utf-8")try:_harvey_process = subprocess.Popen([sys.executable, "-m", "harvey"],@@ -534,7 +534,7 @@ async def start_harvey():# Write PID filetry:- PID_FILE.write_text(str(_harvey_process.pid))+ PID_FILE.write_text(str(_harvey_process.pid), encoding="utf-8")except OSError as e:logger.warning("Could not write PID file: %s", e)diff --git a/harvey/export.py b/harvey/export.pyindex 1013787..10e63ec 100644--- a/harvey/export.py+++ b/harvey/export.py@@ -117,7 +117,7 @@ async def export_prospects_csv()text = to_csv(rows)if out_path:- with open(out_path, "w", newline="") as f:+ with open(out_path, "w", newline="", encoding="utf-8") as f:f.write(text)logger.info(f"Exported {len(rows)} prospects to {out_path}")return len(rows), textdiff --git a/harvey/integrations/gmail.py b/harvey/integrations/gmail.pyindex f97ccac..0a8e061 100644--- a/harvey/integrations/gmail.py+++ b/harvey/integrations/gmail.py@@ -47,7 +47,7 @@ TIMEOUT = httpx.Timeout(connect=10.0, read=30.0, write=30.0, pool=10.0)def _load_token() -> dict | None:try:if TOKEN_FILE.is_file():- data = json.loads(TOKEN_FILE.read_text())+ data = json.loads(TOKEN_FILE.read_text(encoding="utf-8"))if isinstance(data, dict) and data.get("refresh_token"):return dataexcept (OSError, json.JSONDecodeError) as e:@@ -57,7 +57,7 @@ def _load_token() -> dict | None:def _save_token(data: dict):TOKEN_FILE.parent.mkdir(parents=True, exist_ok=True)- TOKEN_FILE.write_text(json.dumps(data, indent=2))+ TOKEN_FILE.write_text(json.dumps(data, indent=2), encoding="utf-8")try:TOKEN_FILE.chmod(0o600)except OSError:diff --git a/harvey/integrations/linkedin.py b/harvey/integrations/linkedin.pyindex b06bdfa..823f8cb 100644--- a/harvey/integrations/linkedin.py+++ b/harvey/integrations/linkedin.py@@ -53,7 +53,7 @@ class LinkedInAutomation:today = date.today().isoformat()try:if ACTIVITY_PATH.exists():- data = json.loads(ACTIVITY_PATH.read_text())+ data = json.loads(ACTIVITY_PATH.read_text(encoding="utf-8"))if isinstance(data, dict) and data.get("date") == today:return {"date": today,@@ -67,7 +67,7 @@ class LinkedInAutomation:def _save_activity(self):try:ACTIVITY_PATH.parent.mkdir(parents=True, exist_ok=True)- ACTIVITY_PATH.write_text(json.dumps(self._activity))+ ACTIVITY_PATH.write_text(json.dumps(self._activity), encoding="utf-8")except OSError as e:logger.debug(f"Could not save LinkedIn activity log: {e}")@@ -139,7 +139,7 @@ class LinkedInAutomation:# Load cookies if we have them (avoids re-login)if COOKIES_PATH.exists():try:- cookies = json.loads(COOKIES_PATH.read_text())+ cookies = json.loads(COOKIES_PATH.read_text(encoding="utf-8"))if isinstance(cookies, list):await self.context.add_cookies(cookies)logger.info("Loaded saved LinkedIn cookies.")@@ -151,7 +151,7 @@ class LinkedInAutomation:def _save_cookies_sync(self, cookies: list):try:COOKIES_PATH.parent.mkdir(parents=True, exist_ok=True)- COOKIES_PATH.write_text(json.dumps(cookies))+ COOKIES_PATH.write_text(json.dumps(cookies), encoding="utf-8")try:COOKIES_PATH.chmod(0o600) # session cookies are credentialsexcept OSError:diff --git a/harvey/integrations/microsoft.py b/harvey/integrations/microsoft.pyindex c958d82..908de9e 100644--- a/harvey/integrations/microsoft.py+++ b/harvey/integrations/microsoft.py@@ -77,7 +77,7 @@ class GraphError(RuntimeError):def _load_token() -> dict | None:try:if TOKEN_FILE.is_file():- data = json.loads(TOKEN_FILE.read_text())+ data = json.loads(TOKEN_FILE.read_text(encoding="utf-8"))if isinstance(data, dict) and data.get("refresh_token"):return dataexcept (OSError, json.JSONDecodeError) as e:@@ -87,7 +87,7 @@ def _load_token() -> dict | None:def _save_token(data: dict):TOKEN_FILE.parent.mkdir(parents=True, exist_ok=True)- TOKEN_FILE.write_text(json.dumps(data, indent=2))+ TOKEN_FILE.write_text(json.dumps(data, indent=2), encoding="utf-8")try:TOKEN_FILE.chmod(0o600)except OSError:diff --git a/harvey/integrations/quota.py b/harvey/integrations/quota.pyindex 558e023..5632dcc 100644--- a/harvey/integrations/quota.py+++ b/harvey/integrations/quota.py@@ -56,7 +56,7 @@ def get_oauth_token() -> Optional[str]:creds_file = claude_config_dir() / ".credentials.json"try:if creds_file.is_file():- token = _token_from_json_blob(creds_file.read_text())+ token = _token_from_json_blob(creds_file.read_text(encoding="utf-8"))if token:return tokenexcept OSError as e:@@ -67,7 +67,7 @@ def get_oauth_token() -> Optional[str]:if default_creds != creds_file:try:if default_creds.is_file():- token = _token_from_json_blob(default_creds.read_text())+ token = _token_from_json_blob(default_creds.read_text(encoding="utf-8"))if token:return tokenexcept OSError:diff --git a/harvey/main.py b/harvey/main.pyindex cf1b0dd..f2bda25 100644--- a/harvey/main.py+++ b/harvey/main.py@@ -418,7 +418,7 @@ def _needs_setup() -> bool:return Truetry:- with open(config_file) as f:+ with open(config_file, encoding="utf-8") as f:import yamlconfig = yaml.safe_load(f)if not isinstance(config, dict):diff --git a/harvey/setup.py b/harvey/setup.pyindex b6cd0a6..497b417 100644--- a/harvey/setup.py+++ b/harvey/setup.py@@ -596,7 +596,7 @@ class SetupWizard:"""Write the .env file, preserving any existing variables we didn't touch."""existing: dict[str, str] = {}if ENV_FILE.exists():- for line in ENV_FILE.read_text().splitlines():+ for line in ENV_FILE.read_text(encoding="utf-8").splitlines():line = line.strip()if line and not line.startswith("#") and "=" in line:key, _, value = line.partition("=")@@ -610,7 +610,7 @@ class SetupWizard:merged[key] = valuelines = [f"{key}={value}" for key, value in merged.items()]- ENV_FILE.write_text("\n".join(lines) + "\n")+ ENV_FILE.write_text("\n".join(lines) + "\n", encoding="utf-8")try:ENV_FILE.chmod(0o600) # credentials — owner read/write onlyexcept OSError:@@ -624,13 +624,13 @@ class SetupWizard:if self.config:self.config["brain"] = brain_section(self.brain_config)if self.config and not CONFIG_FILE.exists():- with open(CONFIG_FILE, "w") as f:+ with open(CONFIG_FILE, "w", encoding="utf-8") as f:yaml.dump(self.config, f, default_flow_style=False, sort_keys=False)logger.info(f"Wrote {CONFIG_FILE}")elif self.config:# Config already exists (from trainer), update behavior settings onlytry:- with open(CONFIG_FILE) as f:+ with open(CONFIG_FILE, encoding="utf-8") as f:existing = yaml.safe_load(f) or {}existing["usage"] = self.config.get("usage", existing.get("usage", {}))existing["brain"] = self.config["brain"]@@ -645,7 +645,7 @@ class SetupWizard:existing["channels"]["email"]["max_daily_sends"] = (self.config["channels"]["email"].get("max_daily_sends", 50))- with open(CONFIG_FILE, "w") as f:+ with open(CONFIG_FILE, "w", encoding="utf-8") as f:yaml.dump(existing, f, default_flow_style=False, sort_keys=False)except Exception:passdiff --git a/harvey/trainer.py b/harvey/trainer.pyindex c61203e..890a920 100644--- a/harvey/trainer.py+++ b/harvey/trainer.py@@ -782,7 +782,7 @@ Do NOT pursue prospects who:"""skills_path = PROJECT_ROOT / "skills" / "product_knowledge.md"- skills_path.write_text(knowledge)+ skills_path.write_text(knowledge, encoding="utf-8")print(f" Generated: skills/product_knowledge.md")async def _generate_battle_cards(self, product_info: dict, competitive_intel: dict):@@ -830,7 +830,7 @@ What prospects do instead of buying a solution like ours:"""skills_path = PROJECT_ROOT / "skills" / "competitive_intel.md"- skills_path.write_text(cards)+ skills_path.write_text(cards, encoding="utf-8")print(f" Generated: skills/competitive_intel.md")def _build_config(diff --git a/tests/test_windows_support.py b/tests/test_windows_support.pynew file mode 100644index 0000000..5207072--- /dev/null+++ b/tests/test_windows_support.py@@ -0,0 +1,140 @@+"""Windows: text files are UTF-8 whatever the locale, and Claude is never run+through npm's claude.cmd shim.++Based on community PR #18 by Tariz Shahid.+"""++import ast+import io+from pathlib import Path++import pytest++from harvey import claude_runtime+from harvey.claude_runtime import find_claude as real_find_claude # conftest stubs it++HARVEY = Path(__file__).resolve().parent.parent / "harvey"+++def _text_io_without_encoding(tree):+ """open()/read_text()/write_text() calls in text mode with no encoding=."""+ for node in ast.walk(tree):+ if not isinstance(node, ast.Call):+ continue+ func = node.func+ name = func.attr if isinstance(func, ast.Attribute) else getattr(func, "id", "")+ if name not in ("open", "read_text", "write_text"):+ continue+ if isinstance(func, ast.Attribute) and isinstance(func.value, ast.Name) and \+ func.value.id in ("os", "webbrowser", "aiosqlite", "sqlite3"):+ continue+ if any(k.arg == "encoding" for k in node.keywords):+ continue+ mode = next((a.value for a in node.args[1:2] if isinstance(a, ast.Constant)), None)+ mode = next((k.value.value for k in node.keywords+ if k.arg == "mode" and isinstance(k.value, ast.Constant)), mode)+ if name == "open" and isinstance(mode, str) and "b" in mode:+ continue+ yield node.lineno+++def test_every_text_file_is_read_and_written_as_utf8():+ # Windows defaults to the locale code page (cp1252): harvey train+ # crashed on "★" and prompts came out garbled.+ offenders = [+ f"{path.relative_to(HARVEY.parent)}:{line}"+ for path in sorted(HARVEY.rglob("*.py"))+ for line in _text_io_without_encoding(ast.parse(path.read_text(encoding="utf-8")))+ ]+ assert offenders == []+++def test_config_and_prompts_with_non_ascii_text_load(tmp_path, monkeypatch):+ from harvey.brain import Brain+ import harvey.brain as brain_mod+ from harvey.config import load_config++ import yaml+ data = yaml.safe_load((HARVEY.parent / "harvey.yaml").read_text(encoding="utf-8"))+ data["persona"]["company"] = "Acme — Studio ★"+ config = tmp_path / "harvey.yaml"+ config.write_text(yaml.safe_dump(data, allow_unicode=True), encoding="utf-8")+ loaded = load_config(str(config))+ assert loaded.persona.company == "Acme — Studio ★"++ (tmp_path / "x.md").write_text("Rules ★ — {{name}}", encoding="utf-8")+ monkeypatch.setattr(brain_mod, "PROMPTS_DIR", tmp_path)+ assert Brain.load_prompt(object.__new__(Brain), "x", name="Zoë") == "Rules ★ — Zoë"+++# ── Finding the Claude CLI on Windows ──++def _npm_install(tmp_path, with_native=True):+ shim = tmp_path / "npm" / "claude.CMD"+ shim.parent.mkdir()+ shim.write_text("@echo off\n", encoding="utf-8")+ native = shim.parent / "node_modules" / "@anthropic-ai" / "claude-code" / "bin" / "claude.exe"+ if with_native:+ native.parent.mkdir(parents=True)+ native.write_bytes(b"MZ")+ return shim, native+++def test_windows_npm_shim_resolves_to_the_native_executable(tmp_path, monkeypatch):+ shim, native = _npm_install(tmp_path)+ monkeypatch.setattr(claude_runtime, "_PLATFORM", "win32")+ monkeypatch.setattr(claude_runtime.shutil, "which", lambda *a, **k: str(shim))+ assert real_find_claude() == str(native)+++def test_windows_npm_shim_without_the_executable_is_refused(tmp_path, monkeypatch):+ # Never through cmd.exe: it re-parses arguments, and the isolation flags+ # include empty ones (--tools "") whose meaning would change if dropped.+ shim, _ = _npm_install(tmp_path, with_native=False)+ monkeypatch.setattr(claude_runtime, "_PLATFORM", "win32")+ monkeypatch.setattr(claude_runtime.shutil, "which", lambda *a, **k: str(shim))+ with pytest.raises(claude_runtime.ClaudeRuntimeError) as err:+ real_find_claude()+ assert "claude.cmd" in str(err.value).lower() and "install" in str(err.value).lower()++[email protected]("platform, found", [+ ("win32", r"C:\Users\me\.local\bin\claude.exe"),+ ("darwin", "/usr/local/bin/claude"),+ ("linux", "/home/me/.local/bin/claude"),+])+def test_other_installs_are_used_as_found(platform, found, monkeypatch):+ monkeypatch.setattr(claude_runtime, "_PLATFORM", platform)+ monkeypatch.setattr(claude_runtime.shutil, "which", lambda *a, **k: found)+ assert real_find_claude() == found+++def test_not_installed_is_still_none(monkeypatch):+ monkeypatch.setattr(claude_runtime, "_PLATFORM", "win32")+ monkeypatch.setattr(claude_runtime.shutil, "which", lambda *a, **k: None)+ assert real_find_claude() is None+++# ── Console output ──++def test_cli_output_never_crashes_on_a_legacy_code_page():+ # The dashboard runs `harvey run` with output going to a log file, which+ # Windows opens in cp1252: "⚠" or "→" in a message would crash print().+ from harvey.cli import _utf8_stdio+ raw = io.BytesIO()+ stream = io.TextIOWrapper(raw, encoding="cp1252")+ _utf8_stdio(stream)+ print("⚠ sending paused → resume", file=stream)+ stream.flush()+ assert raw.getvalue().decode("utf-8").startswith("⚠ sending paused → resume")+++def test_installing_the_missing_executable_changes_the_fingerprint(tmp_path, monkeypatch):+ # A paused brain re-checks the CLI only when this changes.+ shim, native = _npm_install(tmp_path, with_native=False)+ monkeypatch.setattr(claude_runtime, "_PLATFORM", "win32")+ monkeypatch.setattr(claude_runtime.shutil, "which", lambda *a, **k: str(shim))+ before = claude_runtime.fingerprint()+ native.parent.mkdir(parents=True)+ native.write_bytes(b"MZ")+ assert claude_runtime.fingerprint() != before