Skip to content
Merged
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ apt:
- code
- python3-venv
- sonic-pi
- pipewire-jack
python:
packages:
jamkit: "0.1.0"
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/nirj_agent/manifests/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions src/nirj_agent/services/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion src/nirj_agent/services/boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 32 additions & 1 deletion src/nirj_agent/services/desktop_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Expand All @@ -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}"
Expand Down
4 changes: 4 additions & 0 deletions src/nirj_agent/storage/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AgentPaths:
source_background: Path
wallpaper_autostart: Path
desktop_dir: Path
applications_dir: Path
python_environment: Path

@classmethod
Expand Down Expand Up @@ -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"),
)

Expand All @@ -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",
)

Expand Down Expand Up @@ -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",
)

Expand Down
7 changes: 6 additions & 1 deletion tests/manifests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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",
[
Expand Down
32 changes: 32 additions & 0 deletions tests/services/test_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/services/test_desktop_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ())
Loading