Compare a trace with a golden baseline

Create a golden-diff baseline from a known-good trace, check later traces against it in text or JSON, and refresh the baseline when a change is intended.

Use Altifigence CLI 0.1.9 on Linux to record a baseline from a known-good .altrace trace and to check later traces against it. A check exits 0 when every step is within tolerance and 1 when any step drifts.

Prepare two traces

golden-diff reads .altrace traces whose header declares "encoding":"bincode". The script below writes run.altrace, a known-good trace with one core and five events, and changed.altrace, the same trace with one more MAC_COMPUTE event. Each event line is core start duration type [api_name].

PROJECT="$(cd "$HOME/alti-projects/demo-project" && pwd -P)"
cd "$PROJECT"

le() { local n=$1 v=$2; while [ "$n" -gt 0 ]; do printf "\\x$(printf %02x $((v & 255)))"; v=$((v >> 8)); n=$((n - 1)); done; }
str() { le 8 "${#1}"; printf '%s' "$1"; }
write_trace() {
  local out="$1" payload line core start dur type api total=0 header
  local -a lines
  mapfile -t lines
  payload="$(mktemp)"
  {
    for line in "${lines[@]}"; do
      read -r core start dur type api <<<"$line"
      (( start + dur > total )) && total=$((start + dur))
    done
    le 8 "$total"
    le 8 "${#lines[@]}"
    for line in "${lines[@]}"; do
      read -r core start dur type api <<<"$line"
      le 4 "$core"; le 8 "$start"; le 8 "$dur"; str "$type"
      if [ -n "$api" ]; then printf '\x01'; str "$api"; else printf '\x00'; fi
    done
  } >"$payload"
  header="{\"altifigence_trace_version\":\"1.0\",\"trace\":{\"cycles\":$total,\"cores\":1,\"clock_mhz\":1000},\"payload\":{\"encoding\":\"bincode\",\"byte_length\":$(wc -c <"$payload")}}"
  { printf 'ALTR\x01\x01\x00\x00'; le 8 "${#header}"; printf '%s' "$header"; cat "$payload"; } >"$out"
  rm -f "$payload"
}

write_trace run.altrace <<'EVENTS'
0 0 100 MAC_COMPUTE
0 100 50 DMA_READ
0 150 5 API_CALL step_1
0 160 100 MAC_COMPUTE
0 260 100 MAC_COMPUTE
EVENTS

write_trace changed.altrace <<'EVENTS'
0 0 100 MAC_COMPUTE
0 100 50 DMA_READ
0 150 5 API_CALL step_1
0 160 100 MAC_COMPUTE
0 260 100 MAC_COMPUTE
0 360 100 MAC_COMPUTE
EVENTS

wc -c < run.altrace
wc -c < changed.altrace

Require the two wc commands to print the file sizes.

376
416

Create the baseline

alti verification golden-diff emit-profile reads one trace and writes the baseline to stdout, so redirect it to a file. A step starts at each API_CALL, and events before the first API_CALL belong to step 0. --tolerance-pct stamps the allowed drift in percent onto every row, and the default is 10.

alti verification golden-diff emit-profile --project "$PROJECT" run.altrace > "$PROJECT/reference.jsonl"
grep -v '^#' "$PROJECT/reference.jsonl"

Require exit status 0. The file starts with two # comment lines and then holds one JSON row per step.

{"step":0,"api_name":null,"expect_mac":1,"expect_dma_read":1,"expect_dma_write":0,"expect_barrier":0,"cycle_budget":165,"tolerance_pct":10.0,"abs_tolerance":null}
{"step":1,"api_name":"step_1","expect_mac":2,"expect_dma_read":0,"expect_dma_write":0,"expect_barrier":0,"cycle_budget":232,"tolerance_pct":10.0,"abs_tolerance":null}

Each row stores the observed event counts as expect_mac, expect_dma_read, expect_dma_write, and expect_barrier. cycle_budget is the cycles the step spans, raised by tolerance_pct and rounded up: step 0 spans 150 cycles and step 1 spans 210. Blank lines and lines that start with # are skipped when the file is read.

Check a trace against the baseline

alti verification golden-diff check takes the baseline first and the trace second. The text output has a golden-diff: summary between two banner lines made of ═ characters, then one line per step. The excerpts below leave out the banner lines.

