trace

package
v0.14.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package trace provides program-level execution trace collection for AILANG.

This captures what happens when AILANG code evaluates: function calls, effect invocations, contract check results, and budget consumption. Complementary to the chains/observatory system which captures agent-level traces.

M-TRACE-EXPORT Phase 1: Standalone JSONL output via --emit-trace flag.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmitOTELSpans

func EmitOTELSpans(parentCtx context.Context, tracer oteltrace.Tracer, events []TraceEvent, baseTime time.Time) error

EmitOTELSpans converts collected trace events into OTEL spans. Called after program execution completes (batch emission). The parentCtx should carry the root "ailang run" span so program spans appear as children of the run command.

Algorithm:

  1. Walk events sequentially
  2. function_enter → start a new span, push onto stack
  3. effect → create a short-lived child span of current function
  4. contract_check → add as span event on current stack top
  5. budget_delta → add as attributes on current span
  6. function_exit → end current span, pop stack
  7. module_start/end → root span wrapping everything
  8. error → span event with Error status

func EmitOTELSpansWithOptions

func EmitOTELSpansWithOptions(parentCtx context.Context, tracer oteltrace.Tracer, events []TraceEvent, baseTime time.Time, opts TracingOptions) error

EmitOTELSpansWithOptions is like EmitOTELSpans but honors TracingOptions to filter out per-call function/effect spans in non-deep tiers.

Tier behavior:

  • TierOff: nothing is emitted
  • TierStandard: module/contract/budget/error spans emitted; EventFunctionEnter skipped entirely; EventEffect only emitted when evt.Depth <= 1 (top-level, not inside a user function call).
  • TierDeep: everything emitted (same as legacy EmitOTELSpans).

func ExportTrainingData

func ExportTrainingData(w io.Writer, traceFiles []string, cfg ExportConfig) (exported, skipped int, err error)

ExportTrainingData processes JSONL trace files and produces training examples. Reads each .jsonl file in the directory, scores it, and writes qualifying examples as JSONL to the writer.

func IsNonDeterministic

func IsNonDeterministic(effectName, opName string) bool

IsNonDeterministic returns true if the given effect+op pair is known to produce non-deterministic results across runs.

func TraceMetadata

func TraceMetadata(events []TraceEvent) (moduleName string, caps []string)

TraceMetadata extracts metadata from a trace event list. Returns the module name and capabilities from the first module_start event.

func WriteJSONL

func WriteJSONL(w io.Writer, events []TraceEvent) error

WriteJSONL writes all collected events as JSONL (one JSON object per line) to the writer.

Types

type BudgetEvent

type BudgetEvent struct {
	Effect    string `json:"effect"`
	Used      int    `json:"used"`
	Limit     int    `json:"limit"`     // -1 if unlimited
	Remaining int    `json:"remaining"` // -1 if unlimited
	Physical  int    `json:"physical"`  // Physical usage count
}

BudgetEvent captures a budget state change after an effect invocation. Mirrors data from effects/budget.go.

type Collector

type Collector struct {

	// OnEvent is called for each trace event as it is recorded.
	// Used by WASM to stream events to JavaScript in real-time.
	// Nil means no streaming (events are only accumulated in events[]).
	OnEvent func(TraceEvent)
	// contains filtered or unexported fields
}

Collector accumulates trace events during AILANG program execution. Designed for single-threaded use within one evaluation. Set on EffContext.Trace before execution begins.

func NewCollector

func NewCollector() *Collector

NewCollector creates a new trace collector.

func (*Collector) BaseTime

func (c *Collector) BaseTime() time.Time

BaseTime returns the collector's creation time. Used by EmitOTELSpans to reconstruct absolute timestamps.

func (*Collector) Enabled

func (c *Collector) Enabled() bool

Enabled returns whether trace collection is active.

func (*Collector) Events

func (c *Collector) Events() []TraceEvent

Events returns all collected events.

func (*Collector) RecordBudgetDelta

func (c *Collector) RecordBudgetDelta(effect string, used, limit, remaining, physical int)

RecordBudgetDelta records a budget state change after an effect invocation.

func (*Collector) RecordContractCheck

func (c *Collector) RecordContractCheck(kind string, passed bool, msg, location, function string)

RecordContractCheck records a contract verification result.

func (*Collector) RecordEffect

func (c *Collector) RecordEffect(effectName, opName string, args []string, result string)

RecordEffect records an effect invocation.

func (*Collector) RecordError

func (c *Collector) RecordError(msg, location string)

RecordError records an error event.

func (*Collector) RecordFunctionEnter

func (c *Collector) RecordFunctionEnter(name string, args []string)

RecordFunctionEnter records function call entry.

func (*Collector) RecordFunctionExit

func (c *Collector) RecordFunctionExit(name string, result string)

RecordFunctionExit records function return.

func (*Collector) RecordModuleEnd

func (c *Collector) RecordModuleEnd(name string, durationNS int64)

RecordModuleEnd records module completion.

