Hub: send in the field (032), coherent speak box (033), stream head band (034), Match at full width with its pile (035, 036), Home without the outer box (037), badge to Match (038), rejects that survive a changed or withdrawn card (039), the release card (040), code marks in the review diff (041), why Deny is off (042), the undo window clock (043), widgets without inner boxes (044), the Artifacts head on one line (045), accepting a red gate's risk (046), one code box (047), done rows greyed and folded (048), one control height (049), menus that stay open (050), radio settings in the Device card (051), bubbles beside the modes (052), agent settings on every face and name (053), pin only by menu (054), WASD in Match (055), the flowing card edge (056), Schedules in Controls (057) and the hub poll cost (059). Skills: the coach leaves for its own repository kevinveenbirkenbach/coach (058; test_install pins its four skills in skills-lock.json and stays red until that repository is published and pinned), hooks and timers start fewer processes (060), council files on hot paths stay bounded and mirror by marker instead of mtime (061), scripts and hooks hand agents less output (062), and block.py builds the commit table and message block in one run (063). Requirement documents 032 to 063 with their implementation amendments. MEDUSA false positives accepted after kvb-think-criticize (36 lines): MM-SCAN-058 (pla inside replace or plan), JB-SCAN-616/641 (the local clip helper), CODEGEN-SCAN-001 (drop-in paths), CGS-SCAN-029 (systemctl argv from constants), CGS-SCAN-040 (runner poll loop), FLA-SCAN-055 (feedback), WM-SCAN-354, AI-BACKDOOR-006, GENAI-SCAN-1270, B604 (runner shell path for passkey-verified commands), TC004, EXA002, EXA006, EXA010 and WEB-SSRF. 🎯 Fix: 92 % 💥 Break: 8 % 💸 Tokens: 89 % ⏱️ Velocity: – 🛡️ Security: ~100 % 🏆 Score: 95 🚦 Gate: ⛔ break 8 % > 5 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
139 lines
4.7 KiB
Python
Executable File
139 lines
4.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Args:
|
|
--sessions: Claude Code's sessions directory, one <pid>.json per session,
|
|
e.g. ~/.claude/sessions.
|
|
--output: the snapshot the Agent Orchestrator reads, e.g.
|
|
~/.local/state/kvb-orchestrator/agent-sessions.json.
|
|
|
|
Returns:
|
|
Runs on the host, because the hub's container sees neither these files
|
|
nor the host's processes: replaces --output with {"written": <epoch>,
|
|
"sessions": {<session id>: {"name", "status", "statusUpdatedAt",
|
|
"running"}}}, running being whether the record's pid still runs the
|
|
process it started, unless it holds the same sessions written less than
|
|
REFRESH seconds ago. The exit code is 0.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import stat
|
|
import tempfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
PROC = Path("/proc")
|
|
KEPT = ("name", "status", "statusUpdatedAt")
|
|
RECORD_BYTES = 65536
|
|
VALUE_CHARS = 200
|
|
REFRESH = 120
|
|
|
|
|
|
def read_record(path: Path) -> object:
|
|
"""Returns:
|
|
The parsed JSON of path, read without following a symlink and only
|
|
when it is a regular file of at most RECORD_BYTES.
|
|
|
|
Raises:
|
|
OSError: path is missing, a symlink, no regular file or too big.
|
|
ValueError, RecursionError: its content is no JSON or nested too deep.
|
|
"""
|
|
handle = os.open(path, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
|
|
with os.fdopen(handle, "rb") as stream:
|
|
info = os.fstat(stream.fileno())
|
|
if not stat.S_ISREG(info.st_mode) or info.st_size > RECORD_BYTES:
|
|
raise OSError(f"{path}: no regular file of at most {RECORD_BYTES} bytes")
|
|
return json.loads(stream.read(RECORD_BYTES + 1).decode("utf-8"))
|
|
|
|
|
|
def kept(record: dict) -> dict:
|
|
return {
|
|
key: record[key]
|
|
for key in KEPT
|
|
if isinstance(record.get(key), str | int | float) and not isinstance(record[key], bool) and len(str(record[key])) <= VALUE_CHARS
|
|
}
|
|
|
|
|
|
def read_sessions(folder: Path) -> dict[str, dict]:
|
|
"""Returns:
|
|
The session records under folder by session id; unreadable files and
|
|
records without a session id are left out.
|
|
"""
|
|
found = {}
|
|
for path in sorted(folder.glob("*.json")):
|
|
try:
|
|
data = read_record(path)
|
|
except (OSError, ValueError, RecursionError):
|
|
continue
|
|
if isinstance(data, dict) and isinstance(data.get("sessionId"), str):
|
|
found[data["sessionId"]] = data
|
|
return found
|
|
|
|
|
|
def running(record: dict, proc: Path) -> bool:
|
|
"""Returns:
|
|
Whether the record's pid still runs the process it started: the
|
|
start tick in /proc/<pid>/stat (field 22) equals procStart, so a
|
|
reused pid counts as gone.
|
|
"""
|
|
pid, start = record.get("pid"), record.get("procStart")
|
|
if isinstance(pid, bool) or not isinstance(pid, int) or not isinstance(start, str):
|
|
return False
|
|
try:
|
|
fields = (proc / str(pid) / "stat").read_text(encoding="utf-8").rpartition(")")[2].split()
|
|
except OSError:
|
|
return False
|
|
return len(fields) > 19 and fields[19] == start
|
|
|
|
|
|
def snapshot(folder: Path, proc: Path, now: float) -> dict:
|
|
return {
|
|
"written": now,
|
|
"sessions": {
|
|
sid: {**kept(record), "running": running(record, proc)}
|
|
for sid, record in read_sessions(folder).items()
|
|
},
|
|
}
|
|
|
|
|
|
def unchanged(output: Path, data: dict) -> bool:
|
|
"""Returns:
|
|
Whether output already holds data's sessions, written less than
|
|
REFRESH seconds before it; the hub counts a snapshot older than five
|
|
minutes as gone.
|
|
"""
|
|
try:
|
|
stored = json.loads(output.read_bytes())
|
|
except (OSError, ValueError, RecursionError):
|
|
return False
|
|
written = stored.get("written") if isinstance(stored, dict) else None
|
|
fresh = isinstance(written, int | float) and not isinstance(written, bool) and 0 <= data["written"] - written < REFRESH
|
|
return fresh and stored.get("sessions") == data["sessions"]
|
|
|
|
|
|
def write(output: Path, data: dict) -> None:
|
|
if unchanged(output, data):
|
|
return
|
|
with tempfile.NamedTemporaryFile("w", encoding="utf-8", dir=output.parent, prefix=f".{output.name}.", delete=False) as handle:
|
|
try:
|
|
json.dump(data, handle)
|
|
except BaseException:
|
|
os.unlink(handle.name)
|
|
raise
|
|
os.replace(handle.name, output)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description="Snapshot Claude Code's sessions for the Agent Orchestrator.")
|
|
parser.add_argument("--sessions", type=Path, required=True)
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
write(args.output, snapshot(args.sessions, PROC, time.time()))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|