Delete temp files and use system temp dir - #1
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR shifts diff/refcheck temp artifacts to the system temp directory, improves cleanup of generated working directories/files, and changes the diff pipeline to return in-memory bytes for downstream UI download/preview.
Changes:
- Use
tempfile.gettempdir()and rename temp prefixes fromslate-*tosift-*. - Add stale job reaping and ensure diff workdirs are deleted via
try/finally. - Return
pdf_bytes/tex_bytesfromrun_diff()and update Streamlit download/preview to use bytes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| diff.py | Switch temp root handling, add stale-job cleanup, and return PDF/TEX content as bytes while always deleting job workdirs. |
| app.py | Update refcheck/diff flows to use tempdir + cleanup, and consume diff results as bytes for download/preview. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| from typing import Callable | ||
|
|
||
| ROOT = Path("/tmp") / "slate-diff" | ||
| ROOT = Path(tempfile.gettempdir()) / "sift-diff" |
| return {"pdf_bytes": diff_pdf.read_bytes(), | ||
| "tex_bytes": diff_tex.read_bytes(), | ||
| "strikethrough_used": strikethrough} |
| def _reap_stale_jobs(max_age_secs: int = _JOB_MAX_AGE_SECS) -> None: | ||
| if not ROOT.exists(): | ||
| return | ||
| cutoff = time.time() - max_age_secs | ||
| for job in ROOT.glob("job-*"): | ||
| try: | ||
| if job.is_dir() and job.stat().st_mtime < cutoff: | ||
| shutil.rmtree(job, ignore_errors=True) |
| pdf_bytes = result["pdf_bytes"] | ||
| tex_bytes = result["tex_bytes"] | ||
|
|
||
| st.success(f"diff.pdf ready ({pdf_path.stat().st_size/1024:.0f} KB).") | ||
| st.success(f"diff.pdf ready ({len(pdf_bytes)/1024:.0f} KB).") | ||
|
|
||
| c1, c2 = st.columns(2) | ||
| with open(pdf_path, "rb") as f: | ||
| c1.download_button("Download diff.pdf", f.read(), | ||
| file_name="diff.pdf", mime="application/pdf", | ||
| use_container_width=True) | ||
| with open(tex_path, "rb") as f: | ||
| c2.download_button("Download diff.tex", f.read(), | ||
| file_name="diff.tex", mime="text/x-tex", | ||
| use_container_width=True) | ||
| c1.download_button("Download diff.pdf", pdf_bytes, | ||
| file_name="diff.pdf", mime="application/pdf", | ||
| use_container_width=True) | ||
| c2.download_button("Download diff.tex", tex_bytes, |
|
|
||
| st.markdown("---") | ||
| _preview(pdf_path) | ||
| _preview(pdf_bytes) |
|
I pushed d5c88a2 with the temp-dir and reaper changes; notes on each point below. ROOT temp dir (1) Return contract (2) Reaper mtime (3) Memory from bytes in session_state (4 and 5) |
The reference checker wrote every uploaded PDF to /tmp, and the LaTeX diff left its whole job tree (extracted sources, build dir, output PDFs) under /tmp/slate-diff, and nothing ever deleted either one. On the hosted instance that means disk keeps growing and every visitor's uploaded paper and source stays on the box.
What I changed:
I verified locally: an end to end diff produces a valid PDF and leaves no temp directories behind.