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(society): keep a question card open past a CLI's tool-call timeout
rubenluetke10-beep31b68d41fd06
diff --git a/jarvis/agent_chat/service.py b/jarvis/agent_chat/service.pyindex 6661de5fe4..045cc7ac35 100644--- a/jarvis/agent_chat/service.py+++ b/jarvis/agent_chat/service.py@@ -162,11 +162,22 @@ def stop_cli_at_cwd(cwd: str) -> int:class _OpenQuestion:- """A card waiting on the person: one answer slot per question in the series."""+ """A card waiting on the person: one answer slot per question in the series.- __slots__ = ("answers", "closing", "session_id", "specs", "turn_id", "wake")+ It lives independently of the tool call that opened it: a CLI drops an+ MCP call after a minute or so, while the card may stay open for five.+ ``result`` resolves once the card closes; callers wait on it in slices.+ """++ __slots__ = ("answers", "closing", "result", "session_id", "specs", "turn_id", "wake")- def __init__(self, session_id: str, turn_id: str, specs: tuple[QuestionSpec, ...]) -> None:+ def __init__(+ self,+ session_id: str,+ turn_id: str,+ specs: tuple[QuestionSpec, ...],+ result: asyncio.Future[list[QuestionAnswer]],+ ) -> None:self.session_id = session_idself.turn_id = turn_idself.specs = specs@@ -174,12 +185,17 @@ class _OpenQuestion:#: Set by a skip or a cancel: the source every open slot resolves to.self.closing = ""self.wake = asyncio.Event()+ self.result = result@propertydef done(self) -> bool:return bool(self.closing) or all(a is not None for a in self.answers)+def _expires_ms(timeout_s: float) -> int:+ return int(time.time() * 1000 + timeout_s * 1000)++class _Running:__slots__ = ("asks", "task", "cancel", "turn_id")@@ -222,6 +238,7 @@ class AgentChatService:self._approval_session: dict[str, str] = {}# Questions an agent is waiting on (questions.py), by question id.self._questions: dict[str, _OpenQuestion] = {}+ self._question_tasks: set[asyncio.Task[None]] = set()# "Always allow" on the Jarvis surface: the tools a person waved through# for the rest of the session, per session. Claude Code's "don't ask# again for this tool" rather than a mode flip — the unified ladder has@@ -1053,22 +1070,22 @@ class AgentChatService:# ------------------------------------------------------------ questions- async def ask_questions(+ async def open_questions(self,session_id: str,specs: tuple[QuestionSpec, ...],*,asker: str = "",timeout_s: float = QUESTION_TIMEOUT_S,- ) -> list[QuestionAnswer]:- """Show ``specs`` as one card in the running turn and wait for the answers.+ ) -> str:+ """Show ``specs`` as one card in the running turn; returns its question id.- The person answers the series one question at a time. Whenever- ``timeout_s`` passes without a new answer, every open question takes- its recommendation (index 0); each answer restarts that window, so a- workflow behind this turn keeps moving but a person mid-series is- never cut off. Closing the card applies the recommendations at once;- a cancelled or finished turn answers ``cancelled``.+ The card watches itself: whenever ``timeout_s`` passes without a new+ answer, every open question takes its recommendation (index 0). Each+ answer restarts that window, so a workflow behind this turn keeps+ moving but a person mid-series is never cut off. Closing the card+ applies the recommendations at once; a finished turn cancels it. Wait+ for the outcome with :meth:`wait_questions`.Raises ``RuntimeError`` when the session has no running turn, and``TooManyQuestions`` once the turn has used its cards.@@ -1080,13 +1097,9 @@ class AgentChatService:raise TooManyQuestions(f"this turn already asked {run.asks} times")run.asks += 1question_id = uuid.uuid4().hex- open_q = _OpenQuestion(session_id, run.turn_id, specs)- self._questions[question_id] = open_qloop = asyncio.get_running_loop()-- def expires_ms() -> int:- return int(time.time() * 1000 + timeout_s * 1000)-+ open_q = _OpenQuestion(session_id, run.turn_id, specs, loop.create_future())+ self._questions[question_id] = open_qawait self._emit(session_id,make_event(@@ -1097,10 +1110,58 @@ class AgentChatService:"asker": asker,"questions": [spec.to_payload() for spec in specs],"timeout_s": timeout_s,- "expires_ms": expires_ms(),+ "expires_ms": _expires_ms(timeout_s),},),)+ task = asyncio.create_task(+ self._watch_question(open_q, question_id, timeout_s),+ name=f"agent-chat-question-{question_id[:8]}",+ )+ self._question_tasks.add(task)+ task.add_done_callback(self._question_tasks.discard)+ return question_id++ async def wait_questions(+ self, session_id: str, question_id: str, timeout_s: float | None = None+ ) -> list[QuestionAnswer] | None:+ """The card's answers once it closed; ``None`` while it is still open after ``timeout_s``.++ Raises ``KeyError`` for an unknown or foreign question id.+ """+ open_q = self._questions.get(question_id)+ if open_q is None or open_q.session_id != session_id:+ raise KeyError(question_id)+ try:+ return await asyncio.wait_for(asyncio.shield(open_q.result), timeout_s)+ except TimeoutError: # still open: the caller polls again+ return None++ def question_specs(self, session_id: str, question_id: str) -> tuple[QuestionSpec, ...]:+ """The questions on card ``question_id``; raises ``KeyError`` when unknown."""+ open_q = self._questions.get(question_id)+ if open_q is None or open_q.session_id != session_id:+ raise KeyError(question_id)+ return open_q.specs++ async def ask_questions(+ self,+ session_id: str,+ specs: tuple[QuestionSpec, ...],+ *,+ asker: str = "",+ timeout_s: float = QUESTION_TIMEOUT_S,+ ) -> list[QuestionAnswer]:+ """Open a card and wait until it closes (a caller with no call timeout)."""+ question_id = await self.open_questions(session_id, specs, asker=asker, timeout_s=timeout_s)+ answers = await self.wait_questions(session_id, question_id)+ assert answers is not None+ return answers++ async def _watch_question(+ self, open_q: _OpenQuestion, question_id: str, timeout_s: float+ ) -> None:+ loop = asyncio.get_running_loop()reported = 0deadline = loop.time() + timeout_stry:@@ -1110,7 +1171,7 @@ class AgentChatService:log.info("agent chat: question %s in %s timed out; recommendations applied",question_id,- session_id,+ open_q.session_id,)open_q.closing = TIMEOUTbreak@@ -1124,29 +1185,42 @@ class AgentChatService:reported = answereddeadline = loop.time() + timeout_sawait self._emit(- session_id,+ open_q.session_id,make_event("question_progress",{- "turn_id": run.turn_id,+ "turn_id": open_q.turn_id,"question_id": question_id,"answers": [a.to_payload() if a else None for a in open_q.answers],- "expires_ms": expires_ms(),+ "expires_ms": _expires_ms(timeout_s),},),)except asyncio.CancelledError:- # The caller went away (a cancelled tool call, a CLI that gave up- # on the MCP request): close the card rather than leave it open.+ # App shutdown: the card closes with the process.open_q.closing = CANCELLED- self._questions.pop(question_id, None)- await self._emit_resolved(open_q, question_id)raisefinally:- self._questions.pop(question_id, None)- return await self._emit_resolved(open_q, question_id)+ answers = self._final_answers(open_q)+ if not open_q.result.done():+ open_q.result.set_result(answers)+ try:+ await self._emit(+ open_q.session_id,+ make_event(+ "question_resolved",+ {+ "turn_id": open_q.turn_id,+ "question_id": question_id,+ "answers": [a.to_payload() for a in answers],+ },+ ),+ )+ except Exception: # noqa: BLE001 — the outcome is already delivered to the waiter+ log.warning("agent chat: could not record question %s", question_id, exc_info=True)- async def _emit_resolved(self, open_q: _OpenQuestion, question_id: str) -> list[QuestionAnswer]:+ @staticmethod+ def _final_answers(open_q: _OpenQuestion) -> list[QuestionAnswer]:answers: list[QuestionAnswer] = []for spec, answer in zip(open_q.specs, open_q.answers, strict=True):if answer is None:@@ -1157,17 +1231,6 @@ class AgentChatService:else recommended_answer(spec, source))answers.append(answer)- await self._emit(- open_q.session_id,- make_event(- "question_resolved",- {- "turn_id": open_q.turn_id,- "question_id": question_id,- "answers": [a.to_payload() for a in answers],- },- ),- )return answersdef resolve_question(@@ -1205,15 +1268,20 @@ class AgentChatService:return Truedef pending_questions(self, session_id: str) -> list[str]:- return [qid for qid, q in self._questions.items() if q.session_id == session_id]+ return [+ qid for qid, q in self._questions.items() if q.session_id == session_id and not q.done+ ]def _cancel_questions(self, session_id: str) -> None:# Tolerates a service built without __init__ (test doubles), like _controls.questions: dict[str, _OpenQuestion] = getattr(self, "_questions", {})- for open_q in list(questions.values()):- if open_q.session_id == session_id and not open_q.done:+ for qid, open_q in list(questions.items()):+ if open_q.session_id != session_id:+ continue+ if not open_q.done:open_q.closing = CANCELLEDopen_q.wake.set()+ questions.pop(qid, None)__all__ = [diff --git a/jarvis/society/ask_tool.py b/jarvis/society/ask_tool.pyindex 05f147e177..13baffb8df 100644--- a/jarvis/society/ask_tool.py+++ b/jarvis/society/ask_tool.py@@ -46,6 +46,11 @@ __all__ = ["ASK_USER_TOOL_NAME", "AskUserTool"]ASK_USER_TOOL_NAME: Final[str] = "society_ask_user"_MINUTES: Final[int] = int(QUESTION_TIMEOUT_S // 60)+#: How long one call waits for the card. CLI seats drop an MCP call after+#: about a minute (Claude Code: 60 s), so the tool hands back a "still+#: waiting" result in time and the agent polls with ``wait_for``.+WAIT_SLICE_S: Final[float] = 45.0+_QUESTION_SCHEMA: Final[dict[str, Any]] = {"type": "object","properties": {@@ -98,7 +103,9 @@ class AskUserTool:f"{_MINUTES} minutes, your recommendations are applied, so recommend the safest ""sensible choice. Never ask for permission to do what you were asked, never ask ""what you could look up, and never ask again what was already answered. "- f"A turn can show at most {MAX_ASKS_PER_TURN} question cards."+ f"A turn can show at most {MAX_ASKS_PER_TURN} question cards. While the user has "+ "not answered yet, the call returns status 'waiting' with a question_id: then call "+ "this tool again with only wait_for set to that id, and do nothing else meanwhile.")schema: dict[str, Any] = {"type": "object",@@ -110,8 +117,14 @@ class AskUserTool:"items": _QUESTION_SCHEMA,"description": "The questions, most important first. Usually just one.",},+ "wait_for": {+ "type": "string",+ "description": (+ "Only to keep waiting on a card you already opened: the question_id from "+ "a 'waiting' result. Send it alone, without questions."+ ),+ },},- "required": ["questions"],}def __init__(self, runtime: Any, agent_id: str, *, session_id: str) -> None:@@ -120,6 +133,9 @@ class AskUserTool:self._session_id = session_idasync def execute(self, args: dict[str, Any], ctx: Any) -> ToolResult:+ wait_for = str(args.get("wait_for") or "").strip()+ if wait_for:+ return await self._keep_waiting(wait_for)try:specs = parse_questions(args)except ValueError as exc:@@ -131,10 +147,10 @@ class AskUserTool:"applied; continue with them and mention the assumptions in your result.",)service = self._runtime.chat_service()- if service is None or not callable(getattr(service, "ask_questions", None)):+ if service is None or not callable(getattr(service, "open_questions", None)):return _unattended(specs, _NOBODY)try:- answers = await service.ask_questions(self._session_id, specs, asker=self._asker())+ question_id = await service.open_questions(self._session_id, specs, asker=self._asker())except TooManyQuestions:return _unattended(specs,@@ -147,6 +163,42 @@ class AskUserTool:# can answer, so the recommendations stand rather than a stall.log.info("society ask: %s cannot ask here (%s)", self._agent_id, exc)return _unattended(specs, _NOBODY)+ return await self._wait(service, question_id, specs)++ async def _keep_waiting(self, question_id: str) -> ToolResult:+ service = self._runtime.chat_service()+ try:+ specs = service.question_specs(self._session_id, question_id)+ except (AttributeError, KeyError):+ return ToolResult(+ success=False,+ output=None,+ error=(+ "No open question card with that id in this chat (it closed with the "+ "turn). Continue with your recommendations; do not ask again."+ ),+ )+ return await self._wait(service, question_id, specs)++ async def _wait(+ self, service: Any, question_id: str, specs: tuple[QuestionSpec, ...]+ ) -> ToolResult:+ answers = await service.wait_questions(self._session_id, question_id, WAIT_SLICE_S)+ if answers is None:+ return ToolResult(+ success=True,+ output={+ "status": "waiting",+ "question_id": question_id,+ "note": (+ "The user has not finished answering yet; the card stays open and "+ f"applies your recommendations on its own after {_MINUTES} quiet "+ "minutes. Call society_ask_user again now with only "+ f'{{"wait_for": "{question_id}"}} to keep waiting. Do nothing else '+ "meanwhile and do not ask anything new."+ ),+ },+ )return _result(specs, answers)def _asker(self) -> str:diff --git a/jarvis/ui/web/frontend/src/components/agentchat/QuestionCard.test.tsx b/jarvis/ui/web/frontend/src/components/agentchat/QuestionCard.test.tsxindex 0021814fe8..7cc33a0781 100644--- a/jarvis/ui/web/frontend/src/components/agentchat/QuestionCard.test.tsx+++ b/jarvis/ui/web/frontend/src/components/agentchat/QuestionCard.test.tsx@@ -136,6 +136,16 @@ describe("QuestionCard", () => {expect(answer).toHaveBeenCalledWith("q1", 1, { optionIndex: 1 });});+ it("draws no row for the calls that only wait on the card", () => {+ draw(asking([+ ev("tool_result", { turn_id: "t1", call_id: "c1", output: "waiting" }),+ ev("tool_call", { turn_id: "t1", call_id: "c2", name: "mcp__jarvis__society_ask_user", input: { wait_for: "q1" } }),+ ev("tool_result", { turn_id: "t1", call_id: "c2", output: "waiting" }),+ ]));+ expect(screen.getAllByTestId("question-card")).toHaveLength(1);+ expect(document.querySelector("[data-trace-tool]")).toBeNull();+ });+it("sends a typed answer", async () => {const { answer } = draw(asking([], [DB]));expect(screen.queryByTestId("question-step")).toBeNull();diff --git a/jarvis/ui/web/frontend/src/components/agentchat/WorkTrace.tsx b/jarvis/ui/web/frontend/src/components/agentchat/WorkTrace.tsxindex 08c95df418..991c3772c2 100644--- a/jarvis/ui/web/frontend/src/components/agentchat/WorkTrace.tsx+++ b/jarvis/ui/web/frontend/src/components/agentchat/WorkTrace.tsx@@ -5,7 +5,7 @@ import remarkGfm from "remark-gfm";import { useT } from "@/i18n";import { cn } from "@/lib/utils";import type { ApprovalDecision } from "@/lib/agentChatApi";-import type { ReasoningBlock, TextBlock, ToolBlock, TurnBlock, TurnItem, TurnStatus } from "./reduce";+import { isQuestionTool, type ReasoningBlock, type TextBlock, type ToolBlock, type TurnBlock, type TurnItem, type TurnStatus } from "./reduce";import { ChatMarkdown } from "./ChatMarkdown";import { QuestionCard } from "./QuestionCard";import { toolDiff } from "./toolDiff";@@ -430,12 +430,23 @@ function TraceGroups({ groups, live, status, onDecide, renderText, conversation});}-export function WorkTrace({ blocks, status, startedMs, durationMs, error, onDecide, renderText, className, receipt, completionLabel, conversation = false }: {+/**+ * An agent's question shows as its card; the tool calls that only wait on+ * that card (``wait_for`` polls, jarvis/society/ask_tool.py) are plumbing+ * and draw nothing.+ */+function withoutQuestionPolls(blocks: TurnBlock[]): TurnBlock[] {+ const kept = blocks.filter((block) => !(block.kind === "tool" && isQuestionTool(block.name) && !block.question));+ return kept.length === blocks.length ? blocks : kept;+}++export function WorkTrace({ blocks: rawBlocks, status, startedMs, durationMs, error, onDecide, renderText, className, receipt, completionLabel, conversation = false }: {blocks: TurnBlock[]; status: TurnStatus; startedMs: number; durationMs: number | null; error?: string | null;onDecide?: Decide; renderText?: (text: string, id: string) => ReactNode; className?: string;receipt?: ReactNode; completionLabel?: string; conversation?: boolean;}) {const t = useT();+ const blocks = useMemo(() => withoutQuestionPolls(rawBlocks), [rawBlocks]);const live = status === "running";const elapsed = useClock(startedMs, live);const split = useMemo(() => conversation && !live ? splitConversationTurn(blocks) : null, [blocks, conversation, live]);diff --git a/tests/unit/agent_chat/test_questions.py b/tests/unit/agent_chat/test_questions.pyindex 6a32a3ddad..dadb9452cd 100644--- a/tests/unit/agent_chat/test_questions.py+++ b/tests/unit/agent_chat/test_questions.py@@ -186,15 +186,29 @@ def test_asking_without_a_running_turn_raises(tmp_path: Path) -> None:class _FakeService:- def __init__(self, *, limit: bool = False) -> None:+ """Answers option B on every question, after ``polls`` still-open slices."""++ def __init__(self, *, limit: bool = False, polls: int = 0) -> None:self.asked: list[tuple[str, str]] = []self.limit = limit+ self.polls = polls+ self.specs: dict[str, Any] = {}- async def ask_questions(self, session_id: str, specs: Any, *, asker: str = "") -> Any:+ async def open_questions(self, session_id: str, specs: Any, *, asker: str = "") -> str:if self.limit:raise TooManyQuestions("used up")self.asked.append((session_id, asker))- return [answer_from(spec, option_index=1, text=None) for spec in specs]+ self.specs["q1"] = specs+ return "q1"++ def question_specs(self, session_id: str, question_id: str) -> Any:+ return self.specs[question_id]++ async def wait_questions(self, session_id: str, question_id: str, timeout_s: Any) -> Any:+ if self.polls:+ self.polls -= 1+ return None+ return [answer_from(spec, option_index=1, text=None) for spec in self.specs[question_id]]def _tool(session_id: str, service: _FakeService) -> AskUserTool:@@ -213,6 +227,33 @@ def test_tool_returns_the_persons_answers() -> None:assert service.asked == [("society:ada", "Ada")]+def test_a_slow_answer_is_polled_in_slices() -> None:+ service = _FakeService(polls=1)+ tool = _tool("society:ada", service)+ first = asyncio.run(tool.execute(dict(_ARGS), None))+ assert first.success and first.output["status"] == "waiting"+ assert first.output["question_id"] == "q1" and "wait_for" in first.output["note"]+ second = asyncio.run(tool.execute({"wait_for": "q1"}, None))+ assert [r["answer"] for r in second.output["answers"]] == ["Postgres", "VPS"]+ assert service.asked == [("society:ada", "Ada")] # polling never opens a new card+ gone = asyncio.run(tool.execute({"wait_for": "nope"}, None))+ assert not gone.success+++def test_the_card_outlives_a_dropped_call(tmp_path: Path) -> None:+ async def scenario() -> None:+ svc, sid, task = await _service_with_turn(tmp_path)+ qid = await svc.open_questions(sid, parse_questions(_DB))+ assert await svc.wait_questions(sid, qid, 0.01) is None # the slice ends, card stays+ assert svc.pending_questions(sid) == [qid]+ assert svc.resolve_question(sid, qid, index=0, option_index=1)+ answers = await svc.wait_questions(sid, qid, 5)+ assert answers is not None and answers[0].answer == "Postgres"+ task.cancel()++ asyncio.run(scenario())++def test_a_routine_never_waits_for_the_user() -> None:service = _FakeService()tool = _tool("society:ada:routine:t1:run1", service)