Documentation
¶
Overview ¶
Package tasks builds the per-task prompt, grammar, and validation schema.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrParamsNotKeyable = errors.New("params cannot be keyed for the result cache")
ErrParamsNotKeyable is what CacheParamsKey wraps when params have no canonical typed form. A caller must then skip the result cache for that call altogether.
Functions ¶
func CacheParamsKey ¶ added in v0.206.0
CacheParamsKey is the params ingredient of a RESULT-CACHE key: the params rendered as JSON with the keys of every map sorted at every depth, typed, so values that differ in type or in structure render differently. {"seed": "5"} and {"seed": 5} differ, []any{"a", "b"} and []any{"a b"} differ, a nested map holding "k2:v2" and a map with a second entry differ.
It exists because StableParamsKey cannot do this job. That function prints values with %v, which is type-erased and space-separated, so a request could shape a nested value of its params (an extract schema's "required" list) to render exactly like another request's, get the same cache key, and be served that request's cached answer before its own schema was ever checked. StableParamsKey stays for output-file stems and the exemplar rows, where a changed string would move file names; the result cache uses this.
The same logical params render the same string whatever Go type carried them: an int 5 and the float64 5 that the MCP door decodes from JSON are both 5, and a []string and a []any holding the same strings are the same list. Empty or nil params render "".
It FAILS CLOSED. Values with no faithful typed form return an error wrapping ErrParamsNotKeyable, and the caller must then neither read nor write the result cache for that call; falling back to %v or StableParamsKey would reopen the collision this function closes. Those values are: NaN and infinities (JSON has no form for them), functions, channels, complex numbers, structs, byte slices (JSON would render them as a base64 string, indistinguishable from a string), any type with its own JSON or text form (time.Time and the like), maps whose keys are not strings, strings that are not valid UTF-8 (JSON would silently replace the bad bytes, so two different strings would render alike), and values nested deeper than a cycle-guard bound. The error names the params key and the kind of the offender, never a value.
func ShapeInstruction ¶ added in v0.206.0
ShapeInstruction states the exact JSON object a grammar would have forced: the keys in order, each one's type (an enum's allowed values), the rule that no other key may appear, and one compact example. It is deterministic, because it rides in the system prompt and so in the prompt-prefix fingerprint.
func StableParamsKey ¶
StableParamsKey renders params deterministically: "<key>=<value>;" per entry, in key order, the value as %v prints it.
It names the render-, video- and edit- output files (a stem is a hash of the prompt and this string) and it is recorded on the persisted exemplar rows. It is NOT part of the result-cache key: that carries CacheParamsKey, the typed canonical form, because the %v rendering below is type-erased and let a request shape its params to share another request's cache entry.
The format alone left the top-level structure ambiguous: a key or a value holding "=" or ";" could stand in for the structure around it, so {"a": "1;b=2"} and {"a": "1", "b": "2"} were both "a=1;b=2;". Each key and each rendered value is therefore percent-escaped (paramsEscaper) before it is written, which makes every top-level entry unambiguous.
That is the whole guarantee. Nested and non-string values keep Go's %v rendering, which is type-erased and separates elements with a space: {"seed": "5"} and {"seed": 5} render alike, so do []any{"a b"} and []any{"a", "b"}, and so do a map value that holds "k2:v2" and a map with a second entry. That limit is documented and pinned by TestStableParamsKeyNestedAndNonStringValuesKeepGoRendering; lifting it would give every params map with such a value a new file name. It matters only for file stems and exemplar rows now that the result cache has its own key.
Nor does the escaping make output file names injective. A stem is sha256hex(prompt + this string)[:8] with no boundary between the prompt and the string, so the two can still run together. That is older than the escaping and out of its scope: a stem collision only reuses or overwrites a file under a truncated 32-bit name.
The output is byte-identical to the unescaped format for every map whose keys and rendered values contain none of "%", ";" and "=" — prompts, numbers, bools, paths of either slash direction (a Windows path with backslashes included), nested maps and slices of those — so the file names and stored keys of ordinary params did not move. A key or value that does contain one of the three renders differently from before (a negative prompt with a semicolon, a URL with a query string such as "?a=b", a percent-encoded URL), and its output stem and recorded key change once.
Types ¶
type Built ¶
type Built struct {
System string
User string
Grammar string
// Fields is the SAME field list Grammar was compiled from, carried
// alongside it rather than re-derived: a vLLM seat is constrained by
// gbnf.JSONSchema(Fields) instead, because vLLM accepts and DISCARDS
// llama.cpp's `grammar` field (register D-129). Nil on the free-text
// tasks that set no grammar at all, and every builder that sets one sets
// both, pinned by TestBuiltFieldsMatchTheGrammar: the two engines can
// never be handed different shapes for one task.
Fields []gbnf.Field
Schema map[string]any // validation schema (extract); nil when grammar suffices
// Strict is set only by ForUnconstrained: the schema derived from Fields (gbnf.JSONSchema)
// that an unconstrained seat's reply must satisfy, because no grammar enforced it. Nil for
// every seat that took a grammar, which is why a normal build is unchanged.
Strict map[string]any
MaxTokens int
}
Built is everything the pipeline needs to run one task.
func BuildFor ¶ added in v0.206.0
BuildFor is Build for a seat with the given capabilities. For a seat that takes a grammar the result is Build's, unchanged, byte for byte. For an unconstrained seat a task that carries a field list (classify, extract; summarize and triage also build one) comes back with no grammar, the exact shape in the system prompt, and Strict set.
func (Built) ForUnconstrained ¶ added in v0.206.0
ForUnconstrained rewrites an already-built (and possibly exemplar-decorated or re-packed) task for a seat that cannot constrain decoding: Grammar is cleared, the shape instruction is appended to System only (User, and so any injected exemplars and the packed input, are untouched), and Strict is the schema every field of the grammar would have enforced — all keys required, their types, a classify label inside the allowed set, and no other key. A build with no field list (free-text tasks) is returned as it is.
type Caps ¶ added in v0.206.0
type Caps struct {
// Unconstrained: the seat's runtime cannot constrain decoding (the RKLLM runtime refuses any
// grammar / json_schema / response_format with HTTP 400 and ignores logprobs). The JSON shape
// the grammar would have forced is then stated in the prompt, and the reply is validated
// strictly against Built.Strict instead.
Unconstrained bool
}
Caps is what the seat a prompt is built for can do. The zero value is every seat that takes a grammar, and BuildFor(req, Caps{}) is byte-identical to Build(req).