interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

GitHub aien-dev/omega-mv#969 (code only)

closed derived 3d3ae255c9b47f7f #969

Imported from GitHub aien-dev/omega-mv#969 (code only)

GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed).

Base 62d1d0d668f897386a0e311374f8dbcc5c32dde8 · Head f47763e564e1b4a7eb8705e4c0d1afcb8f119e09

Fetch it

A git bundle of the pull request's head, 36907 bytes.

curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/3d3ae255c9b47f7f.bundle -o 3d3ae255c9b47f7f.bundle
git fetch 3d3ae255c9b47f7f.bundle refs/pull/969/head

Raw bundle · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/omega-mv
969
f47763e564e1b4a7eb8705e4c0d1afcb8f119e09
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":969,"url":"https://github.com/aien-dev/omega-mv/pull/969","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:39:32Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/.crumb b/.crumb
index f3e51fe46..3572a1800 100644
--- a/.crumb
+++ b/.crumb
@@ -72,38 +72,38 @@
         },
         {
           "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986",
-          "last_commit": "a8a6a3d3c",
+          "last_commit": "62d1d0d66",
           "name": "spec",
           "purpose": "Directory spec. Purpose not yet described by a human."
         },
         {
           "digest": "aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7",
-          "last_commit": "bf54b19fa",
+          "last_commit": "089e5b053",
           "name": "src",
           "purpose": "Directory src. Purpose not yet described by a human."
         },
         {
-          "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874",
-          "last_commit": "a8a6a3d3c",
+          "digest": "030521c31acc733e6cad338aa4c7b24fc4ddd5bd6c070bfe82a5e267badca07e",
+          "last_commit": "18057225b",
           "name": "tests",
           "purpose": "Directory tests. Purpose not yet described by a human."
         },
         {
           "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29",
-          "last_commit": "a8a6a3d3c",
+          "last_commit": "62d1d0d66",
           "name": "tools",
           "purpose": "Directory tools. Purpose not yet described by a human."
         }
       ],
-      "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc",
-      "compiled_at": "2026-10-30T12:31:50Z",
+      "children_root": "sha256:bd177ce4f03f6ee7db2b31365a536e32a1333fa767c3d745ecd24ed1942452ac",
+      "compiled_at": "2026-10-30T16:30:17Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6",
+      "digest": "sha256:89a34f275bdfded5ecb0c0c3e90a7c82b5495443e054a77af1d0e928475768a5",
       "evidence_files": 12622,
       "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9",
-      "files": 17849,
-      "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06",
-      "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed"
+      "files": 17891,
+      "generated_at_commit": "7e835caaf01a5913e452441642e55e0758d51bab",
+      "source_tree": "sha256:45111fab41d072fc8afd2c460f4e19f596b14e6531a0281cec5ed70ae6dc1cc4"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/seed/README.md b/seed/README.md
index b37d60984..6f5a1cc42 100644
--- a/seed/README.md
+++ b/seed/README.md
@@ -984,6 +984,26 @@ or unrelated runtime failures are not accepted as control evidence.
 These checks qualify host parsing only. They do not access or qualify a GPU,
 alter the empirical scheduling model, or rewrite historical evidence.
 
