~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/socialweb-computer-console

console.socialweb.computer: kcp, kubectl and ssh compiled to wasm in a browser tab with a terminal; a kcp fork

0
0
main
417 lines · 18.6 KB Raw History
// Copyright 2026 The KCP Authors.
// SPDX-License-Identifier: Apache-2.0

// Owner fence for hibernation snapshots (rc130). Two tabs number their
// persists from the same stored base, so seqs alone cannot tell a stale
// holder's capture from the owner's: both outcomes are tested here.

import { keyChanges, readOwner, restampOwner, snapshotKey, snapshotRejection, startHibernation, tryRestore } from "./kcp_hibernate.js";
import { createIndexedDBBackend } from "./state_store.js";
import { fakeIndexedDB, fakeKeyRange } from "./fake_indexeddb_for_tests.js";

function assertEquals(actual, expected, label = "value") {
  const a = JSON.stringify(actual);
  const e = JSON.stringify(expected);
  if (a !== e) throw new Error(`${label}: expected ${e}, got ${a}`);
}

const KEY = "k1";
const SEQS = { kcp: 12, socialweb: 7 };
const meta = (owner) => ({ extra: { key: KEY, seqs: { ...SEQS }, owner }, bytes: 0, stored: 0, wallMs: 0, stats: {} });

Deno.test("snapshotRejection: same seqs from a stale holder are refused by the owner token", () => {
  assertEquals(snapshotRejection(meta("tab-b"), { key: KEY, seqs: SEQS, owner: "tab-b" }), "", "the owner's snapshot");
  assertEquals(
    snapshotRejection(meta("tab-a"), { key: KEY, seqs: SEQS, owner: "tab-b" }),
    "written by a tab that no longer owned the saved state",
    "a stale holder's capture with colliding seqs",
  );
  assertEquals(snapshotRejection(meta(undefined), { key: KEY, seqs: SEQS, owner: null }), "", "no owner yet (first fenced load)");
  assertEquals(snapshotRejection(meta("tab-a"), { key: KEY, seqs: SEQS, owner: undefined }), "", "fence off (memory state)");
  assertEquals(snapshotRejection(meta("tab-b"), { key: KEY, seqs: { kcp: 13, socialweb: 7 }, owner: "tab-b" }).startsWith("persisted state is newer"), true);
});

Deno.test("tryRestore discards a stale holder's snapshot and cold boots", async () => {
  let discarded = 0;
  const store = { kind: "fake", readMeta: async () => meta("tab-a"), discard: async () => void discarded++ };
  const logs = [];
  const r = await tryRestore({ go: {}, module: null, key: KEY, seqs: SEQS, owner: "tab-b", store, log: (l, m) => logs.push(m), before: () => {} });
  assertEquals([r, discarded], [null, 1]);
  assertEquals(logs, ["snapshot discarded: written by a tab that no longer owned the saved state"]);
});

const fakeGo = () => ({ exited: false, _inst: { exports: { __snap_g0: 1 } } });
function capturing(currentOwners) {
  const saved = [];
  const logs = [];
  const store = {
    kind: "fake",
    async save(_go, extra, _gzip, beforeCommit) {
      const e = extra();
      await beforeCommit();
      saved.push(e);
      return { extra: e, bytes: 0, stored: 0, stats: {} };
    },
  };
  let i = 0;
  const loop = startHibernation({
    go: fakeGo(),
    key: KEY,
    ready: () => true,
    seqs: () => ({ ...SEQS }),
    extra: () => ({}),
    store,
    log: (_l, m) => logs.push(m),
    owner: { token: "tab-a", current: async () => currentOwners[Math.min(i++, currentOwners.length - 1)] },
    timers: { setInterval: () => 0 },
  });
  return { loop, saved, logs };
}
const settle = () => new Promise((r) => setTimeout(r, 10));

Deno.test("capture: the owner's snapshot carries its token", async () => {
  const { loop, saved } = capturing(["tab-a"]);
  loop.request();
  await settle();
  assertEquals(saved.map((e) => e.owner), ["tab-a"]);
});

