@lmmslab/harness 0.82.0 → 0.84.0

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.
Files changed (39) hide show
  1. package/README.md +2 -2
  2. package/bin/harness.js +6 -1
  3. package/package.json +1 -1
  4. package/python/harness/__init__.py +1 -1
  5. package/python/harness/builtin/prompts/core/core.md +61 -30
  6. package/python/harness/builtin/prompts/reflect/core.md +3 -12
  7. package/python/harness/builtin/prompts/reflect/pack.toml +3 -2
  8. package/python/harness/builtin/prompts/shared/learning-goal.md +1 -1
  9. package/python/harness/cli.py +104 -18
  10. package/python/harness/config/agent.py +3 -0
  11. package/python/harness/core/trace.py +23 -3
  12. package/python/harness/core/worker.py +6 -0
  13. package/python/harness/host/client.py +26 -21
  14. package/python/harness/host/http.py +11 -1
  15. package/python/harness/host/sessions.py +38 -3
  16. package/python/harness/host/tasks.py +62 -26
  17. package/python/harness/model_list.py +25 -5
  18. package/python/harness/packages/environments.py +51 -12
  19. package/python/harness/packages/manager.py +379 -102
  20. package/python/harness/packages/resolve.py +52 -15
  21. package/python/harness/packages/sources.py +17 -5
  22. package/python/harness/packages/state.py +10 -0
  23. package/python/harness/providers.py +66 -24
  24. package/python/harness/runtime/decision/loop.py +18 -9
  25. package/python/harness/runtime/decision/messages.py +5 -10
  26. package/python/harness/runtime/decision/model.py +21 -11
  27. package/python/harness/runtime/decision/notices.py +66 -0
  28. package/python/harness/runtime/execution/entries.py +53 -7
  29. package/python/harness/runtime/execution/session.py +60 -7
  30. package/python/harness/runtime/learning/active.py +12 -9
  31. package/python/harness/runtime/reactive/tree.py +35 -22
  32. package/python/harness/runtime/task/run.py +259 -162
  33. package/python/harness/runtime/tick.py +34 -3
  34. package/python/harness/settings.py +8 -3
  35. package/python/harness/tui/__init__.py +28 -6
  36. package/python/harness/tui/chat.py +243 -67
  37. package/python/harness/tui/pty.py +16 -0
  38. package/python/harness/tui/renderer.py +34 -0
  39. package/python/harness/tui/views.py +160 -38
@@ -3,8 +3,11 @@
3
3
  A conversation starts with one model. Every program the model writes runs as a unit of its
4
4
  own in the kernel (runtime/execution/kernel.py): the model's call comes back at once, with
5
5
  the program's result when it was over within a short grace, else with its handle, and the
6
- result reaches the model when the unit ends. A message, an unhandled event and a unit's
7
- end are events to the model; a reply made before one of them is set aside.
6
+ result reaches the model when the unit ends. What reaches the model by itself (a message,
7
+ an event, a unit's end, a note of the host) is a notice in one queue with a priority
8
+ (runtime/decision/notices.py): `now` sets a reply made before it aside, `next` is told with
9
+ the next call, `later` after the model's next wait. One place tells the model with each
10
+ call (_deliver): what its last reply came to, the notices, and how things stand now.
8
11
 
9
12
  A turn is the model's work from what started it (the instruction, a message, a unit's end,
10
13
  an event) to its answer: a reply that is text, with no program and no wait (<finish> and a
@@ -34,6 +37,7 @@ import uuid
34
37
 
35
38
  from harness.runtime.decision import loop as decisions
36
39
  from harness.runtime.decision.messages import _inbox_parts, content_parts
40
+ from harness.runtime.decision.notices import LATER, NEXT, NOW, Notice, Notices
37
41
  from harness.runtime.execution.results import _taken_over, _cell_usage, _wrap_up
38
42
  from harness.runtime.safety.approval import CONFIRMATION_POLICIES
39
43
  from harness.runtime.status import Closed, Interrupted
@@ -52,8 +56,8 @@ from harness.runtime.task import context as memory_view
52
56
  from harness.runtime.task.context import WorkingMemory
53
57
  from harness.runtime.learning.usage import fold_uses
54
58
  from harness.runtime.decision.messages import (EMBODIMENT_TEXT, attachment_parts, cell_feedback, image_parts,
55
- prune_images, prune_views, state_text, text_only, unit_feedback,
56
- unit_text, view_parts)
59
+ prune_images, prune_views, state_text, steps_text, text_only,
60
+ unit_feedback, unit_text, view_parts)
57
61
  from harness.runtime.events import EVENTS
58
62
  from harness.runtime.status import ERROR, FINISHED, IDLE, PAUSED, RUNNING, STATUSES, WAITING_FOR_CONFIRMATION
59
63
  from harness.runtime.learning.active import sandbox_of
@@ -145,6 +149,11 @@ class TaskRun:
145
149
  grace = config.runtime.get("turn_grace_seconds", decisions.TURN_GRACE_SECONDS)
146
150
  if isinstance(grace, bool) or not isinstance(grace, (int, float)) or grace < 0:
147
151
  raise ValueError(f"runtime.turn_grace_seconds must be a number of seconds >= 0, got {grace!r}")
152
+ told = config.runtime.get("tell_budget", "never")
153
+ if told not in decisions.TELL_BUDGET:
154
+ raise ValueError(f"runtime.tell_budget must be one of {decisions.TELL_BUDGET}, got {told!r}")
155
+ # Whether the model is told what the run has left (its calls, its steps, its time).
156
+ self._budget_told = told == "always"
148
157
  longest = config.runtime.get("longest_wait_seconds", decisions.LONGEST_WAIT_SECONDS)
149
158
  if isinstance(longest, bool) or not isinstance(longest, (int, float)) or longest <= 0:
150
159
  raise ValueError(f"runtime.longest_wait_seconds must be a number of seconds > 0, got {longest!r}")
@@ -188,7 +197,21 @@ class TaskRun:
188
197
  self.episode = None
189
198
  self.summary = None
190
199
  self._condition = threading.Condition()
191
- self._inbox = []
200
+ # What reaches the model by itself: one queue (runtime/decision/notices.py). With it:
201
+ # what the model's last output came to (told first with its next call), the
202
+ # priority down to which that call is told the queue, the kinds of notices told
203
+ # that no reply has answered yet with the events among them, how many notices
204
+ # set a call aside, and the state as the kernel last reported it.
205
+ self._notices = Notices()
206
+ self._outcome = []
207
+ self._upto = LATER
208
+ self._unanswered = set()
209
+ self._told_events = []
210
+ # The events told and not answered yet, by (kind, source): one that comes again
211
+ # meanwhile tells nothing new.
212
+ self._told_sources = set()
213
+ self._urgent = 0
214
+ self._state_now = None
192
215
  self._pause = False
193
216
  self._closing = False
194
217
  # The user asked to end the turn that runs (cancel()).
@@ -203,12 +226,10 @@ class TaskRun:
203
226
  self._program_span = None
204
227
  # The model's units that run ({unit id: {"name", "kind", "cell", "started"}}), its
205
228
  # programs whose result has not come ({the kernel's cell: {"number", "code", "action",
206
- # "span", "unit"}}),
207
- # the results of the units that ended and the model has not been told, and how
208
- # long a program may hold the model's turn before the turn comes back with its handle.
229
+ # "span", "unit"}}), and how long a program may hold the model's turn before the
230
+ # turn comes back with its handle.
209
231
  self._units = {}
210
232
  self._programs = {}
211
- self._unit_news = []
212
233
  self._grace = float(grace)
213
234
  # The longest a <wait> of the model lasts.
214
235
  self._longest_wait = float(longest)
@@ -218,10 +239,8 @@ class TaskRun:
218
239
  self._post_from = None
219
240
  self._request = None
220
241
  self._revision = 0
221
- self._pending_events = {}
222
242
  self._inbound = None # the listener its worker tells through (runtime/task/inbound.py)
223
243
  self._seen_events = set()
224
- self._reported_events = set()
225
244
  self._thoughts = ThoughtQueue(self._condition, on_exit=self._came_back, trace=self.trace)
226
245
  self._cancelled_origins = set()
227
246
  self._logged = 0
@@ -292,8 +311,9 @@ class TaskRun:
292
311
 
293
312
  def send_message(self, text: str, sender: str = "user", files=()) -> None:
294
313
  """A message from the user (or, `sender` "parent", from the agent that started this
295
- one): recorded now, sent with the model's next call. `files`: paths the user attached
296
- (read now: messages.attachment_parts)."""
314
+ one): recorded now, told with the model's next call (a notice of priority `next`:
315
+ the call in flight goes on; cancel() is what ends a turn). `files`: paths the user
316
+ attached (read now: messages.attachment_parts)."""
297
317
  text, files = str(text).strip(), [str(path) for path in files or ()]
298
318
  if not text and not files:
299
319
  return
@@ -301,7 +321,6 @@ class TaskRun:
301
321
  with self._condition:
302
322
  if self.status in (FINISHED, ERROR):
303
323
  raise RuntimeError("The episode has ended")
304
- self._inbox.append((sender, text, parts))
305
324
  said = " ".join([text] + [f"@{path}" for path in files]).strip()
306
325
  self._said.append(said if sender == "user" else f"(from the agent that started you) {said}")
307
326
  self.trace.emit("user_message", text=text, **({"files": files} if files else {}),
@@ -315,8 +334,10 @@ class TaskRun:
315
334
  self._worker_approvals.pop(request["id"])
316
335
  self._physical = None
317
336
  self._answer_physical(request, False, "once", "superseded by a user message")
318
- self._preempt("user message")
319
- self._condition.notify_all()
337
+ # (The roles' calls that judged without it are stale.)
338
+ self._thoughts.cancel_where(
339
+ lambda item: item.role in ("verifier", "observer", "vlm", "pointer", "reflection"), "user message")
340
+ self._put("message", NEXT, (sender, text, parts))
320
341
 
321
342
  def pause(self) -> None:
322
343
  """Pause: every unit of the model stops at its next execution step or wait, and
@@ -701,6 +722,10 @@ class TaskRun:
701
722
  # How the tree of reflexes stands (runtime/reactive/tree.py tell): told with every turn.
702
723
  with self._condition:
703
724
  self._reflex_nodes = dict(record.get("nodes") or {})
725
+ elif note == "state":
726
+ # The state as the kernel last had it (runtime/execution/session.py): told with every call.
727
+ with self._condition:
728
+ self._state_now = record.get("state")
704
729
  elif note == "preempted":
705
730
  self._taken(record)
706
731
  elif note == "held_back":
@@ -1074,8 +1099,8 @@ class TaskRun:
1074
1099
  def tell(self, content, source: str | None = None) -> None:
1075
1100
  """The environment tells the model something that happened by itself: a message, in
1076
1101
  the form conversations give content (a string, or a list of text and image parts:
1077
- decision/messages.py content_parts). It reaches the model at once as a message does:
1078
- a wait ends, a call in flight is set aside and asked again; the model reads it as
1102
+ decision/messages.py content_parts). It reaches the model at once (a notice of
1103
+ priority `now`): a wait ends, a call in flight is set aside and asked again; the model reads it as
1079
1104
  "Message from the environment". Called by whoever knows of it: the run's worker
1080
1105
  through its listener (runtime/task/inbound.py), or the code that owns this run."""
1081
1106
  parts = content_parts(content)
@@ -1086,38 +1111,72 @@ class TaskRun:
1086
1111
  with self._condition:
1087
1112
  if self.status in (FINISHED, ERROR):
1088
1113
  raise RuntimeError("The episode has ended")
1089
- self._inbox.append((sender, text, parts))
1090
1114
  self.trace.emit("user_message", text=text, sender=sender,
1091
1115
  **({"images": sum(part["type"] == "input_image" for part in parts)}
1092
1116
  if any(part["type"] == "input_image" for part in parts) else {}))
1117
+ self._put("message", NOW, (sender, text, parts), reason="environment message")
1118
+
1119
+ def _put(self, kind, priority, data, *, wakes=True, reason=None) -> None:
1120
+ """Put a notice into the queue (runtime/decision/notices.py; the caller holds the
1121
+ condition). One of priority `now` sets the model's call in flight aside."""
1122
+ self._notices.put(Notice(kind, priority, data, wakes))
1123
+ if priority == NOW:
1093
1124
  self._revision += 1
