Files
MISF-Planner-Download/src/debug_log.py
T

61 lines
1.9 KiB
Python

"""Shared NDJSON debug logging (MISF_DEBUG=1 only). No secrets/PII."""
from __future__ import annotations
import json
import os
import time
import uuid
from config import PROJECT_ROOT
_DEBUG_LOG = PROJECT_ROOT / "debug.log"
_AGENT_SESSION_LOG = PROJECT_ROOT / "debug-6d994d.log"
_AGENT_SESSION_ID = "6d994d"
def write_agent_session_debug(
hypothesis_id: str, location: str, message: str, data: dict
) -> None:
"""NDJSON debug log for Cursor agent debug sessions (MISF_DEBUG=1 only)."""
# region agent log
if os.environ.get("MISF_DEBUG", "").strip() not in ("1", "true", "yes"):
return
try:
ts_ms = int(time.time() * 1000)
payload = {
"sessionId": _AGENT_SESSION_ID,
"runId": os.environ.get("MISF_RUN_ID", "run1"),
"hypothesisId": hypothesis_id,
"id": f"log_{ts_ms}_{uuid.uuid4().hex[:8]}",
"timestamp": ts_ms,
"location": location,
"message": message,
"data": data,
}
with _AGENT_SESSION_LOG.open("a", encoding="utf-8") as f:
f.write(json.dumps(payload, ensure_ascii=False) + "\n")
except Exception:
pass
# endregion
def write_debug(hypothesis_id: str, location: str, message: str, data: dict) -> None:
if os.environ.get("MISF_DEBUG", "").strip() not in ("1", "true", "yes"):
return
try:
ts_ms = int(time.time() * 1000)
payload = {
"sessionId": os.environ.get("MISF_SESSION_ID", "local"),
"runId": os.environ.get("MISF_RUN_ID", "run1"),
"hypothesisId": hypothesis_id,
"id": f"log_{ts_ms}_{uuid.uuid4().hex[:8]}",
"timestamp": ts_ms,
"location": location,
"message": message,
"data": data,
}
with _DEBUG_LOG.open("a", encoding="utf-8") as f:
f.write(json.dumps(payload, ensure_ascii=False) + "\n")
except Exception:
pass