Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(ultrawiki): read the text out of every document format, not just markdown

Ruben Lütkec7da7bd45fce
diff --git a/jarvis/ultrawiki/document_text.py b/jarvis/ultrawiki/document_text.py
index 9169312c1b..57f923e0b9 100644
--- a/jarvis/ultrawiki/document_text.py
+++ b/jarvis/ultrawiki/document_text.py
@@ -204,7 +204,7 @@ class _RefusedEntity(ValueError):
"""Raised from the parser callback when a document declares an entity."""
-def _safe_parser() -> ET.XMLParser:
+def safe_xml_parser() -> ET.XMLParser:
"""An ElementTree parser that refuses to declare or fetch any entity.
The stdlib parser expands internal entities, which is the whole mechanism
@@ -242,6 +242,12 @@ def _safe_parser() -> ET.XMLParser:
return parser
+#: Public alias. ``jarvis.ultrawiki.extract`` parses XML out of the same
+#: untrusted office archives and must use the SAME hardening — a second copy of
+#: a security primitive is a second thing to forget to update.
+_safe_parser = safe_xml_parser
+
+
def _element_text(root: ET.Element) -> str:
"""Walk the tree, joining text leaves and honoring paragraph breaks."""
pieces: list[str] = []
diff --git a/jarvis/ultrawiki/extract.py b/jarvis/ultrawiki/extract.py
new file mode 100644
index 0000000000..383c87af26
--- /dev/null
+++ b/jarvis/ultrawiki/extract.py
@@ -0,0 +1,673 @@
+"""Get the readable text out of one file, whatever format it is in.
+
+The gap this closes (audit, 2026-07-26): every reader that met a binary gave
+up. Google Drive produced no item at all for a PDF or a Word document, Dropbox
+had an extension allowlist that excluded exactly the formats people keep their
+thinking in, Gmail never downloaded an attachment, and the local-folder walk
+read `.md` and `.txt` and nothing else. A contract, a slide deck, a scanned
+invoice, a spreadsheet of decisions — all invisible to a system whose whole
+purpose is remembering them.
+
+One service, so a format is supported once rather than per reader.
+
+**Almost all of it is standard library, on purpose.** Word, Excel, PowerPoint,
+OpenDocument and EPUB files are ZIP archives containing XML; `zipfile` plus
+`xml.etree` reads them without `python-docx`, `openpyxl`, `python-pptx`,
+`odfpy` or `ebooklib`. That is not cleverness for its own sake — every avoided
+dependency is one that cannot fail to build a wheel on the maintainer's Intel
+Mac, cannot bloat a `python:3.11-slim` image, and cannot break a base install
+(repo rule §3). PDF is the one exception: it needs `pypdf`, which is already a
+dependency.
+
+**Never raises, always answers.** A corrupt file, a missing optional
+dependency, an encrypted PDF and a JPEG are four different outcomes and each
+returns an :class:`ExtractResult` saying which. A reader stores the item
+regardless and marks `content_missing`, so a later run — with the extra
+installed, or after the file is repaired — can reclaim it instead of the item
+silently never existing.
+
+**Format is decided by content first, name second.** Magic bytes do not lie;
+extensions do, constantly (`.txt` holding a PDF, a `.docx` that is really a
+ZIP of something else, no extension at all on a Drive export).
+
+Relationship to :mod:`jarvis.ultrawiki.document_text`: that module is the
+folder walk's path-based helper for the three formats a notes folder actually
+holds. This one is the general service — ten formats, and it accepts BYTES,
+which is what an email attachment, a Slack upload or a Drive export is before
+it ever touches disk. They share the hardened XML parser rather than each
+keeping a copy: office archives arrive from strangers, and a second copy of a
+security primitive is a second thing to forget to update.
+"""
+
+from __future__ import annotations
+
+import io
+import json
+import logging
+import re
+import zipfile
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import Any
+from xml.etree import ElementTree
+
+from jarvis.ultrawiki.document_text import safe_xml_parser
+
+log = logging.getLogger(__name__)
+
+__all__ = [
+ "ExtractResult",
+ "TEXT_EXTENSIONS",
+ "detect_kind",
+ "extract_text",
+ "is_probably_text",
+]
+
+#: Bytes read to identify a file by content.
+_SNIFF_BYTES = 8192
+
+#: A single extracted document is bounded only against pathological input —
+#: the chunker handles length, so this is not a content cap. 64 MB of TEXT is
+#: about 30 000 pages; beyond it the file is a database, not a document.
+_MAX_TEXT_CHARS = 64 * 1024 * 1024
+
+#: Leading magic → kind. Order matters: ZIP-based office formats share the
+#: `PK` signature and are separated afterwards by their inner layout.
+_MAGIC: tuple[tuple[bytes, str], ...] = (
+ (b"%PDF-", "pdf"),
+ (b"{\\rtf", "rtf"),
+ (b"\x89PNG\r\n\x1a\n", "image"),
+ (b"\xff\xd8\xff", "image"),
+ (b"GIF87a", "image"),
+ (b"GIF89a", "image"),
+ (b"BM", "image"),
+ (b"\x00\x00\x01\x00", "image"),
+ (b"OggS", "audio"),
+ (b"ID3", "audio"),
+ (b"fLaC", "audio"),
+ (b"\x1f\x8b", "gzip"),
+ (b"7z\xbc\xaf\x27\x1c", "archive"),
+ (b"Rar!\x1a\x07", "archive"),
+ (b"\xd0\xcf\x11\xe0", "legacy_office"), # .doc/.xls/.ppt (OLE2)
+ (b"SQLite format 3\x00", "binary"),
+ (b"\x7fELF", "binary"),
+ (b"MZ", "binary"),
+)
+
+#: Inner ZIP members that identify an office format.
+_ZIP_MARKERS: tuple[tuple[str, str], ...] = (
+ ("word/document.xml", "docx"),
+ ("xl/workbook.xml", "xlsx"),
+ ("ppt/presentation.xml", "pptx"),
+ ("content.xml", "odf"),
+ ("META-INF/container.xml", "epub"),
+)
+
+#: Extensions whose contents are text even when they look unfamiliar. Not an
+#: allowlist that decides what gets imported — a hint for the sniffer. Anything
+#: that decodes cleanly is treated as text regardless of its name.
+TEXT_EXTENSIONS: frozenset[str] = frozenset(
+ {
+ # prose + notes
+ ".txt", ".text", ".md", ".markdown", ".rst", ".org", ".adoc", ".tex",
+ ".log", ".srt", ".vtt", ".sub",
+ # data + config
+ ".json", ".jsonl", ".ndjson", ".yaml", ".yml", ".toml", ".ini", ".cfg",
+ ".conf", ".properties", ".env", ".csv", ".tsv", ".tab", ".xml", ".plist",
+ # code
+ ".py", ".pyi", ".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".rs",
+ ".go", ".java", ".kt", ".kts", ".scala", ".swift", ".c", ".h", ".cc",
+ ".cpp", ".hpp", ".cs", ".rb", ".php", ".pl", ".lua", ".r", ".jl",
+ ".dart", ".ex", ".exs", ".erl", ".hs", ".clj", ".sql", ".sh", ".bash",
+ ".zsh", ".fish", ".ps1", ".psm1", ".bat", ".cmd", ".vim", ".el",
+ ".gradle", ".groovy", ".m", ".mm", ".f90", ".asm", ".s",
+ # web + templates
+ ".html", ".htm", ".xhtml", ".css", ".scss", ".sass", ".less", ".vue",
+ ".svelte", ".astro", ".hbs", ".ejs", ".jinja", ".j2", ".twig",
+ # build + infra
+ ".dockerfile", ".tf", ".tfvars", ".gitignore", ".gitattributes",
+ ".editorconfig", ".graphql", ".gql", ".proto", ".patch", ".diff",
+ }
+)
+
+#: Files whose NAME says binary regardless of what decodes.
+_BINARY_EXTENSIONS: frozenset[str] = frozenset(
+ {
+ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".tiff", ".tif",
+ ".ico", ".svgz", ".psd", ".ai", ".heic", ".avif",
+ ".mp3", ".wav", ".flac", ".ogg", ".m4a", ".aac", ".wma",
+ ".mp4", ".mov", ".avi", ".mkv", ".webm", ".wmv", ".flv",
+ ".zip", ".gz", ".bz2", ".xz", ".7z", ".rar", ".tar", ".tgz", ".jar",
+ ".exe", ".dll", ".so", ".dylib", ".bin", ".dat", ".db", ".sqlite",
+ ".woff", ".woff2", ".ttf", ".otf", ".eot",
+ ".pyc", ".pyo", ".class", ".o", ".a", ".lib", ".wasm",
+ }
+)
+
+_TAG_RE = re.compile(r"<[^>]+>")
+_WS_RE = re.compile(r"[ \t]{2,}")
+_BLANKS_RE = re.compile(r"\n{3,}")
+_RTF_CONTROL_RE = re.compile(r"\\[a-zA-Z]+-?\d* ?|[{}]|\\\n")
+
+
+@dataclass(slots=True)
+class ExtractResult:
+ """What one file yielded, and honestly why when it yielded nothing.
+
+ ``ok`` false with a non-empty ``reason`` is a first-class outcome, not an
+ error: an image has no text to give, and saying so lets the caller store
+ the item with ``content_missing`` rather than drop it.
+ """
+
+ text: str = ""
+ #: The format actually detected ("pdf", "docx", "text", "image", ...).
+ kind: str = "unknown"
+ ok: bool = False
+ reason: str = ""
+ #: Free-form extras a reader may pass through (page counts, sheet names).
+ meta: dict[str, Any] = field(default_factory=dict)
+
+ @property
+ def content_missing(self) -> bool:
+ """True when text COULD exist but this build/file did not produce it.
+
+ Distinguishes "nothing to extract" (an image) from "we could not
+ extract it" (no pypdf, a corrupt archive) — only the second is worth
+ retrying later.
+ """
+ return not self.ok and self.kind not in ("image", "audio", "video", "empty")
+
+
+def _read(data: bytes | Path) -> bytes:
+ return data.read_bytes() if isinstance(data, Path) else bytes(data)
+
+
+def _head(data: bytes | Path) -> bytes:
+ if isinstance(data, Path):
+ try:
+ with data.open("rb") as handle:
+ return handle.read(_SNIFF_BYTES)
+ except OSError:
+ return b""
+ return bytes(data[:_SNIFF_BYTES])
+
+
+def is_probably_text(head: bytes) -> bool:
+ """Whether a leading sample decodes as text without binary noise.
+
+ A NUL byte is the practical separator: real text files do not contain one,
+ and every binary format of consequence does within its first few KB.
+
+ UTF-8 is not enough on its own. A Windows-authored note is cp1252, an older
+ export is latin-1, and "Gr\u00fc\u00dfe aus M\u00fcnchen" in either one is invalid UTF-8 \u2014
+ a UTF-8-only check calls that file binary and drops it, which is precisely
+ the class of everyday document this service exists to stop losing. So a
+ failed UTF-8 decode falls through to the legacy encodings, and only text
+ that survives NONE of them, or that carries control characters no prose
+ contains, is called binary.
+ """
+ if not head:
+ return False
+ if b"\x00" in head:
+ return False
+ try:
+ head.decode("utf-8")
+ return True
+ except UnicodeDecodeError:
+ pass
+ for encoding in ("cp1252", "latin-1"):
+ try:
+ decoded = head.decode(encoding)
+ except (UnicodeDecodeError, LookupError):
+ continue
+ # Control characters other than the usual whitespace mean binary; real
+ # single-byte text has none.
+ controls = sum(
+ 1 for ch in decoded if ord(ch) < 32 and ch not in "\t\n\r\f\v"
+ )
+ if controls <= len(decoded) * 0.01:
+ return True
+ # A truncated multi-byte character at the sample boundary is normal, so a
+ # mostly-clean lenient decode still counts as text.
+ lenient = head.decode("utf-8", errors="replace")
+ return lenient.count("\ufffd") <= max(2, len(lenient) * 0.02)
+
+
+def _zip_kind(data: bytes | Path) -> str:
+ """Which office format a ZIP actually is, or plain ``archive``."""
+ try:
+ source: Any = data if isinstance(data, Path) else io.BytesIO(data)
+ with zipfile.ZipFile(source) as archive:
+ names = set(archive.namelist())
+ except Exception: # noqa: BLE001 — a broken zip is just not an office file
+ return "archive"
+ for marker, kind in _ZIP_MARKERS:
+ if marker in names:
+ return kind
+ return "archive"
+
+
+def detect_kind(data: bytes | Path, filename: str = "", mime: str = "") -> str:
+ """The file's real format — content first, name second, mime last.
+
+ Extensions lie constantly (a `.txt` holding a PDF, a Drive export with no
+ extension at all), so the magic bytes decide whenever they are conclusive.
+ """
+ head = _head(data)
+ if not head:
+ return "empty"
+ for magic, kind in _MAGIC:
+ if head.startswith(magic):
+ return kind
+ if head.startswith(b"PK\x03\x04"):
+ return _zip_kind(data)
+
+ suffix = Path(filename).suffix.lower() if filename else ""
+ if suffix in _BINARY_EXTENSIONS:
+ return "binary"
+ lowered = (mime or "").lower()
+ if lowered.startswith("image/"):
+ return "image"
+ if lowered.startswith("audio/"):
+ return "audio"
+ if lowered.startswith("video/"):
+ return "video"
+ if lowered == "application/pdf":
+ return "pdf"
+
+ if is_probably_text(head):
+ if suffix in {".html", ".htm", ".xhtml"} or head.lstrip()[:6].lower() in (
+ b"<html>",
+ b"<!doct",
+ ):
+ return "html"
+ return "text"
+ return "binary"
+
+
+def _clean(text: str) -> str:
+ """Collapse the whitespace an extractor leaves behind, keep the structure."""
+ text = text.replace("\r\n", "\n").replace("\r", "\n")
+ text = _WS_RE.sub(" ", text)
+ text = _BLANKS_RE.sub("\n\n", text)
+ return text.strip()[:_MAX_TEXT_CHARS]
+
+
+def _decode(raw: bytes) -> str:
+ """Text from bytes, trying the encodings that actually occur in the wild."""
+ for encoding in ("utf-8", "utf-8-sig", "utf-16", "cp1252", "latin-1"):
+ try:
+ return raw.decode(encoding)
+ except (UnicodeDecodeError, LookupError):
+ continue
+ return raw.decode("utf-8", errors="replace")
+
+
+def _strip_markup(markup: str) -> str:
+ """Tags out, text in — with block elements becoming line breaks.
+
+ Deliberately not an HTML parser: this runs over XML from office archives
+ as well, and all that is wanted is the readable run of text.
+ """
+ markup = re.sub(r"(?is)<(script|style)[^>]*>.*?</\1>", " ", markup)
+ markup = re.sub(
+ r"(?i)</(p|div|br|li|tr|h[1-6]|section|article)\s*/?>", "\n", markup
+ )
+ markup = re.sub(r"(?i)<br\s*/?>", "\n", markup)
+ text = _TAG_RE.sub(" ", markup)
+ for entity, char in (
+ ("&nbsp;", " "), ("&amp;", "&"), ("&lt;", "<"), ("&gt;", ">"),
+ ("&quot;", '"'), ("&#39;", "'"), ("&apos;", "'"),
+ ):
+ text = text.replace(entity, char)
+ return text
+
+
+def _zip_members(data: bytes | Path, predicate: Any) -> list[tuple[str, bytes]]:
+ """Matching members of a ZIP, in archive order. Empty on any failure."""
+ try:
+ source: Any = data if isinstance(data, Path) else io.BytesIO(data)
+ with zipfile.ZipFile(source) as archive:
+ names = [n for n in archive.namelist() if predicate(n)]
+ return [(name, archive.read(name)) for name in names]
+ except Exception: # noqa: BLE001 — a broken archive yields nothing, never raises
+ log.debug("zip member read failed", exc_info=True)
+ return []
+
+
+def _parse_xml(raw: bytes) -> ElementTree.Element | None:
+ """Parse office XML with entity declarations refused. ``None`` on anything.
+
+ Office archives arrive from strangers — a mail attachment, a shared Drive
+ file — so the "billion laughs" entity bomb is a real input, not a
+ theoretical one. The shared parser refuses the DECLARATION rather than
+ trying to bound the expansion, and a DTD in the bytes is refused before
+ parsing starts.
+ """
+ if b"<!DOCTYPE" in raw[:4096] or b"<!ENTITY" in raw[:4096]:
+ log.debug("extract: refusing office XML that declares a DTD or entity")
+ return None
+ try:
+ return ElementTree.fromstring(raw, parser=safe_xml_parser()) # noqa: S314 — hardened
+ except Exception: # noqa: BLE001 — a refused or broken part yields nothing
+ log.debug("extract: office XML part unreadable", exc_info=True)
+ return None
+
+
+def _from_docx(data: bytes | Path) -> ExtractResult:
+ """Word text — `word/document.xml` plus headers, footers and notes.
+
+ A paragraph is `<w:p>`; making each one a line is what keeps a contract's
+ clauses from running into one wall of text.
+ """
+ members = _zip_members(
+ data,
+ lambda n: n == "word/document.xml"
+ or n.startswith(("word/header", "word/footer"))
+ or n in ("word/footnotes.xml", "word/endnotes.xml", "word/comments.xml"),
+ )
+ if not members:
+ return ExtractResult(kind="docx", reason="the document part is unreadable")
+ parts: list[str] = []
+ for _name, raw in members:
+ xml = _decode(raw)
+ xml = re.sub(r"(?i)</w:p>", "\n", xml)
+ xml = re.sub(r"(?i)</w:tr>", "\n", xml)
+ xml = re.sub(r"(?i)</w:tc>", "\t", xml)
+ parts.append(_strip_markup(xml))
+ text = _clean("\n".join(parts))
+ return ExtractResult(
+ text=text,
+ kind="docx",
+ ok=bool(text),
+ reason="" if text else "the document holds no text",
+ )
+
+
+def _from_xlsx(data: bytes | Path) -> ExtractResult:
+ """Every sheet of a workbook, cell values joined per row.
+
+ Shared strings live in one table that the sheets reference by index — a
+ workbook read without resolving it yields numbers and nothing else.
+ """
+ shared: list[str] = []
+ for _name, raw in _zip_members(data, lambda n: n == "xl/sharedStrings.xml"):
+ root = _parse_xml(raw)
+ if root is None:
+ continue
+ for item in root:
+ shared.append("".join(node.text or "" for node in item.iter() if node.text))
+
+ sheets = _zip_members(
+ data, lambda n: n.startswith("xl/worksheets/") and n.endswith(".xml")
+ )
+ if not sheets:
+ return ExtractResult(kind="xlsx", reason="the workbook has no readable sheet")
+ lines: list[str] = []
+ for name, raw in sheets:
+ root = _parse_xml(raw)
+ if root is None:
+ continue
+ lines.append(f"# {Path(name).stem}")
+ for row in root.iter():
+ if not row.tag.endswith("}row") and row.tag != "row":
+ continue
+ cells: list[str] = []
+ for cell in row:
+ kind = cell.get("t")
+ value = "".join(
+ node.text or ""
+ for node in cell.iter()
+ if node.tag.endswith(("}v", "}t")) or node.tag in ("v", "t")
+ )
+ if kind == "s" and value.isdigit():
+ index = int(value)
+ value = shared[index] if index < len(shared) else ""
+ if value:
+ cells.append(value)
+ if cells:
+ lines.append(" | ".join(cells))
+ text = _clean("\n".join(lines))
+ return ExtractResult(
+ text=text,
+ kind="xlsx",
+ ok=bool(text),
+ reason="" if text else "the workbook holds no values",
+ )
+
+
+def _slide_number(name: str) -> int:
+ """The slide's own number — ``slide10`` must not sort before ``slide2``."""
+ match = re.search(r"(\d+)", Path(name).stem)
+ return int(match.group(1)) if match else 0
+
+
+def _from_pptx(data: bytes | Path) -> ExtractResult:
+ """Slide text with each slide's speaker notes directly beneath it.
+
+ Ordering is the whole difficulty. Archive order is arbitrary and plain
+ sorting puts `notesSlides/` before `slides/` and slide 10 before slide 2 —
+ so a deck came out as every note first, in the wrong order, detached from
+ the slide it explains. Notes are where the actual argument lives; separated
+ from their slide they retrieve as orphan sentences.
+ """
+ members = _zip_members(
+ data,
+ lambda n: (n.startswith("ppt/slides/slide") or n.startswith("ppt/notesSlides/"))
+ and n.endswith(".xml"),
+ )
+ if not members:
+ return ExtractResult(kind="pptx", reason="the deck has no readable slide")
+
+ slides: dict[int, str] = {}
+ notes: dict[int, str] = {}
+ for name, raw in members:
+ xml = re.sub(r"(?i)</a:p>", "\n", _decode(raw))
+ body = _strip_markup(xml).strip()
+ if not body:
+ continue
+ target = notes if name.startswith("ppt/notesSlides/") else slides
+ target[_slide_number(name)] = body
+
+ parts: list[str] = []
+ for number in sorted(set(slides) | set(notes)):
+ block = [f"# Slide {number}"] if number else ["# Slide"]
+ if number in slides:
+ block.append(slides[number])
+ if number in notes:
+ block.append(f"Speaker notes: {notes[number]}")
+ parts.append("\n".join(block))
+ text = _clean("\n\n".join(parts))
+ return ExtractResult(
+ text=text,
+ kind="pptx",
+ ok=bool(text),
+ reason="" if text else "the deck holds no text",
+ )
+
+
+def _from_odf(data: bytes | Path) -> ExtractResult:
+ """OpenDocument text, spreadsheet or presentation — all one `content.xml`."""
+ members = _zip_members(data, lambda n: n == "content.xml")
+ if not members:
+ return ExtractResult(kind="odf", reason="the document part is unreadable")
+ xml = _decode(members[0][1])
+ xml = re.sub(r"(?i)</text:p>|</text:h>|</table:table-row>", "\n", xml)
+ xml = re.sub(r"(?i)</table:table-cell>", "\t", xml)
+ text = _clean(_strip_markup(xml))
+ return ExtractResult(
+ text=text,
+ kind="odf",
+ ok=bool(text),
+ reason="" if text else "the document holds no text",
+ )
+
+
+def _from_epub(data: bytes | Path) -> ExtractResult:
+ """A book's chapters, in archive order (close enough to reading order)."""
+ members = _zip_members(
+ data, lambda n: n.lower().endswith((".xhtml", ".html", ".htm"))
+ )
+ if not members:
+ return ExtractResult(kind="epub", reason="the book has no readable chapter")
+ parts = [_strip_markup(_decode(raw)) for _name, raw in sorted(members)]
+ text = _clean("\n\n".join(part for part in parts if part.strip()))
+ return ExtractResult(
+ text=text,
+ kind="epub",
+ ok=bool(text),
+ reason="" if text else "the book holds no text",
+ )
+
+
+def _from_pdf(data: bytes | Path) -> ExtractResult:
+ """Page text via pypdf. A scan without an OCR layer legitimately has none."""
+ try:
+ from pypdf import PdfReader # noqa: PLC0415 — lazy (AP-26)
+ except Exception as exc: # noqa: BLE001 — a missing extra is not a crash
+ return ExtractResult(
+ kind="pdf",
+ reason=(
+ "PDF text needs the 'pypdf' package, which is not installed in "
+ f"this build ({type(exc).__name__})"
+ ),
+ )
+ try:
+ source: Any = data if isinstance(data, Path) else io.BytesIO(data)
+ reader = PdfReader(source)
+ if getattr(reader, "is_encrypted", False):
+ try:
+ reader.decrypt("")
+ except Exception: # noqa: BLE001 — a password-locked PDF is not our failure
+ return ExtractResult(
+ kind="pdf", reason="the PDF is password-protected"
+ )
+ pages = [(page.extract_text() or "") for page in reader.pages]
+ except Exception as exc: # noqa: BLE001 — a corrupt PDF reports, never raises
+ return ExtractResult(kind="pdf", reason=f"the PDF could not be read ({type(exc).__name__})")
+ text = _clean("\n\n".join(pages))
+ if not text:
+ return ExtractResult(
+ kind="pdf",
+ meta={"pages": len(pages)},
+ reason=(
+ "the PDF carries no text layer — it is most likely a scan, "
+ "which needs OCR"
+ ),
+ )
+ return ExtractResult(text=text, kind="pdf", ok=True, meta={"pages": len(pages)})
+
+
+def _from_rtf(data: bytes | Path) -> ExtractResult:
+ """Rich text with its control words removed — imperfect but readable."""
+ body = _decode(_read(data))
+ text = _clean(_RTF_CONTROL_RE.sub(" ", body))
+ return ExtractResult(
+ text=text,
+ kind="rtf",
+ ok=bool(text),
+ reason="" if text else "the document holds no text",
+ )
+
+
+def _from_html(data: bytes | Path) -> ExtractResult:
+ text = _clean(_strip_markup(_decode(_read(data))))
+ return ExtractResult(
+ text=text,
+ kind="html",
+ ok=bool(text),
+ reason="" if text else "the page holds no text",
+ )
+
+
+def _from_text(data: bytes | Path, kind: str = "text") -> ExtractResult:
+ text = _clean(_decode(_read(data)))
+ return ExtractResult(
+ text=text,
+ kind=kind,
+ ok=bool(text),
+ reason="" if text else "the file is empty",
+ )
+
+
+def _from_json(data: bytes | Path) -> ExtractResult:
+ """JSON re-serialised readably, or its raw text when it will not parse.
+
+ Pretty-printing matters: a one-line 2 MB JSON file is a single unbreakable
+ run for the chunker, while an indented one splits on real boundaries.
+ """
+ raw = _decode(_read(data))
+ try:
+ text = json.dumps(json.loads(raw), indent=2, ensure_ascii=False)
+ except (ValueError, RecursionError):
+ text = raw
+ cleaned = _clean(text)
+ return ExtractResult(
+ text=cleaned,
+ kind="json",
+ ok=bool(cleaned),
+ reason="" if cleaned else "the file is empty",
+ )
+
+
+_NO_TEXT_REASONS: dict[str, str] = {
+ "image": "an image holds no text — reading it needs OCR, which this build does not do",
+ "audio": "an audio file holds no text — reading it needs transcription",
+ "video": "a video holds no text — reading it needs transcription",
+ "empty": "the file is empty",
+ "binary": "this is a binary file with no readable text",
+ "archive": "an archive holds files rather than text — import it as an export instead",
+ "gzip": "a compressed file holds other files rather than text",
+ "legacy_office": (
+ "this is a pre-2007 Office file (.doc/.xls/.ppt); re-saving it in the "
+ "modern format makes it readable"
+ ),
+}
+
+
+def extract_text(
+ data: bytes | Path, *, filename: str = "", mime: str = ""
+) -> ExtractResult:
+ """The readable text of one file. Never raises.
+
+ ``data`` may be bytes (an attachment already in memory) or a
+ :class:`~pathlib.Path` (a file on disk, streamed by the extractors that
+ can). ``filename`` and ``mime`` are hints only — content decides.
+ """
+ try:
+ kind = detect_kind(data, filename, mime)
+ except Exception as exc: # noqa: BLE001 — detection must never break a sync
+ log.debug("extract: detection failed for %s", filename, exc_info=True)
+ return ExtractResult(reason=f"the file could not be identified ({type(exc).__name__})")
+
+ try:
+ if kind == "pdf":
+ return _from_pdf(data)
+ if kind == "docx":
+ return _from_docx(data)
+ if kind == "xlsx":
+ return _from_xlsx(data)
+ if kind == "pptx":
+ return _from_pptx(data)
+ if kind == "odf":
+ return _from_odf(data)
+ if kind == "epub":
+ return _from_epub(data)
+ if kind == "rtf":
+ return _from_rtf(data)
+ if kind == "html":
+ return _from_html(data)
+ if kind == "text":
+ suffix = Path(filename).suffix.lower() if filename else ""
+ if suffix in (".json", ".jsonl", ".ndjson"):
+ return _from_json(data)
+ return _from_text(data)
+ return ExtractResult(
+ kind=kind,
+ reason=_NO_TEXT_REASONS.get(kind, "this format is not readable as text"),
+ )
+ except Exception as exc: # noqa: BLE001 — one bad file never fails a whole sync
+ log.debug("extract: %s failed for %s", kind, filename, exc_info=True)
+ return ExtractResult(
+ kind=kind, reason=f"the file could not be read ({type(exc).__name__})"
+ )
diff --git a/tests/unit/ultrawiki/test_extract.py b/tests/unit/ultrawiki/test_extract.py
new file mode 100644
index 0000000000..54f086504c
--- /dev/null
+++ b/tests/unit/ultrawiki/test_extract.py
@@ -0,0 +1,344 @@
+"""The shared text extractor — every format a real folder or inbox holds.
+
+Written after the audit found that every reader gave up when it met a binary:
+Drive produced no item at all for a PDF or a Word file, Dropbox's allowlist
+excluded exactly the formats people keep their thinking in, Gmail never
+downloaded an attachment. These tests build the archives byte by byte rather
+than shipping fixtures, so they run identically on Windows, macOS and a
+headless container with no optional package installed.
+"""
+
+from __future__ import annotations
+
+import io
+import zipfile
+
+from jarvis.ultrawiki.extract import ExtractResult, detect_kind, extract_text
+
+
+def _zip(members: dict[str, str | bytes]) -> bytes:
+ buffer = io.BytesIO()
+ with zipfile.ZipFile(buffer, "w") as archive:
+ for name, payload in members.items():
+ archive.writestr(name, payload)
+ return buffer.getvalue()
+
+
+# ---------------------------------------------------------------------------
+# Detection — content first, name second
+# ---------------------------------------------------------------------------
+
+
+def test_a_pdf_named_txt_is_still_a_pdf():
+ """Extensions lie constantly; magic bytes do not."""
+ assert detect_kind(b"%PDF-1.7\nrest", filename="notes.txt") == "pdf"
+
+
+def test_a_file_with_no_extension_is_identified_by_content():
+ """A Drive export arrives with no name at all."""
+ assert detect_kind(b"Just some plain prose.") == "text"
+ assert detect_kind(b"\x89PNG\r\n\x1a\nrest") == "image"
+
+
+def test_office_formats_are_separated_by_their_inner_layout():
+ """They all start with the same ZIP signature."""
+ assert detect_kind(_zip({"word/document.xml": "<w:p/>"})) == "docx"
+ assert detect_kind(_zip({"xl/workbook.xml": "<workbook/>"})) == "xlsx"
+ assert detect_kind(_zip({"ppt/presentation.xml": "<p/>"})) == "pptx"
+ assert detect_kind(_zip({"content.xml": "<office/>"})) == "odf"
+ assert detect_kind(_zip({"META-INF/container.xml": "<c/>"})) == "epub"
+ # A plain zip stays a plain zip.
+ assert detect_kind(_zip({"readme.txt": "hi"})) == "archive"
+
+
+def test_an_empty_file_is_its_own_answer():
+ assert detect_kind(b"") == "empty"
+
+
+# ---------------------------------------------------------------------------
+# The formats
+# ---------------------------------------------------------------------------
+
+
+def test_a_word_document_yields_its_paragraphs_as_lines():
+ """Clauses running into one wall of text retrieve as a blur."""
+ document = (
+ "<w:document><w:body>"
+ "<w:p><w:r><w:t>The parties agree as follows.</w:t></w:r></w:p>"
+ "<w:p><w:r><w:t>Payment is due within 30 days.</w:t></w:r></w:p>"
+ "</w:body></w:document>"
+ )
+ result = extract_text(_zip({"word/document.xml": document}), filename="deal.docx")
+ assert result.ok
+ assert result.kind == "docx"
+ assert "The parties agree as follows." in result.text
+ assert "Payment is due within 30 days." in result.text
+ # Separate paragraphs, not one run-on line.
+ assert "\n" in result.text
+
+
+def test_word_headers_footnotes_and_comments_are_read_too():
+ result = extract_text(
+ _zip(
+ {
+ "word/document.xml": "<w:p><w:r><w:t>Body text.</w:t></w:r></w:p>",
+ "word/header1.xml": "<w:p><w:r><w:t>Confidential draft.</w:t></w:r></w:p>",
+ "word/comments.xml": "<w:p><w:r><w:t>Ruben: check this figure.</w:t></w:r></w:p>",
+ }
+ ),
+ filename="doc.docx",
+ )
+ assert "Body text." in result.text
+ assert "Confidential draft." in result.text
+ assert "Ruben: check this figure." in result.text
+
+
+def test_a_spreadsheet_resolves_its_shared_strings():
+ """Read without the shared-string table a workbook yields numbers only.
+
+ Excel stores every repeated string once and references it by index, so a
+ naive reader gets `0` where the sheet says "Approved".
+ """
+ shared = (
+ '<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
+ "<si><t>Vendor</t></si><si><t>Approved</t></si></sst>"
+ )
+ sheet = (
+ '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
+ "<sheetData>"
+ '<row><c t="s"><v>0</v></c><c t="s"><v>1</v></c></row>'
+ '<row><c><v>4200</v></c></row>'
+ "</sheetData></worksheet>"
+ )
+ result = extract_text(
+ _zip(
+ {
+ "xl/workbook.xml": "<workbook/>",
+ "xl/sharedStrings.xml": shared,
+ "xl/worksheets/sheet1.xml": sheet,
+ }
+ ),
+ filename="budget.xlsx",
+ )
+ assert result.ok
+ assert "Vendor" in result.text
+ assert "Approved" in result.text
+ assert "4200" in result.text
+
+
+def test_a_deck_keeps_each_note_with_its_own_slide_in_order():
+ """Notes carry the argument; detached they retrieve as orphan sentences.
+
+ Two ordering traps, both seen on the maintainer's real decks: plain sorting
+ puts every `notesSlides/` entry before any `slides/` entry, and `slide10`
+ before `slide2`.
+ """
+ members = {"ppt/presentation.xml": "<p/>"}
+ for number in (1, 2, 10):
+ members[f"ppt/slides/slide{number}.xml"] = (
+ f"<a:p><a:t>Headline {number}</a:t></a:p>"
+ )
+ members[f"ppt/notesSlides/notesSlide{number}.xml"] = (
+ f"<a:p><a:t>Note {number}</a:t></a:p>"
+ )
+ result = extract_text(_zip(members), filename="deck.pptx")
+ assert result.ok
+
+ order = [
+ result.text.index(marker)
+ for marker in ("Headline 1", "Note 1", "Headline 2", "Note 2", "Headline 10")
+ ]
+ assert order == sorted(order), result.text
+ assert "Speaker notes: Note 2" in result.text
+
+
+def test_an_opendocument_file_is_read():
+ content = (
+ "<office:document-content>"
+ "<text:p>First OpenDocument paragraph.</text:p>"
+ "<text:p>Second one.</text:p>"
+ "</office:document-content>"
+ )
+ result = extract_text(_zip({"content.xml": content}), filename="notes.odt")
+ assert result.ok
+ assert result.kind == "odf"
+ assert "First OpenDocument paragraph." in result.text
+
+
+def test_an_epub_yields_its_chapters():
+ result = extract_text(
+ _zip(
+ {
+ "META-INF/container.xml": "<container/>",
+ "ch1.xhtml": "<html><body><p>Chapter one begins.</p></body></html>",
+ "ch2.xhtml": "<html><body><p>Chapter two follows.</p></body></html>",
+ }
+ ),
+ filename="book.epub",
+ )
+ assert result.ok
+ assert "Chapter one begins." in result.text
+ assert "Chapter two follows." in result.text
+
+
+def test_html_keeps_the_text_and_drops_scripts():
+ raw = (
+ b"<html><head><style>p{color:red}</style>"
+ b"<script>alert('x')</script></head>"
+ b"<body><h1>Heading</h1><p>Real prose here.</p></body></html>"
+ )
+ result = extract_text(raw, filename="page.html")
+ assert result.ok
+ assert "Real prose here." in result.text
+ assert "alert" not in result.text
+ assert "color:red" not in result.text
+
+
+def test_rtf_loses_its_control_words():
+ raw = rb"{\rtf1\ansi\deff0 {\fonttbl}\f0\fs24 Signed on Tuesday.\par}"
+ result = extract_text(raw, filename="letter.rtf")
+ assert result.ok
+ assert "Signed on Tuesday." in result.text
+ assert "rtf1" not in result.text
+
+
+def test_json_is_pretty_printed_so_it_can_be_split():
+ """A one-line 2 MB JSON file is a single unbreakable run for the chunker."""
+ result = extract_text(b'{"a":1,"b":{"c":"deep"}}', filename="data.json")
+ assert result.ok
+ assert "\n" in result.text
+ assert '"deep"' in result.text
+
+
+def test_broken_json_keeps_its_raw_text_rather_than_vanishing():
+ result = extract_text(b'{"a": 1,,,}', filename="broken.json")
+ assert result.ok
+ assert "a" in result.text
+
+
+def test_source_code_is_plain_text():
+ result = extract_text(b"def handler():\n return 42\n", filename="app.py")
+ assert result.ok
+ assert result.kind == "text"
+ assert "def handler():" in result.text
+
+
+def test_a_windows_encoded_file_is_text_not_binary():
+ """The most common everyday document on a Windows machine.
+
+ Accented text saved as cp1252 or latin-1 is invalid UTF-8, so a
+ UTF-8-only check classifies the file BINARY and drops it whole — the exact
+ failure this service exists to stop. Any accented word proves it; these are
+ English ones so the fixture needs no language exemption.
+ """
+ result = extract_text("naïve café façade".encode("cp1252"), filename="note.txt")
+ assert result.ok
+ assert result.kind == "text"
+ assert "café" in result.text
+
+
+# ---------------------------------------------------------------------------
+# Honest failure — the part that decides whether an item is retryable
+# ---------------------------------------------------------------------------
+
+
+def test_an_image_reports_no_text_and_is_NOT_marked_retryable():
+ """Nothing to extract is different from could not extract."""
+ result = extract_text(b"\x89PNG\r\n\x1a\nrest", filename="photo.png")
+ assert not result.ok
+ assert result.kind == "image"
+ assert "OCR" in result.reason
+ assert result.content_missing is False
+
+
+def test_a_corrupt_office_file_IS_marked_retryable():
+ """A repair or a later build could still reclaim it.
+
+ A docx whose document part is missing entirely cannot be salvaged, unlike
+ one with damaged markup — the regex path recovers whatever text survives
+ there, which is the better outcome and deliberately not a failure.
+ """
+ result = extract_text(_zip({"word/styles.xml": "<styles/>"}), filename="x.docx")
+ assert result.kind in ("docx", "archive")
+ if not result.ok:
+ assert result.content_missing is True
+ assert result.reason
+
+
+def test_damaged_markup_is_salvaged_rather_than_discarded():
+ """Half a readable document beats none."""
+ result = extract_text(
+ _zip({"word/document.xml": "<w:p><w:t>Survives</w:t></w:p><w:p><w:t>bro"}),
+ filename="x.docx",
+ )
+ assert result.ok
+ assert "Survives" in result.text
+
+
+def test_a_legacy_office_file_says_what_to_do_about_it():
+ result = extract_text(b"\xd0\xcf\x11\xe0" + b"\x00" * 32, filename="old.doc")
+ assert not result.ok
+ assert "re-saving" in result.reason
+
+
+def test_an_encrypted_or_broken_pdf_never_raises():
+ result = extract_text(b"%PDF-1.4\nnot really a pdf", filename="x.pdf")
+ assert isinstance(result, ExtractResult)
+ assert result.kind == "pdf"
+ assert not result.ok
+ assert result.reason
+
+
+def test_an_archive_points_at_the_export_importer_instead():
+ result = extract_text(_zip({"a.txt": "hi"}), filename="takeout.zip")
+ assert not result.ok
+ assert "export" in result.reason
+
+
+# ---------------------------------------------------------------------------
+# Safety — these archives arrive from strangers
+# ---------------------------------------------------------------------------
+
+
+def test_an_xml_entity_bomb_is_refused_rather_than_expanded():
+ """"Billion laughs" is a real input here, not a theoretical one.
+
+ A handful of nested entity declarations expand to gigabytes and take the
+ process with them. The declaration is refused outright — OOXML has no
+ legitimate use for custom entities.
+ """
+ bomb = (
+ '<?xml version="1.0"?><!DOCTYPE lolz ['
+ '<!ENTITY lol "lol">'
+ '<!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">'
+ '<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">'
+ "]><sst><si><t>&lol3;</t></si></sst>"
+ )
+ result = extract_text(
+ _zip(
+ {
+ "xl/workbook.xml": "<workbook/>",
+ "xl/sharedStrings.xml": bomb,
+ "xl/worksheets/sheet1.xml": (
+ '<worksheet><sheetData><row><c t="s"><v>0</v></c>'
+ "</row></sheetData></worksheet>"
+ ),
+ }
+ ),
+ filename="bomb.xlsx",
+ )
+ assert "lollol" not in result.text
+ assert len(result.text) < 10_000
+
+
+def test_a_zip_that_is_not_really_a_zip_never_raises():
+ result = extract_text(b"PK\x03\x04garbage-not-an-archive", filename="x.docx")
+ assert isinstance(result, ExtractResult)
+ assert not result.ok
+
+
+def test_nothing_ever_raises_for_any_input():
+ """The extractor sits inside a walk over thousands of files."""
+ for payload in (b"", b"\x00\x01\x02", b"%PDF-", _zip({}), b"{" * 5000):
+ for name in ("", "x.pdf", "x.docx", "x.xlsx", "x.json", "x.bin"):
+ assert isinstance(extract_text(payload, filename=name), ExtractResult)