func (*Collector) RecordModuleStart

func (c *Collector) RecordModuleStart(name string, caps []string)

RecordModuleStart records module entry.

type CompareResult

type CompareResult struct {
	Match      bool       `json:"match"`
	Mismatches []Mismatch `json:"mismatches,omitempty"`
	BaselineN  int        `json:"baseline_events"`
	ReplayN    int        `json:"replay_events"`
}

CompareResult holds the result of comparing two traces.

func CompareTraces

func CompareTraces(baseline, replay []TraceEvent) CompareResult

CompareTraces compares a baseline trace against a replay trace. Timestamps and durations are ignored (non-deterministic). All other fields are compared by event type.

type ContractEvent

type ContractEvent struct {
	Kind     string `json:"kind"` // "requires", "ensures", "invariant"
	Passed   bool   `json:"passed"`
	Message  string `json:"message"`
	Location string `json:"location"`
	Function string `json:"function"`
}

ContractEvent captures a contract check result. Mirrors ContractCheck from effects/contracts.go.

type EffectEvent

type EffectEvent struct {
	EffectName    string   `json:"effect_name"`             // e.g., "IO", "FS", "Net"
	OpName        string   `json:"op_name"`                 // e.g., "println", "readFile"
	Args          []string `json:"args,omitempty"`          //
	Result        string   `json:"result,omitempty"`        //
	Deterministic *bool    `json:"deterministic,omitempty"` // nil=unknown, true=deterministic, false=non-deterministic
}

EffectEvent captures an effect invocation. Mirrors data available in effects.Call().

type ErrorEvent

type ErrorEvent struct {
	Message  string `json:"message"`
	Location string `json:"location,omitempty"`
}

ErrorEvent captures an error during execution.

type EventType

type EventType string

EventType enumerates trace event kinds.

const (
	EventModuleStart   EventType = "module_start"
	EventModuleEnd     EventType = "module_end"
	EventFunctionEnter EventType = "function_enter"
	EventFunctionExit  EventType = "function_exit"
	EventEffect        EventType = "effect"
	EventContractCheck EventType = "contract_check"
	EventBudgetDelta   EventType = "budget_delta"
	EventError         EventType = "error"
)

type ExportConfig

type ExportConfig struct {
	MinScore  float64 // Minimum quality score (0.0-1.0), default 0.0
	SourceDir string  // Directory to resolve source files from
}

ExportConfig controls training data export behavior.

type FunctionEvent

type FunctionEvent struct {
	Name       string   `json:"name"`
	Args       []string `json:"args,omitempty"`
	Result     string   `json:"result,omitempty"`
	DurationNS int64    `json:"duration_ns,omitempty"`
}

FunctionEvent captures function enter/exit.

type Mismatch

type Mismatch struct {
	Index    int    `json:"index"`             // Event index where mismatch occurred
	Field    string `json:"field"`             // Which field differs
	Expected string `json:"expected"`          // Value from baseline trace
	Actual   string `json:"actual"`            // Value from replay trace
	Context  string `json:"context,omitempty"` // Human-readable context
}

Mismatch describes a difference between two trace event streams.

type ModuleEvent

type ModuleEvent struct {
	Name       string   `json:"name"`
	DurationNS int64    `json:"duration_ns,omitempty"`
	Caps       []string `json:"caps,omitempty"`
}

ModuleEvent captures module start/end.

type Tier

type Tier int

Tier controls how much detail the tracing emitter writes.

Precedence (set by callers): CLI flag > AILANG_TRACE env var > AILANG_NO_TRACE=1 (legacy) > default (TierStandard).

const (
	// TierOff emits no spans. Equivalent to legacy AILANG_NO_TRACE=1.
	TierOff Tier = iota
	// TierStandard emits module, top-level effect, coordinator, executor,
	// compile, task/chain-linked spans, but NOT per-call function spans or
	// nested effect spans. This is the default.
	TierStandard
	// TierDeep emits everything, including per-call eval.function.* spans
	// and per-op eval.effect.* spans. Opt-in for profiling / training data.
	TierDeep
)

func ParseTier

func ParseTier(s string) (Tier, error)

ParseTier parses a tier name. Accepts "off", "standard", "deep" (case-insensitive). Returns an error for unrecognized values.

func ResolveTier

func ResolveTier(cliFlag string) (Tier, error)

ResolveTier applies CLI > env > default precedence. cliFlag should be the raw --trace value ("" if unset).

func TierFromEnv

func TierFromEnv() (Tier, error)

TierFromEnv resolves the tier from env vars (no CLI flag). Precedence:

  1. AILANG_TRACE env var (off|standard|deep)
  2. AILANG_NO_TRACE=1 legacy alias → TierOff
  3. TierStandard (default)

Returns the resolved tier and a non-nil error only when AILANG_TRACE is set to an unrecognized value.

func (Tier) String

func (t Tier) String() string

String returns the canonical tier name ("off", "standard", "deep").

type TraceEvent

