utils/app_errors/app_errors/ingest.py
Krzysztof kuhy Rudnicki f05845e2f7 Read GitHub repo from checkout origin
feedback_inbox and app_errors guessed kuhyx/<folder> for a task's repo,
which filed cleanup-plate under kuhyx/cleanup-plate (real:
Sepci0/cleanup-plate) and showed phantom repos on the board. Both now
read the GitHub owner/name from ~/src/<folder>'s origin remote (https or
ssh forms) and fall back to the guess only when there is no checkout or
its origin is not on GitHub. app-errors 0.1.2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZfgt44aEKxvGBJkwP
2026-11-19 18:08:26 +01:00

103 lines
3.6 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki
"""One ingest pass: RTDB and the local spool in, the board's reports out.
In: RTDB ``/<app>/errors/<device>/<id>`` and the spool. Out:
``<state>/app-errors/<app>.json``, the board's contract report.
RTDB entries are deleted once the state holding them is saved (``keep``
leaves them; ``seen`` stops a second count). kartka is skipped: it still runs
its own ingest (``~/src/kartka/tools/errors``), which the board reads through
its ``kartka`` adapter, and two readers of ``/kartka/errors`` would race.
"""
from __future__ import annotations
from dataclasses import dataclass
import functools
import os
from pathlib import Path
from typing import TYPE_CHECKING
from app_errors import origin, spool, store
from app_errors.rtdb import RtdbError
if TYPE_CHECKING:
from app_errors.rtdb import Rtdb
#: Apps with their own ingest, never read here (kartka moved over 2026-11-17).
SKIP_APPS: frozenset[str] = frozenset()
@dataclass(frozen=True)
class Paths:
"""Where the ingest keeps its state, writes reports and reads the spool."""
state: Path
reports: Path
spool: Path
#: Checkouts: ``<src_root>/<app>``'s origin names the repo of a report without one.
src_root: Path
@dataclass(frozen=True)
class Summary:
"""What one pass did."""
merged: int
rtdb_error: str | None
def default_paths() -> Paths:
"""Return the state paths (``$XDG_STATE_HOME``, default ``~/.local/state``) and ``~/src``."""
base = Path(os.environ.get("XDG_STATE_HOME") or Path.home() / ".local" / "state")
return Paths(
base / "app-errors-ingest" / "state.json",
base / "app-errors",
base / "app-errors-spool",
Path.home() / "src",
)
def _rtdb_entries(
db: Rtdb,
) -> tuple[list[tuple[str, str, object]], dict[str, dict[str, object]]]:
"""Return (app, device/id, entry) for every RTDB report, and the deletes that consume them."""
found: list[tuple[str, str, object]] = []
deletes: dict[str, dict[str, object]] = {}
top = db.get("", shallow=True)
for app in sorted(top) if isinstance(top, dict) else []:
if app in SKIP_APPS or not store.APP_NAME.match(app):
continue
devices = db.get(f"{app}/errors")
for device, entries in devices.items() if isinstance(devices, dict) else []:
for entry_id, entry in entries.items() if isinstance(entries, dict) else []:
found.append((app, f"{device}/{entry_id}", entry))
deletes.setdefault(app, {})[f"{device}/{entry_id}"] = None
return found, deletes
def run(paths: Paths, db: Rtdb | None, *, keep: bool = False) -> Summary:
"""Ingest everything once; RTDB failures are returned, the spool still goes in."""
state = store.load(paths.state)
repo_of = functools.partial(origin.repo_slug, paths.src_root, owner=store.OWNER)
merged = 0
rtdb_error = None
deletes: dict[str, dict[str, object]] = {}
if db is not None:
try:
found, deletes = _rtdb_entries(db)
except RtdbError as e:
found, rtdb_error = [], str(e)
merged += sum(store.merge(state, a, k, e, repo_of=repo_of) for a, k, e in found)
lines = spool.drain(paths.spool)
merged += sum(store.merge(state, ln.app, ln.id, ln.entry, repo_of=repo_of) for ln in lines)
store.save(paths.state, state)
store.write_reports(paths.reports, state)
if db is not None and not keep:
try:
for app, paths_gone in deletes.items():
db.patch(f"{app}/errors", paths_gone)
except RtdbError as e:
rtdb_error = str(e)
return Summary(merged, rtdb_error)