The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(tools): overwriting the database file itself is still refused as binary
kshitijk4poor6e7dad760874
diff --git a/tests/tools/test_binary_document_write_guard.py b/tests/tools/test_binary_document_write_guard.pyindex 9cf00349d5f..619200f0a40 100644--- a/tests/tools/test_binary_document_write_guard.py+++ b/tests/tools/test_binary_document_write_guard.py@@ -145,14 +145,16 @@ class TestWriteFileToolGuard:assert not result.get("error")assert pdf.exists()- @pytest.mark.parametrize("sidecar_exists", [True, False])- def test_write_file_rejects_sqlite_wal_sidecar(self, tmp_path: Path, sidecar_exists: bool):+ @pytest.mark.parametrize("target", ["sidecar", "sidecar-absent", "db"])+ def test_write_file_rejects_sqlite_wal_sidecar(self, tmp_path: Path, target: str):# ".db-wal" is not a suffix in BINARY_EXTENSIONS; the sidecar must still# count as its database's extension or text lands in the WAL. A# checkpointed db has no sidecar on disk, so the absent case must be# refused too — otherwise a garbage WAL lands next to a live database.+ # The database file itself takes the separate binary-OVERWRITE branch+ # (sidecar paths return earlier), so it needs its own case.db = tmp_path / "state.db"- if sidecar_exists:+ if target == "sidecar":with _make_wal_db(db) as wal:original = wal.read_bytes()result = json.loads(write_file_tool(str(wal), "CREATE TABLE x(y);"))@@ -160,6 +162,12 @@ class TestWriteFileToolGuard:# refusal so the message steers the model to sqlite3, not to read_file.assert "binary" in result.get("error", ""), resultassert wal.read_bytes() == original+ elif target == "db":+ with _make_wal_db(db):+ original = db.read_bytes()+ result = json.loads(write_file_tool(str(db), "CREATE TABLE x(y);"))+ assert "binary" in result.get("error", ""), result+ assert db.read_bytes() == originalelse:sqlite3.connect(db).close()wal = Path(str(db) + "-wal")diff --git a/tools/file_tools_write_guards.py b/tools/file_tools_write_guards.pyindex d8b3adb4de4..1ec2d31a268 100644--- a/tools/file_tools_write_guards.py+++ b/tools/file_tools_write_guards.py@@ -422,16 +422,18 @@ def _check_cross_profile_path(filepath: str, task_id: str = "default") -> str |def _check_binary_document_write(filepath: str, task_id: str = "default") -> str | None:"""Reject text-tool writes that would corrupt a binary document (read_file showed- EXTRACTED text, so the model may write it back). Opaque formats are always rejected;- .pdf only when OVERWRITING an existing file (raw PDF syntax is text-authorable).+ EXTRACTED text, so the model may write it back). Opaque document formats and+ SQLite sidecars (-wal/-shm/-journal) are always rejected; .pdf and every other+ BINARY_EXTENSIONS suffix only when OVERWRITING an existing file (raw PDF syntax+ is text-authorable and text fixtures named ``*.db`` exist).``read_file`` auto-extracts .docx/.xlsx/.pptx (and PDF, via anydoc) to readable text, so the modelplausibly believes it holds the file's contents and tries to write the edited text back withwrite_file/patch. A plain-text write can never produce a valid OOXML/OLE/ODF container, so that writesilently destroys the document (port of nearai/ironclaw#7109)."""+ ext = os.path.splitext(filepath)[1].lower()if has_opaque_document_extension(filepath):- 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 +445,6 @@ 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 = 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 "@@ -469,7 +470,6 @@ def _check_binary_document_write(filepath: str, task_id: str = "default") -> str"text back would destroy the document. Use the pdf skill or a PDF ""library via the terminal to modify it. (Creating a NEW .pdf file ""is allowed.)")- ext = os.path.splitext(filepath)[1].lower()return (f"Refusing to overwrite existing binary file '{filepath}' ({ext}) ""with plain text — read_file showed you extracted or mojibake "