@lmmslab/harness 0.83.0 → 0.85.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 (38) hide show
  1. package/bin/harness.js +6 -1
  2. package/package.json +1 -1
  3. package/python/harness/__init__.py +1 -1
  4. package/python/harness/builtin/prompts/core/core.md +68 -38
  5. package/python/harness/builtin/prompts/shared/learning-goal.md +1 -1
  6. package/python/harness/cli.py +140 -18
  7. package/python/harness/config/agent.py +3 -0
  8. package/python/harness/core/trace.py +23 -3
  9. package/python/harness/core/worker.py +6 -0
  10. package/python/harness/host/client.py +26 -21
  11. package/python/harness/host/http.py +11 -1
  12. package/python/harness/host/sessions.py +38 -3
  13. package/python/harness/host/tasks.py +71 -27
  14. package/python/harness/instructions.py +329 -0
  15. package/python/harness/model_list.py +25 -5
  16. package/python/harness/packages/environments.py +51 -12
  17. package/python/harness/packages/manager.py +379 -102
  18. package/python/harness/packages/resolve.py +52 -15
  19. package/python/harness/packages/sources.py +17 -5
  20. package/python/harness/packages/state.py +10 -0
  21. package/python/harness/providers.py +66 -24
  22. package/python/harness/runtime/decision/model.py +9 -4
  23. package/python/harness/runtime/events.py +1 -0
  24. package/python/harness/runtime/execution/entries.py +53 -7
  25. package/python/harness/runtime/execution/session.py +57 -10
  26. package/python/harness/runtime/learning/active.py +4 -4
  27. package/python/harness/runtime/reactive/tree.py +96 -53
  28. package/python/harness/runtime/task/environments.py +3 -1
  29. package/python/harness/runtime/task/run.py +93 -4
  30. package/python/harness/runtime/tick.py +34 -3
  31. package/python/harness/settings.py +23 -1
  32. package/python/harness/terminal.py +3 -0
  33. package/python/harness/transcript.py +12 -0
  34. package/python/harness/tui/__init__.py +28 -6
  35. package/python/harness/tui/chat.py +323 -80
  36. package/python/harness/tui/pty.py +16 -0
  37. package/python/harness/tui/renderer.py +1 -0
  38. package/python/harness/tui/views.py +160 -38
@@ -35,6 +35,11 @@ def attach(conversation, host):
35
35
  raise ValueError("attaching a hosted PTY requires an interactive terminal")
36
36
  stopped = threading.Event()
37
37
  failures = []
38
+ resize = getattr(conversation, "terminal_resize", None)
39
+ agent = getattr(conversation, "agent", None)
40
+ if isinstance(agent, dict) and not (agent.get("capabilities") or {}).get("terminal_resize"):
41
+ resize = None # An older host can still attach without the new capability.
42
+ previous_size = None
38
43
  history = host.get(f"/api/events?run={quote(conversation.run, safe='')}&after=-1") or []
39
44
  cursor = max((r["seq"] for r in history), default=-1)
40
45
 
@@ -62,6 +67,17 @@ def attach(conversation, host):
62
67
  tty.setraw(input_fd)
63
68
  reader.start()
64
69
  while not stopped.is_set():
70
+ if resize is not None:
71
+ size = os.get_terminal_size(output_fd)
72
+ if size.columns > 0 and size.lines > 0 and size != previous_size:
73
+ try:
74
+ resize(size.columns, size.lines)
75
+ except (OSError, RuntimeError, ValueError):
76
+ # A resize can race the remote program's normal exit. Keep
77
+ # forwarding its output; resizing is an optional capability.
78
+ resize = None
79
+ else:
80
+ previous_size = size
65
81
  if not select.select([input_fd], [], [], .1)[0]:
66
82
  continue
67
83
  data = os.read(input_fd, 4096)
@@ -43,3 +43,4 @@ class WindowsInlineRenderer(Renderer):
43
43
  def erase(self, leave_alternate_screen=True):
44
44
  self._reflow_cursor()
45
45
  super().erase(leave_alternate_screen)
