Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix: prevent save_output from silently overwriting date-stamped files (#755) (#757)

SAI MOHANEESH602dbde2db3f
diff --git a/skills/last30days/scripts/last30days.py b/skills/last30days/scripts/last30days.py
index 0fd5902a..64b6021d 100644
--- a/skills/last30days/scripts/last30days.py
+++ b/skills/last30days/scripts/last30days.py
@@ -136,9 +136,12 @@ def save_output(
extension = "json" if emit == "json" else "html" if emit == "html" else "md"
raw_label = "raw-html" if emit == "html" else "raw"
suffix_part = f"-{suffix}" if suffix else ""
- out_path = path / f"{slug}-{raw_label}{suffix_part}.{extension}"
- if out_path.exists():
- out_path = path / f"{slug}-{raw_label}{suffix_part}-{datetime.now().strftime('%Y-%m-%d')}.{extension}"
+ base = path / f"{slug}-{raw_label}{suffix_part}.{extension}"
+ date_str = datetime.now().strftime('%Y-%m-%d')
+ candidates = [base]
+ candidates.append(path / f"{slug}-{raw_label}{suffix_part}-{date_str}.{extension}")
+ for i in range(1, 100):
+ candidates.append(path / f"{slug}-{raw_label}{suffix_part}-{date_str}-{i}.{extension}")
# Markdown saves keep the complete debug artifact. JSON and HTML preserve
# their requested wire format so file extensions match their content.
if rendered_content is not None:
@@ -147,8 +150,19 @@ def save_output(
content = emit_output(report, emit, synthesis_md=synthesis_md)
else:
content = render.render_full(report)
- out_path.write_text(content, encoding="utf-8")
- return out_path
+ encoded = content.encode("utf-8")
+ for candidate in candidates:
+ try:
+ fd = os.open(candidate, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644)
+ except FileExistsError:
+ continue
+ with os.fdopen(fd, "wb") as f:
+ f.write(encoded)
+ return candidate
+ # Fallback: all 101 candidates existed (extremely unlikely).
+ raise RuntimeError(
+ f"save_output: could not find a unique filename after 101 attempts in {path}"
+ )
def save_rendered_output(rendered_content: str, output_file: str) -> Path:
diff --git a/tests/test_cli_v3.py b/tests/test_cli_v3.py
index 6e3629e9..a2c28711 100644
--- a/tests/test_cli_v3.py
+++ b/tests/test_cli_v3.py
@@ -287,10 +287,12 @@ class CliV3Tests(unittest.TestCase):
def test_save_output_writes_utf8_encoded_markdown(self):
report = self.make_report()
with tempfile.TemporaryDirectory() as tmp:
- with mock.patch("pathlib.Path.write_text", autospec=True, return_value=1) as write_text:
- cli.save_output(report, "md", tmp)
- _, kwargs = write_text.call_args
- self.assertEqual("utf-8", kwargs.get("encoding"))
+ path = cli.save_output(report, "md", tmp)
+ raw = path.read_bytes()
+ content = path.read_text(encoding="utf-8")
+ self.assertIn(report.topic, content)
+ # Verify the raw bytes decode cleanly as UTF-8.
+ self.assertEqual(content, raw.decode("utf-8"))
def test_save_rendered_output_writes_exact_file_path(self):
with tempfile.TemporaryDirectory() as tmp: