behaviorrule

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package behaviorrule implements the BehaviorRule domain: persisted AI-derived or manually-authored rules that guide agent behavior. Rules flow through a proposal queue (status='proposed') and are promoted to 'active' via apply_behavior_rules outcome='success'. Confidence adjusts with each outcome.

Index

Constants

This section is empty.

Variables

View Source
var AllowedSourceTypes = map[string]bool{
	"reflection": true,
	"outcome":    true,
	"manual":     true,
}

AllowedSourceTypes is the set of valid source_type values.

View Source
var AllowedStatuses = map[string]bool{
	"proposed":   true,
	"active":     true,
	"rejected":   true,
	"deprecated": true,
}

AllowedStatuses is the set of valid status values.

View Source
var ErrNotFound = errors.New("behaviorrule: not found")

ErrNotFound is returned when a requested behavior rule does not exist.

Functions

This section is empty.

Types

type BehaviorRule

type BehaviorRule struct {
	ID          uuid.UUID  `json:"id"`
	WorkspaceID *uuid.UUID `json:"workspace_id,omitempty"`
	Condition   string     `json:"condition"`
	Action      string     `json:"action"`
	SourceType  string     `json:"source_type"`
	SourceID    *uuid.UUID `json:"source_id,omitempty"`
	Confidence  float64    `json:"confidence"`
	Status      string     `json:"status"`
	CreatedAt   time.Time  `json:"created_at"`
	UpdatedAt   time.Time  `json:"updated_at"`
}

BehaviorRule is the domain model for a persisted behavior rule record.

type CreateParams

type CreateParams struct {
	WorkspaceID *uuid.UUID
	Condition   string
	Action      string
	SourceType  string
	SourceID    *uuid.UUID
	Confidence  float64 // defaults to 0.50 when zero
}

CreateParams holds the fields required to propose a new behavior rule.

type ListParams

type ListParams struct {
	WorkspaceID *uuid.UUID
	Status      *string // nil = all statuses
	Limit       int     // 0 = default 20
}

ListParams narrows a List call.

type Store

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

Store is the Postgres-backed implementation of StoreIface. It uses raw pgx without sqlc so the behavior_rules table stays independent of the sqlc-generated query layer.

func New

func New(pool *pgxpool.Pool, workspaceID *uuid.UUID) *Store

New returns a Store backed by pool, scoped to the optional workspaceID. nil workspaceID = legacy unscoped mode.

func (*Store) ApplyOutcome

func (s *Store) ApplyOutcome(ctx context.Context, id uuid.UUID, outcome string) (*BehaviorRule, error)

ApplyOutcome applies the confidence formula atomically. A single UPDATE...RETURNING avoids the read-then-update race condition. SECURITY: workspace-scoped — a store scoped to workspace A cannot apply an outcome to a rule owned by workspace B, matching List's scoping (§ store scoping audit, aligns with the workspace predicate already used by List).

func (*Store) Deprecate

func (s *Store) Deprecate(ctx context.Context, id uuid.UUID) (*BehaviorRule, error)

Deprecate sets the rule's status to 'deprecated'. Idempotent. Returns ErrNotFound when no matching rule exists. SECURITY: workspace-scoped — a store scoped to workspace A cannot deprecate a rule owned by workspace B, matching List's scoping.

func (*Store) List

func (s *Store) List(ctx context.Context, p ListParams) ([]*BehaviorRule, error)

List returns behavior rules matching the filter, ordered by created_at DESC.

func (*Store) Propose

func (s *Store) Propose(ctx context.Context, p CreateParams) (*BehaviorRule, error)

Propose inserts a new behavior rule with status='proposed' and returns the persisted record.

func (*Store) PruneOlderThan

func (s *Store) PruneOlderThan(ctx context.Context, cutoff time.Time) (int64, error)

PruneOlderThan hard-deletes behavior_rules rows where status IN ('rejected','deprecated') AND created_at < cutoff. Active and proposed rows are NEVER deleted regardless of age.

type StoreIface

type StoreIface interface {
	// Propose inserts a new behavior rule with status='proposed' and returns
	// the persisted record.
	Propose(ctx context.Context, p CreateParams) (*BehaviorRule, error)

	// List returns behavior rules matching the filter, ordered by created_at DESC.
	List(ctx context.Context, p ListParams) ([]*BehaviorRule, error)

	// ApplyOutcome applies the confidence formula atomically and conditionally
	// transitions the status:
	//   outcome='success' AND status='proposed' → status='active', confidence += 0.05 (cap 1.00)
	//   outcome='success' AND status='active'   → confidence += 0.05 (cap 1.00)
	//   outcome='failure'                        → confidence -= 0.10 (floor 0.00)
	// The update is atomic (single UPDATE statement) to prevent race conditions.
	// Returns ErrNotFound when no matching rule exists.
	ApplyOutcome(ctx context.Context, id uuid.UUID, outcome string) (*BehaviorRule, error)

	// Deprecate sets the rule's status to 'deprecated'. Idempotent: returns
	// success when the rule is already deprecated.
	// Returns ErrNotFound when no matching rule exists.
	Deprecate(ctx context.Context, id uuid.UUID) (*BehaviorRule, error)

	// PruneOlderThan hard-deletes behavior_rules rows where
	// status IN ('rejected','deprecated') AND created_at < cutoff.
	// Active and proposed rows are NEVER deleted regardless of age.
	// Called daily by the scheduler to enforce the 365-day TTL per
	// backend-security-design.md §1.3.
	PruneOlderThan(ctx context.Context, cutoff time.Time) (int64, error)
}

StoreIface is the backend-agnostic contract for the BehaviorRule domain.

type UpdateConfidenceParams

type UpdateConfidenceParams struct {
	ID         uuid.UUID
	Confidence float64
}

UpdateConfidenceParams holds the fields required to update confidence.

type UpdateStatusParams

type UpdateStatusParams struct {
	ID     uuid.UUID
	Status string
}

UpdateStatusParams holds the fields required to update a rule's status.

Jump to

Keyboard shortcuts

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