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.
This commit is contained in:
kshitijk4poor
2026-09-17 19:00:10 +05:30
committed by kshitij
parent f28b63e5a7
commit c6f3a3cf24
2 changed files with 13 additions and 15 deletions

View File

@@ -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:

View File

@@ -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 "