Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

backend: route AI CLI prompt tempfiles through .podcli/tmp/

Nika Siradzeec3c9fde2532
diff --git a/backend/services/claude_suggest.py b/backend/services/claude_suggest.py
index 483302a..5e0802d 100644
--- a/backend/services/claude_suggest.py
+++ b/backend/services/claude_suggest.py
@@ -399,12 +399,12 @@ def suggest_with_claude(
exclude_clips=exclude_clips,
)
- # Write prompt to temp file to avoid shell escaping issues
+ # Write prompt to temp file to avoid shell escaping issues.
+ # Goes to .podcli/tmp/ (gitignored) so crashes don't litter the repo root.
project_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..")
- with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, dir=project_dir) as f:
- f.write(prompt)
- prompt_file = f.name
+ from utils.prompt_files import write_prompt_file
+ prompt_file = write_prompt_file(prompt)
if progress_callback:
first_label = _engine_label(candidates[0][1])
diff --git a/backend/services/content_generator.py b/backend/services/content_generator.py
index a0635b9..fea47e7 100644
--- a/backend/services/content_generator.py
+++ b/backend/services/content_generator.py
@@ -114,9 +114,8 @@ HASHTAGS:
project_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..")
- with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, dir=project_dir) as f:
- f.write(prompt)
- prompt_file = f.name
+ from utils.prompt_files import write_prompt_file
+ prompt_file = write_prompt_file(prompt)
try:
for idx, (cli_path, engine) in enumerate(candidates):
diff --git a/backend/services/thumbnail_ai.py b/backend/services/thumbnail_ai.py
index da53168..b49f68b 100644
--- a/backend/services/thumbnail_ai.py
+++ b/backend/services/thumbnail_ai.py
@@ -476,9 +476,8 @@ RULES:
prompt_file = None
try:
- with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, dir=project_dir) as f:
- f.write(prompt)
- prompt_file = f.name
+ from utils.prompt_files import write_prompt_file
+ prompt_file = write_prompt_file(prompt)
for cli_path, engine in candidates:
try:
diff --git a/backend/utils/prompt_files.py b/backend/utils/prompt_files.py
new file mode 100644
index 0000000..fbaca3a
--- /dev/null
+++ b/backend/utils/prompt_files.py
@@ -0,0 +1,69 @@
+"""Safe tempfile helpers for AI CLI prompt handoff.
+
+The Claude/Codex CLIs need a file they can read for the prompt. Writing
+these to the repo root (as several call sites used to do via
+`tempfile.NamedTemporaryFile(dir=project_dir)`) litters the working
+directory with `tmp*.txt` files whenever a process crashes before the
+cleanup block runs — which is how we accumulated 150+ stale files.
+
+Route them through `.podcli/tmp/` instead:
+- Gitignored by the existing `.podcli/` rule, so no risk of accidental
+ commits.
+- Easy to wipe as a single directory on a cold start.
+- Still a real filesystem path the AI CLIs can read.
+"""
+
+from __future__ import annotations
+
+import os
+import tempfile
+
+
+def _tmp_dir() -> str:
+ base = os.path.join(
+ os.path.dirname(os.path.abspath(__file__)),
+ "..",
+ "..",
+ ".podcli",
+ "tmp",
+ )
+ os.makedirs(base, exist_ok=True)
+ return base
+
+
+def write_prompt_file(prompt: str, suffix: str = ".txt") -> str:
+ """Write a prompt to `.podcli/tmp/` and return the absolute path.
+
+ Caller is responsible for deleting the file when done (wrap in
+ try/finally). The `.podcli/tmp/` directory stays around across
+ runs; only the file is per-call.
+ """
+ with tempfile.NamedTemporaryFile(
+ mode="w",
+ suffix=suffix,
+ delete=False,
+ dir=_tmp_dir(),
+ ) as f:
+ f.write(prompt)
+ return f.name
+
+
+def cleanup_stale_tmp_files() -> int:
+ """Remove any leftover prompt files from previous crashed runs.
+
+ Safe to call at process startup. Returns the count removed.
+ """
+ tmp = _tmp_dir()
+ removed = 0
+ try:
+ for name in os.listdir(tmp):
+ full = os.path.join(tmp, name)
+ if os.path.isfile(full):
+ try:
+ os.unlink(full)
+ removed += 1
+ except OSError:
+ pass
+ except FileNotFoundError:
+ pass
+ return removed
diff --git a/tests/test_prompt_files.py b/tests/test_prompt_files.py
new file mode 100644
index 0000000..ef75888
--- /dev/null
+++ b/tests/test_prompt_files.py
@@ -0,0 +1,70 @@
+"""Tests for backend.utils.prompt_files."""
+
+import os
+import sys
+import unittest
+
+ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
+if ROOT not in sys.path:
+ sys.path.insert(0, ROOT)
+
+from backend.utils import prompt_files
+
+
+class PromptFilesTests(unittest.TestCase):
+ def test_write_prompt_file_creates_file_in_podcli_tmp(self):
+ path = prompt_files.write_prompt_file("hello prompt")
+ try:
+ self.assertTrue(os.path.exists(path))
+ self.assertIn(".podcli/tmp", path.replace(os.sep, "/"))
+ with open(path) as f:
+ self.assertEqual(f.read(), "hello prompt")
+ finally:
+ if os.path.exists(path):
+ os.unlink(path)
+
+ def test_write_prompt_file_uses_txt_suffix_by_default(self):
+ path = prompt_files.write_prompt_file("x")
+ try:
+ self.assertTrue(path.endswith(".txt"))
+ finally:
+ os.unlink(path)
+
+ def test_write_prompt_file_honors_custom_suffix(self):
+ path = prompt_files.write_prompt_file("x", suffix=".md")
+ try:
+ self.assertTrue(path.endswith(".md"))
+ finally:
+ os.unlink(path)
+
+ def test_cleanup_stale_tmp_files_removes_leftovers(self):
+ a = prompt_files.write_prompt_file("a")
+ b = prompt_files.write_prompt_file("b")
+ # Both files exist before cleanup
+ self.assertTrue(os.path.exists(a))
+ self.assertTrue(os.path.exists(b))
+
+ removed = prompt_files.cleanup_stale_tmp_files()
+ self.assertGreaterEqual(removed, 2)
+ self.assertFalse(os.path.exists(a))
+ self.assertFalse(os.path.exists(b))
+
+ def test_cleanup_stale_is_idempotent(self):
+ # Ensure the tmp dir exists then wipe it
+ prompt_files.cleanup_stale_tmp_files()
+ # Second call on empty dir should not raise
+ self.assertEqual(prompt_files.cleanup_stale_tmp_files(), 0)
+
+ def test_write_prompt_file_returns_unique_paths(self):
+ a = prompt_files.write_prompt_file("same content")
+ b = prompt_files.write_prompt_file("same content")
+ try:
+ self.assertNotEqual(a, b)
+ finally:
+ for p in (a, b):
+ if os.path.exists(p):
+ os.unlink(p)
+
+
+if __name__ == "__main__":
+ unittest.main()