Spaces:
Running
Running
Per-agent retry budgets: narrator waits, Keeper degrades fast
Browse files- agents/base.py +12 -6
agents/base.py
CHANGED
|
@@ -85,6 +85,12 @@ class Agent:
|
|
| 85 |
role: str # registry key: "specialist" | "router"
|
| 86 |
system: str # persona / instructions
|
| 87 |
cfg: LLMConfig = field(default_factory=LLMConfig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
def _messages(self, user: str, history: list[dict] | None) -> list[dict]:
|
| 90 |
return [{"role": "system", "content": f"{self.system} {NO_THINK}"}, *(history or []),
|
|
@@ -99,7 +105,7 @@ class Agent:
|
|
| 99 |
# Open the stream with cold-start retries; once streaming we don't restart
|
| 100 |
# mid-sentence — a stream that breaks just ends the (already-narrated) beat.
|
| 101 |
last_err: Exception | None = None
|
| 102 |
-
for attempt in range(
|
| 103 |
try:
|
| 104 |
s = client.chat.completions.create(
|
| 105 |
model=model, messages=self._messages(user, history),
|
|
@@ -113,8 +119,8 @@ class Agent:
|
|
| 113 |
return
|
| 114 |
except Exception as e: # transient: cold endpoint, timeout, blip
|
| 115 |
last_err = e
|
| 116 |
-
if attempt <
|
| 117 |
-
time.sleep(
|
| 118 |
# Degraded but never crashing: keep the dream moving with a soft beat.
|
| 119 |
yield "The dream wavers for a moment, then steadies."
|
| 120 |
_ = last_err
|
|
@@ -132,7 +138,7 @@ class Agent:
|
|
| 132 |
if MOCK:
|
| 133 |
return loose_json(_mock_line(self.name, user)) or {}
|
| 134 |
client, model = get_client(self.role)
|
| 135 |
-
for attempt in range(
|
| 136 |
try:
|
| 137 |
r = client.chat.completions.create(
|
| 138 |
model=model, messages=self._messages(user, history),
|
|
@@ -146,6 +152,6 @@ class Agent:
|
|
| 146 |
text = msg.content or getattr(msg, "reasoning_content", "") or ""
|
| 147 |
return loose_json(text)
|
| 148 |
except Exception:
|
| 149 |
-
if attempt <
|
| 150 |
-
time.sleep(
|
| 151 |
return {}
|
|
|
|
| 85 |
role: str # registry key: "specialist" | "router"
|
| 86 |
system: str # persona / instructions
|
| 87 |
cfg: LLMConfig = field(default_factory=LLMConfig)
|
| 88 |
+
# Per-agent retry budget. Essential agents (the narrator) inherit the wide
|
| 89 |
+
# default so they patiently wait out a cold start; presentational agents (the
|
| 90 |
+
# Keeper) override to a SHORT budget so a cold endpoint degrades fast instead
|
| 91 |
+
# of holding the whole turn hostage.
|
| 92 |
+
retries: int = RETRIES
|
| 93 |
+
retry_wait: float = RETRY_WAIT
|
| 94 |
|
| 95 |
def _messages(self, user: str, history: list[dict] | None) -> list[dict]:
|
| 96 |
return [{"role": "system", "content": f"{self.system} {NO_THINK}"}, *(history or []),
|
|
|
|
| 105 |
# Open the stream with cold-start retries; once streaming we don't restart
|
| 106 |
# mid-sentence — a stream that breaks just ends the (already-narrated) beat.
|
| 107 |
last_err: Exception | None = None
|
| 108 |
+
for attempt in range(self.retries):
|
| 109 |
try:
|
| 110 |
s = client.chat.completions.create(
|
| 111 |
model=model, messages=self._messages(user, history),
|
|
|
|
| 119 |
return
|
| 120 |
except Exception as e: # transient: cold endpoint, timeout, blip
|
| 121 |
last_err = e
|
| 122 |
+
if attempt < self.retries - 1:
|
| 123 |
+
time.sleep(self.retry_wait)
|
| 124 |
# Degraded but never crashing: keep the dream moving with a soft beat.
|
| 125 |
yield "The dream wavers for a moment, then steadies."
|
| 126 |
_ = last_err
|
|
|
|
| 138 |
if MOCK:
|
| 139 |
return loose_json(_mock_line(self.name, user)) or {}
|
| 140 |
client, model = get_client(self.role)
|
| 141 |
+
for attempt in range(self.retries):
|
| 142 |
try:
|
| 143 |
r = client.chat.completions.create(
|
| 144 |
model=model, messages=self._messages(user, history),
|
|
|
|
| 152 |
text = msg.content or getattr(msg, "reasoning_content", "") or ""
|
| 153 |
return loose_json(text)
|
| 154 |
except Exception:
|
| 155 |
+
if attempt < self.retries - 1:
|
| 156 |
+
time.sleep(self.retry_wait)
|
| 157 |
return {}
|