From c6f3a3cf24a303190bc2253ad2a0c7d2cbb20331 Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Thu, 17 Sep 2026 19:00:10 +0530 Subject: [PATCH] refactor(tools): one suffix helper for the binary-extension checks; drop the dead .db3 member _has_extension_in and is_sqlite_sidecar each re-implemented the same rfind('.') / -1 / .lower() suffix extraction, and the write guard used a third spelling (filepath[filepath.rfind("."):]) for its refusal messages next to os.path.splitext in the overwrite branch. Add _lower_suffix() ("" when no dot) and route both predicates through it; the write guard now uses os.path.splitext for every message's displayed extension. _SQLITE_EXTENSIONS was `{.db,.sqlite,.sqlite3,.db3} & BINARY_EXTENSIONS`, which silently dropped .db3 (not a binary extension anywhere in the codebase, so `x.db3-wal` was never a sidecar). Spell the set as the three members that actually take effect and assert the subset invariant instead of hiding it behind an intersection. Behaviour is unchanged. --- tools/binary_extensions.py | 24 +++++++++++------------- tools/file_tools_write_guards.py | 4 ++-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/tools/binary_extensions.py b/tools/binary_extensions.py index f15f1708a4..d3b159714e 100644 --- a/tools/binary_extensions.py +++ b/tools/binary_extensions.py @@ -38,7 +38,14 @@ _SQLITE_SIDECAR_MARKERS = ("-wal", "-shm", "-journal") # Only these suffixes take a sidecar marker; ``report.docx-wal`` is not a # document and must not be treated as one. -_SQLITE_EXTENSIONS = frozenset({".db", ".sqlite", ".sqlite3", ".db3"}) & BINARY_EXTENSIONS +_SQLITE_EXTENSIONS = frozenset({".db", ".sqlite", ".sqlite3"}) +assert _SQLITE_EXTENSIONS <= BINARY_EXTENSIONS + + +def _lower_suffix(path: str) -> str: + """Lower-cased final ``.suffix`` of ``path`` ("" when there is no dot).""" + dot = path.rfind(".") + return "" if dot == -1 else path[dot:].lower() def _strip_sidecar_marker(suffix: str) -> str | None: @@ -56,24 +63,15 @@ def _has_extension_in(path: str, extensions: frozenset) -> bool: """Case-insensitive check on the final ``.suffix``; pure string, no I/O. A SQLite sidecar counts as its database's extension (only SQLite suffixes are stripped, so ``x.docx-wal`` stays unrecognised).""" - dot = path.rfind(".") - if dot == -1: - return False - suffix = path[dot:].lower() - base = _strip_sidecar_marker(suffix) - if base is not None: - suffix = base - return suffix in extensions + suffix = _lower_suffix(path) + return (_strip_sidecar_marker(suffix) or suffix) in extensions def is_sqlite_sidecar(path: str) -> bool: """True for ``x.db-wal`` / ``x.sqlite3-shm`` / ``x.db-journal`` paths. Pure string, no I/O — a sidecar path is never a legitimate text target even when no sidecar exists on disk (a checkpointed db has none).""" - dot = path.rfind(".") - if dot == -1: - return False - return _strip_sidecar_marker(path[dot:].lower()) is not None + return _strip_sidecar_marker(_lower_suffix(path)) is not None def has_binary_extension(path: str) -> bool: diff --git a/tools/file_tools_write_guards.py b/tools/file_tools_write_guards.py index df40d2905b..d8b3adb4de 100644 --- a/tools/file_tools_write_guards.py +++ b/tools/file_tools_write_guards.py @@ -431,7 +431,7 @@ def _check_binary_document_write(filepath: str, task_id: str = "default") -> str silently destroys the document (port of nearai/ironclaw#7109). """ if has_opaque_document_extension(filepath): - ext = filepath[filepath.rfind("."):].lower() + ext = os.path.splitext(filepath)[1].lower() return ( f"Refusing to write plain text to binary document '{filepath}' ({ext}). " "A text write cannot produce a valid document container and would " @@ -443,7 +443,7 @@ def _check_binary_document_write(filepath: str, task_id: str = "default") -> str # no sidecar exists yet: a checkpointed db has none on disk, and a garbage # WAL dropped next to a live database is picked up on the next open. if is_sqlite_sidecar(filepath): - ext = filepath[filepath.rfind("."):].lower() + ext = os.path.splitext(filepath)[1].lower() return ( f"Refusing to write plain text to binary SQLite sidecar '{filepath}' ({ext}). " "A -wal/-shm/-journal file holds raw database pages that SQLite "