@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,30 +1,33 @@
1
- """Entries: parts of a program that run at the same time, as a tree (runtime/execution/entries.py).
2
- spawn() starts one; its handle waits for it, stops it and sends it messages. What an
3
- entry's executions end with goes to its own handlers first, then up to the entry that
4
- started it, and only what nobody handles reaches the model. Where the embodiment declares
5
- channels (a game's keys, mouse and wheel in groups), entries run at the same time on their
6
- own channels: claim() and unclaim() say which an entry has."""
1
+ """Units a program starts (runtime/execution/entries.py): spawn() runs code as a unit of its
2
+ own, of the model; its handle waits for it, ends it and sends it messages. What a unit's
3
+ executions end with goes to the handlers, and only what nobody handles reaches the model,
4
+ as every unit's end does. Where the embodiment declares channels, units act at the same
5
+ time on their own channels: claim() and unclaim() say which a unit has."""
7
6
 
8
7
 
9
8
  class Entries:
10
- def spawn(self, fn, name=None, claims=None):
11
- """Run fn() at the same time as this code, as an entry of its own under this one,
12
- and return its handle: .wait(timeout=None) returns what fn returned (raises its
13
- failure, an ENTRY_FAILED event; None when it was cancelled or the timeout passed),
14
- .cancel() stops it and the entries it started, .send(message) gives it a message
15
- that its code reads with receive(), .state() says how it stands, .status is
16
- running, done, failed or cancelled. `name` labels it (default: fn's name); `claims`
17
- names the channels it uses (the game's CHANNELS; e.g. ["move", "look"]): it has them until it
18
- ends, and their keys and buttons are let go then; one another entry has makes it
19
- fail at once with a CHANNEL_CONFLICT event. Without channels, executions of entries
20
- take turns on the embodiment, in the order they start. Guards (when, watch) and handlers
21
- (on) set inside fn are the entry's: they watch its executions and those of the
22
- entries it starts, and end with it. An entry that fails goes to whoever waits for
23
- it, else to the handlers of the entry that started it (on("ENTRY_FAILED", ...)),
24
- then up; one nobody handles reaches you. Entries this program starts keep running
25
- after it ends, and you hear how they stand after each program; an entry's own
26
- entries end with it."""
27
- return self._entries.spawn(fn, name=name, claims=claims)
9
+ def spawn(self, fn, name=None, claims=None, priority=None, restart=False, until=None):
10
+ """Run fn() at the same time as this code, as a unit of its own, and return its
11
+ handle: .wait(timeout=None) returns what fn returned (raises its failure, an
12
+ ENTRY_FAILED event; None when it was cancelled or the timeout passed), .cancel()
13
+ ends it, .send(message) gives it a message that its code reads with receive(),
14
+ .output(chars=2000) is what it has printed so far, .state() says how it stands,
15
+ .status is running, done, failed or cancelled. `name`
16
+ labels it (default: fn's name). `priority`: what it holds the embodiment with
17
+ (default: that of the unit that starts it); a unit of higher priority ends a lower
18
+ unit's execution where it is, with the event PREEMPTED and what it had done, and
19
+ holds the environment until it ends; the model is told at once. With restart=True
20
+ the lower call is started again when the higher unit is over, instead.
21
+ Units of equal priority take turns, in the order they start. `claims` names the
22
+ channels it uses where the embodiment declares channels: it has them until it
23
+ ends, and what it held on them is let go then; one another unit has makes it fail
24
+ at once with a CHANNEL_CONFLICT event. The unit is the model's: it and the
25
+ reflexes it sets go on after the program that started it, and end with the model.
26
+ A unit that fails goes to whoever waits for it, else to a reflex that answers
27
+ ENTRY_FAILED; the model hears of every unit's end. `until`: what the unit is for,
28
+ a condition of the view that holds when its work is done; its end says whether it
29
+ held (.ok)."""
30
+ return self._entries.spawn(fn, name=name, claims=claims, priority=priority, restart=restart, until=until)
28
31
 
29
32
  def receive(self, timeout=None):
30
33
  """The oldest message sent to the entry this code runs in (handle.send), waiting at
@@ -49,14 +52,26 @@ class Entries:
49
52
  return self._entries.channels()
50
53
 
51
54
  def entries(self, running=True):
52
- """The handles of the entries started so far (only those running, unless
53
- `running` is False), oldest first; each has .state(), .wait(), .cancel(),
54
- .send(); a sub-agent's (delegate) has .agent."""
55
+ """The handles of the units started so far (only those running, unless
56
+ `running` is False), oldest first; each has .id, .state(), .output(), .wait(),
57
+ .cancel(), .send(); a program the model wrote is one of them (.cell is its number);
58
+ a sub-agent's (delegate) has .agent."""
55
59
  tree = self._entries
56
60
  return [entry for entry in list(tree.entries.values())
57
61
  if entry is not tree.root and (entry.running or not running)]
58
62
 
63
+ def output_of(self, unit, chars=2000):
64
+ """What the unit `unit` (its id, such as "e27", or its handle) has printed so far:
65
+ the last `chars` characters (at most 30000), while it runs or after it ended.
66
+ Reading takes nothing away: the unit's result still has the whole output when it
67
+ ends. "" when it has printed nothing."""
68
+ tree = self._entries
69
+ entry = unit if getattr(unit, "tree", None) is tree else tree.entries.get(str(unit))
70
+ if entry is None or entry is tree.root:
71
+ raise ValueError(f"no unit {unit!r}; entries(running=False) lists the units started so far")
72
+ return entry.output(chars)
73
+
59
74
 
60
75
  def exports(session):
61
76
  return {"spawn": session.spawn, "receive": session.receive, "entries": session.entries,
62
- "claim": session.claim, "unclaim": session.unclaim, "channels": session.channel_board}
77
+ "output_of": session.output_of, "claim": session.claim, "unclaim": session.unclaim, "channels": session.channel_board}
@@ -1,8 +1,7 @@
1
1
  name = "entries"
2
- summary = "Run parts of a program at the same time as entries of a tree: spawn, wait, cancel, send; events go up the tree; claim and unclaim channels."
2
+ summary = "Start code as a unit of its own: spawn, wait, cancel, send, read its output so far, with a priority; claim and unclaim channels."
3
3
  layer = "base"
4
4
  mixin = "skills.entries:Entries"
5
- exports = ["spawn", "receive", "entries", "claim", "unclaim", "channels"]
6
- # Streaming: a configuration that wants entries names it.
5
+ exports = ["spawn", "receive", "entries", "output_of", "claim", "unclaim", "channels"]
7
6
  order = 115
8
7
  embodiments = ["*"]
@@ -12,19 +12,12 @@ conversation. The host keeps every attachment and its reason in the trace.
12
12
  Sub-agents are off unless the user switched them on: request_agents(why) asks the user to.
13
13
  """
14
14
 
15
- from harness.sdk import Escalation, control, user_message
15
+ from harness.sdk import control
16
16
 
17
17
 
18
18
  class Environments:
19
19
  def _ask_conversation(self, operation, **detail):
20
- tree = getattr(self, "_entries", None)
21
- # The program's own code leaves the wait when a message for the model comes.
22
- own = tree is None or tree.current() is tree.root
23
- answer = control.ask_host("environment", stop=control.message_arrived if own else None, action=operation,
24
- **detail)
25
- if answer is None and own and control.message_arrived():
26
- raise Escalation(user_message(operation, detail))
27
- return answer
20
+ return control.ask_host("environment", action=operation, **detail)
28
21
 
29
22
  def use_environment(self, kind, why, folder=None):
30
23
  """Take another environment into this conversation: `kind` is one the user's
@@ -16,7 +16,7 @@ class McpTools:
16
16
  from harness.runtime.execution.entries import current_entry, Cancelled
17
17
  owner = current_entry()
18
18
  def cancelled():
19
- return owner is not None and any(entry._cancel.is_set() for entry in owner.lineage())
19
+ return owner is not None and owner._cancel.is_set()
20
20
  identifier = uuid.uuid4().hex
21
21
  answer = None
22
22
  with control._answered:
@@ -24,8 +24,7 @@ class McpTools:
24
24
  try:
25
25
  spans.note("mcp_request", id=identifier, method=method,
26
26
  entry=owner.id if owner is not None else None, **detail)
27
- answer = control.wait_answer(identifier, stop=lambda: cancelled() or
28
- (owner is None and control.message_arrived()))
27
+ answer = control.wait_answer(identifier, stop=cancelled)
29
28
  finally:
30
29
  with control._answered:
31
30
  control._mcp_calls.discard(identifier)
@@ -2,7 +2,6 @@
2
2
  entries, through one interface. The run's working memory is the host's (shared with the
3
3
  sub-agents); without a host (a test, a script) the kernel keeps one of its own."""
4
4
 
5
- import threading
6
5
  import time
7
6
  import uuid
8
7
 
@@ -53,11 +52,9 @@ class Memory:
53
52
  if not wait:
54
53
  return None
55
54
  deadline = time.monotonic() + ANSWER_SECONDS
56
- own = threading.current_thread() is threading.main_thread()
57
- answer = control.wait_answer(identifier, poll=None, stop=control.message_arrived if own else None,
58
- deadline=deadline)
55
+ answer = control.wait_answer(identifier, poll=None, deadline=deadline)
59
56
  if answer is None:
60
- if control.pause_requested() or (own and control.message_arrived()):
57
+ if control.pause_requested():
61
58
  raise control.stopped("memory")
62
59
  raise RuntimeError("the host did not answer the memory request within 60 seconds")
63
60
  if answer.get("error"):
@@ -0,0 +1,92 @@
1
+ """The web from a program: read a page, search.
2
+
3
+ fetch reads from the kernel itself, so it goes by the user's approvals like any connection
4
+ a program makes (runtime/safety/permissions.py). search is the conversation's: it asks the
5
+ host, which holds the key of the search it is configured with (runtime.search)."""
6
+
7
+ from html.parser import HTMLParser
8
+ import re
9
+ import urllib.request
10
+
11
+ from harness.runtime.execution import control
12
+
13
+ # The most characters of a page one call gives, and the most bytes it reads of one.
14
+ PAGE_CHARS, PAGE_BYTES = 100_000, 10 * 1024 * 1024
15
+ _SKIPPED = frozenset(("script", "style", "noscript", "template", "svg", "head"))
16
+ _BREAKS = frozenset(("p", "div", "br", "li", "tr", "h1", "h2", "h3", "h4", "h5", "h6", "pre", "section", "article",
17
+ "table", "ul", "ol", "blockquote"))
18
+
19
+
20
+ class _Text(HTMLParser):
21
+ """A page's text: what its elements show, a line for each block, links with their address."""
22
+
23
+ def __init__(self):
24
+ super().__init__(convert_charrefs=True)
25
+ self.parts, self.skipping, self.link = [], 0, None
26
+
27
+ def handle_starttag(self, tag, attributes):
28
+ if tag in _SKIPPED:
29
+ self.skipping += 1
30
+ elif tag in _BREAKS:
31
+ self.parts.append("\n")
32
+ elif tag == "a":
33
+ self.link = dict(attributes).get("href")
34
+
35
+ def handle_endtag(self, tag):
36
+ if tag in _SKIPPED:
37
+ self.skipping = max(0, self.skipping - 1)
38
+ elif tag in _BREAKS:
39
+ self.parts.append("\n")
40
+ elif tag == "a":
41
+ if self.link and self.link.startswith(("http://", "https://")):
42
+ self.parts.append(f" ({self.link})")
43
+ self.link = None
44
+
45
+ def handle_data(self, data):
46
+ if not self.skipping:
47
+ self.parts.append(data)
48
+
49
+
50
+ def page_text(body: str, kind: str) -> str:
51
+ """The text of a response: an HTML page as its text, anything else as it is."""
52
+ if "html" not in kind and not re.match(r"\s*<(!doctype|html)", body[:200], re.I):
53
+ return body
54
+ parser = _Text()
55
+ parser.feed(body)
56
+ text = re.sub(r"[ \t\r\f\v]+", " ", "".join(parser.parts))
57
+ return re.sub(r"\n\s*\n\s*", "\n\n", re.sub(r" ?\n ?", "\n", text)).strip()
58
+
59
+
60
+ class Web:
61
+ def fetch(self, url, start=0):
62
+ """The text of the page at `url` (http or https): an HTML page as its text with the
63
+ addresses of its links, anything else as it is; at most 100000 characters from
64
+ character `start`, then how to read on."""
65
+ url = str(url).strip()
66
+ if not url.startswith(("http://", "https://")):
67
+ raise ValueError("fetch takes an http or https address")
68
+ request = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0 (compatible; the-harness)",
69
+ "Accept": "text/html,text/plain,application/json,*/*"})
70
+ with urllib.request.urlopen(request, timeout=30) as response:
71
+ kind = (response.headers.get_content_type() or "").lower()
72
+ raw = response.read(PAGE_BYTES + 1)
73
+ charset = response.headers.get_content_charset() or "utf-8"
74
+ if not (kind.startswith("text/") or any(word in kind for word in ("json", "xml", "javascript", "html"))):
75
+ raise ValueError(f"{url} is {kind or 'of an unknown type'} ({len(raw)} bytes), not text")
76
+ try:
77
+ body = raw[:PAGE_BYTES].decode(charset, errors="replace")
78
+ except LookupError:
79
+ body = raw[:PAGE_BYTES].decode("utf-8", errors="replace")
80
+ text = page_text(body, kind)
81
+ start = max(0, int(start))
82
+ more = len(text) - start - PAGE_CHARS
83
+ return text[start:start + PAGE_CHARS] + (
84
+ f"\n... [{more} more characters: fetch(url, start={start + PAGE_CHARS})]" if more > 0 else "")
85
+
86
+ def search(self, query, results=5):
87
+ """Search the web for `query`: a list of at most `results` (1 to 10) {"title", "url",
88
+ "text"}. ValueError where the conversation has no search (runtime.search)."""
89
+ query = " ".join(str(query).split())
90
+ if not query:
91
+ raise ValueError("search takes what to search for")
92
+ return control.ask_host("web", action="search", query=query[:400], results=max(1, min(10, int(results))))
@@ -0,0 +1,9 @@
1
+ name = "web"
2
+ summary = "Read a web page and search the web from a program."
3
+ layer = "base"
4
+ mixin = "skills.web:Web"
5
+ exports = ["fetch", "search"]
6
+ # Where programs may reach the network (a conversation a user starts; an experiment that enables it).
7
+ optional = true
8
+ order = 85
9
+ embodiments = ["*"]
@@ -66,7 +66,9 @@ def _guarded(call) -> int:
66
66
  except KeyboardInterrupt:
