@yahyahammad/venastine 0.3.15 → 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.
@@ -75,7 +75,8 @@ def available() -> bool:
75
75
 
76
76
  _TIMEOUT_DESCRIPTION = (
77
77
  "The longest this session may run, in seconds. Pick what the work needs: "
78
- "while it runs the user's prompt is blocked. Above "
78
+ "while it runs the user can still message you but cannot switch thread "
79
+ "or model. Above "
79
80
  f"{config.SHELL_SESSION_TIMEOUT_CAP_S} it is capped at "
80
81
  f"{config.SHELL_SESSION_TIMEOUT_CAP_S} and the result says so. When it is "
81
82
  "reached the command is stopped and you are woken.")
@@ -215,8 +216,10 @@ class KillParams(BaseModel):
215
216
 
216
217
  _SHARED = (
217
218
  f"At most {config.SHELL_SESSION_MAX_LIVE} sessions run at once, shared with "
218
- "any subagents, and the user's prompt is blocked while one of yours runs. "
219
- "Read output with shell_output and stop a session with shell_kill.")
219
+ "any subagents. While one of yours runs the user can still message you: "
220
+ "each message is a turn of its own, and a result that lands meanwhile "
221
+ "wakes you after it. Read output with shell_output and stop a session "
222
+ "with shell_kill.")
220
223
 
221
224
 
222
225
  def _schema(name: str, description: str, model, required) -> dict:
@@ -360,7 +360,11 @@ def run(params: dict) -> dict:
360
360
  "The search provider is rate-limiting this harness. "
361
361
  "Wait a minute before searching again.")}
362
362
  last_exc = e
363
- logger.warning(
363
+ # INFO (batch 266, the owner's choice). The last attempt's
364
+ # error is the result below, which both shells print as a
365
+ # line of its own, and an attempt that a retry recovered from
366
+ # is nothing to show anyone. The file keeps every attempt.
367
+ logger.info(
364
368
  "web_search attempt %s/%s failed for %r: %s",
365
369
  attempt + 1, MAX_RETRIES + 1, parsed.query, e)
366
370
  else:
@@ -233,10 +233,11 @@ def _interpret(tool_name: str, stdout: str, stderr: str, returncode: int,
233
233
  return {"error": _limit_message(tool_name, timeout)}
234
234
 
235
235
  excerpt = (stderr or "").strip().replace("\n", " ")[-_STDERR_EXCERPT:]
236
- # logger.warning, not exception: there is no traceback here to keep,
237
- # and this path is reachable by ordinary input.
238
- logger.warning("%s produced no readable result (exit %s): %s",
239
- tool_name, returncode, excerpt)
236
+ # Not logger.exception: there is no traceback here to keep, and this
237
+ # path is reachable by ordinary input. INFO, not WARNING (batch 266):
238
+ # the result below says the same, and both shells print it.
239
+ logger.info("%s produced no readable result (exit %s): %s",
240
+ tool_name, returncode, excerpt)
240
241
  return {"error": f"{tool_name} did not return a usable result "
241
242
  f"(exit {returncode}). {excerpt}".strip()}
242
243
 
@@ -309,8 +310,10 @@ def run_isolated(handler, params: dict, timeout: float,
309
310
  # case, which shares this wording rather than repeating it.
310
311
  return {"error": _limit_message(tool_name, timeout)}
311
312
  except OSError as e:
312
- logger.warning("Could not start an isolated child for %s: %s",
313
- tool_name, e)
313
+ # INFO (batch 266): the result below says this, and both shells
314
+ # print it; the file keeps the line.
315
+ logger.info("Could not start an isolated child for %s: %s",
316
+ tool_name, e)
314
317
  return {"error": f"{tool_name} could not be run: {e}"}
315
318
  stdout, stdout_bytes = _head(out, MAX_CHILD_OUTPUT_BYTES + 1)
316
319
  stderr = _tail(err, 4 * _STDERR_EXCERPT)
package/tui/app.py CHANGED
@@ -187,6 +187,14 @@ DIFF_SNAPSHOT_MAX_BYTES = 512_000
187
187
  #: to guess at.
188
188
  QUIT_CONFIRM_S = 2.0
189
189
 
190
+ #: Where the turn worker's post-turn session wait is (ROADMAP_v3 §49, SS2
191
+ #: as amended in batch 265): between replies, where a message the user
192
+ #: sends is taken and gets a turn of its own, or inside a wake turn, where
193
+ #: it is refused as it is during any turn. None (`_wait_phase`'s default)
194
+ #: means the worker is not waiting at all.
195
+ _WAIT_IDLE = "idle"
196
+ _WAIT_WAKE = "wake"
197
+
190
198
 
191
199
  class LoopEventMessage(Message):
192
200
  """One LoopEvent, forwarded from the worker thread to the UI thread.
@@ -346,6 +354,22 @@ class SessionNote(Message):
346
354
  super().__init__()
347
355
 
348
356
 
357
+ class SessionWait(Message):
358
+ """The turn worker began (or stopped) waiting on its background
359
+ sessions (ROADMAP_v3 §97, UF1).
360
+
361
+ Posted by `_wait_for_sessions` for the meter, which runs on the UI
362
+ thread: the seconds between wake turns are the shell waiting, not the
363
+ model writing, so the completion line's rate leaves them out. FIFO from
364
+ the one worker, like SessionNote, so a wake turn's events always land
365
+ after the `False` that resumes the clock for them.
366
+ """
367
+
368
+ def __init__(self, waiting: bool) -> None:
369
+ self.waiting = waiting
370
+ super().__init__()
371
+
372
+
349
373
  class ConnectCodeReady(Message):
350
374
  """`/connect huggingface` has a device code to show (ROADMAP_v3 §79, HD5).
351
375
 
@@ -487,8 +511,9 @@ class SessionRowsChanged(Message):
487
511
  sink runs on the manager's threads and the handler on the UI thread,
488
512
  so a delta would have the panel reassembling an order it never saw.
489
513
 
490
- FILTERED ON THE UI SIDE, not here. The panel shows live sessions
491
- only (SS14), and doing that in the handler keeps this message a
514
+ FILTERED ON THE UI SIDE, not here. The panel shows the running
515
+ sessions and the five most recent finished ones (SS14, amended in
516
+ batch 258), and doing that in the handler keeps this message a
492
517
  faithful copy of what the manager said rather than a view of it --
493
518
  which is what lets a second reader be added without the first one's
494
519
  filter being in the way.
@@ -656,14 +681,45 @@ def _redacted_command(row) -> str:
656
681
  return check_output_policy(tool, {"command": row.command})["command"]
657
682
 
658
683
 
684
+ #: How many finished sessions the sidebar keeps under the running ones
685
+ #: (SS14 as amended in batch 258; the owner's choice of five). The
686
+ #: manager keeps twenty (`_FINISHED_KEPT`), so this is a view of what it
687
+ #: holds and never a reason for it to hold more.
688
+ PANEL_FINISHED_SHOWN = 5
689
+
690
+
691
+ def _panel_outcome(row) -> tuple[str, str]:
692
+ """A session's `(status, outcome)` for the panel: how it ended, in as
693
+ few cells as say it.
694
+
695
+ `done` is an exit of 0 and nothing else. A non-zero code, a kill, a
696
+ timeout and a start that failed are all `failed`, and the outcome
697
+ text tells them apart -- the code itself where there is one, because
698
+ `2` and `137` are what a person would go and look up.
699
+ """
700
+ if row.state in shell_sessions.LIVE_STATES:
701
+ return "running", ""
702
+ if row.state == shell_sessions.EXITED:
703
+ if row.return_code is None:
704
+ return "failed", "exited"
705
+ return ("done" if row.return_code == 0 else "failed",
706
+ str(row.return_code))
707
+ return "failed", {shell_sessions.KILLED: "killed",
708
+ shell_sessions.TIMED_OUT: "timed out",
709
+ shell_sessions.FAILED: "failed"}.get(row.state,
710
+ row.state)
711
+
712
+
659
713
  def _panel_rows(rows) -> list[dict]:
660
- """`SessionRow`s as the session panel's `{id, label}` pairs (SS14).
714
+ """`SessionRow`s as the session panel's rows (SS14).
661
715
 
662
716
  EIGHTEEN COLUMNS, so the label is the id and as much of the command
663
717
  as fits -- the widget elides, and what it elides from has to be the
664
718
  part that identifies the session rather than a prefix of `docker`.
665
719
  A monitor's match count rides along when there is one, because "it
666
720
  has matched something" is the whole reason a monitor is on screen.
721
+ A finished row adds its `status` and `outcome` (`_panel_outcome`),
722
+ which the widget fits before the command.
667
723
 
668
724
  Module-level beside `_kill_rows`, for its reason.
669
725
  """
@@ -671,8 +727,10 @@ def _panel_rows(rows) -> list[dict]:
671
727
  for row in rows:
672
728
  matches = (f" {row.match_count}✓"
673
729
  if row.kind == "monitor" and row.match_count else "")
730
+ status, outcome = _panel_outcome(row)
674
731
  out.append({"id": row.id,
675
- "label": f"{row.id}{matches} {_redacted_command(row)}"})
732
+ "label": f"{row.id}{matches} {_redacted_command(row)}",
733
+ "status": status, "outcome": outcome})
676
734
  return out
677
735
 
678
736
 
@@ -835,6 +893,20 @@ class VenastineApp(App):
835
893
  # setter runs on bare-built apps across the suite.
836
894
  _pending_restart = None
837
895
 
896
+ # Batch 265 (SS2 as amended). The post-turn session wait's phase
897
+ # (`_WAIT_IDLE`, `_WAIT_WAKE`, or None when the worker is not waiting)
898
+ # and the message the user sent while it was idle, which the wait hands
899
+ # to a turn of its own once the worker is free. Both change under
900
+ # `_wait_lock`, because the UI thread takes a message on the strength
901
+ # of the phase and the worker starts a wake on the strength of the
902
+ # message, and those two decisions must not cross. CLASS attributes for
903
+ # `_shutting_down`'s reason -- bare-built apps run the wait loop and the
904
+ # submit handler across the suite -- with a class-wide lock that
905
+ # __init__ replaces with the instance's own.
906
+ _wait_phase = None
907
+ _handed_message = None
908
+ _wait_lock = threading.Lock()
909
+
838
910
  # §79 (HD5). The stop signal of the one sign-in that may be waiting for
839
911
  # approval, or None. A CLASS attribute for `_shutting_down`'s reason:
840
912
  # `on_unmount` and `exit()` stop it, and both run on bare-built apps
@@ -853,6 +925,7 @@ class VenastineApp(App):
853
925
  _meter_timer = None
854
926
  _meter_last = None
855
927
  _last_turn_elapsed = None
928
+ _last_turn_generating = None
856
929
  _turn_output_tokens = None
857
930
  _history = None
858
931
  # ROADMAP_v3 §61 (CN9). The live turn's CancelToken, or None between
@@ -1070,6 +1143,9 @@ class VenastineApp(App):
1070
1143
  # Per-thread state: `reset()`'s list, beside `_spawn_threads`.
1071
1144
  self._session_calls: dict = {}
1072
1145
  self._permission_channel: queue.Queue | None = None
1146
+ # Batch 265: this app's own, rather than the class-wide default
1147
+ # bare-built apps share.
1148
+ self._wait_lock = threading.Lock()
1073
1149
  # Batch 61. BEFORE the `_busy` assignment below, which is a
1074
1150
  # property setter now and drives this object.
1075
1151
  self._meter = meters.TurnMeter()
@@ -1081,6 +1157,10 @@ class VenastineApp(App):
1081
1157
  #: at the exits, because `_busy` is also cleared on paths
1082
1158
  #: where no turn ever ran.
1083
1159
  self._last_turn_elapsed = None
1160
+ #: The same turn's seconds with no tool running and no session
1161
+ #: wait: the completion line's rate divides by this, `took` by
1162
+ #: the one above (ROADMAP_v3 §97, UF1).
1163
+ self._last_turn_generating = None
1084
1164
  #: The finished turn's OUTPUT tokens, off the terminal
1085
1165
  #: response. None where the provider reports no usage, which
1086
1166
  #: is twelve of the fifteen configured ones -- and None is
@@ -1611,7 +1691,13 @@ class VenastineApp(App):
1611
1691
  now = monotonic()
1612
1692
  if value:
1613
1693
  self._meter.start(now)
1694
+ # §97 (UF1). Summed per terminal response since this batch, so
1695
+ # it starts at the turn: a count left by a turn whose line was
1696
+ # never written must not join the next one's.
1697
+ self._turn_output_tokens = None
1614
1698
  else:
1699
+ # Read BEFORE stop, which closes the turn and zeroes it.
1700
+ self._last_turn_generating = self._meter.generating(now)
1615
1701
  self._last_turn_elapsed = self._meter.stop(now)
1616
1702
  self._sync_meter_timer()
1617
1703
  self._refresh_meter()
@@ -1630,9 +1716,18 @@ class VenastineApp(App):
1630
1716
  """
1631
1717
  return getattr(self._memory, "thread_id", None)
1632
1718
 
1633
- def refuse_if_occupied(self, action: str) -> bool:
1719
+ def refuse_if_occupied(self, action: str, *, by_sessions: bool = True
1720
+ ) -> bool:
1634
1721
  """Whether `action` must be refused right now, saying why.
1635
1722
 
1723
+ `by_sessions=False` is a plain MESSAGE's question since batch 265
1724
+ (SS2 as amended, the owner's choice): live sessions do not hold a
1725
+ message back, only a running turn does. A message sent while the
1726
+ wait is between replies never reaches this -- `_hand_to_wait`
1727
+ takes it first -- so what this still refuses for one is a turn
1728
+ actually running, a wake turn included, with the words any turn
1729
+ gets.
1730
+
1636
1731
  SS2 gives the shell a SECOND way to be occupied. Before this, one
1637
1732
  condition (`_busy`) was checked at eight sites with the same three
1638
1733
  lines copied between them; live background sessions add a
@@ -1654,8 +1749,12 @@ class VenastineApp(App):
1654
1749
  are typed into it.
1655
1750
  """
1656
1751
  thread_id = self._session_thread
1657
- if thread_id is not None and shell_sessions.sessions.blocks(thread_id):
1658
- live = shell_sessions.sessions.live_for(thread_id)
1752
+ if by_sessions and thread_id is not None \
1753
+ and shell_sessions.sessions.blocks(thread_id):
1754
+ # `waited_on`, not `live_for` (batch 265): an interactive
1755
+ # shell does not hold the shell (SS25), so it is not one of
1756
+ # the sessions this sentence asks the user to wait for.
1757
+ live = shell_sessions.sessions.waited_on(thread_id)
1659
1758
  plural = "" if live == 1 else "s"
1660
1759
  self._transcript.write_error(
1661
1760
  f"{live} background session{plural} still running — "
@@ -1667,6 +1766,46 @@ class VenastineApp(App):
1667
1766
  return True
1668
1767
  return False
1669
1768
 
1769
+ def _hand_to_wait(self, text: str) -> bool:
1770
+ """Take a message sent while the session wait is between replies
1771
+ (ROADMAP_v3 §49, SS2 as amended in batch 265). Whether it was taken.
1772
+
1773
+ TAKEN means it will run: the worker sees it, leaves the wait with
1774
+ the sessions still running, and `on_turn_finished` starts its turn
1775
+ -- a turn of its own, with its own clock, escape and held results,
1776
+ because that is what the owner asked for and because it IS a user
1777
+ turn. The results that land meanwhile wake after it (`put_back`,
1778
+ and the wait its own turn ends in).
1779
+
1780
+ NOT taken while a wake turn runs, or once one message is already
1781
+ waiting: the owner's choice is that a send then is refused as it
1782
+ is during any turn, and the caller asks the funnel for the words.
1783
+
1784
+ UNDER `_wait_lock`, beside the worker's own read of it: the phase
1785
+ is what this decides on, and the worker decides to start a wake on
1786
+ the strength of what this writes. The nudge is outside the lock.
1787
+ The order, wherever both are held, is `_wait_lock` and then the
1788
+ manager's (the worker's `put_back`); `wait_for_wake` reads
1789
+ `_handed_message` under the manager's alone, with no lock of ours.
1790
+ """
1791
+ with self._wait_lock:
1792
+ if self._wait_phase != _WAIT_IDLE or \
1793
+ self._handed_message is not None:
1794
+ return False
1795
+ self._handed_message = text
1796
+ shell_sessions.sessions.nudge()
1797
+ return True
1798
+
1799
+ def _message_is_waiting(self) -> bool:
1800
+ """`wait_for_wake`'s stop: a plain attribute read, because it is
1801
+ called under the manager's lock."""
1802
+ return self._handed_message is not None
1803
+
1804
+ def _take_handed_message(self):
1805
+ with self._wait_lock:
1806
+ text, self._handed_message = self._handed_message, None
1807
+ return text
1808
+
1670
1809
  def _sync_meter_timer(self) -> None:
1671
1810
  """Tick only while a turn is running (ThinkingIndicator's rule).
1672
1811
 
@@ -1747,9 +1886,12 @@ class VenastineApp(App):
1747
1886
  if elapsed is None:
1748
1887
  # `_busy` was cleared without a turn having run.
1749
1888
  return
1889
+ generating = self._last_turn_generating
1890
+ self._last_turn_generating = None
1750
1891
  self._transcript.write_role(
1751
1892
  'system',
1752
- self._meter.completion(elapsed, self._turn_output_tokens))
1893
+ self._meter.completion(elapsed, self._turn_output_tokens,
1894
+ generating))
1753
1895
  self._turn_output_tokens = None
1754
1896
 
1755
1897
  def refresh_status(self) -> None:
@@ -2439,13 +2581,15 @@ class VenastineApp(App):
2439
2581
  self.open_session(str(message.session_id))
2440
2582
 
2441
2583
  def open_session(self, session_id: str) -> None:
2442
- """Show a LIVE session, read-only (SS14).
2584
+ """Show a session this process holds, read-only (SS14).
2443
2585
 
2444
2586
  The panel's route, and the only one that starts from a session
2445
2587
  id: the panel draws what the manager holds, so by definition
2446
- this process has the session. A row that has finished between
2447
- the draw and the click is the one race, and it is answered the
2448
- way a missing thread is -- said, not ignored.
2588
+ this process has the session -- running, or one of the finished
2589
+ rows the panel keeps since batch 258, whose output the manager
2590
+ still has. A row that aged out of the manager between the draw
2591
+ and the click is the one race, and it is answered the way a
2592
+ missing thread is -- said, not ignored.
2449
2593
  """
2450
2594
  row = shell_sessions.sessions.row(session_id)
2451
2595
  if row is None:
@@ -3046,8 +3190,17 @@ class VenastineApp(App):
3046
3190
  # Clear AFTER the busy check, not before. /research holds _busy
3047
3191
  # for a whole ten-pass pipeline, so a follow-up typed during one
3048
3192
  # was wiped and had to be retyped from memory.
3049
- if not text.startswith("/") and self.refuse_if_occupied(
3050
- "send a message"):
3193
+ #
3194
+ # BATCH 265 (SS2 as amended, the owner's choice). A message sent
3195
+ # while the turn's background sessions run is no longer held back
3196
+ # by them: between replies the wait takes it and it gets a turn of
3197
+ # its own, and during a reply it is refused as during any turn.
3198
+ # Taken FIRST, because `_busy` is true through the whole wait and
3199
+ # the funnel would otherwise refuse it for that.
3200
+ handed = (not text.startswith("/")
3201
+ and self._hand_to_wait(text))
3202
+ if not text.startswith("/") and not handed and \
3203
+ self.refuse_if_occupied("send a message", by_sessions=False):
3051
3204
  return
3052
3205
  event.prompt.value = ""
3053
3206
  # Batch 63. AFTER the busy refusal above, which returns without
@@ -3064,6 +3217,11 @@ class VenastineApp(App):
3064
3217
  # reason. Guarded on the transition because that is the only
3065
3218
  # time the answer changes: a history never empties.
3066
3219
  self.refresh_bindings()
3220
+ if handed:
3221
+ # The worker leaves the wait and `on_turn_finished` runs it,
3222
+ # so nothing is drawn here: its `you ›` line is the turn's
3223
+ # own, written when the turn starts and not before.
3224
+ return
3067
3225
  if text.startswith("/"):
3068
3226
  name = text[1:].split(" ")[0]
3069
3227
  # CONTAINED HERE, not inside CommandRegistry.dispatch, and the
@@ -3279,8 +3437,11 @@ class VenastineApp(App):
3279
3437
 
3280
3438
  The wait is INSIDE the worker and before `TurnFinished`, which is
3281
3439
  what keeps `_busy` true across it. That is not a side effect but
3282
- the mechanism: SS2 wants input blocked while sessions are live, and
3283
- `_busy` already blocks it everywhere `refuse_if_occupied` asks.
3440
+ the mechanism: SS2 holds the commands that would change what the
3441
+ wait is waiting for, and `_busy` already refuses them everywhere
3442
+ `refuse_if_occupied` asks. A plain message is the exception since
3443
+ batch 265 (SS2 as amended): `_hand_to_wait` takes it between
3444
+ replies, and it gets a turn of its own once this frame returns.
3284
3445
  """
3285
3446
  error = None
3286
3447
  try:
@@ -3311,12 +3472,38 @@ class VenastineApp(App):
3311
3472
  event like any other, and the loop keeps going until nothing is
3312
3473
  live and nothing is pending, so the results of what the user
3313
3474
  stopped are still delivered rather than dropped on the floor.
3475
+
3476
+ A MESSAGE DOES (batch 265, SS2 as amended). One sent between replies
3477
+ is taken by `_hand_to_wait`, and the loop leaves with the sessions
3478
+ still running so `on_turn_finished` can give it a turn of its own;
3479
+ that turn ends in this loop again, which is where what landed in
3480
+ the meantime wakes. The phase says which side of a reply the loop
3481
+ is on, and it is set under `_wait_lock` because the UI thread takes
3482
+ a message on the strength of it.
3314
3483
  """
3484
+ with self._wait_lock:
3485
+ self._wait_phase = _WAIT_IDLE
3486
+ try:
3487
+ self._wait_until_done(thread_id, wake)
3488
+ finally:
3489
+ with self._wait_lock:
3490
+ self._wait_phase = None
3491
+
3492
+ def _wait_until_done(self, thread_id, wake) -> None:
3493
+ """`_wait_for_sessions`'s loop, with its phase already set."""
3315
3494
  from core.session_wake import build_wake
3316
3495
 
3317
3496
  sessions = shell_sessions.sessions
3497
+ # ROADMAP_v3 §97 (UF1). Whether the meter has been told this stretch
3498
+ # is a wait, so it is told once per stretch rather than per second.
3499
+ waiting = False
3318
3500
  while True:
3319
- if not sessions.live_for(thread_id) and not sessions.pending(thread_id):
3501
+ # `waited_on`, not `live_for` (batch 265): an interactive shell
3502
+ # is not waited on (SS25), and `wait_for_wake` already answers
3503
+ # None at once for one -- so counting it here spun this loop
3504
+ # for as long as the shell stayed open.
3505
+ if not sessions.waited_on(thread_id) and \
3506
+ not sessions.pending(thread_id):
3320
3507
  return
3321
3508
  # BATCH 145. The SAME rule as `sessions.closing`
3322
3509
  # below, one step earlier in the same teardown: the flag means
@@ -3373,7 +3560,18 @@ class VenastineApp(App):
3373
3560
  "and their results will arrive with your next "
3374
3561
  "message —"))
3375
3562
  return
3376
- batch = sessions.wait_for_wake(thread_id, timeout=1.0)
3563
+ if self._message_is_waiting():
3564
+ # Batch 265. The user's message is next, and this loop is
3565
+ # not where it runs: `on_turn_finished` gives it a turn.
3566
+ # The meter may be left paused (§97 UF1), as on every
3567
+ # return below: TurnFinished stops it, and the message's
3568
+ # turn starts a fresh one.
3569
+ return
3570
+ if not waiting:
3571
+ self.post_message(SessionWait(True))
3572
+ waiting = True
3573
+ batch = sessions.wait_for_wake(
3574
+ thread_id, timeout=1.0, stop=self._message_is_waiting)
3377
3575
  if batch is None:
3378
3576
  if sessions.suspended(thread_id):
3379
3577
  # SS2's consecutive-wake limit. The block lifts here,
@@ -3387,7 +3585,19 @@ class VenastineApp(App):
3387
3585
  "message —"))
