From a9f486b0088137fdf7fafc43249f98b79a994f5a Mon Sep 17 00:00:00 2001 From: Kamran Pakseresht Date: Thu, 4 Jun 2026 21:04:54 -0700 Subject: [PATCH] Fix agent: old tail anchor not found after old head --- ds4_agent.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ds4_agent.c b/ds4_agent.c index e360ab1db..b4f5648d1 100644 --- a/ds4_agent.c +++ b/ds4_agent.c @@ -5868,6 +5868,15 @@ static bool agent_edit_find_old_span(const char *data, size_t len, size_t head_len = (size_t)(upto - old); const char *tail = upto + strlen(marker); size_t tail_len = old_len - head_len - strlen(marker); + /* Strip leading newline/CR from tail before searching. The head already + * includes the newline at its end, so the extra \n that follows [upto] in + * the old text (whether injected by the forcer or written by the model) + * must not be part of the tail needle -- the file after the head has no + * duplicate newline. */ + while (tail_len > 0 && (*tail == '\n' || *tail == '\r')) { + tail++; + tail_len--; + } if (!agent_span_has_nonspace(tail, tail_len)) { snprintf(err, err_len, "old text after [upto] must include a unique tail anchor");