replay

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: GPL-3.0 Imports: 7 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 replay re-runs a recorded session: either against the current policy only (a dry run, which answers "what would this policy have done yesterday?") or against the current upstreams as well.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Diff

type Diff struct {
	SessionA string `json:"session_a"`
	SessionB string `json:"session_b"`
	Rows     []Row  `json:"rows"`
}

Diff is the comparison of two recorded sessions.

func Compare

func Compare(sessionA, sessionB string, a, b []*audit.Call) *Diff

Compare aligns two recorded sessions and classifies every call.

The alignment is a longest-common-subsequence over the tool names, which is what makes an inserted or removed call show up as one row instead of pushing everything after it out of step.

func (*Diff) Identical

func (d *Diff) Identical() bool

Identical reports whether the two sessions did the same things with the same arguments and got the same answers.

func (*Diff) Summary

func (d *Diff) Summary() map[Status]int

Summary counts the rows by status.

type Entry

type Entry struct {
	Call *audit.Call `json:"call"`
	// Was is the decision recorded at the time.
	Was policy.Decision `json:"was"`
	// Now is the decision the current policy reaches.
	Now policy.Decision `json:"now"`
	// Skipped is set when OnlyAllowed filtered the call out.
	Skipped bool `json:"skipped,omitempty"`
	// Sent reports whether the call was actually re-sent upstream.
	Sent bool `json:"sent"`
	// ResultHash is the hash of the fresh result, when the call was re-sent.
	ResultHash string `json:"result_hash,omitempty"`
	// Error is a failure from the fresh call.
	Error string `json:"error,omitempty"`
	// DurationMS is how long the fresh call took.
	DurationMS int64 `json:"duration_ms,omitempty"`
	// Labels are the labels the session earns with this call under the
	// current policy.
	Labels []string `json:"labels,omitempty"`
}

Entry is one recorded call, re-evaluated and possibly re-sent.

func (*Entry) DecisionChanged

func (e *Entry) DecisionChanged() bool

DecisionChanged reports whether the current policy disagrees with what was recorded. This is the line "replay --dry-run" is really about.

func (*Entry) ResultChanged

func (e *Entry) ResultChanged() bool

ResultChanged reports whether a re-sent call produced a different result than the recorded one. It is false for calls that were not re-sent, and for calls whose recorded result was truncated and cannot be compared.

type Forwarder

type Forwarder interface {
	ForwardJSON(ctx context.Context, tool string, args json.RawMessage) (json.RawMessage, error)
}

Forwarder sends a call to the live upstreams and returns the result as JSON, so that replay can hash it the same way the audit log did.

type Options

type Options struct {
	// Policy is evaluated against every recorded call. Required.
	Policy *policy.Policy
	// OnlyAllowed skips calls that were denied when they were recorded.
	OnlyAllowed bool
	// Forwarder re-sends allowed calls. Nil means a dry run.
	Forwarder Forwarder
	// Host is the client that opened the recorded session, for host.*
	// conditions.
	Host policy.Host
	// Annotations returns what the servers said about their tools in the
	// catalog a call was made under, keyed by exposed tool name, for
	// annotations.* conditions. Nil means nothing is known.
	Annotations func(catalogHash string) map[string]policy.Annotations
	// ResultLabels returns the labels agentgate itself attaches for a
	// result (canary-read, injection-suspected), so a replayed session
	// picks them up where the live one did. Nil attaches none.
	ResultLabels func(result *policy.Result) []string
}

Options control a replay.

type Report

type Report struct {
	SessionID string   `json:"session_id"`
	Entries   []*Entry `json:"entries"`
	DryRun    bool     `json:"dry_run"`
}

Report is the outcome of a replay.

func Run

func Run(ctx context.Context, sessionID string, calls []*audit.Call, opts Options) (*Report, error)

Run replays the calls in order.

The session is simulated as the walk proceeds, the way the proxy tracks a live one: budgets, the rate limit, the loop guard, labels and the list of tools called all build up call by call. A policy that adds a budget shows up in a dry run exactly where the session would have run out, and one that adds a label rule changes the decisions after the call that earns it.

A call counts as having gone through when the current policy allows it, or asks and the call went through at the time: the human who approved it then is taken to approve it again.

func (*Report) Changed

func (r *Report) Changed() []*Entry

Changed returns the entries whose decision differs from the recorded one.

func (*Report) Counts

func (r *Report) Counts() (allowed, denied, changed, failed int)

Counts summarises a report for the CLI footer.

type Row

type Row struct {
	Status Status      `json:"status"`
	A      *audit.Call `json:"a,omitempty"`
	B      *audit.Call `json:"b,omitempty"`
	Tool   string      `json:"tool"`
}

Row is one line of a session diff.

type Status

type Status string

Status is how a pair of calls relates across two sessions.

const (
	// StatusSame means same tool, same arguments, same result.
	StatusSame Status = "same"
	// StatusResult means same tool and arguments, different result.
	StatusResult Status = "result"
	// StatusArgs means same tool, different arguments.
	StatusArgs Status = "args"
	// StatusOnlyA means the call happened only in the first session.
	StatusOnlyA Status = "only-a"
	// StatusOnlyB means the call happened only in the second session.
	StatusOnlyB Status = "only-b"
	// StatusDecision means the call was allowed in one session and not the other.
	StatusDecision Status = "decision"
)

Jump to

Keyboard shortcuts

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