From 15779e311459d0c67b814d003ac1d102aabcdc95 Mon Sep 17 00:00:00 2001 From: Alan Weinstock Date: Mon, 1 Jun 2026 15:37:35 -0700 Subject: [PATCH] fix(session): pass REDDIT_SESSION_FILE env to post.py subprocess; fix status check path refresh_session() was spawning post.py --login without the REDDIT_SESSION_FILE env var, so Playwright wrote the refreshed session to the legacy session.json default instead of sessions/alan_reddit.json. The poster then read a stale file and failed with 'Playwright re-login failed'. - Add os import; pass env={...os.environ, REDDIT_SESSION_FILE: str(session_file)} to subprocess.run so post.py writes to the correct per-account path - manage.sh status now checks sessions/alan_reddit.json (canonical) not session.json - Replace session.json with a symlink to sessions/alan_reddit.json for legacy compat --- app/services/reddit/session.py | 4 +++- manage.sh | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/services/reddit/session.py b/app/services/reddit/session.py index 12b6adf..640f084 100644 --- a/app/services/reddit/session.py +++ b/app/services/reddit/session.py @@ -7,6 +7,7 @@ Session cookies are stored in a JSON file and refreshed automatically when stale from __future__ import annotations import json +import os import subprocess import sys import time @@ -33,7 +34,7 @@ def session_is_valid(session_file: Path | None = None) -> bool: def refresh_session(session_file: Path | None = None) -> None: - """Re-login via Playwright (xvfb-run) and overwrite session.json.""" + """Re-login via Playwright (xvfb-run) and overwrite the session file.""" if session_file is None: session_file = Path(get_settings().reddit_session_file) session_file.parent.mkdir(parents=True, exist_ok=True) @@ -41,6 +42,7 @@ def refresh_session(session_file: Path | None = None) -> None: result = subprocess.run( ["xvfb-run", "--auto-servernum", sys.executable, str(_POST_SCRIPT), "--login"], cwd=str(_POST_SCRIPT.parent), + env={**os.environ, "REDDIT_SESSION_FILE": str(session_file)}, timeout=120, ) if result.returncode != 0: diff --git a/manage.sh b/manage.sh index 8619e4f..dc703f6 100755 --- a/manage.sh +++ b/manage.sh @@ -196,8 +196,8 @@ case "$cmd" in info " Scheduler ${sched} scheduled" fi - # Session age - SESSION_FILE="${DATA_DIR}/session.json" + # Session age — check the canonical per-account file, not the legacy session.json + SESSION_FILE="${DATA_DIR}/sessions/alan_reddit.json" if [[ -f "$SESSION_FILE" ]]; then age_h=$(python3 -c "import time,os; print(f\"{(time.time()-os.path.getmtime('$SESSION_FILE'))/3600:.1f}h\")" 2>/dev/null || echo "?") if python3 -c "import time,os; exit(0 if (time.time()-os.path.getmtime('$SESSION_FILE'))/3600 < 12 else 1)" 2>/dev/null; then @@ -206,7 +206,7 @@ case "$cmd" in warn " Session ${age_h} old (stale — run: ./manage.sh login)" fi else - warn " Session no session.json — run: ./manage.sh login" + warn " Session no session file — run: ./manage.sh login" fi echo ""