+## Local trace segment (omega-mv #608)
+
+`seed/trace/segment.om` is a library unit (no `main`) for a local, append-only trace segment: a
+16-byte header and 64-byte records, each with its sequence number and a CRC-32. It can scan a segment
+read-only, and its recovery keeps the valid prefix and quarantines the damaged file as `<segment>.bad`.
+Run `sh seed/trace/gate.sh` (or `tests/gates/run.sh trace`) from the repository root on an
+AArch64 Linux host. The gate checks `seed/out/omc` against its recorded SHA-256, then requires the 71 checks
+in `seed/trace/segment-test.om` to print exactly `seed/trace/expected.txt`. It also requires four source
+mutants to fail their own checks: CRC comparison removed, torn tail ignored, append alignment guard
+weakened, and create with O_TRUNC in place of O_EXCL. `seed/trace/README.md` describes the format.
+
+`seed/trace/tracecli.om` is the offline read-only CLI for that format (omega-mv #610): `version`,
+`verify`, `show`, `events`, `stats`, `export`, `compare` and `list`. Nothing is written except stdout.
+Run `sh seed/trace/cli-gate.sh` (or `tests/gates/run.sh tracecli`) from the repository root on an
+AArch64 Linux host. The gate checks `seed/out/omc` against its recorded SHA-256, pins the generated
+fixture SHA-256 values and the golden stdout, refuses a mode-0000 segment, and requires four source
+mutants to fail their own checks. One of those is a literal scan of the read-only open flags (`32768`;
+a change to `32770`, which is `O_RDWR|O_NOFOLLOW`, must fail the scan). `seed/trace/README.md`
+describes the format and the CLI.
+
 ## How the checks stay honest
 
 - `check/a64check.c` and `check/audit.sh` are reference checks only. They call the repo's existing C
diff --git a/seed/trace/README.md b/seed/trace/README.md
new file mode 100644
index 000000000..d34a2a62d
--- /dev/null
+++ b/seed/trace/README.md
@@ -0,0 +1,108 @@
+# seed/trace: local trace segment (omega-mv #608)
+
+`segment.om` is a library unit in seeded Omega. It has no `main`, so concatenate it in front of a program
+that defines one (units concatenate; Omega has no import). Host words: `fopen`, `read`, `write`,
+`fseek`, `fsync`, `fclose`, `unlink`, `link`, `renames` (OMEGESE section 10). Linux AArch64 only.
+
+## Format version 1
+
+All integers are little-endian.
+
+| Part | Bytes | Fields |
+|------|-------|--------|
+| header | 16 | `AITR` (65 73 84 82), version u16 = 1, record size u16 = 64, header size u32 = 16, reserved u32 = 0 |
+| record | 64 | type u16, payload length u16 (at most 52), sequence u32, payload 52 bytes (zero past the length), CRC-32 u32 |
+
+- The sequence is the record index, assigned by the writer. A scan refuses a record whose sequence is
+  not its position, so a duplicated or reordered record is detected.
+- The CRC is CRC-32/IEEE (reflected polynomial 0xEDB88320 = 3988292384, initial value and final xor
+  0xFFFFFFFF) over record bytes 0..59. Its check value `CRC-32("123456789") = 3421780262` is the first
+  test (Wolfram: `Hash["123456789","CRC32"] -> 3421780262`).
+- A segment holds at most 4096 records, so the largest segment is `16 + 64*4096 = 262160` bytes.
+
+## Operations
+
+| Function | Behaviour |
+|----------|-----------|
+| `segcreate` | creates `tpath` with O_WRONLY, O_CREAT, O_EXCL and O_NOFOLLOW, mode 0600, and writes and fsyncs the header. An existing path is refused (10); it is never truncated |
+| `segready` | creates a missing segment. For an existing one it requires a valid header, and it never recreates the file |
+| `segappend t n` | appends one record with payload `pay[0..n)`. It refuses if the file length is not header plus whole records (25: torn tail, recover first), when the segment is full (26), or when the payload is over 52 bytes (20). Each record is fsynced |
+| `segscan` | read-only. Sets `scount` (valid records before the first damage), `sreason` (0 clean, 1 torn tail, 2 bad length, 3 CRC mismatch, 4 sequence mismatch, 5 over capacity), `stotal` (file bytes) and `slost` (bytes after the valid prefix). A header that is short, has bad magic (11), an unsupported version (15), or bad geometry or nonzero reserved bytes (16) is refused |
+| `segrecover` | returns 0 for a clean segment. Otherwise it writes the header and valid prefix to `<segment>.new` (O_EXCL, fsync), hard-links the damaged file to `<segment>.bad` (refused with 32 if that exists, so a quarantine is never overwritten), renames the new file over the segment and returns 1. Header errors are returned, not recovered |
+
+Every open uses O_NOFOLLOW, so a symbolic link as the segment path is refused (ELOOP, `serrno = -40`).
+The caller creates directories with mode 0700 (`makedir ... 448`); the test does so.
+
+## Gate
+
+`sh seed/trace/gate.sh`, or `tests/gates/run.sh trace` (manifest `tests/gates/omega-trace.gate`). It
+checks `seed/out/omc` against `seed/out/omc.sha256`, compiles `segment.om` plus `segment-test.om`, and
+runs the test in `build/seed/trace/main`. The test must exit 0 and its stdout must equal `expected.txt`.
+The 71 checks cover:
+
+- appending 100 records;
+- truncating the last record to 30 of its 64 bytes; the scan must then keep 99 records and report a
+  torn tail;
+- append refused until recovery; recovery keeps 99 records and quarantines the 6382-byte file; the next
+  append gets sequence 99;
+- a flipped payload byte in record 50; the scan must stop at 50 records with a CRC mismatch, and
+  recovery is refused while `.bad` exists;
+- crafted records with payload length 60 and with sequence 7;
+- header version, magic, geometry and reserved-byte refusals;
+- a symbolic-link path refused by scan, append and recover.
+
+Four source mutants must each compile, exit 1 and fail their own check: `crc` (CRC no longer compared,
+so `crc-reason` fails), `torn` (`torn-reason`), `align` (`torn-append-refused`) and `excl` (create with
+O_TRUNC, so `create-again-refused`).
+
+The expected sizes and offsets were computed in Wolfram Language:
+`{16+64*100, 16+64*99+30, 16+64*99, 16+64*50+8, (16+64*100)-16-64*50, 16+64*50, Mod[6382-16,64]}
+-> {6416, 6382, 6352, 3224, 3200, 3216, 30}`. The flags were checked the same way:
+`{BitOr[1,1024,32768], BitOr[1,32768], BitOr[1,64,128,32768], BitOr[1,64,512,32768]} -> {33793, 32769,
+32961, 33345}`. The remaining values (errno 2, 17 and 40, and the result codes) are pinned by the source.
+
+## CLI (omega-mv #610)
+
+`tracecli.om` is the offline, read-only inspector. Concatenate it after `segment.om` and `tsha.om`.
+Commands: `version`, `verify`, `show`, `events` (`--type`, `--range`), `stats`, `export`, `compare`,
+`list`. Nothing is written except stdout. Exit status: 0 ok, 1 a compare found a difference, 2 usage,
+3 partial or inconclusive, 4 refused, 5 a stdout write failed. Energy and timing are reported
+unavailable: format v1 carries no typed measurements. There is no `steps` command: format v1 has no
+step index, and `events` is the typed record listing.
+
+`tracegen.om` writes the fixtures (`fx/` in an empty scratch directory; regular files are left mode
+0400). Build it the same way, with `tracegen.om` in place of `tracecli.om`.
+
+`sh seed/trace/cli-gate.sh`, or `tests/gates/run.sh tracecli` (manifest
+`tests/gates/omega-tracecli.gate`). It checks `seed/out/omc` against `seed/out/omc.sha256`, compiles
+both programs, regenerates the fixtures and checks `fixtures.sha256` (plus the `link.seg` target and
+mode 0400), then requires each line of `cli.cases` to match `cli-golden/` and the recorded exit
+status. A copy of `fx/a.seg` made mode 000 must be refused (`refused code=10 errno=13`, exit 4) by
+`verify`, `show`, `events`, `stats` and `export`. The gate refuses to run as root, because mode 000
+would still be readable and the check would pass for the wrong reason.
+
+Four source mutants must each compile and fail their own check:
+
+- `partial`: the unique `ret 3` in `partial` changed to `ret 0`. `verify fx/crc.seg` still prints the
+  partial line and exits 0 (need 3). `lentry`'s directory return is spelled `ret ulong 3` so this
+  site stays unique.
+- `same`: `ret same` changed to `ret true`. `compare fx/a.seg fx/b.seg` prints `differ at=0` (need 17)
+  and exits 1.
+- `qtype`: the type test inverted (`ne` to `eq`). `events fx/a.seg --type 7` prints `matched=40 of=40`
+  (need 0).
+- `ropen`: the flags literal `32768` changed to `32770`. WolframLanguageEvaluator: `BitOr[2,32768] ->
+  32770` (`O_RDWR|O_NOFOLLOW`). The gate scans `tracecli.om` for the exact return line of `ropen`, and
+  this mutant fails that scan. A functional open check does not catch it: `verify` uses `segment.om`'s
+  own open, and an `O_RDWR` open of a mode-0400 file is not a reliable refusal for the owner. The
+  mode-0000 check is the functional refusal, and it runs on the good CLI.
+
+An edited golden must fail the gate on a stdout mismatch.
+
+## Not in this unit
+
+Issue #608 also lists items this unit does not provide: an index, rotation and retention, an async
+writer, schema migration beyond refusing other versions, export, consent and redaction. Concurrent
+writers are not supported either: there is no lock, and the unit assumes a single writer.
+
+The CLI above is a separate program. It does not add an index, rotation, retention, an async writer,
+consent, redaction, or a query-scaling measurement.
diff --git a/seed/trace/cli-gate.sh b/seed/trace/cli-gate.sh
new file mode 100755
index 000000000..75cb4aeb9
--- /dev/null
+++ b/seed/trace/cli-gate.sh
@@ -0,0 +1,169 @@
+#!/bin/sh
+# seed/trace/cli-gate.sh: omega-mv #610 offline trace CLI.
+# Run from the repository root: sh seed/trace/cli-gate.sh
+# Checks seed/out/omc against seed/out/omc.sha256, compiles tracegen and tracecli,
+# regenerates fixtures and pins their SHA-256, runs cli.cases against cli-golden,
+# refuses a mode-0000 segment, and requires four source mutants to fail their own
+# checks. The read-only mutant (ropen flags 32768 -> 32770) is caught by a literal
+# scan of the exact return line in tracecli.om. An O_RDWR open of a mode-0400 file
+# is not a reliable refusal for the owner, and verify uses segment.om's own open,
+# so a functional open check would not catch that mutant.
+set -eu
+cd "$(dirname "$0")/../.."
+
+bad() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
+
+sha256sum -c seed/out/omc.sha256 >/dev/null || bad "omc sha256"
+[ "$(id -u)" != 0 ] || bad "mode-0000 check is meaningless as root"
+
+ROPEN='ret fopen tpath 0 get tlen 0 32768 0'
+ROPEN_MUT='ret fopen tpath 0 get tlen 0 32770 0'
+scan_ropen() {
+  c=$(grep -c -x -F "$ROPEN" "$1" || true)
+  [ "$c" = 1 ]
+}
+scan_ropen seed/trace/tracecli.om || bad "ropen flags constant is not 32768 exactly once"
+if grep -q -x -F "$ROPEN_MUT" seed/trace/tracecli.om; then
+  bad "ropen flags 32770 present in the good source"
+fi
+[ "$(grep -c -x 'ret 3' seed/trace/tracecli.om || true)" = 1 ] || bad "partial site not unique"
+[ "$(grep -c -x 'ret same' seed/trace/tracecli.om || true)" = 1 ] || bad "same site not unique"
+[ "$(grep -c -x -F 'if and ne get qtype 0 65536 ne t get qtype 0' seed/trace/tracecli.om || true)" = 1 ] || bad "qtype site not unique"
+
+if [ -n "${OUT_DIR:-}" ]; then
+  B="$(pwd)/$OUT_DIR/trace-cli"
+else
+  B="$(pwd)/build/seed/trace-cli"
+fi
+mkdir -p "$B"
+compile() {
+  cat seed/trace/segment.om seed/trace/tsha.om "$1" | seed/out/omc > "$2"
+  [ "$(od -An -tx1 -N4 "$2" | tr -d ' ')" = 7f454c46 ] || bad "compile did not emit ELF: $1"
+  chmod 700 "$2"
+}
+compile seed/trace/tracegen.om "$B/tracegen"
+compile seed/trace/tracecli.om "$B/tracecli"
+
+ROOT=$(pwd)
+D=$(mktemp -d "${TMPDIR:-/tmp}/trace-cli.XXXXXX")
+cleanup() {
+  chmod -R u+rwx "$D" 2>/dev/null || true
+  rm -rf "$D"
+}
+trap cleanup EXIT
+
+( cd "$D" && "$B/tracegen" ) || bad "tracegen failed"
+if ! ( cd "$D" && sha256sum -c "$ROOT/seed/trace/fixtures.sha256" > "$D/sha.out" ); then
+  cat "$D/sha.out" >&2
+  bad "fixture sha"
+fi
+[ "$(readlink "$D/fx/link.seg")" = a.seg ] || bad "link target"
+for f in a b c crc torn magic; do
+  m=$(stat -c %a "$D/fx/$f.seg")
+  [ "$m" = 400 ] || bad "fixture $f mode $m want 400"
+done
+
+n=0
+while IFS= read -r line || [ -n "$line" ]; do
+  case "$line" in ''|\#*) continue ;; esac
+  set -f
+  # shellcheck disable=SC2086
+  set -- $line
+  set +f
+  name=$1
+  want=$2
+  shift 2
+  out="$D/case.out"
+  err="$D/case.err"
+  set +e
+  ( cd "$D" && "$B/tracecli" "$@" ) > "$out" 2>"$err"
+  rc=$?
+  set -e
+  [ ! -s "$err" ] || { cat "$err" >&2; bad "case $name wrote stderr"; }
+  cmp -s "$out" "seed/trace/cli-golden/$name.txt" || bad "case $name stdout"
+  [ "$rc" = "$want" ] || bad "case $name exit $rc want $want"
+  n=$((n + 1))
+done < seed/trace/cli.cases
+[ "$n" = 28 ] || bad "ran $n cases, need 28"
+
+( cd "$D" && "$B/tracecli" export fx/a.seg ) > "$D/export.out"
+got=$(sha256sum "$D/fx/a.seg" | cut -d' ' -f1)
+grep -q -F "# source sha256=$got " "$D/export.out" || bad "export hash != sha256sum"
+
+cp "$D/fx/a.seg" "$D/fx/zero.seg"
+chmod 000 "$D/fx/zero.seg"
+for cmd in verify show events stats export; do
+  set +e
+  ( cd "$D" && "$B/tracecli" "$cmd" fx/zero.seg ) > "$D/zero.out" 2>"$D/zero.err"
+  rc=$?
+  set -e
+  [ ! -s "$D/zero.err" ] || bad "mode-0000 $cmd wrote stderr"
+  [ "$rc" = 4 ] || bad "mode-0000 $cmd exit $rc want 4"
+  cmp -s "$D/zero.out" seed/trace/cli-golden/zero-read.txt || bad "mode-0000 $cmd stdout"
+done
+chmod 644 "$D/fx/zero.seg"
+rm -f "$D/fx/zero.seg"
+printf 'mode-0000 refused by verify show events stats export\n'
+
+# $1 dest  $2 exact old line  $3 exact new line. The old line must occur once
+# and be gone afterwards. The new line may already occur; its count must rise by 1.
+mut_swap() {
+  cp seed/trace/tracecli.om "$1"
+  o=$(grep -c -x -F "$2" "$1" || true)
+  before=$(grep -c -x -F "$3" "$1" || true)
+  [ "$o" = 1 ] || bad "mutant site count $o for: $2"
+  awk -v old="$2" -v new="$3" '($0 == old) { print new; next } { print }' "$1" > "$1.tmp"
+  mv "$1.tmp" "$1"
+  o=$(grep -c -x -F "$2" "$1" || true)
+  nn=$(grep -c -x -F "$3" "$1" || true)
+  [ "$o" = 0 ] || bad "mutant site survived: $2"
+  [ "$nn" = "$((before + 1))" ] || bad "mutant replacement count $nn want $((before + 1)) for: $3"
+}
+
+mut_swap "$D/mut-partial.om" 'ret 3' 'ret 0'
+compile "$D/mut-partial.om" "$B/mut-partial"
+set +e
+( cd "$D" && "$B/mut-partial" verify fx/crc.seg ) > "$D/mut.out" 2>"$D/mut.err"
+rc=$?
+set -e
+[ ! -s "$D/mut.err" ] || bad "partial mutant wrote stderr"
+cmp -s "$D/mut.out" seed/trace/cli-golden/verify-crc.txt || bad "partial mutant stdout"
+[ "$rc" = 0 ] || bad "partial mutant exit $rc want 0"
+printf 'caught mutant partial: verify crc exit 0 (need 3)\n'
+
+mut_swap "$D/mut-same.om" 'ret same' 'ret true'
+compile "$D/mut-same.om" "$B/mut-same"
+set +e
+( cd "$D" && "$B/mut-same" compare fx/a.seg fx/b.seg ) > "$D/mut.out" 2>"$D/mut.err"
+rc=$?
+set -e
+[ ! -s "$D/mut.err" ] || bad "same mutant wrote stderr"
+grep -q -F 'compare differ at=0' "$D/mut.out" || bad "same mutant did not differ at 0"
+if grep -q -F 'compare differ at=17' "$D/mut.out"; then
+  bad "same mutant still differs at 17"
+fi
+[ "$rc" = 1 ] || bad "same mutant exit $rc want 1"
+printf 'caught mutant same: compare a b differ at=0 (need 17)\n'
+
+mut_swap "$D/mut-qtype.om" 'if and ne get qtype 0 65536 ne t get qtype 0' 'if and ne get qtype 0 65536 eq t get qtype 0'
+compile "$D/mut-qtype.om" "$B/mut-qtype"
+set +e
+( cd "$D" && "$B/mut-qtype" events fx/a.seg --type 7 ) > "$D/mut.out" 2>"$D/mut.err"
+rc=$?
+set -e
+[ ! -s "$D/mut.err" ] || bad "qtype mutant wrote stderr"
+grep -q -F 'matched=40 of=40' "$D/mut.out" || bad "qtype mutant did not match all"
+if grep -q -F 'matched=0 of=40' "$D/mut.out"; then
+  bad "qtype mutant still matched none"
+fi
+[ "$rc" = 0 ] || bad "qtype mutant exit $rc want 0"
+printf 'caught mutant qtype: events --type 7 matched=40 (need 0)\n'
+
+mut_swap "$D/mut-ropen.om" "$ROPEN" "$ROPEN_MUT"
+compile "$D/mut-ropen.om" "$B/mut-ropen"
+if scan_ropen "$D/mut-ropen.om"; then
+  bad "ropen mutant was not caught by the flag scan"
+fi
+printf 'caught mutant ropen: flag scan rejected 32770\n'
+
+printf 'PASS tracecli %s cases, mode-0000 refused, 4 mutants caught\n' "$n"
diff --git a/seed/trace/cli-golden/compare-aa.txt b/seed/trace/cli-golden/compare-aa.txt
new file mode 100644
index 000000000..d63a4e7bb
--- /dev/null
+++ b/seed/trace/cli-golden/compare-aa.txt
@@ -0,0 +1,4 @@
+compare basis=records (segment format 1 carries no machine, build or precision metadata: not a qualified cross-run comparison)
+a: verify ok records=40 bytes=2576 lost=0 reason=clean
+b: verify ok records=40 bytes=2576 lost=0 reason=clean
+compare identical records=40
diff --git a/seed/trace/cli-golden/compare-ab.txt b/seed/trace/cli-golden/compare-ab.txt
new file mode 100644
index 000000000..34eab76f0
--- /dev/null
+++ b/seed/trace/cli-golden/compare-ab.txt
@@ -0,0 +1,4 @@
+compare basis=records (segment format 1 carries no machine, build or precision metadata: not a qualified cross-run comparison)
+a: verify ok records=40 bytes=2576 lost=0 reason=clean
+b: verify ok records=40 bytes=2576 lost=0 reason=clean
+compare differ at=17
diff --git a/seed/trace/cli-golden/compare-ac.txt b/seed/trace/cli-golden/compare-ac.txt
new file mode 100644
index 000000000..05120ba52
--- /dev/null
+++ b/seed/trace/cli-golden/compare-ac.txt
@@ -0,0 +1,4 @@
+compare basis=records (segment format 1 carries no machine, build or precision metadata: not a qualified cross-run comparison)
+a: verify ok records=40 bytes=2576 lost=0 reason=clean
+b: verify ok records=42 bytes=2704 lost=0 reason=clean
+compare prefix a=40 b=42
diff --git a/seed/trace/cli-golden/compare-crc.txt b/seed/trace/cli-golden/compare-crc.txt
new file mode 100644
index 000000000..28aa6e596
--- /dev/null
+++ b/seed/trace/cli-golden/compare-crc.txt
@@ -0,0 +1,4 @@
+compare basis=records (segment format 1 carries no machine, build or precision metadata: not a qualified cross-run comparison)
+a: verify ok records=40 bytes=2576 lost=0 reason=clean
+b: verify partial records=5 bytes=2576 lost=2240 reason=crc-mismatch
+compare inconclusive records=5
diff --git a/seed/trace/cli-golden/compare-link.txt b/seed/trace/cli-golden/compare-link.txt
new file mode 100644
index 000000000..c69bbfb62
--- /dev/null
+++ b/seed/trace/cli-golden/compare-link.txt
@@ -0,0 +1,4 @@
+compare basis=records (segment format 1 carries no machine, build or precision metadata: not a qualified cross-run comparison)
+a: verify ok records=40 bytes=2576 lost=0 reason=clean
+b: refused code=10 errno=40
+compare inconclusive
diff --git a/seed/trace/cli-golden/events-bad.txt b/seed/trace/cli-golden/events-bad.txt
new file mode 100644
index 000000000..690854819
--- /dev/null
+++ b/seed/trace/cli-golden/events-bad.txt
@@ -0,0 +1 @@
+usage: tracecli version | verify SEG | show SEG | events SEG [--type T] [--range FROM TO] | stats SEG [--type T] [--range FROM TO] | export SEG [--type T] [--range FROM TO] | compare SEGA SEGB | list DIR
diff --git a/seed/trace/cli-golden/events-range.txt b/seed/trace/cli-golden/events-range.txt
new file mode 100644
index 000000000..d5006e429
--- /dev/null
+++ b/seed/trace/cli-golden/events-range.txt
@@ -0,0 +1,9 @@
+segment version=1 recsize=64 hdrsize=16
+query type=any seq>=0 seq<5
+seq=0 type=1 len=0 crc=3468756871 payload=
+seq=1 type=2 len=5 crc=872128613 payload=070a0d1013
+seq=2 type=3 len=10 crc=918843208 payload=0e1114171a1d20232629
+seq=3 type=1 len=15 crc=3868058784 payload=15181b1e2124272a2d303336393c3f
+seq=4 type=2 len=20 crc=2733421296 payload=1c1f2225282b2e3134373a3d404346494c4f5255
+matched=5 of=40
+verify ok records=40 bytes=2576 lost=0 reason=clean
diff --git a/seed/trace/cli-golden/events-t7.txt b/seed/trace/cli-golden/events-t7.txt
new file mode 100644
index 000000000..96834f57e
--- /dev/null
+++ b/seed/trace/cli-golden/events-t7.txt
@@ -0,0 +1,4 @@
+segment version=1 recsize=64 hdrsize=16
+query type=7 seq>=0 seq<4096
+matched=0 of=40
+verify ok records=40 bytes=2576 lost=0 reason=clean
diff --git a/seed/trace/cli-golden/export-a.txt b/seed/trace/cli-golden/export-a.txt
new file mode 100644
index 000000000..e5d75b74f
--- /dev/null
+++ b/seed/trace/cli-golden/export-a.txt
@@ -0,0 +1,45 @@
+# tracecli 1 export
+# source sha256=aea1c831da7756f2131a550188e3a081a5f98287e730ae31a404406c953c352b bytes=2576
+# query type=any seq>=0 seq<4096
+# verify ok records=40 bytes=2576 lost=0 reason=clean
+seq,type,len,crc,payload_hex
+0,1,0,3468756871,
+1,2,5,872128613,070a0d1013
+2,3,10,918843208,0e1114171a1d20232629
+3,1,15,3868058784,15181b1e2124272a2d303336393c3f
+4,2,20,2733421296,1c1f2225282b2e3134373a3d404346494c4f5255
+5,3,25,896332734,2326292c2f3235383b3e4144474a4d505356595c5f6265686b
+6,1,30,447406245,2a2d303336393c3f4245484b4e5154575a5d606366696c6f7275787b7e81
+7,2,35,1969657584,3134373a3d404346494c4f5255585b5e6164676a6d707376797c7f8285888b8e919497
+8,3,40,1839143318,383b3e4144474a4d505356595c5f6265686b6e7174777a7d808386898c8f9295989b9ea1a4a7aaad
+9,1,45,3398721466,3f4245484b4e5154575a5d606366696c6f7275787b7e8184878a8d909396999c9fa2a5a8abaeb1b4b7babdc0c3
+10,2,50,4040774455,46494c4f5255585b5e6164676a6d707376797c7f8285888b8e9194979a9da0a3a6a9acafb2b5b8bbbec1c4c7cacdd0d3d6d9
+11,3,2,3645519854,4d50
+12,1,7,1270825219,54575a5d606366
+13,2,12,2238672133,5b5e6164676a6d707376797c
+14,3,17,1091975593,6265686b6e7174777a7d808386898c8f92
+15,1,22,492768763,696c6f7275787b7e8184878a8d909396999c9fa2a5a8
+16,2,27,1408939315,707376797c7f8285888b8e9194979a9da0a3a6a9acafb2b5b8bbbe
+17,3,32,3770363977,777a7d808386898c8f9295989b9ea1a4a7aaadb0b3b6b9bcbfc2c5c8cbced1d4
+18,1,37,3076818370,7e8184878a8d909396999c9fa2a5a8abaeb1b4b7babdc0c3c6c9cccfd2d5d8dbdee1e4e7ea
+19,2,42,1226721803,85888b8e9194979a9da0a3a6a9acafb2b5b8bbbec1c4c7cacdd0d3d6d9dcdfe2e5e8ebeef1f4f7fafd00
+20,3,47,1680124972,8c8f9295989b9ea1a4a7aaadb0b3b6b9bcbfc2c5c8cbced1d4d7dadde0e3e6e9eceff2f5f8fbfe0104070a0d101316
+21,1,52,2256176060,9396999c9fa2a5a8abaeb1b4b7babdc0c3c6c9cccfd2d5d8dbdee1e4e7eaedf0f3f6f9fcff0205080b0e1114171a1d202326292c
+22,2,4,1023785732,9a9da0a3
+23,3,9,1934154816,a1a4a7aaadb0b3b6b9
+24,1,14,1231062823,a8abaeb1b4b7babdc0c3c6c9cccf
+25,2,19,332895020,afb2b5b8bbbec1c4c7cacdd0d3d6d9dcdfe2e5
+26,3,24,816529961,b6b9bcbfc2c5c8cbced1d4d7dadde0e3e6e9eceff2f5f8fb
+27,1,29,2018055488,bdc0c3c6c9cccfd2d5d8dbdee1e4e7eaedf0f3f6f9fcff0205080b0e11
+28,2,34,2172456284,c4c7cacdd0d3d6d9dcdfe2e5e8ebeef1f4f7fafd000306090c0f1215181b1e212427
+29,3,39,4178906018,cbced1d4d7dadde0e3e6e9eceff2f5f8fbfe0104070a0d101316191c1f2225282b2e3134373a3d
+30,1,44,2661295768,d2d5d8dbdee1e4e7eaedf0f3f6f9fcff0205080b0e1114171a1d202326292c2f3235383b3e4144474a4d5053
+31,2,49,2216236436,d9dcdfe2e5e8ebeef1f4f7fafd000306090c0f1215181b1e2124272a2d303336393c3f4245484b4e5154575a5d60636669
+32,3,1,3140050622,e0
+33,1,6,2284831344,e7eaedf0f3f6
+34,2,11,2074369533,eef1f4f7fafd000306090c
+35,3,16,1844817994,f5f8fbfe0104070a0d101316191c1f22
+36,1,21,2713462898,fcff0205080b0e1114171a1d202326292c2f323538
+37,2,26,2574235802,0306090c0f1215181b1e2124272a2d303336393c3f4245484b4e
+38,3,31,1768968233,0a0d101316191c1f2225282b2e3134373a3d404346494c4f5255585b5e6164
+39,1,36,2690052430,1114171a1d202326292c2f3235383b3e4144474a4d505356595c5f6265686b6e7174777a
diff --git a/seed/trace/cli-golden/list-fx.txt b/seed/trace/cli-golden/list-fx.txt
new file mode 100644
index 000000000..08c6d6b62
--- /dev/null
+++ b/seed/trace/cli-golden/list-fx.txt
@@ -0,0 +1,9 @@
+a.seg: verify ok records=40 bytes=2576 lost=0 reason=clean
+b.seg: verify ok records=40 bytes=2576 lost=0 reason=clean
+c.seg: verify ok records=42 bytes=2704 lost=0 reason=clean
+crc.seg: verify partial records=5 bytes=2576 lost=2240 reason=crc-mismatch
+link.seg: refused code=10 errno=40
+magic.seg: refused code=11
+sub: directory
+torn.seg: verify partial records=39 bytes=2542 lost=30 reason=torn-tail
+list entries=8 ok=3 partial=2 refused=2 directories=1
diff --git a/seed/trace/cli-golden/list-link.txt b/seed/trace/cli-golden/list-link.txt
new file mode 100644
index 000000000..da829b992
--- /dev/null
+++ b/seed/trace/cli-golden/list-link.txt
@@ -0,0 +1 @@
+list refused errno=20
diff --git a/seed/trace/cli-golden/list-miss.txt b/seed/trace/cli-golden/list-miss.txt
new file mode 100644
index 000000000..0ba9e1131
--- /dev/null
+++ b/seed/trace/cli-golden/list-miss.txt
@@ -0,0 +1 @@
+list refused errno=2
diff --git a/seed/trace/cli-golden/list-sub.txt b/seed/trace/cli-golden/list-sub.txt
new file mode 100644
index 000000000..18eb22e53
--- /dev/null
+++ b/seed/trace/cli-golden/list-sub.txt
@@ -0,0 +1 @@
+list entries=0 ok=0 partial=0 refused=0 directories=0
diff --git a/seed/trace/cli-golden/show-a.txt b/seed/trace/cli-golden/show-a.txt
new file mode 100644
index 000000000..259d36199
--- /dev/null
+++ b/seed/trace/cli-golden/show-a.txt
@@ -0,0 +1,44 @@
+segment version=1 recsize=64 hdrsize=16
+query type=any seq>=0 seq<4096
+seq=0 type=1 len=0 crc=3468756871 payload=
+seq=1 type=2 len=5 crc=872128613 payload=070a0d1013
+seq=2 type=3 len=10 crc=918843208 payload=0e1114171a1d20232629
+seq=3 type=1 len=15 crc=3868058784 payload=15181b1e2124272a2d303336393c3f
+seq=4 type=2 len=20 crc=2733421296 payload=1c1f2225282b2e3134373a3d404346494c4f5255
+seq=5 type=3 len=25 crc=896332734 payload=2326292c2f3235383b3e4144474a4d505356595c5f6265686b
+seq=6 type=1 len=30 crc=447406245 payload=2a2d303336393c3f4245484b4e5154575a5d606366696c6f7275787b7e81
+seq=7 type=2 len=35 crc=1969657584 payload=3134373a3d404346494c4f5255585b5e6164676a6d707376797c7f8285888b8e919497
+seq=8 type=3 len=40 crc=1839143318 payload=383b3e4144474a4d505356595c5f6265686b6e7174777a7d808386898c8f9295989b9ea1a4a7aaad
+seq=9 type=1 len=45 crc=3398721466 payload=3f4245484b4e5154575a5d606366696c6f7275787b7e8184878a8d909396999c9fa2a5a8abaeb1b4b7babdc0c3
+seq=10 type=2 len=50 crc=4040774455 payload=46494c4f5255585b5e6164676a6d707376797c7f8285888b8e9194979a9da0a3a6a9acafb2b5b8bbbec1c4c7cacdd0d3d6d9
+seq=11 type=3 len=2 crc=3645519854 payload=4d50
+seq=12 type=1 len=7 crc=1270825219 payload=54575a5d606366
+seq=13 type=2 len=12 crc=2238672133 payload=5b5e6164676a6d707376797c
+seq=14 type=3 len=17 crc=1091975593 payload=6265686b6e7174777a7d808386898c8f92
+seq=15 type=1 len=22 crc=492768763 payload=696c6f7275787b7e8184878a8d909396999c9fa2a5a8
+seq=16 type=2 len=27 crc=1408939315 payload=707376797c7f8285888b8e9194979a9da0a3a6a9acafb2b5b8bbbe
+seq=17 type=3 len=32 crc=3770363977 payload=777a7d808386898c8f9295989b9ea1a4a7aaadb0b3b6b9bcbfc2c5c8cbced1d4
+seq=18 type=1 len=37 crc=3076818370 payload=7e8184878a8d909396999c9fa2a5a8abaeb1b4b7babdc0c3c6c9cccfd2d5d8dbdee1e4e7ea
+seq=19 type=2 len=42 crc=1226721803 payload=85888b8e9194979a9da0a3a6a9acafb2b5b8bbbec1c4c7cacdd0d3d6d9dcdfe2e5e8ebeef1f4f7fafd00
+seq=20 type=3 len=47 crc=1680124972 payload=8c8f9295989b9ea1a4a7aaadb0b3b6b9bcbfc2c5c8cbced1d4d7dadde0e3e6e9eceff2f5f8fbfe0104070a0d101316
+seq=21 type=1 len=52 crc=2256176060 payload=9396999c9fa2a5a8abaeb1b4b7babdc0c3c6c9cccfd2d5d8dbdee1e4e7eaedf0f3f6f9fcff0205080b0e1114171a1d202326292c
+seq=22 type=2 len=4 crc=1023785732 payload=9a9da0a3
+seq=23 type=3 len=9 crc=1934154816 payload=a1a4a7aaadb0b3b6b9
+seq=24 type=1 len=14 crc=1231062823 payload=a8abaeb1b4b7babdc0c3c6c9cccf
+seq=25 type=2 len=19 crc=332895020 payload=afb2b5b8bbbec1c4c7cacdd0d3d6d9dcdfe2e5
+seq=26 type=3 len=24 crc=816529961 payload=b6b9bcbfc2c5c8cbced1d4d7dadde0e3e6e9eceff2f5f8fb
+seq=27 type=1 len=29 crc=2018055488 payload=bdc0c3c6c9cccfd2d5d8dbdee1e4e7eaedf0f3f6f9fcff0205080b0e11
+seq=28 type=2 len=34 crc=2172456284 payload=c4c7cacdd0d3d6d9dcdfe2e5e8ebeef1f4f7fafd000306090c0f1215181b1e212427
+seq=29 type=3 len=39 crc=4178906018 payload=cbced1d4d7dadde0e3e6e9eceff2f5f8fbfe0104070a0d101316191c1f2225282b2e3134373a3d
+seq=30 type=1 len=44 crc=2661295768 payload=d2d5d8dbdee1e4e7eaedf0f3f6f9fcff0205080b0e1114171a1d202326292c2f3235383b3e4144474a4d5053
+seq=31 type=2 len=49 crc=2216236436 payload=d9dcdfe2e5e8ebeef1f4f7fafd000306090c0f1215181b1e2124272a2d303336393c3f4245484b4e5154575a5d60636669
+seq=32 type=3 len=1 crc=3140050622 payload=e0
+seq=33 type=1 len=6 crc=2284831344 payload=e7eaedf0f3f6
+seq=34 type=2 len=11 crc=2074369533 payload=eef1f4f7fafd000306090c
+seq=35 type=3 len=16 crc=1844817994 payload=f5f8fbfe0104070a0d101316191c1f22
+seq=36 type=1 len=21 crc=2713462898 payload=fcff0205080b0e1114171a1d202326292c2f323538
+seq=37 type=2 len=26 crc=2574235802 payload=0306090c0f1215181b1e2124272a2d303336393c3f4245484b4e
+seq=38 type=3 len=31 crc=1768968233 payload=0a0d101316191c1f2225282b2e3134373a3d404346494c4f5255585b5e6164
+seq=39 type=1 len=36 crc=2690052430 payload=1114171a1d202326292c2f3235383b3e4144474a4d505356595c5f6265686b6e7174777a
+matched=40 of=40
+verify ok records=40 bytes=2576 lost=0 reason=clean
diff --git a/seed/trace/cli-golden/show-magic.txt b/seed/trace/cli-golden/show-magic.txt
new file mode 100644
index 000000000..1f675346e
--- /dev/null
+++ b/seed/trace/cli-golden/show-magic.txt
@@ -0,0 +1 @@
+refused code=11
diff --git a/seed/trace/cli-golden/stats-a.txt b/seed/trace/cli-golden/stats-a.txt
new file mode 100644
index 000000000..e12f9afc9
--- /dev/null
+++ b/seed/trace/cli-golden/stats-a.txt
@@ -0,0 +1,8 @@
+segment version=1 recsize=64 hdrsize=16
+query type=any seq>=0 seq<4096
+type=1 records=14 payload-bytes=358
+type=2 records=13 payload-bytes=334
+type=3 records=13 payload-bytes=293
+stats records=40 types=3 payload-bytes=985
+energy=unavailable timing=unavailable (segment format 1 carries no typed measurements)
+verify ok records=40 bytes=2576 lost=0 reason=clean
diff --git a/seed/trace/cli-golden/stats-torn.txt b/seed/trace/cli-golden/stats-torn.txt
new file mode 100644
index 000000000..d13214a1b
--- /dev/null
+++ b/seed/trace/cli-golden/stats-torn.txt
@@ -0,0 +1,8 @@
+segment version=1 recsize=64 hdrsize=16
+query type=any seq>=0 seq<4096
+type=1 records=13 payload-bytes=322
+type=2 records=13 payload-bytes=334
+type=3 records=13 payload-bytes=293
+stats records=39 types=3 payload-bytes=949
+energy=unavailable timing=unavailable (segment format 1 carries no typed measurements)
+verify partial records=39 bytes=2542 lost=30 reason=torn-tail
diff --git a/seed/trace/cli-golden/unknown.txt b/seed/trace/cli-golden/unknown.txt
new file mode 100644
index 000000000..690854819
--- /dev/null
+++ b/seed/trace/cli-golden/unknown.txt
@@ -0,0 +1 @@
+usage: tracecli version | verify SEG | show SEG | events SEG [--type T] [--range FROM TO] | stats SEG [--type T] [--range FROM TO] | export SEG [--type T] [--range FROM TO] | compare SEGA SEGB | list DIR
diff --git a/seed/trace/cli-golden/usage.txt b/seed/trace/cli-golden/usage.txt
new file mode 100644
index 000000000..690854819
--- /dev/null
+++ b/seed/trace/cli-golden/usage.txt
@@ -0,0 +1 @@
+usage: tracecli version | verify SEG | show SEG | events SEG [--type T] [--range FROM TO] | stats SEG [--type T] [--range FROM TO] | export SEG [--type T] [--range FROM TO] | compare SEGA SEGB | list DIR
diff --git a/seed/trace/cli-golden/verify-a.txt b/seed/trace/cli-golden/verify-a.txt
new file mode 100644
index 000000000..8175532c1
--- /dev/null
+++ b/seed/trace/cli-golden/verify-a.txt
@@ -0,0 +1 @@
+verify ok records=40 bytes=2576 lost=0 reason=clean
diff --git a/seed/trace/cli-golden/verify-c.txt b/seed/trace/cli-golden/verify-c.txt
new file mode 100644
index 000000000..493d4db0c
--- /dev/null
+++ b/seed/trace/cli-golden/verify-c.txt
@@ -0,0 +1 @@
+verify ok records=42 bytes=2704 lost=0 reason=clean
diff --git a/seed/trace/cli-golden/verify-crc.txt b/seed/trace/cli-golden/verify-crc.txt
new file mode 100644
index 000000000..16bcc680a
--- /dev/null
+++ b/seed/trace/cli-golden/verify-crc.txt
@@ -0,0 +1 @@
+verify partial records=5 bytes=2576 lost=2240 reason=crc-mismatch
diff --git a/seed/trace/cli-golden/verify-dir.txt b/seed/trace/cli-golden/verify-dir.txt
new file mode 100644
index 000000000..1f675346e
--- /dev/null
+++ b/seed/trace/cli-golden/verify-dir.txt
@@ -0,0 +1 @@
+refused code=11
diff --git a/seed/trace/cli-golden/verify-link.txt b/seed/trace/cli-golden/verify-link.txt
new file mode 100644
index 000000000..22b23d498
--- /dev/null
+++ b/seed/trace/cli-golden/verify-link.txt
@@ -0,0 +1 @@
+refused code=10 errno=40
diff --git a/seed/trace/cli-golden/verify-magic.txt b/seed/trace/cli-golden/verify-magic.txt
new file mode 100644
index 000000000..1f675346e
--- /dev/null
+++ b/seed/trace/cli-golden/verify-magic.txt
@@ -0,0 +1 @@
+refused code=11
diff --git a/seed/trace/cli-golden/verify-miss.txt b/seed/trace/cli-golden/verify-miss.txt
new file mode 100644
index 000000000..83dcb8462
--- /dev/null
+++ b/seed/trace/cli-golden/verify-miss.txt
@@ -0,0 +1 @@
+refused code=10 errno=2
diff --git a/seed/trace/cli-golden/verify-torn.txt b/seed/trace/cli-golden/verify-torn.txt
new file mode 100644
index 000000000..83a8c263f
--- /dev/null
+++ b/seed/trace/cli-golden/verify-torn.txt
@@ -0,0 +1 @@
+verify partial records=39 bytes=2542 lost=30 reason=torn-tail
diff --git a/seed/trace/cli-golden/version.txt b/seed/trace/cli-golden/version.txt
new file mode 100644
index 000000000..ea592f3a9
--- /dev/null
+++ b/seed/trace/cli-golden/version.txt
@@ -0,0 +1 @@
+tracecli 1 segment-format 1
diff --git a/seed/trace/cli-golden/zero-read.txt b/seed/trace/cli-golden/zero-read.txt
new file mode 100644
index 000000000..05b7e8e4a
--- /dev/null
+++ b/seed/trace/cli-golden/zero-read.txt
@@ -0,0 +1 @@
+refused code=10 errno=13
diff --git a/seed/trace/cli.cases b/seed/trace/cli.cases
new file mode 100644
index 000000000..605b42be1
--- /dev/null
+++ b/seed/trace/cli.cases
@@ -0,0 +1,29 @@
+# name exit args...  paths are relative to the scratch directory tracegen writes fx/ into.
+version 0 version
+usage 2
+unknown 2 nosuch
+verify-a 0 verify fx/a.seg
+verify-c 0 verify fx/c.seg
+verify-crc 3 verify fx/crc.seg
+verify-torn 3 verify fx/torn.seg
+verify-magic 4 verify fx/magic.seg
+verify-link 4 verify fx/link.seg
+verify-miss 4 verify fx/missing.seg
+verify-dir 4 verify fx/sub
+events-t7 0 events fx/a.seg --type 7
+events-range 0 events fx/a.seg --range 0 5
+events-bad 2 events fx/a.seg --range 9 3
+stats-a 0 stats fx/a.seg
+stats-torn 3 stats fx/torn.seg
+export-a 0 export fx/a.seg
+compare-ab 1 compare fx/a.seg fx/b.seg
+compare-ac 1 compare fx/a.seg fx/c.seg
+compare-aa 0 compare fx/a.seg fx/a.seg
+compare-crc 3 compare fx/a.seg fx/crc.seg
+compare-link 4 compare fx/a.seg fx/link.seg
+list-fx 3 list fx
+list-sub 0 list fx/sub
+list-link 4 list fx/link.seg
+list-miss 4 list fx/missing
+show-a 0 show fx/a.seg
+show-magic 4 show fx/magic.seg
diff --git a/seed/trace/expected.txt b/seed/trace/expected.txt
new file mode 100644
index 000000000..9e0b44220
--- /dev/null
+++ b/seed/trace/expected.txt
@@ -0,0 +1,72 @@
+crc-123456789 3421780262
+crc-empty 0
+missing-scan 10
+missing-errno 2
+create 0
+create-again-refused 10
+create-again-errno 17
+append-100-errors 0
+append-last-seq 99
+scan 0
+scan-count 100
+scan-reason 0
+scan-total 6416
+scan-lost 0
+append-oversize-refused 20
+truncate-last-record 0
+torn-scan 0
+torn-count 99
+torn-reason 1
+torn-lost 30
+torn-append-refused 25
+torn-size-unchanged 6382
+torn-recover 1
+torn-recover-lost 30
+recovered-scan 0
+recovered-count 99
+recovered-reason 0
+recovered-total 6352
+quarantine-size 6382
+append-after-recover 0
+append-after-seq 99
+recover-clean 0
+clean-count 100
+crc-flip-poke 0
+crc-scan 0
+crc-count 50
+crc-reason 3
+crc-lost 3200
+crc-recover-bad-exists 32
+crc-segment-unchanged 6416
+unlink-bad 0
+crc-recover 1
+crc-recovered-scan 0
+crc-recovered-count 50
+crc-recovered-reason 0
+len-raw-append 0
+len-scan 0
+len-count 50
+len-reason 2
+len-recover 1
+seq-raw-append 0
+seq-scan 0
+seq-count 50
+seq-reason 4
+seq-recover 1
+version-poke 0
+version-scan 15
+version-append-refused 15
+version-size-unchanged 3216
+magic-scan 11
+geometry-scan 16
+reserved-scan 16
+restored-scan 0
+restored-count 50
+symlink-create 0
+symlink-scan 10
+symlink-errno 40
+symlink-append-refused 10
+symlink-recover-refused 10
+final-scan 0
+final-count 50
+segment-test checks=71 fails=0
diff --git a/seed/trace/fixtures.sha256 b/seed/trace/fixtures.sha256
new file mode 100644
index 000000000..ff75659ca
--- /dev/null
+++ b/seed/trace/fixtures.sha256
@@ -0,0 +1,6 @@
+aea1c831da7756f2131a550188e3a081a5f98287e730ae31a404406c953c352b  fx/a.seg
+b9411a839b70bec4237e21a2c4d3175567ecee59d65bd8ff16d5a0e6590b2acd  fx/b.seg
+263276cc880e6d7ba9cc4308af282fbdcf58c8cea3b1a9cab30ab438a523e3c1  fx/c.seg
+dcfc13c5e612f91c7a1c6b3a31c24696736aba387f5dd899f12bc84359ddee0d  fx/crc.seg
+1e4638b03ce43640aaab695405c509411975fef31244d5659827cb5f00890c81  fx/torn.seg
+d411f415475dbd6689a12284967a04d172112337d91b43ffc0446bd519af32fd  fx/magic.seg
diff --git a/seed/trace/gate.sh b/seed/trace/gate.sh
new file mode 100755
index 000000000..ac6ee4b33
--- /dev/null
+++ b/seed/trace/gate.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+# seed/trace/gate.sh -- host gate for the local trace segment (omega-mv #608).
+# Run from anywhere:  sh seed/trace/gate.sh
+# Compiles segment.om + segment-test.om with seed/out/omc (sha checked against seed/out/omc.sha256), runs
+# the test in a fresh scratch directory and requires exit 0 and stdout equal to expected.txt byte for byte.
+# The expected values (sizes, offsets, CRC check value, flags) were computed in Wolfram Language; see
+# README.md. Then each source mutant (a literal swap of a line that must occur exactly once in segment.om)
+# must compile, exit 1, and fail on its own check label:
+#   crc     record CRC no longer compared        -> crc-reason FAIL
+#   torn    partial tail not reported as torn    -> torn-reason FAIL
+#   align   append ignores a misaligned tail     -> torn-append-refused FAIL
+#   excl    create opens with O_TRUNC, not O_EXCL -> create-again-refused FAIL
+# Prints PASS trace and exits 0, or FAIL and exits 1.
+set -eu
+R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd)
+cd "$R"
+D=seed/trace
+B=build/seed/trace
+rm -rf "$B"
+mkdir -p "$B"
+bad() { printf 'FAIL trace: %s\n' "$*" >&2; exit 1; }
+
+want=$(cut -c1-64 seed/out/omc.sha256)
+have=$(sha256sum < seed/out/omc | cut -c1-64)
+[ "$want" = "$have" ] || bad "seed/out/omc is $have, omc.sha256 says $want"
+OMC="$R/seed/out/omc"
+
+# run NAME SRC: compile SRC to $B/NAME/t, run it inside $B/NAME, stdout to $B/NAME/out.txt, sets rc
+run() {
+  mkdir -p "$B/$1"
+  "$OMC" < "$2" > "$B/$1/t" || bad "omc refused $2"
+  chmod 700 "$B/$1/t"
+  rc=0
+  (cd "$B/$1" && ./t > out.txt) || rc=$?
+}
+
+cat "$D/segment.om" "$D/segment-test.om" > "$B/test.om"
+run main "$B/test.om"
+printf 'omc %s\nsegment-test %s\n' "$have" "$(sha256sum < "$B/main/t" | cut -c1-64)"
+[ "$rc" -eq 0 ] || { cat "$B/main/out.txt" >&2; bad "segment-test exited $rc"; }
+cmp -s "$B/main/out.txt" "$D/expected.txt" || { diff "$D/expected.txt" "$B/main/out.txt" >&2 || true; bad "output differs from expected.txt"; }
+
+n=0
+mutant() { # NAME SITE REPLACEMENT LABEL
+  c=$(grep -c -x -F "$2" "$D/segment.om" || true)
+  [ "$c" = 1 ] || bad "mutant $1: site found $c times"
+  awk -v s="$2" -v r="$3" '$0 == s { print r; next } { print }' "$D/segment.om" > "$B/$1.om"
+  cmp -s "$B/$1.om" "$D/segment.om" && bad "mutant $1: source unchanged"
+  cat "$B/$1.om" "$D/segment-test.om" > "$B/$1-test.om"
+  run "$1" "$B/$1-test.om"
+  [ "$rc" -eq 1 ] || bad "mutant $1 exited $rc, need 1"
+  grep -q "^$4 [0-9]* FAIL want=" "$B/$1/out.txt" || bad "mutant $1 did not fail $4"
+  echo "mutant $1: exit 1, $4 FAIL"
+  n=$((n + 1))
+}
+mutant crc 'if ne want segcrc 60' 'if ne want want' crc-reason
+mutant torn 'put sreason 0 1' 'put sreason 0 0' torn-reason
+mutant align 'if ne rem r 64 0' 'if ne rem r 1 0' torn-append-refused
+mutant excl 'fd = fopen tpath 0 get tlen 0 32961 384' 'fd = fopen tpath 0 get tlen 0 33345 384' create-again-refused
+printf 'PASS trace %s checks, %s/4 mutants caught\n' "$(sed -n 's/^segment-test checks=\([0-9]*\) fails=0$/\1/p' "$B/main/out.txt")" "$n"
diff --git a/seed/trace/segment-test.om b/seed/trace/segment-test.om
new file mode 100644
index 000000000..5f291b2f7
--- /dev/null
+++ b/seed/trace/segment-test.om
@@ -0,0 +1,333 @@
+note SPDX-License-Identifier: AGPL-3.0-only
+note seed/trace/segment-test.om: checks for segment.om (#608). Concatenate after segment.om and compile.
+note Runs in the current directory under build/trace-test/. Each check prints "<label> <observed>" and,
+note when it differs, " FAIL want=<expected>". The gate compares stdout with expected.txt byte for byte.
+note Exit 0 when every check holds, 1 otherwise, 2 when stdout cannot be written.
+global checks ulong 1
+global fails ulong 1
+global obuf uchar 256
+global on ulong 1
+global wbad ulong 1
+text labs "crc-123456789\ncrc-empty\nmissing-scan\nmissing-errno\ncreate\ncreate-again-refused\ncreate-again-errno\nappend-100-errors\nappend-last-seq\nscan\nscan-count\nscan-reason\nscan-total\nscan-lost\nappend-oversize-refused\ntruncate-last-record\ntorn-scan\ntorn-count\ntorn-reason\ntorn-lost\ntorn-append-refused\ntorn-size-unchanged\ntorn-recover\ntorn-recover-lost\nrecovered-scan\nrecovered-count\nrecovered-reason\nrecovered-total\nquarantine-size\nappend-after-recover\nappend-after-seq\nrecover-clean\nclean-count\ncrc-flip-poke\ncrc-scan\ncrc-count\ncrc-reason\ncrc-lost\ncrc-recover-bad-exists\ncrc-segment-unchanged\nunlink-bad\ncrc-recover\ncrc-recovered-scan\ncrc-recovered-count\ncrc-recovered-reason\nlen-raw-append\nlen-scan\nlen-count\nlen-reason\nlen-recover\nseq-raw-append\nseq-scan\nseq-count\nseq-reason\nseq-recover\nversion-poke\nversion-scan\nversion-append-refused\nversion-size-unchanged\nmagic-scan\ngeometry-scan\nreserved-scan\nrestored-scan\nrestored-count\nsymlink-create\nsymlink-scan\nsymlink-errno\nsymlink-append-refused\nsymlink-recover-refused\nfinal-scan\nfinal-count\n"
+text paths "build/trace-test/segment\nbuild/trace-test/link\n"
+text dbuild "build"
+text dtest "build/trace-test"
+text tgt "segment"
+text sfail " FAIL want="
+text sumt "segment-test checks="
+text sumf " fails="
+fn oflush to void
+w = write 1 obuf 0 get on 0
+if ne w long get on 0
+put wbad 0 1
+end
+put on 0 0
+end
+fn obyte b uchar to void
+put obuf get on 0 b
+put on 0 add get on 0 1
+end
+fn onum v ulong to void
+d = new uchar 20 0
+x = v
+k = ulong 0
+go = true
+while go bound 21
+put d k uchar add 48 rem x 10
+k = add k 1
+x = div x 10
+if eq x 0
+go = false
+end
+end
+while gt k 0 bound 21
+k = sub k 1
+obyte get d k
+end
+end
+note label k: emit the k-th newline-terminated line of labs (k from 1).
+fn label k ulong to void
+line = ulong 1
+for i ulong len labs
+b = get labs i
+if eq b 10
+line = add line 1
+else
+if eq line k
+obyte b
+end
+end
+end
+end
+fn check got ulong want ulong to void
+put checks 0 add get checks 0 1
+label get checks 0
+obyte 32
+onum got
+if ne got want
+put fails 0 add get fails 0 1
+for i ulong len sfail
+obyte get sfail i
+end
+onum want
+end
+obyte 10
+oflush
+end
+note setp k: the k-th line of paths (k from 1) becomes the segment path.
+fn setp k ulong to void
+line = ulong 1
+n = ulong 0
+for i ulong len paths
+b = get paths i
+if eq b 10
+line = add line 1
+else
+if eq line k
+put tpath n b
+n = add n 1
+end
+end
+end
+put tlen 0 n
+end
+fn errno to ulong
+ret ulong neg get serrno 0
+end
+fn fsize to ulong
+fd = fopen tpath 0 get tlen 0 32768 0
+if lt fd 0
+ret 0
+end
+pos = fseek int fd 0 2
+shut fd
+if lt pos 0
+ret 0
+end
+ret ulong pos
+end
+fn fsizebad to ulong
+sidepath 1
+fd = fopen opath 0 get olen 0 32768 0
+if lt fd 0
+ret 0
+end
+pos = fseek int fd 0 2
+shut fd
+if lt pos 0
+ret 0
+end
+ret ulong pos
+end
+note poke off v: overwrite one byte of the segment in place (fault injection, not a library path).
+fn poke off ulong v ulong to ulong
+fd = fopen tpath 0 get tlen 0 32769 0
+if lt fd 0
+ret 1
+end
+put trec 0 uchar v
+w = pwrite int fd trec 0 1 long off
+shut fd
+if ne w 1
+ret 1
+end
+ret 0
+end
+note rawapp: append trec as-is, bypassing segappend (fault injection for crafted records).
+fn rawapp to ulong
+fd = fopen tpath 0 get tlen 0 33793 0
+if lt fd 0
+ret 1
+end
+w = write int fd trec 0 64
+shut fd
+if ne w 64
+ret 1
+end
+ret 0
+end
+fn unlbad to ulong
+sidepath 1
+u = unlink opath 0 get olen 0
+if lt u 0
+ret 1
+end
+ret 0
+end
+fn mklink to ulong
+setp 2
+put tn 0 0
+tput16 2
+tput16 len tgt
+for i ulong len tgt
+put tpkt add get tn 0 i get tgt i
+end
+put tn 0 add get tn 0 len tgt
+pkttop
+s = symlnk tpkt 0 get tn 0
+setp 1
+if lt s 0
+ret 1
+end
+ret 0
+end
+fn cleanup to void
+setp 2
+u1 = unlink tpath 0 get tlen 0
+setp 1
+u2 = unlink tpath 0 get tlen 0
+sidepath 1
+u3 = unlink opath 0 get olen 0
+sidepath 2
+u4 = unlink opath 0 get olen 0
+end
+fn crcvec to void
+for i ulong 9
+put trec i uchar add 49 i
+end
+check segcrc 9 3421780262
+check segcrc 0 0
+end
+fn fill100 to void
+bad = ulong 0
+for i ulong 100
+put pay 0 uchar and i 255
+put pay 1 uchar and shr i 8 255
+put pay 2 0
+put pay 3 0
+if ne segappend 1 4 0
+bad = add bad 1
+end
+end
+check bad 0
+check get sseq 0 99
+end
+fn scan5 c ulong r ulong t ulong l ulong to void
+check segscan 0
+check get scount 0 c
+check get sreason 0 r
+check get stotal 0 t
+check get slost 0 l
+end
+fn phase1 to void
+check segscan 10
+check errno 2
+check segcreate 0
+check segcreate 10
+check errno 17
+fill100
+scan5 100 0 6416 0
+check segappend 1 53 20
+check ulong truncate tpath 0 get tlen 0 6382 0
+check segscan 0
+check get scount 0 99
+check get sreason 0 1
+check get slost 0 30
+check segappend 1 4 25
+check fsize 6382
+check segrecover 1
+check get slost 0 30
+check segscan 0
+check get scount 0 99
+check get sreason 0 0
+check get stotal 0 6352
+check fsizebad 6382
+check segappend 1 4 0
+check get sseq 0 99
+check segrecover 0
+e = segscan
+end
+fn phase2 to void
+check get scount 0 100
+check poke 3224 51 0
+check segscan 0
+check get scount 0 50
+check get sreason 0 3
+check get slost 0 3200
+check segrecover 32
+check fsize 6416
+check unlbad 0
+check segrecover 1
+check segscan 0
+check get scount 0 50
+check get sreason 0 0
+end
+fn phase3 to void
+segpack 1 4 50
+put trec 2 60
+segseal
+check rawapp 0
+check segscan 0
+check get scount 0 50
+check get sreason 0 2
+e = unlbad
+check segrecover 1
+segpack 1 4 7
+check rawapp 0
+check segscan 0
+check get scount 0 50
+check get sreason 0 4
+e2 = unlbad
+check segrecover 1
+end
+fn phase4 to void
+check poke 4 2 0
+check segscan 15
+check segappend 1 4 15
+check fsize 3216
+p1 = poke 4 1
+p2 = poke 0 0
+check segscan 11
+p3 = poke 0 65
+p4 = poke 6 32
+check segscan 16
+p5 = poke 6 64
+p6 = poke 12 1
+check segscan 16
+p7 = poke 12 0
+check segscan 0
+check get scount 0 50
+end
+fn phase5 to void
+check mklink 0
+setp 2
+check segscan 10
+check errno 40
+check segappend 1 4 10
+check segrecover 10
+setp 1
+check segscan 0
+check get scount 0 50
+end
+fn summary to void
+for i ulong len sumt
+obyte get sumt i
+end
+onum get checks 0
+for i ulong len sumf
+obyte get sumf i
+end
+onum get fails 0
+obyte 10
+oflush
+end
+fn main to int
+m1 = makedir dbuild 0 len dbuild 448
+m2 = makedir dtest 0 len dtest 448
+cleanup
+setp 1
+crcvec
+phase1
+phase2
+phase3
+phase4
+phase5
+summary
+if ne get wbad 0 0
+ret 2
+end
+if ne get fails 0 0
+ret 1
+end
+ret 0
+end
diff --git a/seed/trace/segment.om b/seed/trace/segment.om
new file mode 100644
index 000000000..0d3cf2670
--- /dev/null
+++ b/seed/trace/segment.om
@@ -0,0 +1,424 @@
+note SPDX-License-Identifier: AGPL-3.0-only
+note seed/trace/segment.om: local trace segment, format version 1 (omega-mv #608). Library unit with no
+note main: concatenate it before a program that defines main (units concatenate; there is no import).
+note Header, 16 bytes: "AITR", version u16 = 1, record size u16 = 64, header size u32 = 16, reserved
+note u32 = 0. Record, 64 bytes: type u16, payload length u16 (at most 52), sequence u32 (= record index,
+note assigned by the writer), 52 payload bytes (zero past the length), CRC-32 u32 of bytes 0..59.
+note CRC-32 is IEEE 802.3, reflected polynomial 3988292384 (0xEDB88320), initial value and final xor
+note 4294967295. Every integer is little-endian. Single writer. No network, consent, export, index,
+note rotation or retention in this unit. The caller puts the segment path in tpath and its length in
+note tlen; sibling paths are the same path plus ".bad" (quarantine) or ".new" (recovery staging).
+note Opens carry O_NOFOLLOW (32768), so a symbolic link in the last path component is refused.
+global thdr uchar 16
+global trec uchar 64
+global tpath uchar 1024
+global tlen ulong 1
+global opath uchar 1040
+global olen ulong 1
+global tpkt uchar 2100
+global tn ulong 1
+global pay uchar 52
+global scount ulong 1
+global sreason ulong 1
+global stotal ulong 1
+global slost ulong 1
+global sseq ulong 1
+global serrno long 1
+text sbadsfx ".bad"
+text snewsfx ".new"
+note Error codes (function results): 0 ok; 10 open refused (missing, exists on create, symlink:
+note serrno holds -errno); 11 short header or bad magic; 12 close failed; 13 header write/sync failed;
+note 15 unsupported version; 16 bad geometry or nonzero reserved bytes; 17 read error; 20 payload over
+note 52 bytes; 21 append open failed; 22 short record write (a torn tail may now exist); 23 fsync;
+note 24 close; 25 segment length is not header + whole records (torn tail: recover first); 26 segment
+note full (4096 records); 27 seek failed; 30 staging file; 31 copy failed; 32 quarantine link refused
+note (segment.bad already exists or link failed); 33 rename of the staged segment failed.
+note Scan reasons (sreason): 0 clean; 1 torn tail (partial record); 2 payload length over 52;
+note 3 CRC mismatch; 4 sequence is not the record index; 5 more than 4096 records.
+fn shut fd long to void
+c = fclose int fd
+if lt c 0
+put serrno 0 c
+end
+end
+fn sidepath k ulong to void
+n = get tlen 0
+for i ulong n
+put opath i get tpath i
+end
+if eq k 1
+for i ulong len sbadsfx
+put opath add n i get sbadsfx i
+end
+put olen 0 add n len sbadsfx
+else
+for i ulong len snewsfx
+put opath add n i get snewsfx i
+end
+put olen 0 add n len snewsfx
+end
+end
+fn tput16 v ulong to void
+put tpkt get tn 0 uchar and v 255
+put tpkt add get tn 0 1 uchar and shr v 8 255
+put tn 0 add get tn 0 2
+end
+fn pkttop to void
+tput16 get tlen 0
+for i ulong get tlen 0
+put tpkt add get tn 0 i get tpath i
+end
+put tn 0 add get tn 0 get tlen 0
+end
+fn pktoth to void
+tput16 get olen 0
+for i ulong get olen 0
+put tpkt add get tn 0 i get opath i
+end
+put tn 0 add get tn 0 get olen 0
+end
+fn segcrc n ulong to ulong
+c = ulong 4294967295
+for i ulong n
+c = xor c ulong get trec i
+for k ulong 8
+if eq and c 1 1
+c = xor shr c 1 3988292384
+else
+c = shr c 1
+end
+end
+end
+ret xor c 4294967295
+end
+fn ld16 o ulong to ulong
+ret or ulong get trec o shl ulong get trec add o 1 8
+end
+fn ld32 o ulong to ulong
+lo = ld16 o
+hi = ld16 add o 2
+ret or lo shl hi 16
+end
+fn st32 o ulong v ulong to void
+put trec o uchar and v 255
+put trec add o 1 uchar and shr v 8 255
+put trec add o 2 uchar and shr v 16 255
+put trec add o 3 uchar and shr v 24 255
+end
+fn segseal to void
+st32 60 segcrc 60
+end
+fn segpack t ulong n ulong seq ulong to void
+for i ulong 64
+put trec i 0
+end
+put trec 0 uchar and t 255
+put trec 1 uchar and shr t 8 255
+put trec 2 uchar and n 255
+put trec 3 uchar and shr n 8 255
+st32 4 seq
+for i ulong n
+put trec add 8 i get pay i
+end
+segseal
+end
+fn mkheader to void
+for i ulong 16
+put thdr i 0
+end
+put thdr 0 65
+put thdr 1 73
+put thdr 2 84
+put thdr 3 82
+put thdr 4 1
+put thdr 6 64
+put thdr 8 16
+end
+fn hdrcheck to ulong
+if or ne get thdr 0 65 ne get thdr 1 73
+ret 11
+end
+if or ne get thdr 2 84 ne get thdr 3 82
+ret 11
+end
+if or ne get thdr 4 1 ne get thdr 5 0
+ret 15
+end
+if or ne get thdr 6 64 ne get thdr 7 0
+ret 16
+end
+if ne get thdr 8 16
+ret 16
+end
+for i ulong 7
+if ne get thdr add 9 i 0
+ret 16
+end
+end
+ret 0
+end
+note segcreate refuses an existing path (O_EXCL) instead of truncating it: data is never overwritten.
+fn segcreate to ulong
+mkheader
+fd = fopen tpath 0 get tlen 0 32961 384
+if lt fd 0
+put serrno 0 fd
+ret 10
+end
+n = write int fd thdr 0 16
+if ne n 16
+shut fd
+ret 13
+end
+if ne fsync int fd 0
+shut fd
+ret 13
+end
+if ne fclose int fd 0
+ret 12
+end
+ret 0
+end
+note segready creates a missing segment and otherwise requires a valid header; it never recreates one.
+fn segready to ulong
+fd = fopen tpath 0 get tlen 0 32768 0
+if eq fd -2
+ret segcreate
+end
+if lt fd 0
+put serrno 0 fd
+ret 10
+end
+n = read int fd thdr 0 16
+if ne fclose int fd 0
+ret 12
+end
+if lt n 0
+ret 17
+end
+if ne n 16
+ret 11
+end
+ret hdrcheck
+end
+fn segput fd long t ulong n ulong to ulong
+pos = fseek int fd 0 2
+if lt pos 16
+ret 27
+end
+r = sub ulong pos 16
+if ne rem r 64 0
+ret 25
+end
+q = div r 64
+if ge q 4096
+ret 26
+end
+segpack t n q
+put sseq 0 q
+m = write int fd trec 0 64
+if ne m 64
+ret 22
+end
+if ne fsync int fd 0
+ret 23
+end
+ret 0
+end
+note segappend appends one record of type t with payload pay[0..n); the sequence is the record index.
+fn segappend t ulong n ulong to ulong
+if gt n 52
+ret 20
+end
+e = segready
+if ne e 0
+ret e
+end
+fd = fopen tpath 0 get tlen 0 33793 0
+if lt fd 0
+put serrno 0 fd
+ret 21
+end
+e = segput fd t n
+if ne e 0
+shut fd
+ret e
+end
+if ne fclose int fd 0
+ret 24
+end
+ret 0
+end
+fn recok idx ulong to ulong
+if gt ld16 2 52
+ret 2
+end
+want = ld32 60
+if ne want segcrc 60
+ret 3
+end
+if ne ld32 4 idx
+ret 4
+end
+ret 0
+end
+note Each pass reads one record or ends; at most 4097 full records plus the final read: bound 4098.
+fn scanrecs fd long to ulong
+go = true
+while go bound 4098
+m = read int fd trec 0 64
+if lt m 0
+ret 17
+end
+if eq m 0
+go = false
+else
+if lt m 64
+put sreason 0 1
+go = false
+else
+if ge get scount 0 4096
+put sreason 0 5
+go = false
+else
+r = recok get scount 0
+if ne r 0
+put sreason 0 r
+go = false
+else
+put scount 0 add get scount 0 1
+end
+end
+end
+end
+end
+ret 0
+end
+note segscan is read-only. scount = complete valid records before the first damage, sreason = why the
+note scan stopped, stotal = file bytes, slost = bytes after the valid prefix (what recovery quarantines).
+fn segscan to ulong
+put scount 0 0
+put sreason 0 0
+put stotal 0 0
+put slost 0 0
+fd = fopen tpath 0 get tlen 0 32768 0
+if lt fd 0
+put serrno 0 fd
+ret 10
+end
+n = read int fd thdr 0 16
+if ne n 16
+shut fd
+ret 11
+end
+e = hdrcheck
+if ne e 0
+shut fd
+ret e
+end
+pos = fseek int fd 0 2
+p2 = fseek int fd 16 0
+if or lt pos 16 ne p2 16
+shut fd
+ret 27
+end
+put stotal 0 ulong pos
+e = scanrecs fd
+if ne fclose int fd 0
+ret 12
+end
+used = add 16 mul get scount 0 64
+put slost 0 sub get stotal 0 used
+ret e
+end
+fn copyrecs src long dst long to ulong
+mkheader
+w = write int dst thdr 0 16
+if ne w 16
+ret 31
+end
+p = fseek int src 16 0
+if ne p 16
+ret 31
+end
+for i ulong get scount 0
+m = read int src trec 0 64
+if ne m 64
+ret 31
+end
+w2 = write int dst trec 0 64
+if ne w2 64
+ret 31
+end
+end
+if ne fsync int dst 0
+ret 31
+end
+ret 0
+end
+note stage writes header + the valid prefix to segment.new (a stale .new from an interrupted recovery is
+note removed first; the segment itself is untouched until the final rename).
+fn stage to ulong
+sidepath 2
+u = unlink opath 0 get olen 0
+if and lt u 0 ne u -2
+put serrno 0 u
+ret 30
+end
+src = fopen tpath 0 get tlen 0 32768 0
+if lt src 0
+put serrno 0 src
+ret 30
+end
+dst = fopen opath 0 get olen 0 32961 384
+if lt dst 0
+put serrno 0 dst
+shut src
+ret 30
+end
+e = copyrecs src dst
+shut src
+if ne fclose int dst 0
+ret 31
+end
+ret e
+end
+note segrecover: a damaged record area is never repaired in place. The valid prefix is staged, the damaged
+note file is hard-linked to segment.bad (refused if that exists: a quarantine is never overwritten), then
+note the staged file is renamed over the segment. At every step the segment path names either the old
+note damaged file or the complete new one. Returns 0 clean, 1 recovered (slost bytes quarantined), or an
+note error code. A bad header is not recovered automatically (segscan's error is returned).
+fn segrecover to ulong
+e = segscan
+if ne e 0
+ret e
+end
+if eq get sreason 0 0
+ret 0
+end
+e = stage
+if ne e 0
+ret e
+end
+sidepath 1
+put tn 0 0
+tput16 2
+pkttop
+pktoth
+lk = link tpkt 0 get tn 0
+if lt lk 0
+put serrno 0 lk
+ret 32
+end
+sidepath 2
+put tn 0 0
+tput16 2
+pktoth
+pkttop
+rr = renames tpkt 0 get tn 0 0
+if lt rr 0
+put serrno 0 rr
+ret 33
+end
+ret 1
+end
diff --git a/seed/trace/tracecli.om b/seed/trace/tracecli.om
new file mode 100644
index 000000000..befd5e1e5
--- /dev/null
+++ b/seed/trace/tracecli.om
@@ -0,0 +1,981 @@
+note SPDX-License-Identifier: AGPL-3.0-only
+note seed/trace/tracecli.om: offline, read-only trace segment CLI (omega-mv #610), interface version 1.
+note Build: cat seed/trace/segment.om seed/trace/tsha.om seed/trace/tracecli.om | seed/out/omc > tracecli
+note Gate: sh seed/trace/cli-gate.sh. It scans the exact return line of fn ropen; the flags literal must stay 32768.
+note   tracecli version
+note   tracecli verify SEG                                   integrity of one segment
+note   tracecli show SEG                                     every valid record
+note   tracecli events SEG [--type T] [--range FROM TO]      valid records of type T and FROM <= seq < TO
+note   tracecli stats SEG [--type T] [--range FROM TO]       record count and payload bytes per type
+note   tracecli export SEG [--type T] [--range FROM TO]      CSV with the source SHA-256 and the query
+note   tracecli compare SEGA SEGB                            record-by-record comparison
+note   tracecli list DIR                                     verify every entry of a directory, by name
+note Nothing is written except stdout. Segments are opened O_RDONLY|O_NOFOLLOW (32768) and directories
+note O_RDONLY|O_DIRECTORY|O_NOFOLLOW (49152); nothing is created, so a symbolic link is refused (ELOOP).
+note A damaged segment is never silently shortened: the records before the damage are printed, the verify
+note line says "partial" with the reason and the bytes after the valid prefix, and the exit status is 3.
+note Every record printed is read again and re-checked (CRC, length, sequence) after the scan; a record
+note that no longer checks ends the listing with "changed-during-read at=N" and exit 3.
+note Exit status: 0 ok (compare: identical), 1 compare found a difference, 2 usage, 3 partial or
+note inconclusive, 4 refused (missing, symlink, bad header, unreadable file or directory), 5 stdout write failed.
+global obuf uchar 512
+global on ulong 1
+global wbad ulong 1
+global arec uchar 64
+global argb uchar 64
+global acnt ulong 1
+global moff ulong 96
+global mlen ulong 96
+global qtype ulong 1
+global qlo ulong 1
+global qhi ulong 1
+global wfail ulong 1
+global tcnt ulong 65536
+global tbytes ulong 65536
+global dpath uchar 1024
+global dlen ulong 1
+global dbuf uchar 4096
+global names uchar 16384
+global nlens ulong 64
+global ntype ulong 64
+global nnum ulong 1
+global ntrunc ulong 1
+global nord ulong 64
+note msgs: one message per line; message k is printed by omsg k. Lines 52..57 are the scan reasons
+note (sreason 0..5), lines 58..67 the command and option words.
+text msgs "usage: tracecli version | verify SEG | show SEG | events SEG [--type T] [--range FROM TO] | stats SEG [--type T] [--range FROM TO] | export SEG [--type T] [--range FROM TO] | compare SEGA SEGB | list DIR\ntracecli 1 segment-format 1\nsegment version=1 recsize=64 hdrsize=16\nverify \nok\npartial\n records=\n bytes=\n lost=\n reason=\nrefused code=\n errno=\nseq=\n type=\n len=\n crc=\n payload=\nquery type=\nany\n seq>=\n seq<\nmatched=\n of=\nchanged-during-read at=\ntype=\n payload-bytes=\nstats records=\n types=\nenergy=unavailable timing=unavailable (segment format 1 carries no typed measurements)\na: \nb: \ncompare basis=records (segment format 1 carries no machine, build or precision metadata: not a qualified cross-run comparison)\ncompare identical records=\ncompare differ at=\ncompare prefix a=\n b=\ncompare inconclusive\n# tracecli 1 export\n# source sha256=\n# \n# \nseq,type,len,crc,payload_hex\nlist \n: \ndirectory\nlist entries=\n ok=\n partial=\n refused=\n directories=\nlist truncated at 64 entries\nlist refused errno=\nclean\ntorn-tail\nbad-length\ncrc-mismatch\nseq-mismatch\nover-capacity\nversion\nverify\nshow\nevents\nstats\nexport\ncompare\nlist\n--type\n--range\n"
+text hexd "0123456789abcdef"
+const kusage ulong 0
+const kvers ulong 1
+const khdr ulong 2
+const kverify ulong 3
+const kok ulong 4
+const kpart ulong 5
+const krecs ulong 6
+const kbytes ulong 7
+const klost ulong 8
+const kreason ulong 9
+const krefused ulong 10
+const kerrno ulong 11
+const kseq ulong 12
+const ktype ulong 13
+const klen ulong 14
+const kcrc ulong 15
+const kpay ulong 16
+const kquery ulong 17
+const kany ulong 18
+const kseqge ulong 19
+const kseqlt ulong 20
+const kmatched ulong 21
+const kof ulong 22
+const kchanged ulong 23
+const ktypeeq ulong 24
+const kpbytes ulong 25
+const kstats ulong 26
+const ktypes ulong 27
+const kenergy ulong 28
+const ka ulong 29
+const kb ulong 30
+const kbasis ulong 31
+const kident ulong 32
+const kdiffer ulong 33
+const kprefix ulong 34
+const kbn ulong 35
+const kinconc ulong 36
+const kexport ulong 37
+const ksrc ulong 38
+const khash ulong 39
+const kcsvhdr ulong 41
+const klist ulong 42
+const kcolon ulong 43
+const kdir ulong 44
+const kentries ulong 45
+const knok ulong 46
+const knpart ulong 47
+const knref ulong 48
+const kndirs ulong 49
+const ktrunc ulong 50
+const klistref ulong 51
+const kreasons ulong 52
+const wversion ulong 58
+const wverify ulong 59
+const wshow ulong 60
+const wevents ulong 61
+const wstats ulong 62
+const wexport ulong 63
+const wcompare ulong 64
+const wlist ulong 65
+const wtype ulong 66
+const wrange ulong 67
+fn oflush to void
+w = write 1 obuf 0 get on 0
+if ne w long get on 0
+put wbad 0 1
+end
+put on 0 0
+end
+fn obyte b uchar to void
+if ge get on 0 512
+oflush
+end
+put obuf get on 0 b
+put on 0 add get on 0 1
+end
+fn onl to void
+obyte 10
+end
+fn onum v ulong to void
+d = new uchar 20 0
+x = v
+k = ulong 0
+go = true
+while go bound 21
+put d k uchar add 48 rem x 10
+k = add k 1
+x = div x 10
+if eq x 0
+go = false
+end
+end
+while gt k 0 bound 21
+k = sub k 1
+obyte get d k
+end
+end
+fn ohex b ulong to void
+obyte get hexd and shr b 4 15
+obyte get hexd and b 15
+end
+note minit fills moff/mlen with the start and length of every line of msgs (68 lines, 96 slots).
+fn minit to void
+s = ulong 0
+k = ulong 0
+for i ulong len msgs
+if eq get msgs i 10
+put moff k s
+put mlen k sub i s
+k = add k 1
+s = add i 1
+end
+end
+end
+fn omsg k ulong to void
+o = get moff k
+for i ulong get mlen k
+obyte get msgs add o i
+end
+end
+note omnum k v: message k then the decimal v.
+fn omnum k ulong v ulong to void
+omsg k
+onum v
+end
+note argword i: argument i into argb[0..acnt); acnt = 0 when it does not fit.
+fn argword i ulong to void
+put acnt 0 0
+n = argread i argb 0 64
+if ge n 0
+put acnt 0 ulong n
+end
+end
+note isword k: argb[0..acnt) equals message line k.
+fn isword k ulong to bool
+if ne get acnt 0 get mlen k
+ret false
+end
+o = get moff k
+for i ulong get mlen k
+if ne get argb i get msgs add o i
+ret false
+end
+end
+ret true
+end
+note argpath i: argument i becomes the segment path (tpath, tlen); 0 on success, 1 when it is empty
+note or longer than 1024 bytes.
+fn argpath i ulong to ulong
+n = argread i tpath 0 1024
+if le n 0
+ret 1
+end
+put tlen 0 ulong n
+ret 0
+end
+note argnum i: decimal argument i (1 to 5 digits, value at most 65535); 65536 when malformed.
+fn argnum i ulong to ulong
+argword i
+n = get acnt 0
+if or eq n 0 gt n 5
+ret 65536
+end
+v = ulong 0
+for k ulong n
+b = get argb k
+if or lt b 48 gt b 57
+ret 65536
+end
+v = add mul v 10 sub ulong b 48
+end
+if gt v 65535
+ret 65536
+end
+ret v
+end
+note parseq c: options from argument 3 to c-1 into qtype (65536 = any), qlo, qhi (seq range
+note qlo <= seq < qhi; default 0..4096, the whole segment). 0 ok, 1 usage error. A repeated option
+note replaces the earlier one.
+fn parseq c ulong to ulong
+put qtype 0 65536
+put qlo 0 0
+put qhi 0 4096
+i = ulong 3
+while lt i c bound 64
+argword i
+if isword wtype
+i = qopt i c 1
+else
+if isword wrange
+i = qopt i c 2
+else
+ret 1
+end
+end
+if eq i 0
+ret 1
+end
+end
+ret 0
+end
+note qopt i c k: the option at argument i takes k values (1 --type T, 2 --range FROM TO, FROM <= TO);
+note returns the index of the next argument, 0 when the values are missing or malformed.
+fn qopt i ulong c ulong k ulong to ulong
+if ge add i k c
+ret 0
+end
+a = argnum add i 1
+if gt a 65535
+ret 0
+end
+if eq k 1
+put qtype 0 a
+ret add i 2
+end
+b = argnum add i 2
+if or gt b 65535 gt a b
+ret 0
+end
+put qlo 0 a
+put qhi 0 b
+ret add i 3
+end
+fn oquery to void
+omsg kquery
+if eq get qtype 0 65536
+omsg kany
+else
+onum get qtype 0
+end
+omnum kseqge get qlo 0
+omnum kseqlt get qhi 0
+onl
+end
+note qmatch: the record in trec satisfies the query.
+fn qmatch to bool
+t = ld16 0
+s = ld32 4
+if and ne get qtype 0 65536 ne t get qtype 0
+ret false
+end
+ret and ge s get qlo 0 lt s get qhi 0
+end
+fn ousage to void
+put on 0 0
+omsg kusage
+onl
+end
+note orefused e: the refusal for scan result e; serrno (-errno) is reported for open failures (10).
+fn orefused e ulong to void
+omnum krefused e
+if eq e 10
+omnum kerrno ulong neg get serrno 0
+end
+onl
+end
+note overify: the verify line for the last segscan.
+fn overify to void
+omsg kverify
+if eq get sreason 0 0
+omsg kok
+else
+omsg kpart
+end
+omnum krecs get scount 0
+omnum kbytes get stotal 0
+omnum klost get slost 0
+omsg kreason
+omsg add kreasons get sreason 0
+onl
+end
+note vstatus: exit 3 when the scan stopped on damage (sreason not 0), else 0.
+fn vstatus to int
+ret partial get sreason 0
+end
+note partial r: exit 3 when r is not 0 (damage), else 0. The one place the partial exit status is chosen.
+fn partial r ulong to int
+if ne r 0
+ret 3
+end
+ret 0
+end
+fn orecord to void
+omnum kseq ld32 4
+omnum ktype ld16 0
+n = ld16 2
+omnum klen n
+omnum kcrc ld32 60
+omsg kpay
+for i ulong n
+ohex ulong get trec add 8 i
+end
+onl
+end
+fn ocsv to void
+onum ld32 4
+obyte 44
+onum ld16 0
+obyte 44
+n = ld16 2
+onum n
+obyte 44
+onum ld32 60
+obyte 44
+for i ulong n
+ohex ulong get trec add 8 i
+end
+onl
+end
+note ropen: the one open of a segment path by this unit: O_RDONLY|O_NOFOLLOW, nothing created. cli-gate.sh scans this exact return line.
+fn ropen to long
+ret fopen tpath 0 get tlen 0 32768 0
+end
+note rdrec fd i: read record i into trec and check it again (length, CRC, sequence = i).
+fn rdrec fd long i ulong to bool
+m = pread int fd trec 0 64 long add 16 mul i 64
+if ne m 64
+ret false
+end
+ret eq recok i 0
+end
+note tally: stats accumulation for the record in trec.
+fn tally to void
+t = ld16 0
+put tcnt t add get tcnt t 1
+put tbytes t add get tbytes t ld16 2
+end
+note walk m: the valid records (scount of them, after segscan) that match the query; m 0 prints
+note record lines, 1 CSV rows, 2 tallies them. Returns the match count. wfail = index + 1 of the record
+note whose re-read failed (0 none). At most 4096 valid records: bound 4097.
+fn walk m ulong to ulong
+put wfail 0 0
+fd = ropen
+if lt fd 0
+put wfail 0 1
+ret 0
+end
+hit = ulong 0
+n = get scount 0
+i = ulong 0
+while lt i n bound 4097
+if rdrec fd i
+if qmatch
+hit = add hit 1
+wone m
+end
+i = add i 1
+else
+put wfail 0 add i 1
+i = n
+end
+end
+shut fd
+ret hit
+end
+fn wone m ulong to void
+if eq m 0
+orecord
+end
+if eq m 1
+ocsv
+end
+if eq m 2
+tally
+end
+end
+fn ochanged to void
+if ne get wfail 0 0
+omnum kchanged sub get wfail 0 1
+onl
+end
+end
+note seg i: argument i becomes the segment path and is scanned; 0 scanned (clean or partial), 4
+note refused (the refusal is printed), 2 usage.
+fn seg i ulong to int
+if ne argpath i 0
+ret 2
+end
+e = segscan
+if ne e 0
+orefused e
+ret 4
+end
+ret 0
+end
+note endst: exit 3 when a re-read failed or the scan stopped on damage, else 0.
+fn endst to int
+if ne get wfail 0 0
+ret partial 1
+end
+ret vstatus
+end
+fn cmdverify to int
+e = seg 2
+if ne e 0
+ret e
+end
+overify
+ret vstatus
+end
+note cmdrecs m c: show/events (m 0) and export (m 1) after parseq; header, query, records, totals.
+fn cmdrecs m ulong to int
+e = seg 2
+if ne e 0
+ret e
+end
+if eq m 1
+ret cmdexport
+end
+omsg khdr
+onl
+oquery
+hit = walk 0
+omnum kmatched hit
+omnum kof get scount 0
+onl
+ochanged
+overify
+ret endst
+end
+fn cmdexport to int
+omsg kexport
+onl
+if ne hashfile 0
+omnum kchanged 0
+onl
+ret partial 1
+end
+omsg khash
+oquery
+omsg khash
+overify
+omsg kcsvhdr
+onl
+hit = walk 1
+ochanged
+ret endst
+end
+note hashfile: SHA-256 of the first stotal bytes of the segment (the bytes segscan saw), printed as
+note "# source sha256=<hex> bytes=<n>". 0 ok, 1 when the file could not be read back. stotal is at
+note most 16 + 64*4097 + 63 bytes before segscan stops (over-capacity), so at most 66 chunks of 4096:
+note bound 70.
+fn hashfile to ulong
+st = new ulong 9 0
+blk = new uchar 4096 0
+dig = new uchar 32 0
+shainit st
+fd = ropen
+if lt fd 0
+ret 1
+end
+tot = get stotal 0
+off = ulong 0
+bad = ulong 0
+go = true
+while go bound 70
+want = sub tot off
+if gt want 4096
+want = 4096
+end
+m = pread int fd blk 0 want long off
+if ne m long want
+bad = 1
+go = false
+else
+off = add off want
+if lt off tot
+shaupd st blk want
+else
+shafin st blk want dig
+go = false
+end
+end
+end
+shut fd
+if ne bad 0
+ret 1
+end
+omsg ksrc
+for i ulong 32
+ohex ulong get dig i
+end
+omnum kbytes tot
+onl
+ret 0
+end
+fn cmdstats to int
+e = seg 2
+if ne e 0
+ret e
+end
+omsg khdr
+onl
+oquery
+hit = walk 2
+nt = ulong 0
+pb = ulong 0
+for t ulong 65536
+if ne get tcnt t 0
+nt = add nt 1
+pb = add pb get tbytes t
+omnum ktypeeq t
+omnum krecs get tcnt t
+omnum kpbytes get tbytes t
+onl
+end
+end
+omnum kstats hit
+omnum ktypes nt
+omnum kpbytes pb
+onl
+omsg kenergy
+onl
+ochanged
+overify
+ret endst
+end
+note cmpside k: scan argument k (2 = a, 3 = b) and print its prefixed verify line; 0 when the scan
+note completed (clean or partial), else the refusal status.
+fn cmpside k ulong to int
+if eq k 2
+omsg ka
+else
+omsg kb
+end
+e = seg k
+if eq e 2
+ret 2
+end
+if ne e 0
+ret e
+end
+overify
+ret 0
+end
+note firstdiff fa fb n: index of the first of n records that differs between the two files, n if none,
+note n + 1 when a record could not be read back and re-checked.
+fn firstdiff fa long fb long n ulong to ulong
+at = n
+i = ulong 0
+while lt i n bound 4097
+if rdrec fa i
+for j ulong 64
+put arec j get trec j
+end
+if rdrec fb i
+if recdiff
+at = i
+i = n
+else
+i = add i 1
+end
+else
+at = add n 1
+i = n
+end
+else
+at = add n 1
+i = n
+end
+end
+ret at
+end
+note recdiff: the record in arec (side a) and trec (side b) differ in some byte.
+fn recdiff to bool
+for j ulong 64
+if ne get arec j get trec j
+ret true
+end
+end
+ret same
+end
+note same: recdiff's answer once all 64 bytes matched, so always false (no difference). It is a separate
+note function only so that "ret same" is a unique compare-mutant site for cli-gate.sh (mutant: ret true).
+fn same to bool
+ret false
+end
+note opn i: open argument i read-only (O_RDONLY|O_NOFOLLOW).
+fn opn i ulong to long
+e = argpath i
+if ne e 0
+ret -1
+end
+ret ropen
+end
+fn cmdcompare to int
+omsg kbasis
+onl
+e = cmpside 2
+pa = get sreason 0
+ca = get scount 0
+e2 = cmpside 3
+pb = get sreason 0
+cb = get scount 0
+if or eq e 2 eq e2 2
+ret 2
+end
+if or ne e 0 ne e2 0
+omsg kinconc
+onl
+ret 4
+end
+n = ca
+if lt cb n
+n = cb
+end
+fa = opn 2
+fb = opn 3
+at = add n 1
+if and ge fa 0 ge fb 0
+at = firstdiff fa fb n
+end
+if ge fa 0
+shut fa
+end
+if ge fb 0
+shut fb
+end
+ret cmpout pa ca pb cb at n
+end
+note cmpout: a differing record inside both valid prefixes is a definite difference (1); otherwise
+note damage on either side makes the result inconclusive (3); then unequal lengths differ (1).
+fn cmpout pa ulong ca ulong pb ulong cb ulong at ulong n ulong to int
+if gt at n
+omsg kinconc
+onl
+ret partial 1
+end
+if lt at n
+omnum kdiffer at
+onl
+ret 1
+end
+if or ne pa 0 ne pb 0
+omsg kinconc
+omnum krecs n
+onl
+ret partial 1
+end
+if ne ca cb
+omnum kprefix ca
+omnum kbn cb
+onl
+ret 1
+end
+omnum kident n
+onl
+ret 0
+end
+note dname e: the 2-byte little-endian reclen at dbuf[e+16]; getdents64 record layout d_ino u64,
+note d_off s64, d_reclen u16, d_type u8, d_name NUL-terminated.
+fn dreclen e ulong to ulong
+ret or ulong get dbuf add e 16 shl ulong get dbuf add e 17 8
+end
+note dkeep e r: store the entry at dbuf[e..) (not "." or "..") as name number nnum; ntrunc = 1 when 64
+note names are already stored or the name is longer than 255 bytes (the listing is then inconclusive).
+fn dkeep e ulong r ulong to void
+o = add e 19
+n = dnlen o r
+if dotname o n
+ret
+end
+if or ge get nnum 0 64 gt n 255
+put ntrunc 0 1
+ret
+end
+k = get nnum 0
+dstore k o n
+put ntype k ulong get dbuf add e 18
+put nord k k
+put nnum 0 add k 1
+end
+note dnlen o r: length of the NUL-terminated name at dbuf[o..r), at most 256 bytes are examined.
+fn dnlen o ulong r ulong to ulong
+n = ulong 0
+go = true
+while go bound 257
+if or ge add o n r gt n 255
+go = false
+else
+if eq get dbuf add o n 0
+go = false
+else
+n = add n 1
+end
+end
+end
+ret n
+end
+fn dstore k ulong o ulong n ulong to void
+b = mul k 256
+for i ulong n
+put names add b i get dbuf add o i
+end
+put nlens k n
+end
+fn dotname o ulong n ulong to bool
+if eq n 0
+ret true
+end
+if ne get dbuf o 46
+ret false
+end
+if eq n 1
+ret true
+end
+if eq n 2
+ret eq get dbuf add o 1 46
+end
+ret false
+end
+note dread fd: read every directory entry; 0 ok, else -errno. Every getdnts call that returns more than
+note 0 bytes consumes at least one entry, so the bound 100000 traps (exit 104) only for a directory of
+note more than 100000 entries.
+fn dread fd long to long
+go = true
+while go bound 100000
+r = getdnts int fd dbuf 0 4096
+if lt r 0
+ret r
+end
+if eq r 0
+go = false
+else
+dparse ulong r
+end
+end
+ret 0
+end
+fn dparse r ulong to void
+e = ulong 0
+while lt e r bound 4097
+rl = dreclen e
+if eq rl 0
+e = r
+else
+dkeep e r
+e = add e rl
+end
+end
+end
+note nless a b: name a sorts before name b (bytewise, a proper prefix first).
+fn nless a ulong b ulong to bool
+la = get nlens a
+lb = get nlens b
+m = la
+if lt lb m
+m = lb
+end
+for i ulong m
+x = get names add mul a 256 i
+y = get names add mul b 256 i
+if ne x y
+ret lt x y
+end
+end
+ret lt la lb
+end
+note nsort: insertion sort of nord by name, at most 64 entries.
+fn nsort to void
+n = get nnum 0
+for i ulong n
+j = i
+while gt j 0 bound 64
+p = get nord sub j 1
+q = get nord j
+if nless q p
+put nord j p
+put nord sub j 1 q
+j = sub j 1
+else
+j = 0
+end
+end
+end
+end
+note npath k: tpath = DIR "/" name k; 1 when it does not fit 1024 bytes.
+fn npath k ulong to ulong
+d = get dlen 0
+n = get nlens k
+if gt add add d 1 n 1024
+ret 1
+end
+for i ulong d
+put tpath i get dpath i
+end
+put tpath d 47
+for i ulong n
+put tpath add add d 1 i get names add mul k 256 i
+end
+put tlen 0 add add d 1 n
+ret 0
+end
+fn oname k ulong to void
+for i ulong get nlens k
+obyte get names add mul k 256 i
+end
+omsg kcolon
+end
+note lentry k: one line for name k; returns 0 ok, 1 partial, 2 refused, 3 directory. Spelled ret ulong 3 so the ret 3 in partial stays the unique mutant site.
+fn lentry k ulong to ulong
+oname k
+if eq get ntype k 4
+omsg kdir
+onl
+ret ulong 3
+end
+if ne npath k 0
+orefused 10
+ret 2
+end
+e = segscan
+if ne e 0
+orefused e
+ret 2
+end
+overify
+if ne get sreason 0 0
+ret 1
+end
+ret 0
+end
+fn cmdlist to int
+n = argread 2 dpath 0 1024
+if le n 0
+ret 2
+end
+put dlen 0 ulong n
+fd = fopen dpath 0 get dlen 0 49152 0
+if lt fd 0
+omnum klistref ulong neg fd
+onl
+ret 4
+end
+e = dread fd
+shut fd
+if ne e 0
+omnum klistref ulong neg e
+onl
+ret 4
+end
+nsort
+ret lall
+end
+fn lall to int
+c = new ulong 4 0
+for i ulong get nnum 0
+r = lentry get nord i
+put c r add get c r 1
+end
+omnum kentries get nnum 0
+omnum knok get c 0
+omnum knpart get c 1
+omnum knref get c 2
+omnum kndirs get c 3
+onl
+if ne get ntrunc 0 0
+omsg ktrunc
+onl
+ret partial 1
+end
+if ne add get c 1 get c 2 0
+ret partial 1
+end
+ret 0
+end
+note dispatch: the command word decides the argument count; anything else is usage (2). Nothing is
+note opened before the count and the options are checked.
+fn dispatch to int
+c = ulong argcnt
+if lt c 2
+ret 2
+end
+argword 1
+if isword wversion
+if ne c 2
+ret 2
+end
+omsg kvers
+onl
+ret 0
+end
+if lt c 3
+ret 2
+end
+if isword wcompare
+if ne c 4
+ret 2
+end
+ret cmdcompare
+end
+if ne parseq c 0
+ret 2
+end
+argword 1
+ret disp1 c
+end
+fn disp1 c ulong to int
+if isword wevents
+ret cmdrecs 0
+end
+if isword wexport
+ret cmdrecs 1
+end
+if isword wstats
+ret cmdstats
+end
+if ne c 3
+ret 2
+end
+if isword wverify
+ret cmdverify
+end
+if isword wshow
+ret cmdrecs 0
+end
+if isword wlist
+ret cmdlist
+end
+ret 2
+end
+note main: usage (2) discards any unflushed output and prints the usage line on stdout.
+fn main to int
+minit
+r = dispatch
+if eq r 2
+ousage
+end
+oflush
+if ne get wbad 0 0
+ret 5
+end
+ret r
+end
diff --git a/seed/trace/tracegen.om b/seed/trace/tracegen.om
new file mode 100644
index 000000000..82e0ada98
--- /dev/null
+++ b/seed/trace/tracegen.om
@@ -0,0 +1,142 @@
+note SPDX-License-Identifier: AGPL-3.0-only
+note seed/trace/tracegen.om: deterministic fixtures for the trace CLI gate (omega-mv #610). Run in an empty
+note scratch directory; it makes fx/ (it must not exist) and writes only through segment.om, plus the
+note fault injection below. Build: cat seed/trace/segment.om seed/trace/tsha.om seed/trace/tracegen.om | seed/out/omc.
+note Record i of the generator has type 1 + i mod 3, length (5 i) mod 53, payload byte j = (7 i + 3 j) mod 256.
+note   fx/a.seg      40 records, clean
+note   fx/b.seg      40 records, record 17 payload byte 0 xor 1 (compare a b: differ at=17)
+note   fx/c.seg      42 records, a plus records 40 and 41 (compare a c: prefix)
+note   fx/crc.seg    a with byte 16 + 64*5 + 8 = 344 (record 5 payload byte 0) xor 1: crc-mismatch at 5
+note   fx/torn.seg   a truncated to 16 + 64*39 + 30 = 2542 bytes: 39 records and a torn tail of 30 bytes
+note   fx/magic.seg  3 records, header byte 0 xor 1 ("@ITR"): refused, code 11
+note   fx/link.seg   symbolic link to a.seg: refused, ELOOP
+note   fx/sub/       empty directory (list reports it as a directory)
+note Regular fixtures are left mode 0400, so any open for writing by the CLI fails (EACCES).
+note Exit status 0, or the number of the step that failed.
+text paths "fx\nfx/a.seg\nfx/b.seg\nfx/c.seg\nfx/crc.seg\nfx/torn.seg\nfx/magic.seg\nfx/link.seg\nfx/sub\n"
+text ltgt "a.seg"
+fn setp k ulong to void
+line = ulong 1
+n = ulong 0
+for i ulong len paths
+b = get paths i
+if eq b 10
+line = add line 1
+else
+if eq line k
+put tpath n b
+n = add n 1
+end
+end
+end
+put tlen 0 n
+end
+note mkseg k n flip: segment k with records 0..n-1; when flip < n, payload byte 0 of record flip is xor 1.
+fn mkseg k ulong n ulong flip ulong to ulong
+setp k
+for i ulong n
+l = fill i flip
+if ne segappend add 1 rem i 3 l 0
+ret 1
+end
+end
+ret 0
+end
+note fill i flip: payload of record i into pay; returns its length.
+fn fill i ulong flip ulong to ulong
+l = rem mul i 5 53
+for j ulong l
+put pay j uchar rem add mul i 7 mul j 3 256
+end
+if eq i flip
+put pay 0 xor get pay 0 1
+end
+ret l
+end
+note xbyte k off: xor byte off of file k with 1, in place (O_RDWR|O_NOFOLLOW).
+fn xbyte k ulong off ulong to ulong
+setp k
+fd = fopen tpath 0 get tlen 0 32770 0
+if lt fd 0
+ret 1
+end
+m = pread int fd trec 0 1 long off
+put trec 0 xor get trec 0 1
+w = pwrite int fd trec 0 1 long off
+shut fd
+if or ne m 1 ne w 1
+ret 1
+end
+ret 0
+end
+fn mklink to ulong
+setp 8
+put tn 0 0
+tput16 2
+tput16 len ltgt
+for i ulong len ltgt
+put tpkt add get tn 0 i get ltgt i
+end
+put tn 0 add get tn 0 len ltgt
+pkttop
+s = symlnk tpkt 0 get tn 0
+if lt s 0
+ret 1
+end
+ret 0
+end
+fn ro k ulong to ulong
+setp k
+r = chmod tpath 0 get tlen 0 256
+if ne r 0
+ret 1
+end
+ret 0
+end
+fn main to int
+setp 1
+if ne makedir tpath 0 get tlen 0 448 0
+ret 1
+end
+if ne mkseg 2 40 40 0
+ret 2
+end
+if ne mkseg 3 40 17 0
+ret 3
+end
+if ne mkseg 4 42 42 0
+ret 4
+end
+if ne mkseg 5 40 40 0
+ret 5
+end
+if ne xbyte 5 344 0
+ret 6
+end
+if ne mkseg 6 40 40 0
+ret 7
+end
+setp 6
+if ne truncate tpath 0 get tlen 0 2542 0
+ret 8
+end
+if ne mkseg 7 3 3 0
+ret 9
+end
+if ne xbyte 7 0 0
+ret 10
+end
+if ne mklink 0
+ret 11
+end
+setp 9
+if ne makedir tpath 0 get tlen 0 448 0
+ret 12
+end
+for k ulong 6
+if ne ro add k 2 0
+ret 13
+end
+end
+ret 0
+end
diff --git a/seed/trace/tsha.om b/seed/trace/tsha.om
new file mode 100644
index 000000000..8fa33700c
--- /dev/null
+++ b/seed/trace/tsha.om
@@ -0,0 +1,220 @@
+note SPDX-License-Identifier: Apache-2.0
+note seed/trace/tsha.om: SHA-256 (FIPS 180-4) library unit for tracecli (omega-mv #610). Copied unchanged from
+note om/evidence/evidence.om lines 25-239 at b3dca0f1e (functions kinit .. padlen; parity-gated there against
+note the C sha256). Units concatenate and there is no import, so the copy is the reuse mechanism (FR-3).
+note shainit st / shaupd st p n (n a multiple of 64, at most 4096) / shafin st p n dig, st = 9 ulong cells.
+note ---- SHA-256 (FIPS 180-4), tools/zero-legacy/sha256.om with the state in parameters ----
+note st 0..7 the hash words, st 8 the bytes absorbed. 32-bit words live in ulong cells, masked
+note after every add (4294967295).
+
+fn kinit k edit ulong 64 to void
+put k 0 1116352408
+put k 1 1899447441
+put k 2 3049323471
+put k 3 3921009573
+put k 4 961987163
+put k 5 1508970993
+put k 6 2453635748
+put k 7 2870763221
+put k 8 3624381080
+put k 9 310598401
+put k 10 607225278
+put k 11 1426881987
+put k 12 1925078388
+put k 13 2162078206
+put k 14 2614888103
+put k 15 3248222580
+put k 16 3835390401
+put k 17 4022224774
+put k 18 264347078
+put k 19 604807628
+put k 20 770255983
+put k 21 1249150122
+put k 22 1555081692
+put k 23 1996064986
+put k 24 2554220882
+put k 25 2821834349
+put k 26 2952996808
+put k 27 3210313671
+put k 28 3336571891
+put k 29 3584528711
+put k 30 113926993
+put k 31 338241895
+put k 32 666307205
+put k 33 773529912
+put k 34 1294757372
+put k 35 1396182291
+put k 36 1695183700
+put k 37 1986661051
+put k 38 2177026350
+put k 39 2456956037
+put k 40 2730485921
+put k 41 2820302411
+put k 42 3259730800
+put k 43 3345764771
+put k 44 3516065817
+put k 45 3600352804
+put k 46 4094571909
+put k 47 275423344
+put k 48 430227734
+put k 49 506948616
+put k 50 659060556
+put k 51 883997877
+put k 52 958139571
+put k 53 1322822218
+put k 54 1537002063
+put k 55 1747873779
+put k 56 1955562222
+put k 57 2024104815
+put k 58 2227730452
+put k 59 2361852424
+put k 60 2428436474
+put k 61 2756734187
+put k 62 3204031479
+put k 63 3329325298
+end
+
+fn rotr x ulong r ulong to ulong
+ret and or shr x r shl x sub 32 r 4294967295
+end
+
+fn bsig0 x ulong to ulong
+ret xor rotr x 2 xor rotr x 13 rotr x 22
+end
+
+fn bsig1 x ulong to ulong
+ret xor rotr x 6 xor rotr x 11 rotr x 25
+end
+
+fn ssig0 x ulong to ulong
+ret xor rotr x 7 xor rotr x 18 shr x 3
+end
+
+fn ssig1 x ulong to ulong
+ret xor rotr x 17 xor rotr x 19 shr x 10
+end
+
+fn ch x ulong y ulong z ulong to ulong
+ret xor and x y and and not x 4294967295 z
+end
+
+fn maj x ulong y ulong z ulong to ulong
+ret xor and x y xor and x z and y z
+end
+
+note the 16 big-endian words of the block at byte o of p into w, then the schedule
+fn sched p view uchar 4096 o ulong w edit ulong 64 to void
+for i ulong 16
+q = add o mul i 4
+v = ulong get p q
+v = or shl v 8 ulong get p add q 1
+v = or shl v 8 ulong get p add q 2
+v = or shl v 8 ulong get p add q 3
+put w i v
+end
+for j ulong 48
+i = add j 16
+s = add ssig1 get w sub i 2 get w sub i 7
+s = add s ssig0 get w sub i 15
+s = add s get w sub i 16
+put w i and s 4294967295
+end
+end
+
+fn round s edit ulong 8 kw ulong to void
+a = get s 0
+e = get s 4
+t1 = add get s 7 bsig1 e
+t1 = add t1 ch e get s 5 get s 6
+t1 = and add t1 kw 4294967295
+t2 = and add bsig0 a maj a get s 1 get s 2 4294967295
+put s 7 get s 6
+put s 6 get s 5
+put s 5 e
+put s 4 and add get s 3 t1 4294967295
+put s 3 get s 2
+put s 2 get s 1
+put s 1 a
+put s 0 and add t1 t2 4294967295
+end
+
+note compress the block at byte o of p into st
+fn shacmp st edit ulong 9 p view uchar 4096 o ulong to void
+w = new ulong 64 0
+k = new ulong 64 0
+s = new ulong 8 0
+kinit k
+sched p o w
+for i ulong 8
+put s i get st i
+end
+for i ulong 64
+round s add get k i get w i
+end
+for i ulong 8
+put st i and add get st i get s i 4294967295
+end
+end
+
+fn shainit st edit ulong 9 to void
+put st 0 1779033703
+put st 1 3144134277
+put st 2 1013904242
+put st 3 2773480762
+put st 4 1359893119
+put st 5 2600822924
+put st 6 528734635
+put st 7 1541459225
+put st 8 0
+end
+
+note absorb p[0..n): n a multiple of 64, at most 4096 (trap 4 otherwise)
+fn shaupd st edit ulong 9 p view uchar 4096 n ulong to void
+if ne rem n 64 0
+trap 4
+end
+b = ulong 0
+while lt b n bound 65
+shacmp st p b
+b = add b 64
+end
+put st 8 add get st 8 n
+end
+
+note absorb the last n <= 4096 bytes of p, pad, and write the 32-byte digest into dig
+fn shafin st edit ulong 9 p view uchar 4096 n ulong dig edit uchar 32 to void
+full = sub n rem n 64
+shaupd st p full
+t = new uchar 4096 0
+r = sub n full
+for i ulong r
+put t i get p add full i
+end
+put t r 128
+m = ulong 64
+if ge r 56
+m = 128
+end
+padlen t m mul add get st 8 r 8
+shacmp st t 0
+if eq m 128
+shacmp st t 64
+end
+digout st dig
+end
+
+fn digout st edit ulong 9 dig edit uchar 32 to void
+for i ulong 32
+sh = mul sub 3 rem i 4 8
+w = get st div i 4
+put dig i uchar and shr w sh 255
+end
+end
+
+note the 64-bit big-endian bit count at the end of the m-byte padded tail
+fn padlen t edit uchar 4096 m ulong bits ulong to void
+for i ulong 8
+v = and shr bits mul sub 7 i 8 255
+put t add sub m 8 i uchar v
+end
+end
diff --git a/tests/.crumb b/tests/.crumb
index 5fbfcb29c..32fd73dfb 100644
--- a/tests/.crumb
+++ b/tests/.crumb
@@ -302,14 +302,14 @@
         }
       ],
       "children_root": "sha256:0c57282ff8cf1d34e5685e2729755f2978917e66debb5423961033be9d25c902",
-      "compiled_at": "2026-10-30T12:31:50Z",
+      "compiled_at": "2026-10-30T16:04:59Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874",
+      "digest": "sha256:030521c31acc733e6cad338aa4c7b24fc4ddd5bd6c070bfe82a5e267badca07e",
       "evidence_files": 16,
       "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a",
-      "files": 1386,
-      "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06",
-      "source_tree": "sha256:a3b853bb9c0edfc5f1413ccaa2698cc6ea920f2e8795756121667cc9d37f6679"
+      "files": 1388,
+      "generated_at_commit": "18057225b75e8dd1a1bf05d190b558df6275d71e",
+      "source_tree": "sha256:97de19343eb32d3185597c65f30258d8d3c11e2ac7f65c0f086b84e683b52366"
     },
     "owner_lane": "",
     "provenance": {
diff --git a/tests/gates/omega-trace.gate b/tests/gates/omega-trace.gate
new file mode 100644
index 000000000..95369d7a0
--- /dev/null
+++ b/tests/gates/omega-trace.gate
@@ -0,0 +1,23 @@
+# tests/gates/omega-trace.gate
+# omega#608: local trace segment format v1 in seeded Omega (seed/trace/segment.om): write, scan, torn-tail
+# recovery keeping the valid prefix, CRC-32 per record, and 4 source mutants, built by the committed
+# seed/out/omc. No hosted mirror: local gate only.
+gate: OMEGA-TRACE
+manifest_version: 2
+owner: omega
+requires:
+  - host
+inputs:
+  - seed/trace/
+  - seed/out/omc
+  - seed/out/omc.sha256
+  - tests/gates/
+run:
+  exec: tests/gates/run.sh
+  args: ["trace"]
+expects:
+  exit: 0
+  verdict: PASS
+timeout: 10m
+exactness: EXACT
+cache_scope: machine
diff --git a/tests/gates/omega-tracecli.gate b/tests/gates/omega-tracecli.gate
new file mode 100644
index 000000000..a28b9d1c8
--- /dev/null
+++ b/tests/gates/omega-tracecli.gate
@@ -0,0 +1,24 @@
+# tests/gates/omega-tracecli.gate
+# omega#610: offline read-only trace CLI (seed/trace/tracecli.om) over the local trace segment
+# format v1. Golden stdout and exit codes, a mode-0000 refusal, and 4 source mutants, built by
+# the committed seed/out/omc. No hosted mirror: local gate only. Depends on the segment format
+# from omega#608 (draft PR #958); this gate does not replace tests/gates/omega-trace.gate.
+gate: OMEGA-TRACECLI
+manifest_version: 2
+owner: omega
+requires:
+  - host
+inputs:
+  - seed/trace/
+  - seed/out/omc
+  - seed/out/omc.sha256
+  - tests/gates/
+run:
+  exec: tests/gates/run.sh
+  args: ["tracecli"]
+expects:
+  exit: 0
+  verdict: PASS
+timeout: 10m
+exactness: EXACT
+cache_scope: machine
diff --git a/tests/gates/run.sh b/tests/gates/run.sh
index 66da8e1a9..4b95d69bc 100755
--- a/tests/gates/run.sh
+++ b/tests/gates/run.sh
@@ -327,6 +327,22 @@ case "$step" in
     obs omc_sha256 "\"$(sha256sum < seed/out/omc | cut -c1-64)\""
     sh seed/tilelab/gate.sh
     ;;
+  trace)              # omega#608: local trace segment (seed/trace/segment.om), built by the committed seed/out/omc
+                      # after its recorded SHA-256 is checked: 71 checks against expected.txt (torn-tail recovery
+                      # keeps N-1 records, CRC/length/sequence damage, header refusals, O_EXCL, O_NOFOLLOW) and
+                      # 4 source mutants (CRC check, torn tail, alignment, O_EXCL) that must each fail their check.
+    sha256sum -c seed/out/omc.sha256
+    obs omc_sha256 "\"$(sha256sum < seed/out/omc | cut -c1-64)\""
+    sh seed/trace/gate.sh
+    ;;
+  tracecli)           # omega#610: offline read-only trace CLI (seed/trace/tracecli.om), built by the
+                      # committed seed/out/omc after its recorded SHA-256 is checked. Golden stdout and
+                      # exit codes, a mode-0000 refusal, and 4 source mutants (partial exit, compare
+                      # equality, type filter, read-only open flags) that must each fail their own check.
+    sha256sum -c seed/out/omc.sha256
+    obs omc_sha256 "\"$(sha256sum < seed/out/omc | cut -c1-64)\""
+    sh seed/trace/cli-gate.sh
+    ;;
   native)             # omega#663/#664: native .om units built by the committed seed/out/omc after its recorded
                       # SHA-256 is checked. The metadata test must pass and each recorded ECOSYSTEM663 mutant must
                       # exit 1; with native/autodiff664 present, its tape suite (test + 3 compiled mutants) and the