"""Insert the fail-closed librechat.yaml validation into /app/pkg/start.sh. Run at BUILD time (the runtime rootfs is read-only under Cloudron). Non-fatal: if the anchor line is missing (upstream changed its start script) this prints a loud note and exits 0 so a LibreChat version bump can never brick the build — the RUNNING app then simply lacks the pre-boot check until the patch is fixed, which the deploy verification catches by grepping the boot log. Idempotent: the block carries a marker; re-insertion is skipped when present. The block validates /app/data/librechat.yaml with the container's own librechat-data-provider strict schema (the same check the API server runs at boot, which exits 1 on an unrecognized top-level key — the 2026-10-31 outage). Placement: after the first-run `touch /app/data/librechat.yaml` and before the secrets/env assembly, so a bad config stops the boot BEFORE anything starts. The -s guard skips the empty first-run file; the -f guard keeps a missing validator non-fatal (patch did not apply). An INVALID config is fatal by design — that is the point. """ import sys START = "/app/pkg/start.sh" ANCHOR = "if [[ ! -f /app/data/librechat.yaml ]]; then touch /app/data/librechat.yaml; fi" MARKER = "co-op fail-closed librechat.yaml validation" BLOCK = """ # --- {marker} (overlay, 2026-11-11) --- # Invalid librechat.yaml must stop the boot LOUDLY, naming the offending key, # before the API server can come half-up (SPA serving 200 while the API is # down — the 2026-10-31 outage). The venv python carries pyyaml; the schema # probe runs against the image's own data-provider build. if [ -f /app/code/validate_config.py ] && [ -s /app/data/librechat.yaml ]; then echo "Validating librechat.yaml (fail-closed)..." if ! /app/code/venv/bin/python3 /app/code/validate_config.py librechat /app/data/librechat.yaml; then echo "FATAL: librechat.yaml is invalid - refusing to start. Fix or remove the offending key named above." >&2 exit 1 fi fi """.format(marker=MARKER) def main() -> int: try: with open(START, encoding="utf-8") as fh: src = fh.read() except OSError as exc: print(f"WARN: cannot read {START}: {exc} — validation patch NOT applied", flush=True) return 0 if MARKER in src: print("INFO: validation block already present in start.sh (idempotent skip)", flush=True) return 0 if ANCHOR not in src: print("WARN: anchor line not found in /app/pkg/start.sh — validation " "patch NOT applied; fix patch_start_validator.py after the " "upstream change", flush=True) return 0 patched = src.replace(ANCHOR, ANCHOR + "\n" + BLOCK.strip("\n") + "\n", 1) with open(START, "w", encoding="utf-8") as fh: fh.write(patched) print(f"INFO: inserted fail-closed config validation into {START}", flush=True) return 0 if __name__ == "__main__": sys.exit(main())