Never lose a word held back at one release when the next has nothing to rewrite
Found on the PoC: a streamed list came out as "1. 2. 3." — each item cut at one release and dropped at the next, which sent the engine's own events. A property test now cuts an answer every way and checks it arrives whole. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2cc52fd1a7
commit
62038484d6
2 files changed
+47
-1
No files matched your search
@@ -0,0 +1,42 @@
|
||||
package guard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/rand/v2"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Found on the PoC: a word held back at one release was dropped at the next,
|
||||
// when that release had nothing to rewrite and sent the engine's own events.
|
||||
func TestAWordHeldBackIsNeverLost(t *testing.T) {
|
||||
g := gate(t, lib(map[string]Policy{"p": {Output: Rules{PII: map[string]string{"email": "redact"}}}}, "p"))
|
||||
got, _, _ := stream(g.Output(context.Background(), Subject{Stream: true}),
|
||||
"1", ".", " Red", "\n", "2", ".", " Green", "\n", "3", ".", " Blue")
|
||||
if got != "1. Red\n2. Green\n3. Blue" {
|
||||
t.Errorf("client got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// However an answer is cut into tokens, the client is sent exactly the
|
||||
// whole answer, rewritten.
|
||||
func TestEverySplitOfAnAnswerArrivesWhole(t *testing.T) {
|
||||
g := gate(t, lib(map[string]Policy{"p": {Output: Rules{PII: map[string]string{"email": "redact"}}}}, "p"))
|
||||
text := "Hello. Mail bob@example.com or ann@example.org! Lists:\n1. Red\n2. Green and a long tail without stops " +
|
||||
strings.Repeat("word ", 50) + "end?"
|
||||
want := strings.NewReplacer("bob@example.com", "[EMAIL_1]", "ann@example.org", "[EMAIL_2]").Replace(text)
|
||||
rng := rand.New(rand.NewPCG(1, 2))
|
||||
for i := range 300 {
|
||||
var parts []string
|
||||
for rest := text; rest != ""; {
|
||||
n := 1 + rng.IntN(8)
|
||||
n = min(n, len(rest))
|
||||
parts = append(parts, rest[:n])
|
||||
rest = rest[n:]
|
||||
}
|
||||
got, _, _ := stream(g.Output(context.Background(), Subject{Stream: true}), parts...)
|
||||
if got != want {
|
||||
t.Fatalf("split %d: client got\n%q\nwant\n%q", i, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,6 +96,9 @@ type outFilter struct {
|
||||
|
||||
type choiceState struct {
|
||||
pending strings.Builder // content held, not yet judged
|
||||
// carried says pending begins with text whose events were already
|
||||
// replaced: it reaches the client only in a chunk this filter makes.
|
||||
carried bool
|
||||
calls map[int]*call
|
||||
}
|
||||
|
||||
@@ -405,9 +408,10 @@ func (f *outFilter) flush(final bool) (bool, map[int]string, bool) {
|
||||
}
|
||||
st.pending.Reset()
|
||||
st.pending.WriteString(tail)
|
||||
if tail != "" {
|
||||
if tail != "" || st.carried {
|
||||
differs = true
|
||||
}
|
||||
st.carried = tail != ""
|
||||
if f.answer != nil {
|
||||
f.full.WriteString(head)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user