3388
3586
  return
3389
3587
  continue
3588
+ with self._wait_lock:
3589
+ if self._handed_message is not None:
3590
+ # Batch 265. The message and this batch reached the
3591
+ # shell together, and the owner's rule is that the
3592
+ # message goes first: the batch goes back, and wakes
3593
+ # after the turn the message gets.
3594
+ sessions.put_back(thread_id, batch)
3595
+ return
3596
+ self._wait_phase = _WAIT_WAKE
3390
3597
  text, record = build_wake(batch)
3598
+ # §97 (UF1). The wake turn is the model writing again.
3599
+ self.post_message(SessionWait(False))
3600
+ waiting = False
3391
3601
  # ITS FIRST LINE, matching core/replay.py's rule for the same
3392
3602
  # row: the rest is program output the model was given, which
3393
3603
  # can run to thousands of lines and would bury the
@@ -3398,10 +3608,19 @@ class VenastineApp(App):
3398
3608
  "wake", text.split("\n", 1)[0].strip()))
3399
3609
  for event in wake(text, record):
3400
3610
  self.post_message(LoopEventMessage(event))
3611
+ with self._wait_lock:
3612
+ self._wait_phase = _WAIT_IDLE
3401
3613
 
3402
3614
  def on_session_note(self, message: SessionNote) -> None:
