Change part of a file without rewriting it
file_write replaces a file entirely, so a model wanting to change one line either rewrote the whole thing from memory -- silently dropping everything it did not happen to recall -- or shelled out to sed. file_edit takes a unified diff instead, and services/agent/patch.py applies it. Four behaviours carry that module, and each exists because of how models actually write patches rather than how the format is specified. Fuzzy offset, exact content. A hunk header is a hint: models count from a truncated read or from the file as it was three edits ago and get the numbers wrong, and get the context lines right. So the hinted position is tried, then the file is scanned outward for an exact match of the context block. One match wins; more than one refuses, because guessing between two identical blocks is the one failure that silently corrupts a file. Line endings are normalised in and restored out, or every hunk on a CRLF file fails on context that looks identical in the error message. A blank context line that lost its leading space is read as blank, because trailing whitespace is stripped by half the things a model's output passes through. And nothing is written unless every hunk applies: a half-applied file is worse than a refused one, and the model cannot tell the difference without reading it again. It refuses a file this reply has not read, in those words. A patch written from memory either fails on context -- the good case -- or matches something it did not mean. AgentContext.read_paths records what was read; it lives there because runners never see a Generation and a read path is a fact about the machine, and it is shared with the approved copy because as_approved is dataclasses.replace, which copies field references. It resets each reply, and that is right rather than a limitation: tool_calls_json is never replayed, so on the next turn the model does not have the contents either. Writes and edits both render a git-style diff in the transcript now, escaped like everything else there and bounded at write time -- a generated file's diff can be larger than the file, and it sits on the row forever. That costs file_write one extra SFTP round trip to read the old contents, on the hottest agent operation, and it is a conscious trade: it is the difference between seeing what an agent did and having to go and look. It earns its keep twice, because that read also counts as having read the file. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -400,6 +400,39 @@
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
/* A diff, from a write or an update.
|
||||
|
||||
The same visual language as .tool-result__text above -- both answer "what did
|
||||
it do", and two languages would suggest a difference that is not there. The
|
||||
colours are --success and --danger rather than anything new: --success is
|
||||
deliberately a different hue from --leaf so an added line does not read as the
|
||||
brand accent, and --danger is already what an error border uses, so a removed
|
||||
line reads as removed rather than as broken.
|
||||
|
||||
The padding is on the line, not on the block, so a highlighted row runs the
|
||||
full width instead of stopping short of the rounded corner. */
|
||||
.diff {
|
||||
margin: 0;
|
||||
padding: var(--sp-2) 0;
|
||||
border-radius: var(--radius-sm);
|
||||
background: var(--bg-sunken);
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--text-xs);
|
||||
line-height: var(--leading-relaxed);
|
||||
max-height: 26em;
|
||||
overflow: auto;
|
||||
}
|
||||
.diff__line {
|
||||
display: block;
|
||||
padding: 0 var(--sp-3);
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.diff__line--add { background: var(--success-soft); color: var(--success); }
|
||||
.diff__line--del { background: var(--danger-soft); color: var(--danger); }
|
||||
.diff__line--meta { color: var(--ink-faint); }
|
||||
.diff__line--ctx { color: var(--ink-muted); }
|
||||
|
||||
/* --- The model asking you something --------------------------------------- */
|
||||
/* Attributed to the model on purpose. A card styled like the application is a
|
||||
card people answer with things they would not tell a chatbot. */
|
||||
|
||||
@@ -80,6 +80,41 @@
|
||||
<pre class="tool-result__text">{{ event.text }}</pre>
|
||||
{% endif %}
|
||||
|
||||
{% if event.diff %}
|
||||
{#
|
||||
What a write or an update changed, in the shape everybody already reads a
|
||||
change in.
|
||||
|
||||
Every line is text off somebody's machine and is escaped exactly like the
|
||||
rest of this file. The classification reads the first character and never
|
||||
interprets the rest — a line is coloured, never parsed.
|
||||
|
||||
Header lines are checked BEFORE the bare +/- ones, or `+++ b/x.py` renders
|
||||
as an addition and `--- a/x.py` as a removal at the top of every diff. The
|
||||
test is against the `a/` and `b/` prefixes rather than against three
|
||||
dashes, because a removed line whose own text begins with `--` produces
|
||||
exactly three dashes too.
|
||||
|
||||
No whitespace between the spans: they are `display: block` inside a <pre>,
|
||||
so a newline between two of them is a blank line on screen.
|
||||
#}
|
||||
<pre class="diff"><code>
|
||||
{%- for line in event.diff.split("\n") -%}
|
||||
{%- if line.startswith('@@') or line.startswith('--- a/')
|
||||
or line.startswith('+++ b/') or line.startswith('diff ') -%}
|
||||
{%- set cls = 'meta' -%}
|
||||
{%- elif line.startswith('+') -%}
|
||||
{%- set cls = 'add' -%}
|
||||
{%- elif line.startswith('-') -%}
|
||||
{%- set cls = 'del' -%}
|
||||
{%- else -%}
|
||||
{%- set cls = 'ctx' -%}
|
||||
{%- endif -%}
|
||||
<span class="diff__line diff__line--{{ cls }}">{{ line }}</span>
|
||||
{%- endfor -%}
|
||||
</code></pre>
|
||||
{% endif %}
|
||||
|
||||
{% for result in event.results %}
|
||||
<div class="tool-result">
|
||||
{% set scheme = (result.url or "").split(":")[0] | lower %}
|
||||
|
||||
Reference in New Issue
Block a user