Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ repos:
hooks:
- id: trailing-whitespace
# The plans embed unified diffs, whose blank context lines are a lone space.
exclude: ^docs/superpowers/plans/
# Golden files hold exact output bytes. A fixer must not change them.
exclude: ^docs/superpowers/plans/|\.golden$
- id: end-of-file-fixer
exclude: \.golden$
- id: check-added-large-files
args: ['--maxkb=1024']
- id: check-yaml
Expand Down
19 changes: 19 additions & 0 deletions cmd/agentop/tui/detail_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ func withRaw(data []byte, raw *pipeline.RawCapture, e *pipeline.SessionEvent) []
case p.Body != "":
resp["body"] = bodyView(p.Body, nil)
}
if p.Upstream != nil || p.UpstreamBase64 != nil {
text := ""
if p.Upstream != nil {
text = *p.Upstream
}
resp["upstream"] = "server's reply before translation: " + formatBytes(int64(len(text)+len(p.UpstreamBase64)))
resp["upstreamBody"] = bodyView(text, p.UpstreamBase64)
if p.UpstreamTruncated {
resp["upstreamTruncated"] = true
}
}
r["response"] = resp
} else {
r["request"] = bodyView(raw.Request, raw.RequestBase64)
Expand All @@ -69,6 +80,14 @@ func withRaw(data []byte, raw *pipeline.RawCapture, e *pipeline.SessionEvent) []
default:
r["forwarded"] = "forwarded as sent"
}
if raw.Translated != nil || raw.TranslatedBase64 != nil {
text := ""
if raw.Translated != nil {
text = *raw.Translated
}
r["translated"] = "sent upstream, translated by Cortex: " + formatBytes(int64(len(text)+len(raw.TranslatedBase64)))
r["translatedBody"] = bodyView(text, raw.TranslatedBase64)
}
if len(raw.RequestHeaders) > 0 {
m["headers"] = flatHeaders(raw.RequestHeaders)
}
Expand Down
28 changes: 28 additions & 0 deletions cmd/agentop/tui/detail_raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,31 @@ func TestDetailRaw_ReopenShowsTheHeldCapture(t *testing.T) {
t.Fatalf("the reopened row shows raw %+v, want the held capture", m.detailRow.raw)
}
}

// A translated request shows the body Cortex sent upstream, and its reply the server's reply
// before translation, each with its own label: neither is a plugin's rewrite.
func TestDetailRaw_ShowsTheTranslatedHop(t *testing.T) {
tr := `{"model":"c","messages":[{"role":"user","content":[{"type":"text","text":"translated text"}]}]}`
out := rawDetail(t, &pipeline.RawCapture{Request: `{"model":"m"}`, Translated: &tr})
for _, want := range []string{"sent upstream, translated by Cortex: 95B", "translatedBody", "translated text", "forwarded as sent"} {
if !strings.Contains(out, want) {
t.Errorf("request detail lacks %q:\n%s", want, out)
}
}

ev := &pipeline.SessionEvent{Seq: 4, Phase: pipeline.SessionResponse, Direction: pipeline.Outbound, Host: "h",
Inference: &pipeline.InferenceExtension{Model: "m"}}
m := detailModel(t, "s1", ev)
up := `{"choices":[{"message":{"content":"chat reply"}}]}`
m.applyDetailEvent(detailEventLoadedMsg{sessionID: "s1", seq: 4, event: ev, raw: &pipeline.RawCapture{
Response: &pipeline.RawBody{Status: 200, Body: `{}`, Upstream: &up}}})
m.detailFolds = map[string]foldChoice{"response.raw": {open: true}, "response.raw.response": {open: true},
"response.raw.response.upstreamBody": {open: true}}
m.renderDetail()
got := stripANSI(strings.Join(lineTexts(m.detailLines), "\n"))
for _, want := range []string{"server's reply before translation: 50B", "upstreamBody"} {
if !strings.Contains(got, want) {
t.Errorf("response detail lacks %q:\n%s", want, got)
}
}
}
89 changes: 78 additions & 11 deletions core/listener/forwardproxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/rossoctl/cortex/core/spiffe"
"github.com/rossoctl/cortex/core/tlsbridge"
authtls "github.com/rossoctl/cortex/core/tlsconfig"
"github.com/rossoctl/cortex/core/translate"
)

// maxRequestBodySize caps a buffered request body. It is separate from
Expand Down Expand Up @@ -630,6 +631,8 @@ func (s *Server) serveOutbound(w http.ResponseWriter, r *http.Request, tl *tunne
sessionID = s.hydrateSession(pctx, trusted, r.Header, chain, connProcOf(r.Context()))
}

