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
This commit is contained in:
Alan Weinstock 2026-06-01 15:37:35 -07:00
parent 92799b05f9
commit 15779e3114
2 changed files with 6 additions and 4 deletions

View file

@ -7,6 +7,7 @@ Session cookies are stored in a JSON file and refreshed automatically when stale
from __future__ import annotations from __future__ import annotations
import json import json
import os
import subprocess import subprocess
import sys import sys
import time 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: 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: if session_file is None:
session_file = Path(get_settings().reddit_session_file) session_file = Path(get_settings().reddit_session_file)
session_file.parent.mkdir(parents=True, exist_ok=True) 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( result = subprocess.run(
["xvfb-run", "--auto-servernum", sys.executable, str(_POST_SCRIPT), "--login"], ["xvfb-run", "--auto-servernum", sys.executable, str(_POST_SCRIPT), "--login"],
cwd=str(_POST_SCRIPT.parent), cwd=str(_POST_SCRIPT.parent),
env={**os.environ, "REDDIT_SESSION_FILE": str(session_file)},
timeout=120, timeout=120,
) )
if result.returncode != 0: if result.returncode != 0:

View file

@ -196,8 +196,8 @@ case "$cmd" in
info " Scheduler ${sched} scheduled" info " Scheduler ${sched} scheduled"
fi fi
# Session age # Session age — check the canonical per-account file, not the legacy session.json
SESSION_FILE="${DATA_DIR}/session.json" SESSION_FILE="${DATA_DIR}/sessions/alan_reddit.json"
if [[ -f "$SESSION_FILE" ]]; then 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 "?") 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 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)" warn " Session ${age_h} old (stale — run: ./manage.sh login)"
fi fi
else else
warn " Session no session.json — run: ./manage.sh login" warn " Session no session file — run: ./manage.sh login"
fi fi
echo "" echo ""