@lmmslab/harness 0.81.1 → 0.82.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 (78) hide show
  1. package/README.md +24 -35
  2. package/package.json +2 -2
  3. package/python/harness/README.md +3 -3
  4. package/python/harness/__init__.py +1 -1
  5. package/python/harness/builtin/configs/assistant.toml +0 -1
  6. package/python/harness/builtin/configs/default.toml +1 -1
  7. package/python/harness/builtin/prompts/conversation/core.md +2 -2
  8. package/python/harness/builtin/prompts/core/core.md +177 -41
  9. package/python/harness/builtin/prompts/core/pack.toml +2 -2
  10. package/python/harness/builtin/prompts/reflect/core.md +1 -1
  11. package/python/harness/builtin/prompts/shared/learning-goal.md +5 -29
  12. package/python/harness/builtin/prompts/shared/learning.md +8 -10
  13. package/python/harness/builtin/prompts/shared/web.md +11 -0
  14. package/python/harness/builtin/skills/agents/__init__.py +24 -22
  15. package/python/harness/builtin/skills/entries/__init__.py +43 -28
  16. package/python/harness/builtin/skills/entries/skill.toml +2 -3
  17. package/python/harness/builtin/skills/environments/__init__.py +2 -9
  18. package/python/harness/builtin/skills/mcp/__init__.py +2 -3
  19. package/python/harness/builtin/skills/memory/__init__.py +2 -5
  20. package/python/harness/builtin/skills/web/__init__.py +92 -0
  21. package/python/harness/builtin/skills/web/skill.toml +9 -0
  22. package/python/harness/cli.py +84 -16
  23. package/python/harness/config/agent.py +33 -9
  24. package/python/harness/core/episodes.py +5 -4
  25. package/python/harness/core/lanes.py +3 -0
  26. package/python/harness/core/memory.py +5 -1
  27. package/python/harness/core/remote.py +4 -3
  28. package/python/harness/core/worker.py +58 -3
  29. package/python/harness/extensions.py +23 -2
  30. package/python/harness/host/__init__.py +43 -1
  31. package/python/harness/host/client.py +36 -6
  32. package/python/harness/host/http.py +19 -0
  33. package/python/harness/host/server.py +17 -3
  34. package/python/harness/host/sessions.py +3 -2
  35. package/python/harness/host/tasks.py +107 -26
  36. package/python/harness/host/workspaces.py +69 -7
  37. package/python/harness/model_list.py +1 -1
  38. package/python/harness/packages/manager.py +5 -1
  39. package/python/harness/providers.py +14 -5
  40. package/python/harness/runtime/decision/loop.py +100 -121
  41. package/python/harness/runtime/decision/messages.py +156 -48
  42. package/python/harness/runtime/decision/model.py +79 -21
  43. package/python/harness/runtime/decision/prompts.py +15 -11
  44. package/python/harness/runtime/decision/protocol.py +12 -6
  45. package/python/harness/runtime/decision/thoughts.py +10 -16
  46. package/python/harness/runtime/events.py +28 -13
  47. package/python/harness/runtime/execution/attached.py +1 -1
  48. package/python/harness/runtime/execution/control.py +17 -35
  49. package/python/harness/runtime/execution/entries.py +204 -124
  50. package/python/harness/runtime/execution/kernel.py +471 -281
  51. package/python/harness/runtime/execution/managed.py +231 -0
  52. package/python/harness/runtime/execution/noenv.py +2 -2
  53. package/python/harness/runtime/execution/results.py +2 -0
  54. package/python/harness/runtime/execution/session.py +276 -115
  55. package/python/harness/runtime/learning/active.py +36 -48
  56. package/python/harness/runtime/learning/fixtures.py +2 -2
  57. package/python/harness/runtime/learning/learner.py +75 -2
  58. package/python/harness/runtime/learning/library.py +77 -10
  59. package/python/harness/runtime/learning/views.py +12 -1
  60. package/python/harness/runtime/reactive/__init__.py +1 -1
  61. package/python/harness/runtime/reactive/rules.py +29 -394
  62. package/python/harness/runtime/reactive/tree.py +611 -0
  63. package/python/harness/runtime/safety/approval.py +8 -3
  64. package/python/harness/runtime/safety/permissions.py +242 -0
  65. package/python/harness/runtime/status.py +7 -2
  66. package/python/harness/runtime/task/delegation.py +97 -58
  67. package/python/harness/runtime/task/environments.py +3 -4
  68. package/python/harness/runtime/task/inbound.py +64 -0
  69. package/python/harness/runtime/task/run.py +1092 -726
  70. package/python/harness/runtime/tick.py +257 -91
  71. package/python/harness/sdk/__init__.py +7 -4
  72. package/python/harness/settings.py +27 -3
  73. package/python/harness/terminal.py +10 -6
  74. package/python/harness/transcript.py +18 -6
  75. package/python/harness/tui/answer.py +51 -0
  76. package/python/harness/tui/chat.py +75 -26
  77. package/python/harness/tui/views.py +1 -0
  78. package/python/harness/builtin/prompts/shared/entries.md +0 -86
@@ -0,0 +1,231 @@
1
+ """The host side of a managed kernel: trusted admission of an isolated kernel's proposals.
2
+
3
+ A managed kernel runs the model's programs in an isolated process that holds nothing but
4
+ the names of the operations it may propose. Its programs are units like those of any
5
+ kernel (runtime/execution/kernel.py: side by side, the reply within the grace, unit_end).
6
+ The session that acts lives here, in the host, and every program of the isolated kernel
7
+ has a unit in this session's tree (runtime/execution/entries.py): a proposal runs as that
8
+ unit, so its executions, events, pictures and priority are the unit's own and are kept by
9
+ the host. Of the isolated process a result takes only the output text and how the code
10
+ ended; everything else is what the host kept.
11
+ """
12
+
13
+ import math
14
+ import threading
15
+ import time
16
+
17
+ from harness.runtime import events as ev
18
+ from harness.runtime.events import EpisodeEnded, Escalation, Event, ExecutionTrace
19
+ from harness.runtime.execution.entries import Cancelled
20
+
21
+
22
+ # What a program's result may say of how its code ended, on the isolated kernel's word.
23
+ STATUSES = ("ok", "error", "finished")
24
+ INTERRUPTED = "The managed program was interrupted"
25
+
26
+
27
+ class Admission:
28
+ """The units of one managed kernel and the admission of their proposals onto
29
+ `session`, for `conversation` (runtime/task/run.py). `operations`: the session's calls a
30
+ program may propose. The kernel handle (runtime/execution/kernel.py Kernel) calls
31
+ begin, running, submit, accept_result and result as the isolated process's messages
32
+ come; stop, resume and cancel are the host's own."""
33
+
34
+ def __init__(self, conversation, session, operations):
35
+ self._conversation, self.session, self.operations = conversation, session, tuple(operations)
36
+ self._condition = threading.Condition()
37
+ # The programs whose result has not come: {cell: {"entry", "last", "thread", "began", "running"}};
38
+ # those whose result came (a late message of theirs is left out).
39
+ self._cells = {}
40
+ self._settled = set()
41
+ self._stopped = False
42
+
43
+ def options(self) -> dict:
44
+ """What the isolated kernel is started with."""
45
+ return {"operations": self.operations}
46
+
47
+ # The host's controls.
48
+
49
+ def stopped(self) -> bool:
50
+ """Whether nothing may act now: the host stopped the units, or its conversation is
51
+ paused or closing."""
52
+ return self._stopped or self._conversation._pause or self._conversation._closing
53
+
54
+ def stop(self) -> None:
55
+ """Stop the units: every proposal is refused until resume()."""
56
+ with self._condition:
57
+ self._stopped = True
58
+
59
+ def resume(self) -> None:
60
+ with self._condition:
61
+ self._stopped = False
62
+
63
+ def cancel(self, unit=None) -> list:
64
+ """Cancel the unit `unit` (None: all): its execution ends and its proposals are
65
+ refused. The cells cancelled."""
66
+ with self._condition:
67
+ cells = [cell for cell, record in self._cells.items() if unit is None or record["entry"].id == unit]
68
+ for cell in cells:
69
+ self._cells[cell]["entry"]._cancel.set()
70
+ return cells
71
+
72
+ def close(self) -> None:
73
+ self.stop()
74
+ self.session.close()
75
+
76
+ # What the kernel handle calls.
77
+
78
+ def begin(self, cell, name=None) -> str:
79
+ """A program is sent to the isolated kernel as `cell`: its unit here. The unit's id."""
80
+ tree = self.session._entries
81
+ entry = tree._add(str(name or f"cell {cell}"), "program", tree.root, (), cell=cell)
82
+ with self._condition:
83
+ self._cells[cell] = {"entry": entry, "last": 0, "thread": None, "began": time.time(), "running": False}
84
+ self._record({"op": "entry_start", "id": entry.id, "parent": tree.root.id, "name": entry.name,
85
+ "kind": "program", "cell": cell})
86
+ return entry.id
87
+
88
+ def unit(self, cell):
89
+ """The id of the unit of `cell`, or None when its result has come."""
90
+ with self._condition:
91
+ record = self._cells.get(cell)
92
+ return record["entry"].id if record is not None else None
93
+
94
+ def running(self, cell):
95
+ """The isolated kernel says `cell` still runs after its grace: the unit's id, or
96
+ None when it may not say so (no such program, or said before)."""
97
+ with self._condition:
98
+ record = self._cells.get(cell) if type(cell) is int else None
99
+ if record is None or record["running"]:
100
+ return None
101
+ record["running"] = True
102
+ return record["entry"].id
103
+
104
+ def settled(self, cell) -> bool:
105
+ """Whether the result of `cell` has come (a later message of it is left out)."""
106
+ with self._condition:
107
+ return type(cell) is int and cell in self._settled
108
+
109
+ def accept_result(self, cell) -> bool:
110
+ """Whether a result for `cell` is taken: once for each program."""
111
+ with self._condition:
112
+ if type(cell) is not int or cell not in self._cells or cell in self._settled:
113
+ return False
114
+ self._settled.add(cell)
115
+ return True
116
+
117
+ def submit(self, message, reply) -> bool:
118
+ """Admit a proposal of a unit: run it as that unit on a thread of its own and
119
+ answer with `reply`. False for a message that breaks the protocol (no such
120
+ program, an answer still due, an id out of order): the kernel handle ends the
121
+ isolated process. A proposal of a unit that is stopped, cancelled or over is
122
+ refused with an answer."""
123
+ with self._condition:
124
+ identifier, cell = message.get("id"), message.get("cell")
125
+ if type(identifier) is not int or type(cell) is not int:
126
+ return False
127
+ record = self._cells.get(cell)
128
+ if record is None:
129
+ return False
130
+ refused = cell in self._settled or record["entry"]._cancel.is_set() or self.stopped()
131
+ if record["thread"] is not None or identifier <= record["last"]:
132
+ return False
133
+ record["last"] = identifier
134
+ if not refused:
135
+ record["thread"] = threading.Thread(target=self._run, args=(record, message, reply), daemon=True,
136
+ name="managed-proposal")
137
+ record["thread"].start()
138
+ return True
139
+ try:
140
+ reply({"error": INTERRUPTED})
141
+ except OSError:
142
+ pass
143
+ return True
144
+
145
+ def _run(self, record, message, reply):
146
+ response = {"error": "The managed proposal ended"}
147
+ try:
148
+ with self.session._entries.acting(record["entry"]):
149
+ response = {"value": self._perform(message.get("name"), message.get("args"), message.get("kwargs"))}
150
+ except (Escalation, EpisodeEnded) as error:
151
+ response = {"error": f"{error.trace.event.kind}: {error.trace.event.detail}"}
152
+ except Cancelled:
153
+ response = {"error": INTERRUPTED}
154
+ except Exception as error:
155
+ response = {"error": str(error)[:500] or type(error).__name__}
156
+ finally:
157
+ with self._condition:
158
+ record["thread"] = None
159
+ self._condition.notify_all()
160
+ try:
161
+ reply(response)
162
+ except OSError:
163
+ pass
164
+
165
+ def _perform(self, name, args, kwargs):
166
+ if (not isinstance(name, str) or name not in self.operations or type(args) is not list
167
+ or type(kwargs) is not dict or len(args) + len(kwargs) > 12
168
+ or any(type(value) not in (str, int, float, bool, type(None)) or
169
+ (isinstance(value, str) and len(value) > 8192)
170
+ or (type(value) is float and not math.isfinite(value))
171
+ for value in [*args, *kwargs.values()])):
172
+ raise ValueError("Unsupported managed workspace proposal")
173
+ with self._conversation._condition:
174
+ if self.stopped():
175
+ raise RuntimeError(INTERRUPTED)
176
+ session = self.session
177
+ if name == "show":
178
+ if args or set(kwargs) - {"caption"}:
179
+ raise ValueError("Managed show() takes only an optional caption")
180
+ session.show(session.get_observation(), kwargs.get("caption"))
181
+ return None
182
+ result = getattr(session, name)(*args, **kwargs)
183
+ if name == "get_observation":
184
+ return {"state": result.state, "images": result._urls}
185
+ if isinstance(result, ExecutionTrace):
186
+ return result.to_dict()
187
+ return result
188
+
189
+ def result(self, message, cell) -> dict:
190
+ """The result of the program `cell` (accept_result took it): of the isolated
191
+ kernel's `message` only the output text and how the code ended; the state, the
192
+ events, the pictures and the views are what this session kept for the unit."""
193
+ with self._condition:
194
+ record = self._cells.pop(cell)
195
+ # (A proposal still at work ends first: what it does is the unit's.)
196
+ entry = record["entry"]
197
+ self._condition.wait_for(lambda: record["thread"] is None, timeout=125)
198
+ from harness.runtime.execution.kernel import told
199
+
200
+ # (A hosted kernel's files are not this host's: what is too long is told by its ends.)
201
+ out = {name: told(message[name], name, failed=True) for name in ("stdout", "error", "finish")
202
+ if isinstance(message.get(name), str)}
203
+ status = message.get("status")
204
+ if status not in STATUSES:
205
+ if entry._cancel.is_set():
206
+ status = "cancelled"
207
+ elif self.stopped():
208
+ # The host stopped it: the pause's event, in the host's own words.
209
+ status, out["escalation"] = "escalated", ExecutionTrace(
210
+ "program", {}, 0, 0, Event(ev.USER_PAUSED, "user")).to_dict()
211
+ else:
212
+ status = "error"
213
+ out.setdefault("error", "The program did not end normally")
214
+ out.update(status=status, unit=entry.id, name=entry.name, kind="program", cell=cell,
215
+ seconds=time.time() - record["began"])
216
+ tree = self.session._entries
217
+ try:
218
+ with tree.acting(entry):
219
+ out.update(self.session.drain())
220
+ except Exception as error:
221
+ out["drain_error"] = f"{type(error).__name__}: {error}"
222
+ ended = "done" if status in ("ok", "finished") else "cancelled" if status in ("cancelled", "escalated") else "failed"
223
+ tree._end(entry, ended, error=out.get("error") if ended == "failed" else None)
224
+ self._record({"op": "entry_end", "id": entry.id, "status": ended})
225
+ return out
226
+
227
+ def _record(self, record) -> None:
228
+ """A unit's start or end, as the conversation hears it of any kernel."""
229
+ hear = getattr(self._conversation, "_kernel_span", None)
230
+ if hear is not None:
231
+ hear({**record, "time": time.time()})
@@ -34,9 +34,9 @@ class NoEnvironment:
34
34
  def reset(self, task: str, seed: int) -> Observation:
35
35
  return Observation(self.episode_id, 0, "", dict(self.state))
36
36
 
37
- def call(self, name: str, **arguments):
37
+ def call(self, name: str, /, **arguments):
38
38
  if name == "profile":
39
- return {"kind": KIND, "name": KIND, "benchmark": KIND, "clock": "events", "control_hz": 1,
39
+ return {"kind": KIND, "name": KIND, "benchmark": KIND, "step_seconds": None, "control_hz": 1,
40
40
  "steps_shown": False}
41
41
  if name == "state":
42
42
  return dict(self.state)
@@ -33,6 +33,8 @@ def _wrap_up(kernel, trace):
33
33
  """The benchmark's end-of-episode convention, the same for every layer: the kernel
34
34
  returns the arms to their start pose when the embodiment profile asks for it."""
35
35
  try:
36
+ # (A paused run that ends is wrapped up all the same.)
37
+ kernel.resume()
36
38
  result = kernel.run("__wrap_up__()", timeout=120)
37
39
  trace.emit("wrap_up", status=result["status"], error=result.get("error"),
38
40
  events=[event.get("event", {}).get("kind") for event in result.get("events") or []])