Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 81 additions & 7 deletions bin/omarchy-plugin-update
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export GIT_SSH_COMMAND="${GIT_SSH_COMMAND:-ssh -oBatchMode=yes}"
PLUGINS_DIR="$HOME/.config/omarchy/plugins"
ASSUME_YES=0
UPDATED_ANY=0
WATCH_SUSPENDED=0
SHELL_AVAILABLE=0

fail() {
echo "omarchy-plugin-update: $*" >&2
Expand All @@ -36,7 +38,7 @@ valid_plugin_id() {
[[ $1 =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ && $1 != *..* ]]
}

update_one() {
prepare_one() {
local id="$1"
local dir="$PLUGINS_DIR/$id"

Expand All @@ -47,7 +49,7 @@ update_one() {

if [[ $(git -C "$dir" rev-parse HEAD) == $(git -C "$dir" rev-parse FETCH_HEAD) ]]; then
echo "$id is up to date."
return 0
return 2
fi

if (( ! ASSUME_YES )); then
Expand All @@ -60,10 +62,17 @@ update_one() {
echo
confirm "Update $id?" || {
echo "Skipped $id."
return 0
return 2
}
fi

return 0
}

apply_one() {
local id="$1"
local dir="$PLUGINS_DIR/$id"

if ! git -C "$dir" merge --ff-only FETCH_HEAD >/dev/null 2>&1; then
echo "omarchy-plugin-update: cannot fast-forward '$id'; you have local changes in $dir" >&2
return 1
Expand All @@ -79,6 +88,42 @@ update_one() {
UPDATED_ANY=1
}

resume_local_plugin_watch() {
(( WATCH_SUSPENDED )) || return 0
WATCH_SUSPENDED=0

if (( SHELL_AVAILABLE )); then
if ! omarchy-shell shell setLocalPluginWatch true >/dev/null; then
echo "omarchy-plugin-update: warning: could not resume local plugin watch; restart omarchy-shell" >&2
return 1
fi
fi
}

cleanup() {
local status=$?
trap - EXIT
resume_local_plugin_watch || true
exit "$status"
}

suspend_local_plugin_watch() {
SHELL_AVAILABLE=0
if omarchy-shell shell ping >/dev/null 2>&1; then
SHELL_AVAILABLE=1
fi
Comment on lines +111 to +114

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Busy shell skips suspension

ping failure does not prove the shell has stopped. omarchy-shell also fails when a live shell takes longer than its timeout to answer. This branch then skips suspension and allows the merge. If the shell recovers during the merge, its watcher can reload partially written plugins—the race this PR aims to fix.

Allow mutation without suspension only when the shell is confirmed absent. Abort on an unanswered live shell.

Knowledge Base Used: CLI command platform


# No running shell means there is no live watcher to race. If a shell is
# reachable, however, suspension is part of the mutation invariant: do not
# write the plugin tree if that request fails (including an older shell that
# does not yet expose setLocalPluginWatch).
if (( SHELL_AVAILABLE )); then
omarchy-shell shell setLocalPluginWatch false >/dev/null ||
fail "running shell could not suspend the local plugin watcher; restart the shell and retry"
WATCH_SUSPENDED=1
fi
}

id=""

while (( $# > 0 )); do
Expand Down Expand Up @@ -122,12 +167,41 @@ else
fi
fi

# Serialize complete update sessions so two callers cannot independently
# suspend/resume the one global watcher or merge the same checkout concurrently.
runtime_dir="${XDG_RUNTIME_DIR:-/tmp}"
lock_file="${OMARCHY_PLUGIN_UPDATE_LOCK:-$runtime_dir/omarchy-plugin-update-$UID.lock}"
exec 9>"$lock_file" || fail "cannot open update lock: $lock_file"
Comment on lines +172 to +174

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 security Another user blocks updates

If XDG_RUNTIME_DIR is unset, the new lock uses a predictable file in /tmp. Another local user can create /tmp/omarchy-plugin-update-<victim UID>.lock without granting the victim write access. Opening the lock then fails, and the victim cannot remove the other user's file from sticky /tmp.

Use a private user-owned directory for the fallback.

How this was verified: The fallback opens an attacker-precreated shared-directory file for writing and aborts when that file denies write access.

flock 9 || fail "cannot acquire plugin update lock"

planned=()
rc=0
for id in "${targets[@]}"; do
update_one "$id" || rc=1
if prepare_one "$id"; then
planned+=("$id")
else
status=$?
(( status == 2 )) || rc=1
fi
done

if (( UPDATED_ANY )); then
omarchy-shell shell rescanPlugins >/dev/null
# Nothing will mutate the tree, so leave the watcher untouched.
if (( ${#planned[@]} == 0 )); then
exit "$rc"
fi
exit $rc

suspend_local_plugin_watch
trap cleanup EXIT
Comment on lines +193 to +194

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Failed request leaves watch disabled

Cleanup is installed only after the suspension request succeeds. The shell can apply setLocalPluginWatch false, but its reply can time out or the updater can be interrupted before it records success. The updater then exits without restoring the watcher, leaving local plugin reloads disabled until the shell restarts.

Install cleanup before requesting suspension and record that restoration may be needed before sending the request.


for id in "${planned[@]}"; do
apply_one "$id" || rc=1
done

if (( UPDATED_ANY )) && (( SHELL_AVAILABLE )); then
omarchy-shell shell rescanPlugins >/dev/null ||
echo "omarchy-plugin-update: warning: shell rescan failed; updated plugins will load on the next shell rescan" >&2
Comment on lines +200 to +202

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Failed update loses pending reload

Suspension stops localPluginReloadTimer and discards watch events, but the final rescan runs only after a successful update. If a local edit has a pending reload and every planned merge fails or rolls back, that reload is canceled and never replaced. The shell keeps showing the old plugin code until another edit or a manual reload.

Request a rescan after every suspended mutation phase, including failed updates, so existing edits are not lost.

fi

resume_local_plugin_watch || true
trap - EXIT
exit "$rc"
26 changes: 23 additions & 3 deletions shell/services/PluginRegistry.qml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ QtObject {
signal pluginLoadFailed(string id, string error)
signal localPluginChanged(string id)

// Disabled only while a host-controlled plugin mutation is in progress.
// Events already buffered by inotify are ignored while false.
property bool localPluginWatchWanted: true

// ---------------------------------------------------------------- helpers

function isSafeEntryPoint(value) {
Expand Down Expand Up @@ -655,7 +659,7 @@ QtObject {

property Process initProcess: Process {
onExited: {
localPluginWatcher.running = true
if (registry.localPluginWatchWanted) localPluginWatcher.running = true
registry.rescan()
}
}
Expand All @@ -674,16 +678,32 @@ QtObject {
]
stdout: SplitParser {
onRead: function(path) {
if (!registry.localPluginWatchWanted) return
var pluginId = registry.localPluginIdForPath(path)
if (pluginId) registry.localPluginChanged(pluginId)
}
}
onExited: localPluginWatcherRestart.restart()
onExited: {
if (registry.localPluginWatchWanted) localPluginWatcherRestart.restart()
}
}

property Timer localPluginWatcherRestart: Timer {
interval: 1000
onTriggered: localPluginWatcher.running = true
onTriggered: {
if (registry.localPluginWatchWanted) localPluginWatcher.running = true
}
}

function setLocalPluginWatch(enabled) {
var want = !!enabled
registry.localPluginWatchWanted = want
if (!want) {
localPluginWatcherRestart.stop()
if (localPluginWatcher.running) localPluginWatcher.running = false
return
}
if (!localPluginWatcher.running) localPluginWatcher.running = true
}

function rescan() {
Expand Down
12 changes: 11 additions & 1 deletion shell/shell.qml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ ShellRoot {

Timer {
id: localPluginReloadTimer
interval: 150
// Recursive plugin edits can emit large write bursts. Update suspends the
// watcher around its own merge; this trailing window protects ordinary
// local edits from reloading while a write burst is still settling.
interval: 500
onTriggered: shell.reloadPlugins()
}

Expand Down Expand Up @@ -1593,6 +1596,13 @@ ShellRoot {
shell.reloadPlugins()
}

function setLocalPluginWatch(enabled: string): string {
var on = enabled === "true" || enabled === "1" || enabled === "on"
shell.pluginRegistry.setLocalPluginWatch(on)
if (!on) localPluginReloadTimer.stop()
return on ? "watching" : "suspended"
Comment on lines +1599 to +1603

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Existing reload keeps running

setLocalPluginWatch(false) stops new watch events, but it does not wait for a reload already in progress. A local edit during preparation can start reloadPlugins() before suspension. Its pending finishPluginReload, running scan, or asynchronous component load then continues while the updater merges files. This can still load an incomplete plugin tree and trigger the original crash.

The suspension call must wait for existing plugin reads and loads to finish, or refuse mutation until they finish.

Knowledge Base Used: Shell UI and desktop interactions

}

function reloadConfig(): string {
userConfigFile.reload()
return "ok"
Expand Down
171 changes: 171 additions & 0 deletions test/shell.d/plugin-update-watch-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"
require_command git
require_command flock

test_dir=$(mktemp -d)
trap 'rm -rf "$test_dir"' EXIT
mkdir -p "$test_dir/bin" "$test_dir/home/.config/omarchy/plugins" "$test_dir/remote"
export CALL_LOG="$test_dir/calls"
export VALIDATE_STARTED="$test_dir/validate-started"
export VALIDATE_RELEASE="$test_dir/validate-release"
export OMARCHY_PLUGIN_UPDATE_LOCK="$test_dir/update.lock"
: >"$CALL_LOG"

cat >"$test_dir/bin/omarchy-shell" <<'SH'
#!/bin/bash
printf '%s\n' "$*" >>"$CALL_LOG"

if [[ $* == "shell ping" ]]; then
exit ${SHELL_AVAILABLE:-0}
fi

if [[ $* == "shell setLocalPluginWatch false" && ${FAIL_SUSPEND:-0} == 1 ]]; then
exit 1
fi

exit 0
SH

cat >"$test_dir/bin/omarchy-plugin-validate" <<'SH'
#!/bin/bash
if [[ ${BLOCK_VALIDATE:-0} == 1 && ! -e $VALIDATE_RELEASE ]]; then
touch "$VALIDATE_STARTED"
for _ in {1..500}; do
[[ -e $VALIDATE_RELEASE ]] && break
sleep 0.01
done
fi
exit 0
SH

cat >"$test_dir/bin/omarchy-cmd-present" <<'SH'
#!/bin/bash
exit 1
SH
chmod +x "$test_dir/bin/"*

remote="$test_dir/remote/acme.watch.git"
git init -q --bare "$remote"

work="$test_dir/work"
mkdir -p "$work"
cat >"$work/manifest.json" <<'JSON'
{
"schemaVersion": 1,
"id": "acme.watch",
"name": "Watch",
"version": "1.0.0",
"kinds": ["bar-widget"],
"entryPoints": { "barWidget": "Widget.qml" },
"barWidget": {
"displayName": "Watch",
"category": "Test",
"allowMultiple": false
}
}
JSON
printf 'import QtQuick\nItem {}\n' >"$work/Widget.qml"
git -C "$work" init -q
git -C "$work" add .
git -C "$work" -c user.name=Test -c user.email=test@example.com commit -qm "v1"
git -C "$work" branch -M main
git -C "$work" remote add origin "$remote"
git -C "$work" push -q origin main
git -C "$remote" symbolic-ref HEAD refs/heads/main

installed="$test_dir/home/.config/omarchy/plugins/acme.watch"
git clone -q -b main "$remote" "$installed"

advance_remote() {
local n="$1"
printf 'import QtQuick\nItem { readonly property int n: %s }\n' "$n" >"$work/Widget.qml"
git -C "$work" add Widget.qml
git -C "$work" -c user.name=Test -c user.email=test@example.com commit -qm "v$n"
git -C "$work" push -q origin main
}

run_update() {
HOME="$test_dir/home" OMARCHY_PATH="$ROOT" PATH="$test_dir/bin:$ROOT/bin:$PATH" \
XDG_RUNTIME_DIR="$test_dir" OMARCHY_PLUGIN_UPDATE_LOCK="$OMARCHY_PLUGIN_UPDATE_LOCK" \
"$ROOT/bin/omarchy-plugin-update" acme.watch --yes
}

advance_remote 2
run_update >"$test_dir/out" 2>&1

mapfile -t calls <"$CALL_LOG"
[[ ${calls[0]} == "shell ping" ]] ||
fail "update probes whether a live watcher exists before suspension" "$(cat "$CALL_LOG")"
[[ ${calls[1]} == "shell setLocalPluginWatch false" ]] ||
fail "update suspends the local plugin watcher before mutation" "$(cat "$CALL_LOG")"
[[ ${calls[-2]} == "shell rescanPlugins" ]] ||
fail "update rescans once after successful mutation" "$(cat "$CALL_LOG")"
[[ ${calls[-1]} == "shell setLocalPluginWatch true" ]] ||
fail "update resumes the local plugin watcher after rescan" "$(cat "$CALL_LOG")"
grep -qF "Updated acme.watch." "$test_dir/out" ||
fail "update reports success" "$(cat "$test_dir/out")"
pass "plugin update suspends, mutates, rescans, and resumes in order"

# A reachable shell that cannot suspend the watcher must fail before HEAD moves.
advance_remote 3
before=$(git -C "$installed" rev-parse HEAD)
: >"$CALL_LOG"
if FAIL_SUSPEND=1 run_update >"$test_dir/fail-out" 2>&1; then
fail "update proceeds when a live shell rejects watcher suspension"
fi
after=$(git -C "$installed" rev-parse HEAD)
[[ $before == "$after" ]] ||
fail "failed watcher suspension allowed the plugin checkout to mutate"
grep -qF "shell setLocalPluginWatch false" "$CALL_LOG" ||
fail "fail-closed path did not attempt watcher suspension"
! grep -qF "shell rescanPlugins" "$CALL_LOG" ||
fail "failed watcher suspension should not request a rescan"
pass "live-shell watcher suspension fails closed before mutation"

# Two update processes share one mutation/session lock. The second must not
# suspend or mutate while the first is blocked inside validation.
: >"$CALL_LOG"
rm -f "$VALIDATE_STARTED" "$VALIDATE_RELEASE"
BLOCK_VALIDATE=1 run_update >"$test_dir/first-out" 2>&1 &
first_pid=$!

for _ in {1..300}; do
[[ -e $VALIDATE_STARTED ]] && break
sleep 0.01
done
[[ -e $VALIDATE_STARTED ]] || {
kill "$first_pid" 2>/dev/null || true
fail "first updater did not reach the blocked mutation phase"
}

# The first updater has already merged, so the second would report "up to date"
# even unserialized; only the held lock shows the session is owned.
if flock -n "$OMARCHY_PLUGIN_UPDATE_LOCK" true; then
touch "$VALIDATE_RELEASE"
wait "$first_pid" || true
fail "first updater does not hold the update lock while mutating"
fi

BLOCK_VALIDATE=0 run_update >"$test_dir/second-out" 2>&1 &
second_pid=$!
sleep 0.2

suspends=$(grep -cF "shell setLocalPluginWatch false" "$CALL_LOG" || true)
(( suspends == 1 )) ||
fail "overlapping updater acquired watcher ownership before the first finished" "$(cat "$CALL_LOG")"

touch "$VALIDATE_RELEASE"
wait "$first_pid"
wait "$second_pid"

suspends=$(grep -cF "shell setLocalPluginWatch false" "$CALL_LOG" || true)
resumes=$(grep -cF "shell setLocalPluginWatch true" "$CALL_LOG" || true)
(( suspends == 1 && resumes == 1 )) ||
fail "serialized updaters should produce one suspend/resume mutation cycle" "$(cat "$CALL_LOG")"
grep -qF "acme.watch is up to date." "$test_dir/second-out" ||
fail "second updater did not observe the first updater's completed state" "$(cat "$test_dir/second-out")"
pass "overlapping plugin updates serialize before watcher ownership and mutation"