interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

GitHub aien-dev/aienux#241 (code only)

closed derived 2983cf714a6103a1 #241

Imported from GitHub aien-dev/aienux#241 (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 b52f508e9234067403f76a424350e43b7a8164f7 · Head 885ec4f53fc083de0db66dc3c9c29af5ff4622fd

Apply it

git checkout -b patch-2983cf714a6103a1 b52f508e9234067403f76a424350e43b7a8164f7
curl -fsSL https://interplane.io/drakestapleton/aienux/patches/2983cf714a6103a1.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/aienux
241
885ec4f53fc083de0db66dc3c9c29af5ff4622fd
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":241,"url":"https://github.com/aien-dev/aienux/pull/241","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:02Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/native/kernel/CONTINUITY_RECOVERY_CONTRACT.md b/native/kernel/CONTINUITY_RECOVERY_CONTRACT.md
index ed9b596a..526d2e95 100644
--- a/native/kernel/CONTINUITY_RECOVERY_CONTRACT.md
+++ b/native/kernel/CONTINUITY_RECOVERY_CONTRACT.md
@@ -464,12 +464,26 @@ main design point of this contract.
   challenge (section 1.7) mean the first two units of the **Store region** in
   both. PROPOSED: the C challenge does not also bind the anchor record; adding it
   would make C challenges differ from Rust ones and needs a decision.
