import json as _json
from pathlib import Path

_ROOT = Path(__file__).parent.parent.absolute()
_STATE_FILE = _ROOT / 'logs' / '.agent_state.json'

def get_state() -> dict:
    if not _STATE_FILE.exists():
        return {}
    try:
        return _json.loads(_STATE_FILE.read_text())
    except Exception:
        return {}

def set_state(state: dict) -> None:
    _STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
    _STATE_FILE.write_text(_json.dumps(state, indent=2, ensure_ascii=False))
