@lmmslab/harness 0.81.1 → 0.83.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 (80) 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 +197 -45
  9. package/python/harness/builtin/prompts/core/pack.toml +2 -2
  10. package/python/harness/builtin/prompts/reflect/core.md +4 -13
  11. package/python/harness/builtin/prompts/reflect/pack.toml +3 -2
  12. package/python/harness/builtin/prompts/shared/learning-goal.md +5 -29
  13. package/python/harness/builtin/prompts/shared/learning.md +8 -10
  14. package/python/harness/builtin/prompts/shared/web.md +11 -0
  15. package/python/harness/builtin/skills/agents/__init__.py +24 -22
  16. package/python/harness/builtin/skills/entries/__init__.py +43 -28
  17. package/python/harness/builtin/skills/entries/skill.toml +2 -3
  18. package/python/harness/builtin/skills/environments/__init__.py +2 -9
  19. package/python/harness/builtin/skills/mcp/__init__.py +2 -3
  20. package/python/harness/builtin/skills/memory/__init__.py +2 -5
  21. package/python/harness/builtin/skills/web/__init__.py +92 -0
  22. package/python/harness/builtin/skills/web/skill.toml +9 -0
  23. package/python/harness/cli.py +85 -17
  24. package/python/harness/config/agent.py +36 -9
  25. package/python/harness/core/episodes.py +5 -4
  26. package/python/harness/core/lanes.py +3 -0
  27. package/python/harness/core/memory.py +5 -1
  28. package/python/harness/core/remote.py +4 -3
  29. package/python/harness/core/worker.py +58 -3
  30. package/python/harness/extensions.py +23 -2
  31. package/python/harness/host/__init__.py +43 -1
  32. package/python/harness/host/client.py +36 -6
  33. package/python/harness/host/http.py +19 -0
  34. package/python/harness/host/server.py +17 -3
  35. package/python/harness/host/sessions.py +3 -2
  36. package/python/harness/host/tasks.py +107 -26
  37. package/python/harness/host/workspaces.py +69 -7
  38. package/python/harness/model_list.py +1 -1
  39. package/python/harness/packages/manager.py +5 -1
  40. package/python/harness/providers.py +14 -5
  41. package/python/harness/runtime/decision/loop.py +105 -117
  42. package/python/harness/runtime/decision/messages.py +158 -55
  43. package/python/harness/runtime/decision/model.py +85 -22
  44. package/python/harness/runtime/decision/notices.py +66 -0
  45. package/python/harness/runtime/decision/prompts.py +15 -11
  46. package/python/harness/runtime/decision/protocol.py +12 -6
  47. package/python/harness/runtime/decision/thoughts.py +10 -16
  48. package/python/harness/runtime/events.py +28 -13
  49. package/python/harness/runtime/execution/attached.py +1 -1
  50. package/python/harness/runtime/execution/control.py +17 -35
  51. package/python/harness/runtime/execution/entries.py +204 -124
  52. package/python/harness/runtime/execution/kernel.py +471 -281
  53. package/python/harness/runtime/execution/managed.py +231 -0
  54. package/python/harness/runtime/execution/noenv.py +2 -2
  55. package/python/harness/runtime/execution/results.py +2 -0
  56. package/python/harness/runtime/execution/session.py +284 -115
  57. package/python/harness/runtime/learning/active.py +41 -50
  58. package/python/harness/runtime/learning/fixtures.py +2 -2
  59. package/python/harness/runtime/learning/learner.py +75 -2
  60. package/python/harness/runtime/learning/library.py +77 -10
  61. package/python/harness/runtime/learning/views.py +12 -1
  62. package/python/harness/runtime/reactive/__init__.py +1 -1
  63. package/python/harness/runtime/reactive/rules.py +29 -394
  64. package/python/harness/runtime/reactive/tree.py +611 -0
  65. package/python/harness/runtime/safety/approval.py +8 -3
  66. package/python/harness/runtime/safety/permissions.py +242 -0
  67. package/python/harness/runtime/status.py +7 -2
  68. package/python/harness/runtime/task/delegation.py +97 -58
  69. package/python/harness/runtime/task/environments.py +3 -4
  70. package/python/harness/runtime/task/inbound.py +64 -0
  71. package/python/harness/runtime/task/run.py +1231 -790
  72. package/python/harness/runtime/tick.py +257 -91
  73. package/python/harness/sdk/__init__.py +7 -4
  74. package/python/harness/settings.py +30 -5
  75. package/python/harness/terminal.py +10 -6
  76. package/python/harness/transcript.py +18 -6
  77. package/python/harness/tui/answer.py +51 -0
  78. package/python/harness/tui/chat.py +75 -26
  79. package/python/harness/tui/views.py +1 -0
  80. package/python/harness/builtin/prompts/shared/entries.md +0 -86
@@ -1,26 +1,32 @@
1
1
  """A persistent Python kernel for model-written programs, in its own process.
2
2
 
3
- The host sends cells over stdin and reads one JSON reply per cell from a
4
- private descriptor, so anything a program prints cannot corrupt the protocol.
5
- The kernel namespace comes from a bootstrap callable `module:function` that
6
- receives JSON options and returns a session object with:
3
+ The host sends programs over stdin and reads JSON messages from a private descriptor, so
4
+ anything a program prints cannot corrupt the protocol. The kernel namespace comes from a
5
+ bootstrap callable `module:function` that receives JSON options and returns a session
6
+ object with:
7
7
 
8
8
  namespace: dict names visible to programs
9
- drain() -> dict attachments and events gathered during the cell
9
+ drain() -> dict what the unit that calls it gathered (attachments, events, state)
10
10
  close() -> None
11
11
 
12
- Between cells the host may ask for the truth of an expression on the namespace
13
- (Kernel.check), which runs no cell and drains nothing. While a cell runs it may run code
14
- beside it (Kernel.side), on a thread of its own in the same namespace: what it prints
15
- goes into the running cell's output, and a side_result message says how it ended.
12
+ Every program runs as a unit (runtime/execution/entries.py): a thread of its own in the one
13
+ namespace, with its own output, events, pictures and result, beside the units already
14
+ running. The reply to a program comes at once: its result when it was over within the
15
+ grace the host gave, else {"status": "running", "unit": id}; the result of a unit that
16
+ outlived its reply, and the end of every other unit (spawned code, a sub-agent), is a
17
+ `unit_end` message. The host may cancel a unit, pause all of them and resume.
18
+
19
+ A managed kernel (an isolated process whose programs only propose operations) speaks the
20
+ same protocol: the same loop serves it, and its proposals travel beside the units'
21
+ messages. The host takes nothing of it on trust: runtime/execution/managed.py admits each
22
+ proposal and keeps what each unit did.
16
23
 
17
24
  The kernel reports imported Harness package modules to the host as a `modules` note:
18
- those of the bootstrap once it is ready, then those new after each cell, side run and at
19
- the close. The trace records what code the run loaded.
25
+ those of the bootstrap once it is ready, then those new after each unit and at the close.
26
+ The trace records what code the run loaded.
20
27
  """
