Build a Coding Agent
Stage 4 of 7v1 · 14f46a8f

File Creation and Writing

Implement the agent ability to create new files and overwrite existing files in the workspace.

File Creation in Coding Agents

Beyond inspecting existing code, agents need the ability to create new modules, test files, and configuration files. The write_file tool accepts a relative path and the full content, updating the workspace state atomically.

Input and Output Protocol

Your program receives JSON on standard input:

{
  "op": "write_file",
  "workspace": {
    "existing.py": "x = 10\n"
  },
  "path": "app.py",
  "content": "print('Hello agent')\n"
}

Standard output must emit compact JSON with keys in alphabetical order:

{
  "path": "app.py",
  "status": "ok",
  "workspace": { "app.py": "print('Hello agent')\n", "existing.py": "x = 10\n" }
}

The required fields are:

  • path: the path of the written file.
  • status: "ok".
  • workspace: the updated workspace map containing the new or modified file.

Error Handling

If the path is empty or invalid, if op is unknown, or if standard input is malformed:

  • Leave standard output empty.
  • Write an error description to standard error containing the word error.
  • Exit with status code 1.

Acceptance Criteria

  • Adds new files to the workspace map.
  • Overwrites content for files that already existed.
  • Leaves unrelated workspace files untouched.
  • Rejects invalid inputs with exit code 1.