@lmmslab/harness 0.83.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 (32) hide show
  1. package/bin/harness.js +6 -1
  2. package/package.json +1 -1
  3. package/python/harness/__init__.py +1 -1
  4. package/python/harness/builtin/prompts/core/core.md +45 -30
  5. package/python/harness/builtin/prompts/shared/learning-goal.md +1 -1
  6. package/python/harness/cli.py +103 -17
  7. package/python/harness/core/trace.py +23 -3
  8. package/python/harness/core/worker.py +6 -0
  9. package/python/harness/host/client.py +26 -21
  10. package/python/harness/host/http.py +11 -1
  11. package/python/harness/host/sessions.py +38 -3
  12. package/python/harness/host/tasks.py +62 -26
  13. package/python/harness/model_list.py +25 -5
  14. package/python/harness/packages/environments.py +51 -12
  15. package/python/harness/packages/manager.py +379 -102
  16. package/python/harness/packages/resolve.py +52 -15
  17. package/python/harness/packages/sources.py +17 -5
  18. package/python/harness/packages/state.py +10 -0
  19. package/python/harness/providers.py +66 -24
  20. package/python/harness/runtime/decision/model.py +9 -4
  21. package/python/harness/runtime/execution/entries.py +53 -7
  22. package/python/harness/runtime/execution/session.py +52 -7
  23. package/python/harness/runtime/learning/active.py +4 -4
  24. package/python/harness/runtime/reactive/tree.py +35 -22
  25. package/python/harness/runtime/task/run.py +25 -3
  26. package/python/harness/runtime/tick.py +34 -3
  27. package/python/harness/settings.py +5 -1
  28. package/python/harness/tui/__init__.py +28 -6
  29. package/python/harness/tui/chat.py +243 -67
  30. package/python/harness/tui/pty.py +16 -0
  31. package/python/harness/tui/renderer.py +34 -0
  32. package/python/harness/tui/views.py +160 -38
@@ -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)
@@ -207,6 +207,9 @@ class TaskRun:
207
207
  self._upto = LATER
208
208
  self._unanswered = set()
209
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()
210
213
  self._urgent = 0
211
214
  self._state_now = None
212
215
  self._pause = False
@@ -1132,8 +1135,17 @@ class TaskRun:
1132
1135
  """Whether a message waits to be told (the caller holds the condition)."""
1133
1136
  return bool(self._notices.pending(kinds=("message",)))
1134
1137
 
1138
+ @staticmethod
1139
+ def _event_key(record) -> tuple:
1140
+ event = record.get("event") or {}
1141
+ return event.get("kind"), event.get("source")
1142
+
1135
1143
  def _escalation(self, record, priority=NOW):
1136
- """An event for the model: a notice, at once unless its source says otherwise."""
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."""
1137
1149
  with self._condition:
1138
1150
  identifier = record["id"]
1139
1151
  if identifier in self._seen_events:
@@ -1141,6 +1153,15 @@ class TaskRun:
1141
1153
  self._seen_events.add(identifier)
1142
1154
  self.trace.emit("escalation", **{key: value for key, value in record.items() if key != "event"},
1143
1155
  reported=record["event"])
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
1144
1165
  self._put("event", priority, record, reason="unhandled escalation")
1145
1166
 
1146
1167
  def _held_back(self, record: dict) -> None:
@@ -2184,8 +2205,7 @@ class TaskRun:
2184
2205
 
2185
2206
  if active.is_active(runtime):
2186
2207
  # The active trigger: this run learns the environment for the task (runtime/learning/active.py).
2187
- instruction = active.goal(instruction, active.schedule(runtime.get("library"))["minutes"],
2188
- runtime.get("skills") or (), sandbox_of(runtime),
2208
+ instruction = active.goal(instruction, runtime.get("skills") or (), sandbox_of(runtime),
2189
2209
  legacy_memory="legacy_memory" in EMBODIMENT_KINDS[embodiment].prompt_features)
2190
2210
  self._said.insert(0, instruction)
2191
2211
  opening = f"Instruction: {instruction}\n"
@@ -2433,6 +2453,7 @@ class TaskRun:
2433
2453
  self._settle(current, "used")
2434
2454
  self._unanswered.clear()
2435
2455
  self._told_events = []
2456
+ self._told_sources.clear()
2436
2457
  loop.revision, loop.urgent = current.revision, asked
2437
2458
  loop.call, loop.thought = current.call, current.id
2438
2459
  if policy:
@@ -2902,6 +2923,7 @@ class TaskRun:
2902
2923
  self._unanswered.add(notice.kind)
2903
2924
  if notice.kind == "event":
2904
2925
  self._told_events.append(notice.data.get("id"))
2926
+ self._told_sources.add(self._event_key(notice.data))
2905
2927
  if not taken and reason in ("error", "escalation"):
2906
2928
  self._reflect_learning()
2907
2929
  if taken is not None:
@@ -104,8 +104,11 @@ class Preempted(Exception):
104
104
 
105
105
 
106
106
  def _rank(unit) -> float:
107
- """What a unit holds the embodiment with: its priority."""
108
- return getattr(unit, "priority", 0) or 0
107
+ """What a unit holds the embodiment with now: its priority for the action it does (a
108
+ program's `with at_priority(n):`, a reflex's place), or what the code it runs in the
109
+ environment asks for (runtime/execution/entries.py Entry.rank)."""
110
+ rank = getattr(unit, "rank", None)
111
+ return rank if rank is not None else (getattr(unit, "priority", 0) or 0)
109
112
 
110
113
 
111
114
  class _Entry:
@@ -117,7 +120,6 @@ class _Entry:
117
120
  self.steps = None if callable(steps) else steps
118
121
  self.parent = parent
119
122
  self.owner = owner
120
- self.priority = _rank(owner)
121
123
  self.restart = getattr(owner, "restart", False)
122
124
  self.restarts = 0
123
125
  self.picked = False
@@ -139,6 +141,12 @@ class _Entry:
139
141
  self.fresh = None
140
142
  self.change = None
141
143
 
144
+ @property
145
+ def priority(self) -> float:
146
+ """What its unit holds the embodiment with now: it may change while the execution
147
+ runs (a block of `with at_priority(n):` ends, the environment's code asks for one)."""
148
+ return _rank(self.owner)
149
+
142
150
 
143
151
  class Tick:
144
152
  """The tick of one kernel session (see the module docstring): `run(steps)` runs an
@@ -430,7 +438,29 @@ class Tick:
430
438
  return
431
439
  spans.note("call_resumed", unit=getattr(entry.owner, "id", None), name=getattr(entry.owner, "name", None))
432
440
 
441
+ def _asked(self, entries) -> None:
442
+ """The priority the environment's own code asks for now (its state's "priority":
443
+ code that runs there for long holds the environment for one action of it, and says
444
+ so itself) is that of the unit whose execution runs."""
445
+ asked = self.session.state.get("priority")
446
+ asked = asked if isinstance(asked, (int, float)) and not isinstance(asked, bool) and asked > 0 else None
447
+ for entry in entries:
448
+ owner = entry.owner
449
+ if owner is None or not hasattr(owner, "asked") or owner.asked == asked:
450
+ continue
451
+ owner.asked = asked
452
+ spans.note("priority", unit=getattr(owner, "id", None), priority=_rank(owner), asked=asked)
453
+ if asked is not None and owner not in self._holders:
454
+ self._holders.append(owner)
455
+ layer = getattr(self.session, "reactive", None)
456
+ if layer is not None:
457
+ layer.changed = True
458
+ with self._changed:
459
+ self._judge = True
460
+
433
461
  def _end(self, entry):
462
+ if getattr(entry.owner, "asked", None) is not None:
463
+ entry.owner.asked = None
434
464
  with self._changed:
435
465
  self._undrive(entry)
436
466
  if entry in self._entries:
@@ -556,6 +586,7 @@ class Tick:
556
586
  if (self.bound and (entries or self._line)) or waiting or timed or (
557
587
  self._line and layer is not None and layer.stops()):
558
588
  session._observe()
589
+ self._asked(entries)
559
590
  if session.state.get("ended"):
560
591
  self._line.clear()
561
592
  with self._changed:
@@ -331,8 +331,12 @@ def read(directory) -> dict:
331
331
  try:
332
332
  with file.open("rb") as handle:
333
333
  data = tomllib.load(handle)
334
- except (OSError, tomllib.TOMLDecodeError):
334
+ except FileNotFoundError:
335
335
  return {}
336
+ except UnicodeError:
337
+ raise ValueError(f"{file}: invalid UTF-8; correct the configuration before saving changes") from None
338
+ except (OSError, tomllib.TOMLDecodeError) as error:
339
+ raise ValueError(f"{file}: {error}; correct the configuration before saving changes") from None
336
340
  return data
337
341
 
338
342
 
@@ -5,12 +5,13 @@ the chat (tui/chat.py, tui/views.py). Records are read as every frontend reads t
5
5
  (transcript.py), commands come from the one table (terminal.COMMANDS).
6
6
 
7
7
  prompt_toolkit (input, keys, pickers) and rich (Markdown, colour) draw it; neither is a
8
- dependency of the harness. When this interpreter cannot import them, the first use
8
+ dependency of the harness. When this interpreter lacks their declared pins, the first use
9
9
  installs them pinned into an environment of their own (HOME/envs/tui/<hash>/,
10
- environments.py) and adds its packages to this process's path, after its own.
10
+ environments.py) and gives those pinned packages precedence in this process's path.
11
11
  """
