@lmmslab/harness 0.81.1 → 0.82.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +24 -35
  2. package/package.json +2 -2
  3. package/python/harness/README.md +3 -3
  4. package/python/harness/__init__.py +1 -1
  5. package/python/harness/builtin/configs/assistant.toml +0 -1
  6. package/python/harness/builtin/configs/default.toml +1 -1
  7. package/python/harness/builtin/prompts/conversation/core.md +2 -2
  8. package/python/harness/builtin/prompts/core/core.md +177 -41
  9. package/python/harness/builtin/prompts/core/pack.toml +2 -2
  10. package/python/harness/builtin/prompts/reflect/core.md +1 -1
  11. package/python/harness/builtin/prompts/shared/learning-goal.md +5 -29
  12. package/python/harness/builtin/prompts/shared/learning.md +8 -10
  13. package/python/harness/builtin/prompts/shared/web.md +11 -0
  14. package/python/harness/builtin/skills/agents/__init__.py +24 -22
  15. package/python/harness/builtin/skills/entries/__init__.py +43 -28
  16. package/python/harness/builtin/skills/entries/skill.toml +2 -3
  17. package/python/harness/builtin/skills/environments/__init__.py +2 -9
  18. package/python/harness/builtin/skills/mcp/__init__.py +2 -3
  19. package/python/harness/builtin/skills/memory/__init__.py +2 -5
  20. package/python/harness/builtin/skills/web/__init__.py +92 -0
  21. package/python/harness/builtin/skills/web/skill.toml +9 -0
  22. package/python/harness/cli.py +84 -16
  23. package/python/harness/config/agent.py +33 -9
  24. package/python/harness/core/episodes.py +5 -4
  25. package/python/harness/core/lanes.py +3 -0
  26. package/python/harness/core/memory.py +5 -1
  27. package/python/harness/core/remote.py +4 -3
  28. package/python/harness/core/worker.py +58 -3
  29. package/python/harness/extensions.py +23 -2
  30. package/python/harness/host/__init__.py +43 -1
  31. package/python/harness/host/client.py +36 -6
  32. package/python/harness/host/http.py +19 -0
  33. package/python/harness/host/server.py +17 -3
  34. package/python/harness/host/sessions.py +3 -2
  35. package/python/harness/host/tasks.py +107 -26
  36. package/python/harness/host/workspaces.py +69 -7
  37. package/python/harness/model_list.py +1 -1
  38. package/python/harness/packages/manager.py +5 -1
  39. package/python/harness/providers.py +14 -5
  40. package/python/harness/runtime/decision/loop.py +100 -121
  41. package/python/harness/runtime/decision/messages.py +156 -48
  42. package/python/harness/runtime/decision/model.py +79 -21
  43. package/python/harness/runtime/decision/prompts.py +15 -11
  44. package/python/harness/runtime/decision/protocol.py +12 -6
  45. package/python/harness/runtime/decision/thoughts.py +10 -16
  46. package/python/harness/runtime/events.py +28 -13
  47. package/python/harness/runtime/execution/attached.py +1 -1
  48. package/python/harness/runtime/execution/control.py +17 -35
  49. package/python/harness/runtime/execution/entries.py +204 -124
  50. package/python/harness/runtime/execution/kernel.py +471 -281
  51. package/python/harness/runtime/execution/managed.py +231 -0
  52. package/python/harness/runtime/execution/noenv.py +2 -2
  53. package/python/harness/runtime/execution/results.py +2 -0
  54. package/python/harness/runtime/execution/session.py +276 -115
  55. package/python/harness/runtime/learning/active.py +36 -48
  56. package/python/harness/runtime/learning/fixtures.py +2 -2
  57. package/python/harness/runtime/learning/learner.py +75 -2
  58. package/python/harness/runtime/learning/library.py +77 -10
  59. package/python/harness/runtime/learning/views.py +12 -1
  60. package/python/harness/runtime/reactive/__init__.py +1 -1
  61. package/python/harness/runtime/reactive/rules.py +29 -394
  62. package/python/harness/runtime/reactive/tree.py +611 -0
  63. package/python/harness/runtime/safety/approval.py +8 -3
  64. package/python/harness/runtime/safety/permissions.py +242 -0
  65. package/python/harness/runtime/status.py +7 -2
  66. package/python/harness/runtime/task/delegation.py +97 -58
  67. package/python/harness/runtime/task/environments.py +3 -4
  68. package/python/harness/runtime/task/inbound.py +64 -0
  69. package/python/harness/runtime/task/run.py +1092 -726
  70. package/python/harness/runtime/tick.py +257 -91
  71. package/python/harness/sdk/__init__.py +7 -4
  72. package/python/harness/settings.py +27 -3
  73. package/python/harness/terminal.py +10 -6
  74. package/python/harness/transcript.py +18 -6
  75. package/python/harness/tui/answer.py +51 -0
  76. package/python/harness/tui/chat.py +75 -26
  77. package/python/harness/tui/views.py +1 -0
  78. package/python/harness/builtin/prompts/shared/entries.md +0 -86
@@ -59,7 +59,7 @@ _CORE_SCHEMA = {section: {key: Setting(*row) for key, row in rows.items()} for s
59
59
  "view when the main model reads none (empty: such a model is told the "
60
60
  "view was not sent and works from text)"),
61
61
  "think_while_acting": ("default", "choice", ("default", "off", "on", "on_events", "periodic"),
62
- "one speculative thought during a long program. Legacy on_events and periodic mean on"),
62
+ "accepted for older configurations; it has no effect"),
63
63
  "think_interval": (0, "int", (0, 600), "accepted for older configurations"),
64
64
  },
65
65
  "session": {
@@ -87,11 +87,20 @@ _CORE_SCHEMA = {section: {key: Setting(*row) for key, row in rows.items()} for s
87
87
  "ask": ("sensitive", "choice", ("sensitive", "programs"),
88
88
  "what waits for your approval: sensitive (steps that touch credentials, a payment, sending, deleting "
89
89
  "or cannot be undone; they always wait) or programs (every program as well)"),
90
+ "programs": ("ask_once_session", "choice", ("never", "ask_each", "ask_once_session", "allow"),
91
+ "what a program of the agent may do on this computer beyond its own folder (run a command, "
92
+ "reach the network, write a file elsewhere), where no rule says otherwise: never, ask each "
93
+ "time, ask once per conversation and kind (a command's program, a host, a folder), allow. "
94
+ "Reading is not asked about; a file of credentials and deleting always are. When you skip "
95
+ "confirmations (physical.level allow) it does not ask; never still refuses", "now"),
96
+ "rules": ([], "rules", (0, 200),
97
+ "a level for one kind of step: action is command, network or write, match a glob on the "
98
+ "command's program, the host or the folder; it wins over approvals.programs", "now",
99
+ {"action": "text", "match": "text"}, {"level": "allow", "action": "network", "match": "*.github.com"}),
90
100
  },
91
101
  "limits": {
92
102
  "max_steps": (60, "int", (1, 1000), "model calls a task may take"),
93
103
  "max_minutes": (360, "int", (1, 1440), "minutes a conversation may run"),
94
- "max_cost_usd": (5.0, "float", (0.01, 1000.0), "dollars a conversation may spend on the model"),
95
104
  "agent_depth": (1, "int", (0, 4), "how deep agents start sub-agents (their budgets come out of the "
96
105
  "conversation's): 0 none, 1 the conversation's agent does and they do "
97
106
  "not, 2 theirs do too"),
@@ -104,6 +113,14 @@ _CORE_SCHEMA = {section: {key: Setting(*row) for key, row in rows.items()} for s
104
113
  "recheck eligible entries at the start of ordinary conversations); frozen (read one snapshot)"),
105
114
  "active_minutes": (30, "int", (1, 240), "minutes of live time a \"learn this environment\" conversation "
106
115
  "takes (the app's Learn, /learn, harness run --learn)"),
116
+ "admission": ("fixture", "choice", ("off", "fixture", "own_test"),
117
+ "how a skill or reactive rule the agent wrote itself is admitted to a library that learns: "
118
+ "fixture (only a fixture this computer provides for that entry verifies it; without one "
119
+ "the proposal waits), own_test (where the environment reports that it is not physical, "
120
+ "the host also runs the entry's own test between programs; a pass admits it as "
121
+ "provisional, marked as admitted by its own test, and it goes back to waiting when it "
122
+ "keeps failing in use; every other environment stays at fixture), off (no learned code "
123
+ "is admitted or loaded). $HARNESS_LEARNING_ADMISSION wins over it"),
107
124
  },
108
125
  "packages": {
109
126
  "auto_install": ("official", "choice", ("official", "ask", "off"),
@@ -284,7 +301,14 @@ def _attach_rules(value):
284
301
 
285
302
 
286
303
  # What checks a core setting whose kind's own check is not enough.
287
- _CORE_CHECKS = {("session", "environment"): _environment, ("environments", "rules"): _attach_rules}
304
+ def _program_rules(value):
305
+ from harness.runtime.safety.permissions import rules
306
+
307
+ return rules(value)
308
+
309
+
310
+ _CORE_CHECKS = {("session", "environment"): _environment, ("environments", "rules"): _attach_rules,
311
+ ("approvals", "rules"): _program_rules}
288
312
 
289
313
 
290
314
  # One change at a time in this process (the page's requests come on threads of their own).
@@ -5,6 +5,7 @@ chat (tui/) and harness run rpc."""
5
5
 
6
6
  from typing import NamedTuple
7
7
 
8
+ from harness.runtime.safety.input import REMEMBER as KEPT
8
9
  from .transcript import TextTranscript as Printer, item_lines as item_lines, record_lines as record_lines
9
10
 
10
11
 
@@ -24,8 +25,9 @@ class Command(NamedTuple):
24
25
  # ({"type": "pause"}, those with a method).
25
26
  COMMANDS = {
26
27
  "help": Command(note="the commands and keys"),
27
- "new": Command(note="a new conversation (this one goes on)"),
28
- "resume": Command(note="pick an earlier conversation and go on with it"),
28
+ "new": Command(note="a new conversation with nothing of this one (this one goes on, and /resume comes back to it)"),
29
+ "clear": Command(note="the same as /new"),
30
+ "resume": Command(note="pick an earlier conversation and go on with it, in its own session"),
29
31
  "sessions": Command(note="the sessions that run: switch to one"),
30
32
  "host": Command(words="[stop]", note="the session host: how it runs; stop: end it (and what runs in it)"),
31
33
  "environment": Command(note="where this conversation works; choose where the next one does"),
@@ -42,7 +44,7 @@ COMMANDS = {
42
44
  "doing a task (default: the settings' minutes); during it, where "
43
45
  "it stands"),
44
46
  "pause": Command("pause", note="stop at the next step, as Esc does"),
45
- "cancel": Command("cancel", note="cancel an external turn"),
47
+ "cancel": Command("cancel", note="end the turn that runs; the conversation waits for your next message"),
46
48
  "steer": Command("steer", takes=("text",), words="TEXT", note="redirect an external turn"),
47
49
  "terminal": Command("terminal_input", takes=("text",), words="TEXT", note="send raw PTY input"),
48
50
  "answer": Command(words="REQUEST_ID JSON", note="answer the agent's form, or decline with null"),
@@ -52,17 +54,19 @@ COMMANDS = {
52
54
  "handoff": Command(words="AGENT CONTEXT", note="start a new agent with the context you supply"),
53
55
  "import": Command(words="PROVIDER FILE", note="open a selected native session transcript read-only"),
54
56
  "continue": Command("resume", note="go on after a pause"),
55
- # remember: for physical input, once (or step), app, session or always.
56
- "approve": Command("confirm", {"accept": True}, ("remember", "reason", "request_id"), "[app | session | always]",
57
+ # remember: for physical input, once (or step: the default, not written), app, session or always.
58
+ "approve": Command("confirm", {"accept": True}, ("remember", "reason", "request_id"), f"[{' | '.join(KEPT[1:])}]",
57
59
  "allow what waits; for physical input, keep it for the app, the session or always"),
58
60
  "reject": Command("confirm", {"accept": False}, ("reason", "request_id"), "[why]", "refuse it, saying why"),
59
- "end": Command("close", note="end it; the evaluator scores an episode"),
61
+ "end": Command("close", note="end the conversation; the evaluator scores an episode"),
60
62
  "timeline": Command(note="the timeline (Ctrl+T)"),
61
63
  "detach": Command(note="leave; the session goes on (/sessions comes back to it)"),
62
64
  "exit": Command(note="leave (Ctrl+C twice)"),
63
65
  }
64
66
  # Other words for a remember value.
65
67
  REMEMBER = {"step": "once"}
68
+ # The words /approve takes: what an answer keeps beyond the one step (runtime/safety/input.py REMEMBER).
69
+ APPROVE_WORDS = KEPT[1:]
66
70
 
67
71
  HELP = ("Type a message for the agent, or a command: "
68
72
  + ", ".join(f"/{name}" + (f" {command.words}" if command.words else "")
@@ -30,12 +30,21 @@ SAID = {
30
30
  "model_request": ("call", "call", "model call {call}: {reason}", True),
31
31
  "finish": ("finished", "end", "finished ({source}): {text}", True),
32
32
  "interrupted": ("interrupted", "error", "interrupted", True),
33
+ "resumed": ("resumed", "note", "continued in a new process: {messages} messages restored"),
33
34
  "cleanup_error": ("cleanup_error", "error", "cleanup error: {message}", True),
34
- "context_shortened": ("context_shortened", "call", "history shortened to {history_turns} turns", True),
35
+ "compaction": ("compaction", "call", "conversation compacted at {tokens} tokens: {action}", True),
35
36
  "malformed_dropped": ("malformed_dropped", "call", "{messages} malformed messages left the history", True),
36
37
  }
37
38
 
38
39
 
40
+ # How a turn that ended without an answer is said (the reason of the IDLE state after it:
41
+ # runtime/decision/loop.py TURN_ENDS).
42
+ TURN_STOPPED = {"budget": "out of model calls for this turn (a message goes on)",
43
+ "wall_time": "out of time for this turn (a message goes on)",
44
+ "cost": "the conversation's cost limit is reached", "error": "it ended in an error",
45
+ "interrupted": "interrupted", "episode_ended": "the environment's episode ended"}
46
+
47
+
39
48
  def presentation(record: dict) -> dict:
40
49
  """A model_reply as {kind, thought, body}; one without `presentation` (older) read from its text."""
41
50
  shown = record.get("presentation")
@@ -50,7 +59,7 @@ def presentation(record: dict) -> dict:
50
59
 
51
60
 
52
61
  def answer_of(record: dict) -> str | None:
53
- """The agent's answer when `record` is its final reply (harness run -p), else None."""
62
+ """The agent's answer when `record` is the reply that ends its turn (harness run -p), else None."""
54
63
  if record.get("event") in ("finish", "agent_message") and record.get("text"):
55
64
  return str(record["text"])
56
65
  reply = presentation(record) if record.get("event") == "model_reply" and record.get("ahead", "used") == "used" else {}
@@ -111,7 +120,7 @@ def items(record: dict, names: dict, timeline: bool = False) -> list[dict]:
111
120
  if kind == "agent_report":
112
121
  report = str(get("result") or get("error") or "")
113
122
  return [_item(kind, "error" if get("status") == "error" else "outcome", f"sub-agent {get('agent')} {get('status')} ("
114
- f"{get('model_calls')} calls, {formats.cost(get('cost_usd') or 0)}, {took})"
123
+ f"{get('model_calls')} calls, {took})"
115
124
  + (f": {report}" if report.strip() else ""))]
116
125
  if kind == "model_request" and get("reason") == "think": # a look again while a program runs, and why
117
126
  why = "news" if get("why") == "news" else "a while without a call"
@@ -131,9 +140,9 @@ def items(record: dict, names: dict, timeline: bool = False) -> list[dict]:
131
140
  out.append(_item("program", "code", body, reply=shown, thought=thought) if shown in ("python", "action")
132
141
  else _item("verify_ask" if shown == "verify" else "finish", "agent", body))
133
142
  usage = get("usage") or {}
134
- cost = f", {formats.cost(usage['cost_usd'])}" if isinstance(usage.get("cost_usd"), (int, float)) else ""
143
+ # Tokens, no dollars: only OpenRouter reports a cost (the record keeps it).
135
144
  return out + ([_item("usage", "call", f"{took}, {usage.get('input_tokens')} in / {usage.get('output_tokens')} "
136
- f"out tokens{cost}")] if timeline else [])
145
+ f"out tokens")] if timeline else [])
137
146
  if kind == "skill_step":
