@lmmslab/harness 0.0.0-stage → 0.81.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 (170) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +124 -2
  3. package/bin/harness.js +71 -0
  4. package/package.json +34 -4
  5. package/python/harness/README.md +72 -0
  6. package/python/harness/__init__.py +8 -0
  7. package/python/harness/__main__.py +3 -0
  8. package/python/harness/bench.py +124 -0
  9. package/python/harness/builtin/benches/README.md +5 -0
  10. package/python/harness/builtin/benches/none/bench.toml +6 -0
  11. package/python/harness/builtin/configs/README.md +26 -0
  12. package/python/harness/builtin/configs/assistant.toml +11 -0
  13. package/python/harness/builtin/configs/default.toml +33 -0
  14. package/python/harness/builtin/configs/deploy/example-api.toml +10 -0
  15. package/python/harness/builtin/configs/models/deepseek-api.toml +21 -0
  16. package/python/harness/builtin/configs/models/deepseek-flash.toml +12 -0
  17. package/python/harness/builtin/configs/models/grok-low.toml +8 -0
  18. package/python/harness/builtin/configs/models/grok-medium.toml +43 -0
  19. package/python/harness/builtin/configs/models/opus-5.5.toml +11 -0
  20. package/python/harness/builtin/environment/README.md +53 -0
  21. package/python/harness/builtin/environment/constraints.txt +31 -0
  22. package/python/harness/builtin/model-settings.js +143 -0
  23. package/python/harness/builtin/prompts/conversation/core.md +5 -0
  24. package/python/harness/builtin/prompts/conversation/pack.toml +12 -0
  25. package/python/harness/builtin/prompts/core/core.md +88 -0
  26. package/python/harness/builtin/prompts/core/pack.toml +9 -0
  27. package/python/harness/builtin/prompts/reflect/core.md +45 -0
  28. package/python/harness/builtin/prompts/reflect/pack.toml +7 -0
  29. package/python/harness/builtin/prompts/shared/agent-skills.md +5 -0
  30. package/python/harness/builtin/prompts/shared/agents.md +14 -0
  31. package/python/harness/builtin/prompts/shared/checklist.md +21 -0
  32. package/python/harness/builtin/prompts/shared/entries.md +86 -0
  33. package/python/harness/builtin/prompts/shared/environments.md +21 -0
  34. package/python/harness/builtin/prompts/shared/learning-goal.md +31 -0
  35. package/python/harness/builtin/prompts/shared/learning.md +27 -0
  36. package/python/harness/builtin/prompts/shared/mcp.md +9 -0
  37. package/python/harness/builtin/prompts/shared/world.md +1 -0
  38. package/python/harness/builtin/skills/README.md +10 -0
  39. package/python/harness/builtin/skills/agents/__init__.py +88 -0
  40. package/python/harness/builtin/skills/agents/skill.toml +11 -0
  41. package/python/harness/builtin/skills/checklist/__init__.py +32 -0
  42. package/python/harness/builtin/skills/checklist/skill.toml +9 -0
  43. package/python/harness/builtin/skills/conversation/__init__.py +23 -0
  44. package/python/harness/builtin/skills/conversation/skill.toml +8 -0
  45. package/python/harness/builtin/skills/entries/__init__.py +62 -0
  46. package/python/harness/builtin/skills/entries/skill.toml +8 -0
  47. package/python/harness/builtin/skills/environments/__init__.py +69 -0
  48. package/python/harness/builtin/skills/environments/skill.toml +9 -0
  49. package/python/harness/builtin/skills/experience/__init__.py +27 -0
  50. package/python/harness/builtin/skills/experience/skill.toml +9 -0
  51. package/python/harness/builtin/skills/instructions/__init__.py +29 -0
  52. package/python/harness/builtin/skills/instructions/skill.toml +9 -0
  53. package/python/harness/builtin/skills/learning/__init__.py +189 -0
  54. package/python/harness/builtin/skills/learning/skill.toml +7 -0
  55. package/python/harness/builtin/skills/mcp/__init__.py +54 -0
  56. package/python/harness/builtin/skills/mcp/skill.toml +10 -0
  57. package/python/harness/builtin/skills/memory/__init__.py +130 -0
  58. package/python/harness/builtin/skills/memory/skill.toml +13 -0
  59. package/python/harness/builtin/skills/verify/__init__.py +31 -0
  60. package/python/harness/builtin/skills/verify/skill.toml +7 -0
  61. package/python/harness/builtin/third_party/sources.toml +40 -0
  62. package/python/harness/cli.py +1182 -0
  63. package/python/harness/config/__init__.py +224 -0
  64. package/python/harness/config/agent.py +777 -0
  65. package/python/harness/core/__init__.py +52 -0
  66. package/python/harness/core/artifacts.py +46 -0
  67. package/python/harness/core/codec.py +160 -0
  68. package/python/harness/core/contracts.py +75 -0
  69. package/python/harness/core/episodes.py +150 -0
  70. package/python/harness/core/format.py +51 -0
  71. package/python/harness/core/http.py +124 -0
  72. package/python/harness/core/lanes.py +324 -0
  73. package/python/harness/core/linux_sandbox.py +55 -0
  74. package/python/harness/core/media.py +96 -0
  75. package/python/harness/core/memory.py +147 -0
  76. package/python/harness/core/ports.py +63 -0
  77. package/python/harness/core/procs.py +227 -0
  78. package/python/harness/core/remote.py +102 -0
  79. package/python/harness/core/spans.py +166 -0
  80. package/python/harness/core/trace.py +139 -0
  81. package/python/harness/core/wait.py +12 -0
  82. package/python/harness/core/worker.py +388 -0
  83. package/python/harness/core/worker_pipe.py +119 -0
  84. package/python/harness/embodiments.py +217 -0
  85. package/python/harness/extensions.py +112 -0
  86. package/python/harness/host/__init__.py +397 -0
  87. package/python/harness/host/client.py +201 -0
  88. package/python/harness/host/http.py +720 -0
  89. package/python/harness/host/listing.py +68 -0
  90. package/python/harness/host/server.py +189 -0
  91. package/python/harness/host/sessions.py +406 -0
  92. package/python/harness/host/tasks.py +1308 -0
  93. package/python/harness/host/workers.py +257 -0
  94. package/python/harness/host/workspaces.py +256 -0
  95. package/python/harness/model_list.py +117 -0
  96. package/python/harness/packages/__init__.py +73 -0
  97. package/python/harness/packages/addons.py +121 -0
  98. package/python/harness/packages/archive.py +122 -0
  99. package/python/harness/packages/describe.py +46 -0
  100. package/python/harness/packages/environments.py +448 -0
  101. package/python/harness/packages/fetch.py +57 -0
  102. package/python/harness/packages/manager.py +587 -0
  103. package/python/harness/packages/manifest.py +221 -0
  104. package/python/harness/packages/migrate.py +68 -0
  105. package/python/harness/packages/provision.py +38 -0
  106. package/python/harness/packages/registry.py +275 -0
  107. package/python/harness/packages/resolve.py +89 -0
  108. package/python/harness/packages/sources.py +141 -0
  109. package/python/harness/packages/state.py +213 -0
  110. package/python/harness/packages/versions.py +112 -0
  111. package/python/harness/paths.py +218 -0
  112. package/python/harness/providers.py +303 -0
  113. package/python/harness/runtime/__init__.py +8 -0
  114. package/python/harness/runtime/decision/__init__.py +1 -0
  115. package/python/harness/runtime/decision/loop.py +198 -0
  116. package/python/harness/runtime/decision/messages.py +271 -0
  117. package/python/harness/runtime/decision/model.py +424 -0
  118. package/python/harness/runtime/decision/prompts.py +248 -0
  119. package/python/harness/runtime/decision/protocol.py +169 -0
  120. package/python/harness/runtime/decision/thoughts.py +199 -0
  121. package/python/harness/runtime/events.py +213 -0
  122. package/python/harness/runtime/execution/__init__.py +1 -0
  123. package/python/harness/runtime/execution/attached.py +96 -0
  124. package/python/harness/runtime/execution/control.py +211 -0
  125. package/python/harness/runtime/execution/entries.py +665 -0
  126. package/python/harness/runtime/execution/kernel.py +811 -0
  127. package/python/harness/runtime/execution/noenv.py +84 -0
  128. package/python/harness/runtime/execution/results.py +40 -0
  129. package/python/harness/runtime/execution/session.py +871 -0
  130. package/python/harness/runtime/execution/skills.py +436 -0
  131. package/python/harness/runtime/learning/__init__.py +1 -0
  132. package/python/harness/runtime/learning/active.py +314 -0
  133. package/python/harness/runtime/learning/experience.py +48 -0
  134. package/python/harness/runtime/learning/fixtures.py +167 -0
  135. package/python/harness/runtime/learning/learner.py +651 -0
  136. package/python/harness/runtime/learning/library.py +504 -0
  137. package/python/harness/runtime/learning/reflect.py +151 -0
  138. package/python/harness/runtime/learning/usage.py +30 -0
  139. package/python/harness/runtime/learning/views.py +89 -0
  140. package/python/harness/runtime/reactive/__init__.py +1 -0
  141. package/python/harness/runtime/reactive/rules.py +549 -0
  142. package/python/harness/runtime/safety/__init__.py +1 -0
  143. package/python/harness/runtime/safety/approval.py +131 -0
  144. package/python/harness/runtime/safety/input.py +62 -0
  145. package/python/harness/runtime/safety/isolation.py +419 -0
  146. package/python/harness/runtime/safety/shield.py +129 -0
  147. package/python/harness/runtime/status.py +23 -0
  148. package/python/harness/runtime/task/__init__.py +7 -0
  149. package/python/harness/runtime/task/context.py +431 -0
  150. package/python/harness/runtime/task/delegation.py +468 -0
  151. package/python/harness/runtime/task/environments.py +268 -0
  152. package/python/harness/runtime/task/run.py +2878 -0
  153. package/python/harness/runtime/tick.py +909 -0
  154. package/python/harness/sdk/__init__.py +281 -0
  155. package/python/harness/services/__init__.py +6 -0
  156. package/python/harness/services/broker.py +35 -0
  157. package/python/harness/services/connectors.py +88 -0
  158. package/python/harness/services/mcp.py +298 -0
  159. package/python/harness/services/mcp_stdio.py +131 -0
  160. package/python/harness/services/placement.py +82 -0
  161. package/python/harness/services/registry.py +156 -0
  162. package/python/harness/settings.py +536 -0
  163. package/python/harness/terminal.py +79 -0
  164. package/python/harness/transcript.py +297 -0
  165. package/python/harness/tui/__init__.py +68 -0
  166. package/python/harness/tui/chat.py +1181 -0
  167. package/python/harness/tui/pty.py +81 -0
  168. package/python/harness/tui/renderer.py +45 -0
  169. package/python/harness/tui/timeline_rows.py +58 -0
  170. package/python/harness/tui/views.py +434 -0
@@ -0,0 +1,81 @@
1
+ """Attach a POSIX terminal to a hosted PTY through the existing session API."""
2
+
3
+ import codecs
4
+ import os
5
+ import re
6
+ import select
7
+ import sys
8
+ import threading
9
+ from urllib.parse import quote
10
+
11
+
12
+ def replay_text(records):
13
+ """Replay drawing, but never ask the local terminal historical device queries.
14
+
15
+ The remote client may no longer be waiting for those replies. Forwarding them
16
+ would insert terminal responses into its current input buffer instead.
17
+ """
18
+ text = "".join(str(r.get("text") or "") for r in records if r.get("event") == "terminal_output")
19
+ text = re.sub(r"\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)", "", text)
20
+ text = re.sub(r"\x1b[P^_].*?\x1b\\", "", text, flags=re.DOTALL)
21
+ return re.sub(r"\x1b\[[0-?]*[ -/]*[nct]|\x1bZ", "", text)
22
+
23
+
24
+ def attach(conversation, host):
25
+ """Forward native terminal bytes; Ctrl-] detaches without stopping the session.
26
+
27
+ PTY output is terminal data, not Markdown or a line-oriented transcript.
28
+ Raw records remain the source of truth, including across reconnects.
29
+ """
30
+ import termios
31
+ import tty
32
+
33
+ input_fd, output_fd = sys.stdin.fileno(), sys.stdout.fileno()
34
+ if not os.isatty(input_fd) or not os.isatty(output_fd):
35
+ raise ValueError("attaching a hosted PTY requires an interactive terminal")
36
+ stopped = threading.Event()
37
+ failures = []
38
+ history = host.get(f"/api/events?run={quote(conversation.run, safe='')}&after=-1") or []
39
+ cursor = max((r["seq"] for r in history), default=-1)
40
+
41
+ def write(text):
42
+ data = text.encode("utf-8")
43
+ while data and not stopped.is_set():
44
+ data = data[os.write(output_fd, data):]
45
+
46
+ def receive():
47
+ try:
48
+ write(replay_text(history))
49
+ for record in conversation.records(after=cursor, stop=stopped):
50
+ if record.get("event") == "terminal_output":
51
+ write(str(record.get("text") or ""))
52
+ except Exception as error:
53
+ failures.append(error)
54
+ finally:
55
+ stopped.set()
56
+
57
+ print("Connected to hosted terminal. Ctrl-] detaches.\r", flush=True)
58
+ original = termios.tcgetattr(input_fd)
59
+ decoder = codecs.getincrementaldecoder("utf-8")("replace")
60
+ reader = threading.Thread(target=receive, daemon=True, name="terminal-output")
61
+ try:
62
+ tty.setraw(input_fd)
63
+ reader.start()
64
+ while not stopped.is_set():
65
+ if not select.select([input_fd], [], [], .1)[0]:
66
+ continue
67
+ data = os.read(input_fd, 4096)
68
+ if not data:
69
+ break
70
+ before, detach, _ = data.partition(b"\x1d")
71
+ text = decoder.decode(before)
72
+ if text:
73
+ conversation.terminal_input(text)
74
+ if detach:
75
+ break
76
+ finally:
77
+ stopped.set()
78
+ termios.tcsetattr(input_fd, termios.TCSADRAIN, original)
79
+ os.write(output_fd, b"\x1b[0m\x1b[?25h\x1b[?2004l\r\n")
80
+ if failures:
81
+ raise failures[0]
@@ -0,0 +1,45 @@
1
+ """Keep an inline prompt's erase origin aligned with Windows console reflow."""
2
+
3
+ from prompt_toolkit.data_structures import Point
4
+ from prompt_toolkit.renderer import Renderer
5
+
6
+
7
+ class WindowsInlineRenderer(Renderer):
8
+ """ConPTY reflows painted rows before prompt_toolkit sees the new size.
9
+
10
+ prompt_toolkit's cached cursor is relative to the old physical rows. Both
11
+ resize erases and ordinary refreshes must account for the extra rows before
12
+ using it, otherwise the top of the prompt becomes permanent scrollback.
13
+ """
14
+
15
+ _console_width = None
16
+
17
+ def _reflow_cursor(self):
18
+ width = self.output.get_win32_screen_buffer_info().dwSize.X
19
+ old_width, self._console_width = self._console_width, width
20
+ screen = self._last_screen
21
+ if screen is None or old_width is None or width == old_width:
22
+ return False
23
+
24
+ # The renderer writes hard line breaks with autowrap disabled. Each
25
+ # painted row is therefore independently reflowed by the console.
26
+ y = 0
27
+ for row_index in range(self._cursor_pos.y):
28
+ row = screen.data_buffer[row_index]
29
+ end = max((index + max(cell.width, 1) for index, cell in row.items()
30
+ if cell.char != " " or self._style_string_has_style[cell.style]), default=0)
31
+ end = min(end, self._last_size.columns)
32
+ y += max(1, (end + width - 1) // width)
33
+ y += self._cursor_pos.x // width
34
+ self._cursor_pos = Point(self._cursor_pos.x % width, y)
35
+ return True
36
+
37
+ def render(self, app, layout, is_done=False):
38
+ if self._reflow_cursor():
39
+ super().erase(leave_alternate_screen=False)
40
+ self.request_absolute_cursor_position()
41
+ super().render(app, layout, is_done)
42
+
43
+ def erase(self, leave_alternate_screen=True):
44
+ self._reflow_cursor()
45
+ super().erase(leave_alternate_screen)
@@ -0,0 +1,58 @@
1
+ """Terminal glyphs and rows for a shared timeline."""
2
+
3
+ from ..core.lanes import Lanes
4
+
5
+
6
+ # Characters of a lane in a terminal: a span ran in the cell (only a call whose reply was
7
+ # not used: FAINT), a spawned entry lived but ran nothing, its end (done, failed, cancelled).
8
+ BUSY = "█"
9
+ SHADE = "▒"
10
+ IDLE = "─"
11
+ ENDS = {"done": "┤", "failed": "×", "cancelled": "╳"}
12
+
13
+
14
+ def claimed(lane) -> str:
15
+ """What follows an entry's label: the channels it claims (the last it claimed, once it
16
+ ended)."""
17
+ return f" [{','.join(map(str, lane['claims']))}]" if lane.get("claims") else ""
18
+
19
+
20
+ def rows(lanes: Lanes, t0: float, t1: float, width: int, now: float) -> list[dict]:
21
+ """The lanes living between t0 and t1 as rows of `width` characters (a terminal's
22
+ timeline): each {"lane", "path", "label", "cells"}. Only cells: what the model says is
23
+ the conversation's, never the timeline's."""
24
+ step = (t1 - t0) / max(width, 1)
25
+ out = []
26
+ for lane in lanes.visible(t0, t1, now):
27
+ busy = lanes.columns(lane, t0, t1, width, now, faint=False)
28
+ shaded = lanes.columns(lane, t0, t1, width, now, faint=True)
29
+ cells = []
30
+ for column in range(width):
31
+ a, b = t0 + column * step, t0 + (column + 1) * step
32
+ if busy[column]:
33
+ cells.append(BUSY)
34
+ elif shaded[column]:
35
+ cells.append(SHADE)
36
+ elif lanes.alive(lane, a, b, now):
37
+ end = lane["end"]
38
+ cells.append(ENDS.get(lane["status"], IDLE) if end is not None and a <= end < b else IDLE)
39
+ else:
40
+ cells.append(" ")
41
+ indent = " " * lanes.depth(lane["id"])
42
+ out.append({"lane": lane, "path": lanes.path(lane["id"]), "label": indent + lane["label"] + claimed(lane),
43
+ "cells": "".join(cells)})
44
+ return out
45
+
46
+
47
+ def strip(lanes: Lanes, t0: float, t1: float, width: int, now: float) -> str:
48
+ """A mini timeline in one line of `width` characters: the upper half of a cell when the
49
+ model was called in it, the lower half when a program, entry or the environment
50
+ worked, both when both did (the Sessions page)."""
51
+ thinking = [False] * width
52
+ acting = [False] * width
53
+ for lane in lanes.ordered():
54
+ target = thinking if lane["kind"] == "model" else acting
55
+ for column, busy in enumerate(lanes.columns(lane, t0, t1, width, now)):
56
+ target[column] = target[column] or busy
57
+ return "".join("█" if up and down else "▀" if up else "▄" if down else " "
58
+ for up, down in zip(thinking, acting))
@@ -0,0 +1,434 @@
1
+ """The full-screen views of the terminal chat, each opened over it and closed back to it:
2
+ a picker (sessions, conversations, models), the settings (settings.SCHEMA, whatever the
3
+ packages add), the packages (packages/describe.py, as harness install / remove / list),
4
+ the libraries of what the agent learned (runtime/learning/learner.py) and the timeline
5
+ (core/lanes.py: the GUI's lanes, as bars under an axis)."""
6
+
7
+ import threading
8
+ import time
9
+
10
+ from prompt_toolkit.application import Application
11
+ from prompt_toolkit.buffer import Buffer
12
+ from prompt_toolkit.data_structures import Point
13
+ from prompt_toolkit.key_binding import KeyBindings
14
+ from prompt_toolkit.layout import HSplit, Layout, Window
15
+ from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
16
+ from prompt_toolkit.styles import Style
17
+
18
+ from ..core import format as formats
19
+ from ..core import lanes as timeline_lanes
20
+ from . import timeline_rows
21
+
22
+ STYLE = Style.from_dict({"title": "bold", "selected": "reverse", "dim": "#888888", "error": "#ff5f5f",
23
+ "lane.model": "#d787ff", "lane.verifier": "#5fd7d7", "lane.program": "#5f87ff",
24
+ "lane.agent": "#5fd7d7", "lane.env": "#d7d75f", "lane.other": "#5fd787"})
25
+ # Characters of a lane's name on the timeline.
26
+ LABEL = 24
27
+
28
+
29
+ async def choose(title: str, rows: list[str], *, hint: str = "", current: int = 0, typed: bool = False,
30
+ text: str = "", settle: float = 0.0):
31
+ """A full-screen list to pick from, under `text` (lines about it), filtered as the user
32
+ types: the index picked (in `rows`), None when closed; with `typed`, Enter on a text that
33
+ matches nothing returns it. `settle`: seconds after it opens in which Enter is not
34
+ taken: a view that opens because the user pressed Enter (a message was sent) must not
35
+ be answered by that same key press."""
36
+ query = Buffer(multiline=False)
37
+ state = {"at": max(0, min(current, len(rows) - 1))}
38
+ opened = time.monotonic()
39
+
40
+ def shown():
41
+ words = query.text.lower().split()
42
+ return [index for index, row in enumerate(rows) if all(word in row.lower() for word in words)]
43
+
44
+ def listing():
45
+ found = shown()
46
+ state["at"] = min(state["at"], max(len(found) - 1, 0))
47
+ out = []
48
+ for place, index in enumerate(found):
49
+ out += [("class:selected" if place == state["at"] else "", f"{'❯' if place == state['at'] else ' '} {rows[index]}"),
50
+ ("", "\n")]
51
+ return out or [("class:dim", " nothing matches" + (": Enter takes what you typed" if typed else ""))]
52
+
53
+ keys = KeyBindings()
54
+
55
+ @keys.add("up")
56
+ def _(event):
57
+ state["at"] = max(state["at"] - 1, 0)
58
+
59
+ @keys.add("down")
60
+ def _(event):
61
+ state["at"] = min(state["at"] + 1, max(len(shown()) - 1, 0))
62
+
63
+ @keys.add("pageup")
64
+ def _(event):
65
+ state["at"] = max(state["at"] - 10, 0)
66
+
67
+ @keys.add("pagedown")
68
+ def _(event):
69
+ state["at"] = min(state["at"] + 10, max(len(shown()) - 1, 0))
70
+
71
+ @keys.add("enter")
72
+ def _(event):
73
+ if time.monotonic() - opened < settle:
74
+ return
75
+ found = shown()
76
+ event.app.exit(result=found[state["at"]] if found else (query.text.strip() or None) if typed else None)
77
+
78
+ @keys.add("escape", eager=True)
79
+ @keys.add("c-c")
80
+ def _(event):
81
+ event.app.exit(result=None)
82
+
83
+ lines = [Window(FormattedTextControl([("class:title", title)]), height=1),
84
+ Window(BufferControl(query), height=1, get_line_prefix=lambda *_: [("class:dim", "filter: ")]),
85
+ Window(height=1, char="─", style="class:dim"),
86
+ Window(FormattedTextControl(listing, focusable=False, get_cursor_position=lambda: Point(0, state["at"])),
87
+ wrap_lines=False),
88
+ Window(FormattedTextControl([("class:dim", hint or "↑↓ move · Enter pick · type to filter · Esc close")]),
89
+ height=1)]
90
+ if text:
91
+ lines.insert(1, Window(FormattedTextControl(text), wrap_lines=True, dont_extend_height=True))
92
+ app = Application(Layout(HSplit(lines), focused_element=lines[1]), key_bindings=keys, style=STYLE,
93
+ full_screen=True)
94
+ return await app.run_async()
95
+
96
+
97
+ async def ask(title: str, default: str = "", hint: str = "Enter keep · Esc cancel") -> str | None:
98
+ """One line typed full screen (a setting's value): the text, None when cancelled."""
99
+ field = Buffer(multiline=False)
100
+ field.text = default
101
+ keys = KeyBindings()
102
+
103
+ @keys.add("enter")
104
+ def _(event):
105
+ event.app.exit(result=field.text)
106
+
107
+ @keys.add("escape", eager=True)
108
+ @keys.add("c-c")
109
+ def _(event):
110
+ event.app.exit(result=None)
111
+
112
+ body = Window(BufferControl(field), height=1, get_line_prefix=lambda *_: "› ")
113
+ app = Application(Layout(HSplit([Window(FormattedTextControl([("class:title", title)]), height=1), body,
114
+ Window(FormattedTextControl([("class:dim", hint)]), height=1)]),
115
+ focused_element=body), key_bindings=keys, style=STYLE, full_screen=True)
116
+ return await app.run_async()
117
+
118
+
119
+ async def read(title: str, text, *, close_key=None) -> None:
120
+ """Read text (or a live text callback) on an alternate screen, then restore chat."""
121
+ def lines():
122
+ return (text() if callable(text) else text).splitlines() or [""]
123
+ state = {"at": 0}
124
+ keys = KeyBindings()
125
+
126
+ def move(step):
127
+ state["at"] = max(0, min(state["at"] + step, len(lines()) - 1))
128
+
129
+ keys.add("up")(lambda event: move(-1))
130
+ keys.add("down")(lambda event: move(1))
131
+ keys.add("pageup")(lambda event: move(-20))
132
+ keys.add("pagedown")(lambda event: move(20))
133
+
134
+ @keys.add("escape", eager=True)
135
+ @keys.add("q")
136
+ @keys.add("c-c")
137
+ def _(event):
138
+ event.app.exit()
139
+
140
+ if close_key:
141
+ keys.add(close_key)(lambda event: event.app.exit())
142
+
143
+ body = Window(FormattedTextControl(lambda: "\n".join(lines()), get_cursor_position=lambda: Point(0, state["at"])),
144
+ wrap_lines=True)
145
+ await Application(Layout(HSplit([Window(FormattedTextControl([("class:title", title)]), height=1), body,
146
+ Window(FormattedTextControl([("class:dim", "↑↓ PgUp PgDn scroll · Esc close")]),
147
+ height=1)])), key_bindings=keys, style=STYLE, full_screen=True,
148
+ refresh_interval=0.25 if callable(text) else None).run_async()
149
+
150
+
151
+ async def working(title: str, function):
152
+ """`function(log)` on a thread of its own, full screen with the lines it logs, until it
153
+ returns: (its result, None), or (None, the error)."""
154
+ said, done = [], {}
155
+
156
+ def run():
157
+ try:
158
+ done["result"] = function(lambda message: said.append(str(message)))
159
+ except Exception as error:
160
+ done["error"] = error
161
+ app.exit()
162
+
163
+ app = Application(Layout(HSplit([Window(FormattedTextControl([("class:title", f"{title} ...")]), height=1),
164
+ Window(FormattedTextControl(lambda: "\n".join(said[-40:])), wrap_lines=True)])),
165
+ style=STYLE, full_screen=True, refresh_interval=0.3)
166
+ app.pre_run_callables.append(lambda: threading.Thread(target=run, daemon=True).start())
167
+ await app.run_async()
168
+ return done.get("result"), done.get("error")
169
+
170
+
171
+ async def packages_view() -> list[str]:
172
+ """The installed packages and the registry's, each with what it provides; Enter on one
173
+ shows all of it and what can be done: install, update, turn on or off, remove (asked
174
+ first). Returns what changed."""
175
+ from ..packages import Packages
176
+ from ..packages.describe import COLUMNS, describe, rows
177
+
178
+ changed, registry = [], False
179
+ while True:
180
+ # The registry may be read over the network: on a thread, as installing is.
181
+ found, problem = await working("Reading the packages", lambda log: Packages().search("") if registry
182
+ else Packages().installed())
183
+ found, which = found or [], "available" if registry else "installed"
184
+ table, columns = rows(found, which, 40), COLUMNS[which]
185
+ widths = {column: min(max([len(column)] + [len(str(row[column])) for row in table]), 40) for column in columns}
186
+ lines = ["↔ " + ("the installed packages" if registry else "the registry's packages")] + [
187
+ " ".join(formats.clip(row[column], widths[column]).ljust(widths[column]) for column in columns)
188
+ for row in table]
189
+ heading = " " + " ".join(column.ljust(widths[column]) for column in columns)
190
+ picked = await choose(f"Packages: {'the registry' if registry else 'installed'}", lines, text=(
191
+ f"{problem}" if problem else heading if found else "Nothing installed yet." if not registry else ""))
192
+ if picked is None:
193
+ return changed
194
+ if picked == 0:
195
+ registry = not registry
196
+ continue
197
+ row = found[picked - 1]
198
+ try:
199
+ about = "\n".join(describe(Packages().info(row["name"]))["lines"])
200
+ except Exception as error:
201
+ about = f"{row['name']}: {error}"
202
+ if registry:
203
+ actions = ["update", "remove"] if row.get("installed") else ["install"]
204
+ else:
205
+ actions = ["update", "turn " + ("off" if row.get("enabled") else "on"), "remove"]
206
+ action = await choose(row["name"], actions + ["back"], text=about)
207
+ if action is None or action == len(actions):
208
+ continue
209
+ action = actions[action]
210
+ name = row["name"]
211
+ if action in ("install", "remove") and await choose(
212
+ f"{action.capitalize()} {name}?" + (" Its skills, embodiments and services go with it."
213
+ if action == "remove" else ""), ["no", "yes"]) != 1:
214
+ continue
215
+ if action.startswith("turn"):
216
+ calls = lambda log: Packages().enable(name, action.endswith("on")) # noqa: E731
217
+ else:
218
+ calls = lambda log: getattr(Packages(), action)([name], log=log) # noqa: E731
219
+ _, error = await working(f"{action.capitalize()} {name}", calls)
220
+ changed.append(f"{action} {name}: " + (f"failed: {error}" if error else "done"))
221
+ if error:
222
+ await read(f"{action} {name} failed", str(error))
223
+
224
+
225
+ async def library_view(memory=None) -> None:
226
+ """What the agent remembers: this conversation's memory (`memory`: a call that gives its
227
+ rows, runtime/task/context.py) and the libraries; Enter on one lists its entries as every
228
+ frontend shows them, Enter on an entry shows it."""
229
+ from pathlib import Path
230
+
231
+ from harness.runtime.learning import learner
232
+ from harness.core.memory import COLUMNS, rows as memory_rows
233
+
234
+ while True:
235
+ found = learner.libraries()
236
+ lines = [f"{row['name']:<28} {row['entries']:>4} entries version {row['version'][:12]}" for row in found]
237
+ picked = await choose("Memory: this conversation's and the libraries'",
238
+ (["this conversation: what it saw and where, and its library"] if memory else []) + lines,
239
+ text="" if found or memory else "No library yet: a run with [runtime.library] learn = "
240
+ "\"on\" keeps what it learns there.")
241
+ if picked is None:
242
+ return
243
+ if memory and picked == 0:
244
+ title, load = "this conversation", memory
245
+ else:
246
+ row = found[picked - (1 if memory else 0)]
247
+ title, load = f"{row['name']}: version {row['version'][:12]}", \
248
+ (lambda directory=Path(row["directory"]): memory_rows(library=directory))
249
+ while True:
250
+ entries = load()
251
+ at = await choose(title, [" ".join(f"{cell:<12}" for cell in row["cells"][:7]) for row in entries],
252
+ hint="Enter read · type to filter · Esc back")
253
+ if at is None:
254
+ break
255
+ row = entries[at]
256
+ from json import dumps
257
+
258
+ details = [f"{column}: {cell}" for column, cell in zip(COLUMNS, row["cells"][:-1])]
259
+ if row.get("summary"):
260
+ details.append(f"summary: {row['summary']}")
261
+ if row.get("history"):
262
+ details.append("history: " + dumps(row["history"], ensure_ascii=False, indent=2))
263
+ await read(f"{row['scope']} {row['kind']} {row['name'] or row['id']}",
264
+ "\n".join([row["text"] or "", "", *details]))
265
+
266
+
267
+ async def settings_view(directory) -> list[str]:
268
+ """Every setting (settings.rows) to change one at a time until closed, under the packages'
269
+ note on this computer; returns what changed."""
270
+ from ..extensions import first
271
+ from ..settings import SCHEMA, Settings, rows
272
+
273
+ store, changed, at, note = Settings(directory), [], 0, first("note")
274
+ while True:
275
+ values = store.get()
276
+ found = [row for row in rows(values) if row["setting"] != "model.reasoning_effort"]
277
+ lines = [f"{row['setting']:<36} {_shown(row)}" for row in found]
278
+ at = await choose("Settings" + (f" {note(values)}" if note else ""), lines, current=at,
279
+ hint="Enter change · type to filter · Esc close · /models edits the model list")
280
+ if at is None:
281
+ return changed
282
+ row, spec = found[at], SCHEMA[found[at]["section"]][found[at]["key"]]
283
+ if row["kind"] == "bool":
284
+ value = not row["value"]
285
+ elif row["kind"] == "choice":
286
+ picked = await choose(f"{row['setting']}: {row['about']}", list(spec.bounds),
287
+ current=list(spec.bounds).index(row["value"]) if row["value"] in spec.bounds else 0)
288
+ value = spec.bounds[picked] if picked is not None else None
289
+ elif row["kind"] == "rules":
290
+ try:
291
+ value = await _rules(row, spec)
292
+ except ValueError as error:
293
+ await choose(f"Not changed: {error}", ["back"])
294
+ continue
295
+ else:
296
+ value = await ask(f"{row['setting']} ({row['about']})", str(row["value"]))
297
+ if value is None or value == row["value"]:
298
+ continue
299
+ try:
300
+ store.update({row["section"]: {row["key"]: value}})
301
+ changed.append(f"{row['setting']} = {_shown({**row, 'value': store.get()[row['section']][row['key']]})}"
302
+ + ("" if spec.applies == "now" else f" ({spec.applies.replace('_', ' ')})"))
303
+ except ValueError as error:
304
+ await choose(f"Not changed: {error}", ["back"])
305
+
306
+
307
+ def _shown(row) -> str:
308
+ value = row["value"]
309
+ if row["kind"] == "rules":
310
+ return f"{len(value)} rule{'s' if len(value) != 1 else ''}"
311
+ return formats.clip(str(value).replace("\n", " "), 60) if value != "" else "(empty)"
312
+
313
+
314
+ async def _rules(row, spec) -> list | None:
315
+ """Remove a rule or add one with all of the scopes that must match."""
316
+ from ..settings import SCHEMA
317
+
318
+ rules = list(row["value"])
319
+ levels = list(getattr(SCHEMA[row["section"]].get("level"), "bounds", None) or ())
320
+ picked = await choose(f"{row['setting']}: {row['about']}", [_rule(rule) for rule in rules] + ["+ add a rule"],
321
+ hint="Enter on a rule removes it · Esc back")
322
+ if picked is None:
323
+ return None
324
+ if picked < len(rules):
325
+ return rules[:picked] + rules[picked + 1:]
326
+ level = await choose("Its level", levels) if levels else None
327
+ if level is None:
328
+ return None
329
+ rule = {"level": levels[level]}
330
+ fields = {**(spec.fields or {}), **(spec.fields_when or {}).get(rule["level"], {})}
331
+ while True:
332
+ names = list(fields)
333
+ labels = [f"{name}: {rule[name]}" if name in rule else name for name in names]
334
+ field = await choose("What it is for", [*labels, "Save rule"], current=len(names) if len(rule) > 1 else 0)
335
+ if field is None:
336
+ return None
337
+ if field == len(names):
338
+ return rules + [rule]
339
+ name = names[field]
340
+ kind = fields[name]
341
+ if isinstance(kind, (list, tuple)):
342
+ chosen = await choose(name, list(kind))
343
+ if chosen is not None:
344
+ rule[name] = kind[chosen]
345
+ else:
346
+ value = await ask(name, str(rule.get(name, "")))
347
+ if value:
348
+ rule[name] = float(value) if kind == "float" else value
349
+ elif value == "":
350
+ rule.pop(name, None)
351
+
352
+
353
+ def _rule(rule: dict) -> str:
354
+ return f"{rule.get('level', '?'):<18} " + ", ".join(f"{key} {value}" for key, value in rule.items() if key != "level")
355
+
356
+
357
+ def timeline_lines(lanes, width: int, seconds: float | None = None) -> list:
358
+ """The timeline as formatted lines: an axis with the times at its ends, then each lane's
359
+ name and its bars (core/lanes.rows), no other text."""
360
+ if lanes.t0 is None:
361
+ return [("class:dim", "no records yet")]
362
+ now = lanes.now(time.time())
363
+ start = lanes.t0 if seconds is None else max(lanes.t0, now - seconds)
364
+ start = min(start, now - 1.0)
365
+ cells = max(width - LABEL - 1, 10)
366
+ left, right = formats.clock(start - lanes.t0), formats.clock(now - lanes.t0)
367
+ axis = "├" + "─" * (cells - 2) + "┤"
368
+ out = [("", " " * (LABEL + 1)), ("class:dim", left + right.rjust(cells - len(left))), ("", "\n"),
369
+ ("", " " * (LABEL + 1)), ("class:dim", axis), ("", "\n")]
370
+ for row in timeline_rows.rows(lanes, start, now, cells, now):
371
+ lane = row["lane"]
372
+ style = "class:error" if lane.get("status") == "failed" else "class:dim" if lane.get("end") is not None else ""
373
+ out += [(style, f"{formats.clip(row['label'], LABEL):<{LABEL}} "),
374
+ (f"class:lane.{lane['kind'] if lane['kind'] in LANE_KINDS else 'other'}", row["cells"]), ("", "\n")]
375
+ return out
376
+
377
+
378
+ LANE_KINDS = ("model", "verifier", "program", "agent", "env")
379
+
380
+
381
+ async def timeline_view(lanes, feed=None) -> None:
382
+ """`lanes` full screen, drawn again twice a second (`feed()` first adds what came);
383
+ + and - zoom, 0 shows the whole run, Esc, q or Ctrl+T close."""
384
+ window = {"seconds": None}
385
+ keys = KeyBindings()
386
+
387
+ @keys.add("escape", eager=True)
388
+ @keys.add("q")
389
+ @keys.add("c-t")
390
+ @keys.add("c-c")
391
+ def _(event):
392
+ event.app.exit()
393
+
394
+ @keys.add("+")
395
+ @keys.add("=")
396
+ def _(event):
397
+ whole = lanes.now(time.time()) - (lanes.t0 or 0)
398
+ window["seconds"] = max(5.0, (window["seconds"] or whole) / 2)
399
+
400
+ @keys.add("-")
401
+ def _(event):
402
+ window["seconds"] = window["seconds"] and window["seconds"] * 2
403
+
404
+ @keys.add("0")
405
+ def _(event):
406
+ window["seconds"] = None
407
+
408
+ def draw():
409
+ if feed is not None:
410
+ feed()
411
+ return timeline_lines(lanes, app.output.get_size().columns, window["seconds"])
412
+
413
+ app = Application(Layout(HSplit([
414
+ Window(FormattedTextControl([("class:title", "Timeline")]), height=1),
415
+ Window(FormattedTextControl(draw), wrap_lines=False),
416
+ Window(FormattedTextControl([("class:dim", "+ - zoom · 0 whole run · Esc close")]), height=1)])),
417
+ key_bindings=keys, style=STYLE, full_screen=True, refresh_interval=0.5)
418
+ await app.run_async()
419
+
420
+
421
+ def follow_timeline(directory) -> int:
422
+ """harness timeline RUN in a terminal: the episode in `directory` followed while it is written."""
423
+ import asyncio
424
+
425
+ from ..core.episodes import Follow
426
+
427
+ lanes, follow = timeline_lanes.Lanes(), Follow(directory)
428
+
429
+ def feed():
430
+ for record in follow.next():
431
+ lanes.add(record)
432
+
433
+ asyncio.run(timeline_view(lanes, feed))
434
+ return 0