mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The tree was a debranded snapshot of upstream taken around 2026-02-20, and upstream has moved about five months since. This replaces it with upstream master, mechanically re-derived: bin/debrand does the rename, and the pipeline and squashed migrations are restored from the shipped tree. Not a merge. The previous import replaced the tree wholesale in a single commit, so there is no shared content history to merge against. Reproducing the rename on the new tree is what actually converges. What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md, and insights/migrations. The squashed initial migration is load-bearing — the live database records it by name, so upstream's ~918 individual migrations cannot come back. Nine products are dropped as unreferenced. Twenty-one more that we do not run are kept anyway because the core app still imports them; unpicking those is a refactor and does not belong in a rebase. This base does not build yet, and the reasons are not in the rename: upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against our 4.2, and the dependency manifests still name upstream packages that we republish under our own names. Those are decisions, not renames.
89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
"""Shared format for sandbox addons (tools, MCP servers): named entries chosen
|
|
from a checked-in YAML catalog into a per-user YAML file. Owns the read/write
|
|
plumbing, the registry path, and the error type; per-type schema and apply logic
|
|
live in sandbox_tools.py / sandbox_mcp.py.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from pathlib import Path
|
|
from typing import Protocol, TypeVar
|
|
|
|
import yaml
|
|
|
|
REGISTRY_DIR = Path.home() / ".insights-sandboxes"
|
|
|
|
|
|
class AddonError(Exception):
|
|
"""Raised on any malformed catalog or user-file entry."""
|
|
|
|
|
|
class _Named(Protocol):
|
|
name: str
|
|
|
|
|
|
_T = TypeVar("_T", bound=_Named)
|
|
|
|
|
|
class _BlockLiteralDumper(yaml.SafeDumper):
|
|
"""Renders multi-line strings as `|` blocks (e.g. tool install scripts)."""
|
|
|
|
|
|
def _block_literal_str(dumper: yaml.SafeDumper, data: str) -> yaml.ScalarNode:
|
|
style = "|" if "\n" in data else None
|
|
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style=style)
|
|
|
|
|
|
_BlockLiteralDumper.add_representer(str, _block_literal_str)
|
|
|
|
|
|
def load_named_entries(path: Path, section: str) -> list[dict]:
|
|
"""Read the `section:` list as raw dicts, validating unique non-empty names.
|
|
|
|
Returns [] when the file is absent.
|
|
"""
|
|
if not path.is_file():
|
|
return []
|
|
raw = yaml.safe_load(path.read_text()) or {}
|
|
entries: list[dict] = []
|
|
seen: set[str] = set()
|
|
for i, entry in enumerate(raw.get(section) or []):
|
|
label = f"{path}: {section}[{i}]"
|
|
if not isinstance(entry, dict):
|
|
raise AddonError(f"{label}: must be a mapping.")
|
|
name = entry.get("name")
|
|
if not isinstance(name, str) or not name.strip():
|
|
raise AddonError(f"{label}: 'name' is required and must be a non-empty string.")
|
|
if name in seen:
|
|
raise AddonError(f"{path}: duplicate name {name!r}.")
|
|
seen.add(name)
|
|
entries.append(entry)
|
|
return entries
|
|
|
|
|
|
def save_named_entries(path: Path, section: str, entries: list[dict], *, mode: int | None = None) -> None:
|
|
"""Write `entries` under the `section:` key, preserving order and block scalars."""
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(
|
|
yaml.dump(
|
|
{section: entries},
|
|
Dumper=_BlockLiteralDumper,
|
|
sort_keys=False,
|
|
default_flow_style=False,
|
|
indent=2,
|
|
)
|
|
)
|
|
if mode is not None:
|
|
path.chmod(mode)
|
|
|
|
|
|
def load_entries(path: Path, section: str, parse: Callable[[dict], _T]) -> list[_T]:
|
|
"""Parse each `section:` entry from a YAML file into a typed object."""
|
|
return [parse(entry) for entry in load_named_entries(path, section)]
|
|
|
|
|
|
def load_catalog(path: Path, section: str, parse: Callable[[dict], _T]) -> dict[str, _T]:
|
|
"""Like load_entries, but keyed by entry name for catalog lookups."""
|
|
return {entry.name: entry for entry in load_entries(path, section, parse)}
|