12
12
 
13
13
  import importlib.util
14
+ from importlib import metadata
14
15
  import subprocess
15
16
  import sys
16
17
 
@@ -27,17 +28,38 @@ def environment() -> environments.Environment:
27
28
  return environments.resolve("tui", spec)
28
29
 
29
30
 
31
+ def _compatible() -> bool:
32
+ """Check only the six declared distributions, without running pip or importing UI code."""
33
+ for requirement in LIBRARIES + PINS:
34
+ name, version = requirement.split("==", 1)
35
+ try:
36
+ if metadata.version(name) != version:
37
+ return False
38
+ except metadata.PackageNotFoundError:
39
+ return False
40
+ return all(importlib.util.find_spec(name) is not None for name in ("prompt_toolkit", "rich"))
41
+
42
+
30
43
  def load(*, log=print) -> None:
31
- """Make prompt_toolkit and rich importable here: this interpreter's, else the TUI's
32
- environment's packages (installed first if need be) after this interpreter's own."""
33
- if all(importlib.util.find_spec(name) is not None for name in ("prompt_toolkit", "rich")):
44
+ """Use the pinned TUI libraries here, preparing the managed environment if needed."""
45
+ if _compatible():
34
46
  return
47
+ modules = ("prompt_toolkit", "rich", "wcwidth", "markdown_it", "mdurl", "pygments")
48
+ if any(name in sys.modules for name in modules):
49
+ raise RuntimeError("incompatible terminal libraries are already loaded; restart harness in a clean "
50
+ "Python environment, or install its terminal pins with "
51
+ f"{sys.executable} -m pip install " + " ".join(LIBRARIES + PINS))
35
52
  env = environments.on_first_use(environment(), "prompt_toolkit and rich for the terminal chat", log=log)
36
53
  packages = subprocess.run([str(env.python), "-I", "-X", "utf8", "-c",
37
54
  "import sysconfig; print(sysconfig.get_path('purelib'))"],
38
55
  capture_output=True, encoding="utf-8", check=True).stdout.strip()
39
- sys.path.append(packages)
56
+ # The managed versions must precede incompatible importable libraries in the
57
+ # host interpreter. Appending would leave the incompatible copies selected.
58
+ sys.path.insert(0, packages)
40
59
  importlib.invalidate_caches()
60
+ if not _compatible():
61
+ raise RuntimeError(f"terminal libraries in {env.directory} do not match the declared pins; "
62
+ "remove that managed environment and restart harness to rebuild it")
41
63
 
42
64
 
43
65
  def chat(conversation=None, records=None, *, host=None, start=None, log=print):