resourceauthoring

package
v0.0.1-rc.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 22 Imported by: 1 module, 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 resourceauthoring drafts resource-scoped detector rules from bounded authoring contexts published by discovery plugins.

Graph publication enqueues one durable job, in the same transaction, the first time a bounded resource authoring context appears (see pkg/state). A Worker claims jobs under a renewable lease, calls an Author outside any database transaction, and stores the resulting Proposal on the job. Failures are retried with bounded exponential backoff, and an expired lease is reclaimable after a crash. Processing is at least once: a crash after the model responds but before the proposal commits causes another model call.

Model boundary

ModelAuthor sends a bounded resource posture and observed-behavior baseline. Raw manifests, secret values, environment values, exact host paths, and referenced object names are never stored on the job or sent. The model chooses only a selector, expression, risk, and rationale for each of up to three rules; the host sets the rule ID, plugin wildcard, resource scope, reason, and window. Every model turn must call a tool: add_rule validates a rule against the rules already added and records it, and finish ends the run with the overall rationale. Validation never evaluates synthetic inputs, and the proposal is built only from accepted rules after finish succeeds.

Drafts never change the active ruleset. pkg/detector loads them as resource rules in shadow mode, where matches are only recorded, until an operator approves the job (state.Store.ReviewResourceRuleJobRules); approved rules run in active mode and their matches become findings. The model has no database, graph-publication, or review access, and nothing in this package approves rules. Running a rule never calls a model. See https://agent-shield.dev/docs/architecture/detection-rules/.

Index

Constants

View Source
const MaxStabilizationWindow = 24 * time.Hour

MaxStabilizationWindow bounds WorkerConfig.StabilizationWindow by the telemetry rollup retention: older telemetry has already left the graph.

View Source
const (
	ModelAuthorInputMaxBytes = 96 << 10
)

maxDraftRules bounds one proposal; the tool budget leaves room to correct each rejected rule once.

Variables

This section is empty.

Functions

func ValidateContext

func ValidateContext(resource state.ResourceAuthoringContext) error

ValidateContext checks the bounded resource authoring context before it is sent to a model.

func WithObservedBehavior

func WithObservedBehavior(ctx context.Context, source BaselineSource, resource state.ResourceAuthoringContext) (state.ResourceAuthoringContext, error)

WithObservedBehavior returns resource with its observed behavior from source, the context the authoring worker sends to the model.

Types

type Author

type Author interface {
	Author(context.Context, state.ResourceAuthoringContext) (Proposal, error)
}

Author drafts rules for one resource authoring context.

type BaselineSource

type BaselineSource interface {
	WorkloadObservedBehavior(context.Context, string) (state.WorkloadBehavior, error)
}

BaselineSource reads a resource's observed behavior from the current graph. *state.Store implements it.

type JobStore

type JobStore interface {
	BaselineSource
	// ClaimResourceRuleJob claims a job at least minAge old.
	ClaimResourceRuleJob(ctx context.Context, workerID string, lease, minAge time.Duration) (state.ResourceRuleJob, bool, error)
	RenewResourceRuleJob(context.Context, int64, string, time.Duration) (bool, error)
	CompleteResourceRuleJob(context.Context, int64, string, json.RawMessage) error
	RetryResourceRuleJob(context.Context, int64, string, string, time.Duration, bool) (state.ResourceRuleRetry, error)
	// ResourceRuleQueueStats feeds the worker's periodic status line.
	ResourceRuleQueueStats(context.Context, time.Duration) (state.ResourceRuleQueueStats, error)
}

JobStore is the durable authoring queue and the graph the baseline is read from. *state.Store implements it. Only the current lease owner may renew, complete, or retry a job.

type ModelAuthor

type ModelAuthor struct {
	Client reasoning.Client
	// Logger receives Debug traces of add_rule calls and the run outcome; nil uses slog.Default().
	Logger *slog.Logger
	// Selectors, when set, are the only rule selectors offered to and accepted
	// from the model, such as the selectors this resource has producers for.
	// Empty uses the selectors supported by the resource kind.
	Selectors []string
}

ModelAuthor drafts rules with a model through Client's tool-only loop: the model records rules with add_rule and ends with finish. Each rule is compiled with the production detector when it is added.

func (ModelAuthor) Author

func (author ModelAuthor) Author(ctx context.Context, resource state.ResourceAuthoringContext) (Proposal, error)

Author implements Author.

type Proposal

type Proposal struct {
	Rationale       string                         `json:"rationale"`
	Rules           []ProposalRule                 `json:"rules"`
	ApprovedRuleIDs []string                       `json:"approvedRuleIds,omitempty"`
	ToolCalls       []reasoning.StructuredToolCall `json:"toolCalls,omitempty"`
	Context         json.RawMessage                `json:"context,omitempty"`
	Attributes      map[string]string              `json:"attributes,omitempty"`
	// ObservedBehavior is the baseline the draft was written from, kept for review.
	ObservedBehavior *state.WorkloadBehavior `json:"observedBehavior,omitempty"`
}

Proposal is a validated, inactive rule draft for one resource. Rules holds at most three distinct rules, recorded through add_rule, and may be empty when no available field distinguishes the behavior of interest.

type ProposalRule

type ProposalRule struct {
	Rule      detector.Rule `json:"rule"`
	Rationale string        `json:"rationale"`
}

ProposalRule is a drafted rule with the model's rationale, including the false positives it expects.

type Worker

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

Worker processes resource rule authoring jobs one at a time. It logs each claimed and finished job and a bounded status line with the queue.

func NewWorker

func NewWorker(store JobStore, config WorkerConfig) (*Worker, error)

NewWorker validates config and returns a worker. LeaseDuration must be between 3 seconds and 24 hours.

func (*Worker) Run

func (worker *Worker) Run(ctx context.Context)

Run polls until ctx is cancelled; claim and job failures are reported and retried.

type WorkerConfig

type WorkerConfig struct {
	ID            string
	PollInterval  time.Duration
	LeaseDuration time.Duration
	// StabilizationWindow is how long a job waits after its resource revision
	// appears, so the graph can collect the telemetry the draft is based on.
	StabilizationWindow time.Duration
	// MaxAttempts bounds authoring attempts, retried with exponential backoff;
	// zero uses 8. After the last one, or after a request the model endpoint
	// rejected as invalid, the job fails until an operator requests it again.
	MaxAttempts int
	Author      Author
	ReportError func(error)
}

WorkerConfig configures a Worker. The lease is renewed every third of LeaseDuration while the author runs.

Jump to

Keyboard shortcuts

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