mirror of
https://github.com/block/buzz.git
synced 2026-11-25 02:31:36 +00:00
Long-lived renderer sessions accumulate heap and timer churn until GC pauses queue behind input, degrading scroll and click latency. The Cmd+R clean-teardown reload clears it without touching the native engine or its child agents, but relies on the user noticing and acting. Add an idle backstop that performs the same reload automatically once a session is ~12h old and the user is provably away (OS idle >= 30 min and the window unfocused). It withholds the reload while any reload-destroyed volatile work is outstanding: a starting/active huddle, a foreground or background media upload including its completion settling window, queued local file attachments a draft cannot serialize, or any in-flight send/mutation/native operation. Persisted draft text survives via localStorage, so it is deliberately not a blocker. The reload primitive is one idempotent requestRendererReload() shared by Cmd+R and the backstop: a module-level in-flight promise collapses overlapping triggers so the native WebSocket teardown never runs twice, teardown is bounded to 500 ms, and a rejecting or hung teardown is swallowed so the reload always runs -- a rejection can no longer win the race and strand the renderer on a stale heap. All fire/withhold logic is a pure, exhaustively tested predicate; the hook only wires live signals, arms after the session-age threshold, and serializes overlapping checks. Signals are read imperatively at check time (once a minute) so the hook holds no reactive subscription and never re-renders. The controller mounts inside the ready main-window shell only, guarded so Huddle companion windows and every onboarding/blocking/reset/ keyring/relaunch screen are ineligible by construction. Outstanding volatile work is tracked through one app-global registry (volatileWorkRegistry) that owns acquire/release; the native multi-file picker seam registers at the choke point via withVolatileWork so every caller is covered by construction. The deferred paperclip path is deliberately left unregistered: useFilePicker's hidden input has no cancel signal, so acquiring on open risks a hold that never releases. Co-authored-by: Will Pfleger <pfleger.will@gmail.com> Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
274 lines
9.3 KiB
JavaScript
274 lines
9.3 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
createSerializedIdleReloadCheck,
|
|
IDLE_RELOAD_OS_IDLE_MS,
|
|
IDLE_RELOAD_SESSION_AGE_MS,
|
|
normalizeOsIdleMs,
|
|
runIdleReloadCheck,
|
|
shouldIdleReload,
|
|
} from "./idleAutoReloadPolicy.ts";
|
|
|
|
// ── shouldIdleReload gate matrix ─────────────────────────────────────────────
|
|
//
|
|
// SAFETY-CRITICAL: a wrong "true" navigates the webview out from under the
|
|
// user. The all-green baseline is the ONLY fire case; each never-fire row
|
|
// flips exactly one input away from green to prove that gate alone holds.
|
|
|
|
/** All gates open, session old, user away — the only true case. */
|
|
function greenInputs(overrides = {}) {
|
|
return {
|
|
osIdleMs: IDLE_RELOAD_OS_IDLE_MS,
|
|
sessionAgeMs: IDLE_RELOAD_SESSION_AGE_MS,
|
|
appFocused: false,
|
|
volatileWorkPending: false,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
test("shouldIdleReload_all_gates_green_returns_true", () => {
|
|
assert.equal(shouldIdleReload(greenInputs()), true);
|
|
});
|
|
|
|
const NEVER_FIRE_ROWS = [
|
|
["volatile work pending blocks", { volatileWorkPending: true }],
|
|
["focused app blocks", { appFocused: true }],
|
|
["young session blocks", { sessionAgeMs: IDLE_RELOAD_SESSION_AGE_MS - 1 }],
|
|
["null idle (unsupported platform) blocks", { osIdleMs: null }],
|
|
["below idle threshold blocks", { osIdleMs: IDLE_RELOAD_OS_IDLE_MS - 1 }],
|
|
];
|
|
|
|
for (const [name, override] of NEVER_FIRE_ROWS) {
|
|
test(`shouldIdleReload_${name.replace(/\s+/g, "_")}_returns_false`, () => {
|
|
assert.equal(shouldIdleReload(greenInputs(override)), false);
|
|
});
|
|
}
|
|
|
|
test("shouldIdleReload_exact_thresholds_are_inclusive", () => {
|
|
assert.equal(
|
|
shouldIdleReload(
|
|
greenInputs({
|
|
osIdleMs: IDLE_RELOAD_OS_IDLE_MS,
|
|
sessionAgeMs: IDLE_RELOAD_SESSION_AGE_MS,
|
|
}),
|
|
),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("shouldIdleReload_volatile_work_beats_all_other_green_gates", () => {
|
|
// Every precondition green but volatile work outstanding → still false.
|
|
assert.equal(
|
|
shouldIdleReload(greenInputs({ volatileWorkPending: true })),
|
|
false,
|
|
);
|
|
});
|
|
|
|
// ── runIdleReloadCheck orchestration ─────────────────────────────────────────
|
|
|
|
const HOUR = 60 * 60 * 1000;
|
|
|
|
/** Readers wired to fire: old session, away, all clear. */
|
|
function greenReaders(overrides = {}) {
|
|
const calls = { reload: 0 };
|
|
const base = {
|
|
now: () => 100 * HOUR,
|
|
sessionLoadedAtMs: 100 * HOUR - IDLE_RELOAD_SESSION_AGE_MS,
|
|
isAppFocused: () => false,
|
|
getOsIdleMs: async () => IDLE_RELOAD_OS_IDLE_MS,
|
|
getHuddleActive: async () => false,
|
|
isVolatileWorkPending: () => false,
|
|
reload: async () => {
|
|
calls.reload += 1;
|
|
},
|
|
};
|
|
return { readers: { ...base, ...overrides }, calls };
|
|
}
|
|
|
|
test("runIdleReloadCheck_fires_reload_when_all_conditions_hold", async () => {
|
|
const { readers, calls } = greenReaders();
|
|
assert.equal(await runIdleReloadCheck(readers), true);
|
|
assert.equal(calls.reload, 1);
|
|
});
|
|
|
|
test("runIdleReloadCheck_young_session_skips_before_any_ipc", async () => {
|
|
let osIdleReads = 0;
|
|
let huddleReads = 0;
|
|
const { readers, calls } = greenReaders({
|
|
sessionLoadedAtMs: 100 * HOUR - (IDLE_RELOAD_SESSION_AGE_MS - 1),
|
|
getOsIdleMs: async () => {
|
|
osIdleReads += 1;
|
|
return IDLE_RELOAD_OS_IDLE_MS;
|
|
},
|
|
getHuddleActive: async () => {
|
|
huddleReads += 1;
|
|
return false;
|
|
},
|
|
});
|
|
assert.equal(await runIdleReloadCheck(readers), false);
|
|
assert.equal(calls.reload, 0);
|
|
// A present-enough session must not incur the per-minute IPC reads.
|
|
assert.equal(osIdleReads, 0);
|
|
assert.equal(huddleReads, 0);
|
|
});
|
|
|
|
test("runIdleReloadCheck_focused_user_skips_before_any_ipc", async () => {
|
|
let osIdleReads = 0;
|
|
const { readers, calls } = greenReaders({
|
|
isAppFocused: () => true,
|
|
getOsIdleMs: async () => {
|
|
osIdleReads += 1;
|
|
return IDLE_RELOAD_OS_IDLE_MS;
|
|
},
|
|
});
|
|
assert.equal(await runIdleReloadCheck(readers), false);
|
|
assert.equal(calls.reload, 0);
|
|
assert.equal(osIdleReads, 0);
|
|
});
|
|
|
|
test("runIdleReloadCheck_huddle_active_blocks_reload", async () => {
|
|
const { readers, calls } = greenReaders({
|
|
getHuddleActive: async () => true,
|
|
});
|
|
assert.equal(await runIdleReloadCheck(readers), false);
|
|
assert.equal(calls.reload, 0);
|
|
});
|
|
|
|
test("runIdleReloadCheck_volatile_work_blocks_reload", async () => {
|
|
// Stands in for a foreground/background upload, queued local attachment, or
|
|
// in-flight send/mutation/native operation — the hook folds all into one read.
|
|
const { readers, calls } = greenReaders({
|
|
isVolatileWorkPending: () => true,
|
|
});
|
|
assert.equal(await runIdleReloadCheck(readers), false);
|
|
assert.equal(calls.reload, 0);
|
|
});
|
|
|
|
test("runIdleReloadCheck_null_os_idle_blocks_reload", async () => {
|
|
const { readers, calls } = greenReaders({
|
|
getOsIdleMs: async () => null,
|
|
});
|
|
assert.equal(await runIdleReloadCheck(readers), false);
|
|
assert.equal(calls.reload, 0);
|
|
});
|
|
|
|
// ── normalizeOsIdleMs — the extracted reader normalizer ──────────────────────
|
|
//
|
|
// The MINOR finding: prove a rejecting getOsIdleSeconds withholds the reload
|
|
// this tick, then a later successful read lets it fire. The normalizer is the
|
|
// seam that collapses all three "cannot confirm away" outcomes to null; it was
|
|
// extracted to the pure module precisely so this path tests without the hook's
|
|
// React/timer/IPC weight.
|
|
|
|
test("normalizeOsIdleMs_seconds_convert_to_milliseconds", async () => {
|
|
assert.equal(await normalizeOsIdleMs(async () => 42), 42_000);
|
|
});
|
|
|
|
test("normalizeOsIdleMs_null_reader_stays_null", async () => {
|
|
assert.equal(await normalizeOsIdleMs(async () => null), null);
|
|
});
|
|
|
|
test("normalizeOsIdleMs_rejecting_reader_collapses_to_null", async () => {
|
|
// A failed idle IPC must never surface as a numeric idle the policy could
|
|
// read as "away" — it collapses to null, which shouldIdleReload withholds on.
|
|
assert.equal(
|
|
await normalizeOsIdleMs(async () => {
|
|
throw new Error("idle IPC failed");
|
|
}),
|
|
null,
|
|
);
|
|
});
|
|
|
|
test("idle_read_rejection_withholds_this_tick_then_retry_fires", async () => {
|
|
// The full MINOR contract through the real normalizer + orchestrator: the
|
|
// first tick's idle read rejects → normalizeOsIdleMs → null → withheld; the
|
|
// next tick's read succeeds → the reload fires. No stuck state between ticks.
|
|
let attempt = 0;
|
|
const getOsIdleSeconds = async () => {
|
|
attempt += 1;
|
|
if (attempt === 1) throw new Error("transient idle IPC failure");
|
|
return IDLE_RELOAD_OS_IDLE_MS / 1000;
|
|
};
|
|
const { readers, calls } = greenReaders({
|
|
getOsIdleMs: () => normalizeOsIdleMs(getOsIdleSeconds),
|
|
});
|
|
|
|
assert.equal(await runIdleReloadCheck(readers), false);
|
|
assert.equal(calls.reload, 0, "a rejected idle read must withhold this tick");
|
|
|
|
assert.equal(await runIdleReloadCheck(readers), true);
|
|
assert.equal(calls.reload, 1, "the next tick's successful read must fire");
|
|
});
|
|
|
|
test("runIdleReloadCheck_user_returns_during_ipc_await_blocks_reload", async () => {
|
|
// Focus flips to true while the async idle/huddle reads are in flight; the
|
|
// post-await re-read of the synchronous focus signal must catch it.
|
|
let focused = false;
|
|
const { readers, calls } = greenReaders({
|
|
isAppFocused: () => focused,
|
|
getOsIdleMs: async () => {
|
|
focused = true; // user touched the machine mid-check
|
|
return IDLE_RELOAD_OS_IDLE_MS;
|
|
},
|
|
});
|
|
assert.equal(await runIdleReloadCheck(readers), false);
|
|
assert.equal(calls.reload, 0);
|
|
});
|
|
|
|
test("runIdleReloadCheck_volatile_work_appears_during_ipc_await_blocks_reload", async () => {
|
|
let busy = false;
|
|
const { readers, calls } = greenReaders({
|
|
isVolatileWorkPending: () => busy,
|
|
getHuddleActive: async () => {
|
|
busy = true; // a send/upload began mid-check
|
|
return false;
|
|
},
|
|
});
|
|
assert.equal(await runIdleReloadCheck(readers), false);
|
|
assert.equal(calls.reload, 0);
|
|
});
|
|
|
|
// ── createSerializedIdleReloadCheck ──────────────────────────────────────────
|
|
|
|
test("serialized_check_drops_ticks_while_a_check_is_pending", async () => {
|
|
let releaseIdle;
|
|
let osIdleReads = 0;
|
|
const { readers, calls } = greenReaders({
|
|
getOsIdleMs: () => {
|
|
osIdleReads += 1;
|
|
return new Promise((resolve) => {
|
|
releaseIdle = () => resolve(IDLE_RELOAD_OS_IDLE_MS);
|
|
});
|
|
},
|
|
});
|
|
const check = createSerializedIdleReloadCheck(readers);
|
|
check(); // starts a check that parks on the idle read
|
|
check(); // second tick lands while the first is pending — dropped
|
|
check();
|
|
assert.equal(osIdleReads, 1);
|
|
releaseIdle();
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
assert.equal(calls.reload, 1);
|
|
});
|
|
|
|
test("serialized_check_runs_again_after_the_previous_check_settles", async () => {
|
|
let osIdleReads = 0;
|
|
const { readers, calls } = greenReaders({
|
|
getOsIdleMs: async () => {
|
|
osIdleReads += 1;
|
|
return IDLE_RELOAD_OS_IDLE_MS;
|
|
},
|
|
reload: async () => {
|
|
calls.reload += 1;
|
|
},
|
|
});
|
|
const check = createSerializedIdleReloadCheck(readers);
|
|
check();
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
check();
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
// Both ticks ran to completion because each settled before the next.
|
|
assert.equal(osIdleReads, 2);
|
|
});
|