@lmmslab/harness 0.81.1 → 0.83.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +24 -35
  2. package/package.json +2 -2
  3. package/python/harness/README.md +3 -3
  4. package/python/harness/__init__.py +1 -1
  5. package/python/harness/builtin/configs/assistant.toml +0 -1
  6. package/python/harness/builtin/configs/default.toml +1 -1
  7. package/python/harness/builtin/prompts/conversation/core.md +2 -2
  8. package/python/harness/builtin/prompts/core/core.md +197 -45
  9. package/python/harness/builtin/prompts/core/pack.toml +2 -2
  10. package/python/harness/builtin/prompts/reflect/core.md +4 -13
  11. package/python/harness/builtin/prompts/reflect/pack.toml +3 -2
  12. package/python/harness/builtin/prompts/shared/learning-goal.md +5 -29
  13. package/python/harness/builtin/prompts/shared/learning.md +8 -10
  14. package/python/harness/builtin/prompts/shared/web.md +11 -0
  15. package/python/harness/builtin/skills/agents/__init__.py +24 -22
  16. package/python/harness/builtin/skills/entries/__init__.py +43 -28
  17. package/python/harness/builtin/skills/entries/skill.toml +2 -3
  18. package/python/harness/builtin/skills/environments/__init__.py +2 -9
  19. package/python/harness/builtin/skills/mcp/__init__.py +2 -3
  20. package/python/harness/builtin/skills/memory/__init__.py +2 -5
  21. package/python/harness/builtin/skills/web/__init__.py +92 -0
  22. package/python/harness/builtin/skills/web/skill.toml +9 -0
  23. package/python/harness/cli.py +85 -17
  24. package/python/harness/config/agent.py +36 -9
  25. package/python/harness/core/episodes.py +5 -4
  26. package/python/harness/core/lanes.py +3 -0
  27. package/python/harness/core/memory.py +5 -1
  28. package/python/harness/core/remote.py +4 -3
  29. package/python/harness/core/worker.py +58 -3
  30. package/python/harness/extensions.py +23 -2
  31. package/python/harness/host/__init__.py +43 -1
  32. package/python/harness/host/client.py +36 -6
  33. package/python/harness/host/http.py +19 -0
  34. package/python/harness/host/server.py +17 -3
  35. package/python/harness/host/sessions.py +3 -2
  36. package/python/harness/host/tasks.py +107 -26
  37. package/python/harness/host/workspaces.py +69 -7
  38. package/python/harness/model_list.py +1 -1
  39. package/python/harness/packages/manager.py +5 -1
  40. package/python/harness/providers.py +14 -5
  41. package/python/harness/runtime/decision/loop.py +105 -117
  42. package/python/harness/runtime/decision/messages.py +158 -55
  43. package/python/harness/runtime/decision/model.py +85 -22
  44. package/python/harness/runtime/decision/notices.py +66 -0
  45. package/python/harness/runtime/decision/prompts.py +15 -11
  46. package/python/harness/runtime/decision/protocol.py +12 -6
  47. package/python/harness/runtime/decision/thoughts.py +10 -16
  48. package/python/harness/runtime/events.py +28 -13
  49. package/python/harness/runtime/execution/attached.py +1 -1
  50. package/python/harness/runtime/execution/control.py +17 -35
  51. package/python/harness/runtime/execution/entries.py +204 -124
  52. package/python/harness/runtime/execution/kernel.py +471 -281
  53. package/python/harness/runtime/execution/managed.py +231 -0
  54. package/python/harness/runtime/execution/noenv.py +2 -2
  55. package/python/harness/runtime/execution/results.py +2 -0
  56. package/python/harness/runtime/execution/session.py +284 -115
  57. package/python/harness/runtime/learning/active.py +41 -50
  58. package/python/harness/runtime/learning/fixtures.py +2 -2
  59. package/python/harness/runtime/learning/learner.py +75 -2
  60. package/python/harness/runtime/learning/library.py +77 -10
  61. package/python/harness/runtime/learning/views.py +12 -1
  62. package/python/harness/runtime/reactive/__init__.py +1 -1
  63. package/python/harness/runtime/reactive/rules.py +29 -394
  64. package/python/harness/runtime/reactive/tree.py +611 -0
  65. package/python/harness/runtime/safety/approval.py +8 -3
  66. package/python/harness/runtime/safety/permissions.py +242 -0
  67. package/python/harness/runtime/status.py +7 -2
  68. package/python/harness/runtime/task/delegation.py +97 -58
  69. package/python/harness/runtime/task/environments.py +3 -4
  70. package/python/harness/runtime/task/inbound.py +64 -0
  71. package/python/harness/runtime/task/run.py +1231 -790
  72. package/python/harness/runtime/tick.py +257 -91
  73. package/python/harness/sdk/__init__.py +7 -4
  74. package/python/harness/settings.py +30 -5
  75. package/python/harness/terminal.py +10 -6
  76. package/python/harness/transcript.py +18 -6
  77. package/python/harness/tui/answer.py +51 -0
  78. package/python/harness/tui/chat.py +75 -26
  79. package/python/harness/tui/views.py +1 -0
  80. package/python/harness/builtin/prompts/shared/entries.md +0 -86
@@ -2,17 +2,15 @@
2
2
  same time, each in a workspace of its own (or with the windows this agent hands over),
3
3
  and waits for their results. The conversation that runs the program starts and keeps
4
4
  them (runtime/task/delegation.py): every call here is a request to it, sent as a note, and its
5
- answer comes back through the kernel (runtime/execution/control.py). Each sub-agent is an entry of
6
- the session's tree (runtime/execution/entries.py) under the code that started it: when it ends, the
7
- conversation tells the kernel, and a sub-agent that did not finish is an ENTRY_FAILED
8
- event of that code."""
5
+ answer comes back through the kernel (runtime/execution/control.py). Each turn of a sub-agent is
6
+ a unit of the model (runtime/execution/entries.py): when it ends, the conversation tells the kernel,
7
+ the model hears of it, and a turn that ended without an answer is an ENTRY_FAILED event for
8
+ the handlers first."""
9
9
 
10
10
  import uuid
11
11
 
12
12
  from harness.sdk import spans
13
13
  from harness.sdk import control
14
- from harness.sdk import user_message
15
- from harness.sdk import Escalation
16
14
 
17
15
 
18
16
  # A sub-agent's statuses while it works on (runtime/task/delegation.py state()).
@@ -23,15 +21,9 @@ class Agents:
23
21
  if not spans.connected():
24
22
  raise RuntimeError("sub-agents need the conversation that runs this program; none is connected")
25
23
  identifier = uuid.uuid4().hex[:12]
26
- tree = getattr(self, "_entries", None)
27
- # The program's own code (not an entry it started) leaves a wait for a message for
28
- # the model, with a USER_MESSAGE event; the sub-agents go on.
29
- own = tree is None or tree.current() is tree.root
30
- spans.note("agents", action=operation, id=identifier, own=own, **detail)
31
- answer = control.wait_answer(identifier, stop=control.message_arrived if own else None)
24
+ spans.note("agents", action=operation, id=identifier, **detail)
25
+ answer = control.wait_answer(identifier)
32
26
  if answer is None:
33
- if own and control.message_arrived():
34
- raise Escalation(user_message(f"{operation}_agents" if operation == "wait" else operation, detail))
35
27
  return None
36
28
  if answer.get("error"):
37
29
  raise ValueError(answer["error"])
@@ -41,7 +33,7 @@ class Agents:
41
33
  """Start a sub-agent on `task` (what it is to do and what to report back); returns its
42
34
  id. `workspace`: "new" (a workspace of its own, of this one's kind), "browser",
43
35
  "desktop", "apps" or "display" (a new one of that kind), or "share" (it takes over the windows this agent
44
- holds now and hands them back when it ends). `budget`: {"calls", "cost", "seconds"},
36
+ holds now and hands them back when its turn ends). `budget`: {"calls", "cost", "seconds"},
45
37
  taken from this agent's own (default: all that is left). `model`: another model of
46
38
  the same provider. `agent`: another harness's agent ("claude-code", "codex") that does
47
39
  the task in its own bounded directory. `multi_turn=True` keeps a supported external
@@ -56,18 +48,28 @@ class Agents:
56
48
  return agent
57
49
 
58
50
  def wait_agents(self, ids=None, timeout=None):
59
- """Wait until each sub-agent in `ids` (default: all this agent started) has ended or
60
- asks this agent something, at most `timeout` seconds; returns {id: {"status",
61
- "result", "question", "model_calls", "cost_usd", "seconds", "directory", "files"}}, or
62
- None when the user paused this agent first. A message for this agent ends the wait
63
- at once with a USER_MESSAGE event; the sub-agents go on."""
51
+ """Wait until the turn of each sub-agent in `ids` (default: all this agent started) is
52
+ over, at most `timeout` seconds; returns {id: {"status", "result", "model_calls",
53
+ "cost_usd", "seconds", "directory", "files"}}, or None when the user paused this
54
+ agent first."""
64
55
  ids = [ids] if isinstance(ids, str) else (list(ids) if ids else None)
65
56
  ids = [self._agent_id(item) for item in ids] if ids else ids
66
57
  return self._agents("wait", ids=ids, timeout=timeout)
67
58
 
68
59
  def send_agent(self, agent, text):
69
- """Send sub-agent `agent` a message (the answer to its question, or more to do)."""
70
- return self._agents("send", agent=self._agent_id(agent), text=str(text))
60
+ """Send sub-agent `agent` a message (more to do, or the answer to what it asked). One
61
+ whose turn was over goes on with what it has, and the end of that turn reaches this
62
+ agent as the end of its first did."""
63
+ agent = self._agent_id(agent)
64
+ tree = getattr(self, "_entries", None)
65
+ # The turn the message starts is a unit of this agent, unless one of it still runs.
66
+ turn = tree.adopt_agent(agent, text) if tree is not None and not tree.agent_runs(agent) else None
67
+ try:
68
+ return self._agents("send", agent=agent, text=str(text))
69
+ except Exception:
70
+ if turn is not None:
71
+ tree.agent_ended(agent, {"status": "cancelled"}, cancelled=True)
72
+ raise
71
73
 
72
74
  def cancel_agent(self, agent):
73
75
  """Stop sub-agent `agent` (and those it started); returns its report."""
@@ -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
- root = argparse.ArgumentParser(prog="harness", description="Automatic Control for anything that acts.",
290
+ root = argparse.ArgumentParser(prog="harness", description="Automatic Control for intelligence in action.",
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: