@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.
- package/README.md +24 -35
- package/package.json +2 -2
- package/python/harness/README.md +3 -3
- package/python/harness/__init__.py +1 -1
- package/python/harness/builtin/configs/assistant.toml +0 -1
- package/python/harness/builtin/configs/default.toml +1 -1
- package/python/harness/builtin/prompts/conversation/core.md +2 -2
- package/python/harness/builtin/prompts/core/core.md +177 -41
- package/python/harness/builtin/prompts/core/pack.toml +2 -2
- package/python/harness/builtin/prompts/reflect/core.md +1 -1
- package/python/harness/builtin/prompts/shared/learning-goal.md +5 -29
- package/python/harness/builtin/prompts/shared/learning.md +8 -10
- package/python/harness/builtin/prompts/shared/web.md +11 -0
- package/python/harness/builtin/skills/agents/__init__.py +24 -22
- package/python/harness/builtin/skills/entries/__init__.py +43 -28
- package/python/harness/builtin/skills/entries/skill.toml +2 -3
- package/python/harness/builtin/skills/environments/__init__.py +2 -9
- package/python/harness/builtin/skills/mcp/__init__.py +2 -3
- package/python/harness/builtin/skills/memory/__init__.py +2 -5
- package/python/harness/builtin/skills/web/__init__.py +92 -0
- package/python/harness/builtin/skills/web/skill.toml +9 -0
- package/python/harness/cli.py +84 -16
- package/python/harness/config/agent.py +33 -9
- package/python/harness/core/episodes.py +5 -4
- package/python/harness/core/lanes.py +3 -0
- package/python/harness/core/memory.py +5 -1
- package/python/harness/core/remote.py +4 -3
- package/python/harness/core/worker.py +58 -3
- package/python/harness/extensions.py +23 -2
- package/python/harness/host/__init__.py +43 -1
- package/python/harness/host/client.py +36 -6
- package/python/harness/host/http.py +19 -0
- package/python/harness/host/server.py +17 -3
- package/python/harness/host/sessions.py +3 -2
- package/python/harness/host/tasks.py +107 -26
- package/python/harness/host/workspaces.py +69 -7
- package/python/harness/model_list.py +1 -1
- package/python/harness/packages/manager.py +5 -1
- package/python/harness/providers.py +14 -5
- package/python/harness/runtime/decision/loop.py +100 -121
- package/python/harness/runtime/decision/messages.py +156 -48
- package/python/harness/runtime/decision/model.py +79 -21
- package/python/harness/runtime/decision/prompts.py +15 -11
- package/python/harness/runtime/decision/protocol.py +12 -6
- package/python/harness/runtime/decision/thoughts.py +10 -16
- package/python/harness/runtime/events.py +28 -13
- package/python/harness/runtime/execution/attached.py +1 -1
- package/python/harness/runtime/execution/control.py +17 -35
- package/python/harness/runtime/execution/entries.py +204 -124
- package/python/harness/runtime/execution/kernel.py +471 -281
- package/python/harness/runtime/execution/managed.py +231 -0
- package/python/harness/runtime/execution/noenv.py +2 -2
- package/python/harness/runtime/execution/results.py +2 -0
- package/python/harness/runtime/execution/session.py +276 -115
- package/python/harness/runtime/learning/active.py +36 -48
- package/python/harness/runtime/learning/fixtures.py +2 -2
- package/python/harness/runtime/learning/learner.py +75 -2
- package/python/harness/runtime/learning/library.py +77 -10
- package/python/harness/runtime/learning/views.py +12 -1
- package/python/harness/runtime/reactive/__init__.py +1 -1
- package/python/harness/runtime/reactive/rules.py +29 -394
- package/python/harness/runtime/reactive/tree.py +611 -0
- package/python/harness/runtime/safety/approval.py +8 -3
- package/python/harness/runtime/safety/permissions.py +242 -0
- package/python/harness/runtime/status.py +7 -2
- package/python/harness/runtime/task/delegation.py +97 -58
- package/python/harness/runtime/task/environments.py +3 -4
- package/python/harness/runtime/task/inbound.py +64 -0
- package/python/harness/runtime/task/run.py +1092 -726
- package/python/harness/runtime/tick.py +257 -91
- package/python/harness/sdk/__init__.py +7 -4
- package/python/harness/settings.py +27 -3
- package/python/harness/terminal.py +10 -6
- package/python/harness/transcript.py +18 -6
- package/python/harness/tui/answer.py +51 -0
- package/python/harness/tui/chat.py +75 -26
- package/python/harness/tui/views.py +1 -0
- package/python/harness/builtin/prompts/shared/entries.md +0 -86
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
"""Observe,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"""Observe, judge the rules and deliver one worker command per control period.
|
|
2
|
+
|
|
3
|
+
The tick exists while an execution or a rule on time needs it, and not otherwise: nothing
|
|
4
|
+
is driven by time unless a unit asks for it. Single-resource executions take turns and
|
|
5
|
+
receive the receipts for their own commands. With channels, executions advance
|
|
6
|
+
concurrently, merging their inputs with what units hold before local admission. Delayed
|
|
7
|
+
actions retain their admission and attribution until delivery. The worker shield judges
|
|
8
|
+
every delivered command.
|
|
7
9
|
"""
|
|
8
10
|
|
|
9
11
|
import collections
|
|
@@ -19,10 +21,22 @@ from harness.runtime.safety.approval import WORKER_APPROVAL_SECONDS
|
|
|
19
21
|
from harness.runtime.execution import control
|
|
20
22
|
from harness.runtime import events as ev
|
|
21
23
|
from harness.runtime.reactive import rules as reactive
|
|
24
|
+
from harness.runtime.reactive import tree as reflexes
|
|
22
25
|
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
def driving(profile) -> tuple:
|
|
28
|
+
"""How an environment says it can be driven: (the most commands a second it takes, None:
|
|
29
|
+
as fast as it answers; the time one step of it lasts, None: its time is the wall's).
|
|
30
|
+
From the profile's `max_hz` and `step_seconds`; a profile that still names a `clock`
|
|
31
|
+
is read as what that mode meant: steps (or none), each step lasts 1 / control_hz;
|
|
32
|
+
wall, at most control_hz commands a second; events, neither."""
|
|
33
|
+
profile = profile or {}
|
|
34
|
+
hz = float(profile.get("control_hz", 1))
|
|
35
|
+
named = "max_hz" in profile or "step_seconds" in profile
|
|
36
|
+
clock = None if named else str(profile.get("clock") or "steps")
|
|
37
|
+
bound = profile.get("max_hz") or (hz if clock == "wall" else None)
|
|
38
|
+
step = profile.get("step_seconds") or (1.0 / hz if clock == "steps" else None)
|
|
39
|
+
return (float(bound) if bound else None, float(step) if step else None)
|
|
26
40
|
|
|
27
41
|
|
|
28
42
|
def delay_ticks(reaction_delay_ms, hz) -> int:
|
|
@@ -36,12 +50,12 @@ def delay_ticks(reaction_delay_ms, hz) -> int:
|
|
|
36
50
|
|
|
37
51
|
# How often the code waiting for its execution looks for a reason to stop waiting.
|
|
38
52
|
POLL = 0.05
|
|
39
|
-
# How often a
|
|
53
|
+
# How often an environment driven at a rate tells the host how its periods went (a tick_periods note).
|
|
40
54
|
PERIODS_NOTE_SECONDS = 10.0
|
|
41
55
|
|
|
42
56
|
|
|
43
57
|
class _Periods:
|
|
44
|
-
"""
|
|
58
|
+
"""The periods of an environment driven at a rate since the last tick_periods note: their lengths and parts
|
|
45
59
|
(observe, decide, send), in ms."""
|
|
46
60
|
|
|
47
61
|
PARTS = ("observe", "decide", "send")
|
|
@@ -80,11 +94,32 @@ class _Periods:
|
|
|
80
94
|
self.lengths, self.parts = [], {name: [] for name in self.PARTS}
|
|
81
95
|
|
|
82
96
|
|
|
97
|
+
class Preempted(Exception):
|
|
98
|
+
"""Raised in an execution's steps where they wait for a step's reply: the unit `by`, of
|
|
99
|
+
`priority`, takes the embodiment."""
|
|
100
|
+
|
|
101
|
+
def __init__(self, by=None, priority=0):
|
|
102
|
+
super().__init__(f"preempted by {by}")
|
|
103
|
+
self.by, self.priority = by, priority
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _rank(unit) -> float:
|
|
107
|
+
"""What a unit holds the embodiment with: its priority."""
|
|
108
|
+
return getattr(unit, "priority", 0) or 0
|
|
109
|
+
|
|
110
|
+
|
|
83
111
|
class _Entry:
|
|
84
112
|
def __init__(self, steps, parent, owner=None):
|
|
85
|
-
|
|
113
|
+
# `steps`: the execution's generator, or what makes one (then, where its unit asked
|
|
114
|
+
# to be started again, it is made anew after a unit of higher priority took the
|
|
115
|
+
# embodiment from it).
|
|
116
|
+
self.make = steps if callable(steps) else None
|
|
117
|
+
self.steps = None if callable(steps) else steps
|
|
86
118
|
self.parent = parent
|
|
87
119
|
self.owner = owner
|
|
120
|
+
self.priority = _rank(owner)
|
|
121
|
+
self.restart = getattr(owner, "restart", False)
|
|
122
|
+
self.restarts = 0
|
|
88
123
|
self.picked = False
|
|
89
124
|
self.started = False
|
|
90
125
|
self.trace = None
|
|
@@ -94,6 +129,9 @@ class _Entry:
|
|
|
94
129
|
self.queued = 0
|
|
95
130
|
self.generation = 0
|
|
96
131
|
self.suspended = None
|
|
132
|
+
# What the environment gave when it paused this execution for a unit of higher
|
|
133
|
+
# priority (Tick._pause); None while it is not paused.
|
|
134
|
+
self.paused = None
|
|
97
135
|
self.execution_id = uuid.uuid4().hex
|
|
98
136
|
self.plan = []
|
|
99
137
|
self.uses = frozenset()
|
|
@@ -103,50 +141,51 @@ class _Entry:
|
|
|
103
141
|
|
|
104
142
|
|
|
105
143
|
class Tick:
|
|
106
|
-
"""The tick of one kernel session (see the module docstring): `run(steps)` runs
|
|
107
|
-
|
|
108
|
-
it. `
|
|
144
|
+
"""The tick of one kernel session (see the module docstring): `run(steps)` runs an
|
|
145
|
+
execution on it, `judge()` has the rules judged, `stop()` ends it. `ticks` counts the
|
|
146
|
+
steps it sent. `bound`: the most commands a second the environment takes (its periods
|
|
147
|
+
are paced then), `step_seconds`: the time one step of it lasts (driving())."""
|
|
109
148
|
|
|
110
149
|
def __init__(self, session):
|
|
111
150
|
self.session = session
|
|
112
|
-
self.
|
|
113
|
-
self.
|
|
114
|
-
if self.clock not in CLOCKS:
|
|
115
|
-
raise ValueError(f"A worker's clock is one of {list(CLOCKS)}, got {self.clock!r}")
|
|
151
|
+
self.bound, self.step_seconds = driving(getattr(session, "profile", None))
|
|
152
|
+
self.period = 1.0 / self.bound if self.bound else POLL
|
|
116
153
|
self.delay_ticks = delay_ticks(session.__dict__.get("reaction_delay_ms", 0.0), session.hz)
|
|
117
154
|
self.ticks = self._period = 0
|
|
118
155
|
self._line = collections.deque()
|
|
119
156
|
self._entries = []
|
|
157
|
+
# The units that took the embodiment from a lower one: each holds it until it ends.
|
|
158
|
+
self._holders = []
|
|
120
159
|
self._changed = threading.Condition()
|
|
121
160
|
self._thread = None
|
|
122
|
-
self.
|
|
161
|
+
self._judge = False
|
|
162
|
+
# A judgement that came due while the units were paused: made when they go on.
|
|
163
|
+
self._held_judgement = False
|
|
123
164
|
self._stopped = False
|
|
124
165
|
self._due = None
|
|
125
166
|
# Channels (see the module docstring): {name: channel}, empty when the embodiment
|
|
126
167
|
# is one resource; who claims each, the execution that drives each, what each
|
|
127
168
|
# entry holds down, and whether that changed since the last command went out.
|
|
169
|
+
self._listened = 0.0
|
|
128
170
|
self.channels = dict(getattr(session, "channels", None) or {})
|
|
129
171
|
self._claimed = {}
|
|
130
172
|
self._driving = {}
|
|
131
173
|
self._held = {}
|
|
132
174
|
self._dirty = False
|
|
133
175
|
self._sent_held = None
|
|
134
|
-
self._periods = _Periods(self.period) if self.
|
|
176
|
+
self._periods = _Periods(self.period) if self.bound else None
|
|
135
177
|
|
|
136
178
|
# What the session calls.
|
|
137
179
|
|
|
138
|
-
def
|
|
139
|
-
"""
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
self._wake()
|
|
145
|
-
|
|
146
|
-
def _wake(self):
|
|
147
|
-
"""Wake the tick when an observation rule is registered."""
|
|
180
|
+
def judge(self):
|
|
181
|
+
"""Have the rules judged: one was set, what they read changed, or a rule's program
|
|
182
|
+
ended. Nothing where no rule is judged (no rule, no tick)."""
|
|
183
|
+
layer = getattr(self.session, "reactive", None)
|
|
184
|
+
if layer is None or not (layer.reads_changes() or layer.next_due(0.0) is not None or self._listens()):
|
|
185
|
+
return
|
|
148
186
|
with self._changed:
|
|
149
187
|
if not self._stopped:
|
|
188
|
+
self._judge = True
|
|
150
189
|
self._ensure_thread()
|
|
151
190
|
self._changed.notify_all()
|
|
152
191
|
|
|
@@ -167,7 +206,17 @@ class Tick:
|
|
|
167
206
|
return self._thread is not None and threading.current_thread() is self._thread
|
|
168
207
|
|
|
169
208
|
def run(self, steps):
|
|
170
|
-
"""Run the execution on the tick and return its trace, or raise its error.
|
|
209
|
+
"""Run the execution on the tick and return its trace, or raise its error.
|
|
210
|
+
`steps`: its generator, or a function that makes it. Without channels the
|
|
211
|
+
embodiment is held by priority: the execution of the unit of highest priority
|
|
212
|
+
advances (the oldest among equals), and it ends a lower unit's execution where
|
|
213
|
+
that is: with PREEMPTED and what it had done, and the model is told at once
|
|
214
|
+
(_preempt). The unit that took the embodiment holds it until it ends: what lower
|
|
215
|
+
units start meanwhile waits, and goes on then. A unit that said restart=True has
|
|
216
|
+
its execution started again then instead (its trace counts the `restarts`).
|
|
217
|
+
Where the environment pauses (its profile says "pauses"), the lower execution
|
|
218
|
+
does not end: the environment holds what it was doing, and it goes on from there
|
|
219
|
+
when the higher unit has ended (_pause, _resume)."""
|
|
171
220
|
sys.audit("harness.execution")
|
|
172
221
|
tree = self._tree()
|
|
173
222
|
entry = _Entry(steps, spans.current(), tree.current() if tree is not None else None)
|
|
@@ -198,11 +247,12 @@ class Tick:
|
|
|
198
247
|
if tree is None:
|
|
199
248
|
continue
|
|
200
249
|
with self._changed:
|
|
201
|
-
if entry.picked or tree.interruption(entry.owner, take=False) is None:
|
|
250
|
+
if (entry.picked and entry.paused is None) or tree.interruption(entry.owner, take=False) is None:
|
|
202
251
|
continue
|
|
203
252
|
self._entries.remove(entry)
|
|
204
253
|
entry.done.set()
|
|
205
|
-
entry.steps
|
|
254
|
+
if entry.steps is not None:
|
|
255
|
+
entry.steps.close()
|
|
206
256
|
tree.check(entry.owner)
|
|
207
257
|
except BaseException:
|
|
208
258
|
entry.cancelled = True
|
|
@@ -219,32 +269,70 @@ class Tick:
|
|
|
219
269
|
self._thread = threading.Thread(target=self._loop, name="harness-tick", daemon=True)
|
|
220
270
|
self._thread.start()
|
|
221
271
|
|
|
222
|
-
def
|
|
272
|
+
def _listens(self) -> bool:
|
|
273
|
+
"""Whether a handler waits for an event the environment itself reports (those are
|
|
274
|
+
read on time, at the environment's rate)."""
|
|
275
|
+
kinds = getattr(self.session, "observation_event_kinds", ())
|
|
223
276
|
layer = getattr(self.session, "reactive", None)
|
|
224
|
-
return (layer is not None
|
|
225
|
-
|
|
226
|
-
|
|
277
|
+
return bool(kinds) and layer is not None and layer.listens(kinds)
|
|
278
|
+
|
|
279
|
+
def _timed(self, now):
|
|
280
|
+
"""Seconds until the rules need the tick on time (a rule with a period, a handler
|
|
281
|
+
of the environment's own events), None when none does or the episode has ended."""
|
|
282
|
+
layer = getattr(self.session, "reactive", None)
|
|
283
|
+
if layer is None or (self.session.state or {}).get("ended") or control.pause_requested():
|
|
284
|
+
return None
|
|
285
|
+
due = layer.next_due(now)
|
|
286
|
+
if self._listens():
|
|
287
|
+
listen = max(0.0, self._listened + layer.period - now)
|
|
288
|
+
due = listen if due is None else min(due, listen)
|
|
289
|
+
return due
|
|
227
290
|
|
|
228
291
|
def _loop(self):
|
|
229
292
|
while True:
|
|
230
293
|
with self._changed:
|
|
231
|
-
while not
|
|
232
|
-
|
|
233
|
-
|
|
294
|
+
while not self._stopped:
|
|
295
|
+
if self._held_judgement and not control.pause_requested():
|
|
296
|
+
self._held_judgement, self._judge = False, True
|
|
297
|
+
if self._entries or self._dirty or self._line or self._judge:
|
|
298
|
+
break
|
|
299
|
+
due = self._timed(time.monotonic())
|
|
300
|
+
if due is not None and due <= 0:
|
|
301
|
+
break
|
|
302
|
+
# (While paused the rules are held: the pause is looked at again shortly.)
|
|
303
|
+
self._changed.wait(POLL if due is None and control.pause_requested() else due)
|
|
234
304
|
if self._stopped:
|
|
235
305
|
return
|
|
236
|
-
entries = list(self._entries)
|
|
306
|
+
entries, lower = list(self._entries), []
|
|
237
307
|
if not self.channels:
|
|
238
308
|
suspended = next((entry for entry in entries if entry.suspended is not None), None)
|
|
239
|
-
|
|
309
|
+
if suspended is not None:
|
|
310
|
+
# (An action that waits for its receipt keeps the embodiment.)
|
|
311
|
+
entries = [suspended]
|
|
312
|
+
elif entries:
|
|
313
|
+
top = max(entries, key=lambda entry: entry.priority)
|
|
314
|
+
if top.priority < self._held_at():
|
|
315
|
+
# A unit that took the embodiment still runs: lower ones wait for its end.
|
|
316
|
+
entries = []
|
|
317
|
+
self._changed.wait(POLL)
|
|
318
|
+
else:
|
|
319
|
+
lower = [entry for entry in entries if entry.priority < top.priority and entry.started
|
|
320
|
+
and entry.trace is None and not entry.done.is_set()]
|
|
321
|
+
entries = [top]
|
|
240
322
|
for entry in entries:
|
|
241
323
|
entry.picked = True
|
|
242
|
-
|
|
243
|
-
|
|
324
|
+
for entry in lower:
|
|
325
|
+
self._preempt(entry, entries[0])
|
|
326
|
+
for entry in entries:
|
|
327
|
+
if entry.paused is not None:
|
|
328
|
+
self._resume(entry)
|
|
329
|
+
idle = bool(entries) and all(entry.suspended for entry in entries)
|
|
330
|
+
if entries or self._line or self._dirty:
|
|
331
|
+
self._wait_turn(idle=idle)
|
|
244
332
|
self._step(entries)
|
|
245
333
|
|
|
246
334
|
def _wait_turn(self, idle=False):
|
|
247
|
-
if self.
|
|
335
|
+
if not self.bound and not idle:
|
|
248
336
|
return
|
|
249
337
|
now = time.monotonic()
|
|
250
338
|
if self._due is not None and now < self._due:
|
|
@@ -253,6 +341,95 @@ class Tick:
|
|
|
253
341
|
self._due = self._due + self.period if self._due is not None and self._due + self.period > now \
|
|
254
342
|
else now + self.period
|
|
255
343
|
|
|
344
|
+
def _held_at(self) -> float:
|
|
345
|
+
"""The priority the embodiment is held with: the highest of the units that took it
|
|
346
|
+
from a lower one and have not ended (nothing below it starts an execution)."""
|
|
347
|
+
self._holders = [unit for unit in self._holders if getattr(unit, "running", False)]
|
|
348
|
+
return max((_rank(unit) for unit in self._holders), default=float("-inf"))
|
|
349
|
+
|
|
350
|
+
def hold(self, unit) -> None:
|
|
351
|
+
"""`unit` said with what priority it holds the embodiment (at_priority): it holds it
|
|
352
|
+
with that from now until it ends, between its executions too."""
|
|
353
|
+
with self._changed:
|
|
354
|
+
if unit not in self._holders:
|
|
355
|
+
self._holders.append(unit)
|
|
356
|
+
self._changed.notify_all()
|
|
357
|
+
|
|
358
|
+
def _preempt(self, lower, higher):
|
|
359
|
+
"""The unit of `higher` takes the embodiment: the execution `lower` ends where it
|
|
360
|
+
is, with PREEMPTED and what it had done, and the unit of `higher` holds the
|
|
361
|
+
embodiment until it ends. The `preempted` note tells the conversation at once,
|
|
362
|
+
which tells the model: which unit (and rule) took it, from which unit and
|
|
363
|
+
execution, and the state. An execution whose unit said restart=True is made anew
|
|
364
|
+
when its turn comes again instead."""
|
|
365
|
+
tree, trace = self._tree(), None
|
|
366
|
+
by = getattr(higher.owner, "id", None)
|
|
367
|
+
if higher.owner is not None and higher.owner not in self._holders:
|
|
368
|
+
self._holders.append(higher.owner)
|
|
369
|
+
if lower.paused is not None:
|
|
370
|
+
return
|
|
371
|
+
if getattr(self.session, "pauses", False) and not (lower.restart and lower.make is not None):
|
|
372
|
+
self._pause(lower, higher)
|
|
373
|
+
return
|
|
374
|
+
try:
|
|
375
|
+
with spans.under(lower.parent), _acting(tree, lower.owner):
|
|
376
|
+
try:
|
|
377
|
+
lower.steps.throw(Preempted(by, higher.priority))
|
|
378
|
+
except StopIteration as returned:
|
|
379
|
+
trace = returned.value
|
|
380
|
+
else:
|
|
381
|
+
lower.steps.close()
|
|
382
|
+
except BaseException as error:
|
|
383
|
+
self._fail(lower, error)
|
|
384
|
+
return
|
|
385
|
+
self._discard_plan(lower)
|
|
386
|
+
lower.fresh = lower.check = None
|
|
387
|
+
if trace is not None and trace.event.kind != ev.PREEMPTED:
|
|
388
|
+
# It ended by itself at that moment (a rule had just ended it): that is its end.
|
|
389
|
+
lower.trace = trace
|
|
390
|
+
self._end(lower)
|
|
391
|
+
return
|
|
392
|
+
spans.note("preempted", unit=getattr(lower.owner, "id", None), name=getattr(lower.owner, "name", None),
|
|
393
|
+
execution=getattr(trace, "skill", None), by=by, by_name=getattr(higher.owner, "name", None),
|
|
394
|
+
rule=getattr(higher.owner, "rule", None), priority=getattr(higher.owner, "priority", 0) or 0,
|
|
395
|
+
restart=bool(lower.restart and lower.make is not None), state=dict(self.session.state or {}),
|
|
396
|
+
**({"of_reflex": True} if getattr(lower.owner, "rule", None) else {}))
|
|
397
|
+
if lower.restart and lower.make is not None:
|
|
398
|
+
lower.steps, lower.started = None, False
|
|
399
|
+
lower.restarts += 1
|
|
400
|
+
return
|
|
401
|
+
if trace is None:
|
|
402
|
+
self._fail(lower, RuntimeError("the execution was preempted and gave no trace"))
|
|
403
|
+
return
|
|
404
|
+
lower.trace = trace
|
|
405
|
+
self._end(lower)
|
|
406
|
+
|
|
407
|
+
def _pause(self, lower, higher):
|
|
408
|
+
"""The environment pauses: the unit of `higher` takes the embodiment and the
|
|
409
|
+
execution `lower` stays where it is. The environment holds what it was doing
|
|
410
|
+
(session._pause_execution); nothing of it is advanced until the higher unit has
|
|
411
|
+
ended, and the model is not asked. The `call_paused` note records it."""
|
|
412
|
+
try:
|
|
413
|
+
# (The environment is told who takes it: what it holds may want to know when it goes on.)
|
|
414
|
+
taker = getattr(higher.owner, "rule", None) or getattr(higher.owner, "name", None)
|
|
415
|
+
lower.paused = {"held": self.session._pause_execution(taker)}
|
|
416
|
+
except BaseException as error:
|
|
417
|
+
self._fail(lower, error)
|
|
418
|
+
return
|
|
419
|
+
spans.note("call_paused", unit=getattr(lower.owner, "id", None), name=getattr(lower.owner, "name", None),
|
|
420
|
+
by=getattr(higher.owner, "id", None), by_name=getattr(higher.owner, "name", None),
|
|
421
|
+
rule=getattr(higher.owner, "rule", None), priority=getattr(higher.owner, "priority", 0) or 0)
|
|
422
|
+
|
|
423
|
+
def _resume(self, entry):
|
|
424
|
+
"""The unit that took the embodiment has ended: the paused execution goes on."""
|
|
425
|
+
held, entry.paused = entry.paused["held"], None
|
|
426
|
+
try:
|
|
427
|
+
self.session._resume_execution(held)
|
|
428
|
+
except BaseException as error:
|
|
429
|
+
self._fail(entry, error)
|
|
430
|
+
return
|
|
431
|
+
spans.note("call_resumed", unit=getattr(entry.owner, "id", None), name=getattr(entry.owner, "name", None))
|
|
432
|
+
|
|
256
433
|
def _end(self, entry):
|
|
257
434
|
with self._changed:
|
|
258
435
|
self._undrive(entry)
|
|
@@ -371,11 +548,15 @@ class Tick:
|
|
|
371
548
|
if self._periods is not None:
|
|
372
549
|
self._periods.begin()
|
|
373
550
|
waiting = any(entry.suspended for entry in entries)
|
|
374
|
-
|
|
375
|
-
|
|
551
|
+
now = time.monotonic()
|
|
552
|
+
timed = self._timed(now)
|
|
553
|
+
timed = timed is not None and timed <= 0
|
|
554
|
+
with self._changed:
|
|
555
|
+
judged, self._judge = self._judge or timed, False
|
|
556
|
+
if (self.bound and (entries or self._line)) or waiting or timed or (
|
|
557
|
+
self._line and layer is not None and layer.stops()):
|
|
376
558
|
session._observe()
|
|
377
559
|
if session.state.get("ended"):
|
|
378
|
-
self._holding = False
|
|
379
560
|
self._line.clear()
|
|
380
561
|
with self._changed:
|
|
381
562
|
for entry in self._entries:
|
|
@@ -385,12 +566,14 @@ class Tick:
|
|
|
385
566
|
budget = reactive.period(layer.budget_seconds) if layer is not None else contextlib.nullcontext()
|
|
386
567
|
with budget:
|
|
387
568
|
if tree is not None and getattr(session, "observation_event_kinds", ()):
|
|
569
|
+
self._listened = now
|
|
388
570
|
owner = getattr(entries[0].owner, "id", None) if entries else None
|
|
389
571
|
for observed in session._take_observation_events(session.state):
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
572
|
+
reflexes.emit(tree, owner, "observe", observed, notice=True)
|
|
573
|
+
if judged and control.pause_requested():
|
|
574
|
+
self._held_judgement = True
|
|
575
|
+
elif judged and tree is not None and not session.state.get("ended"):
|
|
576
|
+
reflexes.judge(session, tree)
|
|
394
577
|
self._mark("observe")
|
|
395
578
|
parts = []
|
|
396
579
|
for entry in entries:
|
|
@@ -440,8 +623,7 @@ class Tick:
|
|
|
440
623
|
command, idle = (parts[0].part if parts else None), None
|
|
441
624
|
with self._changed:
|
|
442
625
|
dirty, self._dirty = self._dirty, False
|
|
443
|
-
|
|
444
|
-
if not (dirty or holding or any(part.execution or part.reflex for part in parts)):
|
|
626
|
+
if not (dirty or any(part.execution for part in parts)):
|
|
445
627
|
return None
|
|
446
628
|
cameras = self._cameras(parts) if self.channels else parts[0].cameras if parts else None
|
|
447
629
|
return _Action(command, parts, cameras, self._period + self.delay_ticks, idle)
|
|
@@ -452,34 +634,24 @@ class Tick:
|
|
|
452
634
|
|
|
453
635
|
def _admit(self, parts, entries, tree):
|
|
454
636
|
session = self.session
|
|
455
|
-
acts, taken = self._acts(parts + self._holds(), entries, tree)
|
|
456
637
|
while True:
|
|
457
638
|
held = self._holds()
|
|
458
|
-
|
|
459
|
-
for
|
|
460
|
-
used |= session._channels_of(act.part)
|
|
461
|
-
for part in parts + held:
|
|
462
|
-
part.sent = self.session._mask(part.part, taken)
|
|
463
|
-
idle = self._idle() - used
|
|
464
|
-
command = session._merge([part.sent for part in parts + held + acts], idle)
|
|
639
|
+
idle = self._idle()
|
|
640
|
+
command = session._merge([part.sent for part in parts + held], idle)
|
|
465
641
|
verdict = session._admit(command, commit=False)
|
|
466
642
|
if verdict.command is not None:
|
|
467
643
|
command = verdict.command
|
|
468
644
|
break
|
|
469
|
-
blamed = self._blame(parts, held, verdict.reasons[0]
|
|
645
|
+
blamed = self._blame(parts, held, verdict.reasons[0])
|
|
470
646
|
if blamed is None:
|
|
471
647
|
break
|
|
472
|
-
if blamed.reflex is not None:
|
|
473
|
-
# A reflex's act the shield would reject is left out of this period.
|
|
474
|
-
acts.remove(blamed)
|
|
475
|
-
continue
|
|
476
648
|
reply = {"executed": False, "shield": verdict.to_dict(), "state": dict(session.state)}
|
|
477
649
|
self._settle(blamed, reply, tree)
|
|
478
650
|
if blamed.execution is not None:
|
|
479
651
|
parts.remove(blamed)
|
|
480
652
|
with self._changed:
|
|
481
653
|
self._undrive(blamed.execution)
|
|
482
|
-
return command, parts + held
|
|
654
|
+
return command, parts + held, idle
|
|
483
655
|
|
|
484
656
|
def _check_delayed(self, action, tree):
|
|
485
657
|
for part in action.parts:
|
|
@@ -504,7 +676,7 @@ class Tick:
|
|
|
504
676
|
parts = due.parts if due is not None else []
|
|
505
677
|
with self._changed:
|
|
506
678
|
live, inputs = self._delivery_inputs(parts)
|
|
507
|
-
if parts and not live and not self.
|
|
679
|
+
if parts and not live and not self.channels:
|
|
508
680
|
return
|
|
509
681
|
command = due.command if due is not None else None
|
|
510
682
|
cameras = due.cameras if due is not None else None
|
|
@@ -592,9 +764,6 @@ class Tick:
|
|
|
592
764
|
entry.queued -= 1
|
|
593
765
|
if part.generation != entry.generation or not self._execution_live(entry):
|
|
594
766
|
continue
|
|
595
|
-
elif part.reflex is not None:
|
|
596
|
-
if self.session.reactive.rules.get(part.reflex.name) is not part.reflex:
|
|
597
|
-
continue
|
|
598
767
|
elif self._withdrawn(part.owner):
|
|
599
768
|
continue
|
|
600
769
|
else:
|
|
@@ -634,7 +803,7 @@ class Tick:
|
|
|
634
803
|
if entry is None:
|
|
635
804
|
with self._changed:
|
|
636
805
|
self._let_go(part.owner, {part.channel} if part.channel is not None else set(self.channels))
|
|
637
|
-
|
|
806
|
+
reflexes.emit(tree, getattr(part.owner, "id", None), "hold", ev.input_refusal(
|
|
638
807
|
reply, self.session.state.get("step")))
|
|
639
808
|
return
|
|
640
809
|
accepted = reply.get("executed")
|
|
@@ -651,8 +820,8 @@ class Tick:
|
|
|
651
820
|
|
|
652
821
|
def _failed_period(self, entries, error):
|
|
653
822
|
if not entries:
|
|
654
|
-
print(f"harness tick:
|
|
655
|
-
|
|
823
|
+
print(f"harness tick: a period without an execution failed: {type(error).__name__}: {error}",
|
|
824
|
+
file=sys.stderr)
|
|
656
825
|
for entry in entries:
|
|
657
826
|
self._fail(entry, error)
|
|
658
827
|
|
|
@@ -730,9 +899,13 @@ class Tick:
|
|
|
730
899
|
reply, execution.fresh = execution.fresh, None
|
|
731
900
|
try:
|
|
732
901
|
with spans.under(execution.parent), _acting(tree, execution.owner):
|
|
902
|
+
if execution.steps is None:
|
|
903
|
+
execution.steps = execution.make()
|
|
733
904
|
decided = execution.steps.send(reply) if execution.started else next(execution.steps)
|
|
734
905
|
except StopIteration as returned:
|
|
735
906
|
execution.trace = returned.value
|
|
907
|
+
if execution.restarts and execution.trace is not None:
|
|
908
|
+
execution.trace.restarts = execution.restarts
|
|
736
909
|
return
|
|
737
910
|
execution.started = True
|
|
738
911
|
command, cameras, *checks = decided
|
|
@@ -825,12 +998,12 @@ class Tick:
|
|
|
825
998
|
busy |= self.session._channels_of(held)
|
|
826
999
|
return set(self.channels) - busy
|
|
827
1000
|
|
|
828
|
-
def _blame(self, parts, held, reason
|
|
829
|
-
"""The part of a period's command a shield's reason is about:
|
|
830
|
-
|
|
831
|
-
|
|
1001
|
+
def _blame(self, parts, held, reason):
|
|
1002
|
+
"""The part of a period's command a shield's reason is about: the newest
|
|
1003
|
+
execution's, else a hold, on the channel it names; the newest execution's, else the
|
|
1004
|
+
last hold, when it names none."""
|
|
832
1005
|
channel = reason.get("channel") or self.session._channel_of_reason(reason)
|
|
833
|
-
for part in reversed(held + parts
|
|
1006
|
+
for part in reversed(held + parts):
|
|
834
1007
|
uses = part.execution.uses if part.execution is not None else set()
|
|
835
1008
|
if channel is None or channel in self.session._channels_of(part.part) | uses:
|
|
836
1009
|
part.channel = channel
|
|
@@ -841,20 +1014,14 @@ class Tick:
|
|
|
841
1014
|
with self._changed:
|
|
842
1015
|
return [_Part(None, held, owner=owner) for owner, held in self._held.items()]
|
|
843
1016
|
|
|
844
|
-
def _acts(self, parts, executions, tree):
|
|
845
|
-
if self.clock == "steps" and not executions:
|
|
846
|
-
return [], set()
|
|
847
|
-
goal = self.session._merge([part.part for part in parts], set())
|
|
848
|
-
acts, taken = reactive.apply(self.session, tree, goal=goal, channels=sorted(set(self.channels) - self._idle()))
|
|
849
|
-
return [_Part(None, part, reflex=rule) for rule, part in acts], set(taken)
|
|
850
|
-
|
|
851
1017
|
def _fail(self, execution, error):
|
|
852
1018
|
if execution.done.is_set():
|
|
853
1019
|
return
|
|
854
1020
|
execution.error = error
|
|
855
1021
|
try:
|
|
856
1022
|
with _acting(self._tree(), execution.owner):
|
|
857
|
-
execution.steps
|
|
1023
|
+
if execution.steps is not None:
|
|
1024
|
+
execution.steps.close()
|
|
858
1025
|
except BaseException as closing:
|
|
859
1026
|
if closing is not error:
|
|
860
1027
|
error.add_note(f"Closing the execution raised {type(closing).__name__}: {closing}")
|
|
@@ -884,16 +1051,15 @@ class Refused(Exception):
|
|
|
884
1051
|
|
|
885
1052
|
class _Part:
|
|
886
1053
|
"""One period's part of an execution's command (`last` when it is the command's last),
|
|
887
|
-
what
|
|
1054
|
+
or what a unit (`owner`) holds down."""
|
|
888
1055
|
|
|
889
|
-
def __init__(self, execution, part, last=False, owner=None
|
|
1056
|
+
def __init__(self, execution, part, last=False, owner=None):
|
|
890
1057
|
self.execution = execution
|
|
891
1058
|
self.generation = execution.generation if execution is not None else None
|
|
892
1059
|
self.part = part
|
|
893
1060
|
self.last = last
|
|
894
1061
|
self.owner = owner
|
|
895
1062
|
self.channel = None
|
|
896
|
-
self.reflex = reflex
|
|
897
1063
|
self.sent = part
|
|
898
1064
|
self.cameras = execution.cameras if execution is not None else None
|
|
899
1065
|
|
|
@@ -5,14 +5,15 @@ A package's skills, benches, workers and services import the harness only from h
|
|
|
5
5
|
without breaking them; a change here that breaks a package raises the harness's major
|
|
6
6
|
version. Its names, by what they are for:
|
|
7
7
|
|
|
8
|
-
Skills ClosedLoop, Result, Stop, wait_until,
|
|
8
|
+
Skills ClosedLoop, Result, Stop, wait_until,
|
|
9
9
|
span, spans, control, events, Escalation, Event, ExecutionTrace,
|
|
10
10
|
ProgramFinished, record_kind, ReactiveFault, call_reactive,
|
|
11
11
|
json_list (the JSON list in a model's reply),
|
|
12
12
|
Learner, candidate, entry_key, Library, SECTIONS, WorkingMemory, MemoryKind, memory_entries,
|
|
13
13
|
keep_probe, BUILTIN_ACTIONS, OPEN_GRIPPER, STOP, HOLD, BACK_OFF, Refused
|
|
14
14
|
Sessions EpisodeCore, create_session, session_class (an embodiment kind's kernel
|
|
15
|
-
session builds on the episode core), PhysicalInput, REMEMBER
|
|
15
|
+
session builds on the episode core), PhysicalInput, REMEMBER,
|
|
16
|
+
Admission (the host side of a managed kernel: its proposals admitted as units)
|
|
16
17
|
Workers WORKER_METHODS, create_server, BenchWorker (what a benchmark's worker
|
|
17
18
|
shares), Action, Observation, Transition, EpisodeCursor, OutcomeToken,
|
|
18
19
|
pid_alive, file_lock, start_detached, data_url, image_data_url, decode_data_url, encode_png, fit_into,
|
|
@@ -44,7 +45,8 @@ version. Its names, by what they are for:
|
|
|
44
45
|
Text format (cost, duration, clock, timestamp), transcript, approval (the sensitive kinds,
|
|
45
46
|
request(), and the words of an approval: WORDS, say)
|
|
46
47
|
Evaluation ResolvedAgent, overridden_agent, task_config, bench_names, declares_channels,
|
|
47
|
-
TaskRun,
|
|
48
|
+
TaskRun, saved_conversation (what a run kept to be continued), serve_worker,
|
|
49
|
+
UsageError (a command's usage error: exit status 2),
|
|
48
50
|
cli_output and cli_table (a command's JSON and text table, as the core's print them)
|
|
49
51
|
and the terminal chat's timeline (tui_timeline)
|
|
50
52
|
|
|
@@ -67,7 +69,6 @@ _NAMES = {
|
|
|
67
69
|
"Result": "harness.runtime.execution.entries:Result",
|
|
68
70
|
"Stop": "harness.runtime.execution.entries:Stop",
|
|
69
71
|
"wait_until": "harness.core.wait:wait_until",
|
|
70
|
-
"user_message": "harness.runtime.execution.entries:user_message",
|
|
71
72
|
"span": "harness.core.spans:span",
|
|
72
73
|
"spans": "harness.core.spans",
|
|
73
74
|
"control": "harness.runtime.execution.control",
|
|
@@ -77,6 +78,7 @@ _NAMES = {
|
|
|
77
78
|
"ExecutionTrace": "harness.runtime.events:ExecutionTrace",
|
|
78
79
|
"EpisodeEnded": "harness.runtime.events:EpisodeEnded",
|
|
79
80
|
"Tick": "harness.runtime.tick:Tick",
|
|
81
|
+
"Admission": "harness.runtime.execution.managed:Admission",
|
|
80
82
|
"WorkerPipe": "harness.core.worker_pipe:WorkerPipe",
|
|
81
83
|
"WorkerRejected": "harness.core.worker_pipe:WorkerRejected",
|
|
82
84
|
"linux_sandbox": "harness.core.linux_sandbox",
|
|
@@ -226,6 +228,7 @@ _NAMES = {
|
|
|
226
228
|
"active": "harness.runtime.learning.active",
|
|
227
229
|
"learning_settings": "harness.runtime.learning.learner:settings",
|
|
228
230
|
"TaskRun": "harness.runtime.task.run:TaskRun",
|
|
231
|
+
"saved_conversation": "harness.runtime.task.run:saved",
|
|
229
232
|
"think": "harness.runtime.decision.thoughts:think",
|
|
230
233
|
"ChatModel": "harness.runtime.decision.model:ChatModel",
|
|
231
234
|
"USAGE": "harness.runtime.decision.model:USAGE",
|