@lmmslab/harness 0.81.1 → 0.82.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +24 -35
  2. package/package.json +2 -2
  3. package/python/harness/README.md +3 -3
  4. package/python/harness/__init__.py +1 -1
  5. package/python/harness/builtin/configs/assistant.toml +0 -1
  6. package/python/harness/builtin/configs/default.toml +1 -1
  7. package/python/harness/builtin/prompts/conversation/core.md +2 -2
  8. package/python/harness/builtin/prompts/core/core.md +177 -41
  9. package/python/harness/builtin/prompts/core/pack.toml +2 -2
  10. package/python/harness/builtin/prompts/reflect/core.md +1 -1
  11. package/python/harness/builtin/prompts/shared/learning-goal.md +5 -29
  12. package/python/harness/builtin/prompts/shared/learning.md +8 -10
  13. package/python/harness/builtin/prompts/shared/web.md +11 -0
  14. package/python/harness/builtin/skills/agents/__init__.py +24 -22
  15. package/python/harness/builtin/skills/entries/__init__.py +43 -28
  16. package/python/harness/builtin/skills/entries/skill.toml +2 -3
  17. package/python/harness/builtin/skills/environments/__init__.py +2 -9
  18. package/python/harness/builtin/skills/mcp/__init__.py +2 -3
  19. package/python/harness/builtin/skills/memory/__init__.py +2 -5
  20. package/python/harness/builtin/skills/web/__init__.py +92 -0
  21. package/python/harness/builtin/skills/web/skill.toml +9 -0
  22. package/python/harness/cli.py +84 -16
  23. package/python/harness/config/agent.py +33 -9
  24. package/python/harness/core/episodes.py +5 -4
  25. package/python/harness/core/lanes.py +3 -0
  26. package/python/harness/core/memory.py +5 -1
  27. package/python/harness/core/remote.py +4 -3
  28. package/python/harness/core/worker.py +58 -3
  29. package/python/harness/extensions.py +23 -2
  30. package/python/harness/host/__init__.py +43 -1
  31. package/python/harness/host/client.py +36 -6
  32. package/python/harness/host/http.py +19 -0
  33. package/python/harness/host/server.py +17 -3
  34. package/python/harness/host/sessions.py +3 -2
  35. package/python/harness/host/tasks.py +107 -26
  36. package/python/harness/host/workspaces.py +69 -7
  37. package/python/harness/model_list.py +1 -1
  38. package/python/harness/packages/manager.py +5 -1
  39. package/python/harness/providers.py +14 -5
  40. package/python/harness/runtime/decision/loop.py +100 -121
  41. package/python/harness/runtime/decision/messages.py +156 -48
  42. package/python/harness/runtime/decision/model.py +79 -21
  43. package/python/harness/runtime/decision/prompts.py +15 -11
  44. package/python/harness/runtime/decision/protocol.py +12 -6
  45. package/python/harness/runtime/decision/thoughts.py +10 -16
  46. package/python/harness/runtime/events.py +28 -13
  47. package/python/harness/runtime/execution/attached.py +1 -1
  48. package/python/harness/runtime/execution/control.py +17 -35
  49. package/python/harness/runtime/execution/entries.py +204 -124
  50. package/python/harness/runtime/execution/kernel.py +471 -281
  51. package/python/harness/runtime/execution/managed.py +231 -0
  52. package/python/harness/runtime/execution/noenv.py +2 -2
  53. package/python/harness/runtime/execution/results.py +2 -0
  54. package/python/harness/runtime/execution/session.py +276 -115
  55. package/python/harness/runtime/learning/active.py +36 -48
  56. package/python/harness/runtime/learning/fixtures.py +2 -2
  57. package/python/harness/runtime/learning/learner.py +75 -2
  58. package/python/harness/runtime/learning/library.py +77 -10
  59. package/python/harness/runtime/learning/views.py +12 -1
  60. package/python/harness/runtime/reactive/__init__.py +1 -1
  61. package/python/harness/runtime/reactive/rules.py +29 -394
  62. package/python/harness/runtime/reactive/tree.py +611 -0
  63. package/python/harness/runtime/safety/approval.py +8 -3
  64. package/python/harness/runtime/safety/permissions.py +242 -0
  65. package/python/harness/runtime/status.py +7 -2
  66. package/python/harness/runtime/task/delegation.py +97 -58
  67. package/python/harness/runtime/task/environments.py +3 -4
  68. package/python/harness/runtime/task/inbound.py +64 -0
  69. package/python/harness/runtime/task/run.py +1092 -726
  70. package/python/harness/runtime/tick.py +257 -91
  71. package/python/harness/sdk/__init__.py +7 -4
  72. package/python/harness/settings.py +27 -3
  73. package/python/harness/terminal.py +10 -6
  74. package/python/harness/transcript.py +18 -6
  75. package/python/harness/tui/answer.py +51 -0
  76. package/python/harness/tui/chat.py +75 -26
  77. package/python/harness/tui/views.py +1 -0
  78. package/python/harness/builtin/prompts/shared/entries.md +0 -86
@@ -1,13 +1,27 @@
1
- """One bounded natural-language task, including model decisions and Automatic Control.
2
-
3
- Programs, reactive rules and learned policies execute independently of model transport.
4
- The runtime calls the model at decision points, permits steering/clarification during
5
- execution, and settles speculative decisions against their input revisions. Finish and
6
- budget exhaustion end the run. Persistent identity and multi-task conversations are not
7
- part of this lifecycle. Benchmark and interactive task callers use this same engine.
1
+ """One conversation with one model: turns in it, model decisions and Automatic Control.
2
+
3
+ A conversation starts with one model. Every program the model writes runs as a unit of its
4
+ own in the kernel (runtime/execution/kernel.py): the model's call comes back at once, with
5
+ the program's result when it was over within a short grace, else with its handle, and the
6
+ result reaches the model when the unit ends. A message, an unhandled event and a unit's
7
+ end are events to the model; a reply made before one of them is set aside.
8
+
9
+ A turn is the model's work from what started it (the instruction, a message, a unit's end,
10
+ an event) to its answer: a reply that is text, with no program and no wait (<finish> and a
11
+ program's finish() are that same answer). A limit (model calls and wall time, both of the
12
+ turn; cost, of the conversation), an error, an interrupt and the environment's end end a
13
+ turn too. Every turn ends with a result record (decisions.TURN_ENDS); then the conversation
14
+ is IDLE and keeps its messages, the kernel's namespace, rules, facts and the environment.
15
+ The units still running after an answer go on, and what reaches the model starts the next
16
+ turn. Only the user or the host ends the conversation (close()), or a kernel that died; a
17
+ caller without a user (interactive False: a benchmark's run) ends it after the first turn.
18
+
19
+ A conversation with a user writes what continues it in another process as it goes
20
+ (conversation.json beside its trace: its messages, what the user said, its facts): a
21
+ TaskRun given that (`restore`: saved()) goes on in the same directory and trace with a new
22
+ kernel, and tells the model what died with the old process.
8
23
  """
9
24
 
10
- import ast
11
25
  import base64
12
26
  import hashlib
13
27
  import json
@@ -19,34 +33,52 @@ from types import SimpleNamespace
19
33
  import uuid
20
34
 
21
35
  from harness.runtime.decision import loop as decisions
22
- from harness.runtime.decision.messages import _inbox_parts
36
+ from harness.runtime.decision.messages import _inbox_parts, content_parts
23
37
  from harness.runtime.execution.results import _taken_over, _cell_usage, _wrap_up
24
38
  from harness.runtime.safety.approval import CONFIRMATION_POLICIES
25
- from harness.runtime.status import Closed
39
+ from harness.runtime.status import Closed, Interrupted
26
40
 
27
41
  from harness.bench import bench_names, bench_spec
28
42
  from harness.core.remote import RemoteEnvironment
29
43
  from harness.core.media import write_episode_videos
30
- from harness.core import sha256
44
+ from harness.core import atomic_write, sha256
31
45
  from harness.core.http import Preempted
32
46
  from harness.core.trace import Trace, context_summary, provenance, write_json
33
47
  from harness.embodiments import KINDS as EMBODIMENT_KINDS, embodiment_kind
34
48
  from harness.runtime.decision.thoughts import Thought, ThoughtCancelled, ThoughtQueue
35
49
  from harness.runtime.execution.kernel import DEFAULT_BOOTSTRAP, Kernel, KernelDied, kernel_python
36
- from harness.runtime.learning.library import SECTIONS, eligible, executable, library_for
50
+ from harness.runtime.learning.library import SECTIONS, admitted_by, eligible, executable, library_for
37
51
  from harness.runtime.task import context as memory_view
38
52
  from harness.runtime.task.context import WorkingMemory
39
53
  from harness.runtime.learning.usage import fold_uses
40
54
  from harness.runtime.decision.messages import (EMBODIMENT_TEXT, attachment_parts, cell_feedback, image_parts,
41
- prune_images, prune_views, state_text, text_only, view_parts, window)
55
+ prune_images, prune_views, state_text, text_only, unit_feedback,
56
+ unit_text, view_parts)
42
57
  from harness.runtime.events import EVENTS
43
58
  from harness.runtime.status import ERROR, FINISHED, IDLE, PAUSED, RUNNING, STATUSES, WAITING_FOR_CONFIRMATION
44
59
  from harness.runtime.learning.active import sandbox_of
45
- from harness.runtime.decision.protocol import DIRECT_KINDS, KINDS, ProtocolError, announces, looks_like_action, parse_reply
60
+ from harness.runtime.reactive.tree import told as tree_told
61
+ from harness.runtime.decision.messages import clear_results, latest, recent_start
62
+ from harness.runtime.decision.protocol import (DIRECT_KINDS, KINDS, ProtocolError, announces, looks_like_action,
63
+ parse_reply, wait_seconds)
46
64
 
47
65
  import harness.runtime.learning.views as library_view
48
66
 
49
67
 
68
+ # What continues a conversation in another process, in its directory.
69
+ CONVERSATION_FILE = "conversation.json"
70
+
71
+
72
+ def saved(directory) -> dict | None:
73
+ """What the conversation in `directory` kept to be continued (TaskRun's `restore`):
74
+ {"messages", "said", "facts", "units", "turns", "summary"}; None when it kept nothing."""
75
+ try:
76
+ kept = json.loads((Path(directory) / CONVERSATION_FILE).read_text(encoding="utf-8"))
77
+ except (OSError, ValueError):
78
+ return None
79
+ return kept if isinstance(kept, dict) and isinstance(kept.get("messages"), list) and kept["messages"] else None
80
+
81
+
50
82
  class TaskRun:
51
83
  """One episode of `config.task` with `seed`, its trace and files under `directory`.
52
84
  `chat`, `verifier` and `environment` replace the configured models and worker.
@@ -54,11 +86,13 @@ class TaskRun:
54
86
  CONFIRMATION_POLICIES; `instruction`: the user's own instruction in place of the
55
87
  task's (the evaluator's outcome still scores the task). `agents` (runtime/task/delegation.py)
56
88
  starts the sub-agents its programs ask for; `parent`: the agent that started this one.
89
+ `restore`: what an earlier process kept of this conversation (saved(directory)): it goes
90
+ on from there, in the same directory and trace.
57
91
  Managed workspaces are acquired by the host before the conversation starts."""
58
92
 
59
93
  def __init__(self, config, seed: int, directory, *, chat=None, verifier=None, environment=None,
60
94
  interactive: bool = False, confirmation: str = "never", instruction: str | None = None,
61
- agents=None, parent=None, memory=None):
95
+ agents=None, parent=None, memory=None, restore: dict | None = None):
62
96
  from copy import copy
63
97
 
64
98
  from harness.config.agent import resolve_for
@@ -77,10 +111,25 @@ class TaskRun:
77
111
  self.working_memory = memory if memory is not None else WorkingMemory()
78
112
  self._active_library = {}
79
113
  self._library_prompt = ""
114
+ from harness.runtime.safety.permissions import Programs
115
+
116
+ self.programs = Programs(self)
117
+ # The tokens of context the model's last call read and wrote (what compaction goes
118
+ # by), the summaries in a row the model did not give, and the learning report as
119
+ # the model was last told it.
120
+ self._context_tokens, self._compact_failures, self._learning_told = 0, 0, None
121
+ # When the model last replied (what clearing old results goes by).
122
+ self._replied_at = None
80
123
  self._captured_library = None
81
124
  self._activation_queue = {}
82
125
  self._activation_interrupt = 0
83
126
  self._fixture_attempts = set()
127
+ # What the host allows for admitting learned code (learner.ADMISSION): set by the host
128
+ # that starts the conversation from the user's setting learning.admission; None is
129
+ # $HARNESS_LEARNING_ADMISSION, else "fixture". Never part of the run's configuration.
130
+ self.library_admission = None
131
+ self._admission_level = None
132
+ self._trial_interrupted = set()
84
133
  self._fixture_status = "unchecked"
85
134
  self._fixture_descriptions = []
86
135
  self._fixture_feedback = {}
@@ -93,9 +142,12 @@ class TaskRun:
93
142
  config = copy(config)
94
143
  if table and table.get("learn") == "on":
95
144
  config.system_prompt = config.base_prompt
96
- self.ahead = str(config.runtime.get("think_while_acting") or "on")
97
- if self.ahead not in decisions.THINK_WHILE_ACTING:
98
- raise ValueError(f"runtime.think_while_acting must be one of {decisions.THINK_WHILE_ACTING}, got {self.ahead!r}")
145
+ grace = config.runtime.get("turn_grace_seconds", decisions.TURN_GRACE_SECONDS)
146
+ if isinstance(grace, bool) or not isinstance(grace, (int, float)) or grace < 0:
147
+ raise ValueError(f"runtime.turn_grace_seconds must be a number of seconds >= 0, got {grace!r}")
148
+ longest = config.runtime.get("longest_wait_seconds", decisions.LONGEST_WAIT_SECONDS)
149
+ if isinstance(longest, bool) or not isinstance(longest, (int, float)) or longest <= 0:
150
+ raise ValueError(f"runtime.longest_wait_seconds must be a number of seconds > 0, got {longest!r}")
99
151
  thinking = self.thinking_level = str(config.runtime.get("thinking") or "model")
100
152
  if thinking not in decisions.THINKING:
101
153
  raise ValueError(f"runtime.thinking must be one of {tuple(decisions.THINKING)}, got {thinking!r}")
@@ -107,12 +159,11 @@ class TaskRun:
107
159
  config.base_prompt += decisions.THINK_BRIEFLY
108
160
  if interactive:
109
161
  config = copy(config)
110
- message_prompt = decisions.MESSAGE_PROMPT
111
162
  if parent is None:
112
- role_prompt = decisions.INTERACTIVE_PROMPT + message_prompt
163
+ role_prompt = decisions.INTERACTIVE_PROMPT
113
164
  else:
114
165
  deepest = agents is None or self.depth >= agents.max_depth
115
- role_prompt = SUB_AGENT_PROMPT + message_prompt + (
166
+ role_prompt = SUB_AGENT_PROMPT + (
116
167
  NO_DEEPER if deepest and "agents" in (config.runtime.get("skills") or []) else "")
117
168
  config.system_prompt += role_prompt
118
169
  config.base_prompt += role_prompt
@@ -124,7 +175,13 @@ class TaskRun:
124
175
  self.confirmation = confirmation
125
176
  self.instruction = instruction
126
177
  self.chat, self.verifier, self.env = chat, verifier, environment
127
- self.trace = Trace(directory, EVENTS)
178
+ self.trace = Trace(directory, EVENTS, append=restore is not None)
179
+ self._restore = restore
180
+ if restore is not None:
181
+ # Its last summary said it had ended: it is open again, so nothing takes it for ended.
182
+ write_json(directory / "summary.json", {**(restore.get("summary") or {}), "open": True})
183
+ # The facts of its programs as the kernel reports their changes: kept to be restored.
184
+ self._facts = dict((restore or {}).get("facts") or {})
128
185
  self.status = IDLE
129
186
  self.messages = []
130
187
  self.kernel = None
@@ -134,30 +191,39 @@ class TaskRun:
134
191
  self._inbox = []
135
192
  self._pause = False
136
193
  self._closing = False
194
+ # The user asked to end the turn that runs (cancel()).
195
+ self._interrupt = False
137
196
  self._decision = None
197
+ # Of the turn that runs: when it began, the time it waited for the user, the
198
+ # model's calls before it, and how many turns began.
199
+ self._turn_began = None
138
200
  self._waited = 0.0
139
- self._cell_running = False
201
+ self._calls_before = 0
202
+ self._turns = 0
140
203
  self._program_span = None
141
- # Thinking while acting: what the next user message starts with (a used reply's program
142
- # result), the messages logged and replaced, the calls set aside before they were back.
143
- self._carry = []
144
- # What a call made for a message while a program runs needs (_think_on_message).
145
- self._kinds, self._turns = KINDS, None
146
- # Programs run beside the running one, by id (_run_beside).
147
- self._sides = {}
204
+ # The model's units that run ({unit id: {"name", "kind", "cell", "started"}}), its
205
+ # programs whose result has not come ({the kernel's cell: {"number", "code", "action",
206
+ # "span", "unit"}}),
207
+ # the results of the units that ended and the model has not been told, and how
208
+ # long a program may hold the model's turn before the turn comes back with its handle.
209
+ self._units = {}
210
+ self._programs = {}
211
+ self._unit_news = []
212
+ self._grace = float(grace)
213
+ # The longest a <wait> of the model lasts.
214
+ self._longest_wait = float(longest)
215
+ # A unit ended the turn: "episode_ended", or ("finished", the summary its finish() gave).
216
+ self._run_end = None
217
+ # The model's call count when the learned policy first ran (runtime.post_policy_calls).
218
+ self._post_from = None
148
219
  self._request = None
149
- self._revision = self._ahead_generation = 0
220
+ self._revision = 0
150
221
  self._pending_events = {}
222
+ self._inbound = None # the listener its worker tells through (runtime/task/inbound.py)
151
223
  self._seen_events = set()
152
224
  self._reported_events = set()
153
- self._ahead_due = None
154
- self._program_completing = False
155
- self._interruptions = 0
156
- self._thoughts = ThoughtQueue(self._condition, advance=self._decision_tick,
157
- on_exit=self._came_back, trace=self.trace)
225
+ self._thoughts = ThoughtQueue(self._condition, on_exit=self._came_back, trace=self.trace)
158
226
  self._cancelled_origins = set()
159
- # Set when the episode deadline expires while a program runs.
160
- self._wall_up = False
161
227
  self._logged = 0
162
228
  self._replaces = 0
163
229
  # What the user said, in order: the instruction, then every message.
@@ -187,8 +253,6 @@ class TaskRun:
187
253
  # watch), kept for `_view_keep` replies or while pinned (the model says).
188
254
  self._standing = "cameras" in config.runtime
189
255
  self._view_keep, self._view_pins = 1, set()
190
- # The views a watch(every=seconds) sent while a program runs, for the model's next look.
191
- self._watched = None
192
256
  # A main model that reads no images gets none (its role's `images` is false).
193
257
  self._images = config.main.get("images") is not False
194
258
  # {session, agent} in a sessions registry (host/sessions.py).
@@ -212,7 +276,6 @@ class TaskRun:
212
276
  self._charged = (0, 0.0)
213
277
  self._iterations = 0
214
278
  self.result = None
215
- self.question = None
216
279
  self.waiting_since = None
217
280
  self.trace.context = self._who
218
281
 
@@ -241,12 +304,11 @@ class TaskRun:
241
304
  self._inbox.append((sender, text, parts))
242
305
  said = " ".join([text] + [f"@{path}" for path in files]).strip()
243
306
  self._said.append(said if sender == "user" else f"(from the agent that started you) {said}")
244
- self.question = None
245
307
  self.trace.emit("user_message", text=text, **({"files": files} if files else {}),
246
308
  **({"sender": sender} if sender != "user" else {}))
247
309
  self._revision += 1
248
310
  if self._managed_dispatcher is not None:
249
- self._managed_dispatcher.stop()
311
+ # (The message stops no program; an approval asked before it is void.)
250
312
  self._cancel_managed_worker_approvals("a new user message arrived")
251
313
  if self._physical is not None and self._physical["id"] in self._worker_approvals:
252
314
  request = self._physical
@@ -257,8 +319,8 @@ class TaskRun:
257
319
  self._condition.notify_all()
258
320
 
259
321
  def pause(self) -> None:
260
- """Pause: a running program stops at its next execution step; the model is not
261
- called again until resume() or a message."""
322
+ """Pause: every unit of the model stops at its next execution step or wait, and
323
+ nothing of it acts; the model is not called again until resume() or a message."""
262
324
  with self._condition:
263
325
  if self.status in (FINISHED, ERROR):
264
326
  return
@@ -268,8 +330,7 @@ class TaskRun:
268
330
  if self._managed_dispatcher is not None:
269
331
  self._managed_dispatcher.stop()
270
332
  self._cancel_managed_worker_approvals("paused")
271
- kernel = self.kernel if self._cell_running else None
272
- self.invalidate_lookahead()
333
+ kernel = self.kernel
273
334
  self._condition.notify_all()
274
335
  if self.agents is not None:
275
336
  self.agents.wake()
@@ -281,13 +342,16 @@ class TaskRun:
281
342
  """Go on after a pause."""
282
343
  with self._condition:
283
344
  self._pause = False
345
+ kernel = self.kernel
284
346
  self._condition.notify_all()
347
+ if kernel is not None:
348
+ kernel.resume()
285
349
 
286
350
  def set_model(self, model: str | None = None, reasoning_effort: str | None = None) -> dict:
287
351
  """Change the main model (another id of its provider) or its reasoning effort (EFFORTS,
288
352
  "" for the provider's default; runtime.thinking still bounds it) from its next call on:
289
353
  a model_changed record. Returns the model and effort now."""
290
- from harness.runtime.decision.model import ChatModel
354
+ from harness.runtime.decision.model import BUILT_IN, ChatModel
291
355
 
292
356
  main = dict(self.config.main)
293
357
  if model:
@@ -309,11 +373,15 @@ class TaskRun:
309
373
  if chat is not None:
310
374
  if not isinstance(chat, ChatModel):
311
375
  raise ValueError("this conversation's model is not one a model id names")
312
- # Its connection as it is (a provider's transport), with the new role.
313
- chat = ChatModel(main, transport=chat.transport)
376
+ # Its connection as it is (a provider's transport), with the new role; a
377
+ # package's backend (an ACP agent) is made for its role.
378
+ packaged = chat.packaged or main.get("backend", "openrouter") not in BUILT_IN
379
+ chat = ChatModel(main, transport=None if packaged else chat.transport)
314
380
  self.config.main = main
315
381
  if chat is not None:
316
- self.chat = chat
382
+ replaced, self.chat = self.chat, chat
383
+ if replaced.packaged:
384
+ replaced.close()
317
385
  changed = {"model": main["model"], "reasoning_effort": main.get("reasoning_effort")}
318
386
  self.trace.emit("model_changed", **changed)
319
387
  return changed
@@ -372,8 +440,22 @@ class TaskRun:
372
440
  return
373
441
  self._answer_physical(request, accept, remember or "once", reason)
374
442
 
443
+ # What a frontend may ask of it besides messages (the host's /api/conversations/cancel).
444
+ capabilities = {"cancel_turn": True}
445
+
446
+ def cancel(self) -> None:
447
+ """End the turn that runs: the call in flight is set aside, the model's units are
448
+ cancelled, and the conversation waits for the next message (nothing between turns)."""
449
+ with self._condition:
450
+ if self.status in (FINISHED, ERROR, IDLE) or self._interrupt:
451
+ return
452
+ self._interrupt = True
453
+ self._revision += 1
454
+ self._preempt("interrupted")
455
+ self._condition.notify_all()
456
+
375
457
  def close(self) -> None:
376
- """End the episode: at once while it waits, else at the next step boundary."""
458
+ """End the conversation: at once while it waits, else at the next step boundary."""
377
459
  with self._condition:
378
460
  self._closing = True
379
461
  if self._request is not None:
@@ -381,13 +463,14 @@ class TaskRun:
381
463
  if self._managed_dispatcher is not None:
382
464
  self._managed_dispatcher.stop()
383
465
  self._cancel_managed_worker_approvals("closed")
384
- kernel = self.kernel if self._cell_running else None
466
+ kernel = self.kernel
385
467
  self._condition.notify_all()
386
468
  if self.agents is not None:
387
469
  self.agents.wake()
388
470
  self._drop_physical("closed")
389
471
  if kernel is not None:
390
- kernel.pause()
472
+ # The run ends: so do its units.
473
+ kernel.cancel()
391
474
 
392
475
  # Physical input the policy asks the user for (runtime/safety/input.py).
393
476
 
@@ -559,15 +642,15 @@ class TaskRun:
559
642
  return (getattr(self, "profile", None) or {}).get("steps_shown", True) is not False
560
643
 
561
644
  def _kernel_span(self, record: dict) -> None:
562
- """A span from the kernel (a skill's execution, a service call), under the
563
- program that runs."""
645
+ """A record from the kernel: a span (a skill's execution, a service call) under the
646
+ program whose unit made it, a note, a unit's start or end."""
564
647
  record = dict(record)
565
648
  kind = record.pop("op")
566
649
  if kind == "worker_request":
567
650
  self._queue_host_request(record, worker=True)
568
651
  return
569
- if kind == "side_result":
570
- self._side_ended(record)
652
+ if kind == "unit_end":
653
+ self._unit_arrived(record)
571
654
  return
572
655
  if kind == "note":
573
656
  note = record.pop("kind", None)
@@ -583,14 +666,15 @@ class TaskRun:
583
666
  elif note == "environment":
584
667
  threading.Thread(target=self._environment_request, args=(record,), name="environment",
585
668
  daemon=True).start()
669
+ elif note == "web":
670
+ threading.Thread(target=self._web_request, args=(record,), name="web", daemon=True).start()
671
+ elif note == "program":
672
+ # A step of a program on the machine (runtime/safety/permissions.py): answered
673
+ # through the kernel, on a thread of its own (the user may be asked).
674
+ threading.Thread(target=lambda: self.kernel is not None and self.kernel.answer(
675
+ record.get("id"), self.programs.handle(record)), name="program-step", daemon=True).start()
586
676
  elif note == "views":
587
677
  self._views_changed(record)
588
- elif note == "view":
589
- with self._condition:
590
- if self._cell_running and not self._standing and isinstance(record.get("images"), dict):
591
- self._watched = [{"camera": name, "image_url": url}
592
- for name, url in record["images"].items()]
593
- self._thoughts.wake()
594
678
  elif note == "mcp_request":
595
679
  self._queue_host_request(record)
596
680
  elif note == "mcp_cancel":
@@ -609,31 +693,64 @@ class TaskRun:
609
693
  elif note == "library_use":
610
694
  record.pop("time", None)
611
695
  self.trace.emit("library_use", **record)
612
- elif note == "cell_finishing":
613
- with self._condition:
614
- if self._cell_running:
615
- self._program_completing = True
616
- self._condition.notify_all()
617
696
  elif note == "escalation":
618
697
  self._escalation(record)
619
698
  elif note == "memory":
620
699
  self._memory_request(record)
700
+ elif note == "reflexes":
701
+ # How the tree of reflexes stands (runtime/reactive/tree.py tell): told with every turn.
702
+ with self._condition:
703
+ self._reflex_nodes = dict(record.get("nodes") or {})
704
+ elif note == "preempted":
705
+ self._taken(record)
706
+ elif note == "held_back":
707
+ self._held_back(record)
708
+ elif note == "priority":
709
+ # A program said with what priority it holds the environment (at_priority).
710
+ with self._condition:
711
+ if record.get("unit") in self._units:
712
+ self._units[record["unit"]]["priority"] = record.get("priority")
713
+ elif note == "fact":
714
+ record.pop("time", None)
715
+ data = record.pop("data", None)
716
+ with self._condition:
717
+ if record.get("dropped"):
718
+ self._facts.pop(record.get("name"), None)
719
+ elif data is not None:
720
+ self._facts[record["name"]] = json.loads(data)
721
+ self.trace.emit("fact", **record)
621
722
  elif note in EVENTS:
723
+ if note == "program_output":
724
+ with self._condition:
725
+ unit = self._units.get(record.get("unit"))
726
+ if unit is not None and "printed" in unit:
727
+ unit["printed"] = time.time()
622
728
  record.pop("time", None)
623
729
  self.trace.emit(note, **record)
624
730
  if note == "guard" and self.summary is not None:
625
731
  self.summary["guard_triggers"] += 1
626
- if note == "memory":
627
- self.invalidate_lookahead()
628
732
  return
629
733
  if kind in ("entry_start", "entry_end"):
630
734
  with self._condition:
631
735
  if kind == "entry_start":
632
736
  self._mcp_entries.add(record["id"])
737
+ if record.get("kind") in ("program", "agent"):
738
+ self._units[record["id"]] = {"name": record.get("name"), "kind": record["kind"],
739
+ "cell": record.get("cell"), "started": time.time()}
740
+ if record["kind"] == "program" and self._managed_dispatcher is None:
741
+ # What it prints reaches the host while it runs (program_output).
742
+ self._units[record["id"]]["printed"] = None
743
+ program = self._programs.get(record.get("cell"))
744
+ if program is not None:
745
+ program["unit"] = record["id"]
633
746
  else:
634
747
  self._mcp_entries.discard(record["id"])
748
+ self._units.pop(record["id"], None)
749
+ self._condition.notify_all()
635
750
  if kind == "span_start" and record.get("parent") is None:
636
- record["parent"] = self._program_span
751
+ # Under the program whose unit made it, else under the host's own program span.
752
+ cell = (self._units.get(record.get("entry")) or {}).get("cell")
753
+ record["parent"] = (self._programs.get(cell) or {}).get("span") or self._program_span
637
754
  self.trace.emit(kind, **record)
638
755
 
639
756
  def _queue_host_request(self, request, *, worker=False):
@@ -643,7 +760,7 @@ class TaskRun:
643
760
  return
644
761
  with self._condition:
645
762
  kernel = self.kernel
646
- if kernel is None or (not worker and not (owner in self._mcp_entries if owner else self._cell_running)) or self._closing or self._pause:
763
+ if kernel is None or (not worker and owner not in self._mcp_entries) or self._closing or self._pause:
647
764
  if worker and kernel is not None:
648
765
  kernel.answer(identifier, {"worker": True, "error": "The calling program has stopped"})
649
766
  return
@@ -658,12 +775,10 @@ class TaskRun:
658
775
  if not slot.acquire(blocking=False):
659
776
  kernel.answer(identifier, {"worker": worker, "error": "Another request is still running"})
660
777
  return
661
- revision, cell = self._revision, self.summary["cells"]
662
778
 
663
779
  def still_current():
664
- return (self.kernel is kernel and (worker or (owner in self._mcp_entries if owner else
665
- self._cell_running and self.summary["cells"] == cell))
666
- and (owner or self._revision == revision) and not self._pause and not self._closing
780
+ return (self.kernel is kernel and (worker or owner in self._mcp_entries)
781
+ and not self._pause and not self._closing
667
782
  and (worker or identifier not in self._mcp_cancelled))
668
783
 
669
784
  threading.Thread(target=self._host_request, args=(kernel, request, still_current, slot),
@@ -812,11 +927,11 @@ class TaskRun:
812
927
  from harness.runtime.safety import approval
813
928
 
814
929
  with self._condition:
815
- kernel, revision, cell = self.kernel, self._revision, self.summary["cells"]
930
+ kernel, revision = self.kernel, self._revision
816
931
 
817
932
  def still_current():
818
- return (self.kernel is kernel and self._cell_running and self.summary["cells"] == cell
819
- and self._revision == revision and not self._pause and not self._closing)
933
+ return (self.kernel is kernel and self._revision == revision and not self._pause
934
+ and not self._closing)
820
935
 
821
936
  request = approval.request(kind, target, [kind], why=str(why)[:500], **detail)
822
937
  return self._await_host_decision(request, still_current)
@@ -833,8 +948,6 @@ class TaskRun:
833
948
  self._view_pins.clear()
834
949
  else:
835
950
  self._view_pins.discard(str(record["pin"]))
836
- elif action == "unwatch":
837
- self._watched = None
838
951
  self.trace.emit("views", **{key: value for key, value in record.items() if key not in ("time", "id")})
839
952
 
840
953
  def _described(self, looks) -> list[dict]:
@@ -864,6 +977,42 @@ class TaskRun:
864
977
  parts.append({"type": "input_text", "text": f"{name}: not described ({type(error).__name__}: {error})"})
865
978
  return parts
866
979
 
980
+ def _web_request(self, request):
981
+ """A program searches the web (skills/web): with the search this conversation is
982
+ configured with (runtime.search: a model on OpenRouter, whose web plugin searches;
983
+ the pages it cites are the results), answered through the kernel."""
984
+ from harness.runtime.decision.model import ChatModel
985
+
986
+ config, query = self.config.runtime.get("search"), str(request.get("query") or "")
987
+ count = max(1, min(10, int(request.get("results") or 5)))
988
+ try:
989
+ if not isinstance(config, dict) or not config.get("model"):
990
+ raise ValueError("this conversation has no search (runtime.search names none); fetch(url) reads a "
991
+ "page whose address you know")
992
+ plugin = {"id": "web", "max_results": count, **({"engine": config["engine"]} if config.get("engine") else {})}
993
+ chat = ChatModel({"backend": "openrouter", "max_output_tokens": 512,
994
+ **{key: value for key, value in config.items() if key != "engine"},
995
+ "extra_body": {"plugins": [plugin]}})
996
+ reply = self._ask_role("search", chat, SEARCHER, [{"role": "user", "content": [
997
+ {"type": "input_text", "text": query}]}])
998
+ found, seen = [], set()
999
+ for item in reply.annotations:
1000
+ cited = item.get("url_citation") if isinstance(item, dict) and item.get("type") == "url_citation" else None
1001
+ if cited and cited.get("url") and cited["url"] not in seen:
1002
+ seen.add(cited["url"])
1003
+ found.append({"title": str(cited.get("title") or ""), "url": str(cited["url"]),
1004
+ "text": str(cited.get("content") or "")[:2000]})
1005
+ if not found and reply.text.strip():
1006
+ # (A search that cites nothing: what it said is the one result.)
1007
+ found = [{"title": "", "url": "", "text": reply.text.strip()[:4000]}]
1008
+ answer = {"value": found[:count]}
1009
+ self.trace.emit("web_search", query=query, results=len(found[:count]))
1010
+ except Exception as error:
1011
+ answer = {"error": str(error) if isinstance(error, ValueError) else f"{type(error).__name__}: {error}"}
1012
+ self.trace.emit("web_search", query=query, error=answer["error"][:300])
1013
+ if self.kernel is not None:
1014
+ self.kernel.answer(request.get("id"), answer)
1015
+
867
1016
  def _environment_request(self, request):
868
1017
  """A program asks for another environment, to give one back, or for sub-agents
869
1018
  (skills/environments): answered through the kernel."""
@@ -880,7 +1029,6 @@ class TaskRun:
880
1029
  with self._condition:
881
1030
  self.config = copy(self.config)
882
1031
  self.config.system_prompt += text
883
- self.invalidate_lookahead()
884
1032
 
885
1033
  def _prompt_removed(self, text):
886
1034
  from copy import copy
@@ -888,7 +1036,6 @@ class TaskRun:
888
1036
  with self._condition:
889
1037
  self.config = copy(self.config)
890
1038
  self.config.system_prompt = self.config.system_prompt.replace(text, "", 1)
891
- self.invalidate_lookahead()
892
1039
 
893
1040
  def _skill_added(self, name) -> bool:
894
1041
  """A skill switched on while the conversation runs: part of its skills and prompt now."""
@@ -924,6 +1071,29 @@ class TaskRun:
924
1071
  if result["status"] != "ok":
925
1072
  self.trace.emit("cleanup_error", message=f"skills {due}: {result.get('error')}")
926
1073
 
1074
+ def tell(self, content, source: str | None = None) -> None:
1075
+ """The environment tells the model something that happened by itself: a message, in
1076
+ the form conversations give content (a string, or a list of text and image parts:
1077
+ decision/messages.py content_parts). It reaches the model at once as a message does:
1078
+ a wait ends, a call in flight is set aside and asked again; the model reads it as
1079
+ "Message from the environment". Called by whoever knows of it: the run's worker
1080
+ through its listener (runtime/task/inbound.py), or the code that owns this run."""
1081
+ parts = content_parts(content)
1082
+ if not parts:
1083
+ return
1084
+ sender = "environment" + (f" ({source})" if source and source != "environment" else "")
1085
+ text = "\n".join(part["text"] for part in parts if part["type"] == "input_text")
1086
+ with self._condition:
1087
+ if self.status in (FINISHED, ERROR):
1088
+ raise RuntimeError("The episode has ended")
1089
+ self._inbox.append((sender, text, parts))
1090
+ self.trace.emit("user_message", text=text, sender=sender,
1091
+ **({"images": sum(part["type"] == "input_image" for part in parts)}
1092
+ if any(part["type"] == "input_image" for part in parts) else {}))
1093
+ self._revision += 1
1094
+ self._preempt("environment message")
1095
+ self._condition.notify_all()
1096
+
927
1097
  def _escalation(self, record):
928
1098
  with self._condition:
929
1099
  identifier = record["id"]
@@ -937,13 +1107,40 @@ class TaskRun:
937
1107
  self._preempt("unhandled escalation")
938
1108
  self._condition.notify_all()
939
1109
 
940
- def heard(self, text: str, *, escalation=False, agent_report=False) -> None:
941
- """Child reports are execution results; only explicit escalation asks the model."""
942
- if escalation:
943
- self._escalation({"id": uuid.uuid4().hex, "event": {"kind": "ESCALATION", "detail": str(text)}})
1110
+ def _held_back(self, record: dict) -> None:
1111
+ """A reflex's condition came to hold while a program of higher priority had the
1112
+ environment (at_priority; the kernel's `held_back` note, once per reflex and unit):
1113
+ the reflex did not act and is judged again when that unit has ended. An event to the
1114
+ model, told at once as a preemption is, so that it may let the reflex act by
1115
+ cancelling the unit."""
1116
+ record.pop("time", None)
1117
+ self.trace.emit("held_back", **record)
1118
+ self._escalation({"id": uuid.uuid4().hex, "entry": record.get("by"), "event": {
1119
+ "kind": "HELD_BACK", "source": record.get("rule"),
1120
+ "detail": {key: record[key] for key in ("rule", "priority", "by", "by_name") if record.get(key) is not None}}})
1121
+
1122
+ def _taken(self, record: dict) -> None:
1123
+ """A unit of higher priority (a rule's program) took the environment from an
1124
+ execution that ran (the tick's `preempted` note): an event to the model, told at
1125
+ once as every event that reaches it is: a wait ends, a call in flight is set
1126
+ aside and asked again. It says which unit and rule took the environment, from
1127
+ which unit and execution, whether that execution is started again, and the state."""
1128
+ record.pop("time", None)
1129
+ state = record.pop("state", None) or {}
1130
+ self.trace.emit("preempted", **record)
1131
+ if record.get("of_reflex"):
1132
+ # The call of a reflex's program: its node begins again by itself when its turn comes.
1133
+ return
1134
+ told = {key: record[key] for key in ("rule", "by", "by_name", "unit", "name", "execution", "restart")
1135
+ if record.get(key) is not None}
1136
+ self._escalation({"id": uuid.uuid4().hex, "entry": record.get("unit"), "event": {
1137
+ "kind": "PREEMPTED", "source": record.get("rule") or record.get("by_name") or record.get("by"),
1138
+ "detail": {**told, **({"state": state_text(state, self.config.embodiment, self._steps_shown())}
1139
+ if state else {})}}})
944
1140
 
945
1141
  def _preempt(self, reason):
946
- self._ahead_due = None
1142
+ """Something reached the model while its call is in flight: the call is set aside
1143
+ (its reply would not know), and so are the role calls that reason makes stale."""
947
1144
  thought = self._request
948
1145
  if thought is not None and thought.outcome is None:
949
1146
  self._settle(thought, "discarded", reason)
@@ -953,13 +1150,9 @@ class TaskRun:
953
1150
  elif reason == "unhandled escalation":
954
1151
  self._thoughts.cancel_where(lambda item: item.role == "observer", reason)
955
1152
 
956
- def invalidate_lookahead(self) -> None:
957
- with self._condition:
958
- self._ahead_due = None
959
- self._ahead_generation += 1
960
- if self._request is not None and self._request.trigger == "ahead":
961
- self._settle(self._request, "discarded", "inputs changed")
962
- self._condition.notify_all()
1153
+ def _busy(self) -> bool:
1154
+ """Whether a program of the model runs (code it spawned to go on beside does not count)."""
1155
+ return bool(self._programs)
963
1156
 
964
1157
  def _learner(self):
965
1158
  """The run's library ([runtime.library]) and what its settings allow, opened once per
@@ -981,6 +1174,28 @@ class TaskRun:
981
1174
  chosen = settings(table)
982
1175
  return chosen if chosen["learn"] == "on" else None
983
1176
 
1177
+ def _admission(self) -> str:
1178
+ """How learned code is admitted in this conversation (learner.admission): what the
1179
+ host allows (the conversation that started this one decides for its sub-agents),
1180
+ for this worker's profile. Read once, when the profile is known."""
1181
+ if self._admission_level is None:
1182
+ import os
1183
+
1184
+ from harness.runtime.learning.learner import ADMISSION_VARIABLE, admission
1185
+
1186
+ root = self
1187
+ while root.parent is not None:
1188
+ root = root.parent
1189
+ allowed = os.environ.get(ADMISSION_VARIABLE) or root.library_admission
1190
+ profile = getattr(self, "profile", None)
1191
+ if profile is None:
1192
+ return admission(None, allowed) # before the worker answered: asked again later
1193
+ self._admission_level = admission(profile, allowed)
1194
+ if self.config.runtime.get("library"):
1195
+ self.trace.emit("library", action="admission", level=self._admission_level,
1196
+ allowed=allowed or "fixture", physical=self.profile.get("physical"))
1197
+ return self._admission_level
1198
+
984
1199
  def _learning_rows(self, snapshot=None):
985
1200
  rows = self._learner().entries(snapshot=snapshot)
986
1201
  with self._condition:
@@ -997,7 +1212,8 @@ class TaskRun:
997
1212
  continue
998
1213
  info = self._active_row(item, row)
999
1214
  rows.append({**info, "section": key.split("/", 1)[0], "file": key, "loaded": True,
1000
- "verified": bool(row.get("proof")), "uses": info["seen"], "success_rate": info["rate"]})
1215
+ "verified": bool(row.get("proof")), "admitted_by": admitted_by(row),
1216
+ "uses": info["seen"], "success_rate": info["rate"]})
1001
1217
  return rows
1002
1218
 
1003
1219
  def _learning_progress(self):
@@ -1005,52 +1221,66 @@ class TaskRun:
1005
1221
 
1006
1222
  return {**progress(self._learning_rows(), self.seconds_left(), self.config.max_wall_seconds or 0.0,
1007
1223
  self._situations()), "fixture_status": self._fixture_status,
1008
- "fixture_feedback": dict(self._fixture_feedback)}
1224
+ "fixture_feedback": dict(self._fixture_feedback), "admission": self._admission()}
1009
1225
 
1010
1226
  def _learning_report(self) -> str:
1011
1227
  from harness.runtime.learning import active
1012
1228
 
1013
1229
  if not active.is_active(self.config.runtime):
1014
1230
  return ""
1015
- report = active.report(self._learning_rows(), self.seconds_left(), self._situations())
1231
+ level = self._admission()
1232
+ report = active.report(self._learning_rows(), self.seconds_left(), self._situations(), level)
1016
1233
  if self._fixture_descriptions:
1017
1234
  report += "\n" + "\n".join(self._fixture_descriptions)
1018
- elif self._fixture_status == "unavailable":
1235
+ if level == "off":
1236
+ report += "\nThe host admits no code here. Keep what you learn as notes and memory entries."
1237
+ elif level == "own_test":
1238
+ report += ("\nThe host runs a pending proposal's own test between programs: end the program after "
1239
+ "proposing. A proposal whose test passes is active in your next program.")
1240
+ elif not self._fixture_descriptions and self._fixture_status == "unavailable":
1019
1241
  report += "\nNo trusted fixture is configured here. Keep observations and leave executable proposals pending."
1020
1242
  if self._fixture_feedback:
1021
1243
  report += "\n" + "\n".join(f"{key}: {why}" for key, why in self._fixture_feedback.items())
1022
1244
  return "\n" + report
1023
1245
 
1024
1246
  def _trial_at_boundary(self, *, replay=False):
1025
- """Run at most one trial between programs, or recheck pinned entries at startup."""
1247
+ """Run at most one trial between programs, or recheck pinned entries at startup. What
1248
+ verifies a pending proposal is the admission level in force (_admission): a fixture
1249
+ the host has for it; where the level is own_test and it has none, its own test
1250
+ (_own_test_trial); nothing where the level is off."""
1026
1251
  from harness.runtime.learning import fixtures
1027
- from harness.runtime.learning.learner import CANDIDATES
1252
+ from harness.runtime.learning.learner import CANDIDATES, OWN_TEST
1028
1253
 
1029
1254
  learning = self._learning()
1030
1255
  if not learning or self.parent is not None or (replay and not learning["replay"]):
1031
1256
  return
1257
+ level = self._admission()
1258
+ if level == "off":
1259
+ self._fixture_status = "off"
1260
+ return
1032
1261
  with self._condition:
1033
- if self._closing or self._pause or self._cell_running or self._sides or (self._inbox and not replay):
1262
+ if self._closing or self._pause or self._busy() or (self._inbox and not replay):
1034
1263
  return
1035
1264
  generation = self._activation_interrupt, self._revision
1036
1265
 
1037
1266
  def cancelled():
1038
1267
  with self._condition:
1039
- return (self._closing or self._pause or self._cell_running or bool(self._sides)
1268
+ return (self._closing or self._pause or self._busy()
1040
1269
  or generation != (self._activation_interrupt, self._revision))
1041
1270
 
1271
+ own_test = level == OWN_TEST and not replay
1042
1272
  try:
1043
1273
  inventory = fixtures.configured(self.profile)
1044
1274
  except (OSError, TypeError, ValueError) as error:
1045
- self._fixture_status = "unavailable"
1046
- self.trace.emit("library", action="fixture_unavailable", reason=str(error))
1047
- return
1275
+ inventory = ()
1276
+ if self._fixture_status != "unavailable":
1277
+ self.trace.emit("library", action="fixture_unavailable", reason=str(error))
1048
1278
  self._fixture_descriptions = fixtures.describe(inventory)
1049
1279
  self._fixture_status = "configured" if self._fixture_descriptions else "unavailable"
1050
- if not self._fixture_descriptions:
1280
+ if not self._fixture_descriptions and not own_test:
1051
1281
  return
1052
1282
  learner = self._learner()
1053
- trials = {fixture.get("skill"): fixture for fixture in inventory}
1283
+ trials = {fixture.get("skill"): fixture for fixture in inventory} if self._fixture_descriptions else {}
1054
1284
  for index_key, row in learner.index().items():
1055
1285
  pending = index_key.startswith(f"{CANDIDATES}/")
1056
1286
  if pending and (replay or row.get("evidence") != "proposed"):
@@ -1062,10 +1292,20 @@ class TaskRun:
1062
1292
  continue
1063
1293
  fixture = trials.get(Path(key).stem)
1064
1294
  expected = row.get("revision")
1065
- if (not fixture or not expected or expected in self._fixture_attempts or cancelled()
1066
- or self.seconds_left() <= 0):
1295
+ if not fixture and own_test and pending and (row.get("demoted") or {}).get("revision") == expected:
1296
+ uses = row["demoted"]
1297
+ self._fixture_feedback[key] = (f"Sent back to pending after failing in use ({uses.get('ok', 0)} of "
1298
+ f"{uses.get('ok', 0) + uses.get('failed', 0)} uses succeeded): rewrite it "
1299
+ "and propose it again.")
1300
+ continue
1301
+ if ((not fixture and not (own_test and pending)) or not expected or expected in self._fixture_attempts
1302
+ or cancelled() or self.seconds_left() <= 0):
1067
1303
  continue
1068
1304
  self._fixture_attempts.add(expected)
1305
+ if not fixture:
1306
+ # A fixture, where the host has one for the entry, wins: this is the rest.
1307
+ self._own_test_trial(learner, key, expected, cancelled)
1308
+ return
1069
1309
  budget = fixtures.Budget(self.seconds_left(), cancelled)
1070
1310
  try:
1071
1311
  entry = learner.candidate_from(key, pending=pending)
@@ -1120,6 +1360,116 @@ class TaskRun:
1120
1360
  if not replay:
1121
1361
  return
1122
1362
 
1363
+ def _own_test_trial(self, learner, key, expected, cancelled):
1364
+ """Verify one pending proposal by its own test, where the admission level is own_test:
1365
+ the kernel stages the exact revision, the host runs its test on the staged revision
1366
+ as a cell of its own (Kernel.run_staged: bounded, counted as testing, the entry not
1367
+ yet in the programs' namespace) and reads the cell's result. The kernel never grades:
1368
+ a pass makes the host write the proof (method "own_test", this worker's profile, the
1369
+ receipt) and admit the revision as provisional, then commit it; anything else leaves
1370
+ nothing loaded, and a test that ran and did not pass marks the proposal failed."""
1371
+ from harness.runtime.learning import fixtures
1372
+ from harness.runtime.learning.learner import OWN_TEST, OWN_TEST_SECONDS, STAGED_TEST
1373
+
1374
+ def pending(reason, tell=True):
1375
+ if tell:
1376
+ self._fixture_feedback[key] = f"Still pending: {reason[:300]}."
1377
+ self.trace.emit("library", action="trial_pending", key=key, revision=expected, reason=reason)
1378
+
1379
+ try:
1380
+ entry = learner.candidate_from(key, pending=True)
1381
+ except (OSError, ValueError) as error:
1382
+ return pending(str(error))
1383
+ if entry is None or not entry.test or entry.revision().revision != expected:
1384
+ return
1385
+ if entry.test.startswith(STAGED_TEST) and not sandbox_of(self.config.runtime):
1386
+ return pending("its test stages a scene in the practice sandbox, and this run has none")
1387
+ item = entry.revision()
1388
+ budget = fixtures.Budget(self.seconds_left(), cancelled, limit=OWN_TEST_SECONDS + 60.0)
1389
+ observed = {}
1390
+
1391
+ def test(kernel):
1392
+ """Why the staged revision's own test did not pass; None when it passed."""
1393
+ limit = max(0.01, min(OWN_TEST_SECONDS, self.seconds_left()))
1394
+ self.trace.emit("library", action="trial_started", key=key, revision=expected, method=OWN_TEST)
1395
+ began = time.perf_counter()
1396
+ try:
1397
+ with self._span("program", f"test of {key}") as end:
1398
+ # The test runs as a unit with `limit` as its time: the kernel ends it then.
1399
+ result = kernel.run_staged(entry.test, timeout=limit)
1400
+ end.update(status=result["status"])
1401
+ finally:
1402
+ self.summary["kernel_seconds"] += time.perf_counter() - began
1403
+ _cell_usage(self.summary, result)
1404
+ self.charge_up()
1405
+ why = None
1406
+ if result["status"] != "ok" and "deadline exceeded" in str(result.get("error") or "").lower():
1407
+ why = f"its test did not end within {limit:.0f} s"
1408
+ elif result["status"] != "ok":
1409
+ why = str(result.get("error") or f"its test ended the program with {result['status']}")
1410
+ elif (result.get("state") or {}).get("ended"):
1411
+ why = "its test ended the episode"
1412
+ receipt = {"ok": why is None, "method": OWN_TEST, "run": str(self.directory), "episode_id": self.episode,
1413
+ "revision": expected, "seconds": round(time.perf_counter() - began, 3),
1414
+ **({"error": why[-600:]} if why else {})}
1415
+ observed["receipt"] = receipt
1416
+ self.trace.emit("library", action="trial_observed", key=key, **receipt,
1417
+ stdout=str(result.get("stdout") or "")[-2000:])
1418
+ return why
1419
+
1420
+ def publish():
1421
+ budget.remaining()
1422
+ receipt = observed["receipt"]
1423
+ proof = {"revision": item.revision, "receipt": receipt,
1424
+ "conditions": {"method": OWN_TEST, "profile_sha256": sha256(self.profile)}}
1425
+ value = learner.admit(entry, "provisional", proof=proof, test_run=receipt, expected=expected,
1426
+ check=budget.remaining)
1427
+ self._library_admitted(learner, value, entry.text, entry.kind, entry.source, entry.origin)
1428
+ self._fixture_feedback.pop(key, None)
1429
+ observed["admitted"] = value
1430
+ return value["row"]
1431
+
1432
+ proof = {"revision": item.revision, "conditions": {"method": OWN_TEST, "profile_sha256": sha256(self.profile)}}
1433
+ row = {**(learner.index().get(f"candidates/{key}") or {}), "revision": item.revision, "proof": proof,
1434
+ "evidence": "provisional"}
1435
+ loads = learner.wanted(entry, "provisional") and self._library_eligible(
1436
+ item, row, learner.layers, learner.settings["load"])
1437
+ try:
1438
+ outcome = self._activate_library([(item, row)], publish=publish, budget=budget, test=test, commit=loads)
1439
+ except (OSError, ValueError, RuntimeError, TimeoutError) as error:
1440
+ if isinstance(error, KernelDied):
1441
+ raise
1442
+ outcome = {"status": "error", "error": str(error)}
1443
+ receipt = observed.get("receipt")
1444
+ active = (self.config.runtime.get("library") or {}).get("trigger") == "active"
1445
+ if "admitted" in observed:
1446
+ if not active:
1447
+ self._tell(f"The host ran the test of your proposal {key} and it passed"
1448
+ + (": it is active from your next program." if outcome.get("status") == "activated"
1449
+ else "; it waits for validation before it is loaded."))
1450
+ return
1451
+ if receipt is not None and not receipt["ok"]:
1452
+ try:
1453
+ with self._admission_lock, self._condition:
1454
+ learner.record_trial(key, expected, item, pending=True, evidence="failed", receipt=receipt,
1455
+ check=lambda: None)
1456
+ except (OSError, ValueError) as error:
1457
+ self.trace.emit("library", action="trial_pending", key=key, revision=expected, reason=str(error))
1458
+ self._fixture_feedback[key] = f"Its own test failed when the host ran it: {receipt['error'][-300:]}"
1459
+ if not active:
1460
+ self._tell(f"The host ran the test of your proposal {key} and it failed: {receipt['error'][-300:]}\n"
1461
+ "It stays out of your library; rewrite it and propose it again if you still need it.")
1462
+ return
1463
+ # No verdict: the revision could not be staged, or the boundary was taken (a pause, a
1464
+ # message, an event). One more try at a later boundary, then it waits.
1465
+ if outcome.get("status") == "cancelled" or receipt is not None:
1466
+ if expected not in self._trial_interrupted:
1467
+ self._trial_interrupted.add(expected)
1468
+ self._fixture_attempts.discard(expected)
1469
+ pending("its test was interrupted", tell=False)
1470
+ else:
1471
+ pending(f"it could not be loaded for its test: {outcome.get('error') or 'unknown'}")
1472
+
1123
1473
  def _startup_library(self, learning):
1124
1474
  library = self._learner().library
1125
1475
  with self._admission_lock:
@@ -1150,22 +1500,28 @@ class TaskRun:
1150
1500
  self._activation_queue[item.key] = (item, row)
1151
1501
 
1152
1502
  def _library_eligible(self, item, row, layers, load):
1503
+ if executable(item.key) and row and row.get("origin") != "hand" and self._admission() == "off":
1504
+ return False # the host admits no learned code: it is not loaded either
1153
1505
  conditions = (row.get("proof") or {}).get("conditions") or {}
1154
1506
  if isinstance(conditions, dict) and conditions.get("profile_sha256") not in (None, sha256(self.profile)):
1155
1507
  return False
1156
1508
  return eligible(item, row, layers, load)
1157
1509
 
1158
- def _activate_library(self, selected, *, startup=False, publish=None, budget=None):
1510
+ def _activate_library(self, selected, *, startup=False, publish=None, budget=None, test=None, commit=True):
1511
+ """Stage `selected` in the kernel, publish (the admission, when given) and commit.
1512
+ `test(kernel)` runs after staging and says why the staged revision must not be
1513
+ published (None: go on); without `commit` the staged revision is admitted and not
1514
+ loaded. {"status": "activated", "admitted", "cancelled" or "error"[, "error"]}."""
1159
1515
  if not selected:
1160
- return
1516
+ return {"status": "cancelled"}
1161
1517
  def can_switch():
1162
- return startup or (not (self._sides or self._cell_running or self._inbox)
1518
+ return startup or (not (self._busy() or self._inbox)
1163
1519
  and (publish is not None or all(
1164
1520
  self._activation_queue.get(item.key, (None,))[0] == item for item, _ in selected)))
1165
1521
 
1166
1522
  with self._condition:
1167
1523
  if self._pause or self._closing or not can_switch():
1168
- return
1524
+ return {"status": "cancelled"}
1169
1525
  kernel = self.kernel
1170
1526
  interrupt = self._activation_interrupt
1171
1527
  for item, _ in selected:
@@ -1174,21 +1530,30 @@ class TaskRun:
1174
1530
  with self._span("program", "library"):
1175
1531
  prepared = kernel.load_library("prepare", entries, **({"timeout": budget.remaining()} if budget else {}))
1176
1532
  rejected = None
1533
+ if prepared["status"] == "ok" and test is not None:
1534
+ try:
1535
+ rejected = test(kernel)
1536
+ except KernelDied:
1537
+ raise
1538
+ except (OSError, ValueError, RuntimeError) as error:
1539
+ rejected = str(error)
1177
1540
  with self._admission_lock, self._condition:
1178
1541
  cancelled = (self._closing or self._pause or self.kernel is not kernel
1179
1542
  or self._activation_interrupt != interrupt or not can_switch())
1180
- if not cancelled and prepared["status"] == "ok" and publish is not None:
1543
+ if not cancelled and prepared["status"] == "ok" and rejected is None and publish is not None:
1181
1544
  try:
1182
1545
  selected = [(selected[0][0], publish())]
1183
1546
  except (OSError, ValueError, RuntimeError) as error:
1184
1547
  rejected = str(error)
1185
- if prepared["status"] != "ok" or cancelled or rejected:
1548
+ if prepared["status"] != "ok" or cancelled or rejected or not commit:
1186
1549
  if prepared["status"] == "ok" and self.kernel is kernel:
1187
1550
  kernel.load_library("discard")
1551
+ if prepared["status"] == "ok" and not cancelled and not rejected:
1552
+ return {"status": "admitted"}
1188
1553
  self.trace.emit("library", action="load_failed", status="cancelled" if cancelled else "error",
1189
1554
  error=rejected or prepared.get("error"), revisions={item.key: item.revision for item, _ in selected})
1190
1555
  if cancelled or rejected:
1191
- return
1556
+ return {"status": "cancelled" if cancelled else "error", "error": rejected}
1192
1557
  if startup and any(not hand for _, hand in entries):
1193
1558
  self._activate_library([(item, row) for item, row in selected
1194
1559
  if row.get("origin", "hand") == "hand"], startup=True)
@@ -1199,7 +1564,7 @@ class TaskRun:
1199
1564
  for item, _ in selected:
1200
1565
  if self._activation_queue.get(item.key, (None,))[0] == item:
1201
1566
  del self._activation_queue[item.key]
1202
- return
1567
+ return {"status": "error", "error": prepared.get("error")}
1203
1568
  # The boundary check reserved the switch. The kernel reader can drain notes while
1204
1569
  # the commit is acknowledged and the decision loop still owns program scheduling.
1205
1570
  result = kernel.load_library("commit")
@@ -1225,7 +1590,7 @@ class TaskRun:
1225
1590
  self.config.system_prompt = (self.config.system_prompt.replace(prior, updated, 1)
1226
1591
  if prior else self.config.system_prompt + updated)
1227
1592
  self._library_prompt = updated
1228
- self.invalidate_lookahead()
1593
+ return {"status": "activated"}
1229
1594
 
1230
1595
  @staticmethod
1231
1596
  def _active_row(item, row):
@@ -1237,14 +1602,14 @@ class TaskRun:
1237
1602
  return {**row_data, "key": item.key, "revision": item.revision, "sha256": item.source_sha256,
1238
1603
  "source": row.get("origin", "hand"), "summary": row.get("summary", ""),
1239
1604
  "text": item.source.decode("utf-8")[:400], "active": True,
1240
- **({"verification": row["proof"]} if row.get("proof") else {})}
1605
+ **({"verification": row["proof"], "admitted_by": admitted_by(row)} if row.get("proof") else {})}
1241
1606
 
1242
1607
  def _startup_holdings(self):
1243
1608
  from harness.runtime.learning import active
1244
1609
 
1245
1610
  situations = self._situations()
1246
1611
  rows = [{"section": item.key.split("/", 1)[0], "name": Path(item.key).stem,
1247
- "summary": row.get("summary"), "passed": bool(row.get("proof"))}
1612
+ "summary": row.get("summary"), "passed": bool(row.get("proof")), "admitted_by": admitted_by(row)}
1248
1613
  for item, row in self._active_library.values()]
1249
1614
  holdings = active.holdings(rows, situations)
1250
1615
  return f"\n{holdings}\n" if holdings else ""
@@ -1304,7 +1669,6 @@ class TaskRun:
1304
1669
  with self._condition:
1305
1670
  self.config = copy(self.config)
1306
1671
  self.config.system_prompt += f"\nLearned in this run: {str(request.get('text', '')).strip()}\n"
1307
- self.invalidate_lookahead()
1308
1672
  else:
1309
1673
  self.trace.emit("library", action="rejected", kind=request.get("entry"), name=request.get("name"),
1310
1674
  reason=answer["error"])
@@ -1373,11 +1737,6 @@ class TaskRun:
1373
1737
  """A program asks for sub-agents (skills/agents): done on a thread of its own (a wait
1374
1738
  takes long; the kernel's notes must go on), answered through the kernel."""
1375
1739
 
1376
- if request.get("action") == "wait" and request.get("own", True):
1377
- with self._condition:
1378
- request["interrupt_at"] = self._interruptions
1379
- self.invalidate_lookahead()
1380
-
1381
1740
  def answer():
1382
1741
  if self.agents is None:
1383
1742
  value = {"error": "this conversation cannot start sub-agents"}
@@ -1422,16 +1781,18 @@ class TaskRun:
1422
1781
 
1423
1782
  def _wait(self, status: str, ready, **detail):
1424
1783
  """Wait in `status` until `ready()` holds (under the lock); Closed when the user
1425
- closes the conversation meanwhile."""
1784
+ closes the conversation meanwhile, Interrupted when they end the turn."""
1426
1785
  self._set_status(status, **detail)
1427
1786
  began = time.perf_counter()
1428
1787
  with self._condition:
1429
- while not self._closing and not ready():
1788
+ while not self._closing and not self._interrupt and not ready():
1430
1789
  self._condition.wait()
1431
- closing = self._closing
1790
+ closing, interrupted = self._closing, self._interrupt
1432
1791
  self._waited += time.perf_counter() - began
1433
1792
  if closing:
1434
1793
  raise Closed()
1794
+ if interrupted:
1795
+ raise Interrupted()
1435
1796
  self._set_status(RUNNING)
1436
1797
 
1437
1798
  def _take_inbox(self) -> list[str]:
@@ -1442,13 +1803,19 @@ class TaskRun:
1442
1803
  def _checkpoint(self) -> None:
1443
1804
  """Between steps: stop when closing, wait while paused."""
1444
1805
  with self._condition:
1445
- closing, paused = self._closing, self._pause
1806
+ closing, interrupted, paused = self._closing, self._interrupt, self._pause
1446
1807
  if closing:
1447
1808
  raise Closed()
1809
+ if interrupted:
1810
+ raise Interrupted()
1448
1811
  if paused:
1449
1812
  self._wait(PAUSED, lambda: not self._pause or bool(self._inbox))
1450
1813
  with self._condition:
1451
1814
  self._pause = False
1815
+ kernel = self.kernel
1816
+ if kernel is not None:
1817
+ # (A message ends the pause as Continue does: the units may act again.)
1818
+ kernel.resume()
1452
1819
  self._handed_back()
1453
1820
 
1454
1821
  def _handed_back(self) -> None:
@@ -1463,14 +1830,34 @@ class TaskRun:
1463
1830
  except (ValueError, RuntimeError, OSError):
1464
1831
  pass
1465
1832
 
1466
- def _await_input(self) -> None:
1467
- """Wait for missing task input, without starting a new turn or renewing its budget."""
1833
+ def _woken(self) -> bool:
1834
+ """Whether something reached the model that starts a turn: a message, the end of a
1835
+ unit (one that was cancelled is told with the next turn, it starts none), an event
1836
+ it has not been told."""
1837
+ return (bool(self._inbox) or self._run_end is not None
1838
+ or any(item["status"] != "cancelled" for item in self._unit_news)
1839
+ or any(identifier not in self._reported_events for identifier in self._pending_events))
1840
+
1841
+ def _idle(self, reason: str) -> None:
1842
+ """Between turns: the conversation waits, IDLE with the reason its last turn ended
1843
+ for, with everything it has (messages, the kernel and its units, rules, facts, the
1844
+ environment) until something reaches the model. Closed when the user or the host
1845
+ ends the conversation meanwhile."""
1468
1846
  self.waiting_since = time.time()
1847
+ # No turn runs: the next one has its whole time.
1848
+ self._turn_began = None
1469
1849
  try:
1470
- self._wait(IDLE, lambda: bool(self._inbox) or bool(self._pending_events), reason="message")
1850
+ self._set_status(IDLE, reason=reason)
1851
+ with self._condition:
1852
+ while not self._closing and not self._woken():
1853
+ self._condition.wait()
1854
+ # (An interrupt asked for when the turn was over ends nothing.)
1855
+ closing, self._interrupt = self._closing, False
1856
+ if closing:
1857
+ raise Closed()
1858
+ self._set_status(RUNNING)
1471
1859
  finally:
1472
1860
  self.waiting_since = None
1473
- self.reason = "user"
1474
1861
 
1475
1862
  def _add_user_messages(self) -> None:
1476
1863
  """Put the user's messages into the next request: into the user message that
@@ -1485,50 +1872,94 @@ class TaskRun:
1485
1872
  self._tell(parts)
1486
1873
 
1487
1874
  def _tell(self, content) -> None:
1488
- """Add a user message to the history: what the program of the last used reply
1489
- to a call made while it ran reported (self._carry) first, then `content`."""
1490
- carry, self._carry = self._carry, []
1491
- if carry:
1492
- content = carry + ([{"type": "input_text", "text": content}] if isinstance(content, str) else list(content))
1875
+ """Add a user message to the history."""
1493
1876
  self.messages.append({"role": "user", "content": content})
1494
1877
 
1495
- def _with_what_was_said(self, request, messages):
1496
- """Interactive: when the window leaves earlier turns out, the first message of the
1497
- request still lists everything the user said (they hold the task)."""
1498
- if not self.interactive or len(request) >= len(messages) or len(self._said) < 2:
1499
- return request
1500
- text = "The user's messages so far, oldest first:\n" + "\n".join(
1501
- f"{number}. {said}" for number, said in enumerate(self._said, 1))
1502
- first = dict(request[0])
1503
- first["content"] = (list(first["content"]) + [{"type": "input_text", "text": text}]
1504
- if isinstance(first["content"], list) else f"{first['content']}\n{text}")
1505
- return [first] + request[1:]
1878
+ def _say(self, parts) -> None:
1879
+ """Tell the model `parts` with its next call: in the user message that ends the
1880
+ history, or as a new one after a reply."""
1881
+ if self.messages and self.messages[-1]["role"] == "user":
1882
+ self._extend_request(parts)
1883
+ else:
1884
+ self._tell(parts)
1885
+
1886
+ def _tree_told(self) -> str:
1887
+ """What this turn is told of the tree of reflexes: what changed in it since the
1888
+ model was last told (runtime/reactive/tree.py told)."""
1889
+ nodes = getattr(self, "_reflex_nodes", {})
1890
+ text = tree_told(nodes, getattr(self, "_reflex_told", {}))
1891
+ self._reflex_told = dict(nodes)
1892
+ return text
1893
+
1894
+ def _compact_if_due(self, loop) -> None:
1895
+ """Compaction: when the context the model's last call read has reached
1896
+ runtime.compact_at_tokens, the model writes a summary of the conversation
1897
+ (decisions.COMPACT), which then stands in the first message in place of every
1898
+ message before the recent ones (messages.recent_start). Interactive: the summary is
1899
+ followed by everything the user said (they hold the task). The system prompt, the
1900
+ kernel, the library, the tree of reflexes and the running units are as they were;
1901
+ what a turn is told of the tree and of what was learned is told anew."""
1902
+ import re
1903
+ from copy import deepcopy
1904
+
1905
+ limit, messages = self.config.runtime.get("compact_at_tokens"), self.messages
1906
+ if (not limit or self._context_tokens < int(limit) or len(messages) <= 3
1907
+ or loop.malformed_from is not None):
1908
+ return
1909
+ tail = messages[recent_start(messages, decisions.KEEP_MIN_TOKENS, decisions.KEEP_MIN_MESSAGES,
1910
+ decisions.KEEP_MAX_TOKENS):]
1911
+ request = deepcopy(messages) if self._images else text_only(deepcopy(messages))
1912
+ ask = {"type": "input_text", "text": decisions.COMPACT}
1913
+ if request[-1]["role"] == "user":
1914
+ content = request[-1]["content"]
1915
+ request[-1]["content"] = [*([{"type": "input_text", "text": content}] if isinstance(content, str)
1916
+ else content), ask]
1917
+ else:
1918
+ request.append({"role": "user", "content": [ask]})
1919
+ before, tokens = len(messages), self._context_tokens
1920
+ reply = self._ask_role("compaction", self.chat, self.config.system_prompt, request)
1921
+ found = re.search(r"<summary>(.*?)(?:</summary>|$)", reply.text, re.S)
1922
+ text = (found.group(1) if found else "").strip()
1923
+ if not text:
1924
+ self._compact_failures += 1
1925
+ self.trace.emit("compaction", action="failed", tokens=tokens, attempts=self._compact_failures)
1926
+ if self._compact_failures >= 3:
1927
+ raise RuntimeError("The conversation could not be compacted: the model gave no summary")
1928
+ return
1929
+ if self.interactive and self._said:
1930
+ text += "\n\nThe user's messages so far, oldest first:\n" + "\n".join(
1931
+ f"{number}. {said}" for number, said in enumerate(self._said, 1))
1932
+ with self._condition:
1933
+ first = dict(messages[0])
1934
+ content = first["content"]
1935
+ first["content"] = [
1936
+ *([{"type": "input_text", "text": content}] if isinstance(content, str)
1937
+ else [part for part in content if not part.get("summary")]),
1938
+ {"type": "input_text", "text": decisions.COMPACTED.format(summary=text), "summary": True}]
1939
+ messages[:] = [first, *tail]
1940
+ # The next request's record replaces every message the trace has (rebuild_messages).
1941
+ self._replaces, self._logged = self._replaces + self._logged, 0
1942
+ self._context_tokens, self._compact_failures = 0, 0
1943
+ self._reflex_told, self._learning_told = {}, None
1944
+ self.trace.emit("compaction", action="done", tokens=tokens, messages_before=before, messages=len(messages),
1945
+ summary=text)
1946
+ self._save()
1506
1947
 
1507
1948
  def seconds_left(self) -> float:
1508
- """What the episode has left of its wall time (max_wall_seconds): the time it ran,
1509
- less the time it waited for the user, counts; the whole budget before it starts."""
1510
- started = getattr(self, "started", None)
1511
- ran = time.perf_counter() - started - self._waited if started is not None else 0.0
1949
+ """What the turn has left of its wall time (max_wall_seconds): the time it ran, less
1950
+ the time it waited for the user, counts; the whole budget before it starts."""
1951
+ began = self._turn_began
1952
+ ran = time.perf_counter() - began - self._waited if began is not None else 0.0
1512
1953
  return float(self.config.max_wall_seconds) - ran
1513
1954
 
1514
1955
  def _cell_timeout(self, *, policy=False) -> float:
1515
- """The program's declared deadline, bounded by the episode."""
1956
+ """The program's declared deadline, bounded by the turn."""
1516
1957
  seconds = self.seconds_left()
1517
1958
  configured = self.config.runtime.get("cell_timeout_seconds")
1518
1959
  if configured is not None and not policy:
1519
1960
  seconds = min(seconds, float(configured))
1520
1961
  return max(0.01, seconds)
1521
1962
 
1522
- def _program_deadline(self, number, episode_deadline):
1523
- with self._condition:
1524
- if self._cell_running and not self._program_completing and self.summary["cells"] == number:
1525
- if episode_deadline:
1526
- self._wall_up = True
1527
- self.kernel.pause()
1528
- else:
1529
- self._program_timed_out = True
1530
- self._stop_program_for_decision()
1531
-
1532
1963
  def _stuck(self, program: str, result: dict) -> None:
1533
1964
  """Interactive: note the same program, or the same unhandled event, three times
1534
1965
  in a row (after OpenHands' stuck detector). Only noted; nothing changes."""
@@ -1542,9 +1973,10 @@ class TaskRun:
1542
1973
  self._recent = []
1543
1974
 
1544
1975
  def run(self) -> dict:
1545
- """Run the episode to its end and return its summary (status, evaluator outcome,
1546
- model calls, tokens, cost, times), also written with the trace and manifest under
1547
- the directory. An error ends the episode with status "error" instead of raising."""
1976
+ """Run the conversation to its end and return its summary (the status of its last
1977
+ turn, evaluator outcome, model calls, tokens, cost, times), also written with the
1978
+ trace and manifest under the directory at every turn's end. An error ends the turn
1979
+ (before the first one: the conversation) with status "error" instead of raising."""
1548
1980
  from harness.runtime.decision.model import USAGE, ChatModel
1549
1981
  from harness.config.agent import image_roles, model_services
1550
1982
 
@@ -1591,6 +2023,12 @@ class TaskRun:
1591
2023
  # The embodiment profile is part of the frozen configuration (App. A).
1592
2024
  self.profile = {key: value for key, value in env.call("profile").items()
1593
2025
  if key not in ("task", "step_limit", "fk_check_error_m")}
2026
+ if self.profile.get("tells"):
2027
+ # The worker tells what happens by itself: it is given where (inbound.py).
2028
+ from harness.runtime.task.inbound import Inbound
2029
+
2030
+ self._inbound = Inbound(self)
2031
+ env.call("tell_to", episode_id=self.episode, url=self._inbound.url, token=self._inbound.token)
1594
2032
  # The skills, prompt and session were chosen for one embodiment kind; the worker
1595
2033
  # says which it drives.
1596
2034
  if embodiment_kind(self.profile) != embodiment:
@@ -1645,12 +2083,13 @@ class TaskRun:
1645
2083
  if self._managed_workspace is not None:
1646
2084
  self._managed_dispatcher = self._managed_workspace.dispatcher(self, self.workspace_kind)
1647
2085
  operations = ", ".join(self._managed_dispatcher.options()["operations"])
1648
- scope = f"\nThis isolated workspace exposes only these synchronous operations: {operations}.\n"
2086
+ scope = f"\nThis isolated workspace exposes only these operations: {operations}.\n"
1649
2087
  config.system_prompt += scope
1650
2088
  config.base_prompt += scope
1651
2089
  self.kernel = Kernel(DEFAULT_BOOTSTRAP, {}, managed=self._managed_dispatcher,
1652
2090
  managed_launcher=self._managed_workspace.program_command,
1653
- log_path=directory / "kernel.log", start_timeout=30)
2091
+ log_path=directory / "kernel.log", start_timeout=30,
2092
+ on_span=self._kernel_span)
1654
2093
  else:
1655
2094
  # (The verifier is asked through the conversation, a thought: control.think.)
1656
2095
  services = model_services(runtime, roles)
@@ -1673,6 +2112,8 @@ class TaskRun:
1673
2112
  "skills": skills,
1674
2113
  **kernel_services, **image_clients,
1675
2114
  "memory": self._recall_library,
2115
+ # The facts an earlier process of this conversation kept.
2116
+ **({"facts": self._facts} if self._facts else {}),
1676
2117
  # App. A: programs see only the Python installation and the harness source,
1677
2118
  # and reach only the services named here (runtime/safety/isolation.py).
1678
2119
  "isolation": runtime.get("isolation", True),
@@ -1689,6 +2130,8 @@ class TaskRun:
1689
2130
  **({"sandbox": sandbox_of(runtime)} if sandbox_of(runtime) else {}),
1690
2131
  # The worker's private files (a game's instance): never a program's.
1691
2132
  **({"private_paths": self.profile["private_paths"]} if self.profile.get("private_paths") else {}),
2133
+ # What the environment gives to act with, where programs can read it (core/worker.py).
2134
+ **({"references": list(self.profile["references"])} if self.profile.get("references") else {}),
1692
2135
  }, python=kernel_python(runtime), cwd=str(self.program_directory) if self.program_directory else None,
1693
2136
  log_path=directory / "kernel.log",
1694
2137
  on_span=self._kernel_span)
@@ -1737,17 +2180,35 @@ class TaskRun:
1737
2180
  if self.cameras:
1738
2181
  opening += (f"Views of this environment: {', '.join(self.cameras)}. None is shown until a "
1739
2182
  "program looks: look().\n")
2183
+ if self.profile.get("references"):
2184
+ opening += ("What this environment gives you to act with, its documentation and source, is in these "
2185
+ "folders, which a program can read: " + ", ".join(map(str, self.profile["references"])) + "\n")
1740
2186
  self.messages = [{"role": "user", "content": [
1741
2187
  {"type": "input_text",
1742
2188
  "text": opening + f"{told['state_label']}: "
1743
2189
  f"{state_text(observation.state, embodiment, self._steps_shown())}"},
1744
2190
  *first]}]
1745
- summary["status"] = "budget_exhausted"
1746
- self._loop(skills, told)
2191
+ if self._restore is not None:
2192
+ self._continue(self._restore, f"{told['state_label']}: "
2193
+ f"{state_text(observation.state, embodiment, self._steps_shown())}")
2194
+ self._idle("resumed")
2195
+ while True:
2196
+ reason = self._turn(skills, told)
2197
+ if not self.interactive or self.kernel.process.poll() is not None:
2198
+ # Nobody is there for another turn, or the kernel died with what it held.
2199
+ break
2200
+ self._turn_result(reason, open=True)
2201
+ self._idle(reason)
2202
+ if reason == "answer":
2203
+ # The benchmark's convention at the end of an episode that was answered.
2204
+ self.kernel.cancel()
2205
+ _wrap_up(self.kernel, trace)
1747
2206
  except Closed:
1748
- # Finished, closed by the user, or a sub-agent's budget ran out: the benchmark's
1749
- # convention ends it.
1750
- summary["status"] = "closed"
2207
+ # Closed by the user or the host: the benchmark's convention ends it. Between
2208
+ # turns the summary keeps how the last turn ended; a turn that was cut short
2209
+ # (or a conversation that had none) is "closed".
2210
+ if self._turn_began is not None or not self._turns:
2211
+ summary["status"] = "closed"
1751
2212
  if self.kernel is not None:
1752
2213
  _wrap_up(self.kernel, trace)
1753
2214
  except KeyboardInterrupt:
@@ -1762,40 +2223,143 @@ class TaskRun:
1762
2223
  self._close_episode(token)
1763
2224
  return summary
1764
2225
 
1765
- def _loop(self, skills, told) -> None:
2226
+ def _continue(self, kept: dict, state: str) -> None:
2227
+ """Go on with what an earlier process kept of this conversation (saved()): its
2228
+ messages, what the user said, its turns and the model's spending so far (its facts
2229
+ are in the new kernel already). What died with that process (the namespace, the
2230
+ rules, the units that ran) is a note the model reads with its next prompt, with
2231
+ the state as it is now; it starts no turn."""
2232
+ summary = self.summary
2233
+ self.messages = list(kept["messages"])
2234
+ self._said = [str(said) for said in kept.get("said") or self._said]
2235
+ self._turns = int(kept.get("turns") or 0)
2236
+ self._replied_at = kept.get("replied")
2237
+ spent = kept.get("summary") or {}
2238
+ for key, value in spent.items():
2239
+ if key in summary and type(value) in (int, float):
2240
+ summary[key] = value
2241
+ summary["decisions"] = dict(spent.get("decisions") or {})
2242
+ if isinstance(spent.get("status"), str):
2243
+ # (How its last turn ended, until its next one ends.)
2244
+ summary["status"] = spent["status"]
2245
+ self._logged = len(self.messages)
2246
+ units = [str(unit) for unit in kept.get("units") or ()]
2247
+ self._say([{"type": "input_text", "text": decisions.CONTINUED.format(
2248
+ units=": " + "; ".join(units) if units else "") + "\n" + state}])
2249
+ self.trace.emit("resumed", messages=len(kept["messages"]), facts=sorted(self._facts), units=units)
2250
+
2251
+ def _save(self) -> None:
2252
+ """Write what continues this conversation in another process (CONVERSATION_FILE), at
2253
+ every call of the model and every turn's end: with a user or without one (a run
2254
+ nobody talks to is continued with a message as well). A sub-agent lives and ends
2255
+ with the process of the agent that started it."""
2256
+ if self.parent is not None:
2257
+ return
2258
+ with self._condition:
2259
+ kept = json.dumps({
2260
+ "schema_version": 1, "messages": self.messages, "said": self._said, "facts": self._facts,
2261
+ "units": [unit_text(identifier, unit, printed=False) for identifier, unit in self._units.items()],
2262
+ "turns": self._turns, "replied": self._replied_at, "summary": self.summary}, ensure_ascii=False)
2263
+ atomic_write(self.directory / CONVERSATION_FILE, kept)
2264
+
2265
+ def _turn(self, skills, told) -> str:
2266
+ """One turn, from what started it to how it ended (a key of decisions.TURN_ENDS,
2267
+ whose status the summary takes). Its limits are its own: the model's calls and the
2268
+ wall time begin anew. An error ends the turn; so does cancel(). Only an answer
2269
+ leaves the model's units running: after any other end they are cancelled, so that
2270
+ nothing starts a turn until the user says how to go on."""
2271
+ summary, trace = self.summary, self.trace
2272
+ summary.pop("error", None)
2273
+ with self._condition:
2274
+ self._turns += 1
2275
+ self._turn_began, self._waited = time.perf_counter(), 0.0
2276
+ self._calls_before = summary["model_calls"] + self.agents_usage["model_calls"]
2277
+ self._iterations = 0
2278
+ try:
2279
+ reason = self._loop(skills, told)
2280
+ except Interrupted:
2281
+ trace.emit("interrupted")
2282
+ reason = "interrupted"
2283
+ except Closed:
2284
+ raise
2285
+ except Exception as error:
2286
+ summary["error"] = f"{type(error).__name__}: {error}"
2287
+ trace.emit("error", message=summary["error"])
2288
+ reason = "error"
2289
+ with self._condition:
2290
+ self._interrupt = False
2291
+ if reason in decisions.TURN_STOPS:
2292
+ # What reached the model in this turn and met no call of it is in its
2293
+ # history: it starts no turn by itself, the user's next message does.
2294
+ self._include_escalations()
2295
+ self._add_user_messages()
2296
+ summary["status"] = decisions.TURN_ENDS[reason]
2297
+ if reason != "answer":
2298
+ self.kernel.cancel()
2299
+ return reason
2300
+
2301
+ def _turn_result(self, reason: str, open: bool = False) -> None:
2302
+ """The end of a turn: learning reflects on it, the summary is written (`open`: the
2303
+ conversation goes on, which the file says, so that nothing takes it for ended),
2304
+ and the turn's result record says why it ended, in which session, the model's
2305
+ calls in it (its sub-agents' counted), the conversation's cost so far and the
2306
+ turn's seconds."""
2307
+ summary = self.summary
2308
+ began = self._turn_began
2309
+ if self.kernel is not None and began is not None:
2310
+ self._reflect_learning()
2311
+ self._write_summary(open)
2312
+ self.trace.emit(
2313
+ "turn_result", reason=reason, turn=self._turns, session=(self.identity or {}).get("session"),
2314
+ model_calls=summary["model_calls"] + self.agents_usage["model_calls"] - self._calls_before,
2315
+ cost_usd=round(summary["cost_usd"] + self.agents_usage["cost_usd"], 6),
2316
+ seconds=round(time.perf_counter() - began - self._waited, 3) if began is not None else 0.0,
2317
+ **({"result": self.result} if reason == "answer" and self.result is not None else {}),
2318
+ **({"error": summary["error"]} if summary.get("error") else {}))
2319
+
2320
+ def _write_summary(self, open: bool = False) -> None:
2321
+ """Write the summary as it stands, and what continues the conversation; its
2322
+ spending is charged to the agent that started it."""
2323
+ summary = self.summary
2324
+ summary["wall_seconds"] = time.perf_counter() - self.started
2325
+ if self.agents_usage["started"] or self.agents_usage["model_calls"]:
2326
+ summary["agents"] = {**self.agents_usage, "cost_usd": round(self.agents_usage["cost_usd"], 6)}
2327
+ write_json(self.directory / "summary.json", {**summary, "open": True} if open else summary)
2328
+ self._save()
2329
+ self.charge_up()
2330
+
2331
+ def _loop(self, skills, told) -> str:
1766
2332
  try:
1767
- self._steps(skills, told)
2333
+ return self._steps(skills, told)
1768
2334
  finally:
1769
2335
  with self._condition:
1770
2336
  if self._request is not None:
1771
- self._settle(self._request, "unused", "the episode or the turn ended first")
2337
+ self._settle(self._request, "unused", "the turn ended first")
1772
2338
  self._request = None
1773
2339
 
1774
- def _steps(self, skills, told) -> None:
1775
- """The loop: a thought on the trigger of what happened last (self.reason), then what
1776
- its reply's output does (OUTPUTS), until an output, a budget or the episode ends it."""
1777
- config, trace, summary, runtime = self.config, self.trace, self.summary, self.config.runtime
1778
- self._logged = 0
2340
+ def _steps(self, skills, told) -> str:
2341
+ """The loop of one turn: the model is told what its units ended with, asked on the
2342
+ trigger of what happened last (self.reason), and its reply's output is done
2343
+ (OUTPUTS), until an output, a unit, a limit or the environment ends the turn: how
2344
+ it ended (decisions.TURN_ENDS). No program holds the loop."""
2345
+ config, summary, runtime = self.config, self.summary, self.config.runtime
1779
2346
  direct = runtime.get("mode", "program") == "direct"
1780
2347
  post_calls = runtime.get("post_policy_calls")
1781
2348
  # What the outputs share in this loop. Direct control: the agent's action vocabulary
1782
- # turns each <action> into a program (runtime.actions, "module:function"). turns: history
1783
- # turns sent to the model, lowered after the model server runs out of memory.
2349
+ # turns each <action> into a program (runtime.actions, "module:function").
1784
2350
  # malformed_from: the first reply without a valid block since the last valid one (such
1785
2351
  # exchanges leave the history once a valid reply comes). nudged: replies in a row sent
1786
2352
  # back for only announcing an action (ANNOUNCED); empties: empty replies in a row asked
1787
2353
  # again (EMPTY_RETRIES). post_calls: the model's calls after the learned policy's first
1788
2354
  # run are bounded (finishing with its own tools cost half the spend for no score), from
1789
- # post_from on; then the policy runs on.
2355
+ # self._post_from on; then the policy runs on.
1790
2356
  loop = SimpleNamespace(skills=skills, told=told, direct=direct, kinds=DIRECT_KINDS if direct else KINDS,
1791
2357
  actions=_object(runtime["actions"]) if direct else None,
1792
- turns=runtime.get("max_history_turns"), malformed_from=None, nudged=0, empties=0,
1793
- post_calls=None if post_calls is None else int(post_calls), post_from=None)
1794
- self.reason = "start"
1795
- # Iterations of the loop in this turn (an interactive episode starts one with every
1796
- # message of the user).
1797
- with self._condition:
1798
- self._iterations = 0
2358
+ malformed_from=None, nudged=0, empties=0,
2359
+ post_calls=None if post_calls is None else int(post_calls))
2360
+ # What the first call of the turn is made for: the instruction, else what started
2361
+ # the turn (a message or an event comes first: decisions.TRIGGERS).
2362
+ self.reason = "start" if self._turns == 1 else "unit_end"
1799
2363
  while True:
1800
2364
  self._checkpoint()
1801
2365
  self._load_due_skills()
@@ -1804,94 +2368,73 @@ class TaskRun:
1804
2368
  self._activate_library(selected)
1805
2369
  self._trial_at_boundary()
1806
2370
  self._checkpoint()
1807
- learning_report = self._learning_report()
2371
+ if ended := self._ended_by_unit(loop):
2372
+ return ended
2373
+ self._tell_news(loop)
2374
+ if ended := self._ended_by_unit(loop):
2375
+ return ended
2376
+ self._compact_if_due(loop)
1808
2377
  with self._condition:
1809
- current = self._request
1810
- if current is not None and current.cancelled:
1811
- if "deadline" in str(current.error):
1812
- raise current.error
1813
- if current.program is not None and self._carry:
1814
- self._tell([])
1815
- self._request = current = None
1816
- if current is None:
1817
- if self._iterations >= config.max_model_calls:
1818
- break
1819
- if (loop.post_from is not None and loop.post_calls is not None
1820
- and summary["model_calls"] - loop.post_from >= loop.post_calls and not self._inbox):
1821
- break_for_policy = True
1822
- else:
1823
- break_for_policy = False
1824
- if self.seconds_left() <= 0:
1825
- summary["status"] = "wall_time_exhausted"
1826
- break
1827
- if not self._ahead_allowed():
1828
- summary["status"] = "cost_exhausted"
1829
- break
1830
- if not break_for_policy:
1831
- current = self._think("between", loop.turns, learning_report=learning_report)
1832
- else:
1833
- break_for_policy = False
1834
- if not break_for_policy:
1835
- self._condition.wait_for(lambda: current.back(0) or self._closing)
2378
+ if self._iterations >= config.max_model_calls:
2379
+ return "budget"
2380
+ policy = (self._post_from is not None and loop.post_calls is not None
2381
+ and summary["model_calls"] - self._post_from >= loop.post_calls and not self._inbox)
2382
+ if self.seconds_left() <= 0:
2383
+ return "wall_time"
2384
+ if not self._within_budget():
2385
+ return "cost"
2386
+ if not policy:
2387
+ current = self._think()
2388
+ self._condition.wait_for(lambda: current.back(0) or self._closing or self._interrupt)
1836
2389
  if self._closing:
1837
2390
  raise Closed()
2391
+ if self._interrupt:
2392
+ raise Interrupted()
2393
+ self._request = None
1838
2394
  if current.cancelled:
2395
+ # Set aside (a message, an event, a unit's end came first), or out of time.
1839
2396
  if isinstance(current.error, ThoughtCancelled) and "deadline" in str(current.error):
1840
2397
  raise current.error
1841
2398
  continue
