CI / test (push) Successful in 8m49s
With restore: true on a guardrail policy's input, what redact rewrote as placeholders comes back in the answer: the model reads "[EMAIL_1]", the client reads the address it sent. A streamed placeholder split across events is held until it is whole, and what is held is sent at the end. The swap runs before the output filter, so the output rules judge the answer as the client reads it and a placeholder they make of a value the model invented is never taken for the request's own. The map lives in the Verdict for the request only; nothing stores, logs or traces it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
4.3 KiB
Go
111 lines
4.3 KiB
Go
package router
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"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)
|
|
}
|
|
}
|