improve: text_editor include patched region in response
linuztx committed
Feb 27, 2026 at 20:35 UTC
1f1aa120a2e3acf1f4285a0c0a8fbcc7280b654d
2 files changed
+44
-2
plugins/text_editor/prompts/fw.text_editor.patch_ok.md
+4
-1
@@ -1 +1,4 @@
1
-{{path}} patched {{edit_count}} edits applied, {{total_lines}} lines now
1
+{{path}} patched {{edit_count}} edits applied {{total_lines}} lines now
2
+>>>
3
+{{content}}
4
+<<<
plugins/text_editor/tools/text_editor.py
+40
-1
@@ -127,11 +127,16 @@ class TextEditor(Tool):
127
data={"path": expanded, "total_lines": total_lines},
128
)
129
130
+ patch_content = _read_patch_region(
131
+ expanded, ext_data["edits"], total_lines, _get_config(self.agent)
132
+ )
133
+
134
msg = self.agent.read_prompt(
135
"fw.text_editor.patch_ok.md",
136
path=expanded,
133
- edit_count=str(len(ext_data["edits"])),
137
+ edit_count=str(len(edits or [])),
138
total_lines=str(total_lines),
139
+ content=patch_content,
140
)
141
return Response(message=msg, break_loop=False)
142
@@ -144,6 +149,40 @@ class TextEditor(Tool):
149
)
150
return Response(message=msg, break_loop=False)
151
152
+
153
+# ------------------------------------------------------------------
154
+# Standalone helpers
155
+# ------------------------------------------------------------------
156
+
157
+def _read_patch_region(
158
+ path: str, edits: list[dict], total_lines: int, cfg: dict
159
+) -> str:
160
+ """Read back the affected region after a patch so the LLM sees the result."""
161
+ if not edits:
162
+ return ""
163
+
164
+ min_from = min(e["from"] for e in edits)
165
+ added = sum(
166
+ e["content"].count("\n")
167
+ + (1 if e["content"] and not e["content"].endswith("\n") else 0)
168
+ for e in edits if e.get("content")
169
+ )
170
+ removed = sum(
171
+ max(e["to"] - e["from"] + 1, 0)
172
+ for e in edits if not e.get("insert")
173
+ )
174
+ max_to = max(e["to"] for e in edits)
175
+ end_line = max_to + added - removed + 3
176
+
177
+ result = read_file(
178
+ path,
179
+ line_from=max(min_from - 1, 1),
180
+ line_to=min(end_line, total_lines),
181
+ max_line_tokens=cfg["max_line_tokens"],
182
+ max_total_read_tokens=cfg["max_total_read_tokens"],
183
+ )
184
+ return result.content
185
+
186
# ------------------------------------------------------------------
187
# Config
188
# ------------------------------------------------------------------