Skip to content

StrReplace loses a change another task makes between its read and its write #4

Open

Description

@vinniefalco

StrReplace reads the file and then writes the edited text back. Each is a request to the Host of its own. If another task changes the file between the two, the write holds the whole file as the read found it, and the other task's change is lost.

What happens

crates/plugin-std/src/replace.rs calls crate::read::load(source, tool, path), edits the text in memory, and then calls source.write(path, edited). Nothing ties the write to the version that was read.

  1. Task A runs StrReplace on /project/notes.md. It reads the file.
  2. Task B writes the same file, or appends to it.
  3. Task A writes its edited copy. Task B's change is gone, and neither tool call reports a problem.

The edit itself is right. Only a change made by someone else in the gap is lost.

What the Harness already covers

Write, StrReplace, and Delete set changes_files, so the Host runs a call of one of them by itself among the calls of one model reply, in the order the reply lists them. Two file-changing calls of one reply do not interleave. It does not cover:

  • a change from another run, or from another task in the Host that is not a call of this reply;
  • a change from outside Runvil, such as an editor or a build on the real folder.

Why it is open

The contract has no way to say "write this only if the file is still what I read". Closing the gap takes one of these in runvil-plugin-api, and the matching work in runvil-harness:

  • an edit as one operation, where the Host reads, replaces, and writes without a return to the tool in between;
  • a write that is conditional on a version of the file, such as its size and modified time, or a hash;
  • a lock or a claim that the tool holds across the read and the write.

Which of these fits is a decision for the contract. This is why the plan lists "a contract function for an edit as one operation" as out of scope for the change that added the file effects.

Where it came up

What a fix needs

  • A contract function from the list above, in runvil-plugin-api, and the Harness side of it.
  • StrReplace uses it. When the file changed since the read, it fails with a sentence that tells the model to read the file again.
  • A test with two tasks that interleave: the second one's change must either survive or make the edit fail, and never be lost without a word.
  • The docs in replace.rs and lib.rs lose the sentence about the lost edit.

Related

Write replaces a whole file by design, so a lost update there is the caller's choice. The gap here is specific to a tool that reads first and writes after.

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