@lmmslab/harness 0.81.0 → 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 (79) 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 +158 -29
  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/packages/manifest.py +12 -1
  40. package/python/harness/providers.py +14 -5
  41. package/python/harness/runtime/decision/loop.py +100 -121
  42. package/python/harness/runtime/decision/messages.py +156 -48
  43. package/python/harness/runtime/decision/model.py +79 -21
  44. package/python/harness/runtime/decision/prompts.py +15 -11
  45. package/python/harness/runtime/decision/protocol.py +12 -6
  46. package/python/harness/runtime/decision/thoughts.py +10 -16
  47. package/python/harness/runtime/events.py +28 -13
  48. package/python/harness/runtime/execution/attached.py +1 -1
  49. package/python/harness/runtime/execution/control.py +17 -35
  50. package/python/harness/runtime/execution/entries.py +204 -124
  51. package/python/harness/runtime/execution/kernel.py +471 -281
  52. package/python/harness/runtime/execution/managed.py +231 -0
  53. package/python/harness/runtime/execution/noenv.py +2 -2
  54. package/python/harness/runtime/execution/results.py +2 -0
  55. package/python/harness/runtime/execution/session.py +276 -115
  56. package/python/harness/runtime/learning/active.py +36 -48
  57. package/python/harness/runtime/learning/fixtures.py +2 -2
  58. package/python/harness/runtime/learning/learner.py +75 -2
  59. package/python/harness/runtime/learning/library.py +77 -10
  60. package/python/harness/runtime/learning/views.py +12 -1
  61. package/python/harness/runtime/reactive/__init__.py +1 -1
  62. package/python/harness/runtime/reactive/rules.py +29 -394
  63. package/python/harness/runtime/reactive/tree.py +611 -0
  64. package/python/harness/runtime/safety/approval.py +8 -3
  65. package/python/harness/runtime/safety/permissions.py +242 -0
  66. package/python/harness/runtime/status.py +7 -2
  67. package/python/harness/runtime/task/delegation.py +97 -58
  68. package/python/harness/runtime/task/environments.py +3 -4
  69. package/python/harness/runtime/task/inbound.py +64 -0
  70. package/python/harness/runtime/task/run.py +1092 -726
  71. package/python/harness/runtime/tick.py +257 -91
  72. package/python/harness/sdk/__init__.py +7 -4
  73. package/python/harness/settings.py +27 -3
  74. package/python/harness/terminal.py +10 -6
  75. package/python/harness/transcript.py +18 -6
  76. package/python/harness/tui/answer.py +51 -0
  77. package/python/harness/tui/chat.py +75 -26
  78. package/python/harness/tui/views.py +1 -0
  79. package/python/harness/builtin/prompts/shared/entries.md +0 -86
@@ -0,0 +1,611 @@
1
+ """The reactive layer: one tree of reflexes.
2
+
3
+ What acts without the model is one tree, empty at first. A node says when it has its turn
4
+ (`when`: a condition of the view, or the name of an event) and what it does then (`do`: a
5
+ program, the name of an event to raise, an action that ends what runs, or nothing: then it
6
+ is a group, and the nodes under it are its content). Three statements are the whole of it:
7
+
8
+ 1. A node under another has its turn only while that one's condition holds.
9
+ 2. Among the nodes at one place the earlier (the higher priority, then the older) takes
10
+ the environment from the later, and gives it back when it is done.
11
+ 3. A node has done its work when the situation it answers is over: its program is a
12
+ unit whose completion condition is that the node's condition no longer holds
13
+ (runtime/execution/entries.py: how a unit ended and whether its work is done are two
14
+ things, for every unit). When its program ends and the situation is not over, or
15
+ the program fails, a later node at that place whose condition holds has the
16
+ environment; when none has, the model is told what the program did (the event
17
+ REFLEX_FAILED), once until the node has done its work again.
18
+
19
+ A node's program runs as a unit whose priority is the node's place in the tree (rank()):
20
+ the units' priorities are how the tick lets the earlier take the environment from the later
21
+ (runtime/tick.py: the later call is paused where the environment pauses, else ended, and
22
+ its node begins again). Every node's place is above the model's own programs: a reflex
23
+ takes the environment from them.
24
+
25
+ The tree is judged when the state or the facts change, when one of its programs ends, and
26
+ on time for the nodes that ask (`every`). The units it starts are its own business: their
27
+ ends are recorded and start no call of the model.
28
+ """
29
+
30
+ from contextlib import contextmanager
31
+ from copy import copy
32
+ from dataclasses import dataclass, field
33
+ import inspect
34
+ import threading
35
+ import time
36
+ from typing import Any, Callable
37
+
38
+ from harness.core import spans
39
+ from harness.runtime import events as ev
40
+ from harness.runtime.events import Event, ExecutionTrace
41
+ from harness.runtime.reactive.rules import HOLD, STOP, NotJudged, ReactiveFault, call_reactive
42
+
43
+ # A node whose program failed does not begin again before this many seconds, and no node
44
+ # begins more often than once in RESTART_SECONDS: the tree does not spin on a program.
45
+ FAILED_AGAIN_SECONDS = 2.0
46
+ RESTART_SECONDS = 0.2
47
+ # A node's condition that took longer than the budget this many times in a row is a fault:
48
+ # the node goes and the model is told. Once is a moment of the machine, not of the node.
49
+ LATE_TIMES = 3
50
+
51
+ _staging = threading.local()
52
+ _UNSEEN = object()
53
+ # What a turn is told of a tree with no node in it.
54
+ NO_REFLEXES = "Reflex tree: empty (nothing acts without you)"
55
+ # The most nodes one reflexes() call gives, and the most changed nodes a turn is told of.
56
+ LISTED, TOLD = 50, 10
57
+
58
+
59
+ def told(nodes: dict, before: dict) -> str:
60
+ """What a turn of the model is told of the tree: how many nodes act in it, and the
61
+ lines (Reflexes.digest) that differ from `before`, what it was told last."""
62
+ if not nodes:
63
+ return NO_REFLEXES
64
+ changed = [f" {name}: {line}" for name, line in nodes.items() if before.get(name) != line]
65
+ count = f"{len(nodes)} node" + ("" if len(nodes) == 1 else "s")
66
+ if not changed:
67
+ return f"Reflex tree: {count}, as you were last told (reflexes() lists them)"
68
+ more = f"\n ... {len(changed) - TOLD} more changed: reflexes()" if len(changed) > TOLD else ""
69
+ return (f"Reflex tree: {count}; changed since you were last told:\n"
70
+ + "\n".join(changed[:TOLD]) + more)
71
+
72
+
73
+ @dataclass
74
+ class Reflex:
75
+ """A node of the tree: its `name`; `when` it has its turn (a condition of the view) or
76
+ the `event` it answers; what it does: `do` (a program), or `action` (an event to raise,
77
+ or one of the environment's actions that end what runs), or neither (a group); the node
78
+ it is `under` (None: the top); its `priority` among the nodes at its place; `every`
79
+ seconds its condition is judged (None: when the state or the facts change);
80
+ `again_seconds` before it may begin again after its program ended."""
81
+
82
+ name: str
83
+ when: Callable | None = None
84
+ event: str | None = None
85
+ do: Callable | None = None
86
+ action: str | None = None
87
+ stops: bool = False
88
+ under: str | None = None
89
+ priority: int = 0
90
+ every: float | None = None
91
+ again_seconds: float = 0.0
92
+ order: int = 0
93
+ library_origin: tuple | None = None
94
+ # As it stands: the unit of its program, when it may begin again, whether its condition
95
+ # held when last judged, whether it raised its event since that came to hold, when it
96
+ # is next due, how often it acted, the program of higher priority that kept it from acting,
97
+ # and whether the model was told of its failure since it last succeeded.
98
+ unit: Any = field(default=None, repr=False)
99
+ ready_at: float = field(default=0.0, repr=False)
100
+ holds: Any = field(default=_UNSEEN, repr=False)
101
+ raised: bool = field(default=False, repr=False)
102
+ next_at: float = field(default=0.0, repr=False)
103
+ triggered: int = 0
104
+ kept_by: Any = field(default=None, repr=False)
105
+ told: bool = field(default=False, repr=False)
106
+ # The end report of its program's last run, and how often a run did its work and did not.
107
+ last: Any = field(default=None, repr=False)
108
+ did: int = 0
109
+ did_not: int = 0
110
+ # How often in a row its condition took longer than the budget, and whether it waits to
111
+ # be judged (the period's budget was used up before its turn).
112
+ late: int = field(default=0, repr=False)
113
+ waits: bool = field(default=False, repr=False)
114
+
115
+ @property
116
+ def does(self) -> str:
117
+ if self.do is not None:
118
+ return "answers an event with its program" if self.event else "starts its program"
119
+ if self.action is not None:
120
+ return "ends what runs" if self.stops else "raises an event"
121
+ return "holds the nodes under it"
122
+
123
+ def describe(self) -> dict:
124
+ """What the event it raises reports of it."""
125
+ return {"name": self.name, "action": self.action, "triggered": self.triggered,
126
+ **({} if self.stops else {"stops": False})}
127
+
128
+
129
+ def _takes_argument(function) -> bool:
130
+ try:
131
+ return any(item.kind in (item.POSITIONAL_ONLY, item.POSITIONAL_OR_KEYWORD, item.VAR_POSITIONAL)
132
+ and item.default is item.empty for item in inspect.signature(function).parameters.values())
133
+ except (TypeError, ValueError):
134
+ return False
135
+
136
+
137
+ @contextmanager
138
+ def stage(layer, replacing):
139
+ """While a library entry is loaded: the nodes it sets are gathered in a copy of the
140
+ layer (given), and publish() puts them into the tree, in place of the nodes the entries
141
+ `replacing` had set."""
142
+ if layer is None:
143
+ yield None
144
+ return
145
+ staged = copy(layer)
146
+ staged.nodes = {}
147
+ staged.wake_observer = None
148
+ staged.library_origin = None
149
+ staged.replacing = replacing
150
+ staged.live = layer
151
+ _staging.layers = (layer, staged)
152
+ try:
153
+ yield staged
154
+ finally:
155
+ _staging.layers = None
156
+
157
+
158
+ class Reflexes:
159
+ """The tree of one kernel session (see the module docstring). `min_period`: the shortest
160
+ period a node may ask for, `period` the one `every=True` gets (the environment's upper
161
+ bound and default); `view()`: the session's view now, for a node that answers an event."""
162
+
163
+ def __init__(self, budget_seconds: float, actions=(STOP, HOLD)):
164
+ if not budget_seconds > 0:
165
+ raise ValueError("Reactive budget must be positive")
166
+ self.budget_seconds = budget_seconds
167
+ self.actions = tuple(actions)
168
+ self.nodes: dict[str, Reflex] = {}
169
+ self._count = 0
170
+ self.wake_observer = None
171
+ self.view = None
172
+ self.library_boundary = threading.RLock()
173
+ self.library_origin = None
174
+ self.period = self.min_period = 1.0
175
+ # What the last judgement left: the state and facts it saw, whether the tree changed
176
+ # since, and when a node that waits for its time wants the tree judged again.
177
+ self.stamp, self.changed, self.wake_at = None, True, None
178
+
179
+ # Setting and removing nodes.
180
+
181
+ def set(self, name, when, do=None, *, under=None, priority=0, every=None, again_seconds=0.0) -> str:
182
+ """Put the node `name` into the tree, in place of a node of that name (see Reflex;
183
+ `when`: a function of the view, or the name of an event; `do`: a program, or the
184
+ name of an event to raise, or an action of the environment that ends what runs
185
+ ("stop"), or None for a group). Its name."""
186
+ staging = getattr(_staging, "layers", None)
187
+ if staging is not None and staging[0] is self:
188
+ return staging[1].set(name, when, do, under=under, priority=priority, every=every,
189
+ again_seconds=again_seconds)
190
+ if not isinstance(name, str) or not name:
191
+ raise ValueError("a reflex's name is a nonempty string")
192
+ event = None
193
+ if isinstance(when, str):
194
+ if not when.replace("_", "").isalnum():
195
+ raise ValueError(f"an event's name is an identifier like LOW_BATTERY, got {when!r}")
196
+ event, when = when, None
197
+ if not callable(do):
198
+ raise ValueError("a reflex that answers an event does it with a program: do=function")
199
+ elif not callable(when):
200
+ raise TypeError("set_reflex(name, when, do): `when` is a function of the view, or the name of an event")
201
+ action, stops = None, False
202
+ if isinstance(do, str):
203
+ if do not in self.actions and not do.replace("_", "").isalnum():
204
+ raise ValueError(f"`do` as text is {', '.join(repr(a) for a in self.actions)} or the name of an "
205
+ f"event like EMPTY_GRASP, got {do!r}")
206
+ action, stops, do = do, do in self.actions, None
207
+ elif do is not None and not callable(do):
208
+ raise TypeError("`do` is a program (a function), the name of an event, an action that ends what runs, "
209
+ "or None for a group")
210
+ if isinstance(priority, bool) or not isinstance(priority, int) or priority < 0:
211
+ raise ValueError(f"priority is a whole number, 0 or more, got {priority!r}")
212
+ if isinstance(again_seconds, bool) or not isinstance(again_seconds, (int, float)) or again_seconds < 0:
213
+ raise ValueError(f"again_seconds is a number of seconds >= 0, got {again_seconds!r}")
214
+ if every is not None:
215
+ every = self.period if every is True else every
216
+ if isinstance(every, bool) or not isinstance(every, (int, float)) or every < self.min_period - 1e-9:
217
+ raise ValueError(f"every is a period in seconds, at least {self.min_period:g} here, got {every!r}")
218
+ known = {**getattr(getattr(self, "live", None), "nodes", {}), **self.nodes}
219
+ if under is not None:
220
+ above = known.get(under)
221
+ if above is None:
222
+ raise ValueError(f"no reflex is named {under!r}: the node a reflex is under is set first")
223
+ if above.do is not None or above.action is not None or above.event is not None:
224
+ raise ValueError(f"{under!r} does something itself: a node that has nodes under it is a group "
225
+ "(set without `do`)")
226
+ while above is not None:
227
+ if above.name == name:
228
+ raise ValueError(f"{name!r} cannot be under itself")
229
+ above = known.get(above.under) if above.under is not None else None
230
+ old = known.get(name)
231
+ if (do is not None or action is not None or event is not None) and any(
232
+ node.under == name for node in known.values()):
233
+ raise ValueError(f"{name!r} has nodes under it: it stays a group (set without `do`)")
234
+ self._count += 1
235
+ self._retire(self.nodes.get(name))
236
+ self.nodes[name] = Reflex(name, when, event, do, action, stops, under, priority,
237
+ None if every is None else float(every), float(again_seconds),
238
+ old.order if old is not None else self._count, self.library_origin)
239
+ self._touched()
240
+ return name
241
+
242
+ def drop(self, names=None) -> None:
243
+ """Remove the nodes `names` (a name or a list; None: all) with the nodes under them;
244
+ the programs they started are ended."""
245
+ staging = getattr(_staging, "layers", None)
246
+ if staging is not None and staging[0] is self:
247
+ raise ValueError("a library entry cannot remove a reflex that is set")
248
+ names = list(self.nodes) if names is None else [names] if isinstance(names, str) else list(names)
249
+ for name in names:
250
+ for node in self._below(name):
251
+ self._retire(self.nodes.pop(node.name, None))
252
+ self._touched()
253
+
254
+ def _below(self, name) -> list:
255
+ """The node `name` and the nodes under it."""
256
+ node = self.nodes.get(name)
257
+ if node is None:
258
+ return []
259
+ return [node] + [found for child in self.children(name) for found in self._below(child.name)]
260
+
261
+ def _touched(self) -> None:
262
+ self.changed = True
263
+ self.tell()
264
+ if self.wake_observer is not None:
265
+ self.wake_observer()
266
+
267
+ def digest(self, name=None, path="") -> dict:
268
+ """How each node that acts stands, in one line, by its path in the tree. The host
269
+ tells the model the lines that changed since it was last told (told()), so what a
270
+ turn carries of the tree does not grow with the tree."""
271
+ lines = {}
272
+ for node in self.children(name):
273
+ here = path + node.name
274
+ if node.event is not None:
275
+ lines[here] = f"answers the event {node.event}; began {node.triggered}"
276
+ elif node.action is not None:
277
+ lines[here] = ("ends what runs" if node.stops else f"tells you {node.action}") + f"; {node.triggered} times"
278
+ elif node.do is not None:
279
+ running = node.unit is not None and bool(getattr(node.unit, "running", False))
280
+ lines[here] = (f"began {node.triggered}, did its work {node.did}, did not {node.did_not}"
281
+ + (", runs now" if running else ""))
282
+ else:
283
+ lines.update(self.digest(node.name, here + "/"))
284
+ return lines
285
+
286
+ def tell(self) -> None:
287
+ """Send the host how the tree stands, when that changed (the `reflexes` note)."""
288
+ if getattr(_staging, "layers", None) is not None and getattr(self, "live", None) is not None:
289
+ return
290
+ lines = self.digest()
291
+ if lines != getattr(self, "_told", None):
292
+ self._told = lines
293
+ spans.note("reflexes", nodes=lines)
294
+
295
+ @staticmethod
296
+ def _retire(node) -> None:
297
+ """A node that goes ends the program it started."""
298
+ unit = getattr(node, "unit", None)
299
+ if unit is not None and unit.running:
300
+ threading.Thread(target=unit.cancel, name=f"reflex-{node.name}", daemon=True).start()
301
+
302
+ def publish(self, staged) -> None:
303
+ """Put what a library entry set (stage()) into the tree."""
304
+ for name, node in list(self.nodes.items()):
305
+ if node.library_origin and node.library_origin[0] in staged.replacing and name not in staged.nodes:
306
+ for found in self._below(name):
307
+ self._retire(self.nodes.pop(found.name, None))
308
+ for name, node in staged.nodes.items():
309
+ self._retire(self.nodes.get(name))
310
+ self.nodes[name] = node
311
+ self._count = staged._count
312
+ if staged.nodes:
313
+ self._touched()
314
+
315
+ # The tree as it stands.
316
+
317
+ def children(self, name=None) -> list:
318
+ """The nodes under `name` (None: at the top), the earlier first."""
319
+ return sorted((node for node in self.nodes.copy().values() if node.under == name),
320
+ key=lambda node: (-node.priority, node.order))
321
+
322
+ def _leaves(self, name) -> list:
323
+ """The nodes with a program at and under `name`, in the tree's order."""
324
+ node = self.nodes.get(name)
325
+ if node is None:
326
+ return []
327
+ if node.do is not None:
328
+ return [node]
329
+ return [leaf for child in self.children(name) for leaf in self._leaves(child.name)]
330
+
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)
340
+ place = next((index for index, leaf in enumerate(leaves) if leaf is node), 0)
341
+ return top.priority + (len(leaves) - place) / (len(leaves) + 1)
342
+
343
+ def listing(self, name=None) -> list:
344
+ """The nodes at the top of the tree, or the node `name` where it has none under it,
345
+ else the nodes under it; the earlier first, at most LISTED of them (then a last row
346
+ {"omitted": how many}). Each: {"name", "priority", "when" ("a condition" or the event
347
+ it answers), "does", "holds" (its condition held when last judged), "running" (its
348
+ program runs now), "began" (how often its program began), "last" (the end report of
349
+ its program's last run: {"ended", "done", "returned", "error", "seconds"}), "under":
350
+ the names of the nodes under it (listing(name) gives them)}."""
351
+ if name is not None and name not in self.nodes:
352
+ raise ValueError(f"no reflex {name!r}")
353
+ nodes = self.children(name) or ([self.nodes[name]] if name is not None else [])
354
+ rows = [{"name": node.name, "priority": node.priority,
355
+ "when": f"the event {node.event}" if node.event else "a condition", "does": node.does,
356
+ **({"holds": bool(node.holds)} if node.event is None and node.holds is not _UNSEEN else {}),
357
+ **({"running": node.unit is not None and bool(getattr(node.unit, "running", False)),
358
+ "began": node.triggered} if node.do is not None else {}),
359
+ **({"last": node.last} if node.last is not None else {}),
360
+ **({"under": [child.name for child in self.children(node.name)]}
361
+ if self.children(node.name) else {})}
362
+ for node in nodes[:LISTED]]
363
+ return rows + ([{"omitted": len(nodes) - LISTED}] if len(nodes) > LISTED else [])
364
+
365
+ # Judging.
366
+
367
+ def _run(self, node, view):
368
+ """Whether the node's condition holds on `view`. Where it was not judged (the
369
+ period's budget was used up before its turn, or it was ended at the hard limit), what
370
+ it was: it is judged in the next period. A condition slower than the budget
371
+ LATE_TIMES in a row, ended or not, is a fault."""
372
+ with self.library_boundary:
373
+ if self.nodes.get(node.name) is not node:
374
+ return None
375
+ over, judged = [], True
376
+ try:
377
+ result = call_reactive(node.name, node.when, view, budget_seconds=self.budget_seconds, late=over.append)
378
+ except NotJudged:
379
+ judged = False
380
+ if over:
381
+ node.late += 1
382
+ elif judged:
383
+ node.late = 0
384
+ if node.late >= LATE_TIMES:
385
+ raise ReactiveFault(node.name, f"its condition took {over[0] * 1000:.1f} ms, longer than its budget of "
386
+ f"{self.budget_seconds * 1000:.1f} ms {LATE_TIMES} times in a row")
387
+ node.waits = not judged
388
+ if not judged:
389
+ return None if node.holds is _UNSEEN else node.holds
390
+ if result and node.library_origin:
391
+ spans.note("library_use", key=node.library_origin[0], revision=node.library_origin[2],
392
+ ok=True, rule=node.name, testing=spans.in_test())
393
+ return result
394
+
395
+ def _above_hold(self, node, view) -> bool:
396
+ """Whether the conditions of the nodes above `node` hold on `view`."""
397
+ above = self.nodes.get(node.under) if node.under is not None else None
398
+ while above is not None:
399
+ if not self._run(above, view):
400
+ return False
401
+ above = self.nodes.get(above.under) if above.under is not None else None
402
+ return True
403
+
404
+ def stops(self) -> bool:
405
+ """Whether a node ends what runs (those are judged at every step of an execution)."""
406
+ return any(node.stops for node in self.nodes.copy().values())
407
+
408
+ def evaluate(self, view) -> Reflex | None:
409
+ """The first node that ends what runs and whose turn it is now."""
410
+ for node in self._ordered():
411
+ if node.stops and self._above_hold(node, view) and self._run(node, view):
412
+ node.triggered += 1
413
+ return node
414
+ return None
415
+
416
+ def _ordered(self, name=None) -> list:
417
+ """Every node, in the tree's order."""
418
+ return [found for node in self.children(name) for found in (node, *self._ordered(node.name))]
419
+
420
+ def answer(self, trace, start) -> bool:
421
+ """Offer an event to the first node that answers its kind and whose turn it is:
422
+ its program is started with `start(node, program)` and the event is answered."""
423
+ kind = trace.event.kind
424
+ for node in self._ordered():
425
+ if node.event != kind or (node.unit is not None and node.unit.running):
426
+ continue
427
+ if node.under is not None and self.view is not None and not self._above_hold(node, self.view()):
428
+ continue
429
+ program = (lambda: node.do(trace.event)) if _takes_argument(node.do) else node.do
430
+ node.triggered += 1
431
+ node.unit = start(node, program)
432
+ return True
433
+ return False
434
+
435
+ def listens(self, kinds) -> bool:
436
+ """Whether a node answers one of the event `kinds`."""
437
+ return any(node.event in kinds for node in self.nodes.copy().values())
438
+
439
+ def next_due(self, now) -> float | None:
440
+ """Seconds until the tree is due on time (0 when it is), None when it never is."""
441
+ due = [node.next_at for node in self.nodes.copy().values()
442
+ if node.every is not None and node.event is None and not node.stops]
443
+ if self.wake_at is not None:
444
+ due.append(self.wake_at)
445
+ return max(0.0, min(due) - now) if due else None
446
+
447
+ def reads_changes(self) -> bool:
448
+ """Whether a node is judged when the state or the facts change."""
449
+ return any(node.every is None and node.event is None and not node.stops for node in self.nodes.copy().values())
450
+
451
+
452
+ def fault_event(fault, step, **detail):
453
+ return Event(ev.REACTIVE_FAULT, fault.source, {"reason": fault.reason, **detail}, step)
454
+
455
+
456
+ def emit(tree, owner, skill, event, *, notice=False):
457
+ if tree is not None:
458
+ tree.raise_event(tree.get(owner), ExecutionTrace(skill, {}, event.tick, event.tick, event), notice=notice)
459
+
460
+
461
+ def report(unit) -> dict:
462
+ """The end report of a unit (the one every unit gives): how it ended, whether what it
463
+ was for is the case, what it returned or why it failed, its seconds."""
464
+ how = "by itself" if unit.status == "done" else "an error" if unit.failure is None or unit.raised is not None else \
465
+ f"the event {(unit.failure.trace.event.detail.get('event') or {}).get('kind', unit.failure.trace.event.kind)}"
466
+ returned = unit.result
467
+ if returned is not None and not isinstance(returned, (bool, int, float)):
468
+ text = returned if isinstance(returned, str) else repr(returned)
469
+ returned = text if len(text) <= 400 else text[:400] + "..."
470
+ return {"ended": how, "done": unit.ok, **({"returned": returned} if returned is not None else {}),
471
+ **({"error": str(unit.error)[-600:]} if unit.error else {}),
472
+ "seconds": round((unit.ended or time.time()) - unit.started, 1)}
473
+
474
+
475
+ def _interrupted(unit) -> bool:
476
+ """Whether a unit ended because another took the environment from it, or was cancelled."""
477
+ if unit.status == "cancelled":
478
+ return True
479
+ failure = getattr(unit, "failure", None)
480
+ detail = failure.trace.event.detail if failure is not None else None
481
+ return isinstance(detail, dict) and (detail.get("event") or {}).get("kind") == ev.PREEMPTED
482
+
483
+
484
+ def judge(session, tree):
485
+ """Judge the session's tree on its view now (see the module docstring)."""
486
+ layer = getattr(session, "reactive", None)
487
+ if layer is None or tree is None or not layer.nodes:
488
+ return
489
+ state = session.state
490
+ made = {}
491
+
492
+ def view_of():
493
+ if "view" not in made:
494
+ made["view"] = session._view(state, 0)
495
+ return made["view"]
496
+
497
+ now = time.monotonic()
498
+ stamp = (id(state), session.facts.version)
499
+ changed, layer.stamp = stamp != layer.stamp, stamp
500
+ forced, layer.changed = layer.changed, False
501
+ layer.wake_at = None
502
+ failures = []
503
+ # The programs that ended since the last judgement.
504
+ for node in list(layer.nodes.values()):
505
+ unit = node.unit
506
+ if unit is None or unit.running:
507
+ continue
508
+ node.unit, forced = None, True
509
+ if _interrupted(unit):
510
+ node.ready_at = now + RESTART_SECONDS
511
+ continue
512
+ node.last = report(unit)
513
+ if unit.status == "done" and unit.ok is not False:
514
+ node.did += 1
515
+ node.ready_at, node.told = now + max(node.again_seconds, RESTART_SECONDS), False
516
+ else:
517
+ node.did_not += 1
518
+ # It failed, or it ended and the situation it answers is not over.
519
+ node.ready_at = now + max(node.again_seconds, FAILED_AGAIN_SECONDS if unit.status != "done" else RESTART_SECONDS)
520
+ failures.append((node, unit))
521
+
522
+ def fault(node, error):
523
+ found = error if isinstance(error, ReactiveFault) else ReactiveFault(node.name, f"{type(error).__name__}: {error}")
524
+ layer.drop(node.name)
525
+ emit(tree, None, "reflex", session._rule_event(found, state))
526
+
527
+ def holds(node) -> bool:
528
+ if node.every is not None:
529
+ due = now >= node.next_at
530
+ if due:
531
+ node.next_at = max(node.next_at + node.every, now + node.every / 2)
532
+ else:
533
+ due = changed
534
+ if due or forced or node.waits or node.holds is _UNSEEN:
535
+ node.holds = bool(layer._run(node, view_of()))
536
+ if node.waits:
537
+ # Not judged in this period: again in the next one.
538
+ layer.wake_at = now if layer.wake_at is None else min(layer.wake_at, now)
539
+ return node.holds
540
+
541
+ wanting = []
542
+
543
+ def over(node):
544
+ # What a node's program is for: the situation the node answers is over.
545
+ return lambda view: not layer._run(node, view)
546
+
547
+ def walk(name):
548
+ for node in layer.children(name):
549
+ if node.event is not None or node.stops or layer.nodes.get(node.name) is not node:
550
+ continue
551
+ try:
552
+ held = holds(node)
553
+ except Exception as error:
554
+ fault(node, error)
555
+ continue
556
+ if not held:
557
+ # The situation is over.
558
+ node.raised = node.told = False
559
+ # (What is under it is judged anew when its turn comes again.)
560
+ for below in layer._below(node.name)[1:]:
561
+ below.holds = _UNSEEN
562
+ continue
563
+ 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)
570
+ elif node.do is not None:
571
+ wanting.append(node)
572
+ else:
573
+ walk(node.name)
574
+
575
+ walk(None)
576
+ running = [entry for entry in tree.running() if entry.kind != "agent"]
577
+ startable = set()
578
+ for node in wanting:
579
+ if node.unit is not None:
580
+ startable.add(node.name)
581
+ continue
582
+ if now < node.ready_at:
583
+ layer.wake_at = node.ready_at if layer.wake_at is None else min(layer.wake_at, node.ready_at)
584
+ continue
585
+ rank = layer.rank(node)
586
+ above = next((entry for entry in running if (entry.priority or 0) > rank), None)
587
+ if above is not None:
588
+ # What is before it has the environment: its turn is judged when that ends. Where
589
+ # that is a program that said so (at_priority), the model is told once.
590
+ if getattr(above, "rule", None) is None:
591
+ if node.kept_by != above.id:
592
+ node.kept_by = above.id
593
+ spans.note("held_back", rule=node.name, priority=node.priority, by=above.id, by_name=above.name)
594
+ layer.changed = True
595
+ continue
596
+ node.kept_by = None
597
+ node.triggered += 1
598
+ node.unit = tree.spawn(node.do, name=node.name, rule=node.name, priority=rank, quiet=True,
599
+ until=over(node))
600
+ running.append(node.unit)
601
+ startable.add(node.name)
602
+ spans.note("rule", name=node.name, action="began", unit=node.unit.id, priority=rank)
603
+ layer.tell()
604
+ for node, unit in failures:
605
+ later = layer.children(node.under)
606
+ later = later[later.index(node) + 1:] if node in later else []
607
+ if node.told or any(found.name in startable for other in later for found in layer._leaves(other.name)):
608
+ continue
609
+ node.told = True
610
+ emit(tree, None, "reflex", Event(ev.REFLEX_FAILED, node.name, {
611
+ "reflex": node.name, "unit": unit.id, "began": node.triggered, **node.last}, state.get("step")))
@@ -67,9 +67,13 @@ def read_approval_key(pipe) -> str:
67
67
  # The kinds of sensitive steps.
68
68
  WORKER_APPROVAL_SECONDS = 300.0
69
69
  SENSITIVE = ("credentials", "payment", "sending", "deleting", "irreversible")
70
- # What the host itself asks the user through the same request (runtime/task/environments.py):
71
- # another environment for the conversation, sub-agents switched on. Never a worker's offer.
72
- HOST_ASKS = ("environment", "agents")
70
+ # What the host itself asks the user through the same request: another environment for the
71
+ # conversation, sub-agents switched on (runtime/task/environments.py), a step of a program
72
+ # on the machine (runtime/safety/permissions.py). Never a worker's offer.
73
+ HOST_ASKS = ("environment", "agents", "program")
74
+ # The levels of what the host asks about, strictest first: never; ask every time; ask once
75
+ # per conversation and kind; allow (settings: environments.attach, approvals.programs).
76
+ LEVELS = ("never", "ask_each", "ask_once_session", "allow")
73
77
  # The setting approvals.ask (settings.py).
74
78
  ASK = ("sensitive", "programs")
75
79
 
@@ -93,6 +97,7 @@ WORDS = {
93
97
  "sensitive.credentials": "credentials", "sensitive.payment": "a payment", "sensitive.sending": "sending",
94
98
  "sensitive.deleting": "deleting", "sensitive.irreversible": "a step that cannot be undone",
95
99
  "sensitive.environment": "another environment", "sensitive.agents": "sub-agents",
100
+ "sensitive.program": "a step of its program on this computer",
96
101
  "answer.once": "allow this time", "answer.app": "{app} is fine for this chat",
97
102
  "answer.session": "anything is fine for this chat", "answer.always": "always allow {app}",
98
103
  "answer.no": "don't allow", "answer.later": "not now", "answer.why": "reason (optional, it will see this)",