diff --git a/README.md b/README.md index f5c175f..da12cc7 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ apt: - code - python3-venv - sonic-pi + - pipewire-jack python: packages: jamkit: "0.1.0" @@ -142,6 +143,13 @@ desktop: - 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 594328e..594ad96 100644 --- a/src/nirj_agent/manifests/parser.py +++ b/src/nirj_agent/manifests/parser.py @@ -147,6 +147,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/apply.py b/src/nirj_agent/services/apply.py index 996bc24..bf4da9a 100644 --- a/src/nirj_agent/services/apply.py +++ b/src/nirj_agent/services/apply.py @@ -13,7 +13,7 @@ from nirj_agent.storage.lock import exclusive_lock from nirj_agent.storage.paths import AgentPaths -from .desktop_shortcuts import reconcile_desktop_shortcuts +from .desktop_shortcuts import SHORTCUT_PACKAGES, reconcile_desktop_shortcuts from .python_setup import reconcile_python_setup from .reconciliation import ( ReconciliationPlan, @@ -158,10 +158,9 @@ def install_apt() -> None: python_packages = tuple(sorted(managed.items())) reconcile_python_setup(paths) - required_packages = {"vscode": "code", "sonic-pi": "sonic-pi"} shortcuts = tuple( shortcut for shortcut in manifest.desktop.shortcuts - if required_packages[shortcut] in available_packages + if SHORTCUT_PACKAGES[shortcut] <= available_packages ) for shortcut in set(manifest.desktop.shortcuts) - set(shortcuts): message = f"Skipped {shortcut} shortcut: required package is not confirmed installed" diff --git a/src/nirj_agent/services/boot.py b/src/nirj_agent/services/boot.py index deded14..e814750 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, PipProvider 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, @@ -79,7 +81,11 @@ def boot_prep( return result check = check_for_update(paths, client, persist_target=True) - if check.update_available or load_state(paths.state).errors: + 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 or load_state(paths.state).errors: 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 d9bc6f6..1709770 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 python_environment: Path @classmethod @@ -65,6 +66,7 @@ def system(cls) -> "AgentPaths": "/etc/xdg/autostart/nirj-wallpaper.desktop" ), desktop_dir=Path("/home/jam/Desktop"), + applications_dir=Path("/usr/local/share/applications"), python_environment=Path("/data/nirj/python-venv"), ) @@ -86,6 +88,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", python_environment=root / "python-venv", ) @@ -116,6 +119,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", python_environment=data_root / "python-venv", ) diff --git a/tests/manifests/test_parser.py b/tests/manifests/test_parser.py index 63ce61d..5c2ac2f 100644 --- a/tests/manifests/test_parser.py +++ b/tests/manifests/test_parser.py @@ -139,7 +139,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" ) @@ -157,6 +157,11 @@ def test_parse_manifest_requires_sonic_pi_for_shortcut() -> None: ): 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") + @pytest.mark.parametrize( "packages", [ diff --git a/tests/services/test_boot.py b/tests/services/test_boot.py index 6d79350..9c03c19 100644 --- a/tests/services/test_boot.py +++ b/tests/services/test_boot.py @@ -182,6 +182,38 @@ def test_desktop_setup_is_persisted_on_writable_boot(tmp_path) -> None: ) +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(), PythonPackages(), overlay) + assert result.action == "update_pending" + assert overlay.events == ["disable", "reboot"] + + overlay = Overlay(active=False) + result = boot_prep(paths, SonicClient(), SonicPackages(), PythonPackages(), 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(), PythonPackages(), overlay) + assert result.action == "ready" + assert overlay.events == [] + def test_partial_package_failure_keeps_agent_startable_and_retries(tmp_path): from nirj_agent.config.store import set_config_value from nirj_agent.providers import AptProviderError 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, ())