@lmmslab/harness 0.81.0 → 0.82.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -35
- package/package.json +2 -2
- package/python/harness/README.md +3 -3
- package/python/harness/__init__.py +1 -1
- package/python/harness/builtin/configs/assistant.toml +0 -1
- package/python/harness/builtin/configs/default.toml +1 -1
- package/python/harness/builtin/prompts/conversation/core.md +2 -2
- package/python/harness/builtin/prompts/core/core.md +177 -41
- package/python/harness/builtin/prompts/core/pack.toml +2 -2
- package/python/harness/builtin/prompts/reflect/core.md +1 -1
- package/python/harness/builtin/prompts/shared/learning-goal.md +5 -29
- package/python/harness/builtin/prompts/shared/learning.md +8 -10
- package/python/harness/builtin/prompts/shared/web.md +11 -0
- package/python/harness/builtin/skills/agents/__init__.py +24 -22
- package/python/harness/builtin/skills/entries/__init__.py +43 -28
- package/python/harness/builtin/skills/entries/skill.toml +2 -3
- package/python/harness/builtin/skills/environments/__init__.py +2 -9
- package/python/harness/builtin/skills/mcp/__init__.py +2 -3
- package/python/harness/builtin/skills/memory/__init__.py +2 -5
- package/python/harness/builtin/skills/web/__init__.py +92 -0
- package/python/harness/builtin/skills/web/skill.toml +9 -0
- package/python/harness/cli.py +84 -16
- package/python/harness/config/agent.py +33 -9
- package/python/harness/core/episodes.py +5 -4
- package/python/harness/core/lanes.py +3 -0
- package/python/harness/core/memory.py +5 -1
- package/python/harness/core/remote.py +4 -3
- package/python/harness/core/worker.py +58 -3
- package/python/harness/extensions.py +23 -2
- package/python/harness/host/__init__.py +43 -1
- package/python/harness/host/client.py +36 -6
- package/python/harness/host/http.py +19 -0
- package/python/harness/host/server.py +17 -3
- package/python/harness/host/sessions.py +3 -2
- package/python/harness/host/tasks.py +158 -29
- package/python/harness/host/workspaces.py +69 -7
- package/python/harness/model_list.py +1 -1
- package/python/harness/packages/manager.py +5 -1
- package/python/harness/packages/manifest.py +12 -1
- package/python/harness/providers.py +14 -5
- package/python/harness/runtime/decision/loop.py +100 -121
- package/python/harness/runtime/decision/messages.py +156 -48
- package/python/harness/runtime/decision/model.py +79 -21
- package/python/harness/runtime/decision/prompts.py +15 -11
- package/python/harness/runtime/decision/protocol.py +12 -6
- package/python/harness/runtime/decision/thoughts.py +10 -16
- package/python/harness/runtime/events.py +28 -13
- package/python/harness/runtime/execution/attached.py +1 -1
- package/python/harness/runtime/execution/control.py +17 -35
- package/python/harness/runtime/execution/entries.py +204 -124
- package/python/harness/runtime/execution/kernel.py +471 -281
- package/python/harness/runtime/execution/managed.py +231 -0
- package/python/harness/runtime/execution/noenv.py +2 -2
- package/python/harness/runtime/execution/results.py +2 -0
- package/python/harness/runtime/execution/session.py +276 -115
- package/python/harness/runtime/learning/active.py +36 -48
- package/python/harness/runtime/learning/fixtures.py +2 -2
- package/python/harness/runtime/learning/learner.py +75 -2
- package/python/harness/runtime/learning/library.py +77 -10
- package/python/harness/runtime/learning/views.py +12 -1
- package/python/harness/runtime/reactive/__init__.py +1 -1
- package/python/harness/runtime/reactive/rules.py +29 -394
- package/python/harness/runtime/reactive/tree.py +611 -0
- package/python/harness/runtime/safety/approval.py +8 -3
- package/python/harness/runtime/safety/permissions.py +242 -0
- package/python/harness/runtime/status.py +7 -2
- package/python/harness/runtime/task/delegation.py +97 -58
- package/python/harness/runtime/task/environments.py +3 -4
- package/python/harness/runtime/task/inbound.py +64 -0
- package/python/harness/runtime/task/run.py +1092 -726
- package/python/harness/runtime/tick.py +257 -91
- package/python/harness/sdk/__init__.py +7 -4
- package/python/harness/settings.py +27 -3
- package/python/harness/terminal.py +10 -6
- package/python/harness/transcript.py +18 -6
- package/python/harness/tui/answer.py +51 -0
- package/python/harness/tui/chat.py +75 -26
- package/python/harness/tui/views.py +1 -0
- package/python/harness/builtin/prompts/shared/entries.md +0 -86
|
@@ -42,13 +42,19 @@ class ChatReply:
|
|
|
42
42
|
# Reasoning text or summary, when the provider returns one (e.g. DeepSeek, GPT-6
|
|
43
43
|
# Luna on OpenRouter); for the trace only, never sent back to the model.
|
|
44
44
|
reasoning: str = ""
|
|
45
|
+
# What the provider attached to the reply (the pages a web search cited: url_citation).
|
|
46
|
+
annotations: list = field(default_factory=list)
|
|
45
47
|
|
|
46
48
|
|
|
47
49
|
# The keys of a model role (configs/models/<name>.toml: [main], [vlm], [pointer],
|
|
48
50
|
# [verifier]). Documentation; ChatModel reads them.
|
|
49
51
|
MODEL_KEYS = {
|
|
50
52
|
"service": "a service of the deploy instead of a hosted model: its client config, with this role's keys on top",
|
|
51
|
-
"backend": "openrouter (default)
|
|
53
|
+
"backend": "openrouter (default), openai_chat (any OpenAI-compatible server) or one a package provides "
|
|
54
|
+
"(acp: an ACP agent answers as the model, the external-agents package)",
|
|
55
|
+
"agent": "acp: the agent, acp:<id> as harness agents ls names it",
|
|
56
|
+
"mode": "acp: the agent's session mode (default: the first it offers that asks before it acts)",
|
|
57
|
+
"session_meta": "acp: fields added to the _meta of the agent's new sessions",
|
|
52
58
|
"model": "the model id",
|
|
53
59
|
"base_url": "openai_chat: the server's URL, e.g. http://127.0.0.1:8800/v1",
|
|
54
60
|
"base_url_env": "openai_chat: environment variable holding the server URL; overrides base_url when set",
|
|
@@ -130,15 +136,43 @@ def chat_part(part):
|
|
|
130
136
|
raise ValueError(f"Unsupported content part: {part['type']}")
|
|
131
137
|
|
|
132
138
|
|
|
139
|
+
BUILT_IN = ("openrouter", "openai_chat")
|
|
140
|
+
# Prompt caching. Most providers cache the prefix of a request by themselves; those here
|
|
141
|
+
# cache up to a mark (OpenRouter: Anthropic's models and Alibaba's Qwen). The request
|
|
142
|
+
# marks the system prompt and the last part of its last message, so that what a call read
|
|
143
|
+
# the next one reads from the cache as long as the conversation only grows.
|
|
144
|
+
CACHE_MARK = {"type": "ephemeral"}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def marks_cache(model: str) -> bool:
|
|
148
|
+
return model.startswith("anthropic/") or "qwen" in model.lower()
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def package_transport(config: dict):
|
|
152
|
+
"""The transport of a backend a package provides (extensions.py model_backend)."""
|
|
153
|
+
from harness.extensions import modules
|
|
154
|
+
|
|
155
|
+
for module in modules():
|
|
156
|
+
factory = getattr(module, "model_backend", None)
|
|
157
|
+
transport = factory(config) if factory else None
|
|
158
|
+
if transport is not None:
|
|
159
|
+
return transport
|
|
160
|
+
backend = config.get("backend")
|
|
161
|
+
raise ValueError(f"no installed package provides the {backend} model backend"
|
|
162
|
+
+ (": harness install external-agents" if backend == "acp" else ""))
|
|
163
|
+
|
|
164
|
+
|
|
133
165
|
class ChatModel:
|
|
134
|
-
"""A Chat Completions client for one model role (MODEL_KEYS): OpenRouter
|
|
135
|
-
OpenAI-compatible server
|
|
166
|
+
"""A Chat Completions client for one model role (MODEL_KEYS): OpenRouter, an
|
|
167
|
+
OpenAI-compatible server, or a backend of a package, which answers the same request
|
|
168
|
+
(package_transport). The openrouter backend needs its key in the environment;
|
|
136
169
|
`transport` replaces the HTTP call (tests)."""
|
|
137
170
|
|
|
138
171
|
def __init__(self, config: dict, transport=None):
|
|
139
172
|
self.provider = config.get("backend", "openrouter")
|
|
140
|
-
|
|
141
|
-
|
|
173
|
+
self.packaged = self.provider not in BUILT_IN
|
|
174
|
+
if self.packaged and transport is None:
|
|
175
|
+
transport = package_transport(config)
|
|
142
176
|
self.model = config["model"]
|
|
143
177
|
# A URL in base_url_env is resolved with the agent (config/agent.py _role).
|
|
144
178
|
self.base_url = config.get("base_url")
|
|
@@ -147,7 +181,7 @@ class ChatModel:
|
|
|
147
181
|
raise ValueError(f"openai_chat requires base_url.{f' Set {named}.' if named else ''}")
|
|
148
182
|
# A self-hosted model placed on demand is started by the deploy's broker first.
|
|
149
183
|
self.on_demand = config.get("on_demand")
|
|
150
|
-
self.url = chat_url(self.provider, self.base_url)
|
|
184
|
+
self.url = "" if self.packaged else chat_url(self.provider, self.base_url)
|
|
151
185
|
self.timeout = float(config.get("timeout_seconds", 300.0))
|
|
152
186
|
self.max_output_tokens = int(config.get("max_output_tokens", 8192))
|
|
153
187
|
self.max_tokens_key = str(config.get("max_tokens_key") or "max_tokens")
|
|
@@ -183,6 +217,12 @@ class ChatModel:
|
|
|
183
217
|
else:
|
|
184
218
|
converted.append({"role": message["role"],
|
|
185
219
|
"content": [chat_part(part) for part in content]})
|
|
220
|
+
if (self.provider == "openrouter" or urlsplit(self.url).hostname == "openrouter.ai") and marks_cache(self.model):
|
|
221
|
+
for message in ([converted[0]] if system else []) + converted[-1:]:
|
|
222
|
+
if isinstance(message["content"], str):
|
|
223
|
+
message["content"] = [{"type": "text", "text": message["content"]}]
|
|
224
|
+
if message["content"]:
|
|
225
|
+
message["content"][-1] = {**message["content"][-1], "cache_control": CACHE_MARK}
|
|
186
226
|
payload = {"model": self.model, "messages": converted, self.max_tokens_key: self.max_output_tokens,
|
|
187
227
|
**self.extra_body}
|
|
188
228
|
if self.stream:
|
|
@@ -195,10 +235,16 @@ class ChatModel:
|
|
|
195
235
|
self.max_output_tokens))
|
|
196
236
|
return payload
|
|
197
237
|
|
|
238
|
+
def close(self) -> None:
|
|
239
|
+
"""Let go of what the transport holds (a package backend's process; nothing for HTTP)."""
|
|
240
|
+
close = getattr(self.transport, "close", None)
|
|
241
|
+
if callable(close):
|
|
242
|
+
close()
|
|
243
|
+
|
|
198
244
|
@property
|
|
199
245
|
def streams_text(self) -> bool:
|
|
200
246
|
"""Whether complete() can hand over the reply's text as it streams (on_text)."""
|
|
201
|
-
return self.stream and self.transport is sse_transport
|
|
247
|
+
return self.stream and self.transport is sse_transport or bool(getattr(self.transport, "streams_text", False))
|
|
202
248
|
|
|
203
249
|
def complete(self, system: str, messages: list[dict], on_text=None, *, cancellation=None) -> ChatReply:
|
|
204
250
|
"""One completion of `messages` under `system`, starting an on-demand service through
|
|
@@ -240,7 +286,8 @@ class ChatModel:
|
|
|
240
286
|
try:
|
|
241
287
|
body = self.transport(self.url, headers, payload, self.timeout,
|
|
242
288
|
**({"cancellation": cancellation} if cancellation is not None
|
|
243
|
-
and self.transport in (sse_transport, urllib_transport)
|
|
289
|
+
and (self.transport in (sse_transport, urllib_transport)
|
|
290
|
+
or getattr(self.transport, "cancellable", False)) else {}),
|
|
244
291
|
**({"on_text": on_text} if on_text is not None and self.streams_text else {}))
|
|
245
292
|
except Exception as error:
|
|
246
293
|
if cancellation is not None:
|
|
@@ -262,10 +309,16 @@ class ChatModel:
|
|
|
262
309
|
return ChatReply(
|
|
263
310
|
text=(message.get("content") or "") + _tool_blocks(message.get("tool_calls")),
|
|
264
311
|
reasoning=message.get("reasoning") or "",
|
|
312
|
+
annotations=list(message.get("annotations") or ()),
|
|
265
313
|
usage={"input_tokens": usage.get("prompt_tokens", 0),
|
|
266
314
|
"output_tokens": usage.get("completion_tokens", 0),
|
|
267
315
|
"cost_usd": usage.get("cost"),
|
|
268
|
-
"finish_reason": choices[0].get("finish_reason")
|
|
316
|
+
"finish_reason": choices[0].get("finish_reason"),
|
|
317
|
+
# What of the input came from the provider's cache, where it says.
|
|
318
|
+
**({"cached_tokens": usage["prompt_tokens_details"]["cached_tokens"]}
|
|
319
|
+
if (usage.get("prompt_tokens_details") or {}).get("cached_tokens") is not None else {}),
|
|
320
|
+
# An ACP agent: whether its session went on or a new one took the whole conversation.
|
|
321
|
+
**({"session": usage["session"]} if usage.get("session") else {})},
|
|
269
322
|
response_id=body.get("id"),
|
|
270
323
|
seconds=time.perf_counter() - started,
|
|
271
324
|
)
|
|
@@ -389,33 +442,38 @@ def scripted_models(script) -> dict:
|
|
|
389
442
|
"verifier": ScriptedChat(script.get("verifier") or ["YES (scripted verifier)"] * 100)}
|
|
390
443
|
|
|
391
444
|
|
|
392
|
-
|
|
445
|
+
# The end of a request that says the model's program still runs and nothing else happened.
|
|
446
|
+
_STILL_RUNNING = re.compile(r"Program \d+ is still running")
|
|
447
|
+
_WAIT_OVER = re.compile(r"^Wait ended after [\d.]+ s: its time is over")
|
|
393
448
|
|
|
394
449
|
|
|
395
|
-
def
|
|
396
|
-
"""Whether the final content part
|
|
450
|
+
def still_running(messages: list[dict]) -> bool:
|
|
451
|
+
"""Whether the final content part says a program still runs and nothing reached the
|
|
452
|
+
model: it was just started, or a wait's time is over while units run (a wait whose time
|
|
453
|
+
is over with nothing running is the script's own to answer)."""
|
|
397
454
|
content = messages[-1].get("content") if messages else None
|
|
398
455
|
last = content if isinstance(content, str) else (content[-1].get("text", "") if content else "")
|
|
399
|
-
|
|
456
|
+
last = last or ""
|
|
457
|
+
return bool(_STILL_RUNNING.match(last) or (_WAIT_OVER.match(last) and "\nUnits running:" in last))
|
|
400
458
|
|
|
401
459
|
|
|
402
460
|
class ScriptedChat:
|
|
403
|
-
"""Replays fixed replies in order. For pipeline checks without model calls. A
|
|
404
|
-
|
|
405
|
-
them), so the script is the replies after each program
|
|
406
|
-
|
|
461
|
+
"""Replays fixed replies in order. For pipeline checks without model calls. A turn that
|
|
462
|
+
comes back while a program still runs is answered <wait> without taking a reply (`waits`
|
|
463
|
+
counts them), so the script is the replies after each program's result; with
|
|
464
|
+
`waits=False` every call takes the next reply."""
|
|
407
465
|
|
|
408
|
-
def __init__(self, replies: list[str],
|
|
466
|
+
def __init__(self, replies: list[str], waits: bool = True):
|
|
409
467
|
self.replies = list(replies)
|
|
410
468
|
self.calls = 0
|
|
411
|
-
self.ahead = ahead
|
|
412
469
|
self.waits = 0
|
|
470
|
+
self._waits = waits
|
|
413
471
|
|
|
414
472
|
def complete(self, system: str, messages: list[dict]) -> ChatReply:
|
|
415
473
|
"""The next scripted reply (a `<finish>` block once they run out), with zero usage."""
|
|
416
|
-
if
|
|
474
|
+
if self._waits and still_running(messages):
|
|
417
475
|
self.waits += 1
|
|
418
|
-
return ChatReply(text="<wait
|
|
476
|
+
return ChatReply(text="<wait></wait>", usage=dict(USAGE))
|
|
419
477
|
if self.calls >= len(self.replies):
|
|
420
478
|
text = "<finish>scripted replies exhausted</finish>"
|
|
421
479
|
else:
|
|
@@ -26,10 +26,12 @@ Markers may also name features of the connection (FEATURES): measured_gripper ho
|
|
|
26
26
|
when gripper readings are measured openings, not under a contract that reports only the
|
|
27
27
|
last command, step_limit when the episode's step limit is reported (core/contract.py),
|
|
28
28
|
channels when the bench's embodiment declares channels (a game's keys, mouse and
|
|
29
|
-
wheel in groups that
|
|
30
|
-
|
|
31
|
-
runtime/
|
|
32
|
-
|
|
29
|
+
wheel in groups that units claim: runtime/tick.py), and look when the environment has
|
|
30
|
+
views and the model looks at them on its own demand (no runtime.cameras:
|
|
31
|
+
runtime/task/run.py), and senses when the embodiment's kind says its environment computes
|
|
32
|
+
values the model defines (prompt_features: runtime/execution/session.py sense), and pauses when it says its
|
|
33
|
+
environment pauses a call for a unit of higher priority instead of ending it (runtime/tick.py). think_while_acting never holds (the model's turn is always its own
|
|
34
|
+
now): a pack's text for it is left out. The marker lines themselves never reach the
|
|
33
35
|
model, so a prompt with every skill and feature enabled is the pack's text without them. The API documentation stays in the pack
|
|
34
36
|
because it describes one embodiment (its frames, reach and numbers); the skills'
|
|
35
37
|
code is the same for every embodiment.
|
|
@@ -57,13 +59,15 @@ DEFAULT_SECTIONS = (
|
|
|
57
59
|
# Features of the connection that prompt text may depend on (not skills).
|
|
58
60
|
# `look`: the environment has views and the model looks on demand (look, watch): the
|
|
59
61
|
# configuration declares no standing watch (runtime.cameras).
|
|
60
|
-
|
|
62
|
+
# `machine`: programs are not isolated (runtime.isolation false): they run on the machine as
|
|
63
|
+
# its user, under the user's approvals (runtime/safety/permissions.py).
|
|
64
|
+
FEATURES = ("measured_gripper", "step_limit", "channels", "think_while_acting", "vision_service", "look", "senses", "pauses",
|
|
65
|
+
"machine")
|
|
61
66
|
|
|
62
67
|
|
|
63
68
|
def features_for(runtime: dict, channels: bool = False, *, embodiment=None) -> set[str]:
|
|
64
|
-
"""The prompt features of a runtime configuration (its observation contract and
|
|
65
|
-
|
|
66
|
-
channels."""
|
|
69
|
+
"""The prompt features of a runtime configuration (its observation contract), and
|
|
70
|
+
`channels` when the embodiment declares channels."""
|
|
67
71
|
from harness.embodiments import KINDS
|
|
68
72
|
|
|
69
73
|
kind = KINDS[embodiment] if embodiment is not None else None
|
|
@@ -72,12 +76,12 @@ def features_for(runtime: dict, channels: bool = False, *, embodiment=None) -> s
|
|
|
72
76
|
features.add("step_limit")
|
|
73
77
|
if kind:
|
|
74
78
|
features.update(kind.native_features)
|
|
75
|
-
if runtime.get("think_while_acting") != "off":
|
|
76
|
-
features.add("think_while_acting")
|
|
77
79
|
if runtime.get("vision"):
|
|
78
80
|
features.add("vision_service")
|
|
79
81
|
if "cameras" not in runtime and kind is not None and kind.cameras:
|
|
80
82
|
features.add("look")
|
|
83
|
+
if not runtime.get("isolation", True):
|
|
84
|
+
features.add("machine")
|
|
81
85
|
return features | ({"channels"} if channels else set())
|
|
82
86
|
|
|
83
87
|
|
|
@@ -227,7 +231,7 @@ def mentions(text: str, name: str) -> bool:
|
|
|
227
231
|
|
|
228
232
|
|
|
229
233
|
# These calls are present in EpisodeCore even with no reactive skills enabled.
|
|
230
|
-
CORE_RULE_CALLS = {"
|
|
234
|
+
CORE_RULE_CALLS = {"set_reflex", "drop_reflex", "reflexes", "run_controller", "at_priority", "until"}
|
|
231
235
|
|
|
232
236
|
|
|
233
237
|
def documentation_gaps(directory: Path, skills, exported: dict, features=("measured_gripper",)) -> dict:
|
|
@@ -5,11 +5,17 @@ import json
|
|
|
5
5
|
import re
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
# <wait> (optionally with a number of seconds): the model waits by choice until something
|
|
9
|
+
# reaches it (runtime/task/run.py).
|
|
10
|
+
KINDS = ("python", "verify", "finish", "wait")
|
|
11
|
+
DIRECT_KINDS = ("action", "finish", "wait")
|
|
12
|
+
_SECONDS = re.compile(r"\d+(?:\.\d+)?")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def wait_seconds(body: str) -> float | None:
|
|
16
|
+
"""The seconds a <wait> block asks for at most (its first number), None when it names none."""
|
|
17
|
+
found = _SECONDS.search(body or "")
|
|
18
|
+
return float(found.group(0)) if found else None
|
|
13
19
|
_FENCE = re.compile(r"^\s*```[a-zA-Z0-9_-]*\s*\n(.*?)\n\s*```\s*$", re.DOTALL)
|
|
14
20
|
# DeepSeek models sometimes end a block with their own tool-call markup instead of its
|
|
15
21
|
# closing tag ("</\uff5cDSML\uff5c parameter>", "</\uff5cDSML\uff5c invoke>", ...): 50 of
|
|
@@ -86,7 +92,7 @@ def _invoked(text: str, kinds) -> str | None:
|
|
|
86
92
|
# Count *all* invokes, including unknown tools. Never silently execute a subset.
|
|
87
93
|
openings = re.findall(rf"<\s*{_MARK}\s*invoke\b", text)
|
|
88
94
|
if len(invokes) != 1 or len(openings) != 1 or invokes[0].group(1) not in kinds:
|
|
89
|
-
raise ProtocolError("Expected one supported DSML invoke; use a single <python
|
|
95
|
+
raise ProtocolError("Expected one supported DSML invoke; use a single <python> or <verify> block")
|
|
90
96
|
invoke = invokes[0]
|
|
91
97
|
kind, inner = invoke.group(1), invoke.group(2)
|
|
92
98
|
parameters = _PARAMETER.findall(inner)
|
|
@@ -15,19 +15,18 @@ class ThoughtCancelled(Exception):
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
class Thought:
|
|
18
|
-
def __init__(self, role, trigger, chat, system, messages, *, timeout,
|
|
19
|
-
revision=0, origin=None, owner=None, call=None,
|
|
18
|
+
def __init__(self, role, trigger, chat, system, messages, *, timeout,
|
|
19
|
+
revision=0, origin=None, owner=None, call=None, invoke=None):
|
|
20
20
|
self.id = uuid.uuid4().hex
|
|
21
|
-
self.role, self.trigger
|
|
21
|
+
self.role, self.trigger = role, trigger
|
|
22
22
|
self.origin, self.owner, self.revision, self.call = origin, owner, revision, call
|
|
23
23
|
self.chat, self.system, self.messages = chat, system, deepcopy(messages)
|
|
24
|
-
self.
|
|
24
|
+
self.invoke = invoke
|
|
25
25
|
self.deadline = time.monotonic() + max(0, timeout)
|
|
26
26
|
self.cancellation = RequestCancellation()
|
|
27
27
|
self.ready = threading.Event()
|
|
28
28
|
self.reply, self.error, self.outcome = None, None, None
|
|
29
29
|
self.cancelled, self.exited, self.started = False, False, False
|
|
30
|
-
self.stopped, self.said, self.side = False, False, None
|
|
31
30
|
self.seconds = 0.0
|
|
32
31
|
|
|
33
32
|
def back(self, timeout=None):
|
|
@@ -64,8 +63,8 @@ class Thought:
|
|
|
64
63
|
class ThoughtQueue:
|
|
65
64
|
"""A condition-driven tick for decisions. Network workers only post exit receipts."""
|
|
66
65
|
|
|
67
|
-
def __init__(self, condition, *,
|
|
68
|
-
self.changed, self.
|
|
66
|
+
def __init__(self, condition, *, on_exit=None, trace=None):
|
|
67
|
+
self.changed, self.on_exit, self.trace = condition, on_exit, trace
|
|
69
68
|
self.pending = []
|
|
70
69
|
self.active = self.receipt = self.thread = None
|
|
71
70
|
self.closed = False
|
|
@@ -102,7 +101,7 @@ class ThoughtQueue:
|
|
|
102
101
|
def _emit(self, event, thought, **detail):
|
|
103
102
|
if self.trace is not None:
|
|
104
103
|
self.trace.emit(event, thought=thought.id, role=thought.role, trigger=thought.trigger,
|
|
105
|
-
|
|
104
|
+
owner=thought.owner, origin=thought.origin,
|
|
106
105
|
revision=thought.revision, **detail)
|
|
107
106
|
|
|
108
107
|
def _tick(self):
|
|
@@ -130,11 +129,9 @@ class ThoughtQueue:
|
|
|
130
129
|
now = time.monotonic()
|
|
131
130
|
self.cancel_where(lambda thought: thought.deadline <= now, "Model transport deadline expired")
|
|
132
131
|
self.pending[:] = [thought for thought in self.pending if not thought.cancelled]
|
|
133
|
-
due = self.advance() if self.advance is not None and not self.closed else None
|
|
134
132
|
if self.active is None and self.pending:
|
|
135
|
-
#
|
|
136
|
-
thought = min(self.pending, key=lambda item: (item.
|
|
137
|
-
item.role != "main"))
|
|
133
|
+
# The model's decisions and synchronous role requests precede the observer.
|
|
134
|
+
thought = min(self.pending, key=lambda item: (item.role == "observer", item.role != "main"))
|
|
138
135
|
self.pending.remove(thought)
|
|
139
136
|
self.active = thought
|
|
140
137
|
thought.started = True
|
|
@@ -143,8 +140,6 @@ class ThoughtQueue:
|
|
|
143
140
|
daemon=True).start()
|
|
144
141
|
deadlines = [item.deadline for item in [self.active, *self.pending]
|
|
145
142
|
if item is not None and not item.cancelled]
|
|
146
|
-
if due is not None:
|
|
147
|
-
deadlines.append(due)
|
|
148
143
|
self.changed.wait(max(0, min(deadlines) - time.monotonic()) if deadlines else None)
|
|
149
144
|
|
|
150
145
|
@contextmanager
|
|
@@ -155,8 +150,7 @@ class ThoughtQueue:
|
|
|
155
150
|
identifier = uuid.uuid4().hex
|
|
156
151
|
lane = {"reflection": "model", "verifier": "verifier", "observer": "observer"}.get(thought.role, "service")
|
|
157
152
|
self.trace.emit("span_start", id=identifier, lane=lane, name=getattr(thought.chat, "model", thought.role),
|
|
158
|
-
thought=thought.id, role=thought.role, reason=thought.trigger,
|
|
159
|
-
program=thought.program, parent=None)
|
|
153
|
+
thought=thought.id, role=thought.role, reason=thought.trigger, parent=None)
|
|
160
154
|
try:
|
|
161
155
|
yield
|
|
162
156
|
finally:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Events: the signals of a run, in one catalog.
|
|
2
2
|
|
|
3
|
-
An execution's event (Event) ends every bounded execution and goes
|
|
4
|
-
|
|
3
|
+
An execution's event (Event) ends every bounded execution and goes to the rules that answer
|
|
4
|
+
it (on()); one nobody answers ends the unit that ran it and reaches the model (Escalation).
|
|
5
5
|
A trace record (EVENTS) is what the run writes to its trace (core/trace.py), which the
|
|
6
6
|
timeline, the overlay, replay and the exported video all draw. A family of records is one
|
|
7
7
|
kind with an `action` (library), which record_kind() reads.
|
|
@@ -14,14 +14,14 @@ from typing import Any
|
|
|
14
14
|
# The kinds of an execution's event (Event.kind; a guard may raise one of its own name):
|
|
15
15
|
# nominal ends (NOMINAL), a time bound, the execution's scope, a guard, the shield, reactive
|
|
16
16
|
# code that broke its rules, a skill's error, the arm held back short of its target (contact
|
|
17
|
-
# or a load), the episode's end, the user's pause
|
|
18
|
-
#
|
|
19
|
-
#
|
|
17
|
+
# or a load), the episode's end, the user's pause (it stops every unit of the model), a unit
|
|
18
|
+
# that failed, a channel another unit has, and a unit of higher priority that took the
|
|
19
|
+
# embodiment from an execution that is not to be started again.
|
|
20
20
|
BUILTIN = ("COMPLETED", "PREDICATE_SATISFIED", "TIMEOUT", "REGION_EXIT", "GUARD_TRIGGERED", "SHIELD_REJECTED",
|
|
21
|
-
"REACTIVE_FAULT", "SKILL_ERROR", "BLOCKED", "EPISODE_ENDED", "USER_PAUSED",
|
|
22
|
-
"ENTRY_FAILED", "CHANNEL_CONFLICT")
|
|
21
|
+
"REACTIVE_FAULT", "SKILL_ERROR", "BLOCKED", "EPISODE_ENDED", "USER_PAUSED",
|
|
22
|
+
"ENTRY_FAILED", "CHANNEL_CONFLICT", "PREEMPTED", "REFLEX_FAILED")
|
|
23
23
|
(COMPLETED, PREDICATE_SATISFIED, TIMEOUT, REGION_EXIT, GUARD_TRIGGERED, SHIELD_REJECTED, REACTIVE_FAULT, SKILL_ERROR,
|
|
24
|
-
BLOCKED, EPISODE_ENDED, USER_PAUSED,
|
|
24
|
+
BLOCKED, EPISODE_ENDED, USER_PAUSED, ENTRY_FAILED, CHANNEL_CONFLICT, PREEMPTED, REFLEX_FAILED) = BUILTIN
|
|
25
25
|
# A program may continue after these without consulting a handler.
|
|
26
26
|
NOMINAL = frozenset({COMPLETED, PREDICATE_SATISFIED})
|
|
27
27
|
|
|
@@ -61,6 +61,9 @@ class ExecutionTrace:
|
|
|
61
61
|
samples: list[dict] = field(default_factory=list)
|
|
62
62
|
# A value for the calling program, never part of an event or serialized trace.
|
|
63
63
|
result: Any = field(default=None, repr=False)
|
|
64
|
+
# How often a unit of higher priority took the embodiment and the harness started
|
|
65
|
+
# this execution again.
|
|
66
|
+
restarts: int = 0
|
|
64
67
|
|
|
65
68
|
@property
|
|
66
69
|
def ok(self) -> bool:
|
|
@@ -77,6 +80,8 @@ class ExecutionTrace:
|
|
|
77
80
|
data = {"skill": self.skill, "parameters": self.parameters, "start_tick": self.start_tick,
|
|
78
81
|
"end_tick": self.end_tick, "event": self.event.to_dict(), "samples": self.samples}
|
|
79
82
|
data["ok"] = self.ok
|
|
83
|
+
if self.restarts:
|
|
84
|
+
data["restarts"] = self.restarts
|
|
80
85
|
return data
|
|
81
86
|
|
|
82
87
|
def __repr__(self) -> str:
|
|
@@ -122,11 +127,8 @@ EVENTS = {
|
|
|
122
127
|
"user_message": {"source": "user", "shown": True},
|
|
123
128
|
"model_request": {"source": "harness", "shown": True},
|
|
124
129
|
"model_changed": {"source": "user", "shown": False},
|
|
125
|
-
"
|
|
126
|
-
"
|
|
127
|
-
"side_program": {"source": "harness", "shown": False},
|
|
128
|
-
"side_program_end": {"source": "harness", "shown": False},
|
|
129
|
-
"program_stopped": {"source": "harness", "shown": False},
|
|
130
|
+
"compaction": {"source": "harness", "shown": False},
|
|
131
|
+
"results_cleared": {"source": "harness", "shown": False},
|
|
130
132
|
"model_preempted": {"source": "harness", "shown": False},
|
|
131
133
|
"model_reply": {"source": "agent", "shown": True},
|
|
132
134
|
"agent_message": {"source": "agent", "shown": True},
|
|
@@ -141,19 +143,32 @@ EVENTS = {
|
|
|
141
143
|
"worker_approval_result": {"source": "harness", "shown": False},
|
|
142
144
|
"input": {"source": "environment", "shown": False},
|
|
143
145
|
"cell": {"source": "program", "shown": True},
|
|
146
|
+
"unit_end": {"source": "program", "shown": True},
|
|
147
|
+
"wait": {"source": "agent", "shown": False},
|
|
148
|
+
"wait_end": {"source": "harness", "shown": True},
|
|
144
149
|
"private_paths": {"source": "harness", "shown": False},
|
|
150
|
+
"program_permission": {"source": "harness", "shown": False},
|
|
151
|
+
"web_search": {"source": "harness", "shown": False},
|
|
145
152
|
"file_access": {"source": "program", "shown": False},
|
|
146
153
|
"modules": {"source": "harness", "shown": False},
|
|
147
154
|
"experience": {"source": "program", "shown": False},
|
|
148
155
|
"experience_folded": {"source": "harness", "shown": False},
|
|
149
156
|
"tick_periods": {"source": "program", "shown": False},
|
|
150
157
|
"guard": {"source": "program", "shown": False},
|
|
158
|
+
"rule": {"source": "program", "shown": False},
|
|
159
|
+
"preempted": {"source": "program", "shown": False},
|
|
160
|
+
"call_paused": {"source": "program", "shown": False},
|
|
161
|
+
"call_resumed": {"source": "program", "shown": False},
|
|
162
|
+
"held_back": {"source": "program", "shown": False},
|
|
163
|
+
"fact": {"source": "program", "shown": False},
|
|
151
164
|
"program_held": {"source": "harness", "shown": False},
|
|
152
165
|
"program_dropped": {"source": "harness", "shown": False},
|
|
153
166
|
"user_took_over": {"source": "harness", "shown": False},
|
|
154
167
|
"stuck": {"source": "harness", "shown": False},
|
|
155
168
|
"policy_to_end": {"source": "program", "shown": False},
|
|
156
169
|
"finish": {"source": "agent", "shown": False},
|
|
170
|
+
"turn_result": {"source": "harness", "shown": False},
|
|
171
|
+
"resumed": {"source": "harness", "shown": True},
|
|
157
172
|
"finish_refused": {"source": "harness", "shown": True},
|
|
158
173
|
"wrap_up": {"source": "program", "shown": False},
|
|
159
174
|
"kernel_died": {"source": "harness", "shown": False},
|
|
@@ -35,7 +35,7 @@ class RelayedEnvironment:
|
|
|
35
35
|
self.timeout = float(timeout)
|
|
36
36
|
self.host_controls = frozenset(host_controls)
|
|
37
37
|
|
|
38
|
-
def call(self, name: str, **arguments):
|
|
38
|
+
def call(self, name: str, /, **arguments):
|
|
39
39
|
return control.host_call({"environment": self.name, "name": name, "arguments": arguments}, self.timeout)
|
|
40
40
|
|
|
41
41
|
def close(self) -> None:
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
"""What the host asks of
|
|
1
|
+
"""What the host asks of the kernel's units besides starting programs.
|
|
2
2
|
|
|
3
3
|
The kernel process reads the host's requests on a thread of its own, so a request
|
|
4
|
-
reaches a
|
|
5
|
-
next step of a bounded execution
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
program started ends, the host says so, and its entry ends (runtime/execution/entries.py).
|
|
4
|
+
reaches a unit while it runs. A pause (the user paused the conversation) stops every unit
|
|
5
|
+
of the model: the next step of a bounded execution ends it with USER_PAUSED
|
|
6
|
+
(runtime/execution/session.py), a wait raises that event (runtime/execution/entries.py), and
|
|
7
|
+
nothing acts until the host resumes. A message for the model stops nothing here: it is an
|
|
8
|
+
event to the model, whose turn is its own (runtime/task/run.py). An answer is the host's
|
|
9
|
+
reply to a request a unit sent it as a note (the user's answer to a physical-input request,
|
|
10
|
+
runtime/safety/input.py): the unit waits for it, or for a pause. When a sub-agent ends, the
|
|
11
|
+
host says so, and its unit ends (runtime/execution/entries.py).
|
|
13
12
|
"""
|
|
14
13
|
|
|
15
14
|
import sys
|
|
@@ -19,7 +18,6 @@ import uuid
|
|
|
19
18
|
|
|
20
19
|
|
|
21
20
|
_pause = threading.Event()
|
|
22
|
-
_message = threading.Event()
|
|
23
21
|
_answers = {}
|
|
24
22
|
_answered = threading.Condition()
|
|
25
23
|
_agent_listeners = []
|
|
@@ -31,32 +29,19 @@ host_thoughts = False
|
|
|
31
29
|
|
|
32
30
|
|
|
33
31
|
def request_pause() -> None:
|
|
34
|
-
"""
|
|
32
|
+
"""Stop the model's units: each at its next execution step or wait."""
|
|
35
33
|
with _answered:
|
|
36
34
|
_pause.set()
|
|
37
35
|
_answered.notify_all()
|
|
38
36
|
|
|
39
37
|
|
|
40
|
-
def
|
|
41
|
-
"""
|
|
42
|
-
with _answered:
|
|
43
|
-
_message.set()
|
|
44
|
-
_answered.notify_all()
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
def clear() -> None:
|
|
48
|
-
"""Forget earlier requests (a new cell starts)."""
|
|
38
|
+
def resume() -> None:
|
|
39
|
+
"""The pause is over: units may act again."""
|
|
49
40
|
_pause.clear()
|
|
50
|
-
_message.clear()
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def message_arrived() -> bool:
|
|
54
|
-
"""Whether a message for the model came while the program runs."""
|
|
55
|
-
return _message.is_set()
|
|
56
41
|
|
|
57
42
|
|
|
58
43
|
def pause_requested() -> bool:
|
|
59
|
-
"""Whether the
|
|
44
|
+
"""Whether the model's units are paused."""
|
|
60
45
|
return _pause.is_set()
|
|
61
46
|
|
|
62
47
|
|
|
@@ -153,12 +138,10 @@ def think(role, text="", *, images=(), system=None, entry=None, frame=None, stop
|
|
|
153
138
|
sys.audit("harness.model_request", role)
|
|
154
139
|
if not host_thoughts or not spans.connected():
|
|
155
140
|
return None
|
|
156
|
-
if stop is None and threading.current_thread() is threading.main_thread():
|
|
157
|
-
stop = message_arrived
|
|
158
141
|
owner = current_entry()
|
|
159
142
|
|
|
160
143
|
def owner_cancelled():
|
|
161
|
-
return owner is not None and
|
|
144
|
+
return owner is not None and owner._cancel.is_set()
|
|
162
145
|
|
|
163
146
|
identifier = uuid.uuid4().hex
|
|
164
147
|
with _answered:
|
|
@@ -186,11 +169,10 @@ def think(role, text="", *, images=(), system=None, entry=None, frame=None, stop
|
|
|
186
169
|
|
|
187
170
|
|
|
188
171
|
def stopped(skill: str = "program"):
|
|
189
|
-
"""The typed escalation for
|
|
190
|
-
from harness.runtime.events import
|
|
172
|
+
"""The typed escalation for a unit's wait that the user's pause interrupted."""
|
|
173
|
+
from harness.runtime.events import USER_PAUSED, Escalation, Event, ExecutionTrace
|
|
191
174
|
|
|
192
|
-
|
|
193
|
-
return Escalation(ExecutionTrace(skill, {}, 0, 0, event))
|
|
175
|
+
return Escalation(ExecutionTrace(skill, {}, 0, 0, Event(USER_PAUSED, "user")))
|
|
194
176
|
|
|
195
177
|
|
|
196
178
|
def on_agent_ended(listener, remove: bool = False) -> None:
|