summaryrefslogtreecommitdiffstats
path: root/docs/INVARIANTS.md
blob: c148961ac6c3ce9930fc2b2ad5c8fc9a54078e50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# §2 invariants, and the test that holds each one

Every invariant in DESIGN.md §2 maps to at least one named test. The primary
name is listed first; corroborating tests are listed after it. Run any of them
with `cargo test --test <file> <name>`.

| # | Invariant | Test |
|---|---|---|
| **I1** | Every artifact in a served index traces to an `approval` row. | `invariants::i1_every_index_entry_traces_back_to_an_approval` · `pipeline::i1_no_index_entry_without_an_approval` · `pipeline::publish_refuses_without_an_approval_behind_it` · `db::i1_query_flags_index_entries_without_approval` |
| **I2** | A review's base is the pkgbase's `current_accepted_release`, not the last detected candidate. | `invariants::i2_base_is_the_last_accepted_version_after_many_detections` · `detect::i2_diff_base_is_the_last_accepted_version_however_many_detections_arrive` |
| **I3** | Candidate identity is content-addressed over everything that becomes a byte. | `invariants::i3_materialization_fails_closed_on_the_recipe_pin` · `detect::epoch_and_pkgrel_are_part_of_the_identity` · `detect::recipe_hash_covers_shipped_patches` · `detect::detection_is_idempotent_and_records_no_duplicate_candidate` · `db::candidate_identity_is_unique_per_pkgbase` |
| **I4** | Publishing is the only externally visible step, and it is atomic and serialized. | `invariants::i4_kill_mid_publish_leaves_the_old_index_byte_identical` · `pipeline::i4_kill_mid_publish_leaves_the_old_index_intact` · `pipeline::a_stale_accepted_pointer_refuses_to_publish` |
| **I5** | Every step is idempotent and checkpointed; a worker can die at any instant. | `invariants::i5_resume_after_a_crash_repeats_no_completed_step` · `pipeline::i5_a_restarted_build_resumes_at_the_next_incomplete_step` · `crash::a_process_killed_mid_step_resumes_from_its_checkpoints_with_no_duplicate_work` · `dispatch::a_reaped_build_resumes_at_the_next_incomplete_step_with_no_duplicate_work` |
| **I6** | A manual rebuild reuses the approved source identity, with no new approval. | `invariants::i6_manual_rebuild_publishes_the_same_version_without_a_new_approval` · `pipeline::i6_manual_rebuild_reuses_the_approved_identity` · `surfaces::a_manual_rebuild_reuses_the_approved_identity_without_a_new_approval` |
| **I7** | At most one open review per pkgbase; a newer detection collapses into it. | `invariants::i7_the_schema_refuses_a_second_open_review` · `db::i7_second_open_review_is_rejected_by_the_database` · `approve::collapse_in_place_bumps_revision_without_a_new_row` |
| **I8** | The index is exactly the set of `current_accepted_release` pointers. | `invariants::i8_index_equals_the_accepted_pointers` · `pipeline::i8_served_index_holds_exactly_the_accepted_pointers` |
| **I9** | Build hosts are untrusted: a malicious PKGBUILD must not reach the DB, the signing key, or another build. | `invariants::i9_a_build_cannot_smuggle_bytes_in_from_outside_its_own_staging_dir` |
| **I10** | Every state transition is recorded, append-only, with actor and timestamp. | `invariants::i10_audit_is_append_only_and_records_the_decisions` · `db::audit_log_is_append_only` · `surfaces::the_builds_page_names_the_version_and_the_audit_page_records_the_decision` |
| **I11** | A non-publishable change never silently disappears; it becomes a `notice`. | `notices::i11_nothing_publishable_is_ever_silently_dropped` · `detect::recipe_drift_under_a_static_version_is_a_notice_not_a_candidate` · `vcs::a_moved_tip_with_an_unchanged_pkgver_is_a_notice_not_a_candidate` · `notices::digest_drift_is_a_high_severity_integrity_alert_and_no_candidate` |
| **I12** | Every release records the set of our own packages it was built against. | `invariants::i12_every_build_records_a_world_snapshot` · `pipeline::i12_world_snapshot_is_recorded_at_ingest` · `world::the_world_snapshot_is_intersected_with_our_index_at_ingest_time` · `db::world_snapshot_dedupes_by_hash` |

## What I9 does and does not cover

