refactor(egress): write routes.yaml as actual YAML, not JSON-in-yml
test / unit (pull_request) Successful in 18s
test / integration (pull_request) Successful in 1m7s

`egress_render_routes` now emits hand-rolled YAML in the same style
as `pipelock_render_yaml`. The egress addon parses it via
`yaml_subset.parse_yaml_subset` — the same parser the manifest
loader + pipelock_apply use.

Why bother: routes.yaml is bind-mounted into the egress sidecar
AND surfaced to operators through `routes edit` (PRD 0019). JSON-
in-yml renders ugly in $EDITOR and signals "this is data" rather
than "this is config you can read at a glance". Real YAML reads
cleanly.

Mechanics:

  - `yaml_subset.py` drops its `claude_bottle.log` dependency.
    Errors now raise `YamlSubsetError` (a `ValueError`); the
    manifest loader + pipelock_apply catch it at the boundary
    and forward to `die` / `PipelockApplyError` so callers see
    the same behavior they did before.
  - `Dockerfile.egress` adds one COPY line for `yaml_subset.py`
    so it sits flat in `/app/` next to the addon. The addon
    uses an absolute-import-with-fallback shim so the same file
    works inside the container AND from the host's unit tests.
  - `egress_apply._merge_single_route` round-trips current
    routes.yaml through `parse_yaml_subset` + a new
    `_render_routes_payload` helper instead of `json.loads` +
    `json.dumps`.

End-to-end: rebuilt the egress image, ran `./cli.py start` to a
full bring-up, confirmed the addon's boot log shows `egress:
loaded 9 route(s)` — i.e., the YAML parses inside the container.
453 unit + 3 integration tests pass.
This commit is contained in:
2026-05-26 02:17:42 -04:00
parent 11d5bf1489
commit c9825cf701
11 changed files with 254 additions and 124 deletions
+22 -8
View File
@@ -6,16 +6,28 @@ exercise the parse + decision functions without depending on the
`mitmproxy.http.HTTPFlow` API and is loaded inside the sidecar
container.
Stdlib only: this file ships into the egress image, where the
container's Python is whatever mitmproxy itself runs on.
Imports: stdlib + `yaml_subset` (which is itself stdlib-only and
ships flat into the egress image alongside this file — see
`Dockerfile.egress`).
"""
from __future__ import annotations
import json
import typing
from dataclasses import dataclass
# Absolute import — `yaml_subset.py` is copied flat into the egress
# image's `/app/` next to this file (via `Dockerfile.egress`). The
# host-side unit tests run with the repo on sys.path, where the
# bare `yaml_subset` module also resolves because
# `claude_bottle/yaml_subset.py` shadows it at import time... actually
# no, on host the module lives under the `claude_bottle` package.
# The try/except shim picks whichever import works.
try:
from yaml_subset import YamlSubsetError, parse_yaml_subset # type: ignore[import-not-found]
except ImportError: # pragma: no cover - host-side path
from .yaml_subset import YamlSubsetError, parse_yaml_subset
@dataclass(frozen=True)
class Route:
@@ -126,12 +138,14 @@ def _parse_one(idx: int, raw: object) -> Route:
def load_routes(text: str) -> tuple[Route, ...]:
"""Convenience: parse JSON text → routes. Raises `ValueError` for
both decode and shape errors so callers handle them uniformly."""
"""Parse YAML text → routes. Raises `ValueError` for both
decode and shape errors so callers handle them uniformly.
`YamlSubsetError` from the parser is a `ValueError` subclass so
it already satisfies the same surface; we let it propagate."""
try:
payload = json.loads(text)
except json.JSONDecodeError as e:
raise ValueError(f"routes payload: invalid JSON: {e}") from e
payload = parse_yaml_subset(text)
except YamlSubsetError as e:
raise ValueError(f"routes payload: invalid YAML: {e}") from e
return parse_routes(payload)