diff --git a/internal/guard/held_test.go b/internal/guard/held_test.go new file mode 100644 index 0000000..49a4cac --- /dev/null +++ b/internal/guard/held_test.go @@ -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) + } + } +} diff --git a/internal/guard/output.go b/internal/guard/output.go index 0b8e28a..0db7f60 100644 --- a/internal/guard/output.go +++ b/internal/guard/output.go @@ -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) }