diff --git a/docs/WIDGET-INTEROP-PLAN.md b/docs/WIDGET-INTEROP-PLAN.md index cb5aaa4bd..3e72ee210 100644 --- a/docs/WIDGET-INTEROP-PLAN.md +++ b/docs/WIDGET-INTEROP-PLAN.md @@ -209,7 +209,7 @@ We ship at **level 7 minus `Terminal`**, which covers 102 of the 105 store widge * **Compile errors** turn that cell into the error card, with the caret message and "Tap to fix with AI", and write `.errors/.txt`. * **Run-time errors** (a cap that was hit, a bad value) show `…` in place and are appended to `.errors/` (deduplicated, the last 20 kept), as the guide describes. -* **Renderer faults** that could still take the shell down (a QML binding loop in our node code, a huge image, a Canvas that stalls the scene graph) are covered by the **existing crash guard**: `OwlCell` calls `guardBegin(id, hash)` before the first render and `guardEnd` after it has run for 3 s, as `UserWidgetCell` does. A widget whose load crashed the shell is held back at the next start. Every crash we find this way becomes a conformance test and a fix in our runtime. +* **Renderer faults** that could still take the shell down (a QML binding loop in our node code, a huge image, a Canvas that stalls the scene graph) are covered by the **existing crash guard**: `OwlCell` calls `guardBegin(id, hash)` before the first render and `guardEnd` after it has run for 3 s, as `UserWidgetCell` does. A widget whose load crashed the shell is held back at the next start. The guard file carries the boot id it was written in. A shell death inside the same boot is a crash. A widget still loading when the system itself went down (a reboot, a shutdown, a dead battery, or a system crash it caused) gets one retry: it loads again as a suspect, and is held back if it is still loading at the next start; a clean run clears it. Quickshell dies on the session's SIGTERM without tearing anything down, so ordinary reboots look like this. A deliberate quit (a shell restart, a logout) clears what was still loading from Home's `Component.onDestruction` (omamobile-shell 0.2.64). Every crash we find this way becomes a conformance test and a fix in our runtime. * **Instances outlive cells.** Layout edits rebuild Home's cells. The OWL instance (state, source values, history) lives in a per-placement registry in `Home.qml`, so a rebuild reattaches without re-running sources, which is what `host.cached` does for QML. --- diff --git a/packages/omamobile-shell/plugins/omamobile.home/Home.qml b/packages/omamobile-shell/plugins/omamobile.home/Home.qml index c798aae34..1eda58941 100644 --- a/packages/omamobile-shell/plugins/omamobile.home/Home.qml +++ b/packages/omamobile-shell/plugins/omamobile.home/Home.qml @@ -242,9 +242,24 @@ Item { // (written synchronously) while it loads and for its first seconds; an // entry still there at the next start was running when the shell died, // and that widget stays off until its files change (a new hash). + // The file also carries the boot id it was written in. A widget still + // "loading" from another boot was most likely cut off by a reboot or a + // dead battery (Quickshell dies on the session's SIGTERM without tearing + // anything down), but it could also have brought the whole system down. + // So it gets one retry: it loads again and is kept as a suspect, and if + // it is still loading when the next start finds it (a second unclean + // end in a row), it is held back as a crash. A clean run clears it. readonly property string guardPath: configDir + "/widget-guard.json" property var guardLoading: ({}) property var guardCrashed: ({}) + property var guardSuspect: ({}) + FileView { + id: bootIdFile + path: "/proc/sys/kernel/random/boot_id" + blockLoading: true + printErrors: false + } + readonly property string bootId: String(bootIdFile.text() || "").trim() FileView { id: guardFile path: root.guardPath @@ -255,16 +270,42 @@ Item { onLoaded: { var data = {} try { data = JSON.parse(text() || "{}") || {} } catch (e) { data = {} } - root.guardCrashed = Object.assign({}, data.crashed || {}, data.loading || {}) - root.guardLoading = ({}) - Object.keys(data.loading || {}).forEach(function(id) { - console.warn("omamobile.home: widget " + id + " was loading when the shell stopped; it stays off until it changes") + var loading = data.loading || {} + var sameBoot = !root.bootId || !data.boot || data.boot === root.bootId + var crashed = Object.assign({}, data.crashed || {}) + var suspect = Object.assign({}, data.suspect || {}) + Object.keys(loading).forEach(function(id) { + if (sameBoot || suspect[id] === loading[id]) { + crashed[id] = loading[id] + delete suspect[id] + console.warn("omamobile.home: widget " + id + (sameBoot + ? " was loading when the shell stopped" + : " was loading when the system went down twice in a row") + + "; it stays off until it changes") + } else { + suspect[id] = loading[id] + console.warn("omamobile.home: widget " + id + " was loading when the system went down last boot; it loads once more as a suspect") + } }) + root.guardCrashed = crashed + root.guardSuspect = suspect + root.guardLoading = ({}) root.guardWrite() } } + // A deliberate quit (omarchy-restart-shell's `quickshell kill`, a logout + // through Omarchy) tears the QML tree down, and Home's onDestruction runs; + // a crash or a signal never gets there. Quickshell 0.3 doesn't deliver + // Qt.application's aboutToQuit to QML, so this is the hook. A quit clears + // what was still loading, and only a shell that died leaves entries + // behind for the next start. + Component.onDestruction: { + if (Object.keys(root.guardLoading).length === 0) return + root.guardLoading = ({}) + root.guardWrite() + } function guardWrite() { - guardFile.setText(JSON.stringify({ loading: guardLoading, crashed: guardCrashed }) + "\n") + guardFile.setText(JSON.stringify({ boot: bootId, loading: guardLoading, crashed: guardCrashed, suspect: guardSuspect }) + "\n") } function guardBegin(widgetId, hash) { var next = Object.assign({}, guardLoading); next[widgetId] = hash @@ -273,10 +314,12 @@ Item { // `ran`: the widget has now run cleanly, so an older crash of it is forgotten. function guardEnd(widgetId, ran) { var crashed = ran && (widgetId in guardCrashed) - if (!(widgetId in guardLoading) && !crashed) return + var suspect = ran && (widgetId in guardSuspect) + if (!(widgetId in guardLoading) && !crashed && !suspect) return var next = Object.assign({}, guardLoading); delete next[widgetId] guardLoading = next if (crashed) { var c = Object.assign({}, guardCrashed); delete c[widgetId]; guardCrashed = c } + if (suspect) { var x = Object.assign({}, guardSuspect); delete x[widgetId]; guardSuspect = x } guardWrite() } diff --git a/packages/omamobile-shell/test/all b/packages/omamobile-shell/test/all index b7c7c0376..5925626cc 100755 --- a/packages/omamobile-shell/test/all +++ b/packages/omamobile-shell/test/all @@ -925,6 +925,26 @@ pass "session environment" bash -n bin/omamobile-setup-user || fail "omamobile-setup-user parses" bash test/setup-user.sh || fail "omamobile-setup-user against a scratch home" bash -n bin/omamobile-osk-guard || fail "omamobile-osk-guard parses" +# The widget crash guard counts a shell death inside the same boot as a +# crash. A widget still "loading" from another boot (reboot, shutdown, dead +# battery, or a system crash it caused) gets one retry as a suspect, and is +# held back if it is still loading at the next start; a clean run clears it. +h=plugins/omamobile.home/Home.qml +grep -c 'path: "/proc/sys/kernel/random/boot_id"' $h >/dev/null || fail "the crash guard reads the boot id" +grep -c 'JSON.stringify({ boot: bootId, loading: guardLoading, crashed: guardCrashed, suspect: guardSuspect })' $h >/dev/null || fail "the crash guard records the boot id and the suspects" +grep -c 'if (sameBoot || suspect\[id\] === loading\[id\]) {' $h >/dev/null || fail "a same-boot death or a second unclean end in a row is a crash" +grep -c 'suspect\[id\] = loading\[id\]' $h >/dev/null || fail "a widget loading in another boot is a suspect, not a crash" +grep -c 'var suspect = ran && (widgetId in guardSuspect)' $h >/dev/null || fail "a clean run clears a suspect" +grep -c 'Component.onDestruction: {' $h >/dev/null || fail "a deliberate quit clears what was still loading" +grep -c 'target: Qt.application' $h >/dev/null && fail "Quickshell 0.3 never delivers aboutToQuit to QML" +rc=0; out=$(bash test/widget-guard 2>&1) || rc=$? +if [[ $rc == 77 ]]; then + pass "widget crash guard across starts and boots # SKIP no quickshell" +elif [[ $rc != 0 ]]; then + printf '%s\n' "$out" | grep -v '^ok' >&2; fail "widget crash guard (test/widget-guard)" +else + pass "widget crash guard: same-boot death held back, one retry after a system going down, clean run and quit clear" +fi bash test/osk-guard.sh || fail "omamobile-osk-guard" bash -n bin/omamobile-sleepd || fail "omamobile-sleepd parses" bash test/sleepd.sh || fail "omamobile-sleepd" diff --git a/packages/omamobile-shell/test/widget-guard b/packages/omamobile-shell/test/widget-guard new file mode 100755 index 000000000..abd01f7b9 --- /dev/null +++ b/packages/omamobile-shell/test/widget-guard @@ -0,0 +1,79 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# The home screen's widget crash guard, run for real: the guard block cut +# out of Home.qml into a bare Quickshell scope, driven start by start with a +# scratch config directory. A shell that dies in the same boot holds the +# widget back; a widget still loading when the system went down loads once +# more as a suspect and is held back after a second unclean end in a row; a +# clean run clears a suspect; a new hash starts over; a guard file from +# before the boot id keeps the strict rule; `quickshell kill` (a deliberate +# quit) clears what was still loading. Offscreen. Exits 77 without +# quickshell. +set -euo pipefail +here=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) +command -v quickshell >/dev/null || { echo "widget-guard: no quickshell" >&2; exit 77; } + +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +python3 - "$here/../plugins/omamobile.home/Home.qml" "$work/shell.qml" <<'PY' +import sys +s = open(sys.argv[1]).read() +a = s.index(' // Crash guard.') +b = s.index('\n }\n', s.index(' function guardEnd(')) + 4 +open(sys.argv[2], 'w').write('''import QtQuick +import Quickshell +import Quickshell.Io +Scope { + id: root + readonly property string configDir: Quickshell.env("GUARD_DIR") +''' + s[a:b] + ''' + Timer { + running: true; interval: 500 + onTriggered: (Quickshell.env("GUARD_ACT") || "").split(",").forEach(function(a) { + var p = a.split(":") + if (p[0] === "begin") root.guardBegin(p[1], p[2]) + if (p[0] === "end") root.guardEnd(p[1], true) + }) + } +} +''') +PY + +export QT_QPA_PLATFORM=offscreen QT_QUICK_BACKEND=software QML_DISABLE_DISK_CACHE=1 +# Quickshell needs a runtime directory (for its instance files); a CI job +# or an SSH login may have none. +if [[ -z ${XDG_RUNTIME_DIR:-} || ! -w ${XDG_RUNTIME_DIR:-/nonexistent} ]]; then + export XDG_RUNTIME_DIR=$work/run + mkdir -m 700 -p "$XDG_RUNTIME_DIR" +fi +n=0 rc=0 +dir= +fresh() { n=$((n + 1)); dir=$work/c$n; mkdir -p "$dir"; } +start() { GUARD_DIR=$dir GUARD_ACT=${1:-} timeout 2.5 quickshell -n -p "$work/shell.qml" >/dev/null 2>&1 || true; } +reboot() { python3 -c 'import json,sys; p=sys.argv[1]; d=json.load(open(p)); d["boot"]="another-boot"; json.dump(d, open(p, "w"))' "$dir/widget-guard.json"; } +expect() { + local got + got=$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(json.dumps([d.get("loading"), d.get("crashed"), d.get("suspect")], sort_keys=True))' "$dir/widget-guard.json") + [[ $got == "$2" ]] && echo "ok - $1" || { echo "not ok - $1: [loading, crashed, suspect] $got, want $2" >&2; rc=1; } +} + +fresh; start begin:w:h1; start +expect "a shell death in the same boot holds the widget back" '[{}, {"w": "h1"}, {}]' +fresh; start begin:w:h1; reboot; start +expect "loading when the system went down: a suspect, not a crash" '[{}, {}, {"w": "h1"}]' +start begin:w:h1; reboot; start +expect "a suspect still loading after a second unclean end is held back" '[{}, {"w": "h1"}, {}]' +fresh; start begin:w:h1; reboot; start begin:w:h1,end:w +expect "a clean run clears a suspect" '[{}, {}, {}]' +fresh; start begin:w:h1; reboot; start begin:w:h2; reboot; start +expect "a widget with new files starts over as a suspect" '[{}, {}, {"w": "h2"}]' +fresh; echo '{"loading": {"w": "h1"}, "crashed": {}}' > "$dir/widget-guard.json"; start +expect "a guard file without a boot id keeps the strict rule" '[{}, {"w": "h1"}, {}]' +fresh +GUARD_DIR=$dir GUARD_ACT=begin:w:h1 quickshell -n -p "$work/shell.qml" >/dev/null 2>&1 & +qs=$! +for _ in $(seq 1 40); do grep -c '"w"' "$dir/widget-guard.json" >/dev/null 2>&1 && break; sleep 0.1; done +quickshell kill --pid "$qs" >/dev/null 2>&1 || kill "$qs" +wait "$qs" 2>/dev/null || true +expect "a deliberate quit clears what was still loading" '[{}, {}, {}]' +exit $rc