Help us shape the future of Go! Take the 2026 Go Developer Survey

semantic

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package semantic observes possible completed-answer reuse. Shadow decisions are advisory: they never serve text, change claims, or authorize generation.

Index

Constants

View Source
const (
	DefaultVerifierMaxInputTokens  int64 = 8192
	DefaultVerifierMaxOutputTokens int64 = 128
	DefaultVerifierTimeoutMS             = 30000
	MaxVerifierInputTokens         int64 = 32768
	MaxVerifierOutputTokens        int64 = 512
	MaxVerifierTimeoutMS                 = 120000
	MaxWaitForCandidateMS                = 30000
	MaxVerdictBytes                      = 4096
	// ProtectedTermsVersion identifies the lexical guard, independently of a
	// verifier prompt or any observed quality result.
	ProtectedTermsVersion = 2
)
View Source
const (
	DefaultMinSimilarity = 0.9
	DefaultTopK          = 3
	DefaultMaxScan       = 1000
	DefaultMaxEntries    = 1000
	DefaultMaxInputBytes = 8192
	DefaultTimeoutMS     = 10000
	MaxTopK              = 10
	MaxScan              = 10000
	MaxEntries           = 10000
	MaxInputBytes        = 32768
	MaxTimeoutMS         = 60000
	MaxDimensions        = 4096
)

Variables

This section is empty.

Functions

func CheckProtectedTerms added in v0.15.0

func CheckProtectedTerms(original, current string) []string

CheckProtectedTerms rejects differences in explicit quantities, code-like identifiers, URLs, quoted text, negation, symbolic operators and quantity units. Ordinary sentence capitalization and natural-language constraint words are not symbolic identifiers/operators. Proper-name hints are compared when both questions supply them; an omitted name must be resolved by the mandatory verifier against the task's bindings and evidence. Empty is not proof of equivalent intent or a correct answer. Unsupported, malformed or oversized text fails closed before extraction. Conservative false rejections remain.

func Cosine

func Cosine(a, b []float32) (float64, error)

Cosine compares finite, nonzero vectors with matching bounded dimensions. float64 accumulation avoids overflowing when finite float32 values are large.

func EntryID

func EntryID(e Entry) string

EntryID binds an entry to its exact source snapshot within one embedding partition. Vectors and expiry do not create duplicate copies of that snapshot.

func ValidatePut

func ValidatePut(p PutRequest) error

ValidatePut verifies identities, bounded vectors and the short-lived source pointer. The source artifact itself must also be checked by the index owner.

func ValidateQuery

func ValidateQuery(q Query) error

ValidateQuery checks the fully specified wire request; defaults belong to NormalizeConfig and are deliberately not inferred at the broker boundary.

Types

type Candidate

type Candidate struct {
	Source     Source    `json:"source"`
	Similarity float64   `json:"similarity"`
	CreatedAt  time.Time `json:"created_at"`
	ExpiresAt  time.Time `json:"expires_at"`
}

type Config

type Config struct {
	Mode             string            `json:"mode"`
	Family           string            `json:"family"`
	Domain           string            `json:"domain"`
	EvidenceRevision string            `json:"evidence_revision"`
	Bindings         map[string]string `json:"bindings,omitempty"`
	EmbeddingModel   string            `json:"embedding_model"`
	MinSimilarity    float64           `json:"min_similarity,omitempty"`
	TopK             int               `json:"top_k,omitempty"`
	MaxScan          int               `json:"max_scan,omitempty"`
	MaxEntries       int               `json:"max_entries,omitempty"`
	MaxInputBytes    int               `json:"max_input_bytes,omitempty"`
	TimeoutMS        int               `json:"timeout_ms,omitempty"`
}

Config is opt-in. Mode accepts only "shadow" in this release.

func NormalizeConfig

func NormalizeConfig(c Config) (Config, error)

NormalizeConfig fills bounded defaults and rejects modes that could serve an answer. The returned bindings are a copy so later caller edits cannot alter it.

type Embedder

type Embedder interface {
	Embed(context.Context, string) (Embedding, error)
}

type Embedding

type Embedding struct {
	ModelID     string    `json:"model_id"`
	Vector      []float32 `json:"-"`
	InputTokens *int64    `json:"input_tokens"`
	DurationMS  float64   `json:"duration_ms"`
}

type Entry

type Entry struct {
	ID        string    `json:"id"`
	Partition string    `json:"partition"`
	ModelID   string    `json:"model_id"`
	Vector    []float32 `json:"vector"`
	Source    Source    `json:"source"`
	CreatedAt time.Time `json:"created_at"`
	ExpiresAt time.Time `json:"expires_at"`
}

type Index

type Index interface {
	QuerySemantic(context.Context, Query) (SearchResult, error)
	PutSemantic(context.Context, PutRequest) error
}

type Observation

type Observation struct {
	Mode                 string        `json:"mode"`
	Status               string        `json:"status"`
	Partition            string        `json:"partition,omitempty"`
	EmbeddingModelID     string        `json:"embedding_model_id,omitempty"`
	EmbeddingInputTokens *int64        `json:"embedding_input_tokens"`
	EmbeddingMS          float64       `json:"embedding_ms,omitempty"`
	TotalMS              float64       `json:"total_ms"`
	Search               *SearchResult `json:"search,omitempty"`
	Indexed              bool          `json:"indexed"`
	GenerationAvoided    bool          `json:"generation_avoided"`
}

Observation deliberately has no reused/avoided tokens or alternative answer.

type OllamaEmbedder

type OllamaEmbedder struct {
	// contains filtered or unexported fields
}

OllamaEmbedder captures already-installed local weights and the Ollama runtime version. It never pulls models, follows redirects, or uses environment proxies. Its methods are safe for concurrent use; its pinned identity is immutable.

func NewOllamaEmbedder

func NewOllamaEmbedder(baseURL, model string, maxInputBytes int) (*OllamaEmbedder, error)

NewOllamaEmbedder validates the installed model without generating a vector. The empty origin selects http://127.0.0.1:11434. Discovery is capped at 10s.

func NewOllamaEmbedderContext

func NewOllamaEmbedderContext(ctx context.Context, baseURL, model string, maxInputBytes int) (*OllamaEmbedder, error)

NewOllamaEmbedderContext also includes metadata discovery in caller's deadline.

func (*OllamaEmbedder) Close

func (p *OllamaEmbedder) Close()

Close releases idle local connections. It does not unload model weights.

func (*OllamaEmbedder) Embed

func (p *OllamaEmbedder) Embed(ctx context.Context, text string) (Embedding, error)

Embed sends one bounded text input with truncation disabled. Both sides of inference recheck the model digest, embedding capability and runtime version. A failed or changed snapshot never returns a usable vector or token count.

type PutRequest

type PutRequest struct {
	Entry      Entry `json:"entry"`
	MaxEntries int   `json:"max_entries"`
}

type Query

type Query struct {
	Partition     string    `json:"partition"`
	ModelID       string    `json:"model_id"`
	Vector        []float32 `json:"vector"`
	MinSimilarity float64   `json:"min_similarity"`
	TopK          int       `json:"top_k"`
	MaxScan       int       `json:"max_scan"`
}

type ReuseConfig added in v0.15.0

type ReuseConfig struct {
	Config
	ReadOnly                bool   `json:"read_only"`
	Intent                  string `json:"intent"`
	VerifierModel           string `json:"verifier_model"`
	VerifierMaxInputTokens  int64  `json:"verifier_max_input_tokens,omitempty"`
	VerifierMaxOutputTokens int64  `json:"verifier_max_output_tokens,omitempty"`
	VerifierTimeoutMS       int    `json:"verifier_timeout_ms,omitempty"`
	WaitForCandidateMS      int    `json:"wait_for_candidate_ms,omitempty"`
}

ReuseConfig opts a read-only task into bounded candidate validation. Config's retrieval score is not an answer-validity threshold. The embedded fields have the same flat JSON names as shadow configuration.

func NormalizeReuseConfig added in v0.15.0

func NormalizeReuseConfig(c ReuseConfig) (ReuseConfig, error)

NormalizeReuseConfig preserves the shadow configuration contract while requiring an explicit controlled, read-only policy and a bounded local model. It does not discover a runtime, download weights, or call a model.

type SearchResult

type SearchResult struct {
	Candidates []Candidate `json:"candidates"`
	Scanned    int         `json:"scanned"`
	Truncated  bool        `json:"truncated"`
}

type Source

type Source struct {
	Key          string `json:"key"`
	Version      int64  `json:"version"`
	SHA256       string `json:"sha256"`
	OutputSHA256 string `json:"output_sha256"`
}

type ValidityRequest added in v0.15.0

type ValidityRequest struct {
	OriginalQuestion string            `json:"original_question"`
	Question         string            `json:"question"`
	Instructions     string            `json:"instructions"`
	Context          string            `json:"context"`
	CachedAnswer     string            `json:"cached_answer"`
	Bindings         map[string]string `json:"bindings"`
	Intent           string            `json:"intent"`
}

ValidityRequest supplies both questions and the evidence against which the unchanged cached answer must be assessed. Treat its strings as task data.

type ValidityResult added in v0.15.0

type ValidityResult struct {
	GenerationCalled bool   `json:"generation_called"`
	Complete         bool   `json:"complete"`
	Durable          bool   `json:"durable"`
	Verdict          string `json:"verdict"`
	SameIntent       bool   `json:"same_intent"`
	SameFacts        bool   `json:"same_facts"`
	SameConstraints  bool   `json:"same_constraints"`
	AnswerSupported  bool   `json:"answer_supported"`
	InputTokens      *int64 `json:"input_tokens"`
	OutputTokens     *int64 `json:"output_tokens"`
	Reused           bool   `json:"reused"`
	// SharedWork identifies a zero-incremental-usage decision whose model work
	// is accounted once by its batch's separate work record and shared receipt.
	SharedWork   bool    `json:"shared_work,omitempty"`
	DurationMS   float64 `json:"duration_ms"`
	ReceiptKey   string  `json:"receipt_key,omitempty"`
	OutputSHA256 string  `json:"output_sha256,omitempty"`
}

ValidityResult separates the verdict from execution evidence supplied by the trusted adapter. Missing usage is unknown; it is never inferred as zero.

func ParseVerdict added in v0.15.0

func ParseVerdict(data []byte) (ValidityResult, error)

ParseVerdict accepts exactly the five model-authored decision fields. Usage, receipts and timing must come from the adapter, never model-authored JSON. Reading only primitive tokens also rejects nested/deep values without walking arbitrary structures. No partial result is returned on malformed input.

func (ValidityResult) Accepted added in v0.15.0

func (v ValidityResult) Accepted() bool

Accepted is a verdict check only. Callers must separately establish complete execution, known bounded usage, provenance, freshness and deterministic checks.

type Verifier added in v0.15.0

type Verifier interface {
	Verify(context.Context, ValidityRequest) (ValidityResult, error)
	Identity() string
}

Jump to

Keyboard shortcuts

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