pii: a long text's pieces end where its own text says, so an edited system prompt is read again around the edit only

wangwu25685333 on X (10-29, v0.1.1840, Linux, i5-7500 4c/4t): a fresh
Hermes session of 31.5K tokens spent 98.8s in the local model (AstrLink
Guard) and 2.3s upstream, and every new session (cron opens one each
time) read the same ~30K of system prompt again.

The cache by content was already there (#3682: sha256 of each text or
piece -> spans, in memory and in found-<rev>.bin, no text), and tool
definitions are no longer read (a5315c70d). What made a fresh session
miss it is how a long text is cut: a piece ended after the last line's
end in its second half, so its end was a distance from the piece's
start. A line of the prompt edited (Hermes puts its date, memory and a
workspace snapshot in the prompt) moved every later piece's ends by as
many bytes, and every piece after the edit was new to the cache.

Now a piece ends after the line's end (else space) in that second half
whose 48 bytes before it hash least (FNV-1a): the text's own choice. Once
one end meets an end of before, the rest are the same pieces, known by
hash. The rune-start fallback (no line's end or space in 2 KB) is
unchanged.

Measured, pieces new to the cache after one edit (4 inserts x 2
deletes at every ~1 KB) of three texts, guardChunk pieces (cut only):
  39 KB of AGENTS.md/LESSONS.md/code-standards: mean 3.92 -> 1.65,
    worst 13 of 13 -> 5 of 18
  68 KB of LESSONS.md/agent-wiring.md: 2.80 -> 1.63, worst 14 -> 6
  58 KB of prose(): 4.96 -> 1.64, worst 20 of 20 -> 4 of 28
The cost: pieces are 3/4 as long on average, so a cold read reads
142% of the text where it read 130% (overlaps). Tried and dropped:
a narrower window (3/4 of the piece: 2.49 mean, worst 9) and the last
line's end whose hash is 0 mod K (K 4..32: worst 9 to 15 at the
small K, no better than this at 32).

The real model (AstrLink Guard, int8, pure Go) on the Linux box with
GOMAXPROCS=4 taskset -c 0-3 (wsl magpie-desktop), a 110 KB Hermes-like
system prompt (about 28K tokens; AGENTS.md, code-standards, LESSONS.md
and two subsystem pages, a memory section, a date line):
  cold:                         59.9s -> 62.4s
  a line added at 10/25/50/75%: 3.06/4.77/1.79/2.49s ->
                                1.35/2.93/1.65/2.48s (mean 3.03 -> 2.10)
  memory line + date changed:   2.83s -> 2.72s
  the same prompt again:        0s both (the cache by hash)
On mote (CoreML) the cold read is 1.3s both ways.

Test: TestEditedTextReadsItsPiecesAgainOnly reads a 40 KB prompt-like
text, then the same with a line inserted at 14 places x 4 inserts, and
fails when more than 3 pieces' bytes are read again or a span is lost.
Broken by hand: with find.go of origin/main it fails ("Conversation
started: ..." at 536: 53573 bytes read again, want at most 12288).

Run (mote): go test ./internal/pii/... ./internal/masking ok; gofmt -l
clean.
At ca221e115 with the three pii commits of this push: gofmt -l clean;
go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
./... and go build ok (mote); go test ./internal/pii/...
./internal/masking ok on mote, on Windows (win-run) and with -race on
Linux (wsl-run). Not run before the push: go test ./... (both mote and
the Linux box were running other sessions' full suites, one a machine).

Not tried: the reporter's own Hermes prompt (it is on their machine);
the measure is on this repo's docs and on prose(); a CJK text with no
line's end or space for 2 KB still cuts by distance.
This commit is contained in:
yetone
2026-10-29 01:28:25 +08:00
parent 67d0e4da61
commit 40593e898c
2 changed files with 98 additions and 8 deletions
+44 -8
View File
@@ -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
+54
View File
@@ -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)
}
}
}
}