wangbinyq (#3973, Discord #feedback): after v0.1.1840, with the request archive on, a request to a provider set to go unmasked was still masked whole before it was sent.ea53eb126skipped masking only when bodiesKeptAway() was false (internal/gateway/redact.go:272, called at gateway.go:1947), and that was true whenever archiveDest() was on, or an observer wanted bodies, so the archive put the masking pass back on every request's way, the masked copy kept only for the archive. Now an unmasked route is never masked on its way, whatever is on: - The archive keeps the request as it went, as the log and recent calls do. Its secrets are taken out by redact.ScrubJSON in the background upload, as every archived body's are; the reply half was already as the local model wrote it. - An observer that wants bodies (OTLP export, a JS plugin's) is handed them masked as before, masked once when withBodies first hands them over after a try (Call.maskIn, laterMasked), not before the send. - bodiesKeptAway and serve's masked/plain swap are gone. count_tokens, embeddings, Jev and web search never read the archive switch. docs/subsystems/gateway-routing.md says so. Run on mote at this tree (rebased onto6bec526f7): - gofmt -l internal: none. GOOS=linux/windows go vet -tags nogui ./internal/gateway: ok (before the rebase, no gateway change since but upstream's own). - go test -tags nogui -count=1 -timeout 45m ./...: 76 ok, no FAIL. (A run at the older base had TestPluginStreamsPastTheBudgetWait red, red alone too there; fixed upstream in7fd77a944, green at this head.) - Rebased again onto9e33962c0before the push (26 files, none in internal/gateway): go build -tags nogui ./... ok, and TestUnmaskedRouteIsNotMasked, TestArchive*, TestObserver*, TestPromptText* ok at the pushed head. - TestUnmaskedRouteIsNotMasked gains "the request archive is on" and "the archive is on, unset", and checks what the observer and the bucket get. - Broken by hand: with origin/main's gateway.go, redact.go and capture.go, "the request archive is on" is red ("masked 1 times, want masking false") and the observer case red (log masked). Not run: a real magpie serve with the archive on against a local model (ea53eb126's sandbox benchmark covers the archive-off case); a real S3 bucket; the GUI suite (no GUI file changed).
183 lines
4.5 KiB
Go
183 lines
4.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/yetone/magpie/internal/redact"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// Recent-call bodies are diagnostics, not an unbounded traffic log. Keeping the
|
|
// first 256 KiB is enough to inspect ordinary prompts and responses while
|
|
// preventing images or long streams from multiplying into tens of MiB across
|
|
// 40 calls.
|
|
const callBodyLimit = 256 << 10
|
|
|
|
// wholeBodyLimit asks a spool for every byte it is given, for a body the OTLP
|
|
// export wants uncut (#538).
|
|
const wholeBodyLimit = -1
|
|
|
|
type capturedBody struct {
|
|
buf bytes.Buffer
|
|
truncated bool
|
|
}
|
|
|
|
func (c *capturedBody) add(p []byte) {
|
|
if c.buf.Len() >= callBodyLimit {
|
|
c.truncated = c.truncated || len(p) > 0
|
|
return
|
|
}
|
|
n := min(len(p), callBodyLimit-c.buf.Len())
|
|
_, _ = c.buf.Write(p[:n])
|
|
if n < len(p) {
|
|
c.truncated = true
|
|
}
|
|
}
|
|
|
|
func (c *capturedBody) text() string { return c.buf.String() }
|
|
|
|
// captureRequestBody is a request's first callBodyLimit bytes, copied once:
|
|
// through a capturedBody it was copied into a buffer and then again into
|
|
// its string, 512 KiB for every request of 256 KiB or more.
|
|
func captureRequestBody(p []byte) (string, bool) {
|
|
return string(p[:min(len(p), callBodyLimit)]), len(p) > callBodyLimit
|
|
}
|
|
|
|
type captureResponseWriter struct {
|
|
http.ResponseWriter
|
|
body capturedBody
|
|
// full: the whole body, for the request archive, when it is on
|
|
full *spool
|
|
// whole: the whole body, for an observer that asks for it uncut (#538)
|
|
whole *spool
|
|
// tools: whether the reply calls a tool, for a turn's end (turns)
|
|
tools *toolScan
|
|
}
|
|
|
|
func (w *captureResponseWriter) Write(p []byte) (int, error) {
|
|
w.body.add(p)
|
|
w.tools.add(p)
|
|
if w.full != nil {
|
|
w.full.add(p)
|
|
}
|
|
if w.whole != nil {
|
|
w.whole.add(p)
|
|
}
|
|
return w.ResponseWriter.Write(p)
|
|
}
|
|
|
|
// spool keeps a body whole for the request archive, beyond the first 256
|
|
// KiB Recent calls holds: in a temporary file, not in memory, up to limit
|
|
// bytes; past that only its size is counted. A negative limit keeps every
|
|
// byte (#538). A file that can't be written leaves what was kept so far, cut.
|
|
type spool struct {
|
|
pattern string // empty uses the request archive
|
|
|
|
f *os.File
|
|
limit, kept int64
|
|
size int64 // every byte that came, kept or not
|
|
failed bool
|
|
}
|
|
|
|
func (s *spool) add(p []byte) {
|
|
s.size += int64(len(p))
|
|
if s.failed || (s.limit >= 0 && s.kept >= s.limit) || len(p) == 0 {
|
|
return
|
|
}
|
|
if s.f == nil {
|
|
pattern := s.pattern
|
|
if pattern == "" {
|
|
pattern = "magpie-archive-*"
|
|
}
|
|
f, err := os.CreateTemp("", pattern)
|
|
if err != nil {
|
|
s.failed = true
|
|
return
|
|
}
|
|
s.f = f
|
|
}
|
|
n := int64(len(p))
|
|
if s.limit >= 0 {
|
|
n = min(n, s.limit-s.kept)
|
|
}
|
|
if _, err := s.f.Write(p[:n]); err != nil {
|
|
s.failed = true
|
|
return
|
|
}
|
|
s.kept += n
|
|
}
|
|
|
|
// cut is whether less than the whole body was kept.
|
|
func (s *spool) cut() bool { return s.kept < s.size }
|
|
|
|
// read is what was kept, the file gone after.
|
|
func (s *spool) read() ([]byte, error) {
|
|
if s.f == nil {
|
|
return nil, nil
|
|
}
|
|
defer s.discard()
|
|
if _, err := s.f.Seek(0, io.SeekStart); err != nil {
|
|
return nil, err
|
|
}
|
|
return io.ReadAll(io.LimitReader(s.f, s.kept))
|
|
}
|
|
|
|
// discard removes the file: nothing is kept.
|
|
func (s *spool) discard() {
|
|
if s.f != nil {
|
|
s.f.Close()
|
|
os.Remove(s.f.Name())
|
|
s.f = nil
|
|
}
|
|
}
|
|
|
|
func (w *captureResponseWriter) Flush() {
|
|
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
|
f.Flush()
|
|
}
|
|
}
|
|
|
|
// withBodies hands a call's captured request and reply to its usage
|
|
// record while an observer wants them (the OTLP export's bodies, #538) —
|
|
// their secrets scrubbed as the request archive's are, on top of the
|
|
// placeholders Mask secrets puts in — and leaves the record without them
|
|
// otherwise. A call captured whole (usage.WholeBodies) carries them in
|
|
// wholeIn/wholeOut; otherwise the first 256 KiB Recent calls keeps is what
|
|
// goes.
|
|
func withBodies(rec *usage.Record, c *Call) {
|
|
if usage.ObservedBodies() == usage.NoBodies {
|
|
return
|
|
}
|
|
in, inCut := c.RequestBody, c.RequestTruncated
|
|
if c.wholeIn != nil {
|
|
in, inCut = string(c.wholeIn), c.wholeInCut
|
|
}
|
|
if c.maskIn != nil {
|
|
if m := c.maskIn.body(); c.wholeIn != nil {
|
|
in = string(m)
|
|
} else {
|
|
in, inCut = captureRequestBody(m)
|
|
}
|
|
}
|
|
out, outCut := c.ResponseBody, c.ResponseTruncated
|
|
if c.wholeOut != nil {
|
|
out, outCut = string(c.wholeOut), c.wholeOutCut
|
|
}
|
|
rec.BodyIn = bodyForExport(in, inCut)
|
|
rec.BodyOut = bodyForExport(out, outCut)
|
|
}
|
|
|
|
func bodyForExport(body string, cut bool) string {
|
|
if body == "" {
|
|
return ""
|
|
}
|
|
body = string(redact.ScrubJSON([]byte(body)))
|
|
if cut {
|
|
body += usage.BodyCut
|
|
}
|
|
return body
|
|
}
|