Documentation
¶
Overview ¶
Package tokclient is the harness's REAL-tokenizer path: it asks the serving endpoint's own /tokenize route how a text actually tokenizes, instead of estimating with chars/4. The house has been burned by estimates before — three real transcripts were rejected with exceed_context_size while the chars/4 ladder declined to compact (ADR 0017), and a truncation misdiagnosis cost a full debugging session (truncation-needs-context-check, 2026-08-08). Estimates stay acceptable for BUDGET headroom; any decision that CUTS content at an exact boundary must use this package.
Endpoint shape mirrors internal/agent's /props probe (window.go): llama.cpp serves POST /tokenize at the server root, and llama-swap proxies it per model under /upstream/{model}/tokenize (which may cold-start the model — acceptable for the same reason as the window probe: the caller is about to use exactly that model — and never onto a card a GPU lease fences: the passthrough is built by modelaffinity.AwaitUpstream, which refuses it while a render or an exclusive hold owns the card and the seat is not resident). A trailing /v1 on base is stripped via the one endpoint-normalization rule in internal/swapclient.
FAIL-OPEN CONTRACT: every method returns ok=false on any failure (transport, non-200, malformed payload, a piece accounting that does not reconstruct the input). Callers fall back to their estimate-driven path and MUST NOT fail the run because tokenization was unanswerable — a generic OpenAI endpoint has no /tokenize and that is fine.
Index ¶
Constants ¶
const DefaultTimeout = 60 * time.Second
DefaultTimeout bounds one /tokenize round-trip. Warm, tokenizing tens of KB is milliseconds; the generous bound exists because /upstream/{model}/ may cold-start the model first (same rationale as the window probe's 60s).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client tokenizes text with one served model's own tokenizer.
func New ¶
New builds a client for the model served at base (any /v1 suffix is stripped). timeout <= 0 uses DefaultTimeout.
func NewUpstreamOnly ¶
NewUpstreamOnly builds a client that tries ONLY the llama-swap per-model passthrough — never the bare-server root fallback. The root route answers for WHATEVER model is currently loaded, so in a multi-model process (the cascade) a stale per-model alias falling through to the root would count and cut with the PREVIOUS tier's tokenizer — silently, cached under the callee's key (TO-3 round-1 review finding 2026-08-14: the real cross-model contamination channel). Single-model agent paths keep New's fallback.
func (*Client) Count ¶
Count returns how many tokens text costs under the served model's tokenizer (content tokens only — add_special false). The light call for budget arithmetic: no pieces on the wire.
func (*Client) LastErr ¶
LastErr reports why the most recent failing call failed ("" = none yet). Consulted by the agent loop's sticky wrapper at downgrade time so the degradation is reportable, not just observable-by-absence.
func (*Client) LastFailDefinitive ¶
LastFailDefinitive reports whether the most recent failure was a definitive route absence (all candidates 404/405) rather than a transient failure.
func (*Client) LastFailFenced ¶
LastFailFenced reports whether the most recent failure was the GPU-lease fence refusing the per-model passthrough rather than the endpoint failing. The agent loop's sticky wrapper never counts such a failure toward a downgrade: the card frees when the render ends and the route is healthy.
func (*Client) Pieces ¶
Pieces tokenizes text and returns the BYTE LENGTH of every token's piece, in token order. sum(lengths) == len(text) is verified here — if the pieces do not reconstruct the input exactly, the byte→token mapping a caller would build from them is unreliable, so the whole call fails open instead of handing back a mapping that could cut mid-message.