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(dictation): stop the live preview from spending the transcript's budget

Ruben Lütke2f7230db97ad
diff --git a/jarvis/dictation/preview_budget.py b/jarvis/dictation/preview_budget.py
new file mode 100644
index 0000000000..b4bde849e0
--- /dev/null
+++ b/jarvis/dictation/preview_budget.py
@@ -0,0 +1,101 @@
+"""Request budget for the dictation LIVE PREVIEW.
+
+Measured on the maintainer's box (2026-07-29, from ``jarvis_desktop.log``): a
+dictation spends roughly **40 provider requests per minute of speech**. Only
+about 7.5 of those — one per closed segment — produce any of the final text.
+The rest are live-preview calls: they re-transcribe the open tail every
+``partial_interval_s`` so words appear while you are still speaking, and then
+their result is thrown away and rebuilt on the next tick.
+
+That is 85 % of the traffic buying a cosmetic feature, and it is what pushed a
+137 s dictation past the provider's per-minute limit: five refused calls, and
+367 characters returned for over two minutes of speech. The user's actual words
+lost to a preview nobody keeps.
+
+The budget below fixes the priority the old loop had backwards:
+
+* **Segment closes and the final pass are never budgeted.** They ARE the
+ transcript. Rationing them to protect a preview would be absurd.
+* **The preview spends what is left, and skips when there is nothing.** A
+ preview that updates every few seconds instead of every 1.2 s is a
+ cosmetic difference; a transcript with a hole in it is not.
+
+It is deliberately PROCESS-WIDE, not per session. Provider limits are rolling
+windows, so three back-to-back 20-second dictations hit the same limit one long
+one does — a per-session budget would reset each time and rebuild the exact
+problem it is meant to prevent.
+
+The backoff in the dictation loop stays: this keeps the limit from being
+reached, the backoff recovers when something else reaches it anyway (a shared
+key, another app, a tighter tier than we assumed).
+"""
+
+from __future__ import annotations
+
+import threading
+import time
+
+#: Preview calls allowed per rolling minute, across every dictation in this
+#: process. Chosen against the tightest limit a free cloud STT tier commonly
+#: grants (~20 requests/minute) MINUS the segment traffic that must always fit
+#: (~7.5/min at the default 8 s segments), with room to spare — the cost of
+#: guessing high is the very failure this exists to prevent, and the cost of
+#: guessing low is only that the preview lags a little.
+PREVIEW_CALLS_PER_MINUTE = 10
+
+_WINDOW_S = 60.0
+
+
+class PreviewBudget:
+ """Token budget over a rolling window. Thread-safe, allocation-light."""
+
+ def __init__(self, calls_per_minute: int = PREVIEW_CALLS_PER_MINUTE) -> None:
+ self._limit = max(0, int(calls_per_minute))
+ self._lock = threading.Lock()
+ self._stamps: list[float] = []
+
+ def _prune(self, now: float) -> None:
+ cutoff = now - _WINDOW_S
+ if self._stamps and self._stamps[0] < cutoff:
+ self._stamps = [t for t in self._stamps if t >= cutoff]
+
+ def try_spend(self) -> bool:
+ """Claim one preview call. ``False`` means: skip the preview this tick."""
+ if self._limit <= 0:
+ return False
+ now = time.monotonic()
+ with self._lock:
+ self._prune(now)
+ if len(self._stamps) >= self._limit:
+ return False
+ self._stamps.append(now)
+ return True
+
+ def remaining(self) -> int:
+ """How many preview calls the current window still allows."""
+ now = time.monotonic()
+ with self._lock:
+ self._prune(now)
+ return max(0, self._limit - len(self._stamps))
+
+ def reset(self) -> None:
+ """Drop the window — test-isolation hook."""
+ with self._lock:
+ self._stamps = []
+
+
+#: The one budget every dictation shares. Module-level for the reason in the
+#: docstring: consecutive dictations meet the same rolling provider window.
+_BUDGET = PreviewBudget()
+
+
+def preview_budget() -> PreviewBudget:
+ """The shared preview budget."""
+ return _BUDGET
+
+
+__all__ = [
+ "PREVIEW_CALLS_PER_MINUTE",
+ "PreviewBudget",
+ "preview_budget",
+]
diff --git a/jarvis/speech/pipeline.py b/jarvis/speech/pipeline.py
index bd8bbd659c..effe89fe3f 100644
--- a/jarvis/speech/pipeline.py
+++ b/jarvis/speech/pipeline.py
@@ -8548,6 +8548,7 @@ class SpeechPipeline:
async def _probe() -> None:
"""Close finished segments and publish the live transcript."""
nonlocal last_published, language, error_backoff_s
+ from jarvis.dictation.preview_budget import preview_budget
from jarvis.dictation.segment import is_silent_segment
try:
@@ -8583,8 +8584,18 @@ class SpeechPipeline:
if segment_bytes and len(tail) > segment_bytes:
tail = tail[-segment_bytes:]
tail_text = ""
- if len(tail) >= min_bytes and not is_silent_segment(
- tail, session_peak=session_peak
+ if (
+ len(tail) >= min_bytes
+ and not is_silent_segment(tail, session_peak=session_peak)
+ # The preview is the only budgeted caller. It re-sends
+ # the open tail on every tick and throws the answer away
+ # on the next one, so at ~40 requests per minute of
+ # speech it was spending the provider's per-minute limit
+ # on a cosmetic feature — and the segment closes that
+ # actually produce the transcript were the ones refused.
+ # Skipping a preview costs a slightly staler line on
+ # screen; skipping a segment costs the user their words.
+ and preview_budget().try_spend()
):
tail_text, lang, ok = await _transcribe(tail)
if stop_event.is_set():
diff --git a/tests/unit/dictation/test_preview_budget.py b/tests/unit/dictation/test_preview_budget.py
new file mode 100644
index 0000000000..24e418262f
--- /dev/null
+++ b/tests/unit/dictation/test_preview_budget.py
@@ -0,0 +1,67 @@
+"""The live preview must not spend the request budget the transcript needs.
+
+Measured cause (2026-07-29): dictation spent ~40 provider requests per minute of
+speech, only ~7.5 of them producing final text. The rest were preview calls
+whose answers are discarded on the next tick — and they were what pushed a 137 s
+dictation past the provider's per-minute limit, so the segment closes got the
+429s and the user got 367 characters for two minutes of speech.
+"""
+
+from __future__ import annotations
+
+from jarvis.dictation.preview_budget import (
+ PREVIEW_CALLS_PER_MINUTE,
+ PreviewBudget,
+ preview_budget,
+)
+
+
+def test_the_budget_stops_at_its_limit():
+ budget = PreviewBudget(calls_per_minute=3)
+ assert [budget.try_spend() for _ in range(5)] == [True, True, True, False, False]
+
+
+def test_remaining_reports_what_is_left():
+ budget = PreviewBudget(calls_per_minute=3)
+ assert budget.remaining() == 3
+ budget.try_spend()
+ assert budget.remaining() == 2
+
+
+def test_the_window_rolls(monkeypatch):
+ """A minute later the calls are free again — it is a window, not a quota."""
+ import jarvis.dictation.preview_budget as mod
+
+ now = [1_000.0]
+ monkeypatch.setattr(mod.time, "monotonic", lambda: now[0])
+ budget = PreviewBudget(calls_per_minute=2)
+ assert budget.try_spend() and budget.try_spend()
+ assert not budget.try_spend()
+
+ now[0] += 61.0
+ assert budget.try_spend(), "the rolling window should have released the calls"
+
+
+def test_a_zero_budget_disables_the_preview_entirely():
+ """The escape hatch for a very tight tier: transcript only, no preview."""
+ assert PreviewBudget(calls_per_minute=0).try_spend() is False
+
+
+def test_the_budget_is_shared_across_dictations():
+ """Provider limits are rolling windows; per-session budgets would reset.
+
+ Three back-to-back 20 s dictations meet the same limit one 60 s dictation
+ does. A budget that restarted with each session would rebuild exactly the
+ failure it exists to prevent.
+ """
+ assert preview_budget() is preview_budget()
+
+
+def test_the_default_leaves_room_for_the_segment_traffic():
+ """Segments close ~7.5x/min at the 8 s default and are NEVER budgeted.
+
+ The preview default plus that traffic has to stay under the tightest common
+ free-tier limit (~20 requests/minute), or the budget would be decorative.
+ """
+ segments_per_minute = 60 / 8.0
+ assert PREVIEW_CALLS_PER_MINUTE + segments_per_minute < 20