on main help sign in
chore(tools/edit): various fixes (#1921)
commit9e018b939547b9a953cb5ecba72fcad614b3594e authorChristian Rocha <christian@rocha.is> dateMon 19 Jan 2026, 09:11 (9 months ago) parentc6e7e39
2 files changed+10−12
internal/agent/tools/edit.go+3−12 internal/history/file.go+7
internal/agent/tools/edit.go+3−12
@@ -216,12 +216,10 @@ func deleteContent(edit editContext, filePath, oldString string, replaceAll bool
216216 oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
217217
218218 var newContent string
219− var deletionCount int
220219
221220 if replaceAll {
222221 newContent = strings.ReplaceAll(oldContent, oldString, "")
223− deletionCount = strings.Count(oldContent, oldString)
224− if deletionCount == 0 {
222+ if newContent == oldContent {
225223 return oldStringNotFoundErr, nil
226224 }
227225 } else {
@@ -236,13 +234,12 @@ func deleteContent(edit editContext, filePath, oldString string, replaceAll bool
236234 }
237235
238236 newContent = oldContent[:index] + oldContent[index+len(oldString):]
239− deletionCount = 1
240237 }
241238
242239 sessionID := GetSessionFromContext(edit.ctx)
243240
244241 if sessionID == "" {
245− return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for creating a new file")
242+ return fantasy.ToolResponse{}, fmt.Errorf("session ID is required for deleting content")
246243 }
247244
248245 _, additions, removals := diff.GenerateDiff(
@@ -299,7 +296,7 @@ func deleteContent(edit editContext, filePath, oldString string, replaceAll bool
299296 }
300297 }
301298 // Store the new version
302− _, err = edit.files.CreateVersion(edit.ctx, sessionID, filePath, "")
299+ _, err = edit.files.CreateVersion(edit.ctx, sessionID, filePath, newContent)
303300 if err != nil {
304301 slog.Error("Error creating file history version", "error", err)
305302 }
@@ -352,14 +349,9 @@ func replaceContent(edit editContext, filePath, oldString, newString string, rep
352349 oldContent, isCrlf := fsext.ToUnixLineEndings(string(content))
353350
354351 var newContent string
355− var replacementCount int
356352
357353 if replaceAll {
358354 newContent = strings.ReplaceAll(oldContent, oldString, newString)
359− replacementCount = strings.Count(oldContent, oldString)
360− if replacementCount == 0 {
361− return oldStringNotFoundErr, nil
362− }
363355 } else {
364356 index := strings.Index(oldContent, oldString)
365357 if index == -1 {
@@ -372,7 +364,6 @@ func replaceContent(edit editContext, filePath, oldString, newString string, rep
372364 }
373365
374366 newContent = oldContent[:index] + newString + oldContent[index+len(oldString):]
375− replacementCount = 1
376367 }
377368
378369 if oldContent == newContent {
internal/history/file.go+7
@@ -25,10 +25,14 @@ type File struct {
2525 UpdatedAt int64
2626}
2727
28+// Service manages file versions and history for sessions.
2829type Service interface {
2930 pubsub.Subscriber[File]
3031 Create(ctx context.Context, sessionID, path, content string) (File, error)
32+
33+ // CreateVersion creates a new version of a file.
3134 CreateVersion(ctx context.Context, sessionID, path, content string) (File, error)
35+
3236 Get(ctx context.Context, id string) (File, error)
3337 GetByPathAndSession(ctx context.Context, path, sessionID string) (File, error)
3438 ListBySession(ctx context.Context, sessionID string) ([]File, error)
@@ -55,6 +59,9 @@ func (s *service) Create(ctx context.Context, sessionID, path, content string) (
5559 return s.createWithVersion(ctx, sessionID, path, content, InitialVersion)
5660}
5761
62+// CreateVersion creates a new version of a file with auto-incremented version
63+// number. If no previous versions exist for the path, it creates the initial
64+// version. The provided content is stored as the new version.
5865func (s *service) CreateVersion(ctx context.Context, sessionID, path, content string) (File, error) {
5966 // Get the latest version for this path
6067 files, err := s.q.ListFilesByPath(ctx, path)