Generic platform points for the `diff-viewer` and `commits` plugins (ai-agent-plugins): a `conversation.tab` slot (Thread | Changes N) with setTab/openTab/closeTab/reply bridges; stackable + pullOut tool cards and match.hasReport; `pathShims` (a git PATH shim for every harness) with out-of-band reports (lab: /api/plugin-reports, worker: outbox `report`); `conversations.reports` and `diff.read` hub bridges; ui.pages[].opens for Project/Service "Recent commits". The core commit card, Changes view, diff page and review store are gone; bash_diff.py keeps bashDiff only. Legacy gitCommits / commit-line parsing stays for past transcripts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
5.1 KiB
Python
131 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
"""``aia_report`` — how a plugin's path shim (``pathShims``, path_shims.py)
|
|
reports what a command did, **out of band**: nothing goes through the agent's
|
|
context, the report reaches the hub and the plugin reads it back
|
|
(``conversations.reports`` — backend plugins/hubreports.py).
|
|
|
|
A shim imports it through ``AIA_REPORT_PY``::
|
|
|
|
import importlib.util, os
|
|
spec = importlib.util.spec_from_file_location("aia_report", os.environ["AIA_REPORT_PY"])
|
|
aia_report = importlib.util.module_from_spec(spec); spec.loader.exec_module(aia_report)
|
|
aia_report.report("commit", {"v": 1, ...}, {"v": 1, "patches": {...}})
|
|
|
|
``report(event, summary, blob=None) -> bool``: ``summary`` ≤ 8 KB of JSON
|
|
(refused past it), ``blob`` ≤ 160 KB (dropped past it, ``summary.blobDropped``
|
|
set). Adds ``ts`` (epoch ms), ``cwd`` and ``pid``. Never raises, never prints,
|
|
gives up after ~2 s. The plugin is ``AIA_SHIM_PLUGIN`` (the wrapper sets it),
|
|
the conversation ``AIA_SESSION_ID`` (the sidecar exports it to every run).
|
|
|
|
Transport — the one ``conv add`` uses:
|
|
|
|
* **a worker** (``AI_AGENT_OUTBOX_URL`` + ``AI_AGENT_OUTBOX_TOKEN``): a one-shot
|
|
``report`` item (id ``rpt_`` + 12 hex) in the sidecar's outbox, collected by
|
|
the hub's ``OutboxMirror``;
|
|
* **the lab** (``AIA_REPORT_URL`` + ``AIA_REPORT_TOKEN``): straight to the hub,
|
|
``POST /api/plugin-reports`` — the token is the run's own (an HMAC of its
|
|
session id under the sidecar's bearer), so a run can only file reports on
|
|
its own conversation.
|
|
|
|
On failure the report is appended to ``AIA_REPORT_SPOOL`` (jsonl), which the
|
|
sidecar drains later (path_shims.drain_spool). Stdlib only.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import secrets
|
|
import time
|
|
import urllib.request
|
|
|
|
MAX_SUMMARY = 8 * 1024
|
|
MAX_BLOB = 160 * 1024
|
|
DEADLINE_S = 2.0
|
|
SPOOL_MAX = 4 * 1024 * 1024
|
|
_EVENT_RE = re.compile(r"^[a-z][a-z0-9._-]{0,31}$")
|
|
_PLUGIN_RE = re.compile(r"^[a-z0-9][a-z0-9-]{0,47}$")
|
|
_SID_RE = re.compile(r"^[A-Za-z0-9_-]{8,64}$")
|
|
|
|
|
|
def _dump(v) -> str:
|
|
return json.dumps(v, separators=(",", ":"), ensure_ascii=False)
|
|
|
|
|
|
def build(event: str, summary: dict, blob: dict | None = None) -> dict | None:
|
|
"""The outbox payload / hub body of one report, or None when it is not one."""
|
|
sid = (os.environ.get("AIA_SESSION_ID") or os.environ.get("CLAUDE_SESSION_ID") or "").strip()
|
|
plugin = (os.environ.get("AIA_SHIM_PLUGIN") or "").strip()
|
|
if not _EVENT_RE.match(event or "") or not _SID_RE.match(sid) or not _PLUGIN_RE.match(plugin):
|
|
return None
|
|
if not isinstance(summary, dict):
|
|
return None
|
|
summary = dict(summary)
|
|
if blob is not None:
|
|
if not isinstance(blob, dict) or len(_dump(blob).encode()) > MAX_BLOB:
|
|
blob = None
|
|
summary["blobDropped"] = True
|
|
if len(_dump(summary).encode()) > MAX_SUMMARY:
|
|
return None
|
|
try:
|
|
cwd = os.getcwd()
|
|
except OSError:
|
|
cwd = ""
|
|
rep = {"ts": int(time.time() * 1000), "event": event, "cwd": cwd[:1000],
|
|
"pid": os.getpid(), "summary": summary}
|
|
if blob is not None:
|
|
rep["blob"] = blob
|
|
return {"id": f"rpt_{secrets.token_hex(6)}", "sessionId": sid, "plugin": plugin, "report": rep}
|
|
|
|
|
|
def _post(url: str, body: dict, headers: dict, timeout: float) -> bool:
|
|
req = urllib.request.Request(url, data=_dump(body).encode(), method="POST",
|
|
headers={"Content-Type": "application/json", **headers})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=max(0.2, timeout)) as r:
|
|
return 200 <= r.status < 300
|
|
except Exception: # noqa: BLE001 — any failure spools
|
|
return False
|
|
|
|
|
|
def send(payload: dict, timeout: float = DEADLINE_S) -> bool:
|
|
"""Deliver one built report on this machine's transport."""
|
|
outbox = (os.environ.get("AI_AGENT_OUTBOX_URL") or "").rstrip("/")
|
|
if outbox:
|
|
tok = os.environ.get("AI_AGENT_OUTBOX_TOKEN", "")
|
|
return _post(f"{outbox}/outbox", {"kind": "report", "payload": payload},
|
|
{"Authorization": f"Bearer {tok}"}, timeout)
|
|
url = (os.environ.get("AIA_REPORT_URL") or "").rstrip("/")
|
|
tok = os.environ.get("AIA_REPORT_TOKEN", "")
|
|
if not url or not tok:
|
|
return False
|
|
return _post(f"{url}/api/plugin-reports", {**payload, "token": tok}, {}, timeout)
|
|
|
|
|
|
def spool(payload: dict) -> bool:
|
|
path = os.environ.get("AIA_REPORT_SPOOL", "")
|
|
if not path:
|
|
return False
|
|
try:
|
|
if os.path.exists(path) and os.path.getsize(path) > SPOOL_MAX:
|
|
return False
|
|
line = _dump(payload) + "\n"
|
|
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
|
|
with os.fdopen(fd, "a", encoding="utf-8") as f:
|
|
f.write(line)
|
|
return True
|
|
except Exception: # noqa: BLE001
|
|
return False
|
|
|
|
|
|
def report(event: str, summary: dict, blob: dict | None = None) -> bool:
|
|
"""File one report (see the module doc). True once delivered or spooled."""
|
|
try:
|
|
payload = build(event, summary, blob)
|
|
if payload is None:
|
|
return False
|
|
return send(payload) or spool(payload)
|
|
except Exception: # noqa: BLE001 — never in the way of the command
|
|
return False
|