Automation can be counterintuitive: tighten the checks, cut the AI agent's permissions, and force the validator to reject its patches — and somehow the model starts fixing failures better than before. That's exactly what happened when engineer Maciej Stepkowski built kubemend, an early open-source prototype (v0.1, Apache-2.0) written in plain Python without heavy agent frameworks.
The agent reads metrics from Prometheus and pod logs from Grafana Loki to diagnose Kubernetes failures, but it cannot touch the cluster itself. Its only action is opening a draft Pull Request in a GitOps repo, where the desired cluster state lives in Git and an operator like Argo CD applies the config. It may edit only values*.yaml files in a Helm chart; templates are off-limits. If a fix crosses into another service or needs template changes, the agent stops and hands a written report to a human, keeping the blast radius contained.
Why the Agent's Self-Check Goes Straight in the Trash
Most 'AI SRE' demos end with a nice chat reply from the model. Nobody checks whether it can hit the target on a second try or whether a live cluster would accept the patch.
In kubemend, success is decided solely by an independent re-run of the gate. Along the way, the model can call validate_change as a quick self-check, but at the end the harness throws that report away and runs PipelineGate.verify() from scratch on the active branch. The method signature doesn't even accept a model verdict, and a dedicated test feeds the agent a 'lying' mock tool to verify the gate still returns False.
In kubemend/tools/gitops/validator.py, verification runs through five stages.
| Stage | Tool | What It Catches |
|---|---|---|
| 1. Chart render | helm template (pinned binary) | Broken YAML and template errors before hitting the cluster |
| 2. Admission policies | kyverno apply + rule counter | Dangerous manifests and silent policy skips |
| 3. Live diff | argocd app diff / kubectl diff | Empty edits with no real changes |
| 4. Access boundaries | out_of_scope check | Keeps the patch inside one (namespace, app) |
| 5. Quota headroom | Live ResourceQuota check | Blocks fixes that leave pods stuck in Pending |
On paper, the design looks airtight. Trouble showed up when Maciej ran each scenario five times in a row against a live cluster: the validator itself could silently lie to both the model and the reviewer.
Zero Rules Evaluated and a Green Exit Code 0 in Kyverno
The first trap hid in stage two. Admission policies — rules like banning privileged containers or :latest tags — are enforced by Kyverno CLI, which validates rendered manifests locally via kyverno apply.
In early runs, the gate checked only the exit code. Then kyverno apply exited with exit code 0 while printing: pass: 0, fail: 0, warn: 0, error: 0, skip: 0. Zero rules evaluated! The rendered manifests didn't match the namespace selector tied to the policies. Kyverno found no matching objects and calmly returned zero — by design, the CLI exits 0 whenever fail == 0 and error == 0, even with pass: 0 (flags like --warn-exit-code and --warn-no-pass exist for strict mode).
From the outside, that run looks identical to six passing policies. A check that silently checks nothing creates a dangerous illusion of safety. The stage was switched to fail-closed mode: any ambiguity or harness failure counts as a failed check.
def _policy(self, rendered: str) -> CheckResult:
# Run kyverno apply against the temporary manifest
cmd = [
str(self.kyverno_bin), "apply", str(self.policies_dir),
"--resource", str(manifest_file), "--audit-warn=false",
]
result = self.runner.run(cmd, cwd=self.repo_path)
if not result.ok:
err = _policy_failures(result.stdout, result.stderr)
return CheckResult("kyverno", False, err)
# Fail-closed: 0 evaluated rules means a harness fault
if _policy_evaluated(result.stdout) == 0:
return CheckResult(
name="kyverno", passed=False,
detail="no_policies_applied: harness fault",
)
return CheckResult("kyverno", True, _policy_summary(result.stdout))
def _policy_evaluated(stdout: str) -> int:
# Sum all rule counters from Kyverno output
total = 0
for label in ("pass", "fail", "warn", "error", "skip"):
match = re.search(rf"{label}:\s*(\d+)", stdout)
if match:
total += int(match.group(1))
return total
The parser adds up all five counters from stdout. On zero, the gate returns False with no_policies_applied, explicitly flagging a harness fault. In a normal code review, the Kyverno call looked spotless — only repeated live runs caught the bug.
If a CLI tool returns 0 when zero rules match, checking returncode == 0 will wave broken patches through as green. Always verify that at least one rule actually ran, or enable flags like --warn-no-pass.
How spec.replicas Broke ResourceQuota Checks — and Why Fixing It Helped the Model
The second bug was even sneakier. A namespace has a ResourceQuota capping it at four pods. A broken Deployment asks for six replicas. In Kubernetes, the quota blocks creating the Pod objects with 403 Forbidden, while the Deployment with the inflated count stays in the cluster. The first four gate stages stay green, even though the extra pods sit in Pending forever.
So a fifth stage was added: checking proposed replicas against live quota headroom. Once tests ran, the model's pass rate on this scenario dropped to zero. The agent kept proposing replicaCount=4 under a four-pod quota even with another service running alongside it — and the new gate cheerfully approved the patch.
The bug lived in the gap between two Deployment fields. spec.replicas holds the desired pod count (the broken six), while status.replicas holds how many pods the Deployment actually owns right now. The validator subtracted spec.replicas from used quota, drove the math negative (4 - 6 = -2), and let an unworkable four-replica patch slip under the cap.
def _quota_headroom(
self, namespace: str, name: str, proposed: int
) -> CheckResult | None:
for quota in self.kube.list_resource("resourcequota", namespace):
hard = (quota.get("spec") or {}).get("hard", {}).get("pods")
if hard is None:
continue
used = int(
(quota.get("status") or {}).get("used", {}).get("pods", 0)
)
live = self.kube.get_resource("deployment", namespace, name)
# Use status.replicas (actual), not spec.replicas (desired)
current = int((live.get("status") or {}).get("replicas", 0))
# Account for both live neighbor pods and their desired replicas
other = max(
used - current,
self._desired_by_others(namespace, name),
)
if other + proposed > int(hard):
return CheckResult(
name="quota",
passed=False,
detail=(
f"{name}: {other + proposed} pods > hard.pods={hard} "
f"(other workloads already use {other})"
),
)
return None
Switching to status.replicas and taking max(...) with neighbors' desired replicas fixed the math. That's when the paradox from the intro kicked in: the model's pass rate jumped above its original score. The model didn't get smarter — validate_change simply stopped lying and began returning the exact rejection reason: neighboring workloads already used one pod. With a clear signal, the agent recalculated replicas on its own. A lying validator misleads an LLM just as a broken runbook misleads an on-call engineer at 3 a.m.
What 30 Live Cluster Runs Cost and Where the Agent Stops
Both bugs surfaced during eval runs — automated checks of benchmark failures with repeats. The v0.1-baseline benchmark ran six scenarios five times each on claude-sonnet-5 against a live cluster and cost $11.08 ($0.26–$0.56 per run).
kubemend Baseline Eval Results (6 Scenarios × 5 Runs)
The only failure happened in one of the five bad-probe-path runs: the agent looped between propose_git_change and validate_change until it hit budget_exhausted. That was the language model hitting its limit while the gate did its job. A single green run isn't enough for systems like this: it proves luck, not that silent holes in the checks are closed.
kubemend v0.1 keeps tight boundaries. It works strictly inside a single (namespace, app) pair and edits only Helm values*.yaml, handing anything more complex to a human in a written report. It has no memory across runs and no alert-driven triggers — you invoke it manually. Protection against prompt injection in logs (such as an attacker planting ignore previous instructions) currently lives only in the system prompt, with live adversarial testing left for the next phase.




