Skip to content

Add WriteFile tool for single-step file creation and full-content writing #738

Closed

Description

@philippeback

Problem

In agent and sub-agent workflows, creating a new file or writing full contents currently requires a multi-turn ceremony:

  1. Turn 1: Calling ManageResources(create=["path/to/file"]) to scaffold an empty file and promote it to editable context.
  2. Turn 2: Calling EditFile targeting line anchor @000 to replace the entire empty buffer.

For smaller or local models (e.g. 12B–20B parameters), this creates significant friction:

  • Wastes an entire conversational round and thousands of prompt tokens just to create an empty file.
  • Models naturally try to invoke standard file creation tools (WriteFile), get rejected, and then fall back to fragile shell commands (cat << 'EOF', python -c, echo), which easily break on Windows escaping or special characters (&, quotes, newlines).
  • If the file does not exist yet on disk, EditFile rejects the call with File not found.

Proposed Solution

Add a dedicated, built-in WriteFile tool with sane defaults:

  • Single-step creation and writing: Accepts file_path and content.
  • Automatic directory scaffolding: Automatically creates missing parent directories (os.makedirs(..., exist_ok=True)).
  • Context integration: Automatically registers the new file in coder.abs_fnames (editable context).
  • Change tracking & undo: Integrates with apply_change (change_type="writefile") so all operations can be reverted via UndoChange.
  • Defensive guards:
    • Enforces project-root boundaries (is_path_within_root).
    • Guards against accidental overwriting of files marked read_only in context.
    • Supports overwrite boolean (defaults to True).
    • Tolerates minor argument aliases from local models (path/filename for file_path, text/contents/body for content, and batch arrays).
    • Automatically strips any virtual ReadFile prefix identifiers (strip_hashline) if content was copied from previous reads.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions