@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
@@ -59,6 +59,31 @@ def _short(value, limit=200) -> str:
59
59
  return text if len(text) <= limit else text[:limit] + "..."
60
60
 
61
61
 
62
+ class _Priority:
63
+ """What `at_priority(n)` gives: a block in which the unit holds the embodiment with n.
64
+ Outside a `with` it does nothing, and says so where the program's output is read."""
65
+
66
+ def __init__(self, tree, entry, priority):
67
+ self.tree, self.entry, self.priority, self.before, self.used = tree, entry, priority, None, False
68
+
69
+ def __enter__(self):
70
+ self.used, self.before = True, self.entry.priority
71
+ self.tree._held_with(self.entry, self.priority)
72
+ return self
73
+
74
+ def __exit__(self, *raised):
75
+ self.tree._held_with(self.entry, self.before)
76
+ return False
77
+
78
+ def __del__(self):
79
+ if not self.used:
80
+ try:
81
+ print(f"at_priority({self.priority:g}) did nothing: it holds the environment for the block of "
82
+ f"`with at_priority({self.priority:g}):` only.")
83
+ except Exception:
84
+ pass
85
+
86
+
62
87
  class Entry:
63
88
  """One unit of a session (see the module docstring): its `id`, `name`, `kind` ("root"
64
89
  for the model, "program" for code, "agent" for a sub-agent), the model it belongs to
@@ -83,6 +108,9 @@ class Entry:
83
108
  self.cell = None
84
109
  self.rule = None
85
110
  self.priority = 0
111
+ # The priority the code it runs in the environment asks for now (the environment
112
+ # reports it with its state: core/worker.py), None when it asks for none.
113
+ self.asked = None
86
114
  self.restart = False
87
115
  # What it is for: a condition of the view that holds when its work is done (None: it
88
116
  # states none), and whether it held when the unit ended (None: no condition).
@@ -115,6 +143,12 @@ class Entry:
115
143
  """Whether it has not ended yet."""
116
144
  return self.status == "running"
117
145
 
146
+ @property
147
+ def rank(self) -> float:
148
+ """What it holds the environment with now: its priority, or the one the code it
149
+ runs in the environment asks for, whichever is higher."""
150
+ return max(self.priority or 0, self.asked or 0)
151
+
118
152
  def lineage(self) -> list:
119
153
  """This entry and the entries above it, up to the root."""
120
154
  chain, entry = [], self
@@ -363,20 +397,32 @@ class Tree:
363
397
  raise RuntimeError("until() is called by a program: it says what that program is for")
364
398
  entry.until = condition
365
399
 
366
- def at_priority(self, priority) -> None:
367
- """The unit this code runs in holds the embodiment with `priority` from now until it
368
- ends, between its executions too (runtime/tick.py hold). A unit of higher priority
400
+ def at_priority(self, priority) -> "_Priority":
401
+ """`with at_priority(priority):` the unit this code runs in holds the embodiment
402
+ with `priority` for the block, between its executions too (runtime/tick.py hold),
403
+ and with what it held it before from the block's end on. A unit of higher priority
369
404
  still takes it (a reflex whose place is `priority` or above); one of lower priority
370
- waits. Its own ends stay: it is cancelled, its calls reach their time, a reflex that
371
- ends what runs holds, the episode ends."""
405
+ waits, and is judged again when the block ends. Its own ends stay: it is cancelled,
406
+ its calls reach their time, a reflex that ends what runs holds, the episode ends."""
372
407
  entry = self.current()
373
408
  if entry is self.root:
374
- raise RuntimeError("at_priority() is called by a program: it says with what this program holds the environment")
409
+ raise RuntimeError("at_priority() is used by a program: it says with what the program holds the "
410
+ "environment for an action")
375
411
  if isinstance(priority, bool) or not isinstance(priority, (int, float)) or not priority >= 0:
376
412
  raise ValueError(f"at_priority(priority): a number, 0 or more, got {priority!r}")
413
+ return _Priority(self, entry, priority)
414
+
415
+ def _held_with(self, entry, priority) -> None:
416
+ """`entry` holds the embodiment with `priority` from now: the tick and the tree hear
417
+ of it (a reflex that waited below it is judged again)."""
377
418
  entry.priority = priority
378
419
  spans.note("priority", unit=entry.id, priority=priority)
379
- self.session._ticker.hold(entry)
420
+ ticker = self.session._ticker
421
+ ticker.hold(entry)
422
+ layer = getattr(self.session, "reactive", None)
423
+ if layer is not None:
424
+ layer.changed = True
425
+ ticker.judge()
380
426
 
381
427
  def claim(self, names) -> bool:
382
428
  """The caller's entry claims the channels `names` (see the module docstring): True;
@@ -48,6 +48,8 @@ from harness.runtime.tick import Preempted, Tick, delay_ticks, driving
48
48
 
49
49
  # The session of each embodiment kind, as "module:Class" (embodiments.KINDS).
50
50
  SESSIONS = {name: kind.session for name, kind in KINDS.items()}
51
+ # The seconds between two reports of the state to the host (the `state` note).
52
+ STATE_NOTE_SECONDS = 1.0
51
53
 
52
54
 
53
55
  class EpisodeCore:
@@ -170,6 +172,13 @@ class EpisodeCore:
170
172
  @state.setter
171
173
  def state(self, value):
172
174
  self.__dict__["_state"] = value
175
+ self._note_changes(value)
176
+ # The host keeps the state as it was last reported (the `state` note, at most every
177
+ # STATE_NOTE_SECONDS): what the model is told with every call (runtime/task/run.py _ambient).
178
+ now = time.monotonic()
179
+ if value and now - self.__dict__.get("_state_noted", 0.0) >= STATE_NOTE_SECONDS:
180
+ self.__dict__["_state_noted"] = now
181
+ spans.note("state", state=value)
173
182
  self._facts_changed()
174
183
 
175
184
  def _facts_changed(self):
@@ -179,14 +188,57 @@ class EpisodeCore:
179
188
 
180
189
  def _view(self, state, tick):
