From 43c75f18fb9507a312fd4699788348aff76d0493 Mon Sep 17 00:00:00 2001 From: James Dougan Date: Fri, 13 Nov 2026 22:52:35 +0000 Subject: [PATCH] fix sonic pi boot sequence --- README.md | 8 +++++ src/nirj_agent/manifests/parser.py | 6 ++++ src/nirj_agent/services/boot.py | 8 ++++- src/nirj_agent/services/desktop_shortcuts.py | 33 +++++++++++++++++++- src/nirj_agent/storage/paths.py | 4 +++ tests/manifests/test_parser.py | 7 ++++- tests/services/test_boot.py | 33 ++++++++++++++++++++ tests/services/test_desktop_shortcuts.py | 16 ++++++++++ 8 files changed, 112 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b415c7f..5e7300e 100644 --- a/README.md +++ b/README.md @@ -109,12 +109,20 @@ apt: packages: - code - sonic-pi + - pipewire-jack desktop: shortcuts: - vscode - sonic-pi ``` +Sonic Pi shortcuts require both `sonic-pi` and `pipewire-jack`. The agent +launches Sonic Pi through `pw-jack` from the desktop shortcut and a managed +`/usr/local/share/applications/sonic-pi.desktop` menu override. Audio uses the +desktop session; no HDMI or USB device names are configured. HDMI playback +has been verified on one Pi; USB playback and live output switching still +require device testing. Removing the shortcut also removes the menu override. + ## Development ```bash diff --git a/src/nirj_agent/manifests/parser.py b/src/nirj_agent/manifests/parser.py index ad4f3b7..222ddc7 100644 --- a/src/nirj_agent/manifests/parser.py +++ b/src/nirj_agent/manifests/parser.py @@ -97,6 +97,12 @@ def manifest_from_mapping(data: object, source: str) -> Manifest: "apt.packages" ) + if "sonic-pi" in shortcuts and "pipewire-jack" not in packages: + raise ManifestError( + f"desktop shortcut sonic-pi in {source} requires pipewire-jack in " + "apt.packages" + ) + return Manifest( schema=schema, apt=AptManifest( diff --git a/src/nirj_agent/services/boot.py b/src/nirj_agent/services/boot.py index 58e1804..b08a89b 100644 --- a/src/nirj_agent/services/boot.py +++ b/src/nirj_agent/services/boot.py @@ -2,6 +2,7 @@ from dataclasses import dataclass, replace from nirj_agent.config import load_config +from nirj_agent.manifests.parser import parse_manifest from nirj_agent.manifests.github import GitHubManifestClient from nirj_agent.providers import AptProvider from nirj_agent.state import load_state, save_state @@ -13,6 +14,7 @@ save_update_state, ) +from .desktop_shortcuts import desktop_shortcuts_need_reconcile from .desktop_setup import ( desktop_setup_needs_reconcile, reconcile_desktop_setup, @@ -76,7 +78,11 @@ def boot_prep( return result check = check_for_update(paths, client, persist_target=True) - if check.update_available: + target_manifest = parse_manifest(paths.target_manifest.read_bytes()) + shortcuts_required = desktop_shortcuts_need_reconcile( + paths, target_manifest.desktop.shortcuts, + ) + if check.update_available or shortcuts_required: save_update_state( UpdateState(UpdatePhase.PENDING, check.target_hash), paths.update_state, diff --git a/src/nirj_agent/services/desktop_shortcuts.py b/src/nirj_agent/services/desktop_shortcuts.py index 849c344..3991f5f 100644 --- a/src/nirj_agent/services/desktop_shortcuts.py +++ b/src/nirj_agent/services/desktop_shortcuts.py @@ -19,7 +19,7 @@ Version=1.0 Type=Application Name=Sonic Pi -Exec=/usr/bin/sonic-pi +Exec=/usr/bin/pw-jack /usr/bin/sonic-pi Icon=sonic-pi Terminal=false Categories=AudioVideo;Audio;Development; @@ -45,6 +45,31 @@ class DesktopShortcut: } +SHORTCUT_PACKAGES = { + "vscode": frozenset({"code"}), + "sonic-pi": frozenset({"sonic-pi", "pipewire-jack"}), +} + + +def desktop_shortcuts_need_reconcile(paths: AgentPaths, desired: tuple[str, ...]) -> bool: + try: + for shortcut_id, shortcut in SHORTCUTS.items(): + path = paths.desktop_dir / shortcut.filename + if shortcut_id in desired: + if not path.exists() or path.read_bytes() != shortcut.content: + return True + if path.stat().st_mode & 0o777 != 0o755: + return True + elif path.exists(): + return True + menu = paths.applications_dir / "sonic-pi.desktop" + if "sonic-pi" in desired: + return not menu.exists() or menu.read_bytes() != SONIC_PI_DESKTOP_ENTRY + return menu.exists() + except OSError as exc: + raise DesktopShortcutError(f"Could not inspect desktop shortcuts: {exc}") from exc + + class DesktopShortcutError(RuntimeError): pass @@ -64,6 +89,12 @@ def reconcile_desktop_shortcuts( path.chmod(0o755) else: path.unlink(missing_ok=True) + menu = paths.applications_dir / "sonic-pi.desktop" + if "sonic-pi" in desired_set: + write_bytes(menu, SONIC_PI_DESKTOP_ENTRY) + menu.chmod(0o644) + else: + menu.unlink(missing_ok=True) except (OSError, FileStoreError) as exc: raise DesktopShortcutError( f"Could not reconcile desktop shortcuts: {exc}" diff --git a/src/nirj_agent/storage/paths.py b/src/nirj_agent/storage/paths.py index 015a559..c4584b1 100644 --- a/src/nirj_agent/storage/paths.py +++ b/src/nirj_agent/storage/paths.py @@ -20,6 +20,7 @@ class AgentPaths: source_background: Path wallpaper_autostart: Path desktop_dir: Path + applications_dir: Path @classmethod def system(cls) -> "AgentPaths": @@ -64,6 +65,7 @@ def system(cls) -> "AgentPaths": "/etc/xdg/autostart/nirj-wallpaper.desktop" ), desktop_dir=Path("/home/jam/Desktop"), + applications_dir=Path("/usr/local/share/applications"), ) @classmethod @@ -84,6 +86,7 @@ def windows(cls, root: Path, public_desktop: Path) -> "AgentPaths": source_background=root / "agent-repo/assets/background-base.png", wallpaper_autostart=root / "config/wallpaper", desktop_dir=public_desktop, + applications_dir=root / "applications", ) @classmethod @@ -113,6 +116,7 @@ def sandbox(cls, root: Path) -> "AgentPaths": root / "etc/xdg/autostart/nirj-wallpaper.desktop" ), desktop_dir=root / "home/jam/Desktop", + applications_dir=root / "usr/local/share/applications", ) diff --git a/tests/manifests/test_parser.py b/tests/manifests/test_parser.py index 0577711..9a8e5a3 100644 --- a/tests/manifests/test_parser.py +++ b/tests/manifests/test_parser.py @@ -131,7 +131,7 @@ def test_parse_manifest_requires_code_for_vscode_shortcut() -> None: def test_parse_manifest_accepts_sonic_pi_shortcut_with_package() -> None: content = ( b"schema: 1\n" - b"apt:\n packages: [sonic-pi]\n" + b"apt:\n packages: [sonic-pi, pipewire-jack]\n" b"desktop:\n shortcuts: [sonic-pi]\n" ) @@ -148,3 +148,8 @@ def test_parse_manifest_requires_sonic_pi_for_shortcut() -> None: match="requires sonic-pi in apt.packages", ): parse_manifest(content) + + +def test_sonic_pi_shortcut_requires_pipewire_jack() -> None: + with pytest.raises(ManifestError, match="requires pipewire-jack"): + parse_manifest(b"schema: 1\napt:\n packages: [sonic-pi]\ndesktop:\n shortcuts: [sonic-pi]\n") diff --git a/tests/services/test_boot.py b/tests/services/test_boot.py index ac30068..7715359 100644 --- a/tests/services/test_boot.py +++ b/tests/services/test_boot.py @@ -167,3 +167,36 @@ def test_desktop_setup_is_persisted_on_writable_boot(tmp_path) -> None: paths.base_background.read_bytes() == paths.source_background.read_bytes() ) + + +def test_boot_repairs_stale_shortcuts_even_when_manifest_unchanged(tmp_path) -> None: + manifest = ( + b"schema: 1\napt:\n packages: [sonic-pi, pipewire-jack]\n" + b"desktop:\n shortcuts: [sonic-pi]\n" + ) + + class SonicClient: + def fetch(self, _source): + return "https://example.test/manifest.yaml", manifest + + class SonicPackages(Packages): + def list_installed(self): + return {"sonic-pi", "pipewire-jack"} + + paths = prepare(tmp_path) + paths.current_manifest.parent.mkdir(parents=True, exist_ok=True) + paths.current_manifest.write_bytes(manifest) + overlay = Overlay(active=True) + result = boot_prep(paths, SonicClient(), SonicPackages(), overlay) + assert result.action == "update_pending" + assert overlay.events == ["disable", "reboot"] + + overlay = Overlay(active=False) + result = boot_prep(paths, SonicClient(), SonicPackages(), overlay) + assert result.action == "update_applied" + assert b"pw-jack" in (paths.applications_dir / "sonic-pi.desktop").read_bytes() + + overlay = Overlay(active=True) + result = boot_prep(paths, SonicClient(), SonicPackages(), overlay) + assert result.action == "ready" + assert overlay.events == [] diff --git a/tests/services/test_desktop_shortcuts.py b/tests/services/test_desktop_shortcuts.py index 91a399d..0a276b3 100644 --- a/tests/services/test_desktop_shortcuts.py +++ b/tests/services/test_desktop_shortcuts.py @@ -42,3 +42,19 @@ def test_reconcile_removes_unwanted_sonic_pi_shortcut(tmp_path) -> None: reconcile_desktop_shortcuts(paths, ()) assert not (paths.desktop_dir / "sonic-pi.desktop").exists() + + +def test_sonic_pi_menu_override_and_drift(tmp_path) -> None: + from nirj_agent.services.desktop_shortcuts import desktop_shortcuts_need_reconcile + + paths = AgentPaths.sandbox(tmp_path) + assert desktop_shortcuts_need_reconcile(paths, ("sonic-pi",)) + reconcile_desktop_shortcuts(paths, ("sonic-pi",)) + menu = paths.applications_dir / "sonic-pi.desktop" + assert b"Exec=/usr/bin/pw-jack /usr/bin/sonic-pi\n" in menu.read_bytes() + assert not desktop_shortcuts_need_reconcile(paths, ("sonic-pi",)) + menu.write_bytes(b"stale launcher") + assert desktop_shortcuts_need_reconcile(paths, ("sonic-pi",)) + reconcile_desktop_shortcuts(paths, ()) + assert not menu.exists() + assert not desktop_shortcuts_need_reconcile(paths, ())