fix(ai): magic.from_file() raises MagicException, only OSError is caught — can crash dir loop #81

Open
opened 2026-04-18 20:23:43 -06:00 by claude-code · 0 comments
Collaborator

Problem

Two call sites wrap magic.from_file() in try/except OSError but magic.MagicException (the python-magic library's error type) is not an OSError subclass. On any libmagic failure the exception propagates and crashes the dir loop — violating "Graceful degradation everywhere" (CLAUDE.md).

Site 1 — _build_dir_context (runs on every directory before the loop starts)

luminos_lib/ai.py:897-916:

def _build_dir_context(dir_path):
    lines = []
    try:
        entries = sorted(os.listdir(dir_path))
        for name in entries:
            ...
            try:
                st = os.stat(full)
                if os.path.isdir(full):
                    lines.append(...)
                else:
                    mime = magic.from_file(full, mime=True)   # ← can raise MagicException
                    lines.append(...)
            except OSError:
                lines.append(f"  {name}  (stat failed)")
    except OSError:
        lines.append("  (could not list directory)")

Site 2 — _tool_list_directory (dir-loop tool)

luminos_lib/ai.py:229-257 — same pattern.

Repro

Sufficient to trigger on some systems: a file that libmagic can't classify (corrupted, permission-exotic, or a version mismatch between the libmagic binary and the grammar db). Also triggers if libmagic is missing on the host despite python-magic being installed.

Fix

Broaden the inner except OSError: to except Exception: at both sites, or wrap the magic.from_file() call in its own narrow try and degrade to an empty MIME string on any failure. A file listing without MIME is a degraded-but-correct result; crashing the whole dir investigation is not.

Acceptance

  • _build_dir_context survives a file that magic.from_file() can't classify
  • _tool_list_directory survives the same case
  • Failed MIME lookup shows the file without the [mime] suffix, not "(stat failed)"
## Problem Two call sites wrap `magic.from_file()` in `try/except OSError` but `magic.MagicException` (the `python-magic` library's error type) is **not** an `OSError` subclass. On any libmagic failure the exception propagates and crashes the dir loop — violating "Graceful degradation everywhere" (CLAUDE.md). ### Site 1 — `_build_dir_context` (runs on every directory before the loop starts) `luminos_lib/ai.py:897-916`: ```python def _build_dir_context(dir_path): lines = [] try: entries = sorted(os.listdir(dir_path)) for name in entries: ... try: st = os.stat(full) if os.path.isdir(full): lines.append(...) else: mime = magic.from_file(full, mime=True) # ← can raise MagicException lines.append(...) except OSError: lines.append(f" {name} (stat failed)") except OSError: lines.append(" (could not list directory)") ``` ### Site 2 — `_tool_list_directory` (dir-loop tool) `luminos_lib/ai.py:229-257` — same pattern. ## Repro Sufficient to trigger on some systems: a file that libmagic can't classify (corrupted, permission-exotic, or a version mismatch between the libmagic binary and the grammar db). Also triggers if libmagic is missing on the host despite `python-magic` being installed. ## Fix Broaden the inner `except OSError:` to `except Exception:` at both sites, or wrap the `magic.from_file()` call in its own narrow try and degrade to an empty MIME string on any failure. A file listing without MIME is a degraded-but-correct result; crashing the whole dir investigation is not. ## Acceptance - [ ] `_build_dir_context` survives a file that `magic.from_file()` can't classify - [ ] `_tool_list_directory` survives the same case - [ ] Failed MIME lookup shows the file without the `[mime]` suffix, not "(stat failed)"
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: archeious/luminos#81
No description provided.