// tr is the conversion a plugin asked for with pctx.Translate, nil for none.
var tr *translation
if !skipped {
action := s.OutboundPipeline.Run(r.Context(), pctx)

Expand Down Expand Up @@ -666,6 +669,18 @@ func (s *Server) serveOutbound(w http.ResponseWriter, r *http.Request, tl *tunne
httpx.WriteRejectionForRequest(w, action, pctx)
return
}

// After every request plugin, and before the request row is recorded, so the
// modify/translated record is on that row. A refusal goes nowhere upstream.
var refusal *pipeline.Action
if tr, refusal = startTranslation(pctx); refusal != nil {
s.recordOutboundRejectIn(tl, pctx, *refusal, s.recordingSessionID(sessionID, r.Header, chain))
httpx.WriteRejectionForRequest(w, *refusal, pctx)
return
}
if tr != nil && capture != nil {
capture.Translated = tr.conv.Body
}
}

// A WebSocket upgrade is the one hop-by-hop request this proxy forwards
Expand Down Expand Up @@ -749,13 +764,17 @@ func (s *Server) serveOutbound(w http.ResponseWriter, r *http.Request, tl *tunne
// body-rewrite block below and the transport, so leave them untouched.
// Mirrors reverseproxy's forwarded-request header sync.
httpx.SyncRequestHeaders(r, pctx.Headers)
applyUpstreamHeaders(r, pctx.UpstreamHeaders())

// If a WritesRequestBody plugin rewrote pctx.Body, ship the new bytes
// upstream and clear Content-Encoding (see forwardproxy response
// path for the rationale).
if pctx.BodyMutated() {
httpx.SetRequestBody(r, pctx.Body)
}
if tr != nil {
tr.apply(r)
}

// The pipeline this request runs on for the rest of the handler: every
// capability probe below (encoding, streaming, buffering) must answer
Expand All @@ -772,7 +791,8 @@ func (s *Server) serveOutbound(w http.ResponseWriter, r *http.Request, tl *tunne
bufferResponse := !skipped && pinned.NeedsResponseBody() && (parsed || pinned.ReadsEveryResponseBody())

// Hop branch point 3: the hop always owns the encoding.
ownEncoding := hopped || bufferResponse || (!skipped && pinned.HasStreamingResponders())
// A translated reply is converted, so the transport must decode it first.
ownEncoding := hopped || bufferResponse || tr != nil || (!skipped && pinned.HasStreamingResponders())
stripForUpstream(r, ownEncoding, forwardUpgrade)

// Clear RequestURI — set by the server but must be empty for client requests
Expand Down Expand Up @@ -840,7 +860,7 @@ func (s *Server) serveOutbound(w http.ResponseWriter, r *http.Request, tl *tunne
// itself, with whatever credential it has now. Left as a request-keyed rule
// rather than made cleverer (#1549 review).
if err == nil && !skipped && !forwardUpgrade {
resp, err = s.resendIfAsked(r, pctx, client, resp, tl, ownEncoding, capture)
resp, err = s.resendIfAsked(r, pctx, client, resp, tl, ownEncoding, capture, tr)
}
if err != nil {
// The egress guard's refusal, BEFORE the generic 502: a destination that
Expand Down Expand Up @@ -908,6 +928,22 @@ func (s *Server) serveOutbound(w http.ResponseWriter, r *http.Request, tl *tunne
}
defer resp.Body.Close()

// The session archive's capture of the response (#1494), only when the request was
// captured, so a response never outlives its pair: the request row keeps its capture
// only for inference (recordCapturedRequest).
var rc *pipeline.ResponseCapture
if capture != nil && pctx.Extensions.Inference != nil {
rc = &pipeline.ResponseCapture{Status: resp.StatusCode, Decoded: resp.Uncompressed}
}

// Before the wrap: a not-streamed translated reply is read whole there.
headersAt := time.Now()
// Before any reply plugin or recorder reads the body: they all see the reply in
// the agent's protocol. A hop's own 503 returned above and is not converted.
if tr != nil {
tr.wrapReply(resp, rc)
}

// Response phase: populate pctx and run plugins in reverse order.
pctx.StatusCode = resp.StatusCode
pctx.ResponseHeaders = resp.Header.Clone()
Expand All @@ -918,7 +954,7 @@ func (s *Server) serveOutbound(w http.ResponseWriter, r *http.Request, tl *tunne
// own flow — the moment the delivered response's headers are in hand —
// so ttfb means one thing wherever a row reports it. Stays zero on the
// error path above, where no headers ever arrived (#1251).
pctx.ResponseHeadersAt = time.Now()
pctx.ResponseHeadersAt = headersAt

// BytesDown for the response row (#1309), counted HERE and nowhere else. Every
// arm below reads this one body — buffered whole, relayed SSE block by block, relayed
Expand All @@ -939,10 +975,9 @@ func (s *Server) serveOutbound(w http.ResponseWriter, r *http.Request, tl *tunne
//
// The same wrapper keeps the body for the session archive's capture (#1494): every arm
// reads through it, so what it keeps is the body as it came off the wire, whatever arm
// relays it. Only when the request was captured, so a response never outlives its pair: the
// request row keeps its capture only for inference (recordCapturedRequest).
if capture != nil && pctx.Extensions.Inference != nil {
rc := &pipeline.ResponseCapture{Status: resp.StatusCode, Header: redact.Headers(resp.Header), Decoded: resp.Uncompressed}
// relays it. For a translated reply that is the reply in the agent's protocol.
if rc != nil {
rc.Header = redact.Headers(resp.Header)
pctx.ResponseCapture = rc
resp.Body = bodycount.WrapCapture(resp.Body, &pctx.ResponseBytes, &rc.Body, &rc.Truncated, &rc.Ended, pipeline.MaxResponseCapture)
} else {
Expand Down Expand Up @@ -1260,7 +1295,11 @@ func stripForUpstream(r *http.Request, ownEncoding, keepUpgrade bool) {
// id, so the timeline shows both attempts and each pairs with its own answer. A
// refused answer that is not sent again goes to the client byte for byte, the bytes
// read for the Resender included.
func (s *Server) resendIfAsked(r *http.Request, pctx *pipeline.Context, client *http.Client, resp *http.Response, tl *tunnelLog, ownEncoding bool, capture *pipeline.RequestCapture) (*http.Response, error) {
//
// tr is the translation of the request, nil for none. The Resender reads the raw
// answer, the request is converted again after the Resender changed it, and the
// refused answer's row has the reply in the agent's protocol.
func (s *Server) resendIfAsked(r *http.Request, pctx *pipeline.Context, client *http.Client, resp *http.Response, tl *tunnelLog, ownEncoding bool, capture *pipeline.RequestCapture, tr *translation) (*http.Response, error) {
// Both probes and Resend itself answer for the pipeline this request
// ran on (Holder.ForPctx): the reload-drain window must not flip the
// resend decision onto plugins that never saw the request.
Expand All @@ -1272,7 +1311,19 @@ func (s *Server) resendIfAsked(r *http.Request, pctx *pipeline.Context, client *
// The parse as the refused attempt was sent: a Resender may rewrite the model,
// and the refused attempt's row must name the one the upstream refused.
sent := pipeline.SnapshotInference(pctx.Extensions.Inference)
if !s.OutboundPipeline.Resend(r.Context(), pctx, resp.StatusCode, head) {
asked := s.OutboundPipeline.Resend(r.Context(), pctx, resp.StatusCode, head)
var conv translate.Converted
if asked && tr != nil {
var convErr error
// A request that cannot be converted after the change is not sent again.
conv, convErr = tr.convert(pctx)
asked = convErr == nil
if !asked {
// The answer goes back for the request sent, so its row names that model.
pctx.Extensions.Inference = sent
}
}
if !asked {
resp.Body = struct {
io.Reader
io.Closer
Expand All @@ -1285,7 +1336,11 @@ func (s *Server) resendIfAsked(r *http.Request, pctx *pipeline.Context, client *
// back at once, because the status of the answer the client gets is the
// one OutcomeFromContext must read.
resent := pctx.Extensions.Inference
pctx.StatusCode, pctx.ResponseBody, pctx.Extensions.Inference = resp.StatusCode, head, sent
refused := head
if tr != nil {
refused = tr.replyBody(resp, head)
}
pctx.StatusCode, pctx.ResponseBody, pctx.Extensions.Inference = resp.StatusCode, refused, sent
if capture != nil && pctx.Extensions.Inference != nil {
// The refusal as far as it was read: the resend peeks at most ResendPeekLimit bytes.
pctx.ResponseCapture = &pipeline.ResponseCapture{Status: resp.StatusCode, Header: redact.Headers(resp.Header),
Expand All @@ -1294,11 +1349,23 @@ func (s *Server) resendIfAsked(r *http.Request, pctx *pipeline.Context, client *
s.recordOutboundResponseEvent(pctx, resp.StatusCode, nil)
pctx.StatusCode, pctx.ResponseBody, pctx.Extensions.Inference = 0, nil, resent
pctx.RenewRequestID()
if tr != nil && capture != nil {
// A copy: the refused attempt's row keeps the body it sent.
again := *capture
again.Translated = conv.Body
capture = &again
}
s.recordCapturedRequest(tl, pctx, pctx.OutboundSessionID, capture)
}
again := r.Clone(r.Context())
httpx.SyncRequestHeaders(again, pctx.Headers)
httpx.SetRequestBody(again, pctx.Body)
applyUpstreamHeaders(again, pctx.UpstreamHeaders())
if tr != nil {
tr.conv = conv
tr.apply(again)
} else {
httpx.SetRequestBody(again, pctx.Body)
}
// No keepUpgrade: a resent request is never a forwarded upgrade — the
// 101 branch in serveOutbound skips resendIfAsked entirely, and only that
// branch leaves the headers unstripped.
Expand Down
Loading
Loading