diff --git a/internal/pii/find.go b/internal/pii/find.go index 3ff5c6cd9..5153fe10b 100644 --- a/internal/pii/find.go +++ b/internal/pii/find.go @@ -392,10 +392,11 @@ type span struct{ at, end int } // pieces cuts t into pieces of at most pieceSize, each after the first // beginning pieceOverlap before the one before ends. A piece ends after a -// line's end in its second half, else after a space there, else at a -// rune's start; the next begins after a line's end in the first half of -// the overlap, else after a space there, else at a rune's start. A text of pieceSize -// or less is one piece; an empty one none. +// line's end in its second half, else after a space there (which one is +// the text's own choice: landmark), else at a rune's start; the next +// begins after a line's end in the first half of the overlap, else after +// a space there, else at a rune's start. A text of pieceSize or less is +// one piece; an empty one none. func pieces(t string) []span { return cut(t, pieceSize) } // cut is pieces, of at most size each. @@ -413,10 +414,10 @@ func cut(t string, size int) []span { return append(out, span{at, len(t)}) } half := at + size/2 - if i := strings.LastIndexByte(t[half:end], '\n'); i >= 0 { - end = half + i + 1 - } else if i := strings.LastIndexByte(t[half:end], ' '); i >= 0 { - end = half + i + 1 + if i := landmark(t, half, end, '\n'); i >= 0 { + end = i + } else if i := landmark(t, half, end, ' '); i >= 0 { + end = i } else { for end > half && !utf8.RuneStart(t[end]) { end-- @@ -437,6 +438,41 @@ func cut(t string, size int) []span { } } +// landmark is where in t[from:to] a piece ends after c: of the places +// after a c there, the one whose landmarkLen bytes before it hash least; +// -1 where there is no c. The choice is the text's own, not its distance +// from the piece's start, so a text edited before it (a line added to an +// agent's memory, a name in its workspace changed) is cut at the same +// places after the edit once one end meets an end of before, and those +// pieces are known by hash. Before, a piece ended after the last c +// there, and an edit that moved what followed by a few bytes moved every +// piece after it: of a 40 KB system prompt's 13 pieces, a fresh session +// of the same agent had 5 read again on average and all 13 at worst for +// a line of it edited (wangwu25685333 on X, 10-29: 98.8s of a +// 31.5K-token Hermes request on a 4-core CPU). Now 1.6 and 3 of 18. +func landmark(t string, from, to int, c byte) int { + best, at := uint64(0), -1 + for i := from; i < to; i++ { + if t[i] != c { + continue + } + e := i + 1 + h := uint64(14695981039346656037) // FNV-1a + for _, b := range []byte(t[max(0, e-landmarkLen):e]) { + h ^= uint64(b) + h *= 1099511628211 + } + if at < 0 || h < best { + best, at = h, e + } + } + return at +} + +// landmarkLen is how much of the text before a place landmark hashes: +// enough that two lines alike but for a word or a number differ. +const landmarkLen = 48 + // merge puts a long text's spans from its pieces together: spans that // overlap (one found in two pieces, or cut by a piece's end in one and // whole in the next) become one, its label the longer's, and spans of one diff --git a/internal/pii/pieces_test.go b/internal/pii/pieces_test.go index 97d291339..04b0e2951 100644 --- a/internal/pii/pieces_test.go +++ b/internal/pii/pieces_test.go @@ -203,3 +203,57 @@ func TestShortRequestGoesBetweenPieces(t *testing.T) { t.Fatalf("the short request was request %d of %d (%d pieces)", at, len(reqs), n) } } + +// prose is about n bytes of lines of varied length, as a system prompt's +// are, each its own. +func prose(n int) string { + words := []string{"the", "agent", "reads", "files", "before", "it", "edits", "them,", "and", "runs", "tests", "on", "mote", "when", "Alice", "asks."} + var b strings.Builder + x := uint32(7) + for line := 0; b.Len() < n; line++ { + fmt.Fprintf(&b, "%d.", line) + x = x*1664525 + 1013904223 + for w := range 4 + x>>27 { + x = x*1664525 + 1013904223 + b.WriteString(" " + words[(x>>20+uint32(w))%uint32(len(words))]) + } + b.WriteString("\n") + } + return b.String() +} + +// wangwu25685333 on X (10-29): every fresh session of an agent sends its +// system prompt again, a line of it changed (a date, a memory, a file in +// its workspace), and the model reads again only the pieces around that +// line, not every piece after it. +func TestEditedTextReadsItsPiecesAgainOnly(t *testing.T) { + text := prose(40 << 10) + for at := 500; at < 20000; at += 1499 { + for _, ins := range []string{"x", "Conversation started: Thursday, October 29", "memory: the user likes tea\n", strings.Repeat("y ", 300)} { + at := strings.IndexByte(text[at:], '\n') + at + 1 + edited := text[:at] + ins + text[at:] + r := newReader(t, guardChunk) + if _, err := r.find(context.Background(), []string{text}); err != nil { + t.Fatal(err) + } + r.asked = nil + got, err := r.find(context.Background(), []string{edited}) + if err != nil { + t.Fatal(err) + } + want := alices(edited) + for i := range got[0] { + if i < len(want) && (got[0][i].Start != want[i].Start || got[0][i].End != want[i].End) { + t.Fatalf("edit at %d: span %d %v, want %v", at, i, got[0][i], want[i]) + } + } + if len(got[0]) != len(want) { + t.Fatalf("edit at %d: found %d, want %d", at, len(got[0]), len(want)) + } + read, _ := r.bytes() + if read > 3*guardChunk { + t.Fatalf("%q at %d of %d bytes: %d bytes read again, want at most 3 pieces' (%d)", ins, at, len(text), read, 3*guardChunk) + } + } + } +}