The full statement of I9 is enforced by the chroot, and the chroot tier is gated
behind root and network (§16a). What is *not* gated, and what
`i9_a_build_cannot_smuggle_bytes_in_from_outside_its_own_staging_dir` pins, is
the pipeline's own hygiene: `collect` scans the srcdir for `*.pkg.tar.zst` and
`ingest` copies whatever those paths resolve to into the content-addressed
store, from where the bytes are served from our repo and signed with our key. A
build that could make one of those paths be a symlink to the keyring or the
database would turn a missing chroot into a signed artifact of our own secrets.
The test acts that attack out — the shim plants a symlink to the database file —
and asserts the build fails closed, the store is untouched, and nothing
publishes.

## Invariants that are properties of the schema, not of a code path

Two of these are enforced by SQLite itself so that no future code path can forget
them, and each has a test that tries to violate it through the database rather
than through the application:

* I7 — a partial unique index on `candidate (pkgbase_id) WHERE state='pending_review'`.
* I10 — `UPDATE`/`DELETE` triggers on `audit`, and on `approval` (§7.2, the
  approval record is immutable because it is the only proof of what a human saw).

## Structural invariants, and the test that holds each one

The rest of this file is about the twelve numbered invariants, which are claims
about what the system does. The ones below are claims about how the code is
allowed to be shaped — the kind that a review finds once and that a test then
holds forever, because a convention nobody checks erodes back into inconsistency
within a few commits. Each is enforced by a test that reads the source tree and
fails on the shape, not on behaviour.

| Invariant | Test |
|---|---|
| SQL has one owner: `db/`. No other module holds a statement or touches a rusqlite connection. | `schema_ownership::no_sql_statements_outside_the_db_module` |
| A version string is formatted in exactly one place, `pkgver::full_version` — never again as a `CASE` in SQL. | `schema_ownership::only_rust_formats_a_version` |
| Dependency direction: `db` does not know about `review`/`watch`/`pipeline`; `review` does not call back into the watcher; `watch` does not reach into the pipeline or the UI; `recipe` is a leaf. | `layers::persistence_names_nothing_above_itself` · `layers::review_does_not_call_back_into_the_watcher` · `layers::the_watcher_does_not_reach_into_the_pipeline_or_the_ui` · `layers::the_recipe_leaf_depends_on_nothing_above_it` |
| The five state machines are enums. A row whose state text the code does not define is an error, not an empty string, and a transition the table does not define is refused rather than applied. | `state_machine::the_machine_refuses_a_transition_it_does_not_define` · `state_machine::a_state_the_code_does_not_know_is_an_error_not_an_empty_string` · `state_machine::every_state_round_trips_through_its_column_text` · `state_machine::an_unnamed_state_in_any_row_is_an_error` |
| A worker that lost its lease cannot write over the worker that owns the build. | `state_machine::a_zombie_worker_cannot_finish_a_build_it_no_longer_owns` |
| The production binary contains no test scaffolding: no shim harness, no shim binaries, no ungated fault-injection branch, and a default config with no hooks attached. | `production_surface::no_production_module_references_the_shim_harness` · `production_surface::the_shim_binaries_are_test_only` · `production_surface::the_publish_hot_path_has_no_ungated_test_branch` · `production_surface::a_default_config_has_no_hooks_attached` |
| The served index has exactly one serialization lock, so no second lock can be added that does not know about the first. | `production_surface::the_served_index_has_exactly_one_serialization_lock` |
| The rules a reviewer reads are the same rules the machine scored: no private copies of the host rule, the body-changed rule, or a hand-built audit row. | `production_surface::the_review_modules_do_not_keep_private_copies_of_the_rules` · `production_surface::audit_rows_are_built_by_the_constructors_not_by_hand` |
| `canonical_json` is canonical in fact — key order is a property of the bytes, not of the struct declaration — and a value that cannot be serialized is an error, never an empty object that hashes like a real identity. | `hash::the_rendering_is_canonical_in_fact_not_in_name` · `hash::a_value_that_cannot_be_serialized_is_an_error_not_an_empty_object` |
| The session id is the credential: a cookie a client typed by hand proves nothing, and logging out invalidates the id rather than flipping a bit inside a cookie the client controls. | `http::a_forged_auth_cookie_is_not_a_session` · `http::login_rotates_the_session_and_logout_ends_it` |
| A pinned clock pins everything derived from it, including lease expiry — a test that freezes time must not compute a real-clock deadline. | `time::a_pinned_clock_moves_relative_arithmetic_with_it` · `time::a_lease_taken_under_a_pinned_clock_expires_against_that_clock` |