diff

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package diff provides trajectory comparison for LoopKit. It computes divergence points, outcome deltas, spend deltas, and per-action diffs between two trajectories (event logs).

Usage:

report := diff.Diff(trajA, trajB)
fmt.Println(report.Text())
jsonBytes := report.JSON()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionSignature

type ActionSignature struct {
	// Seq is the event sequence number.
	Seq uint64 `json:"seq"`
	// Kind is the action type (ModelCall, ToolCall, Finish, etc.).
	Kind string `json:"kind"`
	// Tool is the tool name (only set for ToolCall).
	Tool string `json:"tool,omitempty"`
	// Model is the model name (only set for ModelCall).
	Model string `json:"model,omitempty"`
}

ActionSignature is a simplified representation of an action for comparison. It is derived from an ActionRequested event.

type AlignedPair

type AlignedPair struct {
	// A is the action from trajectory A (nil if inserted in B).
	A *ActionSignature `json:"a,omitempty"`
	// B is the action from trajectory B (nil if deleted from A).
	B *ActionSignature `json:"b,omitempty"`
	// Equal is true when A and B have the same kind (and same tool/model if applicable).
	Equal bool `json:"equal"`
}

AlignedPair represents one row in the LCS-aligned action sequence.

type ArgDiff

type ArgDiff struct {
	// OnlyInA contains fields/values present in A but not in B.
	OnlyInA map[string]json.RawMessage `json:"only_in_a,omitempty"`
	// OnlyInB contains fields/values present in B but not in A.
	OnlyInB map[string]json.RawMessage `json:"only_in_b,omitempty"`
	// Changed contains field names with [aValue, bValue] pairs that differ.
	Changed map[string][2]json.RawMessage `json:"changed,omitempty"`
}

ArgDiff is a structural JSON diff of action arguments.

type DivergencePoint

type DivergencePoint struct {
	// Seq is the seq in trajectory A where divergence begins.
	// For B-only insertions, this is the seq just before.
	SeqA uint64 `json:"seq_a"`
	// SeqB is the seq in trajectory B where divergence begins.
	SeqB uint64 `json:"seq_b"`
	// ActionA is the action in trajectory A at the divergence point (nil if A is shorter).
	ActionA *ActionSignature `json:"action_a,omitempty"`
	// ActionB is the action in trajectory B at the divergence point (nil if B is shorter).
	ActionB *ActionSignature `json:"action_b,omitempty"`
	// ArgDiff is a structural JSON diff of the action arguments at the divergence point.
	// Only populated when both A and B have actions of the same kind but different args.
	ArgDiff *ArgDiff `json:"arg_diff,omitempty"`
}

DivergencePoint marks the first action where the two trajectories differ.

type OutcomeDelta

type OutcomeDelta struct {
	// KindA is the outcome kind of trajectory A.
	KindA string `json:"kind_a"`
	// KindB is the outcome kind of trajectory B.
	KindB string `json:"kind_b"`
	// Same is true when both outcomes are the same kind.
	Same bool `json:"same"`
}

OutcomeDelta describes how the outcome changed between trajectories A and B.

type Report

type Report struct {
	// Divergence is non-nil when the trajectories diverge.
	Divergence *DivergencePoint `json:"divergence,omitempty"`
	// OutcomeDelta describes how the final outcomes compare.
	OutcomeDelta OutcomeDelta `json:"outcome_delta"`
	// SpendDelta describes how resource spend compares.
	SpendDelta SpendDelta `json:"spend_delta"`
	// Alignment is the LCS-aligned sequence of actions from both trajectories.
	Alignment []AlignedPair `json:"alignment"`
}

Report is the result of a Diff operation.

func Diff

func Diff[R any](a, b []event.Envelope) Report

Diff computes the difference between two trajectories. A and B are ordered lists of event.Envelope (the raw event logs). Returns a Report with divergence point, outcome delta, spend delta, and LCS alignment.

func DiffTrajectories

func DiffTrajectories(a, b []event.Envelope) Report

DiffTrajectories is a type-erased variant of Diff for use without generics.

func (Report) JSON

func (r Report) JSON() []byte

JSON returns a stable machine-readable JSON encoding of the report.

func (Report) MarshalJSON

func (r Report) MarshalJSON() ([]byte, error)

func (Report) Text

func (r Report) Text() string

Text returns a human-readable string representation of the report.

type SpendDelta

type SpendDelta struct {
	A     budget.Spend `json:"a"`
	B     budget.Spend `json:"b"`
	Delta budget.Spend `json:"delta"` // B - A (positive means B spent more)
}

SpendDelta describes the resource spend difference.

Jump to

Keyboard shortcuts

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