3403
3615
  self._transcript.write_role(message.role, message.text)
3404
3616
 
3617
+ def on_session_wait(self, message: SessionWait) -> None:
3618
+ """ROADMAP_v3 §97 (UF1): pause or resume the rate's clock."""
3619
+ if self._meter is None:
3620
+ return
3621
+ self._meter.set_waiting(message.waiting, monotonic())
3622
+ self._refresh_meter()
3623
+
3405
3624
  def _deliver_held(self, thread_id) -> None:
3406
3625
  """Results held for the user's next message (§49, SS2/SS19): past
3407
3626
  the consecutive-wake limit, or from a kill the user made between
@@ -3651,9 +3870,9 @@ class VenastineApp(App):
3651
3870
  def on_session_rows_changed(self, message: SessionRowsChanged) -> None:
3652
3871
  """The session sink spoke. Runs on the UI thread (§49, SS14)."""
3653
3872
  self.refresh_session_panel(message.rows)
3654
- # SS14: the panel shows live sessions only, so a session that
3655
- # just finished leaves it -- and the view of it, if one is open,
3656
- # is now showing a state that has moved. Repainted here because
3873
+ # SS14: a session that just finished changes its row's mark --
3874
+ # and the view of it, if one is open, is now showing a state
3875
+ # that has moved. Repainted here because
3657
3876
  # this is the only signal: the poll stops the moment the session
3658
3877
  # stops being live, so without this the view would keep the last
3659
3878
  # tick's "running" header for good.
@@ -3675,13 +3894,23 @@ class VenastineApp(App):
3675
3894
  `refresh_agent_panel`'s reason: two writers of one widget is how
3676
3895
  the two come to draw it differently.
3677
3896
 
3678
- THE FILTER IS HERE, which is SS14's "live sessions only" -- the
3679
- message carries everything the manager holds, so this is the one
3680
- place that decides what a reader sees.
3897
+ THE FILTER IS HERE, which is SS14's -- the message carries
3898
+ everything the manager holds, so this is the one place that
3899
+ decides what a reader sees. Since batch 258 that is every running
3900
+ session as it started, then the PANEL_FINISHED_SHOWN most recent
3901
+ finished ones, newest first. It was running only, and a session
3902
+ then left the panel at the moment it ended -- which is also the
3903
+ moment its wake starts the agent's next turn, so the row seemed
3904
+ to be taken by the turn.
3681
3905
  """
