From c5c9152dc51817c5d7d4c76fa59c366cdab61af9 Mon Sep 17 00:00:00 2001 From: Taksh Date: Tue, 25 Aug 2026 08:01:56 +0530 Subject: [PATCH 1/4] Repair Copy URL after install-time migration stamping Fresh installs mark 1786643346 complete before a Chromium profile exists, so a later-synced ghost shortcut never gets repaired. Run the same idempotent command from first-run, login, and browser launch, and rewrite Preferences only when no live Chromium pid holds the profile. Fixes #7983 --- bin/omarchy-cmd-repair-chromium-copy-url | 281 ++++++++++++++++++ bin/omarchy-launch-browser | 8 + bin/omarchy-launch-webapp | 7 + bin/omarchy-migrate-notify | 5 + bin/omarchy-provision-first-run | 2 + install/user/first-run/chromium-copy-url.sh | 15 + migrations/1786643346.sh | 207 +------------ migrations/1787601557.sh | 7 + test/shell.d/chromium-copy-url-test.sh | 5 +- .../copy-url-shortcut-migration-test.sh | 132 ++++++-- test/shell.d/first-run-test.sh | 2 + test/shell.d/launch-browser-test.sh | 15 + test/shell.d/systemd-test.sh | 2 + 13 files changed, 464 insertions(+), 224 deletions(-) create mode 100755 bin/omarchy-cmd-repair-chromium-copy-url create mode 100755 install/user/first-run/chromium-copy-url.sh create mode 100644 migrations/1787601557.sh diff --git a/bin/omarchy-cmd-repair-chromium-copy-url b/bin/omarchy-cmd-repair-chromium-copy-url new file mode 100755 index 000000000000..b732fb29b0cb --- /dev/null +++ b/bin/omarchy-cmd-repair-chromium-copy-url @@ -0,0 +1,281 @@ +#!/bin/bash + +# omarchy:summary=Repair a stale Copy URL shortcut in Chromium-family Preferences +# omarchy:hidden=true +# omarchy:args=[--prompt] + +set -euo pipefail + +# Chromium never hands a suggested shortcut to one extension while another — +# even a long-gone one — still holds the registration. Profiles that first +# loaded Copy URL before its id was pinned registered Alt+Shift+L under an id +# derived from the extension's load path at the time, so the pinned extension +# never receives the shortcut and the keypress does nothing. Those historical +# ids are unknowable in general (they hash long-gone absolute paths), but the +# registration itself names the command: rebind any copy-url command that +# points away from the pinned id, unless that id belongs to an extension that +# is actually installed. +# +# Fresh installs stamp every shipped migration complete, so this cannot live +# only as a migration: a Chromium profile that arrives later (sync, restored +# ~/.config/chromium) would never be repaired. Callers that are not +# migration-stamped invoke this without --prompt. --prompt is for omarchy-migrate +# on unstamped upgrades, where a running browser can be asked to close. + +prompt=0 +if [[ ${1:-} == "--prompt" ]]; then + prompt=1 +fi + +pending=() + +pinned_id="bgpiichlckmfanooecilcjemknkcpngb" + +repair_py=$(cat <<'PY' +import json +import os +import shutil +import sys +import tempfile +from pathlib import Path + +path = Path(sys.argv[1]) +pinned_id = sys.argv[2] +mode = sys.argv[3] + +try: + preferences = json.loads(path.read_text()) +except (OSError, ValueError): + sys.exit(1) + +extensions = preferences.get("extensions", {}) +commands = extensions.get("commands", {}) +settings = extensions.get("settings", {}) + +def ghost_id(command): + if command.get("command_name") != "copy-url": + return None + extension = command.get("extension") + if not extension or extension == pinned_id: + return None + # An installed extension records its install path in settings; only the + # ghosts of Copy URL itself may be rebound. + install_path = settings.get(extension, {}).get("path", "") + if install_path and not install_path.endswith("/default/chromium/extensions/copy-url"): + return None + return extension + +if mode == "check": + sys.exit(0 if any(ghost_id(c) for c in commands.values()) else 1) + +# Chromium keeps one shortcut per command, so a ghost may only be rebound +# when the pinned extension holds no copy-url binding of its own; further +# ghosts are dropped rather than doubled up. +pinned_bound = any( + c.get("command_name") == "copy-url" and c.get("extension") == pinned_id + for c in commands.values() +) + +changed = False +for accelerator in list(commands): + ghost = ghost_id(commands[accelerator]) + if not ghost: + continue + if pinned_bound: + del commands[accelerator] + else: + commands[accelerator]["extension"] = pinned_id + pinned_bound = True + pinned_command = settings.get(pinned_id, {}).get("commands", {}).get("copy-url", {}) + if pinned_command: + pinned_command["was_assigned"] = True + settings.pop(ghost, None) + changed = True + +if changed: + # Chromium HMACs extension prefs under protection.macs. Editing + # Preferences while leaving those MACs in place looks like tampering, and + # the next browser start restores the ghost. Drop the seal so Chromium + # re-computes it from the repaired file. + preferences.pop("protection", None) + shutil.copy2(path, path.with_name(path.name + ".omarchy-copy-url-repair.bak")) + text = json.dumps(preferences, separators=(",", ":")) + fd, tmp_name = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.") + try: + with os.fdopen(fd, "w") as tmp: + tmp.write(text) + tmp.flush() + os.fsync(tmp.fileno()) + os.chmod(tmp_name, path.stat().st_mode) + os.replace(tmp_name, path) + except Exception: + try: + os.unlink(tmp_name) + except OSError: + pass + raise +PY +) + +profile_roots=( + "$HOME/.config/chromium" + "$HOME/.config/google-chrome" + "$HOME/.config/google-chrome-beta" + "$HOME/.config/google-chrome-unstable" + "$HOME/.config/BraveSoftware/Brave-Browser" + "$HOME/.config/BraveSoftware/Brave-Browser-Beta" + "$HOME/.config/BraveSoftware/Brave-Browser-Nightly" + "$HOME/.config/microsoft-edge" + "$HOME/.config/microsoft-edge-beta" + "$HOME/.config/microsoft-edge-dev" + "$HOME/.config/vivaldi" + "$HOME/.config/opera" + "$HOME/.config/helium" +) + +find_pending() { + pending=() + for profile_root in "${profile_roots[@]}"; do + [[ -d $profile_root ]] || continue + + for preferences in "$profile_root"/*/Preferences; do + [[ -f $preferences ]] || continue + python3 -c "$repair_py" "$preferences" "$pinned_id" check || continue + pending+=("$preferences") + done + done +} + +# The backup a repair leaves next to Preferences marks it as attempted but not +# yet verified: a browser that was open during the repair still holds the +# stale registration in memory and restores it when it exits, possibly after +# the disk was already seen clean. +unverified_repairs_exist() { + local profile_root backup + + for profile_root in "${profile_roots[@]}"; do + for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do + [[ -f $backup ]] && return 0 + done + done + + return 1 +} + +# A browser only rewrites its own Preferences on exit, so a repair can only be +# reverted by a browser attached to a profile this command has to touch. +# Chromium writes SingletonLock as a symlink to -; a lock left +# behind by SIGTERM still looks like a lock if we only test for the symlink. +# Trust a live PID (or a non-symlink lock / live socket), not a dangling one. +profile_open() { + local profile=$1 + local lock=$profile/SingletonLock + + if [[ -L $lock ]]; then + local target pid + target=$(readlink "$lock") || return 1 + pid=${target##*-} + if [[ $pid =~ ^[0-9]+$ ]] && kill -0 "$pid" 2>/dev/null; then + return 0 + fi + return 1 + fi + + [[ -e $lock || -S $profile/SingletonSocket ]] +} + +# Gate on a pending — or to-be-verified — profile actually being open, not on +# browsers in general. Waiting on every browser process deadlocks the update on +# machines whose main browser is effectively never closed: the pending ghosts +# commonly sit in a stale profile nobody has open, yet the update blocks on the +# always-open one. +affected_profile_open() { + local preferences backup profile_root + + if (( ${#pending[@]} > 0 )); then + for preferences in "${pending[@]}"; do + profile_open "$(dirname "$(dirname "$preferences")")" && return 0 + done + fi + + for profile_root in "${profile_roots[@]}"; do + for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do + [[ -f $backup ]] || continue + profile_open "$(dirname "$(dirname "$backup")")" && return 0 + done + done + + return 1 +} + +defer_because_browser_running() { + if (( prompt )); then + echo "A running browser would undo the Copy URL shortcut repair." >&2 + echo "Close the browser windows, then run: omarchy-migrate" >&2 + exit 1 + fi + + exit 0 +} + +find_pending +if (( ! ${#pending[@]} )) && ! unverified_repairs_exist; then + exit 0 +fi + +# A running browser holds Preferences in memory and rewrites the file on exit, +# reverting the repair. Quiet callers (login, first-run, browser launch) skip +# and retry later. Prompted migrations ask for the affected windows to close. +# gum draws the prompt on stderr, so it must stay attached: suppressing it +# leaves gum waiting for a keypress behind an unpainted screen. Without a +# terminal to ask in, gum fails; then — as on decline — fail so the migration +# stays pending, and the login notifier keeps prompting until a rerun goes +# through with the affected profiles closed. +while affected_profile_open; do + if (( ! prompt )); then + defer_because_browser_running + fi + + if ! gum confirm "Close the browser windows to repair the Copy URL shortcut, then continue"; then + defer_because_browser_running + fi +done + +# A browser that just closed may have restored ghosts an earlier repair had +# already removed, so only now is the pending list authoritative. +find_pending + +if (( ${#pending[@]} > 0 )); then + for preferences in "${pending[@]}"; do + python3 -c "$repair_py" "$preferences" "$pinned_id" repair + done +fi + +# A browser that started mid-repair read the stale Preferences and will write +# them back on exit; stay pending so the next browser-free run can verify the +# repair stuck. +if affected_profile_open; then + if (( prompt )); then + echo "A browser started during the Copy URL shortcut repair and may undo it on exit." >&2 + echo "Close the browser windows, then run: omarchy-migrate" >&2 + exit 1 + fi + exit 0 +fi + +# One that started and already exited mid-repair wrote its stale in-memory +# Preferences back over the repair; verify every file again. A browser +# starting after this point reads the repaired file, so it can no longer +# restore the ghost. +if (( ${#pending[@]} > 0 )); then + for preferences in "${pending[@]}"; do + if python3 -c "$repair_py" "$preferences" "$pinned_id" check; then + if (( prompt )); then + echo "A browser undid the Copy URL shortcut repair on exit." >&2 + echo "Close the browser windows, then run: omarchy-migrate" >&2 + exit 1 + fi + exit 0 + fi + done +fi diff --git a/bin/omarchy-launch-browser b/bin/omarchy-launch-browser index 044b220d2268..6b542300cc89 100755 --- a/bin/omarchy-launch-browser +++ b/bin/omarchy-launch-browser @@ -3,6 +3,14 @@ # omarchy:summary=Launch the default browser as determined by xdg-settings. # omarchy:args=[url] +# Repair a stale Copy URL shortcut before Chromium reads Preferences. Fresh +# installs stamp the migration complete, so a profile that arrives later via +# sync only gets this repair from a non-stamped hook. Skip (and retry next +# launch) if the profile is already open. +if [[ -x ${OMARCHY_PATH:-}/bin/omarchy-cmd-repair-chromium-copy-url ]]; then + "$OMARCHY_PATH/bin/omarchy-cmd-repair-chromium-copy-url" || true +fi + default_browser=$(env -u BROWSER xdg-settings get default-web-browser) if [[ -z $default_browser ]]; then default_browser=$(xdg-mime query default x-scheme-handler/https) diff --git a/bin/omarchy-launch-webapp b/bin/omarchy-launch-webapp index 6ee0f84a2533..abc44a93cddf 100755 --- a/bin/omarchy-launch-webapp +++ b/bin/omarchy-launch-webapp @@ -3,6 +3,13 @@ # omarchy:summary=Launch a URL as a web app in the default supported browser # omarchy:args= +# Same Copy URL repair as omarchy-launch-browser: web apps start Chromium +# without going through that wrapper, and the migration is stamped complete +# on fresh installs. +if [[ -x ${OMARCHY_PATH:-}/bin/omarchy-cmd-repair-chromium-copy-url ]]; then + "$OMARCHY_PATH/bin/omarchy-cmd-repair-chromium-copy-url" || true +fi + browser=$(xdg-settings get default-web-browser) case $browser in diff --git a/bin/omarchy-migrate-notify b/bin/omarchy-migrate-notify index 00cce3d6e9eb..dbc9f8ab2aa9 100755 --- a/bin/omarchy-migrate-notify +++ b/bin/omarchy-migrate-notify @@ -17,6 +17,11 @@ if update_in_progress; then exit 0 fi +# Copy URL repair is browser-profile state, so install-time migration +# stamping cannot satisfy it. Retry quietly at login when Preferences exists +# and no browser is attached; skip (exit 0) if one is running. +omarchy-cmd-repair-chromium-copy-url || true + pending_migrations=$(omarchy-migrate --pending 2>/dev/null) || exit 0 pending_count=$(printf '%s\n' "$pending_migrations" | sed '/^[[:space:]]*$/d' | wc -l) diff --git a/bin/omarchy-provision-first-run b/bin/omarchy-provision-first-run index 11c2eb881654..ee227384232f 100755 --- a/bin/omarchy-provision-first-run +++ b/bin/omarchy-provision-first-run @@ -83,6 +83,8 @@ run_first_run_step "set GTK primary paste" \ bash "$OMARCHY_PATH/install/user/first-run/gtk-primary-paste.sh" run_first_run_step "apply speaker tuning" \ bash "$OMARCHY_PATH/install/user/first-run/audio-tuning.sh" +run_first_run_step "repair Chromium Copy URL shortcut" \ + bash "$OMARCHY_PATH/install/user/first-run/chromium-copy-url.sh" omarchy-notification-wait || log_first_run "Timed out waiting for notification service; continuing" # Each send returns only once the server has taken the toast, so sending in diff --git a/install/user/first-run/chromium-copy-url.sh b/install/user/first-run/chromium-copy-url.sh new file mode 100755 index 000000000000..fd680769a301 --- /dev/null +++ b/install/user/first-run/chromium-copy-url.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Repair a Copy URL shortcut registered under a path-derived extension id. +# Runs at first-run rather than as a migration because the finalizer marks all +# shipped migrations complete on a fresh install. The Chromium profile is not +# part of that install — it commonly arrives afterwards from sync or a restored +# ~/.config/chromium — so install-time stamping cannot satisfy this repair. +# +# Idempotent: no-ops when Preferences is missing or already points at the +# pinned id. A running browser is skipped and retried from login notify or +# the next browser launch, which are also not migration-stamped. + +set -euo pipefail + +omarchy-cmd-repair-chromium-copy-url diff --git a/migrations/1786643346.sh b/migrations/1786643346.sh index cc197a916c0e..039dcfb26c15 100644 --- a/migrations/1786643346.sh +++ b/migrations/1786643346.sh @@ -1,204 +1,7 @@ echo "Repair the Copy URL shortcut for profiles that predate its pinned extension id" -# Chromium never hands a suggested shortcut to one extension while another — -# even a long-gone one — still holds the registration. Profiles that first -# loaded Copy URL before its id was pinned registered Alt+Shift+L under an id -# derived from the extension's load path at the time, so the pinned extension -# never receives the shortcut and the keypress does nothing. Those historical -# ids are unknowable in general (they hash long-gone absolute paths), but the -# registration itself names the command: rebind any copy-url command that -# points away from the pinned id, unless that id belongs to an extension that -# is actually installed. - -pinned_id="bgpiichlckmfanooecilcjemknkcpngb" - -repair_py=$(cat <<'PY' -import json -import shutil -import sys -from pathlib import Path - -path = Path(sys.argv[1]) -pinned_id = sys.argv[2] -mode = sys.argv[3] - -try: - preferences = json.loads(path.read_text()) -except (OSError, ValueError): - sys.exit(1) - -extensions = preferences.get("extensions", {}) -commands = extensions.get("commands", {}) -settings = extensions.get("settings", {}) - -def ghost_id(command): - if command.get("command_name") != "copy-url": - return None - extension = command.get("extension") - if not extension or extension == pinned_id: - return None - # An installed extension records its install path in settings; only the - # ghosts of Copy URL itself may be rebound. - install_path = settings.get(extension, {}).get("path", "") - if install_path and not install_path.endswith("/default/chromium/extensions/copy-url"): - return None - return extension - -if mode == "check": - sys.exit(0 if any(ghost_id(c) for c in commands.values()) else 1) - -# Chromium keeps one shortcut per command, so a ghost may only be rebound -# when the pinned extension holds no copy-url binding of its own; further -# ghosts are dropped rather than doubled up. -pinned_bound = any( - c.get("command_name") == "copy-url" and c.get("extension") == pinned_id - for c in commands.values() -) - -changed = False -for accelerator in list(commands): - ghost = ghost_id(commands[accelerator]) - if not ghost: - continue - if pinned_bound: - del commands[accelerator] - else: - commands[accelerator]["extension"] = pinned_id - pinned_bound = True - pinned_command = settings.get(pinned_id, {}).get("commands", {}).get("copy-url", {}) - if pinned_command: - pinned_command["was_assigned"] = True - settings.pop(ghost, None) - changed = True - -if changed: - shutil.copy2(path, path.with_name(path.name + ".omarchy-copy-url-repair.bak")) - path.write_text(json.dumps(preferences, separators=(",", ":"))) -PY -) - -profile_roots=( - "$HOME/.config/chromium" - "$HOME/.config/google-chrome" - "$HOME/.config/google-chrome-beta" - "$HOME/.config/google-chrome-unstable" - "$HOME/.config/BraveSoftware/Brave-Browser" - "$HOME/.config/BraveSoftware/Brave-Browser-Beta" - "$HOME/.config/BraveSoftware/Brave-Browser-Nightly" - "$HOME/.config/microsoft-edge" - "$HOME/.config/microsoft-edge-beta" - "$HOME/.config/microsoft-edge-dev" - "$HOME/.config/vivaldi" - "$HOME/.config/opera" - "$HOME/.config/helium" -) - -find_pending() { - pending=() - for profile_root in "${profile_roots[@]}"; do - [[ -d $profile_root ]] || continue - - for preferences in "$profile_root"/*/Preferences; do - [[ -f $preferences ]] || continue - python3 -c "$repair_py" "$preferences" "$pinned_id" check || continue - pending+=("$preferences") - done - done -} - -# The backup a repair leaves next to Preferences marks it as attempted but not -# yet verified: a browser that was open during the repair still holds the -# stale registration in memory and restores it when it exits, possibly after -# the disk was already seen clean. -unverified_repairs_exist() { - local profile_root backup - - for profile_root in "${profile_roots[@]}"; do - for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do - [[ -f $backup ]] && return 0 - done - done - - return 1 -} - -# A browser only rewrites its own Preferences on exit, so a repair can only be -# reverted by a browser attached to a profile this migration has to touch. -# Whether a profile is open is mechanical: a running Chromium-family browser -# holds a SingletonLock (and socket) inside its user-data-dir. -profile_open() { - # -L catches a SingletonLock left as a dangling symlink; -e covers a plain - # file, and -S the socket — any of them means a browser is attached. - [[ -L $1/SingletonLock || -e $1/SingletonLock || -S $1/SingletonSocket ]] -} - -# Gate on a pending — or to-be-verified — profile actually being open, not on -# browsers in general. Waiting on every browser process deadlocks the update on -# machines whose main browser is effectively never closed: the pending ghosts -# commonly sit in a stale profile nobody has open, yet the update blocks on the -# always-open one. -affected_profile_open() { - local preferences backup profile_root - - for preferences in "${pending[@]}"; do - profile_open "$(dirname "$(dirname "$preferences")")" && return 0 - done - - for profile_root in "${profile_roots[@]}"; do - for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do - [[ -f $backup ]] || continue - profile_open "$(dirname "$(dirname "$backup")")" && return 0 - done - done - - return 1 -} - -find_pending -if (( ! ${#pending[@]} )) && ! unverified_repairs_exist; then - exit 0 -fi - -# A running browser holds Preferences in memory and rewrites the file on exit, -# reverting the repair, so ask for the affected windows to be closed first. gum -# draws the prompt on stderr, so it must stay attached: suppressing it leaves -# gum waiting for a keypress behind an unpainted screen. Without a terminal to -# ask in, gum fails; then — as on decline — fail so the migration stays -# pending, and the login notifier keeps prompting until a rerun goes through -# with the affected profiles closed. -while affected_profile_open; do - if ! gum confirm "Close the browser windows to repair the Copy URL shortcut, then continue"; then - echo "A running browser would undo the Copy URL shortcut repair." >&2 - echo "Close the browser windows, then run: omarchy-migrate" >&2 - exit 1 - fi -done - -# A browser that just closed may have restored ghosts an earlier repair had -# already removed, so only now is the pending list authoritative. -find_pending - -for preferences in "${pending[@]}"; do - python3 -c "$repair_py" "$preferences" "$pinned_id" repair -done - -# A browser that started mid-repair read the stale Preferences and will write -# them back on exit; stay pending so the next browser-free run can verify the -# repair stuck. -if affected_profile_open; then - echo "A browser started during the Copy URL shortcut repair and may undo it on exit." >&2 - echo "Close the browser windows, then run: omarchy-migrate" >&2 - exit 1 -fi - -# One that started and already exited mid-repair wrote its stale in-memory -# Preferences back over the repair; verify every file again. A browser -# starting after this point reads the repaired file, so it can no longer -# restore the ghost. -for preferences in "${pending[@]}"; do - if python3 -c "$repair_py" "$preferences" "$pinned_id" check; then - echo "A browser undid the Copy URL shortcut repair on exit." >&2 - echo "Close the browser windows, then run: omarchy-migrate" >&2 - exit 1 - fi -done +# Fresh installs stamp every shipped migration complete, so a Chromium profile +# that arrives later via sync never runs this file. Keep it for unstamped +# upgrades; omarchy-cmd-repair-chromium-copy-url is also invoked from first-run, +# login notify, and browser launch, which are not migration-stamped. +omarchy-cmd-repair-chromium-copy-url --prompt diff --git a/migrations/1787601557.sh b/migrations/1787601557.sh new file mode 100644 index 000000000000..91efa825af57 --- /dev/null +++ b/migrations/1787601557.sh @@ -0,0 +1,7 @@ +echo "Retry the Copy URL shortcut repair after install-time migration stamping" + +# 1786643346 is already stamped complete on fresh installs, so a Chromium +# profile that arrived later via sync never got the repair. This covers +# existing users on update. The same command is also invoked from first-run, +# login notify, and browser launch, which are not migration-stamped. +omarchy-cmd-repair-chromium-copy-url --prompt diff --git a/test/shell.d/chromium-copy-url-test.sh b/test/shell.d/chromium-copy-url-test.sh index 6dad096fe63e..0ec58a7ea925 100644 --- a/test/shell.d/chromium-copy-url-test.sh +++ b/test/shell.d/chromium-copy-url-test.sh @@ -73,9 +73,12 @@ pass "copy-url native host installer registers the stable extension id" # Chromium ships in the base packages, so fresh installs do not go through # omarchy-install-browser, and they mark every migration as already applied. -# The user install still has to register the host itself. +# The user install still has to register the host itself, and the Copy URL +# shortcut repair has to run from a hook that is not migration-stamped. grep -q 'user/chromium.sh' "$ROOT/install/user/all.sh" || fail "user install runs the Chromium native messaging host setup" +grep -q 'first-run/chromium-copy-url.sh' "$ROOT/bin/omarchy-provision-first-run" || + fail "first-run repairs Copy URL after install-time migration stamping" fresh_home="$TMPDIR/fresh-install" HOME="$fresh_home" OMARCHY_PATH="$ROOT" PATH="$ROOT/bin:$PATH" \ diff --git a/test/shell.d/copy-url-shortcut-migration-test.sh b/test/shell.d/copy-url-shortcut-migration-test.sh index f8cf34975b2e..6ea501254430 100644 --- a/test/shell.d/copy-url-shortcut-migration-test.sh +++ b/test/shell.d/copy-url-shortcut-migration-test.sh @@ -8,6 +8,8 @@ require_command jq require_command python3 migration="$ROOT/migrations/1786643346.sh" +retry_migration=$(grep -l 'Retry the Copy URL shortcut repair after install-time migration stamping' "$ROOT/migrations"/*.sh | head -n 1 || true) +repair_cmd="$ROOT/bin/omarchy-cmd-repair-chromium-copy-url" test_dir=$(mktemp -d) trap 'rm -rf "$test_dir"' EXIT @@ -22,7 +24,16 @@ ghost_id="ikkebdkaanlebnifjnbeiaklodhbjcci" pinned_id="bgpiichlckmfanooecilcjemknkcpngb" write_stale_preferences() { - jq -n --arg ghost "$ghost_id" --arg pinned "$pinned_id" '{extensions: {commands: {"linux:Alt+Shift+L": {command_name: "copy-url", extension: $ghost, global: false}}, settings: {($ghost): {commands: {"copy-url": {suggested_key: "Alt+Shift+L", was_assigned: true}}}, ($pinned): {commands: {"copy-url": {suggested_key: "Alt+Shift+L"}}}}}}' >"$preferences" + jq -n --arg ghost "$ghost_id" --arg pinned "$pinned_id" '{ + extensions: { + commands: {"linux:Alt+Shift+L": {command_name: "copy-url", extension: $ghost, global: false}}, + settings: { + ($ghost): {commands: {"copy-url": {suggested_key: "Alt+Shift+L", was_assigned: true}}}, + ($pinned): {commands: {"copy-url": {suggested_key: "Alt+Shift+L"}}} + } + }, + protection: {macs: {extensions: {commands: "stale-command-mac", settings: {($ghost): "stale-settings-mac"}}}} + }' >"$preferences" } stub_bin="$test_dir/bin" @@ -31,21 +42,65 @@ mkdir -p "$stub_bin" REAL_PYTHON=$(command -v python3) export REAL_PYTHON +run_repair() { + HOME="$home" PATH="$stub_bin:$ROOT/bin:$PATH" "$repair_cmd" >/dev/null 2>&1 +} + run_migration() { - HOME="$home" PATH="$stub_bin:$PATH" bash -euo pipefail "$migration" >/dev/null 2>&1 + HOME="$home" PATH="$stub_bin:$ROOT/bin:$PATH" bash -euo pipefail "$migration" >/dev/null 2>&1 } # A running Chromium-family browser marks its profile root with a SingletonLock -# symlink to -, a target that never exists on disk. That lock — -# not the mere presence of a browser process — is what the migration waits on. +# symlink to -. Only a live PID counts as attached; a lock left +# by SIGTERM still points at a dead pid and must not block the repair. open_browser() { mkdir -p "$profile_root" - ln -sfn "test-host-1234" "$profile_root/SingletonLock" + ln -sfn "test-host-$$" "$profile_root/SingletonLock" +} +stale_browser_lock() { + mkdir -p "$profile_root" + ln -sfn "test-host-999999999" "$profile_root/SingletonLock" } close_browser() { rm -f "$profile_root/SingletonLock" } +assert_repaired() { + jq -e --arg ghost "$ghost_id" --arg pinned "$pinned_id" ' + .extensions.commands["linux:Alt+Shift+L"].extension == $pinned and + (.extensions.settings | has($ghost) | not) and + .extensions.settings[$pinned].commands["copy-url"].was_assigned == true and + (has("protection") | not) + ' "$preferences" >/dev/null +} + +assert_no_tmp() { + local leftover + leftover=$(find "$profile_root/Default" -maxdepth 1 \( -name '.Preferences.*' -o -name 'Preferences.tmp*' \)) + [[ -z $leftover ]] +} + +grep -F 'omarchy-cmd-repair-chromium-copy-url --prompt' "$migration" >/dev/null || + fail "1786643346 delegates to the shared repair command" +[[ -n $retry_migration && -f $retry_migration ]] || + fail "a follow-up migration retries the repair for already-stamped installs" +grep -F 'omarchy-cmd-repair-chromium-copy-url --prompt' "$retry_migration" >/dev/null || + fail "follow-up migration delegates to the shared repair command" +pass "migrations keep the repair for unstamped upgrades and already-stamped installs" + +grep -F 'first-run/chromium-copy-url.sh' "$ROOT/bin/omarchy-provision-first-run" >/dev/null || + fail "first-run does not invoke the Copy URL repair" +grep -F 'omarchy-cmd-repair-chromium-copy-url' "$ROOT/bin/omarchy-migrate-notify" >/dev/null || + fail "login notifier does not retry the Copy URL repair" +grep -F 'omarchy-cmd-repair-chromium-copy-url' "$ROOT/bin/omarchy-launch-browser" >/dev/null || + fail "browser launch does not retry the Copy URL repair" +grep -F 'omarchy-cmd-repair-chromium-copy-url' "$ROOT/bin/omarchy-launch-webapp" >/dev/null || + fail "web app launch does not retry the Copy URL repair" +pass "Copy URL repair is invoked from non-stamped first-run, login, and launch hooks" + +# A running Chromium-family browser marks its profile root with a SingletonLock +# symlink to -, a target that never exists on disk. That lock — +# not the mere presence of a browser process — is what the repair waits on. # The affected profile being open prompts for the windows to be closed; # declining (or having no terminal to ask in) defers the repair so a # rewrite-on-exit cannot revert it. @@ -61,6 +116,23 @@ run_migration && fail "migration defers while the affected profile is open" fail "migration leaves preferences alone while the affected profile is open" pass "migration defers the repair while the affected profile is open" +# Quiet callers skip without gum and without failing — login and browser launch +# must not block on a running profile. +run_repair || fail "quiet repair skips while the affected profile is open" +[[ $(sha256sum "$preferences" | cut -d' ' -f1) == "$before_hash" ]] || + fail "quiet repair leaves preferences alone while the affected profile is open" +pass "quiet repair skips while the affected profile is open" + +# A dangling SingletonLock whose pid is dead is not an attached browser; SIGTERM +# leaves that symlink behind and the repair must still persist. +stale_browser_lock +run_repair || fail "quiet repair proceeds past a stale SingletonLock" +assert_repaired || fail "quiet repair rewrites preferences behind a stale lock" +assert_no_tmp || fail "repair leaves no Preferences temp file behind a stale lock" +pass "stale SingletonLock does not block the repair" +rm -f "$preferences.omarchy-copy-url-repair.bak" +close_browser + # gum paints its prompt on stderr, so that stream has to stay attached: # suppressing it leaves gum reading keys behind an unpainted screen, which # reads as a hung update. @@ -69,8 +141,10 @@ cat >"$stub_bin/gum" <<'STUB' echo "gum-prompt-painted" >&2 exit 1 STUB +write_stale_preferences +open_browser prompt_stderr="$test_dir/prompt-stderr" -HOME="$home" PATH="$stub_bin:$PATH" bash -euo pipefail "$migration" >/dev/null 2>"$prompt_stderr" && +HOME="$home" PATH="$stub_bin:$ROOT/bin:$PATH" bash -euo pipefail "$migration" >/dev/null 2>"$prompt_stderr" && fail "migration defers when the browser prompt is declined" grep -q "gum-prompt-painted" "$prompt_stderr" || fail "migration keeps the browser prompt visible" pass "migration keeps the browser prompt visible" @@ -80,11 +154,10 @@ pass "migration keeps the browser prompt visible" # prompt, which the still-declining gum stub would otherwise fail. close_browser mkdir -p "$home/.config/google-chrome" -ln -sfn "test-host-1234" "$home/.config/google-chrome/SingletonLock" +ln -sfn "test-host-$$" "$home/.config/google-chrome/SingletonLock" write_stale_preferences run_migration || fail "migration repairs while a different profile root is open" -jq -e --arg pinned "$pinned_id" '.extensions.commands["linux:Alt+Shift+L"].extension == $pinned' "$preferences" >/dev/null || - fail "migration repairs the shortcut while a different profile root is open" +assert_repaired || fail "migration repairs the shortcut while a different profile root is open" pass "migration ignores a browser on a different profile root" rm -f "$home/.config/google-chrome/SingletonLock" "$preferences.omarchy-copy-url-repair.bak" @@ -104,11 +177,10 @@ rm -f "$HOME/.config/chromium/SingletonLock" STUB chmod +x "$stub_bin/gum" "$stub_bin/close-browser" GUM_CALLED="$test_dir/gum-called" CLOSE_BROWSER="$stub_bin/close-browser" \ - HOME="$home" PATH="$stub_bin:$PATH" bash -euo pipefail "$migration" >/dev/null 2>&1 || + HOME="$home" PATH="$stub_bin:$ROOT/bin:$PATH" bash -euo pipefail "$migration" >/dev/null 2>&1 || fail "migration proceeds once the profile is closed and the prompt confirmed" [[ -e $test_dir/gum-called ]] || fail "migration asks before repairing under a running browser" -jq -e --arg pinned "$pinned_id" '.extensions.commands["linux:Alt+Shift+L"].extension == $pinned' "$preferences" >/dev/null || - fail "migration repairs after the browser prompt is confirmed" +assert_repaired || fail "migration repairs after the browser prompt is confirmed" pass "migration asks to close the browser and repairs on confirmation" rm -f "$preferences.omarchy-copy-url-repair.bak" @@ -118,18 +190,27 @@ close_browser write_stale_preferences run_migration || fail "migration repairs the shortcut when no browser is running" -jq -e --arg ghost "$ghost_id" --arg pinned "$pinned_id" ' - .extensions.commands["linux:Alt+Shift+L"].extension == $pinned and - (.extensions.settings | has($ghost) | not) and - .extensions.settings[$pinned].commands["copy-url"].was_assigned == true -' "$preferences" >/dev/null || fail "migration rebinds the Copy URL shortcut to the pinned extension id" +assert_repaired || fail "migration rebinds the Copy URL shortcut to the pinned extension id" [[ -f $preferences.omarchy-copy-url-repair.bak ]] || fail "migration backs up preferences before the repair" +assert_no_tmp || fail "repair leaves no Preferences temp file after a successful write" pass "migration rebinds the Copy URL shortcut to the pinned extension id" +# Install-time stamping must not prevent the command from repairing a profile +# that arrived later. +mkdir -p "$home/.local/state/omarchy/migrations" +touch "$home/.local/state/omarchy/migrations/1786643346.sh" +touch "$home/.local/state/omarchy/migrations/$(basename "$retry_migration")" +rm -f "$preferences.omarchy-copy-url-repair.bak" +write_stale_preferences +run_repair || fail "repair command runs even when the migrations are stamped complete" +assert_repaired || fail "stamped migrations still leave the command able to repair" +pass "repair command is independent of install-time migration stamps" +rm -f "$preferences.omarchy-copy-url-repair.bak" +rm -rf "$home/.local/state/omarchy/migrations" + # A repaired profile has no ghost registration left, so nothing is pending — # even while that same profile is open. -rm "$preferences.omarchy-copy-url-repair.bak" repaired_hash=$(sha256sum "$preferences" | cut -d' ' -f1) open_browser run_migration || fail "migration reruns cleanly after the repair" @@ -167,11 +248,11 @@ cat >"$stub_bin/python3" <<'STUB' # the check calls report a surviving ghost through their exit status. "${REAL_PYTHON}" "$@" status=$? -[[ ${5:-} == "repair" ]] && ln -sfn "test-host-1234" "$HOME/.config/chromium/SingletonLock" +[[ ${5:-} == "repair" ]] && ln -sfn "test-host-${LIVE_PID:?}" "$HOME/.config/chromium/SingletonLock" exit $status STUB chmod +x "$stub_bin/python3" -if HOME="$home" PATH="$stub_bin:$PATH" bash -euo pipefail "$migration" >/dev/null 2>&1; then +if HOME="$home" PATH="$stub_bin:$ROOT/bin:$PATH" LIVE_PID=$$ bash -euo pipefail "$migration" >/dev/null 2>&1; then fail "migration stays pending when a browser starts mid-repair" fi jq -e --arg pinned "$pinned_id" '.extensions.commands["linux:Alt+Shift+L"].extension == $pinned' "$preferences" >/dev/null || @@ -192,7 +273,7 @@ status=$? exit $status STUB chmod +x "$stub_bin/python3" -if HOME="$home" PATH="$stub_bin:$PATH" STALE_PREFERENCES="$test_dir/stale-preferences" \ +if HOME="$home" PATH="$stub_bin:$ROOT/bin:$PATH" STALE_PREFERENCES="$test_dir/stale-preferences" \ REPAIRED_PREFERENCES="$preferences" bash -euo pipefail "$migration" >/dev/null 2>&1; then fail "migration stays pending when a briefly-lived browser undoes the repair" fi @@ -218,6 +299,15 @@ run_migration || fail "migration completes once the repair is verified with brow pass "migration verifies an attempted repair on a browser-free rerun" rm -f "$preferences.omarchy-copy-url-repair.bak" +# First-run invokes the quiet command, so a profile already present at first +# login is repaired without going through omarchy-migrate. +write_stale_preferences +HOME="$home" PATH="$stub_bin:$ROOT/bin:$PATH" bash "$ROOT/install/user/first-run/chromium-copy-url.sh" || + fail "first-run Copy URL hook repairs a present profile" +assert_repaired || fail "first-run Copy URL hook rebinds the pinned extension id" +pass "first-run hook repairs Copy URL when Preferences already exists" +rm -f "$preferences.omarchy-copy-url-repair.bak" + # An installed third-party extension with a command that happens to be named # copy-url keeps its own registration. jq -n '{extensions: {commands: {"linux:Alt+Shift+L": {command_name: "copy-url", extension: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", global: false}}, settings: {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: {path: "/home/user/.config/some-extension", commands: {}}}}}' >"$preferences" diff --git a/test/shell.d/first-run-test.sh b/test/shell.d/first-run-test.sh index 0fd593757de1..f596a4cae4ee 100644 --- a/test/shell.d/first-run-test.sh +++ b/test/shell.d/first-run-test.sh @@ -32,4 +32,6 @@ if grep -F 'skip-first-run-update-notification' "$ROOT/install/user/first-run/wi fail "first-run does not track update notifications separately" fi +grep -F 'first-run/chromium-copy-url.sh' "$ROOT/bin/omarchy-provision-first-run" >/dev/null || + fail "first-run does not repair the Copy URL shortcut after install-time migration stamping" pass "first-run uses one lifecycle completion marker" diff --git a/test/shell.d/launch-browser-test.sh b/test/shell.d/launch-browser-test.sh index 161ff9ee0702..9e1ddcbb9ec9 100644 --- a/test/shell.d/launch-browser-test.sh +++ b/test/shell.d/launch-browser-test.sh @@ -78,4 +78,19 @@ grep -F 'https://example.test/fallback' "$launch_log" >/dev/null || grep -Fx '^chromium.*$' "$focus_log" >/dev/null || fail "browser launcher focuses the browser resolved from the HTTPS handler" +repair_log="$test_tmp/repair" +fake_omarchy="$test_tmp/omarchy" +mkdir -p "$fake_omarchy/bin" +cat >"$fake_omarchy/bin/omarchy-cmd-repair-chromium-copy-url" <<'SH' +#!/bin/bash +printf 'repair\n' >>"$OMARCHY_TEST_REPAIR_LOG" +SH +chmod +x "$fake_omarchy/bin/omarchy-cmd-repair-chromium-copy-url" + +HOME="$test_home" PATH="$mock_bin:$PATH" HYPRLAND_INSTANCE_SIGNATURE=test \ + OMARCHY_PATH="$fake_omarchy" OMARCHY_TEST_REPAIR_LOG="$repair_log" \ + OMARCHY_TEST_BROWSER_LAUNCH="$launch_log" OMARCHY_TEST_BROWSER_FOCUS="$focus_log" \ + bash "$ROOT/bin/omarchy-launch-browser" + +grep -Fx 'repair' "$repair_log" >/dev/null || fail "browser launcher repairs Copy URL before Chromium starts" pass "browser launcher follows opened links to the browser workspace" diff --git a/test/shell.d/systemd-test.sh b/test/shell.d/systemd-test.sh index 7d4f67c7f9a6..5f666390feeb 100755 --- a/test/shell.d/systemd-test.sh +++ b/test/shell.d/systemd-test.sh @@ -56,6 +56,8 @@ grep -F 'omarchy-migrate-notify.service' "$first_run_units" >/dev/null || fail "first-run does not enable the login migration notifier" grep -F 'omarchy-update-user-notify' "$first_run_units" >/dev/null && fail "first-run still enables the retired notifier units" +grep -F 'omarchy-cmd-repair-chromium-copy-url' "$ROOT/bin/omarchy-migrate-notify" >/dev/null || + fail "login notifier does not retry the Copy URL repair after install-time stamping" pass "first-run enables the login-only migration notifier" fcitx_service="$ROOT/default/systemd/user/omarchy-fcitx5.service" From 9d17935f27070035e5fdcc863e803052e35c408e Mon Sep 17 00:00:00 2001 From: Taksh Date: Wed, 30 Sep 2026 15:25:18 +0530 Subject: [PATCH 2/4] Preserve recovery backups and honor browser sockets during repair --- bin/omarchy-cmd-repair-chromium-copy-url | 16 +++++++++-- test/shell.d/copy-url-repair-test.sh | 36 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/bin/omarchy-cmd-repair-chromium-copy-url b/bin/omarchy-cmd-repair-chromium-copy-url index eb27e564992d..624386b9bcaf 100755 --- a/bin/omarchy-cmd-repair-chromium-copy-url +++ b/bin/omarchy-cmd-repair-chromium-copy-url @@ -48,6 +48,11 @@ try: except (OSError, ValueError): sys.exit(1) +if not isinstance(preferences, dict): + sys.exit(1) +if mode == "readable": + sys.exit(0) + extensions = preferences.get("extensions", {}) commands = extensions.get("commands", {}) settings = extensions.get("settings", {}) @@ -156,7 +161,8 @@ unverified_repairs_exist() { for profile_root in "${profile_roots[@]}"; do for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do - [[ -f $backup ]] && return 0 + [[ -f $backup ]] || continue + python3 -c "$repair_py" "${backup%.omarchy-copy-url-repair.bak}" "$pinned_id" readable && return 0 done done @@ -179,7 +185,8 @@ profile_open() { if [[ $pid =~ ^[0-9]+$ ]] && kill -0 "$pid" 2>/dev/null; then return 0 fi - return 1 + [[ -S $profile/SingletonSocket ]] + return fi [[ -e $lock || -S $profile/SingletonSocket ]] @@ -202,6 +209,7 @@ affected_profile_open() { for profile_root in "${profile_roots[@]}"; do for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do [[ -f $backup ]] || continue + python3 -c "$repair_py" "${backup%.omarchy-copy-url-repair.bak}" "$pinned_id" readable || continue profile_open "$(dirname "$(dirname "$backup")")" && return 0 done done @@ -271,6 +279,10 @@ for profile_root in "${profile_roots[@]}"; do for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do [[ -f $backup ]] || continue preferences=${backup%.omarchy-copy-url-repair.bak} + # Missing/corrupt profiles are outside this repair. Preserve their recovery + # backup without blocking unrelated migrations; a restored profile is + # checked again on the next login or explicit repair. + python3 -c "$repair_py" "$preferences" "$pinned_id" readable || continue if ! python3 -c "$repair_py" "$preferences" "$pinned_id" verify; then if (( prompt )); then echo "The Copy URL shortcut repair could not be verified." >&2 diff --git a/test/shell.d/copy-url-repair-test.sh b/test/shell.d/copy-url-repair-test.sh index 8914c212a89c..0a9a0fa85bcf 100644 --- a/test/shell.d/copy-url-repair-test.sh +++ b/test/shell.d/copy-url-repair-test.sh @@ -113,6 +113,42 @@ pass "stale SingletonLock does not block the repair" rm -f "$preferences.omarchy-copy-url-repair.bak" close_browser +# A lock whose PID is not locally visible can still have a browser socket. +write_stale_preferences +stale_browser_lock +python3 - "$profile_root/SingletonSocket" <<'PY_SOCKET' +import socket +import sys +with socket.socket(socket.AF_UNIX) as sock: + sock.bind(sys.argv[1]) +PY_SOCKET +before_hash=$(sha256sum "$preferences" | cut -d' ' -f1) +run_migration && fail "socket prevents repair when the lock PID is unavailable" +[[ $(sha256sum "$preferences" | cut -d' ' -f1) == "$before_hash" ]] || + fail "socket-protected preferences remain untouched" +rm -f "$profile_root/SingletonSocket" +close_browser +pass "browser socket protects a profile with an unavailable lock PID" + +# An interrupted backup must not block all future migrations if its profile +# was removed or became unreadable. Keep the backup for manual recovery. +write_stale_preferences +cp "$preferences" "$preferences.omarchy-copy-url-repair.bak" +backup_hash=$(sha256sum "$preferences.omarchy-copy-url-repair.bak" | cut -d' ' -f1) +rm "$preferences" +open_browser +run_migration || fail "missing profile does not hold unrelated migrations" +printf '{broken json' >"$preferences" +run_migration || fail "corrupt profile does not hold unrelated migrations" +[[ $(cat "$preferences") == '{broken json' ]] || fail "corrupt preferences remain untouched" +[[ $(sha256sum "$preferences.omarchy-copy-url-repair.bak" | cut -d' ' -f1) == "$backup_hash" ]] || + fail "unreadable profile retains its recovery backup" +close_browser +write_stale_preferences +run_migration || fail "restored profile can be repaired later" +assert_repaired || fail "restored profile receives the shortcut repair" +pass "missing and corrupt profiles keep backups without blocking migrations" + # gum paints its prompt on stderr, so that stream has to stay attached: # suppressing it leaves gum reading keys behind an unpainted screen, which # reads as a hung update. From 213ccc0dc57d5f2db8002b9713333f1c7f1eafbf Mon Sep 17 00:00:00 2001 From: Taksh Date: Wed, 30 Sep 2026 16:04:28 +0530 Subject: [PATCH 3/4] Distinguish live browser sockets from abandoned paths --- bin/omarchy-cmd-repair-chromium-copy-url | 22 +++++++++++++-- test/shell.d/copy-url-repair-test.sh | 35 +++++++++++++++++++++--- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/bin/omarchy-cmd-repair-chromium-copy-url b/bin/omarchy-cmd-repair-chromium-copy-url index 624386b9bcaf..76824e73342c 100755 --- a/bin/omarchy-cmd-repair-chromium-copy-url +++ b/bin/omarchy-cmd-repair-chromium-copy-url @@ -174,6 +174,24 @@ unverified_repairs_exist() { # Chromium writes SingletonLock as a symlink to -; a lock left # behind by SIGTERM still looks like a lock if we only test for the symlink. # Trust a live PID (or a non-symlink lock / live socket), not a dangling one. +socket_open() { + [[ -S $1 ]] || return 1 + python3 - "$1" <<'PY_SOCKET' +import errno +import socket +import sys + +try: + with socket.socket(socket.AF_UNIX) as sock: + sock.settimeout(0.2) + sock.connect(sys.argv[1]) +except OSError as error: + # A refused connection proves the socket is abandoned. Other failures, + # including permission denial or timeout, cannot prove the profile idle. + sys.exit(1 if error.errno in (errno.ECONNREFUSED, errno.ENOENT) else 0) +PY_SOCKET +} + profile_open() { local profile=$1 local lock=$profile/SingletonLock @@ -185,11 +203,11 @@ profile_open() { if [[ $pid =~ ^[0-9]+$ ]] && kill -0 "$pid" 2>/dev/null; then return 0 fi - [[ -S $profile/SingletonSocket ]] + socket_open "$profile/SingletonSocket" return fi - [[ -e $lock || -S $profile/SingletonSocket ]] + [[ -e $lock ]] || socket_open "$profile/SingletonSocket" } # Gate on a pending — or to-be-verified — profile actually being open, not on diff --git a/test/shell.d/copy-url-repair-test.sh b/test/shell.d/copy-url-repair-test.sh index 0a9a0fa85bcf..08d02858da2c 100644 --- a/test/shell.d/copy-url-repair-test.sh +++ b/test/shell.d/copy-url-repair-test.sh @@ -9,7 +9,15 @@ require_command python3 repair_cmd="$ROOT/bin/omarchy-cmd-repair-chromium-copy-url" test_dir=$(mktemp -d) -trap 'rm -rf "$test_dir"' EXIT +socket_pid="" +cleanup() { + if [[ -n $socket_pid ]]; then + kill "$socket_pid" 2>/dev/null || true + wait "$socket_pid" 2>/dev/null || true + fi + rm -rf "$test_dir" +} +trap cleanup EXIT fixture_home="$test_dir/home" profile_root="$fixture_home/.config/chromium" @@ -116,20 +124,39 @@ close_browser # A lock whose PID is not locally visible can still have a browser socket. write_stale_preferences stale_browser_lock -python3 - "$profile_root/SingletonSocket" <<'PY_SOCKET' +python3 - "$profile_root/SingletonSocket" "$test_dir/socket-ready" <<'PY_SOCKET' & +from pathlib import Path +import signal import socket import sys with socket.socket(socket.AF_UNIX) as sock: sock.bind(sys.argv[1]) + sock.listen(5) + Path(sys.argv[2]).touch() + signal.pause() PY_SOCKET +socket_pid=$! +for attempt in {1..100}; do + [[ -f $test_dir/socket-ready ]] && break + sleep 0.01 +done +[[ -f $test_dir/socket-ready ]] || fail "fixture socket starts listening" before_hash=$(sha256sum "$preferences" | cut -d' ' -f1) run_migration && fail "socket prevents repair when the lock PID is unavailable" [[ $(sha256sum "$preferences" | cut -d' ' -f1) == "$before_hash" ]] || fail "socket-protected preferences remain untouched" -rm -f "$profile_root/SingletonSocket" -close_browser pass "browser socket protects a profile with an unavailable lock PID" +kill "$socket_pid" +wait "$socket_pid" 2>/dev/null || true +socket_pid="" +[[ -S $profile_root/SingletonSocket ]] || fail "fixture leaves an abandoned socket" +run_migration || fail "abandoned socket does not block migration" +assert_repaired || fail "repair proceeds after the socket listener exits" +rm -f "$profile_root/SingletonSocket" "$preferences.omarchy-copy-url-repair.bak" +close_browser +pass "abandoned socket does not block repair" + # An interrupted backup must not block all future migrations if its profile # was removed or became unreadable. Keep the backup for manual recovery. write_stale_preferences From 2ebea4f17dbb22f2b7b171808bed61ecfd11658c Mon Sep 17 00:00:00 2001 From: Omarchybot <317366263+omarchybot@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:11:27 +0100 Subject: [PATCH 4/4] Keep extracted Copy URL repair compatible with existing profiles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Include Brave Origin profile roots so extracting the migration cannot silently skip its callers. Give pending repairs their own backup suffix: shipped migrations left ordinary backups after success, and treating those as pending would prompt users with already repaired browsers and block later migrations. Keep every repair behavior case in the existing Chromium extension suite and exercise the real login and first-run orchestrators. Restore quattro's intentional removal of the obsolete first-run test from 0fae8e6966 instead of resurrecting assertions for retired setup behavior; the Copy URL wiring is covered by runtime assertions. Remove comments promising browser-launch retries that no longer exist. 🤖 Generated by GPT-6 High in Codex. Reviewed by Opus 5.5 High and muse-wonder via pi, thinking high. Co-Authored-By: GPT-6 High Co-Authored-By: Opus 5.5 High --- bin/omarchy-cmd-repair-chromium-copy-url | 25 +- install/user/first-run/chromium-copy-url.sh | 11 +- migrations/1786643346.sh | 6 +- migrations/1787601557.sh | 6 +- test/shell.d/chromium-extensions-test.sh | 418 ++++++++++++++++++++ test/shell.d/copy-url-repair-test.sh | 363 ----------------- test/shell.d/first-run-test.sh | 156 -------- test/shell.d/systemd-test.sh | 3 - 8 files changed, 437 insertions(+), 551 deletions(-) delete mode 100644 test/shell.d/copy-url-repair-test.sh delete mode 100644 test/shell.d/first-run-test.sh diff --git a/bin/omarchy-cmd-repair-chromium-copy-url b/bin/omarchy-cmd-repair-chromium-copy-url index 76824e73342c..2ce297c71c3a 100755 --- a/bin/omarchy-cmd-repair-chromium-copy-url +++ b/bin/omarchy-cmd-repair-chromium-copy-url @@ -76,7 +76,7 @@ if mode == "check": if mode == "verify": if any(ghost_id(c) for c in commands.values()): sys.exit(1) - path.with_name(path.name + ".omarchy-copy-url-repair.bak").unlink(missing_ok=True) + path.with_name(path.name + ".omarchy-copy-url-repair.pending.bak").unlink(missing_ok=True) sys.exit(0) # Chromium keeps one shortcut per command, so a ghost may only be rebound @@ -104,7 +104,7 @@ for accelerator in list(commands): changed = True if changed: - shutil.copy2(path, path.with_name(path.name + ".omarchy-copy-url-repair.bak")) + shutil.copy2(path, path.with_name(path.name + ".omarchy-copy-url-repair.pending.bak")) text = json.dumps(preferences, separators=(",", ":")) fd, tmp_name = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.") try: @@ -131,6 +131,9 @@ profile_roots=( "$HOME/.config/BraveSoftware/Brave-Browser" "$HOME/.config/BraveSoftware/Brave-Browser-Beta" "$HOME/.config/BraveSoftware/Brave-Browser-Nightly" + "$HOME/.config/BraveSoftware/Brave-Origin" + "$HOME/.config/BraveSoftware/Brave-Origin-Beta" + "$HOME/.config/BraveSoftware/Brave-Origin-Nightly" "$HOME/.config/microsoft-edge" "$HOME/.config/microsoft-edge-beta" "$HOME/.config/microsoft-edge-dev" @@ -152,17 +155,15 @@ find_pending() { done } -# The backup a repair leaves next to Preferences marks it as attempted but not -# yet verified: a browser that was open during the repair still holds the -# stale registration in memory and restores it when it exits, possibly after -# the disk was already seen clean. +# Pending backups mark repairs this command has not verified. Ordinary .bak +# files came from shipped migrations and do not mean a repair is still pending. unverified_repairs_exist() { local profile_root backup for profile_root in "${profile_roots[@]}"; do - for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do + for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.pending.bak; do [[ -f $backup ]] || continue - python3 -c "$repair_py" "${backup%.omarchy-copy-url-repair.bak}" "$pinned_id" readable && return 0 + python3 -c "$repair_py" "${backup%.omarchy-copy-url-repair.pending.bak}" "$pinned_id" readable && return 0 done done @@ -225,9 +226,9 @@ affected_profile_open() { fi for profile_root in "${profile_roots[@]}"; do - for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do + for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.pending.bak; do [[ -f $backup ]] || continue - python3 -c "$repair_py" "${backup%.omarchy-copy-url-repair.bak}" "$pinned_id" readable || continue + python3 -c "$repair_py" "${backup%.omarchy-copy-url-repair.pending.bak}" "$pinned_id" readable || continue profile_open "$(dirname "$(dirname "$backup")")" && return 0 done done @@ -294,9 +295,9 @@ fi # left by an interrupted earlier run. Once the file is clean, the backup is no # longer a pending marker: keeping it would prompt on every later browser run. for profile_root in "${profile_roots[@]}"; do - for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.bak; do + for backup in "$profile_root"/*/Preferences.omarchy-copy-url-repair.pending.bak; do [[ -f $backup ]] || continue - preferences=${backup%.omarchy-copy-url-repair.bak} + preferences=${backup%.omarchy-copy-url-repair.pending.bak} # Missing/corrupt profiles are outside this repair. Preserve their recovery # backup without blocking unrelated migrations; a restored profile is # checked again on the next login or explicit repair. diff --git a/install/user/first-run/chromium-copy-url.sh b/install/user/first-run/chromium-copy-url.sh index fd680769a301..948bd45fe6b7 100755 --- a/install/user/first-run/chromium-copy-url.sh +++ b/install/user/first-run/chromium-copy-url.sh @@ -1,14 +1,7 @@ #!/bin/bash -# Repair a Copy URL shortcut registered under a path-derived extension id. -# Runs at first-run rather than as a migration because the finalizer marks all -# shipped migrations complete on a fresh install. The Chromium profile is not -# part of that install — it commonly arrives afterwards from sync or a restored -# ~/.config/chromium — so install-time stamping cannot satisfy this repair. -# -# Idempotent: no-ops when Preferences is missing or already points at the -# pinned id. A running browser is skipped and retried from login notify or -# the next browser launch, which are also not migration-stamped. +# Browser profiles can arrive after shipped migrations were stamped complete. +# Quiet first-run repair leaves open profiles for a later login to repair. set -euo pipefail diff --git a/migrations/1786643346.sh b/migrations/1786643346.sh index 039dcfb26c15..c80a567f7b32 100644 --- a/migrations/1786643346.sh +++ b/migrations/1786643346.sh @@ -1,7 +1,5 @@ echo "Repair the Copy URL shortcut for profiles that predate its pinned extension id" -# Fresh installs stamp every shipped migration complete, so a Chromium profile -# that arrives later via sync never runs this file. Keep it for unstamped -# upgrades; omarchy-cmd-repair-chromium-copy-url is also invoked from first-run, -# login notify, and browser launch, which are not migration-stamped. +# Keep unstamped upgrades on the same repair as first-run and login, which +# cover profiles restored after shipped migrations were stamped complete. omarchy-cmd-repair-chromium-copy-url --prompt diff --git a/migrations/1787601557.sh b/migrations/1787601557.sh index 91efa825af57..97e8010d9d42 100644 --- a/migrations/1787601557.sh +++ b/migrations/1787601557.sh @@ -1,7 +1,5 @@ echo "Retry the Copy URL shortcut repair after install-time migration stamping" -# 1786643346 is already stamped complete on fresh installs, so a Chromium -# profile that arrived later via sync never got the repair. This covers -# existing users on update. The same command is also invoked from first-run, -# login notify, and browser launch, which are not migration-stamped. +# Retry for existing installs that already stamped the original migration. +# First-run and login cover profiles that arrive after this migration runs. omarchy-cmd-repair-chromium-copy-url --prompt diff --git a/test/shell.d/chromium-extensions-test.sh b/test/shell.d/chromium-extensions-test.sh index fc9120d38bba..e0c5782551d0 100644 --- a/test/shell.d/chromium-extensions-test.sh +++ b/test/shell.d/chromium-extensions-test.sh @@ -204,3 +204,421 @@ JS else skip "node is not installed; skipping the WhatsApp Slim content script check" fi + +# --- Copy URL profile repair --- + +( +require_command python3 + +repair_cmd="$ROOT/bin/omarchy-cmd-repair-chromium-copy-url" +test_dir=$(mktemp -d) +socket_pid="" +cleanup() { + if [[ -n $socket_pid ]]; then + kill "$socket_pid" 2>/dev/null || true + wait "$socket_pid" 2>/dev/null || true + fi + rm -rf "$test_dir" +} +trap cleanup EXIT + +fixture_home="$test_dir/home" +profile_root="$fixture_home/.config/chromium" +preferences="$profile_root/Default/Preferences" +mkdir -p "$(dirname "$preferences")" + +# Any id Chromium once derived from the extension's keyless load path; the +# repair keys off the registered command name, not the id. +ghost_id="ikkebdkaanlebnifjnbeiaklodhbjcci" +pinned_id="bgpiichlckmfanooecilcjemknkcpngb" + +write_stale_preferences() { + jq -n --arg ghost "$ghost_id" --arg pinned "$pinned_id" '{ + extensions: { + commands: {"linux:Alt+Shift+L": {command_name: "copy-url", extension: $ghost, global: false}}, + settings: { + ($ghost): {commands: {"copy-url": {suggested_key: "Alt+Shift+L", was_assigned: true}}}, + ($pinned): {commands: {"copy-url": {suggested_key: "Alt+Shift+L"}}} + } + }, + protection: {macs: {extensions: {commands: "stale-command-mac", settings: {($ghost): "stale-settings-mac"}}}} + }' >"$preferences" +} + +stub_bin="$test_dir/bin" +mkdir -p "$stub_bin" + +REAL_PYTHON=$(command -v python3) +export REAL_PYTHON + +run_repair() { + HOME="$fixture_home" PATH="$stub_bin:$ROOT/bin:$PATH" "$repair_cmd" >/dev/null 2>&1 +} + +run_migration() { + HOME="$fixture_home" PATH="$stub_bin:$ROOT/bin:$PATH" bash -euo pipefail "$repair_cmd" --prompt >/dev/null 2>&1 +} + +# A running Chromium-family browser marks its profile root with a SingletonLock +# symlink to -. Only a live PID counts as attached; a lock left +# by SIGTERM still points at a dead pid and must not block the repair. +open_browser() { + mkdir -p "$profile_root" + ln -sfn "test-host-$$" "$profile_root/SingletonLock" +} +stale_browser_lock() { + mkdir -p "$profile_root" + ln -sfn "test-host-999999999" "$profile_root/SingletonLock" +} +close_browser() { + rm -f "$profile_root/SingletonLock" +} + +assert_repaired() { + jq -e --arg ghost "$ghost_id" --arg pinned "$pinned_id" ' + .extensions.commands["linux:Alt+Shift+L"].extension == $pinned and + (.extensions.settings | has($ghost) | not) and + .extensions.settings[$pinned].commands["copy-url"].was_assigned == true and + .protection.macs.extensions.commands == "stale-command-mac" + ' "$preferences" >/dev/null +} + +assert_no_tmp() { + local leftover + leftover=$(find "$profile_root/Default" -maxdepth 1 \( -name '.Preferences.*' -o -name 'Preferences.tmp*' \)) + [[ -z $leftover ]] +} + +# A running Chromium-family browser marks its profile root with a SingletonLock +# symlink to -, a target that never exists on disk. That lock — +# not the mere presence of a browser process — is what the repair waits on. +# The affected profile being open prompts for the windows to be closed; +# declining (or having no terminal to ask in) defers the repair so a +# rewrite-on-exit cannot revert it. +printf '#!/bin/bash\nexit 1\n' >"$stub_bin/gum" +chmod +x "$stub_bin/gum" +write_stale_preferences +open_browser + +before_hash=$(sha256sum "$preferences" | cut -d' ' -f1) + +run_migration && fail "migration defers while the affected profile is open" +[[ $(sha256sum "$preferences" | cut -d' ' -f1) == "$before_hash" ]] || + fail "migration leaves preferences alone while the affected profile is open" +pass "migration defers the repair while the affected profile is open" + +# Quiet login repair skips without gum and without failing while a profile +# is running. +run_repair || fail "quiet repair skips while the affected profile is open" +[[ $(sha256sum "$preferences" | cut -d' ' -f1) == "$before_hash" ]] || + fail "quiet repair leaves preferences alone while the affected profile is open" +pass "quiet repair skips while the affected profile is open" + +# A dangling SingletonLock whose pid is dead is not an attached browser; SIGTERM +# leaves that symlink behind and the repair must still persist. +stale_browser_lock +run_repair || fail "quiet repair proceeds past a stale SingletonLock" +assert_repaired || fail "quiet repair rewrites preferences behind a stale lock" +assert_no_tmp || fail "repair leaves no Preferences temp file behind a stale lock" +pass "stale SingletonLock does not block the repair" +rm -f "$preferences.omarchy-copy-url-repair.pending.bak" +close_browser + +# A lock whose PID is not locally visible can still have a browser socket. +write_stale_preferences +stale_browser_lock +python3 - "$profile_root/SingletonSocket" "$test_dir/socket-ready" <<'PY_SOCKET' & +from pathlib import Path +import signal +import socket +import sys +with socket.socket(socket.AF_UNIX) as sock: + sock.bind(sys.argv[1]) + sock.listen(5) + Path(sys.argv[2]).touch() + signal.pause() +PY_SOCKET +socket_pid=$! +for attempt in {1..100}; do + [[ -f $test_dir/socket-ready ]] && break + sleep 0.01 +done +[[ -f $test_dir/socket-ready ]] || fail "fixture socket starts listening" +before_hash=$(sha256sum "$preferences" | cut -d' ' -f1) +run_migration && fail "socket prevents repair when the lock PID is unavailable" +[[ $(sha256sum "$preferences" | cut -d' ' -f1) == "$before_hash" ]] || + fail "socket-protected preferences remain untouched" +pass "browser socket protects a profile with an unavailable lock PID" + +kill "$socket_pid" +wait "$socket_pid" 2>/dev/null || true +socket_pid="" +[[ -S $profile_root/SingletonSocket ]] || fail "fixture leaves an abandoned socket" +run_migration || fail "abandoned socket does not block migration" +assert_repaired || fail "repair proceeds after the socket listener exits" +rm -f "$profile_root/SingletonSocket" "$preferences.omarchy-copy-url-repair.pending.bak" +close_browser +pass "abandoned socket does not block repair" + +# An interrupted backup must not block all future migrations if its profile +# was removed or became unreadable. Keep the backup for manual recovery. +write_stale_preferences +cp "$preferences" "$preferences.omarchy-copy-url-repair.pending.bak" +backup_hash=$(sha256sum "$preferences.omarchy-copy-url-repair.pending.bak" | cut -d' ' -f1) +rm "$preferences" +open_browser +run_migration || fail "missing profile does not hold unrelated migrations" +printf '{broken json' >"$preferences" +run_migration || fail "corrupt profile does not hold unrelated migrations" +[[ $(cat "$preferences") == '{broken json' ]] || fail "corrupt preferences remain untouched" +[[ $(sha256sum "$preferences.omarchy-copy-url-repair.pending.bak" | cut -d' ' -f1) == "$backup_hash" ]] || + fail "unreadable profile retains its recovery backup" +close_browser +write_stale_preferences +run_migration || fail "restored profile can be repaired later" +assert_repaired || fail "restored profile receives the shortcut repair" +pass "missing and corrupt profiles keep backups without blocking migrations" + +# gum paints its prompt on stderr, so that stream has to stay attached: +# suppressing it leaves gum reading keys behind an unpainted screen, which +# reads as a hung update. +cat >"$stub_bin/gum" <<'STUB' +#!/bin/bash +echo "gum-prompt-painted" >&2 +exit 1 +STUB +write_stale_preferences +open_browser +prompt_stderr="$test_dir/prompt-stderr" +HOME="$fixture_home" PATH="$stub_bin:$ROOT/bin:$PATH" bash -euo pipefail "$repair_cmd" --prompt >/dev/null 2>"$prompt_stderr" && + fail "migration defers when the browser prompt is declined" +grep -q "gum-prompt-painted" "$prompt_stderr" || fail "migration keeps the browser prompt visible" +pass "migration keeps the browser prompt visible" + +# A browser holding a different profile root cannot revert this repair, so it +# must not hold the update: the repair goes through without ever reaching the +# prompt, which the still-declining gum stub would otherwise fail. +close_browser +mkdir -p "$fixture_home/.config/google-chrome" +ln -sfn "test-host-$$" "$fixture_home/.config/google-chrome/SingletonLock" +write_stale_preferences +run_migration || fail "migration repairs while a different profile root is open" +assert_repaired || fail "migration repairs the shortcut while a different profile root is open" +pass "migration ignores a browser on a different profile root" +rm -f "$fixture_home/.config/google-chrome/SingletonLock" "$preferences.omarchy-copy-url-repair.pending.bak" + +# Closing the affected profile and confirming the prompt lets the repair +# proceed. +write_stale_preferences +open_browser +cat >"$stub_bin/gum" <<'STUB' +#!/bin/bash +"$CLOSE_BROWSER" +touch "${GUM_CALLED:?}" +exit 0 +STUB +cat >"$stub_bin/close-browser" <<'STUB' +#!/bin/bash +rm -f "$HOME/.config/chromium/SingletonLock" +STUB +chmod +x "$stub_bin/gum" "$stub_bin/close-browser" +GUM_CALLED="$test_dir/gum-called" CLOSE_BROWSER="$stub_bin/close-browser" \ + HOME="$fixture_home" PATH="$stub_bin:$ROOT/bin:$PATH" bash -euo pipefail "$repair_cmd" --prompt >/dev/null 2>&1 || + fail "migration proceeds once the profile is closed and the prompt confirmed" +[[ -e $test_dir/gum-called ]] || fail "migration asks before repairing under a running browser" +assert_repaired || fail "migration repairs after the browser prompt is confirmed" +pass "migration asks to close the browser and repairs on confirmation" +rm -f "$preferences.omarchy-copy-url-repair.pending.bak" + +# With the affected profile closed the ghost registration moves to the pinned id. +printf '#!/bin/bash\nexit 1\n' >"$stub_bin/gum" +close_browser +write_stale_preferences +run_migration || fail "migration repairs the shortcut when no browser is running" + +assert_repaired || fail "migration rebinds the Copy URL shortcut to the pinned extension id" +[[ ! -e $preferences.omarchy-copy-url-repair.pending.bak ]] || + fail "verified repair clears its pending backup" +assert_no_tmp || fail "repair leaves no Preferences temp file after a successful write" +pass "migration rebinds the Copy URL shortcut to the pinned extension id" + +# Install-time stamping must not prevent the command from repairing a profile +# that arrived later. +mkdir -p "$fixture_home/.local/state/omarchy/migrations" +touch "$fixture_home/.local/state/omarchy/migrations/1786643346.sh" + +rm -f "$preferences.omarchy-copy-url-repair.pending.bak" +write_stale_preferences +run_repair || fail "repair command runs even when the migrations are stamped complete" +assert_repaired || fail "stamped migrations still leave the command able to repair" +pass "repair command is independent of install-time migration stamps" +rm -f "$preferences.omarchy-copy-url-repair.pending.bak" +rm -rf "$fixture_home/.local/state/omarchy/migrations" + +# A repaired profile has no ghost registration left, so nothing is pending — +# even while that same profile is open. +repaired_hash=$(sha256sum "$preferences" | cut -d' ' -f1) +open_browser +run_migration || fail "migration reruns cleanly after the repair" +[[ $(sha256sum "$preferences" | cut -d' ' -f1) == "$repaired_hash" && ! -e $preferences.omarchy-copy-url-repair.pending.bak ]] || + fail "migration is idempotent after the repair" +pass "migration is idempotent after the repair" +close_browser + +# A remapped shortcut keeps the user's chosen key while moving to the pinned id. +jq -n --arg ghost "$ghost_id" '{extensions: {commands: {"linux:Ctrl+Alt+P": {command_name: "copy-url", extension: $ghost, global: false}}, settings: {}}}' >"$preferences" +run_migration || fail "migration repairs remapped shortcuts" +jq -e --arg pinned "$pinned_id" '.extensions.commands["linux:Ctrl+Alt+P"].extension == $pinned' "$preferences" >/dev/null || + fail "migration keeps the remapped key while rebinding to the pinned id" +pass "migration keeps remapped shortcut keys" + +# When the pinned extension already holds a copy-url binding (the user fixed +# it by hand), the ghost is dropped rather than doubled into a second binding. +jq -n --arg ghost "$ghost_id" --arg pinned "$pinned_id" '{extensions: {commands: {"linux:Ctrl+Alt+P": {command_name: "copy-url", extension: $pinned, global: false}, "linux:Alt+Shift+L": {command_name: "copy-url", extension: $ghost, global: false}}, settings: {}}}' >"$preferences" +run_migration || fail "migration cleans ghosts alongside a manual repair" +jq -e --arg pinned "$pinned_id" ' + (.extensions.commands | has("linux:Alt+Shift+L") | not) and + .extensions.commands["linux:Ctrl+Alt+P"].extension == $pinned +' "$preferences" >/dev/null || fail "migration drops the ghost instead of double-binding the pinned extension" +pass "migration never double-binds the pinned extension" + +# A browser starting mid-repair may write stale Preferences back on exit, so +# the migration must stay pending for a later browser-free run to verify. A +# stub hands the repair call through and opens the profile right after it. +write_stale_preferences +close_browser +rm -f "$preferences.omarchy-copy-url-repair.pending.bak" +cat >"$stub_bin/python3" <<'STUB' +#!/bin/bash +# Called as `python3 -c