xonecas.io
62159
Sy

symb / commit bf5a0de

feat: better attention retention and todo list for llm

xonecas2026-02-15 bf5a0ded1f4ccad166a800c29345a136d096c240 parent 70fb78f

7 files changed

cmd/symb/main.go+7−1
@@ -93,7 +93,7 @@ func main() {
9393 }
9494
9595 p := tea.NewProgram(
96- tui.New(prov, svc.proxy, tools, providerCfg.Model, svc.webCache, sessionID, tsIndex, svc.deltaTracker, svc.fileTracker, providerName),
96+ tui.New(prov, svc.proxy, tools, providerCfg.Model, svc.webCache, sessionID, tsIndex, svc.deltaTracker, svc.fileTracker, providerName, svc.scratchpad),
9797 tea.WithFilter(tui.MouseEventFilter),
9898 )
9999 svc.lspManager.SetCallback(func(absPath string, lines map[int]int) {
@@ -151,6 +151,7 @@ type services struct {
151151 shellHandler *mcptools.ShellHandler
152152 fileTracker *mcptools.FileReadTracker
153153 deltaTracker *delta.Tracker
154+ scratchpad *mcptools.Scratchpad
154155 }
155156
156157 func setupServices(cfg *config.Config, creds *config.Credentials) services {
@@ -192,6 +193,10 @@ func setupServices(cfg *config.Config, creds *config.Credentials) services {
192193 shellHandler := mcptools.NewShellHandler(sh)
193194 proxy.RegisterTool(mcptools.NewShellTool(), shellHandler.Handle)
194195
196+ // TodoWrite tool — agent scratchpad for plan/notes recitation.
197+ pad := &mcptools.Scratchpad{}
198+ proxy.RegisterTool(mcptools.NewTodoWriteTool(), mcptools.MakeTodoWriteHandler(pad))
199+
195200 return services{
196201 proxy: proxy,
197202 lspManager: lspManager,
@@ -201,6 +206,7 @@ func setupServices(cfg *config.Config, creds *config.Credentials) services {
201206 shellHandler: shellHandler,
202207 fileTracker: fileTracker,
203208 deltaTracker: dt,
209+ scratchpad: pad,
204210 }
205211 }
206212
internal/llm/loop.go+79−1
@@ -24,6 +24,11 @@ type ToolCallCallback func()
2424 // UsageCallback is called with accumulated token usage after each LLM call.
2525 type UsageCallback func(inputTokens, outputTokens int)
2626
27+// ScratchpadReader provides read access to the agent's working plan.
28+type ScratchpadReader interface {
29+ Content() string
30+}
31+
2732 // ProcessTurnOptions holds configuration for processing a turn.
2833 type ProcessTurnOptions struct {
2934 Provider provider.Provider
@@ -34,6 +39,7 @@ type ProcessTurnOptions struct {
3439 OnDelta DeltaCallback // Optional: called for each stream event
3540 OnToolCall ToolCallCallback // Optional: called before executing tool calls
3641 OnUsage UsageCallback // Optional: called with token usage after each LLM call
42+ Scratchpad ScratchpadReader // Optional: agent plan injected at context tail
3743 MaxToolRounds int
3844 }
3945
@@ -75,7 +81,7 @@ func emitAssistant(opts *ProcessTurnOptions, resp *provider.ChatResponse) {
7581 // It streams events via OnDelta and emits complete messages via OnMessage.
7682 func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
7783 if opts.MaxToolRounds == 0 {
78- opts.MaxToolRounds = 20
84+ opts.MaxToolRounds = 40
7985 }
8086
8187 // Convert MCP tools to provider format once
@@ -96,6 +102,13 @@ func ProcessTurn(ctx context.Context, opts ProcessTurnOptions) error {
96102 compactOldToolResults(opts.History)
97103 }
98104
105+ // Inject context recitation at the tail of the history to keep
106+ // 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)
111+
99112 resp, err := streamAndCollect(ctx, &opts, providerTools)
100113 if err != nil {
101114 return fmt.Errorf("LLM stream failed: %w", err)
@@ -283,6 +296,12 @@ func executeToolCalls(ctx context.Context, proxy *mcp.Proxy, toolCalls []provide
283296 // compacted once the LLM has already processed it.
284297 const maxToolResultLen = 200
285298
299+// reminderInterval is the number of tool-calling rounds between synthetic
300+// goal reminders. After this many rounds the loop injects a system message
301+// reciting the user's original request so it stays in the model's recent
302+// attention window.
303+const reminderInterval = 5
304+
286305 // compactOldToolResults replaces verbose tool-result messages that the LLM
287306 // has already responded to with a short summary. A tool result is considered
288307 // "processed" when an assistant message appears after it in the history.
@@ -331,6 +350,65 @@ func trimToolResult(content string) string {
331350 return content[:firstNL] + "\n[Truncated — already processed by assistant]"
332351 }
333352
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.
360+//
361+// 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++
375+ }
376+ history = history[:n]
377+
378+ // If the agent has a scratchpad, always inject it.
379+ if pad != nil {
380+ 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+ })
386+ }
387+ }
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
394+ for _, m := range history {
395+ if m.Role == "user" {
396+ userContent = m.Content
397+ break
398+ }
399+ }
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+ }
407+ }
408+
409+ return history
410+}
411+
334412 // extractTextFromContent extracts text from MCP content blocks.
335413 func extractTextFromContent(content []mcp.ContentBlock) string {
336414 var text string
internal/llm/prompts/base.md+7−0
@@ -85,6 +85,13 @@ Dangerous commands (network, sudo, package managers) are blocked.
8585
8686 Use for: builds, tests, linters, git, file inspection. Default timeout: 60s.
8787
88+### `TodoWrite` — Update your working plan
89+Write or replace your current plan/scratchpad. The content stays visible at the end of your context window across tool-calling rounds.
90+
91+- `{"content": "## Plan\n1. [x] Read config.go\n2. [ ] Fix the timeout bug\n3. [ ] Run tests"}`
92+
93+Use this for multi-step tasks (3+ steps) to track goals and progress. Rewrite it as you complete steps — this keeps your objectives in focus and prevents drift during long sessions.
94+
8895 ### `Edit` — Modify files using hash anchors
8996 **Prerequisite: Read the file first.** The hashes from Read output are your edit anchors.
9097
internal/mcptools/todo.go+71−0
@@ -0,0 +1,71 @@
1+package mcptools
2+
3+import (
4+ "context"
5+ "encoding/json"
6+ "sync"
7+
8+ "github.com/xonecas/symb/internal/mcp"
9+)
10+
11+// Scratchpad holds the agent's current plan/notes. It is safe for concurrent
12+// access. The content is injected into the LLM context at the tail of the
13+// history so the agent's goals stay in the model's recent attention window.
14+type Scratchpad struct {
15+ mu sync.RWMutex
16+ content string
17+}
18+
19+// Content returns the current scratchpad text.
20+func (s *Scratchpad) Content() string {
21+ s.mu.RLock()
22+ defer s.mu.RUnlock()
23+ return s.content
24+}
25+
26+// TodoWriteArgs represents arguments for the TodoWrite tool.
27+type TodoWriteArgs struct {
28+ Content string `json:"content"`
29+}
30+
31+// NewTodoWriteTool creates the TodoWrite tool definition.
32+func NewTodoWriteTool() mcp.Tool {
33+ return mcp.Tool{
34+ Name: "TodoWrite",
35+ Description: `Write or update your working plan/scratchpad. The content replaces any previous plan and is kept visible at the end of your context window. Use this to track goals, progress, and next steps — especially during multi-step tasks.`,
36+ InputSchema: json.RawMessage(`{
37+ "type": "object",
38+ "properties": {
39+ "content": {"type": "string", "description": "Your current plan, todo list, or working notes. This replaces the previous content entirely."}
40+ },
41+ "required": ["content"]
42+ }`),
43+ }
44+}
45+
46+// MakeTodoWriteHandler creates a handler that stores content in the scratchpad.
47+func MakeTodoWriteHandler(pad *Scratchpad) mcp.ToolHandler {
48+ return func(_ context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) {
49+ var args TodoWriteArgs
50+ if err := json.Unmarshal(arguments, &args); err != nil {
51+ return &mcp.ToolResult{
52+ Content: []mcp.ContentBlock{{Type: "text", Text: "Invalid arguments: " + err.Error()}},
53+ IsError: true,
54+ }, nil
55+ }
56+ if args.Content == "" {
57+ return &mcp.ToolResult{
58+ Content: []mcp.ContentBlock{{Type: "text", Text: "Content cannot be empty"}},
59+ IsError: true,
60+ }, nil
61+ }
62+
63+ pad.mu.Lock()
64+ pad.content = args.Content
65+ pad.mu.Unlock()
66+
67+ return &mcp.ToolResult{
68+ Content: []mcp.ContentBlock{{Type: "text", Text: "Plan updated."}},
69+ }, nil
70+ }
71+}
internal/tui/golden_test.go+1−1
@@ -26,7 +26,7 @@ func TestLayout(t *testing.T) {
2626
2727 for _, tt := range tests {
2828 t.Run(tt.name, func(t *testing.T) {
29- m := New(nil, nil, nil, "test-model", nil, "test-session", nil, nil, nil, "test-provider")
29+ m := New(nil, nil, nil, "test-model", nil, "test-session", nil, nil, nil, "test-provider", nil)
3030 updated, _ := m.Update(tea.WindowSizeMsg{Width: tt.width, Height: tt.height})
3131 m = updated.(Model)
3232
internal/tui/messages.go+7−5
@@ -155,6 +155,7 @@ func (m Model) processLLM() tea.Cmd {
155155 ch := m.updateChan
156156 ctx := m.turnCtx
157157 dt := m.deltaTracker
158+ pad := m.scratchpad
158159
159160 return func() tea.Msg {
160161 go func() {
@@ -172,11 +173,12 @@ func (m Model) processLLM() tea.Cmd {
172173 start := time.Now()
173174 var turnIn, turnOut int
174175 err := llm.ProcessTurn(ctx, llm.ProcessTurnOptions{
175- Provider: prov,
176- Proxy: proxy,
177- Tools: tools,
178- History: history,
179- MaxToolRounds: 20,
176+ Provider: prov,
177+ Proxy: proxy,
178+ Tools: tools,
179+ History: history,
180+ Scratchpad: pad,
181+ // Uses the default from ProcessTurn.
180182 OnDelta: func(evt provider.StreamEvent) {
181183 switch evt.Type {
182184 case provider.EventContentDelta:
internal/tui/tui.go+5−1
@@ -226,6 +226,9 @@ type Model struct {
226226 totalInputTokens int // session-wide total input tokens
227227 totalOutputTokens int // session-wide total output tokens
228228
229+ // Context recitation
230+ scratchpad llm.ScratchpadReader // agent plan injected at context tail
231+
229232 // Undo
230233 deltaTracker *delta.Tracker
231234 turnBoundaries []turnBoundary
@@ -270,7 +273,7 @@ type Model struct {
270273 }
271274
272275 // New creates a new TUI model.
273-func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool, modelID string, db *store.Cache, sessionID string, idx *treesitter.Index, dt *delta.Tracker, ft FileReadResetter, providerConfigName string) Model {
276+func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool, modelID string, db *store.Cache, sessionID string, idx *treesitter.Index, dt *delta.Tracker, ft FileReadResetter, providerConfigName string, pad llm.ScratchpadReader) Model {
274277 sty := DefaultStyles()
275278 cursorStyle := lipgloss.NewStyle().Foreground(ColorHighlight)
276279
@@ -330,6 +333,7 @@ func New(prov provider.Provider, proxy *mcp.Proxy, tools []mcp.Tool, modelID str
330333 store: db,
331334 sessionID: sessionID,
332335
336+ scratchpad: pad,
333337 deltaTracker: dt,
334338 fileTracker: ft,
335339 tsIndex: idx,