Files
windy-git/api/app/buildinfo.py
Grant Whitmer 659991b2bd G0: cell substrate — invariants made executable
Strand G0 complete and VERIFIED against real Postgres, not asserted.

  - FastAPI plane, fail-closed provider seams, repair-pointer error taxonomy
  - migration 001: all 10 tables incl. repo_type NOT NULL and model_cards (I-7)
  - 17 invariant tests, ruff clean, vocabulary audit clean

Two bugs found by RUNNING it that review would not have caught:

  1. SQLAlchemy Enum persists .name, not .value — so RepoState.deleted_soft
     and CreatedVia.imported would have written labels migration 001 never
     declared, failing at runtime rather than at review. Pinned via
     values_callable.
  2. op.create_table asks each Enum to emit its own CREATE TYPE with no
     checkfirst, so the second reference raised DuplicateObject and the
     migration died halfway. Types are now created once, referenced with
     create_type=False.

Proven live, with the hostile env var set:
  - I-12: COMMIT_SHA=deadbeef... in the environment, /version reports real HEAD.
    That env pin is the documented root cause of nine sibling services
    misreporting their commit; here it is structurally ignored.
  - I-8: three unconfigured providers -> status degraded, HTTP 503, each saying
    'refusing to report healthy'. No mock, no false green.
  - G0.4: upgrade -> downgrade -> upgrade round-trip clean (10 -> 0 -> 10).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-11 14:19:28 -04:00

88 lines
2.8 KiB
Python

"""Deployment identity (I-12).
Nine of twelve live services in this ecosystem cannot name the commit they are
running. One reports *another repo's* commit. The root cause found on 2026-08-10
was hardcoded `COMMIT_SHA` pins in `/opt/*/.env` that OVERRIDE the build arg, so a
redeploy keeps reporting the old sha until a human hand-edits the file.
The fix here is structural, not procedural:
* the sha is baked into the image at build time (Docker ARG -> this module);
* a runtime `COMMIT_SHA` environment variable is **IGNORED**, loudly;
* in a dev worktree with nothing baked, we read git directly and SAY SO in
`source`, rather than reporting a value we cannot stand behind (I-8).
`make check` fails if /version disagrees with `git rev-parse HEAD` in CI.
"""
from __future__ import annotations
import logging
import os
import subprocess
from dataclasses import dataclass
from functools import lru_cache
from pathlib import Path
log = logging.getLogger(__name__)
# Rewritten at image build time by the Dockerfile. Do not edit by hand, and do
# not "helpfully" default it to something plausible.
BAKED_COMMIT_SHA: str = ""
BAKED_BUILT_AT: str = ""
VERSION = "0.1.0"
@dataclass(frozen=True)
class BuildInfo:
version: str
commit_sha: str | None
built_at: str | None
source: str # "baked" | "git-worktree" | "unknown"
def _git_head() -> str | None:
try:
root = Path(__file__).resolve().parents[2]
out = subprocess.run(
["git", "-C", str(root), "rev-parse", "HEAD"],
capture_output=True,
text=True,
timeout=5,
check=False,
)
return out.stdout.strip() or None if out.returncode == 0 else None
except Exception: # pragma: no cover - defensive
return None
@lru_cache
def get_build_info() -> BuildInfo:
override = os.environ.get("COMMIT_SHA")
if BAKED_COMMIT_SHA:
if override and override != BAKED_COMMIT_SHA:
log.warning(
"IGNORING COMMIT_SHA environment override (%s). This service "
"reports the sha baked into its own artifact (%s). See I-12 — an "
"env pin overriding the build arg is exactly why nine sibling "
"services misreport their commit.",
override[:12],
BAKED_COMMIT_SHA[:12],
)
return BuildInfo(VERSION, BAKED_COMMIT_SHA, BAKED_BUILT_AT or None, "baked")
head = _git_head()
if head:
if override:
log.warning(
"IGNORING COMMIT_SHA environment override (%s); reading the "
"worktree instead.",
override[:12],
)
return BuildInfo(VERSION, head, None, "git-worktree")
# Nothing baked, no git. Say so. Do not invent a sha (I-8).
return BuildInfo(VERSION, None, None, "unknown")