Files
plane-agentd/agentd/store.py
T
jj.wang 27ea9119a5 LXC deployment: live config for agentd-01, oregon rollback config, win10 helpers and board protocol in the repo
- config/agentd.yaml is now the agentd-01 live config (state /var/lib/plane-agentd, logs /var/log/plane-agentd);
  config/agentd.oregon.yaml keeps the oregon dispatcher settings for rollback (cron/agentd-tick.sh uses it).
- cron/agentd-tick-lxc.sh: the LXC cron entry.
- tools/win10/: win10-ps.sh / win10-put.sh (vault client dir via AGENT_ACCESS_HOME); relative helpers_dir
  resolves against the repo, default tools/win10.
- config/protocols/gwent-board-protocol.md: copy of gwent/design/board-protocol.md used in prompts.
- agentd.log_dir (default state_dir).
- smoke --watch: create the item and wait for the scheduled dispatcher instead of ticking locally.
2026-10-21 11:56:15 +00:00

69 lines
2.4 KiB
Python

"""JSON state store under state/: one file for all projects, an exclusive tick lock, heartbeat and log.
state/agentd-state.json:
{"projects": {PROJECT: {"issues": {issue_uuid: rec}}}, "paused_groups": {group: {...}}}
rec: seq, runs, thread_id, thread_on [terminal, executor], thread_rounds, tags [...], last_message,
seen_comments [...], last_comment_at, retries, retry {at, reason}, rebuild, progress_streak, inflight {...}"""
import contextlib
import fcntl
import json
import os
import time
from pathlib import Path
class Busy(Exception):
pass
class Store:
def __init__(self, state_dir, echo=True, log_dir=None):
self.dir = Path(state_dir)
self.log_dir = Path(log_dir) if log_dir else self.dir
self.echo = echo
self.path = self.dir / "agentd-state.json"
def load(self):
if self.path.exists():
data = json.loads(self.path.read_text(encoding="utf-8"))
else:
data = {}
data.setdefault("projects", {})
data.setdefault("paused_groups", {})
return data
def _atomic_write(self, path, obj):
"""Write temp + fsync + rename, so a reader (or a crash) never sees half a file."""
self.dir.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(".tmp")
with open(tmp, "w", encoding="utf-8") as fh:
fh.write(json.dumps(obj, ensure_ascii=False, indent=1))
fh.flush()
os.fsync(fh.fileno())
os.replace(tmp, path)
def save(self, data):
self._atomic_write(self.path, data)
@contextlib.contextmanager
def lock(self):
self.dir.mkdir(parents=True, exist_ok=True)
with open(self.dir / "agentd.lock", "w") as fh:
try:
fcntl.flock(fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
raise Busy("another tick holds %s" % (self.dir / "agentd.lock")) from None
yield
def heartbeat(self, info):
info = dict(info, ts=time.time(), at=time.strftime("%Y-%m-%dT%H:%M:%S"))
self._atomic_write(self.dir / "heartbeat.json", info)
def log(self, msg):
self.log_dir.mkdir(parents=True, exist_ok=True)
line = time.strftime("%Y-%m-%dT%H:%M:%S") + " " + msg
if self.echo:
print(line, flush=True)
with open(self.log_dir / "agentd.log", "a", encoding="utf-8") as fh:
fh.write(line + "\n")