1094
- self._preempt("environment message")
1095
- self._condition.notify_all()
1125
+ self._urgent += 1
1126
+ self._preempt(reason or kind)
1127
+ self._condition.notify_all()
1128
+
1129
+ def _note(self, text, priority=NEXT) -> None:
1130
+ """A note of the host for the model."""
1131
+ with self._condition:
1132
+ self._put("note", priority, str(text))
1096
1133
 
1097
- def _escalation(self, record):
1134
+ def _message_waits(self) -> bool:
1135
+ """Whether a message waits to be told (the caller holds the condition)."""
1136
+ return bool(self._notices.pending(kinds=("message",)))
1137
+
1138
+ @staticmethod
1139
+ def _event_key(record) -> tuple:
1140
+ event = record.get("event") or {}
1141
+ return event.get("kind"), event.get("source")
1142
+
1143
+ def _escalation(self, record, priority=NOW):
1144
+ """An event for the model: a notice, at once unless its source says otherwise. One
1145
+ that repeats an event the model has not answered yet (the same kind from the same
1146
+ source) tells nothing new: where that one still waits in the queue it is counted
1147
+ with it (`times`), and where the model is being told it, the new one is told with
1148
+ the next call and sets none aside."""
1098
1149
  with self._condition:
1099
1150
  identifier = record["id"]
1100
1151
  if identifier in self._seen_events:
1101
1152
  return
1102
1153
  self._seen_events.add(identifier)
1103
- self._pending_events[identifier] = record
1104
1154
  self.trace.emit("escalation", **{key: value for key, value in record.items() if key != "event"},
1105
1155
  reported=record["event"])
1106
- self._revision += 1
1107
- self._preempt("unhandled escalation")
1108
- self._condition.notify_all()
1156
+ key = self._event_key(record)
1157
+ waiting = next((notice for notice in self._notices.pending(kinds=("event",))
1158
+ if self._event_key(notice.data) == key), None)
1159
+ if waiting is not None:
1160
+ waiting.data = {**record, "times": int(waiting.data.get("times", 1)) + 1}
1161
+ self._condition.notify_all()
1162
+ return
1163
+ if priority == NOW and key in self._told_sources:
1164
+ priority = NEXT
1165
+ self._put("event", priority, record, reason="unhandled escalation")
1109
1166
 
1110
1167
  def _held_back(self, record: dict) -> None:
1111
1168
  """A reflex's condition came to hold while a program of higher priority had the
1112
1169
  environment (at_priority; the kernel's `held_back` note, once per reflex and unit):
1113
1170
  the reflex did not act and is judged again when that unit has ended. An event to the
1114
- model, told at once as a preemption is, so that it may let the reflex act by
1171
+ model of priority `later`: the program said itself that it stands above the reflex,
1172
+ so nothing the model thinks is out of date; it may still let the reflex act by
1115
1173
  cancelling the unit."""
1116
1174
  record.pop("time", None)
1117
1175
  self.trace.emit("held_back", **record)
1118
1176
  self._escalation({"id": uuid.uuid4().hex, "entry": record.get("by"), "event": {
1119
1177
  "kind": "HELD_BACK", "source": record.get("rule"),
1120
- "detail": {key: record[key] for key in ("rule", "priority", "by", "by_name") if record.get(key) is not None}}})
1178
+ "detail": {key: record[key] for key in ("rule", "priority", "by", "by_name") if record.get(key) is not None}}},
1179
+ LATER)
1121
1180
 
1122
1181
  def _taken(self, record: dict) -> None:
1123
1182
  """A unit of higher priority (a rule's program) took the environment from an
@@ -1144,10 +1203,7 @@ class TaskRun:
1144
1203
  thought = self._request
1145
1204
  if thought is not None and thought.outcome is None:
1146
1205
  self._settle(thought, "discarded", reason)
1147
- if reason == "user message":
1148
- self._thoughts.cancel_where(lambda item: item.role in ("verifier", "observer", "vlm", "pointer", "reflection"),
1149
- reason)
1150
- elif reason == "unhandled escalation":
1206
+ if reason == "unhandled escalation":
1151
1207
  self._thoughts.cancel_where(lambda item: item.role == "observer", reason)
1152
1208
 
1153
1209
  def _busy(self) -> bool:
@@ -1229,7 +1285,8 @@ class TaskRun:
1229
1285
  if not active.is_active(self.config.runtime):
1230
1286
  return ""
1231
1287
  level = self._admission()
1232
- report = active.report(self._learning_rows(), self.seconds_left(), self._situations(), level)
1288
+ report = active.report(self._learning_rows(), self.seconds_left() if self._budget_told else None,
1289
+ self._situations(), level)
1233
1290
  if self._fixture_descriptions:
1234
1291
  report += "\n" + "\n".join(self._fixture_descriptions)
1235
1292
  if level == "off":
@@ -1259,7 +1316,7 @@ class TaskRun:
1259
1316
  self._fixture_status = "off"
1260
1317
  return
1261
1318
  with self._condition:
1262
- if self._closing or self._pause or self._busy() or (self._inbox and not replay):
1319
+ if self._closing or self._pause or self._busy() or (self._message_waits() and not replay):
1263
1320
  return
1264
1321
  generation = self._activation_interrupt, self._revision
1265
1322
 
@@ -1444,7 +1501,7 @@ class TaskRun:
1444
1501
  active = (self.config.runtime.get("library") or {}).get("trigger") == "active"
1445
1502
  if "admitted" in observed:
1446
1503
  if not active:
1447
- self._tell(f"The host ran the test of your proposal {key} and it passed"
1504
+ self._note(f"The host ran the test of your proposal {key} and it passed"
1448
1505
  + (": it is active from your next program." if outcome.get("status") == "activated"
1449
1506
  else "; it waits for validation before it is loaded."))
1450
1507
  return
@@ -1457,7 +1514,7 @@ class TaskRun:
1457
1514
  self.trace.emit("library", action="trial_pending", key=key, revision=expected, reason=str(error))
1458
1515
  self._fixture_feedback[key] = f"Its own test failed when the host ran it: {receipt['error'][-300:]}"
1459
1516
  if not active:
1460
- self._tell(f"The host ran the test of your proposal {key} and it failed: {receipt['error'][-300:]}\n"
1517
+ self._note(f"The host ran the test of your proposal {key} and it failed: {receipt['error'][-300:]}\n"
1461
1518
  "It stays out of your library; rewrite it and propose it again if you still need it.")
1462
1519
  return
1463
1520
  # No verdict: the revision could not be staged, or the boundary was taken (a pause, a
@@ -1515,7 +1572,7 @@ class TaskRun:
1515
1572
  if not selected:
1516
1573
  return {"status": "cancelled"}
1517
1574
  def can_switch():
1518
- return startup or (not (self._busy() or self._inbox)
1575
+ return startup or (not (self._busy() or self._message_waits())
1519
1576
  and (publish is not None or all(
1520
1577
  self._activation_queue.get(item.key, (None,))[0] == item for item, _ in selected)))
1521
1578
 
@@ -1795,11 +1852,6 @@ class TaskRun:
1795
1852
  raise Interrupted()
1796
1853
  self._set_status(RUNNING)
1797
1854
 
1798
- def _take_inbox(self) -> list[str]:
1799
- with self._condition:
1800
- pending, self._inbox = self._inbox, []
1801
- return pending
1802
-
1803
1855
  def _checkpoint(self) -> None:
1804
1856
  """Between steps: stop when closing, wait while paused."""
1805
1857
  with self._condition:
@@ -1809,7 +1861,7 @@ class TaskRun:
1809
1861
  if interrupted:
1810
1862
  raise Interrupted()
1811
1863
  if paused:
1812
- self._wait(PAUSED, lambda: not self._pause or bool(self._inbox))
1864
+ self._wait(PAUSED, lambda: not self._pause or self._message_waits())
1813
1865
  with self._condition:
1814
1866
  self._pause = False
1815
1867
  kernel = self.kernel
@@ -1831,12 +1883,9 @@ class TaskRun:
1831
1883
  pass
1832
1884
 
1833
1885
  def _woken(self) -> bool:
1834
- """Whether something reached the model that starts a turn: a message, the end of a
1835
- unit (one that was cancelled is told with the next turn, it starts none), an event
1836
- it has not been told."""
1837
- return (bool(self._inbox) or self._run_end is not None
1838
- or any(item["status"] != "cancelled" for item in self._unit_news)
1839
- or any(identifier not in self._reported_events for identifier in self._pending_events))
1886
+ """Whether something reached the model that starts a turn: a notice that wakes (the
1887
+ end of a unit that was cancelled is told with the next turn, it starts none)."""
1888
+ return bool(self._notices.pending(waking=True)) or self._run_end is not None
1840
1889
 
1841
1890
  def _idle(self, reason: str) -> None:
1842
1891
  """Between turns: the conversation waits, IDLE with the reason its last turn ended
@@ -1859,22 +1908,15 @@ class TaskRun:
1859
1908
  finally:
1860
1909
  self.waiting_since = None
1861
1910
 
1862
- def _add_user_messages(self) -> None:
1863
- """Put the user's messages into the next request: into the user message that
1864
- ends the history, or as a new one after a reply."""
1865
- pending = self._take_inbox()
1866
- if not pending:
1867
- return
1868
- parts = _inbox_parts(pending)
1869
- if self.messages[-1]["role"] == "user":
1870
- self._extend_request(parts)
1871
- else:
1872
- self._tell(parts)
1873
-
1874
1911
  def _tell(self, content) -> None:
1875
1912
  """Add a user message to the history."""
1876
1913
  self.messages.append({"role": "user", "content": content})
1877
1914
 
1915
+ def _came_to(self, content) -> None:
1916
+ """What the model's last output came to (its program's handle, the end of its wait,
1917
+ why nothing ran): the first thing its next call is told (_deliver)."""
1918
+ self._outcome += content_parts(content)
1919
+
1878
1920
  def _say(self, parts) -> None:
1879
1921
  """Tell the model `parts` with its next call: in the user message that ends the
1880
1922
  history, or as a new one after a reply."""
@@ -2163,8 +2205,7 @@ class TaskRun:
2163
2205
 
2164
2206
  if active.is_active(runtime):
2165
2207
  # The active trigger: this run learns the environment for the task (runtime/learning/active.py).
2166
- instruction = active.goal(instruction, active.schedule(runtime.get("library"))["minutes"],
2167
- runtime.get("skills") or (), sandbox_of(runtime),
2208
+ instruction = active.goal(instruction, runtime.get("skills") or (), sandbox_of(runtime),
2168
2209
  legacy_memory="legacy_memory" in EMBODIMENT_KINDS[embodiment].prompt_features)
2169
2210
  self._said.insert(0, instruction)
2170
2211
  opening = f"Instruction: {instruction}\n"
@@ -2291,8 +2332,10 @@ class TaskRun:
2291
2332
  if reason in decisions.TURN_STOPS:
2292
2333
  # What reached the model in this turn and met no call of it is in its
2293
2334
  # history: it starts no turn by itself, the user's next message does.
2294
- self._include_escalations()
2295
- self._add_user_messages()
2335
+ parts = [part for notice in self._notices.take(kinds=("message", "event", "note"))
2336
+ for part in self._notice_parts(notice)]
2337
+ if parts:
2338
+ self._say(parts)
2296
2339
  summary["status"] = decisions.TURN_ENDS[reason]
2297
2340
  if reason != "answer":
2298
2341
  self.kernel.cancel()
@@ -2358,8 +2401,10 @@ class TaskRun:
2358
2401
  malformed_from=None, nudged=0, empties=0,
2359
2402
  post_calls=None if post_calls is None else int(post_calls))
2360
2403
  # What the first call of the turn is made for: the instruction, else what started
2361
- # the turn (a message or an event comes first: decisions.TRIGGERS).
2404
+ # the turn (a message or an event it was told comes first: _think).
2362
2405
  self.reason = "start" if self._turns == 1 else "unit_end"
2406
+ with self._condition:
2407
+ self._upto = LATER
2363
2408
  while True:
2364
2409
  self._checkpoint()
2365
2410
  self._load_due_skills()