type TraceEvent struct {
	Version     string    `json:"version"`
	Event       EventType `json:"event"`
	TimestampNS int64     `json:"timestamp_ns"`
	Depth       int       `json:"depth,omitempty"`

	// OTEL-compatible span identifiers (M-WASM-TRACE).
	// TraceID is consistent across all events in one execution.
	// SpanID/ParentSpanID form a parent-child tree for distributed tracing.
	TraceID      string `json:"trace_id,omitempty"`
	SpanID       string `json:"span_id,omitempty"`
	ParentSpanID string `json:"parent_span_id,omitempty"`

	// Event-specific payload (exactly one is non-nil per event)
	Module   *ModuleEvent   `json:"module,omitempty"`
	Function *FunctionEvent `json:"function,omitempty"`
	Effect   *EffectEvent   `json:"effect,omitempty"`
	Contract *ContractEvent `json:"contract,omitempty"`
	Budget   *BudgetEvent   `json:"budget,omitempty"`
	Error    *ErrorEvent    `json:"error,omitempty"`
}

TraceEvent is the top-level envelope for all trace events. Each event has a type, timestamp, and one populated payload field.

func ReadJSONL

func ReadJSONL(r io.Reader) ([]TraceEvent, error)

ReadJSONL reads trace events from JSONL format (one JSON object per line). Empty lines are skipped. Returns error on malformed JSON.

type TraceScore

type TraceScore struct {
	Total            float64        `json:"total"`             // Weighted total 0.0-1.0
	Completion       float64        `json:"completion"`        // Did program complete without errors?
	Complexity       float64        `json:"complexity"`        // Number of functions, effects, depth
	ContractCoverage float64        `json:"contract_coverage"` // Percentage of contracts that passed
	BudgetEfficiency float64        `json:"budget_efficiency"` // Used budget vs declared budget
	EffectDiversity  float64        `json:"effect_diversity"`  // Uses multiple effect types
	Stats            TraceStats     `json:"stats"`             // Raw statistics
	EffectBreakdown  map[string]int `json:"effect_breakdown"`  // Per-effect invocation counts
	FunctionCounts   map[string]int `json:"function_counts"`   // Per-function call counts
}

TraceScore holds the quality assessment of a trace.

func ScoreTrace

func ScoreTrace(events []TraceEvent) TraceScore

ScoreTrace evaluates the quality of a trace for training data purposes. Returns a score between 0.0 and 1.0 with component breakdowns.

func ScoreTraceFile

func ScoreTraceFile(path string) (TraceScore, error)

ScoreTraceFile reads and scores a single JSONL trace file.

type TraceStats

type TraceStats struct {
	TotalEvents       int  `json:"total_events"`
	FunctionCalls     int  `json:"function_calls"` // Enter events
	DistinctFunctions int  `json:"distinct_functions"`
	EffectCalls       int  `json:"effect_calls"`
	DistinctEffects   int  `json:"distinct_effects"`
	ContractChecks    int  `json:"contract_checks"`
	ContractsPassed   int  `json:"contracts_passed"`
	BudgetEvents      int  `json:"budget_events"`
	MaxDepth          int  `json:"max_depth"`
	HasErrors         bool `json:"has_errors"`
	HasModuleEnd      bool `json:"has_module_end"`
}

TraceStats holds raw statistical data extracted from a trace.

type TracingOptions

type TracingOptions struct {
	Tier Tier
	// MaxSpansPerTrace caps spans per trace. 0 = no budget (M2 populates this).
	MaxSpansPerTrace int
}

TracingOptions controls emitter behavior. Threaded from CLI / env parsing down into the OTEL emitter.

func DefaultTracingOptions

func DefaultTracingOptions() TracingOptions

DefaultTracingOptions returns the default tier + budget.

func (TracingOptions) DeepTrace

func (o TracingOptions) DeepTrace() bool

DeepTrace reports whether per-call function/effect spans should fire.

func (TracingOptions) Enabled

func (o TracingOptions) Enabled() bool

Enabled reports whether any spans should be emitted.

type TrainingExample

type TrainingExample struct {
	Source   string       `json:"source"`   // AILANG source code
	Trace    string       `json:"trace"`    // JSONL trace events (newline-separated)
	Score    float64      `json:"score"`    // Quality score 0.0-1.0
	Metadata TrainingMeta `json:"metadata"` // Summary metadata
}

TrainingExample is a single training data record for AI fine-tuning.

type TrainingMeta

type TrainingMeta struct {
	Module    string   `json:"module,omitempty"`  // Module name
	Caps      []string `json:"caps,omitempty"`    // Capabilities used
	Functions int      `json:"functions"`         // Distinct function count
	Effects   []string `json:"effects,omitempty"` // Distinct effect names
	MaxDepth  int      `json:"max_depth"`         // Maximum call depth
	Events    int      `json:"events"`            // Total event count
}

TrainingMeta holds summary metadata for a training example.

Jump to

Keyboard shortcuts

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