-- **K-7 Mode selection.** The Rust qualification picks a mode from a control
-  block on the NVMe disk (store_qual.rs:27-48; store-tool `cfg`,
+- **K-7 Mode selection. DECIDED (cut 6).** The Rust qualification picks a mode
+  from a control block on the NVMe disk (store_qual.rs:27-48; store-tool `cfg`,
   crates/aienos-store-tool/src/main.rs:108-122; response at bytes 16..48). The C
-  disk layout has no control block. PROPOSED: a TEST-only C mechanism with the
-  same mode numbers (5-11), refused in `CK_HARDWARE_STAGING`. Its exact form is
-  left to the first wiring cut.
+  disk layout has no control block and none is added. Decision: a TEST-only
+  build flag `CK_TEST_CONTINUITY=1` builds a separate image
+  (`full-test-continuity`), using the same pattern as `CK_TEST_STORE_CRASH`. The
+  host writes one ASCII plan line
+  `AIENCONT v1 mode=<N>[ cp=<checkpoint>][ response=<64 hex>]` into the last
+  4096-byte unit of the AIENOS partition (the read-write probe scratch unit, which
+  the devices stage restores byte for byte). Modes use the Rust numbers: 5
+  provision, 6 resume, 7 resume + remember, 8 resume + commit with crash point
+  `cp` (halts at the C checkpoint name, section 5.2), 9 Recovery inspect, 10
+  inspect + repair-degraded-peer, 11 inspect + provision-identity; mode 1 (or no
+  plan) is the ordinary Store boot. For modes 5-11 the continuity or Recovery
+  boot runs INSTEAD of the boot-record commit, so read-only rows leave the image
+  byte-identical. The build refuses the flag with `CK_HARDWARE_STAGING`,
+  `CK_QEMU_UNSAFE_DMA` and `CK_TEST_STORE_CRASH`; the default image carries none
+  of the hook code or strings (`cont_check` in the Makefile). The TEST operator
+  key (0x0f x32) exists only in that image and is announced
+  `RECOVERY_OPERATOR_KEY: TEST-ONLY`. Code: `svc/continuity_boot.{c,h}`.
 
 ---
 
diff --git a/native/kernel/Makefile b/native/kernel/Makefile
index ede3f881..ae4341d4 100644
--- a/native/kernel/Makefile
+++ b/native/kernel/Makefile
@@ -206,6 +206,55 @@ $(error CK_TEST_STORE_CRASH_MUTANT must be skip_root_flush or accept_bad_root_cr
 endif
 endif
 
+
+# TEST-ONLY continuity / Recovery Core mode selection (svc/continuity_boot.h,
+# contract K-7; only scripts/qemu_ck_continuity_test.sh and
+# scripts/qemu_ck_recovery_test.sh set it): the Store stage reads a mode plan
+# from the scratch unit and runs the continuity or Recovery Core wiring instead
+# of the boot-record commit. Own OUT subdirectory; announces itself; refused
+# with CK_HARDWARE_STAGING, CK_QEMU_UNSAFE_DMA and CK_TEST_STORE_CRASH.
+# CK_TEST_CONTINUITY_MUTANT (only with it) builds a deliberately broken
+# continuity layer the gates must catch:
+#   resume_no_commit             resume does not commit the new incarnation
+#   resume_provisions            resume mints an identity on an unprovisioned Store
+#   degraded_is_unprovisioned    a degraded mount reads as unprovisioned (MR-5)
+#   compare_16                   operator response compared on 16 of 32 bytes
+#   bare_sha256                  operator response is a bare SHA-256, not an HMAC
+CK_TEST_CONTINUITY ?=
+CK_TEST_CONTINUITY_MUTANT ?=
+ifneq ($(strip $(CK_TEST_CONTINUITY)),)
+ifneq ($(strip $(CK_TEST_CONTINUITY)),1)
+$(error CK_TEST_CONTINUITY must be empty or 1)
+endif
+ifneq ($(strip $(CK_HARDWARE_STAGING)),)
+$(error CK_TEST_CONTINUITY (TEST-ONLY continuity mode selection) cannot be combined with CK_HARDWARE_STAGING)
+endif
+ifneq ($(strip $(CK_QEMU_UNSAFE_DMA)),)
+$(error CK_TEST_CONTINUITY (TEST-ONLY continuity mode selection) cannot be combined with CK_QEMU_UNSAFE_DMA)
+endif
+ifneq ($(strip $(CK_TEST_STORE_CRASH)),)
+$(error CK_TEST_CONTINUITY (TEST-ONLY continuity mode selection) cannot be combined with CK_TEST_STORE_CRASH)
+endif
+endif
+ifneq ($(strip $(CK_TEST_CONTINUITY_MUTANT)),)
+ifneq ($(strip $(CK_TEST_CONTINUITY)),1)
+$(error CK_TEST_CONTINUITY_MUTANT needs CK_TEST_CONTINUITY=1 (TEST-ONLY))
+endif
+ifeq ($(strip $(CK_TEST_CONTINUITY_MUTANT)),resume_no_commit)
+CONT_MUTANT_FLAGS := -DCM_MUTANT_RESUME_NO_COMMIT=1
+else ifeq ($(strip $(CK_TEST_CONTINUITY_MUTANT)),resume_provisions)
+CONT_MUTANT_FLAGS := -DCM_MUTANT_RESUME_PROVISIONS=1
+else ifeq ($(strip $(CK_TEST_CONTINUITY_MUTANT)),degraded_is_unprovisioned)
+CONT_MUTANT_FLAGS := -DRC_MUTANT_DEGRADED_IS_UNPROVISIONED=1
+else ifeq ($(strip $(CK_TEST_CONTINUITY_MUTANT)),compare_16)
+CONT_MUTANT_FLAGS := -DCR_MUTANT_COMPARE_16=1
+else ifeq ($(strip $(CK_TEST_CONTINUITY_MUTANT)),bare_sha256)
+CONT_MUTANT_FLAGS := -DCR_MUTANT_BARE_SHA256=1
+else
+$(error CK_TEST_CONTINUITY_MUTANT must be resume_no_commit, resume_provisions, degraded_is_unprovisioned, compare_16 or bare_sha256)
+endif
+endif
+
 # Owner provisioning. The hardware staging image never embeds the TEST Store
 # keys, the TEST store uuid or the TEST machine id: it needs owner PUBLIC
 # material and the machine identity as files (format: tools/ck_owner_gen.c;
@@ -334,6 +383,19 @@ FULL_NAME := full-test-store-crash
 endif
 STAGE_DMA_FLAGS += -DCK_TEST_STORE_CRASH=1 $(STORE_MUTANT_FLAGS)
 endif
+ifeq ($(strip $(CK_TEST_CONTINUITY)),1)
+# Own OUT subdirectory: TEST objects never mix with a default image. The
+# continuity layer is linked only here (default images carry none of it).
+ifneq ($(strip $(CK_TEST_CONTINUITY_MUTANT)),)
+FULL_NAME := full-test-continuity-mutant-$(strip $(CK_TEST_CONTINUITY_MUTANT))
+else
+FULL_NAME := full-test-continuity
+endif
+STAGE_DMA_FLAGS += -DCK_TEST_CONTINUITY=1 $(CONT_MUTANT_FLAGS) -I$(KDIR)/core
+STAGE_SRCS += $(KDIR)/svc/continuity_codec.c $(KDIR)/svc/continuity_resolve.c \
+	$(KDIR)/svc/continuity_resolve_sealed.c $(KDIR)/svc/continuity_commit.c $(KDIR)/svc/continuity_recovery.c \
+	$(KDIR)/svc/continuity_boot.c
+endif
 ifeq ($(strip $(CK_TEST_DISK_XLATE_BYPASS)),1)
 FULL_NAME := $(FULL_NAME)-test-xlate-bypass
 STAGE_DMA_FLAGS += -DCK_TEST_DISK_XLATE_BYPASS=1
@@ -454,6 +516,19 @@ define crash_check
 	fi
 endef
 
+# cont_check(elf): a full image built without CK_TEST_CONTINUITY must carry no
+# continuity wiring code or strings; a TEST continuity image must announce itself.
+define cont_check
+	@if [ "$(strip $(CK_TEST_CONTINUITY))" = 1 ]; then \
+	  grep -aqF "TEST-ONLY continuity mode selection image" $(1) || { echo "TEST continuity image lacks its TEST-ONLY announcement"; rm -f $(1); exit 1; }; \
+	else \
+	  s=$$(grep -aoE "AIENCONT|RECOVERY_OPERATOR_KEY|RECOVERY_CORE:|CONTINUITY: |TEST-ONLY MUTANT" $(1) | sort -u | tr "\n" " "); \
+	  y=$$($(KNM) $(1) | grep -E " (ck_cont_boot_stage|cr_resume|cr_provision|rc_inspect)$$" | tr "\n" " "); \
+	  if [ -n "$$s$$y" ]; then echo "default image carries the TEST continuity wiring: $$s$$y"; rm -f $(1); exit 1; fi; \
+	  echo "no continuity wiring code in $(1)"; \
+	fi
+endef
+
 # link_image(output elf, objects): refuses undefined symbols (weak stage
 # references excepted) and any relocation other than R_AARCH64_RELATIVE.
 define link_image
@@ -488,6 +563,7 @@ $(FULL_ELF): $(CORE_OBJS) $(STAGE_BUILT) $(BOOT)/image.ld
 	$(call xhci_mut_check,$@)
 	$(call smp_check,$@)
 	$(call crash_check,$@)
+	$(call cont_check,$@)
 
 $(EFI): $(ELF)
 	$(call pe_image,$<,$@)
@@ -747,6 +823,18 @@ $(STORE_IMG_TOOL): $(STORE_IMG_SRCS) $(STAGE_HDRS)
 	$(HOSTCC) $(STAGE_HOST_FLAGS) $(STAGE_INC) -o $@ $(STORE_IMG_SRCS)
 store-image: $(STORE_IMG_TOOL)
 
+# Continuity / Recovery Core QEMU gate tool (scripts/qemu_ck_continuity_test.sh,
+# scripts/qemu_ck_recovery_test.sh): operator response, degraded-peer injection,
+# AgentRoot corruption on a boot-disk image. Hosted; never part of an image.
+CONT_TOOL := $(OUT)/host/ck_cont_tool
+CONT_TOOL_SRCS := $(STAGE_OWN_SRCS) $(STAGE_NATIVE_SRCS) $(NATIVE)/disk/disk_file.c \
+	$(KDIR)/svc/tests/ck_host.c $(KDIR)/svc/continuity_resolve.c $(KDIR)/svc/continuity_codec.c \
+	$(KDIR)/tools/ck_cont_tool.c
+$(CONT_TOOL): $(CONT_TOOL_SRCS) $(STAGE_HDRS)
+	@mkdir -p $(dir $@)
+	$(HOSTCC) $(STAGE_HOST_FLAGS) $(STAGE_INC) -o $@ $(CONT_TOOL_SRCS)
+cont-tool: $(CONT_TOOL)
+
 # GPT boot-disk image tool (QEMU gates; hosted, never part of an image).
 GPT_IMG_TOOL := $(OUT)/host/ck_gpt_image
 GPT_IMG_SRCS := $(KDIR)/dev/disk_part.c $(NATIVE)/disk/disk.c $(NATIVE)/disk/disk_file.c \
@@ -773,4 +861,4 @@ clean:
 	rm -rf $(OUT)
 
 FORCE:
-.PHONY: all full test sanitize handoff-mutant clean print-kcflags udp-echo tool store-image gpt-image owner-gen continuity-mutants continuity-resolve-mutants FORCE
+.PHONY: all full test sanitize handoff-mutant clean print-kcflags udp-echo tool store-image gpt-image cont-tool owner-gen continuity-mutants continuity-resolve-mutants FORCE
diff --git a/native/kernel/svc/compat/stdio.h b/native/kernel/svc/compat/stdio.h
index e14e0dfa..4bd006bc 100644
--- a/native/kernel/svc/compat/stdio.h
+++ b/native/kernel/svc/compat/stdio.h
@@ -9,4 +9,8 @@ int ck_compat_fclose(FILE *f);
 #define fopen(p, m) ck_compat_fopen((p), (m))
 #define fread(b, s, n, f) ck_compat_fread((b), (s), (n), (f))
 #define fclose(f) ck_compat_fclose(f)
+/* snprintf for the TEST-ONLY continuity wiring (svc/continuity_recovery.c): the kernel's own formatter, core/fmt.c. */
+#include <stdarg.h>
+int ck_snprintf(char *buf, size_t n, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
+#define snprintf ck_snprintf
 #endif
diff --git a/native/kernel/svc/continuity_boot.c b/native/kernel/svc/continuity_boot.c
new file mode 100644
index 00000000..1761f335
--- /dev/null
+++ b/native/kernel/svc/continuity_boot.c
@@ -0,0 +1,452 @@
+/* continuity_boot.c -- see continuity_boot.h. TEST-ONLY; empty unless
+ * CK_TEST_CONTINUITY=1 (make full CK_TEST_CONTINUITY=1).
+ *
+ * Marker text is produced by cr_marker_* / rc_marker_* (contract section 3);
+ * this file only chooses what to run and prints. Oracle: the Rust
+ * continuity_phase and recovery_phase in crates/aienos-boot/src/nvme_read.rs
+ * :1100-1400. */
+#include "continuity_boot.h"
+
+#ifdef CK_TEST_CONTINUITY
+#ifdef CK_HARDWARE_STAGING
+#error "CK_TEST_CONTINUITY (TEST-ONLY continuity mode selection) cannot be combined with CK_HARDWARE_STAGING"
+#endif
+#ifdef CK_QEMU_UNSAFE_DMA
+#error "CK_TEST_CONTINUITY cannot be combined with CK_QEMU_UNSAFE_DMA"
+#endif
+#ifdef CK_TEST_STORE_CRASH
+#error "CK_TEST_CONTINUITY cannot be combined with CK_TEST_STORE_CRASH"
+#endif
+#include <stddef.h>
+#include <string.h>
+
+#include "ck.h"
+#include "continuity_recovery.h"
+#include "disk_layout.h"
+#include "entropy.h"
+#include "fmt.h"
+#include "m5.h"
+#include "store_boot.h"
+
+#define PLAN_MAGIC "AIENCONT v1 "
+
+/* TEST operator key of the Rust qualification (store_qual.rs:59-63). */
+static const uint8_t k_test_operator_key[32] = {
+    0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f,
+    0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f};
+
+struct plan {
+    int mode;
+    int cp; /* -1 none */
+    int have_response;
+    uint8_t response[32];
+};
+
+static const int k_cps[] = {ST_CP_BEFORE_FIRST_WRITE, ST_CP_AFTER_PAYLOAD_OBJECTS, ST_CP_AFTER_CATALOG,
+                            ST_CP_AFTER_COMMIT_RECORD, ST_CP_AFTER_FIRST_FLUSH, ST_CP_AFTER_INACTIVE_SUPERBLOCK,
+                            ST_CP_AFTER_FINAL_FLUSH, SS_CP_BEFORE_ANCHOR, SS_CP_AFTER_ANCHOR};
+
+static const char *cp_name(int cp)
+{
+    if (cp == SS_CP_BEFORE_ANCHOR) return "before_anchor";
+    if (cp == SS_CP_AFTER_ANCHOR) return "after_anchor";
+    return st_checkpoint_name(cp);
+}
+
+static size_t str_len(const char *s)
+{
+    size_t n = 0;
+    while (s[n]) n++;
+    return n;
+}
+
+static int hexv(char c)
+{
+    if (c >= '0' && c <= '9') return c - '0';
+    if (c >= 'a' && c <= 'f') return c - 'a' + 10;
+    return -1;
+}
+
+/* Parses "AIENCONT v1 mode=N[ cp=NAME][ response=64hex]\n" (nothing but zeros
+ * after the newline). 0 ok, -1 malformed. */
+static int parse_plan(const char *p, size_t len, struct plan *out)
+{
+    size_t i = sizeof PLAN_MAGIC - 1;
+    memset(out, 0, sizeof *out);
+    out->cp = -1;
+    int have_mode = 0;
+    while (i < len && p[i] != '\n') {
+        size_t s = i;
+        while (i < len && p[i] != ' ' && p[i] != '\n') i++;
+        size_t n = i - s;
+        const char *w = p + s;
+        if (n > 5 && !memcmp(w, "mode=", 5)) {
+            int v = 0;
+            for (size_t k = 5; k < n; k++) {
+                if (w[k] < '0' || w[k] > '9' || v > 100) return -1;
+                v = v * 10 + (w[k] - '0');
+            }
+            out->mode = v;
+            have_mode = 1;
+        } else if (n > 3 && !memcmp(w, "cp=", 3)) {
+            for (size_t k = 0; k < sizeof k_cps / sizeof k_cps[0]; k++) {
+                const char *nm = cp_name(k_cps[k]);
+                if (str_len(nm) == n - 3 && !memcmp(w + 3, nm, n - 3)) out->cp = k_cps[k];
+            }
+            if (out->cp < 0) return -1;
+        } else if (n == 9 + 64 && !memcmp(w, "response=", 9)) {
+            for (size_t k = 0; k < 32; k++) {
+                int hi = hexv(w[9 + 2 * k]), lo = hexv(w[10 + 2 * k]);
+                if (hi < 0 || lo < 0) return -1;
+                out->response[k] = (uint8_t)(hi << 4 | lo);
+            }
+            out->have_response = 1;
+        } else {
+            return -1;
+        }
+        if (i < len && p[i] == ' ') i++;
+    }
+    if (!have_mode || i >= len) return -1;
+    for (i = i + 1; i < len; i++)
+        if (p[i]) return -1;
+    return 0;
+}
+
+/* ---- the entropy source: the kernel's RNDR or a refusal, no fallback ---- */
+static int ent_present(void *ctx)
+{
+    (void)ctx;
+    return ck_entropy_status() == CK_RNG_OK;
+}
+static int ent_read(void *ctx, uint64_t *out)
+{
+    (void)ctx;
+    uint64_t w = 0;
+    if (ck_entropy_fill(&w, sizeof w) != 0) return 0;
+    *out = w;
+    return 1;
+}
+static const struct ck_rng_ops k_ent_ops = {ent_present, ent_read, 0};
+
+/* ---- workspace (heap, once) ---- */
+static struct {
+    ss_workspace *ws;
+    ss_store *s;
+    struct cr_work *w;
+    struct cr_txwork *tw;
+    struct cr_view *v, *v2;
+    struct cc_state *st;
+    st_disk sd;
+    st_dev sdev;
+    ts_device tdev;
+    ss_keys keys;
+    uint64_t store_base_lba, store_units;
+    uint32_t bpu;
+} g;
+
+static int alloc_all(void)
+{
+    g.ws = ck_alloc(sizeof *g.ws);
+    g.s = ck_alloc(sizeof *g.s);
+    g.w = ck_alloc(sizeof *g.w);
+    g.tw = ck_alloc(sizeof *g.tw);
+    g.v = ck_alloc(sizeof *g.v);
+    g.v2 = ck_alloc(sizeof *g.v2);
+    g.st = ck_alloc(sizeof *g.st);
+    return g.ws && g.s && g.w && g.tw && g.v && g.v2 && g.st ? 0 : -1;
+}
+
+static void line(const char *s)
+{
+    ck_puts(s);
+    ck_puts("\n");
+}
+
+static void print_view(const char *word, const struct cr_view *v)
+{
+    char b[256];
+    if (cr_marker_view(b, sizeof b, word, v)) line(b);
+}
+static void print_outcome(int outcome, const char *why, int store_rc)
+{
+    char b[256];
+    if (cr_marker_outcome(b, sizeof b, outcome, why, store_rc)) line(b);
+}
+
+static void hex_into(char *o, const uint8_t *b, size_t n)
+{
+    static const char hx[] = "0123456789abcdef";
+    for (size_t i = 0; i < n; i++) {
+        o[2 * i] = hx[b[i] >> 4];
+        o[2 * i + 1] = hx[b[i] & 15];
+    }
+    o[2 * n] = 0;
+}
+
+/* Observation hook for mode 8: print every checkpoint; halt at the planned one
+ * (the host kills QEMU on seeing the marker; the halt only keeps the guest from
+ * going on). Nothing is flushed or written here. */
+static void cont_hook(void *arg, int cp)
+{
+    const struct plan *p = arg;
+    ck_printf("CHECKPOINT: %s\n", cp_name(cp));
+    if (p->cp == cp) {
+        ck_printf("continuity: HALT at %s (the host kills QEMU now)\n", cp_name(cp));
+        for (;;) __asm__ volatile("wfi");
+    }
+}
+
+static int is_blank(const disk_dev *d, uint8_t *buf)
+{
+    for (uint64_t u = 0; u < CK_LAYOUT_ANCHOR_UNITS + 2u; u++) {
+        if (disk_read(d, u * g.bpu, g.bpu, buf)) return 0;
+        for (uint32_t i = 0; i < CK_LAYOUT_UNIT; i++)
+            if (buf[i]) return 0;
+    }
+    return 1;
+}
+
+static void fact(struct cc_record *r, const char *s)
+{
+    memset(r, 0, sizeof *r);
+    r->status = CC_EPI_DIRECT_OBSERVATION;
+    r->len = (uint16_t)str_len(s);
+    r->statement = (const uint8_t *)s;
+}
+
+static void recovery_record_line(const struct rc_record *r)
+{
+    char b[400];
+    size_t n = 0;
+    n += ck_snprintf(b + n, sizeof b - n, "RECOVERY_RECORD:");
+    for (int i = 0; i < 2; i++) {
+        switch (r->slot[i]) {
+        case RC_SLOT_ZERO: n += ck_snprintf(b + n, sizeof b - n, " slot%d=zero", i); break;
+        case RC_SLOT_SUPERBLOCK:
+            n += ck_snprintf(b + n, sizeof b - n, " slot%d=gen%llu", i, (unsigned long long)r->slot_generation[i]);
+            break;
+        case RC_SLOT_UNDECODABLE: n += ck_snprintf(b + n, sizeof b - n, " slot%d=undecodable", i); break;
+        default: n += ck_snprintf(b + n, sizeof b - n, " slot%d=read-error", i); break;
+        }
+    }
+    if (r->have_mount)
+        n += ck_snprintf(b + n, sizeof b - n, " mount=%s peer=%s generation=%llu",
+                         r->mount_state == ST_VALID ? "Valid" : "DegradedRecovery", rc_peer_name(r->peer),
+                         (unsigned long long)r->generation);
+    if (r->have_catalog)
+        n += ck_snprintf(b + n, sizeof b - n, " roots=%u manifests=%u other=%u", r->n_roots, r->n_manifests, r->n_other);
+    n += ck_snprintf(b + n, sizeof b - n, " agent=");
+    if (r->have_identity)
+        hex_into(b + n, r->agent_id, 32);
+    else
+        ck_snprintf(b + n, sizeof b - n, "none");
+    line(b);
+}
+
+static void recovery_phase(const disk_dev *d, const struct plan *p)
+{
+    (void)d;
+    line("RECOVERY_OPERATOR_KEY: TEST-ONLY");
+    struct rc_env e;
+    memset(&e, 0, sizeof e);
+    e.dev = &g.sdev;
+    e.anchor = &g.tdev;
+    e.anchor_lba = 0;
+    e.keys = &g.keys;
+    e.ws = g.ws;
+    e.store = g.s;
+    e.w = g.w;
+    e.tw = g.tw;
+    e.view = g.v2;
+    static struct rc_record rec, after;
+    if (rc_inspect(&e, &rec) != 0) {
+        line("RECOVERY_REFUSED (Io)");
+        return;
+    }
+    recovery_record_line(&rec);
+    char b[256];
+    if (rc_marker_entry(b, sizeof b, &rec)) line(b);
+    int action = rc_applicable(&rec);
+    if (action && rc_marker_challenge(b, sizeof b, &rec, (uint8_t)action)) line(b);
+    if (p->mode == 9) return;
+
+    int rc, cro = 0, srr = 0;
+    const char *why = 0;
+    uint8_t agent[32];
+    int have_agent = 0;
+    if (p->mode == 10) {
+        rc = rc_repair_degraded_peer(&e, k_test_operator_key, p->response, &after);
+    } else {
+        struct ck_rng rng;
+        memset(&rng, 0, sizeof rng);
+        if (ck_rng_probe(&rng, &k_ent_ops) != CK_RNG_OK) {
+            line("RECOVERY_REFUSED (NoEntropy)");
+            return;
+        }
+        rc = rc_provision_identity(&e, k_test_operator_key, p->response, &rng, g.v, &cro, &why, &srr);
+        if (rc == RC_OK) {
+            memcpy(agent, g.v->root.agent_id, 32);
+            have_agent = 1;
+        }
+    }
+    if (rc == RC_OK) {
+        uint8_t a = p->mode == 10 ? CR_ACTION_REPAIR_DEGRADED_PEER : CR_ACTION_PROVISION_IDENTITY;
+        if (rc_marker_done(b, sizeof b, a, have_agent ? agent : 0)) line(b);
+    } else if (rc_marker_refused(b, sizeof b, rc, cro)) {
+        line(b);
+    }
+}
+
+static void continuity_phase(const disk_dev *d, const struct plan *p)
+{
+    struct cr_source src;
+    struct cr_sink snk;
+    struct cr_sealed_sink sk = {g.s, 0, 0};
+    cr_bind_sealed(&src, g.s);
+    cr_bind_sealed_sink(&snk, &sk);
+    (void)d;
+    const char *why = 0;
+    int store_rc = 0;
+    struct ck_rng rng;
+
+    if (p->mode == 5) {
+        memset(&rng, 0, sizeof rng);
+        if (ck_rng_probe(&rng, &k_ent_ops) != CK_RNG_OK) {
+            line("CONTINUITY: NO_ENTROPY");
+            return;
+        }
+        int o = cr_provision(&src, &snk, g.w, g.tw, &rng, ck_store_test_uuid, CC_SOURCE_QUALIFICATION, g.v, &why,
+                             &store_rc);
+        if (o == CR_RESOLVED) print_view("PROVISIONED", g.v);
+        else print_outcome(o, why, store_rc);
+        return;
+    }
+
+    int committed = 0;
+    int o = cr_resume(&src, &snk, g.w, g.tw, g.v, &committed, &why, &store_rc);
+    if (o != CR_RESOLVED) {
+        print_outcome(o, why, store_rc);
+        return;
+    }
+    if (!committed) {
+        print_view("RESUMED_READONLY", g.v);
+        return;
+    }
+    print_view("RESUMED", g.v);
+
+    struct cc_record rec;
+    if (p->mode == 7) {
+        memcpy(g.st, &g.v->state, sizeof *g.st);
+        uint8_t child[32];
+        if (cc_state_fork(g.st, g.v->root.root_branch, child, 0) != CC_OK) {
+            line("CONTINUITY: STOP (fork)");
+            return;
+        }
+        fact(&rec, "qemu: the operator asked AIEN to remember this");
+        struct cr_update up = {g.st, &rec, 1};
+        o = cr_commit(&src, &snk, g.w, g.tw, g.v, &up, 0, g.v2, &why, &store_rc);
+        if (o == CR_RESOLVED) print_view("REMEMBERED", g.v2);
+        else print_outcome(o, why, store_rc);
+    } else if (p->mode == 8) {
+        sk.hook = cont_hook;
+        sk.hook_arg = (void *)p;
+        fact(&rec, "qemu: committed across a crash");
+        struct cr_update up = {0, &rec, 1};
+        o = cr_commit(&src, &snk, g.w, g.tw, g.v, &up, 0, g.v2, &why, &store_rc);
+        if (o == CR_RESOLVED) print_view("COMMITTED", g.v2);
+        else print_outcome(o, why, store_rc);
+    }
+}
+
+int ck_cont_boot_stage(const disk_dev *d, int *rc)
+{
+    *rc = 0;
+    ck_puts("continuity: TEST-ONLY continuity mode selection image (CK_TEST_CONTINUITY=1); "
+            "never counts toward a default boot\n");
+#ifdef CM_MUTANT_RESUME_NO_COMMIT
+    ck_puts("continuity: TEST-ONLY MUTANT resume_no_commit: resume skips the incarnation commit\n");
+#endif
+#ifdef CM_MUTANT_RESUME_PROVISIONS
+    ck_puts("continuity: TEST-ONLY MUTANT resume_provisions: resume mints an identity on an unprovisioned store\n");
+#endif
+#ifdef RC_MUTANT_DEGRADED_IS_UNPROVISIONED
+    ck_puts("continuity: TEST-ONLY MUTANT degraded_is_unprovisioned: a degraded mount reads as unprovisioned\n");
+#endif
+#ifdef CR_MUTANT_BARE_SHA256
+    ck_puts("continuity: TEST-ONLY MUTANT bare_sha256: operator response is a bare SHA-256\n");
+#endif
+    if (!d || (d->block_size != 512 && d->block_size != 4096)) return 0;
+    uint32_t bpu = CK_LAYOUT_UNIT / d->block_size;
+    uint64_t units = d->block_count / bpu;
+    uint8_t *buf = ck_alloc(CK_LAYOUT_UNIT);
+    if (!units || !buf) {
+        ck_puts("continuity: plan REFUSED (no plan buffer or no unit); Store stage runs as default\n");
+        return 0;
+    }
+    if (disk_read(d, (units - 1u) * bpu, bpu, buf)) {
+        ck_puts("continuity: plan REFUSED (read error); Store stage runs as default\n");
+        return 0;
+    }
+    if (memcmp(buf, PLAN_MAGIC, sizeof PLAN_MAGIC - 1)) {
+        ck_puts("continuity: no plan; Store stage runs as default\n");
+        return 0;
+    }
+    static struct plan p;
+    if (parse_plan((const char *)buf, CK_LAYOUT_UNIT, &p) || !(p.mode == 1 || (p.mode >= 5 && p.mode <= 11)) ||
+        (p.mode >= 10 && !p.have_response)) {
+        ck_puts("continuity: plan REFUSED (malformed); Store stage runs as default\n");
+        return 0;
+    }
+    if (p.mode == 1) {
+        ck_puts("continuity: plan mode=1 (ordinary Store boot)\n");
+        return 0;
+    }
+    ck_printf("continuity: plan mode=%d\n", p.mode);
+
+    g.bpu = bpu;
+    g.store_base_lba = (uint64_t)CK_LAYOUT_ANCHOR_UNITS * bpu;
+    g.store_units = units - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS;
+    if (alloc_all()) {
+        ck_puts("continuity: REFUSED (no workspace)\n");
+        *rc = -1;
+        return 1;
+    }
+    int r = st_disk_bind(&g.sd, d, g.store_base_lba, g.store_units, &g.sdev);
+    if (r) {
+        ck_printf("continuity: REFUSED (st_disk_bind rc=%d)\n", r);
+        *rc = r;
+        return 1;
+    }
+    ss_ts_device(d, &g.tdev);
+    ck_store_test_keys(&g.keys);
+
+    int blank = is_blank(d, g.ws->rd);
+    if (p.mode == 5 && blank) {
+        r = ss_format(&g.sdev, &g.tdev, 0, ck_store_test_uuid, &g.keys, g.ws);
+        if (r) {
+            line("CONTINUITY: STOP (genesis write)");
+            return 1;
+        }
+        blank = 0;
+    }
+    if (blank && p.mode >= 5 && p.mode <= 8) {
+        line("CONTINUITY: STOP (store Unformatted)");
+        return 1;
+    }
+    if (p.mode <= 8) {
+        memset(g.s, 0, sizeof *g.s);
+        r = ss_open(g.s, &g.sdev, &g.tdev, 0, &g.keys, g.ws);
+        if (r) {
+            ck_printf("CONTINUITY: STOP (store Refused(%d))\n", r);
+            return 1;
+        }
+        continuity_phase(d, &p);
+    } else {
+        recovery_phase(d, &p);
+    }
+    memset(&g.keys, 0, sizeof g.keys);
+    return 1;
+}
+#else
+typedef int ck_continuity_boot_not_built; /* ISO C: no empty translation unit */
+#endif
diff --git a/native/kernel/svc/continuity_boot.h b/native/kernel/svc/continuity_boot.h
new file mode 100644
index 00000000..c62b14ba
--- /dev/null
+++ b/native/kernel/svc/continuity_boot.h
@@ -0,0 +1,44 @@
+/* continuity_boot.h -- K-7 TEST-ONLY mode selection and the kernel wiring of the
+ * continuity (cut 3-4) and Recovery Core (cut 5) code into the Store stage.
+ *
+ * Compiled only into the TEST image "make full CK_TEST_CONTINUITY=1"
+ * ($(OUT)/full-test-continuity[-mutant-<name>]/). The Makefile and
+ * continuity_boot.c refuse it together with CK_HARDWARE_STAGING,
+ * CK_QEMU_UNSAFE_DMA and CK_TEST_STORE_CRASH, and every default full image is
+ * checked to carry none of its code or strings. The image announces itself on
+ * the console and never counts toward anything but M4_CONTINUITY / M4_RECOVERY.
+ *
+ * Mode selection (contract K-7, DECIDED): the host writes one ASCII line into the
+ * last 4096-byte unit of the AIENOS partition (the rw-probe scratch unit, which
+ * the devices stage restores byte for byte, dev/nvme_bind.c ck_disk_rw_probe; the
+ * same carrier as svc/store_crash.h):
+ *     AIENCONT v1 mode=<N>[ cp=<checkpoint>][ response=<64 hex>]\n
+ * mode uses the numbers of the Rust control block (store_qual.rs:27-48):
+ *     1  ordinary Store boot (formats a blank disk, commits a boot record): used by
+ *        the harness to make "a formatted Store with data, no identity"
+ *     5  provision (formats a blank disk first, as the Rust mode 5)
+ *     6  resume          7  resume + remember     8  resume + commit with a crash point
+ *     9  Recovery Core inspect
+ *     10 inspect + repair-degraded-peer with `response`
+ *     11 inspect + provision-identity with `response`
+ * cp (mode 8 only): the C checkpoint where the kernel halts after printing
+ * "CHECKPOINT: <name>" (the host then kills QEMU): before_first_write,
+ * after_payload_objects, after_catalog, after_commit_record, after_first_flush,
+ * after_inactive_superblock, after_final_flush, before_anchor, after_anchor.
+ * Without a valid plan (or mode 1) the Store stage runs exactly as the default
+ * image does. A modes 5..11 boot runs INSTEAD of the boot-record commit: it
+ * writes only what the mode's own continuity or recovery action writes.
+ *
+ * The TEST operator key (0x0f x 32) lives only in continuity_boot.c, behind
+ * CK_TEST_CONTINUITY; every recovery boot prints "RECOVERY_OPERATOR_KEY: TEST-ONLY". */
+#ifndef AIENOS_CK_CONTINUITY_BOOT_H
+#define AIENOS_CK_CONTINUITY_BOOT_H
+#include "disk.h"
+
+#ifdef CK_TEST_CONTINUITY
+/* Returns 1 when a modes 5..11 plan was found and run (the caller must not run
+ * the ordinary Store boot), else 0. *rc is 0 unless the harness itself failed. */
+int ck_cont_boot_stage(const disk_dev *d, int *rc);
+/* Last Store opened by a continuity boot (NULL if none). */
+#endif
+#endif
diff --git a/native/kernel/svc/store_boot.c b/native/kernel/svc/store_boot.c
index 60ab91b5..fac490ae 100644
--- a/native/kernel/svc/store_boot.c
+++ b/native/kernel/svc/store_boot.c
@@ -7,6 +7,7 @@
 #include "m5.h"
 #include "sha256.h"
 #include "store_crash.h"
+#include "continuity_boot.h"
 
 #ifdef CK_HARDWARE_STAGING
 /* Hardware staging image: owner provisioning generated at build time by
@@ -346,6 +347,18 @@ int ck_stage_store(void)
 #else
 #ifdef CK_TEST_STORE_CRASH
     d = ck_store_crash_setup(d);
+#endif
+#ifdef CK_TEST_CONTINUITY
+    {
+        /* K-7: a modes 5..11 plan runs the continuity / Recovery Core test
+         * boot INSTEAD of the boot-record commit (svc/continuity_boot.h). */
+        int crc = 0;
+        if (ck_cont_boot_stage(d, &crc)) {
+            ck_art_stage_load(0, "continuity test mode");
+            ck_dev_nvme_release();
+            return crc;
+        }
+    }
 #endif
     ck_store_test_keys(&keys);
     rc = store_boot_run(d, &keys, ck_store_test_uuid, commit ? commit : "unknown", &r);
diff --git a/native/kernel/tools/ck_cont_tool.c b/native/kernel/tools/ck_cont_tool.c
new file mode 100644
index 00000000..3a9d4887
--- /dev/null
+++ b/native/kernel/tools/ck_cont_tool.c
@@ -0,0 +1,178 @@
+/* ck_cont_tool -- host tool for the continuity / Recovery Core QEMU gates
+ * (scripts/qemu_ck_continuity_test.sh, scripts/qemu_ck_recovery_test.sh). It
+ * reuses the kernel's own Store and continuity code on a boot-disk image made by
+ * tools/ck_gpt_image.c (AIENOS GPT partition, TEST keys). Hosted C; test
+ * tooling, never in an image. No Python, no outside libraries.
+ *
+ *   ck_cont_tool respond KEY_HEX CHALLENGE_HEX
+ *        operator response (HMAC-SHA256, contract 1.7 / recovery.rs OperatorAuth), hex on stdout
+ *   ck_cont_tool inject-peer IMG BS
+ *        fill the INACTIVE superblock slot (Store-region unit 1 - active) with seeded garbage
+ *        (the Rust `store-tool inject ... inactive seeded_garbage`); needs a mountable Store
+ *   ck_cont_tool corrupt-root IMG BS
+ *        flip byte 20 of the first AgentRoot (kind 16) envelope (the Rust `corrupt-kind ... 16`
+ *        stand-in; contract K-3/K-4: the sealed mount refuses it)
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "continuity_resolve.h"
+#include "disk_file.h"
+#include "disk_layout.h"
+#include "disk_part.h"
+#include "store_boot.h"
+
+static int usage(void)
+{
+    fprintf(stderr, "usage: ck_cont_tool respond KEY CHALLENGE | inject-peer IMG BS | corrupt-root IMG BS\n");
+    return 2;
+}
+
+static int unhex(const char *s, uint8_t out[32])
+{
+    if (strlen(s) != 64) return -1;
+    for (int i = 0; i < 32; i++) {
+        unsigned v;
+        char t[3] = {s[2 * i], s[2 * i + 1], 0};
+        char *end;
+        v = (unsigned)strtoul(t, &end, 16);
+        if (*end) return -1;
+        out[i] = (uint8_t)v;
+    }
+    return 0;
+}
+
+typedef struct {
+    disk_file f;
+    disk_dev raw;
+    ck_part part;
+    const disk_dev *d;
+    st_disk sd;
+    st_dev sdev;
+    ts_device tdev;
+    uint64_t base_lba, units;
+    uint32_t bpu;
+} image;
+
+static int img_open(image *im, const char *path, const char *bs_s)
+{
+    uint32_t bs = (uint32_t)strtoul(bs_s, NULL, 10);
+    if (bs != 512 && bs != 4096) {
+        fprintf(stderr, "block size must be 512 or 4096\n");
+        return -1;
+    }
+    memset(im, 0, sizeof *im);
+    if (disk_file_open(&im->f, &im->raw, path, bs, 0, 0)) {
+        fprintf(stderr, "cannot open %s\n", path);
+        return -1;
+    }
+    ck_gpt_info gi;
+    int grc = ck_gpt_find_aienos(&im->raw, &im->part, &gi);
+    if (grc) {
+        fprintf(stderr, "%s: no AIENOS partition (gpt: %s, rc=%d)\n", path, ck_gpt_strerror(grc), grc);
+        disk_file_close(&im->f);
+        return -1;
+    }
+    im->d = &im->part.dev;
+    im->bpu = CK_LAYOUT_UNIT / bs;
+    uint64_t units = im->d->block_count / im->bpu;
+    im->base_lba = (uint64_t)CK_LAYOUT_ANCHOR_UNITS * im->bpu;
+    im->units = units - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS;
+    return 0;
+}
+
+static int img_mount(image *im, ss_store *s, ss_workspace *ws)
+{
+    ss_keys keys;
+    ck_store_test_keys(&keys);
+    int rc = st_disk_bind(&im->sd, im->d, im->base_lba, im->units, &im->sdev);
+    if (!rc) {
+        ss_ts_device(im->d, &im->tdev);
+        memset(s, 0, sizeof *s);
+        rc = ss_open(s, &im->sdev, &im->tdev, 0, &keys, ws);
+    }
+    memset(&keys, 0, sizeof keys);
+    if (rc) fprintf(stderr, "Store refused: rc=%d\n", rc);
+    return rc;
+}
+
+static int cmd_respond(int argc, char **argv)
+{
+    uint8_t key[32], ch[32], out[32];
+    if (argc != 4 || unhex(argv[2], key) || unhex(argv[3], ch)) return usage();
+    cr_operator_response(key, ch, out);
+    for (int i = 0; i < 32; i++) printf("%02x", out[i]);
+    printf("\n");
+    return 0;
+}
+
+static int cmd_inject_peer(int argc, char **argv)
+{
+    if (argc != 4) return usage();
+    image im;
+    if (img_open(&im, argv[2], argv[3])) return 1;
+    ss_workspace *ws = calloc(1, sizeof *ws);
+    ss_store *s = calloc(1, sizeof *s);
+    int rc = !ws || !s ? -1 : img_mount(&im, s, ws);
+    if (!rc) {
+        uint32_t active = st_active_slot(&s->st);
+        uint8_t *junk = malloc(CK_LAYOUT_UNIT);
+        for (uint32_t i = 0; i < CK_LAYOUT_UNIT; i++) junk[i] = (uint8_t)(i * 37u + 11u);
+        uint64_t lba = im.base_lba + (uint64_t)(1u - active) * im.bpu;
+        rc = disk_write(im.d, lba, im.bpu, junk);
+        if (!rc) rc = disk_flush(im.d);
+        free(junk);
+        if (!rc) printf("inject-peer: slot %u of the Store region filled (active slot %u)\n", 1u - active, active);
+    }
+    free(s);
+    free(ws);
+    disk_file_close(&im.f);
+    return rc ? 1 : 0;
+}
+
+static int cmd_corrupt_root(int argc, char **argv)
+{
+    if (argc != 4) return usage();
+    image im;
+    if (img_open(&im, argv[2], argv[3])) return 1;
+    ss_workspace *ws = calloc(1, sizeof *ws);
+    ss_store *s = calloc(1, sizeof *s);
+    int rc = !ws || !s ? -1 : img_mount(&im, s, ws), done = 0;
+    for (uint32_t k = 0; !rc && !done && k < s->nclaims; k++) {
+        if (ws->claims[k].c.obj.object_kind != 16u) continue; /* AgentRoot */
+        uint32_t n = 0;
+        const sv1_entry *cat = st_catalog(&s->st, &n);
+        for (uint32_t j = 0; j < n; j++) {
+            if (memcmp(cat[j].object_id, ws->claims[k].sid, 32)) continue;
+            uint8_t *u = malloc(CK_LAYOUT_UNIT);
+            uint64_t lba = im.base_lba + cat[j].first_unit * im.bpu;
+            rc = disk_read(im.d, lba, im.bpu, u);
+            if (!rc) {
+                u[20] ^= 0xff;
+                rc = disk_write(im.d, lba, im.bpu, u);
+            }
+            if (!rc) rc = disk_flush(im.d);
+            free(u);
+            done = 1;
+            if (!rc) printf("corrupt-root: byte 20 of the AgentRoot envelope (store unit %llu) flipped\n", (unsigned long long)cat[j].first_unit);
+            break;
+        }
+    }
+    if (!rc && !done) {
+        fprintf(stderr, "no AgentRoot envelope found\n");
+        rc = -1;
+    }
+    free(s);
+    free(ws);
+    disk_file_close(&im.f);
+    return rc ? 1 : 0;
+}
+
+int main(int argc, char **argv)
+{
+    if (argc < 2) return usage();
+    if (!strcmp(argv[1], "respond")) return cmd_respond(argc, argv);
+    if (!strcmp(argv[1], "inject-peer")) return cmd_inject_peer(argc, argv);
+    if (!strcmp(argv[1], "corrupt-root")) return cmd_corrupt_root(argc, argv);
+    return usage();
+}
diff --git a/scripts/ck_gates.sh b/scripts/ck_gates.sh
index de3c7d20..86007fa2 100755
--- a/scripts/ck_gates.sh
+++ b/scripts/ck_gates.sh
@@ -6,7 +6,8 @@
 # (scripts/qemu_ck_boot_test.sh, scripts/qemu_ck_store_test.sh,
 # scripts/qemu_ck_net_test.sh, scripts/qemu_ck_artifact_test.sh,
 # scripts/qemu_ck_smp_test.sh, scripts/qemu_ck_store_crash_test.sh,