@@ -2370,7 +2415,7 @@ class TaskRun:
2370
2415
  self._checkpoint()
2371
2416
  if ended := self._ended_by_unit(loop):
2372
2417
  return ended
2373
- self._tell_news(loop)
2418
+ self._deliver(loop)
2374
2419
  if ended := self._ended_by_unit(loop):
2375
2420
  return ended
2376
2421
  self._compact_if_due(loop)
@@ -2378,12 +2423,15 @@ class TaskRun:
2378
2423
  if self._iterations >= config.max_model_calls:
2379
2424
  return "budget"
2380
2425
  policy = (self._post_from is not None and loop.post_calls is not None
2381
- and summary["model_calls"] - self._post_from >= loop.post_calls and not self._inbox)
2426
+ and summary["model_calls"] - self._post_from >= loop.post_calls and not self._message_waits())
2382
2427
  if self.seconds_left() <= 0:
2383
2428
  return "wall_time"
2384
2429
  if not self._within_budget():
2385
2430
  return "cost"
2386
2431
  if not policy:
2432
+ if self._notices.pending(self._upto):
2433
+ continue # (It came meanwhile: told first.)
2434
+ asked = self._urgent
2387
2435
  current = self._think()
2388
2436
  self._condition.wait_for(lambda: current.back(0) or self._closing or self._interrupt)
2389
2437
  if self._closing:
@@ -2403,8 +2451,10 @@ class TaskRun:
2403
2451
  or (sum(row["chars"] for row in context_summary(current.messages, current.system))
2404
2452
  + len(reply.text)) // 4)
2405
2453
  self._settle(current, "used")
2406
- self._pending_events.clear()
2407
- loop.revision = current.revision
2454
+ self._unanswered.clear()
2455
+ self._told_events = []
2456
+ self._told_sources.clear()
2457
+ loop.revision, loop.urgent = current.revision, asked
2408
2458
  loop.call, loop.thought = current.call, current.id
2409
2459
  if policy:
2410
2460
  return self._policy_to_end()
@@ -2421,7 +2471,7 @@ class TaskRun:
2421
2471
  return None
2422
2472
  if end == "episode_ended":
2423
2473
  return "episode_ended"
2424
- if self._inbox or self._pending_events:
2474
+ if self._unanswered or self._notices.pending(kinds=("message", "event")):
2425
2475
  return None
2426
2476
  return self._out_answer(None, SimpleNamespace(body=end[1], source="program"), loop)
2427
2477
 
@@ -2480,8 +2530,9 @@ class TaskRun:
2480
2530
  self.reason = "empty"
2481
2531
 
2482
2532
  def _current_output(self, loop):
2483
- """Whether nothing reached the model since the call whose reply is being done."""
2484
- return not self._inbox and not self._pending_events and loop.revision == self._revision
2533
+ """Whether the reply being done knew of everything that set a call aside: no notice
2534
+ of priority `now` came since its call was made."""
2535
+ return loop.urgent == self._urgent
2485
2536
 
2486
2537
  def _out_invalid(self, reply, parsed, loop):
2487
2538
  """A reply nothing came of: told why, and asked again."""
@@ -2497,7 +2548,7 @@ class TaskRun:
2497
2548
  blocks = "<action> or <wait>" if loop.direct else "<python>, <verify> or <wait>"
2498
2549
  text = (f"Format error: {parsed}. Nothing ran. Reply with the {blocks} block itself, starting with its "
2499
2550
  "opening tag, or with your answer in plain text.")
2500
- self._tell(text)
2551
+ self._came_to(text)
2501
2552
  self.trace.emit("protocol_error", message=str(parsed), truncated=reply.usage.get("finish_reason") == "length",
2502
2553
  **({"announced": True} if text.startswith("Nothing ran") else {}))
2503
2554
  self.reason = "format"
@@ -2519,8 +2570,8 @@ class TaskRun:
2519
2570
  before = getattr(self, "_library_version", None)
2520
2571
  self._trial_at_boundary()
2521
2572
  if getattr(self, "_library_version", None) != before:
2522
- self._tell("The host activated your proposal. Use the new revision in a brief practice program, "
2523
- "check what it does, then answer with the result.")
2573
+ self._came_to("The host activated your proposal. Use the new revision in a brief practice program, "
2574
+ "check what it does, then answer with the result.")
2524
2575
  self.reason = "program_end"
2525
2576
  return None
2526
2577
  self.result = text.strip()
@@ -2533,17 +2584,18 @@ class TaskRun:
2533
2584
  return "answer"
2534
2585
 
2535
2586
  def _out_wait(self, reply, parsed, loop):
2536
- """The model waits by choice: no call is made until the first of a unit it started
2537
- ended, a message or an event reached it, or its time (the block's seconds, at most
2538
- the run's longest wait, which a wait without a time gets). It is told which."""
2587
+ """The model waits by choice: no call is made until a notice reaches it (the end of
2588
+ a unit it started, a message, an event, a note), or its time is over (the block's
2589
+ seconds, at most the run's longest wait, which a wait without a time gets). It is
2590
+ told which."""
2539
2591
  asked = wait_seconds(parsed.body)
2540
2592
  limit = max(0.0, min(asked if asked is not None else self._longest_wait, self._longest_wait,
2541
2593
  self.seconds_left()))
2542
2594
  began = time.monotonic()
2543
2595
 
2544
2596
  def reached():
2545
- return ("unit" if self._unit_news or self._run_end is not None else "message" if self._inbox
2546
- else "event" if self._pending_events else None)
2597
+ waking = self._notices.pending(waking=True)
2598
+ return "unit" if self._run_end is not None else waking[0].kind if waking else None
2547
2599
 
2548
2600
  with self._condition:
2549
2601
  self.trace.emit("wait", seconds=round(limit, 3), **({"asked": asked} if asked is not None else {}))
@@ -2552,12 +2604,10 @@ class TaskRun:
2552
2604
  why = reached() or ("time" if time.monotonic() - began >= limit else "pause")
2553
2605
  waited = time.monotonic() - began
2554
2606
  self.trace.emit("wait_end", why=why, seconds=round(waited, 3))
