xonecas.io
62159
Sy

symb / commit df22d6e

fix: reminder was broken - inserted messages. Now is a reminder appened

every 10th tool call

xonecas2026-02-15 df22d6e106216738426437dd7347c41b350e2357 parent 7eaa823

1 file changed

internal/llm/loop.go+34−48
@@ -102,12 +102,11 @@ func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
102102 compactOldToolResults(opts.History)
103103 }
104104
105- // Inject context recitation at the tail of the history to keep
105+ // Inject a <system-reminder> into the last tool result to keep
106106 // the model focused. Two sources:
107- // 1. Scratchpad (agent-written plan) — always injected when present.
108- // 2. Goal reminder (user's original request) — injected every
109- // reminderInterval rounds as a fallback when no scratchpad exists.
110- opts.History = injectRecitation(opts.History, opts.Scratchpad, round)
107+ // 1. Scratchpad (agent-written plan) — preferred when present.
108+ // 2. Goal reminder (user's original request) — fallback.
109+ injectRecitation(opts.History, opts.Scratchpad, round)
111110
112111 resp, err := streamAndCollect(ctx, &opts, providerTools)
113112 if err != nil {
@@ -300,7 +299,7 @@ const maxToolResultLen = 200
300299 // goal reminders. After this many rounds the loop injects a system message
301300 // reciting the user's original request so it stays in the model's recent
302301 // attention window.
303-const reminderInterval = 5
302+const reminderInterval = 10
304303
305304 // compactOldToolResults replaces verbose tool-result messages that the LLM
306305 // has already responded to with a short summary. A tool result is considered
@@ -350,63 +349,50 @@ func trimToolResult(content string) string {
350349 return content[:firstNL] + "\n[Truncated — already processed by assistant]"
351350 }
352351
353-// recitationPrefix marks synthetic recitation messages so stale ones can be
354-// removed before injecting a fresh one.
355-const recitationPrefix = "[Recitation]"
356-
357-// injectRecitation manages the tail-of-context recitation that keeps the
358-// model focused during long tool-calling loops. It removes any stale
359-// recitation message and, when appropriate, appends a fresh one.
352+// injectRecitation appends a <system-reminder> block to the last tool-result
353+// message in history to keep the model focused during long tool-calling loops.
354+// By appending to an existing message instead of creating a new one, we avoid
355+// shifting message positions and invalidating the Anthropic prompt cache.
360356 //
361357 // Priority: if the agent has written a scratchpad (plan/notes), that is
362-// injected on every round. Otherwise, on every reminderInterval rounds,
363-// the user's original request is echoed as a fallback.
364-func injectRecitation(history []provider.Message, pad ScratchpadReader, round int) []provider.Message {
365- // Strip any previous recitation. Use "user" role so recitation stays
366- // in the conversation messages and does not get hoisted into the
367- // Anthropic system blocks (which would invalidate the prompt cache).
368- n := 0
369- for _, m := range history {
370- if m.Role == "user" && strings.HasPrefix(m.Content, recitationPrefix) {
371- continue
372- }
373- history[n] = m
374- n++
358+// injected. Otherwise the user's original request is echoed as a fallback.
359+func injectRecitation(history []provider.Message, pad ScratchpadReader, round int) {
360+ if round == 0 || round%reminderInterval != 0 {
361+ return
375362 }
376- history = history[:n]
377363
378- // If the agent has a scratchpad, always inject it.
364+ // Build the reminder text.
365+ var reminder string
379366 if pad != nil {
380367 if plan := pad.Content(); plan != "" {
381- return append(history, provider.Message{
382- Role: "user",
383- Content: recitationPrefix + " Current plan:\n" + plan,
384- CreatedAt: time.Now(),
385- })
368+ reminder = plan
386369 }
387370 }
388-
389- // Fallback: periodic goal reminder using the user's original request.
390- // Search forward to find the first user message (the original request),
391- // not the last one which may be a follow-up or synthetic message.
392- if round > 0 && round%reminderInterval == 0 {
393- var userContent string
371+ if reminder == "" {
372+ // Fallback: echo the user's original request.
394373 for _, m := range history {
395374 if m.Role == "user" {
396- userContent = m.Content
375+ reminder = "The user's request: " + m.Content
397376 break
398377 }
399378 }
400- if userContent != "" {
401- return append(history, provider.Message{
402- Role: "user",
403- Content: recitationPrefix + " The user's request: " + userContent,
404- CreatedAt: time.Now(),
405- })
406- }
379+ }
380+ if reminder == "" {
381+ return
407382 }
408383
409- return history
384+ // Append to the last tool-result message, stripping any prior
385+ // reminder on that same message to avoid token accumulation.
386+ tag := "\n\n<system-reminder>\n"
387+ for i := len(history) - 1; i >= 0; i-- {
388+ if history[i].Role == "tool" {
389+ if idx := strings.Index(history[i].Content, tag); idx >= 0 {
390+ history[i].Content = history[i].Content[:idx]
391+ }
392+ history[i].Content += tag + reminder + "\n</system-reminder>"
393+ return
394+ }
395+ }
410396 }
411397
412398 // extractTextFromContent extracts text from MCP content blocks.