-# scripts/qemu_ck_disk_layout_test.sh), and prints
+# scripts/qemu_ck_disk_layout_test.sh, scripts/qemu_ck_continuity_test.sh,
+# scripts/qemu_ck_recovery_test.sh), and prints
 # one line per gate:
 #     AIENOS_CK_<gate>: PASS|FAIL|NOT_RUN [(reason)]
 # for M1 M3 SMMU NVME_SHUTDOWN P2_ARTIFACT M0_ROLLBACK M4_NVME M4_STORE M4_STORE_CRASH
@@ -45,6 +46,8 @@ set -uo pipefail
 #         smp   -> verdict line from scripts/qemu_ck_smp_test.sh
 #         crash -> verdict line from scripts/qemu_ck_store_crash_test.sh
 #         disk  -> verdict line from scripts/qemu_ck_disk_layout_test.sh
+#         cont  -> verdict line from scripts/qemu_ck_continuity_test.sh
+#         recov -> verdict line from scripts/qemu_ck_recovery_test.sh
 #         missing -> NOT_RUN (MISSING_IMPLEMENTATION), never run, never PASS
 # ===========================================================================
 CK_GATE_TABLE='
@@ -57,15 +60,15 @@ M0_ROLLBACK|missing|C loader signatures, A/B, BootNext and rollback are parked (
 M4_NVME|store|-
 M4_STORE|store|-
 M4_STORE_CRASH|crash|-
-M4_CONTINUITY|missing|no continuity core (ADR 0016 agent identity and memory) in the C kernel
-M4_RECOVERY|missing|no Recovery Core (ADR 0006) in the C kernel
+M4_CONTINUITY|cont|-
+M4_RECOVERY|recov|-
 ARGUS1_REVOKE|store|-
 KEYBOARD|missing|no xHCI/USB HID keyboard driver in the C kernel
 NET|net|-
 SMP|smp|-
 DISK_LAYOUT|disk|-
 '
-CHILDREN=(boot store net artifact smp crash disk)
+CHILDREN=(boot store net artifact smp crash disk cont recov)
 # ===========================================================================
 
 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
@@ -110,6 +113,8 @@ set_default_children() {
         [smp]="${repo_root}/scripts/qemu_ck_smp_test.sh"
         [crash]="${repo_root}/scripts/qemu_ck_store_crash_test.sh"
         [disk]="${repo_root}/scripts/qemu_ck_disk_layout_test.sh"
+        [cont]="${repo_root}/scripts/qemu_ck_continuity_test.sh"
+        [recov]="${repo_root}/scripts/qemu_ck_recovery_test.sh"
     )
 }
 
