interplane.Get Omega
Read in your language

aien-protocols-mv

Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem

Brought by drakestapleton

GitHub aien-dev/aien-protocols-mv#36 (code only)

closed derived 928bb4be298b8ec0 #36

Imported from GitHub aien-dev/aien-protocols-mv#36 (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 176da2b18fa18d475116e54cc77f70e564e35efa · Head f0e0159822841508a577f0e0a74776e11df1dbff

github-advanced-security[bot] commented on specs/pi-spark-task/tools/pst_verify.c line 319 on 2026-10-18 17:34 UTC

CodeQL / Uncontrolled data used in path expression

This argument to a file access function is derived from user input (string read by fgets) and then passed to check_file(path), which calls slurp(path), which calls fopen(__filename).

Show more details

github-advanced-security[bot] commented on specs/pi-spark-task/tools/pst_verify.c line 346 on 2026-10-18 17:34 UTC

CodeQL / File created without restricting permissions

A file may be created here with mode 0666, which would make it world-writable.

Show more details

github-advanced-security[bot] commented on specs/pi-spark-task/tools/pst_verify.c line 377 on 2026-10-18 17:34 UTC

CodeQL / File created without restricting permissions

A file may be created here with mode 0666, which would make it world-writable.

Show more details

github-advanced-security[bot] reviewed (COMMENTED) on 2026-10-18 17:34 UTC

Apply it

git checkout -b patch-928bb4be298b8ec0 176da2b18fa18d475116e54cc77f70e564e35efa
curl -fsSL https://interplane.io/drakestapleton/aien-protocols-mv/patches/928bb4be298b8ec0.patch | git am

Raw patch (mbox) · 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/aien-protocols-mv
36
f0e0159822841508a577f0e0a74776e11df1dbff
{"kind":"github-import","owner":"aien-dev","repo":"aien-protocols-mv","number":36,"url":"https://github.com/aien-dev/aien-protocols-mv/pull/36","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:39:45Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/.crumb b/.crumb
index 102dc1d..c851afc 100644
--- a/.crumb
+++ b/.crumb
@@ -19,21 +19,21 @@
           "purpose": "Directory crates. Purpose not yet described by a human."
         },
         {
-          "digest": "6504fae30e11f8e79dca0151bb2be96c838c85559c15afd6515dd700d2afe94e",
-          "last_commit": "0622708",
+          "digest": "73532b950b93ca0ace42e0f199e6af467de919c40a31a8ba3489bba4f1e932b5",
+          "last_commit": "b3f8bd4",
           "name": "specs",
           "purpose": "Directory specs. Purpose not yet described by a human."
         }
       ],
-      "children_root": "sha256:1f1e38f78c005237a8281b8d85918d44792ecdd9875a7e63ec997a620d52c7ab",
-      "compiled_at": "2026-10-18T17:25:18Z",
+      "children_root": "sha256:c650904c99898bb27d1eb448c83ff2d50c81df7290ed2b43d908434079f1ded6",
+      "compiled_at": "2026-10-18T17:39:12Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:201b80ddc128d4fc8c118c1997e39506d03598c07ae7d9c0042509899ca56bb0",
+      "digest": "sha256:71ee540e6eed2670441ef803479e7bb8d1591226f6d199a4e1f99dcef4b0cab3",
       "evidence_files": 1,
       "evidence_root": "sha256:87c36bf8ed7be61c064552ebf141834707770b75d070ea49328a3e06d510afba",
-      "files": 475,
-      "generated_at_commit": "7eff296a70c6a3eb2c70eb9dedb319cc5a2998d8",
-      "source_tree": "sha256:498ecab3dce74be1de4ee0caa156a7584747d828dc706117736e2697db100d07"
+      "files": 503,
+      "generated_at_commit": "b3f8bd4948610368d87195fd39f935b759e0a479",
+      "source_tree": "sha256:c8c74cbe1334576d1f9b4d1cf4901769e87e79a95f0d6cb485389059292c101f"
     },
     "owner_lane": "",
     "seed": {
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d624381..8dd86ab 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,15 @@ VERSIONING.md.
 
 ### Specifications
 
+- `specs/pi-spark-task`: PST1 Draft v0.1 (C3, aien-protocols-mv#30): a 4-byte
+  `PST1` magic now opens the header (`reserved1` shrank to 1 byte and
+  `reserved2` was removed, so the header stays 224 bytes; fields after `type`
+  moved by 4), a fixed check order (section 4.1), and two codes
+  (`PST_BAD_MAGIC`, `PST_NO_OBJECTIVE`). Version 0 is refused as
+  `PST_DOWNGRADE`. Adds the C reference checker and encoder
+  `tools/pst_verify.c` (C99, libc only, no heap), 23 header vectors with
+  `vectors/expected.txt`, `tools/check-vectors.sh` and
+  `tools/mutate-verifier.sh` (15 checks, 15 mutants killed).
 - `specs/pi-spark-task`: PST1 Draft v0, the Pi-Spark task message for
   ALLEN Pi/Spark lane C1 (aien-protocols-mv#28): inventory of reused formats,
   layering over M6-A and M6-B, 224-byte big-endian header, 14 message types,
diff --git a/specs/.crumb b/specs/.crumb
index a5d8189..1bb6131 100644
--- a/specs/.crumb
+++ b/specs/.crumb
@@ -71,21 +71,21 @@
           "purpose": "OSH platform ABI draft (osh-platform)"
         },
         {
-          "digest": "9a6ce8d6661b08b2893b5201d9d2de3165b678f448fe8d5f39ce61485c4f6d28",
-          "last_commit": "0622708",
+          "digest": "60d38ed5ad52cb3a0456bea5d18c6d4f4e1e18c50029b1f076b63896137c4e8c",
+          "last_commit": "b3f8bd4",
           "name": "pi-spark-task",
           "purpose": "PST1 Pi-Spark task message format (draft)"
         }
       ],
-      "children_root": "sha256:875d2673c118a7814e5d0e3d0a27791031ef1f48164c60ea2b1218b027ec3b7a",
-      "compiled_at": "2026-10-16T19:28:14Z",
+      "children_root": "sha256:b539526c3e4330241ca84d293805f7190992119a93ca44da4b5c072b561b225c",
+      "compiled_at": "2026-10-18T17:39:12Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:6504fae30e11f8e79dca0151bb2be96c838c85559c15afd6515dd700d2afe94e",
+      "digest": "sha256:73532b950b93ca0ace42e0f199e6af467de919c40a31a8ba3489bba4f1e932b5",
       "evidence_files": 1,
       "evidence_root": "sha256:87c36bf8ed7be61c064552ebf141834707770b75d070ea49328a3e06d510afba",
-      "files": 417,
-      "generated_at_commit": "06227089aecd9d561e70c536108ebf1ce009b973",
-      "source_tree": "sha256:05fc11d76190d3112e2ab26fccef20164319d009e7a6f6868501c85538455957"
+      "files": 445,
+      "generated_at_commit": "b3f8bd4948610368d87195fd39f935b759e0a479",
+      "source_tree": "sha256:6256992925c8b1a72b8407c181dad0a88409818919d7e2bf36d0fb1b1416cfd1"
     },
     "owner_lane": "",
     "seed": {
diff --git a/specs/pi-spark-task/.crumb b/specs/pi-spark-task/.crumb
index e778059..6b5bee9 100644
--- a/specs/pi-spark-task/.crumb
+++ b/specs/pi-spark-task/.crumb
@@ -3,14 +3,14 @@
     "generated": {
       "children": [],
       "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "compiled_at": "2026-10-16T19:28:14Z",
+      "compiled_at": "2026-10-18T17:39:12Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:9a6ce8d6661b08b2893b5201d9d2de3165b678f448fe8d5f39ce61485c4f6d28",
+      "digest": "sha256:60d38ed5ad52cb3a0456bea5d18c6d4f4e1e18c50029b1f076b63896137c4e8c",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "files": 1,
-      "generated_at_commit": "06227089aecd9d561e70c536108ebf1ce009b973",
-      "source_tree": "sha256:98ca102fbe14d7df8b4b8ae5c74bb1e14ceeec7172c5e2b603ad3111d044b17a"
+      "files": 29,
+      "generated_at_commit": "b3f8bd4948610368d87195fd39f935b759e0a479",
+      "source_tree": "sha256:19977d2a0da5c41224599ba432cdd40c4fe088be5fd131142374ed2b23c924be"
     }
   },
   "name": "pi-spark-task",
diff --git a/specs/pi-spark-task/SPEC.md b/specs/pi-spark-task/SPEC.md
index d3cad86..af67bed 100644
--- a/specs/pi-spark-task/SPEC.md
+++ b/specs/pi-spark-task/SPEC.md
@@ -1,11 +1,11 @@
 # Pi-Spark Task Message: PST1
 
-**Status**: Draft v0 (ALLEN Pi/Spark lane C1, 2026-10-16). Not yet produced or read by any runtime.
+**Status**: Draft v0.1 (ALLEN Pi/Spark lane C1, 2026-10-16; C3 added the magic, the check order and two codes). Not yet produced or read by any runtime. Reference checker and vectors: `tools/pst_verify.c`, `vectors/`.
 **Contract version**: 0.1.0 (major zero: may change by minor bump, see VERSIONING.md)
 **Wire version**: 1 (the `version` byte below)
 **License**: Community Specification License 1.0
 **Tracking**: aien-dev/aien-protocols-mv#28 (C1), parent #27, lane C of aien-dev/aien-architecture-mv#231
-**Not in this document**: vectors and the C checker (C3), the Spark gate (C2), the Rust reader (C4), the order rules between messages (C5), the INTERPLANE mapping (C6), network framing and retry (aienos-mv#398).
+**Not in this document**: the Spark gate (C2), the Rust reader (C4), the order rules between messages (C5), the INTERPLANE mapping (C6), network framing and retry (aienos-mv#398).
 
 ## 1. Purpose
 
@@ -47,27 +47,48 @@ One PST1 message is a 224-byte fixed header followed by `payload_len` bytes of p
 
 | Offset | Size | Field | Allowed values |
 |---|---|---|---|
-| 0 | 1 | `version` | 1 |
-| 1 | 1 | `type` | 1 to 14 (section 5) |
-| 2 | 2 | `reserved0` | 0 |
-| 4 | 32 | `cause_id` | Not all zero |
-| 36 | 32 | `origin_run_id` | Any value (opaque, copied) |
-| 68 | 32 | `objective_id` | Not all zero for `request`; copied on every later message |
-| 100 | 32 | `subject_id` | The ALLEN subject reference (ADR 0018); no key material |
-| 132 | 16 | `parent_task_id` | All zero means "no parent" |
-| 148 | 16 | `task_id` | Not all zero |
-| 164 | 16 | `idempotence_key` | Not all zero; see section 8 |
-| 180 | 4 | `ordinal` | u32; see section 5 |
-| 184 | 1 | `effect_class` | 0 NONE, 1 COMPUTE, 2 EFFECT |
-| 185 | 3 | `reserved1` | 0 |
-| 188 | 2 | `payload_len` | 0 to 800 |
-| 190 | 2 | `reserved2` | 0 |
+| 0 | 4 | `magic` | The ASCII bytes `PST1` (0x50 0x53 0x54 0x31) |
+| 4 | 1 | `version` | 1 |
+| 5 | 1 | `type` | 1 to 14 (section 5) |
+| 6 | 2 | `reserved0` | 0 |
+| 8 | 32 | `cause_id` | Not all zero |
+| 40 | 32 | `origin_run_id` | Any value (opaque, copied) |
+| 72 | 32 | `objective_id` | Not all zero for `request`; copied on every later message |
+| 104 | 32 | `subject_id` | The ALLEN subject reference (ADR 0018); no key material |
+| 136 | 16 | `parent_task_id` | All zero means "no parent" |
+| 152 | 16 | `task_id` | Not all zero |
+| 168 | 16 | `idempotence_key` | Not all zero; see section 8 |
+| 184 | 4 | `ordinal` | u32; see section 5 |
+| 188 | 1 | `effect_class` | 0 NONE, 1 COMPUTE, 2 EFFECT |
+| 189 | 1 | `reserved1` | 0 |
+| 190 | 2 | `payload_len` | 0 to 800 |
 | 192 | 32 | `payload_digest` | SHA-256 of the payload bytes (of the empty string when `payload_len` is 0) |
 
 Total header: 224 bytes.
 
+Change from Draft v0 (C3, aien-protocols-mv#30): Draft v0 had no magic, so a receiver could not tell a PST1 message from any other 1024-byte blob, and the C3 vectors could not test a "bad magic" refusal. The 4-byte magic now opens the header. To keep the header at 224 bytes (and the 800-byte payload cap), `reserved1` shrank from 3 bytes to 1 and `reserved2` was removed. Every field after `type` moved by 4 bytes, except `payload_digest`, which stays at 192.
+
 `effect_class` is a claim about the work, not a permission. EFFECT work still needs a real grant from the Spark approval desk (lane G, G6).
 
+### 4.1 Check order
+
+A receiver checks one message in this order and reports the first rule that fails. The C reference checker (`tools/pst_verify.c`) and every other reader follow the same order, so the same bytes always give the same code.
+
+1. Fewer than 224 bytes, or more than 1024 bytes: `PST_BAD_LENGTH`.
+2. `magic` is not `PST1`: `PST_BAD_MAGIC`.
+3. `version` is 0 (below the lowest version this receiver accepts): `PST_DOWNGRADE`. `version` above 1: `PST_BAD_VERSION`.
+4. `type` is 0 or above 14: `PST_BAD_TYPE`.
+5. `reserved0` or `reserved1` is non-zero: `PST_RESERVED_NONZERO`.
+6. The bytes after the header are fewer than `payload_len`: `PST_BAD_LENGTH`. (A `payload_len` above 800 always lands here or in step 1, since 224 plus 801 is more than 1024.)
+7. The bytes after the header are more than `payload_len`: `PST_TRAILING_BYTES`.
+8. `effect_class` above 2: `PST_BAD_EFFECT_CLASS`.
+9. `cause_id` all zero: `PST_NO_CAUSE`.
+10. `task_id` all zero, then `idempotence_key` all zero: `PST_NO_TASK`.
+11. `type` is `request` and `objective_id` is all zero: `PST_NO_OBJECTIVE`.
+12. `payload_digest` differs from the SHA-256 of the payload: `PST_DIGEST_MISMATCH`.
+
+The checks that need more than one message (`PST_WRONG_SIDE`, `PST_SUBJECT_MISMATCH`, a version lower than the one already used in the session) come after these and belong to the session rules (C5).
+
 ## 5. Message types
 
 `request`, `cancel request` and `resume request` are sent by the Pi. Every other type is sent by the Spark. A message of a type from the wrong side is refused.
@@ -101,19 +122,21 @@ Status is derived from authoritative execution state on the Spark. A `completed`
 
 ## 7. Refusal codes
 
-A receiver refuses the message and takes no action. It reports one of these. A refusal never changes task state.
+A receiver refuses the message and takes no action. It reports one of these, choosing by the check order in section 4.1. A refusal never changes task state.
 
 | Code | Meaning |
 |---|---|
-| `PST_BAD_VERSION` | `version` is not 1 |
-| `PST_DOWNGRADE` | `version` is lower than the version already used in this session |
+| `PST_BAD_MAGIC` | The first 4 bytes are not `PST1` |
+| `PST_BAD_VERSION` | `version` is above 1 (unknown version) |
+| `PST_DOWNGRADE` | `version` is 0 (below the lowest accepted version), or lower than the version already used in this session |
 | `PST_BAD_TYPE` | `type` is 0 or above 14 |
 | `PST_WRONG_SIDE` | A Pi-only type from the Spark, or the reverse |
-| `PST_BAD_LENGTH` | Fewer than 224 bytes, `payload_len` above 800, or the total differs from 224 plus `payload_len` |
-| `PST_TRAILING_BYTES` | Bytes after the declared payload |
+| `PST_BAD_LENGTH` | Fewer than 224 bytes, more than 1024 bytes, `payload_len` above 800, or fewer payload bytes than `payload_len` |
+| `PST_TRAILING_BYTES` | More payload bytes than `payload_len` (and the total is at most 1024) |
 | `PST_RESERVED_NONZERO` | Any reserved byte is non-zero |
 | `PST_NO_CAUSE` | `cause_id` is all zero |
 | `PST_NO_TASK` | `task_id` or `idempotence_key` is all zero |
+| `PST_NO_OBJECTIVE` | A `request` whose `objective_id` is all zero (added in C3: Draft v0 stated the rule but named no code) |
 | `PST_DIGEST_MISMATCH` | `payload_digest` does not match the payload |
 | `PST_BAD_EFFECT_CLASS` | `effect_class` above 2 |
 | `PST_SUBJECT_MISMATCH` | `subject_id` differs from the subject bound to this session |
diff --git a/specs/pi-spark-task/tools/check-vectors.sh b/specs/pi-spark-task/tools/check-vectors.sh
new file mode 100755
index 0000000..ae6fe92
--- /dev/null
+++ b/specs/pi-spark-task/tools/check-vectors.sh
@@ -0,0 +1,47 @@
+#!/bin/bash
+# check-vectors.sh: build the PST1 reference checker and check the vectors.
+#
+# Usage: check-vectors.sh [CC]        (default CC: cc)
+#
+# 1. Builds tools/pst_verify.c with warnings as errors (C99, libc only).
+# 2. Regenerates the vectors into a scratch directory (pst_verify
+#    --make-vectors) and requires them byte-identical to vectors/, so a stale
+#    or hand-edited vector fails here.
+# 3. Runs every vectors/expected.txt line (and re-encodes every accepted
+#    message, which must give the same bytes), plus an ASan/UBSan build when
+#    the compiler supports it.
+# 4. Runs tools/mutate-verifier.sh: every check must be caught by a vector.
+# Host-only check: no hardware. Shell and C only. Found by the PROTO-C-VECTORS
+# gate (tests/gates/run.sh c-vectors) because its name matches check-*.sh.
+set -eu
+export LC_ALL=C
+here=$(cd "$(dirname "$0")" && pwd)
+vec="$here/../vectors"
+CC=${1:-cc}
+tmp=$(mktemp -d)
+trap 'rm -rf "$tmp"' EXIT
+
+"$CC" -std=c99 -O2 -Wall -Wextra -Werror -pedantic -o "$tmp/pst_verify" "$here/pst_verify.c"
+
+mkdir -p "$tmp/regen"
+"$tmp/pst_verify" --make-vectors "$tmp/regen"
+ignore="-x .crumb -x .crumb.local -x .gitattributes"
+if ! diff -r $ignore "$vec" "$tmp/regen" >/dev/null; then
+    echo "PST1_VECTORS_STALE: vectors/ differs from pst_verify --make-vectors output" >&2
+    diff -rq $ignore "$vec" "$tmp/regen" >&2 || true
+    exit 1
+fi
+echo "PST1_VECTORS_REGEN identical"
+
+"$tmp/pst_verify" --corpus "$vec"
+
+if "$CC" -std=c99 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all \
+        -o "$tmp/pst_verify_san" "$here/pst_verify.c" 2>/dev/null; then
+    "$tmp/pst_verify_san" --corpus "$vec" | sed 's/^PST1_CONFORMANCE impl=c/PST1_CONFORMANCE impl=c-asan-ubsan/'
+    test "${PIPESTATUS[0]}" -eq 0
+else
+    echo "PST1_SANITIZER NOT_RUN (compiler lacks -fsanitize=address,undefined)"
+fi
+
+bash "$here/mutate-verifier.sh" "$CC"
+echo "PST1_VECTORS_PASS"
diff --git a/specs/pi-spark-task/tools/mutate-verifier.sh b/specs/pi-spark-task/tools/mutate-verifier.sh
new file mode 100755
index 0000000..b6a15de
--- /dev/null
+++ b/specs/pi-spark-task/tools/mutate-verifier.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+# mutate-verifier.sh: show that every check in pst_verify.c is exercised by
+# the vectors. Builds one checker per check with that check disabled
+# (-DPST_MUTANT=K) and requires the vectors to report at least one miss.
+# A mutant the vectors do not notice is a SURVIVOR and fails this script.
+#
+# Usage: mutate-verifier.sh [CC]
+set -eu
+export LC_ALL=C
+here=$(cd "$(dirname "$0")" && pwd)
+vec="$here/../vectors"
+CC=${1:-cc}
+tmp=$(mktemp -d)
+trap 'rm -rf "$tmp"' EXIT
+names=$(sed -n '/CHK_MAX_TOTAL = 1/,/CHK_LAST/p' "$here/pst_verify.c" | tr -d ' \n' | sed 's/CHK_MAX_TOTAL=1/CHK_MAX_TOTAL/; s/,CHK_LAST.*//' | tr ',' ' ')
+test -n "$names" || { echo "cannot read the check list" >&2; exit 2; }
+k=0 killed=0 survived=0
+for name in $names; do
+    k=$((k + 1))
+    "$CC" -std=c99 -O1 -w -DPST_MUTANT=$k -o "$tmp/m$k" "$here/pst_verify.c"
+    if "$tmp/m$k" --corpus "$vec" >"$tmp/out$k" 2>&1; then
+        echo "SURVIVOR $k $name"
+        survived=$((survived + 1))
+    else
+        echo "killed $k $name: $(grep -c 'MISMATCH' "$tmp/out$k") vector(s) differ"
+        killed=$((killed + 1))
+    fi
+done
+echo "PST1_VERIFIER_MUTANTS total=$k killed=$killed survived=$survived"
+test "$survived" -eq 0 && test "$k" -gt 0
diff --git a/specs/pi-spark-task/tools/pst_verify.c b/specs/pi-spark-task/tools/pst_verify.c
new file mode 100644
index 0000000..aefcbb3
--- /dev/null
+++ b/specs/pi-spark-task/tools/pst_verify.c
@@ -0,0 +1,471 @@
+/* pst_verify.c: reference checker for PST1, the Pi-Spark task message.
+ * Spec: ../SPEC.md (Draft v0.1, contract 0.1.0, wire version 1).
+ *
+ * Self-contained C99: its own SHA-256 (FIPS 180-4), libc only, no heap
+ * anywhere (static buffers), no clock. The decoder pst_decode() is a pure
+ * function of one read-only buffer; the encoder pst_encode() is a pure
+ * function of one message struct. It exists to pin the format with vectors,
+ * not to be linked into a runtime.
+ *
+ * Usage:
+ *   pst_verify FILE                print one verdict line
+ *   pst_verify --corpus DIR        check every DIR/expected.txt line, re-encode
+ *                                  every accepted message and require the same
+ *                                  bytes, print PST1_CONFORMANCE impl=c pass=N fail=M
+ *   pst_verify --make-vectors DIR  write the vector files and DIR/expected.txt
+ *
+ * Verdict line (the format expected.txt pins):
+ *   ok type=T version=V cause=H64 origin=H64 objective=H64 subject=H64
+ *      parent=H32 task=H32 idem=H32 ordinal=N effect=E len=L digest=H64
+ *   refuse CODE                    (CODE is a name from SPEC.md section 7)
+ * (one line; Hn is n lower-case hex digits).
+ *
+ * Exit status: 0 on accept / all corpus lines agree, 1 otherwise, 2 on usage
+ * or I/O error.
+ *
+ * Mutation hooks: building with -DPST_MUTANT=K disables check K (the CHK_*
+ * numbers below). tools/mutate-verifier.sh builds every mutant and requires
+ * the vectors to fail each one, which shows every check is exercised.
+ */
+#define _POSIX_C_SOURCE 200809L /* open, fdopen, close under -std=c99 */
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#ifndef PST_MUTANT
+#define PST_MUTANT 0
+#endif
+#define CHECK(k) (PST_MUTANT != (k))
+
+enum {
+    CHK_MAX_TOTAL = 1, CHK_MAGIC, CHK_DOWNGRADE, CHK_VERSION, CHK_TYPE,
+    CHK_RESERVED0, CHK_RESERVED1, CHK_PAYLOAD_SHORT, CHK_TRAILING, CHK_EFFECT,
+    CHK_CAUSE, CHK_TASK, CHK_IDEM, CHK_OBJECTIVE, CHK_DIGEST, CHK_LAST
+};
+
+/* ---- refusal codes (spec section 7) ------------------------------------- */
+enum {
+    PST_OK = 0, PST_BAD_MAGIC, PST_BAD_VERSION, PST_DOWNGRADE, PST_BAD_TYPE,
+    PST_WRONG_SIDE, PST_BAD_LENGTH, PST_TRAILING_BYTES, PST_RESERVED_NONZERO,
+    PST_NO_CAUSE, PST_NO_TASK, PST_NO_OBJECTIVE, PST_DIGEST_MISMATCH,
+    PST_BAD_EFFECT_CLASS, PST_SUBJECT_MISMATCH, PST_CODE_COUNT
+};
+static const char *const code_names[PST_CODE_COUNT] = {
+    "OK", "PST_BAD_MAGIC", "PST_BAD_VERSION", "PST_DOWNGRADE", "PST_BAD_TYPE",
+    "PST_WRONG_SIDE", "PST_BAD_LENGTH", "PST_TRAILING_BYTES", "PST_RESERVED_NONZERO",
+    "PST_NO_CAUSE", "PST_NO_TASK", "PST_NO_OBJECTIVE", "PST_DIGEST_MISMATCH",
+    "PST_BAD_EFFECT_CLASS", "PST_SUBJECT_MISMATCH"
+};
+
+/* ---- SHA-256 ------------------------------------------------------------ */
+typedef struct { uint32_t h[8]; uint64_t n; uint8_t b[64]; uint32_t used; } sha256;
+
+static const uint32_t K256[64] = {
+    0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
+    0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
+    0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
+    0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
+    0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
+    0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
+    0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
+    0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
+};
+#define ROR(x, r) (((x) >> (r)) | ((x) << (32 - (r))))
+
+static void sha_block(sha256 *s, const uint8_t *p) {
+    uint32_t w[64], a, b, c, d, e, f, g, h;
+    for (int i = 0; i < 16; i++)
+        w[i] = (uint32_t)p[4*i] << 24 | (uint32_t)p[4*i+1] << 16 | (uint32_t)p[4*i+2] << 8 | p[4*i+3];
+    for (int i = 16; i < 64; i++) {
+        uint32_t s0 = ROR(w[i-15], 7) ^ ROR(w[i-15], 18) ^ (w[i-15] >> 3);
+        uint32_t s1 = ROR(w[i-2], 17) ^ ROR(w[i-2], 19) ^ (w[i-2] >> 10);
+        w[i] = w[i-16] + s0 + w[i-7] + s1;
+    }
+    a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3];
+    e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7];
+    for (int i = 0; i < 64; i++) {
+        uint32_t t1 = h + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K256[i] + w[i];
+        uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c));
+        h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2;
+    }
+    s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d;
+    s->h[4] += e; s->h[5] += f; s->h[6] += g; s->h[7] += h;
+}
+static void sha_init(sha256 *s) {
+    static const uint32_t iv[8] = { 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,
+                                    0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 };
+    memcpy(s->h, iv, sizeof iv); s->n = 0; s->used = 0;
+}
+static void sha_update(sha256 *s, const void *data, size_t len) {
+    const uint8_t *p = data;
+    s->n += len;
+    while (len) {
+        uint32_t k = 64 - s->used;
+        if (k > len) k = (uint32_t)len;
+        memcpy(s->b + s->used, p, k);
+        s->used += k; p += k; len -= k;
+        if (s->used == 64) { sha_block(s, s->b); s->used = 0; }
+    }
+}
+static void sha_final(sha256 *s, uint8_t out[32]) {
+    uint64_t bits = s->n * 8;
+    uint8_t pad = 0x80, z = 0, l[8];
+    sha_update(s, &pad, 1);
+    while (s->used != 56) sha_update(s, &z, 1);
+    for (int i = 0; i < 8; i++) l[i] = (uint8_t)(bits >> (56 - 8 * i));
+    sha_update(s, l, 8);
+    for (int i = 0; i < 8; i++) {
+        out[4*i] = (uint8_t)(s->h[i] >> 24); out[4*i+1] = (uint8_t)(s->h[i] >> 16);
+        out[4*i+2] = (uint8_t)(s->h[i] >> 8); out[4*i+3] = (uint8_t)s->h[i];
+    }
+}
+static void sha_once(const void *p, size_t n, uint8_t out[32]) {
+    sha256 s; sha_init(&s); sha_update(&s, p, n); sha_final(&s, out);
+}
+
+/* ---- big-endian helpers -------------------------------------------------- */
+static uint16_t g16(const uint8_t *p) { return (uint16_t)(p[0] << 8 | p[1]); }
+static uint32_t g32(const uint8_t *p) { return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; }
+static void p16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)v; }
+static void p32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)(v >> 24); p[1] = (uint8_t)(v >> 16); p[2] = (uint8_t)(v >> 8); p[3] = (uint8_t)v; }
+static int all_zero(const uint8_t *p, size_t n) { while (n--) if (*p++) return 0; return 1; }
+
+/* ---- the format (spec section 4) ---------------------------------------- */
+#define PST_HDR       224u
+#define PST_MAX_PAY   800u
+#define PST_MAX_MSG   1024u
+#define PST_VERSION   1u
+#define PST_MAX_TYPE  14u
+#define T_REQUEST     1u
+
+enum {
+    O_MAGIC = 0, O_VERSION = 4, O_TYPE = 5, O_RES0 = 6, O_CAUSE = 8, O_ORIGIN = 40,
+    O_OBJECTIVE = 72, O_SUBJECT = 104, O_PARENT = 136, O_TASK = 152, O_IDEM = 168,
+    O_ORDINAL = 184, O_EFFECT = 188, O_RES1 = 189, O_PLEN = 190, O_DIGEST = 192
+};
+
+typedef struct {
+    uint8_t version, type;
+    uint8_t cause[32], origin[32], objective[32], subject[32];
+    uint8_t parent[16], task[16], idem[16];
+    uint32_t ordinal;
+    uint8_t effect;
+    uint16_t payload_len;
+    uint8_t digest[32];
+    const uint8_t *payload;   /* points into the decoded buffer */
+} pst_msg;
+
+/* Decode and check one message (spec section 4.1 check order). Reads only
+ * b[0..n); writes only *m. Returns PST_OK or the first refusal code. */
+static int pst_decode(const uint8_t *b, size_t n, pst_msg *m) {
+    size_t body;
+    uint8_t d[32];
+
+    memset(m, 0, sizeof *m);
+    /* 1. length bounds (the short bound is not a mutation point: it guards every read) */
+    if (n < PST_HDR) return PST_BAD_LENGTH;
+    if (CHECK(CHK_MAX_TOTAL) && n > PST_MAX_MSG) return PST_BAD_LENGTH;
+    /* 2. magic */
+    if (CHECK(CHK_MAGIC) && memcmp(b + O_MAGIC, "PST1", 4) != 0) return PST_BAD_MAGIC;
+    /* 3. version */
+    if (CHECK(CHK_DOWNGRADE) && b[O_VERSION] < PST_VERSION) return PST_DOWNGRADE;
+    if (CHECK(CHK_VERSION) && b[O_VERSION] > PST_VERSION) return PST_BAD_VERSION;
+    /* 4. type */
+    if (CHECK(CHK_TYPE) && (b[O_TYPE] == 0 || b[O_TYPE] > PST_MAX_TYPE)) return PST_BAD_TYPE;
+    /* 5. reserved */
+    if (CHECK(CHK_RESERVED0) && !all_zero(b + O_RES0, 2)) return PST_RESERVED_NONZERO;
+    if (CHECK(CHK_RESERVED1) && b[O_RES1] != 0) return PST_RESERVED_NONZERO;
+    /* 6, 7. payload length against the bytes present */
+    m->payload_len = g16(b + O_PLEN);
+    body = n - PST_HDR;
+    if (CHECK(CHK_PAYLOAD_SHORT) && body < m->payload_len) return PST_BAD_LENGTH;
+    if (CHECK(CHK_TRAILING) && body > m->payload_len) return PST_TRAILING_BYTES;
+    /* 8..11. field rules */
+    if (CHECK(CHK_EFFECT) && b[O_EFFECT] > 2) return PST_BAD_EFFECT_CLASS;
+    if (CHECK(CHK_CAUSE) && all_zero(b + O_CAUSE, 32)) return PST_NO_CAUSE;
+    if (CHECK(CHK_TASK) && all_zero(b + O_TASK, 16)) return PST_NO_TASK;
+    if (CHECK(CHK_IDEM) && all_zero(b + O_IDEM, 16)) return PST_NO_TASK;
+    if (CHECK(CHK_OBJECTIVE) && b[O_TYPE] == T_REQUEST && all_zero(b + O_OBJECTIVE, 32)) return PST_NO_OBJECTIVE;
+    /* 12. payload digest (over the bytes present, so a mutant never reads past n) */
+    sha_once(b + PST_HDR, body < m->payload_len ? body : m->payload_len, d);
+    if (CHECK(CHK_DIGEST) && memcmp(d, b + O_DIGEST, 32) != 0) return PST_DIGEST_MISMATCH;
+
+    m->version = b[O_VERSION];
+    m->type = b[O_TYPE];
+    memcpy(m->cause, b + O_CAUSE, 32);
+    memcpy(m->origin, b + O_ORIGIN, 32);
+    memcpy(m->objective, b + O_OBJECTIVE, 32);
+    memcpy(m->subject, b + O_SUBJECT, 32);
+    memcpy(m->parent, b + O_PARENT, 16);
+    memcpy(m->task, b + O_TASK, 16);
+    memcpy(m->idem, b + O_IDEM, 16);
+    m->ordinal = g32(b + O_ORDINAL);
+    m->effect = b[O_EFFECT];
+    memcpy(m->digest, b + O_DIGEST, 32);
+    m->payload = b + PST_HDR;
+    return PST_OK;
+}
+
+/* Encode one message. Writes the fields exactly as given (the digest is not
+ * recomputed, so a bad digest can be encoded on purpose). Returns the byte
+ * count, or 0 if it does not fit in cap or the payload is over the cap. */
+static size_t pst_encode(const pst_msg *m, uint8_t *out, size_t cap) {
+    size_t n = PST_HDR + (size_t)m->payload_len;
+    if (m->payload_len > PST_MAX_PAY || n > cap) return 0;
+    memset(out, 0, PST_HDR);
+    memcpy(out + O_MAGIC, "PST1", 4);
+    out[O_VERSION] = m->version;
+    out[O_TYPE] = m->type;
+    memcpy(out + O_CAUSE, m->cause, 32);
+    memcpy(out + O_ORIGIN, m->origin, 32);
+    memcpy(out + O_OBJECTIVE, m->objective, 32);
+    memcpy(out + O_SUBJECT, m->subject, 32);
+    memcpy(out + O_PARENT, m->parent, 16);
+    memcpy(out + O_TASK, m->task, 16);
+    memcpy(out + O_IDEM, m->idem, 16);
+    p32(out + O_ORDINAL, m->ordinal);
+    out[O_EFFECT] = m->effect;
+    p16(out + O_PLEN, m->payload_len);
+    memcpy(out + O_DIGEST, m->digest, 32);
+    if (m->payload_len) memcpy(out + PST_HDR, m->payload, m->payload_len);
+    return n;
+}
+
+/* ---- verdict line ---------------------------------------------------------- */
+static char *hexw(char *o, const uint8_t *p, size_t n) {
+    static const char hx[] = "0123456789abcdef";
+    for (size_t i = 0; i < n; i++) { *o++ = hx[p[i] >> 4]; *o++ = hx[p[i] & 15]; }
+    *o = 0;
+    return o;
+}
+static void verdict_line(int code, const pst_msg *m, char *buf, size_t cap) {
+    char c[65], o[65], ob[65], s[65], pa[33], t[33], k[33], d[65];
+    if (code != PST_OK) {
+        snprintf(buf, cap, "refuse %s", code > 0 && code < PST_CODE_COUNT ? code_names[code] : "PST_UNKNOWN_CODE");
+        return;
+    }
+    hexw(c, m->cause, 32); hexw(o, m->origin, 32); hexw(ob, m->objective, 32); hexw(s, m->subject, 32);
+    hexw(pa, m->parent, 16); hexw(t, m->task, 16); hexw(k, m->idem, 16); hexw(d, m->digest, 32);
+    snprintf(buf, cap, "ok type=%u version=%u cause=%s origin=%s objective=%s subject=%s parent=%s task=%s idem=%s ordinal=%lu effect=%u len=%u digest=%s",
+             m->type, m->version, c, o, ob, s, pa, t, k, (unsigned long)m->ordinal, m->effect, m->payload_len, d);
+}
+
+/* ---- file I/O (outside the decoder) --------------------------------------- */
+#define FILE_CAP 65536u
+static uint8_t file_buf[FILE_CAP];
+
+/* Reads the whole file into file_buf. Returns 0, or -1 on I/O error or a
+ * file larger than FILE_CAP (no vector is that large). */
+static int slurp(const char *path, size_t *n) {
+    FILE *f = fopen(path, "rb");
+    size_t k;
+    if (!f) return -1;
+    k = fread(file_buf, 1, FILE_CAP, f);
+    if (k == FILE_CAP && fgetc(f) != EOF) { fclose(f); return -1; }
+    fclose(f);
+    *n = k;
+    return 0;
+}
+
+static int check_file(const char *path, char *line, size_t cap, int *roundtrip_ok) {
+    static uint8_t again[PST_MAX_MSG];
+    pst_msg m;
+    size_t n, k;
+    int code;
+    if (slurp(path, &n)) { fprintf(stderr, "cannot read %s\n", path); return -1; }
+    code = pst_decode(file_buf, n, &m);
+    verdict_line(code, &m, line, cap);
+    *roundtrip_ok = 1;
+    if (code == PST_OK) {
+        k = pst_encode(&m, again, sizeof again);
+        *roundtrip_ok = k == n && memcmp(again, file_buf, n) == 0;
+    }
+    return code;
+}
+
+static void rstrip(char *s) { size_t n = strlen(s); while (n && (s[n-1] == '\n' || s[n-1] == '\r')) s[--n] = 0; }
+
+/* A corpus line names a file in the corpus folder itself: no path separator,
+ * no leading dot, so expected.txt can never point outside the folder. */
+static int plain_name(const char *s) { return s[0] && s[0] != '.' && !strchr(s, '/'); }
+
+/* Create (or truncate) a file readable by all but writable only by its owner. */
+static FILE *create_file(const char *path, const char *mode) {
+    int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+    FILE *f;
+    if (fd < 0) return NULL;
+    if (!(f = fdopen(fd, mode))) close(fd);
+    return f;
+}
+
+static int corpus(const char *dir) {
+    char path[4096], line[1024], got[1024];
+    unsigned pass = 0, fail = 0;
+    FILE *f;
+
+    snprintf(path, sizeof path, "%s/expected.txt", dir);
+    if (!(f = fopen(path, "r"))) { fprintf(stderr, "cannot read %s\n", path); return 2; }
+    while (fgets(line, sizeof line, f)) {
+        char *sp;
+        int rt;
+        rstrip(line);
+        if (!line[0] || line[0] == '#') continue;
+        if (!(sp = strchr(line, ' '))) { fprintf(stderr, "bad line: %s\n", line); fail++; continue; }
+        *sp = 0;
+        if (!plain_name(line)) { fprintf(stderr, "not a plain file name: %s\n", line); fail++; continue; }
+        snprintf(path, sizeof path, "%s/%s", dir, line);
+        if (check_file(path, got, sizeof got, &rt) < 0) { fail++; continue; }
+        if (strcmp(got, sp + 1) != 0) { fail++; printf("MISMATCH %s\n  expected: %s\n  got:      %s\n", line, sp + 1, got); }
+        else if (!rt) { fail++; printf("ROUNDTRIP_MISMATCH %s\n", line); }
+        else pass++;
+    }
+    fclose(f);
+    printf("PST1_CONFORMANCE impl=c pass=%u fail=%u\n", pass, fail);
+    return fail || !pass ? 1 : 0;
+}
+
+/* ---- vector generator ------------------------------------------------------ */
+/* Every vector starts from one well-formed message and changes one thing. The
+ * expected line for an accepted vector is built from the generator's own
+ * struct, not from pst_decode, so the decoder is checked against the intent. */
+static uint8_t gen_payload[PST_MAX_PAY + 8];
+
+static void fill(uint8_t *p, size_t n, uint8_t seed) { for (size_t i = 0; i < n; i++) p[i] = (uint8_t)(seed + 7 * i); }
+
+static void base(pst_msg *m, uint8_t type, const char *text) {
+    size_t len = strlen(text);
+    memset(m, 0, sizeof *m);
+    m->version = PST_VERSION;
+    m->type = type;
+    fill(m->cause, 32, 0xC1);
+    fill(m->origin, 32, 0x0A);
+    fill(m->objective, 32, 0x0B);
+    fill(m->subject, 32, 0x5B);
+    fill(m->task, 16, 0x7A);
+    fill(m->idem, 16, 0x1D);
+    m->ordinal = type == T_REQUEST ? 0 : 1;
+    m->effect = 1;
+    memcpy(gen_payload, text, len);
+    m->payload = gen_payload;
+    m->payload_len = (uint16_t)len;
+    sha_once(gen_payload, len, m->digest);
+}
+
+static FILE *gen_expected;
+static int gen_fail;
+
+static void emit(const char *dir, const char *name, const uint8_t *b, size_t n, const char *expect) {
+    char path[4096];
+    FILE *f;
+    snprintf(path, sizeof path, "%s/%s", dir, name);
+    if (!(f = create_file(path, "wb")) || fwrite(b, 1, n, f) != n || fclose(f) != 0) {
+        fprintf(stderr, "cannot write %s\n", path);
+        gen_fail = 1;
+        return;
+    }
+    fprintf(gen_expected, "%s %s\n", name, expect);
+}
+
+static void emit_ok(const char *dir, const char *name, const pst_msg *m) {
+    static uint8_t out[PST_MAX_MSG];
+    char line[1024];
+    size_t n = pst_encode(m, out, sizeof out);
+    verdict_line(PST_OK, m, line, sizeof line);
+    emit(dir, name, out, n, line);
+}
+
+static void emit_bad(const char *dir, const char *name, const pst_msg *m, int at, int value, long extra, const char *code) {
+    static uint8_t out[PST_MAX_MSG + 64];
+    char line[128];
+    size_t n = pst_encode(m, out, sizeof out);
+    if (at >= 0) out[at] = (uint8_t)value;
+    if (extra < 0) n -= (size_t)(-extra);            /* drop bytes from the end */
+    else for (long i = 0; i < extra; i++) out[n++] = (uint8_t)(0xE0 + i);
+    snprintf(line, sizeof line, "refuse %s", code);
+    emit(dir, name, out, n, line);
+}
+
+static int make_vectors(const char *dir) {
+    char path[4096];
+    pst_msg m;
+    snprintf(path, sizeof path, "%s/expected.txt", dir);
+    if (!(gen_expected = create_file(path, "w"))) { fprintf(stderr, "cannot write %s\n", path); return 2; }
+    fprintf(gen_expected,
+        "# PST1 single-message vectors. Spec: ../SPEC.md (Draft v0.1, wire version 1).\n"
+        "# Line: <file> <verdict line>, verdict line as printed by tools/pst_verify.c:\n"
+        "#   ok type=T version=V cause=H64 origin=H64 objective=H64 subject=H64 parent=H32\n"
+        "#      task=H32 idem=H32 ordinal=N effect=E len=L digest=H64   (one line)\n"
+        "#   refuse <code from SPEC.md section 7>\n"
+        "# Generated by tools/pst_verify.c --make-vectors; do not edit by hand.\n");
+
+    /* accepted */
+    base(&m, 1, "Summarise the last three receipts in plain words.");
+    emit_ok(dir, "ok-request.pst", &m);
+    base(&m, 2, "");
+    emit_ok(dir, "ok-accepted-empty.pst", &m);
+    base(&m, 5, "");
+    memset(gen_payload, 'x', PST_MAX_PAY);
+    m.payload_len = PST_MAX_PAY; m.ordinal = 7; m.effect = 0;
+    fill(m.parent, 16, 0x9E);
+    sha_once(gen_payload, PST_MAX_PAY, m.digest);
+    emit_ok(dir, "ok-text-chunk-1024.pst", &m);
+    base(&m, 14, "");
+    m.effect = 2; m.ordinal = 0xFFFFFFFFu;
+    emit_ok(dir, "ok-resume-request-max-ordinal.pst", &m);
+
+    /* refused: one rule each, in check order */
+    base(&m, 1, "hello");
+    emit_bad(dir, "bad-length-short-header.pst", &m, -1, 0, -(long)(5 + 1), "PST_BAD_LENGTH");
+    emit_bad(dir, "bad-length-empty-file.pst", &m, -1, 0, -(long)(5 + PST_HDR), "PST_BAD_LENGTH");
+    base(&m, 5, "");
+    memset(gen_payload, 'y', PST_MAX_PAY);
+    m.payload_len = PST_MAX_PAY;
+    sha_once(gen_payload, PST_MAX_PAY, m.digest);
+    emit_bad(dir, "bad-length-over-1024.pst", &m, -1, 0, 1, "PST_BAD_LENGTH");
+    base(&m, 1, "hello");
+    emit_bad(dir, "bad-magic.pst", &m, O_MAGIC + 3, '2', 0, "PST_BAD_MAGIC");
+    emit_bad(dir, "bad-version-unknown.pst", &m, O_VERSION, 2, 0, "PST_BAD_VERSION");
+    emit_bad(dir, "bad-version-downgrade.pst", &m, O_VERSION, 0, 0, "PST_DOWNGRADE");
+    emit_bad(dir, "bad-type-zero.pst", &m, O_TYPE, 0, 0, "PST_BAD_TYPE");
+    emit_bad(dir, "bad-type-unknown.pst", &m, O_TYPE, 0xFF, 0, "PST_BAD_TYPE");
+    emit_bad(dir, "bad-reserved0.pst", &m, O_RES0 + 1, 1, 0, "PST_RESERVED_NONZERO");
+    emit_bad(dir, "bad-reserved1.pst", &m, O_RES1, 0x80, 0, "PST_RESERVED_NONZERO");
+    emit_bad(dir, "bad-length-mismatch.pst", &m, -1, 0, -2, "PST_BAD_LENGTH");
+    emit_bad(dir, "bad-payload-len-over-800.pst", &m, O_PLEN, 0x04, 0, "PST_BAD_LENGTH");
+    emit_bad(dir, "bad-trailing-byte.pst", &m, -1, 0, 1, "PST_TRAILING_BYTES");
+    emit_bad(dir, "bad-effect-class.pst", &m, O_EFFECT, 3, 0, "PST_BAD_EFFECT_CLASS");
+    memset(m.cause, 0, 32);
+    emit_bad(dir, "bad-no-cause.pst", &m, -1, 0, 0, "PST_NO_CAUSE");
+    base(&m, 1, "hello");
+    memset(m.task, 0, 16);
+    emit_bad(dir, "bad-no-task-id.pst", &m, -1, 0, 0, "PST_NO_TASK");
+    base(&m, 1, "hello");
+    memset(m.idem, 0, 16);
+    emit_bad(dir, "bad-no-idempotence-key.pst", &m, -1, 0, 0, "PST_NO_TASK");
+    base(&m, 1, "hello");
+    memset(m.objective, 0, 32);
+    emit_bad(dir, "bad-request-no-objective.pst", &m, -1, 0, 0, "PST_NO_OBJECTIVE");
+    base(&m, 1, "hello");
+    m.digest[31] ^= 1;
+    emit_bad(dir, "bad-digest.pst", &m, -1, 0, 0, "PST_DIGEST_MISMATCH");
+
+    if (fclose(gen_expected) != 0) gen_fail = 1;
+    return gen_fail ? 2 : 0;
+}
+
+int main(int argc, char **argv) {
+    char out[1024];
+    int rt;
+    if (argc == 2 && argv[1][0] != '-') {
+        int code = check_file(argv[1], out, sizeof out, &rt);
+        if (code < 0) return 2;
+        puts(out);
+        return code == PST_OK && rt ? 0 : 1;
+    }
+    if (argc == 3 && strcmp(argv[1], "--corpus") == 0) return corpus(argv[2]);
+    if (argc == 3 && strcmp(argv[1], "--make-vectors") == 0) return make_vectors(argv[2]);
+    fprintf(stderr, "usage: pst_verify FILE | --corpus DIR | --make-vectors DIR\n");
+    return 2;
+}
diff --git a/specs/pi-spark-task/vectors/.gitattributes b/specs/pi-spark-task/vectors/.gitattributes
new file mode 100644
index 0000000..6b65b27
--- /dev/null
+++ b/specs/pi-spark-task/vectors/.gitattributes
@@ -0,0 +1 @@
+*.pst binary
diff --git a/specs/pi-spark-task/vectors/bad-digest.pst b/specs/pi-spark-task/vectors/bad-digest.pst
new file mode 100644
index 0000000..8737c84
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-digest.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-effect-class.pst b/specs/pi-spark-task/vectors/bad-effect-class.pst
new file mode 100644
index 0000000..5982fa8
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-effect-class.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-length-empty-file.pst b/specs/pi-spark-task/vectors/bad-length-empty-file.pst
new file mode 100644
index 0000000..e69de29
diff --git a/specs/pi-spark-task/vectors/bad-length-mismatch.pst b/specs/pi-spark-task/vectors/bad-length-mismatch.pst
new file mode 100644
index 0000000..846f7f2
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-length-mismatch.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-length-over-1024.pst b/specs/pi-spark-task/vectors/bad-length-over-1024.pst
new file mode 100644
index 0000000..4c05252
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-length-over-1024.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-length-short-header.pst b/specs/pi-spark-task/vectors/bad-length-short-header.pst
new file mode 100644
index 0000000..29fc12f
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-length-short-header.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-magic.pst b/specs/pi-spark-task/vectors/bad-magic.pst
new file mode 100644
index 0000000..519369b
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-magic.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-no-cause.pst b/specs/pi-spark-task/vectors/bad-no-cause.pst
new file mode 100644
index 0000000..971e0e5
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-no-cause.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-no-idempotence-key.pst b/specs/pi-spark-task/vectors/bad-no-idempotence-key.pst
new file mode 100644
index 0000000..e958e0d
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-no-idempotence-key.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-no-task-id.pst b/specs/pi-spark-task/vectors/bad-no-task-id.pst
new file mode 100644
index 0000000..e882c8b
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-no-task-id.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-payload-len-over-800.pst b/specs/pi-spark-task/vectors/bad-payload-len-over-800.pst
new file mode 100644
index 0000000..630f57a
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-payload-len-over-800.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-request-no-objective.pst b/specs/pi-spark-task/vectors/bad-request-no-objective.pst
new file mode 100644
index 0000000..b471c1d
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-request-no-objective.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-reserved0.pst b/specs/pi-spark-task/vectors/bad-reserved0.pst
new file mode 100644
index 0000000..9d1a3bf
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-reserved0.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-reserved1.pst b/specs/pi-spark-task/vectors/bad-reserved1.pst
new file mode 100644
index 0000000..dbb6b8d
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-reserved1.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-trailing-byte.pst b/specs/pi-spark-task/vectors/bad-trailing-byte.pst
new file mode 100644
index 0000000..4a957d0
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-trailing-byte.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-type-unknown.pst b/specs/pi-spark-task/vectors/bad-type-unknown.pst
new file mode 100644
index 0000000..400b0bb
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-type-unknown.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-type-zero.pst b/specs/pi-spark-task/vectors/bad-type-zero.pst
new file mode 100644
index 0000000..98cded0
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-type-zero.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-version-downgrade.pst b/specs/pi-spark-task/vectors/bad-version-downgrade.pst
new file mode 100644
index 0000000..dafe08c
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-version-downgrade.pst differ
diff --git a/specs/pi-spark-task/vectors/bad-version-unknown.pst b/specs/pi-spark-task/vectors/bad-version-unknown.pst
new file mode 100644
index 0000000..3fc9c4a
Binary files /dev/null and b/specs/pi-spark-task/vectors/bad-version-unknown.pst differ
diff --git a/specs/pi-spark-task/vectors/expected.txt b/specs/pi-spark-task/vectors/expected.txt
new file mode 100644
index 0000000..11fae5d
--- /dev/null
+++ b/specs/pi-spark-task/vectors/expected.txt
@@ -0,0 +1,29 @@
+# PST1 single-message vectors. Spec: ../SPEC.md (Draft v0.1, wire version 1).
+# Line: <file> <verdict line>, verdict line as printed by tools/pst_verify.c:
+#   ok type=T version=V cause=H64 origin=H64 objective=H64 subject=H64 parent=H32
+#      task=H32 idem=H32 ordinal=N effect=E len=L digest=H64   (one line)
+#   refuse <code from SPEC.md section 7>
+# Generated by tools/pst_verify.c --make-vectors; do not edit by hand.
+ok-request.pst ok type=1 version=1 cause=c1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939a origin=0a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3 objective=0b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4 subject=5b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d34 parent=00000000000000000000000000000000 task=7a81888f969da4abb2b9c0c7ced5dce3 idem=1d242b323940474e555c636a71787f86 ordinal=0 effect=1 len=49 digest=e57392a27132b97e2cbfaa4b04ad3446cab722297cf48ce22c7c2fb787940cc6
+ok-accepted-empty.pst ok type=2 version=1 cause=c1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939a origin=0a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3 objective=0b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4 subject=5b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d34 parent=00000000000000000000000000000000 task=7a81888f969da4abb2b9c0c7ced5dce3 idem=1d242b323940474e555c636a71787f86 ordinal=1 effect=1 len=0 digest=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
+ok-text-chunk-1024.pst ok type=5 version=1 cause=c1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939a origin=0a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3 objective=0b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4 subject=5b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d34 parent=9ea5acb3bac1c8cfd6dde4ebf2f90007 task=7a81888f969da4abb2b9c0c7ced5dce3 idem=1d242b323940474e555c636a71787f86 ordinal=7 effect=0 len=800 digest=20885ae2150992180a94350ef60c6d8a1599899bcd2752d3392f8ad591ddd8b1
+ok-resume-request-max-ordinal.pst ok type=14 version=1 cause=c1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939a origin=0a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3 objective=0b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4 subject=5b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d34 parent=00000000000000000000000000000000 task=7a81888f969da4abb2b9c0c7ced5dce3 idem=1d242b323940474e555c636a71787f86 ordinal=4294967295 effect=2 len=0 digest=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
+bad-length-short-header.pst refuse PST_BAD_LENGTH
+bad-length-empty-file.pst refuse PST_BAD_LENGTH
+bad-length-over-1024.pst refuse PST_BAD_LENGTH
+bad-magic.pst refuse PST_BAD_MAGIC
+bad-version-unknown.pst refuse PST_BAD_VERSION
+bad-version-downgrade.pst refuse PST_DOWNGRADE
+bad-type-zero.pst refuse PST_BAD_TYPE
+bad-type-unknown.pst refuse PST_BAD_TYPE
+bad-reserved0.pst refuse PST_RESERVED_NONZERO
+bad-reserved1.pst refuse PST_RESERVED_NONZERO
+bad-length-mismatch.pst refuse PST_BAD_LENGTH
+bad-payload-len-over-800.pst refuse PST_BAD_LENGTH
+bad-trailing-byte.pst refuse PST_TRAILING_BYTES
+bad-effect-class.pst refuse PST_BAD_EFFECT_CLASS
+bad-no-cause.pst refuse PST_NO_CAUSE
+bad-no-task-id.pst refuse PST_NO_TASK
+bad-no-idempotence-key.pst refuse PST_NO_TASK
+bad-request-no-objective.pst refuse PST_NO_OBJECTIVE
+bad-digest.pst refuse PST_DIGEST_MISMATCH
diff --git a/specs/pi-spark-task/vectors/ok-accepted-empty.pst b/specs/pi-spark-task/vectors/ok-accepted-empty.pst
new file mode 100644
index 0000000..92bd349
Binary files /dev/null and b/specs/pi-spark-task/vectors/ok-accepted-empty.pst differ
diff --git a/specs/pi-spark-task/vectors/ok-request.pst b/specs/pi-spark-task/vectors/ok-request.pst
new file mode 100644
index 0000000..685b258
Binary files /dev/null and b/specs/pi-spark-task/vectors/ok-request.pst differ
diff --git a/specs/pi-spark-task/vectors/ok-resume-request-max-ordinal.pst b/specs/pi-spark-task/vectors/ok-resume-request-max-ordinal.pst
new file mode 100644
index 0000000..5daa99c
Binary files /dev/null and b/specs/pi-spark-task/vectors/ok-resume-request-max-ordinal.pst differ
diff --git a/specs/pi-spark-task/vectors/ok-text-chunk-1024.pst b/specs/pi-spark-task/vectors/ok-text-chunk-1024.pst
new file mode 100644
index 0000000..9ad7129
Binary files /dev/null and b/specs/pi-spark-task/vectors/ok-text-chunk-1024.pst differ