21
28
 
22
29
  import argparse
23
- import contextlib
24
30
  import importlib
25
31
  import io
26
32
  from functools import partial
@@ -35,11 +41,12 @@ import tempfile
35
41
  import threading
36
42
  import time
37
43
  import traceback
44
+ from types import SimpleNamespace
38
45
 
39
46
  from harness.paths import PACKAGE, ROOT, import_roots, package_roots
40
47
  from harness.core import spans
41
48
  from harness.runtime.execution import control
42
- from harness.runtime.events import EpisodeEnded, Escalation, ProgramFinished
49
+ from harness.runtime.execution.entries import Tree, current_entry
43
50
 
44
51
 
45
52
  # The kernel bootstrap: the embodiment's session with its skills (runtime/execution/session.py).
@@ -56,44 +63,111 @@ def kernel_python(runtime: dict) -> str:
56
63
  return runtime.get("python") or os.environ.get(KERNEL_PYTHON_ENV) or sys.executable
57
64
 
58
65
 
59
- MAX_OUTPUT_CHARS = 8000
66
+ # What the model is told of what a unit printed or returned: all of it up to INLINE_CHARS;
67
+ # more is saved to a file the model's programs can read, and it is told the file and the
68
+ # first PREVIEW_CHARS. A unit that failed is told the first and last FAILED_CHARS / 2.
69
+ INLINE_CHARS, PREVIEW_CHARS, FAILED_CHARS = 30_000, 2_000, 10_000
60
70
  # Program output is a bounded stream, including when a program never returns.
61
71
  MAX_PROGRAM_OUTPUT_CHARS = 1024 * 1024
72
+ _saved = SimpleNamespace(directory=None)
73
+
74
+
75
+ def told(text: str, name: str, failed: bool = False) -> str:
76
+ """`text` (what the unit `name` printed or returned) as the model is told it: whole
77
+ when it is short; else, of a unit that failed, its beginning and end with what lies
78
+ between counted; of any other, the file it was saved to and its beginning."""
79
+ if len(text) <= (FAILED_CHARS if failed else INLINE_CHARS):
80
+ return text
81
+ if failed:
82
+ half = FAILED_CHARS // 2
83
+ return text[:half] + f"\n... [{len(text) - FAILED_CHARS} characters omitted] ...\n" + text[-half:]
84
+ if _saved.directory is None:
85
+ _saved.directory = Path(tempfile.mkdtemp(prefix="harness-output-"))
86
+ path = _saved.directory / f"{name}.txt"
87
+ path.write_text(text, encoding="utf-8")
88
+ return (f"Output too large ({len(text)} characters). Full output saved to: {path}\n"
89
+ f"Preview (first {PREVIEW_CHARS} characters):\n{text[:PREVIEW_CHARS]}")
90
+ # The longest that what a unit printed waits before it goes to the host.
91
+ OUTPUT_NOTE_SECONDS = 1.0
62
92
 
63
93
 
64
94
  class ProgramOutput:
65
- def __init__(self, cell):
66
- self.cell, self.size, self.pending = cell, 0, ""
67
- self.head = self.tail = ""
95
+ """What one unit printed: bounded, and streamed to the host as program_output notes
96
+ (`cell`: the number of the model's program, None for other units), a full chunk at once
97
+ and anything less within OUTPUT_NOTE_SECONDS, so the host knows when a unit last printed."""
98
+
99
+ def __init__(self, cell, unit=None):
100
+ self.cell, self.unit, self.size, self.pending = cell, unit, 0, ""
101
+ self.parts = []
68
102
  self.exhausted = False
103
+ self._writing = threading.RLock()
104
+ self._due = None
69
105
 
70
106
  def write(self, text):
71
107
  text = str(text)
72
- if self.size + len(text) > MAX_PROGRAM_OUTPUT_CHARS:
73
- self.exhausted = True
74
- self.flush()
75
- raise RuntimeError("program output limit exceeded (1048576 characters); write large data in the target")
76
- self.size += len(text)
77
- self.head = (self.head + text)[:MAX_OUTPUT_CHARS // 2]
78
- self.tail = (self.tail + text)[-MAX_OUTPUT_CHARS // 2:]
79
- self.pending += text
80
- while len(self.pending) >= 8192:
81
- chunk, self.pending = self.pending[:8192], self.pending[8192:]
82
- spans.note("program_output", cell=self.cell, text=chunk)
108
+ with self._writing:
109
+ if self.size + len(text) > MAX_PROGRAM_OUTPUT_CHARS:
110
+ self.exhausted = True
111
+ self.flush()
112
+ raise RuntimeError("program output limit exceeded (1048576 characters); write large data in the target")
113
+ self.size += len(text)
114
+ self.parts.append(text)
115
+ self.pending += text
116
+ while len(self.pending) >= 8192:
117
+ chunk, self.pending = self.pending[:8192], self.pending[8192:]
118
+ self._note(chunk)
119
+ if self.pending and self._due is None:
120
+ self._due = threading.Timer(OUTPUT_NOTE_SECONDS, self.flush)
121
+ self._due.daemon = True
122
+ self._due.start()
83
123
  return len(text)
84
124
 
125
+ def _note(self, text):
126
+ spans.note("program_output", cell=self.cell, text=text, **({"unit": self.unit} if self.unit else {}))
127
+
85
128
  def flush(self):
86
- if self.pending:
87
- spans.note("program_output", cell=self.cell, text=self.pending)
88
- self.pending = ""
129
+ with self._writing:
130
+ if self._due is not None:
131
+ self._due.cancel()
132
+ self._due = None
133
+ if self.pending:
134
+ self._note(self.pending)
135
+ self.pending = ""
136
+
137
+ def recent(self, chars):
138
+ """The last `chars` characters printed so far (at most INLINE_CHARS); reading takes
139
+ nothing away."""
140
+ chars = max(0, min(int(chars), INLINE_CHARS))
141
+ with self._writing:
142
+ return "".join(self.parts)[-chars:] if chars else ""
89
143
 
90
144
  def getvalue(self):
91
145
  self.flush()
92
- if self.size <= MAX_OUTPUT_CHARS // 2:
93
- return self.head
94
- if self.size <= MAX_OUTPUT_CHARS:
95
- return self.head + self.tail[-(self.size - len(self.head)):]
96
- return self.head + f"\n... {self.size - MAX_OUTPUT_CHARS} characters omitted; see program_output trace ...\n" + self.tail
146
+ with self._writing:
147
+ return "".join(self.parts)
148
+
149
+
150
+ class UnitOutput:
151
+ """The kernel's sys.stdout or sys.stderr: what a unit's code writes (on its thread, or
152
+ on a thread acting for it) goes to that unit's output, anything else to `fallback`."""
153
+
154
+ def __init__(self, fallback):
155
+ self.fallback = fallback
156
+
157
+ def _sink(self):
158
+ entry = current_entry()
159
+ return (entry.box.output if entry is not None and entry.box.output is not None else None) or self.fallback
160
+
161
+ def write(self, text):
162
+ return self._sink().write(text)
163
+
164
+ def flush(self):
165
+ self._sink().flush()
166
+
167
+ def __getattr__(self, name):
168
+ return getattr(self.fallback, name)
169
+
170
+
97
171
  # Top-level packages of the source checkout that kernels import (skills, benches).
98
172
  REPO_PACKAGES = ("skills", "benches")
99
173
 
@@ -153,9 +227,10 @@ def sandbox_available() -> bool:
153
227
  class Kernel:
154
228
  """Host-side handle. One kernel lives for one episode.
155
229
 
156
- A managed workspace supplies its dispatcher and a launcher context manager factory.
157
- The launcher takes the dispatcher's options and yields the command and descriptors
158
- to pass to the kernel process.
230
+ A managed workspace supplies the admission of its proposals (`managed`:
231
+ runtime/execution/managed.py Admission) and a launcher context manager factory. The
232
+ launcher takes the admission's options and yields the command and descriptors to pass
233
+ to the kernel process.
159
234
  """
160
235
 
161
236
  def __init__(self, bootstrap: str, options: dict, *, python: str = sys.executable,
@@ -182,6 +257,10 @@ class Kernel:
182
257
  command = [python, "-u", "-m", "harness.runtime.execution.kernel",
183
258
  "--bootstrap", bootstrap, "--options", "-"]
184
259
  self._workspace = None
260
+ if not isolation and cwd is None and managed is None:
261
+ # Not isolated and given no folder: one of its own to work in. What a program
262
+ # writes outside it goes by the user's approvals (runtime/safety/permissions.py).
263
+ self._workspace = cwd = tempfile.mkdtemp(prefix="harness-kernel-")
185
264
  if isinstance(isolation, dict) and isolation.get("sandbox") is False:
186
265
  # No sandbox: the file guard inside the kernel (isolation.py) keeps programs to
187
266
  # the installation, the source and this workspace; the environment carries no
@@ -204,7 +283,8 @@ class Kernel:
204
283
  extra = isolation.get("hide", []) if isinstance(isolation, dict) else []
205
284
  workspace = Path(cwd).resolve() if cwd else None
206
285
  keep = [workspace] if workspace else []
207
- command = sandbox_command(command, keep=keep, read_only=[*prefixes, *source],
286
+ command = sandbox_command(command, keep=keep,
287
+ read_only=[*prefixes, *source, *map(Path, options.get("references") or ())],
208
288
  hide=hidden_roots([*prefixes, PACKAGE.parent, *keep], extra), cwd=workspace)
209
289
  env, cwd = kernel_environment(options, env, python), None
210
290
  if workspace:
@@ -216,7 +296,6 @@ class Kernel:
216
296
  with managed_launcher(managed.options()) as (launch, pass_fds):
217
297
  self.process = subprocess.Popen(launch, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
218
298
  stderr=self._log, env={}, pass_fds=pass_fds, bufsize=0)
219
- os.set_blocking(self.process.stdin.fileno(), False)
220
299
  else:
221
300
  self.process = subprocess.Popen(
222
301
  command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=self._log,
@@ -228,8 +307,13 @@ class Kernel:
228
307
  # run() and pause() may write from different threads; the order of what they
229
308
  # write is the order the kernel sees.
230
309
  self._writing = threading.Lock()
231
- self._running = False
232
- self._pause_pending = False
310
+ # A managed kernel: the cell whose reply is awaited, the cells whose reply went as
311
+ # "running" (their result is a unit_end), their deadlines, and what the isolated
312
+ # kernel calls each unit.
313
+ self._awaited = None
314
+ self._detached = set()
315
+ self._deadlines = {}
316
+ self._names = {}
233
317
  self._reader = threading.Thread(target=self._read, daemon=True)
234
318
  self._reader.start()
235
319
  self.cells = 0
@@ -261,7 +345,7 @@ class Kernel:
261
345
  continue
262
346
  if isinstance(message, dict) and message.get("op") in ("span_start", "span_end", "note", "entry_start",
263
347
  "entry_claims", "entry_end", "world",
264
- "side_result"):
348
+ "unit_end"):
265
349
  if self.on_span is not None:
266
350
  try:
267
351
  self.on_span(message)
@@ -278,8 +362,9 @@ class Kernel:
278
362
  self._replies.put(None)
279
363
 
280
364
  def _read_managed(self):
281
- # After startup every child message is untrusted. Only proposals and one reply
282
- # for the active host cell enter the managed conversation.
365
+ # After startup every child message is untrusted. Only proposals, the reply to the
366
+ # program that is awaited and the end of a program that outlived its reply enter
367
+ # the conversation, each admitted by the host (runtime/execution/managed.py).
283
368
  stream = self.process.stdout
284
369
  limit = 64 * 1024
285
370
  try:
@@ -293,14 +378,26 @@ class Kernel:
293
378
  message = json.loads(line)
294
379
  if not isinstance(message, dict):
295
380
  break
296
- if message.get("op") == "workspace_proposal":
381
+ op, cell = message.get("op"), message.get("cell")
382
+ if op == "workspace_proposal":
297
383
  identifier = message.get("id")
298
384
  def reply(result, identifier=identifier):
299
385
  self._write({"op": "workspace_result", "id": identifier, **result})
300
386
  if not self._managed.submit(message, reply):
301
387
  break
302
- elif message.get("op") is None and self._managed.accept_result(message.get("cell")):
303
- self._replies.put(self._managed.result(message))
388
+ elif op is None and message.get("status") == "running":
389
+ unit = self._managed.running(cell) if cell == self._awaited else None
390
+ if unit is None:
391
+ break
392
+ name = message.get("unit")
393
+ self._names[cell] = name[:32] if isinstance(name, str) else None
394
+ self._detached.add(cell)
395
+ self._replies.put({"status": "running", "unit": unit, "cell": cell})
396
+ elif op in (None, "unit_end") and self._managed.settled(cell):
397
+ continue
398
+ elif ((op is None and cell == self._awaited and cell not in self._detached)
399
+ or (op == "unit_end" and cell in self._detached)) and self._managed.accept_result(cell):
400
+ self._managed_result(message, cell)
304
401
  else:
305
402
  break
306
403
  except (ValueError, OSError):
@@ -309,6 +406,32 @@ class Kernel:
309
406
  self.kill()
310
407
  self._replies.put(None)
311
408
 
409
+ def _managed_result(self, message, cell) -> None:
410
+ """The result of a managed program, as the host kept it: the reply to its program
411
+ while that is awaited, else a unit_end for on_span."""
412
+ deadline = self._deadlines.pop(cell, None)
413
+ if deadline is not None:
414
+ deadline.cancel()
415
+ self._names.pop(cell, None)
416
+ result = self._managed.result(message, cell)
417
+ if cell in self._detached:
418
+ self._detached.discard(cell)
419
+ if self.on_span is not None:
420
+ try:
421
+ self.on_span({"op": "unit_end", **result})
422
+ except Exception:
423
+ pass
424
+ else:
425
+ self._replies.put(result)
426
+
427
+ def _managed_expired(self, cell) -> None:
428
+ """A managed program's time is over and no result came: the host cancels its unit
429
+ and ends it itself (the isolated kernel's word is not waited for)."""
430
+ if self._managed.accept_result(cell):
431
+ self._managed.cancel(self._managed.unit(cell))
432
+ self._ask({"op": "cancel", "unit": self._names.get(cell)})
433
+ self._managed_result({"status": "error", "error": "Program deadline exceeded"}, cell)
434
+
312
435
  def _receive(self, timeout):
313
436
  try:
314
437
  reply = self._replies.get(timeout=timeout)
@@ -319,128 +442,134 @@ class Kernel:
319
442
  raise KernelDied(f"Kernel exited with code {self.process.wait()}")
320
443
  return reply
321
444
 
445
+ def start(self, code: str, grace: float, timeout: float | None = None, *, name: str | None = None,
446
+ announce=None) -> dict:
447
+ """Start `code` as a unit called `name` and return the kernel's reply, at most
448
+ `grace` seconds later: the unit's result when it was over by then (status ok,
449
+ error, escalated, episode_ended or finished, clipped stdout, error, seconds, and
450
+ what the unit gathered: attachments, events, state, usage), else {"status":
451
+ "running", "unit"}: its result then comes as a `unit_end` message to on_span. Both
452
+ carry `cell`, this kernel's number of the program, which `announce(cell)` is told
453
+ before the program is sent. The kernel cancels the unit after `timeout` seconds.
454
+ Raises KernelDied when the kernel is not running, exits, or does not reply."""
455
+ request = {"op": "run", "code": code, "grace": max(0.0, float(grace))}
456
+ if timeout is not None:
457
+ request["timeout"] = float(timeout)
458
+ if name:
459
+ request["name"] = str(name)
460
+ return self._cell(request, float(grace) + 60.0, announce)
461
+
322
462
  def run(self, code: str, timeout: float) -> dict:
323
- """Run `code` as the next cell and return the kernel's reply: status (ok, error,
324
- escalated, episode_ended or finished), clipped stdout, error, seconds, and what the
325
- session drained (attachments, events, state, usage). Raises KernelDied when the
326
- kernel is not running, exits, or does not reply within `timeout` seconds."""
327
- return self._cell({"op": "run", "code": code}, timeout)
463
+ """Run `code` as a unit and wait for its result (see start), at most `timeout`
464
+ seconds, after which the kernel cancels it."""
465
+ return self._cell({"op": "run", "code": code, "grace": float(timeout) + 10.0, "timeout": float(timeout)},
466
+ float(timeout) + 70.0)
467
+
468
+ def run_staged(self, code: str, timeout: float) -> dict:
469
+ """Run `code` (an entry's own test) as a cell on the library entries prepared and not
470
+ yet committed (load_library "prepare"): it sees them and the namespace, the names it
471
+ binds are dropped, and what it executes counts as testing, not as uses. It runs as a
472
+ unit like every program and is waited for; the kernel ends it after `timeout`
473
+ seconds. The reply is run()'s; the host that asked reads it (runtime/task/run.py)."""
474
+ return self._cell({"op": "run", "code": code, "staged": True, "grace": float(timeout) + 10.0,
475
+ "timeout": float(timeout)}, float(timeout) + 70.0)
328
476
 
329
477
  def load_library(self, phase, entries=(), *, timeout=120):
330
478
  return self._cell({"op": "library", "phase": phase,
331
479
  "entries": [(item.revision, item.pack().decode(), hand) for item, hand in entries]}, timeout)
332
480
 
333
- def _cell(self, request, timeout):
481
+ def _cell(self, request, timeout, announce=None):
334
482
  if self.process.poll() is not None:
335
483
  raise KernelDied("Kernel is not running")
336
484
  self.cells += 1
485
+ cell = self.cells
486
+ if announce is not None:
487
+ announce(cell)
337
488
  if self._managed is not None:
338
489
  if request["op"] != "run":
339
- raise ValueError("Managed workspace cells only support running programs")
340
- if request["code"] == "__wrap_up__()":
341
- return self._managed.result({"status": "ok"})
342
- self._managed.begin(self.cells)
490
+ raise ValueError("A managed kernel only runs programs")
491
+ self._managed.begin(cell, request.get("name"))
492
+ if request.get("timeout") is not None:
493
+ # The host keeps the program's time itself, a moment after the isolated
494
+ # kernel should have.
495
+ deadline = self._deadlines[cell] = threading.Timer(
496
+ float(request["timeout"]) + 5.0, self._managed_expired, (cell,))
497
+ deadline.daemon = True
498
+ deadline.start()
343
499
  with self._writing:
344
- request["cell"] = self.cells
345
- if self._pause_pending:
346
- request["pause"] = True
347
- self._pause_pending = False
348
- self._running = True
500
+ request["cell"] = cell
501
+ self._awaited = cell
349
502
  self._send(request)
350
- try:
351
- result = self._receive(timeout)
352
- if result.get("cell", self.cells) != self.cells:
353
- self.kill()
354
- raise KernelDied("Kernel returned a result for another cell")
355
- return result
356
- finally:
357
- if self._managed is not None:
358
- self._managed.stop()
359
- with self._writing:
360
- self._running = False
361
- self._pause_pending = False
362
-
363
- def check(self, condition: str, output: str, seconds: float) -> dict:
364
- """Evaluate `condition` (an expression) on what the last cell left, with `output`
365
- (what it printed) as the name output: {"value": its truth} or {"error": why not},
366
- within `seconds` (runtime/conversation.py: what a reply made while the cell ran
367
- expected it to leave)."""
368
- if self._managed is not None:
369
- return {"error": "Managed workspace cells do not support speculative checks"}
370
- if self.process.poll() is not None:
371
- raise KernelDied("Kernel is not running")
372
- with self._writing:
373
- self._send({"op": "check", "condition": condition, "output": output, "seconds": seconds})
374
- return self._receive(seconds + 30)
503
+ result = self._receive(timeout)
504
+ if result.get("cell", cell) != cell:
505
+ self.kill()
506
+ raise KernelDied("Kernel returned a result for another cell")
507
+ return result
375
508
 
376
509
  def _send(self, request: dict) -> None:
377
510
  data = (json.dumps(request) + "\n").encode()
378
511
  if self._managed is None:
379
512
  self.process.stdin.write(data)
380
- else:
381
- import selectors
513
+ self.process.stdin.flush()
514
+ return
515
+ # An isolated kernel that stops reading must not hold the host: the write has
516
+ # five seconds.
517
+ done, failed = threading.Event(), []
382
518
 
383
- view = memoryview(data)
384
- deadline = time.monotonic() + 5
385
- with selectors.DefaultSelector() as selector:
386
- selector.register(self.process.stdin, selectors.EVENT_WRITE)
519
+ def write():
520
+ try:
521
+ view = memoryview(data)
387
522
  while view:
388
- if not selector.select(max(0, deadline - time.monotonic())):
389
- self.kill()
390
- raise BrokenPipeError("The managed kernel stopped reading")
391
- count = self.process.stdin.write(view)
392
- if count is not None:
393
- view = view[count:]
394
- self.process.stdin.flush()
523
+ view = view[self.process.stdin.write(view) or 0:]
524
+ self.process.stdin.flush()
525
+ except (OSError, ValueError) as error:
526
+ failed.append(error)
527
+ finally:
528
+ done.set()
529
+
530
+ threading.Thread(target=write, name="managed-write", daemon=True).start()
531
+ if not done.wait(5):
532
+ self.kill()
533
+ raise BrokenPipeError("The managed kernel stopped reading")
534
+ if failed:
535
+ raise BrokenPipeError(str(failed[0]))
395
536
 
396
537
  def _write(self, request: dict) -> None:
397
538
  with self._writing:
398
539
  self._send(request)
399
540
 
400
541
  def pause(self) -> None:
401
- """Ask the program that runs, or is about to run, to stop at its next execution step
402
- (runtime/execution/control.py); nothing when the kernel no longer runs."""
542
+ """Pause the model's units: each stops at its next execution step or wait, and
543
+ nothing acts until resume() (runtime/execution/control.py); nothing when the kernel no
544
+ longer runs. A managed kernel's proposals are refused by the host meanwhile."""
403
545
  if self._managed is not None:
404
546
  self._managed.stop()
405
- return
406
- if self.process.poll() is not None:
407
- return
408
- with self._writing:
409
- if not self._running:
410
- # run() is about to send its cell: the request goes with it.
411
- self._pause_pending = True
412
- return
413
- try:
414
- self._send({"op": "pause"})
415
- except OSError:
416
- pass
547
+ self._ask({"op": "pause"})
417
548
 
418
- def message(self) -> None:
419
- """Tell the running cell a message for the model came (runtime/execution/control.py)."""
549
+ def resume(self) -> None:
550
+ """The pause is over."""
420
551
  if self._managed is not None:
421
- self._managed.stop()
422
- return
423
- with self._writing:
424
- if not self._running or self.process.poll() is not None:
425
- return
426
- try:
427
- self._send({"op": "message"})
428
- except OSError:
429
- pass
552
+ self._managed.resume()
553
+ self._ask({"op": "resume"})
430
554
 
431
- def side(self, code: str, identifier: str) -> bool:
432
- """Run `code` beside the running cell, in the same namespace (its side_result
433
- message, with `identifier`, goes to on_span); False when no cell runs."""
555
+ def cancel(self, unit: str | None = None) -> None:
556
+ """Cancel the unit `unit` (None: every unit of the model); its end comes as its
557
+ result or a `unit_end` message."""
434
558
  if self._managed is not None:
435
- return False
436
- with self._writing:
437
- if not self._running or self.process.poll() is not None:
438
- return False
439
- try:
440
- self._send({"op": "side", "id": identifier, "code": code})
441
- except OSError:
442
- return False
443
- return True
559
+ # The host ends the unit's part here; the isolated kernel is told its own name of it.
560
+ cells = self._managed.cancel(unit)
561
+ for cell in ([None] if unit is None else cells):
562
+ self._ask({"op": "cancel", "unit": None if cell is None else self._names.get(cell)})
563
+ return
564
+ self._ask({"op": "cancel", "unit": unit})
565
+
566
+ def _ask(self, request) -> None:
567
+ if self.process.poll() is not None:
568
+ return
569
+ try:
570
+ self._write(request)
571
+ except OSError:
572
+ pass
444
573
 
445
574
  def answer(self, identifier, value) -> None:
446
575
  """Answer a request the running program sent as a note (runtime/execution/control.py);
@@ -453,8 +582,8 @@ class Kernel:
453
582
  pass
454
583
 
455
584
  def agent_ended(self, agent, report, *, cancelled=False) -> None:
456
- """Tell the program that a sub-agent it started ended, with its report (its entry
457
- ends, runtime/execution/entries.py); nothing when the kernel no longer runs."""
585
+ """Tell the kernel that a sub-agent ended, with its report (its unit ends,
586
+ runtime/execution/entries.py); nothing when the kernel no longer runs."""
458
587
  if self.process.poll() is not None:
459
588
  return
460
589
  try:
@@ -490,13 +619,6 @@ class Kernel:
490
619
  shutil.rmtree(self._workspace, ignore_errors=True)
491
620
 
492
621
 
493
- def _clip(text: str) -> str:
494
- if len(text) <= MAX_OUTPUT_CHARS:
495
- return text
496
- half = MAX_OUTPUT_CHARS // 2
497
- return text[:half] + f"\n... [{len(text) - MAX_OUTPUT_CHARS} characters omitted] ...\n" + text[-half:]
498
-
499
-
500
622
  def format_error(error: BaseException) -> str:
501
623
  """Traceback restricted to program frames, plus the frame that raised."""
502
624
  summary = traceback.TracebackException.from_exception(error)
@@ -536,8 +658,10 @@ def _serve(bootstrap: str, options: dict, *, forward_workspace_requests: bool =
536
658
  channel = os.fdopen(os.dup(1), "w", buffering=1, encoding="utf-8")
537
659
  # Stray writes to descriptor 1 from programs or libraries go to the log instead.
538
660
  os.dup2(2, 1)
539
- sys.stdout = io.TextIOWrapper(os.fdopen(1, "wb", buffering=0), encoding="utf-8", errors="replace",
540
- write_through=True)
661
+ # What a unit prints is its own output; anything else goes to the log.
662
+ sys.stdout = UnitOutput(io.TextIOWrapper(os.fdopen(1, "wb", buffering=0), encoding="utf-8", errors="replace",
663
+ write_through=True))
664
+ sys.stderr = UnitOutput(sys.stderr)
541
665
 
542
666
  writing = threading.Lock()
543
667
 
@@ -548,20 +672,38 @@ def _serve(bootstrap: str, options: dict, *, forward_workspace_requests: bool =
548
672
 
549
673
  control.host_thoughts = bool(options.get("host_thoughts"))
550
674
  managed = forward_workspace_requests
551
- proposal_result = queue.Queue()
675
+ proposals = {}
552
676
  proposal_lock = threading.Lock()
553
- cell = sequence = 0
677
+ sequence = 0
554
678
 
555
679
  def propose(name, *args, **kwargs):
680
+ """Propose an operation to the host as the unit that calls it, and wait for the
681
+ host's answer (or for what ends the unit: its cancellation, a pause)."""
556
682
  nonlocal sequence
683
+ unit = current_entry()
684
+ if unit is None or unit.cell is None:
685
+ raise RuntimeError("Only a program proposes operations")
686
+ tree.check(unit)
687
+ answered = queue.Queue(1)
557
688
  with proposal_lock:
558
689
  sequence += 1
559
- reply({"op": "workspace_proposal", "id": sequence, "cell": cell,
690
+ identifier = sequence
691
+ proposals[identifier] = answered
692
+ reply({"op": "workspace_proposal", "id": identifier, "cell": unit.cell,
560
693
  "name": name, "args": args, "kwargs": kwargs})
561
- answer = proposal_result.get()
562
- if answer.get("id") != sequence or "error" in answer:
563
- raise RuntimeError(answer.get("error") or "The managed workspace reply is out of order")
564
- return answer.get("value")
694
+ try:
695
+ while True:
696
+ try:
697
+ answer = answered.get(timeout=0.1)
698
+ break
699
+ except queue.Empty:
700
+ tree.check(unit)
701
+ finally:
702
+ with proposal_lock:
703
+ proposals.pop(identifier, None)
704
+ if "error" in answer:
705
+ raise RuntimeError(answer.get("error") or "The host refused the proposal")
706
+ return answer.get("value")
565
707
 
566
708
  if options.get("host_controls"):
567
709
  options["worker_relay"] = partial(control.worker_call, reply)
@@ -582,7 +724,17 @@ def _serve(bootstrap: str, options: dict, *, forward_workspace_requests: bool =
582
724
  sandboxed = not (isinstance(isolation, dict) and isolation.get("sandbox") is False)
583
725
  source = [*map(Path, import_roots()[1:]), PACKAGE, *(ROOT / name for name in REPO_PACKAGES)]
584
726
  # The sandbox's /tmp is its own; without it the workspace is the kernel's working directory.
585
- install_file_guard(read_roots(source), [Path.cwd(), *([Path("/tmp")] if sandboxed else [])])
727
+ # (The environment's references: the documentation and source of what it gives to act with.)
728
+ install_file_guard([*read_roots(source), *map(Path, options.get("references") or ())],
729
+ [Path.cwd(), *([Path("/tmp")] if sandboxed else [])])
730
+ elif not managed:
731
+ # Not isolated: programs run on the machine as the user, and what they do beyond their
732
+ # own folder goes by the user's approvals (runtime/safety/permissions.py).
733
+ from harness.runtime.safety import permissions
734
+ from harness.runtime.safety.isolation import endpoints
735
+
736
+ permissions.install(endpoints(options), [Path.cwd(), Path(tempfile.gettempdir())],
737
+ lambda **step: control.ask_host("program", **step))
586
738
  private = []
587
739
  if options.get("private_paths"):
588
740
  from harness.runtime.safety.isolation import install_private_guard
@@ -605,35 +757,139 @@ def _serve(bootstrap: str, options: dict, *, forward_workspace_requests: bool =
605
757
  spans.note("modules", loaded=new)
606
758
 
607
759
  report_modules()
608
- # The names the session provides, restored after every cell that rebinds them.
760
+ # The names the session provides, restored after every program that rebinds them.
609
761
  api = {name: value for name, value in namespace.items() if not name.startswith("__")}
610
- # Requests are read on their own thread so that a pause reaches a running program.
611
- requests = queue.Queue()
762
+ api_lock = threading.Lock()
763
+ # The session's units (runtime/execution/entries.py); a session without a tree gets one here.
764
+ tree = getattr(session, "_entry_tree", None)
765
+ own_tree = tree is None
766
+ if own_tree:
767
+ tree = Tree(SimpleNamespace(state={}, reactive=None, _events=[], _ticker=SimpleNamespace(channels={})))
768
+ tree.new_output = lambda entry: ProgramOutput(entry.cell, entry.id)
769
+ # The programs whose reply has not gone yet, by number: {"result", "detached", "done"}.
770
+ replies = {}
771
+ replying = threading.Lock()
612
772
  active_library = {}
613
773
  staged_library = None
614
774
 
775
+ def ended(result, timed_out) -> str:
776
+ status = result["status"]
777
+ if timed_out and status == "error":
778
+ return "its time limit"
779
+ trace = result.get("escalation") or result.get("episode_ended")
780
+ if trace:
781
+ return f"the event {trace['event']['kind']}"
782
+ return {"ok": "by itself", "error": "an error", "cancelled": "cancelled", "finished": "finish"}.get(status, status)
783
+
784
+ def unit_ended(entry, handled):
785
+ """A unit ended: its result is the reply to its program when that has not gone
786
+ yet, else a unit_end message."""
787
+ box = entry.box
788
+ result = {"unit": entry.id, "name": entry.name, "kind": entry.kind, **(entry.outcome or {"status": entry.status})}
789
+ if entry.raised is not None:
790
+ result["error"] = "KeyboardInterrupt" if isinstance(entry.raised, KeyboardInterrupt) else format_error(
791
+ entry.raised)
792
+ timer = getattr(entry, "deadline", None)
793
+ if timer is not None:
794
+ timer.cancel()
795
+ if getattr(entry, "timed_out", False) and result["status"] not in ("episode_ended", "finished"):
796
+ result.update(status="error", error=result.get("error") or "Program deadline exceeded")
797
+ # The end report every unit gives, whoever started it (runtime/decision/messages.py ended()):
798
+ # how it ended, and whether what it was for is the case (None: it stated no condition).
799
+ result["ended"] = ended(result, getattr(entry, "timed_out", False))
800
+ result["done"] = entry.ok
801
+ if entry.cell is not None:
802
+ with api_lock:
803
+ # Names the session gained while the program ran (an attached environment,
804
+ # skills switched on) are part of its API from now on; those it gave up
805
+ # are no longer.
806
+ changed = getattr(session, "_api_changes", None)
807
+ if changed:
808
+ for name, value in changed.items():
809
+ if value is None:
810
+ api.pop(name, None)
811
+ namespace.pop(name, None)
812
+ else:
813
+ api[name] = namespace[name] = value
814
+ changed.clear()
815
+ restored = restore_api(namespace, api)
816
+ if restored and box.output is not None and not box.output.exhausted:
817
+ box.output.write(f"\nWarning: the program rebound the API name(s) {', '.join(restored)}; they were "
818
+ "restored to the built-in skills after it ended. Give your own functions and "
819
+ "variables other names.\n")
820
+ if box.output is not None:
821
+ if box.output.exhausted:
822
+ result.update(status="error", error="Program output resource limit exceeded")
823
+ result["stdout"] = told(box.output.getvalue(), f"program-{entry.cell}" if entry.cell is not None
824
+ else f"unit-{entry.id}", failed=result.get("status") == "error")
825
+ result["seconds"] = (entry.ended or time.time()) - entry.started
826
+ report_modules()
827
+ if entry.cell is not None:
828
+ try:
829
+ result.update(session.drain())
830
+ except Exception as error:
831
+ result["drain_error"] = format_error(error)
832
+ result["cell"] = entry.cell
833
+ else:
834
+ for key in ("attachments", "events", "looks", "event_counts"):
835
+ if getattr(box, key):
836
+ result[key] = getattr(box, key)
837
+ if entry.status == "done" and entry.result is not None:
838
+ returned = entry.result if isinstance(entry.result, (dict, list, str, int, float, bool)) else repr(
839
+ entry.result)
840
+ result["returned"] = returned if entry.agent is not None else told(str(returned), f"returned-{entry.id}")
841
+ if entry.agent is not None:
842
+ result["agent"] = entry.agent
843
+ if handled:
844
+ result["handled"] = True
845
+ if entry.quiet:
846
+ # The tree of reflexes answers the ends of its programs itself (runtime/reactive/tree.py).
847
+ result["quiet"] = True
848
+ with replying:
849
+ waiting = replies.get(entry.cell) if entry.cell is not None else None
850
+ if waiting is not None and not waiting["detached"]:
851
+ waiting["result"] = result
852
+ waiting["done"].set()
853
+ else:
854
+ reply({"op": "unit_end", **result})
855
+
856
+ tree.on_end = unit_ended
857
+
615
858
  def wake_entries():
616
- tree = getattr(session, "_entry_tree", None)
617
- if tree is not None:
618
- with tree._changed:
619
- tree._changed.notify_all()
859
+ with tree._changed:
860
+ tree._changed.notify_all()
861
+
862
+ def cancel_units(identifier):
863
+ targets = tree.running() if identifier is None else [tree.entries.get(str(identifier))]
864
+ for entry in targets:
865
+ if entry is not None and entry is not tree.root and entry.running:
866
+ try:
867
+ entry.cancel()
868
+ except BaseException:
869
+ pass
870
+
871
+ # Requests are read on their own thread so that a pause or a cancellation reaches the
872
+ # units while the main thread waits for a program's grace.
873
+ requests = queue.Queue()
620
874
 
621
875
  def read_requests():
622
876
  for line in sys.stdin:
623
877
  request = json.loads(line)
624
878
  if managed and request["op"] == "workspace_result":
625
- proposal_result.put(request)
879
+ with proposal_lock:
880
+ answered = proposals.get(request.get("id"))
881
+ if answered is not None:
882
+ answered.put(request)
626
883
  continue
627
884
  if request["op"] == "pause":
628
885
  control.request_pause()
629
886
  wake_entries()
630
887
  continue
631
- if request["op"] == "message":
632
- control.request_message()
633
- wake_entries()
888
+ if request["op"] == "resume":
889
+ control.resume()
634
890
  continue
635
- if request["op"] == "side":
636
- _side(namespace, request, reply, report_modules, getattr(session, "_entry_tree", None))
891
+ if request["op"] == "cancel":
892
+ threading.Thread(target=cancel_units, args=(request.get("unit"),), name="cancel", daemon=True).start()
637
893
  continue
638
894
  if request["op"] == "answer":
639
895
  control.answer(request["id"], request.get("value"))
@@ -642,11 +898,6 @@ def _serve(bootstrap: str, options: dict, *, forward_workspace_requests: bool =
642
898
  control.agent_ended(request.get("agent"), request.get("report"),
643
899
  cancelled=bool(request.get("cancelled")))
644
900
  continue
645
- if request["op"] == "run":
646
- # A pause belongs to the cell after which it was sent.
647
- control.clear()
648
- if request.get("pause"):
649
- control.request_pause()
650
901
  requests.put(request)
651
902
  requests.put(None)
652
903
 
@@ -655,9 +906,6 @@ def _serve(bootstrap: str, options: dict, *, forward_workspace_requests: bool =
655
906
  request = requests.get()
656
907
  if request is None or request["op"] == "close":
657
908
  break
658
- if request["op"] == "check":
659
- reply(_check(namespace, request))
660
- continue
661
909
  if request["op"] == "library":
662
910
  from harness.runtime.learning.library import Revision, publish_entries, stage_entries
663
911
 
@@ -685,114 +933,56 @@ def _serve(bootstrap: str, options: dict, *, forward_workspace_requests: bool =
685
933
  staged_library = None
686
934
  reply({"status": "error", "error": format_error(error)})
687
935
  continue
688
- if managed:
689
- cell = request["cell"]
690
- started = time.perf_counter()
691
- buffer = ProgramOutput(request["cell"])
692
- result = {"status": "ok", "error": None, "escalation": None, "episode_ended": None}
693
- # The session's entries (runtime/execution/entries.py): an event for the model that came while
694
- # no program ran ends this one before it starts, and one that came while it ran
695
- # ends it if it did not meet it.
696
- tree = getattr(session, "_entry_tree", None)
697
- try:
698
- code = compile(request["code"], f"<cell-{request['cell']}>", "exec")
699
- with contextlib.redirect_stdout(buffer), contextlib.redirect_stderr(buffer):
700
- if tree is not None:
701
- tree.begin_program()
702
- exec(code, namespace)
703
- if tree is not None:
704
- tree.end_program()
705
- except Escalation as escalation:
706
- result["status"] = "escalated"
707
- result["escalation"] = escalation.trace.to_dict()
708
- except EpisodeEnded as ended:
709
- result["status"] = "episode_ended"
710
- result["episode_ended"] = ended.trace.to_dict()
711
- except ProgramFinished as finished:
712
- result["status"] = "finished"
713
- result["finish"] = finished.summary
714
- except KeyboardInterrupt:
715
- result["status"] = "error"
716
- result["error"] = "KeyboardInterrupt"
717
- except BaseException as error:
718
- result["status"] = "error"
719
- result["error"] = format_error(error)
720
- if control.host_thoughts:
721
- spans.note("cell_finishing", cell=request["cell"])
722
- # Names the session gained while the cell ran (an attached environment, skills
723
- # switched on) are part of its API from now on; those it gave up are no longer.
724
- changed = getattr(session, "_api_changes", None)
725
- if changed:
726
- for name, value in changed.items():
727
- if value is None:
728
- api.pop(name, None)
729
- namespace.pop(name, None)
730
- else:
731
- api[name] = namespace[name] = value
732
- changed.clear()
733
- restored = restore_api(namespace, api)
734
- if restored:
735
- buffer.write(f"\nWarning: the program rebound the API name(s) {', '.join(restored)}; they were restored "
736
- "to the built-in skills after it ended. Give your own functions and variables other names.\n")
737
- result["stdout"] = _clip(buffer.getvalue())
738
- if buffer.exhausted:
739
- result.update(status="error", error="Program output resource limit exceeded")
740
- result["seconds"] = time.perf_counter() - started
741
- report_modules()
742
- try:
743
- result.update(session.drain())
744
- except Exception as error:
745
- result["drain_error"] = format_error(error)
746
- result["cell"] = request["cell"]
747
- reply(result)
748
- report_modules()
749
- session.close()
750
-
751
-
752
- def _side(namespace: dict, request: dict, reply, report_modules=None, tree=None) -> None:
753
- """Run a side request's code on a thread of its own in `namespace`, beside the cell,
754
- and reply how it ended (side_result: status ok, escalated or error), after
755
- `report_modules()` tells the host the modules it imported."""
936
+ number = request["cell"]
937
+ waiting = {"result": None, "detached": False, "done": threading.Event()}
938
+ with replying:
939
+ replies[number] = waiting
756
940
 
757
- def run():
758
- result = {"op": "side_result", "id": request.get("id"), "status": "ok", "error": None}
759
- try:
760
- exec(compile(request["code"], f"<side-{request.get('id')}>", "exec"), namespace)
761
- except Escalation as escalation:
762
- result.update(status="escalated", escalation=escalation.trace.to_dict())
763
- except BaseException as error:
764
- result.update(status="error", error=format_error(error))
765
- if report_modules is not None:
766
- report_modules()
767
- reply(result)
768
-
769
- if tree is None:
770
- threading.Thread(target=run, name="side", daemon=True).start()
771
- else:
772
- try:
773
- tree.spawn(run, name=f"parallel {request['id']}")
774
- except Exception as error:
775
- reply({"op": "side_result", "id": request.get("id"), "status": "error", "error": format_error(error)})
941
+ staged = bool(request.get("staged"))
776
942
 
943
+ def program(source=request["code"], number=number, library=staged_library if staged else None):
944
+ code = compile(source, f"<cell-{number}>", "exec")
945
+ if library is None:
946
+ exec(code, namespace)
947
+ return
948
+ from harness.runtime.learning.library import testing_staged
777
949
 
778
- def _check(namespace: dict, request: dict) -> dict:
779
- """The truth of a check request's condition on `namespace`, with its output as the
780
- name output, evaluated on a thread of its own for at most its seconds."""
781
- checked = {}
950
+ # An entry's own test runs on a copy of the staged scope (the entry is not in the
951
+ # programs' namespace before the host admits it), and what it executes counts as
952
+ # testing, not as uses.
953
+ with spans.testing(True), testing_staged(library) as target:
954
+ exec(code, target)
782
955
 
783
- def evaluate():
784
956
  try:
785
- scope = {**namespace, "output": request.get("output") or ""}
786
- checked["value"] = bool(eval(compile(request["condition"], "<assume>", "eval"), scope))
957
+ if staged and staged_library is None:
958
+ raise RuntimeError("no library entry is prepared for its test")
959
+ unit = tree.spawn(program, name=request.get("name") or f"cell {number}", cell=number)
787
960
  except BaseException as error:
788
- checked["error"] = f"{type(error).__name__}: {error}"[:300]
789
-
790
- thread = threading.Thread(target=evaluate, name="check", daemon=True)
791
- thread.start()
792
- thread.join(float(request.get("seconds") or 2.0))
793
- if thread.is_alive():
794
- return {"status": "check", "error": "the condition took too long"}
795
- return {"status": "check", **checked}
961
+ with replying:
962
+ del replies[number]
963
+ reply({"status": "error", "error": format_error(error), "stdout": "", "seconds": 0.0, "cell": number})
964
+ continue
965
+ if request.get("timeout") is not None:
966
+ def expire(unit=unit):
967
+ unit.timed_out = True
968
+ unit.cancel()
969
+
970
+ unit.deadline = threading.Timer(float(request["timeout"]), expire)
971
+ unit.deadline.daemon = True
972
+ unit.deadline.start()
973
+ # The reply goes at once: the result when the program is over within its grace
974
+ # (no grace given: whenever it is over), else its handle.
975
+ waiting["done"].wait(request.get("grace"))
976
+ with replying:
977
+ # (Under the lock: a unit_end of this program never comes before this reply.)
978
+ del replies[number]
979
+ result = waiting["result"]
980
+ waiting["detached"] = result is None
981
+ reply(result if result is not None else {"status": "running", "unit": unit.id, "cell": number})
982
+ report_modules()
983
+ if own_tree:
984
+ tree.close()
985
+ session.close()
796
986
 
797
987
 
798
988
  def main(argv=None):