diff --git a/libs/deepagents/deepagents/backends/filesystem.py b/libs/deepagents/deepagents/backends/filesystem.py index f38697da30b..86bd26824ac 100644 --- a/libs/deepagents/deepagents/backends/filesystem.py +++ b/libs/deepagents/deepagents/backends/filesystem.py @@ -446,15 +446,14 @@ def write( file_path: str, content: str, ) -> WriteResult: - """Create a new file with content. + """Write content to a file, creating it or overwriting it if it already exists. Args: - file_path: Path where the new file will be created. + file_path: Path where the file will be written. content: Text content to write to the file. Returns: - `WriteResult` with path on success, or error message if the file - already exists or write fails. + `WriteResult` with path on success, or error message on write failure. """ try: resolved_path = self._resolve_path(file_path) @@ -462,10 +461,6 @@ def write( return WriteResult(error=f"Error writing file '{file_path}': {e}") try: - if resolved_path.exists(): - msg = f"Cannot write to {file_path} because it already exists. Read and then make an edit, or write to a new path." - return WriteResult(error=msg) - # Create parent directories if needed resolved_path.parent.mkdir(parents=True, exist_ok=True) diff --git a/libs/deepagents/deepagents/backends/protocol.py b/libs/deepagents/deepagents/backends/protocol.py index 7b208b8d232..3a61acf1236 100644 --- a/libs/deepagents/deepagents/backends/protocol.py +++ b/libs/deepagents/deepagents/backends/protocol.py @@ -558,10 +558,10 @@ def write( file_path: str, content: str, ) -> WriteResult: - """Write content to a new file in the filesystem, error if file exists. + """Write content to a file, creating it or overwriting it if it already exists. Args: - file_path: Absolute path where the file should be created. + file_path: Absolute path where the file should be written. Must start with '/'. content: String content to write to the file. diff --git a/libs/deepagents/deepagents/backends/sandbox.py b/libs/deepagents/deepagents/backends/sandbox.py index 705ebe6647d..e7de7978421 100644 --- a/libs/deepagents/deepagents/backends/sandbox.py +++ b/libs/deepagents/deepagents/backends/sandbox.py @@ -79,16 +79,12 @@ """ _WRITE_CHECK_TEMPLATE = """python3 -c " -import os, sys, base64 +import os, base64 path = base64.b64decode('{path_b64}').decode('utf-8') -if os.path.exists(path): - print('Error: File already exists: ' + repr(path)) - sys.exit(1) os.makedirs(os.path.dirname(path) or '.', exist_ok=True) " 2>&1""" -"""Preflight check for write operations: verify the target file does not already -exist and create parent directories. +"""Preflight for write operations: create parent directories for the target path if it doesn't exist. Only the (small) base64-encoded path is interpolated — file content is transferred separately via `upload_files()`. @@ -544,21 +540,20 @@ def read( ) def _write_preflight(self, file_path: str) -> WriteResult | None: - """Run the existence check + parent-directory creation for `write()`. + """Create parent directories for `write()`. Subclasses overriding `write()` (e.g., to use a native SDK transport) - should call this first so they preserve the same "fail if file exists" - and parent-mkdir semantics as `BaseSandbox.write()`. There is a TOCTOU - window between this check and the actual write — an inherent limitation - of splitting the operation across two backend calls. + should call this first so they preserve the parent-mkdir semantics of + `BaseSandbox.write()`. There is a TOCTOU window between this and the + actual write — an inherent limitation of splitting the operation across + two backend calls. Args: file_path: Absolute path for the file about to be written. Returns: - `None` if the preflight passes (target does not exist, parents - created); a populated `WriteResult` with `error` set if the - check fails. + `None` if the preflight passes (parents created); a populated + `WriteResult` with `error` set if the preflight fails. """ path_b64 = base64.b64encode(file_path.encode("utf-8")).decode("ascii") check_cmd = _WRITE_CHECK_TEMPLATE.format(path_b64=path_b64) @@ -573,10 +568,10 @@ def write( file_path: str, content: str, ) -> WriteResult: - """Create a new file, failing if it already exists. + """Write content to a file, creating or overwriting it if it already exists. Args: - file_path: Absolute path for the new file. + file_path: Absolute path for the file. content: UTF-8 text content to write. Returns: diff --git a/libs/deepagents/deepagents/backends/state.py b/libs/deepagents/deepagents/backends/state.py index 28fc6548a2a..da1232bfcc5 100644 --- a/libs/deepagents/deepagents/backends/state.py +++ b/libs/deepagents/deepagents/backends/state.py @@ -250,16 +250,14 @@ def write( file_path: str, content: str, ) -> WriteResult: - """Create a new file with content. + """Write content to a file, creating it or overwriting it if it already exists. The update is queued directly via `CONFIG_KEY_SEND`. """ files = self._read_files() - if file_path in files: - return WriteResult(error=f"Cannot write to {file_path} because it already exists. Read and then make an edit, or write to a new path.") - - new_file_data = create_file_data(content) + existing = files.get(file_path) + new_file_data = update_file_data(existing, content) if existing is not None else create_file_data(content) self._send_files_update({file_path: self._prepare_for_storage(new_file_data)}) return WriteResult(path=file_path) diff --git a/libs/deepagents/deepagents/backends/store.py b/libs/deepagents/deepagents/backends/store.py index f37a8b36382..5e7d10c173e 100644 --- a/libs/deepagents/deepagents/backends/store.py +++ b/libs/deepagents/deepagents/backends/store.py @@ -596,20 +596,19 @@ def write( file_path: str, content: str, ) -> WriteResult: - """Create a new file with content. + """Write content to a file, creating it or overwriting it if it already exists. Returns WriteResult on success or error. """ store = self._get_store() namespace = self._get_namespace() - # Check if file exists existing = store.get(namespace, file_path) if existing is not None: - return WriteResult(error=f"Cannot write to {file_path} because it already exists. Read and then make an edit, or write to a new path.") - - # Create new file - file_data = create_file_data(content) + existing_file_data = self._convert_store_item_to_file_data(existing) + file_data = update_file_data(existing_file_data, content) + else: + file_data = create_file_data(content) store_value = self._convert_file_data_to_store_value(file_data) store.put(namespace, file_path, store_value) return WriteResult(path=file_path) @@ -626,13 +625,12 @@ async def awrite( store = self._get_store() namespace = self._get_namespace() - # Check if file exists using async method existing = await store.aget(namespace, file_path) if existing is not None: - return WriteResult(error=f"Cannot write to {file_path} because it already exists. Read and then make an edit, or write to a new path.") - - # Create new file using async method - file_data = create_file_data(content) + existing_file_data = self._convert_store_item_to_file_data(existing) + file_data = update_file_data(existing_file_data, content) + else: + file_data = create_file_data(content) store_value = self._convert_file_data_to_store_value(file_data) await store.aput(namespace, file_path, store_value) return WriteResult(path=file_path) diff --git a/libs/deepagents/deepagents/middleware/filesystem.py b/libs/deepagents/deepagents/middleware/filesystem.py index 81b0a3b6cb3..4256c3ec066 100644 --- a/libs/deepagents/deepagents/middleware/filesystem.py +++ b/libs/deepagents/deepagents/middleware/filesystem.py @@ -373,7 +373,8 @@ class ReadFileSchema(BaseModel): class WriteFileSchema(BaseModel): """Input schema for the `write_file` tool.""" - file_path: str = Field(description="Absolute path where the file should be created. Must be absolute, not relative.") + file_path: str = Field(description="Absolute path where the file should be written. Must be absolute, not relative.") + content: str = Field(description="The text content to write to the file. This parameter is required.") @@ -462,10 +463,10 @@ class ExecuteSchema(BaseModel): - Only use emojis if the user explicitly requests it.""" -WRITE_FILE_TOOL_DESCRIPTION = """Writes to a new file in the filesystem. +WRITE_FILE_TOOL_DESCRIPTION = """Writes content to a file. Creates the file if it does not exist; replaces it entirely if it does. Usage: -- The write_file tool will create the a new file. +- Use this tool when you intend to create a new file or replace the whole file. You do not need to read the file first. - Prefer to edit existing files (with the edit_file tool) over creating new ones when possible. """ @@ -1113,7 +1114,7 @@ def _create_write_file_tool(self) -> BaseTool: tool_description = self._custom_tool_descriptions.get("write_file") or WRITE_FILE_TOOL_DESCRIPTION def sync_write_file( - file_path: Annotated[str, "Absolute path where the file should be created. Must be absolute, not relative."], + file_path: Annotated[str, "Absolute path where the file should be written. Must be absolute, not relative."], content: Annotated[str, "The text content to write to the file. This parameter is required."], runtime: ToolRuntime[None, FilesystemState], ) -> ToolMessage: @@ -1152,7 +1153,7 @@ def sync_write_file( ) async def async_write_file( - file_path: Annotated[str, "Absolute path where the file should be created. Must be absolute, not relative."], + file_path: Annotated[str, "Absolute path where the file should be written. Must be absolute, not relative."], content: Annotated[str, "The text content to write to the file. This parameter is required."], runtime: ToolRuntime[None, FilesystemState], ) -> ToolMessage: diff --git a/libs/deepagents/tests/integration_tests/test_filesystem_middleware.py b/libs/deepagents/tests/integration_tests/test_filesystem_middleware.py index f3b3529c1c6..77bbb97d9e0 100644 --- a/libs/deepagents/tests/integration_tests/test_filesystem_middleware.py +++ b/libs/deepagents/tests/integration_tests/test_filesystem_middleware.py @@ -346,7 +346,7 @@ def test_write_file_longterm(self): assert isinstance(content, str), f"Expected str content, got {type(content)}" assert "fiery" in content or "Fiery" in content - def test_write_file_fail_already_exists_in_local(self): + def test_write_file_overwrites_existing_in_local(self): checkpointer = MemorySaver() store = InMemoryStore() agent = create_agent( @@ -376,7 +376,8 @@ def test_write_file_fail_already_exists_in_local(self): messages = response["messages"] write_file_message = next(message for message in messages if message.type == "tool" and message.name == "write_file") assert write_file_message is not None - assert "Cannot write" in write_file_message.content + assert write_file_message.status == "success" + assert "fiery" in response["files"]["/charmander.txt"]["content"].lower() or "Fiery" in response["files"]["/charmander.txt"]["content"] def test_edit_file_longterm(self): checkpointer = MemorySaver() diff --git a/libs/deepagents/tests/unit_tests/backends/test_sandbox_backend.py b/libs/deepagents/tests/unit_tests/backends/test_sandbox_backend.py index c0faf20da67..7d41a392369 100644 --- a/libs/deepagents/tests/unit_tests/backends/test_sandbox_backend.py +++ b/libs/deepagents/tests/unit_tests/backends/test_sandbox_backend.py @@ -537,17 +537,31 @@ def test_sandbox_write_with_special_content() -> None: assert sandbox._uploaded[0][1] == content.encode("utf-8") -def test_sandbox_write_returns_error_on_existing_file() -> None: - """Test that write() returns an error when the check command fails.""" +def test_sandbox_write_overwrites_existing_file() -> None: + """Test that write() replaces an existing file without error.""" + sandbox = MockSandbox() + + result = sandbox.write("/test/file.txt", "original content") + assert result.error is None + + result = sandbox.write("/test/file.txt", "new content") + assert result.error is None + + assert len(sandbox._uploaded) == 2 + assert sandbox._uploaded[-1] == ("/test/file.txt", b"new content") + + +def test_sandbox_write_returns_error_on_preflight_failure() -> None: + """Test that write() returns an error and skips upload when the preflight fails.""" sandbox = MockSandbox() def fail_execute(command: str, *, timeout: int | None = None) -> ExecuteResponse: # noqa: ARG001 sandbox.last_command = command - return ExecuteResponse(output="Error: File already exists", exit_code=1) + return ExecuteResponse(output="Error: Permission denied", exit_code=1) sandbox.execute = fail_execute - result = sandbox.write("/test/existing.txt", "content") + result = sandbox.write("/test/protected.txt", "content") assert result.error is not None assert "Error:" in result.error assert len(sandbox._uploaded) == 0 # upload should not have been called diff --git a/libs/deepagents/tests/unit_tests/backends/test_store_backend.py b/libs/deepagents/tests/unit_tests/backends/test_store_backend.py index 122f05b8898..9d1cb69a229 100644 --- a/libs/deepagents/tests/unit_tests/backends/test_store_backend.py +++ b/libs/deepagents/tests/unit_tests/backends/test_store_backend.py @@ -51,7 +51,7 @@ def test_store_backend_crud_and_search(): assert any(i["path"] == "/docs/readme.md" for i in g2) -def test_store_backend_write_rejects_existing_file(): +def test_store_backend_write_overwrites_existing_file(): mem_store = InMemoryStore() be = StoreBackend(store=mem_store, namespace=lambda _ctx: ("filesystem",)) @@ -59,16 +59,14 @@ def test_store_backend_write_rejects_existing_file(): result = be.write("/charmander.txt", "Hello world") assert result.error is None - # Attempt to overwrite — should fail + # Overwrite should succeed result = be.write("/charmander.txt", "New content") - assert result.error is not None - assert "Cannot write" in result.error - assert "already exists" in result.error + assert result.error is None - # Original content preserved + # New content visible read_result = be.read("/charmander.txt") assert read_result.file_data is not None - assert "Hello world" in read_result.file_data["content"] + assert "New content" in read_result.file_data["content"] def test_store_backend_ls_nested_directories(): diff --git a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/custom_system_message_tools.json b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/custom_system_message_tools.json index 83e42d72ead..7aae2a75014 100644 --- a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/custom_system_message_tools.json +++ b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/custom_system_message_tools.json @@ -88,7 +88,7 @@ }, { "function": { - "description": "Writes to a new file in the filesystem.\n\nUsage:\n- The write_file tool will create the a new file.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", + "description": "Writes content to a file. Creates the file if it does not exist; replaces it entirely if it does.\n\nUsage:\n- Use this tool when you intend to create a new file or replace the whole file. You do not need to read the file first.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", "name": "write_file", "parameters": { "properties": { @@ -97,7 +97,7 @@ "type": "string" }, "file_path": { - "description": "Absolute path where the file should be created. Must be absolute, not relative.", + "description": "Absolute path where the file should be written. Must be absolute, not relative.", "type": "string" } }, diff --git a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_execute_tools.json b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_execute_tools.json index 04d972103be..fa8d4c03c6d 100644 --- a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_execute_tools.json +++ b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_execute_tools.json @@ -88,7 +88,7 @@ }, { "function": { - "description": "Writes to a new file in the filesystem.\n\nUsage:\n- The write_file tool will create the a new file.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", + "description": "Writes content to a file. Creates the file if it does not exist; replaces it entirely if it does.\n\nUsage:\n- Use this tool when you intend to create a new file or replace the whole file. You do not need to read the file first.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", "name": "write_file", "parameters": { "properties": { @@ -97,7 +97,7 @@ "type": "string" }, "file_path": { - "description": "Absolute path where the file should be created. Must be absolute, not relative.", + "description": "Absolute path where the file should be written. Must be absolute, not relative.", "type": "string" } }, diff --git a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_memory_and_skills_tools.json b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_memory_and_skills_tools.json index 83e42d72ead..7aae2a75014 100644 --- a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_memory_and_skills_tools.json +++ b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_memory_and_skills_tools.json @@ -88,7 +88,7 @@ }, { "function": { - "description": "Writes to a new file in the filesystem.\n\nUsage:\n- The write_file tool will create the a new file.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", + "description": "Writes content to a file. Creates the file if it does not exist; replaces it entirely if it does.\n\nUsage:\n- Use this tool when you intend to create a new file or replace the whole file. You do not need to read the file first.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", "name": "write_file", "parameters": { "properties": { @@ -97,7 +97,7 @@ "type": "string" }, "file_path": { - "description": "Absolute path where the file should be created. Must be absolute, not relative.", + "description": "Absolute path where the file should be written. Must be absolute, not relative.", "type": "string" } }, diff --git a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_sync_and_async_subagents_tools.json b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_sync_and_async_subagents_tools.json index d7603adf48a..0b692ca9755 100644 --- a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_sync_and_async_subagents_tools.json +++ b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_with_sync_and_async_subagents_tools.json @@ -88,7 +88,7 @@ }, { "function": { - "description": "Writes to a new file in the filesystem.\n\nUsage:\n- The write_file tool will create the a new file.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", + "description": "Writes content to a file. Creates the file if it does not exist; replaces it entirely if it does.\n\nUsage:\n- Use this tool when you intend to create a new file or replace the whole file. You do not need to read the file first.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", "name": "write_file", "parameters": { "properties": { @@ -97,7 +97,7 @@ "type": "string" }, "file_path": { - "description": "Absolute path where the file should be created. Must be absolute, not relative.", + "description": "Absolute path where the file should be written. Must be absolute, not relative.", "type": "string" } }, diff --git a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_without_execute_tools.json b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_without_execute_tools.json index 83e42d72ead..7aae2a75014 100644 --- a/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_without_execute_tools.json +++ b/libs/deepagents/tests/unit_tests/smoke_tests/snapshots/system_prompt_without_execute_tools.json @@ -88,7 +88,7 @@ }, { "function": { - "description": "Writes to a new file in the filesystem.\n\nUsage:\n- The write_file tool will create the a new file.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", + "description": "Writes content to a file. Creates the file if it does not exist; replaces it entirely if it does.\n\nUsage:\n- Use this tool when you intend to create a new file or replace the whole file. You do not need to read the file first.\n- Prefer to edit existing files (with the edit_file tool) over creating new ones when possible.", "name": "write_file", "parameters": { "properties": { @@ -97,7 +97,7 @@ "type": "string" }, "file_path": { - "description": "Absolute path where the file should be created. Must be absolute, not relative.", + "description": "Absolute path where the file should be written. Must be absolute, not relative.", "type": "string" } }, diff --git a/libs/deepagents/tests/unit_tests/test_end_to_end.py b/libs/deepagents/tests/unit_tests/test_end_to_end.py index 386a09dc26c..1ea2c917a57 100644 --- a/libs/deepagents/tests/unit_tests/test_end_to_end.py +++ b/libs/deepagents/tests/unit_tests/test_end_to_end.py @@ -2846,6 +2846,52 @@ def write_then_read(path: str, content: str) -> str: assert "buffered" in tool_msg.content assert "ERROR" not in tool_msg.content + def test_write_overwrites_existing_file(self) -> None: + """write_file on an existing path replaces its content.""" + model = FixedGenericFakeChatModel( + messages=iter( + [ + AIMessage( + content="", + tool_calls=[ + { + "name": "write_file", + "args": {"file_path": "/doc.txt", "content": "new content"}, + "id": "call_w2", + "type": "tool_call", + } + ], + ), + AIMessage( + content="", + tool_calls=[ + { + "name": "read_file", + "args": {"file_path": "/doc.txt"}, + "id": "call_r2", + "type": "tool_call", + } + ], + ), + AIMessage(content="Done."), + ] + ) + ) + + agent = create_deep_agent(model=model) + result = agent.invoke( + { + "messages": [HumanMessage(content="go")], + "files": {"/doc.txt": {"content": "old content", "encoding": "utf-8"}}, + } + ) + + tool_msgs = [m for m in result["messages"] if m.type == "tool"] + # write should succeed and overwrite the old content + assert any("Updated file" in m.content for m in tool_msgs) + assert any("new content" in m.content for m in tool_msgs) + assert not any("old content" in m.content for m in tool_msgs) + def test_state_backend_delete_in_graph_context(self) -> None: """delete() removes a file from state; missing paths report an error.""" backend = StateBackend() diff --git a/libs/deepagents/tests/unit_tests/test_local_sandbox_operations.py b/libs/deepagents/tests/unit_tests/test_local_sandbox_operations.py index dc951f0f5df..125cc63683a 100644 --- a/libs/deepagents/tests/unit_tests/test_local_sandbox_operations.py +++ b/libs/deepagents/tests/unit_tests/test_local_sandbox_operations.py @@ -281,20 +281,19 @@ def test_write_creates_parent_dirs(self, sandbox: LocalSubprocessSandbox) -> Non exec_result = sandbox.execute(f"cat {test_path}") assert exec_result.output.strip() == content - def test_write_existing_file_fails(self, sandbox: LocalSubprocessSandbox) -> None: - """Test that writing to an existing file returns an error.""" + def test_write_existing_file_overwrites(self, sandbox: LocalSubprocessSandbox) -> None: + """Test that writing to an existing file overwrites it.""" test_path = "/tmp/test_sandbox_ops/existing.txt" # Create file first sandbox.write(test_path, "First content") - # Try to write again + # Write again should overwrite result = sandbox.write(test_path, "Second content") - assert result.error is not None - assert "already exists" in result.error.lower() - # Verify original content unchanged + assert result.error is None + # Verify new content exec_result = sandbox.execute(f"cat {test_path}") - assert exec_result.output.strip() == "First content" + assert exec_result.output.strip() == "Second content" def test_write_special_characters(self, sandbox: LocalSubprocessSandbox) -> None: """Test writing content with special characters and escape sequences.""" diff --git a/libs/evals/EVAL_CATALOG.md b/libs/evals/EVAL_CATALOG.md index c0a17911c69..59e3804ab49 100644 --- a/libs/evals/EVAL_CATALOG.md +++ b/libs/evals/EVAL_CATALOG.md @@ -10,37 +10,40 @@ Categories (for `--eval-category` filtering): file_operations,retrieval,tool_use,memory,conversation,summarization,unit_test,langchain/middleware ``` -**123 evals** across **8 categories** - -## File Ops (`file_operations`) (18 evals) - -- [`test_read_file_seeded_state_backend_file`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L35) — `tests/evals/test_file_operations.py:35` -- [`test_write_file_simple`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L55) — `tests/evals/test_file_operations.py:55` -- [`test_write_files_in_parallel`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L77) — `tests/evals/test_file_operations.py:77` -- [`test_write_files_in_parallel_confirm_with_verification`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L109) — `tests/evals/test_file_operations.py:109` -- [`test_write_files_in_parallel_ambiguous_confirmation`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L144) — `tests/evals/test_file_operations.py:144` -- [`test_ls_directory_contains_file_yes_no`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L179) — `tests/evals/test_file_operations.py:179` -- [`test_ls_directory_missing_file_yes_no`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L203) — `tests/evals/test_file_operations.py:203` -- [`test_edit_file_replace_text`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L226) — `tests/evals/test_file_operations.py:226` -- [`test_read_then_write_derived_output`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L249) — `tests/evals/test_file_operations.py:249` -- [`test_avoid_unnecessary_tool_calls`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L274) — `tests/evals/test_file_operations.py:274` -- [`test_read_files_in_parallel`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L292) — `tests/evals/test_file_operations.py:292` -- [`test_read_file_truncation_recovery_with_pagination`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L562) — `tests/evals/test_file_operations.py:562` -- [`test_read_file_empty_file_reports_empty`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L600) — `tests/evals/test_file_operations.py:600` -- [`test_delete_simple`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L617) — `tests/evals/test_file_operations.py:617` -- [`test_delete_one_of_several_files`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L646) — `tests/evals/test_file_operations.py:646` -- [`test_deletes_in_parallel`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L681) — `tests/evals/test_file_operations.py:681` -- [`test_write_then_delete_same_file`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L714) — `tests/evals/test_file_operations.py:714` -- [`test_delete_missing_file_reports_absence`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L744) — `tests/evals/test_file_operations.py:744` +**126 evals** across **8 categories** + +## File Ops (`file_operations`) (21 evals) + +- [`test_read_file_seeded_state_backend_file`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L36) — `tests/evals/test_file_operations.py:36` +- [`test_write_file_overwrites_existing`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L56) — `tests/evals/test_file_operations.py:56` +- [`test_write_file_overwrite_drops_old_content`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L85) — `tests/evals/test_file_operations.py:85` +- [`test_write_file_prefers_edit_for_targeted_change`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L109) — `tests/evals/test_file_operations.py:109` +- [`test_write_file_simple`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L139) — `tests/evals/test_file_operations.py:139` +- [`test_write_files_in_parallel`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L161) — `tests/evals/test_file_operations.py:161` +- [`test_write_files_in_parallel_confirm_with_verification`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L193) — `tests/evals/test_file_operations.py:193` +- [`test_write_files_in_parallel_ambiguous_confirmation`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L228) — `tests/evals/test_file_operations.py:228` +- [`test_ls_directory_contains_file_yes_no`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L263) — `tests/evals/test_file_operations.py:263` +- [`test_ls_directory_missing_file_yes_no`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L287) — `tests/evals/test_file_operations.py:287` +- [`test_edit_file_replace_text`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L310) — `tests/evals/test_file_operations.py:310` +- [`test_read_then_write_derived_output`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L333) — `tests/evals/test_file_operations.py:333` +- [`test_avoid_unnecessary_tool_calls`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L358) — `tests/evals/test_file_operations.py:358` +- [`test_read_files_in_parallel`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L376) — `tests/evals/test_file_operations.py:376` +- [`test_read_file_truncation_recovery_with_pagination`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L646) — `tests/evals/test_file_operations.py:646` +- [`test_read_file_empty_file_reports_empty`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L684) — `tests/evals/test_file_operations.py:684` +- [`test_delete_simple`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L701) — `tests/evals/test_file_operations.py:701` +- [`test_delete_one_of_several_files`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L730) — `tests/evals/test_file_operations.py:730` +- [`test_deletes_in_parallel`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L765) — `tests/evals/test_file_operations.py:765` +- [`test_write_then_delete_same_file`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L798) — `tests/evals/test_file_operations.py:798` +- [`test_delete_missing_file_reports_absence`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L828) — `tests/evals/test_file_operations.py:828` ## Retrieval (`retrieval`) (6 evals) - [`test_frames`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_external_benchmarks.py#L67) — `tests/evals/test_external_benchmarks.py:67` -- [`test_grep_finds_matching_paths`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L322) — `tests/evals/test_file_operations.py:322` -- [`test_glob_lists_markdown_files`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L350) — `tests/evals/test_file_operations.py:350` -- [`test_find_magic_phrase_deep_nesting`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L378) — `tests/evals/test_file_operations.py:378` -- [`test_identify_quote_author_from_directory_parallel_reads`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L414) — `tests/evals/test_file_operations.py:414` -- [`test_identify_quote_author_from_directory_unprompted_efficiency`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L489) — `tests/evals/test_file_operations.py:489` +- [`test_grep_finds_matching_paths`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L406) — `tests/evals/test_file_operations.py:406` +- [`test_glob_lists_markdown_files`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L434) — `tests/evals/test_file_operations.py:434` +- [`test_find_magic_phrase_deep_nesting`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L462) — `tests/evals/test_file_operations.py:462` +- [`test_identify_quote_author_from_directory_parallel_reads`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L498) — `tests/evals/test_file_operations.py:498` +- [`test_identify_quote_author_from_directory_unprompted_efficiency`](https://github.com/langchain-ai/deepagents/blob/main/libs/evals/tests/evals/test_file_operations.py#L573) — `tests/evals/test_file_operations.py:573` ## Tool Use (`tool_use`) (53 evals) diff --git a/libs/evals/tests/evals/test_file_operations.py b/libs/evals/tests/evals/test_file_operations.py index 36f58aff52b..19a42437eeb 100644 --- a/libs/evals/tests/evals/test_file_operations.py +++ b/libs/evals/tests/evals/test_file_operations.py @@ -22,6 +22,7 @@ file_absent, file_contains, file_equals, + file_excludes, final_text_contains, final_text_excludes, run_agent, @@ -49,6 +50,89 @@ def test_read_file_seeded_state_backend_file(model: BaseChatModel) -> None: ) +@pytest.mark.eval_tier("baseline") +@pytest.mark.eval_category("file_operations") +@pytest.mark.langsmith +def test_write_file_overwrites_existing(model: BaseChatModel) -> None: + """Overwrites an existing file without reading it first.""" + agent = create_deep_agent(model=model) + run_agent( + agent, + model=model, + initial_files={"/note.md": "old content\n"}, + query='Rewrite /note.md so it contains only "new content". Reply with DONE only.', + # 1st step: write_file to /note.md (no read needed). + # 2nd step: reply DONE. + # 1 tool call request: write_file. + scorer=TrajectoryScorer() + .expect( + agent_steps=2, + tool_call_requests=1, + tool_calls=[ + tool_call(name="write_file", step=1, args_contains={"file_path": "/note.md"}), + ], + ) + .success( + final_text_contains("DONE"), + file_equals("/note.md", "new content"), + ), + ) + + +@pytest.mark.eval_tier("baseline") +@pytest.mark.eval_category("file_operations") +@pytest.mark.langsmith +def test_write_file_overwrite_drops_old_content(model: BaseChatModel) -> None: + """Overwriting a file replaces it entirely, no trace of old content should remain.""" + agent = create_deep_agent(model=model) + run_agent( + agent, + model=model, + initial_files={"/log.txt": "stale line\n"}, + query='Write "fresh line" to /log.txt. Reply with DONE only.', + # 1st step: write_file to /log.txt. + # 2nd step: reply DONE. + # 1 tool call request: write_file. + scorer=TrajectoryScorer() + .expect(agent_steps=2, tool_call_requests=1) + .success( + final_text_contains("DONE"), + file_contains("/log.txt", "fresh line"), + file_excludes("/log.txt", "stale"), + ), + ) + + +@pytest.mark.eval_tier("baseline") +@pytest.mark.eval_category("file_operations") +@pytest.mark.langsmith +def test_write_file_prefers_edit_for_targeted_change(model: BaseChatModel) -> None: + """Uses edit_file (not write_file) when only a targeted in-place change is needed.""" + agent = create_deep_agent(model=model) + run_agent( + agent, + model=model, + initial_files={"/note.md": "cat dog bird\n"}, + query="In /note.md, replace 'cat' with 'lion'. Reply with DONE only.", + # 1st step: edit_file on /note.md (targeted change, rest of file preserved). + # 2nd step: reply DONE. + # 1 tool call request: edit_file. + scorer=TrajectoryScorer() + .expect( + agent_steps=2, + tool_call_requests=1, + tool_calls=[ + tool_call(name="edit_file", step=1, args_contains={"file_path": "/note.md"}), + ], + ) + .success( + final_text_contains("DONE"), + file_contains("/note.md", "lion"), + file_excludes("/note.md", "cat"), + ), + ) + + @pytest.mark.eval_tier("baseline") @pytest.mark.eval_category("file_operations") @pytest.mark.langsmith