Skip to content

Commit d4a68ed

Browse files
feat(publish): prototype generation-directory publish (not wired), with experiments and a model check (#275)
**Prototype, kept as a draft until the maintainer decides the open design questions.** Nothing calls it yet. ## Why On #264 the maintainer chose to replace per-file output writes with a **generation-directory publish**. This prototype tests that design on evidence first. The publish is meant to close three gaps that exist in every output mode today: - **Check-then-write race:** a symbolic link swapped in between the preflight check and the write. - **Partial files after a crash.** - **Interleaving between concurrent runs.** ## What is here ### `src/publish.rs` For a report `D/S.json`, the layout is: - `D/S.gen/<id>/`: one complete generation per directory. - `D/S.gen/CURRENT`: a JCS manifest naming the current generation and the SHA-256 of each of its files. - Mirrors at `D/S.json` and the other output names: hard links into the current generation, with copies where linking fails. A publish runs in this order: 1. Write the generation into `<id>.partial/`, syncing each file. 2. Rename it to `<id>`. 3. Under a `File::lock`: - refuse if `CURRENT` names a newer generation (ids sort by run start); - otherwise replace `CURRENT` by temp file and rename; - refresh the mirrors; - collect old generations (default: keep 1 older). Readers take no lock. When the generation `CURRENT` named has been collected, they retry from `CURRENT`. ### `examples/publish_stress.rs` Compares three strategies: `inplace` (`fs::write`, as on main), `perfile` (rename each file) and `genset` (this prototype). It runs a multi-process race, kill -9 crash trials and a latency bench. ### `tests/publish_model.rs` An exhaustive explicit-state check of the protocol, reported as **TESTED** with these bounds: - up to 3 writers and 1 reader, 2 files; - a crash at every writer step; - garbage collection of crashed **and live** writers' partial generations; - 13,427 states at most. Results: - **P1–P3 hold:** `CURRENT` names only complete generations, it never moves backwards, and manifest readers never fail or return a partial set. - **P4 fails, as expected:** after a crash the mirrors can be torn. - **Three deliberate mutants are each caught**, showing that the invariants are not trivially true. ## Measured on Linux 6.18, ext4, 4 cores | | inplace | perfile | genset (mirrors) | genset (manifest) | |---|---|---|---|---| | race, 3 writers × 150 + readers: partial reads | 7931 / 8424 | 0 / 3209 | 0 / 6058 | **0 / 2500** | | race: torn sets | 182 | 3008 | 67 | **0** | | kill -9, 300 trials: partial / torn left behind | 176 / 13 | 0 / 155 | 0 / 3 | **0 / 0** | | 100 races: torn final state | 0 | 7 | 0 | **0** | | p50 latency, 1 KiB / 256 KiB / 4 MiB | 0.27 / 1.0 / 12.5 ms | 1.2 / 3.2 / 23.8 ms | 2.7 / 4.6 / 33.7 ms | | Defects the experiments found, each fixed here with a test that fails without the fix: - GC raced a writer renaming its own `.partial` directory, so a writer crashed with NotFound in 1 of 40 races. - A reader could lose a generation to GC: 1 in 2,500 reads. It now retries, and was 0 in about 11,200 reads afterwards. ## Open before this can be wired - **Mirrors torn after a crash** (P4). The options are to document it, to use one `current` symlink pointer (crash-consistent on Unix), or to drop the mirrors. - **Proof legs** under `PROOF-POLICY` §1.2 and §4: - a TLA+ spec checked by TLC (the policy's worked exception), which was not runnable in this session; - Creusot/Pearlite and Verus for the pure selection logic, still OPEN. - **Other questions:** - the generation id format (UUIDv7 or v8 per the estate ID policy); - Windows behaviour (no Windows run yet); - power-loss durability (fsync ordering is not tested; kill -9 tests process crashes only); - what a replacement keeps: ACLs, xattrs, labels and ownership. --- _Generated by [Claude Code](https://claude.ai/code/session_01PFaqv6iBXrt3W5Akd1Fq5k)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent e12b823 commit d4a68ed

17 files changed

Lines changed: 1717 additions & 0 deletions

‎.github/workflows/verify.yml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,32 @@ jobs:
242242
assert any(i.get('status') not in ('analyzed', 'excluded') for i in coverage['inputs']), coverage
243243
print(f"coverage={coverage['status']} (gap recorded)")
244244
PY
245+
246+
# The generation-publish protocol (src/publish.rs) is model-checked with TLC,
247+
# PROOF-POLICY §4's worked exception for concurrency protocols. Results are
248+
# TESTED with the bounds in each .cfg, never PROVEN. Every config states its
249+
# expected outcome; the expected violations (the documented mirror limit and
250+
# three mutants) show the invariants can fail.
251+
#
252+
# Deviation (PROOF-POLICY §6), state: open. Leg: protocol model check. Tool:
253+
# tla2tools.jar v1.7.4 (TLC 2.19), SHA-256 below, checked by check.sh. Why not
254+
# Guix or the container: neither provides TLC yet. Java comes from the runner.
255+
tlaplus:
256+
name: TLA+ model check (GenerationPublish)
257+
runs-on: ubuntu-latest
258+
timeout-minutes: 20
259+
steps:
260+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
261+
with:
262+
persist-credentials: false
263+
- name: Fetch the pinned TLC and model-check every config
264+
shell: bash
265+
run: |
266+
set -euo pipefail
267+
java -version
268+
tools_dir=$(mktemp -d)
269+
trap 'rm -rf "$tools_dir"' EXIT
270+
curl --fail --silent --show-error --location --max-time 120 \
271+
https://github.com/tlaplus/tlaplus/releases/download/v1.7.4/tla2tools.jar \
272+
--output "$tools_dir/tla2tools.jar"
273+
TLA2TOOLS="$tools_dir/tla2tools.jar" bash verification/proofs/tlaplus/check.sh

‎PROOF-NEEDS.adoc‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,37 @@ obligation. The gateway octad projection is lossy by design, so this property
8181
also does not establish a faithful hexad↔octad map.
8282
|===
8383

84+
=== Model-Checked (TESTED, bounded)
85+
86+
[width="100%",cols="26%,30%,44%",options="header",]
87+
|===
88+
|Obligation |Artefact |Status and bounds
89+
|Generation-publish protocol P1–P3 (prototype, `src/publish.rs`;
90+
design `docs/design/GENERATION-PUBLISH.adoc`) |`verification/proofs/tlaplus/GenerationPublish.tla`
91+
(TLC 2.19, tla2tools v1.7.4, run by `verify.yml` job `tlaplus`) and
92+
`tests/publish_model.rs` (independent explicit-state checker) |*TESTED*: up to
93+
3 writers, 1 reader, 2 files, crash at every writer step, collection of crashed
94+
and live writers' partial generations, `keep` 0 and 1. P1 (`CURRENT` names a
95+
complete generation), P2 (never moves backwards) and P3 (manifest reader never
96+
fails) hold. P4 (mirrors consistent once quiet) holds without crashes and
97+
*fails* after a crash, the documented limit. Three mutants each violate P1 or
98+
P2, so the invariants are not vacuous. *PROOF-POLICY §4 exception*: a
99+
concurrency protocol over crash interleavings, which Creusot and Verus do not
100+
state. *§6 deviation (open)*: TLC is fetched from its release, digest-checked,
101+
because neither Guix nor the container provides it yet.
102+
|===
103+
84104
=== What Still Needs Proving
85105

