Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(linux): a busy moment at Ling no longer loses meeting lines

wquguru73113a66fb58
diff --git a/linux/cicada/core/ling.py b/linux/cicada/core/ling.py
index e0d0842..2e116ef 100644
--- a/linux/cicada/core/ling.py
+++ b/linux/cicada/core/ling.py
@@ -96,16 +96,48 @@ class LingError(Exception):
NO_KEY, INVALID_KEY, RATE_LIMITED, HIJACKED, TIMEOUT, HTTP, NETWORK, PARSE = (
"NoKey", "InvalidKey", "RateLimited", "Hijacked", "Timeout", "Http", "Network", "Parse")
- def __init__(self, kind: str, status: int = 0):
+ def __init__(self, kind: str, status: int = 0, retry_after: float | None = None):
super().__init__("http%d" % status if kind == self.HTTP else kind)
self.kind = kind
self.status = status
+ self.retry_after = retry_after # seconds the service asked us to wait (a 429's Retry-After)
class Response:
- def __init__(self, status: int, body: str):
+ def __init__(self, status: int, body: str, retry_after: str | None = None):
self.status = status
self.body = body
+ self.retry_after = retry_after # the Retry-After header, as sent
+
+
+def retry_after_seconds(value: str | None, now: float | None = None) -> float | None:
+ """A Retry-After header in seconds: a number, or an HTTP date. None when absent or unreadable."""
+ if not value:
+ return None
+ try:
+ return max(0.0, float(value.strip()))
+ except ValueError:
+ pass
+ try:
+ import email.utils
+ when = email.utils.parsedate_to_datetime(value.strip())
+ except (TypeError, ValueError, IndexError):
+ return None
+ if when is None:
+ return None
+ return max(0.0, when.timestamp() - (time.time() if now is None else now))
+
+
+class Clock:
+ """Time for the start gate and its waits; tests hand in a fake that moves on by itself."""
+
+ @staticmethod
+ def now() -> float:
+ return time.monotonic()
+
+ @staticmethod
+ def wait(condition: threading.Condition, seconds: float | None) -> None:
+ condition.wait(seconds)
class StreamResponse:
@@ -129,7 +161,8 @@ class UrlTransport:
with urllib.request.urlopen(req, timeout=timeout) as r:
return Response(r.status, r.read().decode("utf-8", "replace"))
except urllib.error.HTTPError as e:
- return Response(e.code, e.read().decode("utf-8", "replace") if e.fp else "")
+ return Response(e.code, e.read().decode("utf-8", "replace") if e.fp else "",
+ e.headers.get("Retry-After") if e.headers else None)
except socket.timeout as e:
raise TimeoutError() from e
except urllib.error.URLError as e:
@@ -199,9 +232,10 @@ class LingClient:
UI thread."""
spacing: float | None = None # tests only: overrides the service's
- def __init__(self, key: AIKey, transport=UrlTransport, config=None):
+ def __init__(self, key: AIKey, transport=UrlTransport, config=None, clock=None):
self.key = key
self.transport = transport
+ self.clock = clock or Clock()
self.config = config or _ling_config
self.key_state = NONE if self.current_key() is None else UNCHECKED
# Step by step, where the service stands (aicheck.py); `key_state` is its short form.
@@ -222,7 +256,8 @@ class LingClient:
self._listeners = []
self._gate = threading.Condition()
self._waiting = []
- self._last_start = 0.0
+ self._last_start = -1e9
+ self._paused_until = 0.0 # after a 429 no request starts before this (clock time)
self._seq = 0
self._check_lock = threading.Lock()
self._check_seq = 0
@@ -535,9 +570,10 @@ class LingClient:
def chat(self, content: str, instruction: str, priority: int, json: bool = False, max_tokens: int = 1024,
timeout: float = 30.0, model: str | None = None, audio: bytes | None = None, patient: bool = False) -> str:
"""One chat completion. `content` goes in the system message, `instruction` is the fixed user
- message, `audio` (a WAV) goes before it for the hear model. Notes retry 429s with backoff;
- heard audio gets one retry (a lost reply loses the words); the rest fail fast so the caller
- can fall back. `patient` (the final notes) keeps retrying for about a minute (retry_waits)."""
+ message, `audio` (a WAV) goes before it for the hear model. Notes and heard audio retry 429s
+ with backoff (retry_waits); the rest fail fast so the caller can fall back. `patient` (the
+ final notes) keeps retrying for about a minute. A 429 holds every request start for a while
+ (_rate_limited), and a retry keeps its place in the queue, so lines stay in order."""
k = self.current_key()
if k is None:
raise LingError(LingError.NO_KEY)
@@ -558,8 +594,10 @@ class LingClient:
waits = retry_waits(priority, audio is not None, patient)
attempt = 1
name = PRIORITY_NAMES[priority]
+ ticket = None
while True:
- self._acquire(priority, c.provider.spacing)
+ # One that wouldn't retry fails at once while the service is cooling down: its caller falls back.
+ ticket = self._acquire(priority, c.provider.spacing, ticket, fail_if_paused=not waits)
started = time.monotonic()
try:
root = self._post(c.base + "chat/completions", payload, k, timeout)
@@ -586,13 +624,12 @@ class LingClient:
self.observe(e, audio=audio is not None)
if e.kind != LingError.RATE_LIMITED:
raise
- if attempt > len(waits):
+ wait = self._rate_limited(waits[min(attempt, len(waits)) - 1] if waits else 1.0, e.retry_after)
+ if attempt > len(waits) or wait is None:
self.rate_limited_at = time.time()
self._changed()
raise
- wait = waits[attempt - 1] + random.random() * 0.5
Log.d("ling: %s 429, retry in %.1f s" % (name, wait))
- time.sleep(wait)
attempt += 1
def speech(self, text: str, voice: str | None = "UK_Charlotte", language: str | None = None, timeout: float = 20.0):
@@ -689,7 +726,7 @@ class LingClient:
self._changed()
raise LingError(LingError.INVALID_KEY)
if res.status == 429:
- raise LingError(LingError.RATE_LIMITED)
+ raise LingError(LingError.RATE_LIMITED, retry_after=retry_after_seconds(getattr(res, "retry_after", None)))
if res.status != 200:
Log.d("ling: HTTP %d" % res.status)
raise LingError(LingError.HTTP, res.status)
@@ -698,39 +735,67 @@ class LingClient:
raise LingError(LingError.PARSE)
return root
- # Start gate: request starts ≥ `spacing` apart, the most urgent waiting one first.
+ # Start gate: request starts ≥ `spacing` apart, the most urgent waiting one first, none while
+ # the service is cooling down after a 429.
- def _acquire(self, priority: int, spacing: float = 0.6) -> None:
+ def _acquire(self, priority: int, spacing: float = 0.6, ticket: int | None = None,
+ fail_if_paused: bool = False) -> int:
+ """Waits for this request's turn to start. `ticket` is a retry's first one, which keeps its
+ place ahead of requests queued after it. Returns the ticket."""
spacing = LingClient.spacing if LingClient.spacing is not None else spacing
with self._gate:
- self._seq += 1
- me = (priority, self._seq)
+ if fail_if_paused and self._paused_until > self.clock.now():
+ raise LingError(LingError.RATE_LIMITED)
+ if ticket is None:
+ self._seq += 1
+ ticket = self._seq
+ me = (priority, ticket)
self._waiting.append(me)
try:
while True:
head = min(self._waiting)
- wait = spacing - (time.monotonic() - self._last_start)
+ now = self.clock.now()
+ wait = max(spacing - (now - self._last_start), self._paused_until - now)
if head == me and wait <= 0:
self._waiting.remove(me)
- self._last_start = time.monotonic()
+ self._last_start = now
self._gate.notify_all()
- return
- self._gate.wait(wait if head == me else None)
+ return ticket
+ self.clock.wait(self._gate, wait if head == me else None)
except BaseException:
if me in self._waiting:
self._waiting.remove(me)
self._gate.notify_all()
raise
+ def _rate_limited(self, backoff: float, retry_after: float | None) -> float | None:
+ """After a 429: no request starts for `backoff` seconds, or as long as the service asked
+ (Retry-After; Ling sends none), plus a little jitter. The queue waits at the gate instead of
+ each request spending its retries on the same limit. None when the service asks for longer
+ than MAX_PAUSE: that is an outage, not a busy moment (the gate still holds for MAX_PAUSE)."""
+ wait = max(backoff, retry_after or 0.0) + random.random() * 0.5
+ with self._gate:
+ self._paused_until = max(self._paused_until, self.clock.now() + min(wait, MAX_PAUSE))
+ self._gate.notify_all()
+ return wait if wait <= MAX_PAUSE else None
+
+
+MAX_PAUSE = 30.0
+
def retry_waits(priority: int, audio: bool = False, patient: bool = False) -> list:
- """Seconds before each retry of a 429 (Ling.swift › retryWaits). Notes retry a few times, heard
- audio once; the rest fail fast so the caller can fall back. `patient` (the final notes) keeps at
- it for about a minute: a model limited upstream (OpenRouter's Ling 3.1 by DeepInfra, 2026-11)
- came back after ~15 s, and a meeting left without its notes is worse than notes a little late."""
+ """Seconds before each retry of a 429 (Ling.swift › retryWaits). Notes retry a few times; the
+ rest fail fast so the caller can fall back, except heard audio. A meeting's: there a lost reply
+ loses the words, and Ling's 429s came in runs of up to ~10 s (two in a row lost a line with one
+ retry, about 2 a minute in 2026-11 testing), so it keeps at it for about 15 s. What the user says
+ (SPEAK) retries once: late into the room is worse than a short "try again". `patient` (the final notes)
+ keeps at it for about a minute: a model limited upstream (OpenRouter's Ling 3.1 by DeepInfra,
+ 2026-11) came back after ~15 s, and a meeting left without its notes is worse than notes a little late."""
if priority == NOTES:
return [1, 2, 4, 8, 16, 30] if patient else [1, 2, 4]
- return [1] if audio else []
+ if audio:
+ return [1, 2, 4, 8] if priority == TRANSLATE else [1]
+ return []
def _b64(s: str) -> bytes:
diff --git a/linux/tests/test_core.py b/linux/tests/test_core.py
index 9438197..3a73766 100644
--- a/linux/tests/test_core.py
+++ b/linux/tests/test_core.py
@@ -157,10 +157,26 @@ def reply(content, finish="stop"):
"usage": {"prompt_tokens": 1, "completion_tokens": 1}}, pretty=False))
-def client(fake):
+def client(fake, clock=None):
key = AIKey()
key.save("sk-test-0000000000")
- return LingClient(key, fake)
+ return LingClient(key, fake, clock=clock)
+
+
+class FakeClock:
+ """The start gate's time, moved on by each wait instead of waiting (one thread only)."""
+
+ def __init__(self):
+ self.start = self.t = 1000.0
+ self.waits = []
+
+ def now(self):
+ return self.t
+
+ def wait(self, condition, seconds):
+ assert seconds is not None, "would wait forever"
+ self.waits.append(seconds)
+ self.t += seconds
def content(body, i):
@@ -579,18 +595,89 @@ class Ling(Base):
ling.chat("c", "i", TRANSLATE)
self.assertEqual(LingError.HIJACKED, e.exception.kind)
- def test_retries_audio_once_when_busy(self):
- n = [0]
-
- def respond(b):
- n[0] += 1
- return Response(429, "") if n[0] == 1 else reply("LANG: en\nORIGINAL: Hi.\nTRANSLATION: 嗨。")
- r = client(Fake(respond)).chat("c", "i", TRANSLATE, model=AUDIO_MODEL, audio=bytes([1]))
+ def test_heard_audio_rides_out_a_run_of_429s(self):
+ # Ling's 429s come in runs (two in a row lost a line with one retry): heard audio backs off
+ # 1, 2, 4 s and gets through; text translation fails at once so the caller can fall back.
+ clock = FakeClock()
+ replies = [Response(429, "")] * 3 + [reply("LANG: en\nORIGINAL: Hi.\nTRANSLATION: 嗨。")]
+ fake = Fake(lambda b: replies.pop(0))
+ r = client(fake, clock).chat("c", "i", TRANSLATE, model=AUDIO_MODEL, audio=bytes([1]))
self.assertIn("ORIGINAL: Hi.", r)
- self.assertEqual(2, n[0])
+ self.assertEqual(4, len(fake.calls))
+ self.assertEqual(3, len(clock.waits))
+ for wait, backoff in zip(clock.waits, (1, 2, 4)):
+ self.assertTrue(backoff <= wait <= backoff + 0.5, clock.waits)
+ with self.assertRaises(LingError) as e:
+ client(Fake(lambda b: Response(429, "")), FakeClock()).chat("c", "i", TRANSLATE)
+ self.assertEqual(LingError.RATE_LIMITED, e.exception.kind)
+
+ def test_heard_audio_is_lost_only_after_an_outage(self):
+ clock = FakeClock()
+ fake = Fake(lambda b: Response(429, ""))
+ ling = client(fake, clock)
with self.assertRaises(LingError) as e:
- client(Fake(lambda b: Response(429, ""))).chat("c", "i", TRANSLATE)
+ ling.chat("c", "i", TRANSLATE, model=AUDIO_MODEL, audio=bytes([1]))
self.assertEqual(LingError.RATE_LIMITED, e.exception.kind)
+ self.assertEqual(5, len(fake.calls))
+ self.assertGreaterEqual(clock.t - clock.start, 15)
+ self.assertIsNotNone(ling.rate_limited_at)
+
+ def test_waits_as_long_as_retry_after_asks(self):
+ from cicada.core.ling import retry_after_seconds
+ clock = FakeClock()
+ replies = [Response(429, "", retry_after="5"), reply("ok")]
+ self.assertEqual("ok", client(Fake(lambda b: replies.pop(0)), clock).chat("c", "i", NOTES))
+ self.assertTrue(5 <= clock.waits[0] <= 5.5, clock.waits)
+ # Asked to wait minutes: an outage, so the line goes now rather than after it.
+ fake = Fake(lambda b: Response(429, "", retry_after="120"))
+ with self.assertRaises(LingError):
+ client(fake, FakeClock()).chat("c", "i", TRANSLATE, model=AUDIO_MODEL, audio=bytes([1]))
+ self.assertEqual(1, len(fake.calls))
+ self.assertEqual(3.0, retry_after_seconds("3"))
+ self.assertAlmostEqual(7.0, retry_after_seconds("Thu, 05 Nov 2026 12:00:07 GMT",
+ now=datetime(2026, 11, 5, 12, 0, tzinfo=timezone.utc).timestamp()))
+ self.assertIsNone(retry_after_seconds("soon"))
+ self.assertIsNone(retry_after_seconds(None))
+
+ def test_a_429_holds_every_start_while_the_service_cools_down(self):
+ clock = FakeClock()
+ replies = [Response(429, ""), reply("notes")]
+ fake = Fake(lambda b: replies.pop(0))
+ ling = client(fake, clock)
+ with self.assertRaises(LingError):
+ ling.chat("first", "i", TRANSLATE)
+ # A translation that wouldn't retry fails at once, without spending a request on the limit...
+ with self.assertRaises(LingError) as e:
+ ling.chat("second", "i", TRANSLATE)
+ self.assertEqual(LingError.RATE_LIMITED, e.exception.kind)
+ self.assertEqual(1, len(fake.calls))
+ # ...and one that would waits at the gate until the pause is over, then goes.
+ self.assertEqual("notes", ling.chat("notes", "i", NOTES))
+ self.assertEqual(2, len(fake.calls))
+ self.assertTrue(1 <= clock.waits[0] <= 1.5, clock.waits)
+
+ def test_a_retried_line_keeps_its_place_in_the_queue(self):
+ LingClient.spacing = 0.05
+ first = [True]
+
+ def respond(b):
+ if content(b, 0) == "a" and first[0]:
+ first[0] = False
+ return Response(429, "")
+ return reply("ok")
+ fake = Fake(respond)
+ ling = client(fake)
+ hear = {"model": AUDIO_MODEL, "audio": bytes([1])}
+ a = threading.Thread(target=ling.chat, args=("a", "i", TRANSLATE), kwargs=hear)
+ a.start()
+ time.sleep(0.2)
+ b = threading.Thread(target=ling.chat, args=("b", "i", TRANSLATE), kwargs=hear)
+ b.start()
+ for t in (a, b):
+ t.join()
+ self.assertEqual(["a", "a", "b"], [content(x[1], 0) for x in fake.calls])
+ # b, queued during the pause, didn't start until it was over.
+ self.assertGreaterEqual(fake.calls[2][0] - fake.calls[0][0], 1.0)
def test_marks_a_bad_key(self):
ling = client(Fake(lambda b: Response(401, "")))
diff --git a/linux/tests/test_providers.py b/linux/tests/test_providers.py
index 0b84e37..52ffc6c 100644
--- a/linux/tests/test_providers.py
+++ b/linux/tests/test_providers.py
@@ -267,7 +267,9 @@ class Words(Base):
def test_final_notes_keep_retrying_for_about_a_minute(self):
from cicada.core.ling import NOTES, retry_waits
self.assertEqual([], retry_waits(TRANSLATE))
- self.assertEqual([1], retry_waits(TRANSLATE, audio=True))
+ self.assertEqual([1, 2, 4, 8], retry_waits(TRANSLATE, audio=True))
+ from cicada.core.ling import SPEAK
+ self.assertEqual([1], retry_waits(SPEAK, audio=True))
self.assertEqual([1, 2, 4], retry_waits(NOTES))
self.assertTrue(50 <= sum(retry_waits(NOTES, patient=True)) <= 70)