3682
3906
  live = [row for row in rows
3683
3907
  if row.state in shell_sessions.LIVE_STATES]
3684
- self._session_panel.show(_panel_rows(live))
3908
+ finished = sorted(
3909
+ (row for row in rows
3910
+ if row.state not in shell_sessions.LIVE_STATES),
3911
+ key=lambda row: row.finished_mono or 0.0,
3912
+ reverse=True)[:PANEL_FINISHED_SHOWN]
3913
+ self._session_panel.show(_panel_rows(live) + _panel_rows(finished))
3685
3914
 
3686
3915
  @property
3687
3916
  def _session_panel(self) -> SessionPanel:
@@ -4010,8 +4239,17 @@ class VenastineApp(App):
4010
4239
  # input deltas a tool-using turn brings in). Dividing
4011
4240
  # either by a duration yields a number that looks like
4012
4241
  # tok/s and is not.
4013
- self._turn_output_tokens = getattr(
4242
+ #
4243
+ # SUMMED, since ROADMAP_v3 §97 (UF1): a turn held open for its
4244
+ # background sessions ends with one line, after every wake
4245
+ # turn, and each wake turn is a run of its own with its own
4246
+ # terminal response. Assigning kept only the LAST wake turn's
4247
+ # count, divided by a time that covered them all.
4248
+ reported = getattr(
4014
4249
  event.final_response, 'turn_output_tokens', None)
4250
+ if reported is not None:
4251
+ self._turn_output_tokens = (
4252
+ (self._turn_output_tokens or 0) + reported)
4015
4253
  transcript.flush_stream()
4016
4254
  if event.stop_reason and event.stop_reason != "complete":
4017
4255
  transcript.write_system(f"[stopped early: {event.stop_reason}]")
@@ -4365,7 +4603,16 @@ class VenastineApp(App):
4365
4603
  for worker in self.workers)
4366
4604
 
4367
4605
  def _fire_pending_restart(self) -> None:
4368
- """The queued restart, once the turn that blocked it has ended."""
4606
+ """The queued restart, once the turn that blocked it has ended.
4607
+
4608
+ BATCH 265. Not while a turn runs: a message sent during the session
4609
+ wait starts its turn in `on_turn_finished`, the same pass that
4610
+ scheduled this, so by the time it fires another turn may already
4611
+ be running. Left queued, it fires when that turn ends -- the
4612
+ `_busy` setter schedules it again.
4613
+ """
4614
+ if self._busy_state:
4615
+ return
4369
4616
  key, self._pending_restart = self._pending_restart, None
4370
4617
  if key is None or self._shutting_down:
4371
4618
  return
@@ -5037,6 +5284,16 @@ class VenastineApp(App):
5037
5284
  self._raven.state = ravens.IDLE
5038
5285
  if message.error is not None:
5039
5286
  self._write_failure(message.error)
5287
+ # Batch 265 (SS2 as amended). A message the user sent while this
5288
+ # turn waited on its sessions gets its turn NOW, as the last thing
5289
+ # here: the line under the answer above is the finished turn's, and
5290
+ # on the UI thread nothing else can run between this and the turn
5291
+ # starting. After a failure too -- the box was cleared when it was
5292
+ # taken, so dropping it here would lose what the user wrote. Not on
5293
+ # the way out, where nothing will answer it.
5294
+ handed = self._take_handed_message()
5295
+ if handed is not None and not self._shutting_down:
5296
+ self.run_agent_turn(handed)
5040
5297
 
5041
5298
  def _write_failure(self, error, label: str = "error") -> None:
5042
5299
  """A turn, a one-shot or a research run that failed, on one line.
@@ -6194,6 +6451,22 @@ def _cmd_network(app: VenastineApp, args: str) -> None:
6194
6451
  app.refresh_network_line(force=True)
6195
6452
 
6196
6453
 
6454
+ def _cmd_usage(app: VenastineApp, args: str) -> None:
6455
+ """ROADMAP_v3 §97 (UF4): this thread's provider-reported counts.
6456
+
6457
+ The lines are `core.usage_view.report_lines`'s, which the CLI prints
6458
+ too, over the tally `ConversationMemory.record_usage` keeps beside
6459
+ `billed` -- so this and the usage line cover one span. `_memory`, not
6460
+ `memory`: a thread with no turn yet has nothing to count, and the
6461
+ public property would write a thread row to say so.
6462
+ """
6463
+ from core.usage_view import report_lines
6464
+
6465
+ memory = app._memory
6466
+ for line in report_lines(getattr(memory, "usage", None)):
6467
+ app._transcript.write_system(line)
6468
+
6469
+
6197
6470
  def _cmd_trigger(app: VenastineApp, args: str) -> None:
6198
6471
  """Override the working-set compaction ceiling for this session.
6199
6472
 
@@ -8369,6 +8642,7 @@ def register_builtin_commands() -> None:
8369
8642
  bind("window", _cmd_window),
8370
8643
  bind("trigger", _cmd_trigger),
8371
8644
  bind("network", _cmd_network),
8645
+ bind("usage", _cmd_usage),
8372
8646
  bind("classifier", _cmd_classifier),
8373
8647
  bind("config", _cmd_config, complete=_config_rows),
8374
8648
  bind("secrets", _cmd_secrets),