Skip to content

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.

Ignore train's backups and temp files; clean up after a failed write

Ethan880ce4e0af33
diff --git a/.gitignore b/.gitignore
index 1cda89d..f2afd9b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -57,3 +57,6 @@ Thumbs.db
# Generated by `harvey train` — product-specific, never commit to the public repo
skills/competitive_intel.md
harvey.local.yaml
+# Backups and in-progress writes from `harvey train` hold the same config.
+harvey.local.yaml.*
+harvey.yaml.*
diff --git a/harvey/trainer.py b/harvey/trainer.py
index be6b3db..f0df175 100644
--- a/harvey/trainer.py
+++ b/harvey/trainer.py
@@ -8,6 +8,7 @@ import asyncio
import copy
import logging
import os
+import shutil
import time
from pathlib import Path
from urllib.parse import urljoin, urlparse
@@ -406,9 +407,14 @@ def write_trained_config(learned: dict, path: Path) -> dict:
print(" compliance, sender identity); updated the product, targeting and company.")
tmp = path.with_name(path.name + ".tmp")
- with open(tmp, "w", encoding="utf-8") as f:
- yaml.safe_dump(config, f, default_flow_style=False, sort_keys=False)
- os.replace(tmp, path)
+ try:
+ with open(tmp, "w", encoding="utf-8") as f:
+ yaml.safe_dump(config, f, default_flow_style=False, sort_keys=False)
+ if path.exists():
+ shutil.copymode(path, tmp)
+ os.replace(tmp, path)
+ finally:
+ tmp.unlink(missing_ok=True)
return config
diff --git a/tests/test_trainer_config.py b/tests/test_trainer_config.py
index fa8f84d..75b5620 100644
--- a/tests/test_trainer_config.py
+++ b/tests/test_trainer_config.py
@@ -184,3 +184,18 @@ def test_a_real_sender_with_a_blank_company_is_kept(tmp_path):
out = write_trained_config(LEARNED, path)
assert out["persona"]["email"] == "[email protected]"
assert out["persona"]["company"] == "Acme Roofing"
+
+
+def test_a_failed_write_leaves_the_config_and_no_temp_file(tmp_path):
+ path = tmp_path / "harvey.local.yaml"
+ write(path, TUNED)
+ before = path.read_text()
+ bad = {**LEARNED, "product": {**LEARNED["product"], "name": object()}}
+ try:
+ write_trained_config(bad, path)
+ except yaml.YAMLError:
+ pass
+ else:
+ raise AssertionError("an unrepresentable value should fail the write")
+ assert path.read_text() == before
+ assert list(tmp_path.glob("*.tmp")) == []