Correctness and contracts scratchpad · NVIDIA/hyperflare-inference #690

Raw Markdown for agents: https://autobot.eidonix.com/p/750ea9db-1386-4789-902b-b58f91271ec8.md

#1 [high] F1 Major: stale/nonexistent inherited CUDA_HOME silently disables toolkit env (no existence check)

Code (head 39796ea, pr/690), ignition/cli/environment.py:56-66:

    cuda = configuration.directory("cuda")
    if cuda is None and os.name != "nt":
        inherited_cuda = os.environ.get("CUDA_HOME", "").strip()
        if inherited_cuda:
            cuda = Path(inherited_cuda)
        elif _DEFAULT_LINUX_CUDA.is_dir():
            cuda = _DEFAULT_LINUX_CUDA
        else:
            os.environ.pop("CUDA_HOME", None)
    if cuda is not None:
        os.environ["CUDA_HOME"] = str(cuda)

No is_dir() / bin/ptxas existence check on the inherited value (contrast the /usr/local/cuda branch at :61 which does check is_dir()). Then :74-81 skip both derivations because bin/ptxas is not a file and include is not a dir.

Test freezing it, tests/cli/test_environment.py:138-148:

def test_inherited_cuda_home_without_local_files_does_not_fall_back_to_default(...):
    inherited = clean_environment / "custom-cuda"      # never created
    default = _make_toolkit(clean_environment / "default-cuda")
    monkeypatch.setattr(environment, "_DEFAULT_LINUX_CUDA", default)
    monkeypatch.setenv("CUDA_HOME", str(inherited))
    environment.apply(_load_config(clean_environment), ())
    assert os.environ["CUDA_HOME"] == str(inherited)
    assert "TRITON_PTXAS_PATH" not in os.environ
    assert "CPATH" not in os.environ

Impact: on a host that previously worked via /usr/local/cuda (main:55-56 if cuda is None and os.name != "nt" and Path("/usr/local/cuda").is_dir(): cuda = Path("/usr/local/cuda")), a stale/leftover exported CUDA_HOME (shell profile, container env, another toolkit) now overrides the working default and yields no TRITON_PTXAS_PATH/CPATH at all — strictly worse than main for that host, and the mirror image of #636. Downstream consumer of the poisoned value: ignition/preparation/kv_codec.py:21-38 _find_nvcc() reads from torch.utils.cpp_extension import CUDA_HOME (which derives from this env var) as its first candidate -> RuntimeError "nvcc is required during core export". Docs do not disclose the no-existence-check rule: docs/cli-design.md:50-53 "preserves a non-empty inherited CUDA_HOME before falling back to /usr/local/cuda" and :60-62 "falls back through inherited CUDA_HOME and /usr/local/cuda" both read as validated fallback chains. AGENTS.md:470-471 same. Fix: if inherited_cuda and Path(inherited_cuda).is_dir(): so a nonexistent path continues to the /usr/local/cuda branch, or (if as-is is intended) state in docs/cli-design.md:50-52 and AGENTS.md:470 that a non-empty inherited CUDA_HOME is used verbatim without an existence check, and drop/adjust the test asserting the no-op.

#3 [high] Verified-unchanged / no-finding areas + test & CI facts

Verified at pr/690 against main:

#4 [high] Research summary

Reviewed PR #690 at head 39796ea across correctness, system-level consequences, contracts and test/claim accuracy, reading environment.py at both heads, config.py, all four apply() call sites (serve.py:210/291/436/545, bench.py:205), kv_codec.py, tuning.py, the docs (cli-design.md, AGENTS.md, model-preparation.md), design-review.md principle 10, the new test suite and the CI workflow.

Two findings, both logged with citations and verbatim quotes in the scratchpad:

Everything else checks out: Windows, TRITON_PTXAS_PATH and CPATH handling are byte-identical to main; the change touches no hot path, lock, cache, allocation, metric, proto/core format or Helios contract (the rate/latency/admission span check finds nothing changed); the doc reconciliation and the principle-10 exception are genuine; and the new suite does run in PR CI (python-cpu.yml:30-33). Remaining gaps are the ones already carried in the dossier: no end-to-end evidence that preserving CUDA_HOME actually steers flashinfer/nvcc, and #636 still has no maintainer decision.

#5 [high] Verbatim doc + PR-text quotes (cli-design, AGENTS, model-preparation, principle 10)

Verbatim quotes for the report, read at pr/690 (head 39796ea):

docs/cli-design.md:46-57 (principle-10 carve-out text added by the PR): "No environment-variable behavior switches. Flare reads environment variables only for CUDA device selection and non-Windows preparation toolkit dependency setup: CUDA_VISIBLE_DEVICES restricts which GPUs the caller exposed, which changes the GPU each index names, so Flare inspects it to interpret --gpus; on non-Windows hosts, when [paths] cuda is unset, ignition.cli.environment preserves a non-empty inherited CUDA_HOME before falling back to /usr/local/cuda (removing blank CUDA_HOME values when no toolkit source exists), and for any resolved toolkit it keeps a pre-set TRITON_PTXAS_PATH and prepends <toolkit>/include to CPATH when not already listed."

docs/cli-design.md:58-62: "Two levels of precedence. ... Host CUDA toolkit discovery on non-Windows hosts falls back through inherited CUDA_HOME and /usr/local/cuda only when [paths] cuda is unset."

docs/cli-design.md:1489 (precedence table, CUDA toolkit row): "| CUDA toolkit | [paths] cuda; otherwise on non-Windows hosts non-empty inherited CUDA_HOME, then /usr/local/cuda when present; otherwise native build libraries discover the system toolkit (on non-Windows hosts, a resolved toolkit also populates unset TRITON_PTXAS_PATH and prepends <toolkit>/include to CPATH) |"

AGENTS.md:461-472: "- The only environment variables Flare inspects belong to its documented CUDA device selection and preparation dependency setup policies. ... on non-Windows hosts, it also preserves a non-empty inherited CUDA_HOME when [paths] cuda is unset (before falling back to /usr/local/cuda), keeps a pre-set TRITON_PTXAS_PATH, and preserves existing CPATH entries, as specified in the CLI design." -> every doc statement says "preserves a non-empty inherited CUDA_HOME", i.e. presence of the variable, never validity of the directory. This is the wording that must be tightened if F1 (entry 1) is resolved as "document the no-existence-check policy" rather than "add is_dir()".

docs/model-preparation.md:123-126 (now defers instead of restating): "[paths] cuda selects an explicit toolkit directory; when unset, non-Windows hosts follow the CUDA_HOME and /usr/local/cuda fallback rules in the CLI design before native build libraries discover the system toolkit."

Principle 10, docs/design-review.md:89-92: "10. Use explicit configuration. Settings belong in recipe declarations, configuration files, or CLI flags; explicit user values win over defaults. Environment-variable behavior switches are prohibited except for the documented CUDA device selection and dependency setup policies." -> the carve-out the PR relies on exists; the "explicit user values win over defaults" clause is what favours the PR's new precedence (inherited CUDA_HOME over /usr/local/cuda) and also what would favour /usr/local/cuda over a stale inherited value only if the inherited value were treated as a default — worth one sentence in the report as the tension F1 sits in.

PR-text accuracy check (section "What changed" / "Claimed result"):

File counts from the header (pr_diff broken at this head: "Could not read c513e46a..."): AGENTS.md +13/-8, docs/cli-design.md +18/-9, docs/model-preparation.md +4/-3, ignition/cli/environment.py +10/-2, tests/cli/test_environment.py +175/-0.

#6 [high] Findings ranking, area verdicts, and tooling caveat

Ranking and disposition guidance for the final report (2 findings total; area verdicts):

  1. F1 — Major. Stale/nonexistent inherited CUDA_HOME silently disables toolkit environment. Evidence: ignition/cli/environment.py:56-66 (no is_dir() on the inherited branch, unlike :61) + tests/cli/test_environment.py:138-148 (freezes it) + docs/cli-design.md:50-53, :1489, AGENTS.md:470 (do not disclose no-existence-check). Concrete fix: if inherited_cuda and Path(inherited_cuda).is_dir(): (keeping :61-64 unchanged so the /usr/local/cuda fallback and the pop still apply), or keep behaviour and document "used verbatim, no existence check" and rewrite the test's name/assertion accordingly. Reviewer should pick one; not merge-blocking on its own but the PR is a regression fix, and this adds a fresh regression for the stale-env population.
  2. F2 — Minor. apply() non-idempotent (:58 reads the slot :64/:66 writes). Latent; note it as a forward hazard, not a defect of this PR's single-call paths.

Areas clean — state plainly, do not invent issues:

Tooling caveat to repeat: pr_diff and search_history both fail at this head ("Could not read c513e46a…"), so the diff was reconstructed by reading files at pr/690 and main.

#2 [medium] F2 Minor: apply() is non-idempotent — it reads its own output as "inherited" CUDA_HOME

ignition/cli/environment.py:58 reads os.environ.get("CUDA_HOME") at the same time :64/:66 writes it. So the input to the precedence decision is the same mutable process dict the function mutates. If apply() runs twice in one process (only one call per process path today: serve.py:210, 291, 436, 545; bench.py:205 — all single-shot per CLI command), the second call can never reach the _DEFAULT_LINUX_CUDA branch: a fallback written at :66 makes the next call see it as an inherited (pre-existing) value, and a popped value at :64 makes the next call see "no inherited CUDA_HOME" even though the caller originally exported a blank one. Result differs from the first call (first call w/ no env + no /usr/local/cuda -> CUDA_HOME unset; but a second call after a first call that set /usr/local/cuda keeps /usr/local/cuda). Impact today: latent only (no second call site); becomes a real stale-state bug the moment apply() is invoked per-test, per-export-step, or in a long-lived supervisor that re-reads config (e.g. a future re-prepare or a config reload). Also means the new suite cannot detect ordering mistakes — every test builds a fresh config but relies on the fixture to reset os.environ. Fix: capture the inherited value once (_INHERITED_CUDA_HOME = os.environ.get("CUDA_HOME") at module import, or pass it into apply as an argument) and treat the mutation of os.environ as output only.