1842
- self._request = None
1843
- reply, loop.turns = current.wait(), current.turns
1844
- if current.program is not None:
1845
- if self._completed_decision(current, loop.kinds):
1846
- continue
1847
- used = {"ahead": "used"} if current.trigger == "ahead" else {}
1848
- else:
1849
- used = {}
2399
+ reply = current.wait()
2400
+ self._replied_at = time.time()
2401
+ self._context_tokens = (
2402
+ int(reply.usage.get("input_tokens") or 0) + int(reply.usage.get("output_tokens") or 0)
2403
+ or (sum(row["chars"] for row in context_summary(current.messages, current.system))
2404
+ + len(reply.text)) // 4)
1850
2405
  self._settle(current, "used")
1851
2406
  self._pending_events.clear()
1852
2407
  loop.revision = current.revision
1853
- loop.assumption = getattr(current, "assumption", None)
1854
- loop.ahead_generation = current.ahead_generation if current.trigger == "ahead" else None
1855
2408
  loop.call, loop.thought = current.call, current.id
1856
- if break_for_policy:
1857
- self._policy_to_end()
1858
- break
1859
- output, parsed = self._output(reply, used, loop)
1860
- if getattr(self, decisions.OUTPUTS["between"][output])(reply, parsed, loop) == "end":
1861
- break
1862
-
1863
- def _completed_decision(self, thought, kinds):
1864
- """A real request outlives program completion. Replan only if its reply depended on it."""
1865
- if thought.trigger == "ahead":
1866
- return False
1867
- if thought.stopped:
1868
- result = thought.program_result
1869
- event = (result.get("escalation") or {}).get("event", {}).get("kind")
1870
- if result["status"] in ("ok", "finished") or event == "USER_MESSAGE":
1871
- return False
1872
- parsed, prose = decisions._running_reply(thought.reply.text, kinds)
1873
- if prose and thought.trigger == "message":
1874
- return False
1875
- if parsed is not None and parsed.kind != "wait" and not decisions._STOP.search(thought.reply.text):
1876
- if parsed.kind not in ("python", "action") or (parsed.kind == "python" and decisions._beside(parsed.body)):
1877
- return False
1878
- self._settle(thought, "waited", "the decision needs the completed program result")
1879
- self._pending_events.clear()
1880
- if self._carry:
1881
- self._extend_request(self._carry)
1882
- self._carry = []
1883
- return True
2409
+ if policy:
2410
+ return self._policy_to_end()
2411
+ output, parsed = self._output(reply, loop)
2412
+ if ended := getattr(self, decisions.OUTPUTS[output])(reply, parsed, loop):
2413
+ return ended
2414
+
2415
+ def _ended_by_unit(self, loop) -> str | None:
2416
+ """How a unit ended the turn, if one did: the episode ended in it, or it called
2417
+ finish() (which a message or an unresolved event for the model overrides)."""
2418
+ with self._condition:
2419
+ end, self._run_end = self._run_end, None
2420
+ if end is None:
2421
+ return None
2422
+ if end == "episode_ended":
2423
+ return "episode_ended"
2424
+ if self._inbox or self._pending_events:
2425
+ return None
2426
+ return self._out_answer(None, SimpleNamespace(body=end[1], source="program"), loop)
1884
2427
 
1885
- def _output(self, reply, used: dict, loop) -> tuple[str, object]:
1886
- """What a reply between programs does (a key of OUTPUTS) and its parsed block (or the
1887
- error that kept it from parsing); the reply joins the trace and the history."""
1888
- summary, trace, messages = self.summary, self.trace, self.messages
1889
- if (not used and not reply.text.strip() and reply.usage.get("finish_reason") != "length"
2428
+ def _output(self, reply, loop) -> tuple[str, object]:
2429
+ """What a reply does (a key of OUTPUTS) and its parsed block (or the error that kept
2430
+ it from parsing); the reply joins the trace and the history."""
2431
+ trace, messages = self.trace, self.messages
2432
+ if (not reply.text.strip() and reply.usage.get("finish_reason") != "length"
1890
2433
  and loop.empties < decisions.EMPTY_RETRIES):
1891
2434
  return "empty", None
1892
2435
  loop.empties = 0
1893
- # An empty reply is a format error after EMPTY_RETRIES, never a message.
1894
- plain = (self.interactive and reply.usage.get("finish_reason") != "length"
2436
+ # An empty reply is a format error after EMPTY_RETRIES, never an answer.
2437
+ plain = (reply.usage.get("finish_reason") != "length"
1895
2438
  and bool(reply.text.strip()) and not looks_like_action(reply.text))
1896
2439
  # A plain reply that only announces the next step ("I'll open the page ...") is
1897
2440
  # sent back as a format error.
@@ -1905,12 +2448,7 @@ class TaskRun:
1905
2448
  parsed.program = (loop.actions(parsed.body, allow_policy="vla" in loop.skills,
1906
2449
  allow_skills=bool(self.config.runtime.get("fixed_skills", False)))
1907
2450
  if parsed.kind == "action" else parsed.body)
1908
- thought_text = decisions._STOP.sub("", parsed.thought or "")
1909
- assumed = decisions._ASSUME.search(thought_text)
1910
- presentation = {"kind": parsed.kind,
1911
- "thought": (decisions._ASSUME.sub("", thought_text) if assumed else thought_text).strip(),
1912
- "body": parsed.body,
1913
- **({"assume": " ".join(assumed[1].split())} if assumed else {})}
2451
+ presentation = {"kind": parsed.kind, "thought": (parsed.thought or "").strip(), "body": parsed.body}
1914
2452
  output = {"python": "program", "action": "program"}.get(parsed.kind, parsed.kind)
1915
2453
  except (ProtocolError, ValueError) as failed:
1916
2454
  parsed, output = failed, "message" if plain else "invalid"
@@ -1918,11 +2456,10 @@ class TaskRun:
1918
2456
  parsed = ProtocolError("The reply said what it would do, without a block")
1919
2457
  presentation = ({"kind": "message", "body": reply.text.strip(), "announced": True} if announced else
1920
2458
  {"kind": "message" if plain else "invalid", "body": reply.text.strip() if plain else ""})
1921
- self._record_reply(reply, loop.call + 1, thought=loop.thought, presentation=presentation, **used)
2459
+ self._record_reply(reply, loop.call + 1, thought=loop.thought, presentation=presentation)
1922
2460
  messages.append({"role": "assistant", "content": reply.text})
1923
2461
  if output == "invalid":
1924
- if loop.malformed_from is None and not used:
1925
- # (A used reply's exchange stays: its message holds its program's result.)
2462
+ if loop.malformed_from is None:
1926
2463
  loop.malformed_from = len(messages) - 1
1927
2464
  elif loop.malformed_from is not None:
1928
2465
  dropped = len(messages) - 1 - loop.malformed_from
@@ -1932,7 +2469,7 @@ class TaskRun:
1932
2469
  loop.malformed_from = None
1933
2470
  return output, parsed
1934
2471
 
1935
- # The outputs of a reply between programs (OUTPUTS): "end" ends the loop.
2472
+ # The outputs of a reply (OUTPUTS): what one returns is how the turn ended (None: it goes on).
1936
2473
 
1937
2474
  def _out_empty(self, reply, parsed, loop):
1938
2475
  """Reasoning only: asked again at once, with no format error to show."""
@@ -1943,19 +2480,8 @@ class TaskRun:
1943
2480
  self.reason = "empty"
1944
2481
 
1945
2482
  def _current_output(self, loop):
1946
- return (not self._inbox and not self._pending_events and loop.revision == self._revision
1947
- and (loop.ahead_generation is None or loop.ahead_generation == self._ahead_generation))
1948
-
1949
- def _out_message(self, reply, parsed, loop):
1950
- """A plain reply: the model talks to the user (or a sub-agent asks its parent)."""
1951
- with self._condition:
1952
- if not self._current_output(loop):
1953
- return None
1954
- self.question = reply.text.strip()
1955
- # Keep the notification for task/inbox consumers, but identify the reply
1956
- # already recorded by _output so transcript consumers do not show it twice.
1957
- self.trace.emit("agent_message", text=reply.text.strip(), reply_call=loop.call + 1)
1958
- self._await_input()
2483
+ """Whether nothing reached the model since the call whose reply is being done."""
2484
+ return not self._inbox and not self._pending_events and loop.revision == self._revision
1959
2485
 
1960
2486
  def _out_invalid(self, reply, parsed, loop):
1961
2487
  """A reply nothing came of: told why, and asked again."""
@@ -1968,40 +2494,71 @@ class TaskRun:
1968
2494
  who = "the agent that started you" if self.parent is not None else "the user"
1969
2495
  text = decisions.ANNOUNCED.format(block="<action>" if loop.direct else "<python>", who=who)
1970
2496
  else:
1971
- blocks = "<action> or <finish>" if loop.direct else "<python>, <verify>, or <finish>"
2497
+ blocks = "<action> or <wait>" if loop.direct else "<python>, <verify> or <wait>"
1972
2498
  text = (f"Format error: {parsed}. Nothing ran. Reply with the {blocks} block itself, starting with its "
1973
- "opening tag.")
2499
+ "opening tag, or with your answer in plain text.")
1974
2500
  self._tell(text)
1975
2501
  self.trace.emit("protocol_error", message=str(parsed), truncated=reply.usage.get("finish_reason") == "length",
1976
2502
  **({"announced": True} if text.startswith("Nothing ran") else {}))
1977
2503
  self.reason = "format"
1978
2504
 
1979
- def _out_finish(self, reply, parsed, loop):
1980
- """The task is done: the reply's <finish>, or a program's finish() (source "program")."""
2505
+ def _out_answer(self, reply, parsed, loop):
2506
+ """The model's answer: a reply that is text, the reply's <finish>, or a program's
2507
+ finish() (source "program"), all the same. The turn ends with it; the conversation
2508
+ and the units that run go on."""
1981
2509
  source = getattr(parsed, "source", None)
1982
2510
  if source is None:
1983
- if loop.assumption is not None and not self._check_assumption(*loop.assumption):
1984
- self._tell("The lookahead condition changed before the finish could be applied.")
1985
- self.reason = "program_end"
1986
- return None
1987
2511
  with self._condition:
1988
2512
  if not self._current_output(loop):
1989
2513
  return None
2514
+ # (A reply without a block parsed to no block: its text is the answer.)
2515
+ text = reply.text if isinstance(parsed, Exception) else parsed.body
1990
2516
  from harness.runtime.learning.active import is_active
1991
2517
 
1992
- if is_active(self.config.runtime) and self._ahead_allowed():
2518
+ if is_active(self.config.runtime) and self._within_budget():
1993
2519
  before = getattr(self, "_library_version", None)
1994
2520
  self._trial_at_boundary()
1995
2521
  if getattr(self, "_library_version", None) != before:
1996
2522
  self._tell("The host activated your proposal. Use the new revision in a brief practice program, "
1997
- "check what it does, then finish with the result.")
2523
+ "check what it does, then answer with the result.")
1998
2524
  self.reason = "program_end"
1999
2525
  return None
2000
- self.result = parsed.body.strip()
2001
- self.trace.emit("finish", text=parsed.body, **({"source": source} if source else {}))
2002
- self.summary["status"] = "finished"
2003
- _wrap_up(self.kernel, self.trace)
2004
- return "end"
2526
+ self.result = text.strip()
2527
+ if isinstance(parsed, Exception):
2528
+ # (The reply is already recorded by _output: reply_call names it, so that
2529
+ # transcript consumers do not show it twice.)
2530
+ self.trace.emit("agent_message", text=self.result, reply_call=loop.call + 1)
2531
+ else:
2532
+ self.trace.emit("finish", text=text, **({"source": source} if source else {}))
2533
+ return "answer"
2534
+
2535
+ def _out_wait(self, reply, parsed, loop):
2536
+ """The model waits by choice: no call is made until the first of a unit it started
2537
+ ended, a message or an event reached it, or its time (the block's seconds, at most
2538
+ the run's longest wait, which a wait without a time gets). It is told which."""
2539
+ asked = wait_seconds(parsed.body)
2540
+ limit = max(0.0, min(asked if asked is not None else self._longest_wait, self._longest_wait,
2541
+ self.seconds_left()))
2542
+ began = time.monotonic()
2543
+
2544
+ def reached():
2545
+ return ("unit" if self._unit_news or self._run_end is not None else "message" if self._inbox
2546
+ else "event" if self._pending_events else None)
2547
+
2548
+ with self._condition:
2549
+ self.trace.emit("wait", seconds=round(limit, 3), **({"asked": asked} if asked is not None else {}))
2550
+ self._condition.wait_for(lambda: reached() or self._closing or self._pause or self._interrupt,
2551
+ timeout=limit)
2552
+ why = reached() or ("time" if time.monotonic() - began >= limit else "pause")
2553
+ waited = time.monotonic() - began
2554
+ self.trace.emit("wait_end", why=why, seconds=round(waited, 3))
2555
+ running = [unit_text(identifier, unit) for identifier, unit in self._units.items()]
2556
+ # (Last in the message when nothing else is told: the bare end of a wait.)
2557
+ self._say([{"type": "input_text", "text": decisions.WAIT_ENDED.format(
2558
+ seconds=waited, why=decisions.WAIT_ENDS.get(why, "the user paused"))
2559
+ + ("\nUnits running: " + latest(running) if running else "")
2560
+ + "\n" + self._tree_told()}])
2561
+ self.reason = "wait_end"
2005
2562
 
2006
2563
  def _out_verify(self, reply, parsed, loop):
2007
2564
  """The verifier's answer on the reply's claim, with the views it judged."""
@@ -2056,7 +2613,7 @@ class TaskRun:
2056
2613
  """What the environment reports as text for a claim (its session's __evidence__: a
2057
2614
  window's elements, a page's text), read between programs; empty when it has none."""
2058
2615
  kernel = self.kernel
2059
- if kernel is None or self._cell_running:
2616
+ if kernel is None:
2060
2617
  return ""
2061
2618
  try:
2062
2619
  result = kernel.run("__evidence__()", timeout=30)
@@ -2073,13 +2630,8 @@ class TaskRun:
2073
2630
  if self.config.max_cost_usd is not None and (
2074
2631
  self.summary["cost_usd"] + self.agents_usage["cost_usd"] >= self.config.max_cost_usd):
2075
2632
  raise RuntimeError("The session model cost budget is exhausted")
2076
- if origin is not None and role != "observer":
2077
- current = self._request
2078
- if current is not None and current.trigger == "ahead":
2079
- self._settle(current, "discarded", "program requested a model result")
2080
- thought = Thought(role, decisions.TRIGGERS["request"][role], chat, system, messages,
2081
- timeout=max(0, self.seconds_left()), program=self.summary["cells"] if self._cell_running else None,
2082
- revision=self._revision, origin=origin,
2633
+ thought = Thought(role, decisions.REQUESTS[role], chat, system, messages,
2634
+ timeout=max(0, self.seconds_left()), revision=self._revision, origin=origin,
2083
2635
  owner={**self._who().get("who", {}), "entry": request.get("entry"),
2084
2636
  "frame": request.get("frame")})
2085
2637
  self._thoughts.submit(thought)
@@ -2128,8 +2680,9 @@ class TaskRun:
2128
2680
  max_output_tokens=int(observer.get("max_output_tokens", 1200)))
2129
2681
  return ChatModel({"max_output_tokens": 1024, "temperature": 0.0, "extra_body": extra, **config})
2130
2682
 
2131
- def _policy_to_end(self) -> None:
2132
- """The model's calls after the learned policy's run are used up: it runs on to the end."""
2683
+ def _policy_to_end(self) -> str:
2684
+ """The model's calls after the learned policy's run are used up: it runs on to the
2685
+ end, which ends the turn (how: decisions.TURN_ENDS)."""
2133
2686
  trace, summary, kernel = self.trace, self.summary, self.kernel
2134
2687
  with self._span("program", "policy to the end"):
2135
2688
  result = kernel.run("__policy_to_end__()", timeout=self._cell_timeout(policy=True))
@@ -2138,24 +2691,19 @@ class TaskRun:
2138
2691
  if result.get("strategy"):
2139
2692
  summary["strategy"] = result["strategy"]
2140
2693
  if result["status"] == "episode_ended":
2141
- summary["status"] = "episode_ended"
2142
- else:
2143
- trace.emit("finish", text="the model's calls after the policy's run were used up", source="harness")
2144
- summary["status"] = "finished"
2145
- _wrap_up(kernel, trace)
2694
+ return "episode_ended"
2695
+ trace.emit("finish", text="the model's calls after the policy's run were used up", source="harness")
2696
+ return "answer"
2146
2697
 
2147
2698
  def _out_program(self, reply, parsed, loop):
2148
- """Run the reply's program, with the thoughts its triggers start while it runs; then
2149
- what it did is the next thought's news."""
2150
- config, trace, summary, kernel, env = self.config, self.trace, self.summary, self.kernel, self.env
2151
- program, embodiment, cameras = parsed.program, config.embodiment, self.cameras
2699
+ """Start the reply's program as a unit of the model. The turn comes back at once:
2700
+ with the program's result when it was over within the grace, else with its handle;
2701
+ the result then reaches the model when the unit ends (_unit_arrived)."""
2702
+ trace, summary, kernel = self.trace, self.summary, self.kernel
2703
+ program = parsed.program
2152
2704
  if not self._approved(program):
2153
2705
  return None
2154
2706
  self._load_due_skills()
2155
- if loop.assumption is not None and not self._check_assumption(*loop.assumption):
2156
- self._tell("The lookahead condition changed before the program could start.")
2157
- self.reason = "program_end"
2158
- return None
2159
2707
  with self._condition:
2160
2708
  waiting = self._pause or self._closing
2161
2709
  if waiting:
@@ -2167,106 +2715,149 @@ class TaskRun:
2167
2715
  if not self._current_output(loop):
2168
2716
  trace.emit("program_dropped", reason="user_message" if self._inbox else "inputs_changed")
2169
2717
  self._tell("The user paused before this program ran, so nothing ran." if waiting
2170
- else "A user message came before this program ran, so nothing ran.")
2718
+ else "A user message came before this program ran, so nothing ran." if self._inbox
2719
+ else "An event reached you before this program ran, so nothing ran.")
2171
2720
  self.reason = "user"
2172
2721
  return None
2173
2722
  summary["cells"] += 1
2174
- number, policy_ran = summary["cells"], loop.post_from is not None
2175
- self._kinds, self._turns = loop.kinds, loop.turns
2176
- self._request = None
2177
- self._ahead_due = time.monotonic() + decisions.AHEAD_SECONDS if self.ahead != "off" and not policy_ran else None
2178
- self._cell_running = True
2179
- self._budget_stop = self._program_timed_out = False
2180
- self._thoughts.wake()
2181
- if self._pause or self._closing:
2182
- kernel.pause()
2183
- before = time.perf_counter()
2184
- remaining = self._cell_timeout()
2185
- limit = config.runtime.get("cell_timeout_seconds")
2186
- episode_deadline = limit is None or float(limit) >= self.seconds_left()
2187
- deadline = threading.Timer(remaining, self._program_deadline, (number, episode_deadline))
2188
- deadline.daemon = True
2189
- deadline.start()
2723
+ number, span = summary["cells"], uuid.uuid4().hex[:12]
2724
+ started = {"number": number, "code": program, "action": parsed.body if loop.direct else None,
2725
+ "span": span, "unit": None}
2726
+ trace.emit("span_start", id=span, lane="program", name=f"program {number}", parent=None)
2727
+
2728
+ def announce(cell):
2729
+ with self._condition:
2730
+ self._programs[cell] = started
2731
+
2190
2732
  try:
2191
- with self._span("program", f"program {number}") as end:
2192
- result = kernel.run(program, timeout=remaining + 60.0)
2193
- end.update(status=result["status"])
2733
+ result = kernel.start(program, self._grace, timeout=self._cell_timeout(), name=f"program {number}",
2734
+ announce=announce)
2194
2735
  except KernelDied as error:
2195
- summary["kernel_seconds"] += time.perf_counter() - before
2736
+ trace.emit("span_end", id=span, error=f"KernelDied: {error}"[:300])
2196
2737
  trace.emit("kernel_died", message=str(error))
2197
2738
  if self.seconds_left() > 0:
2198
2739
  raise
2199
- summary["status"] = "wall_time_exhausted"
2200
- return "end"
2201
- finally:
2202
- deadline.cancel()
2203
- with self._condition:
2204
- self._cell_running = False
2205
- self._program_completing = True
2206
- self._ahead_due = None
2207
- ahead = self._request
2208
- if ahead is not None and ahead.trigger == "ahead" and not ahead.exited:
2209
- self._settle(ahead, "discarded", "program ended before lookahead returned")
2210
- self._condition.notify_all()
2211
- summary["kernel_seconds"] += time.perf_counter() - before
2212
- if self._program_timed_out and result["status"] not in ("episode_ended", "finished"):
2213
- result = {**result, "status": "error", "error": result.get("error") or "Program deadline exceeded"}
2214
- recorded = {k: v for k, v in result.items() if k not in ("attachments", "report")}
2215
- if result.get("report"):
2216
- recorded["report"] = {"text": result["report"].get("text", ""),
2217
- "images": sorted(result["report"].get("images") or {})}
2218
- trace.emit("cell", code=program, action=parsed.body if loop.direct else None, result=recorded,
2219
- attachments=len(result.get("attachments", [])))
2220
- if self.interactive:
2221
- self._stuck(program, result)
2222
- _cell_usage(summary, result)
2740
+ return "wall_time"
2741
+ if result["status"] != "running":
2742
+ self._unit_arrived(result)
2743
+ return None
2744
+ with self._condition:
2745
+ if started in self._programs.values():
2746
+ # (Else it ended meanwhile: its result is what the model is told.)
2747
+ self._tell(decisions.RUNNING.format(number=number, unit=result["unit"])
2748
+ + f"\nModel calls left: {self.config.max_model_calls - self._iterations}")
2749
+ self.reason = "program_running"
2750
+ return None
2751
+
2752
+ def _unit_arrived(self, result: dict) -> None:
2753
+ """A unit of the model ended: its result came as the reply to its program, or as the
2754
+ kernel's unit_end message (any thread). It is recorded now and told to the model
2755
+ with its next call (_tell_news); an end that is an event (an error, an unhandled
2756
+ event, the episode's end, finish()) sets a call in flight aside."""
2757
+ trace, summary = self.trace, self.summary
2758
+ result = {key: value for key, value in result.items() if key not in ("op", "time")}
2759
+ number = result.get("cell")
2760
+ with self._condition:
2761
+ program = self._programs.pop(number, None) if number is not None else None
2762
+ recorded = {key: value for key, value in result.items() if key not in ("attachments", "report", "looks")}
2763
+ if program is not None:
2764
+ if result.get("report"):
2765
+ recorded["report"] = {"text": result["report"].get("text", ""),
2766
+ "images": sorted(result["report"].get("images") or {})}
2767
+ if result.get("looks"):
2768
+ recorded["looks"] = result["looks"]
2769
+ trace.emit("cell", number=program["number"], code=program["code"], action=program["action"],
2770
+ result=recorded, attachments=len(result.get("attachments", [])))
2771
+ trace.emit("span_end", id=program["span"], status=result["status"])
2772
+ summary["kernel_seconds"] += float(result.get("seconds") or 0.0)
2773
+ result["program"] = program
2774
+ elif number is None:
2775
+ trace.emit("unit_end", **recorded, attachments=len(result.get("attachments", [])))
2776
+ if result.get("quiet"):
2777
+ # The tree of reflexes started it and answers its end itself
2778
+ # (runtime/reactive/tree.py): recorded, and nothing for the model.
2779
+ _cell_usage(summary, result)
2780
+ return
2781
+ else:
2782
+ # A program the host ran itself (wrap-up, a library load): nothing for the model.
2783
+ return
2784
+ _cell_usage(summary, result)
2785
+ if result.get("strategy"):
2786
+ summary["strategy"] = result["strategy"]
2787
+ if self._post_from is None and result["strategy"].get("policy_ran"):
2788
+ self._post_from = summary["model_calls"]
2789
+ status = result["status"]
2790
+ if status == "episode_ended":
2791
+ self._run_end = "episode_ended"
2792
+ elif status == "finished" and self._run_end is None:
2793
+ self._run_end = ("finished", str(result.get("finish") or ""))
2794
+ self._unit_news.append(result)
2795
+ if status not in ("ok", "cancelled") and not result.get("handled"):
2796
+ self._revision += 1
2797
+ self._preempt("a unit ended with an event")
2798
+ self._condition.notify_all()
2223
2799
  self.charge_up()
2224
- if result.get("strategy"):
2225
- summary["strategy"] = result["strategy"]
2226
- if not policy_ran and result["strategy"].get("policy_ran"):
2227
- loop.post_from = summary["model_calls"]
2228
- if result["status"] == "episode_ended":
2229
- summary["status"] = "episode_ended"
2230
- return "end"
2231
- if self._wall_up:
2232
- # (Its USER_PAUSED was the wall time's.)
2233
- self._wall_up = False
2234
- summary["status"] = "wall_time_exhausted"
2235
- return "end"
2236
- finished = False
2800
+
2801
+ def _tell_news(self, loop) -> None:
2802
+ """Tell the model what its units ended with since its last call: each program's
2803
+ feedback (status, output, events, state), each other unit's end, what they showed
2804
+ and looked at, the units still running and the calls left."""
2237
2805
  with self._condition:
2238
- if result["status"] == "finished":
2239
- self.reason = "program_end"
2240
- if ahead is not None and ahead.trigger == "ahead":
2241
- self._settle(ahead, "discarded", "the program finished the task")
2242
- self._request = ahead = None
2243
- if ahead is None and not self._inbox and not self._pending_events:
2244
- self._program_completing = False
2245
- finished = True
2806
+ news, self._unit_news = self._unit_news, []
2807
+ if not news:
2808
+ return
2809
+ config, trace, embodiment, cameras = self.config, self.trace, self.config.embodiment, self.cameras
2810
+ order = ("unit_end", "program_end", "escalation", "error")
2811
+ content, looks, outlines, reason, taken, state, programs = [], [], {}, "unit_end", None, None, 0
2812
+ for result in news:
2813
+ program, status = result.get("program"), result["status"]
2814
+ if program is not None:
2815
+ programs += 1
2816
+ if self.interactive:
2817
+ self._stuck(program["code"], result)
2818
+ taken = taken or _taken_over(result)
2819
+ why = {"escalated": "escalation", "error": "error"}.get(status, "program_end")
2820
+ text = cell_feedback(result, program["number"], self.profile.get("control_hz"), embodiment,
2821
+ self._steps_shown())
2822
+ if why == "program_end":
2823
+ text += decisions.PROGRAM_ENDED
2824
+ state = result.get("state") or state
2246
2825
  else:
2247
- self.reason = {"escalated": "escalation", "error": "error"}.get(result["status"], "program_end")
2248
- taken = _taken_over(result) if self.interactive else None
2826
+ why = "escalation" if status in ("error", "escalated") and not result.get("handled") else "unit_end"
2827
+ text = unit_feedback(result)
2828
+ reason = max(reason, why, key=order.index)
2829
+ content.append({"type": "input_text", "text": text, "result": True})
2830
+ for item in result.get("attachments", [])[:6]:
2831
+ content.append({"type": "input_text", "text": f"show(): {item['caption']}"})
2832
+ content.append({"type": "input_image", "image_url": item["image_url"], "detail": "auto"})
2833
+ # A skill's report (the tracked scene) may outline objects in the same views.
2834
+ outlined = (result.get("report") or {}).get("images") or {}
2835
+ outlines.update(outlined)
2836
+ seen = [{**item, "image_url": outlined[item["camera"]]}
2837
+ if item["camera"] in outlined and not item.get("region") else item
2838
+ for item in result.get("looks") or ()]
2839
+ if seen and not self._standing:
2840
+ trace.emit("views", action="looked", program=(program or {}).get("number"), unit=result.get("unit"),
2841
+ views=[{key: item[key] for key in ("camera", "region", "pin") if key in item}
2842
+ for item in seen])
2843
+ looks += seen
2844
+ self.reason = reason
2845
+ with self._condition:
2249
2846
  calls_left = config.max_model_calls - self._iterations
2250
- if finished:
2251
- return self._out_finish(None, SimpleNamespace(body=str(result.get("finish") or ""),
2252
- source="program"), loop)
2253
- outcome, why = self._program_outcome(ahead, loop.kinds, result, taken)
2254
- note = ("\nThe program ended without calling finish(). If the task is done, finish; otherwise "
2255
- "write a program for the rest of the task." if self.reason == "program_end" and outcome != "used"
2256
- else "")
2257
- if loop.post_from is not None and loop.post_calls:
2258
- calls_left = min(calls_left, loop.post_calls - (summary["model_calls"] - loop.post_from))
2259
- note += ("\nAfter these calls the learned policy runs on by itself to the end of the episode."
2260
- if calls_left > 0 else "")
2261
- feedback = cell_feedback(result, number, self.profile.get("control_hz"), embodiment, self._steps_shown())
2262
- if ahead is not None and ahead.stopped:
2263
- feedback = f"Program {ahead.program} stopped for your decision.\n" + feedback
2264
- content = [{"type": "input_text", "text": feedback + note
2265
- + f"\nModel calls left: {calls_left}"}]
2266
- for item in result.get("attachments", [])[:6]:
2267
- content.append({"type": "input_text", "text": f"show(): {item['caption']}"})
2268
- content.append({"type": "input_image", "image_url": item["image_url"], "detail": "auto"})
2269
- if not taken and self.reason in ("error", "escalation") and not self._request:
2847
+ running = [unit_text(identifier, unit) for identifier, unit in self._units.items()]
2848
+ tree = self._tree_told()
2849
+ note = ""
2850
+ if self._post_from is not None and loop.post_calls:
2851
+ calls_left = min(calls_left, loop.post_calls - (self.summary["model_calls"] - self._post_from))
2852
+ note = ("After these calls the learned policy runs on by itself to the end of the episode.\n"
2853
+ if calls_left > 0 else "")
2854
+ # What was learned so far, where this run learns: whole when it changed, else its first line.
2855
+ head, _, body = self._learning_report().strip().partition("\n")
2856
+ learned = (head + ("\n" + body if body != self._learning_told else " as you were last told") + "\n") if head else ""
2857
+ self._learning_told = body
2858
+ content.append({"type": "input_text", "text": ("Units running: " + latest(running) + "\n" if running else "")
2859
+ + (tree + "\n" if tree else "") + learned + note + f"Model calls left: {calls_left}"})
2860
+ if not taken and reason in ("error", "escalation"):
2270
2861
  self._reflect_learning()
2271
2862
  if taken is not None:
2272
2863
  # The user brought a window of theirs to the front: the computer is theirs
@@ -2275,164 +2866,70 @@ class TaskRun:
2275
2866
  self.pause()
2276
2867
  self._checkpoint()
2277
2868
  if self._standing:
2278
- with self._span("env", "observe"):
2279
- view = env.call("observe", episode_id=self.episode, cameras=cameras)
2280
- # A skill's report (the tracked scene) may outline objects in the same views.
2281
- images = {**view["images"], **((result.get("report") or {}).get("images") or {})}
2282
- content.extend(image_parts(images, cameras))
2869
+ if programs:
2870
+ with self._span("env", "observe"):
2871
+ view = self.env.call("observe", episode_id=self.episode, cameras=cameras)
2872
+ content.extend(image_parts({**view["images"], **outlines}, cameras))
2873
+ state = view["state"]
2283
2874
  else:
2284
- # Only what the program looked at (look, a standing watch of the model's own).
2285
- view = {"state": result.get("state") or {}}
2286
- looks = list(result.get("looks") or ())
2287
- outlined = (result.get("report") or {}).get("images") or {}
2288
- looks = [{**item, "image_url": outlined[item["camera"]]}
2289
- if item["camera"] in outlined and not item.get("region") else item for item in looks]
2875
+ # Only what the units looked at (look, a standing watch of the model's own).
2290
2876
  with self._condition:
2291
2877
  self._view_pins |= {item["pin"] for item in looks if item.get("pin")}
2292
- if looks:
2293
- trace.emit("views", action="looked", program=number,
2294
- views=[{key: item[key] for key in ("camera", "region", "pin") if key in item}
2295
- for item in looks])
2296
2878
  content.extend(view_parts(looks) if self._images else self._described(looks))
2297
2879
  with self._condition:
2298
- self._program_completing = False
2299
- if ahead is not None and (ahead.cancelled or ahead.revision != self._revision) and outcome == "used":
2300
- outcome, why = "discarded", "inputs changed before adoption"
2301
- if outcome in ("used", "pending") and not ahead.cancelled:
2302
- self._carry = content
2303
- else:
2304
- if ahead is not None and outcome in ("waited", "discarded"):
2305
- self._settle(ahead, outcome, why)
2306
- if ahead is not None and ahead.said:
2307
- self.messages.append({"role": "assistant", "content": ahead.reply.text})
2308
- if ahead is not None and ahead.side is not None:
2309
- self.messages.append({"role": "assistant", "content": ahead.reply.text})
2310
- content.insert(0, {"type": "input_text", "text": self._beside_report(ahead)})
2311
- self._request = None
2312
- self._tell(content)
2313
- self._condition.notify_all()
2314
- if view["state"].get("ended"):
2315
- summary["status"] = "episode_ended"
2316
- return "end"
2317
- return None
2318
-
2319
- def _program_outcome(self, thought, kinds, result, taken):
2320
- if thought is None:
2321
- return None, ""
2322
- if thought.side is not None or thought.said:
2323
- return "beside", ""
2324
- if thought.cancelled or taken is not None:
2325
- return "discarded", "the decision was superseded"
2326
- if thought.trigger != "ahead":
2327
- thought.program_result = result
2328
- return "pending", ""
2329
- if result["status"] != "ok" or not thought.exited or thought.error is not None:
2330
- return "discarded", "lookahead needs a normal completion and a ready reply"
2331
- reply = thought.reply
2332
- if reply is None or reply.usage.get("finish_reason") == "length":
2333
- return "discarded", "lookahead returned no complete reply"
2334
- parsed, _ = decisions._running_reply(reply.text, kinds)
2335
- if parsed is None or parsed.kind == "wait":
2336
- return "waited", "lookahead deferred to the program result"
2337
- condition = decisions._ASSUME.search(reply.text)
2338
- if condition is None or decisions._STOP.search(reply.text):
2339
- return "discarded", "lookahead requires a checkable completion condition"
2340
- thought.assumption = (condition[1].strip(), result)
2341
- if not self._check_assumption(*thought.assumption):
2342
- return "discarded", "lookahead condition was false or could not be checked"
2343
- return "used", ""
2344
-
2345
- def _check_assumption(self, condition, result):
2346
- """Evaluate a bounded expression over JSON data, never the program namespace."""
2347
- try:
2348
- tree = ast.parse(condition, mode="eval")
2349
- nodes = list(ast.walk(tree))
2350
- if len(nodes) > 64 or not all(isinstance(node, decisions._ASSUME_NODES) for node in nodes):
2351
- return False
2352
- names = {node.id for node in nodes if isinstance(node, ast.Name)}
2353
- if not names <= {"result", "output", "state"} or not names:
2354
- return False
2355
- snapshot = json.loads(json.dumps({key: value for key, value in result.items()
2356
- if key not in ("attachments", "report")}))
2357
- state = (self.env.call("state", episode_id=self.episode) if "state" in names else None)
2358
- values = {"result": snapshot, "output": snapshot.get("stdout") or "", "state": state}
2359
- return bool(eval(compile(tree, "<assume>", "eval"), {"__builtins__": {}}, values))
2360
- except Exception:
2361
- return False
2362
-
2363
- def _think(self, phase, turns, *, learning_report=""):
2364
- """Select a decision from the table and queue its immutable input."""
2365
- triggers = decisions.TRIGGERS[phase]
2366
- now = time.monotonic()
2367
- trigger = next((name for name, check in triggers.items() if check and getattr(self, check)(now)), None)
2368
- running = phase == "running"
2880
+ self._say(content)
2881
+ if (state or {}).get("ended"):
2882
+ self._run_end = "episode_ended"
2883
+
2884
+ def _think(self):
2885
+ """Queue the model's next call with its immutable input, on the trigger of what is
2886
+ pending (a message, an unresolved event), else of what happened last (self.reason)."""
2887
+ triggers = decisions.TRIGGERS
2888
+ trigger = next((name for name, check in triggers.items() if check and getattr(self, check)()), None)
2369
2889
  if trigger is None:
2370
- if running:
2371
- return None
2372
2890
  trigger = self.reason
2373
2891
  if trigger not in triggers:
2374
2892
  raise ValueError(f"No model trigger for {trigger!r}")
2375
-
2376
- number = self.summary["cells"] if running else None
2377
- earlier = self._request
2378
- if running and earlier is not None and (earlier.said or earlier.side is not None):
2379
- self.messages.append({"role": "assistant", "content": earlier.reply.text})
2380
- if earlier.side is not None:
2381
- self._tell(self._beside_report(earlier))
2382
2893
  self._include_escalations()
2383
2894
  self._add_user_messages()
2384
- if running:
2385
- watched = []
2386
- if trigger == "ahead":
2387
- self._ahead_due = None
2388
- text = decisions.AHEAD_RUNNING.format(number=number)
2389
- elif trigger == "watch":
2390
- watched, self._watched = self._watched or [], None
2391
- text = decisions.WATCH_RUNNING.format(number=number)
2392
- else:
2393
- text = decisions.LOOK_RUNNING.format(number=number)
2394
- if trigger == "message":
2395
- text += " You may also answer the user in plain text."
2396
- parts = [{"type": "input_text", "text": text},
2397
- *(view_parts(watched) if self._images else self._described(watched) if watched else [])]
2398
- if self.messages[-1]["role"] == "user":
2399
- self._extend_request(parts)
2400
- else:
2401
- self._tell(parts)
2402
-
2403
2895
  config, summary, messages = self.config, self.summary, self.messages
2404
2896
  if self._standing:
2405
2897
  prune_images(messages, int(config.runtime.get("keep_images", 2)))
2406
2898
  else:
2407
2899
  prune_views(messages, self._view_keep, self._view_pins)
2408
- request = self._with_what_was_said(window(messages, turns), messages)
2409
- if not self._images:
2410
- request = text_only(request)
2900
+ idle = time.time() - self._replied_at if self._replied_at else 0.0
2901
+ after = float(config.runtime.get("clear_results_after_minutes") or 0) * 60
2902
+ if after and idle >= after:
2903
+ # The provider's cache of the conversation is cold: this call reads all of it anew.
2904
+ cleared = clear_results(messages, decisions.KEPT_RESULTS, decisions.CLEARED)
2905
+ if cleared:
2906
+ self._replaces, self._logged = self._replaces + self._logged, 0
2907
+ self.trace.emit("results_cleared", results=cleared, idle_seconds=round(idle))
2908
+ request = messages if self._images else text_only(messages)
2411
2909
  call = summary["model_calls"]
2412
2910
  from copy import deepcopy
2413
2911
  history = deepcopy(messages)
2414
- system = config.system_prompt + learning_report
2912
+ system = config.system_prompt
2415
2913
  thought = Thought("main", trigger, self.chat, system, request,
2416
- timeout=max(0, self.seconds_left()), program=number, owner=self._who().get("who"),
2417
- revision=self._revision, call=call, turns=turns,
2418
- invoke=lambda item: self._complete(item, history))
2419
- thought.ahead_generation = self._ahead_generation
2914
+ timeout=max(0, self.seconds_left()), owner=self._who().get("who"),
2915
+ revision=self._revision, call=call, invoke=self._complete)
2420
2916
  self.trace.emit("model_request", call=call, thought=thought.id, **decisions._model_of(thought.chat), reason=trigger,
2421
- program=number, total_messages=len(messages), sent_messages=len(request),
2917
+ total_messages=len(messages), sent_messages=len(request),
2422
2918
  new_messages=history[self._logged:], context=context_summary(request, system),
2423
2919
  library_version=getattr(self, "_library_version", None), revision=thought.revision,
2424
- events=list(self._pending_events),
2425
- **({"message": True} if trigger == "message" else {}), **self._replaced())
2920
+ events=list(self._pending_events), **self._replaced())
2426
2921
  self._logged = len(messages)
2427
2922
  summary["model_calls"] += 1
2428
2923
  summary["decisions"][trigger] = summary["decisions"].get(trigger, 0) + 1
2429
2924
  self._iterations += 1
2925
+ self._save()
2430
2926
  self._request = thought
2431
2927
  self._thoughts.submit(thought)
2432
2928
  return thought
2433
2929
 
2434
- def _ahead_allowed(self, reserve=0):
2435
- return (self._iterations + reserve < self.config.max_model_calls
2930
+ def _within_budget(self):
2931
+ """Whether the model may be called again: calls and cost left."""
2932
+ return (self._iterations < self.config.max_model_calls
2436
2933
  and (self.config.max_cost_usd is None
2437
2934
  or self.summary["cost_usd"] + self.agents_usage["cost_usd"] < self.config.max_cost_usd))
2438
2935
 
@@ -2453,51 +2950,19 @@ class TaskRun:
2453
2950
  self._reported_events.add(identifier)
2454
2951
  parts.append({"type": "input_text", "text": f"Unresolved event: {json.dumps(report)}"})
2455
2952
  if parts:
2456
- if self.messages[-1]["role"] == "user":
2457
- self._extend_request(parts)
2458
- else:
2459
- self._tell(parts)
2953
+ self._say(parts)
2460
2954
 
2461
- def _pending_message(self, now):
2955
+ def _pending_message(self):
2462
2956
  return bool(self._inbox)
2463
2957
 
2464
- def _unresolved(self, now):
2958
+ def _unresolved(self):
2465
2959
  return bool(self._pending_events)
2466
2960
 
2467
- def _pending_escalation(self, now):
2468
- return bool(self._pending_events) and not self._pending_events.keys() <= self._reported_events
2469
-
2470
- def _pending_watch(self, now):
2471
- return (self._watched is not None and self._request is None and self._ahead_allowed(reserve=1)
2472
- and not self._thoughts.pending)
2473
-
2474
- def _pending_ahead(self, now):
2475
- return (self._ahead_due is not None and self._ahead_due <= now and self._request is None
2476
- and self._ahead_allowed(reserve=1) and not self._thoughts.pending)
2477
-
2478
- def _decision_tick(self):
2479
- """Coalesce what is visible at this tick before acquiring a transport."""
2480
- if not self._cell_running or self._program_completing or self._closing or self._pause:
2481
- return None
2482
- if not self._ahead_allowed():
2483
- if self._inbox and not self._budget_stop:
2484
- self._budget_stop = True
2485
- self._stop_program_for_decision()
2486
- return None
2487
- if self._thoughts.active is not None:
2488
- return None
2489
- now = time.monotonic()
2490
- if self._think("running", self._turns) is not None:
2491
- return None
2492
- if self._ahead_due is not None and self._ahead_due > now:
2493
- return self._ahead_due
2494
- return None
2495
-
2496
2961
  def _came_back(self, thought):
2497
2962
  from harness.runtime.decision.model import add_usage
2498
2963
 
2499
2964
  summary = self.summary
2500
- key = decisions.OUTPUTS["request"].get(thought.role)
2965
+ key = decisions.COUNTS.get(thought.role)
2501
2966
  if key is not None:
2502
2967
  summary[key] = summary.get(key, 0) + 1
2503
2968
  summary["agent_seconds"] += thought.seconds
@@ -2507,108 +2972,8 @@ class TaskRun:
2507
2972
  if key == "vision_calls":
2508
2973
  summary["vision_cost_usd"] += float(usage.get("cost_usd") or 0.0)
2509
2974
  self.charge_up()
2510
- if key is not None:
2511
- return
2512
- if thought.outcome is not None:
2975
+ if key is None and thought.outcome is not None:
2513
2976
  self._record_settlement(thought)
2514
- elif (self._cell_running and not self._program_completing and self._request is thought
2515
- and thought.revision == self._revision
2516
- and not self._inbox and not self._pause):
2517
- self._dispatch_running_reply(thought)
2518
-
2519
- def _dispatch_running_reply(self, thought):
2520
- if thought.cancelled or thought.trigger == "ahead" or thought.reply is None:
2521
- return
2522
- text = thought.reply.text
2523
- if thought.reply.usage.get("finish_reason") == "length":
2524
- return
2525
- parsed, prose = decisions._running_reply(text, self._kinds)
2526
- kind = getattr(parsed, "kind", None)
2527
- stop = bool(decisions._STOP.search(text))
2528
- if prose and thought.trigger == "message":
2529
- output = "message"
2530
- elif kind == "python" and not stop and self.confirmation != "always" and decisions._beside(parsed.body):
2531
- output = "parallel"
2532
- elif kind not in (None, "wait") and (stop or thought.trigger == "message"):
2533
- output = "replace"
2534
- else:
2535
- output = "wait"
2536
- handler = decisions.OUTPUTS["running"][output]
2537
- if handler is not None:
2538
- if output == "parallel":
2539
- self._run_beside(thought, parsed.body)
2540
- else:
2541
- getattr(self, handler)(thought)
2542
-
2543
- def _interrupt(self, thought: Thought) -> None:
2544
- """Stop the running program at its next step for `thought`'s reply, which runs next
2545
- (the loop uses it when the program ends on the stop); a failed thought made for a
2546
- message stops it too: the model reads the message once it has."""
2547
- if thought.trigger in ("escalation", "watch"):
2548
- found = decisions._STOP.search(thought.reply.text)
2549
- why = " ".join(found.group(1).split()) if found else ""
2550
- why = f"a look: {why}"[:300] if why else "a look"
2551
- else:
2552
- why = "the call failed" if thought.error is not None else "the reply to a message"
2553
- with self._condition:
2554
- if self.kernel is None or not self._cell_running or self._request is not thought:
2555
- return
2556
- thought.stopped = thought.error is None
2557
- self.trace.emit("program_stopped", program=thought.program, call=thought.call + 1, why=why)
2558
- self._stop_program_for_decision()
2559
-
2560
- def _stop_program_for_decision(self):
2561
- self._interruptions += 1
2562
- if self.agents is not None:
2563
- self.agents.wake()
2564
- self.kernel.message()
2565
-
2566
- def _say_beside(self, thought: Thought) -> bool:
2567
- """A reply in prose to a message that came while a program runs: it reaches the user
2568
- at once (an agent_message), the program going on, and joins the history when the
2569
- program ends; False when the program has ended."""
2570
- with self._condition:
2571
- if not (self._cell_running and self._request is thought):
2572
- return False
2573
- thought.said = True
2574
- self._settle(thought, "said", "told the user while the program ran")
2575
- self._pending_events.clear()
2576
- self.question = thought.reply.text.strip()
2577
- self.trace.emit("agent_message", text=self.question, program=thought.program)
2578
- return True
2579
-
2580
- def _beside_report(self, ahead: Thought) -> str:
2581
- """What a block run beside the program did, for the model."""
2582
- side = ahead.side["result"] or {"status": "still running"}
2583
- return (f"The program you ran beside program {ahead.program}: {side.get('status')}"
2584
- + (f"\n{side['error']}" if side.get("error") else "")
2585
- + (f"\nEvent: {side['escalation']['event']['kind']}" if side.get("escalation") else ""))
2586
-
2587
- def _run_beside(self, ahead: Thought, program: str) -> bool:
2588
- """Start a block that can run beside the program."""
2589
- identifier = uuid.uuid4().hex[:12]
2590
- with self._condition:
2591
- kernel = self.kernel if self._cell_running and self._request is ahead else None
2592
- if kernel is None or not kernel.side(program, identifier):
2593
- return False
2594
- ahead.side = {"id": identifier, "code": program, "result": None}
2595
- self._sides[identifier] = ahead
2596
- self.trace.emit("side_program", id=identifier, program=ahead.program, call=ahead.call + 1,
2597
- code=program)
2598
- self._settle(ahead, "beside", "ran beside the program")
2599
- self._pending_events.clear()
2600
- return True
2601
-
2602
- def _side_ended(self, record: dict) -> None:
2603
- """How a program run beside the running one ended (the kernel's side_result)."""
2604
- with self._condition:
2605
- ahead = self._sides.pop(record.get("id"), None)
2606
- result = {key: record.get(key) for key in ("status", "error", "escalation") if record.get(key) is not None}
2607
- self.trace.emit("side_program_end", id=record.get("id"), **result)
2608
- if ahead is not None:
2609
- ahead.side["result"] = result
2610
- if result.get("status") in ("error", "escalated"):
2611
- self._escalation({"id": record["id"], "entry": record["id"], "event": result})
2612
2977
 
2613
2978
  def _replaced(self) -> dict:
2614
2979
  """What the next request record says of the history messages it replaces."""
@@ -2616,10 +2981,12 @@ class TaskRun:
2616
2981
  return {"replaces": replaces} if replaces else {}
2617
2982
 
2618
2983
  def _settle(self, thought, outcome, why=""):
2984
+ """What became of a call of the model: used, or set aside (discarded: something
2985
+ reached the model first; unused: the run ended first)."""
2619
2986
  if thought.outcome is not None:
2620
2987
  return
2621
2988
  thought.outcome = outcome, why
2622
- if outcome in ("discarded", "unused"):
2989
+ if outcome != "used":
2623
2990
  if thought.exited:
2624
2991
  thought.cancel(why)
2625
2992
  self.trace.emit("thought_cancelled", thought=thought.id, role=thought.role,
@@ -2631,53 +2998,31 @@ class TaskRun:
2631
2998
 
2632
2999
  def _record_settlement(self, thought):
2633
3000
  outcome, why = thought.outcome
2634
- summary, reply = self.summary, thought.reply
2635
- if thought.program is not None:
2636
- outcomes = summary.setdefault("ahead_outcomes", {})
2637
- outcomes[outcome] = outcomes.get(outcome, 0) + 1
2638
- self.trace.emit("ahead", thought=thought.id, call=thought.call + 1, program=thought.program,
2639
- trigger=thought.trigger, outcome=outcome, why=why,
2640
- seconds=round(thought.seconds, 3), cancelled=thought.cancelled)
2641
- if thought.program is None and outcome != "used":
3001
+ reply = thought.reply
3002
+ if outcome != "used":
2642
3003
  self.trace.emit("model_preempted", thought=thought.id, call=thought.call + 1, reason=why,
2643
3004
  back=reply is not None, **({"text": reply.text, "usage": reply.usage} if reply else {}))
2644
- elif reply is not None and outcome != "used":
2645
- self._record_reply(reply, thought.call + 1, thought=thought.id, ahead=outcome)
2646
3005
 
2647
3006
  def _record_reply(self, reply, call, **detail):
2648
3007
  self.trace.emit("model_reply", call=call, text=reply.text, usage=reply.usage, response_id=reply.response_id,
2649
3008
  seconds=reply.seconds, **detail, **({"reasoning": reply.reasoning} if reply.reasoning else {}))
2650
3009
 
2651
- def _complete(self, thought, history):
2652
- """The main transport, including bounded context shortening within one Thought."""
3010
+ def _complete(self, thought):
3011
+ """The main transport."""
2653
3012
  from harness.runtime.decision.model import ChatModel
2654
3013
 
2655
- request, chat, turns = thought.messages, thought.chat, thought.turns
3014
+ chat = thought.chat
2656
3015
  options = {"on_text": self._streamed(thought.call + 1, stop=lambda: thought.cancelled)} if getattr(
2657
3016
  chat, "streams_text", False) else {}
2658
3017
  if isinstance(chat, ChatModel):
2659
3018
  options["cancellation"] = thought.cancellation
2660
- attempt = 0
2661
3019
  name = decisions._model_of(chat).get("model") or str(self.config.main.get("model", "model"))
2662
3020
  with self._span("model", name, call=thought.call + 1,
2663
- thought=thought.id, role="main", reason=thought.trigger, program=thought.program) as end:
2664
- while True:
2665
- thought.cancellation.check()
2666
- attempt += 1
2667
- try:
2668
- reply = chat.complete(thought.system, request, **options)
2669
- end.update(output_tokens=reply.usage.get("output_tokens"), attempts=attempt)
2670
- thought.turns = turns
2671
- return reply
2672
- except RuntimeError as error:
2673
- if "out of GPU memory" not in str(error) or len(request) <= 2:
2674
- raise
2675
- turns = max(1, (turns or (len(history) - 1) // 2) - 1)
2676
- shorter = window(history, turns)
2677
- if len(shorter) >= len(request):
2678
- raise
2679
- self.trace.emit("context_shortened", history_turns=turns, sent_messages=len(shorter))
2680
- request = shorter
3021
+ thought=thought.id, role="main", reason=thought.trigger) as end:
3022
+ thought.cancellation.check()
3023
+ reply = chat.complete(thought.system, thought.messages, **options)
3024
+ end.update(output_tokens=reply.usage.get("output_tokens"))
3025
+ return reply
2681
3026
 
2682
3027
  def _streamed(self, call, every=decisions.STREAMED_SECONDS, stop=None):
2683
3028
  """What gets the pieces of a streamed reply of call `call`: model_text records of
@@ -2741,7 +3086,7 @@ class TaskRun:
2741
3086
  or cells <= self._reflected_cells or self.seconds_left() <= 0):
2742
3087
  return
2743
3088
  with self._condition:
2744
- if self._pause or self._closing or self._inbox or self._sides:
3089
+ if self._pause or self._closing or self._inbox or self._busy():
2745
3090
  return
2746
3091
  revision = self._revision
2747
3092
  since = self._reflected_cells
@@ -2765,7 +3110,6 @@ class TaskRun:
2765
3110
 
2766
3111
  self.config = copy(self.config)
2767
3112
  self.config.system_prompt += f"\nObservation from this run: {found.text.strip()}\n"
2768
- self.invalidate_lookahead()
2769
3113
  except ThoughtCancelled:
2770
3114
  pass
2771
3115
  except Exception as error:
@@ -2780,8 +3124,9 @@ class TaskRun:
2780
3124
  and "code -15" in summary["error"])
2781
3125
 
2782
3126
  def _close_episode(self, token) -> None:
2783
- """End its sub-agents, close the kernel, read the evaluator's outcome with the token,
2784
- write the summary."""
3127
+ """The conversation ends: end its sub-agents, close the kernel, read the evaluator's
3128
+ outcome with the token (a worker evaluates once: at the end), and give the result
3129
+ of the turn that ran (none ran when it ends between turns)."""
2785
3130
  trace, summary, directory, env = self.trace, self.summary, self.directory, self.env
2786
3131
  with self._condition:
2787
3132
  self._cancel_managed_worker_approvals("the episode ended")
@@ -2797,6 +3142,18 @@ class TaskRun:
2797
3142
  trace.emit("cleanup_error", message=f"kernel: {type(error).__name__}: {error}")
2798
3143
  if self.environments is not None:
2799
3144
  self.environments.close()
3145
+ for model in (self.chat, self.verifier):
3146
+ # A package's backend holds a process of its own (an ACP agent).
3147
+ if getattr(model, "packaged", False):
3148
+ try:
3149
+ model.close()
3150
+ except Exception as error:
3151
+ trace.emit("cleanup_error", message=f"model: {type(error).__name__}: {error}")
3152
+ if self._inbound is not None:
3153
+ try:
3154
+ self._inbound.close()
3155
+ except Exception as error:
3156
+ trace.emit("cleanup_error", message=f"listener: {type(error).__name__}: {error}")
2800
3157
  if hasattr(self, "_mcp_access"):
2801
3158
  try:
2802
3159
  self._mcp_access.close()
@@ -2836,9 +3193,15 @@ class TaskRun:
2836
3193
  except Exception as error:
2837
3194
  trace.emit("cleanup_error", message=f"outcome: {type(error).__name__}: {error}")
2838
3195
  learning = self._learning()
2839
- if self.kernel is not None:
2840
- self._reflect_learning()
2841
3196
  table = self.config.runtime.get("library") or {}
3197
+ if self._turn_began is not None or not self._turns:
3198
+ # The turn that ran (or a conversation that had none) ends with the conversation.
3199
+ ending = summary["status"]
3200
+ self._turn_result(next((reason for reason, status in decisions.TURN_ENDS.items() if status == ending),
3201
+ ending))
3202
+ else:
3203
+ # Ended between turns: the last turn gave its result; the summary is closed.
3204
+ self._write_summary()
2842
3205
  uses = table.get("uses", {"learn": "on", "off": "off"}.get(table.get("experience"), "learn"))
2843
3206
  self._thoughts.close()
2844
3207
  with self._condition:
@@ -2849,14 +3212,16 @@ class TaskRun:
2849
3212
  # What this episode's closed loops of library skills did, as their uses in the
2850
3213
  # library's index (runtime/learning/usage.py fold_uses).
2851
3214
  try:
2852
- trace.emit("library", action="used", uses=fold_uses(self._learner(), directory))
3215
+ learner = self._learner()
3216
+ trace.emit("library", action="used", uses=fold_uses(learner, directory))
3217
+ for entry in learner.demoted:
3218
+ # Admitted by its own test, it kept failing in use (learner.record_uses).
3219
+ trace.emit("library", action="demoted", **entry)
3220
+ learner.demoted.clear()
2853
3221
  except Exception as error:
2854
3222
  trace.emit("cleanup_error", message=f"library uses: {type(error).__name__}: {error}")
2855
- summary["wall_seconds"] = time.perf_counter() - self.started
2856
- if self.agents_usage["started"] or self.agents_usage["model_calls"]:
2857
- summary["agents"] = {**self.agents_usage, "cost_usd": round(self.agents_usage["cost_usd"], 6)}
2858
- write_json(directory / "summary.json", summary)
2859
- self.charge_up()
3223
+ if "model_transport_pending" in summary:
3224
+ write_json(directory / "summary.json", summary)
2860
3225
  self._set_status(ERROR if summary["status"] == "error" else FINISHED, reason=summary["status"])
2861
3226
 
2862
3227
 
@@ -2865,6 +3230,7 @@ VERIFIER_EVIDENCE = (" Besides any pictures, the message gives the state and wha
2865
3230
  "(a window's elements, a page's text): judge on all of it, and answer NO when together it "
2866
3231
  "does not show enough to confirm the claim.")
2867
3232
  # What the vision role is asked of a view, for a main model that reads no images.
3233
+ SEARCHER = "Search the web for what the user names and list what you found, one line for each page."
2868
3234
  VIEWER = ("You describe a view for an agent that cannot see it. Say what is shown: the windows or objects, the "
2869
3235
  "text and numbers exactly as written, the controls and where each is (pixel coordinates from the top "
2870
3236
  "left). Facts only, no advice.")