Skip to content

Conversation

@robch
Copy link
Owner

@robch robch commented Dec 14, 2025

Summary

Adds two new AI tool functions for improved file editing workflows:

  1. ReplaceFileContent - Replace entire file contents with built-in safety verification
  2. ReplaceMultipleInFile - Atomically replace multiple text patterns in a single file

Also includes a bug fix for string array deserialization in FunctionFactory.

Motivation

ReplaceFileContent

The current workflow for replacing file contents requires:

  1. ViewFile to read content
  2. Copy entire old content
  3. ReplaceOneInFile with both old and new content

This is tedious when you know you want to replace the entire file.

ReplaceMultipleInFile

When making several related changes to a file, calling ReplaceOneInFile multiple times is inefficient and error-prone. This function validates all patterns first, then applies all changes atomically.

Solution

ReplaceFileContent

New function with AI-CAPTCHA verification:

  • Requires oldContentLineCount parameter
  • Validates line count matches before replacing
  • Prevents accidental overwrites
  • Returns clear error if verification fails
  • Supports undo via EditHistory

ReplaceMultipleInFile

Atomic multi-pattern replacement:

  • Takes arrays of oldStrings and newStrings
  • Validates all patterns are unique (exactly 1 occurrence each)
  • Validates array lengths match
  • Makes NO changes if any pattern fails validation
  • Supports undo via EditHistory

Bug Fix

Fixed string array deserialization in FunctionFactory where JSON arrays were incorrectly parsed using GetRawText() instead of GetString(), causing quoted strings to include the quotes.

Example Usage

ReplaceFileContent

ReplaceFileContent(
    path: "config.txt",
    newContent: "new content here",
    oldContentLineCount: 42  // Must match actual line count
)

ReplaceMultipleInFile

ReplaceMultipleInFile(
    path: "settings.txt",
    oldStrings: ["foo", "bar", "baz"],
    newStrings: ["FOO", "BAR", "BAZ"]
)

Testing

? Comprehensive test suite added in tests/cycod-yaml/file-replacement-functions.yaml:

  • ReplaceFileContent with correct/wrong line counts
  • ReplaceFileContent error handling (non-existent files)
  • ReplaceFileContent undo functionality
  • ReplaceMultipleInFile success cases
  • ReplaceMultipleInFile validation (missing patterns, array length mismatch, non-unique patterns)
  • ReplaceMultipleInFile string array handling (verifies bug fix)
  • ReplaceMultipleInFile undo functionality

Note: All tests are marked as optional: needsAI and require proper AI API configuration (Copilot, OpenAI, Azure, or Anthropic). They can be run manually with:

cycodt run --file tests/cycod-yaml/file-replacement-functions.yaml --include-optional needsAI

The functions themselves have been manually tested and verified to work correctly.

Changes

  • src/cycod/FunctionCallingTools/StrReplaceEditorHelperFunctions.cs: Added ReplaceFileContent and ReplaceMultipleInFile functions
  • src/cycod/FunctionCalling/FunctionFactory.cs: Fixed string array deserialization bug
  • tests/cycod-yaml/file-replacement-functions.yaml: Comprehensive test coverage for new functions

- Adds new ReplaceFileContent function for AI-safe file replacement
- Requires oldContentLineCount parameter as verification (CAPTCHA for agents)
- Prevents accidental overwrites by validating line count matches
- Returns helpful error if verification fails
- Saves old content to edit history for undo support
…lization bug

- Add ReplaceMultipleInFile function for atomic multi-pattern replacement in single file
- Fix bug in FunctionFactory.cs where string arrays included quotes (GetRawText vs GetString)
- Apply fix to Array, List, and Tuple deserialization
- Clean, concise output: 'File X updated: replaced N occurrences.'
- Validates all patterns before making any changes (atomic operation)
- Supports undo via EditHistory
@robch robch force-pushed the robch/2512-replace-file-content branch from e76a604 to af9e42b Compare December 19, 2025 18:05
@robch robch marked this pull request as ready for review December 19, 2025 18:25
@robch robch merged commit 3897052 into master Dec 20, 2025
1 check passed
@robch robch deleted the robch/2512-replace-file-content branch December 20, 2025 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants