quarantine

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	OutcomeOK       = "ok"
	OutcomeTimeout  = "timeout"
	OutcomeUpstream = "upstream_error"
	OutcomeCanary   = "canary"
	OutcomeInvalid  = "invalid_answer"
)

Outcomes written to quarantine_calls.

View Source
const DefaultTimeout = 10 * time.Second

DefaultTimeout bounds one call to the model from the request to the last byte.

Variables

View Source
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

type Completion struct {
	Text             string
	PromptTokens     *int
	CompletionTokens *int
}

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.

func (*Fake) Complete

func (f *Fake) Complete(ctx context.Context, req Request) (Completion, error)

Complete implements Completer.

func (*Fake) Last

func (f *Fake) Last() (Request, int)

Last returns the last request the fake received and how many it got.

func (*Fake) Set

func (f *Fake) Set(s Script)

Set replaces the script; it is safe to call while requests are served.

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

type Reader struct {
	Completer Completer
	Model     string
	Timeout   time.Duration
	Recorder  Recorder
}

Reader is the QuarantinedReader on top of a Completer. Timeout starts as DefaultTimeout; a test may shorten it.

func NewReader

func NewReader(c Completer, model string, rec Recorder) *Reader

NewReader builds a reader. An empty model switches it off: Extract then gives ErrInvalid without a call.

func (*Reader) Extract

func (r *Reader) Extract(ctx context.Context, n Note, schemaID string) (json.RawMessage, error)

Extract asks the model about the note and returns the checked values. It returns ErrInvalid for every failure of the model side; an error from the recorder comes back as it is, since a call without a cost record is a fault of the gateway.

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.

type Script

type Script struct {
	Text             string
	PromptTokens     *int
	CompletionTokens *int
	Err              error
	Hang             bool
}

Script is what a Fake does on its next calls: answer with Text (the placeholder {{canary}} becomes the marker found in the user message, to play a model that repeats its input), fail with Err, or, when Hang is set, wait until the context ends.

Jump to

Keyboard shortcuts

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