With restore: true and the answer cache on, alice's "write to alice@example.com" and bob's "write to bob@example.com" redact to the same request, and bob was given the answer kept for alice, her address put back in it. A request with restore values is now never looked up in the cache nor kept in it. The similar-question cache compared the last message's text and blanked its parts: two pictures asked about in the same words were one question. A last message with any part that is not text is now reused only when the request is the same. Found in review; both tests fail without the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
6.0 KiB
Go
151 lines
6.0 KiB
Go
package router
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/guard"
|
|
)
|
|
|
|
// restoreGuard redacts alice's address as the gate does, and asks for it
|
|
// back in the answer.
|
|
type restoreGuard struct{}
|
|
|
|
func (restoreGuard) Input(_ context.Context, _ guard.Subject, body []byte) (guard.Verdict, error) {
|
|
if !bytes.Contains(body, []byte("alice@example.com")) {
|
|
return guard.Verdict{}, nil
|
|
}
|
|
return guard.Verdict{Action: guard.Redact, Policy: "pii", Category: "email",
|
|
Body: bytes.ReplaceAll(body, []byte("alice@example.com"), []byte("[EMAIL_1]")),
|
|
Restore: map[string]string{"[EMAIL_1]": `alice@example.com`}}, nil
|
|
}
|
|
func (restoreGuard) Output(context.Context, guard.Subject) guard.OutputFilter { return nil }
|
|
func (restoreGuard) Directives(guard.Subject) guard.Directives { return guard.Directives{} }
|
|
|
|
// echoEngine answers with the placeholder, in one piece or, streamed, split
|
|
// across two events.
|
|
func echoEngine(t *testing.T) *httptest.Server {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /metrics", func(w http.ResponseWriter, _ *http.Request) {
|
|
fmt.Fprint(w, "vllm:num_requests_running 0\nvllm:num_requests_waiting 0\nvllm:kv_cache_usage_perc 0.1\n")
|
|
})
|
|
mux.HandleFunc("POST /v1/chat/completions", func(w http.ResponseWriter, r *http.Request) {
|
|
b := new(bytes.Buffer)
|
|
_, _ = b.ReadFrom(r.Body)
|
|
if bytes.Contains(b.Bytes(), []byte("alice@example.com")) {
|
|
t.Errorf("the engine saw the address: %s", b)
|
|
}
|
|
if !bytes.Contains(b.Bytes(), []byte(`"stream":true`)) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprint(w, `{"model":"m","choices":[{"index":0,"message":{"role":"assistant","content":"Sent to [EMAIL_1]."},"finish_reason":"stop"}],"usage":{"prompt_tokens":4,"completion_tokens":4}}`)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
for _, piece := range []string{"Sent to [EMA", "IL_1]", "."} {
|
|
fmt.Fprintf(w, "data: {\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{\"content\":%q},\"finish_reason\":null}]}\n\n", piece)
|
|
}
|
|
fmt.Fprint(w, "data: {\"model\":\"m\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func TestTheAnswerGetsBackTheValuesItsRequestHadRedacted(t *testing.T) {
|
|
srv := echoEngine(t)
|
|
rt := newTestRouter(t, baseConfig(config.Endpoint{ID: "a", URL: srv.URL, Models: []string{"m"}}))
|
|
rt.SetGuard(restoreGuard{})
|
|
|
|
rec := post(t, rt.Handler(), say("write to alice@example.com"), nil)
|
|
if !strings.Contains(rec.Body.String(), "Sent to alice@example.com.") || strings.Contains(rec.Body.String(), "[EMAIL_1]") {
|
|
t.Errorf("one-shot answer: %s", rec.Body.String())
|
|
}
|
|
|
|
streamed := say("write to alice@example.com")
|
|
streamed["stream"] = true
|
|
rec = post(t, rt.Handler(), streamed, nil)
|
|
var text strings.Builder
|
|
for _, line := range strings.Split(rec.Body.String(), "\n") {
|
|
if i := strings.Index(line, `"content":"`); i >= 0 {
|
|
rest := line[i+len(`"content":"`):]
|
|
text.WriteString(rest[:strings.Index(rest, `"`)])
|
|
}
|
|
}
|
|
if text.String() != "Sent to alice@example.com." {
|
|
t.Errorf("streamed answer reads %q\n%s", text.String(), rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestARestorerHoldsOnlyWhatCouldBeAPlaceholder(t *testing.T) {
|
|
r := newRestorer(map[string]string{"[EMAIL_1]": "a@b.c", "[PHONE_1]": "+46 70"})
|
|
for _, c := range []struct{ in, send, hold string }{
|
|
{"Dear [EMA", "Dear ", "[EMA"},
|
|
{"a [1] b", "a [1] b", ""},
|
|
{"x [EMAIL_1] y", "x a@b.c y", ""},
|
|
{"end [", "end ", "["},
|
|
{"[X", "[X", ""},
|
|
} {
|
|
send, hold := r.split(c.in)
|
|
if send != c.send || hold != c.hold {
|
|
t.Errorf("split(%q) = %q, %q; want %q, %q", c.in, send, hold, c.send, c.hold)
|
|
}
|
|
}
|
|
// A value with a quote stays valid JSON in a whole answer.
|
|
q := newRestorer(map[string]string{"[PERSON_1]": `O"Brien`})
|
|
if got := string(q.body([]byte(`{"c":"hi [PERSON_1]"}`))); got != `{"c":"hi O\"Brien"}` {
|
|
t.Errorf("body: %s", got)
|
|
}
|
|
// What is held at the end is still sent.
|
|
e := newRestorer(map[string]string{"[EMAIL_1]": "a@b.c"})
|
|
e.OnEvent(`{"choices":[{"index":0,"delta":{"content":"to [EM"}}]}`)
|
|
if out := e.OnEnd(); len(out) != 1 || !strings.Contains(out[0], `"content":"[EM"`) {
|
|
t.Errorf("OnEnd: %v", out)
|
|
}
|
|
}
|
|
|
|
// anyEmailGuard redacts whichever address a request has to [EMAIL_1], and
|
|
// asks for it back: two callers' requests then read the same.
|
|
type anyEmailGuard struct{}
|
|
|
|
var anEmail = regexp.MustCompile(`[a-z]+@example\.com`)
|
|
|
|
func (anyEmailGuard) Input(_ context.Context, _ guard.Subject, body []byte) (guard.Verdict, error) {
|
|
addr := anEmail.Find(body)
|
|
if addr == nil {
|
|
return guard.Verdict{}, nil
|
|
}
|
|
return guard.Verdict{Action: guard.Redact, Policy: "pii", Category: "email",
|
|
Body: anEmail.ReplaceAll(body, []byte("[EMAIL_1]")),
|
|
Restore: map[string]string{"[EMAIL_1]": string(addr)}}, nil
|
|
}
|
|
func (anyEmailGuard) Output(context.Context, guard.Subject) guard.OutputFilter { return nil }
|
|
func (anyEmailGuard) Directives(guard.Subject) guard.Directives { return guard.Directives{} }
|
|
|
|
func TestAnAnswerWithACallersValuesPutBackIsNeverReusedForAnother(t *testing.T) {
|
|
// Redacted, alice's request and bob's read the same; the answer alice
|
|
// got has her address in it again, and must not be bob's.
|
|
srv := echoEngine(t)
|
|
rt, secret := cachingRouter(t, srv.URL, false)
|
|
rt.SetGuard(anyEmailGuard{})
|
|
hdr := map[string]string{"Authorization": "Bearer " + secret}
|
|
|
|
first := post(t, rt.Handler(), ask(0, "write to alice@example.com"), hdr)
|
|
second := post(t, rt.Handler(), ask(0, "write to bob@example.com"), hdr)
|
|
if strings.Contains(second.Body.String(), "alice") {
|
|
t.Fatalf("bob was given alice's answer: %s", second.Body.String())
|
|
}
|
|
if !strings.Contains(second.Body.String(), "bob@example.com") {
|
|
t.Errorf("bob's answer lacks his own address: %s", second.Body.String())
|
|
}
|
|
if got := first.Header().Get(cacheHeader) + "/" + second.Header().Get(cacheHeader); got != "bypass/bypass" {
|
|
t.Errorf("x-llmgw-cache %s, want bypass for both", got)
|
|
}
|
|
}
|