Documentation
¶
Overview ¶
Package quarantine is the one place where a model reads partner text (control M5, flow P3).
Input: a note (the stored partner text, its canary and the ids for the cost record) and the id of a closed schema from internal/contracts/notes. Output: JSON that matches that schema and nothing else, or ErrInvalid.
The model has no tools and sees one note. Its answer is trusted only after three checks: it is JSON, it matches the closed schema (enums, booleans and bounded integers, no extra fields), and the raw text does not contain the note's canary (a model that repeats its input repeats the canary too). Any failure, a network error, a timeout and a missing model in the config give ErrInvalid and return nothing; the gateway keeps working.
Every call that reached the model leaves one row in quarantine_calls: tokens, latency and outcome by trace id, never the note text or the answer.
Index ¶
Constants ¶
const ( OutcomeOK = "ok" OutcomeTimeout = "timeout" OutcomeUpstream = "upstream_error" OutcomeCanary = "canary" OutcomeInvalid = "invalid_answer" )
Outcomes written to quarantine_calls.
const DefaultTimeout = 10 * time.Second
DefaultTimeout bounds one call to the model from the request to the last byte.
Variables ¶
var ErrInvalid = errors.New("quarantine: no valid answer")
ErrInvalid is the only error a reader gives for a bad or missing answer.
Functions ¶
This section is empty.
Types ¶
type Completer ¶
type Completer interface {
Complete(ctx context.Context, req Request) (Completion, error)
}
Completer talks to a model. HTTPCompleter is the real one, Fake the test one.
type Completion ¶
Completion is the raw answer of a model and the token counts it reported (nil when it reported none).
type Fake ¶
type Fake struct {
// contains filtered or unexported fields
}
Fake is a Completer for tests. The zero value answers with an empty text.
type HTTPCompleter ¶
type HTTPCompleter struct {
BaseURL string // for example http://127.0.0.1:11434/v1
APIKey string // sent as a Bearer token only when set
Client *http.Client
}
HTTPCompleter calls an OpenAI-compatible chat API (OpenAI, OpenRouter, Ollama under /v1) with net/http and no SDK. It never sends tools. Errors carry no text from the model or from the note.
func NewHTTPCompleter ¶
func NewHTTPCompleter(baseURL, apiKey string) *HTTPCompleter
NewHTTPCompleter builds a completer; the timeout comes from the context of each call.
func (*HTTPCompleter) Complete ¶
func (c *HTTPCompleter) Complete(ctx context.Context, req Request) (Completion, error)
Complete posts one chat request with temperature 0, at most 256 tokens and the closed schema as the response format.
type Note ¶
type Note struct {
TenantID string
TraceID string
DocumentID string
Ref string
Text string
Canary string
}
Note is what a reader gets: the text, its canary, and the ids that go into the cost record.
type QuarantinedReader ¶
type QuarantinedReader interface {
Extract(ctx context.Context, n Note, schemaID string) (json.RawMessage, error)
}
QuarantinedReader turns one note into the values of a closed schema.
type Reader ¶
Reader is the QuarantinedReader on top of a Completer. Timeout starts as DefaultTimeout; a test may shorten it.
func NewReader ¶
NewReader builds a reader. An empty model switches it off: Extract then gives ErrInvalid without a call.
type Recorder ¶
type Recorder interface {
RecordQuarantineCall(ctx context.Context, c store.QuarantineCall) error
}
Recorder stores the cost record of a call.
type Request ¶
type Request struct {
Model string
System string
User string
SchemaID string
Schema json.RawMessage
}
Request is one chat call to a model: no tools, one closed schema.