@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
@@ -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:
@@ -85,8 +85,9 @@ _CORE_SCHEMA = {section: {key: Setting(*row) for key, row in rows.items()} for s
85
85
  },
86
86
  "approvals": {
87
87
  "ask": ("sensitive", "choice", ("sensitive", "programs"),
88
- "what waits for your approval: sensitive (steps that touch credentials, a payment, sending, deleting "
89
- "or cannot be undone; they always wait) or programs (every program as well)"),
88
+ "what waits for your approval: sensitive (steps the environment recognizes as touching credentials, "
89
+ "a payment, sending, deleting or what cannot be undone; they always wait; Permissions lists which "
90
+ "environments check) or programs (every program as well)"),
90
91
  "programs": ("ask_once_session", "choice", ("never", "ask_each", "ask_once_session", "allow"),
91
92
  "what a program of the agent may do on this computer beyond its own folder (run a command, "
92
93
  "reach the network, write a file elsewhere), where no rule says otherwise: never, ask each "
@@ -330,8 +331,12 @@ def read(directory) -> dict:
330
331
  try:
331
332
  with file.open("rb") as handle:
332
333
  data = tomllib.load(handle)
333
- except (OSError, tomllib.TOMLDecodeError):
334
+ except FileNotFoundError:
334
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
335
340
  return data
336
341
 
337
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):