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(voice): keep an answer the call already said from being announced again (#687)

Ruben Lütke96fc4f2fea35
diff --git a/jarvis/plugins/tool/ask_agent.py b/jarvis/plugins/tool/ask_agent.py
index 7b17330b1a..3a876133f1 100644
--- a/jarvis/plugins/tool/ask_agent.py
+++ b/jarvis/plugins/tool/ask_agent.py
@@ -130,15 +130,16 @@ class AskAgentTool:
while reply is None and _now() < deadline:
await asyncio.sleep(_POLL_S)
reply = await result_for(runtime.store, request)
+ if reply is None:
+ # An answer that landed while the wait ended was not announced:
+ # read once more before calling it pending.
+ reply = await result_for(runtime.store, request)
finally:
# Also after a failed or uncertain send: a reply that still comes
- # is then announced like any other.
+ # is then announced like any other. An answer read here is said by
+ # this call, so its delivery stays quiet even if it runs later.
for request_id in marked:
- runtime.stop_awaiting_inline(request_id)
- if reply is None:
- # An answer that landed while the wait ended was not announced:
- # read once more before calling it pending.
- reply = await result_for(runtime.store, request)
+ runtime.stop_awaiting_inline(request_id, answered=reply is not None)
if reply is None:
return ToolResult(
success=True,
diff --git a/jarvis/society/runtime.py b/jarvis/society/runtime.py
index ae0798c011..c0631e62ea 100644
--- a/jarvis/society/runtime.py
+++ b/jarvis/society/runtime.py
@@ -71,6 +71,8 @@ class _UsablePlugins:
#: and says that reply itself (``jarvis.plugins.tool.ask_agent``).
#: The value is an empty list that receives the id of the ONE question it
#: marks; a task that inherits the context finds the list already used.
+#: How many answers said inside a call are remembered until their delivery runs.
+_ANSWERED_INLINE_MAX: Final[int] = 256
reply_awaited_inline: ContextVar[list[str] | None] = ContextVar(
"reply_awaited_inline", default=None
)
@@ -197,6 +199,10 @@ class SocietyRuntime:
#: Requests whose reply a caller is waiting for and will speak itself;
#: their answer is not announced a second time (``reply_awaited_inline``).
self._awaited_inline: set[str] = set()
+ #: Requests whose answer a caller already received and said. The runtime
+ #: may deliver that answer only after the caller stopped waiting; this
+ #: keeps that late delivery quiet (in insertion order, bounded).
+ self._answered_inline: dict[str, None] = {}
#: The Quest Board: the person's jobs, routed to one taker, read back off the board.
self.quests = Quests(self)
from .team_lead import TeamLeads
@@ -409,6 +415,10 @@ class SocietyRuntime:
) -> None:
from jarvis.core.delegation import result_announcement
+ # Read before the first await: the asker may stop waiting meanwhile.
+ said_inline = request.event_id in self._awaited_inline or (
+ self._answered_inline.pop(request.event_id, False) is None
+ )
event = result_announcement(
source="society.lead", request_id=request.event_id,
name=self.display_name(target),
@@ -435,9 +445,9 @@ class SocietyRuntime:
})
except Exception: # A chat failure must not suppress the voice result.
log.warning("society: result notice delivery failed", exc_info=True)
- if request.event_id in self._awaited_inline:
- # The asker is waiting on this very answer and says it itself; the
- # chat notice above keeps the durable copy.
+ if said_inline or request.event_id in self._awaited_inline:
+ # The asker is waiting on this very answer, or already has it, and
+ # says it itself; the chat notice above keeps the durable copy.
return
surface = request.payload.get("reply_surface")
if surface == "voice" or (surface is None and request.trace_id.startswith("voice:")):
@@ -449,9 +459,18 @@ class SocietyRuntime:
except Exception: # The durable board and chat still retain the report.
log.warning("society: result announcement failed", exc_info=True)
- def stop_awaiting_inline(self, request_id: str) -> None:
- """The caller stopped waiting: a later reply is announced as usual."""
+ def stop_awaiting_inline(self, request_id: str, *, answered: bool = False) -> None:
+ """The caller stopped waiting.
+
+ ``answered``: it has the answer and said it, so that answer's delivery
+ stays quiet even when it runs later. Otherwise a later reply is
+ announced as usual.
+ """
self._awaited_inline.discard(request_id)
+ if answered:
+ self._answered_inline[request_id] = None
+ while len(self._answered_inline) > _ANSWERED_INLINE_MAX:
+ self._answered_inline.pop(next(iter(self._answered_inline)))
async def _deliver_pending(self) -> None:
"""Recover committed messages and retry busy chats without opening sockets."""
diff --git a/tests/unit/plugins/tool/test_ask_agent.py b/tests/unit/plugins/tool/test_ask_agent.py
index 4d11889748..619890814d 100644
--- a/tests/unit/plugins/tool/test_ask_agent.py
+++ b/tests/unit/plugins/tool/test_ask_agent.py
@@ -79,6 +79,75 @@ async def test_the_answer_comes_back_in_the_same_call_and_is_not_announced_twice
assert not [e for e in spoken if isinstance(e, AnnouncementRequested)]
+async def test_an_answer_the_call_already_said_is_not_announced_after_it(team):
+ # Seen on macOS: the call read the stored answer and lifted its mark before
+ # the runtime delivered that answer, so the voice heard it a second time.
+ rt, spoken = team
+ scout = await rt.roster.resolve("Scout")
+ request = await rt.say(
+ from_agent=rt.lead_id, to_agent=scout.agent_id, text="Which VPS?",
+ msg_type=MsgType.QUERY, payload={"reply_surface": "voice"},
+ )
+ rt._awaited_inline.add(request.event_id)
+ rt.stop_awaiting_inline(request.event_id, answered=True)
+ assert rt._awaited_inline == set()
+
+ async def announced() -> list:
+ await asyncio.sleep(0.05)
+ return [e for e in spoken if isinstance(e, AnnouncementRequested)]
+
+ await rt._deliver_lead_result(scout, request, status="done", summary="Hetzner.",
+ kind="society_message")
+ assert await announced() == []
+ # Used up by that delivery: a later result for the same request is spoken.
+ assert rt._answered_inline == {}
+ await rt._deliver_lead_result(scout, request, status="done", summary="Changed my mind.",
+ kind="society_message")
+ assert len(await announced()) == 1
+
+
+async def test_a_wait_that_ran_out_leaves_the_answer_to_be_announced(team):
+ rt, _spoken = team
+ scout = await rt.roster.resolve("Scout")
+ request = await rt.say(
+ from_agent=rt.lead_id, to_agent=scout.agent_id, text="Status?", msg_type=MsgType.QUERY
+ )
+ rt._awaited_inline.add(request.event_id)
+ rt.stop_awaiting_inline(request.event_id)
+ assert rt._awaited_inline == set() and rt._answered_inline == {}
+
+
+def test_the_record_of_answers_said_in_a_call_stays_bounded(tmp_path):
+ from jarvis.society import runtime as society_runtime
+
+ rt = SocietyRuntime(tmp_path, chat_service=lambda: None, cfg=lambda: None)
+ for index in range(society_runtime._ANSWERED_INLINE_MAX + 50):
+ rt.stop_awaiting_inline(f"request-{index}", answered=True)
+ assert len(rt._answered_inline) == society_runtime._ANSWERED_INLINE_MAX
+ assert "request-0" not in rt._answered_inline
+
+
+async def test_the_tool_tells_the_runtime_whether_it_has_the_answer(team, monkeypatch):
+ rt, _spoken = team
+ lifted: list[bool] = []
+ lift = rt.stop_awaiting_inline
+
+ def record(request_id: str, *, answered: bool = False) -> None:
+ lifted.append(answered)
+ lift(request_id, answered=answered)
+
+ monkeypatch.setattr(rt, "stop_awaiting_inline", record)
+ tool = AskAgentTool(runtime_resolver=lambda: rt)
+ asking = asyncio.create_task(tool.execute({"agent": "Scout", "question": "Ready?"}, _ctx()))
+ await _answer(rt, "Yes.")
+ assert (await asyncio.wait_for(asking, 10)).output["answered"] is True
+ clock = iter([0.0, 0.0])
+ monkeypatch.setattr(ask_agent, "_now", lambda: next(clock, 1000.0))
+ late = await tool.execute({"agent": "Scout", "question": "And now?"}, _ctx())
+ assert late.output["answered"] is False
+ assert lifted == [True, False]
+
+
async def test_a_late_answer_is_handed_to_the_conversation_instead(team, monkeypatch):
rt, spoken = team
# The wait runs out at once: only this tool's clock jumps, not the loop's.