2555
- running = [unit_text(identifier, unit) for identifier, unit in self._units.items()]
2556
- # (Last in the message when nothing else is told: the bare end of a wait.)
2557
- self._say([{"type": "input_text", "text": decisions.WAIT_ENDED.format(
2558
- seconds=waited, why=decisions.WAIT_ENDS.get(why, "the user paused"))
2559
- + ("\nUnits running: " + latest(running) if running else "")
2560
- + "\n" + self._tree_told()}])
2607
+ self._came_to(decisions.WAIT_ENDED.format(seconds=waited,
2608
+ why=decisions.WAIT_ENDS.get(why, "the user paused")))
2609
+ # (After a wait the queue is told whole.)
2610
+ self._upto = LATER
2561
2611
  self.reason = "wait_end"
2562
2612
 
2563
2613
  def _out_verify(self, reply, parsed, loop):
@@ -2569,12 +2619,13 @@ class TaskRun:
2569
2619
  answer, view = self._verify(parsed.body)
2570
2620
  except ThoughtCancelled:
2571
2621
  with self._condition:
2572
- if self._inbox or self._pending_events or self._closing:
2622
+ if self._notices.pending() or self._closing:
2573
2623
  return None
2574
2624
  raise
2575
- state = state_text(view["state"], self.config.embodiment, self._steps_shown())
2576
- self._tell([{"type": "input_text", "text": f"Verifier: {answer}\n{loop.told['state_label']}: {state}"},
2577
- *(image_parts(view["images"], self.cameras) if self._standing else [])])
2625
+ with self._condition:
2626
+ self._state_now = view["state"]
2627
+ self._came_to([{"type": "input_text", "text": f"Verifier: {answer}"},
2628
+ *(image_parts(view["images"], self.cameras) if self._standing else [])])
2578
2629
  self.reason = "verify"
2579
2630
 
2580
2631
  def _verify(self, claim, cameras=None, request=None):
@@ -2712,11 +2763,12 @@ class TaskRun:
2712
2763
  trace.emit("program_held")
2713
2764
  self._checkpoint()
2714
2765
  with self._condition:
2715
- if not self._current_output(loop):
2716
- trace.emit("program_dropped", reason="user_message" if self._inbox else "inputs_changed")
2717
- self._tell("The user paused before this program ran, so nothing ran." if waiting
2718
- else "A user message came before this program ran, so nothing ran." if self._inbox
2719
- else "An event reached you before this program ran, so nothing ran.")
2766
+ # (After a pause a message of the user replaces the program.)
2767
+ replaced = waiting and self._message_waits()
2768
+ if replaced or not self._current_output(loop):
2769
+ trace.emit("program_dropped", reason="user_message" if replaced else "inputs_changed")
2770
+ self._came_to("The user paused before this program ran, so nothing ran." if waiting
2771
+ else "Something reached you before this program ran, so nothing ran.")
2720
2772
  self.reason = "user"
2721
2773
  return None
2722
2774
  summary["cells"] += 1
@@ -2744,16 +2796,16 @@ class TaskRun:
2744
2796
  with self._condition:
2745
2797
  if started in self._programs.values():
2746
2798
  # (Else it ended meanwhile: its result is what the model is told.)
2747
- self._tell(decisions.RUNNING.format(number=number, unit=result["unit"])
2748
- + f"\nModel calls left: {self.config.max_model_calls - self._iterations}")
2799
+ self._came_to(decisions.RUNNING.format(number=number, unit=result["unit"]))
2749
2800
  self.reason = "program_running"
2750
2801
  return None
2751
2802
 
2752
2803
  def _unit_arrived(self, result: dict) -> None:
2753
2804
  """A unit of the model ended: its result came as the reply to its program, or as the
2754
- kernel's unit_end message (any thread). It is recorded now and told to the model
2755
- with its next call (_tell_news); an end that is an event (an error, an unhandled
2756
- event, the episode's end, finish()) sets a call in flight aside."""
2805
+ kernel's unit_end message (any thread). It is recorded now and is a notice for the
2806
+ model: told with its next call, or at once (a call in flight is set aside) when the
2807
+ end is an event (an error, an unhandled event, the episode's end, finish()). The
2808
+ end of a unit that was cancelled wakes nobody."""
2757
2809
  trace, summary = self.trace, self.summary
2758
2810
  result = {key: value for key, value in result.items() if key not in ("op", "time")}
2759
2811
  number = result.get("cell")
@@ -2791,25 +2843,48 @@ class TaskRun:
2791
2843
  self._run_end = "episode_ended"
2792
2844
  elif status == "finished" and self._run_end is None:
2793
2845
  self._run_end = ("finished", str(result.get("finish") or ""))
2794
- self._unit_news.append(result)
2795
- if status not in ("ok", "cancelled") and not result.get("handled"):
2796
- self._revision += 1
2797
- self._preempt("a unit ended with an event")
2798
- self._condition.notify_all()
2846
+ if result.get("state"):
2847
+ self._state_now = result["state"]
2848
+ urgent = status not in ("ok", "cancelled") and not result.get("handled")
2849
+ self._put("unit", NOW if urgent else NEXT, result, wakes=status != "cancelled",
2850
+ reason="a unit ended with an event")
2799
2851
  self.charge_up()
2800
2852
 
2801
- def _tell_news(self, loop) -> None:
2802
- """Tell the model what its units ended with since its last call: each program's
2803
- feedback (status, output, events, state), each other unit's end, what they showed
2804
- and looked at, the units still running and the calls left."""
2853
+ def _notice_parts(self, notice) -> list:
2854
+ """A message, an event or a note of the queue as the model reads it."""
2855
+ if notice.kind == "message":
2856
+ return _inbox_parts([notice.data])
2857
+ if notice.kind == "event":
2858
+ record = notice.data
2859
+ event = record.get("event") or {}
2860
+ more = {key: value for key, value in record.items() if key not in ("id", "event", "time")}
2861
+ if event.get("detail") not in (None, {}, ""):
2862
+ more["detail"] = event["detail"]
2863
+ return [{"type": "input_text", "text": decisions.EVENT.format(
2864
+ kind=event.get("kind"), source=event.get("source")) + (": " + json.dumps(more) if more else "")}]
2865
+ return [{"type": "input_text", "text": str(notice.data)}]
2866
+
2867
+ def _deliver(self, loop) -> None:
2868
+ """Tell the model what there is to tell, with its next call: the one place that
2869
+ does. First what its last output came to (_came_to), then the notices of the queue
2870
+ down to the priority that is due (runtime/decision/notices.py), each as its kind
2871
+ reads (a program's feedback with what it showed and looked at, another unit's end,
2872
+ a message, an event, a note), then how things stand now (_ambient). Nothing when
2873
+ there is neither."""
2805
2874
  with self._condition:
2806
- news, self._unit_news = self._unit_news, []
2807
- if not news:
2875
+ upto, self._upto = self._upto, NEXT
2876
+ notices = self._notices.take(upto)
2877
+ outcome, self._outcome = self._outcome, []
2878
+ if not notices and not outcome:
2808
2879
  return
2809
- config, trace, embodiment, cameras = self.config, self.trace, self.config.embodiment, self.cameras
2880
+ trace, cameras = self.trace, self.cameras
2810
2881
  order = ("unit_end", "program_end", "escalation", "error")
2811
- content, looks, outlines, reason, taken, state, programs = [], [], {}, "unit_end", None, None, 0
2812
- for result in news:
2882
+ content, looks, outlines, reason, taken, state, programs = list(outcome), [], {}, None, None, None, 0
2883
+ for notice in notices:
2884
+ if notice.kind != "unit":
2885
+ content += self._notice_parts(notice)
2886
+ continue
2887
+ result = notice.data
2813
2888
  program, status = result.get("program"), result["status"]
2814
2889
  if program is not None:
2815
2890
  programs += 1
@@ -2817,15 +2892,14 @@ class TaskRun:
2817
2892
  self._stuck(program["code"], result)
2818
2893
  taken = taken or _taken_over(result)
2819
2894
  why = {"escalated": "escalation", "error": "error"}.get(status, "program_end")
2820
- text = cell_feedback(result, program["number"], self.profile.get("control_hz"), embodiment,
2821
- self._steps_shown())
2895
+ text = cell_feedback(result, program["number"])
2822
2896
  if why == "program_end":
2823
2897
  text += decisions.PROGRAM_ENDED
2824
2898
  state = result.get("state") or state
2825
2899
  else:
2826
2900
  why = "escalation" if status in ("error", "escalated") and not result.get("handled") else "unit_end"
2827
2901
  text = unit_feedback(result)
2828
- reason = max(reason, why, key=order.index)
2902
+ reason = why if reason is None else max(reason, why, key=order.index)
2829
2903
  content.append({"type": "input_text", "text": text, "result": True})
2830
2904
  for item in result.get("attachments", [])[:6]:
2831
2905
  content.append({"type": "input_text", "text": f"show(): {item['caption']}"})
@@ -2841,22 +2915,15 @@ class TaskRun:
2841
2915
  views=[{key: item[key] for key in ("camera", "region", "pin") if key in item}
2842
2916
  for item in seen])
2843
2917
  looks += seen
2844
- self.reason = reason
2918
+ if reason is not None:
2919
+ self.reason = reason
2845
2920
  with self._condition:
2846
- calls_left = config.max_model_calls - self._iterations
2847
- running = [unit_text(identifier, unit) for identifier, unit in self._units.items()]
2848
- tree = self._tree_told()
2849
- note = ""
2850
- if self._post_from is not None and loop.post_calls:
2851
- calls_left = min(calls_left, loop.post_calls - (self.summary["model_calls"] - self._post_from))
2852
- note = ("After these calls the learned policy runs on by itself to the end of the episode.\n"
2853
- if calls_left > 0 else "")
2854
- # What was learned so far, where this run learns: whole when it changed, else its first line.
2855
- head, _, body = self._learning_report().strip().partition("\n")
2856
- learned = (head + ("\n" + body if body != self._learning_told else " as you were last told") + "\n") if head else ""
2857
- self._learning_told = body
2858
- content.append({"type": "input_text", "text": ("Units running: " + latest(running) + "\n" if running else "")
2859
- + (tree + "\n" if tree else "") + learned + note + f"Model calls left: {calls_left}"})
2921
+ for notice in notices:
2922
+ if notice.kind in ("message", "event"):
2923
+ self._unanswered.add(notice.kind)
2924
+ if notice.kind == "event":
2925
+ self._told_events.append(notice.data.get("id"))
2926
+ self._told_sources.add(self._event_key(notice.data))
2860
2927
  if not taken and reason in ("error", "escalation"):
2861
2928
  self._reflect_learning()
2862
2929
  if taken is not None:
@@ -2871,27 +2938,72 @@ class TaskRun:
2871
2938
  view = self.env.call("observe", episode_id=self.episode, cameras=cameras)
2872
2939
  content.extend(image_parts({**view["images"], **outlines}, cameras))
2873
2940
  state = view["state"]
2941
+ with self._condition:
2942
+ self._state_now = state
2874
2943
  else:
2875
2944
  # Only what the units looked at (look, a standing watch of the model's own).
2876
2945
  with self._condition:
2877
2946
  self._view_pins |= {item["pin"] for item in looks if item.get("pin")}
2878
2947
  content.extend(view_parts(looks) if self._images else self._described(looks))
2948
+ with self._condition:
2949
+ # How things stand is told once, last: where the message this call ends with
2950
+ # already says it (a call that was set aside), that part gives way to this one,
2951
+ # which tells again what that one told as changed.
2952
+ last = self.messages[-1] if self.messages else None
2953
+ if last is not None and last["role"] == "user" and not isinstance(last["content"], str) and any(
2954
+ part.get("ambient") for part in last["content"]):
2955
+ last["content"] = [part for part in last["content"] if not part.get("ambient")]
2956
+ self._reflex_told, self._learning_told = getattr(self, "_ambient_before", ({}, None))
2957
+ self._ambient_before = dict(getattr(self, "_reflex_told", {})), self._learning_told
2958
+ content.append(self._ambient(loop))
2879
2959
  with self._condition:
2880
2960
  self._say(content)
2881
2961
  if (state or {}).get("ended"):
2882
2962
  self._run_end = "episode_ended"
2883
2963
 
2964
+ def _ambient(self, loop) -> dict:
2965
+ """How things stand now, told last with every call that tells the model anything,
2966
+ whatever it is told: the state, the units that run, what changed in the tree of
2967
+ reflexes, what was learned. What the run has left (its steps, its calls, its time)
2968
+ is told only where the run asks for it (runtime.tell_budget): the limits are the
2969
+ host's to keep, and a countdown with every call weighs on every judgement."""
2970
+ config, embodiment = self.config, self.config.embodiment
2971
+ with self._condition:
2972
+ calls_left = config.max_model_calls - self._iterations
2973
+ running = [unit_text(identifier, unit) for identifier, unit in self._units.items()]
2974
+ tree = self._tree_told()
2975
+ state = self._state_now
2976
+ lines = []
2977
+ if state:
2978
+ lines.append(f"{EMBODIMENT_TEXT[embodiment]['state_label']}: "
2979
+ + state_text(state, embodiment, self._steps_shown()))
2980
+ if self._budget_told and self._steps_shown() and state.get("step") is not None:
2981
+ lines.append(steps_text(state, self.profile.get("control_hz")))
2982
+ if running:
2983
+ lines.append("Units running: " + latest(running))
2984
+ if tree:
2985
+ lines.append(tree)
2986
+ # What was learned so far, where this run learns: whole when it changed, else its first line.
2987
+ head, _, body = self._learning_report().strip().partition("\n")
2988
+ if head:
2989
+ lines.append(head + ("\n" + body if body != self._learning_told else " as you were last told"))
2990
+ self._learning_told = body
2991
+ if self._budget_told:
2992
+ if self._post_from is not None and loop.post_calls:
2993
+ calls_left = min(calls_left, loop.post_calls - (self.summary["model_calls"] - self._post_from))
2994
+ if calls_left > 0:
2995
+ lines.append("After these calls the learned policy runs on by itself to the end of the episode.")
2996
+ lines.append(f"Model calls left: {calls_left}")
2997
+ return {"type": "input_text", "text": "\n".join(lines), "ambient": True}
2998
+
2884
2999
  def _think(self):
2885
- """Queue the model's next call with its immutable input, on the trigger of what is
2886
- pending (a message, an unresolved event), else of what happened last (self.reason)."""
2887
- triggers = decisions.TRIGGERS
2888
- trigger = next((name for name, check in triggers.items() if check and getattr(self, check)()), None)
2889
- if trigger is None:
2890
- trigger = self.reason
2891
- if trigger not in triggers:
2892
- raise ValueError(f"No model trigger for {trigger!r}")
2893
- self._include_escalations()
2894
- self._add_user_messages()
3000
+ """Queue the model's next call with its immutable input, on the trigger of what it
3001
+ was told and has not answered (a message, then an event), else of what happened
3002
+ last (self.reason)."""
3003
+ trigger = ("user" if "message" in self._unanswered else "escalation" if "event" in self._unanswered
3004
+ else self.reason)
3005
+ if trigger not in decisions.REASONS:
3006
+ raise ValueError(f"No model trigger for {trigger!r}")
2895
3007
  config, summary, messages = self.config, self.summary, self.messages
2896
3008
  if self._standing:
2897
3009
  prune_images(messages, int(config.runtime.get("keep_images", 2)))
@@ -2917,7 +3029,7 @@ class TaskRun:
2917
3029
  total_messages=len(messages), sent_messages=len(request),
2918
3030
  new_messages=history[self._logged:], context=context_summary(request, system),
2919
3031
  library_version=getattr(self, "_library_version", None), revision=thought.revision,
2920
- events=list(self._pending_events), **self._replaced())
3032
+ events=list(self._told_events), **self._replaced())
2921
3033
  self._logged = len(messages)
