Skip to content

fix(k8s): stale environment checkout is read without a warning #7295

Done

Description

@dan-petty

Problem

A command that reads the cluster environment can take it from an infra.repos checkout (resolve_environment in src/devops_cli/k8s/environment.py). It reads the files as they are on disk. It gives no sign when the checkout is behind its upstream branch, is on a topic branch or holds uncommitted changes.

Evidence

On 2026-11-26 the local clone of the environment repository had core.bare=true with a full working tree, and its main branch was 79 commits behind origin/main. No command reported it. The clone was repaired by hand.

After step 17 of the repository switch (#7290), devops-cli holds no environment files, so commands run from this repository read that checkout.

Done when

  • A command that takes the tree from an infra.repos checkout names the checkout and its branch.
  • A command that only reads warns when the checkout is behind the upstream branch that it last fetched, or holds uncommitted changes.
  • A command that writes to the cluster refuses such a checkout, and the message names the fix.
  • The checks use local git data only and make no request.

Alternatives check of 2026-11-26: partly.
Tool: gitpython-developers/GitPython (https://github.com/gitpython-developers/GitPython), BSD-3-Clause, last activity 2026-11-26, maintained.
Reason: Searches surfaced only git libraries, not a stale-checkout checker; GitPython reads the branch, its upstream, ahead/behind counts and dirty or bare state from local data, but the warn and refuse policy is left to us.

Covers:

  • Active branch name and its tracking branch from local config
  • Ahead and behind commit counts without a network request
  • Modified and untracked files in the worktree
  • Bare flag, which catches the core.bare=true clone state

Lacks:

  • No warn-or-refuse rule for commands that write to the cluster
  • No message that names the repair command
  • No check aimed at infra.repos environment checkouts
  • Runs the git binary rather than reading objects in process

Proposed design

Problem statement

A command reads an environment tree from an infra.repos checkout and takes the files as they are on disk. It gives no sign when the checkout is behind its upstream, on a topic branch, bare or dirty, so a command that writes to the cluster can apply files that are not the upstream state. The core.bare=true state comes from the kube-linter pre-commit hook of the infra repo, which runs git init with GIT_DIR set (dan-petty/homelab#323). The change names the checkout and its branch, warns on a read, and refuses a stale checkout on a write, from local git data only.

Acceptance criteria

Requirement: Checkout and branch on the tree

An environment tree that resolve_environment takes from an infra.repos checkout SHALL carry the checkout path and its active branch.

Scenario: Tree from an infra checkout

  • WHEN resolve_environment takes the tree from the checkout of an entry of settings.infra.repos whose branch main is checked out.
  • THEN The returned tree names the checkout path and the branch main.

Requirement: Read warns on a stale checkout

A command that only reads an infra.repos checkout SHALL warn when the checkout is behind the upstream branch that it last fetched.

Scenario: Two commits behind upstream

  • WHEN The local branch is two commits behind origin/main and resolve_environment is called with no write flag.
  • THEN It returns the tree and writes to stderr a warning that names the checkout, the branch, the count 2 and the fix git -C <checkout> pull --ff-only.

Requirement: Read warns on a dirty checkout

A command that only reads an infra.repos checkout SHALL warn when the checkout holds uncommitted changes.

Scenario: One modified and one untracked file

  • WHEN The checkout holds one modified tracked file and one untracked file and resolve_environment is called with no write flag.
  • THEN The warning names the checkout, the branch, the count 2 of changed files and the fix git -C <checkout> stash --include-untracked.

Requirement: Write refuses a stale checkout

A command that writes to the cluster MUST refuse an infra.repos checkout that is behind its upstream or holds uncommitted changes.

Scenario: Write on a stale checkout

  • WHEN resolve_environment is called with write=True for a checkout that is two commits behind its upstream.
  • THEN It raises ConfigurationError with the key infra.repos, and the message names the checkout and git -C <checkout> pull --ff-only.

Requirement: Bare flag with a worktree

The check MUST treat a checkout with core.bare=true and a worktree as unusable and MUST name git -C <checkout> config core.bare false.

Scenario: Bare clone with a worktree

  • WHEN The checkout has core.bare=true and its files are on disk, as the evidence of the item records.
  • THEN A read writes the warning with git -C <checkout> config core.bare false, and a write raises ConfigurationError with the same text.

Requirement: Local data only

The check MUST read local git data only and MUST make no network request.

Scenario: Unreachable origin

  • WHEN The checkout's origin URL is https://example.invalid/env.git and its origin/main ref is two commits ahead of the local branch.
  • THEN The check reports two commits behind, every recorded git call passes local_read=True, and no request reaches that URL.

Requirement: Writing commands ask for a write tree

Every command that changes the cluster MUST resolve its environment tree with write=True unless it runs as a dry run or a plan.

Scenario: Each writing command on a stale checkout

  • WHEN devops argo cd apps bootstrap-gitops, devops k8s deploy-stack, devops k8s teardown-stack, devops k8s bootstrap, devops k8s ollama prune, devops k8s push-secrets, devops k8s bootstrap-mattermost --push-secrets or devops grafana dashboards sync runs without a dry run or plan on a checkout that is two commits behind its upstream.
  • THEN Each command exits 1 with the message that names the checkout and git -C <checkout> pull --ff-only, and no Kubernetes request is recorded.

Requirement: Dry run warns and does not refuse

A dry run or a plan of a writing command MUST warn on a stale checkout and MUST NOT refuse it.

Scenario: Dry run of a writing command

  • WHEN devops argo cd apps bootstrap-gitops --dry-run runs on a checkout that is two commits behind its upstream.
  • THEN The command prints the warning and exits 0 with its plan.

Existing Tool Considered

GitPython (https://github.com/gitpython-developers/GitPython), BSD-3-Clause, maintained: the alternatives check of 2026-11-26 in the issue body gives the verdict partly, and its block states what the tool lacks. The project records that no module of src/ imports GitPython and every start of git goes through run_subprocess (docs/TESTING.md:84; openspec/changes/archive/2026-10-30-2146-one-git-seam-and-fake/proposal.md:11), and GitPython is not a dependency (pyproject.toml:11). The change therefore reads the branch, upstream, ahead and behind counts, dirty files and bare flag with local git commands through that seam, and builds only the warn-or-refuse policy, the repair messages and the infra.repos scope. Cost: about 2,000 tokens to write the reader, the messages and their tests, about 1,000 tokens a year to keep and fix, and about 4 hours elapsed.

Key questions

  • Which open tool reads the branch, the upstream, the ahead and behind counts and the dirty state from local data?: GitPython covers the reads, but the project records that it removed GitPython for one git seam: no module of src/ imports it and every start of git goes through run_subprocess (docs/TESTING.md:84). The change reads the facts with git -C <checkout> ... through run_subprocess(..., local_read=True): config --get core.bare, branch --show-current, rev-parse --abbrev-ref --symbolic-full-name @{upstream}, rev-list --left-right --count HEAD...@{upstream} and status --porcelain. It reuses current_branch (src/devops_cli/git/operations.py:282) and the status read behind is_git_clean (src/devops_cli/git/operations.py:464), and it treats a git failure as no check. (best practice) [sources: docs/TESTING.md:84, openspec/changes/archive/2026-10-30-2146-one-git-seam-and-fake/proposal.md:11, src/devops_cli/git/operations.py:282, src/devops_cli/git/operations.py:464, pyproject.toml:11]
    • Chosen: Local git commands through run_subprocess. The project records one git seam, the commands are local reads that the git fake records, and the code reuses current_branch and the status read.
    • Rejected: GitPython. The alternatives check names it, but the project removed it: no module of src/ imports it and an invariant test fails one that does (docs/TESTING.md:84).
    • Rejected: pygit2. It binds libgit2 and needs a compiled library, and it reads git outside the one seam that the invariant test enforces.
    • Rejected: dulwich. Pure Python, but it reads git objects outside the process runner seam, and the alternatives check names GitPython.
  • Where does the checkout check run?: In resolve_environment, on the tree that it takes from an entry of settings.infra.repos (src/devops_cli/k8s/environment.py:174). A tree from --k8s-dir gets no check because a person gave that path, and a tree from the profile of the current repository gets none because the command runs in that checkout (src/devops_cli/k8s/environment.py:164). When git cannot read the checkout, the tree is returned with no check, so a folder with a .git and no repository still resolves. (best practice) [sources: src/devops_cli/k8s/environment.py:151, src/devops_cli/k8s/environment.py:164, src/devops_cli/k8s/environment.py:174]
    • Chosen: In resolve_environment for an infra.repos checkout. One place reads every tree, so each command gets the check and no command can skip it.
    • Rejected: In each command that reads a tree. Each command repeats the check, and a new command can forget it.
    • Rejected: In _profile_tree. _profile_tree holds no settings, so it cannot tell an infra.repos checkout from the current repository.
  • How does a command declare that it writes to the cluster?: resolve_environment takes the keyword write with the default False. The commands that change the cluster pass write=True unless the run is a dry run or a plan: bootstrap-gitops (src/devops_cli/commands/argo.py:413), deploy-stack and teardown-stack (src/devops_cli/commands/k8s/stack_lifecycle.py:1488), k8s bootstrap (src/devops_cli/commands/k8s/bootstrap.py:43), ollama prune (src/devops_cli/commands/k8s/ollama.py:111), push-secrets (src/devops_cli/commands/k8s/cluster_secret_push.py:227), bootstrap-mattermost --push-secrets (src/devops_cli/commands/k8s/mattermost_bootstrap.py:403) and grafana dashboards sync (src/devops_cli/commands/grafana.py:291). load_cluster_secrets_table takes the same keyword for the commands that read the table before they resolve a tree. (best practice) [sources: src/devops_cli/k8s/environment.py:151, src/devops_cli/commands/argo.py:413, src/devops_cli/commands/k8s/stack_lifecycle.py:1488, src/devops_cli/commands/k8s/bootstrap.py:43, src/devops_cli/commands/k8s/ollama.py:111, src/devops_cli/commands/k8s/cluster_secret_push.py:227, src/devops_cli/commands/k8s/mattermost_bootstrap.py:403, src/devops_cli/commands/grafana.py:291]
    • Chosen: A write keyword on resolve_environment. The refusal happens where the tree is read, before the command uses a file of the tree.
    • Rejected: A second function resolve_writable_environment. It copies the resolution logic, and two names for one tree invite a wrong call.
    • Rejected: A require_fresh() method that each writer calls. A writer can forget the call, and the stale tree then reaches the cluster.
  • How does a read show the warning?: resolve_environment calls print_warning(<message from ERRORS.k8s>, prefix=False, to_stderr=True) from devops_cli.output.console; a test asserts the text on stderr. No module of the project calls warnings.warn, and the project's design of 2026-11-26 returns a warning text and prints it with print_warning (openspec/changes/archive/2026-11-26-7233-a-new-worktree-gets-the-pinned/design.md:9). Here the one place that resolves the tree prints it, so no command can forget it, and stderr keeps stdout whole for --format json. (best practice) [sources: src/devops_cli/output/console.py:447, openspec/changes/archive/2026-11-26-7233-a-new-worktree-gets-the-pinned/design.md:9, src/devops_cli/commands/k8s/catalog.py:85, src/devops_cli/k8s/environment.py:151]
    • Chosen: print_warning with to_stderr=True. The project's leveled warning printer shows it on stderr, a test catches the text, and stdout stays parseable.
    • Rejected: warnings.warn. No module of the project uses it, and Python's default filter shows one warning per location.
    • Rejected: A field on the tree that each command prints. Each command needs print code, and a command that forgets prints nothing.
  • Where do the checkout messages live?: New keys under ERRORS.k8s, next to environment_path_missing at src/devops_cli/lang/en/errors.py:235, and the refusal raises ConfigurationError with the key infra.repos, as environment_root_ambiguous does at src/devops_cli/k8s/environment.py:184. [sources: src/devops_cli/lang/en/errors.py:235, src/devops_cli/k8s/environment.py:184]
  • Which repair command does each state name?: A checkout behind its upstream names git -C <checkout> pull --ff-only, the command that the bootstrap procedure already gives (k8s/README.md:454). A dirty checkout names git -C <checkout> stash --include-untracked, because the count includes untracked files and plain git stash leaves them. A checkout with core.bare=true and a worktree names git -C <checkout> config core.bare false, the repair that the issue records. (best practice) [sources: k8s/README.md:454, src/devops_cli/git/operations.py:464, openspec/changes/archive/2026-11-12-5265-github-token-scope/design.md:12]
    • Chosen: One fix per state, in the message. The message gives the exact command, and the reader repairs the checkout with no search.
    • Rejected: A pointer to the repository documentation. The reader leaves the command to find the steps, and the checkout stays stale.
    • Rejected: No fix in the message. The message states a problem with no action.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    scope/k8s
    source/agent
    status/new
    effort/medium
    value/high
    on Nov 26, 2026
  2. dan-petty commented on Nov 26, 2026

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 26, 2026
  4. dan-petty commented on Nov 26, 2026

    @dan-petty
    OwnerAuthor

    Cause of the core.bare=true state of the environment clone, found on 2026-11-26: the pre-commit hook kube-linter (make k8s-lint) of dan-petty/homelab, run from a linked worktree. kube-linter runs git init for a remote kustomize base, and inside the hook that command inherits GIT_DIR and changes the clone. Reproduction and fix criteria: dan-petty/homelab#323. The clone flipped again today after a commit below k8s/ and was repaired with git config core.bare false.

  5. devops-cli-service commented on Nov 26, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (10)

    Tools considered:

    Searches run:

    • pygit2 ahead_behind branch upstream status uncommitted changes, results: 5
    • python library check git checkout behind upstream branch and uncommitted changes locally, results: 5
    • pygit2 github repository libgit2 python bindings, results: 5
    • GitPython is_dirty active_branch upstream tracking branch, results: 5
    • git status porcelain v2 branch ahead behind upstream local, results: 5
    • pygit2, results: 5
    • GitPython tracking_branch ahead behind commits rev_list count, results: 5
    • tool warns git checkout behind upstream and dirty before deploy git repository health check, results: 5
    • GitPython documentation is_dirty tracking_branch, results: 5
    • dulwich porcelain status branch upstream python, results: 5

    Sources:

  6. added
    status/ready
    and removed
    status/new
    on Nov 28, 2026
  7. devops-cli-service commented on Nov 28, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  8. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 29, 2026
  9. removed
    status/in-review
    owner/agent
    on Nov 29, 2026
  10. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-29 03:13 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/k8ssource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions