Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(audio): macOS master ducking honors never_mute and survives failed restores

rubenluetke10-beep2356b4bcd653
diff --git a/jarvis/audio/ducking/macos.py b/jarvis/audio/ducking/macos.py
index 0ea2795a04..24c28b53b7 100644
--- a/jarvis/audio/ducking/macos.py
+++ b/jarvis/audio/ducking/macos.py
@@ -83,6 +83,17 @@ def _prewarm_script(bundle_id: str) -> str:
)
+def _is_running_script(bundle_id: str) -> str:
+ """Pure running-state query — never launches or scripts the app itself."""
+ return (
+ f'if application id "{bundle_id}" is running then\n'
+ f' return "+"\n'
+ f"else\n"
+ f' return "{_NOT_RUNNING}"\n'
+ f"end if"
+ )
+
+
def _master_duck_script(target: int) -> str:
return (
"set prev to output volume of (get volume settings)\n"
@@ -137,8 +148,10 @@ class MacOSScriptDucker:
# in that case lowered Jarvis's own TTS — the exact side effect the
# opt-in fallback exists to keep rare — for no gain at all.
player_running = False
+ skipped: list[tuple[str, str]] = []
for token, (name, bundle_id) in _PLAYERS.items():
if name.lower() in skip:
+ skipped.append((name, bundle_id))
continue
try:
proc = self._run(_duck_script(bundle_id, self._duck))
@@ -167,12 +180,33 @@ class MacOSScriptDucker:
except Exception: # noqa: BLE001 — timeout/TCC denial: skip player
log.debug("ducking skip (%s)", name, exc_info=True)
if not ducked and not player_running and self._master_fallback:
+ # never_mute must beat the fallback: the master output carries a
+ # protected player's audio too, so ducking it would silence exactly
+ # the app the user told us to leave alone. A merely-listed player
+ # that is NOT running keeps the fallback available for unknown
+ # audio sources (browsers etc.).
+ if self._any_running(skipped):
+ log.debug("ducking: master fallback withheld (never-mute player running)")
+ return ducked
try:
proc = self._run(_master_duck_script(self._duck))
prev = self._parse_volume(proc, "master")
- if prev is not None and prev > self._duck:
+ if prev is None:
+ pass
+ elif prev > self._duck:
self._saved[_MASTER_TOKEN] = prev
ducked.append(_MASTER_TOKEN)
+ elif _MASTER_TOKEN in self._saved:
+ # Same stranded-restore shape as the player re-adoption
+ # above, but for the WHOLE output volume: a master restore
+ # that never landed leaves the Mac quiet for good unless
+ # this session's restore takes the token over.
+ log.info(
+ "ducking: re-adopting the master output (still at the "
+ "duck volume with an unrestored level of %d)",
+ self._saved[_MASTER_TOKEN],
+ )
+ ducked.append(_MASTER_TOKEN)
except Exception: # noqa: BLE001
log.debug("ducking skip (master)", exc_info=True)
return ducked
@@ -204,6 +238,19 @@ class MacOSScriptDucker:
log.debug("ducking prewarm skip (%s)", name, exc_info=True)
# ---- internals ---------------------------------------------------------
+ def _any_running(self, players: list[tuple[str, str]]) -> bool:
+ """True when any of the given players is currently running."""
+ for name, bundle_id in players:
+ try:
+ proc = self._run(_is_running_script(bundle_id))
+ except Exception: # noqa: BLE001 — probe failure = assume not running
+ log.debug("ducking is-running probe skip (%s)", name, exc_info=True)
+ continue
+ out = (getattr(proc, "stdout", "") or "").strip()
+ if getattr(proc, "returncode", 1) == 0 and out and out != _NOT_RUNNING:
+ return True
+ return False
+
@staticmethod
def _normalized_never(never: frozenset[str]) -> set[str]:
"""Map never-mute entries to player names: case-insensitive, and the
diff --git a/tests/unit/audio/test_ducking_macos.py b/tests/unit/audio/test_ducking_macos.py
index 65d872028b..eb04c343be 100644
--- a/tests/unit/audio/test_ducking_macos.py
+++ b/tests/unit/audio/test_ducking_macos.py
@@ -195,3 +195,65 @@ def test_re_adoption_needs_a_saved_level_not_just_a_quiet_player():
d = _ducker(run)
assert d.mute_others(own_pid=1, never=frozenset()) == []
assert d._saved == {}
+
+
+def test_unrestored_master_is_re_adopted_by_the_next_session():
+ """The stranded-restore shape exists for the MASTER output too.
+
+ A master duck whose restore never landed leaves the WHOLE Mac output at
+ the duck volume with our saved level still in ``_saved``; the next duck
+ reads that already-ducked volume, ``prev > duck`` stays false, and without
+ re-adoption no session ever restores it — the Mac stays quiet for good.
+ """
+ run = FakeRunner({_MUSIC: "-", _SPOTIFY: "-", "master": "80"})
+ d = _ducker(run, master_fallback=True)
+ assert d.mute_others(own_pid=1, never=frozenset()) == [_MASTER_TOKEN]
+
+ # Session 1 ends, but the master restore osascript times out.
+ def _raise(_script: str):
+ raise subprocess.TimeoutExpired(cmd="osascript", timeout=3.0)
+
+ original = d._run
+ d._run = _raise
+ d.restore([_MASTER_TOKEN])
+ d._run = original
+ assert d._saved == {_MASTER_TOKEN: 80}
+
+ # Session 2: the master still reads the duck volume (0). It must come
+ # back as a token so THIS session's restore can finally land.
+ run._results["master"] = "0"
+ assert d.mute_others(own_pid=1, never=frozenset()) == [_MASTER_TOKEN]
+ run._results["master"] = "80"
+ d.restore([_MASTER_TOKEN])
+ assert any("set volume output volume 80" in s for s in run.scripts)
+ assert d._saved == {}
+
+
+def test_master_re_adoption_needs_a_saved_level_not_just_a_quiet_master():
+ """An output volume the USER turned down must never be claimed as ours."""
+ run = FakeRunner({_MUSIC: "-", _SPOTIFY: "-", "master": "0"})
+ d = _ducker(run, master_fallback=True)
+ assert d.mute_others(own_pid=1, never=frozenset()) == []
+ assert d._saved == {}
+
+
+def test_master_fallback_withheld_while_a_never_muted_player_is_running():
+ """never_mute must beat the master fallback.
+
+ The master output carries the protected player's audio too: with Spotify
+ on the never list and running, ducking the master would silence exactly
+ the app the user told us to leave alone.
+ """
+ run = FakeRunner({_MUSIC: "-", _SPOTIFY: "40", "master": "80"})
+ d = _ducker(run, master_fallback=True)
+ assert d.mute_others(own_pid=1, never=frozenset({"Spotify"})) == []
+ assert not any("output volume" in s for s in run.scripts)
+ assert d._saved == {}
+
+
+def test_master_fallback_available_when_the_never_muted_player_is_not_running():
+ """A merely-LISTED player must not disable the fallback for good."""
+ run = FakeRunner({_MUSIC: "-", _SPOTIFY: "-", "master": "80"})
+ d = _ducker(run, master_fallback=True)
+ assert d.mute_others(own_pid=1, never=frozenset({"Spotify"})) == [_MASTER_TOKEN]
+ assert d._saved == {_MASTER_TOKEN: 80}