safeguard

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package safeguard provides fail-closed input and output screening as Core chat middleware.

Matcher is the extension boundary for substring lists, classifiers, or remote moderation services. Middleware has no global registry or default policy: callers construct and compose an explicit instance.

Example
package main

import (
	"context"
	"fmt"

	"github.com/Tangerg/scope/core/chatclient/safeguard"
)

func main() {
	matcher, err := safeguard.NewSubstringMatcher([]string{"secret"}, safeguard.SubstringConfig{})
	if err != nil {
		panic(err)
	}
	match, err := matcher.Match(context.Background(), "do not reveal the SECRET")
	if err != nil {
		panic(err)
	}
	fmt.Println(match.Found, match.Term)
}
Output:
true secret

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsafeContent is the stable policy rejection sentinel unwrapped by
	// UnsafeError.
	ErrUnsafeContent = errors.New("safeguard: unsafe content")
	// ErrInvalidMiddlewareConfig identifies an incomplete screening boundary.
	ErrInvalidMiddlewareConfig = errors.New("safeguard: invalid middleware config")
	// ErrInvalidSubstringConfig identifies an empty term policy.
	ErrInvalidSubstringConfig = errors.New("safeguard: invalid substring matcher config")
	// ErrNilStream identifies a wrapped streamer that returned no iterator.
	ErrNilStream = errors.New("safeguard: nil stream sequence")
)

Functions

This section is empty.

Types

type Block

type Block struct {
	Scope Scope
	Term  string
}

Block describes a policy rejection delivered to MiddlewareConfig.OnBlock.

type Match

type Match struct {
	Term  string
	Found bool
}

Match is a Matcher's decision for one text projection. Term should be empty when policy details must not be disclosed.

type Matcher

type Matcher interface {
	// Match evaluates one provider-neutral text projection. Found=false means the
	// text passed; a returned error means no policy decision was reached. Remote
	// implementations must honor ctx and preserve context errors.
	Match(ctx context.Context, text string) (Match, error)
}

Matcher screens a text projection. Implementations may call remote policy services and must preserve context cancellation errors.

type MatcherFunc

type MatcherFunc func(ctx context.Context, text string) (Match, error)

MatcherFunc adapts a screening function to Matcher.

func (MatcherFunc) Match

func (m MatcherFunc) Match(ctx context.Context, text string) (Match, error)

type Middleware

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

Middleware screens model inputs and outputs at the model boundary.

func NewMiddleware

func NewMiddleware(matcher Matcher, config MiddlewareConfig) (*Middleware, error)

NewMiddleware freezes screening direction and callback policy around one explicit Matcher.

func (*Middleware) Call

func (m *Middleware) Call(next chat.Model) chat.Model

Call is a chat.CallMiddleware. Input is screened before the model runs; output is screened before a response becomes visible to the caller.

func (*Middleware) Stream

func (m *Middleware) Stream(next chat.Streamer) chat.Streamer

Stream is a chat.StreamMiddleware. Output chunks are accumulated before screening so a term split across provider chunks is still detected. The chunk that completes an unsafe match is not yielded.

type MiddlewareConfig

type MiddlewareConfig struct {
	Scope   Scope
	OnBlock func(context.Context, Block)
}

MiddlewareConfig controls one immutable Middleware. A zero Scope defaults to ScopeBoth. OnBlock runs synchronously before a rejection is returned.

type Scope

type Scope string

Scope selects which side of a model exchange is screened.

const (
	// ScopeInput screens system and user text before provider I/O.
	ScopeInput Scope = "input"
	// ScopeOutput screens assistant text before it reaches the caller.
	ScopeOutput Scope = "output"
	// ScopeBoth applies one matcher to both model-boundary directions.
	ScopeBoth Scope = "both"
)

func (Scope) Valid

func (s Scope) Valid() bool

type SubstringConfig

type SubstringConfig struct {
	CaseSensitive bool
	HideMatch     bool
}

SubstringConfig controls matching and disclosure. Case-insensitive matching is the default. HideMatch prevents a configured term from entering UnsafeError or OnBlock.

type SubstringMatcher

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

SubstringMatcher is an immutable matcher for small policy term sets. It trims and de-duplicates configuration once, preserves declaration order for the first-match decision, and can withhold the matched term from downstream errors and callbacks without weakening the block decision.

func NewSubstringMatcher

func NewSubstringMatcher(terms []string, config SubstringConfig) (*SubstringMatcher, error)

NewSubstringMatcher normalizes and deduplicates a small term policy once; matching never depends on later caller mutation.

func (*SubstringMatcher) Match

func (s *SubstringMatcher) Match(ctx context.Context, text string) (Match, error)

type UnsafeError

type UnsafeError struct {
	Block Block
}

UnsafeError is a policy rejection. It unwraps to ErrUnsafeContent.

func (*UnsafeError) Error

func (u *UnsafeError) Error() string

func (*UnsafeError) Unwrap

func (u *UnsafeError) Unwrap() error

Jump to

Keyboard shortcuts

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