Files
Agent-Native/runtime/filestate/filestate.go
T
weichenandCursor 6fd1d441a9
ci / vet-and-test (pull_request) Successful in 45s
ci / docker-checkout-hostgateway (pull_request) Successful in 1s
Keep tool pairs and fold reminders into the last result
Why: Old tool results were filling the window, compaction could split a tool group or commit a stale summary, and reminders arrived as extra user messages.
Evidence: Compaction retries a conflict and opens the circuit after three failures; clearing keeps the last five tool results; a stale file write is rejected; RPC reports started, queued, or handled.
Impact: Requests over the context window replace older tool results with a placeholder. Rewriting an existing file now requires a read in the same run. Subagent text over 32000 characters is capped.
Refs: feature/context-engineering
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-03 16:19:36 +00:00

64 lines
1.3 KiB
Go

// Package filestate 记录本轮读过的文件,并在改写前核对修改时间与大小。
package filestate
import (
"fmt"
"os"
"sync"
"time"
)
type stamp struct {
mod time.Time
size int64
}
// Tracker 是一次 run 内的读取记录。nil 接收者表示不检查。
type Tracker struct {
mu sync.Mutex
seen map[string]stamp
}
func New() *Tracker {
return &Tracker{seen: map[string]stamp{}}
}
// NoteRead 记下当前的修改时间与大小。
func (t *Tracker) NoteRead(path string) error {
if t == nil {
return nil
}
info, err := os.Stat(path)
if err != nil {
return err
}
t.mu.Lock()
t.seen[path] = stamp{mod: info.ModTime(), size: info.Size()}
t.mu.Unlock()
return nil
}
// CheckWrite 允许新建。已存在的文件必须在本轮读过,且修改时间与大小未变。
func (t *Tracker) CheckWrite(path string) error {
if t == nil {
return nil
}
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
t.mu.Lock()
got, ok := t.seen[path]
t.mu.Unlock()
if !ok {
return fmt.Errorf("请先读取该文件再修改:%s", path)
}
if !got.mod.Equal(info.ModTime()) || got.size != info.Size() {
return fmt.Errorf("文件在读取后已变化,请重新读取:%s", path)
}
return nil
}