67
67
  return 130
68
68
  except ChoiceRequired as error:
69
- print(json.dumps({"error": str(error), "choice_required": error.choice}), file=sys.stderr, flush=True)
69
+ # A person reads one line; a program reads the choice as JSON.
70
+ print(f"harness: {error}" if sys.stderr.isatty() else
71
+ json.dumps({"error": str(error), "choice_required": error.choice}), file=sys.stderr, flush=True)
70
72
  return 2
71
73
  except UsageError as error:
72
74
  print(json.dumps({"error": f"usage: {error}"}), file=sys.stderr, flush=True)
@@ -158,8 +160,8 @@ def _think_arguments(command):
158
160
 
159
161
  command.add_argument("--think", choices=[level for level in SCHEMA["model"]["think_while_acting"][2]
160
162
  if level != "default"],
161
- help="allow one speculative thought during a long program (on) or disable it (off). "
162
- "Legacy on_events and periodic act like on.")
163
+ help="accepted for older command lines; it has no effect (the model's turn is its own "
164
+ "while a program runs).")
163
165
  command.add_argument("--think-interval", type=float, metavar="SECONDS",
164
166
  help="accepted for older configurations. Decision scheduling is event-driven.")
165
167
 
@@ -260,12 +262,37 @@ def _experiment(arguments) -> int:
260
262
  return int(require("run_experiment", "commands", "program")(list(arguments)) or 0)
261
263
 
262
264
 
265
+ def _continue_arguments(command, name: str):
266
+ """The flags that go on with an earlier session in its own id instead of starting one."""
267
+ which = command.add_mutually_exclusive_group()
268
+ which.add_argument("--continue", dest="continue_last", action="store_true",
269
+ help="go on with the most recent session: its messages and facts, in the same session id")
270
+ which.add_argument(name, dest="session", metavar="SESSION",
271
+ help="go on with that session (its id, as harness sessions ls --all lists it)")
272
+
273
+
274
+ def _continued(args):
275
+ """(host, conversation, the last record's seq) of the session --continue or its id names,
276
+ continued in the session host; None when neither was given."""
277
+ if not (getattr(args, "continue_last", False) or getattr(args, "session", None)):
278
+ return None
279
+ from urllib.parse import quote
280
+
281
+ from .host import Host
282
+
283
+ host = Host.connect(log=_note)
284
+ conversation = host.resume_session(getattr(args, "session", None) or None)
285
+ records = host.get(f"/api/events?run={quote(conversation.run, safe='')}&after=-1") or []
286
+ return host, conversation, max((int(record.get("seq", -1)) for record in records), default=-1)
287
+
288
+
263
289
  def parser(packages: bool = True):
264
290
  root = argparse.ArgumentParser(prog="harness", description="Automatic Control for anything that acts.",
265
291
  epilog="harness with no command: the terminal chat with the agent on this computer "
266
292
  "(/help lists its commands: sessions, the session host, settings, model, "
267
293
  "packages, library)")
268
294
  root.add_argument("--version", action="version", version=f"harness {__version__}")
295
+ _continue_arguments(root, "--resume")
269
296
  run_flags(root)
270
297
  _coding_arguments(root)
271
298
  root.add_argument("--managed-workspace", action="store_true", help="use an isolated managed workspace (supported Linux hosts)")
@@ -285,6 +312,8 @@ def parser(packages: bool = True):
285
312
  help="learn this environment for the task instead of doing it: read, try the controls, write "
286
313
  "skills with tests, practise, for MINUTES of live time (default: the settings' "
287
314
  "learning.active_minutes); what it learns goes into the library")
315
+ # (harness run --resume OUTPUT is the eval package's: a session is named with --session here.)
316
+ _continue_arguments(run, "--session")
288
317
  run.add_argument("--library", type=Path, help="explicit reusable learning library; frozen unless --learn-while-working")
289
318
  run.add_argument("--learn-while-working", action="store_true", help="learn during the task into --library")
290
319
  run_flags(run)
@@ -368,6 +397,7 @@ def _services(args):
368
397
  else:
369
398
  report = _core_report()
370
399
  report["packages"] = Packages().installed()
400
+ _package_problems(report)
371
401
  if args.json:
372
402
  output(report)
373
403
  else:
@@ -387,6 +417,20 @@ def _services(args):
387
417
  return 0 if report["ok"] else 1
388
418
 
389
419
 
420
+ def _package_problems(report: dict) -> None:
421
+ """Installed packages whose host code does not fit this harness (host/workspaces.py
422
+ package_problems): the session host leaves them out, so they are problems of the report,
423
+ each with the command that fixes it."""
424
+ from .host.workspaces import package_problems
425
+
426
+ found = package_problems()
427
+ for row in report["packages"]:
428
+ if row["name"] in found and "problem" not in row:
429
+ row["problem"] = found[row["name"]]
430
+ report["problems"] = [*report.get("problems", []), *found.values()]
431
+ report["ok"] = report.get("ok", True) and not found
432
+
433
+
390
434
  def _core_report() -> dict:
391
435
  """harness doctor with nothing named: the harness itself and whether a model is set up."""
392
436
  from . import providers
@@ -644,7 +688,7 @@ def _starter(args):
644
688
  if selected is None:
645
689
  raise ValueError("add an enabled model in Settings or ~/.harness/models.json")
646
690
  model_entry = selected["id"]
647
- if selected["provider"].startswith(("acp:", "terminal:")):
691
+ if selected["provider"].startswith("terminal:"):
648
692
  coding["folder"] = Path.cwd()
649
693
  start.host, start.conversation = _task(text, workspace,
650
694
  runtime=runtime, files=files, log=log, **coding,
@@ -659,8 +703,16 @@ def _starter(args):
659
703
 
660
704
 
661
705
  def _turn_over(record, answered: bool) -> bool:
706
+ """Whether the turn is over with `record`: its result (turn_result), the conversation's
707
+ end, or an agent of another harness that answered and waits."""
662
708
  status = record.get("status") if record.get("event") == "state" else None
663
- return status in ENDED or (status == "IDLE" and (answered or record.get("reason") == "budget"))
709
+ return record.get("event") == "turn_result" or status in ENDED or (status == "IDLE" and answered)
710
+
711
+
712
+ def _ask(question: str) -> str:
713
+ """A line typed in answer to `question`, asked on stderr (stdout may be the answer's pipe)."""
714
+ print(question, end="", file=sys.stderr, flush=True)
715
+ return input().strip()
664
716
 
665
717
 
666
718
  def _choose_first_use(choice):
@@ -672,7 +724,7 @@ def _choose_first_use(choice):
672
724
  print(f" {index}. {option['label']}", file=sys.stderr)
673
725
  while True:
674
726
  try:
675
- answer = input("Choose a number (no default; q cancels): ").strip()
727
+ answer = _ask("Choose a number (no default; q cancels): ")
676
728
  except EOFError:
677
729
  raise UsageError("choice cancelled; task not started") from None
678
730
  if answer.lower() == "q":
@@ -696,7 +748,7 @@ def _choose_environment(choice):
696
748
  ask = f"Choose a number ({f'Enter: {default}' if default else 'no default'}; q cancels): "
697
749
  while True:
698
750
  try:
699
- answer = input(ask).strip()
751
+ answer = _ask(ask)
700
752
  except EOFError:
701
753
  raise UsageError("choice cancelled; task not started") from None
702
754
  if answer.lower() == "q":
@@ -712,15 +764,16 @@ def _choose_environment(choice):
712
764
  inside = any((parent / ".git").exists() for parent in (here, *here.parents))
713
765
  suggested = str(here if inside or not choice.get("folder") else choice["folder"])
714
766
  try:
715
- folder = input(f"Folder to work in [{suggested}]: ").strip() or suggested
767
+ folder = _ask(f"Folder to work in [{suggested}]: ") or suggested
716
768
  except EOFError:
717
769
  raise UsageError("choice cancelled; task not started") from None
718
770
  return row["value"], str(Path(folder).expanduser())
719
771
 
720
772
 
721
773
  def _run_task(args):
722
- """harness run "task": the agent on this computer does it and the command ends when it has
723
- answered; -p prints only the answer, --json every record."""
774
+ """harness run "task": the agent on this computer does it and the command ends with the
775
+ turn (its answer, or the limit, error or interrupt that ended it), closing the
776
+ conversation; -p prints only the answer, --json every record."""
724
777
  from .terminal import Printer
725
778
  from .transcript import answer_of
726
779
 
@@ -732,14 +785,22 @@ def _run_task(args):
732
785
 
733
786
  start = _starter(args)
734
787
  place = {}
788
+ continued = _continued(args)
789
+ after = -1
735
790
  while True:
791
+ if continued is not None:
792
+ # The task is the next message of that session; only what follows it is shown.
793
+ host, conversation, after = continued
794
+ conversation.send_message(args.task)
795
+ break
736
796
  try:
737
797
  host, conversation = start(args.task, learn=getattr(args, "learn", None),
738
798
  model_entry=args.model, reasoning_effort=args.effort, **place)
739
799
  break
740
800
  except ChoiceRequired as error:
741
- # Nobody to ask: the error names the flag; nothing is chosen for the user.
742
- if args.json or not (sys.stdin.isatty() and sys.stdout.isatty()):
801
+ # Nobody to ask: the error names the flag; nothing is chosen for the user. The
802
+ # question goes to stderr, so `answer=$(harness run "task" -p)` still asks.
803
+ if args.json or not (sys.stdin.isatty() and sys.stderr.isatty()):
743
804
  raise
744
805
  if error.choice.get("per_conversation"):
745
806
  place = dict(zip(("workspace", "folder"), _choose_environment(error.choice)))
@@ -760,7 +821,7 @@ def _run_task(args):
760
821
 
761
822
  watch = _watch_sub_agents(host, conversation, sub_agent)
762
823
  try:
763
- for record in conversation.records():
824
+ for record in conversation.records(after=after):
764
825
  if args.json:
765
826
  print(json.dumps(record, ensure_ascii=False), flush=True)
766
827
  elif not args.print:
@@ -853,7 +914,8 @@ def _watch_sub_agents(host, conversation, ask, seconds: float = 1.0) -> threadin
853
914
  def _rpc(args):
854
915
  """harness run rpc: one conversation driven by JSON lines on stdin, its records on stdout.
855
916
  In: {"type": "prompt", "text", "files"} (the first starts the conversation, later ones are
856
- messages; files: paths it attaches), {"type": "message", "text", "files"}, and the terminal
917
+ messages to it: after a turn_result record a message starts its next turn; files: paths
918
+ it attaches), {"type": "message", "text", "files"}, and the terminal
857
919
  chat's commands with a method (terminal.COMMANDS): pause, continue, {"type": "approve",
858
920
  "remember", "reason"}, {"type": "reject", "reason"}, end; {"type": "confirm", "accept",
859
921
  "reason", "remember"} is approve or reject, stop is end; with "agent", they answer that
@@ -972,7 +1034,11 @@ def _default(args):
972
1034
  raise UsageError("the terminal chat needs a terminal: harness run \"task\", or harness run rpc")
973
1035
  from . import tui
974
1036
 
975
- tui.chat(start=_starter(args))
1037
+ continued = _continued(args)
1038
+ if continued is not None:
1039
+ tui.chat(continued[1], host=continued[0], start=_starter(args))
1040
+ else:
1041
+ tui.chat(start=_starter(args))
976
1042
  return 0
977
1043
 
978
1044
 
@@ -999,7 +1065,7 @@ def _session_commands(parser):
999
1065
  reconnect.add_argument("session")
1000
1066
  reconnect.add_argument("--agent")
1001
1067
  reconnect.add_argument("--released", action="store_true", help="the previous controller has stopped")
1002
- for name in ("show", "stop", "cancel", "diff", "merge", "discard", "attach"):
1068
+ for name in ("show", "stop", "resume", "cancel", "diff", "merge", "discard", "attach"):
1003
1069
  actions.add_parser(name).add_argument("session")
1004
1070
  for name in ("send", "steer", "terminal"):
1005
1071
  send = actions.add_parser(name)
@@ -1056,6 +1122,8 @@ def _sessions(args):
1056
1122
  output(host.session(args.session).row)
1057
1123
  elif action == "stop":
1058
1124
  output({"stopped": host.stop_session(args.session)})
1125
+ elif action == "resume":
1126
+ output({"run": host.resume_session(args.session).run})
1059
1127
  elif action == "approve":
1060
1128
  output({"agent": host.approve(args.session, not args.reject, agent=args.agent, request_id=args.request_id)})
1061
1129
  else:
@@ -101,9 +101,14 @@ RUNTIME_KEYS = {
101
101
  "screenshot (a desktop's)",
102
102
  "default_max_seconds": "max_seconds of a bounded execution that names none",
103
103
  "fixed_skills": "direct mode: locate, pick and place in the action vocabulary",
104
- "isolation": "run programs isolated (default true; \"auto\": where user namespaces work)",
104
+ "isolation": "evaluation: run programs isolated, reaching only the installation, the source and the "
105
+ "environment's services (true; \"auto\": with namespaces where the machine allows them). false: "
106
+ "programs run on the machine as the user, under runtime.approvals. Experiments default to "
107
+ "\"auto\"; a conversation a user starts is not isolated",
105
108
  "keep_images": "with runtime.cameras: images of the most recent turns kept in the history (default 2). "
106
109
  "Without it the model says how long a view stays (look(keep=), pin=)",
110
+ "search": "the web search of search() (skills/web): {model, engine} on OpenRouter: a model id whose web plugin "
111
+ "searches, and optionally the plugin's engine (exa, native, ...). None: search() says there is none",
107
112
  "viewer": "written by the session host: the model role that describes a view for a main model that reads no "
108
113
  "images (the setting model.vision); none: such a model is told the view was not sent",
109
114
  "knowledge": "a game's knowledge: \"learned\" (default: use the agent's own library) or \"handwritten\" "
@@ -122,7 +127,12 @@ RUNTIME_KEYS = {
122
127
  "The active trigger's schedule (runtime/learning/active.py): active_before = true (before "
123
128
  "a task's runs, one run that learns its environment) and active_minutes = 1 to 240 (that run's live "
124
129
  "time, default 30)",
125
- "max_history_turns": "keep only this many exchanges after the first message",
130
+ "clear_results_after_minutes": "after this long without a reply of the model (the provider's cache of the "
131
+ "conversation is cold), the results of all programs but the last 5 are cleared "
132
+ "from the conversation (default 60; 0: never)",
133
+ "compact_at_tokens": "compaction: when the context a call of the model read reaches this many tokens, the model "
134
+ "writes a summary that replaces the conversation before its last messages (default 150000; "
135
+ "0: never)",
126
136
  "mode": "written by the resolution from the agent's mode",
127
137
  "actions": "written by the resolution: direct mode's action vocabulary (the agent's actions)",
128
138
  "physical": "the physical-input policy of this run over the user's settings (the computer package's "
@@ -149,10 +159,14 @@ RUNTIME_KEYS = {
149
159
  "post_policy_calls": "with learned policies: model calls after the first run of a handed-over task, then the policy runs on to the end of the episode (0: it runs on at once; default none: no bound)",
150
160
  "policy_progress_seconds": "a run of a learned policy also ends when no object the last locate tracks has moved 3 cm for this many seconds, measured every half of it (default none: only when it stops)",
151
161
  "strategy_first": "with learned policies: nothing moves before start(strategy, kind) (default false; the agent sets it)",
152
- "think_while_acting": "one speculative thought during a long program (default on). off disables speculation. "
153
- "Legacy on_events and periodic enable the same behavior. User messages and unresolved "
154
- "events are decision points in all modes",
162
+ "think_while_acting": "accepted for older profiles; it has no effect. Every program runs as a unit and the "
163
+ "model's turn is its own while one runs",
155
164
  "think_interval": "accepted for older profiles. Decision scheduling is event-driven",
165
+ "turn_grace_seconds": "how long a program may hold the model's turn (default 2): one that is over by then "
166
+ "gives the turn back with its result, a longer one goes on as a unit and the turn comes "
167
+ "back with its handle",
168
+ "longest_wait_seconds": "the longest a <wait> of the model lasts, and what a wait without a time gets "
169
+ "(default 60)",
156
170
  "thinking": "how long a call of the main model may think (runtime/conversation.py THINKING): model (the "
157
171
  "code's default: the model's own settings), none (reasoning off, replies of at most 2000 tokens, "
158
172
  "and the model is told to think briefly), low (reasoning effort at most low, 2000 tokens, told "
@@ -165,7 +179,11 @@ RUNTIME_KEYS = {
165
179
  "vlm": "written by the resolution: the vlm role's client config",
166
180
  "world_models": "written by the resolution: the world models' client configs",
167
181
  "mcp": "written by the resolution: the MCP servers' client configs",
168
- "approvals": "{ask} (core/approval.py): the recorded choice of sensitive steps or every program.",
182
+ "approvals": "{ask, programs, rules} (runtime/safety/approval.py, permissions.py): ask = sensitive or programs "
183
+ "(whether every program waits for the user); programs = the level of what a program does on the "
184
+ "machine beyond its own folder (a command, the network, a file written elsewhere): never, "
185
+ "ask_each, ask_once_session (default), allow; rules = [{level, action, match}] that win over it. "
186
+ "Where nobody can be asked a level that asks refuses.",
169
187
  "agent_skills": "the app: Agent Skills the user installed, {name: {description, path}} (skills/agent_skills)",
170
188
  "installed_skills": "the app: the Harness skills the user installed, by name (the prompt's installed section, "
171
189
  "runtime/decision/prompts.py)",
@@ -467,10 +485,16 @@ def resolve_agent(agent=None, *, model=None, deploy=None, strict: bool = False,
467
485
  if "approvals" in runtime:
468
486
  from harness.runtime.safety import approval
469
487
 
488
+ from harness.runtime.safety import permissions
489
+
470
490
  policy = runtime["approvals"]
471
- if (not isinstance(policy, dict) or set(policy) - {"ask"}
472
- or policy.get("ask", "sensitive") not in approval.ASK):
473
- raise ValueError("runtime.approvals takes ask = sensitive or programs")
491
+ if (not isinstance(policy, dict) or set(policy) - {"ask", "programs", "rules"}
492
+ or policy.get("ask", "sensitive") not in approval.ASK
493
+ or policy.get("programs", permissions.DEFAULT_LEVEL) not in approval.LEVELS):
494
+ raise ValueError("runtime.approvals takes ask = sensitive or programs, programs = one of "
495
+ f"{', '.join(approval.LEVELS)}, and rules")
496
+ if "rules" in policy:
497
+ policy["rules"] = permissions.rules(policy["rules"])
474
498
  if runtime.get("task_descriptions") is False:
475
499
  runtime.pop("task_descriptions")
476
500
  if tree.get("mode", "program") == "direct":
@@ -2,7 +2,8 @@
2
2
 
3
3
  An episode is a directory with an events.jsonl (runtime/conversation.py); its
4
4
  manifest.json and summary.json say what ran and how it ended. An episode without a
5
- summary is still being written (or was cut off).
5
+ summary, or whose summary is `open` (a conversation between two turns), is still being
6
+ written (or was cut off).
6
7
  """
7
8
 
8
9
  import json
@@ -84,7 +85,7 @@ def describe(root: Path, directory: Path) -> dict:
84
85
  "model": (config.get("main") or {}).get("model"),
85
86
  "interactive": "interactive" in manifest,
86
87
  "modified": trace.stat().st_mtime,
87
- "live": not done,
88
+ "live": not done or bool(done.get("open")),
88
89
  }
89
90
 
90
91
 
@@ -146,5 +147,5 @@ class Follow:
146
147
  return records
147
148
 
148
149
  def ended(self) -> bool:
149
- """Whether the episode has its summary (nothing more will be written)."""
150
- return (self.path.parent / "summary.json").is_file()
150
+ """Whether the episode has its last summary (nothing more will be written)."""
151
+ return bool(summary(self.path.parent)) and not summary(self.path.parent).get("open")
@@ -67,6 +67,9 @@ class Lanes:
67
67
  kind = record.get("event") or ""
68
68
  if kind in ("outcome", "interrupted") or kind == "state" and record.get("status") in ("FINISHED", "ERROR"):
69
69
  self.ended = True
70
+ elif kind == "state":
71
+ # It goes on: after a turn that was cancelled, or continued after it had ended.
72
+ self.ended = False
70
73
  self._mark(record, kind, time)
71
74
  if kind == "span_start":
72
75
  self.has_spans = True