46
+
@@ -4,16 +4,20 @@ packages add), the packages (packages/describe.py, as harness install / remove /
4
4
  the libraries of what the agent learned (runtime/learning/learner.py) and the timeline
5
5
  (core/lanes.py: the GUI's lanes, as bars under an axis)."""
6
6
 
7
+ import asyncio
7
8
  import threading
8
9
  import time
9
10
 
10
- from prompt_toolkit.application import Application
11
+ from prompt_toolkit.application import Application, in_terminal
11
12
  from prompt_toolkit.buffer import Buffer
12
13
  from prompt_toolkit.data_structures import Point
14
+ from prompt_toolkit.document import Document
13
15
  from prompt_toolkit.key_binding import KeyBindings
14
16
  from prompt_toolkit.layout import HSplit, Layout, Window
15
17
  from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
16
18
  from prompt_toolkit.styles import Style
19
+ from rich.console import Console
20
+ from rich.text import Text
17
21
 
18
22
  from ..core import format as formats
19
23
  from ..core import lanes as timeline_lanes
@@ -80,13 +84,28 @@ async def choose(title: str, rows: list[str], *, hint: str = "", current: int =
80
84
  def _(event):
81
85
  event.app.exit(result=None)
82
86
 
87
+ @keys.add("f2")
88
+ async def _(event):
89
+ found = shown()
90
+ if found:
91
+ state["at"] = min(state["at"], len(found) - 1)
92
+ async with in_terminal(render_cli_done=False):
93
+ await read("Selected option · Esc returns to the list", rows[found[state["at"]]])
94
+
83
95
  lines = [Window(FormattedTextControl([("class:title", title)]), height=1),
84
96
  Window(BufferControl(query), height=1, get_line_prefix=lambda *_: [("class:dim", "filter: ")]),
85
97
  Window(height=1, char="─", style="class:dim"),
86
98
  Window(FormattedTextControl(listing, focusable=False, get_cursor_position=lambda: Point(0, state["at"])),
87
99
  wrap_lines=False),
88
- Window(FormattedTextControl([("class:dim", hint or "↑↓ move · Enter pick · type to filter · Esc close")]),
100
+ Window(FormattedTextControl([("class:dim", "F2 full row · " + (hint or "↑↓ move · Enter pick · type filter · Esc close"))]),
89
101
  height=1)]
102
+ def selected():
103
+ found = shown()
104
+ state["at"] = min(state["at"], max(0, len(found) - 1))
105
+ return rows[found[state["at"]]] if found else ""
106
+
107
+ lines.insert(-1, Window(FormattedTextControl(selected),
108
+ height=3, wrap_lines=True))
90
109
  if text:
91
110
  lines.insert(1, Window(FormattedTextControl(text), wrap_lines=True, dont_extend_height=True))
92
111
  app = Application(Layout(HSplit(lines), focused_element=lines[1]), key_bindings=keys, style=STYLE,
@@ -117,20 +136,32 @@ async def ask(title: str, default: str = "", hint: str = "Enter keep · Esc canc
117
136
  return await app.run_async()
118
137
 
119
138
 
120
- async def read(title: str, text, *, close_key=None) -> None:
139
+ async def read(title: str, text, *, close_key=None, live=None) -> None:
121
140
  """Read text (or a live text callback) on an alternate screen, then restore chat."""
122
- def lines():
123
- return (text() if callable(text) else text).splitlines() or [""]
141
+ cache = {"key": None, "lines": [""], "text": ""}
124
142
  state = {"at": 0}
125
143
  keys = KeyBindings()
126
144
 
145
+ def lines():
146
+ value = str(text() if callable(text) else text)
147
+ width = max(1, app.output.get_size().columns)
148
+ if cache["key"] != (value, width):
149
+ console = Console(width=width, color_system=None)
150
+ cache["lines"] = [line.plain for line in Text(value).wrap(console, width, overflow="fold")] or [""]
151
+ cache["text"] = "\n".join(cache["lines"])
152
+ cache["key"] = (value, width)
153
+ state["at"] = min(state["at"], len(cache["lines"]) - 1)
154
+ return cache["lines"]
155
+
127
156
  def move(step):
128
157
  state["at"] = max(0, min(state["at"] + step, len(lines()) - 1))
129
158
 
130
159
  keys.add("up")(lambda event: move(-1))
131
160
  keys.add("down")(lambda event: move(1))
132
- keys.add("pageup")(lambda event: move(-20))
133
- keys.add("pagedown")(lambda event: move(20))
161
+ keys.add("pageup")(lambda event: move(-max(1, event.app.output.get_size().rows - 3)))
162
+ keys.add("pagedown")(lambda event: move(max(1, event.app.output.get_size().rows - 3)))
163
+ keys.add("home")(lambda event: state.update(at=0))
164
+ keys.add("end")(lambda event: state.update(at=len(lines()) - 1))
134
165
 
135
166
  @keys.add("escape", eager=True)
136
167
  @keys.add("q")
@@ -141,47 +172,109 @@ async def read(title: str, text, *, close_key=None) -> None:
141
172
  if close_key:
142
173
  keys.add(close_key)(lambda event: event.app.exit())
143
174
 
144
- body = Window(FormattedTextControl(lambda: "\n".join(lines()), get_cursor_position=lambda: Point(0, state["at"])),
145
- wrap_lines=True)
146
- await Application(Layout(HSplit([Window(FormattedTextControl([("class:title", title)]), height=1), body,
147
- Window(FormattedTextControl([("class:dim", "↑↓ PgUp PgDn scroll · Esc close")]),
175
+ buffer = Buffer(read_only=True)
176
+
177
+ def update(app):
178
+ lines()
179
+ if buffer.text != cache["text"]:
180
+ buffer.set_document(Document(cache["text"]), bypass_readonly=True)
181
+ buffer.cursor_position = buffer.document.translate_row_col_to_index(state["at"], 0)
182
+
183
+ body = Window(BufferControl(buffer), wrap_lines=False)
184
+ app = Application(Layout(HSplit([Window(FormattedTextControl([("class:title", title)]), height=1), body,
185
+ Window(FormattedTextControl([("class:dim", "↑↓ PgUp PgDn Home End scroll · Esc close")]),
148
186
  height=1)])), key_bindings=keys, style=STYLE, full_screen=True,
149
- refresh_interval=0.25 if callable(text) else None).run_async()
187
+ refresh_interval=0.25 if (callable(text) if live is None else live) else None,
188
+ before_render=update)
189
+ await app.run_async()
190
+
150
191
 
192
+ # A dismissed mutation still runs in its host/thread; it has no UI owner afterward.
193
+ _DISMISSED = object()
151
194
 
152
- async def working(title: str, function):
153
- """`function(log)` on a thread of its own, full screen with the lines it logs, until it
154
- returns: (its result, None), or (None, the error)."""
195
+
196
+ async def working(title: str, function, *, cancellable=False, dismissible=False):
197
+ """Run off the UI loop. Read waits can be cancelled; mutation waits can be left.
198
+
199
+ Returns (result, error), or (_DISMISSED, None) when the operation keeps running.
200
+ """
155
201
  said, done = [], {}
202
+ closed, lock = threading.Event(), threading.Lock()
203
+
204
+ def log(message):
205
+ with lock:
206
+ if not closed.is_set():
207
+ said.append(str(message))
208
+
209
+ def finish():
210
+ if not closed.is_set() and app.is_running:
211
+ app.exit()
156
212
 
157
213
  def run():
158
214
  try:
159
- done["result"] = function(lambda message: said.append(str(message)))
160
- except Exception as error:
161
- done["error"] = error
162
- app.exit()
215
+ result, error = function(log), None
216
+ except Exception as problem:
217
+ result, error = None, problem
218
+ with lock:
219
+ if closed.is_set():
220
+ return
221
+ done.update(result=result, error=error)
222
+ if app.is_running:
223
+ app.loop.call_soon_threadsafe(finish)
224
+
225
+ keys = KeyBindings()
163
226
 
164
- app = Application(Layout(HSplit([Window(FormattedTextControl([("class:title", f"{title} ...")]), height=1),
227
+ if cancellable or dismissible:
228
+ @keys.add("escape", eager=True)
229
+ @keys.add("c-c")
230
+ def _(event):
231
+ with lock:
232
+ closed.set()
233
+ if dismissible:
234
+ done["dismissed"] = True
235
+ else:
236
+ done["error"] = InterruptedError("Stopped waiting; the host request may still finish.")
237
+ event.app.exit()
238
+
239
+ hint = " · Esc return; operation keeps running" if dismissible else " · Esc cancel" if cancellable else ""
240
+ app = Application(Layout(HSplit([Window(FormattedTextControl([("class:title", f"{title} ...{hint}")]), height=1),
165
241
  Window(FormattedTextControl(lambda: "\n".join(said[-40:])), wrap_lines=True)])),
166
- style=STYLE, full_screen=True, refresh_interval=0.3)
242
+ style=STYLE, key_bindings=keys, full_screen=True, refresh_interval=0.3)
167
243
  app.pre_run_callables.append(lambda: threading.Thread(target=run, daemon=True).start())
168
- await app.run_async()
244
+ try:
245
+ await app.run_async()
246
+ finally:
247
+ closed.set()
248
+ if done.get("dismissed"):
249
+ return _DISMISSED, None
169
250
  return done.get("result"), done.get("error")
170
251
 
171
252
 
172
- async def packages_view() -> list[str]:
253
+ async def _load(title, function):
254
+ value, error = await working(title, lambda log: function(), cancellable=True)
255
+ if error:
256
+ raise error
257
+ return value
258
+
259
+
260
+ async def packages_view(host) -> list[str]:
173
261
  """The installed packages and the registry's, each with what it provides; Enter on one
174
262
  shows all of it and what can be done: install, update, turn on or off, remove (asked
175
263
  first). Returns what changed."""
176
- from ..packages import Packages
264
+ from urllib.parse import quote
265
+
266
+ from ..packages import activate
177
267
  from ..packages.describe import COLUMNS, describe, rows
178
268
 
269
+ # A previous dismissed host mutation may have completed since this view closed.
270
+ await asyncio.to_thread(activate)
179
271
  changed, registry = [], False
180
272
  while True:
181
273
  # The registry may be read over the network: on a thread, as installing is.
182
- found, problem = await working("Reading the packages", lambda log: Packages().search("") if registry
183
- else Packages().installed())
184
- found, which = found or [], "available" if registry else "installed"
274
+ payload = await _load("Reading the packages", lambda: host.get("/api/app/packages/search?q=") if registry
275
+ else host.get("/api/app/packages"))
276
+ problem = payload.get("error")
277
+ found, which = payload.get("results" if registry else "packages", []), "available" if registry else "installed"
185
278
  table, columns = rows(found, which, 40), COLUMNS[which]
186
279
  widths = {column: min(max([len(column)] + [len(str(row[column])) for row in table]), 40) for column in columns}
187
280
  lines = ["↔ " + ("the installed packages" if registry else "the registry's packages")] + [
@@ -197,7 +290,8 @@ async def packages_view() -> list[str]:
197
290
  continue
198
291
  row = found[picked - 1]
199
292
  try:
200
- about = "\n".join(describe(Packages().info(row["name"]))["lines"])
293
+ info = await _load("Reading package details", lambda: host.get("/api/app/packages/info?name=" + quote(row["name"])))
294
+ about = "\n".join((info.get("shown") or describe(info))["lines"])
201
295
  except Exception as error:
202
296
  about = f"{row['name']}: {error}"
203
297
  if registry:
@@ -214,10 +308,25 @@ async def packages_view() -> list[str]:
214
308
  if action == "remove" else ""), ["no", "yes"]) != 1:
215
309
  continue
216
310
  if action.startswith("turn"):
217
- calls = lambda log: Packages().enable(name, action.endswith("on")) # noqa: E731
311
+ calls = lambda log: host.post("/api/app/packages/enable", {"name": name, "on": action.endswith("on")}, timeout=3600) # noqa: E731
218
312
  else:
219
- calls = lambda log: getattr(Packages(), action)([name], log=log) # noqa: E731
220
- _, error = await working(f"{action.capitalize()} {name}", calls)
313
+ body = {"source": name} if action == "install" else {"names": [name]} if action == "update" else {"name": name}
314
+ calls = lambda log: host.post("/api/app/packages/" + action, body, timeout=3600) # noqa: E731
315
+ result, error = await working(f"{action.capitalize()} {name}", calls, dismissible=True)
316
+ if result is _DISMISSED:
317
+ return [*changed, "Stopped waiting; check /packages for outcome"]
318
+ if not error and result and result.get("needs_trust"):
319
+ # Non-registry sources require a separate explicit trust decision.
320
+ if await choose("Trust and install this package?", ["no", "yes"],
321
+ text="\n".join(str(row) for row in result["found"])) == 1:
322
+ result, error = await working(f"Install {name}", lambda log: host.post(
323
+ "/api/app/packages/install", {"source": name, "trust": True}, timeout=3600), dismissible=True)
324
+ if result is _DISMISSED:
325
+ return [*changed, "Stopped waiting; check /packages for outcome"]
326
+ else:
327
+ continue
328
+ if not error:
329
+ await asyncio.to_thread(activate)
221
330
  changed.append(f"{action} {name}: " + (f"failed: {error}" if error else "done"))
222
331
  if error:
223
332
  await read(f"{action} {name} failed", str(error))
@@ -233,7 +342,7 @@ async def library_view(memory=None) -> None:
233
342
  from harness.core.memory import COLUMNS, rows as memory_rows
234
343
 
235
344
  while True:
236
- found = learner.libraries()
345
+ found = await _load("Reading libraries", learner.libraries)
237
346
  lines = [f"{row['name']:<28} {row['entries']:>4} entries version {row['version'][:12]}" for row in found]
238
347
  picked = await choose("Memory: this conversation's and the libraries'",
239
348
  (["this conversation: what it saw and where, and its library"] if memory else []) + lines,
@@ -248,7 +357,7 @@ async def library_view(memory=None) -> None:
248
357
  title, load = f"{row['name']}: version {row['version'][:12]}", \
249
358
  (lambda directory=Path(row["directory"]): memory_rows(library=directory))
250
359
  while True:
251
- entries = load()
360
+ entries = await _load("Reading memory", load)
252
361
  at = await choose(title, [" ".join(f"{cell:<12}" for cell in row["cells"][:7]) for row in entries],
253
362
  hint="Enter read · type to filter · Esc back")
254
363
  if at is None:
@@ -274,7 +383,8 @@ async def settings_view(directory) -> list[str]:
274
383
  store, changed, at, note = Settings(directory), [], 0, first("note")
275
384
  while True:
276
385
  values = store.get()
277
- found = [row for row in rows(values) if row["setting"] != "model.reasoning_effort"]
386
+ found = [row for row in rows(values) if row["section"] != "general" and row["setting"] not in
387
+ ("model.reasoning_effort", "model.think_while_acting", "model.think_interval")]
278
388
  lines = [f"{row['setting']:<36} {_shown(row)}" for row in found]
279
389
  at = await choose("Settings" + (f" {note(values)}" if note else ""), lines, current=at,
280
390
  hint="Enter change · type to filter · Esc close · /models edits the model list")
@@ -382,7 +492,7 @@ LANE_KINDS = ("model", "verifier", "program", "agent", "env")
382
492
  async def timeline_view(lanes, feed=None) -> None:
383
493
  """`lanes` full screen, drawn again twice a second (`feed()` first adds what came);
384
494
  + and - zoom, 0 shows the whole run, Esc, q or Ctrl+T close."""
385
- window = {"seconds": None}
495
+ window = {"seconds": None, "at": 0, "count": 1}
386
496
  keys = KeyBindings()
387
497
 
388
498
  @keys.add("escape", eager=True)
@@ -406,15 +516,27 @@ async def timeline_view(lanes, feed=None) -> None:
406
516
  def _(event):
407
517
  window["seconds"] = None
408
518
 
519
+ def move(step):
520
+ window["at"] = max(0, min(window["at"] + step, window["count"] - 1))
521
+
522
+ keys.add("up")(lambda event: move(-1))
523
+ keys.add("down")(lambda event: move(1))
524
+ keys.add("pageup")(lambda event: move(-max(1, event.app.output.get_size().rows - 3)))
525
+ keys.add("pagedown")(lambda event: move(max(1, event.app.output.get_size().rows - 3)))
526
+ keys.add("home")(lambda event: window.update(at=0))
527
+ keys.add("end")(lambda event: window.update(at=window["count"] - 1))
528
+
409
529
  def draw():
410
530
  if feed is not None:
411
531
  feed()
412
- return timeline_lines(lanes, app.output.get_size().columns, window["seconds"])
532
+ lines = timeline_lines(lanes, app.output.get_size().columns, window["seconds"])
533
+ window["count"] = 1 + sum(text.count("\n") for _, text in lines)
534
+ return lines
413
535
 
414
536
  app = Application(Layout(HSplit([
415
537
  Window(FormattedTextControl([("class:title", "Timeline")]), height=1),
416
- Window(FormattedTextControl(draw), wrap_lines=False),
417
- Window(FormattedTextControl([("class:dim", "+ - zoom · 0 whole run · Esc close")]), height=1)])),
538
+ Window(FormattedTextControl(draw, get_cursor_position=lambda: Point(0, window["at"])), wrap_lines=False),
539
+ Window(FormattedTextControl([("class:dim", "↑↓ PgUp PgDn scroll · + - zoom · 0 whole run · Esc close")]), height=1)])),
418
540
  key_bindings=keys, style=STYLE, full_screen=True, refresh_interval=0.5)
419
541
  await app.run_async()
420
542