2922
3034
  summary["model_calls"] += 1
2923
3035
  summary["decisions"][trigger] = summary["decisions"].get(trigger, 0) + 1
@@ -2943,21 +3055,6 @@ class TaskRun:
2943
3055
  self._logged -= 1
2944
3056
  self._replaces = 1
2945
3057
 
2946
- def _include_escalations(self):
2947
- parts = []
2948
- for identifier, report in self._pending_events.items():
2949
- if identifier not in self._reported_events:
2950
- self._reported_events.add(identifier)
2951
- parts.append({"type": "input_text", "text": f"Unresolved event: {json.dumps(report)}"})
2952
- if parts:
2953
- self._say(parts)
2954
-
2955
- def _pending_message(self):
2956
- return bool(self._inbox)
2957
-
2958
- def _unresolved(self):
2959
- return bool(self._pending_events)
2960
-
2961
3058
  def _came_back(self, thought):
2962
3059
  from harness.runtime.decision.model import add_usage
2963
3060
 
@@ -3053,7 +3150,7 @@ class TaskRun:
3053
3150
  identifier = self._program_approval_id = uuid.uuid4().hex
3054
3151
  self.trace.emit("confirmation_request", id=identifier, code=program)
3055
3152
  try:
3056
- self._wait(WAITING_FOR_CONFIRMATION, lambda: self._decision is not None or bool(self._inbox),
3153
+ self._wait(WAITING_FOR_CONFIRMATION, lambda: self._decision is not None or self._message_waits(),
3057
3154
  reason="program", request={"id": identifier, "code": program})
3058
3155
  finally:
3059
3156
  with self._condition:
@@ -3066,7 +3163,7 @@ class TaskRun:
3066
3163
  self.trace.emit("confirmation", id=identifier, accepted=accepted, reason=reason)
3067
3164
  if accepted:
3068
3165
  return True
3069
- self._tell("The user rejected this program, so nothing ran." + (f" Their reason: {reason}" if reason else ""))
3166
+ self._came_to("The user rejected this program, so nothing ran." + (f" Their reason: {reason}" if reason else ""))
3070
3167
  self.reason = "rejected"
3071
3168
  return False
3072
3169
 
@@ -3086,7 +3183,7 @@ class TaskRun:
3086
3183
  or cells <= self._reflected_cells or self.seconds_left() <= 0):
3087
3184
  return
3088
3185
  with self._condition:
3089
- if self._pause or self._closing or self._inbox or self._busy():
3186
+ if self._pause or self._closing or self._message_waits() or self._busy():
3090
3187
  return
3091
3188
  revision = self._revision
3092
3189
  since = self._reflected_cells