diff --git a/fixos/cli/anomalies_cmd.py b/fixos/cli/anomalies_cmd.py index 0a532c1..41114fc 100644 --- a/fixos/cli/anomalies_cmd.py +++ b/fixos/cli/anomalies_cmd.py @@ -98,6 +98,11 @@ def anomalies(json_output, min_size_gb, min_age_days, remove, remove_safe, dry_r if removal: for item in removal["removed"]: click.echo(f"usunięto: {item['path']}") + for item in removal["partial"]: + click.echo( + f"częściowo: {item['path']} (pozostało " + f"{_format_bytes(item['remaining_bytes'])}, {item['reason']})" + ) for item in removal["refused"]: click.echo(f"pominięto: {item['path']} ({item['reason']})") click.echo(f"Zwolniono: {_format_bytes(removal['freed_bytes'])}") diff --git a/fixos/diagnostics/host_anomalies.py b/fixos/diagnostics/host_anomalies.py index e13bdec..b940736 100644 --- a/fixos/diagnostics/host_anomalies.py +++ b/fixos/diagnostics/host_anomalies.py @@ -14,13 +14,15 @@ import json import os import shutil +import stat import subprocess +import sys import time from collections import Counter from collections.abc import Callable, Sequence from pathlib import Path -from fixos.diagnostics.artifact_store import is_protected_path +from fixos.diagnostics.artifact_store import is_protected_path, measure_tree SCHEMA = "fixos.host-anomalies/v1" RECEIPT_SCHEMA = "fixos.host-anomaly-removal/v1" @@ -448,6 +450,35 @@ def scan_all( } +def _remove_tree(path: Path) -> int | None: + """Remove *path*; return the errno of the first entry that could not go. + + Trees copied from read-only stores keep directories without the owner + write bit, so an entry owned by this user is made writable and retried + once. Entries owned by somebody else stay, and the caller reports what is + left instead of claiming either success or an untouched directory. + """ + first_error: list[int] = [] + + def retry(function, target, error) -> None: + if isinstance(error, tuple): # Python < 3.12 passes sys.exc_info() + error = error[1] + try: + for entry in (os.path.dirname(target), target): + if os.lstat(entry).st_uid == os.getuid() and not os.path.islink(entry): + os.chmod(entry, os.lstat(entry).st_mode | stat.S_IRWXU) + function(target) + except OSError as again: + if not first_error: + first_error.append(again.errno or getattr(error, "errno", 0) or 0) + + if sys.version_info >= (3, 12): + shutil.rmtree(path, onexc=retry) + else: + shutil.rmtree(path, onerror=retry) + return first_error[0] if first_error else None + + def remove_paths( paths: Sequence[Path], findings: Sequence[dict], @@ -455,10 +486,14 @@ def remove_paths( home: Path | None = None, receipt_dir: Path | None = None, ) -> dict: - """Delete only paths a current scan marks removable; write one receipt.""" + """Delete only paths a current scan marks removable; write one receipt. + + A tree that could only be removed in part is listed under ``partial`` with + the bytes that remain, never under ``removed`` or ``refused``. + """ home = home or Path.home() allowed = {f["subject"]: f for f in findings if f["removable"]} - removed, refused = [], [] + removed, refused, partial = [], [], [] for path in paths: finding = allowed.get(str(path)) reason = None @@ -473,26 +508,36 @@ def remove_paths( if reason: refused.append({"path": str(path), "reason": reason}) continue + size = finding["evidence"].get("size_bytes", 0) try: - shutil.rmtree(path) + errno_left = _remove_tree(path) except OSError as error: - refused.append({"path": str(path), "reason": f"os_error:{error.errno}"}) + errno_left = error.errno or 0 + if not path.exists(): + removed.append( + {"path": str(path), "category": finding["category"], "size_bytes": size} + ) continue - removed.append( + remaining = measure_tree(path) + partial.append( { "path": str(path), "category": finding["category"], - "size_bytes": finding["evidence"].get("size_bytes", 0), + "reason": f"os_error:{errno_left}", + "remaining_bytes": remaining, + "freed_bytes": max(size - remaining, 0), } ) receipt = { "schema": RECEIPT_SCHEMA, "created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "removed": removed, + "partial": partial, "refused": refused, - "freed_bytes": sum(item["size_bytes"] for item in removed), + "freed_bytes": sum(item["size_bytes"] for item in removed) + + sum(item["freed_bytes"] for item in partial), } - if receipt_dir is not None and removed: + if receipt_dir is not None and (removed or partial): receipt_dir.mkdir(parents=True, exist_ok=True) name = f"host-anomaly-removal-{int(time.time())}.json" (receipt_dir / name).write_text( diff --git a/project/ticket-138/README.md b/project/ticket-138/README.md new file mode 100644 index 0000000..a1e649e --- /dev/null +++ b/project/ticket-138/README.md @@ -0,0 +1,34 @@ +# Ticket 138: Report partial removals truthfully and remove read-only directories in fixos anomalies + +- **ID**: ticket-138 +- **Owner**: unresolved:human +- **Status**: IN_PROGRESS +- **Workflow state**: EDIT +- **Created**: 2026-11-16 + +## Goal and scope + +First live use of `fixos anomalies --remove` (2026-11-16) removed four of seven +selected directories. The other three failed with EACCES and were reported as +`pominięto … (os_error:13)`, although `shutil.rmtree` had already deleted part +of each: 2.8, 2.6 and 1.1 GiB trees were left at 1950, 478 and 656 MiB. One of +them held 227 read-only directories owned by the user; the other two held 10 +and 12 files owned by another account. + +Scope: make user-owned read-only entries writable and retry once; report a +tree that still exists after the attempt under a new `partial` list with the +reason and remaining bytes, and count the bytes actually freed. + +## Acceptance criteria + +- [x] AC-01: A tree with read-only directories owned by the user is removed completely. +- [x] AC-02: A tree with an entry that cannot be unlinked is reported under + `partial` with `remaining_bytes`; it is in neither `removed` nor `refused`, + and a receipt is written. +- [x] AC-03: 12 unit tests, `ruff check` and `ruff format --check` pass. +- [ ] AC-04: Governance check and exact-head review pass. + +## Tracking boundary + +This directory contains the minimal reviewed intent. Optional participant prose +and raw command logs are not required delivery output. diff --git a/project/ticket-138/intent.json b/project/ticket-138/intent.json new file mode 100644 index 0000000..9762d9a --- /dev/null +++ b/project/ticket-138/intent.json @@ -0,0 +1,90 @@ +{ + "schema": "new-project.intent/v3", + "ticket": "ticket-138", + "summary": "Report partial removals truthfully and remove read-only directories in fixos anomalies", + "workstream": "application", + "classification": { + "kind": "BUG", + "priority": "P2", + "origin": "requested" + }, + "allowedPaths": [ + "project/ticket-138/**", + "TODO.md", + "project/TICKETS.md", + "fixos/cli/anomalies_cmd.py", + "fixos/diagnostics/host_anomalies.py", + "tests/unit/test_host_anomalies.py" + ], + "forbiddenPaths": [ + "project/ticket-*/user-*.md" + ], + "stacks": [], + "dependsOn": [], + "conflictsWith": [], + "integrationTicket": null, + "delivery": { + "acceptedBaseSha": "3cd0003ac81a7c455a37ccb63d610f85204099cd", + "targetBranch": "main", + "outcome": "fixos anomalies removes trees that contain read-only directories owned by the user and reports a partially removed tree with its remaining bytes instead of 'refused'.", + "nonGoals": [ + "No privilege escalation; entries owned by another user stay and are reported.", + "No change to detection or to which paths are removable." + ], + "complexity": "M", + "estimatedMinutes": 30, + "budgets": { + "maxImplementationFiles": 3, + "maxAffectedComponents": 2, + "maxPublicInterfaceChanges": 1, + "maxRuntimeDependencies": 0 + }, + "architecture": { + "status": "accepted", + "decision": "Wrap shutil.rmtree with a handler that makes user-owned entries writable and retries once; after removal classify by whether the path still exists and add a 'partial' list to the receipt.", + "components": [ + { + "name": "diagnostics", + "paths": [ + "fixos/diagnostics/host_anomalies.py", + "tests/unit/test_host_anomalies.py" + ] + }, + { + "name": "cli", + "paths": [ + "fixos/cli/anomalies_cmd.py" + ] + } + ], + "responsibilityChanges": false, + "interfaceChanges": [ + "fixos.host-anomaly-removal/v1 receipt gains a 'partial' list; freed_bytes includes partially freed bytes" + ], + "dataChanges": [], + "ui": { + "impact": "none", + "states": [], + "evidence": [] + }, + "rollback": "Revert the commit." + }, + "runtimeDependencies": [], + "validation": [ + { + "criterion": "AC-01", + "commands": [ + "python -m pytest tests/unit/test_host_anomalies.py -q" + ], + "evidence": "12 unit tests" + }, + { + "criterion": "AC-02", + "commands": [ + "./project/governance-check.sh" + ], + "evidence": "governance-check output" + } + ] + } +} diff --git a/tests/unit/test_host_anomalies.py b/tests/unit/test_host_anomalies.py index 3c69327..be48fa9 100644 --- a/tests/unit/test_host_anomalies.py +++ b/tests/unit/test_host_anomalies.py @@ -252,3 +252,47 @@ def test_cli_reports_and_dry_run_removes_nothing(tmp_path, monkeypatch): assert result.exit_code == 0, result.output assert json.loads(result.output)["would_remove"] == [str(target)] assert target.exists() + + +def test_remove_paths_removes_trees_with_read_only_directories(tmp_path): + home = tmp_path + target = home / ".local/state/tool/copied-store" + _write(target / "locked/deep/data.bin") + for directory in (target / "locked/deep", target / "locked"): + directory.chmod(0o555) + findings = [ha._finding("stale-state", "review", target, "x", size_bytes=10)] + + receipt = ha.remove_paths([target], findings, home=home) + + assert not target.exists() + assert [item["path"] for item in receipt["removed"]] == [str(target)] + assert receipt["partial"] == [] and receipt["refused"] == [] + + +def test_remove_paths_reports_partial_removal_with_remaining_bytes( + tmp_path, monkeypatch +): + home = tmp_path + target = home / ".local/state/tool/mixed" + _write(target / "mine.bin", 40) + foreign = _write(target / "foreign/kept.bin", 60) + real_unlink = os.unlink + + def unlink(path, *args, **kwargs): + if str(path).endswith("kept.bin"): + raise PermissionError(13, "Permission denied", str(path)) + return real_unlink(path, *args, **kwargs) + + monkeypatch.setattr(os, "unlink", unlink) + findings = [ha._finding("stale-state", "review", target, "x", size_bytes=100)] + + receipt = ha.remove_paths( + [target], findings, home=home, receipt_dir=home / "receipts" + ) + + assert foreign.exists() and not (target / "mine.bin").exists() + assert receipt["removed"] == [] and receipt["refused"] == [] + assert receipt["partial"][0]["reason"] == "os_error:13" + assert 0 < receipt["partial"][0]["remaining_bytes"] < 100 * 1024 + assert receipt["freed_bytes"] == receipt["partial"][0]["freed_bytes"] + assert Path(receipt["receipt_path"]).is_file()