Skip to content
Merged
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 pkgs/mcpx/internal/catalog/embeddings.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ func (r *RemoteEmbedder) Embed(ctx context.Context, text string) (Vector, error)
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
// Contract decision 1 (#4375): an error body is an excerpt
// at HTTPErrorBodyLimit, never a whole read.
body, _ := io.ReadAll(io.LimitReader(resp.Body, defaults.HTTPErrorBodyLimit))
return nil, fmt.Errorf("remote embeddings error %d: %s", resp.StatusCode, string(body))
}

Expand Down
41 changes: 41 additions & 0 deletions pkgs/mcpx/internal/catalog/embeddings_error_bound_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package catalog

// lane-scratch: code-w /tmp/lane-w-20261110-1514 task 13050

import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/developing-today/code/pkgs/mcpx/internal/defaults"
)

// Oracle: response-read contract decision 1 (design #4375, closing
// audit #4376) — a non-200 embeddings body is read as an excerpt
// bounded at defaults.HTTPErrorBodyLimit, never as a whole read. A
// server returning far more than the limit must produce an error
// whose excerpt is exactly the limit, byte for byte.
func TestRemoteEmbedderErrorBodyBounded(t *testing.T) {
oversized := strings.Repeat("x", int(defaults.HTTPErrorBodyLimit)+10000)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(oversized))
}))
defer srv.Close()

rem := NewRemoteEmbedder(srv.URL, "test-key", "test-model")
_, err := rem.Embed(context.Background(), "hello")
if err == nil {
t.Fatal("expected an error from the 500 response")
}
const prefix = "remote embeddings error 500: "
if !strings.HasPrefix(err.Error(), prefix) {
t.Fatalf("unexpected error shape: %.60q", err.Error())
}
excerpt := strings.TrimPrefix(err.Error(), prefix)
if int64(len(excerpt)) != defaults.HTTPErrorBodyLimit {
t.Fatalf("error excerpt is %d bytes, want exactly the %d-byte HTTPErrorBodyLimit", len(excerpt), defaults.HTTPErrorBodyLimit)
}
}
32 changes: 32 additions & 0 deletions pkgs/mcpx/internal/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,38 @@ var (
UpstreamEraFileVersion = builtin.Upstream.EraFileVersion
// HTTPErrorBodyLimit bounds how much of a non-2xx body is read: enough
// for a JSON-RPC error, not enough for an HTML error page to matter.
//
// The response-read contract (design #4375, closing audit #4376)
// governs every outbound call mcpx makes; the next outbound call
// site is expected to follow it:
//
// 1. Error bodies are excerpts: a non-2xx body is read through
// io.LimitReader at HTTPErrorBodyLimit, never whole.
// 2. A buffered success body is refused past its named cap,
// never silently truncated: read cap+1 and fail the call when
// the extra byte exists (see ProviderResponseBodyLimit).
// 3. A retained copy is truncated while the delivered stream is
// not: when a response is both relayed and kept (feedback
// capture, diagnostics), the kept copy goes through a capped
// buffer and the stream itself flows whole.
// 4. Every outbound call carries a deadline as well as a byte
// bound: an http.Client timeout or a context deadline, set at
// the call site or by the shared client it uses.
//
// Audit summary (#4376, 2026-11-10; full inventory recorded on
// #4375): the provider paths (internal/model, internal/scripting),
// the mcpclient transports, and the daemon proxy's retained copy
// conform. The one unbounded peer error read the audit found —
// internal/catalog/embeddings.go — was fixed to decision 1 in the
// audit itself. Reads in internal/cli target mcpx's own daemon
// endpoint and are deliberately local, except where an endpoint
// is operator-configured, which joins this contract.
// Decoder-streamed success paths conform as streaming; the bound
// on the decoded value itself is an open question recorded on
// #4375, not settled here. Stdin and file reads are not peer
// bytes and are out of scope. Inbound request bodies are a
// separate surface, capped at their handlers (MaxBytesReader /
// LimitReader), and are not governed by this contract.
HTTPErrorBodyLimit = mustBytes(builtin.Upstream.ErrorBodyLimit, "upstream.errorBodyLimit")
// ProviderResponseBodyLimit bounds how much of a model provider's
// response body is read on the success path: a chat-completion
Expand Down
Loading