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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(audio): stop a failed duck restore from silencing an app for good
rubenluetke10-beep4152891c9e73
diff --git a/jarvis/audio/ducking/windows.py b/jarvis/audio/ducking/windows.pyindex b1979464b9..17fa9f2b9d 100644--- a/jarvis/audio/ducking/windows.py+++ b/jarvis/audio/ducking/windows.py@@ -52,8 +52,13 @@ class WindowsPycawDucker:def __init__(self, *, duck_volume_percent: int = 0) -> None:self._duck = max(0, min(100, int(duck_volume_percent)))#: pid -> master volume scalar before we lowered it. Only used in- #: volume mode; mute mode needs no state (unmute is unconditional).+ #: volume mode; mute mode records the pid in ``_muted_by_us`` instead.self._saved: dict[int, float] = {}+ #: pids we hard-muted ourselves (mute mode). A muted session is+ #: otherwise indistinguishable from one the USER silenced — and we must+ #: never turn those back on — so without this record a mute whose+ #: restore did not land could never be retried. See ``mute_others``.+ self._muted_by_us: set[int] = set()@classmethoddef from_config(cls, cfg: Any | None) -> WindowsPycawDucker:@@ -73,15 +78,36 @@ class WindowsPycawDucker:the system-sounds session (PID 0 / no process), and the name allowlist.Returns the PIDs actually ducked so restore() touches only those.- Sessions the user already silenced are left alone in BOTH modes — we- only ever touch something currently audible, so restore can never turn- an app back on that the user had muted.+ Sessions the user already silenced are left alone — we only ever touch+ something currently audible, so restore can never turn an app back on+ that the user had muted.++ A session that is *still ducked from a previous voice turn* is re-adopted+ into this turn's token list. Without that, a restore which never landed+ (Jarvis killed mid-session, a COMError on that one session, a shutdown+ that raced the unmute) stranded the app for good: in volume mode the next+ sweep reads the already-lowered level, ``prev > target`` stays false, so+ the pid is never reported again and the user's music sits at the duck+ volume forever; in mute mode the session simply reads as muted and is+ skipped as "the user's". The macOS backend has carried this re-adoption+ since its own stranded-token bug; this one never got it.++ Note the one imprecision the pid-keyed bookkeeping keeps: a process with+ several concurrent audio sessions has them restored to a single level.+ The returned list is deduplicated so the token count is honest."""from pycaw.pycaw import AudioUtilitiesskip = _normalized_never(never)target = self._duck / 100.0ducked: list[int] = []+ seen: set[int] = set()++ def _claim(pid: int) -> None:+ if pid not in seen:+ seen.add(pid)+ ducked.append(pid)+for session in AudioUtilities.GetAllSessions():try:pid = session.ProcessId@@ -95,17 +121,35 @@ class WindowsPycawDucker:if name in skip:continuevol = session.SimpleAudioVolume- if vol.GetMute(): # already silent — nothing of ours to undo+ if vol.GetMute():+ # Silent. Either the USER muted it — never our business — or+ # we did and the unmute never landed, which only our own+ # record can tell apart.+ if pid in self._muted_by_us:+ log.info(+ "ducking: re-adopting pid %d (still muted by us from "+ "an earlier session whose restore did not land)", pid,+ )+ _claim(pid)continueif self._duck <= 0:vol.SetMute(1, None)- ducked.append(pid)+ self._muted_by_us.add(pid)+ _claim(pid)continueprev = float(vol.GetMasterVolume())if prev > target:vol.SetMasterVolume(target, None)self._saved[pid] = prev- ducked.append(pid)+ _claim(pid)+ elif pid in self._saved:+ # Sitting at (or below) the duck level while we still hold+ # the level it had before — same stranded case as above.+ log.info(+ "ducking: re-adopting pid %d (still at the duck volume "+ "with an unrestored level of %.2f)", pid, self._saved[pid],+ )+ _claim(pid)except Exception: # noqa: BLE001 — COMError on protected sessions; skiplog.debug("ducking mute skip", exc_info=True)return ducked@@ -124,7 +168,9 @@ class WindowsPycawDucker:want = set(pids)if not want:return+ failed: set[int] = set()for session in AudioUtilities.GetAllSessions():+ pid = 0try:pid = session.ProcessIdif pid not in want:@@ -136,8 +182,20 @@ class WindowsPycawDucker:session.SimpleAudioVolume.SetMasterVolume(prev, None)except Exception: # noqa: BLE001log.debug("ducking restore skip", exc_info=True)+ if pid in want:+ failed.add(pid)# Drop the bookkeeping for every requested pid, including sessions that# disappeared while ducked — otherwise a closed app's entry would sit# here forever and a later pid reuse would restore a stranger's volume.- for pid in want:+ # A session that is still THERE but whose undo raised is the exception:+ # keep its record so the next mute_others re-adopts and retries it,+ # because dropping it strands the app at the duck volume with nothing+ # left that knows how to put it back.+ if failed:+ log.warning(+ "ducking: %d session(s) could not be restored — keeping their "+ "recorded level so the next voice turn retries them", len(failed),+ )+ for pid in want - failed:self._saved.pop(pid, None)+ self._muted_by_us.discard(pid)diff --git a/tests/unit/audio/test_ducking_windows.py b/tests/unit/audio/test_ducking_windows.pyindex 6c99efc836..f49ee0e284 100644--- a/tests/unit/audio/test_ducking_windows.py+++ b/tests/unit/audio/test_ducking_windows.py@@ -220,6 +220,74 @@ def test_restore_uses_what_was_recorded_not_the_current_mode(fake_pycaw):assert session.SimpleAudioVolume.master == pytest.approx(0.8)+# --------------------------------------------------------------------------- #+# A restore that never landed must not strand the user's audio #+# --------------------------------------------------------------------------- #+++def test_a_still_ducked_session_is_re_adopted_by_the_next_turn(fake_pycaw):+ """Volume mode: the app sat at the duck level FOREVER after a failed undo.++ The next sweep read the already-lowered volume, ``prev > target`` stayed+ false, so the pid was never reported again — and restore() is only ever+ asked about pids that mute_others returned.+ """+ session = _FakeSession(4321, "spotify.exe", master=0.8)+ fake_pycaw.extend([session])+ ducker = WindowsPycawDucker(duck_volume_percent=20)++ ducker.mute_others(own_pid=1, never=frozenset()) # 0.8 -> 0.2+ # ...and the restore never happens (Jarvis killed, COMError, shutdown race).++ second = ducker.mute_others(own_pid=1, never=frozenset())+ assert second == [4321]++ ducker.restore(second)+ assert session.SimpleAudioVolume.master == pytest.approx(0.8)+ assert ducker._saved == {}+++def test_a_session_we_muted_is_re_adopted_but_a_user_muted_one_is_not(fake_pycaw):+ """Mute mode had the same hole, and the harsher symptom: silence for good."""+ ours = _FakeSession(4321, "spotify.exe")+ theirs = _FakeSession(4322, "chrome.exe", muted=True)+ fake_pycaw.extend([ours, theirs])+ ducker = WindowsPycawDucker()++ assert ducker.mute_others(own_pid=1, never=frozenset()) == [4321]+ # No restore happens; both sessions now read back as muted.++ assert ducker.mute_others(own_pid=1, never=frozenset()) == [4321]++ ducker.restore([4321])+ assert ours.SimpleAudioVolume.muted is False+ assert theirs.SimpleAudioVolume.muted is True # never ours to touch+++def test_restore_forgets_a_session_it_successfully_undid(fake_pycaw):+ """The re-adoption record must not survive a restore that DID land —+ otherwise a later pid reuse would unmute a stranger."""+ session = _FakeSession(4321, "spotify.exe")+ fake_pycaw.extend([session])+ ducker = WindowsPycawDucker()++ ducker.restore(ducker.mute_others(own_pid=1, never=frozenset()))++ assert ducker._muted_by_us == set()+ assert ducker.mute_others(own_pid=1, never=frozenset()) == [4321]+++def test_one_pid_with_several_sessions_yields_one_token(fake_pycaw):+ """A process can own more than one audio session; reporting the pid once+ per session inflated the "muted N sessions" count the controller logs."""+ fake_pycaw.extend([+ _FakeSession(4321, "chrome.exe"),+ _FakeSession(4321, "chrome.exe"),+ ])++ assert WindowsPycawDucker().mute_others(own_pid=1, never=frozenset()) == [4321]++# --------------------------------------------------------------------------- ## Config wiring ## --------------------------------------------------------------------------- #