spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / docs / CANARY-CONTRACT.md
| 1 | # Canary contract: supervisor-driven observations |
| 2 | |
| 3 | Status: contract foundation (step 1 of docs/plans/2026-10-12-evidence-driven-rsi-cut.md). |
| 4 | Code: `src/canary/` and the `ExecuteRequest` / `ExecuteResult` pair in `src/supervisor/ipc.rs`. |
| 5 | Vocabulary: CONTEXT.md. |
| 6 | |
| 7 | The one rule above everything: no candidate-controlled statement can independently establish |
| 8 | that a canary passed. Every accepted observation is tied to the exact executable that was built, |
| 9 | checked by the supervisor, and admitted through one authoritative promotion path. |
| 10 | |
| 11 | ## Digests |
| 12 | |
| 13 | Every digest is lowercase hex SHA-256. Structured digests are domain separated: the domain string |
| 14 | and then each field are written length-prefixed (u64 little endian, then the bytes), the same |
| 15 | scheme as the version 2 evaluation receipt. Canonical JSON is one object with keys in byte order |
| 16 | at every level, no insignificant whitespace, serde_json string escaping, integers only. |
| 17 | |
| 18 | | Digest | Domain | Fields | |
| 19 | |---|---|---| |
| 20 | | request | `spark-rsi/canary-request/v1` | workload id, request id, input, expected output, deadline ms (u64 LE), nondeterminism | |
| 21 | | workload | `spark-rsi/canary-workload/v1` | canonical bytes of the workload manifest | |
| 22 | | observation id | `spark-rsi/canary-observation-id/v2` | generation id, request id, request digest | |
| 23 | | observation | `spark-rsi/canary-observation/v2` | canonical bytes of the observation record | |
| 24 | | candidate identity | `spark-rsi/candidate-identity/v1` | patch digest, tree digest, executable digest, generation id | |
| 25 | | observation log | `spark-rsi/canary-observation-log/v1` | every byte of the log before the seal line | |
| 26 | | result | none | SHA-256 of the exact result bytes received | |
| 27 | | adapter manifest | none | SHA-256 of the exact manifest file bytes | |
| 28 | |
| 29 | ## Adapter manifest: `canary-adapter.toml` |
| 30 | |
| 31 | Shipped at the root of the target repository. A target without one is not eligible for canary and |
| 32 | fails closed. It names how the built artifact is exercised; it never names a passing criterion. |
| 33 | |
| 34 | ```toml |
| 35 | manifest_version = 1 |
| 36 | protocol_version = 1 |
| 37 | executable = "release/rsi-canary-fixture" # relative to the build output root, never climbing |
| 38 | args = ["--adapter"] |
| 39 | workload_id = "fixture-v1" # must equal the pinned workload's id |
| 40 | response_format = "utf8-exact" |
| 41 | ``` |
| 42 | |
| 43 | ## Workload manifest (JSON, under the RSI state directory) |
| 44 | |
| 45 | Operator-owned. Its digest is pinned before the candidate starts and written on every observation. |
| 46 | `CanaryWorkloadManifest::load_pinned` refuses a file whose digest differs from the pin. |
| 47 | |
| 48 | ```json |
| 49 | { |
| 50 | "workload_version": 1, |
| 51 | "workload_id": "fixture-v1", |
| 52 | "protocol_version": 1, |
| 53 | "requests": [ |
| 54 | {"request_id": "req-001", "input": "ping", "expected_output": "pong", |
| 55 | "deadline_ms": 2000, "nondeterminism": "exact"} |
| 56 | ] |
| 57 | } |
| 58 | ``` |
| 59 | |
| 60 | Version 1 permits only `exact` nondeterminism: the result bytes must equal the expected output. |
| 61 | |
| 62 | ## Wire protocol (version 1) |
| 63 | |
| 64 | JSON lines over the generation's Unix socket, the existing `IpcConnection` framing. |
| 65 | |
| 66 | 1. The supervisor binds the generation socket `<rsi_root>/workers/<generation_id>.sock` before |
| 67 | it spawns the worker, and passes `--socket /ipc/<generation_id>.sock` inside the worker jail. |
| 68 | 2. The worker connects and sends `Ready { generation_id, pid, auth_token, protocol_version: 1 }`. |
| 69 | 3. The supervisor sends `AuthChallenge { nonce }`; the worker answers |
| 70 | `AuthResponse { response: HMAC-SHA256(secret, "<generation_id>:<nonce>") }`. |
| 71 | 4. On success the connection is `Authenticated(generation_id, pid, protocol_version)` and the |
| 72 | supervisor replies `Ping`. Readiness means exactly this, completed within the readiness |
| 73 | deadline. A bare connection, silence, an older `Ready` without a protocol version, or an |
| 74 | unknown reply is not ready. |
| 75 | 5. For each request in the pinned workload, in order, the supervisor sends |
| 76 | `ExecuteRequest { protocol_version, generation_id, request_id, request_digest, |
| 77 | workload_digest, input, deadline_ms }`, starts its own clock, and waits up to `deadline_ms` |
| 78 | for `ExecuteResult { protocol_version, generation_id, request_id, output, worker_latency_us }`. |
| 79 | 6. The supervisor hashes `output`, compares it with the expected output, and records one |
| 80 | observation per issued request. `worker_latency_us` and every `CanaryReport` are |
| 81 | diagnostics; they never become observations. |
| 82 | |
| 83 | Worker containment requires `/usr/bin/bwrap`, `/usr/bin/systemd-run`, a reachable systemd |
| 84 | user manager, cgroup v2 memory/pids/CPU controllers, and unprivileged user namespaces. Missing |
| 85 | support refuses the canary. A transient user scope enforces `MemoryMax` from |
| 86 | `memory_limit_mb` (systemd `M` units), `MemorySwapMax=0`, `TasksMax=256`, and the default |
| 87 | `SandboxLimits` CPU quota across the entire worker tree, including tmpfs and memfd pages. |
| 88 | No persistent unit or service configuration is |
| 89 | installed. Sampled descendant RSS remains an additional early-revert check, not a hard limit. |
| 90 | |
| 91 | The worker runs as uid/gid 65534 in a new user namespace, with no capabilities and separate |
| 92 | mount, PID, IPC, UTS and network namespaces. Nested user namespaces are disabled and asserted |
| 93 | disabled. It sees only the read-only executable at `/worker`, read-only system runtime |
| 94 | directories, private `/tmp` and `/scratch`, and its single bound Unix socket. The root mount |
| 95 | is read-only. The generation, workload expectations, logs, ledger and operator home are not |
| 96 | mounted. Killing the tracked launcher tears down the namespace worker and descendants through |
| 97 | bubblewrap's `--die-with-parent` chain. |
| 98 | |
| 99 | The launcher and worker environments are cleared. `PATH=/usr/bin:/bin`, `HOME=/tmp` and |
| 100 | `TMPDIR=/tmp` are fixed jail values. Only locale/diagnostic variables, the `RSI_CANARY_*` |
| 101 | namespace and explicitly supplied per-run values are restored. `SUPERVISOR_SECRET` is a fresh |
| 102 | run secret, never the operator's long-lived supervisor secret. Namespace uid isolation is not |
| 103 | a separate persistent host account, and this host sandbox is not hardware qualification. |
| 104 | |
| 105 | The exercised host dependencies for this containment change are bubblewrap 0.9.0 and systemd |
| 106 | 255 (255.4-1ubuntu8.17); the Rust/Cargo compiler is 1.98.1. These are explicit host dependencies, |
| 107 | not an Omega implementation. Their Omega replacement must supply checked namespace/mount |
| 108 | construction, cgroup limits and whole-tree teardown before this path can be replaced. |
| 109 | |
| 110 | Rejections (no observation is created, the event is logged as a rejection, no rollback): |
| 111 | a message on an unauthenticated connection, a message naming another generation, an |
| 112 | `ExecuteResult` for a request that was not issued or was already answered. A result for the |
| 113 | issued request with the wrong protocol version or generation is an `InvalidResponse` |
| 114 | observation. A missing result at the deadline is a `Timeout` observation. The connection closing |
| 115 | first is a `ConnectionClosed` observation. Every non-success observation is an early safety |
| 116 | revert in the supervisor and makes the generation unpromotable. |
| 117 | |
| 118 | ## Observation record (version 2) |
| 119 | |
| 120 | ```json |
| 121 | {"record_version":2,"observation_id":"<hex>","generation_id":"gen-...","request_id":"req-001", |
| 122 | "request_digest":"<hex>","workload_digest":"<hex>","candidate_executable_digest":"<hex>", |
| 123 | "result_digest":"<hex or null>","elapsed_us":1234,"observed_at_ms":1700000000000, |
| 124 | "status":"verified_success","provenance":"measured"} |
| 125 | ``` |
| 126 | |
| 127 | `status` is one of `verified_success`, `wrong_result`, `timeout`, `invalid_response`, |
| 128 | `connection_closed`. `provenance` is `measured` or `synthetic`; synthetic never counts. |
| 129 | |
| 130 | ## Observation log |
| 131 | |
| 132 | `<rsi_root>/canary/observations/<generation_id>.jsonl`, one canonical record per line, each |
| 133 | `{"kind": ..., "record": {...}}` with kinds `header`, `observation`, `rejection`, `seal`. The |
| 134 | header binds the generation id, workload digest and candidate executable digest; every |
| 135 | observation must carry the same three. Each append is synced before it is acknowledged. The |
| 136 | store refuses a known observation id unless the record is byte-identical (idempotent retry); a |
| 137 | different record under a known id is a conflict. A partial trailing record is dropped on reopen. |
| 138 | The seal commits the observation count, the qualifying count and the digest of every byte before |
| 139 | it. Nothing may follow the seal. Only a sealed, verified log (`SealedObservationLog`) can be |
| 140 | handed to the quota validator. |
| 141 | |
| 142 | ## Candidate identity chain |
| 143 | |
| 144 | `CandidateIdentity { patch_digest, tree_digest, executable_digest, generation_id }`. The |
| 145 | supervisor re-hashes the executable right before launching (`verify_executable`) and refuses |
| 146 | anything else. The quota validator compares the executable digest, never the patch digest. |
| 147 | |
| 148 | ## Hand-off to the quota validator |
| 149 | |
| 150 | `canary::admit_sealed_quota(sealed, identity, workload, target)` is the single authority on |
| 151 | evidence sufficiency. It refuses: a target of zero; a log for another generation, executable or |
| 152 | workload; zero observations; any synthetic observation; any non-success observation; a request |
| 153 | not in the workload; a request digest that does not match the workload; a result digest that does |
| 154 | not equal the expected outcome; an elapsed time over the request deadline; and fewer unique |
| 155 | qualifying observations than the target. Version 1 observations |
| 156 | (`crate::canary_observation`) are history: the version 2 path never reads them. |
| 157 | |
| 158 | Sequence in the engine (plan, M2 Admission): Collect -> Seal -> Verify -> Quota admission -> |
| 159 | Safety envelope -> Judge / promotion gate -> Durable. The sealed log digest is committed to the |
| 160 | RSI ledger before admission. The safety envelope's reference-backend evaluation is recorded as |
| 161 | such; it does not establish production safety. |
| 162 | |
| 163 | ## What this foundation does not do |
| 164 | |
| 165 | It defines and freezes formats. It does not launch workers, drive requests, enforce per-connection |
| 166 | authentication, instrument stages, or wire anything into `run_cycle`. Those are steps 2 to 5 of |
| 167 | the plan and must use these definitions unchanged. |