138
147
  number, after = f"{get('step')}" + (f"/{get('of')}" if get("of") else ""), f" after {took}" if get("seconds") is not None else ""
139
148
  return [_item(kind, "skill" if get("ok") else "error",
@@ -195,7 +204,10 @@ def items(record: dict, names: dict, timeline: bool = False) -> list[dict]:
195
204
  return [_item("allowed", "note", say(f"allowed.{remember}", app=app), remember=remember, app=app) if get("accepted")
196
205
  else _item("refused", "note", say("refused", reason=reason))]
197
206
  if kind == "state":
198
- words = {"IDLE": "your turn", "PAUSED": "paused"}.get(get("status")) or get("status") in ENDED and f"episode over: {get('reason')}"
207
+ if get("status") == "IDLE" and get("reason") in TURN_STOPPED:
208
+ # The turn ended without an answer: why, before the user's turn.
209
+ return [_item("state", "turn", f"turn over: {TURN_STOPPED[get('reason')]}"), _item("state", "turn", "your turn")]
210
+ words = {"IDLE": "your turn", "PAUSED": "paused"}.get(get("status")) or get("status") in ENDED and f"conversation over: {get('reason')}"
199
211
  return [_item("state", "turn", words)] if words else []
200
212
  if kind == "outcome":
201
213
  outcome = get("outcome") or {}
@@ -0,0 +1,51 @@
1
+ """The agent's text in the chat: Markdown in which what looks like HTML stays text."""
2
+
3
+ import re
4
+
5
+ from markdown_it import MarkdownIt
6
+ from markdown_it.token import Token
7
+ from rich.markdown import Markdown
8
+
9
+ # A terminal shows no HTML, and rich leaves out what it reads as HTML: each tag (`Vec<String>`
10
+ # read `Vec`) and a block that a line starting with a tag opens (`<div>…`), with every line
11
+ # up to the next blank one. So HTML is read as text here, but for the two tags that mean
12
+ # something in a terminal: a line break, and a key (rich's own style for <kbd>).
13
+ PARSER = MarkdownIt("commonmark", {"html": False}).enable("strikethrough").enable("table")
14
+ TAGS = re.compile(r"(<br\s*/?>|</?kbd>)", re.IGNORECASE)
15
+
16
+
17
+ class Answer(Markdown):
18
+ def __init__(self, markup: str, **options):
19
+ super().__init__("", **options)
20
+ self.markup = markup
21
+ self.parsed = [_tags(token) for token in PARSER.parse(markup)]
22
+
23
+
24
+ def _tags(token: Token) -> Token:
25
+ if token.children:
26
+ children, keys = [], 0
27
+ for piece in (piece for child in token.children for piece in _pieces(child)):
28
+ if piece.type == "html_inline" and piece.tag == "kbd":
29
+ if piece.content == "<kbd>":
30
+ keys += 1
31
+ elif keys:
32
+ keys -= 1
33
+ else: # a key closed that was not opened: rich would fail on it
34
+ piece = Token("text", "", 0, content=piece.content)
35
+ children.append(piece)
36
+ token.children = children
37
+ return token
38
+
39
+
40
+ def _pieces(token: Token) -> list:
41
+ if token.type != "text" or "<" not in token.content:
42
+ return [token]
43
+ pieces = []
44
+ for part in TAGS.split(token.content):
45
+ if TAGS.fullmatch(part):
46
+ tag = part.lower()
47
+ pieces.append(Token("hardbreak", "br", 0) if tag.startswith("<br")
48
+ else Token("html_inline", "kbd", 0, content=tag))
49
+ elif part:
50
+ pieces.append(Token("text", "", 0, content=part))
51
+ return pieces
@@ -40,7 +40,6 @@ from prompt_toolkit.patch_stdout import patch_stdout
40
40
  from prompt_toolkit.styles import Style
41
41
  from prompt_toolkit.widgets import SearchToolbar
42
42
  from rich.console import Console
43
- from rich.markdown import Markdown
44
43
  from rich.syntax import Syntax
45
44
  from rich.text import Text
46
45
 
@@ -48,11 +47,13 @@ from .. import transcript
48
47
  from ..core import format as formats
49
48
  from harness.runtime.safety.approval import say
50
49
  from ..core.lanes import Lanes
50
+ from ..host.client import HostGone
51
51
  from ..host.sessions import ENDED, LIVE, table_rows, word
52
52
  from ..paths import data
53
53
  from harness.runtime.learning.active import progress_line
54
- from ..terminal import COMMANDS, command, item_lines
54
+ from ..terminal import APPROVE_WORDS, COMMANDS, command, item_lines
55
55
  from . import views
56
+ from .answer import Answer
56
57
 
57
58
  # Shift+Enter where the terminal tells it from Enter: as Ctrl+J, a new line.
58
59
  for _sequence in ("\x1b[13;2u", "\x1b[27;2;13~"):
@@ -80,20 +81,26 @@ DETAILS = {"skill", "skill_step", "entry_start", "entry_claims", "entry_end", "s
80
81
  "cell_error", "unhandled"}
81
82
  # Items of the timeline (transcript.items with timeline) the chat leaves out: its calls and
82
83
  # their usage, how the run ended inside.
83
- TIMELINE = {"call", "usage", "call_set_aside", "context_shortened", "malformed_dropped", "library_loaded",
84
+ TIMELINE = {"call", "usage", "call_set_aside", "compaction", "malformed_dropped", "library_loaded",
84
85
  "policy_to_end", "wrap_up", "cleanup_error"}
85
86
  SPINNER = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
86
87
  # Seconds within which a second Ctrl+C, Ctrl+D or Esc counts.
87
88
  TWICE = 1.0
89
+ # The status of a conversation that can no longer be followed (its session host is gone).
90
+ LOST = "LOST"
88
91
  FILE = re.compile(r'@"([^"]+)"|@(\S+)')
89
92
 
90
93
 
91
94
  class Chat:
92
95
  """One terminal chat; run() returns once the user leaves."""
93
96
 
97
+ # Whether the host parked the conversation shown for its workspace (its next message continues it).
98
+ parked = False
99
+
94
100
  def __init__(self, conversation=None, records=None, *, host=None, start=None):
95
101
  self.conversation, self.records, self.host, self.start = conversation, records, host, start
96
102
  self.status = None # the conversation's last status; "starting" while it is created
103
+ self.before_start = None # the status before "starting": back to it when the start is cancelled or fails
97
104
  self.generation = 0
98
105
  self.stop = threading.Event()
99
106
  self.pending = [] # messages typed while the conversation was created
@@ -105,7 +112,7 @@ class Chat:
105
112
  self.interrupted = False # asked to pause, not paused yet
106
113
  self.starting = None # what starting the first message's conversation says now
107
114
  self.armed = {} # key: when it was last pressed (twice to leave, Esc Esc)
108
- self.spent, self.model, self.workspace = 0.0, None, None
115
+ self.model, self.workspace = None, None
109
116
  self.model_entry = self.model_effort = None
110
117
  self.environment = None # (kind, folder) picked with /environment for the next conversation
111
118
  self.agents_on = False # /agents before a conversation starts: on in the next one
@@ -163,6 +170,8 @@ class Chat:
163
170
  state = ("class:status.busy", f"{SPINNER[int(time.time() * 8) % len(SPINNER)]} {self.starting or 'starting'}")
164
171
  elif status == "RUNNING":
165
172
  state = ("class:status.busy", f"{SPINNER[int(time.time() * 8) % len(SPINNER)]} working")
173
+ elif status == LOST:
174
+ state = ("class:status.wait", "● not connected")
166
175
  elif status == "WAITING_FOR_CONFIRMATION" or self.ask is not None:
167
176
  state = ("class:status.wait", "● waits for you")
168
177
  elif status is None:
@@ -171,7 +180,6 @@ class Chat:
171
180
  state = ("class:status.state", "● " + word(status))
172
181
  # Learning the environment first: the line is cut at the terminal's width.
173
182
  parts = [progress_line(self.learning, short=True) if self.learning else None, self.model,
174
- formats.cost(self.spent) if self.spent else None,
175
183
  self.workspace or (self.environment[0] if self.environment and self.conversation is None else None),
176
184
  f"session {self.session}" if self.session else None]
177
185
  return [state] + [("class:status", " · " + part) for part in parts if part]
@@ -274,13 +282,15 @@ class Chat:
274
282
 
275
283
  @keys.add("c-c")
276
284
  def _(event):
285
+ # The second within TWICE leaves, also while the agent works (its session goes on).
286
+ if self._twice("c-c"):
287
+ event.app.exit()
288
+ return
277
289
  if not idle():
278
290
  self.interrupt()
279
- elif self._twice("c-c"):
280
- event.app.exit()
281
291
  else:
282
292
  buffer.reset()
283
- self.armed["leave"] = ("Ctrl+C", time.time())
293
+ self.armed["leave"] = ("Ctrl+C", time.time())
284
294
 
285
295
  @keys.add("c-d", filter=empty)
286
296
  def _(event):
@@ -377,8 +387,8 @@ class Chat:
377
387
  any other (that goes on)."""
378
388
  self.stop.set()
379
389
  self.stop, self.generation = threading.Event(), self.generation + 1
380
- self.conversation, self.ask, self.spent, self.lanes = conversation, None, 0.0, Lanes()
381
- self.learning, self.workspace = None, None
390
+ self.conversation, self.ask, self.lanes = conversation, None, Lanes()
391
+ self.learning, self.workspace, self.parked = None, None, False
382
392
  self.status = getattr(conversation, "status", None)
383
393
  self._reset()
384
394
  generation, stop = self.generation, self.stop
@@ -396,23 +406,33 @@ class Chat:
396
406
  if generation != self.generation:
397
407
  return
398
408
  self._later(self.show, record, generation)
409
+ except HostGone as error:
410
+ self._later(self._lost, generation, f"({error}: /resume or harness --continue goes on with "
411
+ "this conversation in a new one)")
399
412
  except Exception as error:
400
- self.note(f"(the conversation cannot be read any more: {error})", "red")
413
+ self._later(self._lost, generation, f"(the conversation cannot be read any more: {error})")
401
414
 
402
415
  threading.Thread(target=read, daemon=True).start()
403
416
 
417
+ def _lost(self, generation, text):
418
+ """The conversation shown can no longer be followed: say why, and stop showing it as working."""
419
+ if generation != self.generation:
420
+ return
421
+ self.note(text, "red")
422
+ if self.status not in ENDED:
423
+ self.status, self.interrupted, self.ask = LOST, False, None
424
+
404
425
  def show(self, record: dict, generation=None):
405
426
  if generation is not None and generation != self.generation:
406
427
  return
407
428
  self.lanes.add(record)
408
429
  kind = record.get("event")
430
+ if kind in ("workspace_reclaimed", "resumed"):
431
+ self.parked = kind == "workspace_reclaimed"
409
432
  if kind == "state":
410
433
  self.status, self.interrupted = record["status"], False
411
434
  if self.status in ("IDLE", "PAUSED", *ENDED):
412
435
  self.ask = None
413
- usage = record.get("usage") or {}
414
- self.spent += (usage.get("cost_usd") or 0) if kind == "model_reply" else (record.get("cost_usd") or 0) \
415
- if kind == "agent_report" else 0
416
436
  if kind in ("physical_answer", "confirmation"):
417
437
  self.ask = None
418
438
  # The timeline's items too, for the reasoning and the answer of finish() in a program.
@@ -461,10 +481,10 @@ class Chat:
461
481
  return [Text("› " + text, style="bold")]
462
482
  if kind == "finished": # finish(answer) in a program, or the reply that said it
463
483
  answer = str(record.get("text") or "").strip()
464
- return [] if not answer or answer == self.said else [Markdown(answer), Text("")]
484
+ return [] if not answer or answer == self.said else [Answer(answer), Text("")]
465
485
  if kind in ("message", "thought", "finish", "verify_ask", "invalid"):
466
486
  self.said = text.strip()
467
- return [Markdown(text), Text("")]
487
+ return [Answer(text), Text("")]
468
488
  if kind == "reasoning":
469
489
  return [Text("\n".join(formats.clip_lines(text, 12)), style="dim italic")]
470
490
  if kind == "program":
@@ -516,7 +536,7 @@ class Chat:
516
536
  stream = io.StringIO()
517
537
  console = Console(file=stream, force_terminal=False, color_system=None,
518
538
  width=self.console.width, highlight=False)
519
- console.print(*self._program_lines(program))
539
+ console.print(*self._program_lines(program), sep="\n") # each output line on its own
520
540
  return stream.getvalue()
521
541
 
522
542
  def _program_line(self, program) -> Text:
@@ -534,13 +554,14 @@ class Chat:
534
554
 
535
555
  def submit(self, text: str):
536
556
  line = text.strip()
537
- if not line:
538
- return
539
557
  if self.ask is not None and self.ask.get("reason"):
558
+ # The reason is optional: Enter on an empty one refuses without saying why.
540
559
  request_id = self.ask["request_id"]
541
560
  conversation, self.ask = self.conversation, None
542
561
  self._background(command, conversation, "reject", {"reason": line, "request_id": request_id})
543
562
  return
563
+ if not line:
564
+ return
544
565
  if line.startswith("!"):
545
566
  self._shell(line.lstrip("!").strip(), send=not line.startswith("!!"))
546
567
  return
@@ -580,9 +601,13 @@ class Chat:
580
601
  self.pending.append((text, files))
581
602
  elif self.conversation is not None and self.status not in ENDED:
582
603
  self._background(_message, self.conversation, text, files)
604
+ elif self.conversation is not None and self.parked:
605
+ # The host parked it for its workspace: the message continues it.
606
+ self._background(self._continue, self.conversation.run, text, files)
583
607
  elif self.start is None:
584
608
  self.note("The conversation has ended", "red")
585
609
  else:
610
+ self.before_start = self.status
586
611
  self.status, self.echoed = "starting", text
587
612
  self._print(Text("› " + text, style="bold"))
588
613
 
@@ -616,7 +641,9 @@ class Chat:
616
641
  self._later(self._cancel_start, text)
617
642
  return
618
643
  except Exception as error:
619
- self.status = None
644
+ # As before the message: an ended conversation stays ended, and the next
645
+ # message starts a new one again.
646
+ self.status = self.before_start
620
647
  self.note(f"(could not start: {error})", "red")
621
648
  return
622
649
  self._later(self._started, conversation)
@@ -720,9 +747,10 @@ class Chat:
720
747
  self.note("Sub-agents are on for the next conversation (session.agents in /settings keeps them on)")
721
748
 
722
749
  def _cancel_start(self, text):
723
- self.status = self.starting = None
750
+ self.status, self.starting = self.before_start, None
724
751
  self.pending = []
725
752
  self.buffer.text = text
753
+ self.buffer.cursor_position = len(text) # typing goes on from its end, as before it was sent
726
754
  self.note("Task not started. Your message is kept in the input box.")
727
755
 
728
756
  def _starting(self, message):
@@ -835,11 +863,21 @@ class Chat:
835
863
  self._print(Text("── new conversation " + "─" * 40, style="dim"))
836
864
  self._open_environment()
837
865
 
866
+ do_clear = do_new
867
+
868
+ def _continue(self, session, text=None, files=()):
869
+ """Go on with the ended session `session` in its own id (the host starts it again
870
+ from what it kept), then send it `text`."""
871
+ conversation = self._host().resume_session(session)
872
+ if text is not None:
873
+ _message(conversation, text, files)
874
+ self._later(self._follow, conversation)
875
+
838
876
  def _leave(self):
839
877
  """No conversation shown (the one that was goes on)."""
840
878
  self.stop.set()
841
879
  self.generation += 1
842
- self.conversation, self.status, self.ask, self.spent, self.lanes = None, None, None, 0.0, Lanes()
880
+ self.conversation, self.status, self.ask, self.lanes = None, None, None, Lanes()
843
881
  self.learning = None
844
882
  self._reset()
845
883
  try:
@@ -999,12 +1037,16 @@ class Chat:
999
1037
  if row.get("status") in LIVE:
1000
1038
  self._follow(host.conversation(row["run"]))
1001
1039
  return
1040
+ if row.get("kind", "harness") == "harness":
1041
+ # It goes on in its own session: its messages and facts, a new kernel.
1042
+ self._background(self._continue, row["id"])
1043
+ return
1002
1044
  from ..core.episodes import Follow
1003
1045
 
1004
1046
  self._leave()
1005
1047
  for record in Follow(Path(row["directory"])).next() if row.get("directory") else []:
1006
1048
  self.show(record)
1007
- self.status, self.spent = None, 0.0
1049
+ self.status = None
1008
1050
  self.note("This conversation has ended; a message starts a new one")
1009
1051
 
1010
1052
  self.app.create_background_task(self._guarded(pick))
@@ -1038,14 +1080,21 @@ class Chat:
1038
1080
  from ..model_list import models
1039
1081
 
1040
1082
  path = data() / "models.json"
1041
- models(data())
1083
+ try:
1084
+ models(data()) # writes the list when there is none yet
1085
+ except ValueError:
1086
+ pass # one saved with a mistake opens as it is, to be put right
1042
1087
  editor = os.environ.get("EDITOR") or ("notepad.exe" if os.name == "nt" else "vi")
1043
1088
  command = [editor] if os.path.isfile(editor) else split(editor, posix=os.name != "nt")
1044
1089
  async with in_terminal():
1045
1090
  result = await asyncio.to_thread(subprocess.run, [*command, str(path)], check=False)
1046
1091
  if result.returncode:
1047
1092
  raise ValueError(f"the editor exited with code {result.returncode}")
1048
- count = sum(row["enabled"] for row in models(data()))
1093
+ try:
1094
+ rows = models(data())
1095
+ except ValueError as error:
1096
+ raise ValueError(f"{error}; /models opens it again to correct it") from None
1097
+ count = sum(row["enabled"] for row in rows)
1049
1098
  self.note(f"{count} enabled models · /model to select · {path}")
1050
1099
 
1051
1100
  self.app.create_background_task(self._guarded(edit))
@@ -1095,7 +1144,7 @@ class Completions(Completer):
1095
1144
  display_meta=found.note)
1096
1145
  return
1097
1146
  if text.startswith("/approve ") and text.count(" ") == 1:
1098
- for remember in REMEMBER[1:]:
1147
+ for remember in APPROVE_WORDS:
1099
1148
  if remember.startswith(text.split(" ")[1]):
1100
1149
  yield Completion(remember, -len(text.split(" ")[1]))
1101
1150
  return
@@ -98,6 +98,7 @@ async def ask(title: str, default: str = "", hint: str = "Enter keep · Esc canc
98
98
  """One line typed full screen (a setting's value): the text, None when cancelled."""
99
99
  field = Buffer(multiline=False)
100
100
  field.text = default
101
+ field.cursor_position = len(default) # typing goes on from the end of what is there
101
102
  keys = KeyBindings()
102
103
 
103
104
  @keys.add("enter")
@@ -1,86 +0,0 @@
1
- Entries: parts of your work that run at the same time as your programs.
2
-
3
- <!-- if: channels -->
4
- spawn(fn, name=None, claims=None) run fn() beside your program as an entry on the channels
5
- it claims (CHANNELS: e.g. ["move"], ["look", "hands"]);
6
- returns its handle: .wait(timeout=None) returns what fn
7
- returned (raises its failure; None if cancelled or still
8
- running at the timeout), .cancel() stops it, .send(message)
9
- gives it a message, .state(), .status (running, done,
10
- failed, cancelled)
11
- claim(*channels), unclaim(*channels) inside an entry: take channels for it, or let them go
12
- channels() who has each channel now
13
- <!-- else -->
14
- spawn(fn, name=None) run fn() beside your program as an entry; returns its
15
- handle: .wait(timeout=None) returns what fn returned (raises
16
- its failure; None if cancelled or still running at the
17
- timeout), .cancel() stops it, .send(message) gives it a
18
- message, .state(), .status (running, done, failed,
19
- cancelled)
20
- <!-- end -->
21
- receive(timeout=None) inside an entry's fn: the oldest message sent to it, or None
22
- entries(running=True) the handles of the entries started so far
23
-
24
- An entry you spawn keeps running after the program that started it ends; after each
25
- program you read how your entries stand ("Entries: ..."). Each of your replies changes
26
- <!-- if: channels -->
27
- the set: spawn adds an entry, cancel() stops one, cancel() and spawn replace one.
28
- Entries run at the same time, each on its own channels of the game's keys, mouse and
29
- wheel (CHANNELS names them and what is on each). An entry claims the channels it uses
30
- (spawn(..., claims=[...]) or claim()) and no other entry may use them until it lets them
31
- go or ends; its inputs drive only those channels, so one entry walks while another turns
32
- the view or swings. key_down(*keys, buttons=()) keeps keys and buttons down across
33
- periods while that entry's code goes on, until key_up() or the entry's end; when an
34
- entry ends, everything it held is let go. An entry that reaches for a channel another
35
- entry has fails with a CHANNEL_CONFLICT event, which reaches you unless the program that
36
- started it handles it. Use an entry for a part that must go on beside the rest: holding a
37
- key down while other code steers, watching the state and reacting at once, a background
38
- reading or computation.
39
- An entry that fails reaches you as an ENTRY_FAILED event unless something waits for it.
40
-
41
- For example, to keep moving forward while a lookout entry watches what the game reports
42
- and another program goes on with the view:
43
-
44
- <python>
45
- def walk():
46
- key_down("w")
47
- while True:
48
- wait(1)
49
- walker = spawn(walk, name="walk", claims=["move"])
50
- def lookout():
51
- seen = get_state().get("hud")
52
- while get_state().get("hud") == seen:
53
- wait(0.2)
54
- return "the HUD changed"
55
- watch = spawn(lookout, name="hud lookout")
56
- </python>
57
- <!-- else -->
58
- the set: spawn adds an entry, cancel() stops one, cancel() and spawn replace one. Use an
59
- entry for work that goes on beside your next steps in this workspace: watching for a
60
- change (a download or an upload finishing, a page or a window updating) while you act,
61
- waiting for something with a time limit, reading or computing in the background. Input
62
- steps of different entries take turns on this screen, in the order the entries started,
63
- so give an entry reading and waiting, and keep the clicking and typing in one place.
64
- An entry that fails reaches you as an ENTRY_FAILED event unless something waits for it.
65
- <!-- end -->
66
- <!-- if: agents -->
67
- Work that needs its own screen (another site, another document) goes to a sub-agent
68
- instead: each delegate() is an entry too, with a workspace of its own, so several of them
69
- work truly at the same time.
70
- <!-- end -->
71
- <!-- if: channels -->
72
- <!-- else -->
73
- <!-- if: desktop -->
74
-
75
- For example, to hear when the window's title changes (a page or a dialog has loaded)
76
- while your next programs go on:
77
-
78
- <python>
79
- def new_title(start=get_state().get("active_window")):
80
- while get_state().get("active_window") == start:
81
- wait(1)
82
- return get_state().get("active_window")
83
- title_watch = spawn(new_title, name="title watcher")
84
- </python>
85
- <!-- end -->
86
- <!-- end -->