fix(organize): name the cause in a failed watch cleanup line

Every RESOURCE CLEANUP run reported failed (1) for the nested docker
images and builder prune of the production and core stacks with no
reason, because run_step()'s standard error was dropped. A failed line
now ends with the last non-empty stderr line, cut to CAUSE_LIMIT (160)
characters, for the leftover listing, each rmi and every worktree step.
The project lookup itself works from / (docker compose -p production ps
lists the services), so the cause sits inside the exec.

🎯 Fix: 93 %
💥 Break: 2 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 91
🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 %

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-28 09:12:50 +00:00
1 parent 0b4e3d3e6a
commit f7a2586b49
2 files changed
+32 -11

No files matched your search

+19 -6
View File
@@ -2775,8 +2775,20 @@ def run_step(command: str | list[str], shell: bool, env: dict[str, str] | None =
return done.returncode, done.stdout, done.stderr return done.returncode, done.stdout, done.stderr
def result_line(code: int | str, shown: str) -> str: CAUSE_LIMIT = 160
return f"{OK if code == 0 else f'failed ({code}): '}{shown}"
def result_line(code: int | str, shown: str, err: bytes = b"") -> str:
"""Args:
code: run_step()'s exit code or reason.
shown: the command as the line names it.
err: its standard error; a failed line ends with its last non-empty
line, cut to CAUSE_LIMIT characters, so the alert names the cause.
"""
if code == 0:
return f"{OK}{shown}"
cause = next((line.strip() for line in reversed(err.decode(errors="replace").splitlines()) if line.strip()), "")
return f"failed ({code}): {shown}{f' ({cause[:CAUSE_LIMIT]})' if cause else ''}"
def shown(prefix: list[str], args: list[str]) -> str: def shown(prefix: list[str], args: list[str]) -> str:
@@ -2794,13 +2806,13 @@ def remove_leftovers(prefix: list[str]) -> list[str]:
pull by digest keeps both and stays, an image a container uses is pull by digest keeps both and stays, an image a container uses is
`kept (in use)`. `kept (in use)`.
""" """
code, out, _ = run_step([*prefix, *LEFTOVERS], False) code, out, err = run_step([*prefix, *LEFTOVERS], False)
lines = [result_line(code, shown(prefix, LEFTOVERS))] lines = [result_line(code, shown(prefix, LEFTOVERS), err)]
rows = (row.split() for row in out.decode(errors="replace").splitlines()) if code == 0 else () rows = (row.split() for row in out.decode(errors="replace").splitlines()) if code == 0 else ()
for image in dict.fromkeys(fields[0] for fields in rows if fields[1:] == [NONE, NONE] and IMAGE_ID.fullmatch(fields[0])): for image in dict.fromkeys(fields[0] for fields in rows if fields[1:] == [NONE, NONE] and IMAGE_ID.fullmatch(fields[0])):
code, _, err = run_step([*prefix, "rmi", image], False) code, _, err = run_step([*prefix, "rmi", image], False)
in_use = code != 0 and IN_USE.search(err.decode(errors="replace")) is not None in_use = code != 0 and IN_USE.search(err.decode(errors="replace")) is not None
lines.append(KEPT_LINE + shown(prefix, ["rmi", image]) if in_use else result_line(code, shown(prefix, ["rmi", image]))) lines.append(KEPT_LINE + shown(prefix, ["rmi", image]) if in_use else result_line(code, shown(prefix, ["rmi", image]), err))
return lines return lines
@@ -2858,7 +2870,8 @@ def worktree_step(argv: list[str]) -> list[str]:
text = " ".join(argv) text = " ".join(argv)
if WORKTREE_REMOVAL.search(text) or WORKTREE_REMOVAL.search(QUOTING.sub("", text)): if WORKTREE_REMOVAL.search(text) or WORKTREE_REMOVAL.search(QUOTING.sub("", text)):
return [f"refused ({REFUSALS['nested']}): {shlex.join(argv)}"] return [f"refused ({REFUSALS['nested']}): {shlex.join(argv)}"]
return [result_line(run_step(argv, False)[0], shlex.join(argv))] code, _, err = run_step(argv, False)
return [result_line(code, shlex.join(argv), err)]
def run_cleanup(cfg: dict) -> list[str]: def run_cleanup(cfg: dict) -> list[str]:
+13 -5
View File
@@ -1930,10 +1930,9 @@ class TestImagePrune(unittest.TestCase):
(self.base / "in-use.txt").write_text("aaaaaaaaaaaa\n", encoding="utf-8") (self.base / "in-use.txt").write_text("aaaaaaaaaaaa\n", encoding="utf-8")
(self.base / "gone.txt").write_text("dddddddddddd\n", encoding="utf-8") (self.base / "gone.txt").write_text("dddddddddddd\n", encoding="utf-8")
results = self._cleanup("docker image prune -f", "docker builder prune -f") results = self._cleanup("docker image prune -f", "docker builder prune -f")
self.assertEqual(results[1:], [ gone = "failed (1): docker rmi dddddddddddd (Error response from daemon: No such image: dddddddddddd)"
"kept (in use): docker rmi aaaaaaaaaaaa", "failed (1): docker rmi dddddddddddd", "ok: docker builder prune -f", self.assertEqual(results[1:], ["kept (in use): docker rmi aaaaaaaaaaaa", gone, "ok: docker builder prune -f"])
]) self.assertEqual(self.watch.cleanup_failures(results), [gone])
self.assertEqual(self.watch.cleanup_failures(results), ["failed (1): docker rmi dddddddddddd"])
held = "Error response from daemon: conflict: unable to delete f18bae17841e" held = "Error response from daemon: conflict: unable to delete f18bae17841e"
for err, in_use in ( for err, in_use in (
(f"{held} (must be forced) - image is being used by stopped container 3c9a\n", True), (f"{held} (must be forced) - image is being used by stopped container 3c9a\n", True),
@@ -1947,10 +1946,19 @@ class TestImagePrune(unittest.TestCase):
with self.subTest(err=err): with self.subTest(err=err):
self.assertEqual(self.watch.IN_USE.search(err) is not None, in_use) self.assertEqual(self.watch.IN_USE.search(err) is not None, in_use)
def test_a_failed_line_names_the_last_error_line_cut_to_its_limit(self):
cause = "service \"infinito\" is not running"
err = f"WARNING: config\n{cause}\n\n".encode()
self.assertEqual(self.watch.result_line(1, "docker images", err), f"failed (1): docker images ({cause})")
self.assertEqual(self.watch.result_line(1, "docker images"), "failed (1): docker images")
self.assertEqual(self.watch.result_line(0, "docker images", err), "ok: docker images")
long = self.watch.result_line(1, "x", b"e" * 500)
self.assertEqual(long, f"failed (1): x ({'e' * self.watch.CAUSE_LIMIT})")
def test_an_unreachable_daemon_is_reported_and_removes_nothing(self): def test_an_unreachable_daemon_is_reported_and_removes_nothing(self):
(self.base / "images.txt").unlink() (self.base / "images.txt").unlink()
results = self._cleanup("docker image prune -f", "docker builder prune -f") results = self._cleanup("docker image prune -f", "docker builder prune -f")
self.assertEqual(results, [f"failed (1): {shlex.join(['docker', *LISTED])}", "ok: docker builder prune -f"]) self.assertEqual(results, [f"failed (1): {shlex.join(['docker', *LISTED])} (Cannot connect to the Docker daemon)", "ok: docker builder prune -f"])
self.assertEqual(self._calls(), [LISTED, ["builder", "prune", "-f"]]) self.assertEqual(self._calls(), [LISTED, ["builder", "prune", "-f"]])
def test_a_missing_docker_binary_is_reported_not_raised(self): def test_a_missing_docker_binary_is_reported_not_raised(self):