From ee3b00f7251bf40997ab1d22a818199bd24ba40e Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 15:12:00 -0600 Subject: [PATCH 1/2] docs(mcpx): land the #4375 response-read contract + bound the embeddings error excerpt (#4376) --- pkgs/mcpx/internal/catalog/embeddings.go | 4 ++- pkgs/mcpx/internal/defaults/defaults.go | 32 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pkgs/mcpx/internal/catalog/embeddings.go b/pkgs/mcpx/internal/catalog/embeddings.go index 61dba54327..085934f6d3 100644 --- a/pkgs/mcpx/internal/catalog/embeddings.go +++ b/pkgs/mcpx/internal/catalog/embeddings.go @@ -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)) } diff --git a/pkgs/mcpx/internal/defaults/defaults.go b/pkgs/mcpx/internal/defaults/defaults.go index ef3a56aa20..5883838c5f 100644 --- a/pkgs/mcpx/internal/defaults/defaults.go +++ b/pkgs/mcpx/internal/defaults/defaults.go @@ -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 From d9e124aed666efb041747b9eba34fa215d16d2b0 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 15:39:43 -0600 Subject: [PATCH 2/2] test(mcpx/catalog): bound oracle for the embeddings error excerpt (#4376) --- .../catalog/embeddings_error_bound_test.go | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 pkgs/mcpx/internal/catalog/embeddings_error_bound_test.go diff --git a/pkgs/mcpx/internal/catalog/embeddings_error_bound_test.go b/pkgs/mcpx/internal/catalog/embeddings_error_bound_test.go new file mode 100644 index 0000000000..41bb84c6ac --- /dev/null +++ b/pkgs/mcpx/internal/catalog/embeddings_error_bound_test.go @@ -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) + } +}