policy

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 policy asks the embedded OPA whether a tool call may go on, and at which price: at once, after one operator, or after two.

Input: the principal and a ToolCall that the server filled from its own data (the principal record, the partner record, the stored document, the payload of the effect). The client chooses none of it. Output: a Decision with the names of the rules that forbade the action (Deny) or turned it into an approval request (Fired). An error of the engine means no decision: the caller must not do the effect.

The Rego text and its data are embedded (policies/) and prepared once at start. Amounts reach the policy as whole cents, so no rounding can move a limit.

Index

Constants

View Source
const (
	ApprovalNone = "none"
	ApprovalOne  = "one"
	ApprovalTwo  = "two"
)

Approval levels of a decision.

Variables

This section is empty.

Functions

func Cents

func Cents(s string) (int64, bool)

Cents reads a decimal string with at most two places ("12", "12.5", "12.50") into whole cents. ok is false for anything else. An amount above the exact range of the engine is cut to maxCents, which is above any limit.

func InvoiceLines

func InvoiceLines(lines []NormLine) [][]string

InvoiceLines gives the lines of an invoice or of an order to a supplier in the normal form the policy compares: [sku, quantity in thousandths, unit price in cents] per line, merged by (sku, price) and sorted. Both sides are built by this one function, so the policy only tests equality.

Types

type AllowAll

type AllowAll struct{}

AllowAll is the policy of the mode no-policy (C3-A2): every call is allowed at once, with no tier, no approval request and no daily counter. The data checks of the tools (a SKU that is not in the price list, a missing business reference) are not policy and stay.

func (AllowAll) Counts

func (AllowAll) Counts() bool

Counts tells the tools not to count the amount against the daily limit of the partner.

func (AllowAll) Decide

Decide allows the call.

type Decision

type Decision struct {
	Allow    bool     `json:"allow"`
	Tier     int      `json:"tier"`
	Approval string   `json:"approval"`
	Deny     []string `json:"deny"`
	Fired    []string `json:"fired"`
}

Decision is the answer of the policy.

type Document

type Document struct {
	ST01       string `json:"st01"`
	SenderRole string `json:"sender_role"`
}

Document is the source document: its set and the role of its sender.

type Engine

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

Engine is the prepared policy.

func New

func New(ctx context.Context, opts ...Option) (*Engine, error)

New compiles the embedded policy and prepares it for evaluation.

func (*Engine) Decide

func (e *Engine) Decide(ctx context.Context, p Principal, c ToolCall) (Decision, error)

Decide evaluates the policy for one call. Any failure of the engine, or an answer that is not a complete decision, is an error: the default is "no".

type NormLine

type NormLine struct {
	SKU        string
	QtyMilli   int64
	PriceCents int64
}

NormLine is one line before it is put in the normal form.

type Option

type Option func(data map[string]any)

Option changes the data the policy reads (data.json is the default).

func WithUpstreamAllow

func WithUpstreamAllow(allow map[string][]string) Option

WithUpstreamAllow sets data.upstream_allow: the up.* tools each tenant may call. Without it every upstream tool is forbidden (TL-07).

type Partner

type Partner struct {
	Role          string   `json:"role"`
	Active        bool     `json:"active"`
	AutoMaxCents  *int64   `json:"auto_max_cents"`
	DailyMaxCents *int64   `json:"daily_max_cents"`
	ForecastCents *int64   `json:"forecast_cents"`
	ShipToCodes   []string `json:"ship_to_codes"`
	Contacts      []string `json:"contacts"`
}

Partner is the partner record the policy checks the call against. A limit that is nil is not set.

type Principal

type Principal struct {
	ID       string `json:"id"`
	Kind     string `json:"kind"`
	TenantID string `json:"tenant"`
}

Principal is the caller as the server knows it from the principal record.

type SupplierOrder

type SupplierOrder struct {
	TotalCents int64      `json:"total_cents"`
	Currency   string     `json:"currency"`
	LinesNorm  [][]string `json:"lines_norm"`
}

SupplierOrder is the order to a supplier an invoice is checked against. Lines are in the normal form of InvoiceLines.

type ToolCall

type ToolCall struct {
	Tool string
	// Effect is the payload of the effect (the same value the request hash is taken
	// over); it reaches the policy as input.effect, with amount_cents added.
	Effect         any
	AmountCents    int64
	Partner        *Partner // nil: the partner is not registered
	DailyAutoCents int64
	Document       Document
	SupplierOrder  *SupplierOrder // nil: no such order
}

ToolCall is everything the policy sees about a call.

Jump to

Keyboard shortcuts

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