alti verification golden-diff check --project "$PROJECT" reference.jsonl run.altrace; echo "exit=$?"
  golden-diff: all 2 steps within tolerance
step   0 pass
step   1 pass
exit=0

Require exit status 0. Run the commands in this section without the global --json flag of alti, so that stdout holds the report itself. --format json prints the report, and --format json-v1 prints the versioned result with provenance. Both use the same exit status as the text format.

alti verification golden-diff check --project "$PROJECT" reference.jsonl run.altrace --format json \
  | jq '{summary, step_count, pass_count, total_metrics, exact_matches, tolerance_passes, metric_mismatches, unexpected_observed_steps}'
{
  "summary": "golden-diff: all 2 steps within tolerance",
  "step_count": 2,
  "pass_count": 2,
  "total_metrics": 10,
  "exact_matches": 8,
  "tolerance_passes": 2,
  "metric_mismatches": 0,
  "unexpected_observed_steps": 0
}
alti verification golden-diff check --project "$PROJECT" reference.jsonl run.altrace --format json-v1 \
  | jq '{schema, operation_id, artifact_schema: .artifact.artifact_schema, engine: .provenance.engine}'
{
  "schema": "altifigence.golden-diff.operation-result.v1",
  "operation_id": "altifigence.verification.golden-diff.v1",
  "artifact_schema": "altifigence.golden-diff.report.v1",
  "engine": "alti-verification"
}

Read a failing check

Step 1 of changed.altrace holds three MAC_COMPUTE events and spans 310 cycles, while the baseline expects two events and a budget of 232 cycles.

alti verification golden-diff check --project "$PROJECT" reference.jsonl changed.altrace; echo "exit=$?"
  golden-diff: 1 / 2 steps PASS — 1 regressed
step   0 pass
step   1 drift on [mac, cycles]
    └─ mac       observed=3 · expected=2 · tol ±10.0%
    └─ cycles    observed=310 · expected=232 · tol ±10.0%
exit=1

Require exit status 1. alti also prints a status line on stderr that includes exit=1. Only the metrics that failed are listed under a step. To read them from the JSON report, select the metrics whose pass is false.

alti verification golden-diff check --project "$PROJECT" reference.jsonl changed.altrace --format json \
  | jq -c '.steps[] | select(.is_pass | not) | .metrics[] | select(.pass | not) | {name, observed, expected}'
{"name":"mac","observed":3,"expected":2}
{"name":"cycles","observed":310,"expected":232}

A step that the baseline lists but the trace lacks prints step N missing from observed trace. A step in the trace that has no baseline row is counted in unexpected_observed_steps. Both make the check exit 1.

Recover from failures

When the drift is the intended behavior, make the changed trace the new baseline and check again.

alti verification golden-diff emit-profile --project "$PROJECT" changed.altrace > "$PROJECT/reference.jsonl"
alti verification golden-diff check --project "$PROJECT" reference.jsonl changed.altrace; echo "exit=$?"
  golden-diff: all 2 steps within tolerance
step   0 pass
step   1 pass
exit=0

A file that is not a trace makes emit-profile and check exit 1 with the messages below. Pass a file that starts with ALTR, such as run.altrace.

printf 'this is not a trace\n' > "$PROJECT/notes.txt"
alti verification golden-diff emit-profile --project "$PROJECT" notes.txt
alti verification golden-diff check --project "$PROJECT" reference.jsonl notes.txt
parse error in 'notes.txt': Invalid magic number — expected 'ALTR', got '[116, 104, 105, 115]'
trace parse error for 'notes.txt': invalid .altrace magic

A trace whose header declares another payload encoding makes check exit 1 with a message of this form. Pass a trace whose header declares "encoding":"bincode", such as run.altrace.

trace parse error for 'other.altrace': unsupported .altrace payload encoding 'flatbuf'

A baseline without rows makes check exit 1 with the message below. Run emit-profile again to write a baseline with one row per step.

: > "$PROJECT/empty.jsonl"
alti verification golden-diff check --project "$PROJECT" empty.jsonl run.altrace
empty reference file: empty.jsonl

A negative or non-numeric --tolerance-pct exits 2 before the engine starts, with this text in the error. Pass a number of 0 or more, such as 5.

tolerance-pct must be a finite non-negative number
alti verification golden-diff emit-profile --project "$PROJECT" run.altrace --tolerance-pct 5 > "$PROJECT/reference.jsonl"

CLI engine commands