Deno.test("capture: a tab that lost the state writes no snapshot, before or at commit", async () => {
  const before = capturing(["tab-b"]);
  before.loop.request();
  await settle();
  before.loop.request();
  await settle();
  assertEquals(before.saved, [], "refused before capturing, then stopped");
  assertEquals(before.logs.filter((m) => m.startsWith("snapshot capture stopped")).length, 1);
  const atCommit = capturing(["tab-a", "tab-b"]); // taken over during the capture
  atCommit.loop.request();
  await settle();
  assertEquals(atCommit.saved, [], "refused right before the commit");
});

Deno.test("readOwner reads the state database's owner and creates nothing", async () => {
  const factory = fakeIndexedDB();
  const backend = await createIndexedDBBackend("kcp-wasm", { indexedDB: factory, keyRange: fakeKeyRange });
  assertEquals(await readOwner("kcp-wasm", factory), null, "unclaimed");
  await backend.claimOwner("tab-b");
  assertEquals(await readOwner("kcp-wasm", factory), "tab-b");
  assertEquals(await readOwner("missing", factory), null);
  assertEquals(await readOwner("kcp-wasm", undefined), null, "no IndexedDB");
});

Deno.test("restampOwner hands a restored snapshot to the new owner", async () => {
  let stored = meta("tab-a");
  const store = { readMeta: async () => stored, writeMeta: async (m) => void (stored = structuredClone(m)) };
  assertEquals(await restampOwner(store, "tab-b"), true);
  assertEquals(stored.extra.owner, "tab-b");
  assertEquals(await restampOwner(store, "tab-b"), false, "already stamped");
});

Deno.test("page wiring: snapshots get this tab's token and the previous owner's for restores", async () => {
  const app = await Deno.readTextFile(new URL("./app.js", import.meta.url));
  const worker = await Deno.readTextFile(new URL("./kcp_worker.js", import.meta.url));
  const claim = app.slice(app.indexOf("async function claimStateOwner"), app.indexOf("function stateWriteFailed"));
  if (!(claim.indexOf("backend.load(OWNER_KEY)") >= 0 && claim.indexOf("backend.load(OWNER_KEY)") < claim.indexOf("claimOwner("))) {
    throw new Error("previous owner read before the claim");
  }
  if (!/owner: stateOwnerPrevious === undefined \? null : \{ token: stateOwnerToken, previous: stateOwnerPrevious, db: stateDB \}/.test(app)) {
    throw new Error("hibernate init carries the owner");
  }
  for (const needle of ["init.hibernate.owner.previous", "hibernate.readOwner(init.hibernate.owner.db)", "hibernate.restampOwner("]) {
    if (!worker.includes(needle)) throw new Error(`worker lacks ${needle}`);
  }
});

// rc131 cold boots, without a capture storm (review page-rc131 2e7d88ba5,
// 1): a flush asks for a capture (requestSoon) that waits until the seqs
// were still SOON_QUIET_MS and SOON_GAP_MS after the last capture; a hide
// (request) captures at once. A fake clock drives the loop's tick.
function clockedLoop({ captured = { kcp: 9, socialweb: 0 } } = {}) {
  const c = { t: 1_000_000, seqs: { ...captured }, saved: [], tick: null };
  const store = {
    kind: "fake",
    async save(_go, extra, _gzip, beforeCommit) {
      const e = extra();
      await beforeCommit();
      c.saved.push({ at: c.t, kcp: e.seqs.kcp });
      return { extra: e, bytes: 0, stored: 0, stats: {} };
    },
  };
  c.store = store;
  c.loop = startHibernation({
    go: fakeGo(),
    key: KEY,
    ready: () => true,
    seqs: () => ({ ...c.seqs }),
    extra: () => ({}),
    captured,
    store,
    log: () => {},
    timers: { setInterval: (fn) => (c.tick = fn) },
    now: () => c.t,
  });
  // advance the clock second by second, ticking like the 1 s interval
  c.run = async (seconds, each = () => {}) => {
    for (let i = 0; i < seconds; i++) {
      c.t += 1000;
      each(i);
      await c.tick();
    }
  };
  return c;
}

Deno.test("capture after a flush: once the seqs are still, never for a no-op flush", async () => {
  const c = clockedLoop();
  await c.run(15); // past the SOON_GAP_MS boot grace
  c.loop.requestSoon(); // a flush that wrote nothing new
  await c.run(5);
  assertEquals(c.saved, [], "no capture without new records");
  c.seqs.kcp = 10; // the flush persisted one message
  c.loop.requestSoon();
  await c.run(2);
  assertEquals(c.saved, [], "not before SOON_QUIET_MS of still seqs");
  await c.run(3);
  assertEquals(c.saved.map((x) => x.kcp), [10], "captured a few seconds after the flush");
});

Deno.test("no request: background writes capture once still, at most every 60 s", async () => {
  const c = clockedLoop();
  await c.run(60); // past the boot grace
  c.loop.requestSoon(); // unchanged seqs: not armed
  await c.run(1);
  c.seqs.kcp = 10; // a background write, no flush
  await c.run(2);
  assertEquals(c.saved, [], "nothing before QUIET_MS of still seqs");
  await c.run(2);
  assertEquals(c.saved.map((x) => x.kcp), [10], "the regular capture");
});

Deno.test("an idle tab that persists a write every 5-10 s captures with a doubling gap, none during boot", async () => {
  const c = clockedLoop();
  // 30 min of a live idle page: a write every 7 s, never 30 s quiet
  await c.run(1800, (i) => {
    if (i % 7 === 0) c.seqs.kcp++;
  });
  const at = c.saved.map((x) => (x.at - 1_000_000) / 1000);
  assert(at[0] >= 60 && at[0] <= 70, `boot grace: first capture after 60 s, at ${JSON.stringify(at)}`);
  const gaps = at.slice(1).map((x, i) => x - at[i]);
  // 60 s, 120 s, 240 s, 480 s, then capped at 600 s (each +up to 7 s for still seqs)
  const want = [120, 240, 480, 600, 600];
  for (let i = 0; i < gaps.length; i++) {
    assert(gaps[i] >= want[Math.min(i, want.length - 1)] && gaps[i] <= want[Math.min(i, want.length - 1)] + 10, `gaps ${JSON.stringify(gaps)}`);
  }
  assert(c.saved.length <= 6, `captures at ${JSON.stringify(at)} s`);
});

Deno.test("a flush or hide resets the background gap to 60 s", async () => {
  const c = clockedLoop();
  await c.run(900, (i) => {
    if (i % 7 === 0) c.seqs.kcp++;
  });
  const before = c.saved.length; // 60 s, +120 s, +240 s, +480 s: the gap is now long
  c.seqs.kcp++;
  c.loop.requestSoon(); // a contract step change flushed
  await c.run(20);
  assertEquals(c.saved.length, before + 1, "the flush captured");
  await c.run(80, (i) => {
    if (i % 7 === 0) c.seqs.kcp++;
  });
  assertEquals(c.saved.length, before + 2, "next background capture after 60 s again");
});

Deno.test("a typical VM lifecycle captures at most about twice", async () => {
  const c = clockedLoop();
  // Request VM to Held: ~60 s of requester status writes every 1-2 s, a
  // flush at each of 7 phase/step changes.
  await c.run(60, (i) => {
    if (i % 2 === 0) c.seqs.kcp++;
    if (i % 9 === 0) c.loop.requestSoon();
  });
  await c.run(10); // Held, settled
  // minutes of use: occasional background writes, no flushes
  await c.run(120, (i) => {
    if (i % 40 === 0) c.seqs.kcp++;
  });
  // Release: ~15 s of writes, 3 flushes
  await c.run(15, (i) => {
    c.seqs.kcp++;
    if (i % 5 === 0) c.loop.requestSoon();
  });
  await c.run(10);
  // [62 s: Held settled, then the background writes at most once a minute,
  // 208 s: Released settled]: two from flushes.
  const at = c.saved.map((x) => (x.at - 1_000_000) / 1000);
  assert(c.saved.length <= 4, `captures at ${JSON.stringify(at)} s`);
  assertEquals(at.filter((s) => (s > 60 && s <= 70) || s > 195).length, 2, "one per settle point");
  // the two settle points are captured right away
  const settledHeld = c.saved.find((x) => x.at > 1_000_000 + 60_000 && x.at <= 1_000_000 + 70_000);
  assert(settledHeld, "Held captured once settled");
  const burst = c.saved.filter((x) => x.at <= 1_000_000 + 60_000);
  assertEquals(burst.length, 0, "no capture during the bidding burst (seqs never still 3 s)");
});

Deno.test("a hide captures at once; the worker asks requestSoon after a flush", async () => {
  const c = clockedLoop();
  c.seqs.kcp = 10;
  c.loop.request(); // visibilitychange to hidden
  await new Promise((r) => setTimeout(r, 10));
  assertEquals(c.saved.map((x) => x.kcp), [10]);
  const worker = await Deno.readTextFile(new URL("./kcp_worker.js", import.meta.url));
  if (!/if \(out\?\.flushed\) hibernation\?\.requestSoon\(\);/.test(worker)) throw new Error("flushes use requestSoon");
  if (!/type === "hibernate"\) \{\s*hibernation\?\.request\(\);/.test(worker)) throw new Error("hide uses request");
});

Deno.test("a hide whose flush lands after the hide's tick still captures at once", async () => {
  const c = clockedLoop();
  c.loop.request(); // hidden: the flush has not persisted yet, seqs unchanged
  await c.run(1);
  assertEquals(c.saved, [], "nothing new yet");
  c.seqs.kcp = 10; // the hide's flush persists
  c.loop.requestSoon();
  await new Promise((r) => setTimeout(r, 10));
  assertEquals(c.saved.map((x) => x.kcp), [10], "captured without the SOON_QUIET_MS/SOON_GAP_MS wait");
  // later flushes are ordinary again
  await c.run(20);
  c.seqs.kcp = 11;
  c.loop.requestSoon();
  await c.run(1);
  assertEquals(c.saved.length, 1, "a flush long after the hide waits for still seqs");
  await c.run(4);
  assertEquals(c.saved.map((x) => x.kcp), [10, 11]);
});

Deno.test("a hide during an in-flight capture still captures at once afterwards", async () => {
  const c = clockedLoop();
  await c.run(59, (i) => {
    if (i === 0) c.seqs.kcp = 10;
  });
  assertEquals(c.saved, [], "boot grace");
  // the background capture of seq 10 is due on the next tick; hold its save open
  let release, entered;
  const gate = new Promise((r) => (release = r));
  const inSave = new Promise((r) => (entered = r));
  const save = c.store.save;
  c.store.save = async (go, extra, gzip, beforeCommit) => {
    const e = extra(); // the real capture takes memory and seqs at once
    entered();
    await gate;
    return save(go, () => e, gzip, beforeCommit);
  };
  c.t += 1000;
  const inflight = c.tick();
  await inSave;
  c.seqs.kcp = 11; // the hide's flush lands while the capture runs
  c.loop.request(); // hidden
  release();
  await inflight;
  assertEquals(c.saved.map((x) => x.kcp), [10], "the in-flight capture has the pre-hide seqs");
  await c.run(1);
  assertEquals(c.saved.map((x) => x.kcp), [10, 11], "the hide still captured at once");
});

Deno.test("one hide plus a flush every 3 s during 5 s captures: two captures, then throttled", async () => {
  const c = clockedLoop();
  await c.run(60); // past the boot grace, nothing new
  const save = c.store.save;
  let waits = [];
  c.store.save = async (go, extra, gzip, beforeCommit) => {
    const e = extra(); // memory and seqs are taken when the capture starts
    const until = c.t + 5000; // a phone capture takes ~5 s
    await new Promise((r) => waits.push({ until, r }));
    return save(go, () => e, gzip, beforeCommit);
  };
  const step = async (each) => {
    c.t += 1000;
    each?.();
    const due = waits.filter((w) => c.t >= w.until);
    waits = waits.filter((w) => c.t < w.until);
    due.forEach((w) => w.r());
    await new Promise((r) => setTimeout(r, 0));
    // a tick that starts a capture resolves only when its save does
    await Promise.race([c.tick(), new Promise((r) => setTimeout(r, 0))]);
  };
  c.seqs.kcp = 10;
  c.loop.request(); // hidden; capture 1 starts at once
  let i = 0;
  const flushEvery3s = () => {
    if (++i % 3 === 0) {
      c.seqs.kcp++;
      c.loop.requestSoon();
    }
  };
  for (let s = 0; s < 14; s++) await step(flushEvery3s);
  const first = c.saved.length;
  for (let s = 0; s < 30; s++) await step(flushEvery3s);
  const later = c.saved.length;
  const pending = waits.length;
  waits.forEach((w) => w.r()); // let an in-flight capture finish before the test ends
  await new Promise((r) => setTimeout(r, 0));
  assertEquals([first, later, pending], [2, 2, 0], `captures ${JSON.stringify(c.saved.map((x) => [(x.at - 1_000_000) / 1000, x.kcp]))}`);
});

Deno.test("the first load of a new build leaves a snapshot the next reload can use", async () => {
  // forceApply (set on the first load of a new build) is not part of the key,
  // so its snapshot matches the next, ordinary, load of the same build.
  const worker = await Deno.readTextFile(new URL("./kcp_worker.js", import.meta.url));
  if (!/const snapshotConf = \{ \.\.\.init\.socialweb, restore: undefined, forceApply: undefined, noopLog: init\.noopLog \|\| undefined \};[\s\S]{0,200}JSON\.stringify\(snapshotConf\)/.test(worker)) {
    throw new Error("snapshot key must leave out restore and forceApply");
  }
  if (!/async function restoreSnapshot\(\) \{[\s\S]{0,400}if \(init\.socialweb\?\.forceApply\) \{[\s\S]{0,300}return null;/.test(worker)) {
    throw new Error("a forceApply load must cold boot, not restore");
  }
  const forced = await snapshotKey("w", "data:,exec", JSON.stringify({ origin: "o", forceApply: undefined }));
  const plain = await snapshotKey("w", "data:,exec", JSON.stringify({ origin: "o" }));
  assertEquals(forced, plain);
});

function assert(condition, message) {
  if (!condition) throw new Error(message);
}

Deno.test("restampOwner stamps only the snapshot this load restored", async () => {
  let stored = { ...meta("tab-a"), wallMs: 5, memFile: "mem-5.gz" };
  const store = { readMeta: async () => stored, writeMeta: async (m) => void (stored = structuredClone(m)) };
  const restored = structuredClone(stored);
  stored = { ...meta("tab-a"), wallMs: 9, memFile: "mem-9.gz" }; // a capture committed since
  assertEquals(await restampOwner(store, "tab-b", restored), false, "another snapshot");
  stored = structuredClone(restored);
  assertEquals(await restampOwner(store, "tab-b", restored), true, "the restored one");
});

Deno.test("keyChanges names the key parts and config fields that changed", () => {
  const conf = { origin: "a1", commsMode: "b1", did: "c1" };
  assertEquals(keyChanges("f2-w1-e1-c1", "f2-w2-e1-c1"), "module");
  assertEquals(keyChanges("f2-w1-e1-c1", "f2-w1-e2-c1"), "wasm_exec.js");
  assertEquals(keyChanges("f2-w1-e1-c1", "f2-w1-e1-c2"), "boot configuration", "no field hashes in an old capture");
  assertEquals(
    keyChanges("f2-w1-e1-c1", "f2-w1-e1-c2", conf, { ...conf, commsMode: "b2", handle: "h" }),
    "boot configuration (commsMode, handle)",
  );
  assertEquals(keyChanges("f1-w1-e1-c1", "f2-w2-e1-c1"), "snapshot format, module");
});

Deno.test("snapshotRejection names what changed since the capture", () => {
  const m = { extra: { key: "f2-w1-e1-c1", seqs: { ...SEQS }, confHashes: { did: "x" } } };
  assertEquals(
    snapshotRejection(m, { key: "f2-w1-e1-c2", seqs: SEQS, confHashes: { did: "y" } }),
    "changed since the capture: boot configuration (did)",
  );
});

Deno.test("tryRestore reports every outcome", async () => {
  const outcomes = [];
  const outcome = (o) => outcomes.push(o);
  const none = { kind: "opfs", readMeta: async () => null, discard: async () => {} };
  assertEquals(await tryRestore({ go: {}, module: null, key: KEY, seqs: SEQS, store: none, log: () => {}, before: () => {}, outcome }), null);
  const stale = { kind: "opfs", readMeta: async () => meta("tab-a"), discard: async () => {} };
  await tryRestore({ go: {}, module: null, key: KEY, seqs: SEQS, owner: "tab-b", store: stale, log: () => {}, before: () => {}, outcome });
  const broken = { kind: "opfs", readMeta: async () => { throw new Error("bad meta"); }, discard: async () => {} };
  await tryRestore({ go: {}, module: null, key: KEY, seqs: SEQS, store: broken, log: () => {}, before: () => {}, outcome });
  assertEquals(outcomes, [
    "not restored: no snapshot stored (opfs)",
    "not restored: discarded, written by a tab that no longer owned the saved state",
    "not restored: unreadable, bad meta",
  ]);
});