@@ -343,7 +348,7 @@ self_test() {
     }
     scenario() { # NAME BOOT_SCRIPT STORE_SCRIPT [NET_SCRIPT] [ARTIFACT_SCRIPT] [SMP_SCRIPT] [CRASH_SCRIPT] [DISK_SCRIPT] (absent: missing)
         scen="$1"
-        declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}" [artifact]="${5:-${tmp}/kids/no_artifact_child.sh}" [smp]="${6:-${tmp}/kids/no_smp_child.sh}" [crash]="${7:-${tmp}/kids/no_crash_child.sh}" [disk]="${8:-${tmp}/kids/no_disk_child.sh}")
+        declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}" [artifact]="${5:-${tmp}/kids/no_artifact_child.sh}" [smp]="${6:-${tmp}/kids/no_smp_child.sh}" [crash]="${7:-${tmp}/kids/no_crash_child.sh}" [disk]="${8:-${tmp}/kids/no_disk_child.sh}" [cont]="${tmp}/kids/no_cont_child.sh" [recov]="${tmp}/kids/no_recov_child.sh")
         run_children 2>/dev/null
         evaluate_table >"${tmp}/${scen}.out"
     }
@@ -476,7 +481,7 @@ self_test() {
         jq -e '.physical == "NOT_RUN" and (.gates | length) == 16 and .verdict == "NOT_ALL_GATES_PASS"
                and ([.gates[] | select(.id == "M1")][0].verdict == "PASS")
                and ([.gates[] | select(.id == "ARGUS1_REVOKE")][0].verdict == "FAIL")
-               and (.children | length) == 7 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \
+               and (.children | length) == 9 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \
             && ok "receipt is valid JSON (jq) with expected fields" || bad "receipt JSON invalid or fields wrong"
     else
         echo "SKIP  jq not installed; JSON validity not machine-checked"
@@ -495,7 +500,7 @@ self_test() {
         [[ -n "${g}" ]] || continue
         rows=$((rows + 1))
         case "${s}" in
-            boot|store|net|artifact|smp|crash|disk) [[ "${w}" == - ]] || tbad=1 ;;
+            boot|store|net|artifact|smp|crash|disk|cont|recov) [[ "${w}" == - ]] || tbad=1 ;;
             missing) [[ -n "${w}" && "${w}" != - ]] || tbad=1 ;;
             *) tbad=1 ;;
         esac
