Skip to content
Python

fix(sessions): remove copied PDFs when a conversation is deleted

Ayush Tiwari986e2652a425
diff --git a/coworker/server/manager.py b/coworker/server/manager.py
index 4d311829..50853107 100644
--- a/coworker/server/manager.py
+++ b/coworker/server/manager.py
@@ -594,7 +594,11 @@ class SessionManager:
record = self.session_store.load(session_id)
team = (record.team if record else {}) or {}
lead = str(team.get("lead_session") or "")
- if team.get("role") == "worker" and self._SESSION_ID_RE.fullmatch(lead) and lead not in {".", "..", session_id}:
+ if (
+ team.get("role") == "worker"
+ and self._SESSION_ID_RE.fullmatch(lead)
+ and lead not in {".", "..", session_id}
+ ):
d = self.scratch_base() / lead / "workers" / session_id
d.mkdir(parents=True, exist_ok=True)
return str(d.resolve())
@@ -7564,6 +7568,27 @@ class SessionManager:
shutil.rmtree(resolved)
except OSError:
pass # a stale/foreign path must not fail the delete
+ # Project-backed sessions keep their primary folder, but PDF originals copied
+ # into conversation scratch must go away when the conversation is deleted.
+ if (
+ ok
+ and record
+ and self._SESSION_ID_RE.fullmatch(session_id)
+ and session_id not in {".", ".."}
+ ):
+ team = record.team or {}
+ lead = str(team.get("lead_session") or "")
+ scratch = self.scratch_base().resolve()
+ if (
+ team.get("role") == "worker"
+ and self._SESSION_ID_RE.fullmatch(lead)
+ and lead not in {".", "..", session_id}
+ ):
+ attachment_dir = scratch / lead / "workers" / session_id / "attachments"
+ else:
+ attachment_dir = scratch / session_id / "attachments"
+ if attachment_dir.is_dir() and attachment_dir.resolve() == attachment_dir:
+ shutil.rmtree(attachment_dir)
return {"ok": ok, "session_id": session_id}
# -- provider proxy ---------------------------------------------------------