86106
[width="100%",cols="51%,27%,22%",options="header",]
87107
|===
88108
|Component |What |Why
109+
|Generation publish: pure decisions (`src/publish.rs`) |*OPEN* (PROOF-POLICY
110+
§1.2): Creusot/Pearlite and Verus contracts for the supersede comparison and
111+
the collect selection (never removes the current or a newer generation; keeps
112+
the `keep` newest older ones) |The model check covers the protocol, not the Rust
113+
code; these tie the implementation to P1 and P2
114+
89115
|*Layer 1.0 — stripBlockComments + Strings + Composition +
90116
Position-Preservation* (issue #114) |Block-comment (`+/* */+`) and
91117
string-literal (`+"..."+`) strippers with same mutual-recursive shape;
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// SPDX-License-Identifier: CC-BY-SA-4.0
2+
// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
3+
= Generation-directory publish
4+
:toc:
5+
6+
Status: *prototype* (`src/publish.rs`), not wired into any subcommand.
7+
8+
== Why
9+
10+
`assail` writes up to three files for one run: the JSON report, its KYAML
11+
copy and the `--attest` sidecar. Writing them one at a time with `fs::write`
12+
leaves three gaps:
13+
14+
* a crash can leave a partial file;
15+
* two runs into the same paths can interleave, leaving files from different
16+
runs side by side;
17+
* a symbolic link swapped in between the preflight check and the write is
18+
written through.
19+
20+
hyperpolymath/panic-attack#264 chose to close these for every output mode
21+
with one design: publish each run as a *generation*, and move a single
22+
pointer to it.
23+
24+
== Layout
25+
26+
For a report `D/S.json`:
27+
28+
[cols="1,3"]
29+
|===
30+
|`D/S.gen/<id>.partial/` |A generation still being written.
31+
|`D/S.gen/<id>/` |One complete generation: `S.json`, `S.kyaml`, `S.attestation.json`.
32+
|`D/S.gen/CURRENT` |The pointer: a JCS manifest naming the current generation and the SHA-256 of each file in it.
33+
|`D/S.gen/lock` |An advisory lock (`File::lock`) taken by publishers only.
34+
|`D/S.json`, `D/S.kyaml`, … |Mirrors: hard links into the current generation (copies where linking fails).
35+
|===
36+
37+
`<id>` is 16 hex digits of Unix nanoseconds at the run's start followed by 16
38+
random hex digits, so ids sort by start time.
39+
40+
== Protocol
41+
42+
. *Write.* Create `<id>.partial/`. Create each file new inside it and sync it.
43+
. *Complete.* Sync the directory, rename it to `<id>`, sync `S.gen`.
44+
. *Publish, under the lock:*
45+
.. if `CURRENT` names a newer generation, this run is *superseded*: remove
46+
its generation and stop;
47+
.. otherwise replace `CURRENT` (temporary file, sync, rename, sync `S.gen`);
48+
.. refresh each mirror by hard link and rename;
49+
.. collect: remove complete generations older than the current one, except
50+
the newest `keep` (default 1), and partial generations older than the
51+
grace period (default 1 hour).
52+
53+
*Readers* take no lock. A reader that needs one consistent set reads
54+
`CURRENT`, then the generation's files, and checks each against the
55+
manifest's digest. If the generation was collected in between, it starts
56+
again, but only if `CURRENT` has moved; otherwise the loss is real and it
57+
reports an error.
58+
59+
== What is guaranteed
60+
61+
Checked by TLC (`verification/proofs/tlaplus/`) and, independently, by the
62+
explicit-state checker in `tests/publish_model.rs`. Both are `TESTED` under
63+
PROOF-POLICY §4, with the bounds in each configuration: up to 3 writers,
64+
1 reader and 2 files, a crash at every writer step, and collection of the
65+
partial generations of crashed and live writers.
66+
67+
[cols="1,4,1"]
68+
|===
69+
|Property |Statement |Result
70+
71+
|P1 |`CURRENT` only ever names a complete generation. |holds
72+
|P2 |`CURRENT` never moves to an older generation. |holds
73+
|P3 |A manifest reader never fails or returns a mixed set. |holds
74+
|P4 |When no writer is running, every mirror names the current generation. |holds without crashes; *fails after a crash* (see below)
75+
|===
76+
77+
Each checker also runs three mutants of the protocol, each with one
78+
defect. Each mutant violates P1 or P2, which shows the invariants are not
79+
vacuous:
80+
81+
* no supersede check;
82+
* publish before completion;
83+
* collect the current generation.
84+
85+
Process crashes only: a SIGKILL stops the process and the kernel releases
86+
its lock. Power loss, and the ordering of `fsync` against it, is not modelled
87+
or tested.
88+
89+
== The mirror limit (owner decision: hard links, documented)
90+
91+
Mirrors are refreshed one at a time after `CURRENT` moves. A crash between
92+
two refreshes leaves the mirrors from different generations: each file is
93+
complete, but the set is mixed. The checkers find this (P4), and the crash
94+
experiment measured it: 3 torn mirror sets in 300 kills, against none for
95+
manifest readers.
96+
97+
This is accepted and documented, because:
98+
99+
* each mirror is always a complete file, never a partial one;
100+
* the attestation sidecar seals the report's SHA-256, so
101+
`attest verify --report` detects a mixed report and sidecar;
102+
* the next successful publish repairs the mirrors;
103+
* a reader that needs a consistent set reads through `CURRENT`.
104+
105+
The alternative considered was a single `current` symlink with fixed
106+
top-level symlinks through it. It is crash-consistent on Unix, but needs
107+
symlinks (Windows falls back) and must exempt its own links from `assail`'s
108+
symlink refusal. It was not chosen.
109+
110+
== Measurements
111+
112+
Measured on Linux 6.18, ext4, 4 cores, with `examples/publish_stress.rs`.
113+
Three strategies are compared: `fs::write` in place, a rename per file, and
114+
generation publish.
115+
116+
[cols="2,1,1,1,1"]
117+
|===
118+
| |in place |rename per file |generation, mirrors |generation, manifest
119+
120+
|race, 3 writers × 150 runs: partial reads |7931 / 8424 |0 / 3209 |0 / 6058 |0 / 2500
121+
|race: mixed sets read |182 |3008 |67 |0
122+
|kill -9, 300 trials: partial / mixed left |176 / 13 |0 / 155 |0 / 3 |0 / 0
123+
|100 races: mixed final state |0 |7 |0 |0
124+
|p50 latency, 1 KiB / 256 KiB / 4 MiB |0.27 / 1.0 / 12.5 ms |1.2 / 3.2 / 23.8 ms |2.7 / 4.6 / 33.7 ms |
125+
|===
126+
127+
The extra cost is mostly the directory syncs. Against a scan that takes
128+
seconds, a few milliseconds is negligible.
129+
130+
== Open before wiring
131+
132+
* *Rust verification* (PROOF-POLICY §1.2): Creusot/Pearlite and Verus for
133+
the pure decisions: the supersede comparison, and the collect selection
134+
(never the current generation or a newer one). `OPEN`.
135+
* *Generation id format*: align with the estate identifier policy (UUIDv7 or
136+
v8), keeping the start-time ordering.
137+
* *Windows*: there is no Windows run yet. `std::fs::rename` replaces
138+
directories differently there, and a file that is open cannot be removed.
139+
* *What a replacement keeps*: new files inherit the directory's default ACLs,
140+
not the previous file's ACLs, xattrs, labels or owner. A private report
141+
stays private because each file takes the mode of its predecessor in the
142+
current generation.
143+
* *Wiring*: `assail` (all output modes) and the preflight in
144+
hyperpolymath/panic-attack#264 and hyperpolymath/panic-attack#274.

0 commit comments

Comments
 (0)