diff --git a/scripts/lib_ck_cont_qemu.sh b/scripts/lib_ck_cont_qemu.sh
new file mode 100644
index 00000000..c882981f
--- /dev/null
+++ b/scripts/lib_ck_cont_qemu.sh
@@ -0,0 +1,229 @@
+# Shared machinery of the C kernel continuity and Recovery Core QEMU gates
+# (scripts/qemu_ck_continuity_test.sh = CK gate M4_CONTINUITY,
+# scripts/qemu_ck_recovery_test.sh = CK gate M4_RECOVERY). Sourced, not run.
+#
+# Mode selection is contract K-7 (native/kernel/CONTINUITY_RECOVERY_CONTRACT.md,
+# native/kernel/svc/continuity_boot.h): the host writes one ASCII plan line
+#     AIENCONT v1 mode=<N>[ cp=<checkpoint>][ response=<64 hex>]
+# into the last 4096-byte unit of the AIENOS partition of a 4096-byte-LBA GPT
+# image; the TEST image "make full CK_TEST_CONTINUITY=1" (refused with
+# CK_HARDWARE_STAGING / CK_QEMU_UNSAFE_DMA / CK_TEST_STORE_CRASH) runs the
+# continuity or Recovery Core wiring instead of the boot-record commit. Every
+# boot is a fresh QEMU process (cold restart); only the NVMe image persists.
+#
+# The caller defines before sourcing: gate_name (e.g. M4_CONTINUITY), script_tag,
+# and calls cq_init "$@" (parses --mutant NAME), then uses the helpers below.
+# QEMU is not hardware: a PASS qualifies nothing physical.
+
+cq_mutant=""
+cq_mode=gate
+cq_init() {
+    case "${1:-}" in
+        "") ;;
+        --mutant)
+            cq_mode=mutant; cq_mutant="${2:-}"
+            case "${cq_mutant}" in
+                resume_no_commit|resume_provisions|degraded_is_unprovisioned|compare_16|bare_sha256) ;;
+                *) echo "usage: $0 [--mutant resume_no_commit|resume_provisions|degraded_is_unprovisioned|compare_16|bare_sha256]" >&2; exit 2 ;;
+            esac ;;
+        *) echo "usage: $0 [--mutant NAME]" >&2; exit 2 ;;
+    esac
+}
+verdict() {
+    if [[ "${cq_mode}" == mutant ]]; then
+        echo "AIENOS_CK_${gate_name}_MUTANT_${cq_mutant^^}: $1"
+    else
+        echo "AIENOS_CK_${gate_name}: $1"
+    fi
+}
+
+img_bytes=67108864
+unit=4096
+plan_unit=0
+set_part() { plan_unit=$(( ($2 + 1) * $1 / unit - 1 )); }
+
+cq_build() {
+    code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
+    vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
+    command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; verdict NOT_RUN; exit 2; }
+    [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; verdict NOT_RUN; exit 2; }
+    commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
+    cross=""
+    if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
+    out="${repo_root}/target/native-kernel"
+    mk_full() { make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full "$@" >/dev/null; }
+    if [[ "${cq_mode}" == mutant ]]; then
+        mk_full CK_TEST_CONTINUITY=1 CK_TEST_CONTINUITY_MUTANT="${cq_mutant}" || { echo "mutant build failed"; verdict NOT_RUN; exit 2; }
+        test_efi="${out}/full-test-continuity-mutant-${cq_mutant}/BOOTAA64.EFI"
+        plain_efi="${test_efi}"
+    else
+        mk_full || { echo "build failed"; verdict NOT_RUN; exit 2; }
+        mk_full CK_TEST_CONTINUITY=1 || { echo "TEST continuity build failed"; verdict NOT_RUN; exit 2; }
+        test_efi="${out}/full-test-continuity/BOOTAA64.EFI"
+        plain_efi="${out}/full/BOOTAA64.EFI"
+    fi
+    make -s -C native/kernel CROSS="${cross}" OUT="${out}" gpt-image cont-tool >/dev/null || { echo "host tools build failed"; verdict NOT_RUN; exit 2; }
+    gpt_tool="${out}/host/ck_gpt_image"
+    cont_tool="${out}/host/ck_cont_tool"
+}
+
+# make_refusals: parse only (make -n); nothing is built.
+make_refusals() {
+    local out f=0 tmpo
+    mk() { make -n -s -C native/kernel OUT="${1}" full "${@:2}" 2>&1; }
+    tmpo="$(mktemp -d)"
+    out="$(mk "${tmpo}" CK_TEST_CONTINUITY=1 CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS=/dev/null CK_MACHINE_ID=/dev/null)"
+    grep -q "CK_TEST_CONTINUITY (TEST-ONLY continuity mode selection) cannot be combined with CK_HARDWARE_STAGING" <<<"${out}" \
+        && echo "PASS  make refuses the continuity test mode with CK_HARDWARE_STAGING" || { echo "FAIL  hardware staging accepted the continuity test mode"; f=1; }
+    out="$(mk "${tmpo}" CK_TEST_CONTINUITY=1 CK_QEMU_UNSAFE_DMA=1)"
+    grep -q "cannot be combined with CK_QEMU_UNSAFE_DMA" <<<"${out}" \
+        && echo "PASS  make refuses the continuity test mode with CK_QEMU_UNSAFE_DMA" || { echo "FAIL  unsafe DMA build accepted the continuity test mode"; f=1; }
+    out="$(mk "${tmpo}" CK_TEST_CONTINUITY=1 CK_TEST_STORE_CRASH=1)"
+    grep -q "cannot be combined with CK_TEST_STORE_CRASH" <<<"${out}" \
+        && echo "PASS  make refuses the continuity test mode with the Store crash hook" || { echo "FAIL  crash hook build accepted the continuity test mode"; f=1; }
+    out="$(mk "${tmpo}" CK_TEST_CONTINUITY_MUTANT=resume_no_commit)"
+    grep -q "CK_TEST_CONTINUITY_MUTANT needs CK_TEST_CONTINUITY=1" <<<"${out}" \
+        && echo "PASS  make refuses a continuity mutant without the TEST mode" || { echo "FAIL  mutant built without the TEST mode"; f=1; }
+    out="$(mk "${tmpo}" CK_TEST_CONTINUITY=1 CK_TEST_CONTINUITY_MUTANT=bogus)"
+    grep -q "CK_TEST_CONTINUITY_MUTANT must be" <<<"${out}" \
+        && echo "PASS  make refuses an unknown continuity mutant" || { echo "FAIL  unknown continuity mutant accepted"; f=1; }
+    rm -rf "${tmpo}"
+    return "${f}"
+}
+
+# cq_default_image_checks: the default image carries no TEST wiring; the TEST
+# image announces itself. Sets hw_ok.
+cq_default_image_checks() {
+    hw_ok=1
+    make_refusals || hw_ok=0
+    if grep -aqE "AIENCONT|RECOVERY_OPERATOR_KEY|TEST-ONLY continuity" "${plain_efi}"; then
+        echo "FAIL  default image carries continuity test strings"; hw_ok=0
+    else
+        echo "PASS  default image carries no continuity test strings"
+    fi
+    if grep -aqF "TEST-ONLY continuity mode selection image" "${test_efi}"; then
+        echo "PASS  TEST continuity image announces itself"
+    else
+        echo "FAIL  TEST continuity image lacks its announcement"; hw_ok=0
+    fi
+}
+
+cq_quiet_and_trap() {
+    quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}"
+    quiet_tag="${AIENOS_QUIET_TAG:-${script_tag} $$}"
+    if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then
+        echo "NOT_RUN  quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)"
+        verdict NOT_RUN
+        exit 3
+    fi
+    own_flag=1
+    release_flag() {
+        if [[ "${own_flag}" == 1 ]]; then
+            own_flag=0
+            if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi
+        fi
+    }
+    top="$(mktemp -d)"
+    qemu_pid=""
+    cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null; rm -rf "${top}"; release_flag; }
+    trap cleanup EXIT
+    # shellcheck source=scripts/lib_ck_m1_checks.sh
+    source "${repo_root}/scripts/lib_ck_m1_checks.sh"
+    fail=0; m1_fail=0; rows=0; rows_ok=0
+    work="${top}/boot"
+}
+
+pass() { echo "PASS  $1"; rows=$(( rows + 1 )); rows_ok=$(( rows_ok + 1 )); }
+bad() {
+    echo "FAIL  $1"; fail=1; rows=$(( rows + 1 ))
+    if [[ "${cq_mode}" == mutant ]]; then
+        release_flag
+        verdict "KILLED (the gate's check failed on the mutant: $1)"
+        exit 0
+    fi
+}
+sha() { sha256sum "$1" | cut -d' ' -f1; }
+digest() { sha "${image}"; }
+
+new_disk() { # -> ${gpt_tmpl}; sets plan_unit from the range the tool WROTE
+    local l re='^gpt_image bs=[0-9]+ blocks=[0-9]+ aienos_first_lba=([0-9]+) aienos_last_lba=([0-9]+)$'
+    gpt_tmpl="${top}/gpt-4096.img"
+    l=$("${gpt_tool}" create "${gpt_tmpl}" 4096 $(( img_bytes / 1048576 )) aienos-middle) || l=""
+    if [[ "${l}" =~ ${re} ]]; then set_part 4096 "${BASH_REMATCH[2]}"; return 0; fi
+    echo "FAIL  GPT boot disk image not created (${l})"; verdict NOT_RUN; exit 2
+}
+fresh_image() { image="${top}/disk.img"; cp "${gpt_tmpl}" "${image}"; }
+
+# plan MODE [cp=NAME] [response=HEX]: write the K-7 plan line into the scratch unit.
+plan() {
+    local line="AIENCONT v1 mode=$1"; shift
+    local w; for w in "$@"; do line+=" ${w}"; done
+    { printf '%s\n' "${line}"; head -c "${unit}" /dev/zero; } | head -c "${unit}" \
+        | dd of="${image}" bs="${unit}" seek="${plan_unit}" conv=notrunc status=none
+}
+clear_plan() { head -c "${unit}" /dev/zero | dd of="${image}" bs="${unit}" seek="${plan_unit}" conv=notrunc status=none; }
+
+qemu_args() {
+    qargs=(-M "virt,virtualization=on,gic-version=3,iommu=smmuv3" -accel tcg,thread=single -cpu max -smp 4 -m 2048
+        -drive if=pflash,format=raw,readonly=on,file="${code_fd}"
+        -drive if=pflash,format=raw,file="${work}/vars.fd"
+        -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp"
+        -device virtio-blk-pci,drive=esp
+        -drive if=none,id=nvme0,format=raw,file="${image}"
+        -device "nvme,drive=nvme0,serial=aienos-continuity,logical_block_size=4096,physical_block_size=4096"
+        -device ramfb -display none -nic none
+        -serial file:"${work}/serial.log" -no-reboot)
+}
+prep() { # efi
+    rm -rf "${work}"; mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS"
+    touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
+    cp "$1" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
+    cp "${vars_fd}" "${work}/vars.fd"
+}
+# boot: one cold boot that runs to its end (the guest resets); M1 checks.
+# Sets before/after (image digests bracketing only the guest's writes).
+boot() {
+    prep "${test_efi}"; qemu_args
+    before=$(digest)
+    timeout "${AIENOS_QEMU_TIMEOUT:-240}" qemu-system-aarch64 "${qargs[@]}"
+    qemu_status=$?
+    after=$(digest)
+    tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt"
+    [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/${script_tag}_${boot_n:-0}.log"
+    boot_n=$(( ${boot_n:-0} + 1 ))
+    failed=0
+    ck_m1_checks >"${work}/m1.txt"
+    if [[ "${failed}" != 0 || "${qemu_status}" != 0 ]]; then
+        echo "FAIL  boot ${boot_n}: M1 checks or QEMU exit ${qemu_status}"; grep '^FAIL' "${work}/m1.txt" | sed 's/^/      /'
+        m1_fail=1
+        if [[ "${cq_mode}" == mutant ]]; then :; else fail=1; fi
+        return 1
+    fi
+    return 0
+}
+# boot_kill CP: run until "continuity: HALT at CP", then SIGKILL.
+boot_kill() {
+    prep "${test_efi}"; qemu_args
+    : >"${work}/serial.log"
+    qemu-system-aarch64 "${qargs[@]}" &
+    qemu_pid=$!
+    local deadline=$(( $(date +%s) + ${AIENOS_QEMU_TIMEOUT:-240} ))
+    while (( $(date +%s) < deadline )); do
+        grep -qE '^(continuity: HALT at |CONTINUITY: )' "${work}/serial.log" 2>/dev/null && break
+        kill -0 "${qemu_pid}" 2>/dev/null || break
+        sleep 0.1
+    done
+    sleep 0.2
+    kill -9 "${qemu_pid}" 2>/dev/null
+    wait "${qemu_pid}" 2>/dev/null
+    qemu_pid=""
+    tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt"
+    [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/${script_tag}_kill_${1}.log"
+}
+# line: the last serial line of this boot that starts with the pattern (ERE).
+out() { grep -E "$1" "${work}/serial.txt" | tail -1; }
+cont() { grep -oE "^CONTINUITY: $1( .*)?$" "${work}/serial.txt" | tail -1; }
+field() { sed -nE "s/.* $1=([0-9a-f]+).*/\1/p" <<<"$2"; }
+agent_of() { sed -nE 's/.*agent=([0-9a-f]{64}).*/\1/p' <<<"$1"; }
+unchanged() { [[ "${before}" == "${after}" ]]; }
+serial_cont() { grep -E '^(CONTINUITY|RECOVERY)' "${work}/serial.txt" | head -4 | tr '\n' '|'; }
diff --git a/scripts/qemu_ck_continuity_test.sh b/scripts/qemu_ck_continuity_test.sh
new file mode 100755
index 00000000..614946e6
--- /dev/null
+++ b/scripts/qemu_ck_continuity_test.sh
@@ -0,0 +1,119 @@
+#!/usr/bin/env bash
+# CK gate M4_CONTINUITY: C kernel continuity (resolve/provision/resume/commit)
+# on a 4096-byte-LBA NVMe image under QEMU, one cold QEMU process per boot.
+# Oracle: scripts/qemu_continuity_test.sh (Rust). Contract section 6.1.
+# Mode selection: contract K-7 (see scripts/lib_ck_cont_qemu.sh). QEMU is not
+# hardware. Run only through the heavy forge. Usage: [--mutant NAME]
+set -uo pipefail
+cd "$(dirname "${BASH_SOURCE[0]}")/.."
+repo_root="$(pwd)"
+gate_name=M4_CONTINUITY
+script_tag=qemu_ck_continuity
+# shellcheck source=scripts/lib_ck_cont_qemu.sh
+source scripts/lib_ck_cont_qemu.sh
+cq_init "$@"
+cq_build
+[[ "${cq_mode}" == mutant ]] || cq_default_image_checks
+cq_quiet_and_trap
+[[ "${cq_mode}" == mutant || "${hw_ok}" == 1 ]] || fail=1
+new_disk
+
+view_ok() { # line word inc seq cortex branches
+    [[ "$1" =~ ^CONTINUITY:\ $2\ agent=[0-9a-f]{64}\ incarnation=$3\ sequence=$4\ cortex=$5\ branches=$6\ memory=[0-9a-f]{16}$ ]]
+}
+
+# 81 blank media, resume
+fresh_image; plan 6
+boot; l=$(out '^CONTINUITY: ')
+if [[ "${l}" == "CONTINUITY: STOP (store Unformatted)" ]] && unchanged; then pass "81 blank media resume: ${l}, image unchanged"; else bad "81 blank resume: '${l}' unchanged=$(unchanged && echo y || echo n)"; fi
+
+# 82 formatted store, no root (mode 1 = ordinary Store boot formats it)
+fresh_image; plan 1; boot; plan 6
+boot; l=$(out '^CONTINUITY: ')
+if [[ "${l}" == "CONTINUITY: UNPROVISIONED" ]] && unchanged; then pass "82 formatted store, no root: UNPROVISIONED, image unchanged"; else bad "82 unprovisioned: '${l}'"; fi
+
+# 83 provision on a fresh disk
+fresh_image; plan 5
+boot; l=$(out '^CONTINUITY: ')
+if view_ok "${l}" PROVISIONED 1 1 0 1; then agent=$(agent_of "${l}"); pass "83 provision: PROVISIONED agent=${agent:0:16}.. incarnation=1 sequence=1"; else bad "83 provision: '${l}'"; agent=""; fi
+prov_image="${top}/prov.img"; cp "${image}" "${prov_image}"
+
+# 83a provision again
+plan 5
+boot; l=$(out '^CONTINUITY: ')
+if [[ "${l}" == "CONTINUITY: STOP (AlreadyProvisioned)" ]] && unchanged; then pass "83a second provision refused, image unchanged"; else bad "83a second provision: '${l}'"; fi
+
+# 83b independent provisioning gives a different agent
+fresh_image; plan 5
+boot; l2=$(out '^CONTINUITY: ')
+a2=$(agent_of "${l2}")
+if [[ -n "${agent}" && -n "${a2}" && "${a2}" != "${agent}" ]]; then pass "83b second fresh disk gets a different agent"; else bad "83b agents equal or missing"; fi
+
+# 84 resume (commits incarnation 2) then remember
+image="${prov_image}"; plan 7
+boot; l=$(out '^CONTINUITY: RESUMED ')
+view_ok "${l}" RESUMED 2 2 0 1 && [[ "$(agent_of "${l}")" == "${agent}" ]] && r_ok=1 || r_ok=0
+l_rem=$(out '^CONTINUITY: REMEMBERED ')
+if [[ "${r_ok}" == 1 ]] && view_ok "${l_rem}" REMEMBERED 2 3 1 2; then pass "84 resume + remember: incarnation=2 sequence=2 then sequence=3 cortex=1 branches=2"; else bad "84: '${l}' / '${l_rem}'"; fi
+mem=$(field memory "${l_rem}")
+rem_image="${top}/rem.img"; cp "${image}" "${rem_image}"
+
+# 85, 85a cold restarts
+plan 6
+boot; l=$(out '^CONTINUITY: RESUMED ')
+if view_ok "${l}" RESUMED 3 4 1 2 && [[ "$(agent_of "${l}")" == "${agent}" && "$(field memory "${l}")" == "${mem}" ]]; then pass "85 cold restart 1: same agent and memory, incarnation=3 sequence=4"; else bad "85: '${l}' want mem ${mem}"; fi
+boot; l=$(out '^CONTINUITY: RESUMED ')
+if view_ok "${l}" RESUMED 4 5 1 2 && [[ "$(agent_of "${l}")" == "${agent}" && "$(field memory "${l}")" == "${mem}" ]]; then pass "85a cold restart 2: incarnation=4 sequence=5"; else bad "85a: '${l}'"; fi
+
+# 86 reference: the new memory after one uninterrupted mode 8 commit
+work_image="${top}/work.img"; cp "${rem_image}" "${work_image}"; image="${work_image}"; plan 8
+boot; l=$(out '^CONTINUITY: COMMITTED ')
+new_mem=$(field memory "${l}")
+if [[ -n "${new_mem}" && "${new_mem}" != "${mem}" ]]; then pass "86 reference: uninterrupted commit gives new memory ${new_mem}"; else bad "86 reference commit: '${l}'"; fi
+
+# 86a-86i kill at each checkpoint, then cold resume
+i=0
+for cp in before_first_write after_payload_objects after_catalog after_commit_record after_first_flush \
+          after_inactive_superblock after_final_flush before_anchor after_anchor; do
+    case "${cp}" in
+        after_inactive_superblock) want=either ;;
+        after_final_flush|before_anchor|after_anchor) want=new ;;
+        *) want=old ;;
+    esac
+    cp "${rem_image}" "${work_image}"; image="${work_image}"
+    plan 8 "cp=${cp}"
+    boot_kill "${cp}"
+    seen=$(grep -c "^CHECKPOINT: ${cp}\$" "${work}/serial.txt" || true)
+    plan 6
+    boot; l=$(out '^CONTINUITY: ')
+    got=$(field memory "${l}")
+    case "${want}" in
+        old) okm=$([[ "${got}" == "${mem}" ]] && echo 1 || echo 0) ;;
+        new) okm=$([[ "${got}" == "${new_mem}" ]] && echo 1 || echo 0) ;;
+        either) okm=$([[ "${got}" == "${mem}" || "${got}" == "${new_mem}" ]] && echo 1 || echo 0) ;;
+    esac
+    i=$(( i + 1 ))
+    if [[ "${seen}" -ge 1 && "${l}" =~ ^CONTINUITY:\ RESUMED\  && "$(agent_of "${l}")" == "${agent}" && "${okm}" == 1 ]]; then
+        pass "86.${i} kill at ${cp}: marker seen, same agent, ${want} memory"
+    else
+        bad "86.${i} kill at ${cp}: seen=${seen} want=${want} got='${l}'"
+    fi
+done
+
+# 87 malformed peer: read-only resume
+cp "${rem_image}" "${work_image}"; image="${work_image}"
+"${cont_tool}" inject-peer "${image}" 4096 >/dev/null || bad "87 inject-peer tool failed"
+plan 6
+boot; l=$(out '^CONTINUITY: ')
+if view_ok "${l}" RESUMED_READONLY 2 3 1 2 && [[ "$(agent_of "${l}")" == "${agent}" && "$(field memory "${l}")" == "${mem}" ]] && unchanged; then
+    pass "87 malformed peer: RESUMED_READONLY, same agent and memory, image unchanged"
+else bad "87 malformed peer: '${l}'"; fi
+
+echo "rows ${rows_ok}/${rows}; M1 failures: ${m1_fail}"
+if [[ "${cq_mode}" == mutant ]]; then
+    release_flag
+    if [[ "${fail}" == 0 ]]; then verdict SURVIVED; else verdict "KILLED (gate failed)"; fi
+    exit 0
+fi
+[[ "${hw_ok:-1}" == 1 && "${fail}" == 0 ]] && verdict PASS || verdict FAIL
+[[ "${fail}" == 0 ]]
diff --git a/scripts/qemu_ck_recovery_test.sh b/scripts/qemu_ck_recovery_test.sh
new file mode 100755
index 00000000..a80b7c7e
--- /dev/null
+++ b/scripts/qemu_ck_recovery_test.sh
@@ -0,0 +1,110 @@
+#!/usr/bin/env bash
+# CK gate M4_RECOVERY: C kernel Recovery Core (inspect / repair-degraded-peer /
+# provision-identity, operator-authorised) under QEMU, one cold boot each.
+# Oracle: scripts/qemu_recovery_test.sh (Rust). Contract section 6.2.
+# Mode selection: contract K-7. The operator key is TEST-ONLY (0x0f x32).
+# QEMU is not hardware. Run only through the heavy forge. Usage: [--mutant NAME]
+set -uo pipefail
+cd "$(dirname "${BASH_SOURCE[0]}")/.."
+repo_root="$(pwd)"
+gate_name=M4_RECOVERY
+script_tag=qemu_ck_recovery
+# shellcheck source=scripts/lib_ck_cont_qemu.sh
+source scripts/lib_ck_cont_qemu.sh
+cq_init "$@"
+cq_build
+[[ "${cq_mode}" == mutant ]] || cq_default_image_checks
+cq_quiet_and_trap
+[[ "${cq_mode}" == mutant || "${hw_ok}" == 1 ]] || fail=1
+new_disk
+
+test_key=$(printf '0f%.0s' $(seq 32))
+wrong_key=$(printf '0e%.0s' $(seq 32))
+zero=$(printf '00%.0s' $(seq 32))
+respond() { "${cont_tool}" respond "$1" "$2"; }
+challenge_for() { sed -nE "s/^RECOVERY_CHALLENGE: action=$1 challenge=([0-9a-f]{64})\$/\1/p" "${work}/serial.txt" | tail -1; }
+refused_unauth() { [[ -n "$(out '^RECOVERY_REFUSED \(Unauthorised\)$')" ]] && unchanged; }
+b() { plan "$@"; boot; } # plan + boot
+
+# Setup: provisioned store with memory, then a malformed inactive peer.
+fresh_image; b 5
+l=$(out '^CONTINUITY: PROVISIONED '); agent=$(agent_of "${l}")
+[[ -n "${agent}" ]] || bad "setup: provision failed"
+b 7
+lm=$(out '^CONTINUITY: REMEMBERED '); memory=$(field memory "${lm}")
+[[ -n "${memory}" ]] || bad "setup: remember failed"
+good_image="${top}/good.img"; cp "${image}" "${good_image}"
+"${cont_tool}" inject-peer "${image}" 4096 >/dev/null || bad "setup: inject-peer failed"
+
+echo "=== 1. Degraded store ==="
+b 9
+if [[ -n "$(out '^RECOVERY_OPERATOR_KEY: TEST-ONLY$')" && \
+      -n "$(out '^RECOVERY_CORE: ENTERED reason=Degraded\(Malformed\)$')" && \
+      "$(agent_of "$(out '^RECOVERY_RECORD:')")" == "${agent}" ]] && unchanged; then
+    pass "88 inspection enters the Recovery Core (Degraded(Malformed)), same agent, writes nothing"
+else bad "88 inspection: $(grep -E '^RECOVERY_' "${work}/serial.txt" | tr '\n' '|')"; fi
+c_repair=$(challenge_for repair-degraded-peer)
+[[ ${#c_repair} == 64 ]] || bad "88 no repair challenge offered"
+good=$(respond "${test_key}" "${c_repair}")
+flip="${good:0:63}$([[ "${good:63:1}" == 0 ]] && echo 1 || echo 0)"
+i=0
+for c in zero wrong_key wrong_challenge flipped_last_byte; do
+    case "${c}" in
+        zero) r="${zero}" ;;
+        wrong_key) r=$(respond "${wrong_key}" "${c_repair}") ;;
+        wrong_challenge) r=$(respond "${test_key}" "$(printf 'ff%.0s' $(seq 32))") ;;
+        flipped_last_byte) r="${flip}" ;;
+    esac
+    plan 10 "response=${r}"; boot
+    i=$(( i + 1 ))
+    if refused_unauth; then pass "88.${i} repair with ${c} response refused (Unauthorised), image unchanged"
+    else bad "88.${i} repair with ${c}: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
+done
+plan 10 "response=${good}"; boot
+if [[ -n "$(out '^RECOVERY_ACTION: repair-degraded-peer DONE$')" ]] && ! unchanged; then pass "89 authorised repair done, image changed"
+else bad "89 authorised repair: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
+b 6; r=$(out '^CONTINUITY: RESUMED ')
+if [[ "$(agent_of "${r}")" == "${agent}" && "$(field memory "${r}")" == "${memory}" ]]; then pass "89a after repair a cold boot resumes writable, same agent and memory"
+else bad "89a resume after repair: '${r:-$(out '^CONTINUITY:')}'"; fi
+plan 10 "response=${good}"; boot
+if [[ -n "$(out '^RECOVERY_REFUSED \(NotApplicable\)$')" ]] && unchanged; then pass "89b replaying the repair authorisation on the healthy store: NotApplicable, image unchanged"
+else bad "89b replay: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
+
+echo "=== 2. Unprovisioned store ==="
+fresh_image; b 1; b 9
+if [[ -n "$(out '^RECOVERY_CORE: ENTERED reason=Unprovisioned$')" ]] && unchanged; then pass "90 unprovisioned store enters the Recovery Core"
+else bad "90 inspection: $(grep -E '^RECOVERY_' "${work}/serial.txt" | tr '\n' '|')"; fi
+c_prov=$(challenge_for provision-identity)
+[[ ${#c_prov} == 64 ]] || bad "90 no provision challenge offered"
+plan 11 "response=$(respond "${wrong_key}" "${c_prov}")"; boot
+if refused_unauth; then pass "90a provisioning with the wrong key refused, image unchanged"
+else bad "90a wrong key: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
+plan 11 "response=$(respond "${test_key}" "${c_prov}")"; boot
+da=$(out '^RECOVERY_ACTION: provision-identity DONE agent=[0-9a-f]{64}$'); pa=$(agent_of "${da}")
+b 6; r=$(out '^CONTINUITY: RESUMED ')
+if [[ -n "${pa}" && "$(agent_of "${r}")" == "${pa}" ]]; then pass "90b authorised provisioning done; a cold resume returns that agent"
+else bad "90b provisioning: '${da}' resume '${r}'"; fi
+
+echo "=== 3. Corrupted agent root (identity lost) ==="
+cp "${good_image}" "${image}"
+"${cont_tool}" corrupt-root "${image}" 4096 >/dev/null || bad "91 corrupt-root failed"
+b 9
+if [[ -n "$(out '^RECOVERY_CORE: ENTERED ')" && -z "$(out '^RECOVERY_CHALLENGE')" ]] && unchanged; then pass "91 corrupted root: Recovery Core entered, no challenge, image unchanged"
+else bad "91 corrupt root: $(grep -E '^RECOVERY_' "${work}/serial.txt" | tr '\n' '|')"; fi
+for m in 10 11; do
+    plan "${m}" "response=${zero}"; boot
+    if [[ -n "$(out '^RECOVERY_REFUSED ')" && -z "$(out '^RECOVERY_ACTION')" ]] && unchanged; then pass "91.m${m} mode ${m} with a forged response refused, nothing done, image unchanged"
+    else bad "91.m${m} forged response: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
+done
+b 6
+if [[ -z "$(out '^CONTINUITY: (RESUMED|PROVISIONED)')" && -n "$(out '^CONTINUITY: ')" ]] && unchanged; then pass "91c normal boot after identity loss: no RESUMED/PROVISIONED ($(out '^CONTINUITY: ')), image unchanged"
+else bad "91c identity loss resume: '$(out '^CONTINUITY: ')'"; fi
+
+echo "rows ${rows_ok}/${rows}; M1 failures: ${m1_fail}"
+if [[ "${cq_mode}" == mutant ]]; then
+    release_flag
+    if [[ "${fail}" == 0 ]]; then verdict SURVIVED; else verdict "KILLED (gate failed)"; fi
+    exit 0
+fi
+[[ "${hw_ok:-1}" == 1 && "${fail}" == 0 ]] && verdict PASS || verdict FAIL
+[[ "${fail}" == 0 ]]