@yahyahammad/venastine 0.3.16 → 0.3.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -39,6 +39,13 @@ review.py:_validated for reasons that module states.
39
39
  Unrecoverable failure raises ValueError with the final parse error
40
40
  attached -- the orchestrator's try/except turns that into a durable
41
41
  status='failed' record.
42
+
43
+ ROADMAP_v3 §95, amended in batch 267, added the THIRD thing worth
44
+ correcting, for the two passes whose answer is prose (Pass 1 and Final
45
+ synthesis): a reply with no text at all. `retry_until_text` is the same
46
+ recovery with the same thread, the same attempt arithmetic and the same
47
+ `max_json_retries` budget, and both loops send their corrective through
48
+ `_corrective_turn`, so the continuation's arguments have one copy.
42
49
  """
43
50
 
44
51
  from __future__ import annotations
@@ -52,6 +59,23 @@ from core.reasoning.payload_validation import PayloadShapeError
52
59
 
53
60
  MAX_JSON_RETRIES = config.MAX_JSON_RETRIES
54
61
 
62
+ # ROADMAP_v3 §95 (RO6). What an empty reply is told about its reasoning,
63
+ # in ONE copy for the JSON corrective and the free-text one (batch 267):
64
+ # the two would otherwise drift, and this sentence is the part chosen to
65
+ # correct a model that believes its reasoning was the answer.
66
+ _REASONING_IS_NOT_THE_REPLY = (
67
+ "Anything written only in your reasoning is not part of the response, "
68
+ "and a tool call written there is not run.")
69
+
70
+ # Batch 267. RO6's no-text wording with the JSON half taken out: a prose
71
+ # pass has no JSON to be missing, and "ONLY valid JSON" would talk it out
72
+ # of its own output contract.
73
+ _TEXT_CORRECTIVE = (
74
+ "Your last response contained no text at all. "
75
+ + _REASONING_IS_NOT_THE_REPLY + "\n\n"
76
+ "Respond again with the answer your original instructions asked for, "
77
+ "written as the reply itself.")
78
+
55
79
 
56
80
  def parse_json_response(text: str) -> Any:
57
81
  """Callers are instructed (via the universal preamble, or an agent's
@@ -134,8 +158,6 @@ def retry_until_json(
134
158
  brackets the whole attempt-and-retry sequence, and a row
135
159
  per correction would report one run as several.
136
160
  """
137
- from core.loop import RunAgentLoop
138
-
139
161
  raw_text = response.text
140
162
 
141
163
  for attempt in range(max_retries):
@@ -160,32 +182,12 @@ def retry_until_json(
160
182
  _shape_corrective(e, raw_text) if shape
161
183
  else _parse_corrective(e, raw_text)
162
184
  )
163
- retry_response = RunAgentLoop.continue_conversation(
164
- thread_id=response.thread_id,
165
- message=corrective,
166
- system_prompt=system_prompt,
167
- model=model,
168
- provider_name=provider_name,
169
- temperature=temperature,
170
- # A retry is the SAME work continuing. Omitting this would
171
- # strip the gated tools on exactly the attempt where the
172
- # model is already struggling, and the only symptom would
173
- # be a second malformed answer.
174
- authorization=authorization,
175
- context=context,
176
- # Same rule, batch 25 (#139): a retry is the same pass
177
- # continuing, so it carries the pass's effort too --
178
- # effort_for re-validates against this thread's model.
179
- effort=effort,
180
- # And the same rule again for §47's sink (batch 75). This
181
- # hop dropped it, so a spawn inside a corrective attempt
182
- # was invisible -- and this is the one path where the
183
- # channel is the ONLY route, because continue_conversation
184
- # drains its own loop.
185
- activity=activity,
186
- **({} if max_total_tokens is None
187
- else {"max_total_tokens": max_total_tokens}),
188
- )
185
+ retry_response = _corrective_turn(
186
+ response, corrective,
187
+ system_prompt=system_prompt, model=model,
188
+ provider_name=provider_name, temperature=temperature,
189
+ authorization=authorization, context=context, effort=effort,
190
+ activity=activity, max_total_tokens=max_total_tokens)
189
191
  if on_response is not None:
190
192
  on_response(retry_response)
191
193
  raw_text = retry_response.text
@@ -195,6 +197,123 @@ def retry_until_json(
195
197
  return raw_text
196
198
 
197
199
 
200
+ def retry_until_text(
201
+ response,
202
+ label: str,
203
+ system_prompt: str,
204
+ model: str,
205
+ provider_name: str,
206
+ trace: Optional[list] = None,
207
+ temperature: Optional[float] = None,
208
+ authorization=None,
209
+ max_retries: int = MAX_JSON_RETRIES,
210
+ on_response: Optional[Callable] = None,
211
+ effort: Optional[str] = None,
212
+ activity=None,
213
+ ) -> str:
214
+ """Returns the first response text that is not empty, retrying by
215
+ continuing `response`'s thread. ROADMAP_v3 §95, amended in batch 267.
216
+
217
+ The owner's report: a research pass whose model replied with reasoning
218
+ only twice ended `complete` with "" -- RO2 ends the TURN there, which
219
+ is right for a chat turn a person is watching -- and Pass 1 handed
220
+ Pass 2 nothing, so the run wrote an empty raw response and an empty
221
+ report and called itself complete. The JSON passes already survived
222
+ the same reply, because an empty string does not parse and RO6
223
+ corrects it. A prose pass had nothing that failed.
224
+
225
+ So this is retry_until_json for a pass whose contract is "some text":
226
+ the same thread, the same `max_retries` (config.yaml's
227
+ `max_json_retries`, owner's choice, rather than a second knob), and
228
+ the same failure shape -- ValueError once every attempt came back
229
+ empty, which the orchestrator turns into a failed pass and a failed
230
+ run. Whitespace counts as no text, as RO1 counts it.
231
+
232
+ Each corrective attempt is a fresh `continue_conversation` run, so it
233
+ gets RO2's own ask too: one attempt can cost two model calls.
234
+
235
+ The arguments are retry_until_json's, minus the two only its callers
236
+ use (`context`, `max_total_tokens`) and `validate`, which has nothing
237
+ to apply to.
238
+ """
239
+ current = response
240
+ for attempt in range(max_retries):
241
+ if (current.text or "").strip():
242
+ return current.text
243
+ if trace is not None:
244
+ trace.append(
245
+ f"{label}: no text on attempt {attempt + 1}/"
246
+ f"{max_retries + 1} ({_why_empty(current)}), "
247
+ f"retrying with corrective message.")
248
+ current = _corrective_turn(
249
+ response, _TEXT_CORRECTIVE,
250
+ system_prompt=system_prompt, model=model,
251
+ provider_name=provider_name, temperature=temperature,
252
+ authorization=authorization, context=None, effort=effort,
253
+ activity=activity, max_total_tokens=None)
254
+ if on_response is not None:
255
+ on_response(current)
256
+ if (current.text or "").strip():
257
+ return current.text
258
+ raise ValueError(
259
+ f"{label} returned no text after {max_retries + 1} attempt(s); the "
260
+ f"last reply was {_why_empty(current)}. Every later pass would be "
261
+ f"working from nothing, so the pass fails here (max_json_retries "
262
+ f"in config.yaml sets how many times it is asked again).")
263
+
264
+
265
+ def _why_empty(response) -> str:
266
+ """Batch 267. The loop's own account of an empty reply, read from the
267
+ notice RO2 ends a reasoning-only turn with, rather than re-derived from
268
+ the response: that notice is the loop saying why it stopped."""
269
+ from core.loop import REASONING_ONLY_END
270
+
271
+ if any(isinstance(n, dict) and n.get("kind") == REASONING_ONLY_END
272
+ for n in getattr(response, "notices", None) or ()):
273
+ return "reasoning only, with no answer and no tool call"
274
+ return "empty"
275
+
276
+
277
+ def _corrective_turn(response, message: str, *, system_prompt: str,
278
+ model: str, provider_name: str, temperature, authorization,
279
+ context, effort, activity, max_total_tokens):
280
+ """One corrective attempt, sent into the thread `response` came from.
281
+
282
+ Batch 267 moved this out of retry_until_json unchanged, so the JSON
283
+ retry and the text retry continue a thread with ONE set of arguments.
284
+ Every argument below was dropped at this hop once, and each comment
285
+ says what that cost.
286
+ """
287
+ from core.loop import RunAgentLoop
288
+
289
+ return RunAgentLoop.continue_conversation(
290
+ thread_id=response.thread_id,
291
+ message=message,
292
+ system_prompt=system_prompt,
293
+ model=model,
294
+ provider_name=provider_name,
295
+ temperature=temperature,
296
+ # A retry is the SAME work continuing. Omitting this would
297
+ # strip the gated tools on exactly the attempt where the
298
+ # model is already struggling, and the only symptom would
299
+ # be a second malformed answer.
300
+ authorization=authorization,
301
+ context=context,
302
+ # Same rule, batch 25 (#139): a retry is the same pass
303
+ # continuing, so it carries the pass's effort too --
304
+ # effort_for re-validates against this thread's model.
305
+ effort=effort,
306
+ # And the same rule again for §47's sink (batch 75). This
307
+ # hop dropped it, so a spawn inside a corrective attempt
308
+ # was invisible -- and this is the one path where the
309
+ # channel is the ONLY route, because continue_conversation
310
+ # drains its own loop.
311
+ activity=activity,
312
+ **({} if max_total_tokens is None
313
+ else {"max_total_tokens": max_total_tokens}),
314
+ )
315
+
316
+
198
317
  def _check(raw_text: str, validate: Optional[Callable]) -> None:
199
318
  """Both contracts, in the order they can be checked: a payload has to
200
319
  parse before it can have a shape."""
@@ -212,9 +331,7 @@ def _parse_corrective(error: ValueError, raw_text: str) -> str:
212
331
  # reasoning. Say what the harness actually received.
213
332
  return (
214
333
  "Your last response contained no text at all, so there was no "
215
- "JSON to parse. Anything written only in your reasoning is not "
216
- "part of the response, and a tool call written there is not "
217
- "run.\n\n"
334
+ "JSON to parse. " + _REASONING_IS_NOT_THE_REPLY + "\n\n"
218
335
  "Respond again with ONLY valid JSON -- no preamble, no prose, "
219
336
  "no code fence, just the JSON your original instructions "
220
337
  "asked for."
@@ -29,7 +29,9 @@ follow-up message. The failed assistant turn is already in the thread's
29
29
  history (see _run()'s always-persist fix in core/loop.py), so the model
30
30
  sees its own prior output in context. Up to config.MAX_JSON_RETRIES
31
31
  corrective attempts are made; a final ValueError surfaces unrecoverable
32
- failures.
32
+ failures. ROADMAP_v3 §95 (RO6, amended in batch 267) gives the two prose
33
+ passes the same recovery for a reply with no text: _run_pass sends it
34
+ through retry_until_text, and a pass still empty fails.
33
35
 
34
36
  ROADMAP §5 (durable pipeline persistence): each run is wrapped in a
35
37
  try/except that records status='failed' (with a snapshot of the partial
@@ -83,7 +85,7 @@ from core.reasoning.base import Claim, PipelineRun, resolve_by_id
83
85
  from core.reasoning.confidence_scoring import is_contested, run_confidence_tiering
84
86
  from core.reasoning.events import PipelineEvent
85
87
  from core.reasoning.json_retry import parse_json_response as _parse_json_response
86
- from core.reasoning.json_retry import retry_until_json
88
+ from core.reasoning.json_retry import retry_until_json, retry_until_text
87
89
  from core.reasoning.payload_validation import validate as _validate_payload
88
90
  from core.reasoning.pipeline_storage import create_pipeline_run, update_pipeline_run
89
91
  from core.reasoning.scholar import ScholarLookup
@@ -457,6 +459,18 @@ def _run_pass(pass_id: str, pass_input: str, model: str, provider_name: str,
457
459
  threaded from the shell's setting through the pipeline, validated per
458
460
  RECEIVING model by effort_for() inside the loop, so an ensemble roster
459
461
  or critic routing needs no effort-specific handling here.
462
+
463
+ ROADMAP_v3 §95, amended in batch 267: every caller of this function is
464
+ a PROSE pass (Pass 1, each ensemble candidate, Final synthesis), and a
465
+ prose pass that comes back with no text is now asked again in its own
466
+ thread (retry_until_text, the JSON passes' recovery and budget) and
467
+ FAILS if it is still empty. Before, a model that replied with
468
+ reasoning only twice ended the pass `complete` with "", and the run
469
+ finished with an empty raw response and an empty report.
470
+
471
+ The retries share the JSON passes' two §26 limits, stated at
472
+ `_run_pass_with_json_retry`: a tool call a corrective attempt makes
473
+ produces no tool_call event, and a page it fetches reaches no corpus.
460
474
  """
461
475
  yield PipelineEvent(kind="pass_start", pass_id=pass_id)
462
476
  try:
@@ -468,6 +482,24 @@ def _run_pass(pass_id: str, pass_input: str, model: str, provider_name: str,
468
482
  _record_granted_calls(pass_id, response, authorization)
469
483
  _record_pass_thread(pass_id, response, pass_threads)
470
484
  _check_not_truncated(pass_id, response, trace)
485
+ text = retry_until_text(
486
+ response,
487
+ label=pass_id,
488
+ # The prompt the attempt ran under, as the JSON retry builds it
489
+ # and for its reason: a retry that saw a different tool set
490
+ # than the attempt it corrects is a different pass.
491
+ system_prompt=system_prompts.pass_prompt(
492
+ pass_id, *advertisement_facts(authorization)),
493
+ model=model,
494
+ provider_name=provider_name,
495
+ trace=trace,
496
+ temperature=temperature,
497
+ authorization=authorization,
498
+ effort=effort,
499
+ on_response=lambda r: _settle_text_retry(
500
+ pass_id, r, authorization, trace),
501
+ activity=activity,
502
+ )
471
503
  except Exception as exc:
472
504
  # E14/#115, UNIFORM: a fatal pass reports itself before its
473
505
  # exception propagates, so no shell is left holding a row that
@@ -476,7 +508,21 @@ def _run_pass(pass_id: str, pass_input: str, model: str, provider_name: str,
476
508
  yield _pass_failed_event(pass_id, exc)
477
509
  raise
478
510
  yield PipelineEvent(kind="pass_complete", pass_id=pass_id, ok=True)
479
- return response.text
511
+ return text
512
+
513
+
514
+ def _settle_text_retry(pass_id: str, response, authorization,
515
+ trace: list[str] | None) -> None:
516
+ """Batch 267. What `_run_pass` does to its first attempt, done to each
517
+ corrective one: its granted calls join the §25 audit trail (a retry is
518
+ the same pass continuing), and its stop reason is read by the same
519
+ rule. A retry cut off by a ceiling with no text FAILS here, naming the
520
+ stop -- asking again into an exhausted turn says the same thing at a
521
+ cost, and "the last reply was empty" would hide which ceiling it hit.
522
+ One cut off WITH text is traced as truncated and used, like the
523
+ attempt it corrects."""
524
+ _record_granted_calls(pass_id, response, authorization)
525
+ _check_not_truncated(pass_id, response, trace)
480
526
 
481
527
 
482
528
  def _record_pass_thread(pass_id: str, response, pass_threads) -> None:
@@ -72,7 +72,9 @@ KIND_BACKGROUND = "background"
72
72
  KIND_MONITOR = "monitor"
73
73
  KIND_INTERACTIVE = "interactive"
74
74
 
75
- # The kinds whose being live BLOCKS the user's prompt (SS2). Not the same
75
+ # The kinds a post-turn wait waits on, and whose being live holds the
76
+ # shell's commands (SS2; a plain message is not held since batch 265,
77
+ # SS2 as amended). Not the same
76
78
  # question as which kinds occupy the 4-live cap, which is all of them: the
77
79
  # cap is about resources, which an idle shell does hold, and the block is
78
80
  # about whether the agent is waiting on work it cannot proceed without,
@@ -1063,14 +1065,34 @@ class SessionManager:
1063
1065
  with self._lock:
1064
1066
  return self._live_locked(owner_thread)
1065
1067
 
1068
+ def waited_on(self, owner_thread) -> int:
1069
+ """How many of this thread's sessions a post-turn wait is waiting
1070
+ on: the live background and monitor ones (SS25).
1071
+
1072
+ BATCH 265. Both shells' waits asked `live_for`, which counts every
1073
+ kind, while `wait_for_wake` answers for the blocking kinds alone --
1074
+ so with only an interactive shell open, each loop asked again at
1075
+ once, for as long as the shell stayed open. Measured: 37,154 calls
1076
+ in two seconds on the TUI's worker and 38,195 in the CLI's, the turn
1077
+ held open the whole while, until the shell's idle timeout ended it.
1078
+ SS25 is the rule that broke, and this is the count both loops meant.
1079
+ """
1080
+ with self._lock:
1081
+ return self._live_locked(owner_thread, BLOCKING_KINDS)
1082
+
1066
1083
  def blocks(self, owner_thread) -> bool:
1067
- """Whether this thread's input is blocked (SS2): it has a live
1068
- BLOCKING session and waking has not been suspended by the
1084
+ """Whether this thread's sessions hold the shell (SS2): it has a
1085
+ live BLOCKING session and waking has not been suspended by the
1069
1086
  consecutive-wake limit.
1070
1087
 
1071
1088
  An interactive session is deliberately not counted (SS25): the
1072
1089
  block exists because the agent is waiting on work the user cannot
1073
1090
  usefully interrupt, and an open shell is not that.
1091
+
1092
+ SINCE BATCH 265 IT HOLDS COMMANDS, NOT MESSAGES (SS2 as amended).
1093
+ A plain message sent while the shell waits gets a turn of its own;
1094
+ what this still refuses is the commands that would change what the
1095
+ wait is waiting FOR -- another thread, another model -- under it.
1074
1096
  """
1075
1097
  with self._lock:
1076
1098
  inbox = self._inboxes.get(str(owner_thread))
@@ -1115,16 +1137,28 @@ class SessionManager:
1115
1137
  return batch
1116
1138
 
1117
1139
  def wait_for_wake(self, owner_thread,
1118
- timeout: Optional[float] = None
1140
+ timeout: Optional[float] = None,
1141
+ stop: Optional[Callable[[], bool]] = None,
1119
1142
  ) -> Optional[list[WakeEvent]]:
1120
1143
  """Block until there is a batch to wake this thread with. None when
1121
1144
  there is nothing left to wait for: no live session and nothing
1122
- pending, waking suspended, the harness quitting, or *timeout* gone.
1123
- Waits in `poll_s` slices, so a KeyboardInterrupt still lands."""
1145
+ pending, waking suspended, the harness quitting, *timeout* gone, or
1146
+ *stop* answering true. Waits in `poll_s` slices, so a
1147
+ KeyboardInterrupt still lands.
1148
+
1149
+ `stop` (batch 265) is the consumer's own reason to stop waiting --
1150
+ the TUI's, a message the user sent while it waited (SS2 as
1151
+ amended). Asked BEFORE anything is taken, so a stop that is already
1152
+ true never hands out a batch; one that comes true later is read at
1153
+ the next slice, or at once after `nudge`. It is called under this
1154
+ manager's lock, so it must read state and take no lock of its own.
1155
+ """
1124
1156
  deadline = None if timeout is None else self._clock() + timeout
1125
1157
  rows = None
1126
1158
  with self._changed:
1127
1159
  while True:
1160
+ if stop is not None and stop():
1161
+ break
1128
1162
  batch, suspended_now = self._take_locked(owner_thread)
1129
1163
  if batch is not None:
1130
1164
  return batch
@@ -1147,6 +1181,33 @@ class SessionManager:
1147
1181
  self._post(rows)
1148
1182
  return None
1149
1183
 
1184
+ def nudge(self) -> None:
1185
+ """Wake every `wait_for_wake` at once, so a `stop` that has just
1186
+ come true is read now rather than at the end of a poll slice."""
1187
+ with self._changed:
1188
+ self._changed.notify_all()
1189
+
1190
+ def put_back(self, owner_thread, batch: list[WakeEvent]) -> None:
1191
+ """Return a batch `wait_for_wake` handed out that no turn woke with
1192
+ (SS2 as amended in batch 265).
1193
+
1194
+ The user's message and a session's result can reach a waiting shell
1195
+ together, and the owner's rule is that the message goes first: it
1196
+ gets its own turn, and the result wakes after it. So the consumer
1197
+ that took the batch hands it back rather than answering it.
1198
+
1199
+ AT THE FRONT, so these results stay ahead of anything that arrived
1200
+ since, and the wake it was counted as is uncounted, since no turn
1201
+ answered it.
1202
+ """
1203
+ if not batch:
1204
+ return
1205
+ with self._lock:
1206
+ inbox = self._inboxes.setdefault(str(owner_thread), _Inbox())
1207
+ inbox.events[:0] = batch
1208
+ inbox.consecutive_wakes = max(0, inbox.consecutive_wakes - 1)
1209
+ self._changed.notify_all()
1210
+
1150
1211
  def note_user_input(self, owner_thread) -> None:
1151
1212
  """A message from the user resets the consecutive-wake count and
1152
1213
  resumes waking (SS2). Held results stay held until `take_held`."""
@@ -0,0 +1,119 @@
1
+ """
2
+ core/usage_view.py
3
+
4
+ What `/usage` shows, for either shell (ROADMAP_v3 §97, UF4 and UF5).
5
+
6
+ FOUR ROWS, EACH THE PROVIDER'S OWN COUNT. Input, cache read, cache write
7
+ and output, summed over this thread's model calls since it was opened --
8
+ the span the TUI's usage line already gives `billed`
9
+ (`ConversationMemory.record_billed`), so the two figures cannot disagree
10
+ about what they cover. Nothing here is estimated, priced or carried
11
+ across a restart: a figure the provider did not send is not a zero.
12
+
13
+ "NOT REPORTED" IS A ROW'S ANSWER, NOT A ZERO. `core/client.py` puts a
14
+ cache count in a response's usage only when the provider sent one, and
15
+ names on `ModelResponse.usage_unreported` the input or output count that
16
+ `_stream_usage` had to coerce to 0 for the loop's arithmetic (D21). A
17
+ call that did not report a row is not counted for it, and a row reported
18
+ on some calls says on how many: the batch-61 rule that the completion line
19
+ makes no token claim where none was measured, applied to four figures.
20
+
21
+ PURE: no `core/` import, no storage, no clock. `ConversationMemory` holds
22
+ a `UsageTally` for the TUI, which keeps one memory across turns; the CLI
23
+ builds a memory per turn, so `RunAgentLoop.run_agent_conversation` and
24
+ `wake_conversation` hand theirs back on the response and the CLI's
25
+ `ChatSession` adds them up. Both shells then print `report_lines`.
26
+ """
27
+
28
+ from __future__ import annotations
29
+
30
+ #: The rows, in the order `/usage` prints them, as (usage key, label).
31
+ #: The keys are `ModelResponse.usage`'s; the labels are the owner's
32
+ #: choice (§97 UF4).
33
+ ROWS = (
34
+ ("input_tokens", "input"),
35
+ ("cache_read_tokens", "cache read"),
36
+ ("cache_write_tokens", "cache write"),
37
+ ("output_tokens", "output"),
38
+ )
39
+
40
+ #: The two rows that are PART OF input on every provider since §97 UF5:
41
+ #: `core/client.py` adds them into input where a provider reports them
42
+ #: apart, so the report says so rather than inviting a sum.
43
+ CACHE_KEYS = ("cache_read_tokens", "cache_write_tokens")
44
+
45
+
46
+ def _count(value):
47
+ """`value` as a token count, or None. `_token_count`'s rule: a bool is
48
+ not a count and neither is a negative number."""
49
+ if isinstance(value, bool) or not isinstance(value, int):
50
+ return None
51
+ return value if value >= 0 else None
52
+
53
+
54
+ class UsageTally:
55
+ """Provider-reported counts over a run of model calls.
56
+
57
+ `calls` is every call recorded, reported or not, because "reported on
58
+ 3 of 5 calls" needs both numbers. `totals` and `reported` hold a key
59
+ only once some call reported it, so an absent key IS "not reported".
60
+ """
61
+
62
+ def __init__(self):
63
+ self.calls = 0
64
+ self.totals: dict = {}
65
+ self.reported: dict = {}
66
+
67
+ def add(self, usage, unreported=()) -> None:
68
+ """One model call's usage dict, and the keys in it that the
69
+ provider did not send (`ModelResponse.usage_unreported`)."""
70
+ self.calls += 1
71
+ usage = usage if isinstance(usage, dict) else {}
72
+ for key, _label in ROWS:
73
+ if key in unreported:
74
+ continue
75
+ value = _count(usage.get(key))
76
+ if value is None:
77
+ continue
78
+ self.totals[key] = self.totals.get(key, 0) + value
79
+ self.reported[key] = self.reported.get(key, 0) + 1
80
+
81
+ def merge(self, other) -> None:
82
+ """Another tally's calls, added to this one's. A None or a
83
+ non-tally is nothing to add: a response from a path that does not
84
+ set one (a test double, an older caller) carries none."""
85
+ if not isinstance(other, UsageTally):
86
+ return
87
+ self.calls += other.calls
88
+ for key, value in other.totals.items():
89
+ self.totals[key] = self.totals.get(key, 0) + value
90
+ for key, value in other.reported.items():
91
+ self.reported[key] = self.reported.get(key, 0) + value
92
+
93
+
94
+ def report_lines(tally) -> list:
95
+ """The lines `/usage` prints, in either shell."""
96
+ if not isinstance(tally, UsageTally):
97
+ tally = UsageTally()
98
+ head = "usage · this thread since it was opened"
99
+ if not tally.calls:
100
+ return [head + " · no model calls yet"]
101
+ calls = "1 call" if tally.calls == 1 else f"{tally.calls:,} calls"
102
+ lines = [f"{head} · {calls}"]
103
+ figures = {key: format(tally.totals[key], ",")
104
+ for key, _label in ROWS if tally.reported.get(key)}
105
+ width = max((len(text) for text in figures.values()), default=0)
106
+ label_width = max(len(label) for _key, label in ROWS)
107
+ for key, label in ROWS:
108
+ name = label.ljust(label_width)
109
+ if key not in figures:
110
+ lines.append(f" {name} not reported")
111
+ continue
112
+ line = f" {name} {figures[key].rjust(width)}"
113
+ seen = tally.reported[key]
114
+ if seen < tally.calls:
115
+ line += f" (reported on {seen:,} of {tally.calls:,} calls)"
116
+ lines.append(line)
117
+ if any(key in figures for key in CACHE_KEYS):
118
+ lines.append(" cache read and cache write are part of input")
119
+ return lines
@@ -104,7 +104,7 @@ packages on the server and sending their assets to browsers are distinct paths.
104
104
  | textual-serve | `==1.1.3` | MIT | Yes | `deploy/hosted/requirements.txt`; its browser assets only, its server never runs |
105
105
  | uv | `==0.13.4` | MIT OR Apache-2.0 | Yes | `deploy/hosted/requirements.txt`; builds the session environment |
106
106
  | websockets | `==17.3` | BSD-3-Clause | Yes | `deploy/hosted/requirements.txt`; the websocket upgrade every session is |
107
- | gradio | `==6.30.0` | Apache-2.0 | Yes | installed by the Space at the README's `sdk_version`; in the Studio's `bridge.lock` |
107
+ | gradio | `==6.32.0` | Apache-2.0 | Yes | installed by the Space at the README's `sdk_version`; in the Studio's `bridge.lock` |
108
108
  | spaces | `==0.51.3` | Apache-2.0 | Yes | installed by the Space itself; in the Studio's `bridge.lock` |
109
109
 
110
110
  ### Browser assets
package/logging_setup.py CHANGED
@@ -10,6 +10,9 @@ narrow ways, by explicit product decision:
10
10
  retries survive CLI exit and process close.
11
11
  2. Level is env-overridable via AGENT_LOG_LEVEL instead of a hardcoded
12
12
  logging.INFO default. AGENT_LOG_FILE overrides the file path.
13
+ 3. The stderr handler shows WARNING and above unless a level is asked
14
+ for (batch 266), so the CLI's terminal follows the TUI transcript's
15
+ rule while the file keeps INFO. See `_screen_level`.
13
16
 
14
17
  Idempotent on repeated calls so test runs that invoke configure_logging
15
18
  more than once don't stack duplicate handlers and double-emit lines.
@@ -77,8 +80,9 @@ class _RedactingFormatter(logging.Formatter):
77
80
  the message, the arguments and the traceback in one pass.
78
81
 
79
82
  Fixed here rather than at dispatch's call site because dispatch is
80
- not the only site: `fetch_url` logs a warning naming the URL it
81
- failed on, and any future `logger.*` inherits this for free. Fixing
83
+ not the only site: `fetch_url` logs a line naming the URL it failed
84
+ on (INFO since batch 266, so this file is where it lands), and any
85
+ future `logger.*` inherits this for free. Fixing
82
86
  the one site the issue measured would have left the class open --
83
87
  the same producer-vs-consumer argument #47 makes one file over.
84
88
  """
@@ -203,6 +207,30 @@ def _resolve_level(level):
203
207
  return int(level)
204
208
 
205
209
 
210
+ def _screen_level(level) -> int:
211
+ """The stderr handler's own level: WARNING, unless a level was asked for.
212
+
213
+ Batch 266, the owner's choice. The handler used to carry no level, so
214
+ it printed whatever the root did -- INFO by default -- and the CLI's
215
+ terminal showed every INFO line in the program ("MCP server connected",
216
+ cache hits, compaction) while the TUI's transcript shows WARNING and
217
+ above (`TranscriptLogHandler`). It also showed a tool failure twice:
218
+ once as the tool's own log line and once as the result line the shell
219
+ prints for it. INFO is the file's level; the screen's is WARNING, in
220
+ both shells.
221
+
222
+ ASKED FOR means a caller passed `level`, or AGENT_LOG_LEVEL is set.
223
+ Then the handler follows the root as before, so `AGENT_LOG_LEVEL=DEBUG`
224
+ still puts DEBUG on the terminal -- the route `main.py` documents for
225
+ a configuration failure's traceback. A set but unreadable value counts
226
+ as asked for: `_resolve_level` has just printed that it runs at INFO,
227
+ and the terminal then shows what that sentence said.
228
+ """
229
+ if level is not None or os.environ.get("AGENT_LOG_LEVEL") is not None:
230
+ return logging.NOTSET
231
+ return logging.WARNING
232
+
233
+
206
234
  def configure_logging(
207
235
  level=None,
208
236
  *,
@@ -239,6 +267,10 @@ def configure_logging(
239
267
  rotating file keeps everything at full detail either way, and
240
268
  tui/app.py additionally routes WARNING+ into the transcript so
241
269
  nothing that matters is only in a file.
270
+
271
+ The handler shows WARNING and above unless a level was asked for
272
+ (batch 266, the owner's choice): the TUI's transcript rule, so
273
+ both shells put the same lines on screen. See `_screen_level`.
242
274
  """
243
275
  resolved_level = _resolve_level(level)
244
276
  formatter = _RedactingFormatter(_DEFAULT_FORMAT, datefmt=_DEFAULT_DATEFMT)
@@ -260,6 +292,7 @@ def configure_logging(
260
292
  if stderr:
261
293
  stderr_handler = logging.StreamHandler(stream=sys.stderr)
262
294
  stderr_handler.setFormatter(formatter)
295
+ stderr_handler.setLevel(_screen_level(level))
263
296
  root.addHandler(stderr_handler)
264
297
 
265
298
  if log_file is None: