inlinefit

package
v1.130.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package inlinefit renders a tool result within a budget on the rendered result rather than on the bytes a handler read.

A budget applied to an upstream read does not bound what the client receives. Between the two sit the JSON envelope the handler wraps the body in and the indentation the result is rendered with, and a parsed JSON body re-rendered indented is several times the size of the compact bytes it was read from. Issue #1606 measured a 26,809-byte JSON response, comfortably inside a 32 KiB read budget and therefore carrying no truncation flag and no steer, render as a 64,238-character tool result, past the size the client refuses.

Fitting spends the cheapest lever first. The indented rendering is returned when it fits. Otherwise the compact one is, because indentation is whitespace and dropping it costs the caller nothing where the alternative is dropping content. Only when neither fits is the body cut, which is the case a caller flags and steers to a streamed export.

What is measured is the result's text, which is what issue #1606 measured a client refusing. The MCP SDK also marshals the same value into the result's structured output, so the wire message carries the body a second time, compactly; a client that counts the whole message rather than the text it renders sees roughly twice the budget. That copy is not removable here -- a managed script reads the structured output, and the call reference the platform stamps rides on it -- so it is deliberately outside this budget rather than unaccounted for.

Index

Constants

View Source
const DefaultReserve = int64(4096)

DefaultReserve is the headroom a merge keeps under a budget for the envelope its collection is wrapped in: the status, the headers, the pagination signal, the hint and any echoed arguments. A merge that admits whole pages has to leave room before accepting one rather than cut afterwards, since cutting would leave a resume signal pointing past content the caller never received.

Variables

This section is empty.

Functions

func BodyText

func BodyText(v any) string

BodyText is a response body as the text a cut body carries: a string body is itself, and a parsed value is its compact JSON, the form the upstream sent it in. An absent body has no text, rather than the "null" a marshal would give it, so a fit cannot invent one.

func Fit

func Fit(v any, budget int, body string, setBody func(string)) []byte

Fit renders v within budget, shortening body through setBody when re-encoding alone is not enough, and returns the rendering to hand back. The body kept is the longest prefix found that fits.

func ItemsSize

func ItemsSize(items []json.RawMessage) int64

ItemsSize measures a merged collection the way a result renders it, so a merge stops at a page boundary that keeps the whole result inside the budget instead of at a count of compact bytes. A collection that cannot be rendered measures as unbounded, so a merge refuses it rather than admitting it under a cap that could not be checked.

func NeedsCut

func NeedsCut(v any, budget int) bool

NeedsCut reports whether v can be rendered within budget without dropping content. A caller sets its truncation flags from this before calling Fit, so the rendering Fit measures is the one it returns.

func Render

func Render(v any) ([]byte, error)

Render renders v the way a tool result carries it.

func RenderWithin

func RenderWithin(v any, budget int) ([]byte, bool)

RenderWithin renders v within budget by the cheapest lever that works: the indented rendering when it fits, the compact one when it does not. The bool reports whether the returned rendering is within the budget. A budget of zero or less fits everything.

func Reserve

func Reserve(limit int64) int64

Reserve is the envelope headroom to keep under a budget, capped at a quarter of it so a small budget still merges pages instead of having its first one refused by a reserve larger than the budget itself.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL