[PR #170] [MERGED] fix(deepagents): handle empty oldString in performStringReplacement #174

Closed
opened 2026-02-16 06:17:21 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagentsjs/pull/170
Author: @christian-bromann
Created: 2/2/2026
Status: Merged
Merged: 2/3/2026
Merged by: @christian-bromann

Base: mainHead: cb/add-content-in-empty-files


📝 Commits (2)

  • b28af4d fix: handle empty oldString in performStringReplacement (#161)
  • 2e97db4 retrigger

📊 Changes

3 files changed (+77 additions, -0 deletions)

View changed files

📝 libs/deepagents/src/backends/filesystem.test.ts (+36 -0)
📝 libs/deepagents/src/backends/utils.test.ts (+27 -0)
📝 libs/deepagents/src/backends/utils.ts (+14 -0)

📄 Description

Fixes #161 - FilesystemBackend.edit returns success with occurrences = -1 when both file content and oldString are empty.

Problem

When editing an empty file with an empty oldString, the performStringReplacement function in utils.ts exhibited incorrect behavior:

const occurrences = content.split(oldString).length - 1;
// "".split("") returns [] (empty array)
// [].length - 1 = -1

This caused the function to return a success response with occurrences = -1, which is invalid.

Solution

Instead of simply rejecting empty oldString with an error, this PR adds a useful feature:

  1. Empty file + empty oldString: Treats this as "set initial content" - returns [newString, 0]
  2. Non-empty file + empty oldString: Returns clear error "oldString cannot be empty when file has content"

This allows users to populate empty files using the edit operation, which was previously not possible since write only works for new files.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/deepagentsjs/pull/170 **Author:** [@christian-bromann](https://github.com/christian-bromann) **Created:** 2/2/2026 **Status:** ✅ Merged **Merged:** 2/3/2026 **Merged by:** [@christian-bromann](https://github.com/christian-bromann) **Base:** `main` ← **Head:** `cb/add-content-in-empty-files` --- ### 📝 Commits (2) - [`b28af4d`](https://github.com/langchain-ai/deepagentsjs/commit/b28af4db584882ad1484d8e0f126ef204a32b401) fix: handle empty oldString in performStringReplacement (#161) - [`2e97db4`](https://github.com/langchain-ai/deepagentsjs/commit/2e97db487d38bba03dc5e075005a8c8d21b53370) retrigger ### 📊 Changes **3 files changed** (+77 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `libs/deepagents/src/backends/filesystem.test.ts` (+36 -0) 📝 `libs/deepagents/src/backends/utils.test.ts` (+27 -0) 📝 `libs/deepagents/src/backends/utils.ts` (+14 -0) </details> ### 📄 Description Fixes #161 - `FilesystemBackend.edit` returns success with `occurrences = -1` when both file content and `oldString` are empty. ## Problem When editing an empty file with an empty `oldString`, the `performStringReplacement` function in `utils.ts` exhibited incorrect behavior: ```typescript const occurrences = content.split(oldString).length - 1; // "".split("") returns [] (empty array) // [].length - 1 = -1 ``` This caused the function to return a success response with `occurrences = -1`, which is invalid. ## Solution Instead of simply rejecting empty `oldString` with an error, this PR adds a useful feature: 1. **Empty file + empty `oldString`**: Treats this as "set initial content" - returns `[newString, 0]` 2. **Non-empty file + empty `oldString`**: Returns clear error "oldString cannot be empty when file has content" This allows users to populate empty files using the `edit` operation, which was previously not possible since `write` only works for new files. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 06:17:21 -05:00
yindo closed this issue 2026-02-16 06:17:21 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#174