chore(fleet): sync operator memory, hatch menu dialogs, and watchdog alerts
This commit is contained in:
+41
-1
@@ -59,8 +59,40 @@ def register_session(parent, session_id, title=None, prompt=None):
|
||||
return entry
|
||||
|
||||
|
||||
def get_active_sessions(parent=None):
|
||||
DEFAULT_TTL_SECONDS = 3600 # 1 hour idle TTL
|
||||
|
||||
|
||||
def prune_stale_sessions(ttl_seconds=DEFAULT_TTL_SECONDS):
|
||||
"""Archive active sessions whose last activity exceeds ttl_seconds."""
|
||||
data = load_sessions()
|
||||
now = datetime.now(timezone.utc)
|
||||
changed = False
|
||||
for sid, s in data.items():
|
||||
if s.get("status") == "active":
|
||||
last_act = s.get("last_activity_at") or s.get("spawned_at")
|
||||
if last_act:
|
||||
try:
|
||||
dt = datetime.fromisoformat(last_act.replace("Z", "+00:00"))
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
if (now - dt).total_seconds() >= ttl_seconds:
|
||||
s["status"] = "archived"
|
||||
s["archived_at"] = utcnow()
|
||||
s["archive_reason"] = f"idle_ttl_exceeded_{ttl_seconds}s"
|
||||
changed = True
|
||||
except Exception:
|
||||
pass
|
||||
if changed:
|
||||
save_sessions(data)
|
||||
|
||||
|
||||
def get_active_sessions(parent=None, auto_prune=True, ttl_seconds=DEFAULT_TTL_SECONDS):
|
||||
"""Retrieve all active subagent sessions, optionally filtered by parent."""
|
||||
if auto_prune:
|
||||
try:
|
||||
prune_stale_sessions(ttl_seconds=ttl_seconds)
|
||||
except Exception:
|
||||
pass
|
||||
data = load_sessions()
|
||||
results = []
|
||||
for s in data.values():
|
||||
@@ -88,6 +120,14 @@ def complete_session(session_id, note=None):
|
||||
return update_session(session_id, **kwargs)
|
||||
|
||||
|
||||
def archive_session(session_id, reason=None):
|
||||
"""Mark a subagent session archived."""
|
||||
kwargs = {"status": "archived", "archived_at": utcnow()}
|
||||
if reason:
|
||||
kwargs["archive_reason"] = reason
|
||||
return update_session(session_id, **kwargs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "list":
|
||||
print(json.dumps(load_sessions(), indent=2))
|
||||
|
||||
Reference in New Issue
Block a user