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): keep a failed macOS ducking restore re-adoptable

rubenluetke10-beep85e9f1888f5a
diff --git a/jarvis/audio/ducking/macos.py b/jarvis/audio/ducking/macos.py
index 24c28b53b7..cf83d549ec 100644
--- a/jarvis/audio/ducking/macos.py
+++ b/jarvis/audio/ducking/macos.py
@@ -219,10 +219,22 @@ class MacOSScriptDucker:
continue
try:
if token == _MASTER_TOKEN:
- self._run(f"set volume output volume {prev}")
+ proc = self._run(f"set volume output volume {prev}")
else:
_name, bundle_id = _PLAYERS[token]
- self._run(_restore_script(bundle_id, prev))
+ proc = self._run(_restore_script(bundle_id, prev))
+ if getattr(proc, "returncode", 1) != 0:
+ # rc != 0 (e.g. a dismissed Automation prompt, -1743) is a
+ # restore that never landed. The saved level is the very
+ # thing the next session's re-adoption needs — popping it
+ # here stranded the duck for good.
+ log.debug(
+ "ducking restore did not land (token=%s, rc=%s): %s",
+ token,
+ getattr(proc, "returncode", None),
+ (getattr(proc, "stderr", "") or "").strip(),
+ )
+ continue
self._saved.pop(token, None)
except Exception: # noqa: BLE001
log.debug("ducking restore skip (token=%s)", token, exc_info=True)
diff --git a/jarvis/terminal/backend.py b/jarvis/terminal/backend.py
index b0ba03f256..49a8c712c9 100644
--- a/jarvis/terminal/backend.py
+++ b/jarvis/terminal/backend.py
@@ -205,6 +205,15 @@ class _UnixPtyHandle:
except Exception: # noqa: BLE001 - already-reaped/ECHILD reads as unknown
return None
status = getattr(self._proc, "exitstatus", None)
+ if status is None:
+ # A child killed by a signal never gets an ``exitstatus`` from
+ # ptyprocess — the reap records the signal in ``signalstatus``
+ # instead. Follow the ``subprocess`` convention (negated signal
+ # number) so a SIGKILLed agent reads as ``-9`` rather than the
+ # unknown-exit ``-1`` nothing above can name.
+ sig = getattr(self._proc, "signalstatus", None)
+ if sig is not None:
+ return -int(sig)
return None if status is None else int(status)
def write(self, data: str) -> None:
diff --git a/tests/unit/audio/test_ducking_macos.py b/tests/unit/audio/test_ducking_macos.py
index eb04c343be..a5e9b358bf 100644
--- a/tests/unit/audio/test_ducking_macos.py
+++ b/tests/unit/audio/test_ducking_macos.py
@@ -257,3 +257,38 @@ def test_master_fallback_available_when_the_never_muted_player_is_not_running():
d = _ducker(run, master_fallback=True)
assert d.mute_others(own_pid=1, never=frozenset({"Spotify"})) == [_MASTER_TOKEN]
assert d._saved == {_MASTER_TOKEN: 80}
+
+
+def test_denied_restore_keeps_the_saved_level_for_re_adoption():
+ """rc != 0 (a dismissed Automation prompt, -1743) is a restore that never
+ landed — popping the saved level there stranded the duck for good, because
+ re-adoption keys on exactly that saved level."""
+ run = FakeRunner({_MUSIC: "65", _SPOTIFY: "-"})
+ d = _ducker(run)
+ assert d.mute_others(own_pid=1, never=frozenset()) == [1]
+
+ run._results[_MUSIC] = subprocess.CompletedProcess(
+ ["osascript"], 1, stdout="", stderr="Not authorized to send Apple events (-1743)"
+ )
+ d.restore([1])
+ assert d._saved == {1: 65}
+
+ # The user granted the prompt: the next session re-adopts and restores.
+ run._results[_MUSIC] = "0"
+ assert d.mute_others(own_pid=1, never=frozenset()) == [1]
+ d.restore([1])
+ assert any("set sound volume to 65" in s for s in run.scripts)
+ assert d._saved == {}
+
+
+def test_denied_master_restore_keeps_the_saved_level():
+ """The same rc != 0 shape must keep the MASTER level re-adoptable."""
+ run = FakeRunner({_MUSIC: "-", _SPOTIFY: "-", "master": "80"})
+ d = _ducker(run, master_fallback=True)
+ assert d.mute_others(own_pid=1, never=frozenset()) == [_MASTER_TOKEN]
+
+ run._results["master"] = subprocess.CompletedProcess(
+ ["osascript"], 1, stdout="", stderr="execution error (-1743)"
+ )
+ d.restore([_MASTER_TOKEN])
+ assert d._saved == {_MASTER_TOKEN: 80}
diff --git a/tests/unit/terminal/test_unix_pty.py b/tests/unit/terminal/test_unix_pty.py
index 1f3a1eaf4a..186e4cfbbe 100644
--- a/tests/unit/terminal/test_unix_pty.py
+++ b/tests/unit/terminal/test_unix_pty.py
@@ -254,6 +254,26 @@ def test_unix_handle_exitstatus_is_none_while_child_lives():
assert _UnixPtyHandle(_LiveProc()).exitstatus is None
+def test_unix_handle_reports_a_signal_death_as_negative_signal_number():
+ """ptyprocess never fills ``exitstatus`` for a signal-killed child.
+
+ The reap records the signal in ``signalstatus`` instead, so without this
+ mapping a SIGKILLed agent read as the unknown-exit ``-1``. Follow the
+ ``subprocess`` convention: killed by signal N reports as ``-N``.
+ """
+ from jarvis.terminal.backend import _UnixPtyHandle
+
+ class _SignalKilledProc:
+ pid = 10
+ exitstatus = None
+ signalstatus = 9 # SIGKILL
+
+ def isalive(self) -> bool:
+ return False
+
+ assert _UnixPtyHandle(_SignalKilledProc()).exitstatus == -9
+
+
def test_unix_handle_exitstatus_survives_an_already_reaped_child():
"""ECHILD inside isalive() (ptyprocess raises) must read as unknown, not throw."""
from jarvis.terminal.backend import _UnixPtyHandle