181
190
  """What rules, controllers and until predicates observe: the embodiment's view
182
- (_reactive_view) with the run's facts as "facts"."""
191
+ (_reactive_view) with the run's facts as "facts", and what a state alone does not
192
+ show: what each thing it reports was before it last changed ("was") and for how
193
+ many seconds it has not changed ("unchanged"), the senses under "sensed" in both;
194
+ and the units that run ("units": each {"name", "kind", "reflex", "priority",
195
+ "seconds"})."""
183
196
  view = self._reactive_view(state, tick)
184
197
  if isinstance(view, dict):
185
198
  view.setdefault("facts", self.facts.view())
186
199
  if isinstance(state, dict) and "sensed" in state:
187
200
  view.setdefault("sensed", state["sensed"])
201
+ was, unchanged, now = {"sensed": {}}, {"sensed": {}}, time.monotonic()
202
+ for key, (_, since, before) in list(self.__dict__.get("_changes", {}).items()):
203
+ if isinstance(key, tuple):
204
+ was["sensed"][key[1]], unchanged["sensed"][key[1]] = before, now - since
205
+ else:
206
+ was[key], unchanged[key] = before, now - since
207
+ view.setdefault("was", was)
208
+ view.setdefault("unchanged", unchanged)
209
+ tree, wall = self.__dict__.get("_entry_tree"), time.time()
210
+ view.setdefault("units", [
211
+ {"name": unit.name, "kind": unit.kind, "reflex": unit.rule, "priority": unit.rank,
212
+ "seconds": wall - unit.started} for unit in (tree.running() if tree is not None else ())])
188
213
  return view
189
214
 
215
+ def _note_changes(self, state) -> None:
216
+ """Keep, for each thing the state reports (and each sense), its value, when it last
217
+ changed and what it was before: what a condition needs to see that something
218
+ changes, or that it does not."""
219
+ if not isinstance(state, dict):
220
+ return
221
+ seen, now = self.__dict__.setdefault("_changes", {}), time.monotonic()
222
+
223
+ def note(key, value):
224
+ kept = seen.get(key)
225
+ if kept is None:
226
+ seen[key] = (value, now, None)
227
+ return
228
+ try:
229
+ same = bool(kept[0] == value)
230
+ except Exception:
231
+ same = False
232
+ if not same:
233
+ seen[key] = (value, now, kept[0])
234
+
235
+ for key, value in state.items():
236
+ if key == "sensed" and isinstance(value, dict):
237
+ for name, sensed in value.items():
238
+ note(("sensed", name), sensed)
239
+ else:
240
+ note(key, value)
241
+
190
242
  # Senses: values the environment computes itself, in its own language, each time it
191
243
  # reports its state (a worker whose profile says "senses": core/worker.py). What a
192
244
  # condition cannot read from the reported state and may not ask for (it makes no
@@ -725,13 +777,14 @@ class EpisodeCore:
725
777
  self._observe()
726
778
 
727
779
  def at_priority(self, priority):
728
- """This program holds the environment with `priority` from here until it ends. A
780
+ """`with at_priority(priority):` the program holds the environment with `priority`
781
+ for the block: for one action that must not be broken off, not for a stage. A
729
782
  reflex whose place is `priority` or above takes it from the program as before. One
730
- below does not act while the program runs: it is judged again when the program has
731
- ended and acts if its condition still holds, and the model is told once that it was
732
- held back. What still ends the program: cancelling it, the time of its calls, a
733
- reflex that ends what runs, the end of the episode."""
734
- self._entries.at_priority(priority)
783
+ below does not act during the block: it is judged again when the block ends and
784
+ acts if its condition still holds, and the model is told once that it was held
785
+ back. What still ends the program: cancelling it, the time of its calls, a reflex
786
+ that ends what runs, the end of the episode."""
787
+ return self._entries.at_priority(priority)
735
788
 
736
789
  def run_controller(self, fn, until=None, max_seconds=None):
737
790
  """Run `fn(view)` for each step, until it returns None or a stopping condition holds."""
@@ -96,17 +96,17 @@ def body(skills=()) -> dict:
96
96
  return dict(GENERIC)
97
97
 
98
98
 
99
- def goal(instruction: str, minutes: float, skills=(), sandbox: dict | None = None, *, legacy_memory=False) -> str:
99
+ def goal(instruction: str, skills=(), sandbox: dict | None = None, *, legacy_memory=False) -> str:
100
100
  """The active run's instruction: learn the environment for the task `instruction`, the
101
101
  lines of the prompt for the run's `skills` kept, its body's lines (body()) in it, and,
102
- when the environment offers one, its practice sandbox (sandbox_lines())."""
102
+ when the environment offers one, its practice sandbox (sandbox_lines()). How long the
103
+ run is, it does not say: the host keeps that limit (runtime.tell_budget)."""
103
104
  from harness.runtime.decision.prompts import select
104
105
 
105
106
  features = (*skills, "legacy_memory") if legacy_memory else skills
106
107
  text = select(PROMPT.read_text(encoding="utf-8"), features).strip()
107
108
  lines = body(skills)
108
- text = text.format(task=instruction.strip(), minutes=f"{minutes:g}", probe=lines["try"],
109
- sandbox=sandbox_lines(sandbox))
109
+ text = text.format(task=instruction.strip(), probe=lines["try"], sandbox=sandbox_lines(sandbox))
110
110
  # A placeholder with nothing to say leaves no line.
111
111
  return "\n".join(line for line in text.split("\n") if line.strip() or not line).rstrip()
112
112
 
@@ -239,18 +239,21 @@ WAITS_FOR = {"fixture": "an independent fixture", "own_test": "the host to run i
239
239
  "off": "nothing: the host admits no code here"}
240
240
 
241
241
 
242
- def report(rows: list[dict], seconds_left: float, situations: str | None = None, admission: str = "fixture") -> str:
242
+ def report(rows: list[dict], seconds_left: float | None, situations: str | None = None,
243
+ admission: str = "fixture") -> str:
243
244
  """What an active run has learned so far (progress()): what the library holds, what is
244
245
  pending and what for, and the situations entry when there is one. Facts only: what to do
245
- next is the model's to decide. The lines the model reads after each program of an active
246
- run; `admission` is how the host admits code here (learner.admission)."""
247
- now = progress(rows, seconds_left, 0.0, situations)
246
+ next is the model's to decide. The lines the model reads with each call of an active
247
+ run; `admission` is how the host admits code here (learner.admission). `seconds_left`
248
+ None: the time the run has left is not said (runtime.tell_budget)."""
249
+ now = progress(rows, seconds_left or 0.0, 0.0, situations)
250
+ left = "" if seconds_left is None else f"; {now['seconds_left'] / 60:.0f} min of live time left"
248
251
  tested = now["tested"]
249
252
 
250
253
  def names(found):
251
254
  return ", ".join(found) or "none"
252
255
 
253
- lines = [f"Learned so far ({now['entries']} entries; {now['seconds_left'] / 60:.0f} min of live time left):",
256
+ lines = [f"Learned so far ({now['entries']} entries{left}):",
254
257
  " skills with a passing test: " + (", ".join(
255
258
  f"{row['name']}" + (" [own test]" if row.get("admitted_by") == "own_test" else "")
256
259
  + (f" ({row['uses']} uses, {row['success_rate']:.0%} ok)"
@@ -16,7 +16,7 @@ is a group, and the nodes under it are its content). Three statements are the wh
16
16
  environment; when none has, the model is told what the program did (the event
17
17
  REFLEX_FAILED), once until the node has done its work again.
18
18
 
19
- A node's program runs as a unit whose priority is the node's place in the tree (rank()):
19
+ A node's program runs as a unit whose priority is the node's own priority (rank()):
20
20
  the units' priorities are how the tick lets the earlier take the environment from the later
21
21
  (runtime/tick.py: the later call is paused where the environment pauses, else ended, and
22
22
  its node begins again). Every node's place is above the model's own programs: a reflex
@@ -329,16 +329,15 @@ class Reflexes:
329
329
  return [leaf for child in self.children(name) for leaf in self._leaves(child.name)]
330
330
 
331
331
  def rank(self, node) -> float:
332
- """What a node's program holds the environment with: above the priority of the node
333
- at the top of its part of the tree by a part of 1, the larger the earlier the node
334
- comes among the nodes with a program there. So the tree's order is the order of its
335
- units' priorities, and every node is above the model's programs (0)."""
336
- top = node
337
- while top.under is not None and top.under in self.nodes:
338
- top = self.nodes[top.under]
339
- leaves = self._leaves(top.name)
332
+ """What a node's program holds the environment with: the node's own priority,
333
+ wherever it is in the tree, and above it a part of 1, the larger the earlier the
334
+ node comes in the tree's order among the nodes with a program. So the node that
335
+ runs is what a program's priority (at_priority) and every other node's is compared
336
+ with: the higher has the environment, among equals the earlier in the tree; and
337
+ every node is above the model's programs (0)."""
338
+ leaves = [found for found in self._ordered() if found.do is not None]
340
339
  place = next((index for index, leaf in enumerate(leaves) if leaf is node), 0)
341
- return top.priority + (len(leaves) - place) / (len(leaves) + 1)
340
+ return node.priority + (len(leaves) - place) / (len(leaves) + 1)
342
341
 
343
342
  def listing(self, name=None) -> list:
344
343
  """The nodes at the top of the tree, or the node `name` where it has none under it,
@@ -538,13 +537,16 @@ def judge(session, tree):
538
537
  layer.wake_at = now if layer.wake_at is None else min(layer.wake_at, now)
539
538
  return node.holds
540
539
 
541
- wanting = []
540
+ wanting, telling = [], []
542
541
 
543
542
  def over(node):
544
543
  # What a node's program is for: the situation the node answers is over.
545
544
  return lambda view: not layer._run(node, view)
546
545
 
547
546
  def walk(name):
547
+ # The nodes at `name` whose turn it may be, the earlier first: those with a program
548
+ # (returned), and those that tell the model, each with the ones before it at its place.
549
+ before = []
548
550
  for node in layer.children(name):
549
551
  if node.event is not None or node.stops or layer.nodes.get(node.name) is not node:
550
552
  continue
@@ -554,23 +556,22 @@ def judge(session, tree):
554
556
  fault(node, error)
555
557
  continue
556
558
  if not held:
557
- # The situation is over.
558
- node.raised = node.told = False
559
+ # The situation is over. (Whether the model was told that the node failed
560
+ # stays: it is told again only after the node has done its work, else a
561
+ # condition that comes and goes tells the same failure every time.)
562
+ node.raised = False
559
563
  # (What is under it is judged anew when its turn comes again.)
560
564
  for below in layer._below(node.name)[1:]:
561
- below.holds = _UNSEEN
565
+ below.holds, below.raised = _UNSEEN, False
562
566
  continue
563
567
  if node.action is not None:
564
- if not node.raised and now >= node.ready_at:
565
- node.raised, node.ready_at = True, now + node.again_seconds
566
- node.triggered += 1
567
- event = session._rule_event(node, state)
568
- needs_model = isinstance(event.detail, dict) and event.detail.get("needs_model") is True
569
- emit(tree, None, "reflex", event, notice=not needs_model)
568
+ telling.append((node, list(before)))
570
569
  elif node.do is not None:
571
570
  wanting.append(node)
571
+ before.append(node)
572
572
  else:
573
- walk(node.name)
573
+ before += walk(node.name)
574
+ return before
574
575
 
575
576
  walk(None)
576
577
  running = [entry for entry in tree.running() if entry.kind != "agent"]
@@ -583,7 +584,7 @@ def judge(session, tree):
583
584
  layer.wake_at = node.ready_at if layer.wake_at is None else min(layer.wake_at, node.ready_at)
584
585
  continue
585
586
  rank = layer.rank(node)
586
- above = next((entry for entry in running if (entry.priority or 0) > rank), None)
587
+ above = next((entry for entry in running if entry.rank > rank), None)
587
588
  if above is not None:
588
589
  # What is before it has the environment: its turn is judged when that ends. Where
589
590
  # that is a program that said so (at_priority), the model is told once.
@@ -600,6 +601,18 @@ def judge(session, tree):
600
601
  running.append(node.unit)
601
602
  startable.add(node.name)
602
603
  spans.note("rule", name=node.name, action="began", unit=node.unit.id, priority=rank)
604
+ # A node that tells the model has its turn like any other at its place: when none of
605
+ # those before it whose condition holds is acting. So the last node of a situation
606
+ # tells what nothing answered, and no more.
607
+ for node, before in telling:
608
+ if any(found.name in startable for found in before):
609
+ continue
610
+ if not node.raised and now >= node.ready_at:
611
+ node.raised, node.ready_at = True, now + node.again_seconds
612
+ node.triggered += 1
613
+ # (The event is offered to the nodes that answer its kind; one that none answers
614
+ # reaches the model: that is what telling is.)
615
+ emit(tree, None, "reflex", session._rule_event(node, state))
603
616
  layer.tell()
604
617
  for node, unit in failures:
605
618
  later = layer.children(node.under)